ObjFW
OFInflateStream.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFStream.h"
18 
19 OF_ASSUME_NONNULL_BEGIN
20 
21 #define OF_INFLATE_STREAM_BUFFER_SIZE 4096
22 
30 {
31 #ifdef OF_INFLATE_STREAM_M
32 @public
33 #endif
34  OFStream *_stream;
35  uint8_t _buffer[OF_INFLATE_STREAM_BUFFER_SIZE];
36  uint_fast16_t _bufferIndex, _bufferLength;
37  uint8_t _byte;
38  uint_fast8_t _bitIndex, _savedBitsLength;
39  uint_fast16_t _savedBits;
40 @protected
41  uint8_t *_slidingWindow;
42  uint_fast16_t _slidingWindowIndex, _slidingWindowMask;
43  enum {
44  OF_INFLATE_STREAM_BLOCK_HEADER,
45  OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK_HEADER,
46  OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK,
47  OF_INFLATE_STREAM_HUFFMAN_TREE,
48  OF_INFLATE_STREAM_HUFFMAN_BLOCK
49  } _state;
50  union {
51  struct {
52  uint_fast8_t position;
53  uint8_t length[4];
54  } uncompressedHeader;
55  struct {
56  uint_fast16_t position, length;
57  } uncompressed;
58  struct {
59  struct huffman_tree *litLenTree, *distTree;
60  struct huffman_tree *codeLenTree, *treeIter;
61  uint8_t *lengths;
62  uint_fast16_t receivedCount;
63  uint_fast8_t value, litLenCodesCount, distCodesCount;
64  uint_fast8_t codeLenCodesCount;
65  } huffmanTree;
66  struct {
67  struct huffman_tree *litLenTree, *distTree, *treeIter;
68  enum {
69  OF_INFLATE_STREAM_WRITE_VALUE,
70  OF_INFLATE_STREAM_AWAIT_CODE,
71  OF_INFLATE_STREAM_AWAIT_LENGTH_EXTRA_BITS,
72  OF_INFLATE_STREAM_AWAIT_DISTANCE,
73  OF_INFLATE_STREAM_AWAIT_DISTANCE_EXTRA_BITS,
74  OF_INFLATE_STREAM_PROCESS_PAIR
75  } state;
76  uint_fast16_t value, length, distance;
77  uint_fast16_t extraBits;
78  } huffman;
79  } _context;
80  bool _inLastBlock, _atEndOfStream;
81 }
82 
90 + (instancetype)streamWithStream: (OFStream*)stream;
91 
100 - initWithStream: (OFStream*)stream;
101 @end
102 
103 OF_ASSUME_NONNULL_END
A class that handles Deflate decompression transparently for an underlying stream.
Definition: OFInflateStream.h:29
A base class for different types of streams.
Definition: OFStream.h:86