ObjFW
 
Loading...
Searching...
No Matches
OFDeflateStream.m
1/*
2 * Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#include "config.h"
21
22#include <stdlib.h>
23#include <string.h>
24
25#import "OFDeflateStream.h"
26#ifdef OF_DEFLATE64_STREAM_M
27# import "OFDeflate64Stream.h"
28# define OFDeflateStream OFDeflate64Stream
29#endif
30#import "OFHuffmanTree.h"
31
32#import "OFInitializationFailedException.h"
33#import "OFInvalidArgumentException.h"
34#import "OFInvalidFormatException.h"
35#import "OFNotImplementedException.h"
36#import "OFNotOpenException.h"
37#import "OFOutOfMemoryException.h"
38
39enum State {
40 stateBlockHeader,
41 stateUncompressedBlockHeader,
42 stateUncompressedBlock,
43 stateHuffmanTree,
44 stateHuffmanBlock
45};
46
47enum HuffmanState {
48 huffmanStateWriteValue,
49 huffmanStateAwaitCode,
50 huffmanStateAwaitLengthExtraBits,
51 huffmanStateAwaitDistance,
52 huffmanStateAwaitDistanceExtraBits,
53 huffmanStateProcessPair
54};
55
56#ifndef OF_DEFLATE64_STREAM_M
57static const uint16_t slidingWindowMask = 0x7FFF;
58static const uint8_t numDistanceCodes = 30;
59static const uint8_t lengthCodes[29] = {
60 /* indices are -257, values -3 */
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
63};
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,
66 5, 5, 5, 5, 0
67};
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
71};
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
75};
76#else
77static const uint16_t slidingWindowMask = 0xFFFF;
78static const uint8_t numDistanceCodes = 32;
79static const uint8_t lengthCodes[29] = {
80 /* indices are -257, values -3 */
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
83};
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,
86 5, 5, 5, 5, 16
87};
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,
91 32769, 49153
92};
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
96};
97#endif
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
100};
101static OFHuffmanTree fixedLitLenTree, fixedDistTree;
102
103@implementation OFDeflateStream
104@synthesize underlyingStream = _stream;
105
106static OF_INLINE bool
107tryReadBits(OFDeflateStream *stream, uint16_t *bits, uint8_t count)
108{
109 struct OFInflateContext *ctx = stream->_inflateCtx;
110 uint16_t ret = ctx->savedBits;
111
112 OFAssert(ctx->savedBitsLength < count);
113
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++];
118 else {
119 size_t length = [stream->_stream
120 readIntoBuffer: ctx->buffer
121 length: OFInflateStreamBufferSize];
122
123 if OF_UNLIKELY (length < 1) {
124 ctx->savedBits = ret;
125 ctx->savedBitsLength = i;
126 return false;
127 }
128
129 ctx->byte = ctx->buffer[0];
130 ctx->bufferIndex = 1;
131 ctx->bufferLength = (uint16_t)length;
132 }
133
134 ctx->bitIndex = 0;
135 }
136
137 ret |= ((ctx->byte >> ctx->bitIndex++) & 1) << i;
138 }
139
140 ctx->savedBits = 0;
141 ctx->savedBitsLength = 0;
142 *bits = ret;
143
144 return true;
145}
146
147+ (void)initialize
148{
149 uint8_t lengths[288];
150
151 if (self != [OFDeflateStream class])
152 return;
153
154 for (uint16_t i = 0; i <= 143; i++)
155 lengths[i] = 8;
156 for (uint16_t i = 144; i <= 255; i++)
157 lengths[i] = 9;
158 for (uint16_t i = 256; i <= 279; i++)
159 lengths[i] = 7;
160 for (uint16_t i = 280; i <= 287; i++)
161 lengths[i] = 8;
162
163 fixedLitLenTree = _OFHuffmanTreeNew(lengths, 288);
164
165 for (uint16_t i = 0; i <= 31; i++)
166 lengths[i] = 5;
167
168 fixedDistTree = _OFHuffmanTreeNew(lengths, 32);
169}
170
171+ (instancetype)streamWithStream: (OFStream *)stream
172{
173 return objc_autoreleaseReturnValue(
174 [[self alloc] initWithStream: stream]);
175}
176
177+ (instancetype)streamWithStream: (OFStream *)stream mode: (OFString *)mode
178{
179 return objc_autoreleaseReturnValue(
180 [[self alloc] initWithStream: stream
181 mode: mode]);
182}
183
184- (instancetype)init
185{
186 OF_INVALID_INIT_METHOD
187}
188
189- (instancetype)initWithStream: (OFStream *)stream
190{
191 return [self initWithStream: stream mode: @"r"];
192}
193
194- (instancetype)initWithStream: (OFStream *)stream mode: (OFString *)mode
195{
196 self = [super init];
197
198 @try {
199 _stream = objc_retain(stream);
200 _slidingWindow = OFAllocZeroedMemory(slidingWindowMask + 1, 1);
201
202 if ([mode isEqual: @"r"]) {
203 _inflateCtx = OFAllocZeroedMemory(1,
204 sizeof(*_inflateCtx));
205
206 /* 0-7 address the bit, 8 means fetch next byte */
207 _inflateCtx->bitIndex = 8;
208 } else if ([mode isEqual: @"w"])
211 object: nil];
212 else
214 } @catch (id e) {
215 objc_release(self);
216 @throw e;
217 }
218
219 return self;
220}
221
222- (void)dealloc
223{
224 if (_stream != nil)
225 [self close];
226
227 OFFreeMemory(_slidingWindow);
228
229 if (_inflateCtx != NULL) {
230 if (_inflateCtx->state == stateHuffmanTree) {
231 OFFreeMemory(_inflateCtx->ctx.huffmanTree.lengths);
232
233 if (_inflateCtx->ctx.huffmanTree.codeLenTree != NULL)
234 _OFHuffmanTreeFree(
235 _inflateCtx->ctx.huffmanTree.codeLenTree);
236 }
237
238 if (_inflateCtx->state == stateHuffmanTree ||
239 _inflateCtx->state == stateHuffmanBlock) {
240 if (_inflateCtx->ctx.huffman.litLenTree !=
241 fixedLitLenTree)
242 _OFHuffmanTreeFree(
243 _inflateCtx->ctx.huffman.litLenTree);
244 if (_inflateCtx->ctx.huffman.distTree != fixedDistTree)
245 _OFHuffmanTreeFree(
246 _inflateCtx->ctx.huffman.distTree);
247 }
248
249 OFFreeMemory(_inflateCtx);
250 }
251
252 [super dealloc];
253}
254
255- (size_t)lowlevelReadIntoBuffer: (void *)buffer_
256 length: (size_t)length
257{
258 unsigned char *buffer = buffer_;
259 uint16_t bits = 0, tmp, value = 0;
260 size_t bytesWritten = 0;
261 unsigned char *slidingWindow;
262 uint16_t slidingWindowIndex;
263 struct OFInflateContext *ctx;
264
265 if (_stream == nil || _inflateCtx == NULL)
267
268 if (_atEndOfStream)
269 return 0;
270
271 ctx = _inflateCtx;
272
273start:
274 switch ((enum State)ctx->state) {
275 case stateBlockHeader:
276 if OF_UNLIKELY (ctx->inLastBlock) {
277 [_stream unreadFromBuffer: ctx->buffer +
278 ctx->bufferIndex
279 length: ctx->bufferLength -
280 ctx->bufferIndex];
281 ctx->bufferIndex = ctx->bufferLength = 0;
282
283 _atEndOfStream = true;
284 return bytesWritten;
285 }
286
287 if OF_UNLIKELY (!tryReadBits(self, &bits, 3))
288 return bytesWritten;
289
290 ctx->inLastBlock = (bits & 1);
291
292 switch (bits >> 1) {
293 case 0: /* No compression */
294 ctx->state = stateUncompressedBlockHeader;
295 ctx->bitIndex = 8;
296 ctx->ctx.uncompressedHeader.position = 0;
297 memset(ctx->ctx.uncompressedHeader.length, 0, 4);
298 break;
299 case 1: /* Fixed Huffman */
300 ctx->state = stateHuffmanBlock;
301 ctx->ctx.huffman.state = huffmanStateAwaitCode;
302 ctx->ctx.huffman.litLenTree = fixedLitLenTree;
303 ctx->ctx.huffman.distTree = fixedDistTree;
304 ctx->ctx.huffman.treeIter = fixedLitLenTree;
305 break;
306 case 2: /* Dynamic Huffman */
307 ctx->state = stateHuffmanTree;
308 ctx->ctx.huffmanTree.lengths = NULL;
309 ctx->ctx.huffmanTree.receivedCount = 0;
310 ctx->ctx.huffmanTree.value = 0xFE;
311 ctx->ctx.huffmanTree.litLenCodesCount = 0xFF;
312 ctx->ctx.huffmanTree.distCodesCount = 0xFF;
313 ctx->ctx.huffmanTree.codeLenCodesCount = 0xFF;
314 break;
315 default:
317 }
318
319 goto start;
320 case stateUncompressedBlockHeader:
321#define CTX ctx->ctx.uncompressedHeader
322 /* FIXME: This can be done more efficiently than unreading */
323 [_stream unreadFromBuffer: ctx->buffer + ctx->bufferIndex
324 length: ctx->bufferLength -
325 ctx->bufferIndex];
326 ctx->bufferIndex = ctx->bufferLength = 0;
327
328 CTX.position += [_stream
329 readIntoBuffer: CTX.length + CTX.position
330 length: 4 - CTX.position];
331
332 if OF_UNLIKELY (CTX.position < 4)
333 return bytesWritten;
334
335 if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) !=
336 (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8)))
337 @throw [OFInvalidFormatException exception];
338
339 ctx->state = stateUncompressedBlock;
340
341 /*
342 * Do not reorder! _context.uncompressed.position and
343 * _context.uncompressedHeader.length overlap!
344 */
345 ctx->ctx.uncompressed.length =
346 CTX.length[0] | (CTX.length[1] << 8);
347 ctx->ctx.uncompressed.position = 0;
348
349 goto start;
350#undef CTX
351 case stateUncompressedBlock:
352#define CTX ctx->ctx.uncompressed
353 if OF_UNLIKELY (length == 0)
354 return bytesWritten;
355
356 tmp = (length < (size_t)CTX.length - CTX.position
357 ? (uint16_t)length : CTX.length - CTX.position);
358
359 tmp = (uint16_t)[_stream readIntoBuffer: buffer + bytesWritten
360 length: tmp];
361
362 if OF_UNLIKELY (tmp == 0)
363 return bytesWritten;
364
365 slidingWindow = _slidingWindow;
366 slidingWindowIndex = _slidingWindowIndex;
367 for (uint_fast16_t i = 0; i < tmp; i++) {
368 slidingWindow[slidingWindowIndex] =
369 buffer[bytesWritten + i];
370 slidingWindowIndex = (slidingWindowIndex + 1) &
371 slidingWindowMask;
372 }
373 _slidingWindowIndex = slidingWindowIndex;
374
375 length -= tmp;
376 bytesWritten += tmp;
377
378 CTX.position += tmp;
379 if OF_UNLIKELY (CTX.position == CTX.length)
380 ctx->state = stateBlockHeader;
381
382 goto start;
383#undef CTX
384 case stateHuffmanTree:
385#define CTX ctx->ctx.huffmanTree
386 if OF_LIKELY (CTX.value == 0xFE) {
387 if OF_LIKELY (CTX.litLenCodesCount == 0xFF) {
388 if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
389 return bytesWritten;
390
391 if OF_UNLIKELY (bits > 29)
392 @throw [OFInvalidFormatException
393 exception];
394
395 CTX.litLenCodesCount = bits;
396 }
397
398 if OF_LIKELY (CTX.distCodesCount == 0xFF) {
399 if OF_UNLIKELY (!tryReadBits(self, &bits, 5))
400 return bytesWritten;
401
402 CTX.distCodesCount = bits;
403 }
404
405 if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) {
406 if OF_UNLIKELY (!tryReadBits(self, &bits, 4))
407 return bytesWritten;
408
409 CTX.codeLenCodesCount = bits;
410 }
411
412 if OF_LIKELY (CTX.lengths == NULL)
413 CTX.lengths = OFAllocZeroedMemory(19, 1);
414
415 for (uint16_t i = CTX.receivedCount;
416 i < CTX.codeLenCodesCount + 4; i++) {
417 if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) {
418 CTX.receivedCount = i;
419 return bytesWritten;
420 }
421
422 CTX.lengths[codeLengthsOrder[i]] = bits;
423 }
424
425 CTX.codeLenTree = _OFHuffmanTreeNew(CTX.lengths, 19);
426 CTX.treeIter = CTX.codeLenTree;
427
428 OFFreeMemory(CTX.lengths);
429 CTX.lengths = NULL;
430 CTX.receivedCount = 0;
431 CTX.value = 0xFF;
432 }
433
434 if OF_LIKELY (CTX.lengths == NULL)
435 CTX.lengths = OFAllocMemory(
436 CTX.litLenCodesCount + CTX.distCodesCount + 258, 1);
437
438 for (uint16_t i = CTX.receivedCount;
439 i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) {
440 uint8_t j, count;
441
442 if OF_LIKELY (CTX.value == 0xFF) {
443 if OF_UNLIKELY (!_OFHuffmanTreeWalk(self,
444 tryReadBits, &CTX.treeIter, &value)) {
445 CTX.receivedCount = i;
446 return bytesWritten;
447 }
448
449 CTX.treeIter = CTX.codeLenTree;
450
451 if (value < 16) {
452 CTX.lengths[i++] = value;
453 continue;
454 }
455 } else
456 value = CTX.value;
457
458 switch (value) {
459 case 16:
460 if OF_UNLIKELY (i < 1)
462 exception];
463
464 if OF_UNLIKELY (!tryReadBits(self, &bits, 2)) {
465 CTX.receivedCount = i;
466 CTX.value = value;
467 return bytesWritten;
468 }
469
470 value = CTX.lengths[i - 1];
471 count = bits + 3;
472
473 break;
474 case 17:
475 if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) {
476 CTX.receivedCount = i;
477 CTX.value = value;
478 return bytesWritten;
479 }
480
481 value = 0;
482 count = bits + 3;
483
484 break;
485 case 18:
486 if OF_UNLIKELY (!tryReadBits(self, &bits, 7)) {
487 CTX.receivedCount = i;
488 CTX.value = value;
489 return bytesWritten;
490 }
491
492 value = 0;
493 count = bits + 11;
494
495 break;
496 default:
498 }
499
500 if OF_UNLIKELY (i + count >
501 CTX.litLenCodesCount + CTX.distCodesCount + 258)
503
504 for (j = 0; j < count; j++)
505 CTX.lengths[i++] = value;
506
507 CTX.value = 0xFF;
508 }
509
510 _OFHuffmanTreeFree(CTX.codeLenTree);
511 CTX.codeLenTree = NULL;
512
513 CTX.litLenTree = _OFHuffmanTreeNew(CTX.lengths,
514 CTX.litLenCodesCount + 257);
515 CTX.distTree = _OFHuffmanTreeNew(
516 CTX.lengths + CTX.litLenCodesCount + 257,
517 CTX.distCodesCount + 1);
518
519 OFFreeMemory(CTX.lengths);
520
521 /*
522 * litLenTree and distTree are at the same location in
523 * _context.huffman and _context.huffmanTree, thus no need to
524 * set them.
525 */
526 ctx->state = stateHuffmanBlock;
527 ctx->ctx.huffman.state = huffmanStateAwaitCode;
528 ctx->ctx.huffman.treeIter = CTX.litLenTree;
529
530 goto start;
531#undef CTX
532 case stateHuffmanBlock:
533#define CTX ctx->ctx.huffman
534 for (;;) {
535 uint8_t extraBits, lengthCodeIndex;
536
537 if OF_UNLIKELY (CTX.state == huffmanStateWriteValue) {
538 if OF_UNLIKELY (length == 0)
539 return bytesWritten;
540
541 buffer[bytesWritten++] = CTX.value;
542 length--;
543
544 _slidingWindow[_slidingWindowIndex] = CTX.value;
545 _slidingWindowIndex =
546 (_slidingWindowIndex + 1) &
547 slidingWindowMask;
548
549 CTX.state = huffmanStateAwaitCode;
550 CTX.treeIter = CTX.litLenTree;
551 }
552
553 if OF_UNLIKELY (CTX.state ==
554 huffmanStateAwaitLengthExtraBits) {
555 if OF_UNLIKELY (!tryReadBits(self, &bits,
556 CTX.extraBits))
557 return bytesWritten;
558
559 CTX.length += bits;
560
561 CTX.state = huffmanStateAwaitDistance;
562 CTX.treeIter = CTX.distTree;
563 }
564
565 /* Distance of length distance pair */
566 if (CTX.state == huffmanStateAwaitDistance) {
567 if OF_UNLIKELY (!_OFHuffmanTreeWalk(self,
568 tryReadBits, &CTX.treeIter, &value))
569 return bytesWritten;
570
571 if OF_UNLIKELY (value >= numDistanceCodes)
573 exception];
574
575 CTX.distance = distanceCodes[value];
576 extraBits = distanceExtraBits[value];
577
578 if (extraBits > 0) {
579 if OF_UNLIKELY (!tryReadBits(self,
580 &bits, extraBits)) {
581#define HSADEB huffmanStateAwaitDistanceExtraBits
582 CTX.state = HSADEB;
583#undef HSADEB
584 CTX.extraBits = extraBits;
585 return bytesWritten;
586 }
587
588 CTX.distance += bits;
589 }
590
591 CTX.state = huffmanStateProcessPair;
592 } else if (CTX.state ==
593 huffmanStateAwaitDistanceExtraBits) {
594 if OF_UNLIKELY (!tryReadBits(self, &bits,
595 CTX.extraBits))
596 return bytesWritten;
597
598 CTX.distance += bits;
599
600 CTX.state = huffmanStateProcessPair;
601 }
602
603 /* Length distance pair */
604 if (CTX.state == huffmanStateProcessPair) {
605 for (uint_fast16_t j = 0; j < CTX.length; j++) {
606 uint16_t idx;
607
608 if OF_UNLIKELY (length == 0) {
609 CTX.length -= j;
610 return bytesWritten;
611 }
612
613 idx = (_slidingWindowIndex -
614 CTX.distance) & slidingWindowMask;
615 value = _slidingWindow[idx];
616
617 buffer[bytesWritten++] = value;
618 length--;
619
620 _slidingWindow[_slidingWindowIndex] =
621 value;
622 _slidingWindowIndex =
623 (_slidingWindowIndex + 1) &
624 slidingWindowMask;
625 }
626
627 CTX.state = huffmanStateAwaitCode;
628 CTX.treeIter = CTX.litLenTree;
629 }
630
631 if OF_UNLIKELY (!_OFHuffmanTreeWalk(self, tryReadBits,
632 &CTX.treeIter, &value))
633 return bytesWritten;
634
635 /* End of block */
636 if OF_UNLIKELY (value == 256) {
637 if (CTX.litLenTree != fixedLitLenTree)
638 _OFHuffmanTreeFree(CTX.litLenTree);
639 if (CTX.distTree != fixedDistTree)
640 _OFHuffmanTreeFree(CTX.distTree);
641
642 ctx->state = stateBlockHeader;
643 goto start;
644 }
645
646 /* Literal byte */
647 if OF_LIKELY (value < 256) {
648 if OF_UNLIKELY (length == 0) {
649 CTX.state = huffmanStateWriteValue;
650 CTX.value = value;
651 return bytesWritten;
652 }
653
654 buffer[bytesWritten++] = value;
655 length--;
656
657 _slidingWindow[_slidingWindowIndex] = value;
658 _slidingWindowIndex =
659 (_slidingWindowIndex + 1) &
660 slidingWindowMask;
661
662 CTX.treeIter = CTX.litLenTree;
663 continue;
664 }
665
666 if OF_UNLIKELY (value > 285)
668
669 /* Length of length distance pair */
670 lengthCodeIndex = value - 257;
671 CTX.length = lengthCodes[lengthCodeIndex] + 3;
672 extraBits = lengthExtraBits[lengthCodeIndex];
673
674 if (extraBits > 0) {
675 if OF_UNLIKELY (!tryReadBits(self, &bits,
676 extraBits)) {
677 CTX.extraBits = extraBits;
678 CTX.state =
679 huffmanStateAwaitLengthExtraBits;
680 return bytesWritten;
681 }
682
683 CTX.length += bits;
684 }
685
686 CTX.treeIter = CTX.distTree;
687 CTX.state = huffmanStateAwaitDistance;
688 }
689
690 break;
691#undef CTX
692 }
693
694 OF_UNREACHABLE
695}
696
697- (bool)lowlevelIsAtEndOfStream
698{
699 if (_stream == nil)
701
702 return _atEndOfStream;
703}
704
705- (int)fileDescriptorForReading
706{
707 return ((id <OFReadyForReadingObserving>)_stream)
708 .fileDescriptorForReading;
709}
710
711- (bool)lowlevelHasDataInReadBuffer
712{
713 return (_stream.hasDataInReadBuffer || (_inflateCtx != NULL &&
714 _inflateCtx->bufferLength - _inflateCtx->bufferIndex > 0));
715}
716
717- (void)close
718{
719 if (_stream == nil)
721
722 if (_inflateCtx != NULL) {
723 /* Give back our buffer to the stream, in case it's shared */
724 [_stream unreadFromBuffer: _inflateCtx->buffer +
725 _inflateCtx->bufferIndex
726 length: _inflateCtx->bufferLength -
727 _inflateCtx->bufferIndex];
728 _inflateCtx->bufferIndex = _inflateCtx->bufferLength = 0;
729 }
730
731 objc_release(_stream);
732 _stream = nil;
733
734 [super close];
735}
736@end
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:87
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 the format is invalid.
Definition OFInvalidFormatException.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:671
instancetype alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object.
Definition OFObject.m:515
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:143