4 * XPRESS decompression routines.
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 * The XPRESS compression format is an LZ77 and Huffman-code based algorithm.
30 * That means it is fairly similar to LZX compression, but XPRESS is simpler, so
31 * it is a little faster to compress and decompress.
33 * The XPRESS compression format is mostly documented in a file called "[MS-XCA]
34 * Xpress Compression Algorithm". In the MSDN library, it can currently be
35 * found under Open Specifications => Protocols => Windows Protocols => Windows
36 * Server Protocols => [MS-XCA] Xpress Compression Algorithm". The format in
37 * WIMs is specifically the algorithm labeled as the "LZ77+Huffman Algorithm"
38 * (there apparently are some other versions of XPRESS as well).
40 * If you are already familiar with the LZ77 algorithm and Huffman coding, the
41 * XPRESS format is fairly simple. The compressed data begins with 256 bytes
42 * that contain 512 4-bit integers that are the lengths of the symbols in the
43 * Huffman code used for match/literal headers. In contrast with more
44 * complicated formats such as DEFLATE and LZX, this is the only Huffman code
45 * that is used for the entirety of the XPRESS compressed data, and the codeword
46 * lengths are not encoded with a pretree.
48 * The rest of the compressed data is Huffman-encoded symbols. Values 0 through
49 * 255 represent the corresponding literal bytes. Values 256 through 511
50 * represent matches and may require extra bits or bytes to be read to get the
51 * match offset and match length.
53 * The trickiest part is probably the way in which literal bytes for match
54 * lengths are interleaved in the bitstream.
56 * Also, a caveat--- according to Microsoft's documentation for XPRESS,
58 * "Some implementation of the decompression algorithm expect an extra
59 * symbol to mark the end of the data. Specifically, some implementations
60 * fail during decompression if the Huffman symbol 256 is not found after
63 * This is the case for the implementation in WIMGAPI. However, wimlib's
64 * decompressor in this file currently does not care if this extra symbol is
73 #include "wimlib/decompressor_ops.h"
74 #include "wimlib/decompress_common.h"
75 #include "wimlib/xpress.h"
78 * Decodes a symbol @sym that begins an XPRESS match.
80 * The low 8 bits of the symbol are divided into:
82 * bits 0-3: length header
83 * bits 4-7: index of high-order bit of match offset
85 * Returns the match length, or -1 if the data is invalid.
88 xpress_decode_match(unsigned sym, input_idx_t window_pos,
89 input_idx_t window_len, u8 window[restrict],
90 struct input_bitstream * restrict istream)
95 unsigned match_offset;
97 sym -= XPRESS_NUM_CHARS;
99 offset_bsr = sym >> 4;
101 bitstream_ensure_bits(istream, 16);
103 match_offset = (1U << offset_bsr) | bitstream_pop_bits(istream, offset_bsr);
105 if (len_hdr == 0xf) {
106 match_len = bitstream_read_byte(istream);
107 if (unlikely(match_len == 0xff)) {
108 match_len = bitstream_read_byte(istream);
109 match_len |= (unsigned)bitstream_read_byte(istream) << 8;
116 match_len += XPRESS_MIN_MATCH_LEN;
118 if (unlikely(match_len > window_len - window_pos))
121 if (unlikely(match_offset > window_pos))
124 lz_copy(&window[window_pos], match_len, match_offset,
125 &window[window_len]);
130 /* Decodes the Huffman-encoded matches and literal bytes in a region of
131 * XPRESS-encoded data. */
133 xpress_lz_decode(struct input_bitstream * restrict istream,
134 u8 uncompressed_data[restrict],
135 unsigned uncompressed_len,
136 const u16 decode_table[restrict])
141 for (curpos = 0; curpos < uncompressed_len; curpos += match_len) {
145 bitstream_ensure_bits(istream, 16);
147 sym = read_huffsym(istream, decode_table,
148 XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
149 if (sym < XPRESS_NUM_CHARS) {
151 uncompressed_data[curpos] = sym;
155 ret = xpress_decode_match(sym,
160 if (unlikely(ret < 0))
170 xpress_decompress(const void *compressed_data, size_t compressed_size,
171 void *uncompressed_data, size_t uncompressed_size, void *_ctx)
173 const u8 *cdata = compressed_data;
174 u8 lens[XPRESS_NUM_SYMBOLS];
176 u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
177 _aligned_attribute(DECODE_TABLE_ALIGNMENT);
178 struct input_bitstream istream;
180 /* XPRESS uses only one Huffman code. It contains 512 symbols, and the
181 * code lengths of these symbols are given literally as 4-bit integers
182 * in the first 256 bytes of the compressed data. */
183 if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
187 for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
188 *lens_p++ = cdata[i] & 0xf;
189 *lens_p++ = cdata[i] >> 4;
192 if (make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
193 XPRESS_TABLEBITS, lens,
194 XPRESS_MAX_CODEWORD_LEN))
197 init_input_bitstream(&istream, cdata + XPRESS_NUM_SYMBOLS / 2,
198 compressed_size - XPRESS_NUM_SYMBOLS / 2);
200 return xpress_lz_decode(&istream, uncompressed_data,
201 uncompressed_size, decode_table);
204 const struct decompressor_ops xpress_decompressor_ops = {
205 .decompress = xpress_decompress,