summaryrefslogtreecommitdiff
path: root/lundump.c
diff options
context:
space:
mode:
Diffstat (limited to 'lundump.c')
-rw-r--r--lundump.c40
1 files changed, 14 insertions, 26 deletions
diff --git a/lundump.c b/lundump.c
index 119ebdd5..184b768f 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $ 2** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf 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,27 +27,20 @@ typedef struct {
27 const char* name; 27 const char* name;
28} LoadState; 28} LoadState;
29 29
30#ifdef LUAC_TRUST_BINARIES
31#define IF(c,s)
32#else
33#define IF(c,s) if (c) error(S,s)
34
35static void error(LoadState* S, const char* why) 30static void error(LoadState* S, const char* why)
36{ 31{
37 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
38 luaD_throw(S->L,LUA_ERRSYNTAX); 33 luaD_throw(S->L,LUA_ERRSYNTAX);
39} 34}
40#endif
41 35
42#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 36#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
43#define LoadByte(S) (lu_byte)LoadChar(S) 37#define LoadByte(S) (lu_byte)LoadChar(S)
44#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 38#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
45#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 39#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
46 40
47static void LoadBlock(LoadState* S, void* b, size_t size) 41static void LoadBlock(LoadState* S, void* b, size_t size)
48{ 42{
49 size_t r=luaZ_read(S->Z,b,size); 43 if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
50 IF (r!=0, "unexpected end");
51} 44}
52 45
53static int LoadChar(LoadState* S) 46static int LoadChar(LoadState* S)
@@ -61,7 +54,6 @@ static int LoadInt(LoadState* S)
61{ 54{
62 int x; 55 int x;
63 LoadVar(S,x); 56 LoadVar(S,x);
64 IF (x<0, "bad integer");
65 return x; 57 return x;
66} 58}
67 59
@@ -94,7 +86,7 @@ static void LoadCode(LoadState* S, Proto* f)
94 LoadVector(S,f->code,n,sizeof(Instruction)); 86 LoadVector(S,f->code,n,sizeof(Instruction));
95} 87}
96 88
97static Proto* LoadFunction(LoadState* S, TString* p); 89static Proto* LoadFunction(LoadState* S);
98 90
99static void LoadConstants(LoadState* S, Proto* f) 91static void LoadConstants(LoadState* S, Proto* f)
100{ 92{
@@ -113,7 +105,7 @@ static void LoadConstants(LoadState* S, Proto* f)
113 setnilvalue(o); 105 setnilvalue(o);
114 break; 106 break;
115 case LUA_TBOOLEAN: 107 case LUA_TBOOLEAN:
116 setbvalue(o,LoadChar(S)!=0); 108 setbvalue(o,LoadChar(S));
117 break; 109 break;
118 case LUA_TNUMBER: 110 case LUA_TNUMBER:
119 setnvalue(o,LoadNumber(S)); 111 setnvalue(o,LoadNumber(S));
@@ -121,16 +113,13 @@ static void LoadConstants(LoadState* S, Proto* f)
121 case LUA_TSTRING: 113 case LUA_TSTRING:
122 setsvalue2n(S->L,o,LoadString(S)); 114 setsvalue2n(S->L,o,LoadString(S));
123 break; 115 break;
124 default:
125 IF (1, "bad constant");
126 break;
127 } 116 }
128 } 117 }
129 n=LoadInt(S); 118 n=LoadInt(S);
130 f->p=luaM_newvector(S->L,n,Proto*); 119 f->p=luaM_newvector(S->L,n,Proto*);
131 f->sizep=n; 120 f->sizep=n;
132 for (i=0; i<n; i++) f->p[i]=NULL; 121 for (i=0; i<n; i++) f->p[i]=NULL;
133 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 122 for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
134} 123}
135 124
136static void LoadUpvalues(LoadState* S, Proto* f) 125static void LoadUpvalues(LoadState* S, Proto* f)
@@ -150,6 +139,7 @@ static void LoadUpvalues(LoadState* S, Proto* f)
150static void LoadDebug(LoadState* S, Proto* f) 139static void LoadDebug(LoadState* S, Proto* f)
151{ 140{
152 int i,n; 141 int i,n;
142 f->source=LoadString(S);
153 n=LoadInt(S); 143 n=LoadInt(S);
154 f->lineinfo=luaM_newvector(S->L,n,int); 144 f->lineinfo=luaM_newvector(S->L,n,int);
155 f->sizelineinfo=n; 145 f->sizelineinfo=n;
@@ -168,13 +158,10 @@ static void LoadDebug(LoadState* S, Proto* f)
168 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S); 158 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
169} 159}
170 160
171static Proto* LoadFunction(LoadState* S, TString* p) 161static Proto* LoadFunction(LoadState* S)
172{ 162{
173 Proto* f; 163 Proto* f=luaF_newproto(S->L);
174 if (++G(S->L)->nCcalls > LUAI_MAXCCALLS) error(S, "function nest too deep");
175 f=luaF_newproto(S->L);
176 setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 164 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
177 f->source=LoadString(S); if (f->source==NULL) f->source=p;
178 f->linedefined=LoadInt(S); 165 f->linedefined=LoadInt(S);
179 f->lastlinedefined=LoadInt(S); 166 f->lastlinedefined=LoadInt(S);
180 f->numparams=LoadByte(S); 167 f->numparams=LoadByte(S);
@@ -185,7 +172,6 @@ static Proto* LoadFunction(LoadState* S, TString* p)
185 LoadUpvalues(S,f); 172 LoadUpvalues(S,f);
186 LoadDebug(S,f); 173 LoadDebug(S,f);
187 S->L->top--; 174 S->L->top--;
188 G(S->L)->nCcalls--;
189 return f; 175 return f;
190} 176}
191 177
@@ -195,7 +181,7 @@ static void LoadHeader(LoadState* S)
195 char s[LUAC_HEADERSIZE]; 181 char s[LUAC_HEADERSIZE];
196 luaU_header(h); 182 luaU_header(h);
197 LoadBlock(S,s,LUAC_HEADERSIZE); 183 LoadBlock(S,s,LUAC_HEADERSIZE);
198 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 184 if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
199} 185}
200 186
201/* 187/*
@@ -214,11 +200,13 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
214 S.Z=Z; 200 S.Z=Z;
215 S.b=buff; 201 S.b=buff;
216 LoadHeader(&S); 202 LoadHeader(&S);
217 return LoadFunction(&S,luaS_newliteral(L,"=?")); 203 return LoadFunction(&S);
218} 204}
219 205
220/* 206/*
221* make header 207* make header
208* if you make any changes in the header or in LUA_SIGNATURE,
209* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
222*/ 210*/
223void luaU_header (char* h) 211void luaU_header (char* h)
224{ 212{