Browse Source

dumping ints and size_ts compacted

pull/11/head
Roberto Ierusalimschy 7 years ago
parent
commit
124bfd2081
  1. 32
      ldump.c
  2. 26
      lundump.c

32
ldump.c

@ -1,5 +1,5 @@
/* /*
** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp roberto $ ** $Id: ldump.c,v 2.38 2017/06/27 11:35:31 roberto Exp roberto $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -55,8 +55,23 @@ static void DumpByte (int y, DumpState *D) {
} }
/* DumpInt Buff Size */
#define DIBS ((sizeof(size_t) * 8 / 7) + 1)
static void DumpSize (size_t x, DumpState *D) {
lu_byte buff[DIBS];
int n = 0;
do {
buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
x >>= 7;
} while (x != 0);
buff[DIBS - 1] |= 0x80; /* mark last byte */
DumpVector(buff + DIBS - n, n, D);
}
static void DumpInt (int x, DumpState *D) { static void DumpInt (int x, DumpState *D) {
DumpVar(x, D); DumpSize(x, D);
} }
@ -72,17 +87,12 @@ static void DumpInteger (lua_Integer x, DumpState *D) {
static void DumpString (const TString *s, DumpState *D) { static void DumpString (const TString *s, DumpState *D) {
if (s == NULL) if (s == NULL)
DumpByte(0, D); DumpSize(0, D);
else { else {
size_t size = tsslen(s) + 1; /* include trailing '\0' */ size_t size = tsslen(s);
const char *str = getstr(s); const char *str = getstr(s);
if (size < 0xFF) DumpSize(size + 1, D);
DumpByte(cast_int(size), D); DumpVector(str, size, D);
else {
DumpByte(0xFF, D);
DumpVar(size, D);
}
DumpVector(str, size - 1, D); /* no need to save '\0' */
} }
} }

26
lundump.c

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp roberto $ ** $Id: lundump.c,v 2.45 2017/06/27 11:35:31 roberto Exp roberto $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -58,16 +58,26 @@ static void LoadBlock (LoadState *S, void *b, size_t size) {
static lu_byte LoadByte (LoadState *S) { static lu_byte LoadByte (LoadState *S) {
lu_byte x; int b = zgetc(S->Z);
LoadVar(S, x); if (b == EOZ)
error(S, "truncated");
return cast_byte(b);
}
static size_t LoadSize (LoadState *S) {
size_t x = 0;
int b;
do {
b = LoadByte(S);
x = (x << 7) | (b & 0x7f);
} while ((b & 0x80) == 0);
return x; return x;
} }
static int LoadInt (LoadState *S) { static int LoadInt (LoadState *S) {
int x; return cast_int(LoadSize(S));
LoadVar(S, x);
return x;
} }
@ -86,9 +96,7 @@ static lua_Integer LoadInteger (LoadState *S) {
static TString *LoadString (LoadState *S) { static TString *LoadString (LoadState *S) {
size_t size = LoadByte(S); size_t size = LoadSize(S);
if (size == 0xFF)
LoadVar(S, size);
if (size == 0) if (size == 0)
return NULL; return NULL;
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */

Loading…
Cancel
Save