FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lua55c/src/lundump.c at main · 2MoonsLua/lua55c · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
2MoonsLua
/
lua55c
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lua55c
/
src
/
lundump.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
424 lines (358 loc) · 10.8 KB
Breadcrumbs
lua55c
/
src
/
lundump.c
Copy path
File metadata and controls
424 lines (358 loc) · 10.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
** $Id: lundump.c $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
#define
lundump_c
#define
LUA_CORE
#include
"lprefix.h"
#include
<limits.h>
#include
<string.h>
#include
"lua.h"
#include
"ldebug.h"
#include
"ldo.h"
#include
"lfunc.h"
#include
"lmem.h"
#include
"lobject.h"
#include
"lstring.h"
#include
"ltable.h"
#include
"lundump.h"
#include
"lzio.h"
#if
!defined(
luai_verifycode
)
#define
luai_verifycode
(
L
,
f
)
/* empty */
#endif
typedef
struct
{
lua_State
*
L
;
ZIO
*
Z
;
const
char
*
name
;
Table
*
h
;
/* list for string reuse */
size_t
offset
;
/* current position relative to beginning of dump */
lua_Unsigned
nstr
;
/* number of strings in the list */
lu_byte
fixed
;
/* dump is fixed in memory */
}
LoadState
;
static
l_noret
error
(
LoadState
*
S
,
const
char
*
why
) {
luaO_pushfstring
(
S
->
L
,
"%s: bad binary format (%s)"
,
S
->
name
,
why
);
luaD_throw
(
S
->
L
,
LUA_ERRSYNTAX
);
}
/*
** All high-level loads go through loadVector; you can change it to
** adapt to the endianness of the input
*/
#define
loadVector
(
S
,
b
,
n
) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
static
void
loadBlock
(
LoadState
*
S
,
void
*
b
,
size_t
size
) {
if
(
luaZ_read
(
S
->
Z
,
b
,
size
)
!=
0
)
error
(
S
,
"truncated chunk"
);
S
->
offset
+=
size
;
}
static
void
loadAlign
(
LoadState
*
S
,
unsigned
align
) {
unsigned
padding
=
align
-
cast_uint
(
S
->
offset
%
align
);
if
(
padding
<
align
) {
/* (padding == align) means no padding */
lua_Integer
paddingContent
;
loadBlock
(
S
,
&
paddingContent
,
padding
);
lua_assert
(
S
->
offset
%
align
==
0
);
}
}
#define
getaddr
(
S
,
n
,
t
) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
static
const
void
*
getaddr_
(
LoadState
*
S
,
size_t
size
) {
const
void
*
block
=
luaZ_getaddr
(
S
->
Z
,
size
);
S
->
offset
+=
size
;
if
(
block
==
NULL
)
error
(
S
,
"truncated fixed buffer"
);
return
block
;
}
#define
loadVar
(
S
,
x
) loadVector(S,&x,1)
static
lu_byte
loadByte
(
LoadState
*
S
) {
int
b
=
zgetc
(
S
->
Z
);
if
(
b
==
EOZ
)
error
(
S
,
"truncated chunk"
);
S
->
offset
++
;
return
cast_byte
(
b
);
}
static
lua_Unsigned
loadVarint
(
LoadState
*
S
,
lua_Unsigned
limit
) {
lua_Unsigned
x
=
0
;
int
b
;
limit
>>=
7
;
do
{
b
=
loadByte
(
S
);
if
(
x
>
limit
)
error
(
S
,
"integer overflow"
);
x
=
(
x
<<
7
) | (
b
&
0x7f
);
}
while
((
b
&
0x80
)
!=
0
);
return
x
;
}
static
size_t
loadSize
(
LoadState
*
S
) {
return
cast_sizet
(
loadVarint
(
S
,
MAX_SIZE
));
}
static
int
loadInt
(
LoadState
*
S
) {
return
cast_int
(
loadVarint
(
S
,
cast_sizet
(
INT_MAX
)));
}
static
lua_Number
loadNumber
(
LoadState
*
S
) {
lua_Number
x
;
loadVar
(
S
,
x
);
return
x
;
}
static
lua_Integer
loadInteger
(
LoadState
*
S
) {
lua_Unsigned
cx
=
loadVarint
(
S
,
LUA_MAXUNSIGNED
);
/* decode unsigned to signed */
if
((
cx
&
1
)
!=
0
)
return
l_castU2S
(~(
cx
>>
1
));
else
return
l_castU2S
(
cx
>>
1
);
}
/*
** Load a nullable string into slot 'sl' from prototype 'p'. The
** assignment to the slot and the barrier must be performed before any
** possible GC activity, to anchor the string. (Both 'loadVector' and
** 'luaH_setint' can call the GC.)
*/
static
void
loadString
(
LoadState
*
S
,
Proto
*
p
,
TString
*
*
sl
) {
lua_State
*
L
=
S
->
L
;
TString
*
ts
;
TValue
sv
;
size_t
size
=
loadSize
(
S
);
if
(
size
==
0
) {
/* previously saved string? */
lua_Unsigned
idx
=
loadVarint
(
S
,
LUA_MAXUNSIGNED
);
/* get its index */
TValue
stv
;
if
(
idx
==
0
) {
/* no string? */
lua_assert
(
*
sl
==
NULL
);
/* must be prefilled */
return
;
}
if
(
novariant
(
luaH_getint
(
S
->
h
,
l_castU2S
(
idx
),
&
stv
))
!=
LUA_TSTRING
)
error
(
S
,
"invalid string index"
);
*
sl
=
ts
=
tsvalue
(
&
stv
);
/* get its value */
luaC_objbarrier
(
L
,
p
,
ts
);
return
;
/* do not save it again */
}
else
if
((
size
-=
1
) <=
LUAI_MAXSHORTLEN
) {
/* short string? */
char
buff
[
LUAI_MAXSHORTLEN
+
1
];
/* extra space for '\0' */
loadVector
(
S
,
buff
,
size
+
1
);
/* load string into buffer */
*
sl
=
ts
=
luaS_newlstr
(
L
,
buff
,
size
);
/* create string */
luaC_objbarrier
(
L
,
p
,
ts
);
}
else
if
(
S
->
fixed
) {
/* for a fixed buffer, use a fixed string */
const
char
*
s
=
getaddr
(
S
,
size
+
1
,
char
);
/* get content address */
*
sl
=
ts
=
luaS_newextlstr
(
L
,
s
,
size
,
NULL
,
NULL
);
luaC_objbarrier
(
L
,
p
,
ts
);
}
else
{
/* create internal copy */
*
sl
=
ts
=
luaS_createlngstrobj
(
L
,
size
);
/* create string */
luaC_objbarrier
(
L
,
p
,
ts
);
loadVector
(
S
,
getlngstr
(
ts
),
size
+
1
);
/* load directly in final place */
}
/* add string to list of saved strings */
S
->
nstr
++
;
setsvalue
(
L
,
&
sv
,
ts
);
luaH_setint
(
L
,
S
->
h
,
l_castU2S
(
S
->
nstr
),
&
sv
);
luaC_objbarrierback
(
L
,
obj2gco
(
S
->
h
),
ts
);
}
static
void
loadCode
(
LoadState
*
S
,
Proto
*
f
) {
int
n
=
loadInt
(
S
);
loadAlign
(
S
,
sizeof
(
f
->
code
[
0
]));
if
(
S
->
fixed
) {
f
->
code
=
getaddr
(
S
,
n
,
Instruction
);
f
->
sizecode
=
n
;
}
else
{
f
->
code
=
luaM_newvectorchecked
(
S
->
L
,
n
,
Instruction
);
f
->
sizecode
=
n
;
loadVector
(
S
,
f
->
code
,
n
);
}
}
static
void
loadFunction
(
LoadState
*
S
,
Proto
*
f
);
static
void
loadConstants
(
LoadState
*
S
,
Proto
*
f
) {
int
i
;
int
n
=
loadInt
(
S
);
f
->
k
=
luaM_newvectorchecked
(
S
->
L
,
n
,
TValue
);
f
->
sizek
=
n
;
for
(
i
=
0
;
i
<
n
;
i
++
)
setnilvalue
(
&
f
->
k
[
i
]);
for
(
i
=
0
;
i
<
n
;
i
++
) {
TValue
*
o
=
&
f
->
k
[
i
];
int
t
=
loadByte
(
S
);
switch
(
t
) {
case
LUA_VNIL
:
setnilvalue
(
o
);
break
;
case
LUA_VFALSE
:
setbfvalue
(
o
);
break
;
case
LUA_VTRUE
:
setbtvalue
(
o
);
break
;
case
LUA_VNUMFLT
:
setfltvalue
(
o
,
loadNumber
(
S
));
break
;
case
LUA_VNUMINT
:
setivalue
(
o
,
loadInteger
(
S
));
break
;
case
LUA_VSHRSTR
:
case
LUA_VLNGSTR
: {
lua_assert
(
f
->
source
==
NULL
);
loadString
(
S
,
f
,
&
f
->
source
);
/* use 'source' to anchor string */
if
(
f
->
source
==
NULL
)
error
(
S
,
"bad format for constant string"
);
setsvalue2n
(
S
->
L
,
o
,
f
->
source
);
/* save it in the right place */
f
->
source
=
NULL
;
break
;
}
default
:
error
(
S
,
"invalid constant"
);
}
}
}
static
void
loadProtos
(
LoadState
*
S
,
Proto
*
f
) {
int
i
;
int
n
=
loadInt
(
S
);
f
->
p
=
luaM_newvectorchecked
(
S
->
L
,
n
,
Proto
*
);
f
->
sizep
=
n
;
for
(
i
=
0
;
i
<
n
;
i
++
)
f
->
p
[
i
]
=
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
) {
f
->
p
[
i
]
=
luaF_newproto
(
S
->
L
);
luaC_objbarrier
(
S
->
L
,
f
,
f
->
p
[
i
]);
loadFunction
(
S
,
f
->
p
[
i
]);
}
}
/*
** Load the upvalues for a function. The names must be filled first,
** because the filling of the other fields can raise read errors and
** the creation of the error message can call an emergency collection;
** in that case all prototypes must be consistent for the GC.
*/
static
void
loadUpvalues
(
LoadState
*
S
,
Proto
*
f
) {
int
i
;
int
n
=
loadInt
(
S
);
f
->
upvalues
=
luaM_newvectorchecked
(
S
->
L
,
n
,
Upvaldesc
);
f
->
sizeupvalues
=
n
;
for
(
i
=
0
;
i
<
n
;
i
++
)
/* make array valid for GC */
f
->
upvalues
[
i
].
name
=
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
) {
/* following calls can raise errors */
f
->
upvalues
[
i
].
instack
=
loadByte
(
S
);
f
->
upvalues
[
i
].
idx
=
loadByte
(
S
);
f
->
upvalues
[
i
].
kind
=
loadByte
(
S
);
}
}
static
void
loadDebug
(
LoadState
*
S
,
Proto
*
f
) {
int
i
;
int
n
=
loadInt
(
S
);
if
(
S
->
fixed
) {
f
->
lineinfo
=
getaddr
(
S
,
n
,
ls_byte
);
f
->
sizelineinfo
=
n
;
}
else
{
f
->
lineinfo
=
luaM_newvectorchecked
(
S
->
L
,
n
,
ls_byte
);
f
->
sizelineinfo
=
n
;
loadVector
(
S
,
f
->
lineinfo
,
n
);
}
n
=
loadInt
(
S
);
if
(
n
>
0
) {
loadAlign
(
S
,
sizeof
(
int
));
if
(
S
->
fixed
) {
f
->
abslineinfo
=
getaddr
(
S
,
n
,
AbsLineInfo
);
f
->
sizeabslineinfo
=
n
;
}
else
{
f
->
abslineinfo
=
luaM_newvectorchecked
(
S
->
L
,
n
,
AbsLineInfo
);
f
->
sizeabslineinfo
=
n
;
loadVector
(
S
,
f
->
abslineinfo
,
n
);
}
}
n
=
loadInt
(
S
);
f
->
locvars
=
luaM_newvectorchecked
(
S
->
L
,
n
,
LocVar
);
f
->
sizelocvars
=
n
;
for
(
i
=
0
;
i
<
n
;
i
++
)
f
->
locvars
[
i
].
varname
=
NULL
;
for
(
i
=
0
;
i
<
n
;
i
++
) {
loadString
(
S
,
f
,
&
f
->
locvars
[
i
].
varname
);
f
->
locvars
[
i
].
startpc
=
loadInt
(
S
);
f
->
locvars
[
i
].
endpc
=
loadInt
(
S
);
}
n
=
loadInt
(
S
);
if
(
n
!=
0
)
/* does it have debug information? */
n
=
f
->
sizeupvalues
;
/* must be this many */
for
(
i
=
0
;
i
<
n
;
i
++
)
loadString
(
S
,
f
,
&
f
->
upvalues
[
i
].
name
);
}
static
void
loadFunction
(
LoadState
*
S
,
Proto
*
f
) {
f
->
linedefined
=
loadInt
(
S
);
f
->
lastlinedefined
=
loadInt
(
S
);
f
->
numparams
=
loadByte
(
S
);
/* get only the meaningful flags */
f
->
flag
=
cast_byte
(
loadByte
(
S
)
&
~
PF_FIXED
);
if
(
S
->
fixed
)
f
->
flag
|=
PF_FIXED
;
/* signal that code is fixed */
f
->
maxstacksize
=
loadByte
(
S
);
loadCode
(
S
,
f
);
loadConstants
(
S
,
f
);
loadUpvalues
(
S
,
f
);
loadProtos
(
S
,
f
);
loadString
(
S
,
f
,
&
f
->
source
);
loadDebug
(
S
,
f
);
}
static
void
checkliteral
(
LoadState
*
S
,
const
char
*
s
,
const
char
*
msg
) {
char
buff
[
sizeof
(
LUA_SIGNATURE
)
+
sizeof
(
LUAC_DATA
)];
/* larger than both */
size_t
len
=
strlen
(
s
);
loadVector
(
S
,
buff
,
len
);
if
(
memcmp
(
s
,
buff
,
len
)
!=
0
)
error
(
S
,
msg
);
}
static
l_noret
numerror
(
LoadState
*
S
,
const
char
*
what
,
const
char
*
tname
) {
const
char
*
msg
=
luaO_pushfstring
(
S
->
L
,
"%s %s mismatch"
,
tname
,
what
);
error
(
S
,
msg
);
}
static
void
checknumsize
(
LoadState
*
S
,
int
size
,
const
char
*
tname
) {
if
(
size
!=
loadByte
(
S
))
numerror
(
S
,
"size"
,
tname
);
}
static
void
checknumformat
(
LoadState
*
S
,
int
eq
,
const
char
*
tname
) {
if
(!
eq
)
numerror
(
S
,
"format"
,
tname
);
}
#define
checknum
(
S
,
tvar
,
value
,
tname
) \
{ tvar i; checknumsize(S, sizeof(i), tname); \
loadVar(S, i); \
checknumformat(S, i == value, tname); }
static
void
checkHeader
(
LoadState
*
S
) {
/* skip 1st char (already read and checked) */
checkliteral
(
S
,
&
LUA_SIGNATURE
[
1
],
"not a binary chunk"
);
if
(
loadByte
(
S
)
!=
LUAC_VERSION
)
error
(
S
,
"version mismatch"
);
if
(
loadByte
(
S
)
!=
LUAC_FORMAT
)
error
(
S
,
"format mismatch"
);
checkliteral
(
S
,
LUAC_DATA
,
"corrupted chunk"
);
checknum
(
S
,
int
,
LUAC_INT
,
"int"
);
checknum
(
S
,
Instruction
,
LUAC_INST
,
"instruction"
);
checknum
(
S
,
lua_Integer
,
LUAC_INT
,
"Lua integer"
);
checknum
(
S
,
lua_Number
,
LUAC_NUM
,
"Lua number"
);
}
/*
** Load precompiled chunk.
*/
LClosure
*
luaU_undump
(
lua_State
*
L
,
ZIO
*
Z
,
const
char
*
name
,
int
fixed
) {
LoadState
S
;
LClosure
*
cl
;
if
(
*
name
==
'@'
||
*
name
==
'='
)
name
=
name
+
1
;
else
if
(
*
name
==
LUA_SIGNATURE
[
0
])
name
=
"binary string"
;
S
.
name
=
name
;
S
.
L
=
L
;
S
.
Z
=
Z
;
S
.
fixed
=
cast_byte
(
fixed
);
S
.
offset
=
1
;
/* fist byte was already read */
checkHeader
(
&
S
);
cl
=
luaF_newLclosure
(
L
,
loadByte
(
&
S
));
setclLvalue2s
(
L
,
L
->
top
.
p
,
cl
);
luaD_inctop
(
L
);
S
.
h
=
luaH_new
(
L
);
/* create list of saved strings */
S
.
nstr
=
0
;
sethvalue2s
(
L
,
L
->
top
.
p
,
S
.
h
);
/* anchor it */
luaD_inctop
(
L
);
cl
->
p
=
luaF_newproto
(
L
);
luaC_objbarrier
(
L
,
cl
,
cl
->
p
);
loadFunction
(
&
S
,
cl
->
p
);
if
(
cl
->
nupvalues
!=
cl
->
p
->
sizeupvalues
)
error
(
&
S
,
"corrupted chunk"
);
luai_verifycode
(
L
,
cl
->
p
);
L
->
top
.
p
--
;
/* pop table */
return
cl
;
}
Back
|
FazBrowse Home
|
New Git URL