-
Notifications
You must be signed in to change notification settings - Fork 8
/
fileblob.go
361 lines (304 loc) · 6.95 KB
/
fileblob.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
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
package wz
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/goinggo/workpool"
"strconv"
)
type WZFileBlob struct {
reader *bytes.Reader
encryption *Encryption
file *WZFile
contentsStart int32
Debug bool
Name string
debug func(...interface{})
data []byte
workPool *workpool.WorkPool
}
func NewWZFileBlob(data []byte, encryption *Encryption, file *WZFile) *WZFileBlob {
m := new(WZFileBlob)
m.contentsStart = 0
m.encryption = encryption
m.Debug = file.Debug
m.Name = file.Filename
m.debug = file.debug
m.file = file
m.data = data
m.reader = bytes.NewReader(m.data)
m.workPool = file.workPool
return m
}
func (m *WZFileBlob) Copy() *WZFileBlob {
obj := NewWZFileBlob(m.data, m.encryption, m.file)
obj.contentsStart = m.contentsStart
return obj
}
func (m *WZFileBlob) CopySliced(start int) *WZFileBlob {
obj := NewWZFileBlob(m.data[start:], m.encryption, m.file)
obj.contentsStart = m.contentsStart
return obj
}
func (m *WZFileBlob) readByte() (out uint8) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readSByte() (out int8) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readInt16() (out int16) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readInt32() (out int32) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readInt64() (out int64) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readUInt16() (out uint16) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readUInt32() (out uint32) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readUInt64() (out uint64) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readFloat32() (out float32) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readFloat64() (out float64) {
if err := binary.Read(m.reader, binary.LittleEndian, &out); err != nil {
panic(err)
}
return
}
func (m *WZFileBlob) readBytes(size int32) []byte {
var out []byte = make([]byte, size)
amount, err := m.reader.Read(out)
if err != nil {
panic(err)
}
if int32(amount) != size {
panic(errors.New(fmt.Sprintln("Expected ", size, " bytes, got ", amount)))
}
return out
}
// readASCIIZString reads strings until the null terminator
func (m *WZFileBlob) readASCIIZString() string {
ret := make([]byte, 0)
for {
b, err := m.reader.ReadByte()
if err != nil {
panic(err)
}
if b == 0 {
break
}
ret = append(ret, b)
}
return string(ret)
}
func (m *WZFileBlob) readASCIIString(length int32) string {
return string(m.readBytes(length))
}
// Helper functions
func (m *WZFileBlob) seek(offset int64) {
if m.Debug {
m.debug("Seeking ", offset, " bytes (@ ", m.pos(), ")")
}
newOffset, err := m.reader.Seek(offset, 0)
if err != nil {
panic(err)
}
if m.Debug {
m.debug("New offset ", newOffset, " (@ ", m.pos(), ")")
}
}
func (m *WZFileBlob) skip(offset int64) {
_, err := m.reader.Seek(offset, 1)
if err != nil {
panic(err)
}
}
func (m *WZFileBlob) pos() int64 {
offset, err := m.reader.Seek(0, 1)
if err != nil {
panic(err)
}
return offset
}
func (m *WZFileBlob) len() int64 {
curpos := m.pos()
len, err := m.reader.Seek(0, 2)
if err != nil {
panic(err)
}
m.seek(curpos)
return len
}
type AnonFunc func()
func (m *WZFileBlob) peekFor(f AnonFunc) {
offset, err := m.reader.Seek(0, 1)
defer func() {
// Seek back to where we were
m.seek(offset)
}()
if err != nil {
panic(err)
}
f()
}
// WZ data related functions
func (m *WZFileBlob) readDeDuplicatedWZString(uol string, possibleNeededOffset int64, addOffset bool) (result string) {
key := m.readByte()
str := ""
switch key {
case 0, 0x73:
str = m.readWZString(uol)
case 1, 0x1B:
m.peekFor(func() {
tmp := possibleNeededOffset
if addOffset {
tmp += int64(m.readUInt32())
} else {
tmp -= int64(m.readUInt32())
}
if m.Debug {
m.debug("Reading dedup string at ", tmp)
}
m.seek(tmp)
str = m.readWZString(uol)
})
default:
panic("Unknown deduplicated wz string type: " + strconv.Itoa(int(key)) + " at " + uol + " AT " + strconv.Itoa(int(m.pos())))
}
if m.Debug {
m.debug("Dedupped string for ", uol, ": ", str)
}
return str
}
func (m *WZFileBlob) readWZObjectUOL(uol string, possibleNeededOffset int64) (result string) {
key := m.readByte()
str := ""
switch key {
case 0, 0x73:
str = m.readWZString(uol)
break
case 1, 0x1B:
possibleNeededOffset += int64(m.readUInt32())
m.peekFor(func() {
tmp := possibleNeededOffset
if m.Debug {
m.debug("Reading wz object uol at ", tmp)
}
m.seek(tmp)
str = m.readWZString(uol)
})
break
default:
panic("Unknown deduplicated wz string type: " + strconv.Itoa(int(key)) + " at " + uol + " AT " + strconv.Itoa(int(m.pos())))
}
if m.Debug {
m.debug("Dedupped string for ", uol, ": ", str)
}
return str
}
func (m *WZFileBlob) readWZString(uol string) (result string) {
var size int32 = int32(m.readSByte())
ascii := false
// Unicode strings are positive, ASCII negative
if size > 0 {
if size == 127 {
size = m.readInt32()
}
size *= 2
} else {
if size == -128 {
size = m.readInt32()
} else {
size *= -1
}
ascii = true
}
if size == 0 {
return ""
}
characters := m.readBytes(size)
var i int32 = 0
if !ascii {
var mask uint16 = 0xAAAA
for ; i < size; i += 2 {
var char uint16 = uint16(characters[i] | characters[i+1]<<8)
char ^= mask
characters[i] = byte(char)
characters[i+1] = byte(char >> 8)
mask++
}
} else {
var mask uint8 = 0xAA
for ; i < size; i++ {
x := characters[i]
x ^= mask
characters[i] = x
mask++
}
}
if m.encryption != nil && m.encryption.IsEncrypted(uol) {
m.encryption.TransformBuffer(characters)
}
return string(characters)
}
func (m *WZFileBlob) readWZInt() int32 {
if possibleSize := m.readSByte(); possibleSize == -128 {
return m.readInt32()
} else {
return int32(possibleSize)
}
}
func (m *WZFileBlob) readWZLong() int64 {
if possibleSize := m.readSByte(); possibleSize == -128 {
return m.readInt64()
} else {
return int64(possibleSize)
}
}
func (m *WZFileBlob) readWZOffset() uint32 {
offset := uint32(m.pos())
offset = (offset - uint32(m.contentsStart)) ^ 0xFFFFFFFF
offset *= m.file.versionHash
offset -= uint32(0x581C3F6D) // Who doesn't like magic values?
offset = rotl(offset, byte(offset&0x1F))
encryptedOffset := m.readUInt32()
offset ^= encryptedOffset
offset += (uint32(m.contentsStart) * 2)
return offset
}