]> wimlib.net Git - wimlib/blob - src/xpress-decomp.c
Compression code cleanups
[wimlib] / src / xpress-decomp.c
1 /*
2  * xpress-decomp.c
3  *
4  * XPRESS decompression routines.
5  */
6
7 /*
8  *
9  * Copyright (C) 2012 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-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.
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".  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.
42  *
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.
49  *
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.
53  *
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
56  * tree.
57  *
58  * The trickiest part is probably the fact that literal bytes for match lengths
59  * are encoded "separately" from the bitstream.
60  *
61  * Also, a caveat--- according to Microsoft's documentation for XPRESS,
62  *
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
66  *      the actual data."
67  *
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.
71  *
72  * Howeve, wimlib's decompressor in xpress-decomp.c currently does not care if
73  * this extra symbol is there or not.
74  */
75
76 #include "util.h"
77 #include "xpress.h"
78 #include "wimlib.h"
79
80 #define XPRESS_DECOMP
81 #include "decomp.h"
82
83
84 /* Decodes @huffsym, a value >= XPRESS_NUM_CHARS, that is the header of a match.
85  * */
86 static int xpress_decode_match(int huffsym, unsigned window_pos,
87                                unsigned window_len, u8 window[],
88                                struct input_bitstream *istream)
89 {
90         unsigned match_len;
91         unsigned match_offset;
92         u8 match_sym = (u8)huffsym;
93         u8 len_hdr = match_sym & 0xf;
94         u8 offset_bsr = match_sym >> 4;
95         int ret;
96         u8 *match_dest;
97         u8 *match_src;
98         unsigned i;
99
100         ret = bitstream_read_bits(istream, offset_bsr, &match_offset);
101         if (ret != 0)
102                 return -1;
103         match_offset |= (1 << offset_bsr);
104
105         if (len_hdr == 0xf) {
106                 ret = bitstream_read_byte(istream);
107                 if (ret == -1)
108                         return -1;
109                 match_len = ret;
110                 if (match_len == 0xff) {
111
112                         ret = bitstream_read_byte(istream);
113                         if (ret == -1)
114                                 return -1;
115                         match_len = ret;
116
117                         ret = bitstream_read_byte(istream);
118                         if (ret == -1)
119                                 return -1;
120
121                         match_len |= (ret << 8);
122                         if (match_len < 0xf)
123                                 return -1;
124                 } else {
125                         match_len += 0xf;
126                 }
127         } else {
128                 match_len = len_hdr;
129         }
130         match_len += XPRESS_MIN_MATCH;
131
132
133         /* Verify that the match is in the bounds of the part of the window
134          * currently in use, then copy the source of the match to the current
135          * position. */
136
137         match_dest = window + window_pos;
138         match_src = match_dest - match_offset;
139
140         if (window_pos + match_len > window_len) {
141                 ERROR("XPRESS decompression error: match of length %d "
142                       "bytes overflows window", match_len);
143                 return -1;
144         }
145
146         if (match_src < window) {
147                 ERROR("XPRESS decompression error: match of length %d bytes "
148                       "references data before window (match_offset = %d, "
149                       "window_pos = %d)", match_len, match_offset, window_pos);
150                 return -1;
151         }
152
153         for (i = 0; i < match_len; i++)
154                 match_dest[i] = match_src[i];
155
156         return match_len;
157 }
158
159 /* Decodes the Huffman-encoded matches and literal bytes in a block of
160  * XPRESS-encoded data. */
161 static int xpress_decompress_literals(struct input_bitstream *istream,
162                                       u8 uncompressed_data[],
163                                       unsigned uncompressed_len,
164                                       const u8 lens[],
165                                       const u16 decode_table[])
166 {
167         unsigned curpos = 0;
168         unsigned huffsym;
169         int match_len;
170         int ret = 0;
171
172         while (curpos < uncompressed_len) {
173                 ret = read_huffsym(istream, decode_table, lens,
174                                    XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS,
175                                    &huffsym, XPRESS_MAX_CODEWORD_LEN);
176                 if (ret != 0)
177                         break;
178
179                 if (huffsym < XPRESS_NUM_CHARS) {
180                         uncompressed_data[curpos++] = huffsym;
181                 } else {
182                         match_len = xpress_decode_match(huffsym,
183                                                         curpos,
184                                                         uncompressed_len,
185                                                         uncompressed_data,
186                                                         istream);
187                         if (match_len == -1) {
188                                 ret = 1;
189                                 break;
190                         }
191                         curpos += match_len;
192                 }
193         }
194         return ret;
195 }
196
197
198 int xpress_decompress(const void *__compressed_data, unsigned compressed_len,
199                       void *uncompressed_data, unsigned uncompressed_len)
200 {
201         u8 lens[XPRESS_NUM_SYMBOLS];
202         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS];
203         struct input_bitstream istream;
204         u8 *lens_p;
205         const u8 *compressed_data;
206         unsigned i;
207         int ret;
208
209         compressed_data = __compressed_data;
210         lens_p = lens;
211
212         DEBUG2("compressed_len = %d, uncompressed_len = %d",
213                compressed_len, uncompressed_len);
214
215         /* XPRESS uses only one Huffman tree.  It contains 512 symbols, and the
216          * code lengths of these symbols are given literally as 4-bit integers
217          * in the first 256 bytes of the compressed data.
218          */
219         if (compressed_len < XPRESS_NUM_SYMBOLS / 2)
220                 return WIMLIB_ERR_DECOMPRESSION;
221
222         for (i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
223                 *lens_p++ = compressed_data[i] & 0xf;
224                 *lens_p++ = compressed_data[i] >> 4;
225         }
226
227         ret = make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
228                                         XPRESS_TABLEBITS, lens,
229                                         XPRESS_MAX_CODEWORD_LEN);
230         if (ret != 0)
231                 return ret;
232
233         init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2,
234                              compressed_len - XPRESS_NUM_SYMBOLS / 2);
235
236         return xpress_decompress_literals(&istream, uncompressed_data,
237                                           uncompressed_len, lens,
238                                           decode_table);
239 }