lua/lundump.c

268 lines
5.7 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
** $Id: lundump.c,v 1.41 2002/06/06 13:22:56 lhf Exp lhf $
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
*/
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
static const char* ZNAME (ZIO* Z)
2000-04-25 20:44:31 +04:00
{
const char* s=zname(Z);
2002-06-05 21:26:23 +04:00
if (*s=='@' || *s=='=')
return s+1;
else if (*s==LUA_SIGNATURE[0])
return "binary string";
else
return s;
2000-04-25 20:44:31 +04:00
}
1998-03-26 17:50:19 +03:00
1999-12-02 22:11:51 +03:00
static void unexpectedEOZ (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"unexpected end of file in %s",ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static int ezgetc (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
1999-12-02 22:11:51 +03:00
if (c==EOZ) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
return c;
}
1999-12-02 22:11:51 +03:00
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
1998-01-14 18:49:01 +03:00
{
int r=zread(Z,b,n);
1999-12-02 22:11:51 +03:00
if (r!=0) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-20 22:43:54 +04:00
if (swap)
{
char *p=(char*) b+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
2000-09-20 22:43:54 +04:00
}
else
ezread(L,Z,b,size);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
if (swap)
2000-09-20 22:43:54 +04:00
{
char *q=(char*) b;
2000-09-20 22:43:54 +04:00
while (m--)
{
char *p=q+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
2000-09-20 22:43:54 +04:00
q+=size;
}
}
2000-09-04 22:53:41 +04:00
else
2000-09-20 22:43:54 +04:00
ezread(L,Z,b,m*size);
}
static int LoadInt (lua_State* L, ZIO* Z, int swap)
{
int x;
LoadBlock(L,&x,sizeof(x),Z,swap);
2002-06-05 21:26:23 +04:00
if (x<0) luaG_runerror(L,"bad integer in %s",ZNAME(Z));
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
{
2000-09-04 22:53:41 +04:00
size_t x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
1999-03-31 00:29:34 +04:00
{
lua_Number x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1999-03-31 00:29:34 +04:00
}
2000-09-04 22:53:41 +04:00
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
size_t size=LoadSize(L,Z,swap);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
char* s=luaO_openspace(L,size,char);
2000-09-20 22:43:54 +04:00
LoadBlock(L,s,size,Z,0);
2001-07-06 00:29:15 +04:00
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
2001-07-06 00:29:15 +04:00
static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-12-28 15:59:41 +03:00
int size=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->code=luaM_newvector(L,size,Instruction);
f->sizecode=size;
LoadVector(L,f->code,size,sizeof(*f->code),Z,swap);
2000-04-25 20:44:31 +04:00
}
2001-07-06 00:29:15 +04:00
static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
int i,n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->locvars=luaM_newvector(L,n,LocVar);
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
{
2001-07-06 00:29:15 +04:00
f->locvars[i].varname=LoadString(L,Z,swap);
f->locvars[i].startpc=LoadInt(L,Z,swap);
f->locvars[i].endpc=LoadInt(L,Z,swap);
2000-04-25 20:44:31 +04:00
}
}
2001-07-06 00:29:15 +04:00
static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-09-04 22:53:41 +04:00
{
2000-10-20 20:39:03 +04:00
int n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->lineinfo=luaM_newvector(L,n,int);
LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
1998-01-14 18:49:01 +03:00
}
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap);
1998-03-26 17:50:19 +03:00
2001-07-06 00:29:15 +04:00
static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-03-03 17:58:26 +03:00
int i,n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->k=luaM_newvector(L,n,TObject);
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(L,Z);
switch (t)
2001-07-06 00:29:15 +04:00
{
case LUA_TNUMBER:
setnvalue(o,LoadNumber(L,Z,swap));
2001-07-06 00:29:15 +04:00
break;
case LUA_TSTRING:
setsvalue(o,LoadString(L,Z,swap));
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(L,"bad constant type (%d) in %s",t,ZNAME(Z));
2001-07-06 00:29:15 +04:00
break;
}
}
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->p=luaM_newvector(L,n,Proto*);
f->sizep=n;
for (i=0; i<n; i++) f->p[i]=LoadFunction(L,f->source,Z,swap);
2000-09-04 22:53:41 +04:00
}
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2001-07-06 00:29:15 +04:00
Proto* f=luaF_newproto(L);
f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
2001-07-06 00:29:15 +04:00
f->lineDefined=LoadInt(L,Z,swap);
2002-06-05 21:26:23 +04:00
f->nupvalues=LoadByte(L,Z);
f->numparams=LoadByte(L,Z);
f->is_vararg=LoadByte(L,Z);
f->maxstacksize=LoadByte(L,Z);
2001-07-06 00:29:15 +04:00
LoadLocals(L,f,Z,swap);
LoadLines(L,f,Z,swap);
LoadConstants(L,f,Z,swap);
LoadCode(L,f,Z,swap);
#ifndef TRUST_BINARIES
2002-06-05 21:26:23 +04:00
if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in %s",ZNAME(Z));
2001-07-06 00:29:15 +04:00
#endif
return f;
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static void LoadSignature (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
const char* s=LUA_SIGNATURE;
1999-12-02 22:11:51 +03:00
while (*s!=0 && ezgetc(L,Z)==*s)
1998-01-14 18:49:01 +03:00
++s;
2002-06-05 21:26:23 +04:00
if (*s!=0) luaG_runerror(L,"bad signature in %s",ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
2000-09-04 22:53:41 +04:00
{
int r=LoadByte(L,Z);
2000-09-04 22:53:41 +04:00
if (r!=s)
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"virtual machine mismatch in %s: "
"size of %s is %d but read %d",ZNAME(Z),what,s,r);
2000-09-04 22:53:41 +04:00
}
#define TESTSIZE(s,w) TestSize(L,s,w,Z)
#define V(v) v/16,v%16
2000-03-03 17:58:26 +03:00
1999-12-02 22:11:51 +03:00
static int LoadHeader (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
int version,swap;
2001-07-06 00:29:15 +04:00
lua_Number x=0,tx=TEST_NUMBER;
1999-12-02 22:11:51 +03:00
LoadSignature(L,Z);
version=LoadByte(L,Z);
1998-01-14 18:49:01 +03:00
if (version>VERSION)
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"%s too new: "
"read version %d.%d; expected at most %d.%d",
2000-04-25 20:44:31 +04:00
ZNAME(Z),V(version),V(VERSION));
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"%s too old: "
"read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION0));
swap=(luaU_endianness()!=LoadByte(L,Z)); /* 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");
2001-07-06 00:29:15 +04:00
x=LoadNumber(L,Z,swap);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"unknown number format in %s: read %f; expected %f",
2001-07-06 00:29:15 +04:00
ZNAME(Z),x,tx);
2000-09-04 22:53:41 +04:00
return swap;
1998-01-14 18:49:01 +03:00
}
2000-03-10 21:37:44 +03:00
static Proto* LoadChunk (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
return LoadFunction(L,NULL,Z,LoadHeader(L,Z));
1998-01-14 18:49:01 +03:00
}
/*
** load one chunk from a file or buffer
*/
2000-09-04 22:53:41 +04:00
Proto* luaU_undump (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
Proto* f=LoadChunk(L,Z);
if (zgetc(Z)!=EOZ)
2002-06-05 21:26:23 +04:00
luaG_runerror(L,"%s apparently contains more than one chunk",ZNAME(Z));
2001-07-06 00:29:15 +04:00
return f;
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
}