new versions by lhf
This commit is contained in:
262
lundump.c
262
lundump.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.c,v 1.25 2000/08/24 14:19:39 roberto Exp roberto $
|
** $Id: lundump.c,v 1.29 2000/06/28 14:12:55 lhf Exp lhf $
|
||||||
** load bytecodes from files
|
** load bytecodes from files
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,8 +7,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lfunc.h"
|
#include "lfunc.h"
|
||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
@@ -16,6 +14,7 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
#include "lundump.h"
|
#include "lundump.h"
|
||||||
|
|
||||||
|
#define LoadVector(L,b,n,s,Z) LoadBlock(L,b,(n)*(s),Z)
|
||||||
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
||||||
#define LoadByte ezgetc
|
#define LoadByte ezgetc
|
||||||
|
|
||||||
@@ -43,41 +42,46 @@ static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
|||||||
if (r!=0) unexpectedEOZ(L,Z);
|
if (r!=0) unexpectedEOZ(L,Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int LoadWord (lua_State* L, ZIO* Z)
|
static void LoadReverse (lua_State* L, void* b, size_t size, ZIO* Z)
|
||||||
{
|
{
|
||||||
unsigned int hi=ezgetc(L,Z);
|
unsigned char *p=(unsigned char *) b+size;
|
||||||
unsigned int lo=ezgetc(L,Z);
|
int n=size;
|
||||||
return (hi<<8)|lo;
|
while (n--) *p--=ezgetc(L,Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned long LoadLong (lua_State* L, ZIO* Z)
|
static int LoadInt (lua_State* L, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
unsigned long hi=LoadWord(L,Z);
|
int x;
|
||||||
unsigned long lo=LoadWord(L,Z);
|
if (swap)
|
||||||
return (hi<<16)|lo;
|
LoadReverse(L,&x,sizeof(x),Z);
|
||||||
|
else
|
||||||
|
LoadBlock(L,&x,sizeof(x),Z);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Number LoadNumber (lua_State* L, ZIO* Z)
|
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
char b[256];
|
size_t x;
|
||||||
int size=ezgetc(L,Z);
|
if (swap)
|
||||||
LoadBlock(L,b,size,Z);
|
LoadReverse(L,&x,sizeof(x),Z);
|
||||||
b[size]=0;
|
else
|
||||||
return luaU_str2d(L,b,ZNAME(Z));
|
LoadBlock(L,&x,sizeof(x),Z);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
unsigned long l=LoadLong(L,Z);
|
Number x;
|
||||||
unsigned int i=l;
|
if (swap)
|
||||||
if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
|
LoadReverse(L,&x,sizeof(x),Z);
|
||||||
" read %lu; expected %u",message,ZNAME(Z),l,-1);
|
else
|
||||||
return i;
|
LoadBlock(L,&x,sizeof(x),Z);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
static TString* LoadString (lua_State* L, ZIO* Z)
|
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
long size=LoadLong(L,Z);
|
size_t size=LoadSize(L,Z,swap);
|
||||||
if (size==0)
|
if (size==0)
|
||||||
return NULL;
|
return NULL;
|
||||||
else
|
else
|
||||||
@@ -88,96 +92,73 @@ static TString* LoadString (lua_State* L, ZIO* Z)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
|
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
unsigned char* p;
|
int size=LoadInt(L,Z,swap);
|
||||||
unsigned char c;
|
|
||||||
if (sizeof(Instruction)==4)
|
|
||||||
while (size--)
|
|
||||||
{
|
|
||||||
p=(unsigned char *) code++;
|
|
||||||
c=p[0]; p[0]=p[3]; p[3]=c;
|
|
||||||
c=p[1]; p[1]=p[2]; p[2]=c;
|
|
||||||
}
|
|
||||||
else if (sizeof(Instruction)==2)
|
|
||||||
while (size--)
|
|
||||||
{
|
|
||||||
p=(unsigned char *) code++;
|
|
||||||
c=p[0]; p[0]=p[1]; p[1]=c;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
luaL_verror(L,"cannot swap code with %d-byte instructions in `%.255s'",
|
|
||||||
(int)sizeof(Instruction),ZNAME(Z));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z)
|
|
||||||
{
|
|
||||||
int size=LoadInt(L,Z,"code too long");
|
|
||||||
Instruction t=0,tt=TEST_CODE;
|
|
||||||
tf->code=luaM_newvector(L,size,Instruction);
|
tf->code=luaM_newvector(L,size,Instruction);
|
||||||
LoadBlock(L,tf->code,size*sizeof(*tf->code),Z);
|
LoadVector(L,tf->code,size,sizeof(*tf->code),Z);
|
||||||
|
#if 0
|
||||||
|
if (swap) SwapBytes(tf->code,sizeof(*tf->code),size);
|
||||||
|
#endif
|
||||||
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||||
LoadBlock(L,&t,sizeof(t),Z);
|
|
||||||
if (t!=tt)
|
|
||||||
{
|
|
||||||
Instruction ot=t;
|
|
||||||
SwapCode(L,&t,1,Z);
|
|
||||||
if (t!=tt) luaL_verror(L,"cannot swap code in `%.255s';\n"
|
|
||||||
" read 0x%08X; swapped to 0x%08X; expected 0x%08X",
|
|
||||||
ZNAME(Z),(unsigned long)ot,(unsigned long)t,(unsigned long)tt);
|
|
||||||
SwapCode(L,tf->code,size,Z);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
|
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
|
|
||||||
|
|
||||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
|
||||||
{
|
{
|
||||||
int i,n;
|
int i,n;
|
||||||
tf->nkstr=n=LoadInt(L,Z,"too many strings");
|
tf->nlocvars=n=LoadInt(L,Z,swap);
|
||||||
if (n>0)
|
tf->locvars=luaM_newvector(L,n,LocVar);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
{
|
{
|
||||||
tf->kstr=luaM_newvector(L,n,TString*);
|
tf->locvars[i].varname=LoadString(L,Z,swap);
|
||||||
for (i=0; i<n; i++)
|
tf->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||||
{
|
tf->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||||
TString* s=LoadString(L,Z);
|
|
||||||
LoadByte(L,Z);
|
|
||||||
tf->kstr[i]=s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tf->nknum=n=LoadInt(L,Z,"too many numbers");
|
|
||||||
if (n>0)
|
|
||||||
{
|
|
||||||
tf->knum=luaM_newvector(L,n,Number);
|
|
||||||
if (native)
|
|
||||||
LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
|
|
||||||
else
|
|
||||||
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
|
|
||||||
}
|
|
||||||
tf->nkproto=n=LoadInt(L,Z,"too many functions");
|
|
||||||
if (n>0)
|
|
||||||
{
|
|
||||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
|
||||||
for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
|
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int n=LoadInt(L,Z,swap);
|
||||||
|
if (n==0) return;
|
||||||
|
tf->lineinfo=luaM_newvector(L,n,int);
|
||||||
|
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z);
|
||||||
|
#if 0
|
||||||
|
if (swap) SwapBytes(tf->lineinfo,sizeof(*tf->lineinfo),n);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
|
||||||
|
|
||||||
|
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int i,n;
|
||||||
|
tf->nkstr=n=LoadInt(L,Z,swap);
|
||||||
|
tf->kstr=luaM_newvector(L,n,TString*);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
tf->kstr[i]=LoadString(L,Z,swap);
|
||||||
|
tf->nknum=n=LoadInt(L,Z,swap);
|
||||||
|
tf->knum=luaM_newvector(L,n,Number);
|
||||||
|
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z);
|
||||||
|
if (swap)
|
||||||
|
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z,swap); /* TODO */
|
||||||
|
tf->nkproto=n=LoadInt(L,Z,swap);
|
||||||
|
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
tf->kproto[i]=LoadFunction(L,Z,swap);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
Proto* tf=luaF_newproto(L);
|
Proto* tf=luaF_newproto(L);
|
||||||
tf->source=LoadString(L,Z);
|
tf->source=LoadString(L,Z,swap);
|
||||||
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
|
tf->lineDefined=LoadInt(L,Z,swap);
|
||||||
tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
|
tf->numparams=LoadInt(L,Z,swap);
|
||||||
tf->numparams=LoadInt(L,Z,"too many parameters");
|
|
||||||
tf->is_vararg=LoadByte(L,Z);
|
tf->is_vararg=LoadByte(L,Z);
|
||||||
tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
|
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||||
LoadCode(L,tf,Z);
|
LoadCode(L,tf,Z,swap);
|
||||||
LoadLocals(L,tf,Z);
|
LoadLocals(L,tf,Z,swap);
|
||||||
LoadConstants(L,tf,Z,native);
|
LoadLines(L,tf,Z,swap);
|
||||||
|
LoadConstants(L,tf,Z,swap);
|
||||||
return tf;
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,11 +170,21 @@ static void LoadSignature (lua_State* L, ZIO* Z)
|
|||||||
if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
|
||||||
|
{
|
||||||
|
int r=ezgetc(L,Z);
|
||||||
|
if (r!=s)
|
||||||
|
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||||
|
" %s is %d but read %d",ZNAME(Z),what,r,s);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
||||||
#define V(v) v/16,v%16
|
#define V(v) v/16,v%16
|
||||||
|
|
||||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||||
{
|
{
|
||||||
int version,sizeofR,native;
|
int version,swap;
|
||||||
|
Number f=0,tf=TEST_NUMBER;
|
||||||
LoadSignature(L,Z);
|
LoadSignature(L,Z);
|
||||||
version=ezgetc(L,Z);
|
version=ezgetc(L,Z);
|
||||||
if (version>VERSION)
|
if (version>VERSION)
|
||||||
@@ -204,36 +195,20 @@ static int LoadHeader (lua_State* L, ZIO* Z)
|
|||||||
luaL_verror(L,"`%.255s' too old:\n"
|
luaL_verror(L,"`%.255s' too old:\n"
|
||||||
" read version %d.%d; expected at least %d.%d",
|
" read version %d.%d; expected at least %d.%d",
|
||||||
ZNAME(Z),V(version),V(VERSION));
|
ZNAME(Z),V(version),V(VERSION));
|
||||||
{
|
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
||||||
int I=ezgetc(L,Z);
|
TESTSIZE(sizeof(int));
|
||||||
int i=ezgetc(L,Z);
|
TESTSIZE(sizeof(size_t));
|
||||||
int op=ezgetc(L,Z);
|
TESTSIZE(sizeof(Instruction));
|
||||||
int b=ezgetc(L,Z);
|
TESTSIZE(SIZE_INSTRUCTION);
|
||||||
if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
|
TESTSIZE(SIZE_OP);
|
||||||
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
|
TESTSIZE(SIZE_B);
|
||||||
" read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
|
TESTSIZE(sizeof(Number));
|
||||||
" expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
|
f=LoadNumber(L,Z,swap);
|
||||||
ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
|
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||||
}
|
luaL_verror(L,"unknown number format in `%.255s':\n"
|
||||||
sizeofR=ezgetc(L,Z);
|
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||||
native=(sizeofR!=0);
|
ZNAME(Z),f,tf);
|
||||||
if (native) /* test number representation */
|
return swap;
|
||||||
{
|
|
||||||
if (sizeofR!=sizeof(Number))
|
|
||||||
luaL_verror(L,"native number size mismatch in `%.255s':\n"
|
|
||||||
" read %d; expected %d",
|
|
||||||
ZNAME(Z),sizeofR,(int)sizeof(Number));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Number f=0,tf=TEST_NUMBER;
|
|
||||||
LoadBlock(L,&f,sizeof(f),Z);
|
|
||||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
|
||||||
luaL_verror(L,"unknown number format in `%.255s':\n"
|
|
||||||
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
|
||||||
ZNAME(Z),f,tf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return native;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||||
@@ -245,23 +220,26 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
|||||||
** load one chunk from a file or buffer
|
** load one chunk from a file or buffer
|
||||||
** return main if ok and NULL at EOF
|
** return main if ok and NULL at EOF
|
||||||
*/
|
*/
|
||||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z)
|
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||||
{
|
{
|
||||||
int c=zgetc(Z);
|
int c=zgetc(Z);
|
||||||
if (c==ID_CHUNK)
|
if (c==ID_CHUNK)
|
||||||
return LoadChunk(L,Z);
|
return LoadChunk(L,Z);
|
||||||
else if (c!=EOZ)
|
else if (c!=EOZ)
|
||||||
luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
|
luaL_verror(L,"`%.255s' is not a precompiled Lua chunk",ZNAME(Z));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
Proto* luaU_undump1 (lua_State* L, ZIO* Z)
|
||||||
* convert number from text
|
|
||||||
*/
|
|
||||||
double luaU_str2d (lua_State* L, const char* b, const char* where)
|
|
||||||
{
|
{
|
||||||
double x;
|
return luaU_undump(L,Z);
|
||||||
if (!luaO_str2d(b,&x))
|
}
|
||||||
luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
|
|
||||||
return x;
|
/*
|
||||||
|
** find byte order
|
||||||
|
*/
|
||||||
|
int luaU_endianess (void)
|
||||||
|
{
|
||||||
|
int x=1;
|
||||||
|
return *(char*)&x;
|
||||||
}
|
}
|
||||||
|
|||||||
10
lundump.h
10
lundump.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp $
|
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 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
|
||||||
*/
|
*/
|
||||||
@@ -11,10 +11,11 @@
|
|||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
/* load one chunk */
|
/* load one chunk */
|
||||||
|
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
||||||
|
|
||||||
/* convert number from text */
|
/* find byte order */
|
||||||
double luaU_str2d (lua_State* L, const char* b, const char* where);
|
int luaU_endianess (void);
|
||||||
|
|
||||||
/* definitions for headers of binary files */
|
/* definitions for headers of binary files */
|
||||||
#define VERSION 0x40 /* last format change was in 4.0 */
|
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||||
@@ -37,7 +38,4 @@ double luaU_str2d (lua_State* L, const char* b, const char* where);
|
|||||||
/* multiplying by 1E8 gives non-trivial integer values */
|
/* multiplying by 1E8 gives non-trivial integer values */
|
||||||
#define TEST_NUMBER 3.14159265358979323846E8
|
#define TEST_NUMBER 3.14159265358979323846E8
|
||||||
|
|
||||||
/* something for testing byte order in instructions */
|
|
||||||
#define TEST_CODE 0x01020304
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user