diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-17 09:42:43 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-05-17 09:42:43 -0300 |
| commit | 6c8a32217a04f8441fdd9233035e26930f498bf9 (patch) | |
| tree | c0f7a34575b97e5cbbd26c35981856729a78c9b4 /lundump.c | |
| parent | cb09f4fef482ba146dd1c18852edb0fe5efe3b07 (diff) | |
| download | lua-6c8a32217a04f8441fdd9233035e26930f498bf9.tar.gz lua-6c8a32217a04f8441fdd9233035e26930f498bf9.tar.bz2 lua-6c8a32217a04f8441fdd9233035e26930f498bf9.zip | |
changes by lhf (better control of chars x bytes)
Diffstat (limited to 'lundump.c')
| -rw-r--r-- | lundump.c | 62 |
1 files changed, 41 insertions, 21 deletions
| @@ -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 | } |
