]> wimlib.net Git - wimlib/blob - src/xpress-decomp.c
Allow writing to empty files in mounted WIM
[wimlib] / src / xpress-decomp.c
1 /*
2  * xpress-decomp.c
3  *
4  * XPRESS decompression routines.
5  *
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24
25
26 /*
27  * The XPRESS compression format is a LZ77-based algorithm.  That means it is
28  * quite similar to LZX compression, but XPRESS is slightly simpler, so it is a
29  * little faster to compress and decompress.
30  *
31  * The XPRESS compression format is mostly documented in a file called "[MS-XCA]
32  * Xpress Compression Algorithm".  In the MSDN library, it can currently be
33  * found under Open Specifications => Protocols => Windows Protocols => Windows
34  * Server Protocols => [MS-XCA] Xpress Compression Algorithm".  Note that
35  * Microsoft apparently also has either a slightly different format or an
36  * entirely different format that is also called XPRESS.  The other one is
37  * supposedly used in Windows' hibernation file or something, but the one used
38  * in WIM files is the one described in the above document.
39  *
40  * If you are already familiar with the LZ77 algorithm and Huffman coding, the
41  * XPRESS format is pretty 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 tree used for decoding compressed literals.  This is the only Huffman
44  * tree that is used for the entirety of the compressed data, and the codeword
45  * lengths are not encoded with a pretree.
46  *
47  * The rest of the compressed data is Huffman-encoded symbols.  Values 0 through
48  * 255 are literal bytes.  Values 256 through 511 are matches and may require
49  * extra bits or bytes to be read to get the match offset and match length.
50  *
51  * There is no notion of a "compressed block" in the XPRESS format, so in the
52  * XPRESS format, each WIM chunk (32768 bytes) will always use only one Huffman
53  * tree.
54  *
55  * The trickiest part is probably the fact that literal bytes for match lengths
56  * are encoded "separately" from the bitstream.
57  *
58  * Also, a caveat--- according to M$'s documentation for XPRESS,
59  *
60  *      "Some implementation of the decompression algorithm expect an extra
61  *      symbol to mark the end of the data.  Specifically, some implementations
62  *      fail during decompression if the Huffman symbol 256 is not found after
63  *      the actual data."
64  *
65  * This is the case for WIM files--- in we must write this extra symbol "256" at
66  * the end.  Otherwise Microsoft's software will fail to decompress the
67  * XPRESS-compressed data.
68  *
69  * Howeve, WIMLIB's decompressor in xpress-decomp.c currently does not care if
70  * this extra symbol is there or not.
71  */
72
73 #include "util.h"
74 #include "xpress.h"
75 #include "wimlib.h"
76
77 #define XPRESS_DECOMP
78 #include "decomp.h"
79
80 #include "huffman.h"
81
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, uint window_pos, uint window_len, 
87                                 u8 window[], struct input_bitstream *istream)
88 {
89         uint match_len;
90         uint match_offset;
91         u8 match_sym = (u8)huffsym;
92         u8 len_hdr = match_sym & 0xf;
93         u8 offset_bsr = match_sym >> 4;
94         int ret;
95         u8 *match_dest;
96         u8 *match_src;
97         uint i;
98
99         ret = bitstream_read_bits(istream, offset_bsr, &match_offset);
100         if (ret != 0)
101                 return -1;
102         match_offset |= (1 << offset_bsr);
103
104         if (len_hdr == 0xf) {
105                 ret = bitstream_read_byte(istream);
106                 if (ret == -1)
107                         return -1;
108                 match_len = ret;
109                 if (match_len == 0xff) {
110
111                         ret = bitstream_read_byte(istream);
112                         if (ret == -1)
113                                 return -1;
114                         match_len = ret;
115
116                         ret = bitstream_read_byte(istream);
117                         if (ret == -1)
118                                 return -1;
119
120                         match_len |= (ret << 8);
121                         if (match_len < 0xf)
122                                 return -1;
123                 } else {
124                         match_len += 0xf;
125                 }
126         } else {
127                 match_len = len_hdr;
128         }
129         match_len += XPRESS_MIN_MATCH;
130
131
132         /* Verify that the match is in the bounds of the part of the window
133          * currently in use, then copy the source of the match to the current
134          * position. */
135
136         match_dest = window + window_pos;
137         match_src = match_dest - match_offset;
138
139         if (window_pos + match_len > window_len) {
140                 ERROR("XPRESS dedecompression error: match of length %d "
141                                 "bytes overflows window\n", match_len);
142                 return -1;
143         }
144
145         if (match_src < window) {
146                 ERROR("XPRESS decompression error: match of length %d bytes "
147                                 "references data before window (match_offset = "
148                                 "%d, window_pos = %d)\n", match_len,
149                                 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                                       uint uncompressed_len, 
164                                       const u8 lens[], 
165                                       const u16 decode_table[])
166 {
167         uint curpos = 0;
168         uint huffsym;
169         int match_len;
170         int ret;
171
172         while (curpos < uncompressed_len) {
173                 ret = read_huffsym(istream, decode_table, lens, 
174                                 XPRESS_NUM_SYMBOLS, XPRESS_TABLEBITS, &huffsym,
175                                 XPRESS_MAX_CODEWORD_LEN);
176                 if (ret != 0)
177                         return ret;
178
179                 if (huffsym < XPRESS_NUM_CHARS) {
180                         uncompressed_data[curpos++] = huffsym;
181                 } else {
182                         match_len = xpress_decode_match(huffsym, curpos, 
183                                                 uncompressed_len, 
184                                                 uncompressed_data, istream);
185                         if (match_len == -1)
186                                 return 1;
187                         curpos += match_len;
188                 }
189         }
190         return 0;
191 }
192
193
194 int xpress_decompress(const void *__compressed_data, uint compressed_len, 
195                         void *uncompressed_data, uint uncompressed_len)
196 {
197         u8 lens[XPRESS_NUM_SYMBOLS];
198         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS];
199         struct input_bitstream istream;
200         u8 *lens_p;
201         const u8 *compressed_data;
202         uint i;
203         int ret;
204
205         compressed_data = __compressed_data;
206         lens_p = lens;
207
208         DEBUG2("compressed_len = %d, uncompressed_len = %d\n",
209                         compressed_len, uncompressed_len);
210
211         /* XPRESS uses only one Huffman tree.  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          */
215         if (compressed_len < XPRESS_NUM_SYMBOLS / 2)
216                 return WIMLIB_ERR_DECOMPRESSION;
217
218         for (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         ret = make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
224                                         XPRESS_TABLEBITS, lens,
225                                         XPRESS_MAX_CODEWORD_LEN);
226         if (ret != 0)
227                 return ret;
228
229         init_input_bitstream(&istream, compressed_data + XPRESS_NUM_SYMBOLS / 2, 
230                                         compressed_len - XPRESS_NUM_SYMBOLS / 2);
231
232         return xpress_decompress_literals(&istream, uncompressed_data, 
233                                         uncompressed_len, lens, decode_table);
234 }