]> wimlib.net Git - wimlib/blob - src/lzms-compress.c
Switch from suffix array match-finder to binary tree match-finder
[wimlib] / src / lzms-compress.c
1 /*
2  * lzms-compress.c
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 /* This a compressor for the LZMS compression format.  More details about this
25  * format can be found in lzms-decompress.c.
26  *
27  * Also see lzx-compress.c for general information about match-finding and
28  * match-choosing that also applies to this LZMS compressor.
29  *
30  * NOTE: this compressor currently does not code any delta matches.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #  include "config.h"
35 #endif
36
37 #include "wimlib.h"
38 #include "wimlib/assert.h"
39 #include "wimlib/compiler.h"
40 #include "wimlib/compressor_ops.h"
41 #include "wimlib/compress_common.h"
42 #include "wimlib/endianness.h"
43 #include "wimlib/error.h"
44 #include "wimlib/lz.h"
45 #include "wimlib/lz_bt.h"
46 #include "wimlib/lzms.h"
47 #include "wimlib/util.h"
48
49 #include <string.h>
50 #include <limits.h>
51 #include <pthread.h>
52
53 /* Stucture used for writing raw bits to the end of the LZMS-compressed data as
54  * a series of 16-bit little endian coding units.  */
55 struct lzms_output_bitstream {
56         /* Buffer variable containing zero or more bits that have been logically
57          * written to the bitstream but not yet written to memory.  This must be
58          * at least as large as the coding unit size.  */
59         u16 bitbuf;
60
61         /* Number of bits in @bitbuf that are valid.  */
62         unsigned num_free_bits;
63
64         /* Pointer to one past the next position in the compressed data buffer
65          * at which to output a 16-bit coding unit.  */
66         le16 *out;
67
68         /* Maximum number of 16-bit coding units that can still be output to
69          * the compressed data buffer.  */
70         size_t num_le16_remaining;
71
72         /* Set to %true if not all coding units could be output due to
73          * insufficient space.  */
74         bool overrun;
75 };
76
77 /* Stucture used for range encoding (raw version).  */
78 struct lzms_range_encoder_raw {
79
80         /* A 33-bit variable that holds the low boundary of the current range.
81          * The 33rd bit is needed to catch carries.  */
82         u64 low;
83
84         /* Size of the current range.  */
85         u32 range;
86
87         /* Next 16-bit coding unit to output.  */
88         u16 cache;
89
90         /* Number of 16-bit coding units whose output has been delayed due to
91          * possible carrying.  The first such coding unit is @cache; all
92          * subsequent such coding units are 0xffff.  */
93         u32 cache_size;
94
95         /* Pointer to the next position in the compressed data buffer at which
96          * to output a 16-bit coding unit.  */
97         le16 *out;
98
99         /* Maximum number of 16-bit coding units that can still be output to
100          * the compressed data buffer.  */
101         size_t num_le16_remaining;
102
103         /* %true when the very first coding unit has not yet been output.  */
104         bool first;
105
106         /* Set to %true if not all coding units could be output due to
107          * insufficient space.  */
108         bool overrun;
109 };
110
111 /* Structure used for range encoding.  This wraps around `struct
112  * lzms_range_encoder_raw' to use and maintain probability entries.  */
113 struct lzms_range_encoder {
114         /* Pointer to the raw range encoder, which has no persistent knowledge
115          * of probabilities.  Multiple lzms_range_encoder's share the same
116          * lzms_range_encoder_raw.  */
117         struct lzms_range_encoder_raw *rc;
118
119         /* Bits recently encoded by this range encoder.  This are used as in
120          * index into @prob_entries.  */
121         u32 state;
122
123         /* Bitmask for @state to prevent its value from exceeding the number of
124          * probability entries.  */
125         u32 mask;
126
127         /* Probability entries being used for this range encoder.  */
128         struct lzms_probability_entry prob_entries[LZMS_MAX_NUM_STATES];
129 };
130
131 /* Structure used for Huffman encoding.  */
132 struct lzms_huffman_encoder {
133
134         /* Bitstream to write Huffman-encoded symbols and verbatim bits to.
135          * Multiple lzms_huffman_encoder's share the same lzms_output_bitstream.
136          */
137         struct lzms_output_bitstream *os;
138
139         /* Number of symbols that have been written using this code far.  Reset
140          * to 0 whenever the code is rebuilt.  */
141         u32 num_syms_written;
142
143         /* When @num_syms_written reaches this number, the Huffman code must be
144          * rebuilt.  */
145         u32 rebuild_freq;
146
147         /* Number of symbols in the represented Huffman code.  */
148         unsigned num_syms;
149
150         /* Running totals of symbol frequencies.  These are diluted slightly
151          * whenever the code is rebuilt.  */
152         u32 sym_freqs[LZMS_MAX_NUM_SYMS];
153
154         /* The length, in bits, of each symbol in the Huffman code.  */
155         u8 lens[LZMS_MAX_NUM_SYMS];
156
157         /* The codeword of each symbol in the Huffman code.  */
158         u32 codewords[LZMS_MAX_NUM_SYMS];
159 };
160
161 /* State of the LZMS compressor.  */
162 struct lzms_compressor {
163         struct wimlib_lzms_compressor_params params;
164
165         /* Pointer to a buffer holding the preprocessed data to compress.  */
166         u8 *window;
167
168         /* Current position in @buffer.  */
169         u32 cur_window_pos;
170
171         /* Size of the data in @buffer.  */
172         u32 window_size;
173
174         /* Binary tree match-finder.  */
175         struct lz_bt mf;
176
177         /* Temporary space to store found matches.  */
178         struct raw_match *matches;
179
180         /* Match-chooser data.  */
181         struct lzms_mc_pos_data *optimum;
182         unsigned optimum_cur_idx;
183         unsigned optimum_end_idx;
184
185         /* Maximum block size this compressor instantiation allows.  This is the
186          * allocated size of @window.  */
187         u32 max_block_size;
188
189         /* Raw range encoder which outputs to the beginning of the compressed
190          * data buffer, proceeding forwards.  */
191         struct lzms_range_encoder_raw rc;
192
193         /* Bitstream which outputs to the end of the compressed data buffer,
194          * proceeding backwards.  */
195         struct lzms_output_bitstream os;
196
197         /* Range encoders.  */
198         struct lzms_range_encoder main_range_encoder;
199         struct lzms_range_encoder match_range_encoder;
200         struct lzms_range_encoder lz_match_range_encoder;
201         struct lzms_range_encoder lz_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
202         struct lzms_range_encoder delta_match_range_encoder;
203         struct lzms_range_encoder delta_repeat_match_range_encoders[LZMS_NUM_RECENT_OFFSETS - 1];
204
205         /* Huffman encoders.  */
206         struct lzms_huffman_encoder literal_encoder;
207         struct lzms_huffman_encoder lz_offset_encoder;
208         struct lzms_huffman_encoder length_encoder;
209         struct lzms_huffman_encoder delta_power_encoder;
210         struct lzms_huffman_encoder delta_offset_encoder;
211
212         /* LRU (least-recently-used) queues for match information.  */
213         struct lzms_lru_queues lru;
214
215         /* Used for preprocessing.  */
216         s32 last_target_usages[65536];
217 };
218
219 struct lzms_mc_pos_data {
220         u32 cost;
221 #define MC_INFINITE_COST ((u32)~0UL)
222         union {
223                 struct {
224                         u32 link;
225                         u32 match_offset;
226                 } prev;
227                 struct {
228                         u32 link;
229                         u32 match_offset;
230                 } next;
231         };
232         struct lzms_adaptive_state {
233                 struct lzms_lz_lru_queues lru;
234                 u8 main_state;
235                 u8 match_state;
236                 u8 lz_match_state;
237                 u8 lz_repeat_match_state[LZMS_NUM_RECENT_OFFSETS - 1];
238         } state;
239 };
240
241 /* Initialize the output bitstream @os to write forwards to the specified
242  * compressed data buffer @out that is @out_limit 16-bit integers long.  */
243 static void
244 lzms_output_bitstream_init(struct lzms_output_bitstream *os,
245                            le16 *out, size_t out_limit)
246 {
247         os->bitbuf = 0;
248         os->num_free_bits = 16;
249         os->out = out + out_limit;
250         os->num_le16_remaining = out_limit;
251         os->overrun = false;
252 }
253
254 /* Write @num_bits bits, contained in the low @num_bits bits of @bits (ordered
255  * from high-order to low-order), to the output bitstream @os.  */
256 static void
257 lzms_output_bitstream_put_bits(struct lzms_output_bitstream *os,
258                                u32 bits, unsigned num_bits)
259 {
260         bits &= (1U << num_bits) - 1;
261
262         while (num_bits > os->num_free_bits) {
263
264                 if (unlikely(os->num_le16_remaining == 0)) {
265                         os->overrun = true;
266                         return;
267                 }
268
269                 unsigned num_fill_bits = os->num_free_bits;
270
271                 os->bitbuf <<= num_fill_bits;
272                 os->bitbuf |= bits >> (num_bits - num_fill_bits);
273
274                 *--os->out = cpu_to_le16(os->bitbuf);
275                 --os->num_le16_remaining;
276
277                 os->num_free_bits = 16;
278                 num_bits -= num_fill_bits;
279                 bits &= (1U << num_bits) - 1;
280         }
281         os->bitbuf <<= num_bits;
282         os->bitbuf |= bits;
283         os->num_free_bits -= num_bits;
284 }
285
286 /* Flush the output bitstream, ensuring that all bits written to it have been
287  * written to memory.  Returns %true if all bits were output successfully, or
288  * %false if an overrun occurred.  */
289 static bool
290 lzms_output_bitstream_flush(struct lzms_output_bitstream *os)
291 {
292         if (os->num_free_bits != 16)
293                 lzms_output_bitstream_put_bits(os, 0, os->num_free_bits + 1);
294         return !os->overrun;
295 }
296
297 /* Initialize the range encoder @rc to write forwards to the specified
298  * compressed data buffer @out that is @out_limit 16-bit integers long.  */
299 static void
300 lzms_range_encoder_raw_init(struct lzms_range_encoder_raw *rc,
301                             le16 *out, size_t out_limit)
302 {
303         rc->low = 0;
304         rc->range = 0xffffffff;
305         rc->cache = 0;
306         rc->cache_size = 1;
307         rc->out = out;
308         rc->num_le16_remaining = out_limit;
309         rc->first = true;
310         rc->overrun = false;
311 }
312
313 /*
314  * Attempt to flush bits from the range encoder.
315  *
316  * Note: this is based on the public domain code for LZMA written by Igor
317  * Pavlov.  The only differences in this function are that in LZMS the bits must
318  * be output in 16-bit coding units instead of 8-bit coding units, and that in
319  * LZMS the first coding unit is not ignored by the decompressor, so the encoder
320  * cannot output a dummy value to that position.
321  *
322  * The basic idea is that we're writing bits from @rc->low to the output.
323  * However, due to carrying, the writing of coding units with value 0xffff, as
324  * well as one prior coding unit, must be delayed until it is determined whether
325  * a carry is needed.
326  */
327 static void
328 lzms_range_encoder_raw_shift_low(struct lzms_range_encoder_raw *rc)
329 {
330         LZMS_DEBUG("low=%"PRIx64", cache=%"PRIx64", cache_size=%u",
331                    rc->low, rc->cache, rc->cache_size);
332         if ((u32)(rc->low) < 0xffff0000 ||
333             (u32)(rc->low >> 32) != 0)
334         {
335                 /* Carry not needed (rc->low < 0xffff0000), or carry occurred
336                  * ((rc->low >> 32) != 0, a.k.a. the carry bit is 1).  */
337                 do {
338                         if (!rc->first) {
339                                 if (rc->num_le16_remaining == 0) {
340                                         rc->overrun = true;
341                                         return;
342                                 }
343                                 *rc->out++ = cpu_to_le16(rc->cache +
344                                                          (u16)(rc->low >> 32));
345                                 --rc->num_le16_remaining;
346                         } else {
347                                 rc->first = false;
348                         }
349
350                         rc->cache = 0xffff;
351                 } while (--rc->cache_size != 0);
352
353                 rc->cache = (rc->low >> 16) & 0xffff;
354         }
355         ++rc->cache_size;
356         rc->low = (rc->low & 0xffff) << 16;
357 }
358
359 static void
360 lzms_range_encoder_raw_normalize(struct lzms_range_encoder_raw *rc)
361 {
362         if (rc->range <= 0xffff) {
363                 rc->range <<= 16;
364                 lzms_range_encoder_raw_shift_low(rc);
365         }
366 }
367
368 static bool
369 lzms_range_encoder_raw_flush(struct lzms_range_encoder_raw *rc)
370 {
371         for (unsigned i = 0; i < 4; i++)
372                 lzms_range_encoder_raw_shift_low(rc);
373         return !rc->overrun;
374 }
375
376 /* Encode the next bit using the range encoder (raw version).
377  *
378  * @prob is the chance out of LZMS_PROBABILITY_MAX that the next bit is 0.  */
379 static void
380 lzms_range_encoder_raw_encode_bit(struct lzms_range_encoder_raw *rc, int bit,
381                                   u32 prob)
382 {
383         lzms_range_encoder_raw_normalize(rc);
384
385         u32 bound = (rc->range >> LZMS_PROBABILITY_BITS) * prob;
386         if (bit == 0) {
387                 rc->range = bound;
388         } else {
389                 rc->low += bound;
390                 rc->range -= bound;
391         }
392 }
393
394 /* Encode a bit using the specified range encoder. This wraps around
395  * lzms_range_encoder_raw_encode_bit() to handle using and updating the
396  * appropriate probability table.  */
397 static void
398 lzms_range_encode_bit(struct lzms_range_encoder *enc, int bit)
399 {
400         struct lzms_probability_entry *prob_entry;
401         u32 prob;
402
403         /* Load the probability entry corresponding to the current state.  */
404         prob_entry = &enc->prob_entries[enc->state];
405
406         /* Treat the number of zero bits in the most recently encoded
407          * LZMS_PROBABILITY_MAX bits with this probability entry as the chance,
408          * out of LZMS_PROBABILITY_MAX, that the next bit will be a 0.  However,
409          * don't allow 0% or 100% probabilities.  */
410         prob = prob_entry->num_recent_zero_bits;
411         if (prob == 0)
412                 prob = 1;
413         else if (prob == LZMS_PROBABILITY_MAX)
414                 prob = LZMS_PROBABILITY_MAX - 1;
415
416         /* Encode the next bit.  */
417         lzms_range_encoder_raw_encode_bit(enc->rc, bit, prob);
418
419         /* Update the state based on the newly encoded bit.  */
420         enc->state = ((enc->state << 1) | bit) & enc->mask;
421
422         /* Update the recent bits, including the cached count of 0's.  */
423         BUILD_BUG_ON(LZMS_PROBABILITY_MAX > sizeof(prob_entry->recent_bits) * 8);
424         if (bit == 0) {
425                 if (prob_entry->recent_bits & (1ULL << (LZMS_PROBABILITY_MAX - 1))) {
426                         /* Replacing 1 bit with 0 bit; increment the zero count.
427                          */
428                         prob_entry->num_recent_zero_bits++;
429                 }
430         } else {
431                 if (!(prob_entry->recent_bits & (1ULL << (LZMS_PROBABILITY_MAX - 1)))) {
432                         /* Replacing 0 bit with 1 bit; decrement the zero count.
433                          */
434                         prob_entry->num_recent_zero_bits--;
435                 }
436         }
437         prob_entry->recent_bits = (prob_entry->recent_bits << 1) | bit;
438 }
439
440 /* Encode a symbol using the specified Huffman encoder.  */
441 static void
442 lzms_huffman_encode_symbol(struct lzms_huffman_encoder *enc, u32 sym)
443 {
444         LZMS_ASSERT(sym < enc->num_syms);
445         lzms_output_bitstream_put_bits(enc->os,
446                                        enc->codewords[sym],
447                                        enc->lens[sym]);
448         ++enc->sym_freqs[sym];
449         if (++enc->num_syms_written == enc->rebuild_freq) {
450                 /* Adaptive code needs to be rebuilt.  */
451                 LZMS_DEBUG("Rebuilding code (num_syms=%u)", enc->num_syms);
452                 make_canonical_huffman_code(enc->num_syms,
453                                             LZMS_MAX_CODEWORD_LEN,
454                                             enc->sym_freqs,
455                                             enc->lens,
456                                             enc->codewords);
457
458                 /* Dilute the frequencies.  */
459                 for (unsigned i = 0; i < enc->num_syms; i++) {
460                         enc->sym_freqs[i] >>= 1;
461                         enc->sym_freqs[i] += 1;
462                 }
463                 enc->num_syms_written = 0;
464         }
465 }
466
467 static void
468 lzms_encode_length(struct lzms_huffman_encoder *enc, u32 length)
469 {
470         unsigned slot;
471         unsigned num_extra_bits;
472         u32 extra_bits;
473
474         slot = lzms_get_length_slot(length);
475
476         num_extra_bits = lzms_extra_length_bits[slot];
477
478         extra_bits = length - lzms_length_slot_base[slot];
479
480         lzms_huffman_encode_symbol(enc, slot);
481         lzms_output_bitstream_put_bits(enc->os, extra_bits, num_extra_bits);
482 }
483
484 static void
485 lzms_encode_offset(struct lzms_huffman_encoder *enc, u32 offset)
486 {
487         unsigned slot;
488         unsigned num_extra_bits;
489         u32 extra_bits;
490
491         slot = lzms_get_position_slot(offset);
492
493         num_extra_bits = lzms_extra_position_bits[slot];
494
495         extra_bits = offset - lzms_position_slot_base[slot];
496
497         lzms_huffman_encode_symbol(enc, slot);
498         lzms_output_bitstream_put_bits(enc->os, extra_bits, num_extra_bits);
499 }
500
501 static void
502 lzms_begin_encode_item(struct lzms_compressor *ctx)
503 {
504         ctx->lru.lz.upcoming_offset = 0;
505         ctx->lru.delta.upcoming_offset = 0;
506         ctx->lru.delta.upcoming_power = 0;
507 }
508
509 static void
510 lzms_end_encode_item(struct lzms_compressor *ctx, u32 length)
511 {
512         LZMS_ASSERT(ctx->window_size - ctx->cur_window_pos >= length);
513         ctx->cur_window_pos += length;
514         lzms_update_lru_queues(&ctx->lru);
515 }
516
517 /* Encode a literal byte.  */
518 static void
519 lzms_encode_literal(struct lzms_compressor *ctx, u8 literal)
520 {
521         LZMS_DEBUG("Position %u: Encoding literal 0x%02x ('%c')",
522                    ctx->cur_window_pos, literal, literal);
523
524         lzms_begin_encode_item(ctx);
525
526         /* Main bit: 0 = a literal, not a match.  */
527         lzms_range_encode_bit(&ctx->main_range_encoder, 0);
528
529         /* Encode the literal using the current literal Huffman code.  */
530         lzms_huffman_encode_symbol(&ctx->literal_encoder, literal);
531
532         lzms_end_encode_item(ctx, 1);
533 }
534
535 /* Encode a (length, offset) pair (LZ match).  */
536 static void
537 lzms_encode_lz_match(struct lzms_compressor *ctx, u32 length, u32 offset)
538 {
539         int recent_offset_idx;
540
541         LZMS_DEBUG("Position %u: Encoding LZ match {length=%u, offset=%u}",
542                    ctx->cur_window_pos, length, offset);
543
544         LZMS_ASSERT(length <= ctx->window_size - ctx->cur_window_pos);
545         LZMS_ASSERT(offset <= ctx->cur_window_pos);
546         LZMS_ASSERT(!memcmp(&ctx->window[ctx->cur_window_pos],
547                             &ctx->window[ctx->cur_window_pos - offset],
548                             length));
549
550         lzms_begin_encode_item(ctx);
551
552         /* Main bit: 1 = a match, not a literal.  */
553         lzms_range_encode_bit(&ctx->main_range_encoder, 1);
554
555         /* Match bit: 0 = an LZ match, not a delta match.  */
556         lzms_range_encode_bit(&ctx->match_range_encoder, 0);
557
558         /* Determine if the offset can be represented as a recent offset.  */
559         for (recent_offset_idx = 0;
560              recent_offset_idx < LZMS_NUM_RECENT_OFFSETS;
561              recent_offset_idx++)
562                 if (offset == ctx->lru.lz.recent_offsets[recent_offset_idx])
563                         break;
564
565         if (recent_offset_idx == LZMS_NUM_RECENT_OFFSETS) {
566                 /* Explicit offset.  */
567
568                 /* LZ match bit: 0 = explicit offset, not a recent offset.  */
569                 lzms_range_encode_bit(&ctx->lz_match_range_encoder, 0);
570
571                 /* Encode the match offset.  */
572                 lzms_encode_offset(&ctx->lz_offset_encoder, offset);
573         } else {
574                 int i;
575
576                 /* Recent offset.  */
577
578                 /* LZ match bit: 1 = recent offset, not an explicit offset.  */
579                 lzms_range_encode_bit(&ctx->lz_match_range_encoder, 1);
580
581                 /* Encode the recent offset index.  A 1 bit is encoded for each
582                  * index passed up.  This sequence of 1 bits is terminated by a
583                  * 0 bit, or automatically when (LZMS_NUM_RECENT_OFFSETS - 1) 1
584                  * bits have been encoded.  */
585                 for (i = 0; i < recent_offset_idx; i++)
586                         lzms_range_encode_bit(&ctx->lz_repeat_match_range_encoders[i], 1);
587
588                 if (i < LZMS_NUM_RECENT_OFFSETS - 1)
589                         lzms_range_encode_bit(&ctx->lz_repeat_match_range_encoders[i], 0);
590
591                 /* Initial update of the LZ match offset LRU queue.  */
592                 for (; i < LZMS_NUM_RECENT_OFFSETS; i++)
593                         ctx->lru.lz.recent_offsets[i] = ctx->lru.lz.recent_offsets[i + 1];
594         }
595
596         /* Encode the match length.  */
597         lzms_encode_length(&ctx->length_encoder, length);
598
599         /* Save the match offset for later insertion at the front of the LZ
600          * match offset LRU queue.  */
601         ctx->lru.lz.upcoming_offset = offset;
602
603         lzms_end_encode_item(ctx, length);
604 }
605
606 #define LZMS_COST_SHIFT 5
607
608 /*#define LZMS_RC_COSTS_USE_FLOATING_POINT*/
609
610 static u32
611 lzms_rc_costs[LZMS_PROBABILITY_MAX + 1];
612
613 #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
614 #  include <math.h>
615 #endif
616
617 static void
618 lzms_do_init_rc_costs(void)
619 {
620         /* Fill in a table that maps range coding probabilities needed to code a
621          * bit X (0 or 1) to the number of bits (scaled by a constant factor, to
622          * handle fractional costs) needed to code that bit X.
623          *
624          * Consider the range of the range decoder.  To eliminate exactly half
625          * the range (logical probability of 0.5), we need exactly 1 bit.  For
626          * lower probabilities we need more bits and for higher probabilities we
627          * need fewer bits.  In general, a logical probability of N will
628          * eliminate the proportion 1 - N of the range; this information takes
629          * log2(1 / N) bits to encode.
630          *
631          * The below loop is simply calculating this number of bits for each
632          * possible probability allowed by the LZMS compression format, but
633          * without using real numbers.  To handle fractional probabilities, each
634          * cost is multiplied by (1 << LZMS_COST_SHIFT).  These techniques are
635          * based on those used by LZMA.
636          *
637          * Note that in LZMS, a probability x really means x / 64, and 0 / 64 is
638          * really interpreted as 1 / 64 and 64 / 64 is really interpreted as
639          * 63 / 64.
640          */
641         for (u32 i = 0; i <= LZMS_PROBABILITY_MAX; i++) {
642                 u32 prob = i;
643
644                 if (prob == 0)
645                         prob = 1;
646                 else if (prob == LZMS_PROBABILITY_MAX)
647                         prob = LZMS_PROBABILITY_MAX - 1;
648
649         #ifdef LZMS_RC_COSTS_USE_FLOATING_POINT
650                 lzms_rc_costs[i] = log2((double)LZMS_PROBABILITY_MAX / prob) *
651                                         (1 << LZMS_COST_SHIFT);
652         #else
653                 u32 w = prob;
654                 u32 bit_count = 0;
655                 for (u32 j = 0; j < LZMS_COST_SHIFT; j++) {
656                         w *= w;
657                         bit_count <<= 1;
658                         while (w >= (1U << 16)) {
659                                 w >>= 1;
660                                 ++bit_count;
661                         }
662                 }
663                 lzms_rc_costs[i] = (LZMS_PROBABILITY_BITS << LZMS_COST_SHIFT) -
664                                    (15 + bit_count);
665         #endif
666         }
667 }
668
669 static void
670 lzms_init_rc_costs(void)
671 {
672         static bool done = false;
673         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
674
675         if (unlikely(!done)) {
676                 pthread_mutex_lock(&mutex);
677                 if (!done) {
678                         lzms_do_init_rc_costs();
679                         done = true;
680                 }
681                 pthread_mutex_unlock(&mutex);
682         }
683 }
684
685 /*
686  * Return the cost to range-encode the specified bit when in the specified
687  * state.
688  *
689  * @enc         The range encoder to use.
690  * @cur_state   Current state, which indicates the probability entry to choose.
691  *              Updated by this function.
692  * @bit         The bit to encode (0 or 1).
693  */
694 static u32
695 lzms_rc_bit_cost(const struct lzms_range_encoder *enc, u8 *cur_state, int bit)
696 {
697         u32 prob_zero;
698         u32 prob_correct;
699
700         prob_zero = enc->prob_entries[*cur_state & enc->mask].num_recent_zero_bits;
701
702         *cur_state = (*cur_state << 1) | bit;
703
704         if (bit == 0)
705                 prob_correct = prob_zero;
706         else
707                 prob_correct = LZMS_PROBABILITY_MAX - prob_zero;
708
709         return lzms_rc_costs[prob_correct];
710 }
711
712 static u32
713 lzms_huffman_symbol_cost(const struct lzms_huffman_encoder *enc, u32 sym)
714 {
715         return enc->lens[sym] << LZMS_COST_SHIFT;
716 }
717
718 static u32
719 lzms_offset_cost(const struct lzms_huffman_encoder *enc, u32 offset)
720 {
721         u32 slot;
722         u32 num_extra_bits;
723         u32 cost = 0;
724
725         slot = lzms_get_position_slot(offset);
726
727         cost += lzms_huffman_symbol_cost(enc, slot);
728
729         num_extra_bits = lzms_extra_position_bits[slot];
730
731         cost += num_extra_bits << LZMS_COST_SHIFT;
732
733         return cost;
734 }
735
736 static u32
737 lzms_length_cost(const struct lzms_huffman_encoder *enc, u32 length)
738 {
739         u32 slot;
740         u32 num_extra_bits;
741         u32 cost = 0;
742
743         slot = lzms_get_length_slot(length);
744
745         cost += lzms_huffman_symbol_cost(enc, slot);
746
747         num_extra_bits = lzms_extra_length_bits[slot];
748
749         cost += num_extra_bits << LZMS_COST_SHIFT;
750
751         return cost;
752 }
753
754 static u32
755 lzms_get_matches(struct lzms_compressor *ctx, struct raw_match **matches_ret)
756 {
757         *matches_ret = ctx->matches;
758         return lz_bt_get_matches(&ctx->mf, ctx->matches);
759 }
760
761 static void
762 lzms_skip_bytes(struct lzms_compressor *ctx, u32 n)
763 {
764         lz_bt_skip_positions(&ctx->mf, n);
765 }
766
767 static u32
768 lzms_get_literal_cost(struct lzms_compressor *ctx,
769                       struct lzms_adaptive_state *state, u8 literal)
770 {
771         u32 cost = 0;
772
773         state->lru.upcoming_offset = 0;
774         lzms_update_lz_lru_queues(&state->lru);
775
776         cost += lzms_rc_bit_cost(&ctx->main_range_encoder,
777                                  &state->main_state, 0);
778
779         cost += lzms_huffman_symbol_cost(&ctx->literal_encoder, literal);
780
781         return cost;
782 }
783
784 static u32
785 lzms_get_lz_match_cost(struct lzms_compressor *ctx,
786                        struct lzms_adaptive_state *state,
787                        u32 length, u32 offset)
788 {
789         u32 cost = 0;
790         int recent_offset_idx;
791
792         cost += lzms_rc_bit_cost(&ctx->main_range_encoder,
793                                  &state->main_state, 1);
794         cost += lzms_rc_bit_cost(&ctx->match_range_encoder,
795                                  &state->match_state, 0);
796
797         for (recent_offset_idx = 0;
798              recent_offset_idx < LZMS_NUM_RECENT_OFFSETS;
799              recent_offset_idx++)
800                 if (offset == state->lru.recent_offsets[recent_offset_idx])
801                         break;
802
803         if (recent_offset_idx == LZMS_NUM_RECENT_OFFSETS) {
804                 /* Explicit offset.  */
805                 cost += lzms_rc_bit_cost(&ctx->lz_match_range_encoder,
806                                          &state->lz_match_state, 0);
807
808                 cost += lzms_offset_cost(&ctx->lz_offset_encoder, offset);
809         } else {
810                 int i;
811
812                 /* Recent offset.  */
813                 cost += lzms_rc_bit_cost(&ctx->lz_match_range_encoder,
814                                          &state->lz_match_state, 1);
815
816                 for (i = 0; i < recent_offset_idx; i++)
817                         cost += lzms_rc_bit_cost(&ctx->lz_repeat_match_range_encoders[i],
818                                                  &state->lz_repeat_match_state[i], 0);
819
820                 if (i < LZMS_NUM_RECENT_OFFSETS - 1)
821                         cost += lzms_rc_bit_cost(&ctx->lz_repeat_match_range_encoders[i],
822                                                  &state->lz_repeat_match_state[i], 1);
823
824
825                 /* Initial update of the LZ match offset LRU queue.  */
826                 for (; i < LZMS_NUM_RECENT_OFFSETS; i++)
827                         state->lru.recent_offsets[i] = state->lru.recent_offsets[i + 1];
828         }
829
830         cost += lzms_length_cost(&ctx->length_encoder, length);
831
832         state->lru.upcoming_offset = offset;
833         lzms_update_lz_lru_queues(&state->lru);
834
835         return cost;
836 }
837
838 static struct raw_match
839 lzms_match_chooser_reverse_list(struct lzms_compressor *ctx, unsigned cur_pos)
840 {
841         unsigned prev_link, saved_prev_link;
842         unsigned prev_match_offset, saved_prev_match_offset;
843
844         ctx->optimum_end_idx = cur_pos;
845
846         saved_prev_link = ctx->optimum[cur_pos].prev.link;
847         saved_prev_match_offset = ctx->optimum[cur_pos].prev.match_offset;
848
849         do {
850                 prev_link = saved_prev_link;
851                 prev_match_offset = saved_prev_match_offset;
852
853                 saved_prev_link = ctx->optimum[prev_link].prev.link;
854                 saved_prev_match_offset = ctx->optimum[prev_link].prev.match_offset;
855
856                 ctx->optimum[prev_link].next.link = cur_pos;
857                 ctx->optimum[prev_link].next.match_offset = prev_match_offset;
858
859                 cur_pos = prev_link;
860         } while (cur_pos != 0);
861
862         ctx->optimum_cur_idx = ctx->optimum[0].next.link;
863
864         return (struct raw_match)
865                 { .len = ctx->optimum_cur_idx,
866                   .offset = ctx->optimum[0].next.match_offset,
867                 };
868 }
869
870 /* This is similar to lzx_get_near_optimal_match() in lzx-compress.c.
871  * Read that one if you want to understand it.  */
872 static struct raw_match
873 lzms_get_near_optimal_match(struct lzms_compressor *ctx)
874 {
875         u32 num_matches;
876         struct raw_match *matches;
877         struct raw_match match;
878         u32 longest_len;
879         u32 longest_rep_len;
880         u32 longest_rep_offset;
881         struct raw_match *matchptr;
882         unsigned cur_pos;
883         unsigned end_pos;
884         struct lzms_adaptive_state initial_state;
885
886         if (ctx->optimum_cur_idx != ctx->optimum_end_idx) {
887                 match.len = ctx->optimum[ctx->optimum_cur_idx].next.link -
888                                     ctx->optimum_cur_idx;
889                 match.offset = ctx->optimum[ctx->optimum_cur_idx].next.match_offset;
890
891                 ctx->optimum_cur_idx = ctx->optimum[ctx->optimum_cur_idx].next.link;
892                 return match;
893         }
894
895         ctx->optimum_cur_idx = 0;
896         ctx->optimum_end_idx = 0;
897
898         longest_rep_len = ctx->params.min_match_length - 1;
899         if (lz_bt_get_position(&ctx->mf) >= 1) {
900                 u32 limit = min(ctx->params.max_match_length,
901                                 lz_bt_get_remaining_size(&ctx->mf));
902                 for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS; i++) {
903                         u32 offset = ctx->lru.lz.recent_offsets[i];
904                         const u8 *strptr = lz_bt_get_window_ptr(&ctx->mf);
905                         const u8 *matchptr = strptr - offset;
906                         u32 len = 0;
907                         while (len < limit && strptr[len] == matchptr[len])
908                                 len++;
909                         if (len > longest_rep_len) {
910                                 longest_rep_len = len;
911                                 longest_rep_offset = offset;
912                         }
913                 }
914         }
915
916         if (longest_rep_len >= ctx->params.nice_match_length) {
917                 lzms_skip_bytes(ctx, longest_rep_len);
918                 return (struct raw_match) {
919                         .len = longest_rep_len,
920                         .offset = longest_rep_offset,
921                 };
922         }
923
924         num_matches = lzms_get_matches(ctx, &matches);
925
926         if (num_matches) {
927                 longest_len = matches[num_matches - 1].len;
928                 if (longest_len >= ctx->params.nice_match_length) {
929                         lzms_skip_bytes(ctx, longest_len - 1);
930                         return matches[num_matches - 1];
931                 }
932         } else {
933                 longest_len = 1;
934         }
935
936         initial_state.lru = ctx->lru.lz;
937         initial_state.main_state = ctx->main_range_encoder.state;
938         initial_state.match_state = ctx->match_range_encoder.state;
939         initial_state.lz_match_state = ctx->lz_match_range_encoder.state;
940         for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
941                 initial_state.lz_repeat_match_state[i] = ctx->lz_repeat_match_range_encoders[i].state;
942
943         ctx->optimum[1].state = initial_state;
944         ctx->optimum[1].cost = lzms_get_literal_cost(ctx,
945                                                      &ctx->optimum[1].state,
946                                                      *(lz_bt_get_window_ptr(&ctx->mf) - 1));
947         ctx->optimum[1].prev.link = 0;
948
949         matchptr = matches;
950         for (u32 len = 2; len <= longest_len; len++) {
951                 u32 offset = matchptr->offset;
952
953                 ctx->optimum[len].state = initial_state;
954                 ctx->optimum[len].prev.link = 0;
955                 ctx->optimum[len].prev.match_offset = offset;
956                 ctx->optimum[len].cost = lzms_get_lz_match_cost(ctx,
957                                                                 &ctx->optimum[len].state,
958                                                                 len, offset);
959                 if (len == matchptr->len)
960                         matchptr++;
961         }
962         end_pos = longest_len;
963
964         if (longest_rep_len >= ctx->params.min_match_length) {
965                 struct lzms_adaptive_state state;
966                 u32 cost;
967
968                 while (end_pos < longest_rep_len)
969                         ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
970
971                 state = initial_state;
972                 cost = lzms_get_lz_match_cost(ctx,
973                                               &state,
974                                               longest_rep_len,
975                                               longest_rep_offset);
976                 if (cost <= ctx->optimum[longest_rep_len].cost) {
977                         ctx->optimum[longest_rep_len].state = state;
978                         ctx->optimum[longest_rep_len].prev.link = 0;
979                         ctx->optimum[longest_rep_len].prev.match_offset = longest_rep_offset;
980                         ctx->optimum[longest_rep_len].cost = cost;
981                 }
982         }
983
984         cur_pos = 0;
985         for (;;) {
986                 u32 cost;
987                 struct lzms_adaptive_state state;
988
989                 cur_pos++;
990
991                 if (cur_pos == end_pos || cur_pos == ctx->params.optim_array_length)
992                         return lzms_match_chooser_reverse_list(ctx, cur_pos);
993
994                 longest_rep_len = ctx->params.min_match_length - 1;
995                 u32 limit = min(ctx->params.max_match_length,
996                                 lz_bt_get_remaining_size(&ctx->mf));
997                 for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS; i++) {
998                         u32 offset = ctx->optimum[cur_pos].state.lru.recent_offsets[i];
999                         const u8 *strptr = lz_bt_get_window_ptr(&ctx->mf);
1000                         const u8 *matchptr = strptr - offset;
1001                         u32 len = 0;
1002                         while (len < limit && strptr[len] == matchptr[len])
1003                                 len++;
1004                         if (len > longest_rep_len) {
1005                                 longest_rep_len = len;
1006                                 longest_rep_offset = offset;
1007                         }
1008                 }
1009
1010                 if (longest_rep_len >= ctx->params.nice_match_length) {
1011                         match = lzms_match_chooser_reverse_list(ctx, cur_pos);
1012
1013                         ctx->optimum[cur_pos].next.match_offset = longest_rep_offset;
1014                         ctx->optimum[cur_pos].next.link = cur_pos + longest_rep_len;
1015                         ctx->optimum_end_idx = cur_pos + longest_rep_len;
1016
1017                         lzms_skip_bytes(ctx, longest_rep_len);
1018
1019                         return match;
1020                 }
1021
1022                 num_matches = lzms_get_matches(ctx, &matches);
1023
1024                 if (num_matches) {
1025                         longest_len = matches[num_matches - 1].len;
1026                         if (longest_len >= ctx->params.nice_match_length) {
1027                                 match = lzms_match_chooser_reverse_list(ctx, cur_pos);
1028
1029                                 ctx->optimum[cur_pos].next.match_offset =
1030                                         matches[num_matches - 1].offset;
1031                                 ctx->optimum[cur_pos].next.link = cur_pos + longest_len;
1032                                 ctx->optimum_end_idx = cur_pos + longest_len;
1033
1034                                 lzms_skip_bytes(ctx, longest_len - 1);
1035
1036                                 return match;
1037                         }
1038                 } else {
1039                         longest_len = 1;
1040                 }
1041
1042                 while (end_pos < cur_pos + longest_len)
1043                         ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
1044
1045                 state = ctx->optimum[cur_pos].state;
1046                 cost = ctx->optimum[cur_pos].cost +
1047                         lzms_get_literal_cost(ctx,
1048                                               &state,
1049                                               *(lz_bt_get_window_ptr(&ctx->mf) - 1));
1050                 if (cost < ctx->optimum[cur_pos + 1].cost) {
1051                         ctx->optimum[cur_pos + 1].state = state;
1052                         ctx->optimum[cur_pos + 1].cost = cost;
1053                         ctx->optimum[cur_pos + 1].prev.link = cur_pos;
1054                 }
1055
1056                 matchptr = matches;
1057                 for (u32 len = 2; len <= longest_len; len++) {
1058                         u32 offset;
1059
1060                         offset = matchptr->offset;
1061                         state = ctx->optimum[cur_pos].state;
1062
1063                         cost = ctx->optimum[cur_pos].cost +
1064                                 lzms_get_lz_match_cost(ctx, &state, len, offset);
1065                         if (cost < ctx->optimum[cur_pos + len].cost) {
1066                                 ctx->optimum[cur_pos + len].state = state;
1067                                 ctx->optimum[cur_pos + len].prev.link = cur_pos;
1068                                 ctx->optimum[cur_pos + len].prev.match_offset = offset;
1069                                 ctx->optimum[cur_pos + len].cost = cost;
1070                         }
1071                         if (len == matchptr->len)
1072                                 matchptr++;
1073                 }
1074
1075                 if (longest_rep_len >= ctx->params.min_match_length) {
1076
1077                         while (end_pos < cur_pos + longest_rep_len)
1078                                 ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
1079
1080                         state = ctx->optimum[cur_pos].state;
1081
1082                         cost = ctx->optimum[cur_pos].cost +
1083                                 lzms_get_lz_match_cost(ctx,
1084                                                        &state,
1085                                                        longest_rep_len,
1086                                                        longest_rep_offset);
1087                         if (cost <= ctx->optimum[cur_pos + longest_rep_len].cost) {
1088                                 ctx->optimum[cur_pos + longest_rep_len].state =
1089                                         state;
1090                                 ctx->optimum[cur_pos + longest_rep_len].prev.link =
1091                                         cur_pos;
1092                                 ctx->optimum[cur_pos + longest_rep_len].prev.match_offset =
1093                                         longest_rep_offset;
1094                                 ctx->optimum[cur_pos + longest_rep_len].cost =
1095                                         cost;
1096                         }
1097                 }
1098         }
1099 }
1100
1101 /*
1102  * The main loop for the LZMS compressor.
1103  *
1104  * Notes:
1105  *
1106  * - This uses near-optimal LZ parsing backed by a binary tree match-finder.
1107  *
1108  * - This does not output any delta matches.
1109  *
1110  * - The costs of literals and matches are estimated using the range encoder
1111  *   states and the semi-adaptive Huffman codes.  Except for range encoding
1112  *   states, costs are assumed to be constant throughout a single run of the
1113  *   parsing algorithm, which can parse up to @optim_array_length (from the
1114  *   `struct wimlib_lzms_compressor_params') bytes of data.  This introduces a
1115  *   source of inaccuracy because the probabilities and Huffman codes can change
1116  *   over this part of the data.
1117  */
1118 static void
1119 lzms_encode(struct lzms_compressor *ctx)
1120 {
1121         struct raw_match match;
1122
1123         /* Load window into the binary tree match-finder.  */
1124         lz_bt_load_window(&ctx->mf, ctx->window, ctx->window_size);
1125
1126         /* Reset the match-chooser.  */
1127         ctx->optimum_cur_idx = 0;
1128         ctx->optimum_end_idx = 0;
1129
1130         while (ctx->cur_window_pos != ctx->window_size) {
1131                 match = lzms_get_near_optimal_match(ctx);
1132                 if (match.len <= 1)
1133                         lzms_encode_literal(ctx, ctx->window[ctx->cur_window_pos]);
1134                 else
1135                         lzms_encode_lz_match(ctx, match.len, match.offset);
1136         }
1137 }
1138
1139 static void
1140 lzms_init_range_encoder(struct lzms_range_encoder *enc,
1141                         struct lzms_range_encoder_raw *rc, u32 num_states)
1142 {
1143         enc->rc = rc;
1144         enc->state = 0;
1145         enc->mask = num_states - 1;
1146         for (u32 i = 0; i < num_states; i++) {
1147                 enc->prob_entries[i].num_recent_zero_bits = LZMS_INITIAL_PROBABILITY;
1148                 enc->prob_entries[i].recent_bits = LZMS_INITIAL_RECENT_BITS;
1149         }
1150 }
1151
1152 static void
1153 lzms_init_huffman_encoder(struct lzms_huffman_encoder *enc,
1154                           struct lzms_output_bitstream *os,
1155                           unsigned num_syms,
1156                           unsigned rebuild_freq)
1157 {
1158         enc->os = os;
1159         enc->num_syms_written = 0;
1160         enc->rebuild_freq = rebuild_freq;
1161         enc->num_syms = num_syms;
1162         for (unsigned i = 0; i < num_syms; i++)
1163                 enc->sym_freqs[i] = 1;
1164
1165         make_canonical_huffman_code(enc->num_syms,
1166                                     LZMS_MAX_CODEWORD_LEN,
1167                                     enc->sym_freqs,
1168                                     enc->lens,
1169                                     enc->codewords);
1170 }
1171
1172 /* Initialize the LZMS compressor.  */
1173 static void
1174 lzms_init_compressor(struct lzms_compressor *ctx, const u8 *udata, u32 ulen,
1175                      le16 *cdata, u32 clen16)
1176 {
1177         unsigned num_position_slots;
1178
1179         /* Copy the uncompressed data into the @ctx->window buffer.  */
1180         memcpy(ctx->window, udata, ulen);
1181         ctx->cur_window_pos = 0;
1182         ctx->window_size = ulen;
1183
1184         /* Initialize the raw range encoder (writing forwards).  */
1185         lzms_range_encoder_raw_init(&ctx->rc, cdata, clen16);
1186
1187         /* Initialize the output bitstream for Huffman symbols and verbatim bits
1188          * (writing backwards).  */
1189         lzms_output_bitstream_init(&ctx->os, cdata, clen16);
1190
1191         /* Calculate the number of position slots needed for this compressed
1192          * block.  */
1193         num_position_slots = lzms_get_position_slot(ulen - 1) + 1;
1194
1195         LZMS_DEBUG("Using %u position slots", num_position_slots);
1196
1197         /* Initialize Huffman encoders for each alphabet used in the compressed
1198          * representation.  */
1199         lzms_init_huffman_encoder(&ctx->literal_encoder, &ctx->os,
1200                                   LZMS_NUM_LITERAL_SYMS,
1201                                   LZMS_LITERAL_CODE_REBUILD_FREQ);
1202
1203         lzms_init_huffman_encoder(&ctx->lz_offset_encoder, &ctx->os,
1204                                   num_position_slots,
1205                                   LZMS_LZ_OFFSET_CODE_REBUILD_FREQ);
1206
1207         lzms_init_huffman_encoder(&ctx->length_encoder, &ctx->os,
1208                                   LZMS_NUM_LEN_SYMS,
1209                                   LZMS_LENGTH_CODE_REBUILD_FREQ);
1210
1211         lzms_init_huffman_encoder(&ctx->delta_offset_encoder, &ctx->os,
1212                                   num_position_slots,
1213                                   LZMS_DELTA_OFFSET_CODE_REBUILD_FREQ);
1214
1215         lzms_init_huffman_encoder(&ctx->delta_power_encoder, &ctx->os,
1216                                   LZMS_NUM_DELTA_POWER_SYMS,
1217                                   LZMS_DELTA_POWER_CODE_REBUILD_FREQ);
1218
1219         /* Initialize range encoders, all of which wrap around the same
1220          * lzms_range_encoder_raw.  */
1221         lzms_init_range_encoder(&ctx->main_range_encoder,
1222                                 &ctx->rc, LZMS_NUM_MAIN_STATES);
1223
1224         lzms_init_range_encoder(&ctx->match_range_encoder,
1225                                 &ctx->rc, LZMS_NUM_MATCH_STATES);
1226
1227         lzms_init_range_encoder(&ctx->lz_match_range_encoder,
1228                                 &ctx->rc, LZMS_NUM_LZ_MATCH_STATES);
1229
1230         for (size_t i = 0; i < ARRAY_LEN(ctx->lz_repeat_match_range_encoders); i++)
1231                 lzms_init_range_encoder(&ctx->lz_repeat_match_range_encoders[i],
1232                                         &ctx->rc, LZMS_NUM_LZ_REPEAT_MATCH_STATES);
1233
1234         lzms_init_range_encoder(&ctx->delta_match_range_encoder,
1235                                 &ctx->rc, LZMS_NUM_DELTA_MATCH_STATES);
1236
1237         for (size_t i = 0; i < ARRAY_LEN(ctx->delta_repeat_match_range_encoders); i++)
1238                 lzms_init_range_encoder(&ctx->delta_repeat_match_range_encoders[i],
1239                                         &ctx->rc, LZMS_NUM_DELTA_REPEAT_MATCH_STATES);
1240
1241         /* Initialize LRU match information.  */
1242         lzms_init_lru_queues(&ctx->lru);
1243 }
1244
1245 /* Flush the output streams, prepare the final compressed data, and return its
1246  * size in bytes.
1247  *
1248  * A return value of 0 indicates that the data could not be compressed to fit in
1249  * the available space.  */
1250 static size_t
1251 lzms_finalize(struct lzms_compressor *ctx, u8 *cdata, size_t csize_avail)
1252 {
1253         size_t num_forwards_bytes;
1254         size_t num_backwards_bytes;
1255         size_t compressed_size;
1256
1257         /* Flush both the forwards and backwards streams, and make sure they
1258          * didn't cross each other and start overwriting each other's data.  */
1259         if (!lzms_output_bitstream_flush(&ctx->os)) {
1260                 LZMS_DEBUG("Backwards bitstream overrun.");
1261                 return 0;
1262         }
1263
1264         if (!lzms_range_encoder_raw_flush(&ctx->rc)) {
1265                 LZMS_DEBUG("Forwards bitstream overrun.");
1266                 return 0;
1267         }
1268
1269         if (ctx->rc.out > ctx->os.out) {
1270                 LZMS_DEBUG("Two bitstreams crossed.");
1271                 return 0;
1272         }
1273
1274         /* Now the compressed buffer contains the data output by the forwards
1275          * bitstream, then empty space, then data output by the backwards
1276          * bitstream.  Move the data output by the backwards bitstream to be
1277          * adjacent to the data output by the forward bitstream, and calculate
1278          * the compressed size that this results in.  */
1279         num_forwards_bytes = (u8*)ctx->rc.out - (u8*)cdata;
1280         num_backwards_bytes = ((u8*)cdata + csize_avail) - (u8*)ctx->os.out;
1281
1282         memmove(cdata + num_forwards_bytes, ctx->os.out, num_backwards_bytes);
1283
1284         compressed_size = num_forwards_bytes + num_backwards_bytes;
1285         LZMS_DEBUG("num_forwards_bytes=%zu, num_backwards_bytes=%zu, "
1286                    "compressed_size=%zu",
1287                    num_forwards_bytes, num_backwards_bytes, compressed_size);
1288         LZMS_ASSERT(compressed_size % 2 == 0);
1289         return compressed_size;
1290 }
1291
1292 static size_t
1293 lzms_compress(const void *uncompressed_data, size_t uncompressed_size,
1294               void *compressed_data, size_t compressed_size_avail, void *_ctx)
1295 {
1296         struct lzms_compressor *ctx = _ctx;
1297         size_t compressed_size;
1298
1299         LZMS_DEBUG("uncompressed_size=%zu, compressed_size_avail=%zu",
1300                    uncompressed_size, compressed_size_avail);
1301
1302         /* Make sure the uncompressed size is compatible with this compressor.
1303          */
1304         if (uncompressed_size > ctx->max_block_size) {
1305                 LZMS_DEBUG("Can't compress %zu bytes: LZMS context "
1306                            "only supports %u bytes",
1307                            uncompressed_size, ctx->max_block_size);
1308                 return 0;
1309         }
1310
1311         /* Don't bother compressing extremely small inputs.  */
1312         if (uncompressed_size < 4) {
1313                 LZMS_DEBUG("Input too small to bother compressing.");
1314                 return 0;
1315         }
1316
1317         /* Cap the available compressed size to a 32-bit integer and round it
1318          * down to the nearest multiple of 2.  */
1319         if (compressed_size_avail > UINT32_MAX)
1320                 compressed_size_avail = UINT32_MAX;
1321         if (compressed_size_avail & 1)
1322                 compressed_size_avail--;
1323
1324         /* Initialize the compressor structures.  */
1325         lzms_init_compressor(ctx, uncompressed_data, uncompressed_size,
1326                              compressed_data, compressed_size_avail / 2);
1327
1328         /* Preprocess the uncompressed data.  */
1329         lzms_x86_filter(ctx->window, ctx->window_size,
1330                         ctx->last_target_usages, false);
1331
1332         /* Compute and encode a literal/match sequence that decompresses to the
1333          * preprocessed data.  */
1334         lzms_encode(ctx);
1335
1336         /* Get and return the compressed data size.  */
1337         compressed_size = lzms_finalize(ctx, compressed_data,
1338                                         compressed_size_avail);
1339
1340         if (compressed_size == 0) {
1341                 LZMS_DEBUG("Data did not compress to requested size or less.");
1342                 return 0;
1343         }
1344
1345         LZMS_DEBUG("Compressed %zu => %zu bytes",
1346                    uncompressed_size, compressed_size);
1347
1348 #if defined(ENABLE_VERIFY_COMPRESSION) || defined(ENABLE_LZMS_DEBUG)
1349         /* Verify that we really get the same thing back when decompressing.  */
1350         {
1351                 struct wimlib_decompressor *decompressor;
1352
1353                 LZMS_DEBUG("Verifying LZMS compression.");
1354
1355                 if (0 == wimlib_create_decompressor(WIMLIB_COMPRESSION_TYPE_LZMS,
1356                                                     ctx->max_block_size,
1357                                                     NULL,
1358                                                     &decompressor))
1359                 {
1360                         int ret;
1361                         ret = wimlib_decompress(compressed_data,
1362                                                 compressed_size,
1363                                                 ctx->window,
1364                                                 uncompressed_size,
1365                                                 decompressor);
1366                         wimlib_free_decompressor(decompressor);
1367
1368                         if (ret) {
1369                                 ERROR("Failed to decompress data we "
1370                                       "compressed using LZMS algorithm");
1371                                 wimlib_assert(0);
1372                                 return 0;
1373                         }
1374                         if (memcmp(uncompressed_data, ctx->window,
1375                                    uncompressed_size))
1376                         {
1377                                 ERROR("Data we compressed using LZMS algorithm "
1378                                       "didn't decompress to original");
1379                                 wimlib_assert(0);
1380                                 return 0;
1381                         }
1382                 } else {
1383                         WARNING("Failed to create decompressor for "
1384                                 "data verification!");
1385                 }
1386         }
1387 #endif /* ENABLE_LZMS_DEBUG || ENABLE_VERIFY_COMPRESSION  */
1388
1389         return compressed_size;
1390 }
1391
1392 static void
1393 lzms_free_compressor(void *_ctx)
1394 {
1395         struct lzms_compressor *ctx = _ctx;
1396
1397         if (ctx) {
1398                 FREE(ctx->window);
1399                 FREE(ctx->matches);
1400                 lz_bt_destroy(&ctx->mf);
1401                 FREE(ctx->optimum);
1402                 FREE(ctx);
1403         }
1404 }
1405
1406 static const struct wimlib_lzms_compressor_params lzms_default = {
1407         .hdr = {
1408                 .size = sizeof(struct wimlib_lzms_compressor_params),
1409         },
1410         .min_match_length = 2,
1411         .max_match_length = UINT32_MAX,
1412         .nice_match_length = 32,
1413         .max_search_depth = 50,
1414         .optim_array_length = 1024,
1415 };
1416
1417 static bool
1418 lzms_params_valid(const struct wimlib_compressor_params_header *);
1419
1420 static const struct wimlib_lzms_compressor_params *
1421 lzms_get_params(const struct wimlib_compressor_params_header *_params)
1422 {
1423         const struct wimlib_lzms_compressor_params *params =
1424                 (const struct wimlib_lzms_compressor_params*)_params;
1425
1426         if (params == NULL)
1427                 params = &lzms_default;
1428
1429         LZMS_ASSERT(lzms_params_valid(&params->hdr));
1430
1431         return params;
1432 }
1433
1434 static int
1435 lzms_create_compressor(size_t max_block_size,
1436                        const struct wimlib_compressor_params_header *_params,
1437                        void **ctx_ret)
1438 {
1439         struct lzms_compressor *ctx;
1440         const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
1441
1442         if (max_block_size == 0 || max_block_size >= INT32_MAX) {
1443                 LZMS_DEBUG("Invalid max_block_size (%u)", max_block_size);
1444                 return WIMLIB_ERR_INVALID_PARAM;
1445         }
1446
1447         ctx = CALLOC(1, sizeof(struct lzms_compressor));
1448         if (ctx == NULL)
1449                 goto oom;
1450
1451         ctx->window = MALLOC(max_block_size);
1452         if (ctx->window == NULL)
1453                 goto oom;
1454
1455         ctx->matches = MALLOC(min(params->max_match_length -
1456                                         params->min_match_length + 1,
1457                                   params->max_search_depth + 2) *
1458                                 sizeof(ctx->matches[0]));
1459         if (ctx->matches == NULL)
1460                 goto oom;
1461
1462         if (!lz_bt_init(&ctx->mf,
1463                         max_block_size,
1464                         params->min_match_length,
1465                         params->max_match_length,
1466                         params->nice_match_length,
1467                         params->max_search_depth))
1468                 goto oom;
1469
1470         ctx->optimum = MALLOC((params->optim_array_length +
1471                                min(params->nice_match_length,
1472                                    params->max_match_length)) *
1473                                         sizeof(ctx->optimum[0]));
1474         if (!ctx->optimum)
1475                 goto oom;
1476
1477         /* Initialize position and length slot data if not done already.  */
1478         lzms_init_slots();
1479
1480         /* Initialize range encoding cost table if not done already.  */
1481         lzms_init_rc_costs();
1482
1483         ctx->max_block_size = max_block_size;
1484         memcpy(&ctx->params, params, sizeof(*params));
1485
1486         *ctx_ret = ctx;
1487         return 0;
1488
1489 oom:
1490         lzms_free_compressor(ctx);
1491         return WIMLIB_ERR_NOMEM;
1492 }
1493
1494 static u64
1495 lzms_get_needed_memory(size_t max_block_size,
1496                        const struct wimlib_compressor_params_header *_params)
1497 {
1498         const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
1499
1500         u64 size = 0;
1501
1502         size += max_block_size;
1503         size += sizeof(struct lzms_compressor);
1504         size += lz_bt_get_needed_memory(max_block_size);
1505         size += (params->optim_array_length +
1506                  min(params->nice_match_length,
1507                      params->max_match_length)) *
1508                          sizeof(((struct lzms_compressor *)0)->optimum[0]);
1509         size += min(params->max_match_length - params->min_match_length + 1,
1510                     params->max_search_depth + 2) *
1511                 sizeof(((struct lzms_compressor*)0)->matches[0]);
1512         return size;
1513 }
1514
1515 static bool
1516 lzms_params_valid(const struct wimlib_compressor_params_header *_params)
1517 {
1518         const struct wimlib_lzms_compressor_params *params =
1519                 (const struct wimlib_lzms_compressor_params*)_params;
1520
1521         if (params->hdr.size != sizeof(*params) ||
1522             params->max_match_length < params->min_match_length ||
1523             params->min_match_length < 2 ||
1524             params->optim_array_length == 0 ||
1525             min(params->max_match_length, params->nice_match_length) > 65536)
1526                 return false;
1527
1528         return true;
1529 }
1530
1531 const struct compressor_ops lzms_compressor_ops = {
1532         .params_valid       = lzms_params_valid,
1533         .get_needed_memory  = lzms_get_needed_memory,
1534         .create_compressor  = lzms_create_compressor,
1535         .compress           = lzms_compress,
1536         .free_compressor    = lzms_free_compressor,
1537 };