-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLZ4Stream.cs
560 lines (484 loc) · 20.8 KB
/
LZ4Stream.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#region license
/*
Copyright (c) 2013, Milosz Krajewski
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
#region Modification license
//
// Copyright 2014 Matthew Ducker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.IO;
namespace LZ4PCL
{
public enum CompressionMode
{
Compress,
Decompress
}
/// <summary>Block compression stream. Allows to use LZ4 for stream compression.</summary>
public class LZ4Stream : Stream
{
#region ChunkFlags
/// <summary>
/// Flags of a chunk. Please note, this
/// </summary>
[Flags]
public enum ChunkFlags
{
/// <summary>None.</summary>
None = 0x00,
/// <summary>Set if chunk is compressed.</summary>
Compressed = 0x01,
/// <summary>
/// Set if high compression has been selected (does not affect decoder,
/// but might be useful when rewriting)
/// </summary>
HighCompression = 0x02,
/// <summary>
/// 3 bits for number of passes. Currently only 1 pass (value 0)
/// is supported.
/// </summary>
Passes = 0x04 | 0x08 | 0x10, // not used currently
}
#endregion
#region fields
/// <summary>The block size (compression only).</summary>
private readonly int _blockSize;
/// <summary>The compression mode.</summary>
private readonly CompressionMode _compressionMode;
/// <summary>Whether to close the inner stream when closing/disposing.</summary>
private readonly bool _closeOnDispose;
/// <summary>The high compression flag (compression only).</summary>
private readonly bool _highCompression;
/// <summary>The inner stream.</summary>
private readonly Stream _innerStream;
/// <summary>The buffer.</summary>
private byte[] _buffer;
/// <summary>The buffer length (can be different than _buffer.Length).</summary>
private int _bufferLength;
/// <summary>The offset in a buffer.</summary>
private int _bufferOffset;
#endregion
#region constructor
/// <summary>Initializes a new instance of the <see cref="LZ4Stream" /> class.</summary>
/// <param name="innerStream">The inner stream.</param>
/// <param name="compressionMode">The compression mode.</param>
/// <param name="closeOnDispose">Whether to close <paramref name="innerStream"/> when closing/disposing.</param>
/// <param name="highCompression">if set to <c>true</c> [high compression].</param>
/// <param name="blockSize">Size of the block.</param>
public LZ4Stream(
Stream innerStream,
CompressionMode compressionMode,
bool closeOnDispose = true,
bool highCompression = false,
int blockSize = 1024 * 1024)
{
_innerStream = innerStream;
_compressionMode = compressionMode;
_closeOnDispose = closeOnDispose;
_highCompression = highCompression;
_blockSize = Math.Max(16, blockSize);
}
#endregion
#region utilities
/// <summary>Returns NotSupportedException.</summary>
/// <param name="operationName">Name of the operation.</param>
/// <returns>NotSupportedException</returns>
private static NotSupportedException NotSupported(string operationName)
{
return new NotSupportedException(
string.Format(
"Operation '{0}' is not supported", operationName));
}
/// <summary>Returns EndOfStreamException.</summary>
/// <returns>EndOfStreamException</returns>
private static EndOfStreamException EndOfStream()
{
return new EndOfStreamException("Unexpected end of stream");
}
/// <summary>Tries to read variable length int.</summary>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if integer has been read, <c>false</c> if end of stream has been
/// encountered. If end of stream has been encoutered in the middle of value
/// <see cref="EndOfStreamException" /> is thrown.
/// </returns>
private bool TryReadVarInt(out ulong result)
{
var buffer = new byte[1];
int count = 0;
result = 0;
while (true) {
if (_innerStream.Read(buffer, 0, 1) == 0) {
if (count == 0) {
return false;
}
throw EndOfStream();
}
byte b = buffer[0];
result = result + ((ulong) (b & 0x7F) << count);
count += 7;
if ((b & 0x80) == 0 || count >= 64) {
break;
}
}
return true;
}
/// <summary>
/// Reads the variable length int. Work with assumption that value is in the stream
/// and throws exception if it isn't. If you want to check if value is in the stream
/// use <see cref="TryReadVarInt" /> instead.
/// </summary>
/// <returns></returns>
private ulong ReadVarInt()
{
ulong result;
if (!TryReadVarInt(out result)) {
throw EndOfStream();
}
return result;
}
/// <summary>
/// Reads the block of bytes.
/// Contrary to <see cref="Stream.Read" /> does not read partial data if possible.
/// If there is no data (yet) it waits.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>Number of bytes read.</returns>
private int ReadBlock(byte[] buffer, int offset, int length)
{
int total = 0;
while (length > 0) {
int read = _innerStream.Read(buffer, offset, length);
if (read == 0) {
break;
}
offset += read;
length -= read;
total += read;
}
return total;
}
/// <summary>Writes the variable length integer.</summary>
/// <param name="value">The value.</param>
private void WriteVarInt(ulong value)
{
var buffer = new byte[1];
while (true) {
var b = (byte) (value & 0x7F);
value >>= 7;
buffer[0] = (byte) (b | (value == 0 ? 0 : 0x80));
_innerStream.Write(buffer, 0, 1);
if (value == 0) {
break;
}
}
}
/// <summary>Flushes current chunk.</summary>
private void FlushCurrentChunk()
{
if (_bufferOffset <= 0) {
return;
}
var compressed = new byte[_bufferOffset];
int compressedLength = LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset,
_highCompression);
if (compressedLength <= 0 || compressedLength >= _bufferOffset) {
// incompressible block
compressed = _buffer;
compressedLength = _bufferOffset;
}
bool isCompressed = compressedLength < _bufferOffset;
var flags = ChunkFlags.None;
if (isCompressed) {
flags |= ChunkFlags.Compressed;
}
if (_highCompression) {
flags |= ChunkFlags.HighCompression;
}
WriteVarInt((ulong) flags);
WriteVarInt((ulong) _bufferOffset);
if (isCompressed) {
WriteVarInt((ulong) compressedLength);
}
_innerStream.Write(compressed, 0, compressedLength);
_bufferOffset = 0;
}
/// <summary>Reads the next chunk from stream.</summary>
/// <returns>
/// <c>true</c> if next has been read, or <c>false</c> if it is legitimate end of file.
/// Throws <see cref="EndOfStreamException" /> if end of stream was unexpected.
/// </returns>
private bool AcquireNextChunk()
{
do {
ulong varint;
if (!TryReadVarInt(out varint)) {
return false;
}
var flags = (ChunkFlags) varint;
bool isCompressed = (flags & ChunkFlags.Compressed) != 0;
var originalLength = (int) ReadVarInt();
int compressedLength = isCompressed ? (int) ReadVarInt() : originalLength;
if (compressedLength > originalLength) {
throw EndOfStream(); // corrupted
}
var compressed = new byte[compressedLength];
int chunk = ReadBlock(compressed, 0, compressedLength);
if (chunk != compressedLength) {
throw EndOfStream(); // corrupted
}
if (!isCompressed) {
_buffer = compressed; // no compression on this chunk
_bufferLength = compressedLength;
} else {
if (_buffer == null || _buffer.Length < originalLength) {
_buffer = new byte[originalLength];
}
int passes = (int) flags >> 2;
if (passes != 0) {
throw new NotSupportedException("Chunks with multiple passes are not supported.");
}
LZ4Codec.Decode(compressed, 0, compressedLength, _buffer, 0, originalLength, true);
_bufferLength = originalLength;
}
_bufferOffset = 0;
} while (_bufferLength == 0); // skip empty block (shouldn't happen but...)
return true;
}
#endregion
#region overrides
/// <summary>When overridden in a derived class, gets a value indicating whether the current stream supports reading.</summary>
/// <returns>true if the stream supports reading; otherwise, false.</returns>
public override bool CanRead
{
get { return _compressionMode == CompressionMode.Decompress; }
}
/// <summary>When overridden in a derived class, gets a value indicating whether the current stream supports seeking.</summary>
/// <returns>true if the stream supports seeking; otherwise, false.</returns>
public override bool CanSeek
{
get { return false; }
}
/// <summary>When overridden in a derived class, gets a value indicating whether the current stream supports writing.</summary>
/// <returns>true if the stream supports writing; otherwise, false.</returns>
public override bool CanWrite
{
get { return _compressionMode == CompressionMode.Compress; }
}
/// <summary>When overridden in a derived class, gets the length in bytes of the stream.</summary>
/// <returns>A long value representing the length of the stream in bytes.</returns>
public override long Length
{
get { return -1; }
}
/// <summary>When overridden in a derived class, gets or sets the position within the current stream.</summary>
/// <returns>The current position within the stream.</returns>
public override long Position
{
get { return -1; }
set { throw NotSupported("SetPosition"); }
}
/// <summary>
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be
/// written to the underlying device.
/// </summary>
public override void Flush()
{
if (_bufferOffset > 0 && CanWrite) {
FlushCurrentChunk();
}
}
/// <summary>
/// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the
/// end of the stream.
/// </summary>
/// <returns>The unsigned byte cast to an Int32, or -1 if at the end of the stream.</returns>
public override int ReadByte()
{
if (!CanRead) {
throw NotSupported("Read");
}
if (_bufferOffset >= _bufferLength && !AcquireNextChunk()) {
return -1; // that's just end of stream
}
return _buffer[_bufferOffset++];
}
/// <summary>
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the
/// position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the
/// values between <paramref name="offset" /> and (<paramref name="offset" /> + <paramref name="count" /> - 1) replaced
/// by the bytes read from the current source.
/// </param>
/// <param name="offset">
/// The zero-based byte offset in <paramref name="buffer" /> at which to begin storing the data read
/// from the current stream.
/// </param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that
/// many bytes are not currently available, or zero (0) if the end of the stream has been reached.
/// </returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (!CanRead) {
throw NotSupported("Read");
}
int total = 0;
while (count > 0) {
int chunk = Math.Min(count, _bufferLength - _bufferOffset);
if (chunk > 0) {
#if INCLUDE_UNSAFE
unsafe {
fixed (byte* srcPtr = _buffer) {
fixed (byte* dstPtr = buffer) {
LZ4Codec.BlockCopy(srcPtr + _bufferOffset, dstPtr + offset, chunk);
}
}
}
#else
Buffer.BlockCopy(_buffer, _bufferOffset, buffer, offset, chunk);
#endif
_bufferOffset += chunk;
offset += chunk;
count -= chunk;
total += chunk;
} else {
if (!AcquireNextChunk()) {
break;
}
}
}
return total;
}
/// <summary>When overridden in a derived class, sets the position within the current stream.</summary>
/// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
/// <param name="origin">
/// A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to
/// obtain the new position.
/// </param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
throw NotSupported("Seek");
}
/// <summary>When overridden in a derived class, sets the length of the current stream.</summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
throw NotSupported("SetLength");
}
/// <summary>Writes a byte to the current position in the stream and advances the position within the stream by one byte.</summary>
/// <param name="value">The byte to write to the stream.</param>
public override void WriteByte(byte value)
{
if (!CanWrite) {
throw NotSupported("Write");
}
if (_buffer == null) {
_buffer = new byte[_blockSize];
_bufferLength = _blockSize;
_bufferOffset = 0;
}
if (_bufferOffset >= _bufferLength) {
FlushCurrentChunk();
}
_buffer[_bufferOffset++] = value;
}
/// <summary>
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current
/// position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">
/// An array of bytes. This method copies <paramref name="count" /> bytes from
/// <paramref name="buffer" /> to the current stream.
/// </param>
/// <param name="offset">
/// The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the
/// current stream.
/// </param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (!CanWrite) {
throw NotSupported("Write");
}
if (_buffer == null) {
_buffer = new byte[_blockSize];
_bufferLength = _blockSize;
_bufferOffset = 0;
}
while (count > 0) {
int chunk = Math.Min(count, _bufferLength - _bufferOffset);
if (chunk > 0) {
#if INCLUDE_UNSAFE
unsafe {
fixed (byte* srcPtr = buffer) {
fixed (byte* dstPtr = _buffer) {
LZ4Codec.BlockCopy(srcPtr + offset, dstPtr + _bufferOffset, chunk);
}
}
}
#else
Buffer.BlockCopy(buffer, offset, _buffer, _bufferOffset, chunk);
#endif
offset += chunk;
count -= chunk;
_bufferOffset += chunk;
} else {
FlushCurrentChunk();
}
}
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> and optionally releases the
/// managed resources.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources; false to release only unmanaged
/// resources.
/// </param>
protected override void Dispose(bool disposing)
{
Flush();
if (_closeOnDispose) {
_innerStream.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}