| 1 | /* | 
|---|
| 2 | ** $Id: lundump.c,v 2.7.1.2 2008/01/18 16:39:11 roberto Exp $ | 
|---|
| 3 | ** load precompiled Lua chunks | 
|---|
| 4 | ** See Copyright Notice in lua.h | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #include <string.h> | 
|---|
| 8 |  | 
|---|
| 9 | #define lundump_c | 
|---|
| 10 | #define LUA_CORE | 
|---|
| 11 |  | 
|---|
| 12 | #include "lua.h" | 
|---|
| 13 |  | 
|---|
| 14 | #include "ldebug.h" | 
|---|
| 15 | #include "ldo.h" | 
|---|
| 16 | #include "lfunc.h" | 
|---|
| 17 | #include "lmem.h" | 
|---|
| 18 | #include "lobject.h" | 
|---|
| 19 | #include "lstring.h" | 
|---|
| 20 | #include "lundump.h" | 
|---|
| 21 | #include "lzio.h" | 
|---|
| 22 |  | 
|---|
| 23 | typedef struct { | 
|---|
| 24 | lua_State* L; | 
|---|
| 25 | ZIO* Z; | 
|---|
| 26 | Mbuffer* b; | 
|---|
| 27 | const char* name; | 
|---|
| 28 | } LoadState; | 
|---|
| 29 |  | 
|---|
| 30 | #ifdef LUAC_TRUST_BINARIES | 
|---|
| 31 | #define IF(c,s) | 
|---|
| 32 | #define error(S,s) | 
|---|
| 33 | #else | 
|---|
| 34 | #define IF(c,s)         if (c) error(S,s) | 
|---|
| 35 |  | 
|---|
| 36 | static void error(LoadState* S, const char* why) | 
|---|
| 37 | { | 
|---|
| 38 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); | 
|---|
| 39 | luaD_throw(S->L,LUA_ERRSYNTAX); | 
|---|
| 40 | } | 
|---|
| 41 | #endif | 
|---|
| 42 |  | 
|---|
| 43 | #define LoadMem(S,b,n,size)     LoadBlock(S,b,(n)*(size)) | 
|---|
| 44 | #define LoadByte(S)             (lu_byte)LoadChar(S) | 
|---|
| 45 | #define LoadVar(S,x)            LoadMem(S,&x,1,sizeof(x)) | 
|---|
| 46 | #define LoadVector(S,b,n,size)  LoadMem(S,b,n,size) | 
|---|
| 47 |  | 
|---|
| 48 | static void LoadBlock(LoadState* S, void* b, size_t size) | 
|---|
| 49 | { | 
|---|
| 50 | size_t r=luaZ_read(S->Z,b,size); | 
|---|
| 51 | UNUSED(r); | 
|---|
| 52 | IF (r!=0, "unexpected end"); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | static int LoadChar(LoadState* S) | 
|---|
| 56 | { | 
|---|
| 57 | char x; | 
|---|
| 58 | LoadVar(S,x); | 
|---|
| 59 | return x; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | static int LoadInt(LoadState* S) | 
|---|
| 63 | { | 
|---|
| 64 | int x; | 
|---|
| 65 | LoadVar(S,x); | 
|---|
| 66 | IF (x<0, "bad integer"); | 
|---|
| 67 | return x; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | static lua_Number LoadNumber(LoadState* S) | 
|---|
| 71 | { | 
|---|
| 72 | lua_Number x; | 
|---|
| 73 | LoadVar(S,x); | 
|---|
| 74 | return x; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | static TString* LoadString(LoadState* S) | 
|---|
| 78 | { | 
|---|
| 79 | size_t size; | 
|---|
| 80 | LoadVar(S,size); | 
|---|
| 81 | if (size==0) | 
|---|
| 82 | return NULL; | 
|---|
| 83 | else | 
|---|
| 84 | { | 
|---|
| 85 | char* s=luaZ_openspace(S->L,S->b,size); | 
|---|
| 86 | LoadBlock(S,s,size); | 
|---|
| 87 | return luaS_newlstr(S->L,s,size-1);           /* remove trailing '\0' */ | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | static void LoadCode(LoadState* S, Proto* f) | 
|---|
| 92 | { | 
|---|
| 93 | int n=LoadInt(S); | 
|---|
| 94 | f->code=luaM_newvector(S->L,n,Instruction); | 
|---|
| 95 | f->sizecode=n; | 
|---|
| 96 | LoadVector(S,f->code,n,sizeof(Instruction)); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | static Proto* LoadFunction(LoadState* S, TString* p); | 
|---|
| 100 |  | 
|---|
| 101 | static void LoadConstants(LoadState* S, Proto* f) | 
|---|
| 102 | { | 
|---|
| 103 | int i,n; | 
|---|
| 104 | n=LoadInt(S); | 
|---|
| 105 | f->k=luaM_newvector(S->L,n,TValue); | 
|---|
| 106 | f->sizek=n; | 
|---|
| 107 | for (i=0; i<n; i++) setnilvalue(&f->k[i]); | 
|---|
| 108 | for (i=0; i<n; i++) | 
|---|
| 109 | { | 
|---|
| 110 | TValue* o=&f->k[i]; | 
|---|
| 111 | int t=LoadChar(S); | 
|---|
| 112 | switch (t) | 
|---|
| 113 | { | 
|---|
| 114 | case LUA_TNIL: | 
|---|
| 115 | setnilvalue(o); | 
|---|
| 116 | break; | 
|---|
| 117 | case LUA_TBOOLEAN: | 
|---|
| 118 | setbvalue(o,LoadChar(S)); | 
|---|
| 119 | break; | 
|---|
| 120 | case LUA_TNUMBER: | 
|---|
| 121 | setnvalue(o,LoadNumber(S)); | 
|---|
| 122 | break; | 
|---|
| 123 | case LUA_TSTRING: | 
|---|
| 124 | setsvalue2n(S->L,o,LoadString(S)); | 
|---|
| 125 | break; | 
|---|
| 126 | default: | 
|---|
| 127 | error(S,"bad constant"); | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | n=LoadInt(S); | 
|---|
| 132 | f->p=luaM_newvector(S->L,n,Proto*); | 
|---|
| 133 | f->sizep=n; | 
|---|
| 134 | for (i=0; i<n; i++) f->p[i]=NULL; | 
|---|
| 135 | for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | static void LoadDebug(LoadState* S, Proto* f) | 
|---|
| 139 | { | 
|---|
| 140 | int i,n; | 
|---|
| 141 | n=LoadInt(S); | 
|---|
| 142 | f->lineinfo=luaM_newvector(S->L,n,int); | 
|---|
| 143 | f->sizelineinfo=n; | 
|---|
| 144 | LoadVector(S,f->lineinfo,n,sizeof(int)); | 
|---|
| 145 | n=LoadInt(S); | 
|---|
| 146 | f->locvars=luaM_newvector(S->L,n,LocVar); | 
|---|
| 147 | f->sizelocvars=n; | 
|---|
| 148 | for (i=0; i<n; i++) f->locvars[i].varname=NULL; | 
|---|
| 149 | for (i=0; i<n; i++) | 
|---|
| 150 | { | 
|---|
| 151 | f->locvars[i].varname=LoadString(S); | 
|---|
| 152 | f->locvars[i].startpc=LoadInt(S); | 
|---|
| 153 | f->locvars[i].endpc=LoadInt(S); | 
|---|
| 154 | } | 
|---|
| 155 | n=LoadInt(S); | 
|---|
| 156 | f->upvalues=luaM_newvector(S->L,n,TString*); | 
|---|
| 157 | f->sizeupvalues=n; | 
|---|
| 158 | for (i=0; i<n; i++) f->upvalues[i]=NULL; | 
|---|
| 159 | for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | static Proto* LoadFunction(LoadState* S, TString* p) | 
|---|
| 163 | { | 
|---|
| 164 | Proto* f=luaF_newproto(S->L); | 
|---|
| 165 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); | 
|---|
| 166 | f->source=LoadString(S); if (f->source==NULL) f->source=p; | 
|---|
| 167 | f->linedefined=LoadInt(S); | 
|---|
| 168 | f->lastlinedefined=LoadInt(S); | 
|---|
| 169 | f->nups=LoadByte(S); | 
|---|
| 170 | f->numparams=LoadByte(S); | 
|---|
| 171 | f->is_vararg=LoadByte(S); | 
|---|
| 172 | f->maxstacksize=LoadByte(S); | 
|---|
| 173 | LoadCode(S,f); | 
|---|
| 174 | LoadConstants(S,f); | 
|---|
| 175 | LoadDebug(S,f); | 
|---|
| 176 | IF (!luaG_checkcode(f), "bad code"); | 
|---|
| 177 | S->L->top--; | 
|---|
| 178 | return f; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | static void LoadHeader(LoadState* S) | 
|---|
| 182 | { | 
|---|
| 183 | char h[LUAC_HEADERSIZE]; | 
|---|
| 184 | char s[LUAC_HEADERSIZE]; | 
|---|
| 185 | luaU_header(h); | 
|---|
| 186 | LoadBlock(S,s,LUAC_HEADERSIZE); | 
|---|
| 187 | IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | /* | 
|---|
| 191 | ** load precompiled chunk | 
|---|
| 192 | */ | 
|---|
| 193 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) | 
|---|
| 194 | { | 
|---|
| 195 | LoadState S; | 
|---|
| 196 | if (*name=='@' || *name=='=') | 
|---|
| 197 | S.name=name+1; | 
|---|
| 198 | else if (*name==LUA_SIGNATURE[0]) | 
|---|
| 199 | S.name="binary string"; | 
|---|
| 200 | else | 
|---|
| 201 | S.name=name; | 
|---|
| 202 | S.L=L; | 
|---|
| 203 | S.Z=Z; | 
|---|
| 204 | S.b=buff; | 
|---|
| 205 | LoadHeader(&S); | 
|---|
| 206 | return LoadFunction(&S,luaS_newliteral(L,"=?")); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | /* | 
|---|
| 210 | * make header | 
|---|
| 211 | */ | 
|---|
| 212 | void luaU_header (char* h) | 
|---|
| 213 | { | 
|---|
| 214 | int x=1; | 
|---|
| 215 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); | 
|---|
| 216 | h+=sizeof(LUA_SIGNATURE)-1; | 
|---|
| 217 | *h++=(char)LUAC_VERSION; | 
|---|
| 218 | *h++=(char)LUAC_FORMAT; | 
|---|
| 219 | *h++=(char)*(char*)&x;                         /* endianness */ | 
|---|
| 220 | *h++=(char)sizeof(int); | 
|---|
| 221 | *h++=(char)sizeof(size_t); | 
|---|
| 222 | *h++=(char)sizeof(Instruction); | 
|---|
| 223 | *h++=(char)sizeof(lua_Number); | 
|---|
| 224 | *h++=(char)(((lua_Number)0.5)==0);             /* is lua_Number integral? */ | 
|---|
| 225 | } | 
|---|