[1803] | 1 | /* |
---|
| 2 | ** $Id: lundump.c,v 1.49 2003/04/07 20:34:20 lhf Exp $ |
---|
| 3 | ** load pre-compiled Lua chunks |
---|
| 4 | ** See Copyright Notice in lua.h |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #define lundump_c |
---|
| 8 | |
---|
| 9 | #include "lua.h" |
---|
| 10 | |
---|
| 11 | #include "ldebug.h" |
---|
| 12 | #include "lfunc.h" |
---|
| 13 | #include "lmem.h" |
---|
| 14 | #include "lopcodes.h" |
---|
| 15 | #include "lstring.h" |
---|
| 16 | #include "lundump.h" |
---|
| 17 | #include "lzio.h" |
---|
| 18 | |
---|
| 19 | #define LoadByte (lu_byte) ezgetc |
---|
| 20 | |
---|
| 21 | typedef struct { |
---|
| 22 | lua_State* L; |
---|
| 23 | ZIO* Z; |
---|
| 24 | Mbuffer* b; |
---|
| 25 | int swap; |
---|
| 26 | const char* name; |
---|
| 27 | } LoadState; |
---|
| 28 | |
---|
| 29 | static void unexpectedEOZ (LoadState* S) |
---|
| 30 | { |
---|
| 31 | luaG_runerror(S->L,"unexpected end of file in %s",S->name); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | static int ezgetc (LoadState* S) |
---|
| 35 | { |
---|
| 36 | int c=zgetc(S->Z); |
---|
| 37 | if (c==EOZ) unexpectedEOZ(S); |
---|
| 38 | return c; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | static void ezread (LoadState* S, void* b, int n) |
---|
| 42 | { |
---|
| 43 | int r=luaZ_read(S->Z,b,n); |
---|
| 44 | if (r!=0) unexpectedEOZ(S); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | static void LoadBlock (LoadState* S, void* b, size_t size) |
---|
| 48 | { |
---|
| 49 | if (S->swap) |
---|
| 50 | { |
---|
| 51 | char* p=(char*) b+size-1; |
---|
| 52 | int n=size; |
---|
| 53 | while (n--) *p--=(char)ezgetc(S); |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | ezread(S,b,size); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static void LoadVector (LoadState* S, void* b, int m, size_t size) |
---|
| 60 | { |
---|
| 61 | if (S->swap) |
---|
| 62 | { |
---|
| 63 | char* q=(char*) b; |
---|
| 64 | while (m--) |
---|
| 65 | { |
---|
| 66 | char* p=q+size-1; |
---|
| 67 | int n=size; |
---|
| 68 | while (n--) *p--=(char)ezgetc(S); |
---|
| 69 | q+=size; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | ezread(S,b,m*size); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static int LoadInt (LoadState* S) |
---|
| 77 | { |
---|
| 78 | int x; |
---|
| 79 | LoadBlock(S,&x,sizeof(x)); |
---|
| 80 | if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name); |
---|
| 81 | return x; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static size_t LoadSize (LoadState* S) |
---|
| 85 | { |
---|
| 86 | size_t x; |
---|
| 87 | LoadBlock(S,&x,sizeof(x)); |
---|
| 88 | return x; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | static lua_Number LoadNumber (LoadState* S) |
---|
| 92 | { |
---|
| 93 | lua_Number x; |
---|
| 94 | LoadBlock(S,&x,sizeof(x)); |
---|
| 95 | return x; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static TString* LoadString (LoadState* S) |
---|
| 99 | { |
---|
| 100 | size_t size=LoadSize(S); |
---|
| 101 | if (size==0) |
---|
| 102 | return NULL; |
---|
| 103 | else |
---|
| 104 | { |
---|
| 105 | char* s=luaZ_openspace(S->L,S->b,size); |
---|
| 106 | ezread(S,s,size); |
---|
| 107 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | static void LoadCode (LoadState* S, Proto* f) |
---|
| 112 | { |
---|
| 113 | int size=LoadInt(S); |
---|
| 114 | f->code=luaM_newvector(S->L,size,Instruction); |
---|
| 115 | f->sizecode=size; |
---|
| 116 | LoadVector(S,f->code,size,sizeof(*f->code)); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | static void LoadLocals (LoadState* S, Proto* f) |
---|
| 120 | { |
---|
| 121 | int i,n; |
---|
| 122 | n=LoadInt(S); |
---|
| 123 | f->locvars=luaM_newvector(S->L,n,LocVar); |
---|
| 124 | f->sizelocvars=n; |
---|
| 125 | for (i=0; i<n; i++) |
---|
| 126 | { |
---|
| 127 | f->locvars[i].varname=LoadString(S); |
---|
| 128 | f->locvars[i].startpc=LoadInt(S); |
---|
| 129 | f->locvars[i].endpc=LoadInt(S); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | static void LoadLines (LoadState* S, Proto* f) |
---|
| 134 | { |
---|
| 135 | int size=LoadInt(S); |
---|
| 136 | f->lineinfo=luaM_newvector(S->L,size,int); |
---|
| 137 | f->sizelineinfo=size; |
---|
| 138 | LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo)); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static void LoadUpvalues (LoadState* S, Proto* f) |
---|
| 142 | { |
---|
| 143 | int i,n; |
---|
| 144 | n=LoadInt(S); |
---|
| 145 | if (n!=0 && n!=f->nups) |
---|
| 146 | luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d", |
---|
| 147 | S->name,n,f->nups); |
---|
| 148 | f->upvalues=luaM_newvector(S->L,n,TString*); |
---|
| 149 | f->sizeupvalues=n; |
---|
| 150 | for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | static Proto* LoadFunction (LoadState* S, TString* p); |
---|
| 154 | |
---|
| 155 | static void LoadConstants (LoadState* S, Proto* f) |
---|
| 156 | { |
---|
| 157 | int i,n; |
---|
| 158 | n=LoadInt(S); |
---|
| 159 | f->k=luaM_newvector(S->L,n,TObject); |
---|
| 160 | f->sizek=n; |
---|
| 161 | for (i=0; i<n; i++) |
---|
| 162 | { |
---|
| 163 | TObject* o=&f->k[i]; |
---|
| 164 | int t=LoadByte(S); |
---|
| 165 | switch (t) |
---|
| 166 | { |
---|
| 167 | case LUA_TNUMBER: |
---|
| 168 | setnvalue(o,LoadNumber(S)); |
---|
| 169 | break; |
---|
| 170 | case LUA_TSTRING: |
---|
| 171 | setsvalue2n(o,LoadString(S)); |
---|
| 172 | break; |
---|
| 173 | case LUA_TNIL: |
---|
| 174 | setnilvalue(o); |
---|
| 175 | break; |
---|
| 176 | default: |
---|
| 177 | luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name); |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | n=LoadInt(S); |
---|
| 182 | f->p=luaM_newvector(S->L,n,Proto*); |
---|
| 183 | f->sizep=n; |
---|
| 184 | for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | static Proto* LoadFunction (LoadState* S, TString* p) |
---|
| 188 | { |
---|
| 189 | Proto* f=luaF_newproto(S->L); |
---|
| 190 | f->source=LoadString(S); if (f->source==NULL) f->source=p; |
---|
| 191 | f->lineDefined=LoadInt(S); |
---|
| 192 | f->nups=LoadByte(S); |
---|
| 193 | f->numparams=LoadByte(S); |
---|
| 194 | f->is_vararg=LoadByte(S); |
---|
| 195 | f->maxstacksize=LoadByte(S); |
---|
| 196 | LoadLines(S,f); |
---|
| 197 | LoadLocals(S,f); |
---|
| 198 | LoadUpvalues(S,f); |
---|
| 199 | LoadConstants(S,f); |
---|
| 200 | LoadCode(S,f); |
---|
| 201 | #ifndef TRUST_BINARIES |
---|
| 202 | if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name); |
---|
| 203 | #endif |
---|
| 204 | return f; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | static void LoadSignature (LoadState* S) |
---|
| 208 | { |
---|
| 209 | const char* s=LUA_SIGNATURE; |
---|
| 210 | while (*s!=0 && ezgetc(S)==*s) |
---|
| 211 | ++s; |
---|
| 212 | if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | static void TestSize (LoadState* S, int s, const char* what) |
---|
| 216 | { |
---|
| 217 | int r=LoadByte(S); |
---|
| 218 | if (r!=s) |
---|
| 219 | luaG_runerror(S->L,"virtual machine mismatch in %s: " |
---|
| 220 | "size of %s is %d but read %d",S->name,what,s,r); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | #define TESTSIZE(s,w) TestSize(S,s,w) |
---|
| 224 | #define V(v) v/16,v%16 |
---|
| 225 | |
---|
| 226 | static void LoadHeader (LoadState* S) |
---|
| 227 | { |
---|
| 228 | int version; |
---|
| 229 | lua_Number x,tx=TEST_NUMBER; |
---|
| 230 | LoadSignature(S); |
---|
| 231 | version=LoadByte(S); |
---|
| 232 | if (version>VERSION) |
---|
| 233 | luaG_runerror(S->L,"%s too new: " |
---|
| 234 | "read version %d.%d; expected at most %d.%d", |
---|
| 235 | S->name,V(version),V(VERSION)); |
---|
| 236 | if (version<VERSION0) /* check last major change */ |
---|
| 237 | luaG_runerror(S->L,"%s too old: " |
---|
| 238 | "read version %d.%d; expected at least %d.%d", |
---|
| 239 | S->name,V(version),V(VERSION0)); |
---|
| 240 | S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */ |
---|
| 241 | TESTSIZE(sizeof(int),"int"); |
---|
| 242 | TESTSIZE(sizeof(size_t), "size_t"); |
---|
| 243 | TESTSIZE(sizeof(Instruction), "Instruction"); |
---|
| 244 | TESTSIZE(SIZE_OP, "OP"); |
---|
| 245 | TESTSIZE(SIZE_A, "A"); |
---|
| 246 | TESTSIZE(SIZE_B, "B"); |
---|
| 247 | TESTSIZE(SIZE_C, "C"); |
---|
| 248 | TESTSIZE(sizeof(lua_Number), "number"); |
---|
| 249 | x=LoadNumber(S); |
---|
| 250 | if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ |
---|
| 251 | luaG_runerror(S->L,"unknown number format in %s",S->name); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | static Proto* LoadChunk (LoadState* S) |
---|
| 255 | { |
---|
| 256 | LoadHeader(S); |
---|
| 257 | return LoadFunction(S,NULL); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /* |
---|
| 261 | ** load precompiled chunk |
---|
| 262 | */ |
---|
| 263 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff) |
---|
| 264 | { |
---|
| 265 | LoadState S; |
---|
| 266 | const char* s=zname(Z); |
---|
| 267 | if (*s=='@' || *s=='=') |
---|
| 268 | S.name=s+1; |
---|
| 269 | else if (*s==LUA_SIGNATURE[0]) |
---|
| 270 | S.name="binary string"; |
---|
| 271 | else |
---|
| 272 | S.name=s; |
---|
| 273 | S.L=L; |
---|
| 274 | S.Z=Z; |
---|
| 275 | S.b=buff; |
---|
| 276 | return LoadChunk(&S); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | /* |
---|
| 280 | ** find byte order |
---|
| 281 | */ |
---|
| 282 | int luaU_endianness (void) |
---|
| 283 | { |
---|
| 284 | int x=1; |
---|
| 285 | return *(char*)&x; |
---|
| 286 | } |
---|