1/*	$NetBSD$	*/
2
3/*
4** Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp
5** load precompiled Lua chunks
6** See Copyright Notice in lua.h
7*/
8
9#include <string.h>
10
11#define lundump_c
12#define LUA_CORE
13
14#include "lua.h"
15
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"
24
25typedef struct {
26 lua_State* L;
27 ZIO* Z;
28 Mbuffer* b;
29 const char* name;
30} LoadState;
31
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)
37
38static void error(LoadState* S, const char* why)
39{
40 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
41 luaD_throw(S->L,LUA_ERRSYNTAX);
42}
43#endif
44
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)
49
50static void LoadBlock(LoadState* S, void* b, size_t size)
51{
52 size_t r=luaZ_read(S->Z,b,size);
53 IF (r!=0, "unexpected end");
54}
55
56static int LoadChar(LoadState* S)
57{
58 char x;
59 LoadVar(S,x);
60 return x;
61}
62
63static int LoadInt(LoadState* S)
64{
65 int x;
66 LoadVar(S,x);
67 IF (x<0, "bad integer");
68 return x;
69}
70
71static lua_Number LoadNumber(LoadState* S)
72{
73 lua_Number x;
74 LoadVar(S,x);
75 return x;
76}
77
78static TString* LoadString(LoadState* S)
79{
80 size_t size;
81 LoadVar(S,size);
82 if (size==0)
83  return NULL;
84 else
85 {
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' */
89 }
90}
91
92static void LoadCode(LoadState* S, Proto* f)
93{
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));
98}
99
100static Proto* LoadFunction(LoadState* S, TString* p);
101
102static void LoadConstants(LoadState* S, Proto* f)
103{
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++)
110 {
111  TValue* o=&f->k[i];
112  int t=LoadChar(S);
113  switch (t)
114  {
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;
130  }
131 }
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);
137}
138
139static void LoadDebug(LoadState* S, Proto* f)
140{
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++)
151 {
152  f->locvars[i].varname=LoadString(S);
153  f->locvars[i].startpc=LoadInt(S);
154  f->locvars[i].endpc=LoadInt(S);
155 }
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);
161}
162
163static Proto* LoadFunction(LoadState* S, TString* p)
164{
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;
183}
184
185static void LoadHeader(LoadState* S)
186{
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");
192}
193
194/*
195** load precompiled chunk
196*/
197Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
198{
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,"=?"));
211}
212
213/*
214* make header
215*/
216void luaU_header (char* h)
217{
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? */
229}
230