/
decode.go
193 lines (166 loc) · 4.83 KB
/
decode.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
package pfs
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"sort"
)
// Decode will load a pfs file
func Decode(r io.ReadSeeker) (*Pfs, error) {
pfs := &Pfs{}
err := parse(r, pfs)
if err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
return pfs, nil
}
func parse(r io.ReadSeeker, pfs *Pfs) error {
var directoryIndex uint32
var magicNumber uint32
var versionNumber uint32
var value uint32
err := binary.Read(r, binary.LittleEndian, &directoryIndex)
if err != nil {
return fmt.Errorf("read directory index: %w", err)
}
err = binary.Read(r, binary.LittleEndian, &magicNumber)
if err != nil {
return fmt.Errorf("read magic number: %w", err)
}
if magicNumber != 0x20534650 {
return fmt.Errorf("invalid magic number, got 0x%x, want 0x20534650", magicNumber)
}
err = binary.Read(r, binary.LittleEndian, &versionNumber)
if err != nil {
return fmt.Errorf("read version number: %w", err)
}
_, err = r.Seek(int64(directoryIndex), io.SeekStart)
if err != nil {
return fmt.Errorf("seek directory index: %w", err)
}
var fileCount uint32
err = binary.Read(r, binary.LittleEndian, &fileCount)
if err != nil {
return fmt.Errorf("read file count: %w", err)
}
if fileCount == 0 {
return fmt.Errorf("empty file")
}
filenames := []string{}
for i := 0; i < int(fileCount); i++ {
entry := &PfsEntry{}
err = binary.Read(r, binary.LittleEndian, &entry.CRC)
if err != nil {
return fmt.Errorf("read crc %d/%d: %w", i, fileCount, err)
}
err = binary.Read(r, binary.LittleEndian, &entry.Offset)
if err != nil {
return fmt.Errorf("read offset %d/%d: %w", i, fileCount, err)
}
debugInfo := fmt.Sprintf("%d/%d 0x%x", i, fileCount, entry.Offset)
// size is the uncompressed size of the file
var size uint32
err = binary.Read(r, binary.LittleEndian, &size)
if err != nil {
return fmt.Errorf("read size %s: %w", debugInfo, err)
}
buf := bytes.NewBuffer(nil)
cachedOffset, err := r.Seek(0, io.SeekCurrent)
if err != nil {
return fmt.Errorf("seek cached offset %s: %w", debugInfo, err)
}
_, err = r.Seek(int64(entry.Offset), io.SeekStart)
if err != nil {
return fmt.Errorf("seek offset %s: %w", debugInfo, err)
}
for uint32(buf.Len()) != size {
var deflatedLength uint32
var inflatedLength uint32
err = binary.Read(r, binary.LittleEndian, &deflatedLength)
if err != nil {
return fmt.Errorf("read deflated length %s: %w", debugInfo, err)
}
err = binary.Read(r, binary.LittleEndian, &inflatedLength)
if err != nil {
return fmt.Errorf("read inflated length %s: %w", debugInfo, err)
}
//if inflatedLength < deflatedLength {
// return fmt.Errorf("inflated < deflated, offset misaligned? %d/%d", i, fileCount)
//}
compressedData := make([]byte, deflatedLength)
err = binary.Read(r, binary.LittleEndian, compressedData)
if err != nil {
return fmt.Errorf("read compressed data: %s: %w", debugInfo, err)
}
fr, err := zlib.NewReaderDict(bytes.NewReader(compressedData), nil)
if err != nil {
return fmt.Errorf("zlib new reader dict %s: %w", debugInfo, err)
}
inflatedWritten, err := io.Copy(buf, fr)
if err != nil {
return fmt.Errorf("copy: %s: %w", debugInfo, err)
}
if inflatedWritten != int64(inflatedLength) {
return fmt.Errorf("inflate mismatch %s: %w", debugInfo, err)
}
}
if buf.Len() < int(size) {
return fmt.Errorf("size mismatch %s: %w", debugInfo, err)
}
entry.Data = buf.Bytes()
if entry.CRC == 0x61580AC9 {
fr := bytes.NewReader(buf.Bytes())
var filenameCount uint32
err = binary.Read(fr, binary.LittleEndian, &filenameCount)
if err != nil {
return fmt.Errorf("filename count %s: %w", debugInfo, err)
}
for j := uint32(0); j < filenameCount; j++ {
err = binary.Read(fr, binary.LittleEndian, &value)
if err != nil {
return fmt.Errorf("filename length %s: %w", debugInfo, err)
}
filename, err := parseFixedString(fr, value)
if err != nil {
return fmt.Errorf("filename %s: %w", debugInfo, err)
}
filenames = append(filenames, filename)
}
_, err = r.Seek(cachedOffset, io.SeekStart)
if err != nil {
return fmt.Errorf("seek cached offset %s: %w", debugInfo, err)
}
continue
}
pfs.Files = append(pfs.Files, entry)
_, err = r.Seek(cachedOffset, io.SeekStart)
if err != nil {
return fmt.Errorf("seek cached offset %s: %w", debugInfo, err)
}
}
sort.Sort(ByOffset(pfs.Files))
for i, entry := range pfs.Files {
if len(filenames) < i {
return fmt.Errorf("entry %d has no name", i)
}
entry.Name = filenames[i]
}
return nil
}
func parseFixedString(r io.ReadSeeker, size uint32) (string, error) {
in := make([]byte, size)
_, err := r.Read(in)
if err != nil {
return "", fmt.Errorf("read: %w", err)
}
final := ""
for _, char := range in {
if char == 0x0 {
continue
}
final += string(char)
}
return final, nil
}