diff options
| -rw-r--r-- | ldump.c | 6 | ||||
| -rw-r--r-- | lundump.c | 62 | ||||
| -rw-r--r-- | lundump.h | 20 |
3 files changed, 50 insertions, 38 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $ | 2 | ** $Id: ldump.c,v 1.18 2011/05/06 13:35:17 lhf Exp $ |
| 3 | ** save precompiled Lua chunks | 3 | ** save precompiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -69,7 +69,7 @@ static void DumpString(const TString* s, DumpState* D) | |||
| 69 | { | 69 | { |
| 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ | 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ |
| 71 | DumpVar(size,D); | 71 | DumpVar(size,D); |
| 72 | DumpBlock(getstr(s),size,D); | 72 | DumpBlock(getstr(s),size*sizeof(char),D); |
| 73 | } | 73 | } |
| 74 | } | 74 | } |
| 75 | 75 | ||
| @@ -150,7 +150,7 @@ static void DumpFunction(const Proto* f, DumpState* D) | |||
| 150 | 150 | ||
| 151 | static void DumpHeader(DumpState* D) | 151 | static void DumpHeader(DumpState* D) |
| 152 | { | 152 | { |
| 153 | char h[LUAC_HEADERSIZE]; | 153 | lu_byte h[LUAC_HEADERSIZE]; |
| 154 | luaU_header(h); | 154 | luaU_header(h); |
| 155 | DumpBlock(h,LUAC_HEADERSIZE,D); | 155 | DumpBlock(h,LUAC_HEADERSIZE,D); |
| 156 | } | 156 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.c,v 2.15 2011/02/07 19:15:24 roberto Exp roberto $ | 2 | ** $Id: lundump.c,v 1.69 2011/05/06 13:35:17 lhf Exp $ |
| 3 | ** load precompiled Lua chunks | 3 | ** load precompiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -38,6 +38,10 @@ static void error(LoadState* S, const char* why) | |||
| 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) | 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) |
| 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) | 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) |
| 40 | 40 | ||
| 41 | #if !defined(luai_verifycode) | ||
| 42 | #define luai_verifycode(L,b,f) (f) | ||
| 43 | #endif | ||
| 44 | |||
| 41 | static void LoadBlock(LoadState* S, void* b, size_t size) | 45 | static void LoadBlock(LoadState* S, void* b, size_t size) |
| 42 | { | 46 | { |
| 43 | if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted"); | 47 | if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted"); |
| @@ -54,6 +58,7 @@ static int LoadInt(LoadState* S) | |||
| 54 | { | 58 | { |
| 55 | int x; | 59 | int x; |
| 56 | LoadVar(S,x); | 60 | LoadVar(S,x); |
| 61 | if (x<0) error(S,"corrupted"); | ||
| 57 | return x; | 62 | return x; |
| 58 | } | 63 | } |
| 59 | 64 | ||
| @@ -73,7 +78,7 @@ static TString* LoadString(LoadState* S) | |||
| 73 | else | 78 | else |
| 74 | { | 79 | { |
| 75 | char* s=luaZ_openspace(S->L,S->b,size); | 80 | char* s=luaZ_openspace(S->L,S->b,size); |
| 76 | LoadBlock(S,s,size); | 81 | LoadBlock(S,s,size*sizeof(char)); |
| 77 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ | 82 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ |
| 78 | } | 83 | } |
| 79 | } | 84 | } |
| @@ -175,13 +180,23 @@ static Proto* LoadFunction(LoadState* S) | |||
| 175 | return f; | 180 | return f; |
| 176 | } | 181 | } |
| 177 | 182 | ||
| 183 | /* the code below must be consistent with the code in luaU_header */ | ||
| 184 | #define N0 LUAC_HEADERSIZE | ||
| 185 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) | ||
| 186 | #define N2 N1+2 | ||
| 187 | #define N3 N2+6 | ||
| 188 | |||
| 178 | static void LoadHeader(LoadState* S) | 189 | static void LoadHeader(LoadState* S) |
| 179 | { | 190 | { |
| 180 | char h[LUAC_HEADERSIZE]; | 191 | lu_byte h[LUAC_HEADERSIZE]; |
| 181 | char s[LUAC_HEADERSIZE]; | 192 | lu_byte s[LUAC_HEADERSIZE]; |
| 182 | luaU_header(h); | 193 | luaU_header(h); |
| 183 | LoadBlock(S,s,LUAC_HEADERSIZE-1); /* 1st char already read */ | 194 | memcpy(s,h,sizeof(char)); /* first char already read */ |
| 184 | if (memcmp(h+1,s,LUAC_HEADERSIZE-1)!=0) error(S,"incompatible"); | 195 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); |
| 196 | if (memcmp(h,s,N0)==0) return; | ||
| 197 | if (memcmp(h,s,N1)!=0) error(S,"not a"); | ||
| 198 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); | ||
| 199 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); | ||
| 185 | } | 200 | } |
| 186 | 201 | ||
| 187 | /* | 202 | /* |
| @@ -200,25 +215,30 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) | |||
| 200 | S.Z=Z; | 215 | S.Z=Z; |
| 201 | S.b=buff; | 216 | S.b=buff; |
| 202 | LoadHeader(&S); | 217 | LoadHeader(&S); |
| 203 | return LoadFunction(&S); | 218 | return luai_verifycode(L,buff,LoadFunction(&S)); |
| 204 | } | 219 | } |
| 205 | 220 | ||
| 221 | #define MYINT(s) (s[0]-'0') | ||
| 222 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) | ||
| 223 | #define FORMAT 0 /* this is the official format */ | ||
| 224 | |||
| 206 | /* | 225 | /* |
| 207 | * make header | 226 | * make header for precompiled chunks |
| 208 | * if you make any changes in the header or in LUA_SIGNATURE, | 227 | * if you change the code below be sure to update LoadHeader and FORMAT above |
| 209 | * be sure to update LUAC_HEADERSIZE accordingly in lundump.h. | 228 | * and LUAC_HEADERSIZE in lundump.h |
| 210 | */ | 229 | */ |
| 211 | void luaU_header (char* h) | 230 | void luaU_header (lu_byte* h) |
| 212 | { | 231 | { |
| 213 | int x=1; | 232 | int x=1; |
| 214 | memcpy(h,LUA_SIGNATURE,(sizeof(LUA_SIGNATURE)-1)*sizeof(char)); | 233 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); |
| 215 | h+=(sizeof(LUA_SIGNATURE)-1)*sizeof(char); | 234 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); |
| 216 | *h++=(char)LUAC_VERSION; | 235 | *h++=cast_byte(VERSION); |
| 217 | *h++=(char)LUAC_FORMAT; | 236 | *h++=cast_byte(FORMAT); |
| 218 | *h++=(char)*(char*)&x; /* endianness */ | 237 | *h++=cast_byte(*(char*)&x); /* endianness */ |
| 219 | *h++=(char)sizeof(int); | 238 | *h++=cast_byte(sizeof(int)); |
| 220 | *h++=(char)sizeof(size_t); | 239 | *h++=cast_byte(sizeof(size_t)); |
| 221 | *h++=(char)sizeof(Instruction); | 240 | *h++=cast_byte(sizeof(Instruction)); |
| 222 | *h++=(char)sizeof(lua_Number); | 241 | *h++=cast_byte(sizeof(lua_Number)); |
| 223 | *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ | 242 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ |
| 243 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); | ||
| 224 | } | 244 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.h,v 1.39 2005/11/01 17:04:55 lhf Exp lhf $ | 2 | ** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $ |
| 3 | ** load precompiled Lua chunks | 3 | ** load precompiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -14,23 +14,15 @@ | |||
| 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); | 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); |
| 15 | 15 | ||
| 16 | /* make header; from lundump.c */ | 16 | /* make header; from lundump.c */ |
| 17 | LUAI_FUNC void luaU_header (char* h); | 17 | LUAI_FUNC void luaU_header (lu_byte* h); |
| 18 | 18 | ||
| 19 | /* dump one chunk; from ldump.c */ | 19 | /* dump one chunk; from ldump.c */ |
| 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); | 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); |
| 21 | 21 | ||
| 22 | #ifdef luac_c | 22 | /* data to catch conversion errors */ |
| 23 | /* print one chunk; from print.c */ | 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" |
| 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); | ||
| 25 | #endif | ||
| 26 | |||
| 27 | /* for header of binary files -- this is Lua 5.1 */ | ||
| 28 | #define LUAC_VERSION 0x51 | ||
| 29 | |||
| 30 | /* for header of binary files -- this is the official format */ | ||
| 31 | #define LUAC_FORMAT 0 | ||
| 32 | 24 | ||
| 33 | /* size of header of binary files */ | 25 | /* size in bytes of header of binary files */ |
| 34 | #define LUAC_HEADERSIZE 12 | 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) |
| 35 | 27 | ||
| 36 | #endif | 28 | #endif |
