]> wimlib.net Git - wimlib/blob - include/wimlib/decompress_common.h
Get rid of input_idx_t
[wimlib] / include / wimlib / decompress_common.h
1 /*
2  * decompress_common.h
3  *
4  * Header for decompression code shared by multiple compression formats.
5  *
6  * The author dedicates this file to the public domain.
7  * You can do whatever you want with this file.
8  */
9
10 #ifndef _WIMLIB_DECOMPRESS_COMMON_H
11 #define _WIMLIB_DECOMPRESS_COMMON_H
12
13 #include "wimlib/assert.h"
14 #include "wimlib/compiler.h"
15 #include "wimlib/endianness.h"
16 #include "wimlib/types.h"
17
18 /* Structure to encapsulate a block of in-memory data that is being interpreted
19  * as a stream of bits.
20  *
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 {
24
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         u32 bitbuf;
29
30         /* Number of bits in @bitbuf that are valid.  */
31         unsigned bitsleft;
32
33         /* Pointer to the next byte to be retrieved from the input.  */
34         const u8 *data;
35
36         /* Number of bytes of data that are left.  */
37         u32 data_bytes_left;
38 };
39
40 /* Initializes a bitstream to receive its input from @data. */
41 static inline void
42 init_input_bitstream(struct input_bitstream *istream,
43                      const void *data, u32 num_data_bytes)
44 {
45         istream->bitbuf          = 0;
46         istream->bitsleft        = 0;
47         istream->data            = data;
48         istream->data_bytes_left = num_data_bytes;
49 }
50
51 /* Ensures the bit buffer variable for the bitstream contains at least @num_bits
52  * bits.  Following this, bitstream_peek_bits() and/or bitstream_remove_bits()
53  * may be called on the bitstream to peek or remove up to @num_bits bits.
54  *
55  * If the input data is exhausted, any further bits are assumed to be 0.  */
56 static inline void
57 bitstream_ensure_bits(struct input_bitstream *istream, unsigned num_bits)
58 {
59         for (int nbits = num_bits; (int)istream->bitsleft < nbits; nbits -= 16) {
60                 u16 nextword;
61                 unsigned shift;
62
63                 if (unlikely(istream->data_bytes_left < 2)) {
64                         istream->bitsleft = num_bits;
65                         return;
66                 }
67
68                 nextword = le16_to_cpu(*(const le16*)istream->data);
69                 shift = sizeof(istream->bitbuf) * 8 - 16 - istream->bitsleft;
70                 istream->bitbuf |= (u32)nextword << shift;
71                 istream->data += 2;
72                 istream->bitsleft += 16;
73                 istream->data_bytes_left -= 2;
74         }
75 }
76
77 /* Returns the next @num_bits bits from the bitstream, without removing them.
78  * There must be at least @num_bits remaining in the buffer variable, from a
79  * previous call to bitstream_ensure_bits().  */
80 static inline u32
81 bitstream_peek_bits(const struct input_bitstream *istream, unsigned num_bits)
82 {
83         if (unlikely(num_bits == 0))
84                 return 0;
85         return istream->bitbuf >> (sizeof(istream->bitbuf) * 8 - num_bits);
86 }
87
88 /* Removes @num_bits from the bitstream.  There must be at least @num_bits
89  * remaining in the buffer variable, from a previous call to
90  * bitstream_ensure_bits().  */
91 static inline void
92 bitstream_remove_bits(struct input_bitstream *istream, unsigned num_bits)
93 {
94         istream->bitbuf <<= num_bits;
95         istream->bitsleft -= num_bits;
96 }
97
98 /* Removes and returns @num_bits bits from the bitstream.  There must be at
99  * least @num_bits remaining in the buffer variable, from a previous call to
100  * bitstream_ensure_bits().  */
101 static inline u32
102 bitstream_pop_bits(struct input_bitstream *istream, unsigned num_bits)
103 {
104         u32 n = bitstream_peek_bits(istream, num_bits);
105         bitstream_remove_bits(istream, num_bits);
106         return n;
107 }
108
109 /* Reads and returns the next @num_bits bits from the bitstream.
110  * If the input data is exhausted, the bits are assumed to be 0.  */
111 static inline u32
112 bitstream_read_bits(struct input_bitstream *istream, unsigned num_bits)
113 {
114         bitstream_ensure_bits(istream, num_bits);
115         return bitstream_pop_bits(istream, num_bits);
116 }
117
118 /* Reads and returns the next literal byte embedded in the bitstream.
119  * If the input data is exhausted, the byte is assumed to be 0.  */
120 static inline u8
121 bitstream_read_byte(struct input_bitstream *istream)
122 {
123         if (unlikely(istream->data_bytes_left == 0))
124                 return 0;
125         istream->data_bytes_left--;
126         return *istream->data++;
127 }
128
129
130 /* Needed alignment of decode_table parameter to make_huffman_decode_table().
131  *
132  * Reason: We may fill the entries with SSE instructions without worrying
133  * about dealing with the unaligned case.  */
134 #define DECODE_TABLE_ALIGNMENT 16
135
136 /* Maximum supported symbol count for make_huffman_decode_table().
137  *
138  * Reason: In direct mapping entries, we store the symbol in 11 bits.  */
139 #define DECODE_TABLE_MAX_SYMBOLS 2048
140
141 /* Maximum supported table bits for make_huffman_decode_table().
142  *
143  * Reason: In internal binary tree nodes, offsets are encoded in 14 bits.
144  * But the real limit is 13, because we allocate entries past the end of
145  * the direct lookup part of the table for binary tree nodes.  (Note: if
146  * needed this limit could be removed by encoding the offsets relative to
147  * &decode_table[1 << table_bits].)  */
148 #define DECODE_TABLE_MAX_TABLE_BITS 13
149
150 /* Maximum supported codeword length for make_huffman_decode_table().
151  *
152  * Reason: In direct mapping entries, we encode the codeword length in 5
153  * bits, and the top 2 bits can't both be set because that has special
154  * meaning.  */
155 #define DECODE_TABLE_MAX_CODEWORD_LEN 23
156
157 /* Reads and returns the next Huffman-encoded symbol from a bitstream.  If the
158  * input data is exhausted, the Huffman symbol is decoded as if the missing bits
159  * are all zeroes.
160  *
161  * XXX: This is mostly duplicated in lzms_huffman_decode_symbol() in
162  * lzms-decompress.c.  */
163 static inline u16
164 read_huffsym(struct input_bitstream *istream, const u16 decode_table[],
165              unsigned table_bits, unsigned max_codeword_len)
166 {
167         u16 entry;
168         u16 key_bits;
169
170         bitstream_ensure_bits(istream, max_codeword_len);
171
172         /* Index the decode table by the next table_bits bits of the input.  */
173         key_bits = bitstream_peek_bits(istream, table_bits);
174         entry = decode_table[key_bits];
175         if (likely(entry < 0xC000)) {
176                 /* Fast case: The decode table directly provided the
177                  * symbol and codeword length.  The low 11 bits are the
178                  * symbol, and the high 5 bits are the codeword length.  */
179                 bitstream_remove_bits(istream, entry >> 11);
180                 return entry & 0x7FF;
181         } else {
182                 /* Slow case: The codeword for the symbol is longer than
183                  * table_bits, so the symbol does not have an entry
184                  * directly in the first (1 << table_bits) entries of the
185                  * decode table.  Traverse the appropriate binary tree
186                  * bit-by-bit to decode the symbol.  */
187                 bitstream_remove_bits(istream, table_bits);
188                 do {
189                         key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
190                 } while ((entry = decode_table[key_bits]) >= 0xC000);
191                 return entry;
192         }
193 }
194
195 extern int
196 make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
197                           unsigned num_bits, const u8 lens[],
198                           unsigned max_codeword_len);
199
200
201 /*
202  * Copy a LZ77 match at (dst - offset) to dst.
203  *
204  * The length and offset must be already validated --- that is, (dst - offset)
205  * can't underrun the output buffer, and (dst + length) can't overrun the output
206  * buffer.  Also, the length cannot be 0.
207  *
208  * @winend points to the byte past the end of the output buffer.
209  * This function won't write any data beyond this position.
210  */
211 static inline void
212 lz_copy(u8 *dst, unsigned length, unsigned offset, const u8 *winend)
213 {
214         const u8 *src = dst - offset;
215 #if defined(__x86_64__) || defined(__i386__)
216         /* Copy one 'unsigned long' at a time.  On i386 and x86_64 this is
217          * faster than copying one byte at a time, unless the data is
218          * near-random and all the matches have very short lengths.  Note that
219          * since this requires unaligned memory accesses, it won't necessarily
220          * be faster on every architecture.
221          *
222          * Also note that we might copy more than the length of the match.  For
223          * example, if an 'unsigned long' is 8 bytes and the match is of length
224          * 5, then we'll simply copy 8 bytes.  This is okay as long as we don't
225          * write beyond the end of the output buffer, hence the check for
226          * (winend - (dst + length) >= sizeof(unsigned long) - 1).  */
227         if (offset >= sizeof(unsigned long) &&
228                         winend - (dst + length) >= sizeof(unsigned long) - 1)
229         {
230                 /* Access memory through a packed struct.  This tricks the
231                  * compiler into allowing unaligned memory accesses.  */
232                 struct ulong_wrapper {
233                         unsigned long v;
234                 } _packed_attribute;
235
236                 const u8 *end = dst + length;
237                 do {
238                         unsigned long v = ((struct ulong_wrapper *)src)->v;
239                         ((struct ulong_wrapper *)dst)->v = v;
240                         dst += sizeof(unsigned long);
241                         src += sizeof(unsigned long);
242                 } while (dst < end);
243
244                 return;
245         }
246 #endif
247         do {
248                 *dst++ = *src++;
249         } while (--length);
250 }
251
252 #endif /* _WIMLIB_DECOMPRESS_COMMON_H */