1998-01-14 18:49:01 +03:00
|
|
|
/*
|
2015-09-08 18:41:05 +03:00
|
|
|
** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp roberto $
|
2005-11-16 14:55:07 +03:00
|
|
|
** load precompiled Lua chunks
|
1998-01-14 18:49:01 +03:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define lundump_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-03-26 18:31:49 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2001-07-06 00:29:15 +04:00
|
|
|
#include "ldebug.h"
|
2003-08-28 01:01:44 +04:00
|
|
|
#include "ldo.h"
|
1998-01-14 18:49:01 +03:00
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lmem.h"
|
2005-11-16 14:55:07 +03:00
|
|
|
#include "lobject.h"
|
1998-01-14 18:49:01 +03:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "lundump.h"
|
2002-06-05 21:26:23 +04:00
|
|
|
#include "lzio.h"
|
1998-01-14 18:49:01 +03:00
|
|
|
|
|
|
|
|
2011-05-17 16:42:43 +04:00
|
|
|
#if !defined(luai_verifycode)
|
2014-03-10 21:56:32 +04:00
|
|
|
#define luai_verifycode(L,b,f) /* empty */
|
2011-05-17 16:42:43 +04:00
|
|
|
#endif
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
lua_State *L;
|
|
|
|
ZIO *Z;
|
|
|
|
const char *name;
|
|
|
|
} LoadState;
|
|
|
|
|
|
|
|
|
|
|
|
static l_noret error(LoadState *S, const char *why) {
|
|
|
|
luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
|
|
|
|
luaD_throw(S->L, LUA_ERRSYNTAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 22:56:27 +04:00
|
|
|
/*
|
|
|
|
** All high-level loads go through LoadVector; you can change it to
|
2014-03-27 19:58:05 +04:00
|
|
|
** adapt to the endianness of the input
|
2014-03-11 22:56:27 +04:00
|
|
|
*/
|
|
|
|
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
static void LoadBlock (LoadState *S, void *b, size_t size) {
|
|
|
|
if (luaZ_read(S->Z, b, size) != 0)
|
|
|
|
error(S, "truncated");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 22:56:27 +04:00
|
|
|
#define LoadVar(S,x) LoadVector(S,&x,1)
|
|
|
|
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
static lu_byte LoadByte (LoadState *S) {
|
|
|
|
lu_byte x;
|
|
|
|
LoadVar(S, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int LoadInt (LoadState *S) {
|
|
|
|
int x;
|
|
|
|
LoadVar(S, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lua_Number LoadNumber (LoadState *S) {
|
|
|
|
lua_Number x;
|
|
|
|
LoadVar(S, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lua_Integer LoadInteger (LoadState *S) {
|
|
|
|
lua_Integer x;
|
|
|
|
LoadVar(S, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static TString *LoadString (LoadState *S) {
|
|
|
|
size_t size = LoadByte(S);
|
|
|
|
if (size == 0xFF)
|
|
|
|
LoadVar(S, size);
|
|
|
|
if (size == 0)
|
|
|
|
return NULL;
|
2015-09-08 18:41:05 +03:00
|
|
|
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
|
|
|
char buff[LUAI_MAXSHORTLEN];
|
|
|
|
LoadVector(S, buff, size);
|
|
|
|
return luaS_newlstr(S->L, buff, size);
|
|
|
|
}
|
|
|
|
else { /* long string */
|
|
|
|
TString *ts = luaS_createlngstrobj(S->L, size);
|
|
|
|
LoadVector(S, getaddrstr(ts), size); /* load directly in final place */
|
|
|
|
return ts;
|
2001-07-06 00:29:15 +04:00
|
|
|
}
|
2014-02-27 20:56:20 +04:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
static void LoadCode (LoadState *S, Proto *f) {
|
|
|
|
int n = LoadInt(S);
|
|
|
|
f->code = luaM_newvector(S->L, n, Instruction);
|
|
|
|
f->sizecode = n;
|
|
|
|
LoadVector(S, f->code, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-18 22:35:43 +04:00
|
|
|
static void LoadFunction(LoadState *S, Proto *f, TString *psource);
|
2014-03-10 21:56:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
static void LoadConstants (LoadState *S, Proto *f) {
|
2014-06-18 22:35:43 +04:00
|
|
|
int i;
|
|
|
|
int n = LoadInt(S);
|
2014-03-10 21:56:32 +04:00
|
|
|
f->k = luaM_newvector(S->L, n, TValue);
|
|
|
|
f->sizek = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
setnilvalue(&f->k[i]);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
TValue *o = &f->k[i];
|
|
|
|
int t = LoadByte(S);
|
|
|
|
switch (t) {
|
|
|
|
case LUA_TNIL:
|
|
|
|
setnilvalue(o);
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
setbvalue(o, LoadByte(S));
|
|
|
|
break;
|
|
|
|
case LUA_TNUMFLT:
|
2014-04-29 22:14:16 +04:00
|
|
|
setfltvalue(o, LoadNumber(S));
|
2014-03-10 21:56:32 +04:00
|
|
|
break;
|
|
|
|
case LUA_TNUMINT:
|
|
|
|
setivalue(o, LoadInteger(S));
|
|
|
|
break;
|
|
|
|
case LUA_TSHRSTR:
|
|
|
|
case LUA_TLNGSTR:
|
|
|
|
setsvalue2n(S->L, o, LoadString(S));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
2014-06-18 22:35:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadProtos (LoadState *S, Proto *f) {
|
|
|
|
int i;
|
|
|
|
int n = LoadInt(S);
|
2014-03-10 21:56:32 +04:00
|
|
|
f->p = luaM_newvector(S->L, n, Proto *);
|
|
|
|
f->sizep = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
f->p[i] = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
f->p[i] = luaF_newproto(S->L);
|
2014-06-18 22:35:43 +04:00
|
|
|
LoadFunction(S, f->p[i], f->source);
|
2014-03-10 21:56:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadUpvalues (LoadState *S, Proto *f) {
|
|
|
|
int i, n;
|
|
|
|
n = LoadInt(S);
|
|
|
|
f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
|
|
|
|
f->sizeupvalues = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
f->upvalues[i].name = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
f->upvalues[i].instack = LoadByte(S);
|
|
|
|
f->upvalues[i].idx = LoadByte(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadDebug (LoadState *S, Proto *f) {
|
|
|
|
int i, n;
|
|
|
|
n = LoadInt(S);
|
|
|
|
f->lineinfo = luaM_newvector(S->L, n, int);
|
|
|
|
f->sizelineinfo = n;
|
|
|
|
LoadVector(S, f->lineinfo, n);
|
|
|
|
n = LoadInt(S);
|
|
|
|
f->locvars = luaM_newvector(S->L, n, LocVar);
|
|
|
|
f->sizelocvars = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
f->locvars[i].varname = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
f->locvars[i].varname = LoadString(S);
|
|
|
|
f->locvars[i].startpc = LoadInt(S);
|
|
|
|
f->locvars[i].endpc = LoadInt(S);
|
|
|
|
}
|
|
|
|
n = LoadInt(S);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
f->upvalues[i].name = LoadString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-18 22:35:43 +04:00
|
|
|
static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
|
|
|
|
f->source = LoadString(S);
|
|
|
|
if (f->source == NULL) /* no source in dump? */
|
|
|
|
f->source = psource; /* reuse parent's source */
|
2014-03-10 21:56:32 +04:00
|
|
|
f->linedefined = LoadInt(S);
|
|
|
|
f->lastlinedefined = LoadInt(S);
|
|
|
|
f->numparams = LoadByte(S);
|
|
|
|
f->is_vararg = LoadByte(S);
|
|
|
|
f->maxstacksize = LoadByte(S);
|
|
|
|
LoadCode(S, f);
|
|
|
|
LoadConstants(S, f);
|
|
|
|
LoadUpvalues(S, f);
|
2014-06-18 22:35:43 +04:00
|
|
|
LoadProtos(S, f);
|
2014-03-10 21:56:32 +04:00
|
|
|
LoadDebug(S, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 22:05:46 +04:00
|
|
|
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
|
|
|
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
2014-04-01 18:39:55 +04:00
|
|
|
size_t len = strlen(s);
|
2014-03-11 22:05:46 +04:00
|
|
|
LoadVector(S, buff, len);
|
|
|
|
if (memcmp(s, buff, len) != 0)
|
2014-03-10 21:56:32 +04:00
|
|
|
error(S, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
|
|
|
if (LoadByte(S) != size)
|
|
|
|
error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 20:56:20 +04:00
|
|
|
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
|
2011-05-17 16:42:43 +04:00
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
static void checkHeader (LoadState *S) {
|
2014-03-11 22:05:46 +04:00
|
|
|
checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
|
2014-03-10 21:56:32 +04:00
|
|
|
if (LoadByte(S) != LUAC_VERSION)
|
|
|
|
error(S, "version mismatch in");
|
|
|
|
if (LoadByte(S) != LUAC_FORMAT)
|
|
|
|
error(S, "format mismatch in");
|
2014-03-11 22:05:46 +04:00
|
|
|
checkliteral(S, LUAC_DATA, "corrupted");
|
2014-03-10 21:56:32 +04:00
|
|
|
checksize(S, int);
|
|
|
|
checksize(S, size_t);
|
|
|
|
checksize(S, Instruction);
|
|
|
|
checksize(S, lua_Integer);
|
|
|
|
checksize(S, lua_Number);
|
|
|
|
if (LoadInteger(S) != LUAC_INT)
|
2014-03-27 19:58:05 +04:00
|
|
|
error(S, "endianness mismatch in");
|
2014-03-10 21:56:32 +04:00
|
|
|
if (LoadNumber(S) != LUAC_NUM)
|
|
|
|
error(S, "float format mismatch in");
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:56:32 +04:00
|
|
|
|
1998-01-14 18:49:01 +03:00
|
|
|
/*
|
2002-10-26 01:30:41 +04:00
|
|
|
** load precompiled chunk
|
1998-01-14 18:49:01 +03:00
|
|
|
*/
|
2015-09-08 18:41:05 +03:00
|
|
|
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
2014-03-10 21:56:32 +04:00
|
|
|
LoadState S;
|
2014-06-19 22:27:20 +04:00
|
|
|
LClosure *cl;
|
2014-03-10 21:56:32 +04:00
|
|
|
if (*name == '@' || *name == '=')
|
|
|
|
S.name = name + 1;
|
|
|
|
else if (*name == LUA_SIGNATURE[0])
|
|
|
|
S.name = "binary string";
|
|
|
|
else
|
|
|
|
S.name = name;
|
|
|
|
S.L = L;
|
|
|
|
S.Z = Z;
|
|
|
|
checkHeader(&S);
|
|
|
|
cl = luaF_newLclosure(L, LoadByte(&S));
|
|
|
|
setclLvalue(L, L->top, cl);
|
|
|
|
incr_top(L);
|
2014-06-19 22:27:20 +04:00
|
|
|
cl->p = luaF_newproto(L);
|
|
|
|
LoadFunction(&S, cl->p, NULL);
|
|
|
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
|
|
|
luai_verifycode(L, buff, cl->p);
|
2014-03-10 21:56:32 +04:00
|
|
|
return cl;
|
2000-09-04 22:53:41 +04:00
|
|
|
}
|
2014-03-10 21:56:32 +04:00
|
|
|
|