diff options
Diffstat (limited to 'lundump.c')
-rw-r--r-- | lundump.c | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -36,6 +36,7 @@ typedef struct { | |||
36 | ZIO *Z; | 36 | ZIO *Z; |
37 | const char *name; | 37 | const char *name; |
38 | Table *h; /* list for string reuse */ | 38 | Table *h; /* list for string reuse */ |
39 | lu_mem offset; /* current position relative to beginning of dump */ | ||
39 | lua_Integer nstr; /* number of strings in the list */ | 40 | lua_Integer nstr; /* number of strings in the list */ |
40 | } LoadState; | 41 | } LoadState; |
41 | 42 | ||
@@ -55,6 +56,17 @@ static l_noret error (LoadState *S, const char *why) { | |||
55 | static void loadBlock (LoadState *S, void *b, size_t size) { | 56 | static void loadBlock (LoadState *S, void *b, size_t size) { |
56 | if (luaZ_read(S->Z, b, size) != 0) | 57 | if (luaZ_read(S->Z, b, size) != 0) |
57 | error(S, "truncated chunk"); | 58 | error(S, "truncated chunk"); |
59 | S->offset += size; | ||
60 | } | ||
61 | |||
62 | |||
63 | static void loadAlign (LoadState *S, int align) { | ||
64 | int padding = align - (S->offset % align); | ||
65 | if (padding < align) { /* apd == align means no padding */ | ||
66 | lua_Integer paddingContent; | ||
67 | loadBlock(S, &paddingContent, padding); | ||
68 | lua_assert(S->offset % align == 0); | ||
69 | } | ||
58 | } | 70 | } |
59 | 71 | ||
60 | 72 | ||
@@ -65,6 +77,7 @@ static lu_byte loadByte (LoadState *S) { | |||
65 | int b = zgetc(S->Z); | 77 | int b = zgetc(S->Z); |
66 | if (b == EOZ) | 78 | if (b == EOZ) |
67 | error(S, "truncated chunk"); | 79 | error(S, "truncated chunk"); |
80 | S->offset++; | ||
68 | return cast_byte(b); | 81 | return cast_byte(b); |
69 | } | 82 | } |
70 | 83 | ||
@@ -158,6 +171,7 @@ static void loadCode (LoadState *S, Proto *f) { | |||
158 | int n = loadInt(S); | 171 | int n = loadInt(S); |
159 | f->code = luaM_newvectorchecked(S->L, n, Instruction); | 172 | f->code = luaM_newvectorchecked(S->L, n, Instruction); |
160 | f->sizecode = n; | 173 | f->sizecode = n; |
174 | loadAlign(S, sizeof(f->code[0])); | ||
161 | loadVector(S, f->code, n); | 175 | loadVector(S, f->code, n); |
162 | } | 176 | } |
163 | 177 | ||
@@ -321,7 +335,7 @@ static void checkHeader (LoadState *S) { | |||
321 | /* | 335 | /* |
322 | ** Load precompiled chunk. | 336 | ** Load precompiled chunk. |
323 | */ | 337 | */ |
324 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { | 338 | LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name) { |
325 | LoadState S; | 339 | LoadState S; |
326 | LClosure *cl; | 340 | LClosure *cl; |
327 | if (*name == '@' || *name == '=') | 341 | if (*name == '@' || *name == '=') |
@@ -332,6 +346,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { | |||
332 | S.name = name; | 346 | S.name = name; |
333 | S.L = L; | 347 | S.L = L; |
334 | S.Z = Z; | 348 | S.Z = Z; |
349 | S.offset = 1; /* fist byte was already read */ | ||
335 | checkHeader(&S); | 350 | checkHeader(&S); |
336 | cl = luaF_newLclosure(L, loadByte(&S)); | 351 | cl = luaF_newLclosure(L, loadByte(&S)); |
337 | setclLvalue2s(L, L->top.p, cl); | 352 | setclLvalue2s(L, L->top.p, cl); |