]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lua/lib/contrib/src/lundump.c
update
[l4.git] / l4 / pkg / lua / lib / contrib / src / lundump.c
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 */
6
7 #include <string.h>
8
9 #define lundump_c
10 #define LUA_CORE
11
12 #include "lua.h"
13
14 #include "ldebug.h"
15 #include "ldo.h"
16 #include "lfunc.h"
17 #include "lmem.h"
18 #include "lobject.h"
19 #include "lstring.h"
20 #include "lundump.h"
21 #include "lzio.h"
22
23 typedef struct {
24  lua_State* L;
25  ZIO* Z;
26  Mbuffer* b;
27  const char* name;
28 } LoadState;
29
30 #ifdef LUAC_TRUST_BINARIES
31 #define IF(c,s)
32 #define error(S,s)
33 #else
34 #define IF(c,s)         if (c) error(S,s)
35
36 static void error(LoadState* S, const char* why)
37 {
38  luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
39  luaD_throw(S->L,LUA_ERRSYNTAX);
40 }
41 #endif
42
43 #define LoadMem(S,b,n,size)     LoadBlock(S,b,(n)*(size))
44 #define LoadByte(S)             (lu_byte)LoadChar(S)
45 #define LoadVar(S,x)            LoadMem(S,&x,1,sizeof(x))
46 #define LoadVector(S,b,n,size)  LoadMem(S,b,n,size)
47
48 static void LoadBlock(LoadState* S, void* b, size_t size)
49 {
50  size_t r=luaZ_read(S->Z,b,size);
51  IF (r!=0, "unexpected end");
52 }
53
54 static int LoadChar(LoadState* S)
55 {
56  char x;
57  LoadVar(S,x);
58  return x;
59 }
60
61 static int LoadInt(LoadState* S)
62 {
63  int x;
64  LoadVar(S,x);
65  IF (x<0, "bad integer");
66  return x;
67 }
68
69 static lua_Number LoadNumber(LoadState* S)
70 {
71  lua_Number x;
72  LoadVar(S,x);
73  return x;
74 }
75
76 #ifdef LUA_TINT
77 static lua_Integer LoadInteger(LoadState* S)
78 {
79  lua_Integer x;
80  LoadVar(S,x);
81  return x;
82 }
83 #endif
84
85 static TString* LoadString(LoadState* S)
86 {
87  size_t size;
88  LoadVar(S,size);
89  if (size==0)
90   return NULL;
91  else
92  {
93   char* s=luaZ_openspace(S->L,S->b,size);
94   LoadBlock(S,s,size);
95   return luaS_newlstr(S->L,s,size-1);           /* remove trailing '\0' */
96  }
97 }
98
99 static void LoadCode(LoadState* S, Proto* f)
100 {
101  int n=LoadInt(S);
102  f->code=luaM_newvector(S->L,n,Instruction);
103  f->sizecode=n;
104  LoadVector(S,f->code,n,sizeof(Instruction));
105 }
106
107 static Proto* LoadFunction(LoadState* S, TString* p);
108
109 static void LoadConstants(LoadState* S, Proto* f)
110 {
111  int i,n;
112  n=LoadInt(S);
113  f->k=luaM_newvector(S->L,n,TValue);
114  f->sizek=n;
115  for (i=0; i<n; i++) setnilvalue(&f->k[i]);
116  for (i=0; i<n; i++)
117  {
118   TValue* o=&f->k[i];
119   int t=LoadChar(S);
120   switch (t)
121   {
122    case LUA_TNIL:
123         setnilvalue(o);
124         break;
125    case LUA_TBOOLEAN:
126         setbvalue(o,LoadChar(S)!=0);
127         break;
128    case LUA_TNUMBER:
129         setnvalue(o,LoadNumber(S));
130         break;
131 #ifdef LUA_TINT
132    case LUA_TINT:   /* Integer type saved in bytecode (see lcode.c) */
133         setivalue(o,LoadInteger(S));
134         break;
135 #endif
136    case LUA_TSTRING:
137         setsvalue2n(S->L,o,LoadString(S));
138         break;
139    default:
140         error(S,"bad constant");
141         break;
142   }
143  }
144  n=LoadInt(S);
145  f->p=luaM_newvector(S->L,n,Proto*);
146  f->sizep=n;
147  for (i=0; i<n; i++) f->p[i]=NULL;
148  for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
149 }
150
151 static void LoadDebug(LoadState* S, Proto* f)
152 {
153  int i,n;
154  n=LoadInt(S);
155  f->lineinfo=luaM_newvector(S->L,n,int);
156  f->sizelineinfo=n;
157  LoadVector(S,f->lineinfo,n,sizeof(int));
158  n=LoadInt(S);
159  f->locvars=luaM_newvector(S->L,n,LocVar);
160  f->sizelocvars=n;
161  for (i=0; i<n; i++) f->locvars[i].varname=NULL;
162  for (i=0; i<n; i++)
163  {
164   f->locvars[i].varname=LoadString(S);
165   f->locvars[i].startpc=LoadInt(S);
166   f->locvars[i].endpc=LoadInt(S);
167  }
168  n=LoadInt(S);
169  f->upvalues=luaM_newvector(S->L,n,TString*);
170  f->sizeupvalues=n;
171  for (i=0; i<n; i++) f->upvalues[i]=NULL;
172  for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
173 }
174
175 static Proto* LoadFunction(LoadState* S, TString* p)
176 {
177  Proto* f;
178  if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
179  f=luaF_newproto(S->L);
180  setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
181  f->source=LoadString(S); if (f->source==NULL) f->source=p;
182  f->linedefined=LoadInt(S);
183  f->lastlinedefined=LoadInt(S);
184  f->nups=LoadByte(S);
185  f->numparams=LoadByte(S);
186  f->is_vararg=LoadByte(S);
187  f->maxstacksize=LoadByte(S);
188  LoadCode(S,f);
189  LoadConstants(S,f);
190  LoadDebug(S,f);
191  IF (!luaG_checkcode(f), "bad code");
192  S->L->top--;
193  S->L->nCcalls--;
194  return f;
195 }
196
197 static void LoadHeader(LoadState* S)
198 {
199  char h[LUAC_HEADERSIZE];
200  char s[LUAC_HEADERSIZE];
201  luaU_header(h);
202  LoadBlock(S,s,LUAC_HEADERSIZE);
203  IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
204 }
205
206 /*
207 ** load precompiled chunk
208 */
209 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
210 {
211  LoadState S;
212  if (*name=='@' || *name=='=')
213   S.name=name+1;
214  else if (*name==LUA_SIGNATURE[0])
215   S.name="binary string";
216  else
217   S.name=name;
218  S.L=L;
219  S.Z=Z;
220  S.b=buff;
221  LoadHeader(&S);
222  return LoadFunction(&S,luaS_newliteral(L,"=?"));
223 }
224
225 /*
226 * make header
227 */
228 void luaU_header (char* h)
229 {
230  int x=1;
231  memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
232  h+=sizeof(LUA_SIGNATURE)-1;
233  *h++=(char)LUAC_VERSION;
234  *h++=(char)LUAC_FORMAT;
235  *h++=(char)*(char*)&x;                         /* endianness */
236  *h++=(char)sizeof(int);
237  *h++=(char)sizeof(size_t);
238  *h++=(char)sizeof(Instruction);
239  *h++=(char)sizeof(lua_Number);
240
241  /* 
242   * Last byte of header (0/1 in unpatched Lua 5.1.3):
243   *
244   * 0: lua_Number is float/double/ldouble (nonpatched only)
245   * 1: lua_Number is integer (nonpatched only)
246   * 4: LNUM_INT32: sizeof(lua_Integer)
247   * 8: LNUM_INT64: sizeof(lua_Integer)
248   * +0x80: LNUM_COMPLEX
249   */
250  *h++ = (char)( sizeof(lua_Integer)
251 #ifdef LNUM_COMPLEX
252     | 0x80
253 #endif
254     );
255 }