4 * XPRESS compression routines.
6 * See the comments in xpress-decompress.c about the XPRESS format.
10 * Copyright (C) 2012, 2013 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
33 #include "wimlib/assert.h"
34 #include "wimlib/compressor_ops.h"
35 #include "wimlib/compress_common.h"
36 #include "wimlib/error.h"
37 #include "wimlib/lz_hash.h"
38 #include "wimlib/util.h"
39 #include "wimlib/xpress.h"
43 struct xpress_record_ctx {
44 input_idx_t freqs[XPRESS_NUM_SYMBOLS];
45 struct xpress_match *matches;
48 struct xpress_compressor {
51 struct xpress_match *matches;
52 input_idx_t *prev_tab;
53 u32 codewords[XPRESS_NUM_SYMBOLS];
54 u8 lens[XPRESS_NUM_SYMBOLS];
55 struct xpress_record_ctx record_ctx;
58 /* Intermediate XPRESS match/literal representation. */
60 u16 adjusted_len; /* Match length minus XPRESS_MIN_MATCH_LEN */
61 u16 offset; /* Match offset */
62 /* For literals, offset == 0 and adjusted_len is the literal. */
66 * Writes @match, which is a match given in the intermediate representation for
67 * XPRESS matches, to the output stream @ostream.
69 * @codewords and @lens provide the Huffman code that is being used.
72 xpress_write_match(struct xpress_match match,
73 struct output_bitstream *restrict ostream,
74 const u32 codewords[restrict],
75 const u8 lens[restrict])
77 u8 len_hdr = min(match.adjusted_len, 0xf);
78 u8 offset_bsr = bsr32(match.offset);
79 unsigned sym = XPRESS_NUM_CHARS + ((offset_bsr << 4) | len_hdr);
81 bitstream_put_bits(ostream, codewords[sym], lens[sym]);
83 if (match.adjusted_len >= 0xf) {
84 u8 byte1 = min(match.adjusted_len - 0xf, 0xff);
85 bitstream_put_byte(ostream, byte1);
87 bitstream_put_byte(ostream, match.adjusted_len & 0xff);
88 bitstream_put_byte(ostream, match.adjusted_len >> 8);
91 bitstream_put_bits(ostream, match.offset ^ (1U << offset_bsr), offset_bsr);
95 xpress_write_matches_and_literals(struct output_bitstream *ostream,
96 const struct xpress_match matches[restrict],
97 input_idx_t num_matches,
98 const u32 codewords[restrict],
99 const u8 lens[restrict])
101 for (input_idx_t i = 0; i < num_matches; i++) {
102 if (matches[i].offset) {
104 xpress_write_match(matches[i], ostream, codewords, lens);
107 u8 lit = matches[i].adjusted_len;
108 bitstream_put_bits(ostream, codewords[lit], lens[lit]);
111 bitstream_put_bits(ostream, codewords[XPRESS_END_OF_DATA], lens[XPRESS_END_OF_DATA]);
115 xpress_record_literal(u8 lit, void *_ctx)
117 struct xpress_record_ctx *ctx = _ctx;
119 *(ctx->matches++) = (struct xpress_match) { .offset = 0, .adjusted_len = lit };
123 xpress_record_match(unsigned len, unsigned offset, void *_ctx)
125 struct xpress_record_ctx *ctx = _ctx;
127 XPRESS_ASSERT(len >= XPRESS_MIN_MATCH_LEN);
128 XPRESS_ASSERT(len <= XPRESS_MAX_MATCH_LEN);
129 XPRESS_ASSERT(offset >= XPRESS_MIN_OFFSET);
130 XPRESS_ASSERT(offset <= XPRESS_MAX_OFFSET);
132 unsigned adjusted_len = len - XPRESS_MIN_MATCH_LEN;
133 unsigned len_hdr = min(adjusted_len, 0xf);
134 unsigned sym = XPRESS_NUM_CHARS + ((bsr32(offset) << 4) | len_hdr);
136 XPRESS_ASSERT(sym >= XPRESS_NUM_CHARS);
137 XPRESS_ASSERT(sym < XPRESS_NUM_SYMBOLS);
140 *(ctx->matches++) = (struct xpress_match) { .offset = offset,
141 .adjusted_len = adjusted_len };
144 static const struct lz_params xpress_lz_params = {
145 .min_match = XPRESS_MIN_MATCH_LEN,
146 .max_match = XPRESS_MAX_MATCH_LEN,
147 .max_offset = XPRESS_MAX_OFFSET,
151 .max_lazy_match = 16,
156 xpress_compress(const void *uncompressed_data, size_t uncompressed_size,
157 void *compressed_data, size_t compressed_size_avail, void *_c)
159 struct xpress_compressor *c = _c;
160 u8 *cptr = compressed_data;
161 struct output_bitstream ostream;
162 input_idx_t num_matches;
164 size_t compressed_size;
166 /* XPRESS requires 256 bytes of overhead for the Huffman code, so it's
167 * impossible to compress 256 bytes or less of data to less than the
170 * +1 to take into account that the buffer for compressed data is 1 byte
171 * smaller than the buffer for uncompressed data.
173 * +4 to take into account that init_output_bitstream() requires at
174 * least 4 bytes of data. */
175 if (compressed_size_avail < XPRESS_NUM_SYMBOLS / 2 + 1 + 4)
178 /* Copy the data to a temporary buffer, but only to avoid
179 * inconsequential accesses of uninitialized memory in
180 * lz_analyze_block(). */
181 memcpy(c->window, uncompressed_data, uncompressed_size);
182 memset(c->window + uncompressed_size, 0, 8);
184 /* Determine match/literal sequence to divide the data into. */
185 memset(c->record_ctx.freqs, 0, sizeof(c->record_ctx.freqs));
186 c->record_ctx.matches = c->matches;
187 lz_analyze_block(c->window,
190 xpress_record_literal,
195 num_matches = (c->record_ctx.matches - c->matches);
197 /* Account for end of data symbol. */
198 c->record_ctx.freqs[XPRESS_END_OF_DATA]++;
200 /* Build the Huffman code. */
201 make_canonical_huffman_code(XPRESS_NUM_SYMBOLS, XPRESS_MAX_CODEWORD_LEN,
202 c->record_ctx.freqs, c->lens, c->codewords);
204 /* Output the Huffman code as a series of 512 4-bit lengths. */
205 for (i = 0; i < XPRESS_NUM_SYMBOLS; i += 2)
206 *cptr++ = (c->lens[i] & 0xf) | (c->lens[i + 1] << 4);
208 /* Output the encoded matches/literals. */
209 init_output_bitstream(&ostream, cptr,
210 compressed_size_avail - XPRESS_NUM_SYMBOLS / 2 - 1);
212 xpress_write_matches_and_literals(&ostream, c->matches,
213 num_matches, c->codewords, c->lens);
215 /* Flush any pending data and get the length of the compressed data. */
216 compressed_size = flush_output_bitstream(&ostream);
217 if (compressed_size == ~(input_idx_t)0)
220 compressed_size += XPRESS_NUM_SYMBOLS / 2;
222 #if defined(ENABLE_XPRESS_DEBUG) || defined(ENABLE_VERIFY_COMPRESSION)
223 /* Verify that we really get the same thing back when decompressing. */
225 struct wimlib_decompressor *decompressor;
227 if (0 == wimlib_create_decompressor(WIMLIB_COMPRESSION_TYPE_XPRESS,
233 ret = wimlib_decompress(compressed_data,
238 wimlib_free_decompressor(decompressor);
241 ERROR("Failed to decompress data we "
242 "compressed using XPRESS algorithm");
246 if (memcmp(uncompressed_data, c->window,
249 ERROR("Data we compressed using XPRESS algorithm "
250 "didn't decompress to original");
255 WARNING("Failed to create decompressor for "
256 "data verification!");
261 return compressed_size;
265 xpress_free_compressor(void *_c)
267 struct xpress_compressor *c = _c;
278 xpress_create_compressor(size_t max_window_size,
279 const struct wimlib_compressor_params_header *params,
282 struct xpress_compressor *c;
284 if (max_window_size == 0 || max_window_size > (1U << 26))
285 return WIMLIB_ERR_INVALID_PARAM;
287 c = CALLOC(1, sizeof(struct xpress_compressor));
291 c->window = MALLOC(max_window_size + 8);
292 if (c->window == NULL)
295 c->max_window_size = max_window_size;
297 c->matches = MALLOC(max_window_size * sizeof(c->matches[0]));
298 if (c->matches == NULL)
301 c->prev_tab = MALLOC(max_window_size * sizeof(c->prev_tab[0]));
302 if (c->prev_tab == NULL)
309 xpress_free_compressor(c);
310 return WIMLIB_ERR_NOMEM;
314 xpress_get_needed_memory(size_t max_window_size,
315 const struct wimlib_compressor_params_header *params)
319 size += sizeof(struct xpress_compressor);
320 size += max_window_size + 8;
321 size += max_window_size * sizeof(((struct xpress_compressor*)0)->matches[0]);
322 size += max_window_size * sizeof(((struct xpress_compressor*)0)->prev_tab[0]);
327 const struct compressor_ops xpress_compressor_ops = {
328 .get_needed_memory = xpress_get_needed_memory,
329 .create_compressor = xpress_create_compressor,
330 .compress = xpress_compress,
331 .free_compressor = xpress_free_compressor,