]> wimlib.net Git - wimlib/blob - src/xpress_decompress.c
Rename _full_path => d_full_path
[wimlib] / src / xpress_decompress.c
1 /*
2  * xpress_decompress.c
3  *
4  * A decompressor for the XPRESS compression format (Huffman variant).
5  */
6
7 /*
8  *
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see http://www.gnu.org/licenses/.
23  */
24
25
26 /*
27  * The XPRESS compression format is an LZ77 and Huffman-code based algorithm.
28  * That means it is fairly similar to LZX compression, but XPRESS is simpler, so
29  * it is a 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".  The format in
35  * WIMs is specifically the algorithm labeled as the "LZ77+Huffman Algorithm"
36  * (there apparently are some other versions of XPRESS as well).
37  *
38  * If you are already familiar with the LZ77 algorithm and Huffman coding, the
39  * XPRESS format is fairly simple.  The compressed data begins with 256 bytes
40  * that contain 512 4-bit integers that are the lengths of the symbols in the
41  * Huffman code used for match/literal headers.  In contrast with more
42  * complicated formats such as DEFLATE and LZX, this is the only Huffman code
43  * that is used for the entirety of the XPRESS compressed data, and the codeword
44  * lengths are not encoded with a pretree.
45  *
46  * The rest of the compressed data is Huffman-encoded symbols.  Values 0 through
47  * 255 represent the corresponding literal bytes.  Values 256 through 511
48  * represent matches and may require extra bits or bytes to be read to get the
49  * match offset and match length.
50  *
51  * The trickiest part is probably the way in which literal bytes for match
52  * lengths are interleaved in the bitstream.
53  *
54  * Also, a caveat--- according to Microsoft's documentation for XPRESS,
55  *
56  *      "Some implementation of the decompression algorithm expect an extra
57  *      symbol to mark the end of the data.  Specifically, some implementations
58  *      fail during decompression if the Huffman symbol 256 is not found after
59  *      the actual data."
60  *
61  * This is the case for the implementation in WIMGAPI.  However, wimlib's
62  * decompressor in this file currently does not care if this extra symbol is
63  * there or not.
64  */
65
66 #ifdef HAVE_CONFIG_H
67 #  include "config.h"
68 #endif
69
70 #include "wimlib/decompressor_ops.h"
71 #include "wimlib/decompress_common.h"
72 #include "wimlib/error.h"
73 #include "wimlib/xpress_constants.h"
74
75 /* This value is chosen for fast decompression.  */
76 #define XPRESS_TABLEBITS 12
77
78 /* Decode the matches and literal bytes in a region of XPRESS-encoded data.  */
79 static int
80 xpress_decode_window(struct input_bitstream *istream, const u16 *decode_table,
81                      u8 *window, unsigned window_size)
82 {
83         u8 *window_ptr = window;
84         u8 *window_end = &window[window_size];
85         unsigned sym;
86         unsigned match_len;
87         unsigned offset_high_bit;
88         unsigned match_offset;
89
90         while (window_ptr != window_end) {
91
92                 sym = read_huffsym(istream, decode_table,
93                                    XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
94                 if (sym < XPRESS_NUM_CHARS) {
95                         /* Literal  */
96                         *window_ptr++ = sym;
97                         continue;
98                 }
99
100                 /* Match  */
101                 match_len = sym & 0xf;
102                 offset_high_bit = (sym >> 4) & 0xf;
103
104                 bitstream_ensure_bits(istream, 16);
105
106                 match_offset = (1 << offset_high_bit) |
107                                 bitstream_pop_bits(istream, offset_high_bit);
108
109                 if (match_len == 0xf) {
110                         match_len += bitstream_read_byte(istream);
111                         if (match_len == 0xf + 0xff)
112                                 match_len = bitstream_read_u16(istream);
113                 }
114                 match_len += XPRESS_MIN_MATCH_LEN;
115
116                 if (unlikely(match_offset > window_ptr - window))
117                         return -1;
118
119                 if (unlikely(match_len > window_end - window_ptr))
120                         return -1;
121
122                 lz_copy(window_ptr, match_len, match_offset, window_end,
123                         XPRESS_MIN_MATCH_LEN);
124
125                 window_ptr += match_len;
126         }
127         return 0;
128 }
129
130 static int
131 xpress_decompress(const void *compressed_data, size_t compressed_size,
132                   void *uncompressed_data, size_t uncompressed_size, void *_ctx)
133 {
134         const u8 *cdata = compressed_data;
135         u8 lens[XPRESS_NUM_SYMBOLS];
136         u8 *lens_p;
137         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
138                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
139         struct input_bitstream istream;
140
141         /* XPRESS uses only one Huffman code.  It contains 512 symbols, and the
142          * code lengths of these symbols are given literally as 4-bit integers
143          * in the first 256 bytes of the compressed data.  */
144         if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
145                 return -1;
146
147         lens_p = lens;
148         for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
149                 *lens_p++ = cdata[i] & 0xf;
150                 *lens_p++ = cdata[i] >> 4;
151         }
152
153         if (make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
154                                       XPRESS_TABLEBITS, lens,
155                                       XPRESS_MAX_CODEWORD_LEN))
156                 return -1;
157
158         init_input_bitstream(&istream, cdata + XPRESS_NUM_SYMBOLS / 2,
159                              compressed_size - XPRESS_NUM_SYMBOLS / 2);
160
161         return xpress_decode_window(&istream, decode_table,
162                                     uncompressed_data, uncompressed_size);
163 }
164
165 static int
166 xpress_create_decompressor(size_t max_block_size, void **dec_ret)
167 {
168         if (max_block_size > XPRESS_MAX_OFFSET + 1)
169                 return WIMLIB_ERR_INVALID_PARAM;
170
171         return 0;
172 }
173
174 const struct decompressor_ops xpress_decompressor_ops = {
175         .create_decompressor = xpress_create_decompressor,
176         .decompress          = xpress_decompress,
177 };