aboutsummaryrefslogtreecommitdiff
path: root/lundump.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-10 14:56:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-10 14:56:32 -0300
commit197e249433027e72e5d4ef1a13c4edc6c24bca6c (patch)
treeaf0eb4632ab7f0332fb0db448586c087166e51bd /lundump.c
parent2aff75f58eb202dbf245787f2ae98c43124724f4 (diff)
downloadlua-197e249433027e72e5d4ef1a13c4edc6c24bca6c.tar.gz
lua-197e249433027e72e5d4ef1a13c4edc6c24bca6c.tar.bz2
lua-197e249433027e72e5d4ef1a13c4edc6c24bca6c.zip
"indent -kr -i2 -br -brf -nut" plus a few manual formating
Diffstat (limited to 'lundump.c')
-rw-r--r--lundump.c429
1 files changed, 222 insertions, 207 deletions
diff --git a/lundump.c b/lundump.c
index 9a3ee218..ae2d19f9 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 2.29 2014/02/28 16:13:01 roberto Exp roberto $ 2** $Id: lundump.c,v 2.30 2014/03/01 15:18:44 roberto Exp roberto $
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*/
@@ -20,227 +20,242 @@
20#include "lundump.h" 20#include "lundump.h"
21#include "lzio.h" 21#include "lzio.h"
22 22
23typedef struct {
24 lua_State* L;
25 ZIO* Z;
26 Mbuffer* b;
27 const char* name;
28} LoadState;
29
30static l_noret error(LoadState* S, const char* why)
31{
32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
33 luaD_throw(S->L,LUA_ERRSYNTAX);
34}
35 23
36#define LoadVar(S,x) LoadBlock(S,&x,sizeof(x)) 24#define LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
37#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 25#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
38 26
39#if !defined(luai_verifycode) 27#if !defined(luai_verifycode)
40#define luai_verifycode(L,b,f) /* empty */ 28#define luai_verifycode(L,b,f) /* empty */
41#endif 29#endif
42 30
43static void LoadBlock(LoadState* S, void* b, size_t size) 31
44{ 32typedef struct {
45 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); 33 lua_State *L;
46} 34 ZIO *Z;
47 35 Mbuffer *b;
48static lu_byte LoadByte(LoadState* S) 36 const char *name;
49{ 37} LoadState;
50 lu_byte x; 38
51 LoadVar(S,x); 39
52 return x; 40static l_noret error(LoadState *S, const char *why) {
53} 41 luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
54 42 luaD_throw(S->L, LUA_ERRSYNTAX);
55static int LoadInt(LoadState* S) 43}
56{ 44
57 int x; 45
58 LoadVar(S,x); 46static void LoadBlock (LoadState *S, void *b, size_t size) {
59 if (x<0) error(S,"corrupted"); 47 if (luaZ_read(S->Z, b, size) != 0)
60 return x; 48 error(S, "truncated");
61} 49}
62 50
63static lua_Number LoadNumber(LoadState* S) 51
64{ 52static lu_byte LoadByte (LoadState *S) {
65 lua_Number x; 53 lu_byte x;
66 LoadVar(S,x); 54 LoadVar(S, x);
67 return x; 55 return x;
68} 56}
69 57
70static lua_Integer LoadInteger(LoadState* S) 58
71{ 59static int LoadInt (LoadState *S) {
72 lua_Integer x; 60 int x;
73 LoadVar(S,x); 61 LoadVar(S, x);
74 return x; 62 if (x < 0)
75} 63 error(S, "corrupted");
76 64 return x;
77static TString* LoadString(LoadState* S) 65}
78{ 66
79 size_t size = LoadByte(S); 67
80 if (size == 0xFF) LoadVar(S,size); 68static lua_Number LoadNumber (LoadState *S) {
81 if (size==0) 69 lua_Number x;
82 return NULL; 70 LoadVar(S, x);
83 else 71 return x;
84 { 72}
85 char* s=luaZ_openspace(S->L,S->b,--size); 73
86 LoadVector(S,s,size); 74
87 return luaS_newlstr(S->L,s,size); 75static lua_Integer LoadInteger (LoadState *S) {
88 } 76 lua_Integer x;
89} 77 LoadVar(S, x);
90 78 return x;
91static void LoadCode(LoadState* S, Proto* f) 79}
92{ 80
93 int n=LoadInt(S); 81
94 f->code=luaM_newvector(S->L,n,Instruction); 82static TString *LoadString (LoadState *S) {
95 f->sizecode=n; 83 size_t size = LoadByte(S);
96 LoadVector(S,f->code,n); 84 if (size == 0xFF)
97} 85 LoadVar(S, size);
98 86 if (size == 0)
99static void LoadFunction(LoadState* S, Proto* f); 87 return NULL;
100 88 else {
101static void LoadConstants(LoadState* S, Proto* f) 89 char *s = luaZ_openspace(S->L, S->b, --size);
102{ 90 LoadVector(S, s, size);
103 int i,n; 91 return luaS_newlstr(S->L, s, size);
104 n=LoadInt(S);
105 f->k=luaM_newvector(S->L,n,TValue);
106 f->sizek=n;
107 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
108 for (i=0; i<n; i++)
109 {
110 TValue* o=&f->k[i];
111 int t=LoadByte(S);
112 switch (t)
113 {
114 case LUA_TNIL:
115 setnilvalue(o);
116 break;
117 case LUA_TBOOLEAN:
118 setbvalue(o,LoadByte(S));
119 break;
120 case LUA_TNUMFLT:
121 setnvalue(o,LoadNumber(S));
122 break;
123 case LUA_TNUMINT:
124 setivalue(o,LoadInteger(S));
125 break;
126 case LUA_TSHRSTR: case LUA_TLNGSTR:
127 setsvalue2n(S->L,o,LoadString(S));
128 break;
129 default: lua_assert(0);
130 } 92 }
131 }
132 n=LoadInt(S);
133 f->p=luaM_newvector(S->L,n,Proto*);
134 f->sizep=n;
135 for (i=0; i<n; i++) f->p[i]=NULL;
136 for (i=0; i<n; i++)
137 {
138 f->p[i]=luaF_newproto(S->L);
139 LoadFunction(S,f->p[i]);
140 }
141}
142
143static void LoadUpvalues(LoadState* S, Proto* f)
144{
145 int i,n;
146 n=LoadInt(S);
147 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
148 f->sizeupvalues=n;
149 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
150 for (i=0; i<n; i++)
151 {
152 f->upvalues[i].instack=LoadByte(S);
153 f->upvalues[i].idx=LoadByte(S);
154 }
155}
156
157static void LoadDebug(LoadState* S, Proto* f)
158{
159 int i,n;
160 f->source=LoadString(S);
161 n=LoadInt(S);
162 f->lineinfo=luaM_newvector(S->L,n,int);
163 f->sizelineinfo=n;
164 LoadVector(S,f->lineinfo,n);
165 n=LoadInt(S);
166 f->locvars=luaM_newvector(S->L,n,LocVar);
167 f->sizelocvars=n;
168 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
169 for (i=0; i<n; i++)
170 {
171 f->locvars[i].varname=LoadString(S);
172 f->locvars[i].startpc=LoadInt(S);
173 f->locvars[i].endpc=LoadInt(S);
174 }
175 n=LoadInt(S);
176 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
177}
178
179static void LoadFunction(LoadState* S, Proto* f)
180{
181 f->linedefined=LoadInt(S);
182 f->lastlinedefined=LoadInt(S);
183 f->numparams=LoadByte(S);
184 f->is_vararg=LoadByte(S);
185 f->maxstacksize=LoadByte(S);
186 LoadCode(S,f);
187 LoadConstants(S,f);
188 LoadUpvalues(S,f);
189 LoadDebug(S,f);
190}
191
192static void checkstring(LoadState *S, const char *s, const char *msg)
193{
194 char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
195 LoadVector(S,buff,strlen(s)+1);
196 if (strcmp(s,buff)!=0) error(S,msg);
197}
198
199static void fchecksize(LoadState *S, size_t size, const char *tname)
200{
201 if (LoadByte(S) != size)
202 error(S,luaO_pushfstring(S->L,"%s size mismatch in",tname));
203} 93}
204 94
95
96static void LoadCode (LoadState *S, Proto *f) {
97 int n = LoadInt(S);
98 f->code = luaM_newvector(S->L, n, Instruction);
99 f->sizecode = n;
100 LoadVector(S, f->code, n);
101}
102
103
104static void LoadFunction(LoadState *S, Proto *f);
105
106
107static void LoadConstants (LoadState *S, Proto *f) {
108 int i, n;
109 n = LoadInt(S);
110 f->k = luaM_newvector(S->L, n, TValue);
111 f->sizek = n;
112 for (i = 0; i < n; i++)
113 setnilvalue(&f->k[i]);
114 for (i = 0; i < n; i++) {
115 TValue *o = &f->k[i];
116 int t = LoadByte(S);
117 switch (t) {
118 case LUA_TNIL:
119 setnilvalue(o);
120 break;
121 case LUA_TBOOLEAN:
122 setbvalue(o, LoadByte(S));
123 break;
124 case LUA_TNUMFLT:
125 setnvalue(o, LoadNumber(S));
126 break;
127 case LUA_TNUMINT:
128 setivalue(o, LoadInteger(S));
129 break;
130 case LUA_TSHRSTR:
131 case LUA_TLNGSTR:
132 setsvalue2n(S->L, o, LoadString(S));
133 break;
134 default:
135 lua_assert(0);
136 }
137 }
138 n = LoadInt(S);
139 f->p = luaM_newvector(S->L, n, Proto *);
140 f->sizep = n;
141 for (i = 0; i < n; i++)
142 f->p[i] = NULL;
143 for (i = 0; i < n; i++) {
144 f->p[i] = luaF_newproto(S->L);
145 LoadFunction(S, f->p[i]);
146 }
147}
148
149
150static void LoadUpvalues (LoadState *S, Proto *f) {
151 int i, n;
152 n = LoadInt(S);
153 f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
154 f->sizeupvalues = n;
155 for (i = 0; i < n; i++)
156 f->upvalues[i].name = NULL;
157 for (i = 0; i < n; i++) {
158 f->upvalues[i].instack = LoadByte(S);
159 f->upvalues[i].idx = LoadByte(S);
160 }
161}
162
163
164static void LoadDebug (LoadState *S, Proto *f) {
165 int i, n;
166 f->source = LoadString(S);
167 n = LoadInt(S);
168 f->lineinfo = luaM_newvector(S->L, n, int);
169 f->sizelineinfo = n;
170 LoadVector(S, f->lineinfo, n);
171 n = LoadInt(S);
172 f->locvars = luaM_newvector(S->L, n, LocVar);
173 f->sizelocvars = n;
174 for (i = 0; i < n; i++)
175 f->locvars[i].varname = NULL;
176 for (i = 0; i < n; i++) {
177 f->locvars[i].varname = LoadString(S);
178 f->locvars[i].startpc = LoadInt(S);
179 f->locvars[i].endpc = LoadInt(S);
180 }
181 n = LoadInt(S);
182 for (i = 0; i < n; i++)
183 f->upvalues[i].name = LoadString(S);
184}
185
186
187static void LoadFunction (LoadState *S, Proto *f) {
188 f->linedefined = LoadInt(S);
189 f->lastlinedefined = LoadInt(S);
190 f->numparams = LoadByte(S);
191 f->is_vararg = LoadByte(S);
192 f->maxstacksize = LoadByte(S);
193 LoadCode(S, f);
194 LoadConstants(S, f);
195 LoadUpvalues(S, f);
196 LoadDebug(S, f);
197}
198
199
200static void checkstring (LoadState *S, const char *s, const char *msg) {
201 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than each */
202 LoadVector(S, buff, strlen(s) + 1);
203 if (strcmp(s, buff) != 0)
204 error(S, msg);
205}
206
207
208static void fchecksize (LoadState *S, size_t size, const char *tname) {
209 if (LoadByte(S) != size)
210 error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
211}
212
213
205#define checksize(S,t) fchecksize(S,sizeof(t),#t) 214#define checksize(S,t) fchecksize(S,sizeof(t),#t)
206 215
207static void checkHeader(LoadState* S) 216static void checkHeader (LoadState *S) {
208{ 217 checkstring(S, LUA_SIGNATURE + 1, "not a");
209 checkstring(S,LUA_SIGNATURE+1,"not a"); 218 checkstring(S, LUAC_DATA, "corrupted");
210 checkstring(S,LUAC_DATA,"corrupted"); 219 if (LoadByte(S) != LUAC_VERSION)
211 if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in"); 220 error(S, "version mismatch in");
212 if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in"); 221 if (LoadByte(S) != LUAC_FORMAT)
213 checksize(S,int); 222 error(S, "format mismatch in");
214 checksize(S,size_t); 223 checksize(S, int);
215 checksize(S,Instruction); 224 checksize(S, size_t);
216 checksize(S,lua_Integer); 225 checksize(S, Instruction);
217 checksize(S,lua_Number); 226 checksize(S, lua_Integer);
218 if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in"); 227 checksize(S, lua_Number);
219 if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in"); 228 if (LoadInteger(S) != LUAC_INT)
229 error(S, "endianess mismatch in");
230 if (LoadNumber(S) != LUAC_NUM)
231 error(S, "float format mismatch in");
220} 232}
221 233
234
222/* 235/*
223** load precompiled chunk 236** load precompiled chunk
224*/ 237*/
225Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 238Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
226{ 239 const char *name) {
227 LoadState S; 240 LoadState S;
228 Closure* cl; 241 Closure *cl;
229 if (*name=='@' || *name=='=') 242 if (*name == '@' || *name == '=')
230 S.name=name+1; 243 S.name = name + 1;
231 else if (*name==LUA_SIGNATURE[0]) 244 else if (*name == LUA_SIGNATURE[0])
232 S.name="binary string"; 245 S.name = "binary string";
233 else 246 else
234 S.name=name; 247 S.name = name;
235 S.L=L; 248 S.L = L;
236 S.Z=Z; 249 S.Z = Z;
237 S.b=buff; 250 S.b = buff;
238 checkHeader(&S); 251 checkHeader(&S);
239 cl=luaF_newLclosure(L,LoadByte(&S)); 252 cl = luaF_newLclosure(L, LoadByte(&S));
240 setclLvalue(L,L->top,cl); incr_top(L); 253 setclLvalue(L, L->top, cl);
241 cl->l.p=luaF_newproto(L); 254 incr_top(L);
242 LoadFunction(&S,cl->l.p); 255 cl->l.p = luaF_newproto(L);
243 lua_assert(cl->l.nupvalues==cl->l.p->sizeupvalues); 256 LoadFunction(&S, cl->l.p);
244 luai_verifycode(L,buff,cl->l.p); 257 lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
245 return cl; 258 luai_verifycode(L, buff, cl->l.p);
259 return cl;
246} 260}
261