]> wimlib.net Git - wimlib/blob - src/xpress-decompress.c
xpress-decompress.c: Performance tweaks
[wimlib] / src / xpress-decompress.c
1 /*
2  * xpress-decompress.c
3  *
4  * A very fast decompressor for XPRESS (Huffman version).
5  */
6
7 /*
8  *
9  * Copyright (C) 2012, 2013 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  * The XPRESS compression format is an LZ77 and Huffman-code based algorithm.
30  * That means it is fairly similar to LZX compression, but XPRESS is simpler, so
31  * it is a little faster to compress and decompress.
32  *
33  * The XPRESS compression format is mostly documented in a file called "[MS-XCA]
34  * Xpress Compression Algorithm".  In the MSDN library, it can currently be
35  * found under Open Specifications => Protocols => Windows Protocols => Windows
36  * Server Protocols => [MS-XCA] Xpress Compression Algorithm".  The format in
37  * WIMs is specifically the algorithm labeled as the "LZ77+Huffman Algorithm"
38  * (there apparently are some other versions of XPRESS as well).
39  *
40  * If you are already familiar with the LZ77 algorithm and Huffman coding, the
41  * XPRESS format is fairly 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 code used for match/literal headers.  In contrast with more
44  * complicated formats such as DEFLATE and LZX, this is the only Huffman code
45  * that is used for the entirety of the XPRESS 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 represent the corresponding literal bytes.  Values 256 through 511
50  * represent matches and may require extra bits or bytes to be read to get the
51  * match offset and match length.
52  *
53  * The trickiest part is probably the way in which literal bytes for match
54  * lengths are interleaved in the bitstream.
55  *
56  * Also, a caveat--- according to Microsoft's documentation for XPRESS,
57  *
58  *      "Some implementation of the decompression algorithm expect an extra
59  *      symbol to mark the end of the data.  Specifically, some implementations
60  *      fail during decompression if the Huffman symbol 256 is not found after
61  *      the actual data."
62  *
63  * This is the case for the implementation in WIMGAPI.  However, wimlib's
64  * decompressor in this file currently does not care if this extra symbol is
65  * there or not.
66  */
67
68 #ifdef HAVE_CONFIG_H
69 #  include "config.h"
70 #endif
71
72 #include "wimlib/decompressor_ops.h"
73 #include "wimlib/decompress_common.h"
74 #include "wimlib/error.h"
75 #include "wimlib/xpress.h"
76
77 /* This value is chosen for fast decompression.  */
78 #define XPRESS_TABLEBITS 12
79
80 /* Decode the matches and literal bytes in a region of XPRESS-encoded data.  */
81 static int
82 xpress_decode_window(struct input_bitstream *istream, const u16 *decode_table,
83                      u8 *window, unsigned window_size)
84 {
85         u8 *window_ptr = window;
86         u8 *window_end = &window[window_size];
87         unsigned sym;
88         unsigned match_len;
89         unsigned offset_bsr;
90         unsigned match_offset;
91
92         while (window_ptr != window_end) {
93
94                 sym = read_huffsym(istream, decode_table,
95                                    XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
96                 if (sym < XPRESS_NUM_CHARS) {
97                         /* Literal  */
98                         *window_ptr++ = sym;
99                         continue;
100                 }
101
102                 /* Match  */
103                 match_len = sym & 0xf;
104                 offset_bsr = (sym >> 4) & 0xf;
105
106                 bitstream_ensure_bits(istream, 16);
107
108                 match_offset = (1 << offset_bsr) |
109                                 bitstream_pop_bits(istream, offset_bsr);
110
111                 if (match_len == 0xf) {
112                         match_len += bitstream_read_byte(istream);
113                         if (match_len == 0xf + 0xff)
114                                 match_len = bitstream_read_u16(istream);
115                 }
116                 match_len += XPRESS_MIN_MATCH_LEN;
117
118                 if (unlikely(match_offset > window_ptr - window))
119                         return -1;
120
121                 if (unlikely(match_len > window_end - window_ptr))
122                         return -1;
123
124                 lz_copy(window_ptr, match_len, match_offset, window_end);
125
126                 window_ptr += match_len;
127         }
128         return 0;
129 }
130
131 static int
132 xpress_decompress(const void *compressed_data, size_t compressed_size,
133                   void *uncompressed_data, size_t uncompressed_size, void *_ctx)
134 {
135         const u8 *cdata = compressed_data;
136         u8 lens[XPRESS_NUM_SYMBOLS];
137         u8 *lens_p;
138         u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS]
139                         _aligned_attribute(DECODE_TABLE_ALIGNMENT);
140         struct input_bitstream istream;
141
142         /* XPRESS uses only one Huffman code.  It contains 512 symbols, and the
143          * code lengths of these symbols are given literally as 4-bit integers
144          * in the first 256 bytes of the compressed data.  */
145         if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
146                 return -1;
147
148         lens_p = lens;
149         for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
150                 *lens_p++ = cdata[i] & 0xf;
151                 *lens_p++ = cdata[i] >> 4;
152         }
153
154         if (make_huffman_decode_table(decode_table, XPRESS_NUM_SYMBOLS,
155                                       XPRESS_TABLEBITS, lens,
156                                       XPRESS_MAX_CODEWORD_LEN))
157                 return -1;
158
159         init_input_bitstream(&istream, cdata + XPRESS_NUM_SYMBOLS / 2,
160                              compressed_size - XPRESS_NUM_SYMBOLS / 2);
161
162         return xpress_decode_window(&istream, decode_table,
163                                     uncompressed_data, uncompressed_size);
164 }
165
166 static int
167 xpress_create_decompressor(size_t max_block_size, void **dec_ret)
168 {
169         if (max_block_size > XPRESS_MAX_OFFSET + 1)
170                 return WIMLIB_ERR_INVALID_PARAM;
171
172         return 0;
173 }
174
175 const struct decompressor_ops xpress_decompressor_ops = {
176         .create_decompressor = xpress_create_decompressor,
177         .decompress          = xpress_decompress,
178 };