25#ifndef OF_INFLATE64_STREAM_M
26# import "OFInflateStream.h"
28# import "OFInflate64Stream.h"
29# define OFInflateStream OFInflate64Stream
31#import "OFHuffmanTree.h"
33#import "OFInitializationFailedException.h"
34#import "OFInvalidFormatException.h"
35#import "OFNotOpenException.h"
36#import "OFOutOfMemoryException.h"
38#ifndef OF_INFLATE64_STREAM_M
39# define bufferSize OFInflateStreamBufferSize
41# define bufferSize OFInflate64StreamBufferSize
46 stateUncompressedBlockHeader,
47 stateUncompressedBlock,
53 huffmanStateWriteValue,
54 huffmanStateAwaitCode,
55 huffmanStateAwaitLengthExtraBits,
56 huffmanStateAwaitDistance,
57 huffmanStateAwaitDistanceExtraBits,
58 huffmanStateProcessPair
61#ifndef OF_INFLATE64_STREAM_M
62static const uint8_t numDistanceCodes = 30;
63static const uint8_t lengthCodes[29] = {
65 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
66 64, 80, 96, 112, 128, 160, 192, 224, 255
68static const uint8_t lengthExtraBits[29] = {
69 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
72static const uint16_t distanceCodes[30] = {
73 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
74 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
76static const uint8_t distanceExtraBits[30] = {
77 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
78 10, 11, 11, 12, 12, 13, 13
81static const uint8_t numDistanceCodes = 32;
82static const uint8_t lengthCodes[29] = {
84 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
85 64, 80, 96, 112, 128, 160, 192, 224, 0
87static const uint8_t lengthExtraBits[29] = {
88 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
91static const uint16_t distanceCodes[32] = {
92 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385,
93 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
96static const uint8_t distanceExtraBits[32] = {
97 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
98 10, 11, 11, 12, 12, 13, 13, 14, 14
101static const uint8_t codeLengthsOrder[19] = {
102 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
104static OFHuffmanTree fixedLitLenTree, fixedDistTree;
112 uint16_t ret = stream->_savedBits;
114 OFAssert(stream->_savedBitsLength < count);
116 for (uint_fast8_t i = stream->_savedBitsLength; i < count; i++) {
117 if OF_UNLIKELY (stream->_bitIndex == 8) {
118 if OF_LIKELY (stream->_bufferIndex <
119 stream->_bufferLength)
121 stream->_buffer[stream->_bufferIndex++];
123 size_t length = [stream->_stream
127 if OF_UNLIKELY (length < 1) {
128 stream->_savedBits = ret;
129 stream->_savedBitsLength = i;
133 stream->_byte = stream->_buffer[0];
134 stream->_bufferIndex = 1;
135 stream->_bufferLength = (uint16_t)length;
138 stream->_bitIndex = 0;
141 ret |= ((stream->_byte >> stream->_bitIndex++) & 1) << i;
144 stream->_savedBits = 0;
145 stream->_savedBitsLength = 0;
153 uint8_t lengths[288];
158 for (uint16_t i = 0; i <= 143; i++)
160 for (uint16_t i = 144; i <= 255; i++)
162 for (uint16_t i = 256; i <= 279; i++)
164 for (uint16_t i = 280; i <= 287; i++)
167 fixedLitLenTree = _OFHuffmanTreeNew(lengths, 288);
169 for (uint16_t i = 0; i <= 31; i++)
172 fixedDistTree = _OFHuffmanTreeNew(lengths, 32);
175+ (instancetype)streamWithStream: (
OFStream *)stream
177 return [[[
self alloc] initWithStream: stream] autorelease];
182 OF_INVALID_INIT_METHOD
185- (instancetype)initWithStream: (
OFStream *)stream
190 _stream = [stream
retain];
195#ifdef OF_INFLATE64_STREAM_M
196 _slidingWindowMask = 0xFFFF;
198 _slidingWindowMask = 0x7FFF;
216 if (_state == stateHuffmanTree) {
219 if (_context.huffmanTree.codeLenTree != NULL)
220 _OFHuffmanTreeFree(_context.huffmanTree.codeLenTree);
223 if (_state == stateHuffmanTree || _state == stateHuffmanBlock) {
224 if (_context.huffman.litLenTree != fixedLitLenTree)
225 _OFHuffmanTreeFree(_context.huffman.litLenTree);
226 if (_context.huffman.distTree != fixedDistTree)
227 _OFHuffmanTreeFree(_context.huffman.distTree);
233- (size_t)lowlevelReadIntoBuffer: (
void *)buffer_
234 length: (
size_t)length
236 unsigned char *buffer = buffer_;
237 uint16_t bits = 0, tmp, value = 0;
238 size_t bytesWritten = 0;
239 unsigned char *slidingWindow;
240 uint16_t slidingWindowIndex;
249 switch ((
enum State)_state) {
250 case stateBlockHeader:
251 if OF_UNLIKELY (_inLastBlock) {
252 [_stream unreadFromBuffer: _buffer + _bufferIndex
253 length: _bufferLength -
255 _bufferIndex = _bufferLength = 0;
257 _atEndOfStream =
true;
261 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3))
264 _inLastBlock = (bits & 1);
268 _state = stateUncompressedBlockHeader;
270 _context.uncompressedHeader.position = 0;
271 memset(_context.uncompressedHeader.length, 0, 4);
274 _state = stateHuffmanBlock;
275 _context.huffman.state = huffmanStateAwaitCode;
276 _context.huffman.litLenTree = fixedLitLenTree;
277 _context.huffman.distTree = fixedDistTree;
278 _context.huffman.treeIter = fixedLitLenTree;
281 _state = stateHuffmanTree;
282 _context.huffmanTree.lengths = NULL;
283 _context.huffmanTree.receivedCount = 0;
284 _context.huffmanTree.value = 0xFE;
285 _context.huffmanTree.litLenCodesCount = 0xFF;
286 _context.huffmanTree.distCodesCount = 0xFF;
287 _context.huffmanTree.codeLenCodesCount = 0xFF;
294 case stateUncompressedBlockHeader:
295#define CTX _context.uncompressedHeader
297 [_stream unreadFromBuffer: _buffer + _bufferIndex
298 length: _bufferLength - _bufferIndex];
299 _bufferIndex = _bufferLength = 0;
301 CTX.position += [_stream
302 readIntoBuffer: CTX.length + CTX.position
303 length: 4 - CTX.position];
305 if OF_UNLIKELY (CTX.position < 4)
308 if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
309 (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
312 _state = stateUncompressedBlock;
318 _context.uncompressed.length =
319 CTX.length[0] | (CTX.length[1] << 8);
320 _context.uncompressed.position = 0;
324 case stateUncompressedBlock:
325#define CTX _context.uncompressed
326 if OF_UNLIKELY (length == 0)
329 tmp = (length < (
size_t)CTX.length - CTX.position
330 ? (uint16_t)length : CTX.length - CTX.position);
332 tmp = (uint16_t)[_stream readIntoBuffer: buffer + bytesWritten
335 if OF_UNLIKELY (tmp == 0)
338 slidingWindow = _slidingWindow;
339 slidingWindowIndex = _slidingWindowIndex;
340 for (uint_fast16_t i = 0; i < tmp; i++) {
341 slidingWindow[slidingWindowIndex] =
342 buffer[bytesWritten + i];
343 slidingWindowIndex = (slidingWindowIndex + 1) &
346 _slidingWindowIndex = slidingWindowIndex;
352 if OF_UNLIKELY (CTX.position == CTX.length)
353 _state = stateBlockHeader;
357 case stateHuffmanTree:
358#define CTX _context.huffmanTree
359 if OF_LIKELY (CTX.value == 0xFE) {
360 if OF_LIKELY (CTX.litLenCodesCount == 0xFF) {
361 if OF_UNLIKELY (!tryReadBits(
self, &bits, 5))
364 if OF_UNLIKELY (bits > 29)
368 CTX.litLenCodesCount = bits;
371 if OF_LIKELY (CTX.distCodesCount == 0xFF) {
372 if OF_UNLIKELY (!tryReadBits(
self, &bits, 5))
375 CTX.distCodesCount = bits;
378 if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) {
379 if OF_UNLIKELY (!tryReadBits(
self, &bits, 4))
382 CTX.codeLenCodesCount = bits;
385 if OF_LIKELY (CTX.lengths == NULL)
388 for (uint16_t i = CTX.receivedCount;
389 i < CTX.codeLenCodesCount + 4; i++) {
390 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3)) {
391 CTX.receivedCount = i;
395 CTX.lengths[codeLengthsOrder[i]] = bits;
398 CTX.codeLenTree = _OFHuffmanTreeNew(CTX.lengths, 19);
399 CTX.treeIter = CTX.codeLenTree;
403 CTX.receivedCount = 0;
407 if OF_LIKELY (CTX.lengths == NULL)
409 CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);
411 for (uint16_t i = CTX.receivedCount;
412 i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
415 if OF_LIKELY (CTX.value == 0xFF) {
416 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self,
417 tryReadBits, &CTX.treeIter, &value)) {
418 CTX.receivedCount = i;
422 CTX.treeIter = CTX.codeLenTree;
425 CTX.lengths[i++] = value;
433 if OF_UNLIKELY (i < 1)
437 if OF_UNLIKELY (!tryReadBits(
self, &bits, 2)) {
438 CTX.receivedCount = i;
443 value = CTX.lengths[i - 1];
448 if OF_UNLIKELY (!tryReadBits(
self, &bits, 3)) {
449 CTX.receivedCount = i;
459 if OF_UNLIKELY (!tryReadBits(
self, &bits, 7)) {
460 CTX.receivedCount = i;
473 if OF_UNLIKELY (i + count >
474 CTX.litLenCodesCount + CTX.distCodesCount + 258)
477 for (j = 0; j < count; j++)
478 CTX.lengths[i++] = value;
483 _OFHuffmanTreeFree(CTX.codeLenTree);
484 CTX.codeLenTree = NULL;
486 CTX.litLenTree = _OFHuffmanTreeNew(CTX.lengths,
487 CTX.litLenCodesCount + 257);
488 CTX.distTree = _OFHuffmanTreeNew(
489 CTX.lengths + CTX.litLenCodesCount + 257,
490 CTX.distCodesCount + 1);
499 _state = stateHuffmanBlock;
500 _context.huffman.state = huffmanStateAwaitCode;
501 _context.huffman.treeIter = CTX.litLenTree;
505 case stateHuffmanBlock:
506#define CTX _context.huffman
508 uint8_t extraBits, lengthCodeIndex;
510 if OF_UNLIKELY (CTX.state == huffmanStateWriteValue) {
511 if OF_UNLIKELY (length == 0)
514 buffer[bytesWritten++] = CTX.value;
517 _slidingWindow[_slidingWindowIndex] = CTX.value;
518 _slidingWindowIndex =
519 (_slidingWindowIndex + 1) &
522 CTX.state = huffmanStateAwaitCode;
523 CTX.treeIter = CTX.litLenTree;
526 if OF_UNLIKELY (CTX.state ==
527 huffmanStateAwaitLengthExtraBits) {
528 if OF_UNLIKELY (!tryReadBits(
self, &bits,
534 CTX.state = huffmanStateAwaitDistance;
535 CTX.treeIter = CTX.distTree;
539 if (CTX.state == huffmanStateAwaitDistance) {
540 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self,
541 tryReadBits, &CTX.treeIter, &value))
544 if OF_UNLIKELY (value >= numDistanceCodes)
548 CTX.distance = distanceCodes[value];
549 extraBits = distanceExtraBits[value];
552 if OF_UNLIKELY (!tryReadBits(
self,
554#define HSADEB huffmanStateAwaitDistanceExtraBits
557 CTX.extraBits = extraBits;
561 CTX.distance += bits;
564 CTX.state = huffmanStateProcessPair;
565 }
else if (CTX.state ==
566 huffmanStateAwaitDistanceExtraBits) {
567 if OF_UNLIKELY (!tryReadBits(
self, &bits,
571 CTX.distance += bits;
573 CTX.state = huffmanStateProcessPair;
577 if (CTX.state == huffmanStateProcessPair) {
578 for (uint_fast16_t j = 0; j < CTX.length; j++) {
581 if OF_UNLIKELY (length == 0) {
586 idx = (_slidingWindowIndex -
587 CTX.distance) & _slidingWindowMask;
588 value = _slidingWindow[idx];
590 buffer[bytesWritten++] = value;
593 _slidingWindow[_slidingWindowIndex] =
595 _slidingWindowIndex =
596 (_slidingWindowIndex + 1) &
600 CTX.state = huffmanStateAwaitCode;
601 CTX.treeIter = CTX.litLenTree;
604 if OF_UNLIKELY (!_OFHuffmanTreeWalk(
self, tryReadBits,
605 &CTX.treeIter, &value))
609 if OF_UNLIKELY (value == 256) {
610 if (CTX.litLenTree != fixedLitLenTree)
611 _OFHuffmanTreeFree(CTX.litLenTree);
612 if (CTX.distTree != fixedDistTree)
613 _OFHuffmanTreeFree(CTX.distTree);
615 _state = stateBlockHeader;
620 if OF_LIKELY (value < 256) {
621 if OF_UNLIKELY (length == 0) {
622 CTX.state = huffmanStateWriteValue;
627 buffer[bytesWritten++] = value;
630 _slidingWindow[_slidingWindowIndex] = value;
631 _slidingWindowIndex =
632 (_slidingWindowIndex + 1) &
635 CTX.treeIter = CTX.litLenTree;
639 if OF_UNLIKELY (value > 285)
643 lengthCodeIndex = value - 257;
644 CTX.length = lengthCodes[lengthCodeIndex] + 3;
645 extraBits = lengthExtraBits[lengthCodeIndex];
648 if OF_UNLIKELY (!tryReadBits(
self, &bits,
650 CTX.extraBits = extraBits;
652 huffmanStateAwaitLengthExtraBits;
659 CTX.treeIter = CTX.distTree;
660 CTX.state = huffmanStateAwaitDistance;
675 return _atEndOfStream;
680 return ((id <OFReadyForReadingObserving>)_stream)
681 .fileDescriptorForReading;
687 _bufferLength - _bufferIndex > 0);
696 [_stream unreadFromBuffer: _buffer + _bufferIndex
697 length: _bufferLength - _bufferIndex];
698 _bufferIndex = _bufferLength = 0;
void * OFAllocMemory(size_t count, size_t size)
Allocates memory for the specified number of items of the specified size.
Definition OFObject.m:112
void OFFreeMemory(void *pointer)
Frees memory allocated by OFAllocMemory, OFAllocZeroedMemory or OFResizeMemory.
Definition OFObject.m:167
void * OFAllocZeroedMemory(size_t count, size_t size)
Allocates memory for the specified number of items of the specified size and initializes it with zero...
Definition OFObject.m:130
#define nil
A value representing no object.
Definition ObjFWRT.h:68
instancetype exception()
Creates a new, autoreleased exception.
Definition OFException.m:283
A class that handles Deflate decompression transparently for an underlying stream.
Definition OFInflateStream.h:39
OFStream * underlyingStream
The underlying stream of the inflate stream.
Definition OFInflateStream.h:84
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:696
void dealloc()
Deallocates the object.
Definition OFObject.m:1339
void initialize()
A method which is called the moment before the first call to the class is being made.
Definition OFObject.m:544
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:134
bool lowlevelIsAtEndOfStream()
Returns whether the lowlevel is at the end of the stream.
Definition OFStream.m:111
void close()
Closes the stream.
Definition OFStream.m:1690
bool lowlevelHasDataInReadBuffer()
Returns whether the lowlevel has data in the read buffer.
Definition OFStream.m:116
bool hasDataInReadBuffer
Whether data is present in the internal read buffer.
Definition OFStream.h:306
instancetype retain()
Increases the retain count.
int fileDescriptorForReading
The file descriptor for reading that should be checked by the OFKernelEventObserver.
Definition OFKernelEventObserver.h:88