-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathReduceStream.cs
More file actions
291 lines (246 loc) · 7.38 KB
/
Copy pathReduceStream.cs
File metadata and controls
291 lines (246 loc) · 7.38 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
using System;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Reduce;
public partial class ReduceStream : Stream
{
private readonly long unCompressedSize;
private readonly long compressedSize;
private readonly Stream inStream;
private long inByteCount;
private const int EOF = 1234;
private readonly int factor;
private readonly int distanceMask;
private readonly int lengthMask;
private long outBytesCount;
private readonly byte[] windowsBuffer;
private int windowIndex;
private int length;
private int distance;
private ReduceStream(Stream inStr, long compsize, long unCompSize, int factor)
{
inStream = inStr;
compressedSize = compsize;
unCompressedSize = unCompSize;
inByteCount = 0;
outBytesCount = 0;
this.factor = factor;
distanceMask = (int)mask_bits[factor] << 8;
lengthMask = 0xff >> factor;
windowIndex = 0;
length = 0;
distance = 0;
windowsBuffer = new byte[WSIZE];
outByte = 0;
LoadBitLengthTable();
}
public static ReduceStream Create(Stream inStr, long compsize, long unCompSize, int factor)
{
var stream = new ReduceStream(inStr, compsize, unCompSize, factor);
stream.LoadNextByteTable();
return stream;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
public override void Flush()
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => unCompressedSize;
public override long Position
{
get => outBytesCount;
set { }
}
private const int RunLengthCode = 144;
private const int WSIZE = 0x4000;
private readonly uint[] mask_bits = new uint[]
{
0x0000,
0x0001,
0x0003,
0x0007,
0x000f,
0x001f,
0x003f,
0x007f,
0x00ff,
0x01ff,
0x03ff,
0x07ff,
0x0fff,
0x1fff,
0x3fff,
0x7fff,
0xffff,
};
private int bitBufferCount;
private ulong bitBuffer;
private bool _inputExhausted;
private int NEXTBYTE()
{
if (inByteCount == compressedSize)
{
_inputExhausted = true;
return EOF;
}
inByteCount++;
int b = inStream.ReadByte();
if (b < 0)
{
_inputExhausted = true;
return EOF;
}
return b;
}
private void READBITS(int nbits, out byte zdest)
{
if (nbits > bitBufferCount)
{
int temp;
while (bitBufferCount <= 8 * (int)(4 - 1) && (temp = NEXTBYTE()) != EOF)
{
bitBuffer |= (ulong)temp << bitBufferCount;
bitBufferCount += 8;
}
}
zdest = (byte)(bitBuffer & (ulong)mask_bits[nbits]);
bitBuffer >>= nbits;
bitBufferCount -= nbits;
}
private byte[] bitCountTable = [];
private void LoadBitLengthTable()
{
byte[] bitPos = { 0, 2, 4, 8, 16, 32, 64, 128, 255 };
bitCountTable = new byte[256];
for (byte i = 1; i <= 8; i++)
{
int vMin = bitPos[i - 1] + 1;
int vMax = bitPos[i];
for (int j = vMin; j <= vMax; j++)
{
bitCountTable[j] = i;
}
}
}
private byte[][] nextByteTable = [];
private void LoadNextByteTable()
{
nextByteTable = new byte[256][];
for (int x = 255; x >= 0; x--)
{
READBITS(6, out byte Slen);
nextByteTable[x] = new byte[Slen];
for (int i = 0; i < Slen; i++)
{
READBITS(8, out nextByteTable[x][i]);
}
}
}
private byte outByte;
private byte GetNextByte()
{
if (nextByteTable[outByte].Length == 0)
{
READBITS(8, out outByte);
return outByte;
}
READBITS(1, out byte nextBit);
if (nextBit == 1)
{
READBITS(8, out outByte);
return outByte;
}
READBITS(bitCountTable[nextByteTable[outByte].Length], out byte nextByteIndex);
if (nextByteIndex >= nextByteTable[outByte].Length)
{
throw new InvalidFormatException("ReduceStream: next byte table index out of range");
}
outByte = nextByteTable[outByte][nextByteIndex];
return outByte;
}
public override int Read(byte[] buffer, int offset, int count)
{
int countIndex = 0;
while (countIndex < count && outBytesCount < unCompressedSize)
{
if (length == 0)
{
if (_inputExhausted && bitBufferCount <= 0)
{
throw new InvalidFormatException(
"ReduceStream: compressed data exhausted before uncompressed size reached"
);
}
byte nextByte = GetNextByte();
if (nextByte != RunLengthCode)
{
buffer[offset + (countIndex++)] = nextByte;
windowsBuffer[windowIndex++] = nextByte;
outBytesCount++;
if (windowIndex == WSIZE)
{
windowIndex = 0;
}
continue;
}
nextByte = GetNextByte();
if (nextByte == 0)
{
buffer[offset + (countIndex++)] = RunLengthCode;
windowsBuffer[windowIndex++] = RunLengthCode;
outBytesCount++;
if (windowIndex == WSIZE)
{
windowIndex = 0;
}
continue;
}
int lengthDistanceByte = nextByte;
length = lengthDistanceByte & lengthMask;
if (length == lengthMask)
{
length += GetNextByte();
}
length += 3;
int distanceHighByte = (lengthDistanceByte << factor) & distanceMask;
distance = windowIndex - (distanceHighByte + GetNextByte() + 1);
distance &= WSIZE - 1;
}
while (length != 0 && countIndex < count)
{
byte nextByte = windowsBuffer[distance++];
buffer[offset + (countIndex++)] = nextByte;
windowsBuffer[windowIndex++] = nextByte;
outBytesCount++;
if (distance == WSIZE)
{
distance = 0;
}
if (windowIndex == WSIZE)
{
windowIndex = 0;
}
length--;
}
}
return countIndex;
}
}