4 * LZX decompression routines, originally based on code taken from cabextract
5 * v0.5, which was, itself, a modified version of the lzx decompression code
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 * LZX is a LZ77 and Huffman-code based compression format that has many
30 * similarities to the DEFLATE format used in zlib. The compression ratio is as
31 * good or better than DEFLATE. However, in WIM files only up to 32768 bytes of
32 * data can ever compressed be in the same LZX block, so a .tar.gz file could
33 * potentially be smaller than a WIM file that uses LZX compression because it
34 * can use a larger LZ77 window size.
36 * Some notes on the LZX compression format as used in Windows Imaging (WIM)
39 * A compressed WIM resource consists of a table of chunk offsets followed by
40 * the compressed chunks themselves. All compressed chunks except possibly the
41 * last decompress to WIM_CHUNK_SIZE (= 32768) bytes. This is quite similar to
42 * the cabinet (.cab) file format, but they are not the same. According to the
43 * cabinet format documentation, the LZX block size is independent from the
44 * CFDATA blocks, and a LZX block may span several CFDATA blocks. However, in
45 * WIMs, LZX blocks do not appear to ever span multiple WIM chunks. Note that
46 * this means any WIM chunk may be decompressed or compressed independently from
47 * any other chunk, which is convenient.
49 * A LZX compressed WIM chunk contains one or more LZX blocks of the aligned,
50 * verbatim, or uncompressed block types. For aligned and verbatim blocks, the
51 * size of the block in uncompressed bytes is specified by a bit following the 3
52 * bits that specify the block type, possibly followed by an additional 16 bits.
53 * '1' means to use the default block size (equal to 32768, the size of a WIM
54 * chunk--- and this seems to only be valid for the first LZX block in a WIM
55 * chunk), while '0' means that the block size is provided by the next 16 bits.
57 * The cabinet format, as documented, allows for the possibility that a
58 * compressed CFDATA chunk is up to 6144 bytes larger than the data it
59 * uncompresses to. However, in the WIM format it appears that every chunk that
60 * would be 32768 bytes or more when compressed is actually stored fully
63 * The 'e8' preprocessing step that changes x86 call instructions to use
64 * absolute offsets instead of relative offsets relies on a filesize parameter.
65 * There is no such parameter for this in the WIM files (even though the size of
66 * the file resource could be used for this purpose), and instead a magic file
67 * size of 12000000 is used. The 'e8' preprocessing is always done, and there
68 * is no bit to indicate whether it is done or not.
72 * Some more notes about errors in Microsoft's LZX documentation:
74 * Microsoft's LZX document and their implementation of the com.ms.util.cab Java
75 * package do not concur.
77 * In the LZX document, there is a table showing the correlation between window
78 * size and the number of position slots. It states that the 1MB window = 40
79 * slots and the 2MB window = 42 slots. In the implementation, 1MB = 42 slots,
80 * 2MB = 50 slots. The actual calculation is 'find the first slot whose position
81 * base is equal to or more than the required window size'. This would explain
82 * why other tables in the document refer to 50 slots rather than 42.
84 * The constant NUM_PRIMARY_LENS used in the decompression pseudocode is not
85 * defined in the specification.
87 * The LZX document states that aligned offset blocks have their aligned offset
88 * huffman tree AFTER the main and length trees. The implementation suggests
89 * that the aligned offset tree is BEFORE the main and length trees.
91 * The LZX document decoding algorithm states that, in an aligned offset block,
92 * if an extra_bits value is 1, 2 or 3, then that number of bits should be read
93 * and the result added to the match offset. This is correct for 1 and 2, but
94 * not 3, where just a huffman symbol (using the aligned tree) should be read.
96 * Regarding the E8 preprocessing, the LZX document states 'No translation may
97 * be performed on the last 6 bytes of the input block'. This is correct.
98 * However, the pseudocode provided checks for the *E8 leader* up to the last 6
99 * bytes. If the leader appears between -10 and -7 bytes from the end, this
100 * would cause the next four bytes to be modified, at least one of which would
101 * be in the last 6 bytes, which is not allowed according to the spec.
103 * The specification states that the huffman trees must always contain at least
104 * one element. However, many CAB files contain blocks where the length tree is
105 * completely empty (because there are no matches), and this is expected to
111 #include "decompress.h"
114 /* Huffman decoding tables and maps from symbols to code lengths. */
117 u16 maintree_decode_table[(1 << LZX_MAINTREE_TABLEBITS) +
118 (LZX_MAINTREE_NUM_SYMBOLS * 2)];
119 u8 maintree_lens[LZX_MAINTREE_NUM_SYMBOLS];
122 u16 lentree_decode_table[(1 << LZX_LENTREE_TABLEBITS) +
123 (LZX_LENTREE_NUM_SYMBOLS * 2)];
124 u8 lentree_lens[LZX_LENTREE_NUM_SYMBOLS];
127 u16 alignedtree_decode_table[(1 << LZX_ALIGNEDTREE_TABLEBITS) +
128 (LZX_ALIGNEDTREE_NUM_SYMBOLS * 2)];
129 u8 alignedtree_lens[LZX_ALIGNEDTREE_NUM_SYMBOLS];
134 * Reads a Huffman-encoded symbol using the pre-tree.
136 static inline int read_huffsym_using_pretree(struct input_bitstream *istream,
137 const u16 pretree_decode_table[],
138 const u8 pretree_lens[], unsigned *n)
140 return read_huffsym(istream, pretree_decode_table, pretree_lens,
141 LZX_PRETREE_NUM_SYMBOLS, LZX_PRETREE_TABLEBITS, n,
142 LZX_MAX_CODEWORD_LEN);
145 /* Reads a Huffman-encoded symbol using the main tree. */
146 static inline int read_huffsym_using_maintree(struct input_bitstream *istream,
147 const struct lzx_tables *tables,
150 return read_huffsym(istream, tables->maintree_decode_table,
151 tables->maintree_lens, LZX_MAINTREE_NUM_SYMBOLS,
152 LZX_MAINTREE_TABLEBITS, n, LZX_MAX_CODEWORD_LEN);
155 /* Reads a Huffman-encoded symbol using the length tree. */
156 static inline int read_huffsym_using_lentree(struct input_bitstream *istream,
157 const struct lzx_tables *tables,
160 return read_huffsym(istream, tables->lentree_decode_table,
161 tables->lentree_lens, LZX_LENTREE_NUM_SYMBOLS,
162 LZX_LENTREE_TABLEBITS, n, LZX_MAX_CODEWORD_LEN);
165 /* Reads a Huffman-encoded symbol using the aligned offset tree. */
166 static inline int read_huffsym_using_alignedtree(struct input_bitstream *istream,
167 const struct lzx_tables *tables,
170 return read_huffsym(istream, tables->alignedtree_decode_table,
171 tables->alignedtree_lens,
172 LZX_ALIGNEDTREE_NUM_SYMBOLS,
173 LZX_ALIGNEDTREE_TABLEBITS, n, 8);
177 * Reads the pretree from the input, then uses the pretree to decode @num_lens
178 * code length values from the input.
180 * @istream: The bit stream for the input. It is positioned on the beginning
181 * of the pretree for the code length values.
182 * @lens: An array that contains the length values from the previous time
183 * the code lengths for this Huffman tree were read, or all
184 * 0's if this is the first time.
185 * @num_lens: Number of length values to decode and return.
188 static int lzx_read_code_lens(struct input_bitstream *istream, u8 lens[],
191 /* Declare the decoding table and length table for the pretree. */
192 u16 pretree_decode_table[(1 << LZX_PRETREE_TABLEBITS) +
193 (LZX_PRETREE_NUM_SYMBOLS * 2)];
194 u8 pretree_lens[LZX_PRETREE_NUM_SYMBOLS];
199 /* Read the code lengths of the pretree codes. There are 20 lengths of
201 for (i = 0; i < LZX_PRETREE_NUM_SYMBOLS; i++) {
202 ret = bitstream_read_bits(istream, LZX_PRETREE_ELEMENT_SIZE,
206 pretree_lens[i] = len;
209 /* Make the decoding table for the pretree. */
210 ret = make_huffman_decode_table(pretree_decode_table,
211 LZX_PRETREE_NUM_SYMBOLS,
212 LZX_PRETREE_TABLEBITS,
214 LZX_MAX_CODEWORD_LEN);
218 /* Pointer past the last length value that needs to be filled in. */
219 u8 *lens_end = lens + num_lens;
223 /* Decode a symbol from the input. If the symbol is between 0
224 * and 16, it is the difference from the old length. If it is
225 * between 17 and 19, it is a special code that indicates that
226 * some number of the next lengths are all 0, or some number of
227 * the next lengths are all equal to the next symbol in the
235 ret = read_huffsym_using_pretree(istream, pretree_decode_table,
236 pretree_lens, &tree_code);
240 case 17: /* Run of 0's */
241 ret = bitstream_read_bits(istream, 4, &num_zeroes);
245 while (num_zeroes--) {
247 if (++lens == lens_end)
251 case 18: /* Longer run of 0's */
252 ret = bitstream_read_bits(istream, 5, &num_zeroes);
256 while (num_zeroes--) {
258 if (++lens == lens_end)
262 case 19: /* Run of identical lengths */
263 ret = bitstream_read_bits(istream, 1, &num_same);
267 ret = read_huffsym_using_pretree(istream,
268 pretree_decode_table,
273 value = (char)*lens - (char)code;
278 if (++lens == lens_end)
282 default: /* Difference from old length. */
283 value = (char)*lens - (char)tree_code;
287 if (++lens == lens_end)
295 * Reads the header for an LZX-compressed block.
297 * @istream: The input bitstream.
298 * @block_size_ret: A pointer to an int into which the size of the block,
299 * in bytes, will be returned.
300 * @block_type_ret: A pointer to an int into which the type of the block
301 * (LZX_BLOCKTYPE_*) will be returned.
302 * @tables: A pointer to a lzx_tables structure in which the
303 * main tree, the length tree, and possibly the
304 * aligned offset tree will be constructed.
305 * @queue: A pointer to the least-recently-used queue into which
306 * R0, R1, and R2 will be written (only for uncompressed
307 * blocks, which contain this information in the header)
309 static int lzx_read_block_header(struct input_bitstream *istream,
310 unsigned *block_size_ret,
311 unsigned *block_type_ret,
312 struct lzx_tables *tables,
313 struct lru_queue *queue)
323 ret = bitstream_ensure_bits(istream, 4);
325 ERROR("LZX input stream overrun");
329 /* The first three bits tell us what kind of block it is, and are one
330 * of the LZX_BLOCKTYPE_* values. */
331 block_type = bitstream_read_bits_nocheck(istream, 3);
333 /* The next bit indicates whether the block size is the default (32768),
334 * indicated by a 1 bit, or whether the block size is given by the next
335 * 16 bits, indicated by a 0 bit. */
336 s = bitstream_read_bits_nocheck(istream, 1);
341 ret = bitstream_read_bits(istream, 16, &block_size);
344 block_size = le16_to_cpu(block_size);
347 switch (block_type) {
348 case LZX_BLOCKTYPE_ALIGNED:
349 /* Read the path lengths for the elements of the aligned tree,
352 for (i = 0; i < LZX_ALIGNEDTREE_NUM_SYMBOLS; i++) {
353 ret = bitstream_read_bits(istream,
354 LZX_ALIGNEDTREE_ELEMENT_SIZE,
358 tables->alignedtree_lens[i] = len;
361 LZX_DEBUG("Building the aligned tree.");
362 ret = make_huffman_decode_table(tables->alignedtree_decode_table,
363 LZX_ALIGNEDTREE_NUM_SYMBOLS,
364 LZX_ALIGNEDTREE_TABLEBITS,
365 tables->alignedtree_lens,
368 ERROR("lzx_decompress(): Failed to make the decode "
369 "table for the aligned offset tree");
373 /* Fall though, since the rest of the header for aligned offset
374 * blocks is the same as that for verbatim blocks */
376 case LZX_BLOCKTYPE_VERBATIM:
377 if (block_type == LZX_BLOCKTYPE_VERBATIM)
378 LZX_DEBUG("Found verbatim block.");
380 LZX_DEBUG("Reading path lengths for main tree.");
381 /* Read the path lengths for the first 256 elements of the main
383 ret = lzx_read_code_lens(istream, tables->maintree_lens,
386 ERROR("lzx_decompress(): Failed to read the code "
387 "lengths for the first 256 elements of the "
392 /* Read the path lengths for the remaining elements of the main
394 LZX_DEBUG("Reading path lengths for remaining elements of "
395 "main tree (%d elements).",
396 LZX_MAINTREE_NUM_SYMBOLS - LZX_NUM_CHARS);
397 ret = lzx_read_code_lens(istream,
398 tables->maintree_lens + LZX_NUM_CHARS,
399 LZX_MAINTREE_NUM_SYMBOLS - LZX_NUM_CHARS);
401 ERROR("lzx_decompress(): Failed to read the path "
402 "lengths for the remaining elements of the main "
407 LZX_DEBUG("Building the Huffman decoding "
408 "table for the main tree.");
410 ret = make_huffman_decode_table(tables->maintree_decode_table,
411 LZX_MAINTREE_NUM_SYMBOLS,
412 LZX_MAINTREE_TABLEBITS,
413 tables->maintree_lens,
414 LZX_MAX_CODEWORD_LEN);
416 ERROR("lzx_decompress(): Failed to make the decode "
417 "table for the main tree");
421 LZX_DEBUG("Reading path lengths for the length tree.");
422 ret = lzx_read_code_lens(istream, tables->lentree_lens,
423 LZX_LENTREE_NUM_SYMBOLS);
425 ERROR("lzx_decompress(): Failed to read the path "
426 "lengths for the length tree");
430 LZX_DEBUG("Building the length tree.");
431 ret = make_huffman_decode_table(tables->lentree_decode_table,
432 LZX_LENTREE_NUM_SYMBOLS,
433 LZX_LENTREE_TABLEBITS,
434 tables->lentree_lens,
435 LZX_MAX_CODEWORD_LEN);
437 ERROR("lzx_decompress(): Failed to build the length "
441 /* The bitstream of compressed literals and matches for this
442 * block directly follows and will be read in
443 * lzx_decompress_block(). */
445 case LZX_BLOCKTYPE_UNCOMPRESSED:
446 LZX_DEBUG("Found uncompressed block.");
447 /* Before reading the three LRU match offsets from the
448 * uncompressed block header, the stream needs to be aligned on
449 * a 16-bit boundary. But, unexpectedly, if the stream is
450 * *already* aligned, the correct thing to do is to throw away
451 * the next 16 bits. */
452 if (istream->bitsleft == 0) {
453 if (istream->data_bytes_left < 14)
456 istream->data_bytes_left -= 2;
458 if (istream->data_bytes_left < 12)
460 istream->bitsleft = 0;
463 queue->R0 = le32_to_cpu(*(u32*)(istream->data + 0));
464 queue->R1 = le32_to_cpu(*(u32*)(istream->data + 4));
465 queue->R2 = le32_to_cpu(*(u32*)(istream->data + 8));
467 istream->data_bytes_left -= 12;
468 /* The uncompressed data of this block directly follows and will
469 * be read in lzx_decompress(). */
472 ERROR("lzx_decompress(): Found invalid block");
475 *block_type_ret = block_type;
476 *block_size_ret = block_size;
481 * Decodes a compressed match from a block of LZX-compressed data. A match
482 * refers to some match_offset to a point earlier in the window as well as some
483 * match_len, for which the data is to be copied to the current position in the
486 * @main_element: The start of the match data, as decoded using the main
489 * @block_type: The type of the block (LZX_BLOCKTYPE_ALIGNED or
490 * LZX_BLOCKTYPE_VERBATIM)
492 * @bytes_remaining: The amount of uncompressed data remaining to be
493 * uncompressed in this block. It is an error if the match
494 * is longer than this number.
496 * @window: A pointer to the window into which the uncompressed
497 * data is being written.
499 * @window_pos: The current byte offset in the window.
501 * @tables: The Huffman decoding tables for this LZX block (main
502 * code, length code, and for LZX_BLOCKTYPE_ALIGNED blocks,
503 * also the aligned offset code).
505 * @queue: The least-recently used queue for match offsets.
507 * @istream: The input bitstream.
509 * Returns the length of the match, or a negative number on error. The possible
511 * - Match would exceed the amount of data remaining to be uncompressed.
512 * - Match refers to data before the window.
513 * - The input bitstream ended unexpectedly.
515 static int lzx_decode_match(unsigned main_element, int block_type,
516 unsigned bytes_remaining, u8 *window,
518 const struct lzx_tables *tables,
519 struct lru_queue *queue,
520 struct input_bitstream *istream)
522 unsigned length_header;
523 unsigned position_slot;
525 unsigned match_offset;
526 unsigned additional_len;
527 unsigned num_extra_bits;
528 unsigned verbatim_bits;
529 unsigned aligned_bits;
535 /* The main element is offset by 256 because values under 256 indicate a
537 main_element -= LZX_NUM_CHARS;
539 /* The length header consists of the lower 3 bits of the main element.
540 * The position slot is the rest of it. */
541 length_header = main_element & LZX_NUM_PRIMARY_LENS;
542 position_slot = main_element >> 3;
544 /* If the length_header is less than LZX_NUM_PRIMARY_LENS (= 7), it
545 * gives the match length as the offset from LZX_MIN_MATCH. Otherwise,
546 * the length is given by an additional symbol encoded using the length
547 * tree, offset by 9 (LZX_MIN_MATCH + LZX_NUM_PRIMARY_LENS) */
548 match_len = LZX_MIN_MATCH + length_header;
549 if (length_header == LZX_NUM_PRIMARY_LENS) {
550 ret = read_huffsym_using_lentree(istream, tables,
554 match_len += additional_len;
558 /* If the position_slot is 0, 1, or 2, the match offset is retrieved
559 * from the LRU queue. Otherwise, the match offset is not in the LRU
561 switch (position_slot) {
563 match_offset = queue->R0;
566 match_offset = queue->R1;
567 swap(queue->R0, queue->R1);
570 /* The queue doesn't work quite the same as a real LRU queue,
571 * since using the R2 offset doesn't bump the R1 offset down to
573 match_offset = queue->R2;
574 swap(queue->R0, queue->R2);
577 /* Otherwise, the offset was not encoded as one the offsets in
578 * the queue. Depending on the position slot, there is a
579 * certain number of extra bits that need to be read to fully
580 * decode the match offset. */
582 /* Look up the number of extra bits that need to be read. */
583 num_extra_bits = lzx_get_num_extra_bits(position_slot);
585 /* For aligned blocks, if there are at least 3 extra bits, the
586 * actual number of extra bits is 3 less, and they encode a
587 * number of 8-byte words that are added to the offset; there
588 * is then an additional symbol read using the aligned tree that
589 * specifies the actual byte alignment. */
590 if (block_type == LZX_BLOCKTYPE_ALIGNED && num_extra_bits >= 3) {
592 /* There is an error in the LZX "specification" at this
593 * point; it indicates that a Huffman symbol is to be
594 * read only if num_extra_bits is greater than 3, but
595 * actually it is if num_extra_bits is greater than or
596 * equal to 3. (Note that in the case with
597 * num_extra_bits == 3, the assignment to verbatim_bits
598 * will just set it to 0. ) */
599 ret = bitstream_read_bits(istream, num_extra_bits - 3,
606 ret = read_huffsym_using_alignedtree(istream, tables,
611 /* For non-aligned blocks, or for aligned blocks with
612 * less than 3 extra bits, the extra bits are added
613 * directly to the match offset, and the correction for
614 * the alignment is taken to be 0. */
615 ret = bitstream_read_bits(istream, num_extra_bits,
623 /* Calculate the match offset. */
624 match_offset = lzx_position_base[position_slot] +
625 verbatim_bits + aligned_bits - 2;
627 /* Update the LRU queue. */
628 queue->R2 = queue->R1;
629 queue->R1 = queue->R0;
630 queue->R0 = match_offset;
634 /* Verify that the match is in the bounds of the part of the window
635 * currently in use, then copy the source of the match to the current
637 match_dest = window + window_pos;
638 match_src = match_dest - match_offset;
640 if (match_len > bytes_remaining) {
641 ERROR("lzx_decode_match(): Match of length %u bytes overflows "
642 "uncompressed block size", match_len);
646 if (match_src < window) {
647 ERROR("lzx_decode_match(): Match of length %u bytes references "
648 "data before window (match_offset = %u, window_pos = %u)",
649 match_len, match_offset, window_pos);
654 printf("Match: src %u, dst %u, len %u\n", match_src - window,
658 for (i = 0; i < match_len; i++) {
659 match_dest[i] = match_src[i];
660 putchar(match_src[i]);
665 for (i = 0; i < match_len; i++)
666 match_dest[i] = match_src[i];
672 static void undo_call_insn_translation(u32 *call_insn_target, int input_pos,
678 abs_offset = le32_to_cpu(*call_insn_target);
679 if (abs_offset >= -input_pos && abs_offset < file_size) {
680 if (abs_offset >= 0) {
681 /* "good translation" */
682 rel_offset = abs_offset - input_pos;
684 /* "compensating translation" */
685 rel_offset = abs_offset + file_size;
687 *call_insn_target = cpu_to_le32(rel_offset);
691 /* Undo the 'E8' preprocessing, where the targets of x86 CALL instructions were
692 * changed from relative offsets to absolute offsets.
694 * Note that this call instruction preprocessing can and will be used on any
695 * data even if it is not actually x86 machine code. In fact, this type of
696 * preprocessing appears to always be used in LZX-compressed resources in WIM
697 * files; there is no bit to indicate whether it is used or not, unlike in the
698 * LZX compressed format as used in cabinet files, where a bit is reserved for
701 * Call instruction preprocessing is disabled in the last 6 bytes of the
702 * uncompressed data, which really means the 5-byte call instruction cannot
703 * start in the last 10 bytes of the uncompressed data. This is one of the
704 * errors in the LZX documentation.
706 * Call instruction preprocessing does not appear to be disabled after the
707 * 32768th chunk of a WIM stream, which is apparently is yet another difference
708 * from the LZX compression used in cabinet files.
710 * Call instruction processing is supposed to take the file size as a parameter,
711 * as it is used in calculating the translated jump targets. But in WIM files,
712 * this file size is always the same (LZX_WIM_MAGIC_FILESIZE == 12000000).*/
713 static void undo_call_insn_preprocessing(u8 uncompressed_data[],
714 int uncompressed_data_len)
716 for (int i = 0; i < uncompressed_data_len - 10; i++) {
717 if (uncompressed_data[i] == 0xe8) {
718 undo_call_insn_translation((u32*)&uncompressed_data[i + 1],
720 LZX_WIM_MAGIC_FILESIZE);
727 * Decompresses a LZX-compressed block of data from which the header has already
730 * @block_type: The type of the block (LZX_BLOCKTYPE_VERBATIM or
731 * LZX_BLOCKTYPE_ALIGNED)
732 * @block_size: The size of the block, in bytes.
733 * @window: Pointer to the decompression window.
734 * @window_pos: The current position in the window. Will be 0 for the first
736 * @tables: The Huffman decoding tables for the block (main, length, and
737 * aligned offset, the latter only for LZX_BLOCKTYPE_ALIGNED)
738 * @queue: The least-recently-used queue for match offsets.
739 * @istream: The input bitstream for the compressed literals.
741 static int lzx_decompress_block(int block_type, unsigned block_size,
744 const struct lzx_tables *tables,
745 struct lru_queue *queue,
746 struct input_bitstream *istream)
748 unsigned main_element;
753 end = window_pos + block_size;
754 while (window_pos < end) {
755 ret = read_huffsym_using_maintree(istream, tables,
760 if (main_element < LZX_NUM_CHARS) {
761 /* literal: 0 to LZX_NUM_CHARS - 1 */
762 window[window_pos++] = main_element;
764 /* match: LZX_NUM_CHARS to LZX_MAINTREE_NUM_SYMBOLS - 1 */
765 ret = lzx_decode_match(main_element,
782 * Decompresses a block of LZX-compressed data as used in the WIM file format.
784 * Note that this will NOT work unmodified for LZX as used in the cabinet
785 * format, which is not the same as in the WIM format!
787 * @compressed_data: A pointer to the compressed data.
789 * @compressed_len: The length of the compressed data, in bytes.
791 * @uncompressed_data: A pointer to the buffer into which to write the
794 * @uncompressed_len: The length of the uncompressed data. It must be
795 * 32768 bytes or less.
797 * Return 0 on success; non-zero on failure.
799 int lzx_decompress(const void *compressed_data, unsigned compressed_len,
800 void *uncompressed_data, unsigned uncompressed_len)
802 struct lzx_tables tables;
803 struct input_bitstream istream;
804 struct lru_queue queue;
809 bool e8_preprocessing_done;
811 LZX_DEBUG("lzx_decompress (compressed_data = %p, compressed_len = %d, "
812 "uncompressed_data = %p, uncompressed_len = %d).",
813 compressed_data, compressed_len,
814 uncompressed_data, uncompressed_len);
816 wimlib_assert(uncompressed_len <= 32768);
818 memset(tables.maintree_lens, 0, sizeof(tables.maintree_lens));
819 memset(tables.lentree_lens, 0, sizeof(tables.lentree_lens));
823 init_input_bitstream(&istream, compressed_data, compressed_len);
825 e8_preprocessing_done = false; /* Set to true if there may be 0xe8 bytes
826 in the uncompressed data. */
828 /* The compressed data will consist of one or more blocks. The
829 * following loop decompresses one block, and it runs until there all
830 * the compressed data has been decompressed, so there are no more
834 window_pos < uncompressed_len;
835 window_pos += block_size)
837 LZX_DEBUG("Reading block header.");
838 ret = lzx_read_block_header(&istream, &block_size,
839 &block_type, &tables, &queue);
843 LZX_DEBUG("block_size = %u, window_pos = %u",
844 block_size, window_pos);
846 if (block_size > uncompressed_len - window_pos) {
847 ERROR("lzx_decompress(): Expected a block size of at "
848 "most %u bytes (found %u bytes)",
849 uncompressed_len - window_pos, block_size);
853 switch (block_type) {
854 case LZX_BLOCKTYPE_VERBATIM:
855 case LZX_BLOCKTYPE_ALIGNED:
856 if (block_type == LZX_BLOCKTYPE_VERBATIM)
857 LZX_DEBUG("LZX_BLOCKTYPE_VERBATIM");
859 LZX_DEBUG("LZX_BLOCKTYPE_ALIGNED");
860 ret = lzx_decompress_block(block_type,
869 if (tables.maintree_lens[0xe8] != 0)
870 e8_preprocessing_done = true;
872 case LZX_BLOCKTYPE_UNCOMPRESSED:
873 LZX_DEBUG("LZX_BLOCKTYPE_UNCOMPRESSED");
874 if (istream.data_bytes_left < block_size) {
875 ERROR("Unexpected end of input when "
876 "reading %zu bytes from LZX bitstream "
877 "(only have %u bytes left)",
878 block_size, istream.data_bytes_left);
881 memcpy(&((u8*)uncompressed_data)[window_pos], istream.data,
883 istream.data += block_size;
884 istream.data_bytes_left -= block_size;
885 /* Re-align bitstream if an odd number of bytes were
887 if (istream.data_bytes_left && (block_size & 1)) {
888 istream.data_bytes_left--;
891 e8_preprocessing_done = true;
895 if (e8_preprocessing_done)
896 undo_call_insn_preprocessing(uncompressed_data, uncompressed_len);