]> wimlib.net Git - wimlib/blob - include/wimlib/decompress_common.h
Remove unused 'num_syms' argument to read_huffsym()
[wimlib] / include / wimlib / decompress_common.h
1 /*
2  * decompress_common.h
3  *
4  * Header for decompression code shared by multiple compression formats.
5  */
6
7 #ifndef _WIMLIB_DECOMPRESS_COMMON_H
8 #define _WIMLIB_DECOMPRESS_COMMON_H
9
10 #include "wimlib/assert.h"
11 #include "wimlib/compiler.h"
12 #include "wimlib/error.h"
13 #include "wimlib/endianness.h"
14 #include "wimlib/types.h"
15
16 #ifndef INPUT_IDX_T_DEFINED
17 #define INPUT_IDX_T_DEFINED
18 typedef u32 input_idx_t;
19 #endif
20
21 /* Structure to encapsulate a block of in-memory data that is being interpreted
22  * as a stream of bits.
23  *
24  * This is geared specifically towards the XPRESS and LZX compression formats
25  * with regards to the actual ordering the bits within the byte sequence.  */
26 struct input_bitstream {
27
28         /* A variable of length at least 32 bits that is used to hold bits that
29          * have been read from the stream.  The bits are ordered from high-order
30          * to low-order, and the next bit is always the high-order bit.  */
31         u32 bitbuf;
32
33         /* Number of bits in @bitbuf that are valid.  */
34         unsigned bitsleft;
35
36         /* Pointer to the next byte to be retrieved from the input.  */
37         const u8 *data;
38
39         /* Number of bytes of data that are left.  */
40         input_idx_t data_bytes_left;
41 };
42
43 /* Initializes a bitstream to receive its input from @data. */
44 static inline void
45 init_input_bitstream(struct input_bitstream *istream,
46                      const void *data, input_idx_t num_data_bytes)
47 {
48         istream->bitbuf          = 0;
49         istream->bitsleft        = 0;
50         istream->data            = data;
51         istream->data_bytes_left = num_data_bytes;
52 }
53
54 /* Ensures the bit buffer variable for the bitstream contains at least @num_bits
55  * bits.  Following this, bitstream_peek_bits() and/or bitstream_remove_bits()
56  * may be called on the bitstream to peek or remove up to @num_bits bits.
57  *
58  * If the input data is exhausted, any further bits are assumed to be 0.  */
59 static inline void
60 bitstream_ensure_bits(struct input_bitstream *istream, unsigned num_bits)
61 {
62         for (int nbits = num_bits; (int)istream->bitsleft < nbits; nbits -= 16) {
63                 u16 nextword;
64                 unsigned shift;
65
66                 if (unlikely(istream->data_bytes_left < 2)) {
67                         istream->bitsleft = num_bits;
68                         return;
69                 }
70
71                 nextword = le16_to_cpu(*(const le16*)istream->data);
72                 shift = sizeof(istream->bitbuf) * 8 - 16 - istream->bitsleft;
73                 istream->bitbuf |= (u32)nextword << shift;
74                 istream->data += 2;
75                 istream->bitsleft += 16;
76                 istream->data_bytes_left -= 2;
77         }
78 }
79
80 /* Returns the next @num_bits bits from the bitstream, without removing them.
81  * There must be at least @num_bits remaining in the buffer variable, from a
82  * previous call to bitstream_ensure_bits().  */
83 static inline u32
84 bitstream_peek_bits(const struct input_bitstream *istream, unsigned num_bits)
85 {
86         if (unlikely(num_bits == 0))
87                 return 0;
88         return istream->bitbuf >> (sizeof(istream->bitbuf) * 8 - num_bits);
89 }
90
91 /* Removes @num_bits from the bitstream.  There must be at least @num_bits
92  * remaining in the buffer variable, from a previous call to
93  * bitstream_ensure_bits().  */
94 static inline void
95 bitstream_remove_bits(struct input_bitstream *istream, unsigned num_bits)
96 {
97         istream->bitbuf <<= num_bits;
98         istream->bitsleft -= num_bits;
99 }
100
101 /* Removes and returns @num_bits bits from the bitstream.  There must be at
102  * least @num_bits remaining in the buffer variable, from a previous call to
103  * bitstream_ensure_bits().  */
104 static inline u32
105 bitstream_pop_bits(struct input_bitstream *istream, unsigned num_bits)
106 {
107         u32 n = bitstream_peek_bits(istream, num_bits);
108         bitstream_remove_bits(istream, num_bits);
109         return n;
110 }
111
112 /* Reads and returns the next @num_bits bits from the bitstream.
113  * If the input data is exhausted, the bits are assumed to be 0.  */
114 static inline u32
115 bitstream_read_bits(struct input_bitstream *istream, unsigned num_bits)
116 {
117         bitstream_ensure_bits(istream, num_bits);
118         return bitstream_pop_bits(istream, num_bits);
119 }
120
121 /* Reads and returns the next literal byte embedded in the bitstream.
122  * If the input data is exhausted, the byte is assumed to be 0.  */
123 static inline u8
124 bitstream_read_byte(struct input_bitstream *istream)
125 {
126         if (unlikely(istream->data_bytes_left == 0))
127                 return 0;
128         istream->data_bytes_left--;
129         return *istream->data++;
130 }
131
132
133 /* Needed alignment of decode_table parameter to make_huffman_decode_table().
134  *
135  * Reason: We may fill the entries with SSE instructions without worrying
136  * about dealing with the unaligned case.  */
137 #define DECODE_TABLE_ALIGNMENT 16
138
139 /* Maximum supported symbol count for make_huffman_decode_table().
140  *
141  * Reason: In direct mapping entries, we store the symbol in 11 bits.  */
142 #define DECODE_TABLE_MAX_SYMBOLS 2048
143
144 /* Maximum supported table bits for make_huffman_decode_table().
145  *
146  * Reason: In internal binary tree nodes, offsets are encoded in 14 bits.
147  * But the real limit is 13, because we allocate entries past the end of
148  * the direct lookup part of the table for binary tree nodes.  (Note: if
149  * needed this limit could be removed by encoding the offsets relative to
150  * &decode_table[1 << table_bits].)  */
151 #define DECODE_TABLE_MAX_TABLE_BITS 13
152
153 /* Maximum supported codeword length for make_huffman_decode_table().
154  *
155  * Reason: In direct mapping entries, we encode the codeword length in 5
156  * bits, and the top 2 bits can't both be set because that has special
157  * meaning.  */
158 #define DECODE_TABLE_MAX_CODEWORD_LEN 23
159
160 /* Reads and returns the next Huffman-encoded symbol from a bitstream.  If the
161  * input data is exhausted, the Huffman symbol is decoded as if the missing bits
162  * are all zeroes.
163  *
164  * XXX: This is mostly duplicated in lzms_huffman_decode_symbol() in
165  * lzms-decompress.c.  */
166 static inline u16
167 read_huffsym(struct input_bitstream *istream, const u16 decode_table[],
168              unsigned table_bits, unsigned max_codeword_len)
169 {
170         u16 entry;
171         u16 key_bits;
172
173         bitstream_ensure_bits(istream, max_codeword_len);
174
175         /* Index the decode table by the next table_bits bits of the input.  */
176         key_bits = bitstream_peek_bits(istream, table_bits);
177         entry = decode_table[key_bits];
178         if (likely(entry < 0xC000)) {
179                 /* Fast case: The decode table directly provided the
180                  * symbol and codeword length.  The low 11 bits are the
181                  * symbol, and the high 5 bits are the codeword length.  */
182                 bitstream_remove_bits(istream, entry >> 11);
183                 return entry & 0x7FF;
184         } else {
185                 /* Slow case: The codeword for the symbol is longer than
186                  * table_bits, so the symbol does not have an entry
187                  * directly in the first (1 << table_bits) entries of the
188                  * decode table.  Traverse the appropriate binary tree
189                  * bit-by-bit to decode the symbol.  */
190                 bitstream_remove_bits(istream, table_bits);
191                 do {
192                         key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
193                 } while ((entry = decode_table[key_bits]) >= 0xC000);
194                 return entry;
195         }
196 }
197
198 extern int
199 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
200                           unsigned num_bits, const u8 lens[],
201                           unsigned max_codeword_len);
202
203 #endif /* _WIMLIB_DECOMPRESS_H */