LZX: Allow max_block_size not a power of 2
[wimlib] / src / lzx-decompress.c
1 /*
2  * lzx-decompress.c
3  *
4  * A very fast decompressor for LZX, as used in WIM files.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013, 2014 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 /*
27  * LZX is an LZ77 and Huffman-code based compression format that has many
28  * similarities to DEFLATE (the format used by zlib/gzip).  The compression
29  * ratio is as good or better than DEFLATE.  See lzx-compress.c for a format
30  * overview, and see https://en.wikipedia.org/wiki/LZX_(algorithm) for a
31  * historical overview.  Here I make some pragmatic notes.
32  *
33  * The old specification for LZX is the document "Microsoft LZX Data Compression
34  * Format" (1997).  It defines the LZX format as used in cabinet files.  Allowed
35  * window sizes are 2^n where 15 <= n <= 21.  However, this document contains
36  * several errors, so don't read too much into it...
37  *
38  * The new specification for LZX is the document "[MS-PATCH]: LZX DELTA
39  * Compression and Decompression" (2014).  It defines the LZX format as used by
40  * Microsoft's binary patcher.  It corrects several errors in the 1997 document
41  * and extends the format in several ways --- namely, optional reference data,
42  * up to 2^25 byte windows, and longer match lengths.
43  *
44  * WIM files use a more restricted form of LZX.  No LZX DELTA extensions are
45  * present, the window is not "sliding", E8 preprocessing is done
46  * unconditionally with a fixed file size, and the maximum window size is always
47  * 2^15 bytes (equal to the size of each "chunk" in a compressed WIM resource).
48  * This code is primarily intended to implement this form of LZX.  But although
49  * not compatible with WIMGAPI, this code also supports maximum window sizes up
50  * to 2^21 bytes.
51  *
52  * TODO: Add support for window sizes up to 2^25 bytes.
53  */
54
55 #ifdef HAVE_CONFIG_H
56 #  include "config.h"
57 #endif
58
59 #include "wimlib/decompressor_ops.h"
60 #include "wimlib/decompress_common.h"
61 #include "wimlib/error.h"
62 #include "wimlib/lzx.h"
63 #include "wimlib/util.h"
64
65 #include <string.h>
66
67 /* These values are chosen for fast decompression.  */
68 #define LZX_MAINCODE_TABLEBITS          11
69 #define LZX_LENCODE_TABLEBITS           10
70 #define LZX_PRECODE_TABLEBITS           6
71 #define LZX_ALIGNEDCODE_TABLEBITS       7
72
73 /* Huffman decoding tables, and arrays that map symbols to codeword lengths.  */
74 struct lzx_tables {
75
76         u16 maincode_decode_table[(1 << LZX_MAINCODE_TABLEBITS) +
77                                         (LZX_MAINCODE_MAX_NUM_SYMBOLS * 2)]
78                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
79         u8 maincode_lens[LZX_MAINCODE_MAX_NUM_SYMBOLS];
80
81
82         u16 lencode_decode_table[(1 << LZX_LENCODE_TABLEBITS) +
83                                         (LZX_LENCODE_NUM_SYMBOLS * 2)]
84                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
85         u8 lencode_lens[LZX_LENCODE_NUM_SYMBOLS];
86
87
88         u16 alignedcode_decode_table[(1 << LZX_ALIGNEDCODE_TABLEBITS) +
89                                         (LZX_ALIGNEDCODE_NUM_SYMBOLS * 2)]
90                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
91         u8 alignedcode_lens[LZX_ALIGNEDCODE_NUM_SYMBOLS];
92 } _aligned_attribute(DECODE_TABLE_ALIGNMENT);
93
94 /* The main LZX decompressor structure.
95  *
96  * Note: we keep track of most of the decompression state outside this
97  * structure.  This structure only exists so that (1) we can store @window_order
98  * and @num_main_syms for multiple calls to lzx_decompress(); and (2) so that we
99  * don't have to allocate the large 'struct lzx_tables' on the stack.  */
100 struct lzx_decompressor {
101         unsigned window_order;
102         unsigned num_main_syms;
103         struct lzx_tables tables;
104 };
105
106 /* Read a Huffman-encoded symbol using the precode.  */
107 static inline u16
108 read_huffsym_using_precode(struct input_bitstream *istream,
109                            const u16 precode_decode_table[])
110 {
111         return read_huffsym(istream, precode_decode_table,
112                             LZX_PRECODE_TABLEBITS, LZX_MAX_PRE_CODEWORD_LEN);
113 }
114
115 /* Read a Huffman-encoded symbol using the main code.  */
116 static inline u16
117 read_huffsym_using_maincode(struct input_bitstream *istream,
118                             const struct lzx_tables *tables)
119 {
120         return read_huffsym(istream, tables->maincode_decode_table,
121                             LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
122 }
123
124 /* Read a Huffman-encoded symbol using the length code.  */
125 static inline u16
126 read_huffsym_using_lencode(struct input_bitstream *istream,
127                            const struct lzx_tables *tables)
128 {
129         return read_huffsym(istream, tables->lencode_decode_table,
130                             LZX_LENCODE_TABLEBITS, LZX_MAX_LEN_CODEWORD_LEN);
131 }
132
133 /* Read a Huffman-encoded symbol using the aligned offset code.  */
134 static inline u16
135 read_huffsym_using_alignedcode(struct input_bitstream *istream,
136                                const struct lzx_tables *tables)
137 {
138         return read_huffsym(istream, tables->alignedcode_decode_table,
139                             LZX_ALIGNEDCODE_TABLEBITS, LZX_MAX_ALIGNED_CODEWORD_LEN);
140 }
141
142 /*
143  * Read the precode from the compressed input bitstream, then use it to decode
144  * @num_lens codeword length values.
145  *
146  * @istream:
147  *      The input bitstream.
148  *
149  * @lens:
150  *      An array that contains the length values from the previous time the
151  *      codeword lengths for this Huffman code were read, or all 0's if this is
152  *      the first time.
153  *
154  * @num_lens:
155  *      Number of length values to decode.
156  *
157  * Returns 0 on success, or -1 if the data was invalid.
158  */
159 static int
160 lzx_read_codeword_lens(struct input_bitstream *istream, u8 lens[], unsigned num_lens)
161 {
162         /* Declare the decoding table and length table for the precode.  */
163         u16 precode_decode_table[(1 << LZX_PRECODE_TABLEBITS) +
164                                         (LZX_PRECODE_NUM_SYMBOLS * 2)]
165                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
166         u8 precode_lens[LZX_PRECODE_NUM_SYMBOLS];
167         int ret;
168
169         /* Read the lengths of the precode codewords.  These are given
170          * explicitly.  */
171         for (int i = 0; i < LZX_PRECODE_NUM_SYMBOLS; i++) {
172                 precode_lens[i] = bitstream_read_bits(istream,
173                                                       LZX_PRECODE_ELEMENT_SIZE);
174         }
175
176         /* Make the decoding table for the precode.  */
177         ret = make_huffman_decode_table(precode_decode_table,
178                                         LZX_PRECODE_NUM_SYMBOLS,
179                                         LZX_PRECODE_TABLEBITS,
180                                         precode_lens,
181                                         LZX_MAX_PRE_CODEWORD_LEN);
182         if (ret)
183                 return ret;
184
185         /* Pointer past the last length value that needs to be filled in.  */
186         u8 *lens_end = lens + num_lens;
187
188         for (;;) {
189
190                 unsigned presym;
191                 unsigned run_len;
192                 signed char value;
193
194                 /* Decode a symbol from the input.
195                  *
196                  * If the symbol is between 0 and 16, it is the difference from
197                  * the old length, modulo 17.
198                  *
199                  * If the symbol is between 17 and 19, it is a special symbol
200                  * that indicates that some number of the next lengths are all
201                  * 0, or that some number of the next lengths are all equal to
202                  * the next symbol.  */
203
204                 presym = read_huffsym_using_precode(istream,
205                                                     precode_decode_table);
206                 switch (presym) {
207
208                 case 17: /* Run of 0's  */
209                         run_len = 4 + bitstream_read_bits(istream, 4);
210                         do {
211                                 *lens = 0;
212                                 if (++lens == lens_end)
213                                         return 0;
214                         } while (--run_len);
215                         break;
216
217                 case 18: /* Longer run of 0's  */
218                         run_len = 20 + bitstream_read_bits(istream, 5);
219                         do {
220                                 *lens = 0;
221                                 if (++lens == lens_end)
222                                         return 0;
223                         } while (--run_len);
224                         break;
225
226                 case 19: /* Run of identical lengths  */
227                         run_len = 4 + bitstream_read_bits(istream, 1);
228                         presym = read_huffsym_using_precode(istream,
229                                                             precode_decode_table);
230                         value = (signed char)*lens - (signed char)presym;
231                         if (value < 0)
232                                 value += 17;
233                         do {
234                                 *lens = value;
235                                 if (++lens == lens_end)
236                                         return 0;
237                         } while (--run_len);
238                         break;
239
240                 default: /* Difference from old length  */
241                         value = (signed char)*lens - (signed char)presym;
242                         if (value < 0)
243                                 value += 17;
244                         *lens = value;
245                         if (++lens == lens_end)
246                                 return 0;
247                         break;
248                 }
249         }
250 }
251
252 /*
253  * Read the header of an LZX block and save the block type and size in
254  * *block_type_ret and *block_size_ret, respectively.
255  *
256  * If the block is compressed, also update the Huffman decode @tables with the
257  * new Huffman codes.
258  *
259  * If the block is uncompressed, also update the match offset @queue with the
260  * new match offsets.
261  *
262  * Return 0 on success, or -1 if the data was invalid.
263  */
264 static int
265 lzx_read_block_header(struct input_bitstream *istream,
266                       unsigned num_main_syms,
267                       unsigned window_order,
268                       int *block_type_ret,
269                       u32 *block_size_ret,
270                       struct lzx_tables *tables,
271                       struct lzx_lru_queue *queue)
272 {
273         int block_type;
274         u32 block_size;
275         int ret;
276
277         bitstream_ensure_bits(istream, 4);
278
279         /* The first three bits tell us what kind of block it is, and should be
280          * one of the LZX_BLOCKTYPE_* values.  */
281         block_type = bitstream_pop_bits(istream, 3);
282
283         /* Read the block size.  This mirrors the behavior of
284          * lzx_write_compressed_block() in lzx-compress.c; see that for more
285          * details.  */
286         if (bitstream_pop_bits(istream, 1)) {
287                 block_size = LZX_DEFAULT_BLOCK_SIZE;
288         } else {
289                 u32 tmp;
290                 block_size = 0;
291
292                 tmp = bitstream_read_bits(istream, 8);
293                 block_size |= tmp;
294                 tmp = bitstream_read_bits(istream, 8);
295                 block_size <<= 8;
296                 block_size |= tmp;
297
298                 if (window_order >= 16) {
299                         tmp = bitstream_read_bits(istream, 8);
300                         block_size <<= 8;
301                         block_size |= tmp;
302                 }
303         }
304
305         switch (block_type) {
306
307         case LZX_BLOCKTYPE_ALIGNED:
308
309                 /* Read the aligned offset code and prepare its decode table.
310                  */
311
312                 for (int i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
313                         tables->alignedcode_lens[i] =
314                                 bitstream_read_bits(istream,
315                                                     LZX_ALIGNEDCODE_ELEMENT_SIZE);
316                 }
317
318                 ret = make_huffman_decode_table(tables->alignedcode_decode_table,
319                                                 LZX_ALIGNEDCODE_NUM_SYMBOLS,
320                                                 LZX_ALIGNEDCODE_TABLEBITS,
321                                                 tables->alignedcode_lens,
322                                                 LZX_MAX_ALIGNED_CODEWORD_LEN);
323                 if (ret)
324                         return ret;
325
326                 /* Fall though, since the rest of the header for aligned offset
327                  * blocks is the same as that for verbatim blocks.  */
328
329         case LZX_BLOCKTYPE_VERBATIM:
330
331                 /* Read the main code and prepare its decode table.
332                  *
333                  * Note that the codeword lengths in the main code are encoded
334                  * in two parts: one part for literal symbols, and one part for
335                  * match symbols.  */
336
337                 ret = lzx_read_codeword_lens(istream, tables->maincode_lens,
338                                              LZX_NUM_CHARS);
339                 if (ret)
340                         return ret;
341
342                 ret = lzx_read_codeword_lens(istream,
343                                              tables->maincode_lens + LZX_NUM_CHARS,
344                                              num_main_syms - LZX_NUM_CHARS);
345                 if (ret)
346                         return ret;
347
348                 ret = make_huffman_decode_table(tables->maincode_decode_table,
349                                                 num_main_syms,
350                                                 LZX_MAINCODE_TABLEBITS,
351                                                 tables->maincode_lens,
352                                                 LZX_MAX_MAIN_CODEWORD_LEN);
353                 if (ret)
354                         return ret;
355
356                 /* Read the length code and prepare its decode table.  */
357
358                 ret = lzx_read_codeword_lens(istream, tables->lencode_lens,
359                                              LZX_LENCODE_NUM_SYMBOLS);
360                 if (ret)
361                         return ret;
362
363                 ret = make_huffman_decode_table(tables->lencode_decode_table,
364                                                 LZX_LENCODE_NUM_SYMBOLS,
365                                                 LZX_LENCODE_TABLEBITS,
366                                                 tables->lencode_lens,
367                                                 LZX_MAX_LEN_CODEWORD_LEN);
368                 if (ret)
369                         return ret;
370
371                 break;
372
373         case LZX_BLOCKTYPE_UNCOMPRESSED:
374
375                 /* Before reading the three LRU match offsets from the
376                  * uncompressed block header, the stream must be aligned on a
377                  * 16-bit boundary.  But, unexpectedly, if the stream is
378                  * *already* aligned, the correct thing to do is to throw away
379                  * the next 16 bits.  */
380
381                 if (istream->bitsleft == 0) {
382                         if (istream->data_bytes_left < 14)
383                                 return -1;
384                         istream->data += 2;
385                         istream->data_bytes_left -= 2;
386                 } else {
387                         if (istream->data_bytes_left < 12)
388                                 return -1;
389                         istream->bitsleft = 0;
390                         istream->bitbuf = 0;
391                 }
392                 queue->R[0] = le32_to_cpu(*(le32*)(istream->data + 0));
393                 queue->R[1] = le32_to_cpu(*(le32*)(istream->data + 4));
394                 queue->R[2] = le32_to_cpu(*(le32*)(istream->data + 8));
395                 istream->data += 12;
396                 istream->data_bytes_left -= 12;
397                 break;
398
399         default:
400                 /* Unrecognized block type.  */
401                 return -1;
402         }
403
404         *block_type_ret = block_type;
405         *block_size_ret = block_size;
406         return 0;
407 }
408
409 /*
410  * Decode a match and copy its bytes into the decompression window.
411  *
412  * Return the length of the match in bytes, or 0 if the match underflowed the
413  * window or overflowed the current block.
414  */
415 static u32
416 lzx_decode_match(unsigned main_symbol, int block_type,
417                  u32 bytes_remaining, u8 *window, u32 window_pos,
418                  const struct lzx_tables *tables,
419                  struct lzx_lru_queue *queue,
420                  struct input_bitstream *istream)
421 {
422         unsigned length_header;
423         unsigned position_slot;
424         u32 match_len;
425         u32 match_offset;
426         unsigned num_extra_bits;
427         u32 verbatim_bits;
428         u32 aligned_bits;
429
430         /* The main symbol is offset by 256 because values under 256 indicate a
431          * literal value.  */
432         main_symbol -= LZX_NUM_CHARS;
433
434         /* The length header consists of the lower 3 bits of the main element.
435          * The position slot is the rest of it. */
436         length_header = main_symbol & LZX_NUM_PRIMARY_LENS;
437         position_slot = main_symbol >> 3;
438
439         /* If the length_header is less than LZX_NUM_PRIMARY_LENS (= 7), it
440          * gives the match length as the offset from LZX_MIN_MATCH_LEN.
441          * Otherwise, the length is given by an additional symbol encoded using
442          * the length code, offset by 9 (LZX_MIN_MATCH_LEN +
443          * LZX_NUM_PRIMARY_LENS) */
444         match_len = LZX_MIN_MATCH_LEN + length_header;
445         if (length_header == LZX_NUM_PRIMARY_LENS)
446                 match_len += read_huffsym_using_lencode(istream, tables);
447
448         /* If the position_slot is 0, 1, or 2, the match offset is retrieved
449          * from the LRU queue.  Otherwise, the match offset is not in the LRU
450          * queue. */
451         if (position_slot <= 2) {
452                 /* Note: This isn't a real LRU queue, since using the R2 offset
453                  * doesn't bump the R1 offset down to R2.  This quirk allows all
454                  * 3 recent offsets to be handled by the same code.  (For R0,
455                  * the swap is a no-op.)  */
456                 match_offset = queue->R[position_slot];
457                 queue->R[position_slot] = queue->R[0];
458                 queue->R[0] = match_offset;
459         } else {
460                 /* Otherwise, the offset was not encoded as one the offsets in
461                  * the queue.  Depending on the position slot, there is a
462                  * certain number of extra bits that need to be read to fully
463                  * decode the match offset. */
464
465                 /* Look up the number of extra bits that need to be read. */
466                 num_extra_bits = lzx_get_num_extra_bits(position_slot);
467
468                 /* For aligned blocks, if there are at least 3 extra bits, the
469                  * actual number of extra bits is 3 less, and they encode a
470                  * number of 8-byte words that are added to the offset; there
471                  * is then an additional symbol read using the aligned offset
472                  * code that specifies the actual byte alignment. */
473                 if (block_type == LZX_BLOCKTYPE_ALIGNED && num_extra_bits >= 3) {
474
475                         /* There is an error in the LZX "specification" at this
476                          * point; it indicates that a Huffman symbol is to be
477                          * read only if num_extra_bits is greater than 3, but
478                          * actually it is if num_extra_bits is greater than or
479                          * equal to 3.  (Note that in the case with
480                          * num_extra_bits == 3, the assignment to verbatim_bits
481                          * will just set it to 0. ) */
482                         verbatim_bits = bitstream_read_bits(istream,
483                                                             num_extra_bits - 3);
484                         verbatim_bits <<= 3;
485                         aligned_bits = read_huffsym_using_alignedcode(istream,
486                                                                       tables);
487                 } else {
488                         /* For non-aligned blocks, or for aligned blocks with
489                          * less than 3 extra bits, the extra bits are added
490                          * directly to the match offset, and the correction for
491                          * the alignment is taken to be 0. */
492                         verbatim_bits = bitstream_read_bits(istream, num_extra_bits);
493                         aligned_bits = 0;
494                 }
495
496                 /* Calculate the match offset. */
497                 match_offset = lzx_position_base[position_slot] +
498                                verbatim_bits + aligned_bits - LZX_OFFSET_OFFSET;
499
500                 /* Update the LRU queue. */
501                 queue->R[2] = queue->R[1];
502                 queue->R[1] = queue->R[0];
503                 queue->R[0] = match_offset;
504         }
505
506         /* Validate the match, then copy it to the current position.  */
507
508         if (unlikely(match_len > bytes_remaining))
509                 return 0;
510
511         if (unlikely(match_offset > window_pos))
512                 return 0;
513
514         lz_copy(&window[window_pos], match_len, match_offset,
515                 &window[window_pos + bytes_remaining]);
516
517         return match_len;
518 }
519
520 /*
521  * Decompress an LZX-compressed block of data.
522  *
523  * @block_type:
524  *      The type of the block (LZX_BLOCKTYPE_VERBATIM or LZX_BLOCKTYPE_ALIGNED).
525  *
526  * @block_size:
527  *      The size of the block, in bytes.
528  *
529  * @window:
530  *      Pointer to the beginning of the decompression window.
531  *
532  * @window_pos:
533  *      The position in the window at which the block starts.
534  *
535  * @tables:
536  *      The Huffman decoding tables for the block.
537  *
538  * @queue:
539  *      The least-recently-used queue for match offsets.
540  *
541  * @istream:
542  *      The input bitstream, positioned at the start of the block data.
543  *
544  * Returns 0 on success, or -1 if the data was invalid.
545  */
546 static int
547 lzx_decompress_block(int block_type, u32 block_size,
548                      u8 *window, u32 window_pos,
549                      const struct lzx_tables *tables,
550                      struct lzx_lru_queue *queue,
551                      struct input_bitstream *istream)
552 {
553         u32 block_end;
554         unsigned main_symbol;
555         u32 match_len;
556
557         block_end = window_pos + block_size;
558         while (window_pos < block_end) {
559                 main_symbol = read_huffsym_using_maincode(istream, tables);
560                 if (main_symbol < LZX_NUM_CHARS) {
561                         /* Literal  */
562                         window[window_pos++] = main_symbol;
563                 } else {
564                         /* Match  */
565                         match_len = lzx_decode_match(main_symbol,
566                                                      block_type,
567                                                      block_end - window_pos,
568                                                      window,
569                                                      window_pos,
570                                                      tables,
571                                                      queue,
572                                                      istream);
573                         if (unlikely(match_len == 0))
574                                 return -1;
575                         window_pos += match_len;
576                 }
577         }
578         return 0;
579 }
580
581 static int
582 lzx_decompress(const void *compressed_data, size_t compressed_size,
583                void *uncompressed_data, size_t uncompressed_size,
584                void *_dec)
585 {
586         struct lzx_decompressor *dec = _dec;
587         struct input_bitstream istream;
588         struct lzx_lru_queue queue;
589         u32 window_pos;
590         int block_type;
591         u32 block_size;
592         bool may_have_e8_byte;
593         int ret;
594
595         init_input_bitstream(&istream, compressed_data, compressed_size);
596
597         /* Initialize the recent offsets queue.  */
598         lzx_lru_queue_init(&queue);
599
600         /* Codeword lengths begin as all 0's for delta encoding purposes.  */
601         memset(dec->tables.maincode_lens, 0, sizeof(dec->tables.maincode_lens));
602         memset(dec->tables.lencode_lens, 0, sizeof(dec->tables.lencode_lens));
603
604         /* Set this to true if there may be 0xe8 bytes in the uncompressed data.
605          */
606         may_have_e8_byte = false;
607
608         /* The compressed data will consist of one or more blocks.  The
609          * following loop decompresses one block, and it runs until there all
610          * the compressed data has been decompressed, so there are no more
611          * blocks.  */
612
613         for (window_pos = 0;
614              window_pos < uncompressed_size;
615              window_pos += block_size)
616         {
617                 ret = lzx_read_block_header(&istream, dec->num_main_syms,
618                                             dec->window_order, &block_type,
619                                             &block_size, &dec->tables, &queue);
620                 if (ret)
621                         return ret;
622
623                 if (block_size > uncompressed_size - window_pos)
624                         return -1;
625
626                 if (block_type != LZX_BLOCKTYPE_UNCOMPRESSED) {
627
628                         /* Compressed block.  */
629
630                         ret = lzx_decompress_block(block_type,
631                                                    block_size,
632                                                    uncompressed_data,
633                                                    window_pos,
634                                                    &dec->tables,
635                                                    &queue,
636                                                    &istream);
637                         if (ret)
638                                 return ret;
639
640                         /* If the first 0xe8 byte was in this block, it must
641                          * have been encoded as a literal using mainsym 0xe8. */
642                         if (dec->tables.maincode_lens[0xe8] != 0)
643                                 may_have_e8_byte = true;
644                 } else {
645
646                         /* Uncompressed block.  */
647
648                         if (istream.data_bytes_left < block_size)
649                                 return -1;
650
651                         memcpy(&((u8*)uncompressed_data)[window_pos], istream.data,
652                                block_size);
653                         istream.data += block_size;
654                         istream.data_bytes_left -= block_size;
655
656                         /* Re-align the bitstream if an odd number of bytes was
657                          * read.  */
658                         if (istream.data_bytes_left && (block_size & 1)) {
659                                 istream.data_bytes_left--;
660                                 istream.data++;
661                         }
662                         may_have_e8_byte = true;
663                 }
664         }
665
666         /* Postprocess the data unless it cannot possibly contain 0xe8 bytes  */
667         if (may_have_e8_byte)
668                 lzx_undo_e8_preprocessing(uncompressed_data, uncompressed_size);
669
670         return 0;
671 }
672
673 static void
674 lzx_free_decompressor(void *_dec)
675 {
676         struct lzx_decompressor *dec = _dec;
677
678         ALIGNED_FREE(dec);
679 }
680
681 static int
682 lzx_create_decompressor(size_t max_block_size, void **dec_ret)
683 {
684         struct lzx_decompressor *dec;
685         unsigned window_order;
686
687         window_order = lzx_get_window_order(max_block_size);
688         if (window_order == 0)
689                 return WIMLIB_ERR_INVALID_PARAM;
690
691         /* The aligned allocation is needed to ensure that the lzx_tables are
692          * aligned properly.  */
693         dec = ALIGNED_MALLOC(sizeof(struct lzx_decompressor),
694                              DECODE_TABLE_ALIGNMENT);
695         if (!dec)
696                 return WIMLIB_ERR_NOMEM;
697
698         dec->window_order = window_order;
699         dec->num_main_syms = lzx_get_num_main_syms(window_order);
700
701         *dec_ret = dec;
702         return 0;
703 }
704
705 const struct decompressor_ops lzx_decompressor_ops = {
706         .create_decompressor = lzx_create_decompressor,
707         .decompress          = lzx_decompress,
708         .free_decompressor   = lzx_free_decompressor,
709 };