2 ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
27 #if !defined(luai_verifycode)
28 #define luai_verifycode(L,b,f) /* empty */
39 static l_noret error(LoadState *S, const char *why) {
40 luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
41 luaD_throw(S->L, LUA_ERRSYNTAX);
46 ** All high-level loads go through LoadVector; you can change it to
47 ** adapt to the endianness of the input
49 #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
51 static void LoadBlock (LoadState *S, void *b, size_t size) {
52 if (luaZ_read(S->Z, b, size) != 0)
53 error(S, "truncated");
57 #define LoadVar(S,x) LoadVector(S,&x,1)
60 static lu_byte LoadByte (LoadState *S) {
67 static int LoadInt (LoadState *S) {
74 static lua_Number LoadNumber (LoadState *S) {
81 static lua_Integer LoadInteger (LoadState *S) {
88 static TString *LoadString (LoadState *S, Proto *p) {
90 size_t size = LoadByte(S);
96 else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
97 char buff[LUAI_MAXSHORTLEN];
98 LoadVector(S, buff, size);
99 ts = luaS_newlstr(L, buff, size);
101 else { /* long string */
102 ts = luaS_createlngstrobj(L, size);
103 setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */
105 LoadVector(S, getstr(ts), size); /* load directly in final place */
106 L->top--; /* pop string */
108 luaC_objbarrier(L, p, ts);
113 static void LoadCode (LoadState *S, Proto *f) {
115 f->code = luaM_newvector(S->L, n, Instruction);
117 LoadVector(S, f->code, n);
121 static void LoadFunction(LoadState *S, Proto *f, TString *psource);
124 static void LoadConstants (LoadState *S, Proto *f) {
127 f->k = luaM_newvector(S->L, n, TValue);
129 for (i = 0; i < n; i++)
130 setnilvalue(&f->k[i]);
131 for (i = 0; i < n; i++) {
132 TValue *o = &f->k[i];
139 setbvalue(o, LoadByte(S));
142 setfltvalue(o, LoadNumber(S));
145 setivalue(o, LoadInteger(S));
149 setsvalue2n(S->L, o, LoadString(S, f));
158 static void LoadProtos (LoadState *S, Proto *f) {
161 f->p = luaM_newvector(S->L, n, Proto *);
163 for (i = 0; i < n; i++)
165 for (i = 0; i < n; i++) {
166 f->p[i] = luaF_newproto(S->L);
167 luaC_objbarrier(S->L, f, f->p[i]);
168 LoadFunction(S, f->p[i], f->source);
173 static void LoadUpvalues (LoadState *S, Proto *f) {
176 f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
178 for (i = 0; i < n; i++)
179 f->upvalues[i].name = NULL;
180 for (i = 0; i < n; i++) {
181 f->upvalues[i].instack = LoadByte(S);
182 f->upvalues[i].idx = LoadByte(S);
187 static void LoadDebug (LoadState *S, Proto *f) {
190 f->lineinfo = luaM_newvector(S->L, n, int);
192 LoadVector(S, f->lineinfo, n);
194 f->locvars = luaM_newvector(S->L, n, LocVar);
196 for (i = 0; i < n; i++)
197 f->locvars[i].varname = NULL;
198 for (i = 0; i < n; i++) {
199 f->locvars[i].varname = LoadString(S, f);
200 f->locvars[i].startpc = LoadInt(S);
201 f->locvars[i].endpc = LoadInt(S);
204 for (i = 0; i < n; i++)
205 f->upvalues[i].name = LoadString(S, f);
209 static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
210 f->source = LoadString(S, f);
211 if (f->source == NULL) /* no source in dump? */
212 f->source = psource; /* reuse parent's source */
213 f->linedefined = LoadInt(S);
214 f->lastlinedefined = LoadInt(S);
215 f->numparams = LoadByte(S);
216 f->is_vararg = LoadByte(S);
217 f->maxstacksize = LoadByte(S);
226 static void checkliteral (LoadState *S, const char *s, const char *msg) {
227 char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
228 size_t len = strlen(s);
229 LoadVector(S, buff, len);
230 if (memcmp(s, buff, len) != 0)
235 static void fchecksize (LoadState *S, size_t size, const char *tname) {
236 if (LoadByte(S) != size)
237 error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
241 #define checksize(S,t) fchecksize(S,sizeof(t),#t)
243 static void checkHeader (LoadState *S) {
244 checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
245 if (LoadByte(S) != LUAC_VERSION)
246 error(S, "version mismatch in");
247 if (LoadByte(S) != LUAC_FORMAT)
248 error(S, "format mismatch in");
249 checkliteral(S, LUAC_DATA, "corrupted");
251 checksize(S, size_t);
252 checksize(S, Instruction);
253 checksize(S, lua_Integer);
254 checksize(S, lua_Number);
255 if (LoadInteger(S) != LUAC_INT)
256 error(S, "endianness mismatch in");
257 if (LoadNumber(S) != LUAC_NUM)
258 error(S, "float format mismatch in");
263 ** load precompiled chunk
265 LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
268 if (*name == '@' || *name == '=')
270 else if (*name == LUA_SIGNATURE[0])
271 S.name = "binary string";
277 cl = luaF_newLclosure(L, LoadByte(&S));
278 setclLvalue(L, L->top, cl);
280 cl->p = luaF_newproto(L);
281 luaC_objbarrier(L, cl, cl->p);
282 LoadFunction(&S, cl->p, NULL);
283 lua_assert(cl->nupvalues == cl->p->sizeupvalues);
284 luai_verifycode(L, buff, cl->p);