00001
00002
00003
00004
00005
00006
00007 #include <string.h>
00008
00009 #define lundump_c
00010 #define LUA_CORE
00011
00012 #include "lua.h"
00013
00014 #include "ldebug.h"
00015 #include "ldo.h"
00016 #include "lfunc.h"
00017 #include "lmem.h"
00018 #include "lobject.h"
00019 #include "lstring.h"
00020 #include "lundump.h"
00021 #include "lzio.h"
00022
00023 typedef struct {
00024 lua_State* L;
00025 ZIO* Z;
00026 Mbuffer* b;
00027 const char* name;
00028 } LoadState;
00029
00030 #ifdef LUAC_TRUST_BINARIES
00031 #define IF(c,s)
00032 #define error(S,s)
00033 #else
00034 #define IF(c,s) if (c) error(S,s)
00035
00036 static void error(LoadState* S, const char* why)
00037 {
00038 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
00039 luaD_throw(S->L,LUA_ERRSYNTAX);
00040 }
00041 #endif
00042
00043 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
00044 #define LoadByte(S) (lu_byte)LoadChar(S)
00045 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
00046 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
00047
00048 static void LoadBlock(LoadState* S, void* b, size_t size)
00049 {
00050 size_t r=luaZ_read(S->Z,b,size);
00051 IF (r!=0, "unexpected end");
00052 }
00053
00054 static int LoadChar(LoadState* S)
00055 {
00056 char x;
00057 LoadVar(S,x);
00058 return x;
00059 }
00060
00061 static int LoadInt(LoadState* S)
00062 {
00063 int x;
00064 LoadVar(S,x);
00065 IF (x<0, "bad integer");
00066 return x;
00067 }
00068
00069 static lua_Number LoadNumber(LoadState* S)
00070 {
00071 lua_Number x;
00072 LoadVar(S,x);
00073 return x;
00074 }
00075
00076 static TString* LoadString(LoadState* S)
00077 {
00078 size_t size;
00079 LoadVar(S,size);
00080 if (size==0)
00081 return NULL;
00082 else
00083 {
00084 char* s=luaZ_openspace(S->L,S->b,size);
00085 LoadBlock(S,s,size);
00086 return luaS_newlstr(S->L,s,size-1);
00087 }
00088 }
00089
00090 static void LoadCode(LoadState* S, Proto* f)
00091 {
00092 int n=LoadInt(S);
00093 f->code=luaM_newvector(S->L,n,Instruction);
00094 f->sizecode=n;
00095 LoadVector(S,f->code,n,sizeof(Instruction));
00096 }
00097
00098 static Proto* LoadFunction(LoadState* S, TString* p);
00099
00100 static void LoadConstants(LoadState* S, Proto* f)
00101 {
00102 int i,n;
00103 n=LoadInt(S);
00104 f->k=luaM_newvector(S->L,n,TValue);
00105 f->sizek=n;
00106 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
00107 for (i=0; i<n; i++)
00108 {
00109 TValue* o=&f->k[i];
00110 int t=LoadChar(S);
00111 switch (t)
00112 {
00113 case LUA_TNIL:
00114 setnilvalue(o);
00115 break;
00116 case LUA_TBOOLEAN:
00117 setbvalue(o,LoadChar(S)!=0);
00118 break;
00119 case LUA_TNUMBER:
00120 setnvalue(o,LoadNumber(S));
00121 break;
00122 case LUA_TSTRING:
00123 setsvalue2n(S->L,o,LoadString(S));
00124 break;
00125 default:
00126 error(S,"bad constant");
00127 break;
00128 }
00129 }
00130 n=LoadInt(S);
00131 f->p=luaM_newvector(S->L,n,Proto*);
00132 f->sizep=n;
00133 for (i=0; i<n; i++) f->p[i]=NULL;
00134 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
00135 }
00136
00137 static void LoadDebug(LoadState* S, Proto* f)
00138 {
00139 int i,n;
00140 n=LoadInt(S);
00141 f->lineinfo=luaM_newvector(S->L,n,int);
00142 f->sizelineinfo=n;
00143 LoadVector(S,f->lineinfo,n,sizeof(int));
00144 n=LoadInt(S);
00145 f->locvars=luaM_newvector(S->L,n,LocVar);
00146 f->sizelocvars=n;
00147 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
00148 for (i=0; i<n; i++)
00149 {
00150 f->locvars[i].varname=LoadString(S);
00151 f->locvars[i].startpc=LoadInt(S);
00152 f->locvars[i].endpc=LoadInt(S);
00153 }
00154 n=LoadInt(S);
00155 f->upvalues=luaM_newvector(S->L,n,TString*);
00156 f->sizeupvalues=n;
00157 for (i=0; i<n; i++) f->upvalues[i]=NULL;
00158 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
00159 }
00160
00161 static Proto* LoadFunction(LoadState* S, TString* p)
00162 {
00163 Proto* f;
00164 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
00165 f=luaF_newproto(S->L);
00166 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
00167 f->source=LoadString(S); if (f->source==NULL) f->source=p;
00168 f->linedefined=LoadInt(S);
00169 f->lastlinedefined=LoadInt(S);
00170 f->nups=LoadByte(S);
00171 f->numparams=LoadByte(S);
00172 f->is_vararg=LoadByte(S);
00173 f->maxstacksize=LoadByte(S);
00174 LoadCode(S,f);
00175 LoadConstants(S,f);
00176 LoadDebug(S,f);
00177 IF (!luaG_checkcode(f), "bad code");
00178 S->L->top--;
00179 S->L->nCcalls--;
00180 return f;
00181 }
00182
00183 static void LoadHeader(LoadState* S)
00184 {
00185 char h[LUAC_HEADERSIZE];
00186 char s[LUAC_HEADERSIZE];
00187 luaU_header(h);
00188 LoadBlock(S,s,LUAC_HEADERSIZE);
00189 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
00190 }
00191
00192
00193
00194
00195 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
00196 {
00197 LoadState S;
00198 if (*name=='@' || *name=='=')
00199 S.name=name+1;
00200 else if (*name==LUA_SIGNATURE[0])
00201 S.name="binary string";
00202 else
00203 S.name=name;
00204 S.L=L;
00205 S.Z=Z;
00206 S.b=buff;
00207 LoadHeader(&S);
00208 return LoadFunction(&S,luaS_newliteral(L,"=?"));
00209 }
00210
00211
00212
00213
00214 void luaU_header (char* h)
00215 {
00216 int x=1;
00217 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
00218 h+=sizeof(LUA_SIGNATURE)-1;
00219 *h++=(char)LUAC_VERSION;
00220 *h++=(char)LUAC_FORMAT;
00221 *h++=(char)*(char*)&x;
00222 *h++=(char)sizeof(int);
00223 *h++=(char)sizeof(size_t);
00224 *h++=(char)sizeof(Instruction);
00225 *h++=(char)sizeof(lua_Number);
00226 *h++=(char)(((lua_Number)0.5)==0);
00227 }