1998-01-14 18:49:01 +03:00
|
|
|
/*
|
2006-09-11 18:07:24 +04:00
|
|
|
** $Id: lundump.c,v 2.7 2006/02/17 15:51:03 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
|
|
|
|
*/
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
#include <string.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
|
|
|
|
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
|
|
|
|
2002-08-12 17:37:19 +04:00
|
|
|
typedef struct {
|
|
|
|
lua_State* L;
|
|
|
|
ZIO* Z;
|
2002-10-26 01:30:41 +04:00
|
|
|
Mbuffer* b;
|
2002-08-12 17:37:19 +04:00
|
|
|
const char* name;
|
|
|
|
} LoadState;
|
1998-03-26 17:50:19 +03:00
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
#ifdef LUAC_TRUST_BINARIES
|
|
|
|
#define IF(c,s)
|
|
|
|
#else
|
|
|
|
#define IF(c,s) if (c) error(S,s)
|
1998-01-14 18:49:01 +03:00
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void error(LoadState* S, const char* why)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
|
|
|
|
luaD_throw(S->L,LUA_ERRSYNTAX);
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
2005-11-16 14:55:07 +03:00
|
|
|
#endif
|
1998-01-14 18:49:01 +03:00
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
|
|
|
|
#define LoadByte(S) (lu_byte)LoadChar(S)
|
|
|
|
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
|
|
|
|
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
|
1998-01-14 18:49:01 +03:00
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void LoadBlock(LoadState* S, void* b, size_t size)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
size_t r=luaZ_read(S->Z,b,size);
|
|
|
|
IF (r!=0, "unexpected end");
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static int LoadChar(LoadState* S)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
char x;
|
|
|
|
LoadVar(S,x);
|
2000-09-04 22:53:41 +04:00
|
|
|
return x;
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static int LoadInt(LoadState* S)
|
1999-07-08 16:43:23 +04:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
int x;
|
|
|
|
LoadVar(S,x);
|
|
|
|
IF (x<0, "bad integer");
|
2000-09-04 22:53:41 +04:00
|
|
|
return x;
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static lua_Number LoadNumber(LoadState* S)
|
1999-03-31 00:29:34 +04:00
|
|
|
{
|
2000-12-04 21:33:40 +03:00
|
|
|
lua_Number x;
|
2005-11-16 14:55:07 +03:00
|
|
|
LoadVar(S,x);
|
2000-09-04 22:53:41 +04:00
|
|
|
return x;
|
1999-03-31 00:29:34 +04:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static TString* LoadString(LoadState* S)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
size_t size;
|
|
|
|
LoadVar(S,size);
|
1998-03-26 17:50:19 +03:00
|
|
|
if (size==0)
|
|
|
|
return NULL;
|
1998-01-14 18:49:01 +03:00
|
|
|
else
|
|
|
|
{
|
2002-10-26 01:30:41 +04:00
|
|
|
char* s=luaZ_openspace(S->L,S->b,size);
|
2005-11-16 14:55:07 +03:00
|
|
|
LoadBlock(S,s,size);
|
2003-04-10 21:39:41 +04:00
|
|
|
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void LoadCode(LoadState* S, Proto* f)
|
2000-04-25 20:44:31 +04:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
int n=LoadInt(S);
|
|
|
|
f->code=luaM_newvector(S->L,n,Instruction);
|
|
|
|
f->sizecode=n;
|
|
|
|
LoadVector(S,f->code,n,sizeof(Instruction));
|
2000-04-25 20:44:31 +04:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static Proto* LoadFunction(LoadState* S, TString* p);
|
1998-01-14 18:49:01 +03:00
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void LoadConstants(LoadState* S, Proto* f)
|
2003-01-27 18:52:57 +03:00
|
|
|
{
|
2003-02-11 13:46:24 +03:00
|
|
|
int i,n;
|
2003-01-27 18:52:57 +03:00
|
|
|
n=LoadInt(S);
|
2005-11-16 14:55:07 +03:00
|
|
|
f->k=luaM_newvector(S->L,n,TValue);
|
2001-07-06 00:29:15 +04:00
|
|
|
f->sizek=n;
|
2003-08-28 01:01:44 +04:00
|
|
|
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
|
2000-09-04 22:53:41 +04:00
|
|
|
for (i=0; i<n; i++)
|
2001-07-06 00:29:15 +04:00
|
|
|
{
|
2003-12-10 15:13:36 +03:00
|
|
|
TValue* o=&f->k[i];
|
2005-11-16 14:55:07 +03:00
|
|
|
int t=LoadChar(S);
|
2002-06-17 17:51:01 +04:00
|
|
|
switch (t)
|
2001-07-06 00:29:15 +04:00
|
|
|
{
|
2002-06-05 21:26:23 +04:00
|
|
|
case LUA_TNIL:
|
2006-09-11 18:07:24 +04:00
|
|
|
setnilvalue(o);
|
2002-06-05 21:26:23 +04:00
|
|
|
break;
|
2004-10-04 23:01:12 +04:00
|
|
|
case LUA_TBOOLEAN:
|
2006-09-11 18:07:24 +04:00
|
|
|
setbvalue(o,LoadChar(S));
|
2005-11-16 14:55:07 +03:00
|
|
|
break;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
setnvalue(o,LoadNumber(S));
|
|
|
|
break;
|
|
|
|
case LUA_TSTRING:
|
|
|
|
setsvalue2n(S->L,o,LoadString(S));
|
2004-10-04 23:01:12 +04:00
|
|
|
break;
|
2001-07-06 00:29:15 +04:00
|
|
|
default:
|
2005-11-16 14:55:07 +03:00
|
|
|
IF (1, "bad constant");
|
2001-07-06 00:29:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-08-12 17:37:19 +04:00
|
|
|
n=LoadInt(S);
|
2005-11-16 14:55:07 +03:00
|
|
|
f->p=luaM_newvector(S->L,n,Proto*);
|
2001-07-06 00:29:15 +04:00
|
|
|
f->sizep=n;
|
2003-08-28 01:01:44 +04:00
|
|
|
for (i=0; i<n; i++) f->p[i]=NULL;
|
2002-08-12 17:37:19 +04:00
|
|
|
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
|
2000-09-04 22:53:41 +04:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
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,sizeof(int));
|
|
|
|
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);
|
|
|
|
f->upvalues=luaM_newvector(S->L,n,TString*);
|
|
|
|
f->sizeupvalues=n;
|
|
|
|
for (i=0; i<n; i++) f->upvalues[i]=NULL;
|
|
|
|
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Proto* LoadFunction(LoadState* S, TString* p)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
Proto* f=luaF_newproto(S->L);
|
|
|
|
setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
|
2002-08-12 17:37:19 +04:00
|
|
|
f->source=LoadString(S); if (f->source==NULL) f->source=p;
|
2005-05-06 00:47:02 +04:00
|
|
|
f->linedefined=LoadInt(S);
|
|
|
|
f->lastlinedefined=LoadInt(S);
|
2003-02-11 13:46:24 +03:00
|
|
|
f->nups=LoadByte(S);
|
2002-08-12 17:37:19 +04:00
|
|
|
f->numparams=LoadByte(S);
|
|
|
|
f->is_vararg=LoadByte(S);
|
|
|
|
f->maxstacksize=LoadByte(S);
|
|
|
|
LoadCode(S,f);
|
2005-11-16 14:55:07 +03:00
|
|
|
LoadConstants(S,f);
|
|
|
|
LoadDebug(S,f);
|
|
|
|
IF (!luaG_checkcode(f), "bad code");
|
|
|
|
S->L->top--;
|
2001-07-06 00:29:15 +04:00
|
|
|
return f;
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void LoadHeader(LoadState* S)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
char h[LUAC_HEADERSIZE];
|
|
|
|
char s[LUAC_HEADERSIZE];
|
|
|
|
luaU_header(h);
|
|
|
|
LoadBlock(S,s,LUAC_HEADERSIZE);
|
|
|
|
IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
|
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
|
|
|
*/
|
2005-11-16 14:55:07 +03:00
|
|
|
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
2002-08-12 17:37:19 +04:00
|
|
|
LoadState S;
|
2005-11-16 14:55:07 +03:00
|
|
|
if (*name=='@' || *name=='=')
|
|
|
|
S.name=name+1;
|
|
|
|
else if (*name==LUA_SIGNATURE[0])
|
2002-08-12 17:37:19 +04:00
|
|
|
S.name="binary string";
|
|
|
|
else
|
2005-11-16 14:55:07 +03:00
|
|
|
S.name=name;
|
2002-08-12 17:37:19 +04:00
|
|
|
S.L=L;
|
|
|
|
S.Z=Z;
|
2002-10-26 01:30:41 +04:00
|
|
|
S.b=buff;
|
2005-11-16 14:55:07 +03:00
|
|
|
LoadHeader(&S);
|
2006-02-17 18:51:03 +03:00
|
|
|
return LoadFunction(&S,luaS_newliteral(L,"=?"));
|
2000-09-04 22:53:41 +04:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
/*
|
2005-11-16 14:55:07 +03:00
|
|
|
* make header
|
1999-12-02 22:11:51 +03:00
|
|
|
*/
|
2005-11-16 14:55:07 +03:00
|
|
|
void luaU_header (char* h)
|
1999-12-02 22:11:51 +03:00
|
|
|
{
|
2000-09-04 22:53:41 +04:00
|
|
|
int x=1;
|
2005-11-16 14:55:07 +03:00
|
|
|
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
|
|
|
|
h+=sizeof(LUA_SIGNATURE)-1;
|
|
|
|
*h++=(char)LUAC_VERSION;
|
|
|
|
*h++=(char)LUAC_FORMAT;
|
|
|
|
*h++=(char)*(char*)&x; /* endianness */
|
|
|
|
*h++=(char)sizeof(int);
|
|
|
|
*h++=(char)sizeof(size_t);
|
|
|
|
*h++=(char)sizeof(Instruction);
|
|
|
|
*h++=(char)sizeof(lua_Number);
|
|
|
|
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
|
1999-03-31 00:29:34 +04:00
|
|
|
}
|