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