-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPakExtractor.cs
More file actions
358 lines (310 loc) · 13.3 KB
/
PakExtractor.cs
File metadata and controls
358 lines (310 loc) · 13.3 KB
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using UAssetAPI;
namespace AstroModIntegrator
{
public enum CompressionMethod
{
NONE,
ZLIB,
GZIP,
CUSTOM // Also "Oodle"
}
[Flags]
public enum RecordFlags : uint
{
Flag_None = 0x00,
Flag_Encrypted = 0x01,
Flag_Deleted = 0x02
}
public enum PakVersion
{
PakFile_Version_Initial = 1,
PakFile_Version_NoTimestamps = 2,
PakFile_Version_CompressionEncryption = 3,
PakFile_Version_IndexEncryption = 4,
PakFile_Version_RelativeChunkOffsets = 5,
PakFile_Version_DeleteRecords = 6,
PakFile_Version_EncryptionKeyGuid = 7,
PakFile_Version_FNameBasedCompressionMethod = 8,
PakFile_Version_FrozenIndex = 9,
PakFile_Version_PathHashIndex = 10,
PakFile_Version_Fnv64BugFix = 11,
PakFile_Version_Last,
PakFile_Version_Invalid,
PakFile_Version_Latest = PakFile_Version_Last - 1
}
public struct Block
{
public long Start;
public long Size;
public Block(long start, long size)
{
Start = start;
Size = size;
}
}
public class Record
{
public string fileName;
public long offset;
public long fileSize;
public long sizeDecompressed;
public CompressionMethod compressionMethod;
public RecordFlags Flags;
public uint compressionBlockSize;
public List<Block> compressionBlocks;
public byte[] dataHash;
public void Read(BinaryReader reader, PakVersion fileVersion, bool includesHeader)
{
if (includesHeader) fileName = reader.ReadUString();
offset = reader.ReadInt64();
fileSize = reader.ReadInt64();
sizeDecompressed = reader.ReadInt64();
compressionMethod = (CompressionMethod)reader.ReadUInt32();
if (fileVersion <= PakVersion.PakFile_Version_Initial)
{
ulong timestamp = reader.ReadUInt64();
}
dataHash = reader.ReadBytes(20); // sha1 hash
if (fileVersion >= PakVersion.PakFile_Version_CompressionEncryption)
{
if (compressionMethod != CompressionMethod.NONE)
{
compressionBlocks = new List<Block>();
uint blockCount = reader.ReadUInt32();
for (int j = 0; j < blockCount; j++)
{
long startOffset = reader.ReadInt64();
long endOffset = reader.ReadInt64();
compressionBlocks.Add(new Block(startOffset, endOffset - startOffset));
}
}
Flags = (RecordFlags)reader.ReadByte();
compressionBlockSize = reader.ReadUInt32(); // max size of each block
}
}
public void Write(BinaryWriter writer, byte[] data, bool includesHeader, bool autoAdjustBlocks, List<Block> blockOffsets = null, byte[] compressedData = null) // fileVersion is 4
{
if (autoAdjustBlocks)
{
fileSize = compressedData.Length;
sizeDecompressed = data.Length;
compressionMethod = CompressionMethod.ZLIB;
dataHash = new SHA1Managed().ComputeHash(compressedData);
}
if (includesHeader) writer.WriteUString(fileName);
writer.Write(offset);
writer.Write(fileSize); // normal size
writer.Write(sizeDecompressed); // decompressed size
writer.Write((int)compressionMethod);
writer.Write(dataHash);
long blockOffsetWritingStart = 0;
if (autoAdjustBlocks)
{
writer.Write(blockOffsets.Count);
blockOffsetWritingStart = writer.BaseStream.Position;
foreach (Block b in blockOffsets)
{
writer.Write(b.Start);
writer.Write(b.Start + b.Size);
}
}
else
{
writer.Write(compressionBlocks.Count);
foreach (Block b in compressionBlocks)
{
writer.Write(b.Start);
writer.Write(b.Start + b.Size);
}
}
writer.Write((byte)0); // not encrypted
writer.Write((int)sizeDecompressed);
if (autoAdjustBlocks)
{
long endOffset = writer.BaseStream.Position;
writer.Seek((int)blockOffsetWritingStart, SeekOrigin.Begin);
compressionBlocks = new List<Block>();
for (int i = 0; i < blockOffsets.Count; i++)
{
long newStart = endOffset + blockOffsets[i].Start;
long newEnd = endOffset + blockOffsets[i].Start + blockOffsets[i].Size;
writer.Write(newStart);
writer.Write(newEnd);
compressionBlocks.Add(new Block(newStart, newEnd - newStart));
}
writer.Seek((int)endOffset, SeekOrigin.Begin);
}
}
public Record()
{
}
}
public class MalformattedFileException : FormatException
{
public MalformattedFileException(string exText) : base(exText) { }
}
public class InvalidFileTypeException : IOException
{
public InvalidFileTypeException(string txt) : base(txt)
{
}
}
public class PakExtractor
{
internal static uint UE4_PAK_MAGIC = 0x5A6F12E1;
private PakVersion fileVersion;
private BinaryReader reader;
public Dictionary<string, long> PathToOffset;
public PakExtractor(BinaryReader reader)
{
this.reader = reader;
BuildDict();
}
private void BuildDict()
{
PathToOffset = new Dictionary<string, long>();
reader.BaseStream.Seek(-44, SeekOrigin.End); // First we head straight to the footer
uint magic = reader.ReadUInt32();
if (magic != UE4_PAK_MAGIC) // Magic number
{
reader.BaseStream.Seek(-204, SeekOrigin.End);
magic = reader.ReadUInt32();
if (magic != UE4_PAK_MAGIC) throw new InvalidFileTypeException("Invalid file format, magic = " + magic);
}
fileVersion = (PakVersion)reader.ReadUInt32();
ulong indexOffset = reader.ReadUInt64();
ulong indexSize = reader.ReadUInt64();
// First we read the first file record to see if everything is OK
reader.BaseStream.Seek(0, SeekOrigin.Begin);
var firstRec = new Record();
firstRec.Read(reader, fileVersion, false);
if (firstRec.Flags.HasFlag(RecordFlags.Flag_Encrypted)) throw new NotImplementedException("Encryption is not supported");
// Start reading the proper index
reader.BaseStream.Seek((long)indexOffset, SeekOrigin.Begin);
string mountPoint = reader.ReadUString();
int recordCount = reader.ReadInt32();
for (int i = 0; i < recordCount; i++)
{
var rec = new Record();
rec.Read(reader, fileVersion, true);
PathToOffset.Add(rec.fileName, (long)rec.offset);
}
}
public IReadOnlyList<string> GetAllPaths()
{
return new List<string>(PathToOffset.Keys).AsReadOnly();
}
public bool HasPath(string searchPath)
{
return PathToOffset.ContainsKey(searchPath);
}
public byte[] ReadRaw(string searchPath, bool verifyChecksums = false)
{
if (!HasPath(searchPath)) return new byte[0];
long fullOffset = PathToOffset[searchPath];
return ReadRaw(fullOffset, verifyChecksums);
}
public byte[] ReadRaw(long fullOffset, bool verifyChecksums = false)
{
reader.BaseStream.Seek(fullOffset, SeekOrigin.Begin);
var rec2 = new Record();
rec2.Read(reader, fileVersion, false);
switch (rec2.compressionMethod)
{
case CompressionMethod.NONE:
return reader.ReadBytes((int)rec2.fileSize);
case CompressionMethod.ZLIB:
MemoryStream fullStream = new MemoryStream();
foreach (Block block in rec2.compressionBlocks)
{
long blockOffset = block.Start;
long blockSize = block.Size;
if (fileVersion >= PakVersion.PakFile_Version_RelativeChunkOffsets) // Relative offset
{
reader.BaseStream.Seek((long)blockOffset + fullOffset, SeekOrigin.Begin);
}
else // Absolute offset
{
reader.BaseStream.Seek((long)blockOffset, SeekOrigin.Begin);
}
byte[] thisRawBlockData = reader.ReadBytes((int)blockSize);
byte[] thisBlockData = new byte[thisRawBlockData.Length - 4];
byte[] blockRawChecksum = new byte[4];
Array.Copy(thisRawBlockData, 0, thisBlockData, 0, thisBlockData.Length);
Array.Copy(thisRawBlockData, thisRawBlockData.Length - blockRawChecksum.Length, blockRawChecksum, 0, blockRawChecksum.Length);
Array.Reverse(blockRawChecksum); // Read the hash in reverse, zlib checksums are stored as big endian
uint blockChecksum = BitConverter.ToUInt32(blockRawChecksum, 0);
var memStream = new MemoryStream(thisBlockData);
int CMF = memStream.ReadByte();
int CM = CMF & 15;
int CINFO = (CMF & 240) >> 4;
int FLG = memStream.ReadByte();
int FCHECK = FLG & 31;
bool FDICT = (FLG & 32) >> 5 == 1;
int FLEVEL = (FLG & 192) >> 6;
if (CM != 8 || CINFO > 7 || (CMF * 256 + FLG) % 31 != 0) throw new MalformattedFileException("Invalid zlib header: " + BitConverter.ToString(new byte[2] { (byte)CMF, (byte)FLG }));
if (FDICT) throw new NotImplementedException("Preset dictionary is not supported");
if (verifyChecksums)
{
var decompressedBlockStream = new MemoryStream((int)blockSize * 2);
decompressedBlockStream.Seek(0, SeekOrigin.Begin);
using (DeflateStream decompressionStream = new DeflateStream(memStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedBlockStream);
}
decompressedBlockStream.Seek(0, SeekOrigin.Begin);
fullStream.Seek(0, SeekOrigin.End);
decompressedBlockStream.CopyTo(fullStream);
decompressedBlockStream.Seek(0, SeekOrigin.Begin);
uint calculatedChecksum = PakBaker.Adler32(new BinaryReader(decompressedBlockStream));
if (calculatedChecksum != blockChecksum) throw new MalformattedFileException("Checksum check failed; compression block likely corrupted");
}
else
{
using (DeflateStream decompressionStream = new DeflateStream(memStream, CompressionMode.Decompress))
{
fullStream.Seek(0, SeekOrigin.End);
decompressionStream.CopyTo(fullStream);
}
}
}
return fullStream.ToArray();
default:
throw new NotImplementedException("Unimplemented compression method " + rec2.compressionMethod);
}
}
private static Metadata ParseMetadata(string data)
{
JObject jobj = JObject.Parse(data);
int schemaVersion = jobj.ContainsKey("schema_version") ? (int)jobj["schema_version"] : 1;
switch(schemaVersion)
{
case 1:
return JsonConvert.DeserializeObject<Metadata>(data);
default:
throw new NotImplementedException("Unimplemented schema version " + schemaVersion);
}
}
public Metadata ReadMetadata()
{
string data = Encoding.UTF8.GetString(ReadRaw("metadata.json"));
if (string.IsNullOrEmpty(data)) return null;
try
{
return ParseMetadata(data);
}
catch (JsonReaderException)
{
return null;
}
}
}
}