Browse Source

Small changes in the header of binary files

- LUAC_VERSION is equal to LUA_VERSION_NUM, and it is stored
as an int.

- 'sizeof(int)' and 'sizeof(size_t)' removed from the header, as
the binary format does not depend on these sizes. (It uses its
own serialization for unsigned integer values.)
pull/22/head
Roberto Ierusalimschy 6 years ago
parent
commit
f53eabeed8
  1. 4
      ldump.c
  2. 38
      lundump.c
  3. 3
      lundump.h
  4. 20
      testes/calls.lua

4
ldump.c

@ -198,11 +198,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
static void DumpHeader (DumpState *D) { static void DumpHeader (DumpState *D) {
DumpLiteral(LUA_SIGNATURE, D); DumpLiteral(LUA_SIGNATURE, D);
DumpByte(LUAC_VERSION, D); DumpInt(LUAC_VERSION, D);
DumpByte(LUAC_FORMAT, D); DumpByte(LUAC_FORMAT, D);
DumpLiteral(LUAC_DATA, D); DumpLiteral(LUAC_DATA, D);
DumpByte(sizeof(int), D);
DumpByte(sizeof(size_t), D);
DumpByte(sizeof(Instruction), D); DumpByte(sizeof(Instruction), D);
DumpByte(sizeof(lua_Integer), D); DumpByte(sizeof(lua_Integer), D);
DumpByte(sizeof(lua_Number), D); DumpByte(sizeof(lua_Number), D);

38
lundump.c

@ -10,6 +10,7 @@
#include "lprefix.h" #include "lprefix.h"
#include <limits.h>
#include <string.h> #include <string.h>
#include "lua.h" #include "lua.h"
@ -37,7 +38,7 @@ typedef struct {
static l_noret error (LoadState *S, const char *why) { static l_noret error (LoadState *S, const char *why) {
luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
luaD_throw(S->L, LUA_ERRSYNTAX); luaD_throw(S->L, LUA_ERRSYNTAX);
} }
@ -50,7 +51,7 @@ static l_noret error (LoadState *S, const char *why) {
static void LoadBlock (LoadState *S, void *b, size_t size) { static void LoadBlock (LoadState *S, void *b, size_t size) {
if (luaZ_read(S->Z, b, size) != 0) if (luaZ_read(S->Z, b, size) != 0)
error(S, "truncated"); error(S, "truncated chunk");
} }
@ -60,24 +61,32 @@ static void LoadBlock (LoadState *S, void *b, size_t size) {
static lu_byte LoadByte (LoadState *S) { static lu_byte LoadByte (LoadState *S) {
int b = zgetc(S->Z); int b = zgetc(S->Z);
if (b == EOZ) if (b == EOZ)
error(S, "truncated"); error(S, "truncated chunk");
return cast_byte(b); return cast_byte(b);
} }
static size_t LoadSize (LoadState *S) { static size_t LoadUnsigned (LoadState *S, size_t limit) {
size_t x = 0; size_t x = 0;
int b; int b;
limit >>= 7;
do { do {
b = LoadByte(S); b = LoadByte(S);
if (x >= limit)
error(S, "integer overflow");
x = (x << 7) | (b & 0x7f); x = (x << 7) | (b & 0x7f);
} while ((b & 0x80) == 0); } while ((b & 0x80) == 0);
return x; return x;
} }
static size_t LoadSize (LoadState *S) {
return LoadUnsigned(S, ~(size_t)0);
}
static int LoadInt (LoadState *S) { static int LoadInt (LoadState *S) {
return cast_int(LoadSize(S)); return cast_int(LoadUnsigned(S, INT_MAX));
} }
@ -255,28 +264,27 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) {
static void fchecksize (LoadState *S, size_t size, const char *tname) { static void fchecksize (LoadState *S, size_t size, const char *tname) {
if (LoadByte(S) != size) if (LoadByte(S) != size)
error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
} }
#define checksize(S,t) fchecksize(S,sizeof(t),#t) #define checksize(S,t) fchecksize(S,sizeof(t),#t)
static void checkHeader (LoadState *S) { static void checkHeader (LoadState *S) {
checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ /* 1st char already checked */
if (LoadByte(S) != LUAC_VERSION) checkliteral(S, LUA_SIGNATURE + 1, "not a binary chunk");
error(S, "version mismatch in"); if (LoadInt(S) != LUAC_VERSION)
error(S, "version mismatch");
if (LoadByte(S) != LUAC_FORMAT) if (LoadByte(S) != LUAC_FORMAT)
error(S, "format mismatch in"); error(S, "format mismatch");
checkliteral(S, LUAC_DATA, "corrupted"); checkliteral(S, LUAC_DATA, "corrupted chunk");
checksize(S, int);
checksize(S, size_t);
checksize(S, Instruction); checksize(S, Instruction);
checksize(S, lua_Integer); checksize(S, lua_Integer);
checksize(S, lua_Number); checksize(S, lua_Number);
if (LoadInteger(S) != LUAC_INT) if (LoadInteger(S) != LUAC_INT)
error(S, "endianness mismatch in"); error(S, "integer format mismatch");
if (LoadNumber(S) != LUAC_NUM) if (LoadNumber(S) != LUAC_NUM)
error(S, "float format mismatch in"); error(S, "float format mismatch");
} }

3
lundump.h

@ -18,8 +18,7 @@
#define LUAC_INT 0x5678 #define LUAC_INT 0x5678
#define LUAC_NUM cast_num(370.5) #define LUAC_NUM cast_num(370.5)
#define MYINT(s) (s[0]-'0') #define LUAC_VERSION LUA_VERSION_NUM
#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
#define LUAC_FORMAT 0 /* this is the official format */ #define LUAC_FORMAT 0 /* this is the official format */
/* load one chunk; from lundump.c */ /* load one chunk; from lundump.c */

20
testes/calls.lua

@ -397,17 +397,15 @@ assert((function (a) return a end)() == nil)
print("testing binary chunks") print("testing binary chunks")
do do
local header = string.pack("c4BBc6BBBBBj", local header = string.pack("c4BBBc6BBBj",
"\27Lua", -- signature "\27Lua", -- signature
5*16 + 4, -- version 5.4 (504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504)
0, -- format 0, -- format
"\x19\x93\r\n\x1a\n", -- data "\x19\x93\r\n\x1a\n", -- data
string.packsize("i"), -- sizeof(int) 4, -- size of instruction
string.packsize("T"), -- sizeof(size_t) string.packsize("j"), -- sizeof(lua integer)
4, -- size of instruction string.packsize("n"), -- sizeof(lua number)
string.packsize("j"), -- sizeof(lua integer) 0x5678 -- LUAC_INT
string.packsize("n"), -- sizeof(lua number)
0x5678 -- LUAC_INT
-- LUAC_NUM may not have a unique binary representation (padding...) -- LUAC_NUM may not have a unique binary representation (padding...)
) )
local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)

Loading…
Cancel
Save