|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
-** $Id: lundump.c,v 2.15 2011/02/07 19:15:24 roberto Exp roberto $
|
|
|
+** $Id: lundump.c,v 1.69 2011/05/06 13:35:17 lhf Exp $
|
|
|
** load precompiled Lua chunks
|
|
|
** See Copyright Notice in lua.h
|
|
|
*/
|
|
@@ -38,6 +38,10 @@ static void error(LoadState* S, const char* why)
|
|
|
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
|
|
|
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
|
|
|
|
|
|
+#if !defined(luai_verifycode)
|
|
|
+#define luai_verifycode(L,b,f) (f)
|
|
|
+#endif
|
|
|
+
|
|
|
static void LoadBlock(LoadState* S, void* b, size_t size)
|
|
|
{
|
|
|
if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
|
|
@@ -54,6 +58,7 @@ static int LoadInt(LoadState* S)
|
|
|
{
|
|
|
int x;
|
|
|
LoadVar(S,x);
|
|
|
+ if (x<0) error(S,"corrupted");
|
|
|
return x;
|
|
|
}
|
|
|
|
|
@@ -73,7 +78,7 @@ static TString* LoadString(LoadState* S)
|
|
|
else
|
|
|
{
|
|
|
char* s=luaZ_openspace(S->L,S->b,size);
|
|
|
- LoadBlock(S,s,size);
|
|
|
+ LoadBlock(S,s,size*sizeof(char));
|
|
|
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
|
|
|
}
|
|
|
}
|
|
@@ -175,13 +180,23 @@ static Proto* LoadFunction(LoadState* S)
|
|
|
return f;
|
|
|
}
|
|
|
|
|
|
+/* the code below must be consistent with the code in luaU_header */
|
|
|
+#define N0 LUAC_HEADERSIZE
|
|
|
+#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
|
|
|
+#define N2 N1+2
|
|
|
+#define N3 N2+6
|
|
|
+
|
|
|
static void LoadHeader(LoadState* S)
|
|
|
{
|
|
|
- char h[LUAC_HEADERSIZE];
|
|
|
- char s[LUAC_HEADERSIZE];
|
|
|
+ lu_byte h[LUAC_HEADERSIZE];
|
|
|
+ lu_byte s[LUAC_HEADERSIZE];
|
|
|
luaU_header(h);
|
|
|
- LoadBlock(S,s,LUAC_HEADERSIZE-1); /* 1st char already read */
|
|
|
- if (memcmp(h+1,s,LUAC_HEADERSIZE-1)!=0) error(S,"incompatible");
|
|
|
+ memcpy(s,h,sizeof(char)); /* first char already read */
|
|
|
+ LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
|
|
|
+ if (memcmp(h,s,N0)==0) return;
|
|
|
+ if (memcmp(h,s,N1)!=0) error(S,"not a");
|
|
|
+ if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
|
|
|
+ if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -200,25 +215,30 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
|
|
|
S.Z=Z;
|
|
|
S.b=buff;
|
|
|
LoadHeader(&S);
|
|
|
- return LoadFunction(&S);
|
|
|
+ return luai_verifycode(L,buff,LoadFunction(&S));
|
|
|
}
|
|
|
|
|
|
+#define MYINT(s) (s[0]-'0')
|
|
|
+#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
|
|
|
+#define FORMAT 0 /* this is the official format */
|
|
|
+
|
|
|
/*
|
|
|
-* make header
|
|
|
-* if you make any changes in the header or in LUA_SIGNATURE,
|
|
|
-* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
|
|
|
+* make header for precompiled chunks
|
|
|
+* if you change the code below be sure to update LoadHeader and FORMAT above
|
|
|
+* and LUAC_HEADERSIZE in lundump.h
|
|
|
*/
|
|
|
-void luaU_header (char* h)
|
|
|
+void luaU_header (lu_byte* h)
|
|
|
{
|
|
|
int x=1;
|
|
|
- memcpy(h,LUA_SIGNATURE,(sizeof(LUA_SIGNATURE)-1)*sizeof(char));
|
|
|
- h+=(sizeof(LUA_SIGNATURE)-1)*sizeof(char);
|
|
|
- *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? */
|
|
|
+ memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
|
|
|
+ h+=sizeof(LUA_SIGNATURE)-sizeof(char);
|
|
|
+ *h++=cast_byte(VERSION);
|
|
|
+ *h++=cast_byte(FORMAT);
|
|
|
+ *h++=cast_byte(*(char*)&x); /* endianness */
|
|
|
+ *h++=cast_byte(sizeof(int));
|
|
|
+ *h++=cast_byte(sizeof(size_t));
|
|
|
+ *h++=cast_byte(sizeof(Instruction));
|
|
|
+ *h++=cast_byte(sizeof(lua_Number));
|
|
|
+ *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
|
|
|
+ memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
|
|
|
}
|