|
8 | 8 |
|
9 | 9 | using ICSharpCode.SharpZipLib.Zip.Compression;
|
10 | 10 |
|
11 |
| -namespace PDTools.Compression |
12 |
| -{ |
13 |
| - public class PDIZIP |
14 |
| - { |
15 |
| - public const uint PDIZIP_MAGIC = 0xFF_F7_F3_2F; |
| 11 | +namespace PDTools.Compression; |
16 | 12 |
|
17 |
| - public static bool Inflate(Stream input, Stream output, bool skipMagic = false) |
18 |
| - { |
19 |
| - if (!skipMagic && input.ReadUInt32() == PDIZIP_MAGIC) |
20 |
| - return false; |
| 13 | +public class PDIZIP |
| 14 | +{ |
| 15 | + public const uint PDIZIP_MAGIC = 0xFF_F7_F3_2F; |
21 | 16 |
|
22 |
| - // Read PDIZIP Header |
23 |
| - uint expand_size = input.ReadUInt32(); |
24 |
| - uint compressed_size = input.ReadUInt32(); |
25 |
| - uint fragment_size = input.ReadUInt32(); |
26 |
| - int method = input.ReadInt32(); |
27 |
| - input.ReadInt32s(3); |
| 17 | + public static bool Inflate(Stream input, Stream output, bool skipMagic = false) |
| 18 | + { |
| 19 | + if (!skipMagic && input.ReadUInt32() == PDIZIP_MAGIC) |
| 20 | + return false; |
28 | 21 |
|
29 |
| - // Calculate the fragment (or chunk) count for the file |
30 |
| - uint fragmentCount = (compressed_size + fragment_size - 1) / fragment_size; |
| 22 | + // Read PDIZIP Header |
| 23 | + uint expand_size = input.ReadUInt32(); |
| 24 | + uint compressed_size = input.ReadUInt32(); |
| 25 | + uint fragment_size = input.ReadUInt32(); |
| 26 | + int method = input.ReadInt32(); |
| 27 | + input.ReadInt32s(3); |
31 | 28 |
|
32 |
| - // Set up inflater and buffers |
33 |
| - Inflater inflater = new Inflater(true); |
34 |
| - byte[] fragment_buffer = ArrayPool<byte>.Shared.Rent((int)fragment_size); |
35 |
| - byte[] expand_buffer = ArrayPool<byte>.Shared.Rent(0x20000); |
| 29 | + // Calculate the fragment (or chunk) count for the file |
| 30 | + uint fragmentCount = (compressed_size + fragment_size - 1) / fragment_size; |
36 | 31 |
|
37 |
| - for (uint i = 0; i < fragmentCount; i++) |
38 |
| - { |
39 |
| - if (input.ReadUInt32() != PDIZIP_MAGIC) |
40 |
| - throw new Exception("PDIZip Fragment magic did not match expected magic."); |
| 32 | + // Set up inflater and buffers |
| 33 | + Inflater inflater = new Inflater(true); |
| 34 | + byte[] fragment_buffer = ArrayPool<byte>.Shared.Rent((int)fragment_size); |
| 35 | + byte[] expand_buffer = ArrayPool<byte>.Shared.Rent(0x20000); |
41 | 36 |
|
42 |
| - // Read Fragment header |
43 |
| - uint fragmentExpandSize = input.ReadUInt32(); |
44 |
| - uint fragmentCompressedSize = input.ReadUInt32(); |
45 |
| - uint Unk = input.ReadUInt32(); |
46 |
| - |
47 |
| - // Only way to do it properly apparently, inflate streams read too much data |
48 |
| - input.Read(fragment_buffer, 0, (int)fragmentCompressedSize); |
49 |
| - inflater.SetInput(fragment_buffer, 0, (int)fragmentCompressedSize); |
| 37 | + for (uint i = 0; i < fragmentCount; i++) |
| 38 | + { |
| 39 | + if (input.ReadUInt32() != PDIZIP_MAGIC) |
| 40 | + throw new Exception("PDIZip Fragment magic did not match expected magic."); |
50 | 41 |
|
51 |
| - while (!inflater.IsFinished) |
52 |
| - { |
53 |
| - int inflated = inflater.Inflate(expand_buffer); |
54 |
| - output.Write(expand_buffer, 0, inflated); |
55 |
| - } |
| 42 | + // Read Fragment header |
| 43 | + uint fragmentExpandSize = input.ReadUInt32(); |
| 44 | + uint fragmentCompressedSize = input.ReadUInt32(); |
| 45 | + uint Unk = input.ReadUInt32(); |
56 | 46 |
|
57 |
| - // Crypto stream can't seek, so just manually move.. |
58 |
| - int left = (int)(fragment_size - fragmentCompressedSize) - 0x10; |
59 |
| - if (i == 0) |
60 |
| - left -= 0x20; |
61 |
| - while (left-- > 0) input.ReadByte(); |
| 47 | + // Only way to do it properly apparently, inflate streams read too much data |
| 48 | + input.ReadExactly(fragment_buffer, 0, (int)fragmentCompressedSize); |
| 49 | + inflater.SetInput(fragment_buffer, 0, (int)fragmentCompressedSize); |
62 | 50 |
|
63 |
| - inflater.Reset(); // Prepare for next chunk |
| 51 | + while (!inflater.IsFinished) |
| 52 | + { |
| 53 | + int inflated = inflater.Inflate(expand_buffer); |
| 54 | + output.Write(expand_buffer, 0, inflated); |
64 | 55 | }
|
65 | 56 |
|
66 |
| - ArrayPool<byte>.Shared.Return(fragment_buffer); |
67 |
| - ArrayPool<byte>.Shared.Return(expand_buffer); |
| 57 | + // Crypto stream can't seek, so just manually move.. |
| 58 | + int left = (int)(fragment_size - fragmentCompressedSize) - 0x10; |
| 59 | + if (i == 0) |
| 60 | + left -= 0x20; |
| 61 | + while (left-- > 0) input.ReadByte(); |
68 | 62 |
|
69 |
| - return output.Length == expand_size; |
| 63 | + inflater.Reset(); // Prepare for next chunk |
70 | 64 | }
|
| 65 | + |
| 66 | + ArrayPool<byte>.Shared.Return(fragment_buffer); |
| 67 | + ArrayPool<byte>.Shared.Return(expand_buffer); |
| 68 | + |
| 69 | + return output.Length == expand_size; |
71 | 70 | }
|
72 | 71 | }
|
0 commit comments