Browse Source

dumping ints and size_ts compacted

Roberto Ierusalimschy 8 years ago
parent
commit
124bfd2081
2 changed files with 38 additions and 20 deletions
  1. 21 11
      ldump.c
  2. 17 9
      lundump.c

+ 21 - 11
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)
-      DumpByte(cast_int(size), D);
-    else {
-      DumpByte(0xFF, D);
-      DumpVar(size, D);
-    }
-    DumpVector(str, size - 1, D);  /* no need to save '\0' */
+    DumpSize(size + 1, D);
+    DumpVector(str, size, D);
   }
   }
 }
 }
 
 

+ 17 - 9
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;
-  LoadVar(S, x);
+  int b = zgetc(S->Z);
+  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;
-  LoadVar(S, x);
-  return x;
+  return cast_int(LoadSize(S));
 }
 }
 
 
 
 
@@ -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);
-  if (size == 0xFF)
-    LoadVar(S, size);
+  size_t size = LoadSize(S);
   if (size == 0)
   if (size == 0)
     return NULL;
     return NULL;
   else if (--size <= LUAI_MAXSHORTLEN) {  /* short string? */
   else if (--size <= LUAI_MAXSHORTLEN) {  /* short string? */