summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lundump.c55
-rw-r--r--lundump.h22
2 files changed, 44 insertions, 33 deletions
diff --git a/lundump.c b/lundump.c
index 57a36935..a1519f38 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,11 +1,9 @@
1/* 1/*
2** $Id: lundump.c,v 1.47 2002/05/15 18:57:44 roberto Exp roberto $ 2** $Id: lundump.c,v 1.40 2002/03/01 01:48:42 lhf Exp lhf $
3** load pre-compiled Lua chunks 3** load pre-compiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7#include <string.h>
8
9#include "lua.h" 7#include "lua.h"
10 8
11#include "ldebug.h" 9#include "ldebug.h"
@@ -14,19 +12,24 @@
14#include "lopcodes.h" 12#include "lopcodes.h"
15#include "lstring.h" 13#include "lstring.h"
16#include "lundump.h" 14#include "lundump.h"
15#include "lzio.h"
17 16
18#define LoadByte ezgetc 17#define LoadByte (lu_byte) ezgetc
19#define LoadShort (short) LoadInt
20 18
21static const char* ZNAME (ZIO* Z) 19static const char* ZNAME (ZIO* Z)
22{ 20{
23 const char* s=zname(Z); 21 const char* s=zname(Z);
24 return (*s=='@') ? s+1 : s; 22 if (*s=='@' || *s=='=')
23 return s+1;
24 else if (*s==LUA_SIGNATURE[0])
25 return "binary string";
26 else
27 return s;
25} 28}
26 29
27static void unexpectedEOZ (lua_State* L, ZIO* Z) 30static void unexpectedEOZ (lua_State* L, ZIO* Z)
28{ 31{
29 luaG_runerror(L,"unexpected end of file in `%s'",ZNAME(Z)); 32 luaG_runerror(L,"unexpected end of file in %s",ZNAME(Z));
30} 33}
31 34
32static int ezgetc (lua_State* L, ZIO* Z) 35static int ezgetc (lua_State* L, ZIO* Z)
@@ -75,6 +78,7 @@ static int LoadInt (lua_State* L, ZIO* Z, int swap)
75{ 78{
76 int x; 79 int x;
77 LoadBlock(L,&x,sizeof(x),Z,swap); 80 LoadBlock(L,&x,sizeof(x),Z,swap);
81 if (x<0) luaG_runerror(L,"bad integer in %s",ZNAME(Z));
78 return x; 82 return x;
79} 83}
80 84
@@ -155,8 +159,10 @@ static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
155 case LUA_TSTRING: 159 case LUA_TSTRING:
156 tsvalue(o)=LoadString(L,Z,swap); 160 tsvalue(o)=LoadString(L,Z,swap);
157 break; 161 break;
162 case LUA_TNIL:
163 break;
158 default: 164 default:
159 luaG_runerror(L,"bad constant type (%d) in `%s'",ttype(o),ZNAME(Z)); 165 luaG_runerror(L,"bad constant type (%d) in %s",ttype(o),ZNAME(Z));
160 break; 166 break;
161 } 167 }
162 } 168 }
@@ -171,16 +177,16 @@ static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
171 Proto* f=luaF_newproto(L); 177 Proto* f=luaF_newproto(L);
172 f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p; 178 f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
173 f->lineDefined=LoadInt(L,Z,swap); 179 f->lineDefined=LoadInt(L,Z,swap);
174 f->nupvalues=LoadShort(L,Z,swap); 180 f->nupvalues=LoadByte(L,Z);
175 f->numparams=LoadShort(L,Z,swap); 181 f->numparams=LoadByte(L,Z);
176 f->is_vararg=LoadShort(L,Z,swap); 182 f->is_vararg=LoadByte(L,Z);
177 f->maxstacksize=LoadShort(L,Z,swap); 183 f->maxstacksize=LoadByte(L,Z);
178 LoadLocals(L,f,Z,swap); 184 LoadLocals(L,f,Z,swap);
179 LoadLines(L,f,Z,swap); 185 LoadLines(L,f,Z,swap);
180 LoadConstants(L,f,Z,swap); 186 LoadConstants(L,f,Z,swap);
181 LoadCode(L,f,Z,swap); 187 LoadCode(L,f,Z,swap);
182#ifndef TRUST_BINARIES 188#ifndef TRUST_BINARIES
183 if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in `%s'",ZNAME(Z)); 189 if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in %s",ZNAME(Z));
184#endif 190#endif
185 return f; 191 return f;
186} 192}
@@ -190,15 +196,15 @@ static void LoadSignature (lua_State* L, ZIO* Z)
190 const char* s=LUA_SIGNATURE; 196 const char* s=LUA_SIGNATURE;
191 while (*s!=0 && ezgetc(L,Z)==*s) 197 while (*s!=0 && ezgetc(L,Z)==*s)
192 ++s; 198 ++s;
193 if (*s!=0) luaG_runerror(L,"bad signature in `%s'",ZNAME(Z)); 199 if (*s!=0) luaG_runerror(L,"bad signature in %s",ZNAME(Z));
194} 200}
195 201
196static void TestSize (lua_State* L, int s, const char* what, ZIO* Z) 202static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
197{ 203{
198 int r=LoadByte(L,Z); 204 int r=LoadByte(L,Z);
199 if (r!=s) 205 if (r!=s)
200 luaG_runerror(L,"virtual machine mismatch in `%s':\n" 206 luaG_runerror(L,"virtual machine mismatch in %s: "
201 " size of %.20s is %d but read %d",ZNAME(Z),what,s,r); 207 "size of %s is %d but read %d",ZNAME(Z),what,s,r);
202} 208}
203 209
204#define TESTSIZE(s,w) TestSize(L,s,w,Z) 210#define TESTSIZE(s,w) TestSize(L,s,w,Z)
@@ -211,17 +217,17 @@ static int LoadHeader (lua_State* L, ZIO* Z)
211 LoadSignature(L,Z); 217 LoadSignature(L,Z);
212 version=LoadByte(L,Z); 218 version=LoadByte(L,Z);
213 if (version>VERSION) 219 if (version>VERSION)
214 luaG_runerror(L,"`%s' too new:\n" 220 luaG_runerror(L,"%s too new: "
215 " read version %d.%d; expected at most %d.%d", 221 "read version %d.%d; expected at most %d.%d",
216 ZNAME(Z),V(version),V(VERSION)); 222 ZNAME(Z),V(version),V(VERSION));
217 if (version<VERSION0) /* check last major change */ 223 if (version<VERSION0) /* check last major change */
218 luaG_runerror(L,"`%s' too old:\n" 224 luaG_runerror(L,"%s too old: "
219 " read version %d.%d; expected at least %d.%d", 225 "read version %d.%d; expected at least %d.%d",
220 ZNAME(Z),V(version),V(VERSION)); 226 ZNAME(Z),V(version),V(VERSION0));
221 swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */ 227 swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
222 TESTSIZE(sizeof(int),"int"); 228 TESTSIZE(sizeof(int),"int");
223 TESTSIZE(sizeof(size_t), "size_t"); 229 TESTSIZE(sizeof(size_t), "size_t");
224 TESTSIZE(sizeof(Instruction), "size_t"); 230 TESTSIZE(sizeof(Instruction), "Instruction");
225 TESTSIZE(SIZE_OP, "OP"); 231 TESTSIZE(SIZE_OP, "OP");
226 TESTSIZE(SIZE_A, "A"); 232 TESTSIZE(SIZE_A, "A");
227 TESTSIZE(SIZE_B, "B"); 233 TESTSIZE(SIZE_B, "B");
@@ -229,8 +235,7 @@ static int LoadHeader (lua_State* L, ZIO* Z)
229 TESTSIZE(sizeof(lua_Number), "number"); 235 TESTSIZE(sizeof(lua_Number), "number");
230 x=LoadNumber(L,Z,swap); 236 x=LoadNumber(L,Z,swap);
231 if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ 237 if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
232 luaG_runerror(L,"unknown number format in `%s':\n" 238 luaG_runerror(L,"unknown number format in %s: read %f; expected %f",
233 " read " LUA_NUMBER_FMT "; expected " LUA_NUMBER_FMT,
234 ZNAME(Z),x,tx); 239 ZNAME(Z),x,tx);
235 return swap; 240 return swap;
236} 241}
@@ -247,7 +252,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z)
247{ 252{
248 Proto* f=LoadChunk(L,Z); 253 Proto* f=LoadChunk(L,Z);
249 if (zgetc(Z)!=EOZ) 254 if (zgetc(Z)!=EOZ)
250 luaG_runerror(L,"`%s' apparently contains more than one chunk",ZNAME(Z)); 255 luaG_runerror(L,"%s apparently contains more than one chunk",ZNAME(Z));
251 return f; 256 return f;
252} 257}
253 258
diff --git a/lundump.h b/lundump.h
index 473da103..7f9ecc27 100644
--- a/lundump.h
+++ b/lundump.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.h,v 1.24 2002/06/03 17:46:34 roberto Exp roberto $ 2** $Id: lundump.h,v 1.24 2001/07/19 14:34:06 lhf Exp lhf $
3** load pre-compiled Lua chunks 3** load pre-compiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,21 +10,27 @@
10#include "lobject.h" 10#include "lobject.h"
11#include "lzio.h" 11#include "lzio.h"
12 12
13/* load one chunk */ 13typedef size_t (*Writer)(const void* p, size_t size, size_t n, void* u);
14
15/* load one chunk; from lundump.c */
14Proto* luaU_undump (lua_State* L, ZIO* Z); 16Proto* luaU_undump (lua_State* L, ZIO* Z);
15 17
16/* find byte order */ 18/* find byte order; from lundump.c */
17int luaU_endianness (void); 19int luaU_endianness (void);
18 20
21/* dump one chunk; from dump.c */
22void luaU_dump (const Proto* Main, Writer w, void* data);
23
24/* print one chunk; from print.c */
25void luaU_print (const Proto* Main);
26
19/* definitions for headers of binary files */ 27/* definitions for headers of binary files */
20#define VERSION 0x41 /* last format change was in 4.1 */ 28#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
21#define VERSION0 0x41 /* last major change was in 4.1 */ 29#define VERSION 0x50 /* last format change was in 5.0 */
30#define VERSION0 0x50 /* last major change was in 5.0 */
22 31
23/* a multiple of PI for testing native format */ 32/* a multiple of PI for testing native format */
24/* multiplying by 1E8 gives non-trivial integer values */ 33/* multiplying by 1E8 gives non-trivial integer values */
25#define TEST_NUMBER 3.14159265358979323846E8 34#define TEST_NUMBER 3.14159265358979323846E8
26 35
27/* binary files start with <esc>Lua */
28#define LUA_SIGNATURE "\033Lua"
29
30#endif 36#endif