25#import "OFDeflateStream.h"
26#ifdef OF_DEFLATE64_STREAM_M
27# import "OFDeflate64Stream.h"
28# define OFDeflateStream OFDeflate64Stream
30#import "OFHuffmanTree.h"
32#import "OFInitializationFailedException.h"
33#import "OFInvalidArgumentException.h"
34#import "OFInvalidFormatException.h"
35#import "OFNotImplementedException.h"
36#import "OFNotOpenException.h"
37#import "OFOutOfMemoryException.h"
41 stateUncompressedBlockHeader,
42 stateUncompressedBlock,
48 huffmanStateWriteValue,
49 huffmanStateAwaitCode,
50 huffmanStateAwaitLengthExtraBits,
51 huffmanStateAwaitDistance,
52 huffmanStateAwaitDistanceExtraBits,
53 huffmanStateProcessPair
56#ifndef OF_DEFLATE64_STREAM_M
57static const uint16_t slidingWindowMask = 0x7FFF;
58static const uint8_t numDistanceCodes = 30;
59static const uint8_t lengthCodes[29] = {
61 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
62 64, 80, 96, 112, 128, 160, 192, 224, 255
64static const uint8_t lengthExtraBits[29] = {
65 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
68static const uint16_t distanceCodes[30] = {
69 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
70 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
72static const uint8_t distanceExtraBits[30] = {
73 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
74 10, 11, 11, 12, 12, 13, 13
77static const uint16_t slidingWindowMask = 0xFFFF;
78static const uint8_t numDistanceCodes = 32;
79static const uint8_t lengthCodes[29] = {
81 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
82 64, 80, 96, 112, 128, 160, 192, 224, 0
84static const uint8_t lengthExtraBits[29] = {
85 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
88static const uint16_t distanceCodes[32] = {
89 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
90 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
93static const uint8_t distanceExtraBits[32] = {
94 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
95 10, 11, 11, 12, 12, 13, 13, 14, 14
98static const uint8_t codeLengthsOrder[19] = {
99 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
101static OFHuffmanTree fixedLitLenTree, fixedDistTree;
109 struct OFInflateContext *ctx = stream->_inflateCtx;
110 uint16_t ret = ctx->savedBits;
112 OFAssert(ctx->savedBitsLength < count);
114 for (uint_fast8_t i = ctx->savedBitsLength; i < count; i++) {
115 if OF_UNLIKELY (ctx->bitIndex == 8) {
116 if OF_LIKELY (ctx->bufferIndex < ctx->bufferLength)
117 ctx->
byte = ctx->buffer[ctx->bufferIndex++];
119 size_t length = [stream->_stream
121 length: OFInflateStreamBufferSize];
123 if OF_UNLIKELY (length < 1) {
124 ctx->savedBits = ret;
125 ctx->savedBitsLength = i;
129 ctx->byte = ctx->buffer[0];
130 ctx->bufferIndex = 1;
131 ctx->bufferLength = (uint16_t)length;
137 ret |= ((ctx->byte >> ctx->bitIndex++) & 1) << i;
141 ctx->savedBitsLength = 0;
149 uint8_t lengths[288];
151 if (
self != [OFDeflateStream
class])
154 for (uint16_t i = 0; i <= 143; i++)
156 for (uint16_t i = 144; i <= 255; i++)
158 for (uint16_t i = 256; i <= 279; i++)
160 for (uint16_t i = 280; i <= 287; i++)
163 fixedLitLenTree = _OFHuffmanTreeNew(lengths, 288);
165 for (uint16_t i = 0; i <= 31; i++)
168 fixedDistTree = _OFHuffmanTreeNew(lengths, 32);
171+ (instancetype)streamWithStream: (
OFStream *)stream
173 return objc_autoreleaseReturnValue(
174 [[
self alloc] initWithStream: stream]);
179 return objc_autoreleaseReturnValue(
180 [[
self alloc] initWithStream: stream
186 OF_INVALID_INIT_METHOD
199 _stream = objc_retain(stream);
202 if ([mode isEqual:
@"r"]) {
204 sizeof(*_inflateCtx));
207 _inflateCtx->bitIndex = 8;
208 }
else if ([mode isEqual:
@"w"])
229 if (_inflateCtx != NULL) {
230 if (_inflateCtx->state == stateHuffmanTree) {
234 _inflateCtx->ctx.huffmanTree.codeLenTree);
237 if (_inflateCtx->state == stateHuffmanTree ||
238 _inflateCtx->state == stateHuffmanBlock) {
239 if (_inflateCtx->ctx.huffman.litLenTree !=
242 _inflateCtx->ctx.huffman.litLenTree);
243 if (_inflateCtx->ctx.huffman.distTree != fixedDistTree)
245 _inflateCtx->ctx.huffman.distTree);
254- (size_t)lowlevelReadIntoBuffer: (
void *)buffer_
255 length: (
size_t)length
257 unsigned char *buffer = buffer_;
258 uint16_t bits = 0, tmp, value = 0;
259 size_t bytesWritten = 0;
260 unsigned char *slidingWindow;
261 uint16_t slidingWindowIndex;
262 struct OFInflateContext *ctx;
264 if (_stream ==
nil || _inflateCtx == NULL)
273 switch ((
enum State)ctx->state) {
274 case stateBlockHeader:
275 if OF_UNLIKELY (ctx->inLastBlock) {
276 [_stream unreadFromBuffer: ctx->buffer +
278 length: ctx->bufferLength -
280 ctx->bufferIndex = ctx->bufferLength = 0;
282 _atEndOfStream =
true;
286 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3))
289 ctx->inLastBlock = (bits & 1);
293 ctx->state = stateUncompressedBlockHeader;
295 ctx->ctx.uncompressedHeader.position = 0;
296 memset(ctx->ctx.uncompressedHeader.length, 0, 4);
299 ctx->state = stateHuffmanBlock;
300 ctx->ctx.huffman.state = huffmanStateAwaitCode;
301 ctx->ctx.huffman.litLenTree = fixedLitLenTree;
302 ctx->ctx.huffman.distTree = fixedDistTree;
303 ctx->ctx.huffman.treeIter = fixedLitLenTree;
306 ctx->state = stateHuffmanTree;
307 ctx->ctx.huffmanTree.lengths = NULL;
308 ctx->ctx.huffmanTree.receivedCount = 0;
309 ctx->ctx.huffmanTree.value = 0xFE;
310 ctx->ctx.huffmanTree.litLenCodesCount = 0xFF;
311 ctx->ctx.huffmanTree.distCodesCount = 0xFF;
312 ctx->ctx.huffmanTree.codeLenCodesCount = 0xFF;
319 case stateUncompressedBlockHeader:
320#define CTX ctx->ctx.uncompressedHeader
322 [_stream unreadFromBuffer: ctx->buffer + ctx->bufferIndex
323 length: ctx->bufferLength -
325 ctx->bufferIndex = ctx->bufferLength = 0;
327 CTX.position += [_stream
328 readIntoBuffer: CTX.length + CTX.position
329 length: 4 - CTX.position];
331 if OF_UNLIKELY (CTX.position < 4)
334 if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
335 (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
336 @throw [OFInvalidFormatException exception];
338 ctx->state = stateUncompressedBlock;
344 ctx->ctx.uncompressed.length =
345 CTX.length[0] | (CTX.length[1] << 8);
346 ctx->ctx.uncompressed.position = 0;
350 case stateUncompressedBlock:
351#define CTX ctx->ctx.uncompressed
352 if OF_UNLIKELY (length == 0)
355 tmp = (length < (
size_t)CTX.length - CTX.position
356 ? (uint16_t)length : CTX.length - CTX.position);
358 tmp = (uint16_t)[_stream readIntoBuffer: buffer + bytesWritten
361 if OF_UNLIKELY (tmp == 0)
364 slidingWindow = _slidingWindow;
365 slidingWindowIndex = _slidingWindowIndex;
366 for (uint_fast16_t i = 0; i < tmp; i++) {
367 slidingWindow[slidingWindowIndex] =
368 buffer[bytesWritten + i];
369 slidingWindowIndex = (slidingWindowIndex + 1) &
372 _slidingWindowIndex = slidingWindowIndex;
378 if OF_UNLIKELY (CTX.position == CTX.length)
379 ctx->state = stateBlockHeader;
383 case stateHuffmanTree:
384#define CTX ctx->ctx.huffmanTree
385 if OF_LIKELY (CTX.value == 0xFE) {
386 if OF_LIKELY (CTX.litLenCodesCount == 0xFF) {
387 if OF_UNLIKELY (!tryReadBits(
self, &bits, 5))
390 if OF_UNLIKELY (bits > 29)
391 @throw [OFInvalidFormatException
394 CTX.litLenCodesCount = bits;
397 if OF_LIKELY (CTX.distCodesCount == 0xFF) {
398 if OF_UNLIKELY (!tryReadBits(
self, &bits, 5))
401 CTX.distCodesCount = bits;
404 if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) {
405 if OF_UNLIKELY (!tryReadBits(
self, &bits, 4))
408 CTX.codeLenCodesCount = bits;
411 if OF_LIKELY (CTX.lengths == NULL)
414 for (uint16_t i = CTX.receivedCount;
415 i < CTX.codeLenCodesCount + 4; i++) {
416 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3)) {
417 CTX.receivedCount = i;
421 CTX.lengths[codeLengthsOrder[i]] = bits;
424 CTX.codeLenTree = _OFHuffmanTreeNew(CTX.lengths, 19);
425 CTX.treeIter = CTX.codeLenTree;
429 CTX.receivedCount = 0;
433 if OF_LIKELY (CTX.lengths == NULL)
435 CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);
437 for (uint16_t i = CTX.receivedCount;
438 i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
441 if OF_LIKELY (CTX.value == 0xFF) {
442 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self,
443 tryReadBits, &CTX.treeIter, &value)) {
444 CTX.receivedCount = i;
448 CTX.treeIter = CTX.codeLenTree;
451 CTX.lengths[i++] = value;
459 if OF_UNLIKELY (i < 1)
463 if OF_UNLIKELY (!tryReadBits(
self, &bits, 2)) {
464 CTX.receivedCount = i;
469 value = CTX.lengths[i - 1];
474 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3)) {
475 CTX.receivedCount = i;
485 if OF_UNLIKELY (!tryReadBits(
self, &bits, 7)) {
486 CTX.receivedCount = i;
499 if OF_UNLIKELY (i + count >
500 CTX.litLenCodesCount + CTX.distCodesCount + 258)
503 for (j = 0; j < count; j++)
504 CTX.lengths[i++] = value;
509 _OFHuffmanTreeFree(CTX.codeLenTree);
510 CTX.codeLenTree = NULL;
512 CTX.litLenTree = _OFHuffmanTreeNew(CTX.lengths,
513 CTX.litLenCodesCount + 257);
514 CTX.distTree = _OFHuffmanTreeNew(
515 CTX.lengths + CTX.litLenCodesCount + 257,
516 CTX.distCodesCount + 1);
525 ctx->state = stateHuffmanBlock;
526 ctx->ctx.huffman.state = huffmanStateAwaitCode;
527 ctx->ctx.huffman.treeIter = CTX.litLenTree;
531 case stateHuffmanBlock:
532#define CTX ctx->ctx.huffman
534 uint8_t extraBits, lengthCodeIndex;
536 if OF_UNLIKELY (CTX.state == huffmanStateWriteValue) {
537 if OF_UNLIKELY (length == 0)
540 buffer[bytesWritten++] = CTX.value;
543 _slidingWindow[_slidingWindowIndex] = CTX.value;
544 _slidingWindowIndex =
545 (_slidingWindowIndex + 1) &
548 CTX.state = huffmanStateAwaitCode;
549 CTX.treeIter = CTX.litLenTree;
552 if OF_UNLIKELY (CTX.state ==
553 huffmanStateAwaitLengthExtraBits) {
554 if OF_UNLIKELY (!tryReadBits(
self, &bits,
560 CTX.state = huffmanStateAwaitDistance;
561 CTX.treeIter = CTX.distTree;
565 if (CTX.state == huffmanStateAwaitDistance) {
566 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self,
567 tryReadBits, &CTX.treeIter, &value))
570 if OF_UNLIKELY (value >= numDistanceCodes)
574 CTX.distance = distanceCodes[value];
575 extraBits = distanceExtraBits[value];
578 if OF_UNLIKELY (!tryReadBits(
self,
580#define HSADEB huffmanStateAwaitDistanceExtraBits
583 CTX.extraBits = extraBits;
587 CTX.distance += bits;
590 CTX.state = huffmanStateProcessPair;
591 }
else if (CTX.state ==
592 huffmanStateAwaitDistanceExtraBits) {
593 if OF_UNLIKELY (!tryReadBits(
self, &bits,
597 CTX.distance += bits;
599 CTX.state = huffmanStateProcessPair;
603 if (CTX.state == huffmanStateProcessPair) {
604 for (uint_fast16_t j = 0; j < CTX.length; j++) {
607 if OF_UNLIKELY (length == 0) {
612 idx = (_slidingWindowIndex -
613 CTX.distance) & slidingWindowMask;
614 value = _slidingWindow[idx];
616 buffer[bytesWritten++] = value;
619 _slidingWindow[_slidingWindowIndex] =
621 _slidingWindowIndex =
622 (_slidingWindowIndex + 1) &
626 CTX.state = huffmanStateAwaitCode;
627 CTX.treeIter = CTX.litLenTree;
630 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self, tryReadBits,
631 &CTX.treeIter, &value))
635 if OF_UNLIKELY (value == 256) {
636 if (CTX.litLenTree != fixedLitLenTree) {
637 _OFHuffmanTreeFree(CTX.litLenTree);
638 CTX.litLenTree = NULL;
640 if (CTX.distTree != fixedDistTree) {
641 _OFHuffmanTreeFree(CTX.distTree);
645 ctx->state = stateBlockHeader;
650 if OF_LIKELY (value < 256) {
651 if OF_UNLIKELY (length == 0) {
652 CTX.state = huffmanStateWriteValue;
657 buffer[bytesWritten++] = value;
660 _slidingWindow[_slidingWindowIndex] = value;
661 _slidingWindowIndex =
662 (_slidingWindowIndex + 1) &
665 CTX.treeIter = CTX.litLenTree;
669 if OF_UNLIKELY (value > 285)
673 lengthCodeIndex = value - 257;
674 CTX.length = lengthCodes[lengthCodeIndex] + 3;
675 extraBits = lengthExtraBits[lengthCodeIndex];
678 if OF_UNLIKELY (!tryReadBits(
self, &bits,
680 CTX.extraBits = extraBits;
682 huffmanStateAwaitLengthExtraBits;
689 CTX.treeIter = CTX.distTree;
690 CTX.state = huffmanStateAwaitDistance;
700- (bool)lowlevelIsAtEndOfStream
705 return _atEndOfStream;
708- (int)fileDescriptorForReading
710 return ((id <OFReadyForReadingObserving>)_stream)
711 .fileDescriptorForReading;
714- (bool)lowlevelHasDataInReadBuffer
716 return (_stream.hasDataInReadBuffer || (_inflateCtx != NULL &&
717 _inflateCtx->bufferLength - _inflateCtx->bufferIndex > 0));
725 if (_inflateCtx != NULL) {
727 [_stream unreadFromBuffer: _inflateCtx->buffer +
728 _inflateCtx->bufferIndex
729 length: _inflateCtx->bufferLength -
730 _inflateCtx->bufferIndex];
731 _inflateCtx->bufferIndex = _inflateCtx->bufferLength = 0;
734 objc_release(_stream);
void * OFAllocZeroedMemory(size_t count, size_t size) OF_MALLOC_FUNC
Allocates memory for the specified number of items of the specified size and initializes it with zero...
Definition OFObject.m:119
void OFFreeMemory(void *pointer)
Frees memory allocated by OFAllocMemory, OFAllocZeroedMemory or OFResizeMemory.
Definition OFObject.m:156
void * OFAllocMemory(size_t count, size_t size) OF_MALLOC_FUNC
Allocates memory for the specified number of items of the specified size.
Definition OFObject.m:101
#define nil
A value representing no object.
Definition ObjFWRT.h:68
A class that handles Deflate decompression transparently for an underlying stream.
Definition OFDeflateStream.h:38
OFStream * underlyingStream
The underlying stream of the deflate stream.
Definition OFDeflateStream.h:89
instancetype initWithStream:mode:(OFStream *stream,[mode] OFString *mode)
Initializes an already allocated OFDeflateStream with the specified underlying stream.
Definition OFDeflateStream.m:194
instancetype exception()
Creates a new, autoreleased exception.
Definition OFException.m:283
An exception indicating that the argument is invalid for this method.
Definition OFInvalidArgumentException.h:30
An exception indicating that a method or part of it is not implemented.
Definition OFNotImplementedException.h:31
instancetype exceptionWithSelector:object:(SEL selector,[object] nullable id object)
Creates a new, autoreleased not implemented exception.
Definition OFNotImplementedException.m:33
An exception indicating an object is not open, connected or bound.
Definition OFNotOpenException.h:30
instancetype exceptionWithObject:(id object)
Creates a new, autoreleased not open exception.
Definition OFNotOpenException.m:33
instancetype init()
Initializes an already allocated object.
Definition OFObject.m:674
instancetype alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object.
Definition OFObject.m:518
A base class for different types of streams.
Definition OFStream.h:280
size_t readIntoBuffer:length:(void *buffer,[length] size_t length)
Reads at most length bytes from the stream into a buffer.
Definition OFStream.m:135
A class for handling strings.
Definition OFString.h:144