-
Notifications
You must be signed in to change notification settings - Fork 2
/
blob.go
375 lines (328 loc) · 6.01 KB
/
blob.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package wzexplorer
import (
"encoding/binary"
"errors"
"io"
"math"
"unicode/utf16"
)
type BlobReader interface {
io.ReaderAt
io.Closer
}
type Blob struct {
off, len int64
o binary.ByteOrder
fd BlobReader
provider *CryptProvider
swap []byte // attention: concurrent problem
}
func newBlob(reader BlobReader, o binary.ByteOrder, crypt *CryptProvider, size int64) *Blob {
b := &Blob{}
b.off = 0
b.len = size
b.fd = reader
b.o = o
b.provider = crypt
b.swap = make([]byte, 8, 8)
return b
}
func (b *Blob) Read(data []byte) (n int, err error) {
if b.len == -1 {
return -1, io.ErrClosedPipe
}
n, err = b.fd.ReadAt(data, b.off)
if n > 0 {
b.off += int64(n)
}
return
}
func (b *Blob) readSwap(size int) (buf []byte, err error) {
if size > 8 || size <= 0 {
err = errors.New("invalid size out of range")
return
}
var n int
n, err = b.Read(b.swap[:size])
if err != nil {
return
}
if n > 0 {
buf = b.swap[:size]
} else {
err = io.EOF
}
return
}
func (b *Blob) ReadByte() (value byte, err error) {
var buf []byte
buf, err = b.readSwap(1)
if err != nil {
return
} else {
value = buf[0]
}
return
}
func (b *Blob) ReadUInt8() (value uint8, err error) {
value, err = b.ReadByte()
return
}
func (b *Blob) ReadInt8() (value int8, err error) {
var value1 byte
value1, err = b.ReadByte()
if err != nil {
return
} else {
value = int8(value1)
}
return
}
func (b *Blob) ReadUInt16() (value uint16, err error) {
var buf []byte
buf, err = b.readSwap(2)
if err != nil {
return
} else {
value = b.o.Uint16(buf)
}
return
}
func (b *Blob) ReadInt16() (value int16, err error) {
var u uint16
u, err = b.ReadUInt16()
if err != nil {
return
} else {
value = int16(u)
}
return
}
func (b *Blob) ReadUInt32() (value uint32, err error) {
var buf []byte
buf, err = b.readSwap(4)
if err != nil {
return
} else {
value = b.o.Uint32(buf)
}
return
}
func (b *Blob) ReadInt32() (value int32, err error) {
var u uint32
u, err = b.ReadUInt32()
if err != nil {
return
} else {
value = int32(u)
}
return
}
func (b *Blob) ReadUInt64() (value uint64, err error) {
var buf []byte
buf, err = b.readSwap(8)
if err != nil {
return
} else {
value = b.o.Uint64(buf)
}
return
}
func (b *Blob) ReadInt64() (value int64, err error) {
var u uint64
u, err = b.ReadUInt64()
if err != nil {
return
} else {
value = int64(u)
}
return
}
func (b *Blob) ReadFloat32() (value float32, err error) {
var u32 uint32
u32, err = b.ReadUInt32()
if err != nil {
return
}
value = math.Float32frombits(u32)
return
}
func (b *Blob) ReadFloat64() (value float64, err error) {
var u64 uint64
u64, err = b.ReadUInt64()
if err != nil {
return
}
value = math.Float64frombits(u64)
return
}
func (b *Blob) ReadCompressInt32() (value int32, err error) {
var flag int8
flag, err = b.ReadInt8()
if err != nil {
return
}
if flag == -128 {
value, err = b.ReadInt32()
} else {
value = int32(flag)
}
return
}
func (b *Blob) ReadCompressInt64() (value int64, err error) {
var flag int8
flag, err = b.ReadInt8()
if err != nil {
return
}
if flag == -128 {
value, err = b.ReadInt64()
} else {
value = int64(flag)
}
return
}
func (b *Blob) ReadUTF8String(size int, mask bool) (value string, err error) {
buf := make([]byte, size, size)
var n int
n, err = b.Read(buf)
if err != nil {
return
}
if n != size {
err = io.EOF
return
}
if mask {
start := byte(0xaa)
for i := 0; i < size; i++ {
buf[i] ^= start
start++
}
}
b.provider.crypt.Transform(buf)
value = string(buf)
return
}
func (b *Blob) ReadUTF16String(size int, mask bool) (value string, err error) {
size = size << 1
buf := make([]byte, size, size)
var n int
n, err = b.Read(buf)
if err != nil {
return
}
if n != size {
err = io.EOF
return
}
var unicode []uint16
if mask {
b.provider.crypt.ExpandXorTable(size)
xor := b.provider.crypt.Xor()
start := uint16(0xaaaa)
for i := 0; i < size; i += 2 {
unicode = append(unicode, b.o.Uint16(buf[i:])^start^b.o.Uint16(xor[i:]))
start++
}
} else {
b.provider.crypt.Transform(buf)
for i := 0; i < size; i += 2 {
unicode = append(unicode, b.o.Uint16(buf[i:]))
}
}
value = string(utf16.Decode(unicode))
return
}
func (b *Blob) ReadEncryptString() (value string, err error) {
var size int8
size, err = b.ReadInt8()
if err != nil {
return
}
strLength := int32(size)
unicode := size > 0
if strLength == -128 || strLength == 127 {
strLength, err = b.ReadInt32()
if err != nil {
return
}
}
if strLength == 0 {
return "", nil
}
if !unicode {
strLength = int32(math.Abs(float64(strLength)))
}
if unicode {
value, err = b.ReadUTF16String(int(strLength), true)
} else {
value, err = b.ReadUTF8String(int(strLength), true)
}
return
}
func (b *Blob) ReadUOLString(offset int64) (tag string, err error) {
var k byte
if k, err = b.ReadByte(); err != nil {
return
}
// 00, 01 for UOL string
switch k {
case 0x00, 0x73:
tag, err = b.ReadEncryptString()
case 0x01, 0x1B:
// UOL
var off int32
if off, err = b.ReadInt32(); err != nil {
return
}
if err = b.Peek(func() error {
if _, err = b.Seek(int64(off)+offset, io.SeekStart); err != nil {
return err
}
tag, err = b.ReadEncryptString()
return err
}); err != nil {
return
}
default:
err = errors.New("invalid uol key")
}
return
}
func (b *Blob) Peek(cb func() error) error {
cur, err := b.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
defer b.Seek(cur, io.SeekStart)
if err = cb(); err != nil {
return err
}
return nil
}
func (b *Blob) Seek(offset int64, whence int) (ret int64, err error) {
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = b.off + offset
case io.SeekEnd:
abs = b.len + offset
default:
return 0, errors.New("invalid whence")
}
if abs < 0 {
return 0, errors.New("negative position")
}
b.off = abs
return abs, nil
}
func (b *Blob) Len() int64 {
return b.len
}
func (b *Blob) Close() error {
b.len = -1
return b.fd.Close()
}