1998-01-14 23:49:01 +08:00
|
|
|
/*
|
2018-08-24 01:26:12 +08:00
|
|
|
** $Id: lundump.c $
|
2005-11-16 19:55:07 +08:00
|
|
|
** load precompiled Lua chunks
|
1998-01-14 23:49:01 +08:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2002-12-05 01:38:31 +08:00
|
|
|
#define lundump_c
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
2002-12-05 01:38:31 +08:00
|
|
|
|
2014-11-03 03:19:04 +08:00
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
2019-03-20 02:31:08 +08:00
|
|
|
#include <limits.h>
|
2014-11-03 03:19:04 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-03-26 22:31:49 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2001-07-06 04:29:15 +08:00
|
|
|
#include "ldebug.h"
|
2003-08-28 05:01:44 +08:00
|
|
|
#include "ldo.h"
|
1998-01-14 23:49:01 +08:00
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lmem.h"
|
2005-11-16 19:55:07 +08:00
|
|
|
#include "lobject.h"
|
1998-01-14 23:49:01 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "lundump.h"
|
2002-06-06 01:26:23 +08:00
|
|
|
#include "lzio.h"
|
1998-01-14 23:49:01 +08:00
|
|
|
|
|
|
|
|
2011-05-17 20:42:43 +08:00
|
|
|
#if !defined(luai_verifycode)
|
2014-03-11 01:56:32 +08:00
|
|
|
#define luai_verifycode(L,b,f) /* empty */
|
2011-05-17 20:42:43 +08:00
|
|
|
#endif
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
lua_State *L;
|
|
|
|
ZIO *Z;
|
|
|
|
const char *name;
|
|
|
|
} LoadState;
|
|
|
|
|
|
|
|
|
2018-06-02 00:45:58 +08:00
|
|
|
static l_noret error (LoadState *S, const char *why) {
|
2019-03-20 02:31:08 +08:00
|
|
|
luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
|
2014-03-11 01:56:32 +08:00
|
|
|
luaD_throw(S->L, LUA_ERRSYNTAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-12 02:56:27 +08:00
|
|
|
/*
|
|
|
|
** All high-level loads go through LoadVector; you can change it to
|
2014-03-27 23:58:05 +08:00
|
|
|
** adapt to the endianness of the input
|
2014-03-12 02:56:27 +08:00
|
|
|
*/
|
|
|
|
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
static void LoadBlock (LoadState *S, void *b, size_t size) {
|
|
|
|
if (luaZ_read(S->Z, b, size) != 0)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "truncated chunk");
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-12 02:56:27 +08:00
|
|
|
#define LoadVar(S,x) LoadVector(S,&x,1)
|
|
|
|
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
static lu_byte LoadByte (LoadState *S) {
|
2017-06-27 22:21:12 +08:00
|
|
|
int b = zgetc(S->Z);
|
|
|
|
if (b == EOZ)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "truncated chunk");
|
2017-06-27 22:21:12 +08:00
|
|
|
return cast_byte(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 02:31:08 +08:00
|
|
|
static size_t LoadUnsigned (LoadState *S, size_t limit) {
|
2017-06-27 22:21:12 +08:00
|
|
|
size_t x = 0;
|
|
|
|
int b;
|
2019-03-20 02:31:08 +08:00
|
|
|
limit >>= 7;
|
2017-06-27 22:21:12 +08:00
|
|
|
do {
|
|
|
|
b = LoadByte(S);
|
2019-03-20 02:31:08 +08:00
|
|
|
if (x >= limit)
|
|
|
|
error(S, "integer overflow");
|
2017-06-27 22:21:12 +08:00
|
|
|
x = (x << 7) | (b & 0x7f);
|
|
|
|
} while ((b & 0x80) == 0);
|
2014-03-11 01:56:32 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 02:31:08 +08:00
|
|
|
static size_t LoadSize (LoadState *S) {
|
|
|
|
return LoadUnsigned(S, ~(size_t)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
static int LoadInt (LoadState *S) {
|
2019-03-20 02:31:08 +08:00
|
|
|
return cast_int(LoadUnsigned(S, INT_MAX));
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 00:45:58 +08:00
|
|
|
/*
|
|
|
|
** Load a nullable string
|
|
|
|
*/
|
|
|
|
static TString *LoadStringN (LoadState *S) {
|
2017-06-27 22:21:12 +08:00
|
|
|
size_t size = LoadSize(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
if (size == 0)
|
|
|
|
return NULL;
|
2015-09-08 23:41:05 +08: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);
|
2015-09-17 23:51:05 +08:00
|
|
|
LoadVector(S, getstr(ts), size); /* load directly in final place */
|
2015-09-08 23:41:05 +08:00
|
|
|
return ts;
|
2001-07-06 04:29:15 +08:00
|
|
|
}
|
2014-02-28 00:56:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
|
2018-06-02 00:45:58 +08:00
|
|
|
/*
|
|
|
|
** Load a non-nullable string.
|
|
|
|
*/
|
|
|
|
static TString *LoadString (LoadState *S) {
|
|
|
|
TString *st = LoadStringN(S);
|
|
|
|
if (st == NULL)
|
|
|
|
error(S, "bad format for constant string");
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
static void LoadCode (LoadState *S, Proto *f) {
|
|
|
|
int n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
2014-03-11 01:56:32 +08:00
|
|
|
f->sizecode = n;
|
|
|
|
LoadVector(S, f->code, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 02:35:43 +08:00
|
|
|
static void LoadFunction(LoadState *S, Proto *f, TString *psource);
|
2014-03-11 01:56:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void LoadConstants (LoadState *S, Proto *f) {
|
2014-06-19 02:35:43 +08:00
|
|
|
int i;
|
|
|
|
int n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->k = luaM_newvectorchecked(S->L, n, TValue);
|
2014-03-11 01:56:32 +08:00
|
|
|
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) {
|
2017-11-28 19:19:07 +08:00
|
|
|
case LUA_TNIL:
|
|
|
|
setnilvalue(o);
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
setbvalue(o, LoadByte(S));
|
|
|
|
break;
|
|
|
|
case LUA_TNUMFLT:
|
|
|
|
setfltvalue(o, LoadNumber(S));
|
|
|
|
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-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 02:35:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadProtos (LoadState *S, Proto *f) {
|
|
|
|
int i;
|
|
|
|
int n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->p = luaM_newvectorchecked(S->L, n, Proto *);
|
2014-03-11 01:56:32 +08:00
|
|
|
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-19 02:35:43 +08:00
|
|
|
LoadFunction(S, f->p[i], f->source);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadUpvalues (LoadState *S, Proto *f) {
|
|
|
|
int i, n;
|
|
|
|
n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
2014-03-11 01:56:32 +08:00
|
|
|
f->sizeupvalues = n;
|
|
|
|
for (i = 0; i < n; i++) {
|
2019-07-12 22:38:42 +08:00
|
|
|
f->upvalues[i].name = NULL;
|
2014-03-11 01:56:32 +08:00
|
|
|
f->upvalues[i].instack = LoadByte(S);
|
|
|
|
f->upvalues[i].idx = LoadByte(S);
|
2019-07-12 22:38:42 +08:00
|
|
|
f->upvalues[i].kind = LoadByte(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LoadDebug (LoadState *S, Proto *f) {
|
|
|
|
int i, n;
|
|
|
|
n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
2014-03-11 01:56:32 +08:00
|
|
|
f->sizelineinfo = n;
|
|
|
|
LoadVector(S, f->lineinfo, n);
|
|
|
|
n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
2017-06-27 19:35:31 +08:00
|
|
|
f->sizeabslineinfo = n;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
f->abslineinfo[i].pc = LoadInt(S);
|
|
|
|
f->abslineinfo[i].line = LoadInt(S);
|
|
|
|
}
|
|
|
|
n = LoadInt(S);
|
2017-12-08 02:59:52 +08:00
|
|
|
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
2014-03-11 01:56:32 +08:00
|
|
|
f->sizelocvars = n;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
f->locvars[i].varname = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-06-02 00:45:58 +08:00
|
|
|
f->locvars[i].varname = LoadStringN(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
f->locvars[i].startpc = LoadInt(S);
|
|
|
|
f->locvars[i].endpc = LoadInt(S);
|
|
|
|
}
|
|
|
|
n = LoadInt(S);
|
|
|
|
for (i = 0; i < n; i++)
|
2018-06-02 00:45:58 +08:00
|
|
|
f->upvalues[i].name = LoadStringN(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 02:35:43 +08:00
|
|
|
static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
|
2018-06-02 00:45:58 +08:00
|
|
|
f->source = LoadStringN(S);
|
2014-06-19 02:35:43 +08:00
|
|
|
if (f->source == NULL) /* no source in dump? */
|
|
|
|
f->source = psource; /* reuse parent's source */
|
2014-03-11 01:56:32 +08: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-19 02:35:43 +08:00
|
|
|
LoadProtos(S, f);
|
2014-03-11 01:56:32 +08:00
|
|
|
LoadDebug(S, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-12 02:05:46 +08: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 22:39:55 +08:00
|
|
|
size_t len = strlen(s);
|
2014-03-12 02:05:46 +08:00
|
|
|
LoadVector(S, buff, len);
|
|
|
|
if (memcmp(s, buff, len) != 0)
|
2014-03-11 01:56:32 +08:00
|
|
|
error(S, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
|
|
|
if (LoadByte(S) != size)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-28 00:56:20 +08:00
|
|
|
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
|
2011-05-17 20:42:43 +08:00
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
static void checkHeader (LoadState *S) {
|
2019-04-18 01:57:29 +08:00
|
|
|
/* skip 1st char (already read and checked) */
|
|
|
|
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
|
2019-03-20 02:31:08 +08:00
|
|
|
if (LoadInt(S) != LUAC_VERSION)
|
|
|
|
error(S, "version mismatch");
|
2014-03-11 01:56:32 +08:00
|
|
|
if (LoadByte(S) != LUAC_FORMAT)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "format mismatch");
|
|
|
|
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
2014-03-11 01:56:32 +08:00
|
|
|
checksize(S, Instruction);
|
|
|
|
checksize(S, lua_Integer);
|
|
|
|
checksize(S, lua_Number);
|
|
|
|
if (LoadInteger(S) != LUAC_INT)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "integer format mismatch");
|
2014-03-11 01:56:32 +08:00
|
|
|
if (LoadNumber(S) != LUAC_NUM)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "float format mismatch");
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:56:32 +08:00
|
|
|
|
1998-01-14 23:49:01 +08:00
|
|
|
/*
|
2002-10-26 05:30:41 +08:00
|
|
|
** load precompiled chunk
|
1998-01-14 23:49:01 +08:00
|
|
|
*/
|
2015-09-08 23:41:05 +08:00
|
|
|
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
2014-03-11 01:56:32 +08:00
|
|
|
LoadState S;
|
2014-06-20 02:27:20 +08:00
|
|
|
LClosure *cl;
|
2014-03-11 01:56:32 +08: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));
|
2017-06-29 23:06:44 +08:00
|
|
|
setclLvalue2s(L, L->top, cl);
|
2015-11-03 00:09:30 +08:00
|
|
|
luaD_inctop(L);
|
2014-06-20 02:27:20 +08: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-11 01:56:32 +08:00
|
|
|
return cl;
|
2000-09-05 02:53:41 +08:00
|
|
|
}
|
2014-03-11 01:56:32 +08:00
|
|
|
|