1
1
/*
2
- ** $Id: lundump.c,v 1.66 2009/05/01 01:25:34 lhf Exp lhf $
2
+ ** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp lhf $
3
3
** load precompiled Lua chunks
4
4
** See Copyright Notice in lua.h
5
5
*/
@@ -175,13 +175,22 @@ static Proto* LoadFunction(LoadState* S)
175
175
return f ;
176
176
}
177
177
178
+ /* the code below must be consistent with the code in luaU_header */
179
+ #define N0 LUAC_HEADERSIZE
180
+ #define N1 (sizeof(LUA_SIGNATURE)-1)
181
+ #define N2 N1+2
182
+ #define N3 N2+6
183
+
178
184
static void LoadHeader (LoadState * S )
179
185
{
180
186
char h [LUAC_HEADERSIZE ];
181
187
char s [LUAC_HEADERSIZE ];
182
188
luaU_header (h );
183
189
LoadBlock (S ,s ,LUAC_HEADERSIZE );
184
- if (memcmp (h ,s ,LUAC_HEADERSIZE )!= 0 ) error (S ,"incompatible" );
190
+ if (memcmp (h ,s ,N0 )== 0 ) return ;
191
+ if (memcmp (h ,s ,N1 )!= 0 ) error (S ,"not a" );
192
+ if (memcmp (h ,s ,N2 )!= 0 ) error (S ,"version mismatch in" );
193
+ if (memcmp (h ,s ,N3 )!= 0 ) error (S ,"incompatible" ); else error (S ,"corrupted" );
185
194
}
186
195
187
196
/*
@@ -203,22 +212,26 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
203
212
return LoadFunction (& S );
204
213
}
205
214
215
+ /* data to catch conversion errors */
216
+ #define TAIL "\x19\x93\r\n\x1a\n"
217
+
206
218
/*
207
- * make header
208
- * if you make any changes in the header or in LUA_SIGNATURE,
209
- * be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
219
+ * make header for precompiled chunks
220
+ * if you make any changes in the code below or in LUA_SIGNATURE in lua.h ,
221
+ * be sure to update LoadHeader above and LUAC_HEADERSIZE in lundump.h
210
222
*/
211
223
void luaU_header (char * h )
212
224
{
213
225
int x = 1 ;
214
226
memcpy (h ,LUA_SIGNATURE ,sizeof (LUA_SIGNATURE )- 1 );
215
227
h += sizeof (LUA_SIGNATURE )- 1 ;
216
- * h ++ = (char )LUAC_VERSION ;
217
- * h ++ = (char )LUAC_FORMAT ;
228
+ * h ++ = (char )0x52 ; /* Lua 5.2 */
229
+ * h ++ = (char )0 ; /* the official format */
218
230
* h ++ = (char )* (char * )& x ; /* endianness */
219
231
* h ++ = (char )sizeof (int );
220
232
* h ++ = (char )sizeof (size_t );
221
233
* h ++ = (char )sizeof (Instruction );
222
234
* h ++ = (char )sizeof (lua_Number );
223
235
* h ++ = (char )(((lua_Number )0.5 )== 0 ); /* is lua_Number integral? */
236
+ memcpy (h ,TAIL ,sizeof (TAIL )- 1 );
224
237
}
0 commit comments