]> wimlib.net Git - wimlib/blob - src/xpress-decompress.c
Tweak reading of concat runs
[wimlib] / src / xpress-decompress.c
1 /*
2  * xpress-decompress.c
3  *
4  * XPRESS decompression routines.
5  */
6
7 /*
8  *
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
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)
16  * any later version.
17  *
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
21  * details.
22  *
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/.
25  */
26
27
28 /*
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.
32  *
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).
39  *
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.
47  *
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.
52  *
53  * The trickiest part is probably the way in which literal bytes for match
54  * lengths are interleaved in the bitstream.
55  *
56  * Also, a caveat--- according to Microsoft's documentation for XPRESS,
57  *
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
61  *      the actual data."
62  *
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
65  * there or not.
66  */
67
68 #ifdef HAVE_CONFIG_H
69 #  include "config.h"
70 #endif
71
72 #include "wimlib.h"
73 #include "wimlib/decompress.h"
74 #include "wimlib/xpress.h"
75
76 /*
77  * Decodes a symbol @sym that begins an XPRESS match.
78  *
79  * The low 8 bits of the symbol are divided into:
80  *
81  * bits 0-3:  length header
82  * bits 4-7:  index of high-order bit of match offset
83  *
84  * Returns the match length, or -1 if the data is invalid.
85  */
86 static int
87 xpress_decode_match(unsigned sym, input_idx_t window_pos,
88                     input_idx_t window_len, u8 window[restrict],
89                     struct input_bitstream * restrict istream)
90 {
91
92         u8 len_hdr;
93         u8 offset_bsr;
94         int ret;
95         u8 *match_dest;
96         u8 *match_src;
97         unsigned i;
98         unsigned match_len;
99         unsigned match_offset;
100
101         sym -= XPRESS_NUM_CHARS;
102         len_hdr = sym & 0xf;
103         offset_bsr = sym >> 4;
104
105         if (bitstream_ensure_bits(istream, 16))
106                 return -1;
107
108         match_offset = (1U << offset_bsr) | bitstream_pop_bits(istream, offset_bsr);
109
110         if (len_hdr == 0xf) {
111                 ret = bitstream_read_byte(istream);
112                 if (ret < 0)
113                         return ret;
114                 match_len = ret;
115                 if (unlikely(match_len == 0xff)) {
116                         ret = bitstream_read_byte(istream);
117                         if (ret < 0)
118                                 return ret;
119                         match_len = ret;
120
121                         ret = bitstream_read_byte(istream);
122                         if (ret < 0)
123                                 return ret;
124
125                         match_len |= (ret << 8);
126                 } else {
127                         match_len += 0xf;
128                 }
129         } else {
130                 match_len = len_hdr;
131         }
132         match_len += XPRESS_MIN_MATCH_LEN;
133
134
135         /* Verify the match is in bounds, then copy its data to the the current
136          * position.  */
137
138         if (window_pos + match_len > window_len)
139                 return -1;
140
141         if (match_offset > window_pos)
142                 return -1;
143
144         match_dest = window + window_pos;
145         match_src = match_dest - match_offset;
146
147         for (i = 0; i < match_len; i++)
148                 match_dest[i] = match_src[i];
149
150         return match_len;
151 }
152
153 /* Decodes the Huffman-encoded matches and literal bytes in a region of
154  * XPRESS-encoded data.  */
155 static int
156 xpress_lz_decode(struct input_bitstream * restrict istream,
157                  u8 uncompressed_data[restrict],
158                  unsigned uncompressed_len,
159                  const u8 lens[restrict],
160                  const u16 decode_table[restrict])
161 {
162         input_idx_t curpos;
163         unsigned match_len;
164
165         for (curpos = 0; curpos < uncompressed_len; curpos += match_len) {
166                 unsigned sym;
167                 int ret;
168
169                 if (unlikely(bitstream_ensure_bits(istream, 16)))
170                         return -1;
171
172                 if (unlikely(read_huffsym(istream, decode_table, lens,
173                                           XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS,
174                                           &sym, XPRESS_MAX_CODEWORD_LEN)))
175                         return -1;
176
177                 if (sym < XPRESS_NUM_CHARS) {
178                         /* Literal  */
179                         uncompressed_data[curpos] = sym;
180                         match_len = 1;
181                 } else {
182                         /* Match  */
183                         ret = xpress_decode_match(sym,
184                                                   curpos,
185                                                   uncompressed_len,
186                                                   uncompressed_data,
187                                                   istream);
188                         if (unlikely(ret < 0))
189                                 return -1;
190                         match_len = ret;
191                 }
192         }
193         return 0;
194 }
195
196
197 /* API function documented in wimlib.h  */
198 WIMLIBAPI int
199 wimlib_xpress_decompress(const void * const restrict _compressed_data,
200                          const unsigned compressed_len,
201                          void * const restrict uncompressed_data,
202                          const unsigned uncompressed_len)
203 {
204         const u8 *compressed_data = _compressed_data;
205         u8 lens[XPRESS_NUM_SYMBOLS];
206         u8 *lens_p;
207         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
208                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
209         struct input_bitstream istream;
210
211         /* XPRESS uses only one Huffman code.  It contains 512 symbols, and the
212          * code lengths of these symbols are given literally as 4-bit integers
213          * in the first 256 bytes of the compressed data.  */
214         if (compressed_len < XPRESS_NUM_SYMBOLS / 2)
215                 return -1;
216
217         lens_p = lens;
218         for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
219                 *lens_p++ = compressed_data[i] & 0xf;
220                 *lens_p++ = compressed_data[i] >> 4;
221         }
222
223         if (make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
224                                       XPRESS_TABLEBITS, lens,
225                                       XPRESS_MAX_CODEWORD_LEN))
226                 return -1;
227
228         init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2,
229                              compressed_len - XPRESS_NUM_SYMBOLS / 2);
230
231         return xpress_lz_decode(&istream, uncompressed_data,
232                                 uncompressed_len, lens, decode_table);
233 }