/
parser_pak.go
298 lines (238 loc) · 7.6 KB
/
parser_pak.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
package parser
import (
"context"
"github.com/rs/zerolog/log"
)
const INDEX_NONE = int64(-1)
func (parser *PakParser) Parse(ctx context.Context) *PakFile {
// Find magic number
magicOffset := int64(-44)
for {
parser.Seek(magicOffset, 2)
magicArray := parser.Read(4)
if magicArray[0] == 0xE1 && magicArray[1] == 0x12 && magicArray[2] == 0x6F && magicArray[3] == 0x5A {
break
}
magicOffset -= 1
if magicOffset < -1024 {
panic("Could not find magic bytes in pak!")
}
}
// Seek and read the footer of the file
parser.Seek(magicOffset, 2)
pakFooter := &FPakInfo{}
pakFooter.Magic = parser.ReadUint32()
pakFooter.Version = parser.ReadUint32()
pakFooter.IndexOffset = parser.ReadUint64()
pakFooter.IndexSize = parser.ReadUint64()
pakFooter.IndexSHA1Hash = parser.Read(20)
// Skip 1 byte
parser.Read(1)
pakFooter.CompressionType = parser.ReadStringNull()
if pakFooter.CompressionType != "" && pakFooter.CompressionType != "Zlib" {
log.Ctx(ctx).Error().Msgf("Compression method unsupported: %s", pakFooter.CompressionType)
return nil
}
// Seek and read the index of the file
parser.Seek(int64(pakFooter.IndexOffset), 0)
parser.Preload(int32(pakFooter.IndexSize))
mountPoint := parser.ReadString()
recordCount := parser.ReadInt32()
pakIndex := &FPakIndex{
MountPoint: mountPoint,
Records: make([]*FPakEntry, recordCount),
}
if pakFooter.Version >= 10 {
parser.DecodePakEntries(pakIndex, pakFooter)
} else {
parser.DecodeLegacyPakEntries(pakIndex, pakFooter)
}
return &PakFile{
Footer: pakFooter,
Index: pakIndex,
}
}
func (parser *PakParser) DecodePakEntries(pakIndex *FPakIndex, pakFooter *FPakInfo) {
parser.ReadUint64() // PathHashSeed
bReaderHasPathHashIndex := parser.ReadInt32() == 1
//PathHashIndexOffset := INDEX_NONE
//PathHashIndexSize := int64(0)
//var PathHashIndexHash []byte
if bReaderHasPathHashIndex {
parser.ReadInt64() // PathHashIndexOffset
parser.ReadInt64() // PathHashIndexSize
parser.Read(20) // PathHashIndexHash
}
bReaderHasFullDirectoryIndex := parser.ReadInt32() == 1
FullDirectoryIndexOffset := INDEX_NONE
FullDirectoryIndexSize := int64(0)
//var FullDirectoryIndexHash []byte
if bReaderHasFullDirectoryIndex {
FullDirectoryIndexOffset = parser.ReadInt64()
FullDirectoryIndexSize = parser.ReadInt64()
parser.Read(20) // FullDirectoryIndexHash
}
parser.ReadInt32() // EncodedPakEntryLength
encodedIndex := make(map[int32]*FPakEntry)
tracker := parser.TrackRead()
for i := 0; i < len(pakIndex.Records); i++ {
position := tracker.bytesRead
entry := &FPakEntry{}
// Grab the big bitfield value:
// Bit 31 = Offset 32-bit safe?
// Bit 30 = Uncompressed size 32-bit safe?
// Bit 29 = Size 32-bit safe?
// Bits 28-23 = Compression method
// Bit 22 = Encrypted
// Bits 21-6 = Compression blocks count
// Bits 5-0 = Compression block size
value := parser.ReadUint32()
entry.CompressionMethod = value >> 23 & 0x3f
bIsOffset32BitSafe := (value & (1 << 31)) != 0
if bIsOffset32BitSafe {
entry.FileOffset = int64(parser.ReadUint32())
} else {
entry.FileOffset = parser.ReadInt64()
}
bIsUncompressedSize32BitSafe := (value & (1 << 30)) != 0
if bIsUncompressedSize32BitSafe {
entry.UncompressedSize = int64(parser.ReadUint32())
} else {
entry.UncompressedSize = parser.ReadInt64()
}
if entry.CompressionMethod != 0 {
bIsSize32BitSafe := (value & (1 << 29)) != 0
if bIsSize32BitSafe {
entry.FileSize = int64(parser.ReadUint32())
} else {
entry.FileSize = parser.ReadInt64()
}
} else {
entry.FileSize = entry.UncompressedSize
}
entry.IsEncrypted = (value & (1 << 22)) != 0
CompressionBlocksCount := (value >> 6) & 0xffff
entry.CompressionBlocks = make([]*FPakCompressedBlock, CompressionBlocksCount)
entry.CompressionBlockSize = 0
if CompressionBlocksCount > 0 {
if entry.UncompressedSize < 65536 {
entry.CompressionBlockSize = uint32(entry.UncompressedSize)
} else {
entry.CompressionBlockSize = (value & 0x3f) << 11
}
}
// TODO
//baseOffset := entry.FileOffset
//if pakFooter.Version >= 5 {
// baseOffset = 0
//}
if len(entry.CompressionBlocks) == 1 && !entry.IsEncrypted {
panic("TODO") // TODO
} else if len(entry.CompressionBlocks) > 0 {
panic("TODO") // TODO
}
encodedIndex[position] = entry
pakIndex.Records[i] = entry
}
parser.UnTrackRead()
FilesNum := parser.ReadInt32()
if FilesNum > 0 {
pakIndex.Records = make([]*FPakEntry, FilesNum)
for i := int32(0); i < FilesNum; i++ {
parser.DecodeFPakEntry(pakIndex.Records[i], pakFooter.Version)
}
}
//bWillUseFullDirectoryIndex := false
//bWillUsePathHashIndex := false
bReadFullDirectoryIndex := false
if bReaderHasPathHashIndex && bReaderHasFullDirectoryIndex {
//bWillUseFullDirectoryIndex = IsPakKeepFullDirectory()
//bWillUsePathHashIndex = !bWillUseFullDirectoryIndex
bWantToReadFullDirectoryIndex := IsPakKeepFullDirectory()
bReadFullDirectoryIndex = bReaderHasFullDirectoryIndex && bWantToReadFullDirectoryIndex
} else if bReaderHasPathHashIndex {
//bWillUsePathHashIndex = true
//bWillUseFullDirectoryIndex = false
bReadFullDirectoryIndex = false
} else if bReaderHasFullDirectoryIndex {
//bWillUsePathHashIndex = false
//bWillUseFullDirectoryIndex = true
bReadFullDirectoryIndex = true
}
//bHasFullDirectoryIndex := false
FDirectoryIndex := make(map[string]map[string]FPakEntryLocation)
if !bReadFullDirectoryIndex {
// TODO
// PathHashIndexReader << DirectoryIndex
//bHasFullDirectoryIndex = false
panic("TODO") // TODO
} else {
parser.Seek(FullDirectoryIndexOffset, 0)
parser.Preload(int32(FullDirectoryIndexSize))
directoryCount := parser.ReadInt32()
for i := int32(0); i < directoryCount; i++ {
directoryName := parser.ReadString()
fileCount := parser.ReadInt32()
FPakDirectory := make(map[string]FPakEntryLocation, fileCount)
for j := int32(0); j < fileCount; j++ {
fileName := parser.ReadString()
location := FPakEntryLocation{
Index: parser.ReadInt32(),
}
if entry, ok := encodedIndex[location.Index]; ok {
// Strip null byte from end of directory name
entry.FileName = directoryName[:len(directoryName)-1] + fileName
}
FPakDirectory[fileName] = location
}
FDirectoryIndex[directoryName] = FPakDirectory
}
//bHasFullDirectoryIndex = true
}
}
func IsPakKeepFullDirectory() bool {
return true
}
func (parser *PakParser) DecodeLegacyPakEntries(pakIndex *FPakIndex, pakFooter *FPakInfo) {
for i := 0; i < len(pakIndex.Records); i++ {
entry := &FPakEntry{}
parser.DecodeFPakEntry(entry, pakFooter.Version)
pakIndex.Records[i] = entry
}
}
func (parser *PakParser) DecodeFPakEntry(entry *FPakEntry, version uint32) {
entry.FileName = parser.ReadString()
entry.FileOffset = parser.ReadInt64()
entry.FileSize = parser.ReadInt64()
entry.UncompressedSize = parser.ReadInt64()
if version <= 8 {
entry.CompressionMethod = uint32(parser.Read(1)[0])
} else {
entry.CompressionMethod = uint32(parser.ReadInt32())
}
if version <= 1 {
entry.Timestamp = parser.ReadUint64()
}
entry.DataSHA1Hash = parser.Read(20)
if version >= 3 {
if entry.CompressionMethod != 0 {
blockCount := parser.ReadUint32()
entry.CompressionBlocks = make([]*FPakCompressedBlock, blockCount)
for j := 0; j < len(entry.CompressionBlocks); j++ {
entry.CompressionBlocks[j] = &FPakCompressedBlock{
StartOffset: parser.ReadUint64(),
EndOffset: parser.ReadUint64(),
}
}
}
entry.IsEncrypted = parser.Read(1)[0] > 0
entry.CompressionBlockSize = parser.ReadUint32()
}
if version == 4 {
// TODO ???
}
if version >= 9 {
// TODO Unknown bytes
// parser.Read(3)
}
}