]> wimlib.net Git - wimlib/blob - src/lzx-decompress.c
Compression updates
[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 #define LZX_READ_LENS_MAX_OVERRUN 50
74
75 /* Huffman decoding tables, and arrays that map symbols to codeword lengths.  */
76 struct lzx_tables {
77
78         u16 maincode_decode_table[(1 << LZX_MAINCODE_TABLEBITS) +
79                                         (LZX_MAINCODE_MAX_NUM_SYMBOLS * 2)]
80                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
81         u8 maincode_lens[LZX_MAINCODE_MAX_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
82
83
84         u16 lencode_decode_table[(1 << LZX_LENCODE_TABLEBITS) +
85                                         (LZX_LENCODE_NUM_SYMBOLS * 2)]
86                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
87         u8 lencode_lens[LZX_LENCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
88
89
90         u16 alignedcode_decode_table[(1 << LZX_ALIGNEDCODE_TABLEBITS) +
91                                         (LZX_ALIGNEDCODE_NUM_SYMBOLS * 2)]
92                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
93         u8 alignedcode_lens[LZX_ALIGNEDCODE_NUM_SYMBOLS];
94 } _aligned_attribute(DECODE_TABLE_ALIGNMENT);
95
96 /* The main LZX decompressor structure.
97  *
98  * Note: we keep track of most of the decompression state outside this
99  * structure.  This structure only exists so that (1) we can store @window_order
100  * and @num_main_syms for multiple calls to lzx_decompress(); and (2) so that we
101  * don't have to allocate the large 'struct lzx_tables' on the stack.  */
102 struct lzx_decompressor {
103         unsigned window_order;
104         unsigned num_main_syms;
105         struct lzx_tables tables;
106 };
107
108 /* Read a Huffman-encoded symbol using the precode.  */
109 static inline u16
110 read_huffsym_using_precode(struct input_bitstream *istream,
111                            const u16 precode_decode_table[])
112 {
113         return read_huffsym(istream, precode_decode_table,
114                             LZX_PRECODE_TABLEBITS, LZX_MAX_PRE_CODEWORD_LEN);
115 }
116
117 /* Read a Huffman-encoded symbol using the main code.  */
118 static inline u16
119 read_huffsym_using_maincode(struct input_bitstream *istream,
120                             const struct lzx_tables *tables)
121 {
122         return read_huffsym(istream, tables->maincode_decode_table,
123                             LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
124 }
125
126 /* Read a Huffman-encoded symbol using the length code.  */
127 static inline u16
128 read_huffsym_using_lencode(struct input_bitstream *istream,
129                            const struct lzx_tables *tables)
130 {
131         return read_huffsym(istream, tables->lencode_decode_table,
132                             LZX_LENCODE_TABLEBITS, LZX_MAX_LEN_CODEWORD_LEN);
133 }
134
135 /* Read a Huffman-encoded symbol using the aligned offset code.  */
136 static inline u16
137 read_huffsym_using_alignedcode(struct input_bitstream *istream,
138                                const struct lzx_tables *tables)
139 {
140         return read_huffsym(istream, tables->alignedcode_decode_table,
141                             LZX_ALIGNEDCODE_TABLEBITS, LZX_MAX_ALIGNED_CODEWORD_LEN);
142 }
143
144 /*
145  * Read the precode from the compressed input bitstream, then use it to decode
146  * @num_lens codeword length values.
147  *
148  * @istream:
149  *      The input bitstream.
150  *
151  * @lens:
152  *      An array that contains the length values from the previous time the
153  *      codeword lengths for this Huffman code were read, or all 0's if this is
154  *      the first time.  This array must have at least (@num_lens +
155  *      LZX_READ_LENS_MAX_OVERRUN) entries.
156  *
157  * @num_lens:
158  *      Number of length values to decode.
159  *
160  * Returns 0 on success, or -1 if the data was invalid.
161  */
162 static int
163 lzx_read_codeword_lens(struct input_bitstream *istream, u8 *lens, unsigned num_lens)
164 {
165         u16 precode_decode_table[(1 << LZX_PRECODE_TABLEBITS) +
166                                         (LZX_PRECODE_NUM_SYMBOLS * 2)]
167                                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
168         u8 precode_lens[LZX_PRECODE_NUM_SYMBOLS];
169         u8 *len_ptr = lens;
170         u8 *lens_end = lens + num_lens;
171         int ret;
172
173         /* Read the lengths of the precode codewords.  These are given
174          * explicitly.  */
175         for (int i = 0; i < LZX_PRECODE_NUM_SYMBOLS; i++) {
176                 precode_lens[i] = bitstream_read_bits(istream,
177                                                       LZX_PRECODE_ELEMENT_SIZE);
178         }
179
180         /* Make the decoding table for the precode.  */
181         ret = make_huffman_decode_table(precode_decode_table,
182                                         LZX_PRECODE_NUM_SYMBOLS,
183                                         LZX_PRECODE_TABLEBITS,
184                                         precode_lens,
185                                         LZX_MAX_PRE_CODEWORD_LEN);
186         if (ret)
187                 return ret;
188
189         /* Decode the codeword lengths.  */
190         do {
191                 unsigned presym;
192                 u8 len;
193
194                 /* Read the next precode symbol.  */
195                 presym = read_huffsym_using_precode(istream,
196                                                     precode_decode_table);
197                 if (presym < 17) {
198                         /* Difference from old length  */
199                         len = *len_ptr - presym;
200                         if ((s8)len < 0)
201                                 len += 17;
202                         *len_ptr++ = len;
203                 } else {
204                         /* Special RLE values  */
205
206                         unsigned run_len;
207
208                         if (presym == 17) {
209                                 /* Run of 0's  */
210                                 run_len = 4 + bitstream_read_bits(istream, 4);
211                                 len = 0;
212                         } else if (presym == 18) {
213                                 /* Longer run of 0's  */
214                                 run_len = 20 + bitstream_read_bits(istream, 5);
215                                 len = 0;
216                         } else {
217                                 /* Run of identical lengths  */
218                                 run_len = 4 + bitstream_read_bits(istream, 1);
219                                 presym = read_huffsym_using_precode(istream,
220                                                                     precode_decode_table);
221                                 len = *len_ptr - presym;
222                                 if ((s8)len < 0)
223                                         len += 17;
224                         }
225
226                         do {
227                                 *len_ptr++ = len;
228                         } while (--run_len);
229                         /* Worst case overrun is when presym == 18,
230                          * run_len == 20 + 31, and only 1 length was remaining.
231                          * So LZX_READ_LENS_MAX_OVERRUN == 50.
232                          *
233                          * Overrun while reading the first half of maincode_lens
234                          * can corrupt the previous values in the second half.
235                          * This doesn't really matter because the resulting
236                          * lengths will still be in range, and data that
237                          * generates overruns is invalid anyway.  */
238                 }
239         } while (len_ptr < lens_end);
240         return 0;
241 }
242
243 /*
244  * Read the header of an LZX block and save the block type and size in
245  * *block_type_ret and *block_size_ret, respectively.
246  *
247  * If the block is compressed, also update the Huffman decode @tables with the
248  * new Huffman codes.
249  *
250  * If the block is uncompressed, also update the match offset @queue with the
251  * new match offsets.
252  *
253  * Return 0 on success, or -1 if the data was invalid.
254  */
255 static int
256 lzx_read_block_header(struct input_bitstream *istream,
257                       unsigned num_main_syms,
258                       unsigned window_order,
259                       int *block_type_ret,
260                       u32 *block_size_ret,
261                       struct lzx_tables *tables,
262                       struct lzx_lru_queue *queue)
263 {
264         int block_type;
265         u32 block_size;
266         int ret;
267
268         bitstream_ensure_bits(istream, 4);
269
270         /* The first three bits tell us what kind of block it is, and should be
271          * one of the LZX_BLOCKTYPE_* values.  */
272         block_type = bitstream_pop_bits(istream, 3);
273
274         /* Read the block size.  This mirrors the behavior of
275          * lzx_write_compressed_block() in lzx-compress.c; see that for more
276          * details.  */
277         if (bitstream_pop_bits(istream, 1)) {
278                 block_size = LZX_DEFAULT_BLOCK_SIZE;
279         } else {
280                 u32 tmp;
281                 block_size = 0;
282
283                 tmp = bitstream_read_bits(istream, 8);
284                 block_size |= tmp;
285                 tmp = bitstream_read_bits(istream, 8);
286                 block_size <<= 8;
287                 block_size |= tmp;
288
289                 if (window_order >= 16) {
290                         tmp = bitstream_read_bits(istream, 8);
291                         block_size <<= 8;
292                         block_size |= tmp;
293                 }
294         }
295
296         switch (block_type) {
297
298         case LZX_BLOCKTYPE_ALIGNED:
299
300                 /* Read the aligned offset code and prepare its decode table.
301                  */
302
303                 for (int i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
304                         tables->alignedcode_lens[i] =
305                                 bitstream_read_bits(istream,
306                                                     LZX_ALIGNEDCODE_ELEMENT_SIZE);
307                 }
308
309                 ret = make_huffman_decode_table(tables->alignedcode_decode_table,
310                                                 LZX_ALIGNEDCODE_NUM_SYMBOLS,
311                                                 LZX_ALIGNEDCODE_TABLEBITS,
312                                                 tables->alignedcode_lens,
313                                                 LZX_MAX_ALIGNED_CODEWORD_LEN);
314                 if (ret)
315                         return ret;
316
317                 /* Fall though, since the rest of the header for aligned offset
318                  * blocks is the same as that for verbatim blocks.  */
319
320         case LZX_BLOCKTYPE_VERBATIM:
321
322                 /* Read the main code and prepare its decode table.
323                  *
324                  * Note that the codeword lengths in the main code are encoded
325                  * in two parts: one part for literal symbols, and one part for
326                  * match symbols.  */
327
328                 ret = lzx_read_codeword_lens(istream, tables->maincode_lens,
329                                              LZX_NUM_CHARS);
330                 if (ret)
331                         return ret;
332
333                 ret = lzx_read_codeword_lens(istream,
334                                              tables->maincode_lens + LZX_NUM_CHARS,
335                                              num_main_syms - LZX_NUM_CHARS);
336                 if (ret)
337                         return ret;
338
339                 ret = make_huffman_decode_table(tables->maincode_decode_table,
340                                                 num_main_syms,
341                                                 LZX_MAINCODE_TABLEBITS,
342                                                 tables->maincode_lens,
343                                                 LZX_MAX_MAIN_CODEWORD_LEN);
344                 if (ret)
345                         return ret;
346
347                 /* Read the length code and prepare its decode table.  */
348
349                 ret = lzx_read_codeword_lens(istream, tables->lencode_lens,
350                                              LZX_LENCODE_NUM_SYMBOLS);
351                 if (ret)
352                         return ret;
353
354                 ret = make_huffman_decode_table(tables->lencode_decode_table,
355                                                 LZX_LENCODE_NUM_SYMBOLS,
356                                                 LZX_LENCODE_TABLEBITS,
357                                                 tables->lencode_lens,
358                                                 LZX_MAX_LEN_CODEWORD_LEN);
359                 if (ret)
360                         return ret;
361
362                 break;
363
364         case LZX_BLOCKTYPE_UNCOMPRESSED:
365
366                 /* Before reading the three LRU match offsets from the
367                  * uncompressed block header, the stream must be aligned on a
368                  * 16-bit boundary.  But, unexpectedly, if the stream is
369                  * *already* aligned, the correct thing to do is to throw away
370                  * the next 16 bits.  */
371
372                 bitstream_ensure_bits(istream, 1);
373                 bitstream_align(istream);
374                 queue->R[0] = bitstream_read_u32(istream);
375                 queue->R[1] = bitstream_read_u32(istream);
376                 queue->R[2] = bitstream_read_u32(istream);
377
378                 /* Offsets of 0 are invalid.  */
379                 if (queue->R[0] == 0 || queue->R[1] == 0 || queue->R[2] == 0)
380                         return -1;
381                 break;
382
383         default:
384                 /* Unrecognized block type.  */
385                 return -1;
386         }
387
388         *block_type_ret = block_type;
389         *block_size_ret = block_size;
390         return 0;
391 }
392
393 /*
394  * Decompress an LZX-compressed block of data.
395  *
396  * @block_type:
397  *      The type of the block (LZX_BLOCKTYPE_VERBATIM or LZX_BLOCKTYPE_ALIGNED).
398  *
399  * @block_size:
400  *      The size of the block, in bytes.
401  *
402  * @window:
403  *      Pointer to the beginning of the decompression window.
404  *
405  * @window_pos:
406  *      The position in the window at which the block starts.
407  *
408  * @tables:
409  *      The Huffman decoding tables for the block.
410  *
411  * @queue:
412  *      The least-recently-used queue for match offsets.
413  *
414  * @istream:
415  *      The input bitstream, positioned at the start of the block data.
416  *
417  * Returns 0 on success, or -1 if the data was invalid.
418  */
419 static int
420 lzx_decompress_block(int block_type, u32 block_size,
421                      u8 *window, u32 window_pos,
422                      const struct lzx_tables *tables,
423                      struct lzx_lru_queue *queue,
424                      struct input_bitstream *istream)
425 {
426         u8 *window_ptr = &window[window_pos];
427         u8 *window_end = window_ptr + block_size;
428         unsigned mainsym;
429         u32 match_len;
430         unsigned offset_slot;
431         u32 match_offset;
432         unsigned num_extra_bits;
433         unsigned ones_if_aligned = 0U - (block_type == LZX_BLOCKTYPE_ALIGNED);
434
435         while (window_ptr != window_end) {
436
437                 mainsym = read_huffsym_using_maincode(istream, tables);
438                 if (mainsym < LZX_NUM_CHARS) {
439                         /* Literal  */
440                         *window_ptr++ = mainsym;
441                         continue;
442                 }
443
444                 /* Match  */
445
446                 /* Decode the length header and offset slot.  */
447                 mainsym -= LZX_NUM_CHARS;
448                 match_len = mainsym & 0x7;
449                 offset_slot = mainsym >> 3;
450
451                 /* If needed, read a length symbol to decode the full length. */
452                 if (match_len == 0x7)
453                         match_len += read_huffsym_using_lencode(istream, tables);
454                 match_len += LZX_MIN_MATCH_LEN;
455
456                 if (offset_slot <= 2) {
457                         /* Repeat offset  */
458
459                         /* Note: This isn't a real LRU queue, since using the R2
460                          * offset doesn't bump the R1 offset down to R2.  This
461                          * quirk allows all 3 recent offsets to be handled by
462                          * the same code.  (For R0, the swap is a no-op.)  */
463                         match_offset = queue->R[offset_slot];
464                         queue->R[offset_slot] = queue->R[0];
465                         queue->R[0] = match_offset;
466                 } else {
467                         /* Explicit offset  */
468
469                         /* Look up the number of extra bits that need to be read
470                          * to decode offsets with this offset slot.  */
471                         num_extra_bits = lzx_extra_offset_bits[offset_slot];
472
473                         /* Start with the offset slot base value.  */
474                         match_offset = lzx_offset_slot_base[offset_slot];
475
476                         /* In aligned offset blocks, the low-order 3 bits of
477                          * each offset are encoded using the aligned offset
478                          * code.  Otherwise, all the extra bits are literal.  */
479
480                         /*if (block_type == LZX_BLOCKTYPE_ALIGNED && num_extra_bits >= 3) {*/
481                         if ((num_extra_bits & ones_if_aligned) >= 3) {
482                                 match_offset += bitstream_read_bits(istream, num_extra_bits - 3) << 3;
483                                 match_offset += read_huffsym_using_alignedcode(istream, tables);
484                         } else {
485                                 match_offset += bitstream_read_bits(istream, num_extra_bits);
486                         }
487
488                         /* Adjust the offset.  */
489                         match_offset -= LZX_OFFSET_OFFSET;
490
491                         /* Update the match offset LRU queue.  */
492                         queue->R[2] = queue->R[1];
493                         queue->R[1] = queue->R[0];
494                         queue->R[0] = match_offset;
495                 }
496
497                 /* Validate the match, then copy it to the current position.  */
498
499                 if (unlikely(match_len > window_end - window_ptr))
500                         return -1;
501
502                 if (unlikely(match_offset > window_ptr - window))
503                         return -1;
504
505                 lz_copy(window_ptr, match_len, match_offset, window_end);
506
507                 window_ptr += match_len;
508         }
509         return 0;
510 }
511
512 static int
513 lzx_decompress(const void *compressed_data, size_t compressed_size,
514                void *uncompressed_data, size_t uncompressed_size,
515                void *_dec)
516 {
517         struct lzx_decompressor *dec = _dec;
518         struct input_bitstream istream;
519         struct lzx_lru_queue queue;
520         u32 window_pos;
521         int block_type;
522         u32 block_size;
523         bool may_have_e8_byte;
524         int ret;
525
526         init_input_bitstream(&istream, compressed_data, compressed_size);
527
528         /* Initialize the recent offsets queue.  */
529         lzx_lru_queue_init(&queue);
530
531         /* Codeword lengths begin as all 0's for delta encoding purposes.  */
532         memset(dec->tables.maincode_lens, 0, dec->num_main_syms);
533         memset(dec->tables.lencode_lens, 0, LZX_LENCODE_NUM_SYMBOLS);
534
535         /* Set this to true if there may be 0xe8 bytes in the uncompressed data.
536          */
537         may_have_e8_byte = false;
538
539         /* The compressed data will consist of one or more blocks.  The
540          * following loop decompresses one block, and it runs until there all
541          * the compressed data has been decompressed, so there are no more
542          * blocks.  */
543
544         for (window_pos = 0;
545              window_pos < uncompressed_size;
546              window_pos += block_size)
547         {
548                 ret = lzx_read_block_header(&istream, dec->num_main_syms,
549                                             dec->window_order, &block_type,
550                                             &block_size, &dec->tables, &queue);
551                 if (ret)
552                         return ret;
553
554                 if (block_size > uncompressed_size - window_pos)
555                         return -1;
556
557                 if (block_type != LZX_BLOCKTYPE_UNCOMPRESSED) {
558
559                         /* Compressed block.  */
560
561                         ret = lzx_decompress_block(block_type,
562                                                    block_size,
563                                                    uncompressed_data,
564                                                    window_pos,
565                                                    &dec->tables,
566                                                    &queue,
567                                                    &istream);
568                         if (ret)
569                                 return ret;
570
571                         /* If the first 0xe8 byte was in this block, it must
572                          * have been encoded as a literal using mainsym 0xe8. */
573                         if (dec->tables.maincode_lens[0xe8] != 0)
574                                 may_have_e8_byte = true;
575                 } else {
576
577                         /* Uncompressed block.  */
578                         const u8 *p;
579
580                         p = bitstream_read_bytes(&istream, block_size);
581                         if (!p)
582                                 return -1;
583
584                         memcpy(&((u8*)uncompressed_data)[window_pos], p, block_size);
585
586                         /* Re-align the bitstream if an odd number of bytes was
587                          * read.  */
588                         if (block_size & 1)
589                                 bitstream_read_byte(&istream);
590
591                         may_have_e8_byte = true;
592                 }
593         }
594
595         /* Postprocess the data unless it cannot possibly contain 0xe8 bytes  */
596         if (may_have_e8_byte)
597                 lzx_undo_e8_preprocessing(uncompressed_data, uncompressed_size);
598
599         return 0;
600 }
601
602 static void
603 lzx_free_decompressor(void *_dec)
604 {
605         struct lzx_decompressor *dec = _dec;
606
607         ALIGNED_FREE(dec);
608 }
609
610 static int
611 lzx_create_decompressor(size_t max_block_size, void **dec_ret)
612 {
613         struct lzx_decompressor *dec;
614         unsigned window_order;
615
616         window_order = lzx_get_window_order(max_block_size);
617         if (window_order == 0)
618                 return WIMLIB_ERR_INVALID_PARAM;
619
620         /* The aligned allocation is needed to ensure that the lzx_tables are
621          * aligned properly.  */
622         dec = ALIGNED_MALLOC(sizeof(struct lzx_decompressor),
623                              DECODE_TABLE_ALIGNMENT);
624         if (!dec)
625                 return WIMLIB_ERR_NOMEM;
626
627         dec->window_order = window_order;
628         dec->num_main_syms = lzx_get_num_main_syms(window_order);
629
630         *dec_ret = dec;
631         return 0;
632 }
633
634 const struct decompressor_ops lzx_decompressor_ops = {
635         .create_decompressor = lzx_create_decompressor,
636         .decompress          = lzx_decompress,
637         .free_decompressor   = lzx_free_decompressor,
638 };