4 * A decompressor for the XPRESS compression format (Huffman variant).
9 * Copyright (C) 2012, 2013, 2015 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
27 * The XPRESS compression format is an LZ77 and Huffman-code based algorithm.
28 * That means it is fairly similar to LZX compression, but XPRESS is simpler, so
29 * it is a little faster to compress and decompress.
31 * The XPRESS compression format is mostly documented in a file called "[MS-XCA]
32 * Xpress Compression Algorithm". In the MSDN library, it can currently be
33 * found under Open Specifications => Protocols => Windows Protocols => Windows
34 * Server Protocols => [MS-XCA] Xpress Compression Algorithm". The format in
35 * WIMs is specifically the algorithm labeled as the "LZ77+Huffman Algorithm"
36 * (there apparently are some other versions of XPRESS as well).
38 * If you are already familiar with the LZ77 algorithm and Huffman coding, the
39 * XPRESS format is fairly simple. The compressed data begins with 256 bytes
40 * that contain 512 4-bit integers that are the lengths of the symbols in the
41 * Huffman code used for match/literal headers. In contrast with more
42 * complicated formats such as DEFLATE and LZX, this is the only Huffman code
43 * that is used for the entirety of the XPRESS compressed data, and the codeword
44 * lengths are not encoded with a pretree.
46 * The rest of the compressed data is Huffman-encoded symbols. Values 0 through
47 * 255 represent the corresponding literal bytes. Values 256 through 511
48 * represent matches and may require extra bits or bytes to be read to get the
49 * match offset and match length.
51 * The trickiest part is probably the way in which literal bytes for match
52 * lengths are interleaved in the bitstream.
54 * Also, a caveat--- according to Microsoft's documentation for XPRESS,
56 * "Some implementation of the decompression algorithm expect an extra
57 * symbol to mark the end of the data. Specifically, some implementations
58 * fail during decompression if the Huffman symbol 256 is not found after
61 * This is the case for the implementation in WIMGAPI. However, wimlib's
62 * decompressor in this file currently does not care if this extra symbol is
70 #include "wimlib/decompressor_ops.h"
71 #include "wimlib/decompress_common.h"
72 #include "wimlib/error.h"
73 #include "wimlib/xpress_constants.h"
75 /* This value is chosen for fast decompression. */
76 #define XPRESS_TABLEBITS 12
79 xpress_decompress(const void *compressed_data, size_t compressed_size,
80 void *uncompressed_data, size_t uncompressed_size, void *_ctx)
82 const u8 * const in_begin = compressed_data;
83 u8 * const out_begin = uncompressed_data;
84 u8 *out_next = out_begin;
85 u8 * const out_end = out_begin + uncompressed_size;
87 u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
88 _aligned_attribute(DECODE_TABLE_ALIGNMENT);
89 u8 lens[XPRESS_NUM_SYMBOLS];
91 struct input_bitstream is;
93 /* Read the Huffman codeword lengths. */
94 if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
96 for (int i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
97 u.lens[2 * i + 0] = in_begin[i] & 0xf;
98 u.lens[2 * i + 1] = in_begin[i] >> 4;
101 /* Build a decoding table for the Huffman code. */
102 if (make_huffman_decode_table(u.decode_table, XPRESS_NUM_SYMBOLS,
103 XPRESS_TABLEBITS, u.lens,
104 XPRESS_MAX_CODEWORD_LEN))
107 /* Decode the matches and literals. */
109 init_input_bitstream(&is, in_begin + XPRESS_NUM_SYMBOLS / 2,
110 compressed_size - XPRESS_NUM_SYMBOLS / 2);
112 while (out_next != out_end) {
114 unsigned log2_offset;
118 sym = read_huffsym(&is, u.decode_table,
119 XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
120 if (sym < XPRESS_NUM_CHARS) {
126 log2_offset = (sym >> 4) & 0xf;
128 bitstream_ensure_bits(&is, 16);
130 offset = ((u32)1 << log2_offset) |
131 bitstream_pop_bits(&is, log2_offset);
134 length += bitstream_read_byte(&is);
135 if (length == 0xf + 0xff)
136 length = bitstream_read_u16(&is);
138 length += XPRESS_MIN_MATCH_LEN;
140 if (unlikely(offset > out_next - out_begin))
143 if (unlikely(length > out_end - out_next))
146 lz_copy(out_next, length, offset, out_end,
147 XPRESS_MIN_MATCH_LEN);
156 xpress_create_decompressor(size_t max_block_size, void **dec_ret)
158 if (max_block_size > XPRESS_MAX_OFFSET + 1)
159 return WIMLIB_ERR_INVALID_PARAM;
164 const struct decompressor_ops xpress_decompressor_ops = {
165 .create_decompressor = xpress_create_decompressor,
166 .decompress = xpress_decompress,