-
Notifications
You must be signed in to change notification settings - Fork 192
/
undump.go
323 lines (298 loc) · 7.3 KB
/
undump.go
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
package lua
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"unsafe"
)
type loadState struct {
in io.Reader
order binary.ByteOrder
}
var header struct {
Signature [4]byte
Version, Format, Endianness, IntSize byte
PointerSize, InstructionSize byte
NumberSize, IntegralNumber byte
Tail [6]byte
}
var (
errUnknownConstantType = errors.New("lua: unknown constant type in lua binary")
errNotPrecompiledChunk = errors.New("lua: is not a precompiled chunk")
errVersionMismatch = errors.New("lua: version mismatch in precompiled chunk")
errIncompatible = errors.New("lua: incompatible precompiled chunk")
errCorrupted = errors.New("lua: corrupted precompiled chunk")
)
func (state *loadState) read(data interface{}) error {
return binary.Read(state.in, state.order, data)
}
func (state *loadState) readNumber() (f float64, err error) {
err = state.read(&f)
return
}
func (state *loadState) readInt() (i int32, err error) {
err = state.read(&i)
return
}
func (state *loadState) readPC() (pc, error) {
i, err := state.readInt()
return pc(i), err
}
func (state *loadState) readByte() (b byte, err error) {
err = state.read(&b)
return
}
func (state *loadState) readBool() (bool, error) {
b, err := state.readByte()
return b != 0, err
}
func (state *loadState) readString() (s string, err error) {
// Feel my pain
maxUint := ^uint(0)
var size uintptr
var size64 uint64
var size32 uint32
if uint64(maxUint) == math.MaxUint64 {
err = state.read(&size64)
size = uintptr(size64)
} else if maxUint == math.MaxUint32 {
err = state.read(&size32)
size = uintptr(size32)
} else {
panic(fmt.Sprintf("unsupported pointer size (%d)", maxUint))
}
if err != nil || size == 0 {
return
}
ba := make([]byte, size)
if err = state.read(ba); err == nil {
s = string(ba[:len(ba)-1])
}
return
}
func (state *loadState) readCode() (code []instruction, err error) {
n, err := state.readInt()
if err != nil || n == 0 {
return
}
code = make([]instruction, n)
err = state.read(code)
return
}
func (state *loadState) readUpValues() (u []upValueDesc, err error) {
n, err := state.readInt()
if err != nil || n == 0 {
return
}
v := make([]struct{ IsLocal, Index byte }, n)
err = state.read(v)
if err != nil {
return
}
u = make([]upValueDesc, n)
for i := range v {
u[i].isLocal, u[i].index = v[i].IsLocal != 0, int(v[i].Index)
}
return
}
func (state *loadState) readLocalVariables() (localVariables []localVariable, err error) {
var n int32
if n, err = state.readInt(); err != nil || n == 0 {
return
}
localVariables = make([]localVariable, n)
for i := range localVariables {
if localVariables[i].name, err = state.readString(); err != nil {
return
}
if localVariables[i].startPC, err = state.readPC(); err != nil {
return
}
if localVariables[i].endPC, err = state.readPC(); err != nil {
return
}
}
return
}
func (state *loadState) readLineInfo() (lineInfo []int32, err error) {
var n int32
if n, err = state.readInt(); err != nil || n == 0 {
return
}
lineInfo = make([]int32, n)
err = state.read(lineInfo)
return
}
func (state *loadState) readDebug(p *prototype) (source string, lineInfo []int32, localVariables []localVariable, names []string, err error) {
var n int32
if source, err = state.readString(); err != nil {
return
}
if lineInfo, err = state.readLineInfo(); err != nil {
return
}
if localVariables, err = state.readLocalVariables(); err != nil {
return
}
if n, err = state.readInt(); err != nil {
return
}
names = make([]string, n)
for i := range names {
if names[i], err = state.readString(); err != nil {
return
}
}
return
}
func (state *loadState) readConstants() (constants []value, prototypes []prototype, err error) {
var n int32
if n, err = state.readInt(); err != nil || n == 0 {
return
}
constants = make([]value, n)
for i := range constants {
var t byte
switch t, err = state.readByte(); {
case err != nil:
return
case t == byte(TypeNil):
constants[i] = nil
case t == byte(TypeBoolean):
constants[i], err = state.readBool()
case t == byte(TypeNumber):
constants[i], err = state.readNumber()
case t == byte(TypeString):
constants[i], err = state.readString()
default:
err = errUnknownConstantType
}
if err != nil {
return
}
}
return
}
func (state *loadState) readPrototypes() (prototypes []prototype, err error) {
var n int32
if n, err = state.readInt(); err != nil || n == 0 {
return
}
prototypes = make([]prototype, n)
for i := range prototypes {
if prototypes[i], err = state.readFunction(); err != nil {
return
}
}
return
}
func (state *loadState) readFunction() (p prototype, err error) {
var n int32
if n, err = state.readInt(); err != nil {
return
}
p.lineDefined = int(n)
if n, err = state.readInt(); err != nil {
return
}
p.lastLineDefined = int(n)
var b byte
if b, err = state.readByte(); err != nil {
return
}
p.parameterCount = int(b)
if b, err = state.readByte(); err != nil {
return
}
p.isVarArg = b != 0
if b, err = state.readByte(); err != nil {
return
}
p.maxStackSize = int(b)
if p.code, err = state.readCode(); err != nil {
return
}
if p.constants, p.prototypes, err = state.readConstants(); err != nil {
return
}
if p.prototypes, err = state.readPrototypes(); err != nil {
return
}
if p.upValues, err = state.readUpValues(); err != nil {
return
}
var names []string
if p.source, p.lineInfo, p.localVariables, names, err = state.readDebug(&p); err != nil {
return
}
for i, name := range names {
p.upValues[i].name = name
}
return
}
func init() {
copy(header.Signature[:], Signature)
header.Version = VersionMajor<<4 | VersionMinor
header.Format = 0
if endianness() == binary.LittleEndian {
header.Endianness = 1
} else {
header.Endianness = 0
}
header.IntSize = 4
header.PointerSize = byte(1+^uintptr(0)>>32&1) * 4
header.InstructionSize = byte(1+^instruction(0)>>32&1) * 4
header.NumberSize = 8
header.IntegralNumber = 0
tail := "\x19\x93\r\n\x1a\n"
copy(header.Tail[:], tail)
// The uintptr numeric type is implementation-specific
uintptrBitCount := byte(0)
for bits := ^uintptr(0); bits != 0; bits >>= 1 {
uintptrBitCount++
}
if uintptrBitCount != header.PointerSize*8 {
panic(fmt.Sprintf("invalid pointer size (%d)", uintptrBitCount))
}
}
func endianness() binary.ByteOrder {
if x := 1; *(*byte)(unsafe.Pointer(&x)) == 1 {
return binary.LittleEndian
}
return binary.BigEndian
}
func (state *loadState) checkHeader() error {
h := header
if err := state.read(&h); err != nil {
return err
} else if h == header {
return nil
} else if string(h.Signature[:]) != Signature {
return errNotPrecompiledChunk
} else if h.Version != header.Version || h.Format != header.Format {
return errVersionMismatch
} else if h.Tail != header.Tail {
return errCorrupted
}
return errIncompatible
}
func (l *State) undump(in io.Reader, name string) (c *luaClosure, err error) {
if name[0] == '@' || name[0] == '=' {
name = name[1:]
} else if name[0] == Signature[0] {
name = "binary string"
}
// TODO assign name to p.source?
s := &loadState{in, endianness()}
var p prototype
if err = s.checkHeader(); err != nil {
return
} else if p, err = s.readFunction(); err != nil {
return
}
c = l.newLuaClosure(&p)
l.push(c)
return
}