summaryrefslogtreecommitdiff
path: root/lib/lua/src/lundump.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lua/src/lundump.c')
-rw-r--r--lib/lua/src/lundump.c287
1 files changed, 112 insertions, 175 deletions
diff --git a/lib/lua/src/lundump.c b/lib/lua/src/lundump.c
index 7d9f031..a8692a8 100644
--- a/lib/lua/src/lundump.c
+++ b/lib/lua/src/lundump.c
@@ -1,286 +1,223 @@
/*
-** $Id: lundump.c,v 1.4 2004-11-27 21:46:07 pixel Exp $
-** load pre-compiled Lua chunks
+** $Id: lundump.c,v 1.5 2007-07-27 10:05:54 pixel Exp $
+** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
+#include <string.h>
+
#define lundump_c
+#define LUA_CORE
#include "lua.h"
#include "ldebug.h"
+#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
-#include "lopcodes.h"
+#include "lobject.h"
#include "lstring.h"
#include "lundump.h"
#include "lzio.h"
-#define LoadByte (lu_byte) ezgetc
-
typedef struct {
lua_State* L;
ZIO* Z;
Mbuffer* b;
- int swap;
const char* name;
} LoadState;
-static void unexpectedEOZ (LoadState* S)
-{
- luaG_runerror(S->L,"unexpected end of file in %s",S->name);
-}
+#ifdef LUAC_TRUST_BINARIES
+#define IF(c,s)
+#else
+#define IF(c,s) if (c) error(S,s)
-static int ezgetc (LoadState* S)
+static void error(LoadState* S, const char* why)
{
- int c=zgetc(S->Z);
- if (c==EOZ) unexpectedEOZ(S);
- return c;
+ luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
+ luaD_throw(S->L,LUA_ERRSYNTAX);
}
+#endif
-static void ezread (LoadState* S, void* b, int n)
-{
- int r=luaZ_read(S->Z,b,n);
- if (r!=0) unexpectedEOZ(S);
-}
+#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
+#define LoadByte(S) (lu_byte)LoadChar(S)
+#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
+#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
-static void LoadBlock (LoadState* S, void* b, size_t size)
+static void LoadBlock(LoadState* S, void* b, size_t size)
{
- if (S->swap)
- {
- char* p=(char*) b+size-1;
- int n=size;
- while (n--) *p--=(char)ezgetc(S);
- }
- else
- ezread(S,b,size);
+ size_t r=luaZ_read(S->Z,b,size);
+ IF (r!=0, "unexpected end");
}
-static void LoadVector (LoadState* S, void* b, int m, size_t size)
+static int LoadChar(LoadState* S)
{
- if (S->swap)
- {
- char* q=(char*) b;
- while (m--)
- {
- char* p=q+size-1;
- int n=size;
- while (n--) *p--=(char)ezgetc(S);
- q+=size;
- }
- }
- else
- ezread(S,b,m*size);
-}
-
-static int LoadInt (LoadState* S)
-{
- int x;
- LoadBlock(S,&x,sizeof(x));
- if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
+ char x;
+ LoadVar(S,x);
return x;
}
-static size_t LoadSize (LoadState* S)
+static int LoadInt(LoadState* S)
{
- size_t x;
- LoadBlock(S,&x,sizeof(x));
+ int x;
+ LoadVar(S,x);
+ IF (x<0, "bad integer");
return x;
}
-static lua_Number LoadNumber (LoadState* S)
+static lua_Number LoadNumber(LoadState* S)
{
lua_Number x;
- LoadBlock(S,&x,sizeof(x));
+ LoadVar(S,x);
return x;
}
-static TString* LoadString (LoadState* S)
+static TString* LoadString(LoadState* S)
{
- size_t size=LoadSize(S);
+ size_t size;
+ LoadVar(S,size);
if (size==0)
return NULL;
else
{
char* s=luaZ_openspace(S->L,S->b,size);
- ezread(S,s,size);
+ LoadBlock(S,s,size);
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
}
}
-static void LoadCode (LoadState* S, Proto* f)
+static void LoadCode(LoadState* S, Proto* f)
{
- int size=LoadInt(S);
- f->code=luaM_newvector(S->L,size,Instruction);
- f->sizecode=size;
- LoadVector(S,f->code,size,sizeof(*f->code));
-}
-
-static void LoadLocals (LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- f->locvars=luaM_newvector(S->L,n,LocVar);
- f->sizelocvars=n;
- for (i=0; i<n; i++)
- {
- f->locvars[i].varname=LoadString(S);
- f->locvars[i].startpc=LoadInt(S);
- f->locvars[i].endpc=LoadInt(S);
- }
-}
-
-static void LoadLines (LoadState* S, Proto* f)
-{
- int size=LoadInt(S);
- f->lineinfo=luaM_newvector(S->L,size,int);
- f->sizelineinfo=size;
- LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
-}
-
-static void LoadUpvalues (LoadState* S, Proto* f)
-{
- int i,n;
- n=LoadInt(S);
- if (n!=0 && n!=f->nups)
- luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d",
- S->name,n,f->nups);
- f->upvalues=luaM_newvector(S->L,n,TString*);
- f->sizeupvalues=n;
- for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
+ int n=LoadInt(S);
+ f->code=luaM_newvector(S->L,n,Instruction);
+ f->sizecode=n;
+ LoadVector(S,f->code,n,sizeof(Instruction));
}
-static Proto* LoadFunction (LoadState* S, TString* p);
+static Proto* LoadFunction(LoadState* S, TString* p);
-static void LoadConstants (LoadState* S, Proto* f)
+static void LoadConstants(LoadState* S, Proto* f)
{
int i,n;
n=LoadInt(S);
- f->k=luaM_newvector(S->L,n,TObject);
+ f->k=luaM_newvector(S->L,n,TValue);
f->sizek=n;
+ for (i=0; i<n; i++) setnilvalue(&f->k[i]);
for (i=0; i<n; i++)
{
- TObject* o=&f->k[i];
- int t=LoadByte(S);
+ TValue* o=&f->k[i];
+ int t=LoadChar(S);
switch (t)
{
+ case LUA_TNIL:
+ setnilvalue(o);
+ break;
+ case LUA_TBOOLEAN:
+ setbvalue(o,LoadChar(S));
+ break;
case LUA_TNUMBER:
setnvalue(o,LoadNumber(S));
break;
case LUA_TSTRING:
- setsvalue2n(o,LoadString(S));
- break;
- case LUA_TNIL:
- setnilvalue(o);
+ setsvalue2n(S->L,o,LoadString(S));
break;
default:
- luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
+ IF (1, "bad constant");
break;
}
}
n=LoadInt(S);
f->p=luaM_newvector(S->L,n,Proto*);
f->sizep=n;
+ for (i=0; i<n; i++) f->p[i]=NULL;
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
}
-static Proto* LoadFunction (LoadState* S, TString* p)
+static void LoadDebug(LoadState* S, Proto* f)
+{
+ int i,n;
+ n=LoadInt(S);
+ f->lineinfo=luaM_newvector(S->L,n,int);
+ f->sizelineinfo=n;
+ LoadVector(S,f->lineinfo,n,sizeof(int));
+ n=LoadInt(S);
+ f->locvars=luaM_newvector(S->L,n,LocVar);
+ f->sizelocvars=n;
+ for (i=0; i<n; i++) f->locvars[i].varname=NULL;
+ for (i=0; i<n; i++)
+ {
+ f->locvars[i].varname=LoadString(S);
+ f->locvars[i].startpc=LoadInt(S);
+ f->locvars[i].endpc=LoadInt(S);
+ }
+ n=LoadInt(S);
+ f->upvalues=luaM_newvector(S->L,n,TString*);
+ f->sizeupvalues=n;
+ for (i=0; i<n; i++) f->upvalues[i]=NULL;
+ for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
+}
+
+static Proto* LoadFunction(LoadState* S, TString* p)
{
Proto* f=luaF_newproto(S->L);
+ setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
f->source=LoadString(S); if (f->source==NULL) f->source=p;
- f->lineDefined=LoadInt(S);
+ f->linedefined=LoadInt(S);
+ f->lastlinedefined=LoadInt(S);
f->nups=LoadByte(S);
f->numparams=LoadByte(S);
f->is_vararg=LoadByte(S);
f->maxstacksize=LoadByte(S);
- LoadLines(S,f);
- LoadLocals(S,f);
- LoadUpvalues(S,f);
- LoadConstants(S,f);
LoadCode(S,f);
-#ifndef TRUST_BINARIES
- if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name);
-#endif
+ LoadConstants(S,f);
+ LoadDebug(S,f);
+ IF (!luaG_checkcode(f), "bad code");
+ S->L->top--;
return f;
}
-static void LoadSignature (LoadState* S)
-{
- const char* s=LUA_SIGNATURE;
- while (*s!=0 && ezgetc(S)==*s)
- ++s;
- if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
-}
-
-static void TestSize (LoadState* S, int s, const char* what)
-{
- int r=LoadByte(S);
- if (r!=s)
- luaG_runerror(S->L,"virtual machine mismatch in %s: "
- "size of %s is %d but read %d",S->name,what,s,r);
-}
-
-#define TESTSIZE(s,w) TestSize(S,s,w)
-#define V(v) v/16,v%16
-
-static void LoadHeader (LoadState* S)
-{
- int version;
- lua_Number x,tx=TEST_NUMBER;
- LoadSignature(S);
- version=LoadByte(S);
- if (version>VERSION)
- luaG_runerror(S->L,"%s too new: "
- "read version %d.%d; expected at most %d.%d",
- S->name,V(version),V(VERSION));
- if (version<VERSION0) /* check last major change */
- luaG_runerror(S->L,"%s too old: "
- "read version %d.%d; expected at least %d.%d",
- S->name,V(version),V(VERSION0));
- S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
- TESTSIZE(sizeof(int),"int");
- TESTSIZE(sizeof(size_t), "size_t");
- TESTSIZE(sizeof(Instruction), "Instruction");
- TESTSIZE(SIZE_OP, "OP");
- TESTSIZE(SIZE_A, "A");
- TESTSIZE(SIZE_B, "B");
- TESTSIZE(SIZE_C, "C");
- TESTSIZE(sizeof(lua_Number), "number");
- x=LoadNumber(S);
- if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
- luaG_runerror(S->L,"unknown number format in %s",S->name);
-}
-
-static Proto* LoadChunk (LoadState* S)
+static void LoadHeader(LoadState* S)
{
- LoadHeader(S);
- return LoadFunction(S,NULL);
+ char h[LUAC_HEADERSIZE];
+ char s[LUAC_HEADERSIZE];
+ luaU_header(h);
+ LoadBlock(S,s,LUAC_HEADERSIZE);
+ IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
}
/*
** load precompiled chunk
*/
-Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff)
+Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
{
LoadState S;
- const char* s=zname(Z);
- if (*s=='@' || *s=='=')
- S.name=s+1;
- else if (*s==LUA_SIGNATURE[0])
+ if (*name=='@' || *name=='=')
+ S.name=name+1;
+ else if (*name==LUA_SIGNATURE[0])
S.name="binary string";
else
- S.name=s;
+ S.name=name;
S.L=L;
S.Z=Z;
S.b=buff;
- return LoadChunk(&S);
+ LoadHeader(&S);
+ return LoadFunction(&S,luaS_newliteral(L,"=?"));
}
/*
-** find byte order
+* make header
*/
-int luaU_endianness (void)
+void luaU_header (char* h)
{
int x=1;
- return *(char*)&x;
+ memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
+ h+=sizeof(LUA_SIGNATURE)-1;
+ *h++=(char)LUAC_VERSION;
+ *h++=(char)LUAC_FORMAT;
+ *h++=(char)*(char*)&x; /* endianness */
+ *h++=(char)sizeof(int);
+ *h++=(char)sizeof(size_t);
+ *h++=(char)sizeof(Instruction);
+ *h++=(char)sizeof(lua_Number);
+ *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
}