1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00
lua/lundump.c

247 lines
5.0 KiB
C
Raw Normal View History

1998-01-14 13:49:01 -02:00
/*
** $Id: lundump.c,v 2.29 2014/02/28 16:13:01 roberto Exp roberto $
2005-11-16 09:55:07 -02:00
** load precompiled Lua chunks
1998-01-14 13:49:01 -02:00
** See Copyright Notice in lua.h
*/
2005-11-16 09:55:07 -02:00
#include <string.h>
2002-12-04 15:38:31 -02:00
#define lundump_c
#define LUA_CORE
2002-12-04 15:38:31 -02:00
2001-03-26 11:31:49 -03:00
#include "lua.h"
2001-07-05 17:29:15 -03:00
#include "ldebug.h"
2003-08-27 18:01:44 -03:00
#include "ldo.h"
1998-01-14 13:49:01 -02:00
#include "lfunc.h"
#include "lmem.h"
2005-11-16 09:55:07 -02:00
#include "lobject.h"
1998-01-14 13:49:01 -02:00
#include "lstring.h"
#include "lundump.h"
2002-06-05 14:26:23 -03:00
#include "lzio.h"
1998-01-14 13:49:01 -02:00
typedef struct {
lua_State* L;
ZIO* Z;
2002-10-25 18:30:41 -03:00
Mbuffer* b;
const char* name;
} LoadState;
1998-03-26 11:50:19 -03:00
2012-03-19 19:58:09 -03:00
static l_noret error(LoadState* S, const char* why)
1998-01-14 13:49:01 -02:00
{
2010-10-25 12:33:38 -02:00
luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
2005-11-16 09:55:07 -02:00
luaD_throw(S->L,LUA_ERRSYNTAX);
1998-01-14 13:49:01 -02:00
}
#define LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
1998-01-14 13:49:01 -02:00
#if !defined(luai_verifycode)
#define luai_verifycode(L,b,f) /* empty */
#endif
2005-11-16 09:55:07 -02:00
static void LoadBlock(LoadState* S, void* b, size_t size)
1998-01-14 13:49:01 -02:00
{
2011-11-24 11:25:41 -02:00
if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
1998-01-14 13:49:01 -02:00
}
static lu_byte LoadByte(LoadState* S)
1998-01-14 13:49:01 -02:00
{
lu_byte x;
2005-11-16 09:55:07 -02:00
LoadVar(S,x);
2000-09-04 15:53:41 -03:00
return x;
1998-01-14 13:49:01 -02:00
}
2005-11-16 09:55:07 -02:00
static int LoadInt(LoadState* S)
{
2005-11-16 09:55:07 -02:00
int x;
LoadVar(S,x);
if (x<0) error(S,"corrupted");
2000-09-04 15:53:41 -03:00
return x;
1998-01-14 13:49:01 -02:00
}
2005-11-16 09:55:07 -02:00
static lua_Number LoadNumber(LoadState* S)
1999-03-30 17:29:34 -03:00
{
lua_Number x;
2005-11-16 09:55:07 -02:00
LoadVar(S,x);
2000-09-04 15:53:41 -03:00
return x;
1999-03-30 17:29:34 -03:00
}
2013-04-26 15:48:35 -03:00
static lua_Integer LoadInteger(LoadState* S)
{
lua_Integer x;
LoadVar(S,x);
return x;
}
2005-11-16 09:55:07 -02:00
static TString* LoadString(LoadState* S)
1998-01-14 13:49:01 -02:00
{
size_t size = LoadByte(S);
if (size == 0xFF) LoadVar(S,size);
1998-03-26 11:50:19 -03:00
if (size==0)
return NULL;
1998-01-14 13:49:01 -02:00
else
{
char* s=luaZ_openspace(S->L,S->b,--size);
LoadVector(S,s,size);
return luaS_newlstr(S->L,s,size);
1998-01-14 13:49:01 -02:00
}
}
2005-11-16 09:55:07 -02:00
static void LoadCode(LoadState* S, Proto* f)
2000-04-25 13:44:31 -03:00
{
2005-11-16 09:55:07 -02:00
int n=LoadInt(S);
f->code=luaM_newvector(S->L,n,Instruction);
f->sizecode=n;
LoadVector(S,f->code,n);
2000-04-25 13:44:31 -03:00
}
static void LoadFunction(LoadState* S, Proto* f);
1998-01-14 13:49:01 -02:00
2005-11-16 09:55:07 -02:00
static void LoadConstants(LoadState* S, Proto* f)
2003-01-27 13:52:57 -02:00
{
int i,n;
2003-01-27 13:52:57 -02:00
n=LoadInt(S);
2005-11-16 09:55:07 -02:00
f->k=luaM_newvector(S->L,n,TValue);
2001-07-05 17:29:15 -03:00
f->sizek=n;
2003-08-27 18:01:44 -03:00
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
2000-09-04 15:53:41 -03:00
for (i=0; i<n; i++)
2001-07-05 17:29:15 -03:00
{
TValue* o=&f->k[i];
int t=LoadByte(S);
switch (t)
2001-07-05 17:29:15 -03:00
{
2002-06-05 14:26:23 -03:00
case LUA_TNIL:
2006-09-11 11:07:24 -03:00
setnilvalue(o);
2002-06-05 14:26:23 -03:00
break;
case LUA_TBOOLEAN:
setbvalue(o,LoadByte(S));
2005-11-16 09:55:07 -02:00
break;
2013-04-26 15:48:35 -03:00
case LUA_TNUMFLT:
2005-11-16 09:55:07 -02:00
setnvalue(o,LoadNumber(S));
break;
2013-04-26 15:48:35 -03:00
case LUA_TNUMINT:
setivalue(o,LoadInteger(S));
break;
case LUA_TSHRSTR: case LUA_TLNGSTR:
2005-11-16 09:55:07 -02:00
setsvalue2n(S->L,o,LoadString(S));
break;
default: lua_assert(0);
2001-07-05 17:29:15 -03:00
}
}
n=LoadInt(S);
2005-11-16 09:55:07 -02:00
f->p=luaM_newvector(S->L,n,Proto*);
2001-07-05 17:29:15 -03:00
f->sizep=n;
2003-08-27 18:01:44 -03:00
for (i=0; i<n; i++) f->p[i]=NULL;
for (i=0; i<n; i++)
{
f->p[i]=luaF_newproto(S->L);
LoadFunction(S,f->p[i]);
}
2000-09-04 15:53:41 -03: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++)
{
2011-12-07 16:03:47 -02:00
f->upvalues[i].instack=LoadByte(S);
f->upvalues[i].idx=LoadByte(S);
}
}
2005-11-16 09:55:07 -02:00
static void LoadDebug(LoadState* S, Proto* f)
{
int i,n;
2010-10-25 12:33:38 -02:00
f->source=LoadString(S);
2005-11-16 09:55:07 -02:00
n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int);
f->sizelineinfo=n;
LoadVector(S,f->lineinfo,n);
2005-11-16 09:55:07 -02:00
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);
2005-11-16 09:55:07 -02:00
}
static void LoadFunction(LoadState* S, Proto* f)
1998-01-14 13:49:01 -02: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);
2005-11-16 09:55:07 -02:00
LoadConstants(S,f);
LoadUpvalues(S,f);
2005-11-16 09:55:07 -02:00
LoadDebug(S,f);
1998-01-14 13:49:01 -02:00
}
static void checkstring(LoadState *S, const char *s, const char *msg)
{
char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
LoadVector(S,buff,strlen(s)+1);
if (strcmp(s,buff)!=0) 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));
}
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
static void checkHeader(LoadState* S)
1998-01-14 13:49:01 -02:00
{
checkstring(S,LUA_SIGNATURE+1,"not a");
checkstring(S,LUAC_DATA,"corrupted");
if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in");
if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in");
checksize(S,int);
checksize(S,size_t);
checksize(S,Instruction);
checksize(S,lua_Integer);
checksize(S,lua_Number);
if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in");
if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in");
1998-01-14 13:49:01 -02:00
}
/*
2002-10-25 18:30:41 -03:00
** load precompiled chunk
1998-01-14 13:49:01 -02:00
*/
Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
1998-01-14 13:49:01 -02:00
{
LoadState S;
Closure* cl;
2005-11-16 09:55:07 -02:00
if (*name=='@' || *name=='=')
S.name=name+1;
else if (*name==LUA_SIGNATURE[0])
S.name="binary string";
else
2005-11-16 09:55:07 -02:00
S.name=name;
S.L=L;
S.Z=Z;
2002-10-25 18:30:41 -03:00
S.b=buff;
checkHeader(&S);
cl=luaF_newLclosure(L,LoadByte(&S));
setclLvalue(L,L->top,cl); incr_top(L);
cl->l.p=luaF_newproto(L);
LoadFunction(&S,cl->l.p);
lua_assert(cl->l.nupvalues==cl->l.p->sizeupvalues);
luai_verifycode(L,buff,cl->l.p);
return cl;
2000-09-04 15:53:41 -03:00
}