21 #if TUKLIB_GNUC_REQ(7, 0)
22 # pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
29 #define seq_4(seq) seq
31 #define seq_6(seq) seq
33 #define seq_8(seq) seq
35 #define seq_len(seq) \
40 #define len_decode(target, ld, pos_state, seq) \
42 case seq ## _CHOICE: \
43 rc_if_0(ld.choice, seq ## _CHOICE) { \
44 rc_update_0(ld.choice); \
45 probs = ld.low[pos_state];\
46 limit = LEN_LOW_SYMBOLS; \
47 target = MATCH_LEN_MIN; \
49 rc_update_1(ld.choice); \
50 case seq ## _CHOICE2: \
51 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
52 rc_update_0(ld.choice2); \
53 probs = ld.mid[pos_state]; \
54 limit = LEN_MID_SYMBOLS; \
55 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
57 rc_update_1(ld.choice2); \
59 limit = LEN_HIGH_SYMBOLS; \
60 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \
65 case seq ## _BITTREE: \
67 rc_bit(probs[symbol], , , seq ## _BITTREE); \
68 } while (symbol < limit); \
69 target += symbol - limit; \
99 #define seq_len(seq) \
117 #define len_decode(target, ld, pos_state, seq) \
120 case seq ## _CHOICE: \
121 rc_if_0(ld.choice, seq ## _CHOICE) { \
122 rc_update_0(ld.choice); \
123 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW0); \
124 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW1); \
125 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW2); \
126 target = symbol - LEN_LOW_SYMBOLS + MATCH_LEN_MIN; \
128 rc_update_1(ld.choice); \
129 case seq ## _CHOICE2: \
130 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
131 rc_update_0(ld.choice2); \
132 rc_bit_case(ld.mid[pos_state][symbol], , , \
134 rc_bit_case(ld.mid[pos_state][symbol], , , \
136 rc_bit_case(ld.mid[pos_state][symbol], , , \
138 target = symbol - LEN_MID_SYMBOLS \
139 + MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
141 rc_update_1(ld.choice2); \
142 rc_bit_case(ld.high[symbol], , , seq ## _HIGH0); \
143 rc_bit_case(ld.high[symbol], , , seq ## _HIGH1); \
144 rc_bit_case(ld.high[symbol], , , seq ## _HIGH2); \
145 rc_bit_case(ld.high[symbol], , , seq ## _HIGH3); \
146 rc_bit_case(ld.high[symbol], , , seq ## _HIGH4); \
147 rc_bit_case(ld.high[symbol], , , seq ## _HIGH5); \
148 rc_bit_case(ld.high[symbol], , , seq ## _HIGH6); \
149 rc_bit_case(ld.high[symbol], , , seq ## _HIGH7); \
150 target = symbol - LEN_HIGH_SYMBOLS \
152 + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; \
250 seq_8(SEQ_LITERAL_MATCHED),
316 const size_t dict_start = dict.
pos;
328 const uint32_t pos_mask = coder->pos_mask;
338 const uint32_t literal_pos_mask = coder->literal_pos_mask;
339 const uint32_t literal_context_bits = coder->literal_context_bits;
348 const bool no_eopm = coder->uncompressed_size
350 if (no_eopm && coder->uncompressed_size < dict.
limit - dict.
pos)
355 switch (coder->sequence)
360 pos_state = dict.
pos & pos_mask;
367 rc_if_0(coder->is_match[
state][pos_state], SEQ_IS_MATCH) {
373 literal_context_bits, literal_pos_mask,
382 rc_bit(probs[symbol], , , SEQ_LITERAL);
383 }
while (symbol < (1 << 8));
410 case SEQ_LITERAL_MATCHED:
418 rc_bit(probs[subcoder_index],
421 SEQ_LITERAL_MATCHED);
430 }
while (symbol < (1 << 8));
438 match_bit = len & offset; \
439 subcoder_index = offset + match_bit + symbol; \
440 rc_bit(probs[subcoder_index], \
441 offset &= ~match_bit, \
442 offset &= match_bit, \
445 d(SEQ_LITERAL_MATCHED0);
447 d(SEQ_LITERAL_MATCHED1);
449 d(SEQ_LITERAL_MATCHED2);
451 d(SEQ_LITERAL_MATCHED3);
453 d(SEQ_LITERAL_MATCHED4);
455 d(SEQ_LITERAL_MATCHED5);
457 d(SEQ_LITERAL_MATCHED6);
459 d(SEQ_LITERAL_MATCHED7);
484 case SEQ_LITERAL_WRITE:
486 coder->sequence = SEQ_LITERAL_WRITE;
513 pos_state, SEQ_MATCH_LEN);
523 rc_bit(probs[symbol], , , SEQ_DIST_SLOT);
544 limit = (symbol >> 1) - 1;
546 rep0 = 2 + (symbol & 1);
564 probs = coder->pos_special + rep0
626 }
while (--
limit > 0);
643 rc_bit(coder->pos_align[symbol], ,
644 rep0 += 1, SEQ_ALIGN0);
646 rc_bit(coder->pos_align[symbol], ,
647 rep0 += 2, SEQ_ALIGN1);
649 rc_bit(coder->pos_align[symbol], ,
650 rep0 += 4, SEQ_ALIGN2);
656 rep0 += 8, SEQ_ALIGN3);
664 if (coder->uncompressed_size
708 case SEQ_IS_REP0_LONG:
719 coder->sequence = SEQ_SHORTREP;
776 pos_state, SEQ_REP_LEN);
791 coder->sequence = SEQ_COPY;
797 coder->sequence = SEQ_IS_MATCH;
803 dictptr->pos = dict.
pos;
804 dictptr->full = dict.
full;
808 coder->state =
state;
814 coder->probs = probs;
815 coder->symbol = symbol;
816 coder->limit =
limit;
823 coder->uncompressed_size -= dict.
pos - dict_start;
828 if (coder->uncompressed_size == 0 && ret ==
LZMA_OK
829 && coder->sequence != SEQ_NORMALIZE)
830 ret = coder->sequence == SEQ_IS_MATCH
915 for (
uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
1002 if (
byte > (4 * 5 + 4) * 9 + 8)
1038 if (props_size != 5)
const lzma_allocator const uint8_t size_t * in_pos
const lzma_allocator const uint8_t size_t in_size
const lzma_allocator * allocator
const lzma_allocator const uint8_t * in
const lzma_allocator const uint8_t size_t uint8_t * out
const lzma_filter * filters
const lzma_allocator const uint8_t * props
static const char struct stat static buf struct stat static buf static vhangup int options
lzma_ret lzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options))
uint64_t lzma_lz_decoder_memusage(size_t dictionary_size)
static bool dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len)
Repeat *len bytes at distance.
static bool dict_put(lzma_dict *dict, uint8_t byte)
static uint8_t dict_get(const lzma_dict *const dict, const uint32_t distance)
Get a byte from the history buffer.
static bool dict_is_distance_valid(const lzma_dict *const dict, const size_t distance)
Validate the match distance.
Private definitions common to LZMA encoder and decoder.
#define LITERAL_CODER_SIZE
#define is_literal_state(state)
Test if the previous state was a literal.
#define update_match(state)
Indicate that the latest state was a match.
#define get_dist_state(len)
#define literal_subcoder(probs, lc, lp_mask, pos, prev_byte)
static void literal_init(probability(*probs)[LITERAL_CODER_SIZE], uint32_t lc, uint32_t lp)
#define STATES
Total number of states.
#define update_long_rep(state)
Indicate that the latest state was a long repeated match.
#define LITERAL_CODERS_MAX
Maximum number of literal coders.
static bool is_lclppb_valid(const lzma_options_lzma *options)
Validates lc, lp, and pb.
#define update_short_rep(state)
Indicate that the latest state was a short match.
lzma_ret lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *opt, lzma_lz_options *lz_options)
bool lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
Decodes the LZMA Properties byte (lc/lp/pb)
static void lzma_decoder_reset(void *coder_ptr, const void *opt)
static void lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
uint64_t lzma_lzma_decoder_memusage_nocheck(const void *options)
lzma_ret lzma_lzma_props_decode(void **options, const lzma_allocator *allocator, const uint8_t *props, size_t props_size)
uint64_t lzma_lzma_decoder_memusage(const void *options)
lzma_ret lzma_lzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Allocates and initializes LZMA decoder.
static lzma_ret lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
#define len_decode(target, ld, pos_state, seq)
static lzma_ret lzma_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)
static void literal(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position)
assert(limit<=UINT32_MAX/2)
static uint32_t const uint8_t uint32_t uint32_t limit
#define bittree_reset(probs, bit_levels)
uint16_t probability
Type of probabilities used with range coder.
#define rc_from_local(range_decoder, in_pos)
Stores the local copes back to the range decoder structure.
#define rc_bit_case(prob, action0, action1, seq)
#define rc_normalize(seq)
#define rc_direct(dest, seq)
Decode a bit without using a probability.
#define rc_update_0(prob)
#define rc_update_1(prob)
#define rc_is_finished(range_decoder)
#define rc_bit_last(prob, action0, action1, seq)
#define rc_if_0(prob, seq)
#define rc_to_local(range_decoder, in_pos)
#define rc_bit(prob, action0, action1, seq)
#define rc_reset(range_decoder)
Resets the range decoder structure.
static lzma_ret rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
Reads the first five bytes to initialize the range decoder.
Custom functions for memory handling.
Length decoder probabilities; see comments in lzma_common.h.
probability high[LEN_HIGH_SYMBOLS]
probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS]
probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS]
void * coder
Data specific to the LZ-based decoder.
void(* set_uncompressed)(void *coder, lzma_vli uncompressed_size)
Set the uncompressed size.
lzma_ret(* code)(void *coder, lzma_dict *restrict dict, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size)
Function to decode from in[] to *dict.
void(* reset)(void *coder, const void *options)
const uint8_t * preset_dict
TODO: Comment.
size_t dict_size
Size of the history buffer.
probability is_rep1[STATES]
probability is_rep0_long[STATES][POS_STATES_MAX]
probability is_rep0[STATES]
lzma_vli uncompressed_size
probability is_rep2[STATES]
If 0, distance of a repeated match is rep2. Otherwise it is rep3.
uint32_t rep0
Distance of the latest match.
probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE]
Literals; see comments in lzma_common.h.
probability is_rep[STATES]
If 1, it's a repeated match. The distance is one of rep0 .. rep3.
uint32_t literal_context_bits
probability pos_align[ALIGN_SIZE]
uint32_t literal_pos_mask
probability * probs
Base of the current probability tree.
enum lzma_lzma1_decoder::@657 sequence
Position where to continue the decoder loop.
uint32_t rep3
Distance of fourth latest match.
probability is_match[STATES][POS_STATES_MAX]
If 1, it's a match. Otherwise it's a single 8-bit literal.
probability dist_slot[DIST_STATES][DIST_SLOTS]
lzma_length_decoder rep_len_decoder
Length of a repeated match.
lzma_length_decoder match_len_decoder
Length of a normal match.
uint32_t rep2
Distance of third latest match.
probability pos_special[FULL_DISTANCES - DIST_MODEL_END]
uint32_t rep1
Distance of second latest match.
Hold data and function pointers of the next filter in the chain.
Options specific to the LZMA1 and LZMA2 filters.
const uint8_t * preset_dict
Pointer to an initial dictionary.
uint32_t preset_dict_size
Size of the preset dictionary.
uint32_t dict_size
Dictionary size in bytes.
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
uint64_t uncompressed_size
static uint32_t read32le(const uint8_t *buf)
void error(const char *msg)
uint64_t lzma_vli
Variable-length integer type.
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
lzma_ret
Return values used by several functions in liblzma.
@ LZMA_PROG_ERROR
Programming error.
@ LZMA_DATA_ERROR
Data is corrupt.
@ LZMA_MEM_ERROR
Cannot allocate memory.
@ LZMA_STREAM_END
End of stream was reached.
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
@ LZMA_OK
Operation completed successfully.
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.