4 * XPRESS decompression routines.
9 * Copyright (C) 2012 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/.
30 * The XPRESS compression format is a LZ77 and Huffman-code based algorithm.
31 * That means it is quite similar to LZX compression, but XPRESS is slightly
32 * simpler, so it is a little faster to compress and decompress.
34 * The XPRESS compression format is mostly documented in a file called "[MS-XCA]
35 * Xpress Compression Algorithm". In the MSDN library, it can currently be
36 * found under Open Specifications => Protocols => Windows Protocols => Windows
37 * Server Protocols => [MS-XCA] Xpress Compression Algorithm". The format in
38 * WIMs is specifically the algorithm labeled as the "LZ77+Huffman Algorithm"
39 * (there apparently are some other versions of XPRESS as well).
41 * If you are already familiar with the LZ77 algorithm and Huffman coding, the
42 * XPRESS format is fairly simple. The compressed data begins with 256 bytes
43 * that contain 512 4-bit integers that are the lengths of the symbols in the
44 * Huffman tree used for decoding compressed literals. This is the only Huffman
45 * tree that is used for the entirety of the 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 are literal bytes. Values 256 through 511 are matches and may require
50 * extra bits or bytes to be read to get the match offset and match length.
52 * There is no notion of a "compressed block" in the XPRESS format, so in the
53 * XPRESS format, each WIM chunk (32768 bytes) will always use only one Huffman
56 * The trickiest part is probably the fact that literal bytes for match lengths
57 * are encoded "separately" from the bitstream.
59 * Also, a caveat--- according to Microsoft's documentation for XPRESS,
61 * "Some implementation of the decompression algorithm expect an extra
62 * symbol to mark the end of the data. Specifically, some implementations
63 * fail during decompression if the Huffman symbol 256 is not found after
66 * This is the case for WIM files--- in we must write this extra symbol "256" at
67 * the end. Otherwise Microsoft's software will fail to decompress the
68 * XPRESS-compressed data.
70 * However, wimlib's decompressor in this file currently does not care if this
71 * extra symbol is there or not.
79 #include "decompress.h"
82 * Decodes a symbol @huffsym that begins an XPRESS match.
84 * The low 8 bits of the symbol are divided into:
86 * bits 0-3: length header
87 * bits 4-7: index of high-order bit of match offset
89 * Note: taking the low 8 bits of the symbol is the same as subtracting 256, the
90 * number of symbols reserved for literals.
92 * Returns the match length, or -1 on error.
94 static int xpress_decode_match(unsigned huffsym, unsigned window_pos,
95 unsigned window_len, u8 window[],
96 struct input_bitstream *istream)
99 unsigned match_offset;
100 u8 match_sym = (u8)huffsym;
101 u8 len_hdr = match_sym & 0xf;
102 u8 offset_bsr = match_sym >> 4;
108 ret = bitstream_read_bits(istream, offset_bsr, &match_offset);
111 match_offset |= (1 << offset_bsr);
113 if (len_hdr == 0xf) {
114 ret = bitstream_read_byte(istream);
118 if (match_len == 0xff) {
119 ret = bitstream_read_byte(istream);
124 ret = bitstream_read_byte(istream);
128 match_len |= (ret << 8);
135 match_len += XPRESS_MIN_MATCH;
137 /* Verify that the match is in the bounds of the part of the window
138 * currently in use, then copy the source of the match to the current
141 match_dest = window + window_pos;
142 match_src = match_dest - match_offset;
144 if (window_pos + match_len > window_len) {
145 ERROR("XPRESS decompression error: match of length %u "
146 "bytes overflows window", match_len);
150 if (match_src < window) {
151 ERROR("XPRESS decompression error: match of length %u bytes "
152 "references data before window (match_offset = %u, "
153 "window_pos = %u)", match_len, match_offset, window_pos);
157 for (i = 0; i < match_len; i++)
158 match_dest[i] = match_src[i];
163 /* Decodes the Huffman-encoded matches and literal bytes in a block of
164 * XPRESS-encoded data. */
165 static int xpress_decompress_block(struct input_bitstream *istream,
166 u8 uncompressed_data[],
167 unsigned uncompressed_len,
169 const u16 decode_table[])
177 while (curpos < uncompressed_len) {
178 ret = read_huffsym(istream, decode_table, lens,
179 XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS,
180 &huffsym, XPRESS_MAX_CODEWORD_LEN);
184 if (huffsym < XPRESS_NUM_CHARS) {
185 uncompressed_data[curpos++] = huffsym;
187 match_len = xpress_decode_match(huffsym,
201 int xpress_decompress(const void *__compressed_data, unsigned compressed_len,
202 void *uncompressed_data, unsigned uncompressed_len)
204 u8 lens[XPRESS_NUM_SYMBOLS];
205 u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS];
206 struct input_bitstream istream;
208 const u8 *compressed_data;
212 compressed_data = __compressed_data;
215 DEBUG2("compressed_len = %d, uncompressed_len = %d",
216 compressed_len, uncompressed_len);
218 /* XPRESS uses only one Huffman tree. It contains 512 symbols, and the
219 * code lengths of these symbols are given literally as 4-bit integers
220 * in the first 256 bytes of the compressed data.
222 if (compressed_len < XPRESS_NUM_SYMBOLS / 2)
225 for (i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
226 *lens_p++ = compressed_data[i] & 0xf;
227 *lens_p++ = compressed_data[i] >> 4;
230 ret = make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
231 XPRESS_TABLEBITS, lens,
232 XPRESS_MAX_CODEWORD_LEN);
236 init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2,
237 compressed_len - XPRESS_NUM_SYMBOLS / 2);
239 return xpress_decompress_block(&istream, uncompressed_data,
240 uncompressed_len, lens,