Port to 1.99 extras build system.
[grub-extras.git] / lundump.c
blob637d842376b87fb23b130d682e067a7608c99d67
1 /*
2 ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
5 */
7 #if 0
8 #include <string.h>
9 #endif
11 #define lundump_c
12 #define LUA_CORE
14 #include "lua.h"
16 #include "ldebug.h"
17 #include "ldo.h"
18 #include "lfunc.h"
19 #include "lmem.h"
20 #include "lobject.h"
21 #include "lstring.h"
22 #include "lundump.h"
23 #include "lzio.h"
25 typedef struct {
26 lua_State* L;
27 ZIO* Z;
28 Mbuffer* b;
29 const char* name;
30 } LoadState;
32 #ifdef LUAC_TRUST_BINARIES
33 #define IF(c,s)
34 #define error(S,s)
35 #else
36 #define IF(c,s) if (c) error(S,s)
38 static void error(LoadState* S, const char* why)
40 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
41 luaD_throw(S->L,LUA_ERRSYNTAX);
43 #endif
45 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
46 #define LoadByte(S) (lu_byte)LoadChar(S)
47 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
48 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
50 static void LoadBlock(LoadState* S, void* b, size_t size)
52 size_t r=luaZ_read(S->Z,b,size);
53 IF (r!=0, "unexpected end");
56 static int LoadChar(LoadState* S)
58 char x;
59 LoadVar(S,x);
60 return x;
63 static int LoadInt(LoadState* S)
65 int x;
66 LoadVar(S,x);
67 IF (x<0, "bad integer");
68 return x;
71 static lua_Number LoadNumber(LoadState* S)
73 lua_Number x;
74 LoadVar(S,x);
75 return x;
78 static TString* LoadString(LoadState* S)
80 size_t size;
81 LoadVar(S,size);
82 if (size==0)
83 return NULL;
84 else
86 char* s=luaZ_openspace(S->L,S->b,size);
87 LoadBlock(S,s,size);
88 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
92 static void LoadCode(LoadState* S, Proto* f)
94 int n=LoadInt(S);
95 f->code=luaM_newvector(S->L,n,Instruction);
96 f->sizecode=n;
97 LoadVector(S,f->code,n,sizeof(Instruction));
100 static Proto* LoadFunction(LoadState* S, TString* p);
102 static void LoadConstants(LoadState* S, Proto* f)
104 int i,n;
105 n=LoadInt(S);
106 f->k=luaM_newvector(S->L,n,TValue);
107 f->sizek=n;
108 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
109 for (i=0; i<n; i++)
111 TValue* o=&f->k[i];
112 int t=LoadChar(S);
113 switch (t)
115 case LUA_TNIL:
116 setnilvalue(o);
117 break;
118 case LUA_TBOOLEAN:
119 setbvalue(o,LoadChar(S)!=0);
120 break;
121 case LUA_TNUMBER:
122 setnvalue(o,LoadNumber(S));
123 break;
124 case LUA_TSTRING:
125 setsvalue2n(S->L,o,LoadString(S));
126 break;
127 default:
128 error(S,"bad constant");
129 break;
132 n=LoadInt(S);
133 f->p=luaM_newvector(S->L,n,Proto*);
134 f->sizep=n;
135 for (i=0; i<n; i++) f->p[i]=NULL;
136 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
139 static void LoadDebug(LoadState* S, Proto* f)
141 int i,n;
142 n=LoadInt(S);
143 f->lineinfo=luaM_newvector(S->L,n,int);
144 f->sizelineinfo=n;
145 LoadVector(S,f->lineinfo,n,sizeof(int));
146 n=LoadInt(S);
147 f->locvars=luaM_newvector(S->L,n,LocVar);
148 f->sizelocvars=n;
149 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
150 for (i=0; i<n; i++)
152 f->locvars[i].varname=LoadString(S);
153 f->locvars[i].startpc=LoadInt(S);
154 f->locvars[i].endpc=LoadInt(S);
156 n=LoadInt(S);
157 f->upvalues=luaM_newvector(S->L,n,TString*);
158 f->sizeupvalues=n;
159 for (i=0; i<n; i++) f->upvalues[i]=NULL;
160 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
163 static Proto* LoadFunction(LoadState* S, TString* p)
165 Proto* f;
166 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
167 f=luaF_newproto(S->L);
168 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
169 f->source=LoadString(S); if (f->source==NULL) f->source=p;
170 f->linedefined=LoadInt(S);
171 f->lastlinedefined=LoadInt(S);
172 f->nups=LoadByte(S);
173 f->numparams=LoadByte(S);
174 f->is_vararg=LoadByte(S);
175 f->maxstacksize=LoadByte(S);
176 LoadCode(S,f);
177 LoadConstants(S,f);
178 LoadDebug(S,f);
179 IF (!luaG_checkcode(f), "bad code");
180 S->L->top--;
181 S->L->nCcalls--;
182 return f;
185 static void LoadHeader(LoadState* S)
187 char h[LUAC_HEADERSIZE];
188 char s[LUAC_HEADERSIZE];
189 luaU_header(h);
190 LoadBlock(S,s,LUAC_HEADERSIZE);
191 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
195 ** load precompiled chunk
197 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
199 LoadState S;
200 if (*name=='@' || *name=='=')
201 S.name=name+1;
202 else if (*name==LUA_SIGNATURE[0])
203 S.name="binary string";
204 else
205 S.name=name;
206 S.L=L;
207 S.Z=Z;
208 S.b=buff;
209 LoadHeader(&S);
210 return LoadFunction(&S,luaS_newliteral(L,"=?"));
214 * make header
216 void luaU_header (char* h)
218 int x=1;
219 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
220 h+=sizeof(LUA_SIGNATURE)-1;
221 *h++=(char)LUAC_VERSION;
222 *h++=(char)LUAC_FORMAT;
223 *h++=(char)*(char*)&x; /* endianness */
224 *h++=(char)sizeof(int);
225 *h++=(char)sizeof(size_t);
226 *h++=(char)sizeof(Instruction);
227 *h++=(char)sizeof(lua_Number);
228 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */