1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_decoder.c
4 /// \brief LZMA decoder
6 // Authors: Igor Pavlov
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
12 ///////////////////////////////////////////////////////////////////////////////
14 #include "lz_decoder.h"
15 #include "lzma_common.h"
16 #include "lzma_decoder.h"
17 #include "range_decoder.h"
19 // The macros unroll loops with switch statements.
20 // Silence warnings about missing fall-through comments.
21 #if TUKLIB_GNUC_REQ(7, 0)
22 # pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
28 // Macros for (somewhat) size-optimized code.
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; \
160 /// Length decoder probabilities; see comments in lzma_common.h.
164 probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
165 probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
166 probability high[LEN_HIGH_SYMBOLS];
167 } lzma_length_decoder;
175 /// Literals; see comments in lzma_common.h.
176 probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
178 /// If 1, it's a match. Otherwise it's a single 8-bit literal.
179 probability is_match[STATES][POS_STATES_MAX];
181 /// If 1, it's a repeated match. The distance is one of rep0 .. rep3.
182 probability is_rep[STATES];
184 /// If 0, distance of a repeated match is rep0.
185 /// Otherwise check is_rep1.
186 probability is_rep0[STATES];
188 /// If 0, distance of a repeated match is rep1.
189 /// Otherwise check is_rep2.
190 probability is_rep1[STATES];
192 /// If 0, distance of a repeated match is rep2. Otherwise it is rep3.
193 probability is_rep2[STATES];
195 /// If 1, the repeated match has length of one byte. Otherwise
196 /// the length is decoded from rep_len_decoder.
197 probability is_rep0_long[STATES][POS_STATES_MAX];
199 /// Probability tree for the highest two bits of the match distance.
200 /// There is a separate probability tree for match lengths of
201 /// 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
202 probability dist_slot[DIST_STATES][DIST_SLOTS];
204 /// Probability trees for additional bits for match distance when the
205 /// distance is in the range [4, 127].
206 probability pos_special[FULL_DISTANCES - DIST_MODEL_END];
208 /// Probability tree for the lowest four bits of a match distance
209 /// that is equal to or greater than 128.
210 probability pos_align[ALIGN_SIZE];
212 /// Length of a normal match
213 lzma_length_decoder match_len_decoder;
215 /// Length of a repeated match
216 lzma_length_decoder rep_len_decoder;
223 lzma_range_decoder rc;
225 // Types of the most recently seen LZMA symbols
226 lzma_lzma_state state;
228 uint32_t rep0; ///< Distance of the latest match
229 uint32_t rep1; ///< Distance of second latest match
230 uint32_t rep2; ///< Distance of third latest match
231 uint32_t rep3; ///< Distance of fourth latest match
233 uint32_t pos_mask; // (1U << pb) - 1
234 uint32_t literal_context_bits;
235 uint32_t literal_pos_mask;
237 /// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of
238 /// payload marker is expected.
239 lzma_vli uncompressed_size;
241 ////////////////////////////////
242 // State of incomplete symbol //
243 ////////////////////////////////
245 /// Position where to continue the decoder loop
250 seq_8(SEQ_LITERAL_MATCHED),
253 seq_len(SEQ_MATCH_LEN),
254 seq_6(SEQ_DIST_SLOT),
264 seq_len(SEQ_REP_LEN),
268 /// Base of the current probability tree
271 /// Symbol being decoded. This is also used as an index variable in
272 /// bittree decoders: probs[symbol]
275 /// Used as a loop termination condition on bittree decoders and
276 /// direct bits decoder.
279 /// Matched literal decoder: 0x100 or 0 to help avoiding branches.
280 /// Bittree reverse decoders: Offset of the next bit: 1 << offset
283 /// If decoding a literal: match byte.
284 /// If decoding a match: length of the match.
286 } lzma_lzma1_decoder;
290 lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
291 const uint8_t *restrict in,
292 size_t *restrict in_pos, size_t in_size)
294 lzma_lzma1_decoder *restrict coder = coder_ptr;
301 const lzma_ret ret = rc_read_init(
302 &coder->rc, in, in_pos, in_size);
303 if (ret != LZMA_STREAM_END)
311 // Making local copies of often-used variables improves both
312 // speed and readability.
314 lzma_dict dict = *dictptr;
316 const size_t dict_start = dict.pos;
319 rc_to_local(coder->rc, *in_pos);
322 uint32_t state = coder->state;
323 uint32_t rep0 = coder->rep0;
324 uint32_t rep1 = coder->rep1;
325 uint32_t rep2 = coder->rep2;
326 uint32_t rep3 = coder->rep3;
328 const uint32_t pos_mask = coder->pos_mask;
330 // These variables are actually needed only if we last time ran
331 // out of input in the middle of the decoder loop.
332 probability *probs = coder->probs;
333 uint32_t symbol = coder->symbol;
334 uint32_t limit = coder->limit;
335 uint32_t offset = coder->offset;
336 uint32_t len = coder->len;
338 const uint32_t literal_pos_mask = coder->literal_pos_mask;
339 const uint32_t literal_context_bits = coder->literal_context_bits;
341 // Temporary variables
342 uint32_t pos_state = dict.pos & pos_mask;
344 lzma_ret ret = LZMA_OK;
346 // If uncompressed size is known, there must be no end of payload
348 const bool no_eopm = coder->uncompressed_size
350 if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
351 dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
353 // The main decoder loop. The "switch" is used to restart the decoder at
354 // correct location. Once restarted, the "switch" is no longer used.
355 switch (coder->sequence)
357 // Calculate new pos_state. This is skipped on the first loop
358 // since we already calculated it when setting up the local
360 pos_state = dict.pos & pos_mask;
364 if (unlikely(no_eopm && dict.pos == dict.limit))
367 rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
368 rc_update_0(coder->is_match[state][pos_state]);
370 // It's a literal i.e. a single 8-bit byte.
372 probs = literal_subcoder(coder->literal,
373 literal_context_bits, literal_pos_mask,
374 dict.pos, dict_get(&dict, 0));
377 if (is_literal_state(state)) {
378 // Decode literal without match byte.
382 rc_bit(probs[symbol], , , SEQ_LITERAL);
383 } while (symbol < (1 << 8));
385 rc_bit_case(probs[symbol], , , SEQ_LITERAL0);
386 rc_bit_case(probs[symbol], , , SEQ_LITERAL1);
387 rc_bit_case(probs[symbol], , , SEQ_LITERAL2);
388 rc_bit_case(probs[symbol], , , SEQ_LITERAL3);
389 rc_bit_case(probs[symbol], , , SEQ_LITERAL4);
390 rc_bit_case(probs[symbol], , , SEQ_LITERAL5);
391 rc_bit_case(probs[symbol], , , SEQ_LITERAL6);
392 rc_bit_case(probs[symbol], , , SEQ_LITERAL7);
395 // Decode literal with match byte.
397 // We store the byte we compare against
398 // ("match byte") to "len" to minimize the
399 // number of variables we need to store
400 // between decoder calls.
401 len = dict_get(&dict, rep0) << 1;
403 // The usage of "offset" allows omitting some
404 // branches, which should give tiny speed
405 // improvement on some CPUs. "offset" gets
406 // set to zero if match_bit didn't match.
410 case SEQ_LITERAL_MATCHED:
412 const uint32_t match_bit
414 const uint32_t subcoder_index
418 rc_bit(probs[subcoder_index],
419 offset &= ~match_bit,
421 SEQ_LITERAL_MATCHED);
423 // It seems to be faster to do this
424 // here instead of putting it to the
425 // beginning of the loop and then
426 // putting the "case" in the middle
430 } while (symbol < (1 << 8));
434 uint32_t subcoder_index;
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);
464 //update_literal(state);
465 // Use a lookup table to update to literal state,
466 // since compared to other state updates, this would
467 // need two branches.
468 static const lzma_lzma_state next_state[] = {
475 STATE_SHORTREP_LIT_LIT,
482 state = next_state[state];
484 case SEQ_LITERAL_WRITE:
485 if (unlikely(dict_put(&dict, symbol))) {
486 coder->sequence = SEQ_LITERAL_WRITE;
493 // Instead of a new byte we are going to get a byte range
494 // (distance and length) which will be repeated from our
497 rc_update_1(coder->is_match[state][pos_state]);
500 rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
501 // Not a repeated match
502 rc_update_0(coder->is_rep[state]);
505 // The latest three match distances are kept in
506 // memory in case there are repeated matches.
511 // Decode the length of the match.
512 len_decode(len, coder->match_len_decoder,
513 pos_state, SEQ_MATCH_LEN);
515 // Prepare to decode the highest two bits of the
517 probs = coder->dist_slot[get_dist_state(len)];
523 rc_bit(probs[symbol], , , SEQ_DIST_SLOT);
524 } while (symbol < DIST_SLOTS);
526 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT0);
527 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT1);
528 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT2);
529 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT3);
530 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT4);
531 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT5);
533 // Get rid of the highest bit that was needed for
534 // indexing of the probability array.
535 symbol -= DIST_SLOTS;
536 assert(symbol <= 63);
538 if (symbol < DIST_MODEL_START) {
539 // Match distances [0, 3] have only two bits.
542 // Decode the lowest [1, 29] bits of
543 // the match distance.
544 limit = (symbol >> 1) - 1;
545 assert(limit >= 1 && limit <= 30);
546 rep0 = 2 + (symbol & 1);
548 if (symbol < DIST_MODEL_END) {
549 // Prepare to decode the low bits for
550 // a distance of [4, 127].
554 // -1 is fine, because we start
555 // decoding at probs[1], not probs[0].
556 // NOTE: This violates the C standard,
557 // since we are doing pointer
558 // arithmetic past the beginning of
560 assert((int32_t)(rep0 - symbol - 1)
562 assert((int32_t)(rep0 - symbol - 1)
564 probs = coder->pos_special + rep0
571 rc_bit(probs[symbol], ,
574 } while (++offset < limit);
579 rc_bit(probs[symbol], ,
585 rc_bit(probs[symbol], ,
591 rc_bit(probs[symbol], ,
597 rc_bit(probs[symbol], ,
603 // We need "symbol" only for
604 // indexing the probability
605 // array, thus we can use
606 // rc_bit_last() here to omit
607 // the unneeded updating of
609 rc_bit_last(probs[symbol], ,
615 // The distance is >= 128. Decode the
616 // lower bits without probabilities
617 // except the lowest four bits.
618 assert(symbol >= 14);
623 // Not worth manual unrolling
625 rc_direct(rep0, SEQ_DIRECT);
626 } while (--limit > 0);
628 // Decode the lowest four bits using
636 rc_bit(coder->pos_align[
640 } while (++offset < ALIGN_BITS);
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);
652 // Like in SEQ_DIST_MODEL, we don't
653 // need "symbol" for anything else
654 // than indexing the probability array.
655 rc_bit_last(coder->pos_align[symbol], ,
656 rep0 += 8, SEQ_ALIGN3);
659 if (rep0 == UINT32_MAX) {
660 // End of payload marker was
661 // found. It must not be
662 // present if uncompressed
664 if (coder->uncompressed_size
665 != LZMA_VLI_UNKNOWN) {
666 ret = LZMA_DATA_ERROR;
672 // end-of-payload marker.
673 rc_normalize(SEQ_EOPM);
674 ret = LZMA_STREAM_END;
680 // Validate the distance we just decoded.
681 if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
682 ret = LZMA_DATA_ERROR;
687 rc_update_1(coder->is_rep[state]);
691 // The match distance is a value that we have had
692 // earlier. The latest four match distances are
693 // available as rep0, rep1, rep2 and rep3. We will
694 // now decode which of them is the new distance.
696 // There cannot be a match if we haven't produced
697 // any output, so check that first.
698 if (unlikely(!dict_is_distance_valid(&dict, 0))) {
699 ret = LZMA_DATA_ERROR;
704 rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
705 rc_update_0(coder->is_rep0[state]);
706 // The distance is rep0.
708 case SEQ_IS_REP0_LONG:
709 rc_if_0(coder->is_rep0_long[state][pos_state],
711 rc_update_0(coder->is_rep0_long[
714 update_short_rep(state);
717 if (unlikely(dict_put(&dict, dict_get(
719 coder->sequence = SEQ_SHORTREP;
726 // Repeating more than one byte at
728 rc_update_1(coder->is_rep0_long[
732 rc_update_1(coder->is_rep0[state]);
735 // The distance is rep1, rep2 or rep3. Once
736 // we find out which one of these three, it
737 // is stored to rep0 and rep1, rep2 and rep3
738 // are updated accordingly.
739 rc_if_0(coder->is_rep1[state], SEQ_IS_REP1) {
740 rc_update_0(coder->is_rep1[state]);
742 const uint32_t distance = rep1;
747 rc_update_1(coder->is_rep1[state]);
749 rc_if_0(coder->is_rep2[state],
751 rc_update_0(coder->is_rep2[
754 const uint32_t distance = rep2;
760 rc_update_1(coder->is_rep2[
763 const uint32_t distance = rep3;
772 update_long_rep(state);
774 // Decode the length of the repeated match.
775 len_decode(len, coder->rep_len_decoder,
776 pos_state, SEQ_REP_LEN);
779 /////////////////////////////////
780 // Repeat from history buffer. //
781 /////////////////////////////////
783 // The length is always between these limits. There is no way
784 // to trigger the algorithm to set len outside this range.
785 assert(len >= MATCH_LEN_MIN);
786 assert(len <= MATCH_LEN_MAX);
789 // Repeat len bytes from distance of rep0.
790 if (unlikely(dict_repeat(&dict, rep0, &len))) {
791 coder->sequence = SEQ_COPY;
796 rc_normalize(SEQ_NORMALIZE);
797 coder->sequence = SEQ_IS_MATCH;
802 // NOTE: Must not copy dict.limit.
803 dictptr->pos = dict.pos;
804 dictptr->full = dict.full;
806 rc_from_local(coder->rc, *in_pos);
808 coder->state = state;
814 coder->probs = probs;
815 coder->symbol = symbol;
816 coder->limit = limit;
817 coder->offset = offset;
820 // Update the remaining amount of uncompressed data if uncompressed
822 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
823 coder->uncompressed_size -= dict.pos - dict_start;
825 // Since there cannot be end of payload marker if the
826 // uncompressed size was known, we check here if we
827 // finished decoding.
828 if (coder->uncompressed_size == 0 && ret == LZMA_OK
829 && coder->sequence != SEQ_NORMALIZE)
830 ret = coder->sequence == SEQ_IS_MATCH
831 ? LZMA_STREAM_END : LZMA_DATA_ERROR;
834 // We can do an additional check in the range decoder to catch some
836 if (ret == LZMA_STREAM_END) {
837 if (!rc_is_finished(coder->rc))
838 ret = LZMA_DATA_ERROR;
840 // Reset the range decoder so that it is ready to reinitialize
841 // for a new LZMA2 chunk.
851 lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
853 lzma_lzma1_decoder *coder = coder_ptr;
854 coder->uncompressed_size = uncompressed_size;
859 lzma_decoder_reset(void *coder_ptr, const void *opt)
861 lzma_lzma1_decoder *coder = coder_ptr;
862 const lzma_options_lzma *options = opt;
864 // NOTE: We assume that lc/lp/pb are valid since they were
865 // successfully decoded with lzma_lzma_decode_properties().
867 // Calculate pos_mask. We don't need pos_bits as is for anything.
868 coder->pos_mask = (1U << options->pb) - 1;
870 // Initialize the literal decoder.
871 literal_init(coder->literal, options->lc, options->lp);
873 coder->literal_context_bits = options->lc;
874 coder->literal_pos_mask = (1U << options->lp) - 1;
877 coder->state = STATE_LIT_LIT;
882 coder->pos_mask = (1U << options->pb) - 1;
887 // Bit and bittree decoders
888 for (uint32_t i = 0; i < STATES; ++i) {
889 for (uint32_t j = 0; j <= coder->pos_mask; ++j) {
890 bit_reset(coder->is_match[i][j]);
891 bit_reset(coder->is_rep0_long[i][j]);
894 bit_reset(coder->is_rep[i]);
895 bit_reset(coder->is_rep0[i]);
896 bit_reset(coder->is_rep1[i]);
897 bit_reset(coder->is_rep2[i]);
900 for (uint32_t i = 0; i < DIST_STATES; ++i)
901 bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS);
903 for (uint32_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)
904 bit_reset(coder->pos_special[i]);
906 bittree_reset(coder->pos_align, ALIGN_BITS);
908 // Len decoders (also bit/bittree)
909 const uint32_t num_pos_states = 1U << options->pb;
910 bit_reset(coder->match_len_decoder.choice);
911 bit_reset(coder->match_len_decoder.choice2);
912 bit_reset(coder->rep_len_decoder.choice);
913 bit_reset(coder->rep_len_decoder.choice2);
915 for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
916 bittree_reset(coder->match_len_decoder.low[pos_state],
918 bittree_reset(coder->match_len_decoder.mid[pos_state],
921 bittree_reset(coder->rep_len_decoder.low[pos_state],
923 bittree_reset(coder->rep_len_decoder.mid[pos_state],
927 bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
928 bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
930 coder->sequence = SEQ_IS_MATCH;
942 lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator,
943 const void *opt, lzma_lz_options *lz_options)
945 if (lz->coder == NULL) {
946 lz->coder = lzma_alloc(sizeof(lzma_lzma1_decoder), allocator);
947 if (lz->coder == NULL)
948 return LZMA_MEM_ERROR;
950 lz->code = &lzma_decode;
951 lz->reset = &lzma_decoder_reset;
952 lz->set_uncompressed = &lzma_decoder_uncompressed;
955 // All dictionary sizes are OK here. LZ decoder will take care of
956 // the special cases.
957 const lzma_options_lzma *options = opt;
958 lz_options->dict_size = options->dict_size;
959 lz_options->preset_dict = options->preset_dict;
960 lz_options->preset_dict_size = options->preset_dict_size;
966 /// Allocate and initialize LZMA decoder. This is used only via LZ
967 /// initialization (lzma_lzma_decoder_init() passes function pointer to
968 /// the LZ initialization).
970 lzma_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,
971 const void *options, lzma_lz_options *lz_options)
973 if (!is_lclppb_valid(options))
974 return LZMA_PROG_ERROR;
976 return_if_error(lzma_lzma_decoder_create(
977 lz, allocator, options, lz_options));
979 lzma_decoder_reset(lz->coder, options);
980 lzma_decoder_uncompressed(lz->coder, LZMA_VLI_UNKNOWN);
987 lzma_lzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
988 const lzma_filter_info *filters)
990 // LZMA can only be the last filter in the chain. This is enforced
991 // by the raw_decoder initialization.
992 assert(filters[1].init == NULL);
994 return lzma_lz_decoder_init(next, allocator, filters,
1000 lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
1002 if (byte > (4 * 5 + 4) * 9 + 8)
1005 // See the file format specification to understand this.
1006 options->pb = byte / (9 * 5);
1007 byte -= options->pb * 9 * 5;
1008 options->lp = byte / 9;
1009 options->lc = byte - options->lp * 9;
1011 return options->lc + options->lp > LZMA_LCLP_MAX;
1016 lzma_lzma_decoder_memusage_nocheck(const void *options)
1018 const lzma_options_lzma *const opt = options;
1019 return sizeof(lzma_lzma1_decoder)
1020 + lzma_lz_decoder_memusage(opt->dict_size);
1025 lzma_lzma_decoder_memusage(const void *options)
1027 if (!is_lclppb_valid(options))
1030 return lzma_lzma_decoder_memusage_nocheck(options);
1035 lzma_lzma_props_decode(void **options, const lzma_allocator *allocator,
1036 const uint8_t *props, size_t props_size)
1038 if (props_size != 5)
1039 return LZMA_OPTIONS_ERROR;
1041 lzma_options_lzma *opt
1042 = lzma_alloc(sizeof(lzma_options_lzma), allocator);
1044 return LZMA_MEM_ERROR;
1046 if (lzma_lzma_lclppb_decode(opt, props[0]))
1049 // All dictionary sizes are accepted, including zero. LZ decoder
1050 // will automatically use a dictionary at least a few KiB even if
1051 // a smaller dictionary is requested.
1052 opt->dict_size = unaligned_read32le(props + 1);
1054 opt->preset_dict = NULL;
1055 opt->preset_dict_size = 0;
1062 lzma_free(opt, allocator);
1063 return LZMA_OPTIONS_ERROR;