4 * Declarations shared between LZMS compression and decompression.
10 #include "wimlib/compiler.h"
11 #include "wimlib/lzms_constants.h"
12 #include "wimlib/types.h"
14 //#define ENABLE_LZMS_DEBUG
15 #ifdef ENABLE_LZMS_DEBUG
16 # define LZMS_DEBUG DEBUG
17 # define LZMS_ASSERT wimlib_assert
18 # include "wimlib/assert.h"
19 # include "wimlib/error.h"
21 # define LZMS_DEBUG(format, ...)
22 # define LZMS_ASSERT(...)
26 lzms_x86_filter(u8 data[], s32 size, s32 last_target_usages[], bool undo);
28 /* Probability entry for use by the range coder when in a specific state. */
29 struct lzms_probability_entry {
31 /* Number of zeroes in the most recent LZMS_PROBABILITY_MAX bits that
32 * have been coded using this probability entry. This is a cached value
33 * because it can be computed as LZMS_PROBABILITY_MAX minus the number
34 * of bits set in the low-order LZMS_PROBABILITY_MAX bits of
36 u32 num_recent_zero_bits;
38 /* The most recent LZMS_PROBABILITY_MAX bits that have been coded using
39 * this probability entry. The size of this variable, in bits, must be
40 * at least LZMS_PROBABILITY_MAX. */
44 /* Offset slot tables */
45 extern const u32 lzms_offset_slot_base[LZMS_MAX_NUM_OFFSET_SYMS + 1];
46 extern const u8 lzms_extra_offset_bits[LZMS_MAX_NUM_OFFSET_SYMS];
48 /* Length slot tables */
49 extern const u32 lzms_length_slot_base[LZMS_NUM_LENGTH_SYMS + 1];
50 extern const u8 lzms_extra_length_bits[LZMS_NUM_LENGTH_SYMS];
53 lzms_get_slot(u32 value, const u32 slot_base_tab[], unsigned num_slots);
55 /* Return the offset slot for the specified offset */
56 static inline unsigned
57 lzms_get_offset_slot(u32 offset)
59 return lzms_get_slot(offset, lzms_offset_slot_base, LZMS_MAX_NUM_OFFSET_SYMS);
62 /* Return the length slot for the specified length */
63 static inline unsigned
64 lzms_get_length_slot(u32 length)
66 return lzms_get_slot(length, lzms_length_slot_base, LZMS_NUM_LENGTH_SYMS);
70 lzms_get_num_offset_slots(size_t uncompressed_size);
73 lzms_init_probability_entries(struct lzms_probability_entry *entries, size_t count);
76 lzms_init_symbol_frequencies(u32 freqs[], size_t num_syms);
78 /* Given a decoded bit, update the probability entry. */
80 lzms_update_probability_entry(struct lzms_probability_entry *prob_entry, int bit)
84 BUILD_BUG_ON(LZMS_PROBABILITY_MAX != sizeof(prob_entry->recent_bits) * 8);
86 delta_zero_bits = (s32)(prob_entry->recent_bits >> (LZMS_PROBABILITY_MAX - 1)) - bit;
88 prob_entry->num_recent_zero_bits += delta_zero_bits;
89 prob_entry->recent_bits <<= 1;
90 prob_entry->recent_bits |= bit;
93 /* Given a probability entry, return the chance out of LZMS_PROBABILITY_MAX that
94 * the next decoded bit will be a 0. */
96 lzms_get_probability(const struct lzms_probability_entry *prob_entry)
100 prob = prob_entry->num_recent_zero_bits;
102 /* 0% and 100% probabilities aren't allowed. */
105 if (prob == LZMS_PROBABILITY_MAX)
110 #endif /* _WIMLIB_LZMS_H */