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