4 * XPRESS compression routines.
6 * See the comments in xpress-decomp.c about the XPRESS format.
10 * Copyright (C) 2012 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 static inline u32 bsr32(u32 n)
35 #if defined(__x86__) || defined(__x86_64__)
42 while ((n >>= 1) != 0)
50 * Writes @match, which is a match given in the intermediate representation for
51 * XPRESS matches, to the output stream @ostream.
53 * @codewords and @lens provide the Huffman code that is being used.
55 static int xpress_write_match(struct output_bitstream *ostream, u32 match,
56 const u16 codewords[], const u8 lens[])
66 main_sym = (match & 0xff);
67 huff_sym = main_sym + XPRESS_NUM_CHARS;
68 ret = bitstream_put_bits(ostream, codewords[huff_sym], lens[huff_sym]);
72 offset_bsr = main_sym >> 4;
74 match_len = (match >> 8) & 0xff;
75 match_offset = (match >> 16);
78 match_len -= XPRESS_MIN_MATCH;
79 if (match_len >= 0xf) {
80 byte1 = (u8)(match_len - 0xf);
81 ret = bitstream_put_byte(ostream, byte1);
85 ret = bitstream_put_two_bytes(ostream, match_len);
90 return bitstream_put_bits(ostream, match_offset ^ (1 << offset_bsr),
94 static int xpress_write_compressed_literals(struct output_bitstream *ostream,
95 const u32 match_tab[],
97 const u16 codewords[],
104 for (i = 0; i < num_matches; i++) {
105 match = match_tab[i];
106 if (match >= XPRESS_NUM_CHARS) {
108 ret = xpress_write_match(ostream, match, codewords,
114 ret = bitstream_put_bits(ostream, codewords[match],
120 return bitstream_put_bits(ostream, codewords[256], lens[256]);
123 static u32 xpress_record_literal(u8 literal, void *__freq_tab)
125 u32 *freq_tab = __freq_tab;
130 static u32 xpress_record_match(uint match_offset, uint match_len,
131 void *__freq_tab, void *ignore)
133 u32 *freq_tab = __freq_tab;
138 wimlib_assert(match_len >= XPRESS_MIN_MATCH &&
139 match_len <= XPRESS_MAX_MATCH);
140 wimlib_assert(match_offset > 0);
142 len_hdr = min(match_len - XPRESS_MIN_MATCH, 15);
143 offset_bsr = bsr32(match_offset);
144 match = (offset_bsr << 4) | len_hdr;
145 freq_tab[match + XPRESS_NUM_CHARS]++;
146 match |= match_len << 8;
147 match |= match_offset << 16;
151 static const struct lz_params xpress_lz_params = {
153 .max_match = XPRESS_MAX_MATCH,
157 .max_lazy_match = 16,
162 * Performs XPRESS compression on a block of data.
164 * @__uncompressed_data: Pointer to the data to be compressed.
165 * @uncompressed_len: Length, in bytes, of the data to be compressed.
166 * @__compressed_data: Pointer to a location at least (@uncompressed_len - 1)
167 * bytes long into which the compressed data may be
169 * @compressed_len_ret: A pointer to an unsigned int into which the length of
170 * the compressed data may be returned.
172 * Returns zero if compression was successfully performed. In that case
173 * @compressed_data and @compressed_len_ret will contain the compressed data and
174 * its length. A return value of nonzero means that compressing the data did
175 * not reduce its size, and @compressed_data will not contain the full
178 int xpress_compress(const void *__uncompressed_data, uint uncompressed_len,
179 void *__compressed_data, uint *compressed_len_ret)
181 const u8 *uncompressed_data = __uncompressed_data;
182 u8 *compressed_data = __compressed_data;
183 struct output_bitstream ostream;
184 u32 match_tab[uncompressed_len];
185 u32 freq_tab[XPRESS_NUM_SYMBOLS];
186 u16 codewords[XPRESS_NUM_SYMBOLS];
187 u8 lens[XPRESS_NUM_SYMBOLS];
193 XPRESS_DEBUG("uncompressed_len = %u", uncompressed_len);
195 if (uncompressed_len < 300)
198 ZERO_ARRAY(freq_tab);
200 num_matches = lz_analyze_block(uncompressed_data, uncompressed_len,
201 match_tab, xpress_record_match,
202 xpress_record_literal, freq_tab,
206 XPRESS_DEBUG("using %u matches", num_matches);
210 make_canonical_huffman_code(XPRESS_NUM_SYMBOLS, XPRESS_MAX_CODEWORD_LEN,
211 freq_tab, lens, codewords);
215 * It's tempting to output the 512 Huffman codeword lengths using the
216 * bitstream_put_bits() function. However, this is NOT correct because
217 * bitstream_put_bits() will output 2 bytes at a time in little-endian
218 * order, which is the order that is needed for the compressed literals.
219 * However, the bytes in the lengths table are in order, so they need to
220 * be written one at a time without using bitstream_put_bits().
222 * Because of this, init_output_bitstream() is not called until after
223 * the lengths table is output.
225 for (i = 0; i < XPRESS_NUM_SYMBOLS; i += 2)
226 *compressed_data++ = (lens[i] & 0xf) | (lens[i + 1] << 4);
228 init_output_bitstream(&ostream, compressed_data, uncompressed_len -
229 XPRESS_NUM_SYMBOLS / 2 - 1);
231 ret = xpress_write_compressed_literals(&ostream, match_tab, num_matches,
236 ret = flush_output_bitstream(&ostream);
241 compressed_len = ostream.output - (u8*)__compressed_data;
243 XPRESS_DEBUG("Compressed %u => %u bytes",
244 uncompressed_len, compressed_len);
246 *compressed_len_ret = compressed_len;
248 #ifdef ENABLE_VERIFY_COMPRESSION
249 /* Verify that we really get the same thing back when decompressing. */
250 XPRESS_DEBUG("Verifying the compressed data.");
251 u8 buf[uncompressed_len];
252 ret = xpress_decompress(__compressed_data, compressed_len, buf,
255 ERROR("xpress_compress(): Failed to decompress data we "
259 for (i = 0; i < uncompressed_len; i++) {
260 if (buf[i] != uncompressed_data[i]) {
261 ERROR("xpress_compress(): Data we compressed didn't "
262 "decompress to the original data (difference at "
263 "byte %u of %u)", i + 1, uncompressed_len);
267 XPRESS_DEBUG("Compression verified to be correct.");