rlm@1: /* rlm@1: ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ rlm@1: ** load precompiled Lua chunks rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: #include rlm@1: rlm@1: #define lundump_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "ldebug.h" rlm@1: #include "ldo.h" rlm@1: #include "lfunc.h" rlm@1: #include "lmem.h" rlm@1: #include "lobject.h" rlm@1: #include "lstring.h" rlm@1: #include "lundump.h" rlm@1: #include "lzio.h" rlm@1: rlm@1: typedef struct { rlm@1: lua_State* L; rlm@1: ZIO* Z; rlm@1: Mbuffer* b; rlm@1: const char* name; rlm@1: } LoadState; rlm@1: rlm@1: #ifdef LUAC_TRUST_BINARIES rlm@1: #define IF(c,s) rlm@1: #define error(S,s) rlm@1: #else rlm@1: #define IF(c,s) if (c) error(S,s) rlm@1: rlm@1: static void error(LoadState* S, const char* why) rlm@1: { rlm@1: luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); rlm@1: luaD_throw(S->L,LUA_ERRSYNTAX); rlm@1: } rlm@1: #endif rlm@1: rlm@1: #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) rlm@1: #define LoadByte(S) (lu_byte)LoadChar(S) rlm@1: #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) rlm@1: #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) rlm@1: rlm@1: static void LoadBlock(LoadState* S, void* b, size_t size) rlm@1: { rlm@1: size_t r=luaZ_read(S->Z,b,size); rlm@1: IF (r!=0, "unexpected end"); rlm@1: } rlm@1: rlm@1: static int LoadChar(LoadState* S) rlm@1: { rlm@1: char x; rlm@1: LoadVar(S,x); rlm@1: return x; rlm@1: } rlm@1: rlm@1: static int LoadInt(LoadState* S) rlm@1: { rlm@1: int x; rlm@1: LoadVar(S,x); rlm@1: IF (x<0, "bad integer"); rlm@1: return x; rlm@1: } rlm@1: rlm@1: static lua_Number LoadNumber(LoadState* S) rlm@1: { rlm@1: lua_Number x; rlm@1: LoadVar(S,x); rlm@1: return x; rlm@1: } rlm@1: rlm@1: static TString* LoadString(LoadState* S) rlm@1: { rlm@1: size_t size; rlm@1: LoadVar(S,size); rlm@1: if (size==0) rlm@1: return NULL; rlm@1: else rlm@1: { rlm@1: char* s=luaZ_openspace(S->L,S->b,size); rlm@1: LoadBlock(S,s,size); rlm@1: return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ rlm@1: } rlm@1: } rlm@1: rlm@1: static void LoadCode(LoadState* S, Proto* f) rlm@1: { rlm@1: int n=LoadInt(S); rlm@1: f->code=luaM_newvector(S->L,n,Instruction); rlm@1: f->sizecode=n; rlm@1: LoadVector(S,f->code,n,sizeof(Instruction)); rlm@1: } rlm@1: rlm@1: static Proto* LoadFunction(LoadState* S, TString* p); rlm@1: rlm@1: static void LoadConstants(LoadState* S, Proto* f) rlm@1: { rlm@1: int i,n; rlm@1: n=LoadInt(S); rlm@1: f->k=luaM_newvector(S->L,n,TValue); rlm@1: f->sizek=n; rlm@1: for (i=0; ik[i]); rlm@1: for (i=0; ik[i]; rlm@1: int t=LoadChar(S); rlm@1: switch (t) rlm@1: { rlm@1: case LUA_TNIL: rlm@1: setnilvalue(o); rlm@1: break; rlm@1: case LUA_TBOOLEAN: rlm@1: setbvalue(o,LoadChar(S)!=0); rlm@1: break; rlm@1: case LUA_TNUMBER: rlm@1: setnvalue(o,LoadNumber(S)); rlm@1: break; rlm@1: case LUA_TSTRING: rlm@1: setsvalue2n(S->L,o,LoadString(S)); rlm@1: break; rlm@1: default: rlm@1: error(S,"bad constant"); rlm@1: break; rlm@1: } rlm@1: } rlm@1: n=LoadInt(S); rlm@1: f->p=luaM_newvector(S->L,n,Proto*); rlm@1: f->sizep=n; rlm@1: for (i=0; ip[i]=NULL; rlm@1: for (i=0; ip[i]=LoadFunction(S,f->source); rlm@1: } rlm@1: rlm@1: static void LoadDebug(LoadState* S, Proto* f) rlm@1: { rlm@1: int i,n; rlm@1: n=LoadInt(S); rlm@1: f->lineinfo=luaM_newvector(S->L,n,int); rlm@1: f->sizelineinfo=n; rlm@1: LoadVector(S,f->lineinfo,n,sizeof(int)); rlm@1: n=LoadInt(S); rlm@1: f->locvars=luaM_newvector(S->L,n,LocVar); rlm@1: f->sizelocvars=n; rlm@1: for (i=0; ilocvars[i].varname=NULL; rlm@1: for (i=0; ilocvars[i].varname=LoadString(S); rlm@1: f->locvars[i].startpc=LoadInt(S); rlm@1: f->locvars[i].endpc=LoadInt(S); rlm@1: } rlm@1: n=LoadInt(S); rlm@1: f->upvalues=luaM_newvector(S->L,n,TString*); rlm@1: f->sizeupvalues=n; rlm@1: for (i=0; iupvalues[i]=NULL; rlm@1: for (i=0; iupvalues[i]=LoadString(S); rlm@1: } rlm@1: rlm@1: static Proto* LoadFunction(LoadState* S, TString* p) rlm@1: { rlm@1: Proto* f; rlm@1: if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); rlm@1: f=luaF_newproto(S->L); rlm@1: setptvalue2s(S->L,S->L->top,f); incr_top(S->L); rlm@1: f->source=LoadString(S); if (f->source==NULL) f->source=p; rlm@1: f->linedefined=LoadInt(S); rlm@1: f->lastlinedefined=LoadInt(S); rlm@1: f->nups=LoadByte(S); rlm@1: f->numparams=LoadByte(S); rlm@1: f->is_vararg=LoadByte(S); rlm@1: f->maxstacksize=LoadByte(S); rlm@1: LoadCode(S,f); rlm@1: LoadConstants(S,f); rlm@1: LoadDebug(S,f); rlm@1: IF (!luaG_checkcode(f), "bad code"); rlm@1: S->L->top--; rlm@1: S->L->nCcalls--; rlm@1: return f; rlm@1: } rlm@1: rlm@1: static void LoadHeader(LoadState* S) rlm@1: { rlm@1: char h[LUAC_HEADERSIZE]; rlm@1: char s[LUAC_HEADERSIZE]; rlm@1: luaU_header(h); rlm@1: LoadBlock(S,s,LUAC_HEADERSIZE); rlm@1: IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); rlm@1: } rlm@1: rlm@1: /* rlm@1: ** load precompiled chunk rlm@1: */ rlm@1: Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) rlm@1: { rlm@1: LoadState S; rlm@1: if (*name=='@' || *name=='=') rlm@1: S.name=name+1; rlm@1: else if (*name==LUA_SIGNATURE[0]) rlm@1: S.name="binary string"; rlm@1: else rlm@1: S.name=name; rlm@1: S.L=L; rlm@1: S.Z=Z; rlm@1: S.b=buff; rlm@1: LoadHeader(&S); rlm@1: return LoadFunction(&S,luaS_newliteral(L,"=?")); rlm@1: } rlm@1: rlm@1: /* rlm@1: * make header rlm@1: */ rlm@1: void luaU_header (char* h) rlm@1: { rlm@1: int x=1; rlm@1: memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); rlm@1: h+=sizeof(LUA_SIGNATURE)-1; rlm@1: *h++=(char)LUAC_VERSION; rlm@1: *h++=(char)LUAC_FORMAT; rlm@1: *h++=(char)*(char*)&x; /* endianness */ rlm@1: *h++=(char)sizeof(int); rlm@1: *h++=(char)sizeof(size_t); rlm@1: *h++=(char)sizeof(Instruction); rlm@1: *h++=(char)sizeof(lua_Number); rlm@1: *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ rlm@1: }