lua/lundump.c

277 lines
5.4 KiB
C
Raw Normal View History

1998-01-14 23:49:01 +08:00
/*
2002-10-09 21:42:01 +08:00
** $Id: lundump.c,v 1.54 2002/10/08 18:46:08 roberto Exp roberto $
2001-07-06 04:29:15 +08:00
** load pre-compiled Lua chunks
1998-01-14 23:49:01 +08:00
** See Copyright Notice in lua.h
*/
2001-03-26 22:31:49 +08:00
#include "lua.h"
2001-07-06 04:29:15 +08:00
#include "ldebug.h"
1998-01-14 23:49:01 +08:00
#include "lfunc.h"
#include "lmem.h"
1999-03-31 04:29:34 +08:00
#include "lopcodes.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
2002-06-06 01:26:23 +08:00
#define LoadByte (lu_byte) ezgetc
2000-04-26 00:44:31 +08:00
typedef struct {
lua_State* L;
ZIO* Z;
2002-10-09 21:42:01 +08:00
Mbuffer *buff;
int swap;
const char* name;
} LoadState;
1998-03-26 22:50:19 +08:00
static void unexpectedEOZ (LoadState* S)
1998-01-14 23:49:01 +08:00
{
luaG_runerror(S->L,"unexpected end of file in %s",S->name);
1998-01-14 23:49:01 +08:00
}
static int ezgetc (LoadState* S)
1998-01-14 23:49:01 +08:00
{
int c=zgetc(S->Z);
if (c==EOZ) unexpectedEOZ(S);
1998-01-14 23:49:01 +08:00
return c;
}
static void ezread (LoadState* S, void* b, int n)
1998-01-14 23:49:01 +08:00
{
int r=luaZ_read(S->Z,b,n);
if (r!=0) unexpectedEOZ(S);
1998-01-14 23:49:01 +08:00
}
static void LoadBlock (LoadState* S, void* b, size_t size)
1998-01-14 23:49:01 +08:00
{
if (S->swap)
2000-09-21 02:43:54 +08:00
{
char *p=(char*) b+size-1;
2000-09-21 02:43:54 +08:00
int n=size;
while (n--) *p--=(char)ezgetc(S);
2000-09-21 02:43:54 +08:00
}
else
ezread(S,b,size);
1998-01-14 23:49:01 +08:00
}
static void LoadVector (LoadState* S, void* b, int m, size_t size)
1998-01-14 23:49:01 +08:00
{
if (S->swap)
2000-09-21 02:43:54 +08:00
{
char *q=(char*) b;
2000-09-21 02:43:54 +08:00
while (m--)
{
char *p=q+size-1;
2000-09-21 02:43:54 +08:00
int n=size;
while (n--) *p--=(char)ezgetc(S);
2000-09-21 02:43:54 +08:00
q+=size;
}
}
2000-09-05 02:53:41 +08:00
else
ezread(S,b,m*size);
2000-09-21 02:43:54 +08:00
}
static int LoadInt (LoadState* S)
2000-09-21 02:43:54 +08:00
{
int x;
LoadBlock(S,&x,sizeof(x));
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
2000-09-05 02:53:41 +08:00
return x;
1998-01-14 23:49:01 +08:00
}
static size_t LoadSize (LoadState* S)
{
2000-09-05 02:53:41 +08:00
size_t x;
LoadBlock(S,&x,sizeof(x));
2000-09-05 02:53:41 +08:00
return x;
1998-01-14 23:49:01 +08:00
}
static lua_Number LoadNumber (LoadState* S)
1999-03-31 04:29:34 +08:00
{
lua_Number x;
LoadBlock(S,&x,sizeof(x));
2000-09-05 02:53:41 +08:00
return x;
1999-03-31 04:29:34 +08:00
}
static TString* LoadString (LoadState* S)
1998-01-14 23:49:01 +08:00
{
size_t size=LoadSize(S);
1998-03-26 22:50:19 +08:00
if (size==0)
return NULL;
1998-01-14 23:49:01 +08:00
else
{
2002-10-09 21:42:01 +08:00
char* s=luaZ_openspace(S->L,S->buff,size);
ezread(S,s,size);
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
1998-01-14 23:49:01 +08:00
}
}
static void LoadCode (LoadState* S, Proto* f)
2000-04-26 00:44:31 +08:00
{
int size=LoadInt(S);
f->code=luaM_newvector(S->L,size,Instruction);
2001-07-06 04:29:15 +08:00
f->sizecode=size;
LoadVector(S,f->code,size,sizeof(*f->code));
2000-04-26 00:44:31 +08:00
}
static void LoadLocals (LoadState* S, Proto* f)
2000-04-26 00:44:31 +08:00
{
2000-09-05 02:53:41 +08:00
int i,n;
n=LoadInt(S);
f->locvars=luaM_newvector(S->L,n,LocVar);
2001-07-06 04:29:15 +08:00
f->sizelocvars=n;
2000-09-05 02:53:41 +08:00
for (i=0; i<n; i++)
2000-04-26 00:44:31 +08:00
{
f->locvars[i].varname=LoadString(S);
f->locvars[i].startpc=LoadInt(S);
f->locvars[i].endpc=LoadInt(S);
2000-04-26 00:44:31 +08:00
}
}
static void LoadLines (LoadState* S, Proto* f)
2000-09-05 02:53:41 +08:00
{
2000-10-21 00:39:03 +08:00
int n;
n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int);
LoadVector(S,f->lineinfo,n,sizeof(*f->lineinfo));
1998-01-14 23:49:01 +08:00
}
static Proto* LoadFunction (LoadState* S, TString* p);
1998-03-26 22:50:19 +08:00
static void LoadConstants (LoadState* S, Proto* f)
1998-01-14 23:49:01 +08:00
{
2000-03-03 22:58:26 +08:00
int i,n;
n=LoadInt(S);
f->k=luaM_newvector(S->L,n,TObject);
2001-07-06 04:29:15 +08:00
f->sizek=n;
2000-09-05 02:53:41 +08:00
for (i=0; i<n; i++)
2001-07-06 04:29:15 +08:00
{
TObject* o=&f->k[i];
int t=LoadByte(S);
switch (t)
2001-07-06 04:29:15 +08:00
{
case LUA_TNUMBER:
setnvalue(o,LoadNumber(S));
2001-07-06 04:29:15 +08:00
break;
case LUA_TSTRING:
setsvalue(o,LoadString(S));
2001-07-06 04:29:15 +08:00
break;
2002-06-06 01:26:23 +08:00
case LUA_TNIL:
setnilvalue(o);
2002-06-06 01:26:23 +08:00
break;
2001-07-06 04:29:15 +08:00
default:
luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
2001-07-06 04:29:15 +08:00
break;
}
}
n=LoadInt(S);
f->p=luaM_newvector(S->L,n,Proto*);
2001-07-06 04:29:15 +08:00
f->sizep=n;
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
2000-09-05 02:53:41 +08:00
}
static Proto* LoadFunction (LoadState* S, TString* p)
1998-01-14 23:49:01 +08: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 04:29:15 +08:00
#ifndef TRUST_BINARIES
if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name);
2001-07-06 04:29:15 +08:00
#endif
return f;
1998-01-14 23:49:01 +08:00
}
static void LoadSignature (LoadState* S)
1998-01-14 23:49:01 +08:00
{
const char* s=LUA_SIGNATURE;
while (*s!=0 && ezgetc(S)==*s)
1998-01-14 23:49:01 +08:00
++s;
if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
1998-01-14 23:49:01 +08:00
}
static void TestSize (LoadState* S, int s, const char* what)
2000-09-05 02:53:41 +08:00
{
int r=LoadByte(S);
2000-09-05 02:53:41 +08: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-05 02:53:41 +08:00
}
#define TESTSIZE(s,w) TestSize(S,s,w)
#define V(v) v/16,v%16
2000-03-03 22:58:26 +08:00
static void LoadHeader (LoadState* S)
1998-01-14 23:49:01 +08:00
{
int version;
2001-07-06 04:29:15 +08:00
lua_Number x=0,tx=TEST_NUMBER;
LoadSignature(S);
version=LoadByte(S);
1998-01-14 23:49:01 +08:00
if (version>VERSION)
luaG_runerror(S->L,"%s too new: "
2002-06-06 01:26:23 +08:00
"read version %d.%d; expected at most %d.%d",
S->name,V(version),V(VERSION));
1998-06-26 00:48:44 +08:00
if (version<VERSION0) /* check last major change */
luaG_runerror(S->L,"%s too old: "
2002-06-06 01:26:23 +08: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-06 01:26:23 +08: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 04:29:15 +08:00
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
luaG_runerror(S->L,"unknown number format in %s: read %f; expected %f",
S->name,x,tx);
1998-01-14 23:49:01 +08:00
}
static Proto* LoadChunk (LoadState* S)
1998-01-14 23:49:01 +08:00
{
LoadHeader(S);
return LoadFunction(S,NULL);
1998-01-14 23:49:01 +08:00
}
/*
** load one chunk from a file or buffer
*/
2002-10-09 21:42:01 +08:00
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer *buff)
1998-01-14 23:49:01 +08:00
{
LoadState S;
Proto* f;
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-09 21:42:01 +08:00
S.buff=buff;
f=LoadChunk(&S);
if (zgetc(Z)!=EOZ)
luaG_runerror(L,"%s apparently contains more than one chunk",S.name);
2001-07-06 04:29:15 +08:00
return f;
2000-09-05 02:53:41 +08:00
}
1999-12-03 03:11:51 +08:00
/*
2000-09-05 02:53:41 +08:00
** find byte order
1999-12-03 03:11:51 +08:00
*/
2001-07-06 04:29:15 +08:00
int luaU_endianness (void)
1999-12-03 03:11:51 +08:00
{
2000-09-05 02:53:41 +08:00
int x=1;
return *(char*)&x;
1999-03-31 04:29:34 +08:00
}