1996-02-23 22:04:38 +03:00
|
|
|
/*
|
|
|
|
** undump.c
|
|
|
|
** load bytecodes from files
|
|
|
|
*/
|
|
|
|
|
1997-08-04 23:15:05 +04:00
|
|
|
char* rcs_undump="$Id: undump.c,v 1.25 1997/07/29 19:44:02 roberto Exp roberto $";
|
1996-02-23 22:04:38 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1997-04-14 16:12:40 +04:00
|
|
|
#include "auxlib.h"
|
1996-03-12 01:01:46 +03:00
|
|
|
#include "opcode.h"
|
1997-04-14 16:12:40 +04:00
|
|
|
#include "luamem.h"
|
1996-03-12 01:01:46 +03:00
|
|
|
#include "table.h"
|
1996-03-09 00:44:12 +03:00
|
|
|
#include "undump.h"
|
1997-06-16 20:50:22 +04:00
|
|
|
#include "zio.h"
|
1996-02-23 22:04:38 +03:00
|
|
|
|
1996-03-06 04:41:18 +03:00
|
|
|
static int swapword=0;
|
|
|
|
static int swapfloat=0;
|
1996-03-09 00:44:12 +03:00
|
|
|
static TFunc* Main=NULL; /* functions in a chunk */
|
|
|
|
static TFunc* lastF=NULL;
|
1996-03-06 04:41:18 +03:00
|
|
|
|
1996-03-09 00:44:12 +03:00
|
|
|
static void FixCode(Byte* code, Byte* end) /* swap words */
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1996-03-09 00:44:12 +03:00
|
|
|
Byte* p;
|
|
|
|
for (p=code; p!=end;)
|
|
|
|
{
|
1997-04-14 16:12:40 +04:00
|
|
|
int op=*p;
|
1996-03-09 00:44:12 +03:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case PUSHNIL:
|
|
|
|
case PUSH0:
|
|
|
|
case PUSH1:
|
|
|
|
case PUSH2:
|
|
|
|
case PUSHLOCAL0:
|
|
|
|
case PUSHLOCAL1:
|
|
|
|
case PUSHLOCAL2:
|
|
|
|
case PUSHLOCAL3:
|
|
|
|
case PUSHLOCAL4:
|
|
|
|
case PUSHLOCAL5:
|
|
|
|
case PUSHLOCAL6:
|
|
|
|
case PUSHLOCAL7:
|
|
|
|
case PUSHLOCAL8:
|
|
|
|
case PUSHLOCAL9:
|
|
|
|
case PUSHINDEXED:
|
|
|
|
case STORELOCAL0:
|
|
|
|
case STORELOCAL1:
|
|
|
|
case STORELOCAL2:
|
|
|
|
case STORELOCAL3:
|
|
|
|
case STORELOCAL4:
|
|
|
|
case STORELOCAL5:
|
|
|
|
case STORELOCAL6:
|
|
|
|
case STORELOCAL7:
|
|
|
|
case STORELOCAL8:
|
|
|
|
case STORELOCAL9:
|
|
|
|
case STOREINDEXED0:
|
|
|
|
case ADJUST0:
|
|
|
|
case EQOP:
|
|
|
|
case LTOP:
|
|
|
|
case LEOP:
|
|
|
|
case GTOP:
|
|
|
|
case GEOP:
|
|
|
|
case ADDOP:
|
|
|
|
case SUBOP:
|
|
|
|
case MULTOP:
|
|
|
|
case DIVOP:
|
|
|
|
case POWOP:
|
|
|
|
case CONCOP:
|
|
|
|
case MINUSOP:
|
|
|
|
case NOTOP:
|
|
|
|
case RETCODE0:
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case PUSHBYTE:
|
|
|
|
case PUSHLOCAL:
|
|
|
|
case STORELOCAL:
|
|
|
|
case STOREINDEXED:
|
|
|
|
case STORELIST0:
|
|
|
|
case ADJUST:
|
|
|
|
case RETCODE:
|
1997-04-14 16:12:40 +04:00
|
|
|
case VARARGS:
|
|
|
|
case STOREMAP:
|
1996-03-09 00:44:12 +03:00
|
|
|
p+=2;
|
|
|
|
break;
|
|
|
|
case STORELIST:
|
|
|
|
case CALLFUNC:
|
|
|
|
p+=3;
|
|
|
|
break;
|
|
|
|
case PUSHWORD:
|
|
|
|
case PUSHSELF:
|
|
|
|
case CREATEARRAY:
|
|
|
|
case ONTJMP:
|
|
|
|
case ONFJMP:
|
|
|
|
case JMP:
|
|
|
|
case UPJMP:
|
|
|
|
case IFFJMP:
|
|
|
|
case IFFUPJMP:
|
|
|
|
case SETLINE:
|
|
|
|
case PUSHGLOBAL:
|
|
|
|
case STOREGLOBAL:
|
|
|
|
{
|
|
|
|
Byte t;
|
|
|
|
t=p[1]; p[1]=p[2]; p[2]=t;
|
|
|
|
p+=3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STORERECORD:
|
|
|
|
{
|
|
|
|
int n=*++p;
|
|
|
|
p++;
|
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
Byte t;
|
|
|
|
t=p[0]; p[0]=p[1]; p[1]=t;
|
|
|
|
p+=2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
1997-04-14 16:12:40 +04:00
|
|
|
luaL_verror("corrupt binary file: bad opcode %d at %d\n",
|
|
|
|
op,(int)(p-code));
|
1996-03-09 00:44:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
|
1996-03-06 19:01:08 +03:00
|
|
|
static void Unthread(Byte* code, int i, int v)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
|
|
|
while (i!=0)
|
|
|
|
{
|
1996-11-07 16:59:51 +03:00
|
|
|
Word w;
|
1996-03-06 19:01:08 +03:00
|
|
|
Byte* p=code+i;
|
1996-11-07 17:13:28 +03:00
|
|
|
memcpy(&w,p,sizeof(w));
|
|
|
|
i=w; w=v;
|
|
|
|
memcpy(p,&w,sizeof(w));
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static int LoadWord(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
|
|
|
Word w;
|
1997-06-16 20:50:22 +04:00
|
|
|
zread(Z,&w,sizeof(w));
|
1996-03-06 04:41:18 +03:00
|
|
|
if (swapword)
|
|
|
|
{
|
1997-04-14 16:12:40 +04:00
|
|
|
Byte* p=(Byte*)&w;
|
1996-03-06 04:41:18 +03:00
|
|
|
Byte t;
|
|
|
|
t=p[0]; p[0]=p[1]; p[1]=t;
|
|
|
|
}
|
1996-02-23 22:04:38 +03:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static int LoadSize(ZIO* Z)
|
1996-03-07 00:40:10 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
Word hi=LoadWord(Z);
|
|
|
|
Word lo=LoadWord(Z);
|
1996-03-07 00:40:10 +03:00
|
|
|
int s=(hi<<16)|lo;
|
1996-03-09 00:44:12 +03:00
|
|
|
if ((Word)s != s) lua_error("code too long");
|
1996-03-07 00:40:10 +03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static void* LoadBlock(int size, ZIO* Z)
|
1996-03-12 01:01:46 +03:00
|
|
|
{
|
1996-11-16 23:14:23 +03:00
|
|
|
void* b=luaI_malloc(size);
|
1997-06-16 20:50:22 +04:00
|
|
|
zread(Z,b,size);
|
1996-03-12 01:01:46 +03:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static char* LoadString(ZIO* Z)
|
1996-03-12 01:01:46 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int size=LoadWord(Z);
|
1996-03-12 01:01:46 +03:00
|
|
|
char *b=luaI_buffer(size);
|
1997-06-16 20:50:22 +04:00
|
|
|
zread(Z,b,size);
|
1996-03-12 01:01:46 +03:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static char* LoadNewString(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
return LoadBlock(LoadWord(Z),Z);
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static void LoadFunction(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-07-29 23:44:02 +04:00
|
|
|
int size;
|
1996-03-06 19:01:08 +03:00
|
|
|
TFunc* tf=new(TFunc);
|
1996-02-29 02:10:46 +03:00
|
|
|
tf->next=NULL;
|
1996-03-06 19:01:08 +03:00
|
|
|
tf->locvars=NULL;
|
1997-07-29 23:44:02 +04:00
|
|
|
size=LoadSize(Z);
|
1997-06-16 20:50:22 +04:00
|
|
|
tf->lineDefined=LoadWord(Z);
|
1996-02-29 02:10:46 +03:00
|
|
|
if (IsMain(tf)) /* new main */
|
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
tf->fileName=LoadNewString(Z);
|
1996-02-24 19:44:28 +03:00
|
|
|
Main=lastF=tf;
|
1996-02-29 02:10:46 +03:00
|
|
|
}
|
1996-02-24 01:00:26 +03:00
|
|
|
else /* fix PUSHFUNCTION */
|
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
tf->marked=LoadWord(Z);
|
1996-02-29 02:10:46 +03:00
|
|
|
tf->fileName=Main->fileName;
|
1996-11-07 16:59:51 +03:00
|
|
|
memcpy(Main->code+tf->marked,&tf,sizeof(tf));
|
1996-03-01 06:43:50 +03:00
|
|
|
lastF=lastF->next=tf;
|
1996-02-24 01:00:26 +03:00
|
|
|
}
|
1997-07-29 23:44:02 +04:00
|
|
|
tf->code=LoadBlock(size,Z);
|
|
|
|
if (swapword || swapfloat) FixCode(tf->code,tf->code+size);
|
1996-02-24 01:00:26 +03:00
|
|
|
while (1) /* unthread */
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int c=zgetc(Z);
|
1996-03-01 06:43:50 +03:00
|
|
|
if (c==ID_VAR) /* global var */
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int i=LoadWord(Z);
|
|
|
|
char* s=LoadString(Z);
|
1996-03-12 01:01:46 +03:00
|
|
|
int v=luaI_findsymbolbyname(s);
|
1996-02-23 22:04:38 +03:00
|
|
|
Unthread(tf->code,i,v);
|
|
|
|
}
|
1996-03-01 06:43:50 +03:00
|
|
|
else if (c==ID_STR) /* constant string */
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int i=LoadWord(Z);
|
|
|
|
char* s=LoadString(Z);
|
1997-08-04 23:15:05 +04:00
|
|
|
int v; /*=luaI_findconstantbyname(s); ??????? */
|
1996-02-23 22:04:38 +03:00
|
|
|
Unthread(tf->code,i,v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
zungetc(Z);
|
1996-03-01 06:43:50 +03:00
|
|
|
break;
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static void LoadSignature(ZIO* Z)
|
1996-03-01 06:43:50 +03:00
|
|
|
{
|
1996-03-06 19:01:08 +03:00
|
|
|
char* s=SIGNATURE;
|
1997-06-16 20:50:22 +04:00
|
|
|
while (*s!=0 && zgetc(Z)==*s)
|
1996-03-01 06:43:50 +03:00
|
|
|
++s;
|
1997-04-14 16:12:40 +04:00
|
|
|
if (*s!=0) lua_error("cannot load binary file: bad signature");
|
1996-03-01 06:43:50 +03:00
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static void LoadHeader(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
|
|
|
Word w,tw=TEST_WORD;
|
|
|
|
float f,tf=TEST_FLOAT;
|
1996-11-14 14:44:34 +03:00
|
|
|
int version;
|
1997-06-16 20:50:22 +04:00
|
|
|
LoadSignature(Z);
|
|
|
|
version=zgetc(Z);
|
1996-11-14 18:00:32 +03:00
|
|
|
if (version>0x23) /* after 2.5 */
|
1996-11-14 14:44:34 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int oldsizeofW=zgetc(Z);
|
|
|
|
int oldsizeofF=zgetc(Z);
|
|
|
|
int oldsizeofP=zgetc(Z);
|
1996-11-18 14:18:29 +03:00
|
|
|
if (oldsizeofW!=2)
|
1997-04-14 16:12:40 +04:00
|
|
|
luaL_verror(
|
|
|
|
"cannot load binary file created on machine with sizeof(Word)=%d; "
|
|
|
|
"expected 2",oldsizeofW);
|
1996-11-18 14:18:29 +03:00
|
|
|
if (oldsizeofF!=4)
|
1997-04-14 16:12:40 +04:00
|
|
|
luaL_verror(
|
|
|
|
"cannot load binary file created on machine with sizeof(float)=%d; "
|
|
|
|
"expected 4\nnot an IEEE machine?",oldsizeofF);
|
|
|
|
if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
|
|
|
|
luaL_verror(
|
|
|
|
"cannot load binary file created on machine with sizeof(TFunc*)=%d; "
|
1997-06-16 20:50:22 +04:00
|
|
|
"expected %d",oldsizeofP,(int)sizeof(TFunc*));
|
1996-11-14 14:44:34 +03:00
|
|
|
}
|
1997-06-16 20:50:22 +04:00
|
|
|
zread(Z,&w,sizeof(w)); /* test word */
|
1996-03-06 04:41:18 +03:00
|
|
|
if (w!=tw)
|
|
|
|
{
|
|
|
|
swapword=1;
|
|
|
|
}
|
1997-06-16 20:50:22 +04:00
|
|
|
zread(Z,&f,sizeof(f)); /* test float */
|
1996-03-06 04:41:18 +03:00
|
|
|
if (f!=tf)
|
|
|
|
{
|
1997-04-14 16:12:40 +04:00
|
|
|
Byte* p=(Byte*)&f;
|
1996-03-09 00:44:12 +03:00
|
|
|
Byte t;
|
|
|
|
swapfloat=1;
|
|
|
|
t=p[0]; p[0]=p[3]; p[3]=t;
|
|
|
|
t=p[1]; p[1]=p[2]; p[2]=t;
|
|
|
|
if (f!=tf) /* TODO: try another perm? */
|
1997-04-14 16:12:40 +04:00
|
|
|
lua_error("cannot load binary file: unknown float representation");
|
1996-03-06 04:41:18 +03:00
|
|
|
}
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
|
1997-06-16 20:50:22 +04:00
|
|
|
static void LoadChunk(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
LoadHeader(Z);
|
1996-02-23 22:04:38 +03:00
|
|
|
while (1)
|
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int c=zgetc(Z);
|
|
|
|
if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-03-09 00:44:12 +03:00
|
|
|
/*
|
|
|
|
** load one chunk from a file.
|
|
|
|
** return list of functions found, headed by main, or NULL at EOF.
|
|
|
|
*/
|
1997-06-17 22:19:17 +04:00
|
|
|
TFunc* luaI_undump1(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
int c=zgetc(Z);
|
|
|
|
if (c==ID_CHUNK)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1997-06-16 20:50:22 +04:00
|
|
|
LoadChunk(Z);
|
|
|
|
return Main;
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
1997-06-16 20:50:22 +04:00
|
|
|
else if (c!=EOZ)
|
|
|
|
lua_error("not a lua binary file");
|
|
|
|
return NULL;
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|
|
|
|
|
1996-03-09 00:44:12 +03:00
|
|
|
/*
|
|
|
|
** load and run all chunks in a file
|
|
|
|
*/
|
1997-06-16 20:50:22 +04:00
|
|
|
int luaI_undump(ZIO* Z)
|
1996-02-23 22:04:38 +03:00
|
|
|
{
|
1996-03-09 00:44:12 +03:00
|
|
|
TFunc* m;
|
1997-06-16 20:50:22 +04:00
|
|
|
while ((m=luaI_undump1(Z)))
|
1996-02-26 22:44:17 +03:00
|
|
|
{
|
1996-03-12 01:01:46 +03:00
|
|
|
int status=luaI_dorun(m);
|
1997-08-04 23:15:05 +04:00
|
|
|
/* luaI_freefunc(m); ???*/
|
1996-03-12 01:01:46 +03:00
|
|
|
if (status!=0) return status;
|
1996-02-26 22:44:17 +03:00
|
|
|
}
|
1996-03-12 01:01:46 +03:00
|
|
|
return 0;
|
1996-02-23 22:04:38 +03:00
|
|
|
}
|