]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/lundump.c
2 ** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
27 #if !defined(luai_verifycode)
28 #define luai_verifycode(L,b,f) /* empty */
40 static l_noret
error(LoadState
*S
, const char *why
) {
41 luaO_pushfstring(S
->L
, "%s: %s precompiled chunk", S
->name
, why
);
42 luaD_throw(S
->L
, LUA_ERRSYNTAX
);
47 ** All high-level loads go through LoadVector; you can change it to
48 ** adapt to the endianness of the input
50 #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
52 static void LoadBlock (LoadState
*S
, void *b
, size_t size
) {
53 if (luaZ_read(S
->Z
, b
, size
) != 0)
54 error(S
, "truncated");
58 #define LoadVar(S,x) LoadVector(S,&x,1)
61 static lu_byte
LoadByte (LoadState
*S
) {
68 static int LoadInt (LoadState
*S
) {
75 static lua_Number
LoadNumber (LoadState
*S
) {
82 static lua_Integer
LoadInteger (LoadState
*S
) {
89 static TString
*LoadString (LoadState
*S
) {
90 size_t size
= LoadByte(S
);
96 char *s
= luaZ_openspace(S
->L
, S
->b
, --size
);
97 LoadVector(S
, s
, size
);
98 return luaS_newlstr(S
->L
, s
, size
);
103 static void LoadCode (LoadState
*S
, Proto
*f
) {
105 f
->code
= luaM_newvector(S
->L
, n
, Instruction
);
107 LoadVector(S
, f
->code
, n
);
111 static void LoadFunction(LoadState
*S
, Proto
*f
, TString
*psource
);
114 static void LoadConstants (LoadState
*S
, Proto
*f
) {
117 f
->k
= luaM_newvector(S
->L
, n
, TValue
);
119 for (i
= 0; i
< n
; i
++)
120 setnilvalue(&f
->k
[i
]);
121 for (i
= 0; i
< n
; i
++) {
122 TValue
*o
= &f
->k
[i
];
129 setbvalue(o
, LoadByte(S
));
132 setfltvalue(o
, LoadNumber(S
));
135 setivalue(o
, LoadInteger(S
));
139 setsvalue2n(S
->L
, o
, LoadString(S
));
148 static void LoadProtos (LoadState
*S
, Proto
*f
) {
151 f
->p
= luaM_newvector(S
->L
, n
, Proto
*);
153 for (i
= 0; i
< n
; i
++)
155 for (i
= 0; i
< n
; i
++) {
156 f
->p
[i
] = luaF_newproto(S
->L
);
157 LoadFunction(S
, f
->p
[i
], f
->source
);
162 static void LoadUpvalues (LoadState
*S
, Proto
*f
) {
165 f
->upvalues
= luaM_newvector(S
->L
, n
, Upvaldesc
);
167 for (i
= 0; i
< n
; i
++)
168 f
->upvalues
[i
].name
= NULL
;
169 for (i
= 0; i
< n
; i
++) {
170 f
->upvalues
[i
].instack
= LoadByte(S
);
171 f
->upvalues
[i
].idx
= LoadByte(S
);
176 static void LoadDebug (LoadState
*S
, Proto
*f
) {
179 f
->lineinfo
= luaM_newvector(S
->L
, n
, int);
181 LoadVector(S
, f
->lineinfo
, n
);
183 f
->locvars
= luaM_newvector(S
->L
, n
, LocVar
);
185 for (i
= 0; i
< n
; i
++)
186 f
->locvars
[i
].varname
= NULL
;
187 for (i
= 0; i
< n
; i
++) {
188 f
->locvars
[i
].varname
= LoadString(S
);
189 f
->locvars
[i
].startpc
= LoadInt(S
);
190 f
->locvars
[i
].endpc
= LoadInt(S
);
193 for (i
= 0; i
< n
; i
++)
194 f
->upvalues
[i
].name
= LoadString(S
);
198 static void LoadFunction (LoadState
*S
, Proto
*f
, TString
*psource
) {
199 f
->source
= LoadString(S
);
200 if (f
->source
== NULL
) /* no source in dump? */
201 f
->source
= psource
; /* reuse parent's source */
202 f
->linedefined
= LoadInt(S
);
203 f
->lastlinedefined
= LoadInt(S
);
204 f
->numparams
= LoadByte(S
);
205 f
->is_vararg
= LoadByte(S
);
206 f
->maxstacksize
= LoadByte(S
);
215 static void checkliteral (LoadState
*S
, const char *s
, const char *msg
) {
216 char buff
[sizeof(LUA_SIGNATURE
) + sizeof(LUAC_DATA
)]; /* larger than both */
217 size_t len
= strlen(s
);
218 LoadVector(S
, buff
, len
);
219 if (memcmp(s
, buff
, len
) != 0)
224 static void fchecksize (LoadState
*S
, size_t size
, const char *tname
) {
225 if (LoadByte(S
) != size
)
226 error(S
, luaO_pushfstring(S
->L
, "%s size mismatch in", tname
));
230 #define checksize(S,t) fchecksize(S,sizeof(t),#t)
232 static void checkHeader (LoadState
*S
) {
233 checkliteral(S
, LUA_SIGNATURE
+ 1, "not a"); /* 1st char already checked */
234 if (LoadByte(S
) != LUAC_VERSION
)
235 error(S
, "version mismatch in");
236 if (LoadByte(S
) != LUAC_FORMAT
)
237 error(S
, "format mismatch in");
238 checkliteral(S
, LUAC_DATA
, "corrupted");
240 checksize(S
, size_t);
241 checksize(S
, Instruction
);
242 checksize(S
, lua_Integer
);
243 checksize(S
, lua_Number
);
244 if (LoadInteger(S
) != LUAC_INT
)
245 error(S
, "endianness mismatch in");
246 if (LoadNumber(S
) != LUAC_NUM
)
247 error(S
, "float format mismatch in");
252 ** load precompiled chunk
254 LClosure
*luaU_undump(lua_State
*L
, ZIO
*Z
, Mbuffer
*buff
,
258 if (*name
== '@' || *name
== '=')
260 else if (*name
== LUA_SIGNATURE
[0])
261 S
.name
= "binary string";
268 cl
= luaF_newLclosure(L
, LoadByte(&S
));
269 setclLvalue(L
, L
->top
, cl
);
271 cl
->p
= luaF_newproto(L
);
272 LoadFunction(&S
, cl
->p
, NULL
);
273 lua_assert(cl
->nupvalues
== cl
->p
->sizeupvalues
);
274 luai_verifycode(L
, buff
, cl
->p
);