]> wimlib.net Git - wimlib/blob - src/xpress_decompress.c
decompress_common: move temp space for building decode table to heap
[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-2016 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/util.h"
74 #include "wimlib/xpress_constants.h"
75
76 /* This value is chosen for fast decompression.  */
77 #define XPRESS_TABLEBITS 11
78
79 struct xpress_decompressor {
80         union {
81                 DECODE_TABLE(decode_table, XPRESS_NUM_SYMBOLS,
82                              XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
83                 u8 lens[XPRESS_NUM_SYMBOLS];
84         };
85         DECODE_TABLE_WORKING_SPACE(working_space, XPRESS_NUM_SYMBOLS,
86                                    XPRESS_MAX_CODEWORD_LEN);
87 } _aligned_attribute(DECODE_TABLE_ALIGNMENT);
88
89 static int
90 xpress_decompress(const void *restrict compressed_data, size_t compressed_size,
91                   void *restrict uncompressed_data, size_t uncompressed_size,
92                   void *restrict _d)
93 {
94         struct xpress_decompressor *d  = _d;
95         const u8 * const in_begin = compressed_data;
96         u8 * const out_begin = uncompressed_data;
97         u8 *out_next = out_begin;
98         u8 * const out_end = out_begin + uncompressed_size;
99         struct input_bitstream is;
100
101         /* Read the Huffman codeword lengths.  */
102         if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
103                 return -1;
104         for (int i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
105                 d->lens[2 * i + 0] = in_begin[i] & 0xf;
106                 d->lens[2 * i + 1] = in_begin[i] >> 4;
107         }
108
109         /* Build a decoding table for the Huffman code.  */
110         if (make_huffman_decode_table(d->decode_table, XPRESS_NUM_SYMBOLS,
111                                       XPRESS_TABLEBITS, d->lens,
112                                       XPRESS_MAX_CODEWORD_LEN,
113                                       d->working_space))
114                 return -1;
115
116         /* Decode the matches and literals.  */
117
118         init_input_bitstream(&is, in_begin + XPRESS_NUM_SYMBOLS / 2,
119                              compressed_size - XPRESS_NUM_SYMBOLS / 2);
120
121         while (out_next != out_end) {
122                 unsigned sym;
123                 unsigned log2_offset;
124                 u32 length;
125                 u32 offset;
126
127                 sym = read_huffsym(&is, d->decode_table,
128                                    XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
129                 if (sym < XPRESS_NUM_CHARS) {
130                         /* Literal  */
131                         *out_next++ = sym;
132                 } else {
133                         /* Match  */
134                         length = sym & 0xf;
135                         log2_offset = (sym >> 4) & 0xf;
136
137                         bitstream_ensure_bits(&is, 16);
138
139                         offset = ((u32)1 << log2_offset) |
140                                  bitstream_pop_bits(&is, log2_offset);
141
142                         if (length == 0xf) {
143                                 length += bitstream_read_byte(&is);
144                                 if (length == 0xf + 0xff)
145                                         length = bitstream_read_u16(&is);
146                         }
147                         length += XPRESS_MIN_MATCH_LEN;
148
149                         if (unlikely(lz_copy(length, offset,
150                                              out_begin, out_next, out_end,
151                                              XPRESS_MIN_MATCH_LEN)))
152                                 return -1;
153
154                         out_next += length;
155                 }
156         }
157         return 0;
158 }
159
160 static int
161 xpress_create_decompressor(size_t max_block_size, void **d_ret)
162 {
163         struct xpress_decompressor *d;
164
165         if (max_block_size > XPRESS_MAX_OFFSET + 1)
166                 return WIMLIB_ERR_INVALID_PARAM;
167
168         d = ALIGNED_MALLOC(sizeof(*d), DECODE_TABLE_ALIGNMENT);
169         if (!d)
170                 return WIMLIB_ERR_NOMEM;
171
172         *d_ret = d;
173         return 0;
174 }
175
176 static void
177 xpress_free_decompressor(void *_d)
178 {
179         ALIGNED_FREE(_d);
180 }
181
182 const struct decompressor_ops xpress_decompressor_ops = {
183         .create_decompressor = xpress_create_decompressor,
184         .decompress          = xpress_decompress,
185         .free_decompressor   = xpress_free_decompressor,
186 };