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++;
133 /* Needed alignment of decode_table parameter to make_huffman_decode_table().
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
139 /* Maximum supported symbol count for make_huffman_decode_table().
141 * Reason: In direct mapping entries, we store the symbol in 11 bits. */
142 #define DECODE_TABLE_MAX_SYMBOLS 2048
144 /* Maximum supported table bits for make_huffman_decode_table().
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
153 /* Maximum supported codeword length for make_huffman_decode_table().
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
158 #define DECODE_TABLE_MAX_CODEWORD_LEN 23
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
164 * XXX: This is mostly duplicated in lzms_huffman_decode_symbol() in
165 * lzms-decompress.c. */
167 read_huffsym(struct input_bitstream *istream, const u16 decode_table[],
168 unsigned num_syms, unsigned table_bits, unsigned max_codeword_len)
173 bitstream_ensure_bits(istream, max_codeword_len);
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;
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);
192 key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
193 } while ((entry = decode_table[key_bits]) >= 0xC000);
199 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
200 unsigned num_bits, const u8 lens[],
201 unsigned max_codeword_len);
203 #endif /* _WIMLIB_DECOMPRESS_H */