-
Notifications
You must be signed in to change notification settings - Fork 10
/
DecompressionStorage.cs
210 lines (187 loc) · 6.54 KB
/
DecompressionStorage.cs
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
using LibHac;
using LibHac.Fs;
using System;
using System.IO;
using System.IO.Compression;
using Zstandard.Net;
namespace nsZip
{
class DecompressionStorage : StorageBase
{
private int bs;
private IStorage[] compressedBlocks;
private int[] compressionAlgorithm;
private int amountOfBlocks;
private int lastBlockSize = -1;
private long length = 0;
private byte[] decompressBuff;
public DecompressionStorage(IFile inputFile)
{
var inputFileStream = inputFile.AsStream();
var nsZipMagic = new byte[] { 0x6e, 0x73, 0x5a, 0x69, 0x70 };
var nsZipMagicEncrypted = new byte[5];
inputFileStream.Read(nsZipMagicEncrypted, 0, 5);
var nsZipMagicRandomKey = new byte[5];
inputFileStream.Read(nsZipMagicRandomKey, 0, 5);
Util.XorArrays(nsZipMagicEncrypted, nsZipMagicRandomKey);
if (!Util.ArraysEqual(nsZipMagicEncrypted, nsZipMagic))
{
throw new InvalidDataException($"Invalid nsZip magic!\r\n");
}
var version = inputFileStream.ReadByte();
var type = inputFileStream.ReadByte();
var bsArray = new byte[5];
inputFileStream.Read(bsArray, 0, 5);
long bsReal = (bsArray[0] << 32)
+ (bsArray[1] << 24)
+ (bsArray[2] << 16)
+ (bsArray[3] << 8)
+ bsArray[4];
if (bsReal > int.MaxValue)
{
throw new NotImplementedException("Block sizes above 2 GB aren't supported yet!");
}
bs = (int)bsReal;
var amountOfBlocksArray = new byte[4];
inputFileStream.Read(amountOfBlocksArray, 0, 4);
amountOfBlocks = (amountOfBlocksArray[0] << 24)
+ (amountOfBlocksArray[1] << 16)
+ (amountOfBlocksArray[2] << 8)
+ amountOfBlocksArray[3];
var sizeOfSize = (int)Math.Ceiling(Math.Log(bs, 2) / 8);
var perBlockHeaderSize = sizeOfSize + 1;
decompressBuff = new byte[bs];
compressionAlgorithm = new int[amountOfBlocks];
var compressedBlockSize = new int[amountOfBlocks];
var compressedBlockOffset = new long[amountOfBlocks];
long currentOffset = 0;
for (var currentBlockID = 0; currentBlockID < amountOfBlocks; ++currentBlockID)
{
compressedBlockOffset[currentBlockID] = currentOffset;
compressionAlgorithm[currentBlockID] = inputFileStream.ReadByte();
compressedBlockSize[currentBlockID] = 0;
for (var j = 0; j < sizeOfSize; ++j)
{
compressedBlockSize[currentBlockID] += inputFileStream.ReadByte() << ((sizeOfSize - j - 1) * 8);
}
currentOffset += compressedBlockSize[currentBlockID];
}
compressedBlocks = new IStorage[amountOfBlocks];
var compressedData = new FileStorage(inputFile).Slice(inputFileStream.Position);
for(int i = 0; i < amountOfBlocks; ++i)
{
compressedBlocks[i] = compressedData.Slice(compressedBlockOffset[i], compressedBlockSize[i]);
}
// Cast to long is VERY important or files larger than 2 GB will have a negative size!
lastBlockSize = getSizeOfLastBlock();
length = ((long)(amountOfBlocks-1) * bs) + lastBlockSize;
Console.WriteLine($"length={length} lastBlockSize={lastBlockSize}");
}
private int getSizeOfLastBlock()
{
if (lastBlockSize > -1)
{
return lastBlockSize;
}
switch (compressionAlgorithm[amountOfBlocks - 1])
{
case 0:
var rawBS = (int)compressedBlocks[amountOfBlocks - 1].GetSize();
return rawBS; //DON'T return bs here as the last block will be smaller!
case 1:
using (var decompressionStream = new ZstandardStream(compressedBlocks[amountOfBlocks - 1].AsStream(), CompressionMode.Decompress))
using (var memoryStream = new MemoryStream())
{
decompressionStream.CopyTo(memoryStream);
return (int)memoryStream.Length;
}
default:
throw new NotImplementedException(
"The specified compression algorithm isn't implemented yet!");
}
}
protected override void ReadImpl(Span<byte> destination, long offset)
{
int size = destination.Length;
var startBlockID = (int)(offset / bs);
var initialRelativeOffset = (int)(offset % bs);
var relativeOffset = initialRelativeOffset;
var maxRelativeBlockID = (int)((relativeOffset + size - 1) / bs);
//Console.WriteLine($"ReadImpl: startBlockID={startBlockID} offset={offset} relativeOffset={relativeOffset} size={size}, maxRelativeBlockID={maxRelativeBlockID} totalSize={length}");
//Console.WriteLine(new System.Diagnostics.StackTrace());
for (int relativeBlockID = 0; relativeBlockID <= maxRelativeBlockID; ++relativeBlockID)
{
int currentBlockID = startBlockID + relativeBlockID;
int destinationOffset = 0;
if (relativeBlockID > 0)
{
destinationOffset = bs - initialRelativeOffset + bs * (relativeBlockID - 1);
}
int readSize = -1;
if (maxRelativeBlockID == 0)
{
readSize = size;
}
else if (relativeBlockID == 0)
{
readSize = bs - initialRelativeOffset;
}
else if (relativeBlockID == maxRelativeBlockID)
{
readSize = size + initialRelativeOffset - (bs * maxRelativeBlockID);
}
else
{
readSize = bs;
}
if (currentBlockID == amountOfBlocks - 1 && lastBlockSize < readSize)
{
readSize = lastBlockSize;
}
switch (compressionAlgorithm[currentBlockID])
{
case 0:
//Console.WriteLine("memcpy");
var rawBS = compressedBlocks[currentBlockID].GetSize();
//This safety check doesn't work for the last block and so must be excluded!
if (rawBS != bs && currentBlockID < amountOfBlocks - 1)
{
throw new FormatException("NSZ header seems to be corrupted!");
}
compressedBlocks[currentBlockID].Read(destination.Slice(destinationOffset, readSize), relativeOffset);
break;
case 1:
//Console.WriteLine("ZStandard");
var cachedBlock = DecompressBlock(compressedBlocks[currentBlockID]);
cachedBlock.Slice(relativeOffset, readSize).CopyTo(destination.Slice(destinationOffset));
//Console.Out.WriteLine(System.Text.Encoding.ASCII.GetString(destination.ToArray()));
break;
default:
throw new NotImplementedException(
"The specified compression algorithm isn't implemented yet!");
}
relativeOffset = 0;
}
}
protected override void WriteImpl(ReadOnlySpan<byte> source, long offset)
{
throw new NotImplementedException("DecompressionStorage is read only!");
}
public override void Flush()
{
}
private Span<byte> DecompressBlock(IStorage input)
{
// decompress
using (var decompressionStream = new ZstandardStream(input.AsStream(), CompressionMode.Decompress))
{
decompressionStream.Read(decompressBuff, 0, bs);
return new Span<byte>(decompressBuff);
}
}
public override long GetSize()
{
return length;
}
}
}