commit 6c8a32217a04f8441fdd9233035e26930f498bf9
parent cb09f4fef482ba146dd1c18852edb0fe5efe3b07
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 17 May 2011 09:42:19 -0300
changes by lhf (better control of chars x bytes)
Diffstat:
M | ldump.c | | | 6 | +++--- |
M | lundump.c | | | 62 | +++++++++++++++++++++++++++++++++++++++++--------------------- |
M | lundump.h | | | 20 | ++++++-------------- |
3 files changed, 50 insertions(+), 38 deletions(-)
diff --git a/ldump.c b/ldump.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $
+** $Id: ldump.c,v 1.18 2011/05/06 13:35:17 lhf Exp $
** save precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -69,7 +69,7 @@ static void DumpString(const TString* s, DumpState* D)
{
size_t size=s->tsv.len+1; /* include trailing '\0' */
DumpVar(size,D);
- DumpBlock(getstr(s),size,D);
+ DumpBlock(getstr(s),size*sizeof(char),D);
}
}
@@ -150,7 +150,7 @@ static void DumpFunction(const Proto* f, DumpState* D)
static void DumpHeader(DumpState* D)
{
- char h[LUAC_HEADERSIZE];
+ lu_byte h[LUAC_HEADERSIZE];
luaU_header(h);
DumpBlock(h,LUAC_HEADERSIZE,D);
}
diff --git a/lundump.c b/lundump.c
@@ -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));
}
diff --git a/lundump.h b/lundump.h
@@ -1,5 +1,5 @@
/*
-** $Id: lundump.h,v 1.39 2005/11/01 17:04:55 lhf Exp lhf $
+** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -14,23 +14,15 @@
LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
/* make header; from lundump.c */
-LUAI_FUNC void luaU_header (char* h);
+LUAI_FUNC void luaU_header (lu_byte* h);
/* dump one chunk; from ldump.c */
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
-#ifdef luac_c
-/* print one chunk; from print.c */
-LUAI_FUNC void luaU_print (const Proto* f, int full);
-#endif
-
-/* for header of binary files -- this is Lua 5.1 */
-#define LUAC_VERSION 0x51
-
-/* for header of binary files -- this is the official format */
-#define LUAC_FORMAT 0
+/* data to catch conversion errors */
+#define LUAC_TAIL "\x19\x93\r\n\x1a\n"
-/* size of header of binary files */
-#define LUAC_HEADERSIZE 12
+/* size in bytes of header of binary files */
+#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char))
#endif