]> wimlib.net Git - wimlib/blob - include/wimlib/decompress_common.h
3b20f4e09e74b744be139817df513d5b2f6c9372
[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 /* 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
134  * are all zeroes.  */
135 static inline u16
136 read_huffsym(struct input_bitstream * restrict istream,
137              const u16 decode_table[restrict],
138              const u8 lens[restrict],
139              unsigned num_syms,
140              unsigned table_bits,
141              unsigned max_codeword_len)
142 {
143
144         bitstream_ensure_bits(istream, max_codeword_len);
145
146         /* Use the next table_bits of the input as an index into the
147          * decode_table.  */
148         u16 key_bits = bitstream_peek_bits(istream, table_bits);
149
150         u16 sym = decode_table[key_bits];
151
152         if (likely(sym < num_syms)) {
153                 /* Fast case: The decode table directly provided the symbol.  */
154                 bitstream_remove_bits(istream, lens[sym]);
155         } else {
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);
160                 do {
161                         key_bits = sym + bitstream_pop_bits(istream, 1);
162                 } while ((sym = decode_table[key_bits]) >= num_syms);
163         }
164         return sym;
165 }
166
167 extern int
168 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
169                           unsigned num_bits, const u8 lengths[],
170                           unsigned max_codeword_len);
171
172 /* Minimum alignment for the decode_table parameter to
173  * make_huffman_decode_table().  */
174 #define DECODE_TABLE_ALIGNMENT 16
175
176 #endif /* _WIMLIB_DECOMPRESS_H */