more explicit handling of headers for binary chunks

This commit is contained in:
Roberto Ierusalimschy 2014-02-27 13:56:20 -03:00
parent 986c11daa6
commit 054179c2ff
3 changed files with 54 additions and 60 deletions

16
ldump.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldump.c,v 2.18 2013/04/12 19:07:09 roberto Exp roberto $
** $Id: ldump.c,v 2.19 2013/04/26 18:48:35 roberto Exp roberto $
** save precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -159,9 +159,17 @@ static void DumpFunction(const Proto* f, DumpState* D)
static void DumpHeader(DumpState* D)
{
lu_byte h[LUAC_HEADERSIZE];
luaU_header(h);
DumpBlock(h,LUAC_HEADERSIZE,D);
DumpBlock(LUA_SIGNATURE,sizeof(LUA_SIGNATURE),D);
DumpBlock(LUAC_DATA,sizeof(LUAC_DATA),D);
DumpChar(LUAC_VERSION,D);
DumpChar(LUAC_FORMAT,D);
DumpChar(sizeof(int),D);
DumpChar(sizeof(size_t),D);
DumpChar(sizeof(Instruction),D);
DumpChar(sizeof(lua_Integer),D);
DumpChar(sizeof(lua_Number),D);
DumpInteger(LUAC_INT,D);
DumpNumber(LUAC_NUM,D);
}
/*

View File

@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 2.24 2013/08/16 18:55:49 roberto Exp roberto $
** $Id: lundump.c,v 2.25 2014/02/13 12:11:34 roberto Exp roberto $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -84,11 +84,9 @@ static TString* LoadString(LoadState* S)
return NULL;
else
{
TString* ts;
char* s=luaZ_openspace(S->L,S->b,size);
LoadBlock(S,s,size*sizeof(char));
ts = luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
return ts;
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
}
}
@ -193,23 +191,34 @@ static void LoadFunction(LoadState* S, Proto* f)
LoadDebug(S,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)
static void checkstring(LoadState *S, const char *s, const char *msg)
{
lu_byte h[LUAC_HEADERSIZE];
lu_byte s[LUAC_HEADERSIZE];
luaU_header(h);
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");
char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
LoadMem(S,buff,strlen(s)+1,sizeof(char));
if (strcmp(s,buff)!=0) error(S,msg);
}
static void fchecksize(LoadState *S, size_t size, const char *tname)
{
if (LoadByte(S) != size)
error(S,luaO_pushfstring(S->L,"%s size mismatch in",tname));
}
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
static void checkHeader(LoadState* S)
{
checkstring(S,LUA_SIGNATURE+1,"not a");
checkstring(S,LUAC_DATA,"corrupted");
if (LoadByte(S) != LUAC_VERSION) error(S,"version mismatch in");
if (LoadByte(S) != LUAC_FORMAT) error(S,"format mismatch in");
checksize(S,int);
checksize(S,size_t);
checksize(S,Instruction);
checksize(S,lua_Integer);
checksize(S,lua_Number);
if (LoadInteger(S) != LUAC_INT) error(S,"endianess mismatch in");
if (LoadNumber(S) != LUAC_NUM) error(S,"float format mismatch in");
}
/*
@ -228,7 +237,7 @@ Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
S.L=L;
S.Z=Z;
S.b=buff;
LoadHeader(&S);
checkHeader(&S);
cl=luaF_newLclosure(L,1);
setclLvalue(L,L->top,cl); incr_top(L);
cl->l.p=luaF_newproto(L);
@ -243,28 +252,3 @@ Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
luai_verifycode(L,buff,cl->l.p);
return cl;
}
#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 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 (lu_byte* h)
{
int x=1;
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));
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lundump.h,v 1.38 2011/05/17 12:42:43 roberto Exp roberto $
** $Id: lundump.h,v 1.39 2012/05/08 13:53:33 roberto Exp roberto $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -10,19 +10,21 @@
#include "lobject.h"
#include "lzio.h"
/* data to catch conversion errors */
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
#define LUAC_INT 0xABCD
#define LUAC_NUM 370.5
#define MYINT(s) (s[0]-'0')
#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
#define LUAC_FORMAT 0 /* this is the official format */
/* load one chunk; from lundump.c */
LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
/* make header; from lundump.c */
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);
/* data to catch conversion errors */
#define LUAC_TAIL "\x19\x93\r\n\x1a\n"
/* size in bytes of header of binary files */
#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char))
#endif