aboutsummaryrefslogtreecommitdiff
path: root/ldump.c
diff options
context:
space:
mode:
Diffstat (limited to 'ldump.c')
-rw-r--r--ldump.c35
1 files changed, 18 insertions, 17 deletions
diff --git a/ldump.c b/ldump.c
index b31e7bc7..34cfb576 100644
--- a/ldump.c
+++ b/ldump.c
@@ -30,7 +30,7 @@ typedef struct {
30 int strip; 30 int strip;
31 int status; 31 int status;
32 Table *h; /* table to track saved strings */ 32 Table *h; /* table to track saved strings */
33 lua_Integer nstr; /* counter to number saved strings */ 33 lua_Unsigned nstr; /* counter to number saved strings */
34} DumpState; 34} DumpState;
35 35
36 36
@@ -83,26 +83,27 @@ static void dumpByte (DumpState *D, int y) {
83 83
84 84
85/* 85/*
86** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" 86** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
87** rounds up the division.) 87** (The "+6" rounds up the division.)
88*/ 88*/
89#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) 89#define DIBS ((sizeof(varint_t) * CHAR_BIT + 6) / 7)
90 90
91static void dumpSize (DumpState *D, size_t x) { 91/*
92** Dumps an unsigned integer using the MSB Varint encoding
93*/
94static void dumpVarint (DumpState *D, varint_t x) {
92 lu_byte buff[DIBS]; 95 lu_byte buff[DIBS];
93 int n = 0; 96 int n = 1;
94 do { 97 buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
95 buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ 98 while ((x >>= 7) != 0) /* fill other bytes in reverse order */
96 x >>= 7; 99 buff[DIBS - (++n)] = (x & 0x7f) | 0x80;
97 } while (x != 0);
98 buff[DIBS - 1] |= 0x80; /* mark last byte */
99 dumpVector(D, buff + DIBS - n, n); 100 dumpVector(D, buff + DIBS - n, n);
100} 101}
101 102
102 103
103static void dumpInt (DumpState *D, int x) { 104static void dumpInt (DumpState *D, int x) {
104 lua_assert(x >= 0); 105 lua_assert(x >= 0);
105 dumpSize(D, x); 106 dumpVarint(D, x);
106} 107}
107 108
108 109
@@ -125,22 +126,22 @@ static void dumpInteger (DumpState *D, lua_Integer x) {
125*/ 126*/
126static void dumpString (DumpState *D, TString *ts) { 127static void dumpString (DumpState *D, TString *ts) {
127 if (ts == NULL) 128 if (ts == NULL)
128 dumpSize(D, 0); 129 dumpVarint(D, 0);
129 else { 130 else {
130 TValue idx; 131 TValue idx;
131 if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ 132 if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */
132 dumpSize(D, 1); /* reuse a saved string */ 133 dumpVarint(D, 1); /* reuse a saved string */
133 dumpInt(D, ivalue(&idx)); /* index of saved string */ 134 dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */
134 } 135 }
135 else { /* must write and save the string */ 136 else { /* must write and save the string */
136 TValue key, value; /* to save the string in the hash */ 137 TValue key, value; /* to save the string in the hash */
137 size_t size; 138 size_t size;
138 const char *s = getlstr(ts, size); 139 const char *s = getlstr(ts, size);
139 dumpSize(D, size + 2); 140 dumpVarint(D, size + 2);
140 dumpVector(D, s, size + 1); /* include ending '\0' */ 141 dumpVector(D, s, size + 1); /* include ending '\0' */
141 D->nstr++; /* one more saved string */ 142 D->nstr++; /* one more saved string */
142 setsvalue(D->L, &key, ts); /* the string is the key */ 143 setsvalue(D->L, &key, ts); /* the string is the key */
143 setivalue(&value, D->nstr); /* its index is the value */ 144 setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */
144 luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ 145 luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
145 /* integer value does not need barrier */ 146 /* integer value does not need barrier */
146 } 147 }