1998-01-14 23:49:01 +08:00
|
|
|
/*
|
1999-03-05 05:23:39 +08:00
|
|
|
** $Id: lundump.c,v 1.6 1998/12/27 20:23:22 roberto Exp roberto $
|
1998-01-14 23:49:01 +08:00
|
|
|
** load bytecodes from files
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "lundump.h"
|
|
|
|
|
1998-03-26 22:50:19 +08:00
|
|
|
#define LoadBlock(b,size,Z) ezread(Z,b,size)
|
1998-06-26 00:48:44 +08:00
|
|
|
#define LoadNative(t,Z) LoadBlock(&t,sizeof(t),Z)
|
1998-03-26 22:50:19 +08:00
|
|
|
|
1998-06-26 00:48:44 +08:00
|
|
|
#if ID_NUMBER==ID_NATIVE
|
|
|
|
#define doLoadNumber(f,Z) LoadNative(f,Z)
|
1998-03-26 22:50:19 +08:00
|
|
|
#else
|
1998-06-26 00:48:44 +08:00
|
|
|
#define doLoadNumber(f,Z) f=LoadNumber(Z)
|
1998-03-26 22:50:19 +08:00
|
|
|
#endif
|
1998-01-14 23:49:01 +08:00
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void unexpectedEOZ (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
luaL_verror("unexpected end of file in %s",zname(Z));
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static int ezgetc (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
int c=zgetc(Z);
|
|
|
|
if (c==EOZ) unexpectedEOZ(Z);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void ezread (ZIO* Z, void* b, int n)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
int r=zread(Z,b,n);
|
|
|
|
if (r!=0) unexpectedEOZ(Z);
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static unsigned int LoadWord (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
unsigned int hi=ezgetc(Z);
|
|
|
|
unsigned int lo=ezgetc(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
return (hi<<8)|lo;
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static unsigned long LoadLong (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
unsigned long hi=LoadWord(Z);
|
|
|
|
unsigned long lo=LoadWord(Z);
|
|
|
|
return (hi<<16)|lo;
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
1998-06-19 00:52:04 +08:00
|
|
|
#if ID_NUMBER==ID_REAL4
|
1998-03-26 22:50:19 +08:00
|
|
|
/* LUA_NUMBER */
|
|
|
|
/* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */
|
1998-12-15 22:59:43 +08:00
|
|
|
static float LoadFloat (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-06-19 00:52:04 +08:00
|
|
|
unsigned long l=LoadLong(Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
float f=*(float*)&l;
|
|
|
|
return f;
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
1998-06-19 00:52:04 +08:00
|
|
|
#endif
|
1998-01-14 23:49:01 +08:00
|
|
|
|
1998-06-19 00:52:04 +08:00
|
|
|
#if ID_NUMBER==ID_REAL8
|
1998-03-26 22:50:19 +08:00
|
|
|
/* LUA_NUMBER */
|
|
|
|
/* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */
|
1998-12-15 22:59:43 +08:00
|
|
|
static double LoadDouble (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-06-19 00:52:04 +08:00
|
|
|
unsigned long l[2];
|
1998-03-26 22:50:19 +08:00
|
|
|
double f;
|
|
|
|
int x=1;
|
|
|
|
if (*(char*)&x==1) /* little-endian */
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
l[1]=LoadLong(Z);
|
|
|
|
l[0]=LoadLong(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
1998-03-26 22:50:19 +08:00
|
|
|
else /* big-endian */
|
|
|
|
{
|
|
|
|
l[0]=LoadLong(Z);
|
|
|
|
l[1]=LoadLong(Z);
|
|
|
|
}
|
|
|
|
f=*(double*)l;
|
|
|
|
return f;
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
1998-06-19 00:52:04 +08:00
|
|
|
#endif
|
1998-01-14 23:49:01 +08:00
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static Byte* LoadCode (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-06-19 00:52:04 +08:00
|
|
|
unsigned long size=LoadLong(Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
unsigned int s=size;
|
|
|
|
void* b;
|
|
|
|
if (s!=size) luaL_verror("code too long (%ld bytes) in %s",size,zname(Z));
|
|
|
|
b=luaM_malloc(size);
|
|
|
|
LoadBlock(b,size,Z);
|
|
|
|
return b;
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static TaggedString* LoadTString (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
int size=LoadWord(Z);
|
|
|
|
if (size==0)
|
|
|
|
return NULL;
|
1998-01-14 23:49:01 +08:00
|
|
|
else
|
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
char* s=luaL_openspace(size);
|
|
|
|
LoadBlock(s,size,Z);
|
|
|
|
return luaS_newlstr(s,size-1);
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void LoadLocals (TProtoFunc* tf, ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
int i,n=LoadWord(Z);
|
|
|
|
if (n==0) return;
|
|
|
|
tf->locvars=luaM_newvector(n+1,LocVar);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
tf->locvars[i].line=LoadWord(Z);
|
|
|
|
tf->locvars[i].varname=LoadTString(Z);
|
|
|
|
}
|
|
|
|
tf->locvars[i].line=-1; /* flag end of vector */
|
|
|
|
tf->locvars[i].varname=NULL;
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static TProtoFunc* LoadFunction (ZIO* Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void LoadConstants (TProtoFunc* tf, ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
int i,n=LoadWord(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
tf->nconsts=n;
|
|
|
|
if (n==0) return;
|
|
|
|
tf->consts=luaM_newvector(n,TObject);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
TObject* o=tf->consts+i;
|
1998-03-26 22:50:19 +08:00
|
|
|
int c=ezgetc(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case ID_NUM:
|
|
|
|
ttype(o)=LUA_T_NUMBER;
|
1998-06-26 00:48:44 +08:00
|
|
|
doLoadNumber(nvalue(o),Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
break;
|
|
|
|
case ID_STR:
|
|
|
|
ttype(o)=LUA_T_STRING;
|
1998-03-26 22:50:19 +08:00
|
|
|
tsvalue(o)=LoadTString(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
break;
|
|
|
|
case ID_FUN:
|
|
|
|
ttype(o)=LUA_T_PROTO;
|
1998-03-26 22:50:19 +08:00
|
|
|
tfvalue(o)=LoadFunction(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
break;
|
1998-03-26 22:50:19 +08:00
|
|
|
default:
|
|
|
|
luaL_verror("bad constant #%d in %s: type=%d ('%c')",i,zname(Z),c,c);
|
1998-01-14 23:49:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static TProtoFunc* LoadFunction (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
TProtoFunc* tf=luaF_newproto();
|
|
|
|
tf->lineDefined=LoadWord(Z);
|
1999-03-05 05:23:39 +08:00
|
|
|
tf->source=LoadTString(Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
tf->code=LoadCode(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
LoadLocals(tf,Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
LoadConstants(tf,Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
return tf;
|
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void LoadSignature (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
char* s=SIGNATURE;
|
|
|
|
while (*s!=0 && ezgetc(Z)==*s)
|
|
|
|
++s;
|
1998-03-26 22:50:19 +08:00
|
|
|
if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static void LoadHeader (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
int version,id,sizeofR;
|
|
|
|
real f=-TEST_NUMBER,tf=TEST_NUMBER;
|
1998-01-14 23:49:01 +08:00
|
|
|
LoadSignature(Z);
|
|
|
|
version=ezgetc(Z);
|
|
|
|
if (version>VERSION)
|
|
|
|
luaL_verror(
|
1998-03-26 22:50:19 +08:00
|
|
|
"%s too new: version=0x%02x; expected at most 0x%02x",
|
|
|
|
zname(Z),version,VERSION);
|
1998-06-26 00:48:44 +08:00
|
|
|
if (version<VERSION0) /* check last major change */
|
1998-01-14 23:49:01 +08:00
|
|
|
luaL_verror(
|
1998-03-26 22:50:19 +08:00
|
|
|
"%s too old: version=0x%02x; expected at least 0x%02x",
|
1998-06-26 00:48:44 +08:00
|
|
|
zname(Z),version,VERSION0);
|
1998-03-26 22:50:19 +08:00
|
|
|
id=ezgetc(Z); /* test number representation */
|
|
|
|
sizeofR=ezgetc(Z);
|
|
|
|
if (id!=ID_NUMBER || sizeofR!=sizeof(real))
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-06-26 00:48:44 +08:00
|
|
|
luaL_verror("unknown number signature in %s: "
|
|
|
|
"read 0x%02x%02x; expected 0x%02x%02x",
|
1998-03-26 22:50:19 +08:00
|
|
|
zname(Z),id,sizeofR,ID_NUMBER,sizeof(real));
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
1998-06-26 00:48:44 +08:00
|
|
|
doLoadNumber(f,Z);
|
1998-03-26 22:50:19 +08:00
|
|
|
if (f!=tf)
|
1998-12-28 04:23:22 +08:00
|
|
|
luaL_verror("unknown number representation in %s: read %g; expected %g",
|
|
|
|
zname(Z),(double)f,(double)tf); /* LUA_NUMBER */
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 22:59:43 +08:00
|
|
|
static TProtoFunc* LoadChunk (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
1998-03-26 22:50:19 +08:00
|
|
|
LoadHeader(Z);
|
|
|
|
return LoadFunction(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** load one chunk from a file or buffer
|
|
|
|
** return main if ok and NULL at EOF
|
|
|
|
*/
|
1998-12-15 22:59:43 +08:00
|
|
|
TProtoFunc* luaU_undump1 (ZIO* Z)
|
1998-01-14 23:49:01 +08:00
|
|
|
{
|
|
|
|
int c=zgetc(Z);
|
|
|
|
if (c==ID_CHUNK)
|
1998-03-26 22:50:19 +08:00
|
|
|
return LoadChunk(Z);
|
1998-01-14 23:49:01 +08:00
|
|
|
else if (c!=EOZ)
|
1998-03-26 22:50:19 +08:00
|
|
|
luaL_verror("%s is not a Lua binary file",zname(Z));
|
1998-01-14 23:49:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|