4 * Functions useful for decompression, mainly bitstreams.
7 #ifndef _WIMLIB_DECOMPRESS_H
8 #define _WIMLIB_DECOMPRESS_H
10 #include "wimlib/assert.h"
11 #include "wimlib/endianness.h"
12 #include "wimlib/error.h"
13 #include "wimlib/types.h"
15 /* Must be at least 32 bits. */
16 typedef unsigned long input_bitbuf_t;
18 /* Structure to encapsulate a block of in-memory data that is being interpreted
19 * as a stream of bits.
21 * This is geared specifically towards the XPRESS and LZX compression formats
22 * with regards to the actual ordering the bits within the byte sequence. */
23 struct input_bitstream {
25 /* A variable of length at least 32 bits that is used to hold bits that
26 * have been read from the stream. The bits are ordered from high-order
27 * to low-order, and the next bit is always the high-order bit. */
28 input_bitbuf_t bitbuf;
30 /* Pointer to the next byte to be retrieved from the input. */
33 /* Number of bits in @bitbuf that are valid. */
36 /* Number of words of data that are left. */
37 unsigned data_bytes_left;
40 /* Initializes a bitstream to receive its input from @data. */
42 init_input_bitstream(struct input_bitstream *istream,
43 const void *data, unsigned num_data_bytes)
46 istream->bitsleft = 0;
48 istream->data_bytes_left = num_data_bytes;
51 /* Ensures that the bit buffer variable for the bitstream contains @num_bits
54 * If there are at least @num_bits bits remaining in the bitstream, 0 is
55 * returned. Otherwise, -1 is returned. */
57 bitstream_ensure_bits(struct input_bitstream *istream, unsigned num_bits)
59 wimlib_assert2(num_bits <= 16);
63 /* Unfortunately this needs to be different for the different
64 * compression types. LZX requires reading no more than the number of
65 * bits needed, otherwise the end of the compressed data may be overrun.
66 * XPRESS, on the other hand, requires that we always return with at
67 * least 16 bits in the buffer, even if fewer are requested. This is
68 * important because this may change the location of a literal byte
69 * read with bitstream_read_byte(). */
71 if (istream->bitsleft < 16) {
73 if (istream->bitsleft < num_bits) {
75 if (istream->data_bytes_left >= 2) {
76 unsigned shift = sizeof(input_bitbuf_t) * 8 - 16 -
78 istream->bitbuf |= (input_bitbuf_t)le16_to_cpu(
79 *(le16*)istream->data) << shift;
81 istream->bitsleft += 16;
82 istream->data_bytes_left -= 2;
87 wimlib_assert2(ret != 0 || istream->bitsleft >= num_bits);
91 /* Returns the next @num_bits bits in the buffer variable, which must contain at
92 * least @num_bits bits, for the bitstream. */
93 static inline unsigned
94 bitstream_peek_bits(const struct input_bitstream *istream, unsigned num_bits)
96 wimlib_assert2(istream->bitsleft >= num_bits);
101 ret = istream->bitbuf >> (sizeof(input_bitbuf_t) * 8 - num_bits);
105 /* Removes @num_bits bits from the buffer variable, which must contain at least
106 * @num_bits bits, for the bitstream. */
108 bitstream_remove_bits(struct input_bitstream *istream, unsigned num_bits)
110 wimlib_assert2(istream->bitsleft >= num_bits);
111 istream->bitbuf <<= num_bits;
112 istream->bitsleft -= num_bits;
115 /* Reads @num_bits bits from the input bitstream. @num_bits must be 16 or fewer.
116 * On success, returns 0 and returns the requested bits in @n. If there are
117 * fewer than @num_bits remaining in the bitstream, -1 is returned. */
119 bitstream_read_bits(struct input_bitstream *istream,
120 unsigned num_bits, unsigned *n)
122 wimlib_assert2(num_bits <= 16);
123 int ret = bitstream_ensure_bits(istream, num_bits);
125 *n = bitstream_peek_bits(istream, num_bits);
126 bitstream_remove_bits(istream, num_bits);
128 ERROR("bitstream_read_bits(): Input buffer exhausted");
133 /* In the XPRESS format there can be literal bytes embedded in the bitstream.
134 * These bytes are basically separate from the bitstream, as they come AFTER the
135 * bits that are currently in the buffer variable (based on reading 16 bits at a
136 * time), even though the buffer variable may not be empty.
138 * This function returns the next such literal byte, or -1 if there are no more.
141 bitstream_read_byte(struct input_bitstream *istream)
143 wimlib_assert2(istream->bitsleft < 32);
146 if (istream->data_bytes_left == 0) {
147 ERROR("bitstream_read_byte(): Input buffer exhausted");
150 istream->data_bytes_left--;
151 ret = *istream->data++;
156 /* Reads @num_bits bits from the buffer variable for a bistream without checking
157 * to see if that many bits are in the buffer or not. */
158 static inline unsigned
159 bitstream_read_bits_nocheck(struct input_bitstream *istream, unsigned num_bits)
161 unsigned n = bitstream_peek_bits(istream, num_bits);
162 bitstream_remove_bits(istream, num_bits);
167 read_huffsym_near_end_of_input(struct input_bitstream *istream,
168 const u16 decode_table[],
175 * Reads a Huffman-encoded symbol from a bitstream.
177 * This function may be called hundreds of millions of times when extracting a
178 * large WIM file. I'm not sure it could be made much faster that it is,
179 * especially since there isn't enough time to make a big table that allows
180 * decoding multiple symbols per lookup. But if extracting files to a hard
181 * disk, the I/O will be the bottleneck anyway.
183 * @buf: The input buffer from which the symbol will be read.
184 * @decode_table: The fast Huffman decoding table for the Huffman tree.
185 * @lengths: The table that gives the length of the code for each
187 * @num_symbols: The number of symbols in the Huffman code.
188 * @table_bits: Huffman codes this length or less can be looked up
189 * directory in the decode_table, as the
190 * decode_table contains 2**table_bits entries.
192 static _always_inline_attribute int
193 read_huffsym(struct input_bitstream *istream,
194 const u16 decode_table[],
199 unsigned max_codeword_len)
203 /* In the most common case, there are at least max_codeword_len bits
204 * remaining in the stream. */
205 if (bitstream_ensure_bits(istream, max_codeword_len) == 0) {
207 /* Use the next table_bits of the input as an index into the
209 u16 key_bits = bitstream_peek_bits(istream, table_bits);
211 u16 sym = decode_table[key_bits];
213 /* If the entry in the decode table is not a valid symbol, it is
214 * the offset of the root of its Huffman subtree. */
215 if (sym >= num_syms) {
216 bitstream_remove_bits(istream, table_bits);
218 key_bits = sym + bitstream_peek_bits(istream, 1);
219 bitstream_remove_bits(istream, 1);
221 wimlib_assert2(key_bits < num_syms * 2 +
223 } while ((sym = decode_table[key_bits]) >= num_syms);
225 wimlib_assert2(lens[sym] <= table_bits);
226 bitstream_remove_bits(istream, lens[sym]);
231 /* Otherwise, we must be careful to use only the bits that are
232 * actually remaining. */
233 ret = read_huffsym_near_end_of_input(istream, decode_table,
241 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
242 unsigned num_bits, const u8 lengths[],
243 unsigned max_codeword_len);
245 #endif /* _WIMLIB_DECOMPRESS_H */