lua/lundump.c

280 lines
6.5 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
2000-06-26 23:28:31 +04:00
** $Id: lundump.c,v 1.22 2000/06/12 13:52:05 roberto Exp roberto $
1998-01-14 18:49:01 +03:00
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#include <stdio.h>
1999-03-31 00:29:34 +04:00
#include <string.h>
1999-12-02 22:11:51 +03:00
#define LUA_REENTRANT
#include "lua.h"
1998-01-14 18:49:01 +03:00
#include "lauxlib.h"
#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"
1999-12-02 22:11:51 +03:00
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
2000-04-25 20:44:31 +04:00
#define LoadByte ezgetc
static const char* ZNAME(ZIO* Z)
{
const char* s=zname(Z);
return (*s=='@') ? s+1 : s;
}
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
{
2000-04-25 20:44:31 +04:00
luaL_verror(L,"unexpected end of file in `%.255s'",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
}
1999-12-02 22:11:51 +03:00
static unsigned int LoadWord (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
unsigned int hi=ezgetc(L,Z);
unsigned int lo=ezgetc(L,Z);
1998-01-14 18:49:01 +03:00
return (hi<<8)|lo;
}
1999-12-02 22:11:51 +03:00
static unsigned long LoadLong (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
unsigned long hi=LoadWord(L,Z);
unsigned long lo=LoadWord(L,Z);
1998-03-26 17:50:19 +03:00
return (hi<<16)|lo;
1998-01-14 18:49:01 +03:00
}
2000-03-10 21:37:44 +03:00
static Number LoadNumber (lua_State* L, ZIO* Z)
{
2000-03-03 17:58:26 +03:00
char b[256];
int size=ezgetc(L,Z);
LoadBlock(L,b,size,Z);
b[size]=0;
2000-04-25 20:44:31 +04:00
return luaU_str2d(L,b,ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
1999-03-31 00:29:34 +04:00
{
1999-12-02 22:11:51 +03:00
unsigned long l=LoadLong(L,Z);
1999-03-31 00:29:34 +04:00
unsigned int i=l;
2000-04-25 20:44:31 +04:00
if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
" read %lu; expected %u",message,ZNAME(Z),l,-1);
1999-03-31 00:29:34 +04:00
return i;
}
2000-03-10 21:37:44 +03:00
static TString* LoadString (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
long size=LoadLong(L,Z);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
1999-12-02 22:11:51 +03:00
char* s=luaL_openspace(L,size);
LoadBlock(L,s,size,Z);
2000-03-03 17:58:26 +03:00
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
2000-04-25 20:44:31 +04:00
static void SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
{
unsigned char* p;
2000-06-26 23:28:31 +04:00
unsigned char c;
2000-04-25 20:44:31 +04:00
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);
LoadBlock(L,tf->code,size*sizeof(*tf->code),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);
}
}
2000-03-10 21:37:44 +03:00
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-04-25 20:44:31 +04:00
int i,n=LoadInt(L,Z,"too many locals");
1998-01-14 18:49:01 +03:00
if (n==0) return;
1999-12-02 22:11:51 +03:00
tf->locvars=luaM_newvector(L,n+1,LocVar);
1998-01-14 18:49:01 +03:00
for (i=0; i<n; i++)
{
2000-06-26 23:28:31 +04:00
tf->locvars[i].pc=LoadInt(L,Z,"too many lines");
2000-03-03 17:58:26 +03:00
tf->locvars[i].varname=LoadString(L,Z);
1998-01-14 18:49:01 +03:00
}
2000-06-26 23:28:31 +04:00
tf->locvars[i].pc=-1; /* flag end of vector */
1998-01-14 18:49:01 +03:00
tf->locvars[i].varname=NULL;
}
2000-03-10 21:37:44 +03:00
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
1998-03-26 17:50:19 +03:00
2000-03-10 21:37:44 +03:00
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
1998-01-14 18:49:01 +03:00
{
2000-03-03 17:58:26 +03:00
int i,n;
2000-04-25 20:44:31 +04:00
tf->nkstr=n=LoadInt(L,Z,"too many strings");
2000-03-03 17:58:26 +03:00
if (n>0)
1998-01-14 18:49:01 +03:00
{
2000-03-10 21:37:44 +03:00
tf->kstr=luaM_newvector(L,n,TString*);
2000-04-25 20:44:31 +04:00
for (i=0; i<n; i++)
{
TString* s=LoadString(L,Z);
LoadByte(L,Z);
2000-04-25 20:44:31 +04:00
tf->kstr[i]=s;
}
2000-03-03 17:58:26 +03:00
}
2000-04-25 20:44:31 +04:00
tf->nknum=n=LoadInt(L,Z,"too many numbers");
2000-03-03 17:58:26 +03:00
if (n>0)
{
2000-03-10 21:37:44 +03:00
tf->knum=luaM_newvector(L,n,Number);
2000-03-03 17:58:26 +03:00
if (native)
2000-04-25 20:44:31 +04:00
LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
2000-03-03 17:58:26 +03:00
else
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
}
2000-04-25 20:44:31 +04:00
tf->nkproto=n=LoadInt(L,Z,"too many functions");
2000-03-03 17:58:26 +03:00
if (n>0)
{
2000-03-10 21:37:44 +03:00
tf->kproto=luaM_newvector(L,n,Proto*);
2000-03-03 17:58:26 +03:00
for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
1998-01-14 18:49:01 +03:00
}
}
2000-03-10 21:37:44 +03:00
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
1998-01-14 18:49:01 +03:00
{
2000-03-10 21:37:44 +03:00
Proto* tf=luaF_newproto(L);
2000-03-03 17:58:26 +03:00
tf->source=LoadString(L,Z);
1999-12-02 22:11:51 +03:00
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
2000-04-25 20:44:31 +04:00
tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
tf->numparams=LoadInt(L,Z,"too many parameters");
tf->is_vararg=LoadByte(L,Z);
tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
2000-03-03 17:58:26 +03:00
LoadCode(L,tf,Z);
1999-12-02 22:11:51 +03:00
LoadLocals(L,tf,Z);
LoadConstants(L,tf,Z,native);
1998-01-14 18:49:01 +03:00
return tf;
}
1999-12-02 22:11:51 +03:00
static void LoadSignature (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-08-17 00:52:00 +04:00
const char* s=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;
2000-04-25 20:44:31 +04:00
if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
2000-03-03 17:58:26 +03:00
#define V(v) v/16,v%16
1999-12-02 22:11:51 +03:00
static int LoadHeader (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
int version,sizeofR,native;
LoadSignature(L,Z);
version=ezgetc(L,Z);
1998-01-14 18:49:01 +03:00
if (version>VERSION)
2000-04-25 20:44:31 +04:00
luaL_verror(L,"`%.255s' too new:\n"
" read version %d.%d; expected at most %d.%d",
ZNAME(Z),V(version),V(VERSION));
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
2000-04-25 20:44:31 +04:00
luaL_verror(L,"`%.255s' too old:\n"
" read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION));
{
int I=ezgetc(L,Z);
int i=ezgetc(L,Z);
int op=ezgetc(L,Z);
int b=ezgetc(L,Z);
if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
" read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
" expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
}
1999-12-02 22:11:51 +03:00
sizeofR=ezgetc(L,Z);
native=(sizeofR!=0);
if (native) /* test number representation */
{
2000-03-10 21:37:44 +03:00
if (sizeofR!=sizeof(Number))
2000-04-25 20:44:31 +04:00
luaL_verror(L,"native number size mismatch in `%.255s':\n"
" read %d; expected %d",
ZNAME(Z),sizeofR,(int)sizeof(Number));
else
{
2000-03-10 21:37:44 +03:00
Number f=0,tf=TEST_NUMBER;
2000-03-03 17:58:26 +03:00
LoadBlock(L,&f,sizeof(f),Z);
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
2000-04-25 20:44:31 +04:00
luaL_verror(L,"unknown number format in `%.255s':\n"
" read " NUMBER_FMT "; expected " NUMBER_FMT,
ZNAME(Z),f,tf);
}
}
return native;
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
{
1999-12-02 22:11:51 +03:00
return LoadFunction(L,Z,LoadHeader(L,Z));
1998-01-14 18:49:01 +03:00
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
2000-03-10 21:37:44 +03:00
Proto* luaU_undump1 (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
if (c==ID_CHUNK)
1999-12-02 22:11:51 +03:00
return LoadChunk(L,Z);
1998-01-14 18:49:01 +03:00
else if (c!=EOZ)
2000-04-25 20:44:31 +04:00
luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
1998-01-14 18:49:01 +03:00
return NULL;
}
1999-03-31 00:29:34 +04:00
1999-12-02 22:11:51 +03:00
/*
* convert number from text
*/
double luaU_str2d (lua_State* L, const char* b, const char* where)
{
double x;
if (!luaO_str2d(b,&x))
2000-04-25 20:44:31 +04:00
luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
1999-12-02 22:11:51 +03:00
return x;
1999-03-31 00:29:34 +04:00
}