changes by lhf
This commit is contained in:
265
lundump.c
265
lundump.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.c,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
** $Id: lundump.c,v 1.7 1998/03/05 15:45:08 lhf Exp lhf $
|
||||||
** load bytecodes from files
|
** load bytecodes from files
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,15 +11,31 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
#include "lundump.h"
|
#include "lundump.h"
|
||||||
|
|
||||||
typedef struct {
|
#define LoadBlock(b,size,Z) ezread(Z,b,size)
|
||||||
ZIO* Z;
|
#define LoadNative(t,D) LoadBlock(&t,sizeof(t),D)
|
||||||
int SwapNumber;
|
|
||||||
int LoadFloat;
|
/* LUA_NUMBER */
|
||||||
} Sundump;
|
/* if you change the definition of real, make sure you set ID_NUMBER
|
||||||
|
* accordingly lundump.h, specially if sizeof(long)!=4.
|
||||||
|
* for types other than the ones listed below, you'll have to write your own
|
||||||
|
* dump and undump routines.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if ID_NUMBER==ID_REAL4
|
||||||
|
#define LoadNumber LoadFloat
|
||||||
|
#elif ID_NUMBER==ID_REAL8
|
||||||
|
#define LoadNumber LoadDouble
|
||||||
|
#elif ID_NUMBER==ID_INT4
|
||||||
|
#define LoadNumber LoadLong
|
||||||
|
#elif ID_NUMBER==ID_NATIVE
|
||||||
|
#define LoadNumber LoadNative
|
||||||
|
#else
|
||||||
|
#define LoadNumber LoadWhat
|
||||||
|
#endif
|
||||||
|
|
||||||
static void unexpectedEOZ(ZIO* Z)
|
static void unexpectedEOZ(ZIO* Z)
|
||||||
{
|
{
|
||||||
luaL_verror("unexpected end of binary file %s",Z->name);
|
luaL_verror("unexpected end of file in %s",zname(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ezgetc(ZIO* Z)
|
static int ezgetc(ZIO* Z)
|
||||||
@@ -29,91 +45,77 @@ static int ezgetc(ZIO* Z)
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ezread(ZIO* Z, void* b, int n)
|
static void ezread(ZIO* Z, void* b, int n)
|
||||||
{
|
{
|
||||||
int r=zread(Z,b,n);
|
int r=zread(Z,b,n);
|
||||||
if (r!=0) unexpectedEOZ(Z);
|
if (r!=0) unexpectedEOZ(Z);
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LoadWord(ZIO* Z)
|
static unsigned int LoadWord(ZIO* Z)
|
||||||
{
|
{
|
||||||
int hi=ezgetc(Z);
|
unsigned int hi=ezgetc(Z);
|
||||||
int lo=ezgetc(Z);
|
unsigned int lo=ezgetc(Z);
|
||||||
return (hi<<8)|lo;
|
return (hi<<8)|lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* LoadBlock(int size, ZIO* Z)
|
static unsigned long LoadLong(ZIO* Z)
|
||||||
{
|
{
|
||||||
void* b=luaM_malloc(size);
|
unsigned long hi=LoadWord(Z);
|
||||||
ezread(Z,b,size);
|
unsigned long lo=LoadWord(Z);
|
||||||
|
return (hi<<16)|lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* LUA_NUMBER */
|
||||||
|
/* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */
|
||||||
|
static float LoadFloat(ZIO* Z)
|
||||||
|
{
|
||||||
|
long l=LoadLong(Z);
|
||||||
|
float f=*(float*)&l;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* LUA_NUMBER */
|
||||||
|
/* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */
|
||||||
|
static double LoadDouble(ZIO* Z)
|
||||||
|
{
|
||||||
|
long l[2];
|
||||||
|
double f;
|
||||||
|
int x=1;
|
||||||
|
if (*(char*)&x==1) /* little-endian */
|
||||||
|
{
|
||||||
|
l[1]=LoadLong(Z);
|
||||||
|
l[0]=LoadLong(Z);
|
||||||
|
}
|
||||||
|
else /* big-endian */
|
||||||
|
{
|
||||||
|
l[0]=LoadLong(Z);
|
||||||
|
l[1]=LoadLong(Z);
|
||||||
|
}
|
||||||
|
f=*(double*)l;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Byte* LoadCode(ZIO* Z)
|
||||||
|
{
|
||||||
|
long size=LoadLong(Z);
|
||||||
|
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;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LoadSize(ZIO* Z)
|
static TaggedString* LoadTString(ZIO* Z)
|
||||||
{
|
|
||||||
int hi=LoadWord(Z);
|
|
||||||
int lo=LoadWord(Z);
|
|
||||||
int s=(hi<<16)|lo;
|
|
||||||
if (hi!=0 && s==lo)
|
|
||||||
luaL_verror("code too long (%ld bytes)",(hi<<16)|(long)lo);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char* LoadString(ZIO* Z)
|
|
||||||
{
|
{
|
||||||
int size=LoadWord(Z);
|
int size=LoadWord(Z);
|
||||||
if (size==0)
|
if (size==0)
|
||||||
return NULL;
|
return NULL;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char* b=luaL_openspace(size);
|
char* s=luaL_openspace(size);
|
||||||
ezread(Z,b,size);
|
LoadBlock(s,size,Z);
|
||||||
return b;
|
return luaS_newlstr(s,size-1);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static TaggedString* LoadTString(ZIO* Z)
|
|
||||||
{
|
|
||||||
char* s=LoadString(Z);
|
|
||||||
return (s==NULL) ? NULL : luaS_new(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void SwapFloat(float* f)
|
|
||||||
{
|
|
||||||
Byte* p=(Byte*)f;
|
|
||||||
Byte* q=p+sizeof(float)-1;
|
|
||||||
Byte t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void SwapDouble(double* f)
|
|
||||||
{
|
|
||||||
Byte* p=(Byte*)f;
|
|
||||||
Byte* q=p+sizeof(double)-1;
|
|
||||||
Byte t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
t=*p; *p++=*q; *q--=t;
|
|
||||||
}
|
|
||||||
|
|
||||||
static real LoadNumber(Sundump* S)
|
|
||||||
{
|
|
||||||
if (S->LoadFloat)
|
|
||||||
{
|
|
||||||
float f;
|
|
||||||
ezread(S->Z,&f,sizeof(f));
|
|
||||||
if (S->SwapNumber) SwapFloat(&f);
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
double f;
|
|
||||||
ezread(S->Z,&f,sizeof(f));
|
|
||||||
if (S->SwapNumber) SwapDouble(&f);
|
|
||||||
return f;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,63 +133,51 @@ static void LoadLocals(TProtoFunc* tf, ZIO* Z)
|
|||||||
tf->locvars[i].varname=NULL;
|
tf->locvars[i].varname=NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LoadConstants(TProtoFunc* tf, Sundump* S)
|
static TProtoFunc* LoadFunction(ZIO* Z);
|
||||||
|
|
||||||
|
static void LoadConstants(TProtoFunc* tf, ZIO* Z)
|
||||||
{
|
{
|
||||||
int i,n=LoadWord(S->Z);
|
int i,n=LoadWord(Z);
|
||||||
tf->nconsts=n;
|
tf->nconsts=n;
|
||||||
if (n==0) return;
|
if (n==0) return;
|
||||||
tf->consts=luaM_newvector(n,TObject);
|
tf->consts=luaM_newvector(n,TObject);
|
||||||
for (i=0; i<n; i++)
|
for (i=0; i<n; i++)
|
||||||
{
|
{
|
||||||
TObject* o=tf->consts+i;
|
TObject* o=tf->consts+i;
|
||||||
int c=ezgetc(S->Z);
|
int c=ezgetc(Z);
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case ID_NUM:
|
case ID_NUM:
|
||||||
ttype(o)=LUA_T_NUMBER;
|
ttype(o)=LUA_T_NUMBER;
|
||||||
nvalue(o)=LoadNumber(S);
|
#if ID_NUMBER==ID_NATIVE
|
||||||
|
LoadNative(nvalue(o),Z)
|
||||||
|
#else
|
||||||
|
nvalue(o)=LoadNumber(Z);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
case ID_STR:
|
case ID_STR:
|
||||||
ttype(o)=LUA_T_STRING;
|
ttype(o)=LUA_T_STRING;
|
||||||
tsvalue(o)=LoadTString(S->Z);
|
tsvalue(o)=LoadTString(Z);
|
||||||
break;
|
break;
|
||||||
case ID_FUN:
|
case ID_FUN:
|
||||||
ttype(o)=LUA_T_PROTO;
|
ttype(o)=LUA_T_PROTO;
|
||||||
tfvalue(o)=NULL;
|
tfvalue(o)=LoadFunction(Z);
|
||||||
break;
|
break;
|
||||||
#ifdef DEBUG
|
default:
|
||||||
default: /* cannot happen */
|
luaL_verror("bad constant #%d in %s: type=%d ('%c')",i,zname(Z),c,c);
|
||||||
luaL_verror("internal error in LoadConstants: "
|
|
||||||
"bad constant #%d type=%d ('%c')\n",i,c,c);
|
|
||||||
break;
|
break;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static TProtoFunc* LoadFunction(Sundump* S);
|
static TProtoFunc* LoadFunction(ZIO* Z)
|
||||||
|
|
||||||
static void LoadFunctions(TProtoFunc* tf, Sundump* S)
|
|
||||||
{
|
{
|
||||||
while (zgetc(S->Z)==ID_FUNCTION)
|
|
||||||
{
|
|
||||||
int i=LoadWord(S->Z);
|
|
||||||
TProtoFunc* t=LoadFunction(S);
|
|
||||||
TObject* o=tf->consts+i;
|
|
||||||
tfvalue(o)=t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static TProtoFunc* LoadFunction(Sundump* S)
|
|
||||||
{
|
|
||||||
ZIO* Z=S->Z;
|
|
||||||
TProtoFunc* tf=luaF_newproto();
|
TProtoFunc* tf=luaF_newproto();
|
||||||
tf->lineDefined=LoadWord(Z);
|
tf->lineDefined=LoadWord(Z);
|
||||||
tf->fileName=LoadTString(Z);
|
tf->fileName=LoadTString(Z);
|
||||||
tf->code=LoadBlock(LoadSize(Z),Z);
|
tf->code=LoadCode(Z);
|
||||||
LoadConstants(tf,S);
|
|
||||||
LoadLocals(tf,Z);
|
LoadLocals(tf,Z);
|
||||||
LoadFunctions(tf,S);
|
LoadConstants(tf,Z);
|
||||||
return tf;
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,61 +186,42 @@ static void LoadSignature(ZIO* Z)
|
|||||||
char* s=SIGNATURE;
|
char* s=SIGNATURE;
|
||||||
while (*s!=0 && ezgetc(Z)==*s)
|
while (*s!=0 && ezgetc(Z)==*s)
|
||||||
++s;
|
++s;
|
||||||
if (*s!=0) luaL_verror("bad signature in binary file %s",Z->name);
|
if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LoadHeader(Sundump* S)
|
static void LoadHeader(ZIO* Z)
|
||||||
{
|
{
|
||||||
ZIO* Z=S->Z;
|
int version,id,sizeofR;
|
||||||
int version,sizeofR;
|
real f=-TEST_NUMBER,tf=TEST_NUMBER;
|
||||||
LoadSignature(Z);
|
LoadSignature(Z);
|
||||||
version=ezgetc(Z);
|
version=ezgetc(Z);
|
||||||
if (version>VERSION)
|
if (version>VERSION)
|
||||||
luaL_verror(
|
luaL_verror(
|
||||||
"binary file %s too new: version=0x%02x; expected at most 0x%02x",
|
"%s too new: version=0x%02x; expected at most 0x%02x",
|
||||||
Z->name,version,VERSION);
|
zname(Z),version,VERSION);
|
||||||
if (version<0x31) /* major change in 3.1 */
|
if (version<0x31) /* major change in 3.1 */
|
||||||
luaL_verror(
|
luaL_verror(
|
||||||
"binary file %s too old: version=0x%02x; expected at least 0x%02x",
|
"%s too old: version=0x%02x; expected at least 0x%02x",
|
||||||
Z->name,version,0x31);
|
zname(Z),version,0x31);
|
||||||
sizeofR=ezgetc(Z); /* test float representation */
|
id=ezgetc(Z); /* test number representation */
|
||||||
if (sizeofR==sizeof(float))
|
sizeofR=ezgetc(Z);
|
||||||
|
if (id!=ID_NUMBER || sizeofR!=sizeof(real))
|
||||||
{
|
{
|
||||||
float f,tf=TEST_FLOAT;
|
luaL_verror("unknown number representation in %s: "
|
||||||
ezread(Z,&f,sizeof(f));
|
"read 0x%02x %d; expected 0x%02x %d",
|
||||||
if (f!=tf)
|
zname(Z),id,sizeofR,ID_NUMBER,sizeof(real));
|
||||||
{
|
|
||||||
SwapFloat(&f);
|
|
||||||
if (f!=tf)
|
|
||||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
|
||||||
S->SwapNumber=1;
|
|
||||||
}
|
|
||||||
S->LoadFloat=1;
|
|
||||||
}
|
}
|
||||||
else if (sizeofR==sizeof(double))
|
f=LoadNumber(Z);
|
||||||
{
|
if (f!=tf)
|
||||||
double f,tf=TEST_FLOAT;
|
luaL_verror("unknown number representation in %s: "
|
||||||
ezread(Z,&f,sizeof(f));
|
"read %g; expected %g", /* LUA_NUMBER */
|
||||||
if (f!=tf)
|
zname(Z),(double)f,(double)tf);
|
||||||
{
|
|
||||||
SwapDouble(&f);
|
|
||||||
if (f!=tf)
|
|
||||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
|
||||||
S->SwapNumber=1;
|
|
||||||
}
|
|
||||||
S->LoadFloat=0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
luaL_verror(
|
|
||||||
"floats in binary file %s have %d bytes; "
|
|
||||||
"expected %d (float) or %d (double)",
|
|
||||||
Z->name,sizeofR,sizeof(float),sizeof(double));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static TProtoFunc* LoadChunk(Sundump* S)
|
static TProtoFunc* LoadChunk(ZIO* Z)
|
||||||
{
|
{
|
||||||
LoadHeader(S);
|
LoadHeader(Z);
|
||||||
return LoadFunction(S);
|
return LoadFunction(Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -260,13 +231,9 @@ static TProtoFunc* LoadChunk(Sundump* S)
|
|||||||
TProtoFunc* luaU_undump1(ZIO* Z)
|
TProtoFunc* luaU_undump1(ZIO* Z)
|
||||||
{
|
{
|
||||||
int c=zgetc(Z);
|
int c=zgetc(Z);
|
||||||
Sundump S;
|
|
||||||
S.Z=Z;
|
|
||||||
S.SwapNumber=0;
|
|
||||||
S.LoadFloat=1;
|
|
||||||
if (c==ID_CHUNK)
|
if (c==ID_CHUNK)
|
||||||
return LoadChunk(&S);
|
return LoadChunk(Z);
|
||||||
else if (c!=EOZ)
|
else if (c!=EOZ)
|
||||||
luaL_verror("%s is not a lua binary file",Z->name);
|
luaL_verror("%s is not a Lua binary file",zname(Z));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
44
lundump.h
44
lundump.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.h,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
** $Id: lundump.h,v 1.5 1998/02/06 20:05:39 lhf Exp lhf $
|
||||||
** load pre-compiled Lua chunks
|
** load pre-compiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,18 +10,44 @@
|
|||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
#define ID_CHUNK 27 /* ESC */
|
TProtoFunc* luaU_undump1(ZIO* Z); /* load one chunk */
|
||||||
#define ID_FUNCTION '#'
|
|
||||||
#define ID_END '$'
|
|
||||||
#define ID_NUM 'N'
|
|
||||||
#define ID_STR 'S'
|
|
||||||
#define ID_FUN 'F'
|
|
||||||
#define SIGNATURE "Lua"
|
#define SIGNATURE "Lua"
|
||||||
#define VERSION 0x31 /* last format change was in 3.1 */
|
#define VERSION 0x31 /* last format change was in 3.1 */
|
||||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
|
||||||
|
|
||||||
#define IsMain(f) (f->lineDefined==0)
|
#define IsMain(f) (f->lineDefined==0)
|
||||||
|
|
||||||
TProtoFunc* luaU_undump1(ZIO* Z); /* load one chunk */
|
#define ID_CHUNK 27 /* ESC */
|
||||||
|
#define ID_NUM 'N'
|
||||||
|
#define ID_STR 'S'
|
||||||
|
#define ID_FUN 'F'
|
||||||
|
|
||||||
|
#define ID_INT4 'l'
|
||||||
|
#define ID_REAL4 'f'
|
||||||
|
#define ID_REAL8 'd'
|
||||||
|
#define ID_NATIVE '?'
|
||||||
|
|
||||||
|
/*
|
||||||
|
* use a multiple of PI for testing number representation.
|
||||||
|
* multiplying by 10E8 gives notrivial integer values.
|
||||||
|
*/
|
||||||
|
#define TEST_NUMBER 3.14159265358979323846E8
|
||||||
|
|
||||||
|
/* LUA_NUMBER */
|
||||||
|
/* if you change the definition of real, make sure you set ID_NUMBER
|
||||||
|
* accordingly, specially if sizeof(long)!=4.
|
||||||
|
* for types other than the ones listed below, you'll have to write your own
|
||||||
|
* dump and undump routines.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if real==float
|
||||||
|
#define ID_NUMBER ID_REAL4
|
||||||
|
#elif real==double
|
||||||
|
#define ID_NUMBER ID_REAL8
|
||||||
|
#elif real==long
|
||||||
|
#define ID_NUMBER ID_INT4
|
||||||
|
#else
|
||||||
|
#define ID_NUMBER ID_NATIVE
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user