]> wimlib.net Git - wimlib/blob - src/xpress-compress.c
export compression functions when using special define
[wimlib] / src / xpress-compress.c
1 /*
2  * xpress-compress.c
3  *
4  * XPRESS compression routines.
5  *
6  * See the comments in xpress-decompress.c about the XPRESS format.
7  */
8
9 /*
10  * Copyright (C) 2012, 2013 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
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)
17  * any later version.
18  *
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
22  * details.
23  *
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/.
26  */
27
28 #include "xpress.h"
29 #include "compress.h"
30 #include <stdlib.h>
31 #include <string.h>
32
33 /*
34  * Writes @match, which is a match given in the intermediate representation for
35  * XPRESS matches, to the output stream @ostream.
36  *
37  * @codewords and @lens provide the Huffman code that is being used.
38  */
39 static int
40 xpress_write_match(struct output_bitstream *ostream, u32 match,
41                    const u16 codewords[], const u8 lens[])
42 {
43         u32 adjusted_match_len = match & 0xffff;
44         u32 match_offset = match >> 16;
45         u32 len_hdr = min(adjusted_match_len, 0xf);
46         u32 offset_bsr = bsr32(match_offset);
47         u32 sym = len_hdr | (offset_bsr << 4) | XPRESS_NUM_CHARS;
48         int ret;
49
50         ret = bitstream_put_bits(ostream, codewords[sym], lens[sym]);
51         if (ret != 0)
52                 return ret;
53
54         if (adjusted_match_len >= 0xf) {
55                 u8 byte1 = min(adjusted_match_len - 0xf, 0xff);
56                 ret = bitstream_put_byte(ostream, byte1);
57                 if (ret != 0)
58                         return ret;
59                 if (byte1 == 0xff) {
60                         ret = bitstream_put_two_bytes(ostream, adjusted_match_len);
61                         if (ret != 0)
62                                 return ret;
63                 }
64         }
65         return bitstream_put_bits(ostream,
66                                   match_offset ^ (1 << offset_bsr), offset_bsr);
67 }
68
69 static int
70 xpress_write_compressed_literals(struct output_bitstream *ostream,
71                                  const u32 match_tab[],
72                                  unsigned num_matches,
73                                  const u16 codewords[],
74                                  const u8 lens[])
75 {
76         for (unsigned i = 0; i < num_matches; i++) {
77                 int ret;
78                 u32 match = match_tab[i];
79
80                 if (match >= XPRESS_NUM_CHARS) /* match */
81                         ret = xpress_write_match(ostream, match, codewords,
82                                                  lens);
83                 else /* literal byte */
84                         ret = bitstream_put_bits(ostream, codewords[match],
85                                                  lens[match]);
86                 if (ret != 0)
87                         return ret;
88         }
89         return bitstream_put_bits(ostream, codewords[XPRESS_END_OF_DATA],
90                                   lens[XPRESS_END_OF_DATA]);
91 }
92
93 static u32
94 xpress_record_literal(u8 literal, void *__freq_tab)
95 {
96         freq_t *freq_tab = __freq_tab;
97         freq_tab[literal]++;
98         return literal;
99 }
100
101 static u32
102 xpress_record_match(unsigned match_offset, unsigned match_len,
103                     void *freq_tab, void *ignore)
104 {
105         wimlib_assert(match_len >= XPRESS_MIN_MATCH &&
106                       match_len <= XPRESS_MAX_MATCH);
107         wimlib_assert(match_offset >= XPRESS_MIN_OFFSET &&
108                       match_offset <= XPRESS_MAX_OFFSET);
109
110         /*
111          * The intermediate representation of XPRESS matches is as follows:
112          *
113          * bits    description
114          * ----    -----------------------------------------------------------
115          *
116          * 16-31   match offset (XPRESS_MIN_OFFSET < x < XPRESS_MAX_OFFSET)
117          *
118          * 0-15    adjusted match length (0 <= x <= XPRESS_MAX_MATCH - XPRESS_MIN_MATCH)
119          *
120          * Literals are simply represented as themselves and can be
121          * distinguished from matches by the fact that only literals will have
122          * the upper three bytes completely clear. */
123
124         u32 adjusted_match_len = match_len - XPRESS_MIN_MATCH;
125         u32 len_hdr = min(adjusted_match_len, 0xf);
126         u32 offset_bsr = bsr32(match_offset);
127         u32 sym = len_hdr | (offset_bsr << 4) | XPRESS_NUM_CHARS;
128         ((freq_t*)freq_tab)[sym]++;
129         return adjusted_match_len | (match_offset << 16);
130 }
131
132 static const struct lz_params xpress_lz_params = {
133         .min_match      = XPRESS_MIN_MATCH,
134         .max_match      = XPRESS_MAX_MATCH,
135         .good_match     = 16,
136         .nice_match     = 32,
137         .max_chain_len  = 16,
138         .max_lazy_match = 16,
139         .too_far        = 4096,
140 };
141
142 /*
143  * Performs XPRESS compression on a block of data.
144  *
145  * Please see the documentation for the 'compress_func_t' type in write.c for
146  * the exact behavior of this function and how to call it.
147  */
148 #ifdef EXPORT_COMPRESSION_FUNCTIONS
149 WIMLIBAPI
150 #endif
151 unsigned
152 xpress_compress(const void *__uncompressed_data, unsigned uncompressed_len,
153                 void *__compressed_data)
154 {
155         const u8 *uncompressed_data = __uncompressed_data;
156         u8 *compressed_data = __compressed_data;
157         struct output_bitstream ostream;
158         u32 match_tab[uncompressed_len];
159         freq_t freq_tab[XPRESS_NUM_SYMBOLS];
160         u16 codewords[XPRESS_NUM_SYMBOLS];
161         u8 lens[XPRESS_NUM_SYMBOLS];
162         unsigned num_matches;
163         unsigned compressed_len;
164         unsigned i;
165         int ret;
166
167         wimlib_assert(uncompressed_len <= 32768);
168
169         /* XPRESS requires 256 bytes of overhead for the Huffman tables, so it's
170          * impossible cannot compress 256 bytes or less of data to less than the
171          * input size.
172          *
173          * +1 to take into account that the buffer for compressed data is 1 byte
174          * smaller than the buffer for uncompressed data.
175          *
176          * +4 to take into account that init_output_bitstream() requires at
177          * least 4 bytes of data. */
178         if (uncompressed_len < XPRESS_NUM_SYMBOLS / 2 + 1 + 4)
179                 return 0;
180
181         ZERO_ARRAY(freq_tab);
182         num_matches = lz_analyze_block(uncompressed_data, uncompressed_len,
183                                        match_tab, xpress_record_match,
184                                        xpress_record_literal, freq_tab,
185                                        NULL, freq_tab,
186                                        &xpress_lz_params);
187
188         freq_tab[XPRESS_END_OF_DATA]++;
189
190         make_canonical_huffman_code(XPRESS_NUM_SYMBOLS, XPRESS_MAX_CODEWORD_LEN,
191                                     freq_tab, lens, codewords);
192
193         /* IMPORTANT NOTE:
194          *
195          * It's tempting to output the 512 Huffman codeword lengths using the
196          * bitstream_put_bits() function.  However, this is NOT correct because
197          * bitstream_put_bits() will output 2 bytes at a time in little-endian
198          * order, which is the order that is needed for the compressed literals.
199          * However, the bytes in the lengths table are in order, so they need to
200          * be written one at a time without using bitstream_put_bits().
201          *
202          * Because of this, init_output_bitstream() is not called until after
203          * the lengths table is output.
204          */
205         for (i = 0; i < XPRESS_NUM_SYMBOLS; i += 2)
206                 *compressed_data++ = (lens[i] & 0xf) | (lens[i + 1] << 4);
207
208         init_output_bitstream(&ostream, compressed_data,
209                               uncompressed_len - XPRESS_NUM_SYMBOLS / 2 - 1);
210
211         ret = xpress_write_compressed_literals(&ostream, match_tab,
212                                                num_matches, codewords, lens);
213         if (ret)
214                 return 0;
215
216         /* Flush any bits that are buffered. */
217         ret = flush_output_bitstream(&ostream);
218         if (ret)
219                 return 0;
220
221         /* Assert that there are no output bytes between the ostream.output
222          * pointer and the ostream.next_bit_output pointer.  This can only
223          * happen if bytes had been written at the ostream.output pointer before
224          * the last bit word was written to the stream.  But, this does not
225          * occur since xpress_write_match() always finishes by writing some bits
226          * (a Huffman symbol), and the bitstream was just flushed. */
227         wimlib_assert(ostream.output - ostream.next_bit_output == 2);
228
229         /* The length of the compressed data is supposed to be the value of the
230          * ostream.output pointer before flushing, which is now the
231          * output.next_bit_output pointer after flushing.
232          *
233          * There will be an extra 2 bytes at the ostream.bit_output pointer,
234          * which is zeroed out.  (These 2 bytes may be either the last bytes in
235          * the compressed data, in which case they are actually unnecessary, or
236          * they may precede a number of bytes embedded into the bitstream.) */
237         if (ostream.bit_output >
238             (const u8*)__compressed_data + uncompressed_len - 3)
239                 return 0;
240         *(u16*)ostream.bit_output = cpu_to_le16(0);
241         compressed_len = ostream.next_bit_output - (const u8*)__compressed_data;
242
243         wimlib_assert(compressed_len <= uncompressed_len - 1);
244
245 #ifdef ENABLE_VERIFY_COMPRESSION
246         /* Verify that we really get the same thing back when decompressing. */
247         u8 buf[uncompressed_len];
248         ret = xpress_decompress(__compressed_data, compressed_len, buf,
249                                 uncompressed_len);
250         if (ret) {
251                 ERROR("xpress_compress(): Failed to decompress data we "
252                       "compressed");
253                 abort();
254         }
255         for (i = 0; i < uncompressed_len; i++) {
256                 if (buf[i] != uncompressed_data[i]) {
257                         ERROR("xpress_compress(): Data we compressed didn't "
258                               "decompress to the original data (difference at "
259                               "byte %u of %u)", i + 1, uncompressed_len);
260                         abort();
261                 }
262         }
263 #endif
264         return compressed_len;
265 }