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"
|
2022-12-16 03:44:22 +08:00
|
|
|
#include "ltable.h"
|
1998-01-14 23:49:01 +08:00
|
|
|
#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)
|
2020-06-18 22:07:27 +08:00
|
|
|
#define luai_verifycode(L,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;
|
2022-12-16 03:44:22 +08:00
|
|
|
Table *h; /* list for string reuse */
|
2023-12-28 04:42:00 +08:00
|
|
|
size_t offset; /* current position relative to beginning of dump */
|
2024-02-13 02:16:11 +08:00
|
|
|
lua_Unsigned nstr; /* number of strings in the list */
|
2023-09-06 02:30:45 +08:00
|
|
|
lu_byte fixed; /* dump is fixed in memory */
|
2014-03-11 01:56:32 +08:00
|
|
|
} 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
|
|
|
/*
|
2020-02-28 02:17:44 +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
|
|
|
*/
|
2020-02-28 02:17:44 +08:00
|
|
|
#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0]))
|
2014-03-12 02:56:27 +08:00
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadBlock (LoadState *S, void *b, size_t size) {
|
2014-03-11 01:56:32 +08:00
|
|
|
if (luaZ_read(S->Z, b, size) != 0)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "truncated chunk");
|
2023-08-26 04:40:20 +08:00
|
|
|
S->offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void loadAlign (LoadState *S, int align) {
|
|
|
|
int padding = align - (S->offset % align);
|
|
|
|
if (padding < align) { /* apd == align means no padding */
|
|
|
|
lua_Integer paddingContent;
|
|
|
|
loadBlock(S, &paddingContent, padding);
|
|
|
|
lua_assert(S->offset % align == 0);
|
|
|
|
}
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-13 02:16:11 +08:00
|
|
|
#define getaddr(S,n,t) cast(t *, getaddr_(S,(n) * sizeof(t)))
|
2023-09-06 02:30:45 +08:00
|
|
|
|
2024-02-13 02:16:11 +08:00
|
|
|
static const void *getaddr_ (LoadState *S, size_t size) {
|
2023-12-28 04:42:00 +08:00
|
|
|
const void *block = luaZ_getaddr(S->Z, size);
|
|
|
|
S->offset += size;
|
2023-09-06 02:30:45 +08:00
|
|
|
if (block == NULL)
|
|
|
|
error(S, "truncated fixed buffer");
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
#define loadVar(S,x) loadVector(S,&x,1)
|
2014-03-12 02:56:27 +08:00
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +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");
|
2023-08-26 04:40:20 +08:00
|
|
|
S->offset++;
|
2017-06-27 22:21:12 +08:00
|
|
|
return cast_byte(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-13 02:16:11 +08:00
|
|
|
static varint_t loadVarint (LoadState *S, varint_t limit) {
|
|
|
|
varint_t x = 0;
|
2017-06-27 22:21:12 +08:00
|
|
|
int b;
|
2019-03-20 02:31:08 +08:00
|
|
|
limit >>= 7;
|
2017-06-27 22:21:12 +08:00
|
|
|
do {
|
2020-02-28 02:17:44 +08:00
|
|
|
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);
|
2024-02-13 02:16:11 +08:00
|
|
|
} while ((b & 0x80) != 0);
|
2014-03-11 01:56:32 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static size_t loadSize (LoadState *S) {
|
2024-02-13 02:16:11 +08:00
|
|
|
return cast_sizet(loadVarint(S, MAX_SIZET));
|
2019-03-20 02:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static int loadInt (LoadState *S) {
|
2024-02-13 02:16:11 +08:00
|
|
|
return cast_int(loadVarint(S, INT_MAX));
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static lua_Number loadNumber (LoadState *S) {
|
2014-03-11 01:56:32 +08:00
|
|
|
lua_Number x;
|
2020-02-28 02:17:44 +08:00
|
|
|
loadVar(S, x);
|
2014-03-11 01:56:32 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static lua_Integer loadInteger (LoadState *S) {
|
2014-03-11 01:56:32 +08:00
|
|
|
lua_Integer x;
|
2020-02-28 02:17:44 +08:00
|
|
|
loadVar(S, x);
|
2014-03-11 01:56:32 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-02 00:45:58 +08:00
|
|
|
/*
|
2023-11-14 00:11:09 +08:00
|
|
|
** Load a nullable string into slot 'sl' from prototype 'p'. The
|
|
|
|
** assignment to the slot and the barrier must be performed before any
|
|
|
|
** possible GC activity, to anchor the string. (Both 'loadVector' and
|
|
|
|
** 'luaH_setint' can call the GC.)
|
2018-06-02 00:45:58 +08:00
|
|
|
*/
|
2023-11-14 00:11:09 +08:00
|
|
|
static void loadString (LoadState *S, Proto *p, TString **sl) {
|
2020-06-16 20:54:20 +08:00
|
|
|
lua_State *L = S->L;
|
|
|
|
TString *ts;
|
2022-12-16 03:44:22 +08:00
|
|
|
TValue sv;
|
2020-02-28 02:17:44 +08:00
|
|
|
size_t size = loadSize(S);
|
2023-11-14 00:11:09 +08:00
|
|
|
if (size == 0) { /* no string? */
|
2023-12-28 04:42:00 +08:00
|
|
|
lua_assert(*sl == NULL); /* must be prefilled */
|
2023-11-14 00:11:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-12-16 03:44:22 +08:00
|
|
|
else if (size == 1) { /* previously saved string? */
|
2024-02-13 02:16:11 +08:00
|
|
|
/* get its index */
|
|
|
|
lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED));
|
2023-11-08 21:02:06 +08:00
|
|
|
TValue stv;
|
2024-02-13 02:16:11 +08:00
|
|
|
luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */
|
2023-11-14 00:11:09 +08:00
|
|
|
*sl = ts = tsvalue(&stv);
|
|
|
|
luaC_objbarrier(L, p, ts);
|
2023-12-14 22:41:57 +08:00
|
|
|
return; /* do not save it again */
|
2022-12-16 03:44:22 +08:00
|
|
|
}
|
2023-11-10 23:35:48 +08:00
|
|
|
else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */
|
|
|
|
char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */
|
|
|
|
loadVector(S, buff, size + 1); /* load string into buffer */
|
2023-11-14 00:11:09 +08:00
|
|
|
*sl = ts = luaS_newlstr(L, buff, size); /* create string */
|
|
|
|
luaC_objbarrier(L, p, ts);
|
2015-09-08 23:41:05 +08:00
|
|
|
}
|
2023-11-14 00:11:09 +08:00
|
|
|
else if (S->fixed) { /* for a fixed buffer, use a fixed string */
|
|
|
|
const char *s = getaddr(S, size + 1, char); /* get content address */
|
|
|
|
*sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
|
|
|
|
luaC_objbarrier(L, p, ts);
|
|
|
|
}
|
|
|
|
else { /* create internal copy */
|
|
|
|
*sl = ts = luaS_createlngstrobj(L, size); /* create string */
|
|
|
|
luaC_objbarrier(L, p, ts);
|
2023-12-14 22:41:57 +08:00
|
|
|
loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */
|
2001-07-06 04:29:15 +08:00
|
|
|
}
|
2023-12-14 22:41:57 +08:00
|
|
|
/* add string to list of saved strings */
|
|
|
|
S->nstr++;
|
2022-12-16 03:44:22 +08:00
|
|
|
setsvalue(L, &sv, ts);
|
2024-02-13 02:16:11 +08:00
|
|
|
luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
|
2022-12-16 03:44:22 +08:00
|
|
|
luaC_objbarrierback(L, obj2gco(S->h), ts);
|
2018-06-02 00:45:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadCode (LoadState *S, Proto *f) {
|
|
|
|
int n = loadInt(S);
|
2023-08-26 04:40:20 +08:00
|
|
|
loadAlign(S, sizeof(f->code[0]));
|
2023-09-06 02:30:45 +08:00
|
|
|
if (S->fixed) {
|
|
|
|
f->code = getaddr(S, n, Instruction);
|
|
|
|
f->sizecode = n;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
|
|
|
f->sizecode = n;
|
|
|
|
loadVector(S, f->code, n);
|
|
|
|
}
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-20 22:14:52 +08:00
|
|
|
static void loadFunction(LoadState *S, Proto *f);
|
2014-03-11 01:56:32 +08:00
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadConstants (LoadState *S, Proto *f) {
|
2014-06-19 02:35:43 +08:00
|
|
|
int i;
|
2020-02-28 02:17:44 +08:00
|
|
|
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];
|
2020-02-28 02:17:44 +08:00
|
|
|
int t = loadByte(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
switch (t) {
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VNIL:
|
2017-11-28 19:19:07 +08:00
|
|
|
setnilvalue(o);
|
|
|
|
break;
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VFALSE:
|
2020-01-06 22:38:31 +08:00
|
|
|
setbfvalue(o);
|
|
|
|
break;
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VTRUE:
|
2020-01-06 22:38:31 +08:00
|
|
|
setbtvalue(o);
|
2017-11-28 19:19:07 +08:00
|
|
|
break;
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VNUMFLT:
|
2020-02-28 02:17:44 +08:00
|
|
|
setfltvalue(o, loadNumber(S));
|
2017-11-28 19:19:07 +08:00
|
|
|
break;
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VNUMINT:
|
2020-02-28 02:17:44 +08:00
|
|
|
setivalue(o, loadInteger(S));
|
2017-11-28 19:19:07 +08:00
|
|
|
break;
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VSHRSTR:
|
2023-11-14 00:11:09 +08:00
|
|
|
case LUA_VLNGSTR: {
|
|
|
|
lua_assert(f->source == NULL);
|
|
|
|
loadString(S, f, &f->source); /* use 'source' to anchor string */
|
|
|
|
if (f->source == NULL)
|
|
|
|
error(S, "bad format for constant string");
|
|
|
|
setsvalue2n(S->L, o, f->source); /* save it in the right place */
|
|
|
|
f->source = NULL;
|
2017-11-28 19:19:07 +08:00
|
|
|
break;
|
2023-11-14 00:11:09 +08:00
|
|
|
}
|
2017-11-28 19:19:07 +08:00
|
|
|
default: lua_assert(0);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 02:35:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadProtos (LoadState *S, Proto *f) {
|
2014-06-19 02:35:43 +08:00
|
|
|
int i;
|
2020-02-28 02:17:44 +08:00
|
|
|
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);
|
2020-06-16 20:54:20 +08:00
|
|
|
luaC_objbarrier(S->L, f, f->p[i]);
|
2022-12-20 22:14:52 +08:00
|
|
|
loadFunction(S, f->p[i]);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-01 02:36:26 +08:00
|
|
|
/*
|
|
|
|
** Load the upvalues for a function. The names must be filled first,
|
|
|
|
** because the filling of the other fields can raise read errors and
|
|
|
|
** the creation of the error message can call an emergency collection;
|
|
|
|
** in that case all prototypes must be consistent for the GC.
|
|
|
|
*/
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadUpvalues (LoadState *S, Proto *f) {
|
2014-03-11 01:56:32 +08:00
|
|
|
int i, n;
|
2020-02-28 02:17:44 +08:00
|
|
|
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;
|
2020-07-01 02:36:26 +08:00
|
|
|
for (i = 0; i < n; i++) /* make array valid for GC */
|
2019-07-12 22:38:42 +08:00
|
|
|
f->upvalues[i].name = NULL;
|
2020-07-01 02:36:26 +08:00
|
|
|
for (i = 0; i < n; i++) { /* following calls can raise errors */
|
2020-02-28 02:17:44 +08:00
|
|
|
f->upvalues[i].instack = loadByte(S);
|
|
|
|
f->upvalues[i].idx = loadByte(S);
|
|
|
|
f->upvalues[i].kind = loadByte(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 02:17:44 +08:00
|
|
|
static void loadDebug (LoadState *S, Proto *f) {
|
2014-03-11 01:56:32 +08:00
|
|
|
int i, n;
|
2020-02-28 02:17:44 +08:00
|
|
|
n = loadInt(S);
|
2023-09-06 02:30:45 +08:00
|
|
|
if (S->fixed) {
|
|
|
|
f->lineinfo = getaddr(S, n, ls_byte);
|
|
|
|
f->sizelineinfo = n;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
|
|
|
f->sizelineinfo = n;
|
|
|
|
loadVector(S, f->lineinfo, n);
|
|
|
|
}
|
2020-02-28 02:17:44 +08:00
|
|
|
n = loadInt(S);
|
2023-12-28 04:42:00 +08:00
|
|
|
if (n > 0) {
|
|
|
|
loadAlign(S, sizeof(int));
|
|
|
|
if (S->fixed) {
|
|
|
|
f->abslineinfo = getaddr(S, n, AbsLineInfo);
|
|
|
|
f->sizeabslineinfo = n;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
|
|
|
f->sizeabslineinfo = n;
|
|
|
|
loadVector(S, f->abslineinfo, n);
|
|
|
|
}
|
2017-06-27 19:35:31 +08:00
|
|
|
}
|
2020-02-28 02:17:44 +08:00
|
|
|
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++) {
|
2023-11-14 00:11:09 +08:00
|
|
|
loadString(S, f, &f->locvars[i].varname);
|
2020-02-28 02:17:44 +08:00
|
|
|
f->locvars[i].startpc = loadInt(S);
|
|
|
|
f->locvars[i].endpc = loadInt(S);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
2020-02-28 02:17:44 +08:00
|
|
|
n = loadInt(S);
|
2023-03-18 02:52:09 +08:00
|
|
|
if (n != 0) /* does it have debug information? */
|
|
|
|
n = f->sizeupvalues; /* must be this many */
|
2014-03-11 01:56:32 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2023-11-14 00:11:09 +08:00
|
|
|
loadString(S, f, &f->upvalues[i].name);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-20 22:14:52 +08:00
|
|
|
static void loadFunction (LoadState *S, Proto *f) {
|
2020-02-28 02:17:44 +08:00
|
|
|
f->linedefined = loadInt(S);
|
|
|
|
f->lastlinedefined = loadInt(S);
|
|
|
|
f->numparams = loadByte(S);
|
2023-09-06 02:30:45 +08:00
|
|
|
f->flag = loadByte(S) & PF_ISVARARG; /* get only the meaningful flags */
|
|
|
|
if (S->fixed)
|
|
|
|
f->flag |= PF_FIXED; /* signal that code is fixed */
|
2020-02-28 02:17:44 +08:00
|
|
|
f->maxstacksize = loadByte(S);
|
|
|
|
loadCode(S, f);
|
|
|
|
loadConstants(S, f);
|
|
|
|
loadUpvalues(S, f);
|
|
|
|
loadProtos(S, f);
|
2023-11-14 00:11:09 +08:00
|
|
|
loadString(S, f, &f->source);
|
2020-02-28 02:17:44 +08:00
|
|
|
loadDebug(S, f);
|
2014-03-11 01:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2020-02-28 02:17:44 +08:00
|
|
|
loadVector(S, buff, len);
|
2014-03-12 02:05:46 +08:00
|
|
|
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) {
|
2020-02-28 02:17:44 +08:00
|
|
|
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");
|
2020-05-07 01:19:08 +08:00
|
|
|
if (loadByte(S) != LUAC_VERSION)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "version mismatch");
|
2020-02-28 02:17:44 +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);
|
2020-02-28 02:17:44 +08:00
|
|
|
if (loadInteger(S) != LUAC_INT)
|
2019-03-20 02:31:08 +08:00
|
|
|
error(S, "integer format mismatch");
|
2020-02-28 02:17:44 +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
|
|
|
/*
|
2020-02-28 02:17:44 +08:00
|
|
|
** Load precompiled chunk.
|
1998-01-14 23:49:01 +08:00
|
|
|
*/
|
2023-09-06 02:30:45 +08:00
|
|
|
LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
|
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;
|
2023-09-06 02:30:45 +08:00
|
|
|
S.fixed = fixed;
|
2023-08-26 04:40:20 +08:00
|
|
|
S.offset = 1; /* fist byte was already read */
|
2014-03-11 01:56:32 +08:00
|
|
|
checkHeader(&S);
|
2020-02-28 02:17:44 +08:00
|
|
|
cl = luaF_newLclosure(L, loadByte(&S));
|
2022-10-29 23:06:37 +08:00
|
|
|
setclLvalue2s(L, L->top.p, cl);
|
2015-11-03 00:09:30 +08:00
|
|
|
luaD_inctop(L);
|
2022-12-16 03:44:22 +08:00
|
|
|
S.h = luaH_new(L); /* create list of saved strings */
|
|
|
|
S.nstr = 0;
|
|
|
|
sethvalue2s(L, L->top.p, S.h); /* anchor it */
|
|
|
|
luaD_inctop(L);
|
2014-06-20 02:27:20 +08:00
|
|
|
cl->p = luaF_newproto(L);
|
2020-06-16 20:54:20 +08:00
|
|
|
luaC_objbarrier(L, cl, cl->p);
|
2022-12-20 22:14:52 +08:00
|
|
|
loadFunction(&S, cl->p);
|
2014-06-20 02:27:20 +08:00
|
|
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
2020-06-18 22:07:27 +08:00
|
|
|
luai_verifycode(L, cl->p);
|
2022-12-16 03:44:22 +08:00
|
|
|
L->top.p--; /* pop table */
|
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
|
|
|
|