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 Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 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 Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 * The XPRESS compression format is a LZ77-based algorithm. That means it is
31 * quite similar to LZX compression, but XPRESS is slightly simpler, so it is a
32 * 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". Note that
38 * Microsoft apparently also has either a slightly different format or an
39 * entirely different format that is also called XPRESS. The other one is
40 * supposedly used in Windows' hibernation file or something, but the one used
41 * in WIM files is the one described in the above document.
43 * If you are already familiar with the LZ77 algorithm and Huffman coding, the
44 * XPRESS format is pretty simple. The compressed data begins with 256 bytes
45 * that contain 512 4-bit integers that are the lengths of the symbols in the
46 * Huffman tree used for decoding compressed literals. This is the only Huffman
47 * tree that is used for the entirety of the compressed data, and the codeword
48 * lengths are not encoded with a pretree.
50 * The rest of the compressed data is Huffman-encoded symbols. Values 0 through
51 * 255 are literal bytes. Values 256 through 511 are matches and may require
52 * extra bits or bytes to be read to get the match offset and match length.
54 * There is no notion of a "compressed block" in the XPRESS format, so in the
55 * XPRESS format, each WIM chunk (32768 bytes) will always use only one Huffman
58 * The trickiest part is probably the fact that literal bytes for match lengths
59 * are encoded "separately" from the bitstream.
61 * Also, a caveat--- according to M$'s documentation for XPRESS,
63 * "Some implementation of the decompression algorithm expect an extra
64 * symbol to mark the end of the data. Specifically, some implementations
65 * fail during decompression if the Huffman symbol 256 is not found after
68 * This is the case for WIM files--- in we must write this extra symbol "256" at
69 * the end. Otherwise Microsoft's software will fail to decompress the
70 * XPRESS-compressed data.
72 * Howeve, WIMLIB's decompressor in xpress-decomp.c currently does not care if
73 * this extra symbol is there or not.
86 /* Decodes @huffsym, a value >= XPRESS_NUM_CHARS, that is the header of a match.
88 static int xpress_decode_match(int huffsym, uint window_pos, uint window_len,
89 u8 window[], struct input_bitstream *istream)
93 u8 match_sym = (u8)huffsym;
94 u8 len_hdr = match_sym & 0xf;
95 u8 offset_bsr = match_sym >> 4;
101 ret = bitstream_read_bits(istream, offset_bsr, &match_offset);
104 match_offset |= (1 << offset_bsr);
106 if (len_hdr == 0xf) {
107 ret = bitstream_read_byte(istream);
111 if (match_len == 0xff) {
113 ret = bitstream_read_byte(istream);
118 ret = bitstream_read_byte(istream);
122 match_len |= (ret << 8);
131 match_len += XPRESS_MIN_MATCH;
134 /* Verify that the match is in the bounds of the part of the window
135 * currently in use, then copy the source of the match to the current
138 match_dest = window + window_pos;
139 match_src = match_dest - match_offset;
141 if (window_pos + match_len > window_len) {
142 ERROR("XPRESS dedecompression error: match of length %d "
143 "bytes overflows window\n", match_len);
147 if (match_src < window) {
148 ERROR("XPRESS decompression error: match of length %d bytes "
149 "references data before window (match_offset = "
150 "%d, window_pos = %d)\n", match_len,
151 match_offset, window_pos);
155 for (i = 0; i < match_len; i++)
156 match_dest[i] = match_src[i];
161 /* Decodes the Huffman-encoded matches and literal bytes in a block of
162 * XPRESS-encoded data. */
163 static int xpress_decompress_literals(struct input_bitstream *istream,
164 u8 uncompressed_data[],
165 uint uncompressed_len,
167 const u16 decode_table[])
174 while (curpos < uncompressed_len) {
175 ret = read_huffsym(istream, decode_table, lens,
176 XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS, &huffsym,
177 XPRESS_MAX_CODEWORD_LEN);
181 if (huffsym < XPRESS_NUM_CHARS) {
182 uncompressed_data[curpos++] = huffsym;
184 match_len = xpress_decode_match(huffsym, curpos,
186 uncompressed_data, istream);
196 int xpress_decompress(const void *__compressed_data, uint compressed_len,
197 void *uncompressed_data, uint uncompressed_len)
199 u8 lens[XPRESS_NUM_SYMBOLS];
200 u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS];
201 struct input_bitstream istream;
203 const u8 *compressed_data;
207 compressed_data = __compressed_data;
210 DEBUG2("compressed_len = %d, uncompressed_len = %d\n",
211 compressed_len, uncompressed_len);
213 /* XPRESS uses only one Huffman tree. It contains 512 symbols, and the
214 * code lengths of these symbols are given literally as 4-bit integers
215 * in the first 256 bytes of the compressed data.
217 if (compressed_len < XPRESS_NUM_SYMBOLS / 2)
218 return WIMLIB_ERR_DECOMPRESSION;
220 for (i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
221 *lens_p++ = compressed_data[i] & 0xf;
222 *lens_p++ = compressed_data[i] >> 4;
225 ret = make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
226 XPRESS_TABLEBITS, lens,
227 XPRESS_MAX_CODEWORD_LEN);
231 init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2,
232 compressed_len - XPRESS_NUM_SYMBOLS / 2);
234 return xpress_decompress_literals(&istream, uncompressed_data,
235 uncompressed_len, lens, decode_table);