diff options
| -rw-r--r-- | lundump.c | 121 | ||||
| -rw-r--r-- | lundump.h | 12 |
2 files changed, 73 insertions, 60 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.c,v 1.39 2001/02/23 17:17:25 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 1.35 2001/06/28 13:55:17 lhf Exp lhf $ |
| 3 | ** load bytecodes from files | 3 | ** load pre-compiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| @@ -10,6 +10,7 @@ | |||
| 10 | #define LUA_PRIVATE | 10 | #define LUA_PRIVATE |
| 11 | #include "lua.h" | 11 | #include "lua.h" |
| 12 | 12 | ||
| 13 | #include "ldebug.h" | ||
| 13 | #include "lfunc.h" | 14 | #include "lfunc.h" |
| 14 | #include "lmem.h" | 15 | #include "lmem.h" |
| 15 | #include "lopcodes.h" | 16 | #include "lopcodes.h" |
| @@ -46,7 +47,7 @@ static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap) | |||
| 46 | { | 47 | { |
| 47 | if (swap) | 48 | if (swap) |
| 48 | { | 49 | { |
| 49 | l_char *p=(l_char *) b+size-1; | 50 | l_char *p=(l_char*) b+size-1; |
| 50 | int n=size; | 51 | int n=size; |
| 51 | while (n--) *p--=(l_char)ezgetc(L,Z); | 52 | while (n--) *p--=(l_char)ezgetc(L,Z); |
| 52 | } | 53 | } |
| @@ -58,7 +59,7 @@ static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int s | |||
| 58 | { | 59 | { |
| 59 | if (swap) | 60 | if (swap) |
| 60 | { | 61 | { |
| 61 | l_char *q=(l_char *) b; | 62 | l_char *q=(l_char*) b; |
| 62 | while (m--) | 63 | while (m--) |
| 63 | { | 64 | { |
| 64 | l_char *p=q+size-1; | 65 | l_char *p=q+size-1; |
| @@ -99,77 +100,92 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap) | |||
| 99 | return NULL; | 100 | return NULL; |
| 100 | else | 101 | else |
| 101 | { | 102 | { |
| 102 | char* s=luaO_openspace(L,size,char); | 103 | l_char* s=luaO_openspace(L,size,l_char); |
| 103 | LoadBlock(L,s,size,Z,0); | 104 | LoadBlock(L,s,size,Z,0); |
| 104 | return luaS_newlstr(L,s,size-1); /* remove trailing l_c('\0') */ | 105 | return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */ |
| 105 | } | 106 | } |
| 106 | } | 107 | } |
| 107 | 108 | ||
| 108 | static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap) | 109 | static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap) |
| 109 | { | 110 | { |
| 110 | int size=LoadInt(L,Z,swap); | 111 | int size=LoadInt(L,Z,swap); |
| 111 | tf->code=luaM_newvector(L,size,Instruction); | 112 | f->code=luaM_newvector(L,size,Instruction); |
| 112 | tf->sizecode=size; | 113 | f->sizecode=size; |
| 113 | LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap); | 114 | LoadVector(L,f->code,size,sizeof(*f->code),Z,swap); |
| 114 | } | 115 | } |
| 115 | 116 | ||
| 116 | static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap) | 117 | static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap) |
| 117 | { | 118 | { |
| 118 | int i,n; | 119 | int i,n; |
| 119 | n=LoadInt(L,Z,swap); | 120 | n=LoadInt(L,Z,swap); |
| 120 | tf->locvars=luaM_newvector(L,n,LocVar); | 121 | f->locvars=luaM_newvector(L,n,LocVar); |
| 121 | tf->sizelocvars=n; | 122 | f->sizelocvars=n; |
| 122 | for (i=0; i<n; i++) | 123 | for (i=0; i<n; i++) |
| 123 | { | 124 | { |
| 124 | tf->locvars[i].varname=LoadString(L,Z,swap); | 125 | f->locvars[i].varname=LoadString(L,Z,swap); |
| 125 | tf->locvars[i].startpc=LoadInt(L,Z,swap); | 126 | f->locvars[i].startpc=LoadInt(L,Z,swap); |
| 126 | tf->locvars[i].endpc=LoadInt(L,Z,swap); | 127 | f->locvars[i].endpc=LoadInt(L,Z,swap); |
| 127 | } | 128 | } |
| 128 | } | 129 | } |
| 129 | 130 | ||
| 130 | static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap) | 131 | static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap) |
| 131 | { | 132 | { |
| 132 | int n; | 133 | int n; |
| 133 | n=LoadInt(L,Z,swap); | 134 | n=LoadInt(L,Z,swap); |
| 134 | tf->lineinfo=luaM_newvector(L,n,int); | 135 | f->lineinfo=luaM_newvector(L,n,int); |
| 135 | tf->sizelineinfo=n; | 136 | f->sizelineinfo=n; |
| 136 | LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap); | 137 | LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap); |
| 137 | } | 138 | } |
| 138 | 139 | ||
| 139 | static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap); | 140 | static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap); |
| 140 | 141 | ||
| 141 | static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap) | 142 | static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap) |
| 142 | { | 143 | { |
| 143 | int i,n; | 144 | int i,n; |
| 144 | n=LoadInt(L,Z,swap); | 145 | n=LoadInt(L,Z,swap); |
| 145 | tf->kstr=luaM_newvector(L,n,TString*); | 146 | f->k=luaM_newvector(L,n,TObject); |
| 146 | tf->sizekstr=n; | 147 | f->sizek=n; |
| 147 | for (i=0; i<n; i++) | 148 | for (i=0; i<n; i++) |
| 148 | tf->kstr[i]=LoadString(L,Z,swap); | 149 | { |
| 149 | n=LoadInt(L,Z,swap); | 150 | TObject* o=&f->k[i]; |
| 150 | tf->knum=luaM_newvector(L,n,lua_Number); | 151 | ttype(o)=LoadByte(L,Z); |
| 151 | tf->sizeknum=n; | 152 | switch (ttype(o)) |
| 152 | LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap); | 153 | { |
| 154 | case LUA_TNUMBER: | ||
| 155 | nvalue(o)=LoadNumber(L,Z,swap); | ||
| 156 | break; | ||
| 157 | case LUA_TSTRING: | ||
| 158 | tsvalue(o)=LoadString(L,Z,swap); | ||
| 159 | break; | ||
| 160 | default: | ||
| 161 | luaO_verror(L,l_s("bad constant type (%d) in `%.99s'"),ttype(o),ZNAME(Z)); | ||
| 162 | break; | ||
| 163 | } | ||
| 164 | } | ||
| 153 | n=LoadInt(L,Z,swap); | 165 | n=LoadInt(L,Z,swap); |
| 154 | tf->kproto=luaM_newvector(L,n,Proto*); | 166 | f->p=luaM_newvector(L,n,Proto*); |
| 155 | tf->sizekproto=n; | 167 | f->sizep=n; |
| 156 | for (i=0; i<n; i++) | 168 | for (i=0; i<n; i++) |
| 157 | tf->kproto[i]=LoadFunction(L,Z,swap); | 169 | f->p[i]=LoadFunction(L,Z,swap); |
| 158 | } | 170 | } |
| 159 | 171 | ||
| 160 | static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap) | 172 | static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap) |
| 161 | { | 173 | { |
| 162 | Proto* tf=luaF_newproto(L); | 174 | Proto* f=luaF_newproto(L); |
| 163 | tf->source=LoadString(L,Z,swap); | 175 | f->source=LoadString(L,Z,swap); |
| 164 | tf->lineDefined=LoadInt(L,Z,swap); | 176 | f->lineDefined=LoadInt(L,Z,swap); |
| 165 | tf->numparams=LoadInt(L,Z,swap); | 177 | f->nupvalues=LoadInt(L,Z,swap); |
| 166 | tf->is_vararg=LoadByte(L,Z); | 178 | f->numparams=LoadInt(L,Z,swap); |
| 167 | tf->maxstacksize=LoadInt(L,Z,swap); | 179 | f->is_vararg=LoadByte(L,Z); |
| 168 | LoadLocals(L,tf,Z,swap); | 180 | f->maxstacksize=LoadInt(L,Z,swap); |
| 169 | LoadLines(L,tf,Z,swap); | 181 | LoadLocals(L,f,Z,swap); |
| 170 | LoadConstants(L,tf,Z,swap); | 182 | LoadLines(L,f,Z,swap); |
| 171 | LoadCode(L,tf,Z,swap); | 183 | LoadConstants(L,f,Z,swap); |
| 172 | return tf; | 184 | LoadCode(L,f,Z,swap); |
| 185 | #ifndef TRUST_BINARIES | ||
| 186 | if (luaG_checkcode(f)==0) luaO_verror(L,l_s("bad code in `%.99s'"),ZNAME(Z)); | ||
| 187 | #endif | ||
| 188 | return f; | ||
| 173 | } | 189 | } |
| 174 | 190 | ||
| 175 | static void LoadSignature (lua_State* L, ZIO* Z) | 191 | static void LoadSignature (lua_State* L, ZIO* Z) |
| @@ -194,7 +210,7 @@ static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z) | |||
| 194 | static int LoadHeader (lua_State* L, ZIO* Z) | 210 | static int LoadHeader (lua_State* L, ZIO* Z) |
| 195 | { | 211 | { |
| 196 | int version,swap; | 212 | int version,swap; |
| 197 | lua_Number f=0,tf=TEST_NUMBER; | 213 | lua_Number x=0,tx=TEST_NUMBER; |
| 198 | LoadSignature(L,Z); | 214 | LoadSignature(L,Z); |
| 199 | version=ezgetc(L,Z); | 215 | version=ezgetc(L,Z); |
| 200 | if (version>VERSION) | 216 | if (version>VERSION) |
| @@ -205,19 +221,20 @@ static int LoadHeader (lua_State* L, ZIO* Z) | |||
| 205 | luaO_verror(L,l_s("`%.99s' too old:\n") | 221 | luaO_verror(L,l_s("`%.99s' too old:\n") |
| 206 | l_s(" read version %d.%d; expected at least %d.%d"), | 222 | l_s(" read version %d.%d; expected at least %d.%d"), |
| 207 | ZNAME(Z),V(version),V(VERSION)); | 223 | ZNAME(Z),V(version),V(VERSION)); |
| 208 | swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */ | 224 | swap=(luaU_endianness()!=ezgetc(L,Z)); /* need to swap bytes? */ |
| 209 | TESTSIZE(sizeof(int)); | 225 | TESTSIZE(sizeof(int)); |
| 210 | TESTSIZE(sizeof(size_t)); | 226 | TESTSIZE(sizeof(size_t)); |
| 211 | TESTSIZE(sizeof(Instruction)); | 227 | TESTSIZE(sizeof(Instruction)); |
| 212 | TESTSIZE(SIZE_INSTRUCTION); | ||
| 213 | TESTSIZE(SIZE_OP); | 228 | TESTSIZE(SIZE_OP); |
| 229 | TESTSIZE(SIZE_A); | ||
| 214 | TESTSIZE(SIZE_B); | 230 | TESTSIZE(SIZE_B); |
| 231 | TESTSIZE(SIZE_C); | ||
| 215 | TESTSIZE(sizeof(lua_Number)); | 232 | TESTSIZE(sizeof(lua_Number)); |
| 216 | f=LoadNumber(L,Z,swap); | 233 | x=LoadNumber(L,Z,swap); |
| 217 | if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */ | 234 | if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ |
| 218 | luaO_verror(L,l_s("unknown number format in `%.99s':\n") | 235 | luaO_verror(L,l_s("unknown number format in `%.99s':\n") |
| 219 | l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT), | 236 | l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT), |
| 220 | ZNAME(Z),f,tf); | 237 | ZNAME(Z),x,tx); |
| 221 | return swap; | 238 | return swap; |
| 222 | } | 239 | } |
| 223 | 240 | ||
| @@ -232,20 +249,20 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z) | |||
| 232 | */ | 249 | */ |
| 233 | Proto* luaU_undump (lua_State* L, ZIO* Z) | 250 | Proto* luaU_undump (lua_State* L, ZIO* Z) |
| 234 | { | 251 | { |
| 235 | Proto* tf=NULL; | 252 | Proto* f=NULL; |
| 236 | int c=zgetc(Z); | 253 | int c=zgetc(Z); |
| 237 | if (c==ID_CHUNK) | 254 | if (c==ID_CHUNK) |
| 238 | tf=LoadChunk(L,Z); | 255 | f=LoadChunk(L,Z); |
| 239 | c=zgetc(Z); | 256 | c=zgetc(Z); |
| 240 | if (c!=EOZ) | 257 | if (c!=EOZ) |
| 241 | luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z)); | 258 | luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z)); |
| 242 | return tf; | 259 | return f; |
| 243 | } | 260 | } |
| 244 | 261 | ||
| 245 | /* | 262 | /* |
| 246 | ** find byte order | 263 | ** find byte order |
| 247 | */ | 264 | */ |
| 248 | int luaU_endianess (void) | 265 | int luaU_endianness (void) |
| 249 | { | 266 | { |
| 250 | int x=1; | 267 | int x=1; |
| 251 | return *(l_char*)&x; | 268 | return *(l_char*)&x; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.h,v 1.20 2001/02/23 17:17:25 roberto Exp roberto $ | 2 | ** $Id: lundump.h,v 1.23 2001/06/28 13:55:17 lhf Exp $ |
| 3 | ** load pre-compiled Lua chunks | 3 | ** load pre-compiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -14,18 +14,14 @@ | |||
| 14 | Proto* luaU_undump (lua_State* L, ZIO* Z); | 14 | Proto* luaU_undump (lua_State* L, ZIO* Z); |
| 15 | 15 | ||
| 16 | /* find byte order */ | 16 | /* find byte order */ |
| 17 | int luaU_endianess (void); | 17 | int luaU_endianness (void); |
| 18 | 18 | ||
| 19 | /* definitions for headers of binary files */ | 19 | /* definitions for headers of binary files */ |
| 20 | #define VERSION 0x40 /* last format change was in 4.0 */ | 20 | #define VERSION 0x41 /* last format change was in 4.1 */ |
| 21 | #define VERSION0 0x40 /* last major change was in 4.0 */ | 21 | #define VERSION0 0x41 /* last major change was in 4.1 */ |
| 22 | #define ID_CHUNK 27 /* binary files start with ESC... */ | 22 | #define ID_CHUNK 27 /* binary files start with ESC... */ |
| 23 | #define SIGNATURE "Lua" /* ...followed by this signature */ | 23 | #define SIGNATURE "Lua" /* ...followed by this signature */ |
| 24 | 24 | ||
| 25 | /* formats for error messages */ | ||
| 26 | #define SOURCE tf->lineDefined,tf->source->str | ||
| 27 | #define IN tf,SOURCE | ||
| 28 | |||
| 29 | /* a multiple of PI for testing native format */ | 25 | /* a multiple of PI for testing native format */ |
| 30 | /* multiplying by 1E8 gives non-trivial integer values */ | 26 | /* multiplying by 1E8 gives non-trivial integer values */ |
| 31 | #define TEST_NUMBER 3.14159265358979323846E8 | 27 | #define TEST_NUMBER 3.14159265358979323846E8 |
