4 * Header for decompression code shared by multiple compression formats.
7 #ifndef _WIMLIB_DECOMPRESS_COMMON_H
8 #define _WIMLIB_DECOMPRESS_COMMON_H
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"
16 #ifndef INPUT_IDX_T_DEFINED
17 #define INPUT_IDX_T_DEFINED
18 typedef u32 input_idx_t;
21 /* Structure to encapsulate a block of in-memory data that is being interpreted
22 * as a stream of bits.
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 {
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. */
33 /* Number of bits in @bitbuf that are valid. */
36 /* Pointer to the next byte to be retrieved from the input. */
39 /* Number of bytes of data that are left. */
40 input_idx_t data_bytes_left;
43 /* Initializes a bitstream to receive its input from @data. */
45 init_input_bitstream(struct input_bitstream *istream,
46 const void *data, input_idx_t num_data_bytes)
49 istream->bitsleft = 0;
51 istream->data_bytes_left = num_data_bytes;
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.
58 * If the input data is exhausted, any further bits are assumed to be 0. */
60 bitstream_ensure_bits(struct input_bitstream *istream, unsigned num_bits)
62 for (int nbits = num_bits; (int)istream->bitsleft < nbits; nbits -= 16) {
66 if (unlikely(istream->data_bytes_left < 2)) {
67 istream->bitsleft = num_bits;
71 nextword = le16_to_cpu(*(const le16*)istream->data);
72 shift = sizeof(istream->bitbuf) * 8 - 16 - istream->bitsleft;
73 istream->bitbuf |= (u32)nextword << shift;
75 istream->bitsleft += 16;
76 istream->data_bytes_left -= 2;
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(). */
84 bitstream_peek_bits(const struct input_bitstream *istream, unsigned num_bits)
86 if (unlikely(num_bits == 0))
88 return istream->bitbuf >> (sizeof(istream->bitbuf) * 8 - num_bits);
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(). */
95 bitstream_remove_bits(struct input_bitstream *istream, unsigned num_bits)
97 istream->bitbuf <<= num_bits;
98 istream->bitsleft -= num_bits;
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(). */
105 bitstream_pop_bits(struct input_bitstream *istream, unsigned num_bits)
107 u32 n = bitstream_peek_bits(istream, num_bits);
108 bitstream_remove_bits(istream, num_bits);
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. */
115 bitstream_read_bits(struct input_bitstream *istream, unsigned num_bits)
117 bitstream_ensure_bits(istream, num_bits);
118 return bitstream_pop_bits(istream, num_bits);
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. */
124 bitstream_read_byte(struct input_bitstream *istream)
126 if (unlikely(istream->data_bytes_left == 0))
128 istream->data_bytes_left--;
129 return *istream->data++;
132 /* Reads and returns the next Huffman-encoded symbol from a bitstream. If the
133 * input data is exhausted, the Huffman symbol is decoded as if the missing bits
136 read_huffsym(struct input_bitstream * restrict istream,
137 const u16 decode_table[restrict],
138 const u8 lens[restrict],
141 unsigned max_codeword_len)
144 bitstream_ensure_bits(istream, max_codeword_len);
146 /* Use the next table_bits of the input as an index into the
148 u16 key_bits = bitstream_peek_bits(istream, table_bits);
150 u16 sym = decode_table[key_bits];
152 if (likely(sym < num_syms)) {
153 /* Fast case: The decode table directly provided the symbol. */
154 bitstream_remove_bits(istream, lens[sym]);
156 /* Slow case: The symbol took too many bits to include directly
157 * in the decode table, so search for it in a binary tree at the
158 * end of the decode table. */
159 bitstream_remove_bits(istream, table_bits);
161 key_bits = sym + bitstream_pop_bits(istream, 1);
162 } while ((sym = decode_table[key_bits]) >= num_syms);
168 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
169 unsigned num_bits, const u8 lengths[],
170 unsigned max_codeword_len);
172 /* Minimum alignment for the decode_table parameter to
173 * make_huffman_decode_table(). */
174 #define DECODE_TABLE_ALIGNMENT 16
176 #endif /* _WIMLIB_DECOMPRESS_H */