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
|
#ifndef OF_INFLATE64_STREAM_M
# import "OFInflateStream.h"
#else
# import "OFInflate64Stream.h"
# define OFInflateStream OFInflate64Stream
#endif
#import "huffman_tree.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#ifndef OF_INFLATE64_STREAM_M
# define BUFFER_SIZE OF_INFLATE_STREAM_BUFFER_SIZE
#else
# define BUFFER_SIZE OF_INFLATE64_STREAM_BUFFER_SIZE
#endif
enum state {
BLOCK_HEADER,
UNCOMPRESSED_BLOCK_HEADER,
UNCOMPRESSED_BLOCK,
HUFFMAN_TREE,
HUFFMAN_BLOCK
};
enum huffman_state {
WRITE_VALUE,
AWAIT_CODE,
AWAIT_LENGTH_EXTRA_BITS,
AWAIT_DISTANCE,
|
<
|
|
|
|
|
|
|
|
|
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
|
#ifndef OF_INFLATE64_STREAM_M
# import "OFInflateStream.h"
#else
# import "OFInflate64Stream.h"
# define OFInflateStream OFInflate64Stream
#endif
#import "OFHuffmanTree.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#ifndef OF_INFLATE64_STREAM_M
# define bufferSize OFInflateStreamBufferSize
#else
# define bufferSize OFInflate64StreamBufferSize
#endif
enum state {
StateBlockHeader,
StateUncompressedBlockHeader,
StateUncompressedBlock,
StateHuffmanTree,
StateHuffmanBlock
};
enum huffman_state {
WRITE_VALUE,
AWAIT_CODE,
AWAIT_LENGTH_EXTRA_BITS,
AWAIT_DISTANCE,
|
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
|
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
10, 11, 11, 12, 12, 13, 13, 14, 14
};
#endif
static const uint8_t codeLengthsOrder[19] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
static struct of_huffman_tree *fixedLitLenTree, *fixedDistTree;
@implementation OFInflateStream
static OF_INLINE bool
tryReadBits(OFInflateStream *stream, uint16_t *bits, uint8_t count)
{
uint16_t ret = stream->_savedBits;
assert(stream->_savedBitsLength < count);
for (uint_fast8_t i = stream->_savedBitsLength; i < count; i++) {
if OF_UNLIKELY (stream->_bitIndex == 8) {
if OF_LIKELY (stream->_bufferIndex <
stream->_bufferLength)
stream->_byte =
stream->_buffer[stream->_bufferIndex++];
else {
size_t length = [stream->_stream
readIntoBuffer: stream->_buffer
length: BUFFER_SIZE];
if OF_UNLIKELY (length < 1) {
stream->_savedBits = ret;
stream->_savedBitsLength = i;
return false;
}
|
|
|
|
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
|
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
10, 11, 11, 12, 12, 13, 13, 14, 14
};
#endif
static const uint8_t codeLengthsOrder[19] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
static OFHuffmanTree *fixedLitLenTree, *fixedDistTree;
@implementation OFInflateStream
static OF_INLINE bool
tryReadBits(OFInflateStream *stream, uint16_t *bits, uint8_t count)
{
uint16_t ret = stream->_savedBits;
assert(stream->_savedBitsLength < count);
for (uint_fast8_t i = stream->_savedBitsLength; i < count; i++) {
if OF_UNLIKELY (stream->_bitIndex == 8) {
if OF_LIKELY (stream->_bufferIndex <
stream->_bufferLength)
stream->_byte =
stream->_buffer[stream->_bufferIndex++];
else {
size_t length = [stream->_stream
readIntoBuffer: stream->_buffer
length: bufferSize];
if OF_UNLIKELY (length < 1) {
stream->_savedBits = ret;
stream->_savedBitsLength = i;
return false;
}
|
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
|
@throw [OFNotOpenException exceptionWithObject: self];
if (_atEndOfStream)
return 0;
start:
switch ((enum state)_state) {
case BLOCK_HEADER:
if OF_UNLIKELY (_inLastBlock) {
[_stream unreadFromBuffer: _buffer + _bufferIndex
length: _bufferLength -
_bufferIndex];
_bufferIndex = _bufferLength = 0;
_atEndOfStream = true;
return bytesWritten;
}
if OF_UNLIKELY (!tryReadBits(self, &bits, 3))
return bytesWritten;
_inLastBlock = (bits & 1);
switch (bits >> 1) {
case 0: /* No compression */
_state = UNCOMPRESSED_BLOCK_HEADER;
_bitIndex = 8;
_context.uncompressedHeader.position = 0;
memset(_context.uncompressedHeader.length, 0, 4);
break;
case 1: /* Fixed Huffman */
_state = HUFFMAN_BLOCK;
_context.huffman.state = AWAIT_CODE;
_context.huffman.litLenTree = fixedLitLenTree;
_context.huffman.distTree = fixedDistTree;
_context.huffman.treeIter = fixedLitLenTree;
break;
case 2: /* Dynamic Huffman */
_state = HUFFMAN_TREE;
_context.huffmanTree.lengths = NULL;
_context.huffmanTree.receivedCount = 0;
_context.huffmanTree.value = 0xFE;
_context.huffmanTree.litLenCodesCount = 0xFF;
_context.huffmanTree.distCodesCount = 0xFF;
_context.huffmanTree.codeLenCodesCount = 0xFF;
break;
default:
@throw [OFInvalidFormatException exception];
}
goto start;
case UNCOMPRESSED_BLOCK_HEADER:
#define CTX _context.uncompressedHeader
/* FIXME: This can be done more efficiently than unreading */
[_stream unreadFromBuffer: _buffer + _bufferIndex
length: _bufferLength - _bufferIndex];
_bufferIndex = _bufferLength = 0;
CTX.position += [_stream
readIntoBuffer: CTX.length + CTX.position
length: 4 - CTX.position];
if OF_UNLIKELY (CTX.position < 4)
return bytesWritten;
if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
(uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
@throw [OFInvalidFormatException exception];
_state = UNCOMPRESSED_BLOCK;
/*
* Do not reorder! _context.uncompressed.position and
* _context.uncompressedHeader.length overlap!
*/
_context.uncompressed.length =
CTX.length[0] | (CTX.length[1] << 8);
_context.uncompressed.position = 0;
goto start;
#undef CTX
case UNCOMPRESSED_BLOCK:
#define CTX _context.uncompressed
if OF_UNLIKELY (length == 0)
return bytesWritten;
tmp = (length < (size_t)CTX.length - CTX.position
? (uint16_t)length : CTX.length - CTX.position);
|
|
|
|
|
|
|
|
|
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
|
@throw [OFNotOpenException exceptionWithObject: self];
if (_atEndOfStream)
return 0;
start:
switch ((enum state)_state) {
case StateBlockHeader:
if OF_UNLIKELY (_inLastBlock) {
[_stream unreadFromBuffer: _buffer + _bufferIndex
length: _bufferLength -
_bufferIndex];
_bufferIndex = _bufferLength = 0;
_atEndOfStream = true;
return bytesWritten;
}
if OF_UNLIKELY (!tryReadBits(self, &bits, 3))
return bytesWritten;
_inLastBlock = (bits & 1);
switch (bits >> 1) {
case 0: /* No compression */
_state = StateUncompressedBlockHeader;
_bitIndex = 8;
_context.uncompressedHeader.position = 0;
memset(_context.uncompressedHeader.length, 0, 4);
break;
case 1: /* Fixed Huffman */
_state = StateHuffmanBlock;
_context.huffman.state = AWAIT_CODE;
_context.huffman.litLenTree = fixedLitLenTree;
_context.huffman.distTree = fixedDistTree;
_context.huffman.treeIter = fixedLitLenTree;
break;
case 2: /* Dynamic Huffman */
_state = StateHuffmanTree;
_context.huffmanTree.lengths = NULL;
_context.huffmanTree.receivedCount = 0;
_context.huffmanTree.value = 0xFE;
_context.huffmanTree.litLenCodesCount = 0xFF;
_context.huffmanTree.distCodesCount = 0xFF;
_context.huffmanTree.codeLenCodesCount = 0xFF;
break;
default:
@throw [OFInvalidFormatException exception];
}
goto start;
case StateUncompressedBlockHeader:
#define CTX _context.uncompressedHeader
/* FIXME: This can be done more efficiently than unreading */
[_stream unreadFromBuffer: _buffer + _bufferIndex
length: _bufferLength - _bufferIndex];
_bufferIndex = _bufferLength = 0;
CTX.position += [_stream
readIntoBuffer: CTX.length + CTX.position
length: 4 - CTX.position];
if OF_UNLIKELY (CTX.position < 4)
return bytesWritten;
if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
(uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
@throw [OFInvalidFormatException exception];
_state = StateUncompressedBlock;
/*
* Do not reorder! _context.uncompressed.position and
* _context.uncompressedHeader.length overlap!
*/
_context.uncompressed.length =
CTX.length[0] | (CTX.length[1] << 8);
_context.uncompressed.position = 0;
goto start;
#undef CTX
case StateUncompressedBlock:
#define CTX _context.uncompressed
if OF_UNLIKELY (length == 0)
return bytesWritten;
tmp = (length < (size_t)CTX.length - CTX.position
? (uint16_t)length : CTX.length - CTX.position);
|
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
|
CTX.receivedCount = i;
return bytesWritten;
}
CTX.lengths[codeLengthsOrder[i]] = bits;
}
CTX.codeLenTree = of_huffman_tree_construct(
CTX.lengths, 19);
CTX.treeIter = CTX.codeLenTree;
OFFreeMemory(CTX.lengths);
CTX.lengths = NULL;
CTX.receivedCount = 0;
CTX.value = 0xFF;
}
if OF_LIKELY (CTX.lengths == NULL)
CTX.lengths = OFAllocMemory(
CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);
for (uint16_t i = CTX.receivedCount;
i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
uint8_t j, count;
if OF_LIKELY (CTX.value == 0xFF) {
if OF_UNLIKELY (!of_huffman_tree_walk(self,
tryReadBits, &CTX.treeIter, &value)) {
CTX.receivedCount = i;
return bytesWritten;
}
CTX.treeIter = CTX.codeLenTree;
|
<
|
|
|
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
|
CTX.receivedCount = i;
return bytesWritten;
}
CTX.lengths[codeLengthsOrder[i]] = bits;
}
CTX.codeLenTree = OFHuffmanTreeNew(CTX.lengths, 19);
CTX.treeIter = CTX.codeLenTree;
OFFreeMemory(CTX.lengths);
CTX.lengths = NULL;
CTX.receivedCount = 0;
CTX.value = 0xFF;
}
if OF_LIKELY (CTX.lengths == NULL)
CTX.lengths = OFAllocMemory(
CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);
for (uint16_t i = CTX.receivedCount;
i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
uint8_t j, count;
if OF_LIKELY (CTX.value == 0xFF) {
if OF_UNLIKELY (!OFHuffmanTreeWalk(self,
tryReadBits, &CTX.treeIter, &value)) {
CTX.receivedCount = i;
return bytesWritten;
}
CTX.treeIter = CTX.codeLenTree;
|
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
|
for (j = 0; j < count; j++)
CTX.lengths[i++] = value;
CTX.value = 0xFF;
}
of_huffman_tree_release(CTX.codeLenTree);
CTX.codeLenTree = NULL;
CTX.litLenTree = of_huffman_tree_construct(CTX.lengths,
CTX.litLenCodesCount + 257);
CTX.distTree = of_huffman_tree_construct(
CTX.lengths + CTX.litLenCodesCount + 257,
CTX.distCodesCount + 1);
OFFreeMemory(CTX.lengths);
/*
* litLenTree and distTree are at the same location in
* _context.huffman and _context.huffmanTree, thus no need to
* set them.
*/
_state = HUFFMAN_BLOCK;
_context.huffman.state = AWAIT_CODE;
_context.huffman.treeIter = CTX.litLenTree;
goto start;
#undef CTX
case HUFFMAN_BLOCK:
#define CTX _context.huffman
for (;;) {
uint8_t extraBits, lengthCodeIndex;
if OF_UNLIKELY (CTX.state == WRITE_VALUE) {
if OF_UNLIKELY (length == 0)
return bytesWritten;
|
|
|
|
|
|
|
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
|
for (j = 0; j < count; j++)
CTX.lengths[i++] = value;
CTX.value = 0xFF;
}
OFHuffmanTreeFree(CTX.codeLenTree);
CTX.codeLenTree = NULL;
CTX.litLenTree = OFHuffmanTreeNew(CTX.lengths,
CTX.litLenCodesCount + 257);
CTX.distTree = OFHuffmanTreeNew(
CTX.lengths + CTX.litLenCodesCount + 257,
CTX.distCodesCount + 1);
OFFreeMemory(CTX.lengths);
/*
* litLenTree and distTree are at the same location in
* _context.huffman and _context.huffmanTree, thus no need to
* set them.
*/
_state = StateHuffmanBlock;
_context.huffman.state = AWAIT_CODE;
_context.huffman.treeIter = CTX.litLenTree;
goto start;
#undef CTX
case StateHuffmanBlock:
#define CTX _context.huffman
for (;;) {
uint8_t extraBits, lengthCodeIndex;
if OF_UNLIKELY (CTX.state == WRITE_VALUE) {
if OF_UNLIKELY (length == 0)
return bytesWritten;
|