lua/lundump.c

274 lines
5.3 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
2002-12-04 20:38:31 +03:00
** $Id: lundump.c,v 1.57 2002/11/14 16:15:53 roberto Exp roberto $
2001-07-06 00:29:15 +04:00
** load pre-compiled 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
2001-03-26 18:31:49 +04:00
#include "lua.h"
2001-07-06 00:29:15 +04:00
#include "ldebug.h"
1998-01-14 18:49:01 +03:00
#include "lfunc.h"
#include "lmem.h"
1999-03-31 00:29:34 +04:00
#include "lopcodes.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-06-05 21:26:23 +04:00
#define LoadByte (lu_byte) ezgetc
2000-04-25 20:44:31 +04:00
typedef struct {
lua_State* L;
ZIO* Z;
2002-10-26 01:30:41 +04:00
Mbuffer* b;
int swap;
const char* name;
} LoadState;
1998-03-26 17:50:19 +03:00
static void unexpectedEOZ (LoadState* S)
1998-01-14 18:49:01 +03:00
{
luaG_runerror(S->L,"unexpected end of file in %s",S->name);
1998-01-14 18:49:01 +03:00
}
static int ezgetc (LoadState* S)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(S->Z);
if (c==EOZ) unexpectedEOZ(S);
1998-01-14 18:49:01 +03:00
return c;
}
static void ezread (LoadState* S, void* b, int n)
1998-01-14 18:49:01 +03:00
{
int r=luaZ_read(S->Z,b,n);
if (r!=0) unexpectedEOZ(S);
1998-01-14 18:49:01 +03:00
}
static void LoadBlock (LoadState* S, void* b, size_t size)
1998-01-14 18:49:01 +03:00
{
if (S->swap)
2000-09-20 22:43:54 +04:00
{
2002-10-26 01:30:41 +04:00
char* p=(char*) b+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
while (n--) *p--=(char)ezgetc(S);
2000-09-20 22:43:54 +04:00
}
else
ezread(S,b,size);
1998-01-14 18:49:01 +03:00
}
static void LoadVector (LoadState* S, void* b, int m, size_t size)
1998-01-14 18:49:01 +03:00
{
if (S->swap)
2000-09-20 22:43:54 +04:00
{
2002-10-26 01:30:41 +04:00
char* q=(char*) b;
2000-09-20 22:43:54 +04:00
while (m--)
{
2002-10-26 01:30:41 +04:00
char* p=q+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
while (n--) *p--=(char)ezgetc(S);
2000-09-20 22:43:54 +04:00
q+=size;
}
}
2000-09-04 22:53:41 +04:00
else
ezread(S,b,m*size);
2000-09-20 22:43:54 +04:00
}
static int LoadInt (LoadState* S)
2000-09-20 22:43:54 +04:00
{
int x;
LoadBlock(S,&x,sizeof(x));
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
static size_t LoadSize (LoadState* S)
{
2000-09-04 22:53:41 +04:00
size_t x;
LoadBlock(S,&x,sizeof(x));
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
static lua_Number LoadNumber (LoadState* S)
1999-03-31 00:29:34 +04:00
{
lua_Number x;
LoadBlock(S,&x,sizeof(x));
2000-09-04 22:53:41 +04:00
return x;
1999-03-31 00:29:34 +04:00
}
static TString* LoadString (LoadState* S)
1998-01-14 18:49:01 +03:00
{
size_t size=LoadSize(S);
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);
ezread(S,s,size);
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
static void LoadCode (LoadState* S, Proto* f)
2000-04-25 20:44:31 +04:00
{
int size=LoadInt(S);
f->code=luaM_newvector(S->L,size,Instruction);
2001-07-06 00:29:15 +04:00
f->sizecode=size;
LoadVector(S,f->code,size,sizeof(*f->code));
2000-04-25 20:44:31 +04:00
}
static void LoadLocals (LoadState* S, Proto* f)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
int i,n;
n=LoadInt(S);
f->locvars=luaM_newvector(S->L,n,LocVar);
2001-07-06 00:29:15 +04:00
f->sizelocvars=n;
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2000-04-25 20:44:31 +04:00
{
f->locvars[i].varname=LoadString(S);
f->locvars[i].startpc=LoadInt(S);
f->locvars[i].endpc=LoadInt(S);
2000-04-25 20:44:31 +04:00
}
}
static void LoadLines (LoadState* S, Proto* f)
2000-09-04 22:53:41 +04:00
{
2002-10-26 01:30:41 +04:00
int size=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,size,int);
f->sizelineinfo=size;
LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
1998-01-14 18:49:01 +03:00
}
static Proto* LoadFunction (LoadState* S, TString* p);
1998-03-26 17:50:19 +03:00
static void LoadConstants (LoadState* S, Proto* f)
1998-01-14 18:49:01 +03:00
{
2000-03-03 17:58:26 +03:00
int i,n;
n=LoadInt(S);
f->k=luaM_newvector(S->L,n,TObject);
2001-07-06 00:29:15 +04:00
f->sizek=n;
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2001-07-06 00:29:15 +04:00
{
TObject* o=&f->k[i];
int t=LoadByte(S);
switch (t)
2001-07-06 00:29:15 +04:00
{
case LUA_TNUMBER:
setnvalue(o,LoadNumber(S));
2001-07-06 00:29:15 +04:00
break;
case LUA_TSTRING:
2002-11-14 19:15:53 +03:00
setsvalue2n(o,LoadString(S));
2001-07-06 00:29:15 +04:00
break;
2002-06-05 21:26:23 +04:00
case LUA_TNIL:
setnilvalue(o);
2002-06-05 21:26:23 +04:00
break;
2001-07-06 00:29:15 +04:00
default:
luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
2001-07-06 00:29:15 +04:00
break;
}
}
n=LoadInt(S);
f->p=luaM_newvector(S->L,n,Proto*);
2001-07-06 00:29:15 +04:00
f->sizep=n;
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
2000-09-04 22:53:41 +04:00
}
static Proto* LoadFunction (LoadState* S, TString* p)
1998-01-14 18:49:01 +03:00
{
Proto* f=luaF_newproto(S->L);
f->source=LoadString(S); if (f->source==NULL) f->source=p;
f->lineDefined=LoadInt(S);
f->nupvalues=LoadByte(S);
f->numparams=LoadByte(S);
f->is_vararg=LoadByte(S);
f->maxstacksize=LoadByte(S);
LoadLocals(S,f);
LoadLines(S,f);
LoadConstants(S,f);
LoadCode(S,f);
2001-07-06 00:29:15 +04:00
#ifndef TRUST_BINARIES
if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name);
2001-07-06 00:29:15 +04:00
#endif
return f;
1998-01-14 18:49:01 +03:00
}
static void LoadSignature (LoadState* S)
1998-01-14 18:49:01 +03:00
{
const char* s=LUA_SIGNATURE;
while (*s!=0 && ezgetc(S)==*s)
1998-01-14 18:49:01 +03:00
++s;
if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
1998-01-14 18:49:01 +03:00
}
static void TestSize (LoadState* S, int s, const char* what)
2000-09-04 22:53:41 +04:00
{
int r=LoadByte(S);
2000-09-04 22:53:41 +04:00
if (r!=s)
luaG_runerror(S->L,"virtual machine mismatch in %s: "
"size of %s is %d but read %d",S->name,what,s,r);
2000-09-04 22:53:41 +04:00
}
#define TESTSIZE(s,w) TestSize(S,s,w)
#define V(v) v/16,v%16
2000-03-03 17:58:26 +03:00
static void LoadHeader (LoadState* S)
1998-01-14 18:49:01 +03:00
{
int version;
2001-07-06 00:29:15 +04:00
lua_Number x=0,tx=TEST_NUMBER;
LoadSignature(S);
version=LoadByte(S);
1998-01-14 18:49:01 +03:00
if (version>VERSION)
luaG_runerror(S->L,"%s too new: "
2002-06-05 21:26:23 +04:00
"read version %d.%d; expected at most %d.%d",
S->name,V(version),V(VERSION));
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
luaG_runerror(S->L,"%s too old: "
2002-06-05 21:26:23 +04:00
"read version %d.%d; expected at least %d.%d",
S->name,V(version),V(VERSION0));
S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
TESTSIZE(sizeof(int),"int");
TESTSIZE(sizeof(size_t), "size_t");
2002-06-05 21:26:23 +04:00
TESTSIZE(sizeof(Instruction), "Instruction");
TESTSIZE(SIZE_OP, "OP");
TESTSIZE(SIZE_A, "A");
TESTSIZE(SIZE_B, "B");
TESTSIZE(SIZE_C, "C");
TESTSIZE(sizeof(lua_Number), "number");
x=LoadNumber(S);
2001-07-06 00:29:15 +04:00
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
2002-10-26 01:30:41 +04:00
luaG_runerror(S->L,"unknown number format in %s",S->name);
1998-01-14 18:49:01 +03:00
}
static Proto* LoadChunk (LoadState* S)
1998-01-14 18:49:01 +03:00
{
LoadHeader(S);
return LoadFunction(S,NULL);
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
*/
2002-10-26 01:30:41 +04:00
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff)
1998-01-14 18:49:01 +03:00
{
LoadState S;
const char* s=zname(Z);
if (*s=='@' || *s=='=')
S.name=s+1;
else if (*s==LUA_SIGNATURE[0])
S.name="binary string";
else
S.name=s;
S.L=L;
S.Z=Z;
2002-10-26 01:30:41 +04:00
S.b=buff;
return LoadChunk(&S);
2000-09-04 22:53:41 +04:00
}
1999-12-02 22:11:51 +03:00
/*
2000-09-04 22:53:41 +04:00
** find byte order
1999-12-02 22:11:51 +03:00
*/
2001-07-06 00:29:15 +04:00
int luaU_endianness (void)
1999-12-02 22:11:51 +03:00
{
2000-09-04 22:53:41 +04:00
int x=1;
return *(char*)&x;
1999-03-31 00:29:34 +04:00
}