make all dumps/loads go trhough Load/DumpVector (so it is easier

to adapt the code to correct endianess, if needed)
This commit is contained in:
Roberto Ierusalimschy 2014-03-11 15:56:27 -03:00
parent e976384213
commit 80fe8504f5
2 changed files with 20 additions and 8 deletions

14
ldump.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldump.c,v 2.25 2014/03/10 17:56:32 roberto Exp roberto $
** $Id: ldump.c,v 2.26 2014/03/11 18:05:46 roberto Exp roberto $
** save precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -25,9 +25,14 @@ typedef struct {
} DumpState;
#define DumpVar(x,D) DumpBlock(&x,sizeof(x),D)
/*
** All high-level dumps go through DumpVector; you can change it to
** change the endianess of the result
*/
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
static void DumpBlock (const void *b, size_t size, DumpState *D) {
if (D->status == 0) {
@ -38,6 +43,9 @@ static void DumpBlock (const void *b, size_t size, DumpState *D) {
}
#define DumpVar(x,D) DumpVector(&x,1,D)
static void DumpByte (int y, DumpState *D) {
lu_byte x = (lu_byte)y;
DumpVar(x, D);
@ -160,8 +168,6 @@ static void DumpFunction (const Proto *f, DumpState *D) {
}
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
static void DumpHeader (DumpState *D) {
DumpLiteral(LUA_SIGNATURE, D);
DumpByte(LUAC_VERSION, D);

View File

@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 2.32 2014/03/10 19:50:19 roberto Exp roberto $
** $Id: lundump.c,v 2.33 2014/03/11 18:05:46 roberto Exp roberto $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -21,9 +21,6 @@
#include "lzio.h"
#define LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
#if !defined(luai_verifycode)
#define luai_verifycode(L,b,f) /* empty */
#endif
@ -43,12 +40,21 @@ static l_noret error(LoadState *S, const char *why) {
}
/*
** All high-level loads go through LoadVector; you can change it to
** adapt to the endianess of the input
*/
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
static void LoadBlock (LoadState *S, void *b, size_t size) {
if (luaZ_read(S->Z, b, size) != 0)
error(S, "truncated");
}
#define LoadVar(S,x) LoadVector(S,&x,1)
static lu_byte LoadByte (LoadState *S) {
lu_byte x;
LoadVar(S, x);