lua/lundump.c

249 lines
5.4 KiB
C
Raw Normal View History

1998-01-14 07:49:01 -08:00
/*
2001-02-23 09:17:25 -08:00
** $Id: lundump.c,v 1.38 2001/01/15 16:13:24 roberto Exp roberto $
1998-01-14 07:49:01 -08:00
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#include <stdio.h>
1999-03-30 12:29:34 -08:00
#include <string.h>
1999-12-02 11:11:51 -08:00
1998-01-14 07:49:01 -08:00
#include "lfunc.h"
#include "lmem.h"
1999-03-30 12:29:34 -08:00
#include "lopcodes.h"
1998-01-14 07:49:01 -08:00
#include "lstring.h"
#include "lundump.h"
2000-04-25 09:44:31 -07:00
#define LoadByte ezgetc
2001-02-23 09:17:25 -08:00
static const l_char* ZNAME (ZIO* Z)
2000-04-25 09:44:31 -07:00
{
2001-02-23 09:17:25 -08:00
const l_char* s=zname(Z);
return (*s==l_c('@')) ? s+1 : s;
2000-04-25 09:44:31 -07:00
}
1998-03-26 06:50:19 -08:00
1999-12-02 11:11:51 -08:00
static void unexpectedEOZ (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("unexpected end of file in `%.99s'"),ZNAME(Z));
1998-01-14 07:49:01 -08:00
}
1999-12-02 11:11:51 -08:00
static int ezgetc (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
int c=zgetc(Z);
1999-12-02 11:11:51 -08:00
if (c==EOZ) unexpectedEOZ(L,Z);
1998-01-14 07:49:01 -08:00
return c;
}
1999-12-02 11:11:51 -08:00
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
1998-01-14 07:49:01 -08:00
{
int r=zread(Z,b,n);
1999-12-02 11:11:51 -08:00
if (r!=0) unexpectedEOZ(L,Z);
1998-01-14 07:49:01 -08:00
}
2000-09-20 11:43:54 -07:00
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
1998-01-14 07:49:01 -08:00
{
2000-09-20 11:43:54 -07:00
if (swap)
{
2001-02-23 09:17:25 -08:00
l_char *p=(l_char *) b+size-1;
2000-09-20 11:43:54 -07:00
int n=size;
2001-02-23 09:17:25 -08:00
while (n--) *p--=(l_char)ezgetc(L,Z);
2000-09-20 11:43:54 -07:00
}
else
ezread(L,Z,b,size);
1998-01-14 07:49:01 -08:00
}
2000-09-20 11:43:54 -07:00
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
1998-01-14 07:49:01 -08:00
{
2000-09-04 11:53:41 -07:00
if (swap)
2000-09-20 11:43:54 -07:00
{
2001-02-23 09:17:25 -08:00
l_char *q=(l_char *) b;
2000-09-20 11:43:54 -07:00
while (m--)
{
2001-02-23 09:17:25 -08:00
l_char *p=q+size-1;
2000-09-20 11:43:54 -07:00
int n=size;
2001-02-23 09:17:25 -08:00
while (n--) *p--=(l_char)ezgetc(L,Z);
2000-09-20 11:43:54 -07:00
q+=size;
}
}
2000-09-04 11:53:41 -07:00
else
2000-09-20 11:43:54 -07:00
ezread(L,Z,b,m*size);
}
static int LoadInt (lua_State* L, ZIO* Z, int swap)
{
int x;
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 11:53:41 -07:00
return x;
1998-01-14 07:49:01 -08:00
}
2000-09-04 11:53:41 -07:00
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
{
2000-09-04 11:53:41 -07:00
size_t x;
2000-09-20 11:43:54 -07:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 11:53:41 -07:00
return x;
1998-01-14 07:49:01 -08:00
}
static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
1999-03-30 12:29:34 -08:00
{
lua_Number x;
2000-09-20 11:43:54 -07:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 11:53:41 -07:00
return x;
1999-03-30 12:29:34 -08:00
}
2000-09-04 11:53:41 -07:00
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
1998-01-14 07:49:01 -08:00
{
2000-09-04 11:53:41 -07:00
size_t size=LoadSize(L,Z,swap);
1998-03-26 06:50:19 -08:00
if (size==0)
return NULL;
1998-01-14 07:49:01 -08:00
else
{
2001-02-23 09:17:25 -08:00
l_char* s=luaO_openspace(L,size);
2000-09-20 11:43:54 -07:00
LoadBlock(L,s,size,Z,0);
2001-02-23 09:17:25 -08:00
return luaS_newlstr(L,s,size-1); /* remove trailing l_c('\0') */
1998-01-14 07:49:01 -08:00
}
}
2000-09-04 11:53:41 -07:00
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
2000-04-25 09:44:31 -07:00
{
2000-12-28 04:59:41 -08:00
int size=LoadInt(L,Z,swap);
2000-09-04 11:53:41 -07:00
tf->code=luaM_newvector(L,size,Instruction);
2000-12-28 04:59:41 -08:00
tf->sizecode=size;
2000-09-20 11:43:54 -07:00
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
2000-04-25 09:44:31 -07:00
}
2000-09-04 11:53:41 -07:00
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
2000-04-25 09:44:31 -07:00
{
2000-09-04 11:53:41 -07:00
int i,n;
2000-12-28 04:59:41 -08:00
n=LoadInt(L,Z,swap);
2000-09-04 11:53:41 -07:00
tf->locvars=luaM_newvector(L,n,LocVar);
2000-12-28 04:59:41 -08:00
tf->sizelocvars=n;
2000-09-04 11:53:41 -07:00
for (i=0; i<n; i++)
2000-04-25 09:44:31 -07:00
{
2000-09-04 11:53:41 -07:00
tf->locvars[i].varname=LoadString(L,Z,swap);
tf->locvars[i].startpc=LoadInt(L,Z,swap);
tf->locvars[i].endpc=LoadInt(L,Z,swap);
2000-04-25 09:44:31 -07:00
}
}
2000-09-04 11:53:41 -07:00
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
2000-10-20 09:39:03 -07:00
int n;
2000-12-28 04:59:41 -08:00
n=LoadInt(L,Z,swap);
2000-09-04 11:53:41 -07:00
tf->lineinfo=luaM_newvector(L,n,int);
2000-12-28 04:59:41 -08:00
tf->sizelineinfo=n;
2000-09-20 11:43:54 -07:00
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
1998-01-14 07:49:01 -08:00
}
2000-09-04 11:53:41 -07:00
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
1998-03-26 06:50:19 -08:00
2000-09-04 11:53:41 -07:00
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
1998-01-14 07:49:01 -08:00
{
2000-03-03 06:58:26 -08:00
int i,n;
2000-12-28 04:59:41 -08:00
n=LoadInt(L,Z,swap);
2000-09-04 11:53:41 -07:00
tf->kstr=luaM_newvector(L,n,TString*);
2000-12-28 04:59:41 -08:00
tf->sizekstr=n;
2000-09-04 11:53:41 -07:00
for (i=0; i<n; i++)
tf->kstr[i]=LoadString(L,Z,swap);
2000-12-28 04:59:41 -08:00
n=LoadInt(L,Z,swap);
tf->knum=luaM_newvector(L,n,lua_Number);
2000-12-28 04:59:41 -08:00
tf->sizeknum=n;
2000-09-20 11:43:54 -07:00
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
2000-12-28 04:59:41 -08:00
n=LoadInt(L,Z,swap);
2000-09-04 11:53:41 -07:00
tf->kproto=luaM_newvector(L,n,Proto*);
2000-12-28 04:59:41 -08:00
tf->sizekproto=n;
2000-09-04 11:53:41 -07:00
for (i=0; i<n; i++)
tf->kproto[i]=LoadFunction(L,Z,swap);
}
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
1998-01-14 07:49:01 -08:00
{
2000-03-10 10:37:44 -08:00
Proto* tf=luaF_newproto(L);
2000-09-04 11:53:41 -07:00
tf->source=LoadString(L,Z,swap);
tf->lineDefined=LoadInt(L,Z,swap);
tf->numparams=LoadInt(L,Z,swap);
2000-04-25 09:44:31 -07:00
tf->is_vararg=LoadByte(L,Z);
2000-09-04 11:53:41 -07:00
tf->maxstacksize=LoadInt(L,Z,swap);
LoadLocals(L,tf,Z,swap);
LoadLines(L,tf,Z,swap);
LoadConstants(L,tf,Z,swap);
2000-10-20 09:39:03 -07:00
LoadCode(L,tf,Z,swap);
1998-01-14 07:49:01 -08:00
return tf;
}
1999-12-02 11:11:51 -08:00
static void LoadSignature (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
2001-02-23 09:17:25 -08:00
const l_char* s=SIGNATURE;
1999-12-02 11:11:51 -08:00
while (*s!=0 && ezgetc(L,Z)==*s)
1998-01-14 07:49:01 -08:00
++s;
2001-02-23 09:17:25 -08:00
if (*s!=0) luaO_verror(L,l_s("bad signature in `%.99s'"),ZNAME(Z));
1998-01-14 07:49:01 -08:00
}
2001-02-23 09:17:25 -08:00
static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z)
2000-09-04 11:53:41 -07:00
{
int r=ezgetc(L,Z);
if (r!=s)
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("virtual machine mismatch in `%.99s':\n")
l_s(" %.20s is %d but read %d"),ZNAME(Z),what,s,r);
2000-09-04 11:53:41 -07:00
}
#define TESTSIZE(s) TestSize(L,s,#s,Z)
2000-03-03 06:58:26 -08:00
#define V(v) v/16,v%16
1999-12-02 11:11:51 -08:00
static int LoadHeader (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
2000-09-04 11:53:41 -07:00
int version,swap;
lua_Number f=0,tf=TEST_NUMBER;
1999-12-02 11:11:51 -08:00
LoadSignature(L,Z);
version=ezgetc(L,Z);
1998-01-14 07:49:01 -08:00
if (version>VERSION)
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("`%.99s' too new:\n")
l_s(" read version %d.%d; expected at most %d.%d"),
2000-04-25 09:44:31 -07:00
ZNAME(Z),V(version),V(VERSION));
1998-06-25 09:48:44 -07:00
if (version<VERSION0) /* check last major change */
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("`%.99s' too old:\n")
l_s(" read version %d.%d; expected at least %d.%d"),
2000-04-25 09:44:31 -07:00
ZNAME(Z),V(version),V(VERSION));
2000-09-04 11:53:41 -07:00
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
TESTSIZE(sizeof(int));
TESTSIZE(sizeof(size_t));
TESTSIZE(sizeof(Instruction));
TESTSIZE(SIZE_INSTRUCTION);
TESTSIZE(SIZE_OP);
TESTSIZE(SIZE_B);
TESTSIZE(sizeof(lua_Number));
2000-09-04 11:53:41 -07:00
f=LoadNumber(L,Z,swap);
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("unknown number format in `%.99s':\n")
l_s(" read ") NUMBER_FMT l_s("; expected ") NUMBER_FMT, ZNAME(Z),f,tf);
2000-09-04 11:53:41 -07:00
return swap;
1998-01-14 07:49:01 -08:00
}
2000-03-10 10:37:44 -08:00
static Proto* LoadChunk (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
1999-12-02 11:11:51 -08:00
return LoadFunction(L,Z,LoadHeader(L,Z));
1998-01-14 07:49:01 -08:00
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
2000-09-04 11:53:41 -07:00
Proto* luaU_undump (lua_State* L, ZIO* Z)
1998-01-14 07:49:01 -08:00
{
2000-09-18 12:42:05 -07:00
Proto* tf=NULL;
1998-01-14 07:49:01 -08:00
int c=zgetc(Z);
if (c==ID_CHUNK)
2000-09-18 12:42:05 -07:00
tf=LoadChunk(L,Z);
c=zgetc(Z);
if (c!=EOZ)
2001-02-23 09:17:25 -08:00
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
2000-09-18 12:42:05 -07:00
return tf;
2000-09-04 11:53:41 -07:00
}
1999-12-02 11:11:51 -08:00
/*
2000-09-04 11:53:41 -07:00
** find byte order
1999-12-02 11:11:51 -08:00
*/
2000-09-04 11:53:41 -07:00
int luaU_endianess (void)
1999-12-02 11:11:51 -08:00
{
2000-09-04 11:53:41 -07:00
int x=1;
2001-02-23 09:17:25 -08:00
return *(l_char*)&x;
1999-03-30 12:29:34 -08:00
}