]> wimlib.net Git - wimlib/blob - src/xpress-decompress.c
Remove verify_dentry(); separate refcnt recalc. from verify_inode()
[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 /*
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.
33  *
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).
40  *
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.
47  *
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.
51  *
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
54  * tree.
55  *
56  * The trickiest part is probably the fact that literal bytes for match lengths
57  * are encoded "separately" from the bitstream.
58  *
59  * Also, a caveat--- according to Microsoft's documentation for XPRESS,
60  *
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
64  *      the actual data."
65  *
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.
69  *
70  * However, wimlib's decompressor in this file currently does not care if this
71  * extra symbol is there or not.
72  */
73
74 #ifdef HAVE_CONFIG_H
75 #  include "config.h"
76 #endif
77
78 #include "wimlib.h"
79 #include "wimlib/assert.h"
80 #define XPRESS_DECOMP
81 #include "wimlib/decompress.h"
82 #include "wimlib/util.h"
83 #include "wimlib/xpress.h"
84
85 /*
86  * Decodes a symbol @huffsym that begins an XPRESS match.
87  *
88  * The low 8 bits of the symbol are divided into:
89  *
90  * bits 0-3:  length header
91  * bits 4-7:  index of high-order bit of match offset
92  *
93  * Note: taking the low 8 bits of the symbol is the same as subtracting 256, the
94  * number of symbols reserved for literals.
95  *
96  * Returns the match length, or -1 on error.
97  */
98 static int
99 xpress_decode_match(unsigned huffsym, unsigned window_pos,
100                     unsigned window_len, u8 window[restrict],
101                     struct input_bitstream * restrict istream)
102 {
103         unsigned match_len;
104         unsigned match_offset;
105         u8 match_sym = (u8)huffsym;
106         u8 len_hdr = match_sym & 0xf;
107         u8 offset_bsr = match_sym >> 4;
108         int ret;
109         u8 *match_dest;
110         u8 *match_src;
111         unsigned i;
112
113         ret = bitstream_read_bits(istream, offset_bsr, &match_offset);
114         if (ret)
115                 return ret;
116         match_offset |= (1 << offset_bsr);
117
118         if (len_hdr == 0xf) {
119                 ret = bitstream_read_byte(istream);
120                 if (ret < 0)
121                         return ret;
122                 match_len = ret;
123                 if (match_len == 0xff) {
124                         ret = bitstream_read_byte(istream);
125                         if (ret < 0)
126                                 return ret;
127                         match_len = ret;
128
129                         ret = bitstream_read_byte(istream);
130                         if (ret < 0)
131                                 return ret;
132
133                         match_len |= (ret << 8);
134                 } else {
135                         match_len += 0xf;
136                 }
137         } else {
138                 match_len = len_hdr;
139         }
140         match_len += XPRESS_MIN_MATCH;
141
142         /* Verify that the match is in the bounds of the part of the window
143          * currently in use, then copy the source of the match to the current
144          * position. */
145
146         match_dest = window + window_pos;
147         match_src = match_dest - match_offset;
148
149         if (window_pos + match_len > window_len) {
150                 ERROR("XPRESS decompression error: match of length %u "
151                       "bytes overflows window", match_len);
152                 return -1;
153         }
154
155         if (match_src < window) {
156                 ERROR("XPRESS decompression error: match of length %u bytes "
157                       "references data before window (match_offset = %u, "
158                       "window_pos = %u)", match_len, match_offset, window_pos);
159                 return -1;
160         }
161
162         for (i = 0; i < match_len; i++)
163                 match_dest[i] = match_src[i];
164
165         return match_len;
166 }
167
168 /* Decodes the Huffman-encoded matches and literal bytes in a block of
169  * XPRESS-encoded data. */
170 static int
171 xpress_decompress_block(struct input_bitstream * restrict istream,
172                         u8 uncompressed_data[restrict],
173                         unsigned uncompressed_len,
174                         const u8 lens[restrict],
175                         const u16 decode_table[restrict])
176 {
177         unsigned curpos;
178         unsigned huffsym;
179         int ret;
180         int match_len;
181
182         curpos = 0;
183         while (curpos < uncompressed_len) {
184                 ret = read_huffsym(istream, decode_table, lens,
185                                    XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS,
186                                    &huffsym, XPRESS_MAX_CODEWORD_LEN);
187                 if (ret)
188                         return ret;
189
190                 if (huffsym < XPRESS_NUM_CHARS) {
191                         uncompressed_data[curpos++] = huffsym;
192                 } else {
193                         match_len = xpress_decode_match(huffsym,
194                                                         curpos,
195                                                         uncompressed_len,
196                                                         uncompressed_data,
197                                                         istream);
198                         if (match_len < 0)
199                                 return match_len;
200                         curpos += match_len;
201                 }
202         }
203         return 0;
204 }
205
206
207 /* Documented in wimlib.h */
208 WIMLIBAPI int
209 wimlib_xpress_decompress(const void * restrict _compressed_data, unsigned compressed_len,
210                          void * restrict uncompressed_data, unsigned uncompressed_len)
211 {
212         u8 lens[XPRESS_NUM_SYMBOLS];
213         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
214                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
215         struct input_bitstream istream;
216         u8 *lens_p;
217         const u8 *compressed_data;
218         unsigned i;
219         int ret;
220
221         compressed_data = _compressed_data;
222         lens_p = lens;
223
224         DEBUG2("compressed_len = %d, uncompressed_len = %d",
225                compressed_len, uncompressed_len);
226
227         /* XPRESS uses only one Huffman tree.  It contains 512 symbols, and the
228          * code lengths of these symbols are given literally as 4-bit integers
229          * in the first 256 bytes of the compressed data.
230          */
231         if (compressed_len < XPRESS_NUM_SYMBOLS / 2) {
232                 ERROR("xpress_decompress(): Compressed length too short!");
233                 return -1;
234         }
235
236         for (i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
237                 *lens_p++ = compressed_data[i] & 0xf;
238                 *lens_p++ = compressed_data[i] >> 4;
239         }
240
241         ret = make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
242                                         XPRESS_TABLEBITS, lens,
243                                         XPRESS_MAX_CODEWORD_LEN);
244         if (ret)
245                 return ret;
246
247         init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2,
248                              compressed_len - XPRESS_NUM_SYMBOLS / 2);
249
250         return xpress_decompress_block(&istream, uncompressed_data,
251                                        uncompressed_len, lens,
252                                        decode_table);
253 }