summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lundump.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lundump.c')
-rw-r--r--apps/plugins/lua/lundump.c142
1 files changed, 87 insertions, 55 deletions
diff --git a/apps/plugins/lua/lundump.c b/apps/plugins/lua/lundump.c
index 8010a45795..0db70d20ea 100644
--- a/apps/plugins/lua/lundump.c
+++ b/apps/plugins/lua/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 2** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $
3** load precompiled Lua chunks 3** load precompiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,28 +27,24 @@ typedef struct {
27 const char* name; 27 const char* name;
28} LoadState; 28} LoadState;
29 29
30#ifdef LUAC_TRUST_BINARIES 30static l_noret error(LoadState* S, const char* why)
31#define IF(c,s)
32#define error(S,s)
33#else
34#define IF(c,s) if (c) error(S,s)
35
36static void error(LoadState* S, const char* why)
37{ 31{
38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
39 luaD_throw(S->L,LUA_ERRSYNTAX); 33 luaD_throw(S->L,LUA_ERRSYNTAX);
40} 34}
41#endif
42 35
43#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 36#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44#define LoadByte(S) (lu_byte)LoadChar(S) 37#define LoadByte(S) (lu_byte)LoadChar(S)
45#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 38#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 39#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
47 40
41#if !defined(luai_verifycode)
42#define luai_verifycode(L,b,f) /* empty */
43#endif
44
48static void LoadBlock(LoadState* S, void* b, size_t size) 45static void LoadBlock(LoadState* S, void* b, size_t size)
49{ 46{
50 size_t r=luaZ_read(S->Z,b,size); 47 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
51 IF (r!=0, "unexpected end");
52} 48}
53 49
54static int LoadChar(LoadState* S) 50static int LoadChar(LoadState* S)
@@ -62,7 +58,7 @@ static int LoadInt(LoadState* S)
62{ 58{
63 int x; 59 int x;
64 LoadVar(S,x); 60 LoadVar(S,x);
65 IF (x<0, "bad integer"); 61 if (x<0) error(S,"corrupted");
66 return x; 62 return x;
67} 63}
68 64
@@ -82,7 +78,7 @@ static TString* LoadString(LoadState* S)
82 else 78 else
83 { 79 {
84 char* s=luaZ_openspace(S->L,S->b,size); 80 char* s=luaZ_openspace(S->L,S->b,size);
85 LoadBlock(S,s,size); 81 LoadBlock(S,s,size*sizeof(char));
86 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 82 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
87 } 83 }
88} 84}
@@ -95,7 +91,7 @@ static void LoadCode(LoadState* S, Proto* f)
95 LoadVector(S,f->code,n,sizeof(Instruction)); 91 LoadVector(S,f->code,n,sizeof(Instruction));
96} 92}
97 93
98static Proto* LoadFunction(LoadState* S, TString* p); 94static void LoadFunction(LoadState* S, Proto* f);
99 95
100static void LoadConstants(LoadState* S, Proto* f) 96static void LoadConstants(LoadState* S, Proto* f)
101{ 97{
@@ -111,10 +107,10 @@ static void LoadConstants(LoadState* S, Proto* f)
111 switch (t) 107 switch (t)
112 { 108 {
113 case LUA_TNIL: 109 case LUA_TNIL:
114 setnilvalue(o); 110 setnilvalue(o);
115 break; 111 break;
116 case LUA_TBOOLEAN: 112 case LUA_TBOOLEAN:
117 setbvalue(o,LoadChar(S)!=0); 113 setbvalue(o,LoadChar(S));
118 break; 114 break;
119 case LUA_TNUMBER: 115 case LUA_TNUMBER:
120 setnvalue(o,LoadNumber(S)); 116 setnvalue(o,LoadNumber(S));
@@ -122,21 +118,38 @@ static void LoadConstants(LoadState* S, Proto* f)
122 case LUA_TSTRING: 118 case LUA_TSTRING:
123 setsvalue2n(S->L,o,LoadString(S)); 119 setsvalue2n(S->L,o,LoadString(S));
124 break; 120 break;
125 default: 121 default: lua_assert(0);
126 error(S,"bad constant");
127 break;
128 } 122 }
129 } 123 }
130 n=LoadInt(S); 124 n=LoadInt(S);
131 f->p=luaM_newvector(S->L,n,Proto*); 125 f->p=luaM_newvector(S->L,n,Proto*);
132 f->sizep=n; 126 f->sizep=n;
133 for (i=0; i<n; i++) f->p[i]=NULL; 127 for (i=0; i<n; i++) f->p[i]=NULL;
134 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 128 for (i=0; i<n; i++)
129 {
130 f->p[i]=luaF_newproto(S->L);
131 LoadFunction(S,f->p[i]);
132 }
133}
134
135static void LoadUpvalues(LoadState* S, Proto* f)
136{
137 int i,n;
138 n=LoadInt(S);
139 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
140 f->sizeupvalues=n;
141 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
142 for (i=0; i<n; i++)
143 {
144 f->upvalues[i].instack=LoadByte(S);
145 f->upvalues[i].idx=LoadByte(S);
146 }
135} 147}
136 148
137static void LoadDebug(LoadState* S, Proto* f) 149static void LoadDebug(LoadState* S, Proto* f)
138{ 150{
139 int i,n; 151 int i,n;
152 f->source=LoadString(S);
140 n=LoadInt(S); 153 n=LoadInt(S);
141 f->lineinfo=luaM_newvector(S->L,n,int); 154 f->lineinfo=luaM_newvector(S->L,n,int);
142 f->sizelineinfo=n; 155 f->sizelineinfo=n;
@@ -152,49 +165,48 @@ static void LoadDebug(LoadState* S, Proto* f)
152 f->locvars[i].endpc=LoadInt(S); 165 f->locvars[i].endpc=LoadInt(S);
153 } 166 }
154 n=LoadInt(S); 167 n=LoadInt(S);
155 f->upvalues=luaM_newvector(S->L,n,TString*); 168 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
156 f->sizeupvalues=n;
157 for (i=0; i<n; i++) f->upvalues[i]=NULL;
158 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
159} 169}
160 170
161static Proto* LoadFunction(LoadState* S, TString* p) 171static void LoadFunction(LoadState* S, Proto* f)
162{ 172{
163 Proto* f;
164 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
165 f=luaF_newproto(S->L);
166 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
167 f->source=LoadString(S); if (f->source==NULL) f->source=p;
168 f->linedefined=LoadInt(S); 173 f->linedefined=LoadInt(S);
169 f->lastlinedefined=LoadInt(S); 174 f->lastlinedefined=LoadInt(S);
170 f->nups=LoadByte(S);
171 f->numparams=LoadByte(S); 175 f->numparams=LoadByte(S);
172 f->is_vararg=LoadByte(S); 176 f->is_vararg=LoadByte(S);
173 f->maxstacksize=LoadByte(S); 177 f->maxstacksize=LoadByte(S);
174 LoadCode(S,f); 178 LoadCode(S,f);
175 LoadConstants(S,f); 179 LoadConstants(S,f);
180 LoadUpvalues(S,f);
176 LoadDebug(S,f); 181 LoadDebug(S,f);
177 IF (!luaG_checkcode(f), "bad code");
178 S->L->top--;
179 S->L->nCcalls--;
180 return f;
181} 182}
182 183
184/* the code below must be consistent with the code in luaU_header */
185#define N0 LUAC_HEADERSIZE
186#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
187#define N2 N1+2
188#define N3 N2+6
189
183static void LoadHeader(LoadState* S) 190static void LoadHeader(LoadState* S)
184{ 191{
185 char h[LUAC_HEADERSIZE]; 192 lu_byte h[LUAC_HEADERSIZE];
186 char s[LUAC_HEADERSIZE]; 193 lu_byte s[LUAC_HEADERSIZE];
187 luaU_header(h); 194 luaU_header(h);
188 LoadBlock(S,s,LUAC_HEADERSIZE); 195 memcpy(s,h,sizeof(char)); /* first char already read */
189 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 196 LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
197 if (memcmp(h,s,N0)==0) return;
198 if (memcmp(h,s,N1)!=0) error(S,"not a");
199 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
200 if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
190} 201}
191 202
192/* 203/*
193** load precompiled chunk 204** load precompiled chunk
194*/ 205*/
195Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 206Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
196{ 207{
197 LoadState S; 208 LoadState S;
209 Closure* cl;
198 if (*name=='@' || *name=='=') 210 if (*name=='@' || *name=='=')
199 S.name=name+1; 211 S.name=name+1;
200 else if (*name==LUA_SIGNATURE[0]) 212 else if (*name==LUA_SIGNATURE[0])
@@ -205,23 +217,43 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
205 S.Z=Z; 217 S.Z=Z;
206 S.b=buff; 218 S.b=buff;
207 LoadHeader(&S); 219 LoadHeader(&S);
208 return LoadFunction(&S,luaS_newliteral(L,"=?")); 220 cl=luaF_newLclosure(L,1);
221 setclLvalue(L,L->top,cl); incr_top(L);
222 cl->l.p=luaF_newproto(L);
223 LoadFunction(&S,cl->l.p);
224 if (cl->l.p->sizeupvalues != 1)
225 {
226 Proto* p=cl->l.p;
227 cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
228 cl->l.p=p;
229 setclLvalue(L,L->top-1,cl);
230 }
231 luai_verifycode(L,buff,cl->l.p);
232 return cl;
209} 233}
210 234
235#define MYINT(s) (s[0]-'0')
236#undef VERSION
237#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
238#define FORMAT 0 /* this is the official format */
239
211/* 240/*
212* make header 241* make header for precompiled chunks
242* if you change the code below be sure to update LoadHeader and FORMAT above
243* and LUAC_HEADERSIZE in lundump.h
213*/ 244*/
214void luaU_header (char* h) 245void luaU_header (lu_byte* h)
215{ 246{
216 int x=1; 247 int x=1;
217 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 248 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
218 h+=sizeof(LUA_SIGNATURE)-1; 249 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
219 *h++=(char)LUAC_VERSION; 250 *h++=cast_byte(VERSION);
220 *h++=(char)LUAC_FORMAT; 251 *h++=cast_byte(FORMAT);
221 *h++=(char)*(char*)&x; /* endianness */ 252 *h++=cast_byte(*(char*)&x); /* endianness */
222 *h++=(char)sizeof(int); 253 *h++=cast_byte(sizeof(int));
223 *h++=(char)sizeof(size_t); 254 *h++=cast_byte(sizeof(size_t));
224 *h++=(char)sizeof(Instruction); 255 *h++=cast_byte(sizeof(Instruction));
225 *h++=(char)sizeof(lua_Number); 256 *h++=cast_byte(sizeof(lua_Number));
226 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
258 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
227} 259}