-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ds_store.go
405 lines (365 loc) · 8.04 KB
/
ds_store.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
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
package ds_store
import (
"bytes"
"encoding/binary"
"errors"
"reflect"
"unicode/utf16"
"unicode/utf8"
"unsafe"
)
type Block struct {
Allocator *Allocator
Offset uint32
Size uint32
Data []byte
Pos uint32
}
type Allocator struct {
Data []byte
Pos uint32
Root *Block
Offsets []uint32
Toc map[string]uint32
FreeList map[uint32][]uint32
}
func NewBlock(a *Allocator, pos uint32, size uint32) (block *Block, err error) {
if len(a.Data) < int(pos+0x4+size) {
return nil, errors.New("Not enought Data")
}
block = &Block{Size: size, Allocator: a, Data: a.Data[pos+0x4 : pos+0x4+size]}
return block, nil
}
func (block *Block) readUint32() (value uint32, err error) {
if block.Size-block.Pos < 4 {
return 0, errors.New("Not enough bytes to read")
}
data := bytes.NewBuffer(block.Data)
data.Next(int(block.Pos))
binary.Read(data, binary.BigEndian, &value)
block.Pos += 4
return value, nil
}
func (block *Block) readByte() (value byte, err error) {
if block.Size-block.Pos < 1 {
return 0, errors.New("Not enough bytes to read")
}
data := bytes.NewBuffer(block.Data)
data.Next(int(block.Pos))
binary.Read(data, binary.BigEndian, &value)
block.Pos += 1
return value, nil
}
func (block *Block) readBuf(length int) (buf []byte, err error) {
if int(block.Size)-int(block.Pos) < length {
return nil, errors.New("Not enough bytes to read")
}
data := bytes.NewBuffer(block.Data)
data.Next(int(block.Pos))
buf = make([]byte, length)
binary.Read(data, binary.BigEndian, &buf)
block.Pos += uint32(length)
return buf, nil
}
func (block *Block) readFileName() (name string, err error) {
length, err := block.readUint32()
if err != nil {
return "", err
}
buf, err := block.readBuf(int(2 * length))
if err != nil {
return "", err
}
/*
sid, err := block.readUint32()
if err != nil {
return "", err
}
*/
block.skip(4)
stype, err := block.readBuf(4)
if err != nil {
return "", err
}
t := string(stype)
bytesToSkip := -1
switch {
case t == "bool":
bytesToSkip = 1
break
case t == "type" || t == "long" || t == "shor":
bytesToSkip = 4
break
case t == "comp" || t == "dutc":
bytesToSkip = 8
break
case t == "blob":
blen, err := block.readUint32()
if err != nil {
break
}
bytesToSkip = int(blen)
break
case t == "ustr":
blen, err := block.readUint32()
if err != nil {
break
}
bytesToSkip = int(2 * blen)
default:
break
}
if bytesToSkip <= 0 {
return "", errors.New("Unknown file format")
}
block.skip(uint32(bytesToSkip))
name = utf16be2utf8(buf)
return name, nil
}
func (block *Block) skip(i uint32) {
block.Pos += i
}
func NewAllocator(data []byte) (a *Allocator, err error) {
a = &Allocator{Data: data} //bytes.NewBuffer(data)}
a.Toc = make(map[string]uint32)
a.FreeList = make(map[uint32][]uint32)
offset, size, err := a.readHeader()
if err != nil {
return nil, err
}
a.Root, err = NewBlock(a, offset, size)
if err != nil {
return nil, err
}
err = a.readOffsets()
if err != nil {
return nil, err
}
err = a.readToc()
if err != nil {
return nil, err
}
err = a.readFreeList()
if err != nil {
return nil, err
}
return a, err
}
func (a *Allocator) GetBlock(bid uint32) (block *Block, err error) {
if len(a.Offsets) <= int(bid) {
return nil, errors.New("Cannot find key in Offset-Table")
}
addr := a.Offsets[bid]
offset := int(addr) & ^0x1f
size := 1 << (uint(addr) & 0x1f)
block, err = NewBlock(a, uint32(offset), uint32(size)) ///+4??
if err != nil {
return nil, errors.New("Cannot create/read block")
}
return block, nil
}
func (a *Allocator) TraverseFromRootNode() (filenames []string, err error) {
rootBlk, err := a.GetBlock(a.Toc["DSDB"])
if err != nil {
return nil, err
}
rootNode, err := rootBlk.readUint32()
if err != nil {
return nil, err
}
/*height, err := rootBlk.readUint32()
if err != nil {
return nil, err
}
recordsCount, err := rootBlk.readUint32()
if err != nil {
return nil, err
}
nodesCount, err := rootBlk.readUint32()
if err != nil {
return nil, err
}
blksize, err := rootBlk.readUint32()
if err != nil {
return nil, err
}*/
rootBlk.skip(4 * 4)
return a.Traverse(rootNode)
}
func (a *Allocator) Traverse(bid uint32) (filenames []string, err error) {
node, err := a.GetBlock(bid)
if err != nil {
return nil, err
}
nextPtr, err := node.readUint32()
if err != nil {
return nil, err
}
count, err := node.readUint32()
if err != nil {
return nil, err
}
if nextPtr > 0 {
//This may be broken
for i := 0; i < int(count); i++ {
next, err := node.readUint32()
if err != nil {
return nil, err
}
files, err := a.Traverse(next)
if err != nil {
return nil, err
}
for _, f := range files {
filenames = append(filenames, f)
}
f, err := node.readFileName()
if err != nil {
return nil, err
}
filenames = append(filenames, f)
}
files, err := a.Traverse(nextPtr)
if err != nil {
return nil, err
}
for _, f := range files {
filenames = append(filenames, f)
}
} else {
for i := 0; i < int(count); i++ {
f, err := node.readFileName()
if err != nil {
return nil, err
}
filenames = append(filenames, f)
}
}
return filenames, nil
}
func (a *Allocator) readFreeList() error {
for i := 0; i < 32; i++ {
blkcount, err := a.Root.readUint32()
if err != nil {
return err
}
if blkcount == 0 {
continue
}
a.FreeList[uint32(i)] = make([]uint32, 0)
for k := 0; k < int(blkcount); k++ {
val, err := a.Root.readUint32()
if err != nil {
return err
}
if val == 0 {
continue
}
a.FreeList[uint32(i)] = append(a.FreeList[uint32(i)], val)
}
}
return nil
}
func (a *Allocator) readToc() error {
toccount, err := a.Root.readUint32()
if err != nil {
return err
}
for i := toccount; i > 0; i-- {
tlen, err := a.Root.readByte()
if err != nil {
return err
}
name, err := a.Root.readBuf(int(tlen))
if err != nil {
return err
}
value, err := a.Root.readUint32()
if err != nil {
return err
}
a.Toc[string(name)] = value
}
return nil
}
func (a *Allocator) readOffsets() error {
count, err := a.Root.readUint32()
if err != nil {
return err
}
a.Root.skip(4)
for offcount := int(count); offcount > 0; offcount -= 256 {
for i := 0; i < 256; i++ {
val, err := a.Root.readUint32()
if err != nil {
return err
}
if val == 0 {
continue
}
a.Offsets = append(a.Offsets, val)
}
}
return nil
}
func (a *Allocator) readHeader() (offset uint32, size uint32, err error) {
data := bytes.NewBuffer(a.Data)
if data.Len() < 32 {
return offset, size, errors.New("Header not long enough")
}
var magic1, magic, offset2 uint32
binary.Read(data, binary.BigEndian, &magic1)
if magic1 != 1 {
return offset, size, errors.New("Wrong magic bytes")
}
a.Pos += 4
binary.Read(data, binary.BigEndian, &magic)
if magic != 0x42756431 {
return offset, size, errors.New("Wrong magic bytes")
}
a.Pos += 4
binary.Read(data, binary.BigEndian, &offset)
a.Pos += 4
binary.Read(data, binary.BigEndian, &size)
a.Pos += 4
binary.Read(data, binary.BigEndian, &offset2)
if offset != offset2 {
return offset, size, errors.New("Offets do not match")
}
a.Pos += 4
return offset, size, nil
}
func utf16be2utf8(utf16be []byte) string {
//Taken from http://play.golang.org/p/xtG1e9iqA1
n := len(utf16be)
// Convert to []uint16
// hop through unsafe to skip any actual allocation/copying
header := *(*reflect.SliceHeader)(unsafe.Pointer(&utf16be))
header.Len /= 2
shorts := *(*[]uint16)(unsafe.Pointer(&header))
// shorts may need byte-swapping
for i := 0; i < n; i += 2 {
shorts[i/2] = (uint16(utf16be[i]) << 8) | uint16(utf16be[i+1])
}
// Convert to []byte
count := 0
for i := 0; i < len(shorts); i++ {
r := rune(shorts[i])
if utf16.IsSurrogate(r) {
i++
r = utf16.DecodeRune(r, rune(shorts[i]))
}
count += utf8.RuneLen(r)
}
buf := make([]byte, count)
bi := 0
for i := 0; i < len(shorts); i++ {
r := rune(shorts[i])
if utf16.IsSurrogate(r) {
i++
r = utf16.DecodeRune(r, rune(shorts[i]))
}
bi += utf8.EncodeRune(buf[bi:], r)
}
return string(buf)
}