]> git.zerfleddert.de Git - proxmark3-svn/blame - liblua/lundump.c
fix #532. looks at length of parameters.
[proxmark3-svn] / liblua / lundump.c
CommitLineData
ba8b5c17 1/*
2** $Id: lundump.c,v 2.22 2012/05/08 13:53:33 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
23typedef struct {
24 lua_State* L;
25 ZIO* Z;
26 Mbuffer* b;
27 const char* name;
28} LoadState;
29
30static l_noret error(LoadState* S, const char* why)
31{
32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
33 luaD_throw(S->L,LUA_ERRSYNTAX);
34}
35
36#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
37#define LoadByte(S) (lu_byte)LoadChar(S)
38#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
39#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
40
41#if !defined(luai_verifycode)
42#define luai_verifycode(L,b,f) /* empty */
43#endif
44
45static void LoadBlock(LoadState* S, void* b, size_t size)
46{
47 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
48}
49
50static int LoadChar(LoadState* S)
51{
52 char x;
53 LoadVar(S,x);
54 return x;
55}
56
57static int LoadInt(LoadState* S)
58{
59 int x;
60 LoadVar(S,x);
61 if (x<0) error(S,"corrupted");
62 return x;
63}
64
65static lua_Number LoadNumber(LoadState* S)
66{
67 lua_Number x;
68 LoadVar(S,x);
69 return x;
70}
71
72static TString* LoadString(LoadState* S)
73{
74 size_t size;
75 LoadVar(S,size);
76 if (size==0)
77 return NULL;
78 else
79 {
80 char* s=luaZ_openspace(S->L,S->b,size);
81 LoadBlock(S,s,size*sizeof(char));
82 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
83 }
84}
85
86static void LoadCode(LoadState* S, Proto* f)
87{
88 int n=LoadInt(S);
89 f->code=luaM_newvector(S->L,n,Instruction);
90 f->sizecode=n;
91 LoadVector(S,f->code,n,sizeof(Instruction));
92}
93
94static void LoadFunction(LoadState* S, Proto* f);
95
96static void LoadConstants(LoadState* S, Proto* f)
97{
98 int i,n;
99 n=LoadInt(S);
100 f->k=luaM_newvector(S->L,n,TValue);
101 f->sizek=n;
102 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
103 for (i=0; i<n; i++)
104 {
105 TValue* o=&f->k[i];
106 int t=LoadChar(S);
107 switch (t)
108 {
109 case LUA_TNIL:
110 setnilvalue(o);
111 break;
112 case LUA_TBOOLEAN:
113 setbvalue(o,LoadChar(S));
114 break;
115 case LUA_TNUMBER:
116 setnvalue(o,LoadNumber(S));
117 break;
118 case LUA_TSTRING:
119 setsvalue2n(S->L,o,LoadString(S));
120 break;
121 default: lua_assert(0);
122 }
123 }
124 n=LoadInt(S);
125 f->p=luaM_newvector(S->L,n,Proto*);
126 f->sizep=n;
127 for (i=0; i<n; i++) f->p[i]=NULL;
128 for (i=0; i<n; i++)
129 {
130 f->p[i]=luaF_newproto(S->L);
131 LoadFunction(S,f->p[i]);
132 }
133}
134
135static void LoadUpvalues(LoadState* S, Proto* f)
136{
137 int i,n;
138 n=LoadInt(S);
139 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
140 f->sizeupvalues=n;
141 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
142 for (i=0; i<n; i++)
143 {
144 f->upvalues[i].instack=LoadByte(S);
145 f->upvalues[i].idx=LoadByte(S);
146 }
147}
148
149static void LoadDebug(LoadState* S, Proto* f)
150{
151 int i,n;
152 f->source=LoadString(S);
153 n=LoadInt(S);
154 f->lineinfo=luaM_newvector(S->L,n,int);
155 f->sizelineinfo=n;
156 LoadVector(S,f->lineinfo,n,sizeof(int));
157 n=LoadInt(S);
158 f->locvars=luaM_newvector(S->L,n,LocVar);
159 f->sizelocvars=n;
160 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
161 for (i=0; i<n; i++)
162 {
163 f->locvars[i].varname=LoadString(S);
164 f->locvars[i].startpc=LoadInt(S);
165 f->locvars[i].endpc=LoadInt(S);
166 }
167 n=LoadInt(S);
168 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
169}
170
171static void LoadFunction(LoadState* S, Proto* f)
172{
173 f->linedefined=LoadInt(S);
174 f->lastlinedefined=LoadInt(S);
175 f->numparams=LoadByte(S);
176 f->is_vararg=LoadByte(S);
177 f->maxstacksize=LoadByte(S);
178 LoadCode(S,f);
179 LoadConstants(S,f);
180 LoadUpvalues(S,f);
181 LoadDebug(S,f);
182}
183
184/* the code below must be consistent with the code in luaU_header */
185#define N0 LUAC_HEADERSIZE
186#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
187#define N2 N1+2
188#define N3 N2+6
189
190static void LoadHeader(LoadState* S)
191{
192 lu_byte h[LUAC_HEADERSIZE];
193 lu_byte s[LUAC_HEADERSIZE];
194 luaU_header(h);
195 memcpy(s,h,sizeof(char)); /* first char already read */
196 LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
197 if (memcmp(h,s,N0)==0) return;
198 if (memcmp(h,s,N1)!=0) error(S,"not a");
199 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
200 if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
201}
202
203/*
204** load precompiled chunk
205*/
206Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
207{
208 LoadState S;
209 Closure* cl;
210 if (*name=='@' || *name=='=')
211 S.name=name+1;
212 else if (*name==LUA_SIGNATURE[0])
213 S.name="binary string";
214 else
215 S.name=name;
216 S.L=L;
217 S.Z=Z;
218 S.b=buff;
219 LoadHeader(&S);
220 cl=luaF_newLclosure(L,1);
221 setclLvalue(L,L->top,cl); incr_top(L);
222 cl->l.p=luaF_newproto(L);
223 LoadFunction(&S,cl->l.p);
224 if (cl->l.p->sizeupvalues != 1)
225 {
226 Proto* p=cl->l.p;
227 cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
228 cl->l.p=p;
229 setclLvalue(L,L->top-1,cl);
230 }
231 luai_verifycode(L,buff,cl->l.p);
232 return cl;
233}
234
235#define MYINT(s) (s[0]-'0')
236#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
237#define FORMAT 0 /* this is the official format */
238
239/*
240* make header for precompiled chunks
241* if you change the code below be sure to update LoadHeader and FORMAT above
242* and LUAC_HEADERSIZE in lundump.h
243*/
244void luaU_header (lu_byte* h)
245{
246 int x=1;
247 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
248 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
249 *h++=cast_byte(VERSION);
250 *h++=cast_byte(FORMAT);
251 *h++=cast_byte(*(char*)&x); /* endianness */
252 *h++=cast_byte(sizeof(int));
253 *h++=cast_byte(sizeof(size_t));
254 *h++=cast_byte(sizeof(Instruction));
255 *h++=cast_byte(sizeof(lua_Number));
256 *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
257 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
258}
Impressum, Datenschutz