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"
22 // Macros for (somewhat) size-optimized code.
23 #define seq_4(seq) seq
25 #define seq_6(seq) seq
27 #define seq_8(seq) seq
29 #define seq_len(seq) \
34 #define len_decode(target, ld, pos_state, seq) \
36 case seq ## _CHOICE: \
37 rc_if_0(ld.choice, seq ## _CHOICE) { \
38 rc_update_0(ld.choice); \
39 probs = ld.low[pos_state];\
40 limit = LEN_LOW_SYMBOLS; \
41 target = MATCH_LEN_MIN; \
43 rc_update_1(ld.choice); \
44 case seq ## _CHOICE2: \
45 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
46 rc_update_0(ld.choice2); \
47 probs = ld.mid[pos_state]; \
48 limit = LEN_MID_SYMBOLS; \
49 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
51 rc_update_1(ld.choice2); \
53 limit = LEN_HIGH_SYMBOLS; \
54 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \
59 case seq ## _BITTREE: \
61 rc_bit(probs[symbol], , , seq ## _BITTREE); \
62 } while (symbol < limit); \
63 target += symbol - limit; \
93 #define seq_len(seq) \
111 #define len_decode(target, ld, pos_state, seq) \
114 case seq ## _CHOICE: \
115 rc_if_0(ld.choice, seq ## _CHOICE) { \
116 rc_update_0(ld.choice); \
117 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW0); \
118 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW1); \
119 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW2); \
120 target = symbol - LEN_LOW_SYMBOLS + MATCH_LEN_MIN; \
122 rc_update_1(ld.choice); \
123 case seq ## _CHOICE2: \
124 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
125 rc_update_0(ld.choice2); \
126 rc_bit_case(ld.mid[pos_state][symbol], , , \
128 rc_bit_case(ld.mid[pos_state][symbol], , , \
130 rc_bit_case(ld.mid[pos_state][symbol], , , \
132 target = symbol - LEN_MID_SYMBOLS \
133 + MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
135 rc_update_1(ld.choice2); \
136 rc_bit_case(ld.high[symbol], , , seq ## _HIGH0); \
137 rc_bit_case(ld.high[symbol], , , seq ## _HIGH1); \
138 rc_bit_case(ld.high[symbol], , , seq ## _HIGH2); \
139 rc_bit_case(ld.high[symbol], , , seq ## _HIGH3); \
140 rc_bit_case(ld.high[symbol], , , seq ## _HIGH4); \
141 rc_bit_case(ld.high[symbol], , , seq ## _HIGH5); \
142 rc_bit_case(ld.high[symbol], , , seq ## _HIGH6); \
143 rc_bit_case(ld.high[symbol], , , seq ## _HIGH7); \
144 target = symbol - LEN_HIGH_SYMBOLS \
146 + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; \
154 /// Length decoder probabilities; see comments in lzma_common.h.
158 probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
159 probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
160 probability high[LEN_HIGH_SYMBOLS];
161 } lzma_length_decoder;
164 struct lzma_coder_s {
169 /// Literals; see comments in lzma_common.h.
170 probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
172 /// If 1, it's a match. Otherwise it's a single 8-bit literal.
173 probability is_match[STATES][POS_STATES_MAX];
175 /// If 1, it's a repeated match. The distance is one of rep0 .. rep3.
176 probability is_rep[STATES];
178 /// If 0, distance of a repeated match is rep0.
179 /// Otherwise check is_rep1.
180 probability is_rep0[STATES];
182 /// If 0, distance of a repeated match is rep1.
183 /// Otherwise check is_rep2.
184 probability is_rep1[STATES];
186 /// If 0, distance of a repeated match is rep2. Otherwise it is rep3.
187 probability is_rep2[STATES];
189 /// If 1, the repeated match has length of one byte. Otherwise
190 /// the length is decoded from rep_len_decoder.
191 probability is_rep0_long[STATES][POS_STATES_MAX];
193 /// Probability tree for the highest two bits of the match distance.
194 /// There is a separate probability tree for match lengths of
195 /// 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
196 probability dist_slot[DIST_STATES][DIST_SLOTS];
198 /// Probability trees for additional bits for match distance when the
199 /// distance is in the range [4, 127].
200 probability pos_special[FULL_DISTANCES - DIST_MODEL_END];
202 /// Probability tree for the lowest four bits of a match distance
203 /// that is equal to or greater than 128.
204 probability pos_align[ALIGN_SIZE];
206 /// Length of a normal match
207 lzma_length_decoder match_len_decoder;
209 /// Length of a repeated match
210 lzma_length_decoder rep_len_decoder;
217 lzma_range_decoder rc;
219 // Types of the most recently seen LZMA symbols
220 lzma_lzma_state state;
222 uint32_t rep0; ///< Distance of the latest match
223 uint32_t rep1; ///< Distance of second latest match
224 uint32_t rep2; ///< Distance of third latest match
225 uint32_t rep3; ///< Distance of fourth latest match
227 uint32_t pos_mask; // (1U << pb) - 1
228 uint32_t literal_context_bits;
229 uint32_t literal_pos_mask;
231 /// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of
232 /// payload marker is expected.
233 lzma_vli uncompressed_size;
235 ////////////////////////////////
236 // State of incomplete symbol //
237 ////////////////////////////////
239 /// Position where to continue the decoder loop
244 seq_8(SEQ_LITERAL_MATCHED),
247 seq_len(SEQ_MATCH_LEN),
248 seq_6(SEQ_DIST_SLOT),
258 seq_len(SEQ_REP_LEN),
262 /// Base of the current probability tree
265 /// Symbol being decoded. This is also used as an index variable in
266 /// bittree decoders: probs[symbol]
269 /// Used as a loop termination condition on bittree decoders and
270 /// direct bits decoder.
273 /// Matched literal decoder: 0x100 or 0 to help avoiding branches.
274 /// Bittree reverse decoders: Offset of the next bit: 1 << offset
277 /// If decoding a literal: match byte.
278 /// If decoding a match: length of the match.
284 lzma_decode(lzma_coder *restrict coder, lzma_dict *restrict dictptr,
285 const uint8_t *restrict in,
286 size_t *restrict in_pos, size_t in_size)
293 const lzma_ret ret = rc_read_init(
294 &coder->rc, in, in_pos, in_size);
295 if (ret != LZMA_STREAM_END)
303 // Making local copies of often-used variables improves both
304 // speed and readability.
306 lzma_dict dict = *dictptr;
308 const size_t dict_start = dict.pos;
311 rc_to_local(coder->rc, *in_pos);
314 uint32_t state = coder->state;
315 uint32_t rep0 = coder->rep0;
316 uint32_t rep1 = coder->rep1;
317 uint32_t rep2 = coder->rep2;
318 uint32_t rep3 = coder->rep3;
320 const uint32_t pos_mask = coder->pos_mask;
322 // These variables are actually needed only if we last time ran
323 // out of input in the middle of the decoder loop.
324 probability *probs = coder->probs;
325 uint32_t symbol = coder->symbol;
326 uint32_t limit = coder->limit;
327 uint32_t offset = coder->offset;
328 uint32_t len = coder->len;
330 const uint32_t literal_pos_mask = coder->literal_pos_mask;
331 const uint32_t literal_context_bits = coder->literal_context_bits;
333 // Temporary variables
334 uint32_t pos_state = dict.pos & pos_mask;
336 lzma_ret ret = LZMA_OK;
338 // If uncompressed size is known, there must be no end of payload
340 const bool no_eopm = coder->uncompressed_size
342 if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
343 dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
345 // The main decoder loop. The "switch" is used to restart the decoder at
346 // correct location. Once restarted, the "switch" is no longer used.
347 switch (coder->sequence)
349 // Calculate new pos_state. This is skipped on the first loop
350 // since we already calculated it when setting up the local
352 pos_state = dict.pos & pos_mask;
356 if (unlikely(no_eopm && dict.pos == dict.limit))
359 rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
360 rc_update_0(coder->is_match[state][pos_state]);
362 // It's a literal i.e. a single 8-bit byte.
364 probs = literal_subcoder(coder->literal,
365 literal_context_bits, literal_pos_mask,
366 dict.pos, dict_get(&dict, 0));
369 if (is_literal_state(state)) {
370 // Decode literal without match byte.
374 rc_bit(probs[symbol], , , SEQ_LITERAL);
375 } while (symbol < (1 << 8));
377 rc_bit_case(probs[symbol], , , SEQ_LITERAL0);
378 rc_bit_case(probs[symbol], , , SEQ_LITERAL1);
379 rc_bit_case(probs[symbol], , , SEQ_LITERAL2);
380 rc_bit_case(probs[symbol], , , SEQ_LITERAL3);
381 rc_bit_case(probs[symbol], , , SEQ_LITERAL4);
382 rc_bit_case(probs[symbol], , , SEQ_LITERAL5);
383 rc_bit_case(probs[symbol], , , SEQ_LITERAL6);
384 rc_bit_case(probs[symbol], , , SEQ_LITERAL7);
387 // Decode literal with match byte.
389 // We store the byte we compare against
390 // ("match byte") to "len" to minimize the
391 // number of variables we need to store
392 // between decoder calls.
393 len = dict_get(&dict, rep0) << 1;
395 // The usage of "offset" allows omitting some
396 // branches, which should give tiny speed
397 // improvement on some CPUs. "offset" gets
398 // set to zero if match_bit didn't match.
402 case SEQ_LITERAL_MATCHED:
404 const uint32_t match_bit
406 const uint32_t subcoder_index
410 rc_bit(probs[subcoder_index],
411 offset &= ~match_bit,
413 SEQ_LITERAL_MATCHED);
415 // It seems to be faster to do this
416 // here instead of putting it to the
417 // beginning of the loop and then
418 // putting the "case" in the middle
422 } while (symbol < (1 << 8));
426 uint32_t subcoder_index;
430 match_bit = len & offset; \
431 subcoder_index = offset + match_bit + symbol; \
432 rc_bit(probs[subcoder_index], \
433 offset &= ~match_bit, \
434 offset &= match_bit, \
437 d(SEQ_LITERAL_MATCHED0);
439 d(SEQ_LITERAL_MATCHED1);
441 d(SEQ_LITERAL_MATCHED2);
443 d(SEQ_LITERAL_MATCHED3);
445 d(SEQ_LITERAL_MATCHED4);
447 d(SEQ_LITERAL_MATCHED5);
449 d(SEQ_LITERAL_MATCHED6);
451 d(SEQ_LITERAL_MATCHED7);
456 //update_literal(state);
457 // Use a lookup table to update to literal state,
458 // since compared to other state updates, this would
459 // need two branches.
460 static const lzma_lzma_state next_state[] = {
467 STATE_SHORTREP_LIT_LIT,
474 state = next_state[state];
476 case SEQ_LITERAL_WRITE:
477 if (unlikely(dict_put(&dict, symbol))) {
478 coder->sequence = SEQ_LITERAL_WRITE;
485 // Instead of a new byte we are going to get a byte range
486 // (distance and length) which will be repeated from our
489 rc_update_1(coder->is_match[state][pos_state]);
492 rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
493 // Not a repeated match
494 rc_update_0(coder->is_rep[state]);
497 // The latest three match distances are kept in
498 // memory in case there are repeated matches.
503 // Decode the length of the match.
504 len_decode(len, coder->match_len_decoder,
505 pos_state, SEQ_MATCH_LEN);
507 // Prepare to decode the highest two bits of the
509 probs = coder->dist_slot[get_dist_state(len)];
515 rc_bit(probs[symbol], , , SEQ_DIST_SLOT);
516 } while (symbol < DIST_SLOTS);
518 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT0);
519 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT1);
520 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT2);
521 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT3);
522 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT4);
523 rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT5);
525 // Get rid of the highest bit that was needed for
526 // indexing of the probability array.
527 symbol -= DIST_SLOTS;
528 assert(symbol <= 63);
530 if (symbol < DIST_MODEL_START) {
531 // Match distances [0, 3] have only two bits.
534 // Decode the lowest [1, 29] bits of
535 // the match distance.
536 limit = (symbol >> 1) - 1;
537 assert(limit >= 1 && limit <= 30);
538 rep0 = 2 + (symbol & 1);
540 if (symbol < DIST_MODEL_END) {
541 // Prepare to decode the low bits for
542 // a distance of [4, 127].
546 // -1 is fine, because we start
547 // decoding at probs[1], not probs[0].
548 // NOTE: This violates the C standard,
549 // since we are doing pointer
550 // arithmetic past the beginning of
552 assert((int32_t)(rep0 - symbol - 1)
554 assert((int32_t)(rep0 - symbol - 1)
556 probs = coder->pos_special + rep0
563 rc_bit(probs[symbol], ,
566 } while (++offset < limit);
571 rc_bit(probs[symbol], ,
577 rc_bit(probs[symbol], ,
583 rc_bit(probs[symbol], ,
589 rc_bit(probs[symbol], ,
595 // We need "symbol" only for
596 // indexing the probability
597 // array, thus we can use
598 // rc_bit_last() here to omit
599 // the unneeded updating of
601 rc_bit_last(probs[symbol], ,
607 // The distance is >= 128. Decode the
608 // lower bits without probabilities
609 // except the lowest four bits.
610 assert(symbol >= 14);
615 // Not worth manual unrolling
617 rc_direct(rep0, SEQ_DIRECT);
618 } while (--limit > 0);
620 // Decode the lowest four bits using
628 rc_bit(coder->pos_align[
632 } while (++offset < ALIGN_BITS);
635 rc_bit(coder->pos_align[symbol], ,
636 rep0 += 1, SEQ_ALIGN0);
638 rc_bit(coder->pos_align[symbol], ,
639 rep0 += 2, SEQ_ALIGN1);
641 rc_bit(coder->pos_align[symbol], ,
642 rep0 += 4, SEQ_ALIGN2);
644 // Like in SEQ_DIST_MODEL, we don't
645 // need "symbol" for anything else
646 // than indexing the probability array.
647 rc_bit_last(coder->pos_align[symbol], ,
648 rep0 += 8, SEQ_ALIGN3);
651 if (rep0 == UINT32_MAX) {
652 // End of payload marker was
653 // found. It must not be
654 // present if uncompressed
656 if (coder->uncompressed_size
657 != LZMA_VLI_UNKNOWN) {
658 ret = LZMA_DATA_ERROR;
664 // end-of-payload marker.
665 rc_normalize(SEQ_EOPM);
666 ret = LZMA_STREAM_END;
672 // Validate the distance we just decoded.
673 if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
674 ret = LZMA_DATA_ERROR;
679 rc_update_1(coder->is_rep[state]);
683 // The match distance is a value that we have had
684 // earlier. The latest four match distances are
685 // available as rep0, rep1, rep2 and rep3. We will
686 // now decode which of them is the new distance.
688 // There cannot be a match if we haven't produced
689 // any output, so check that first.
690 if (unlikely(!dict_is_distance_valid(&dict, 0))) {
691 ret = LZMA_DATA_ERROR;
696 rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
697 rc_update_0(coder->is_rep0[state]);
698 // The distance is rep0.
700 case SEQ_IS_REP0_LONG:
701 rc_if_0(coder->is_rep0_long[state][pos_state],
703 rc_update_0(coder->is_rep0_long[
706 update_short_rep(state);
709 if (unlikely(dict_put(&dict, dict_get(
711 coder->sequence = SEQ_SHORTREP;
718 // Repeating more than one byte at
720 rc_update_1(coder->is_rep0_long[
724 rc_update_1(coder->is_rep0[state]);
727 // The distance is rep1, rep2 or rep3. Once
728 // we find out which one of these three, it
729 // is stored to rep0 and rep1, rep2 and rep3
730 // are updated accordingly.
731 rc_if_0(coder->is_rep1[state], SEQ_IS_REP1) {
732 rc_update_0(coder->is_rep1[state]);
734 const uint32_t distance = rep1;
739 rc_update_1(coder->is_rep1[state]);
741 rc_if_0(coder->is_rep2[state],
743 rc_update_0(coder->is_rep2[
746 const uint32_t distance = rep2;
752 rc_update_1(coder->is_rep2[
755 const uint32_t distance = rep3;
764 update_long_rep(state);
766 // Decode the length of the repeated match.
767 len_decode(len, coder->rep_len_decoder,
768 pos_state, SEQ_REP_LEN);
771 /////////////////////////////////
772 // Repeat from history buffer. //
773 /////////////////////////////////
775 // The length is always between these limits. There is no way
776 // to trigger the algorithm to set len outside this range.
777 assert(len >= MATCH_LEN_MIN);
778 assert(len <= MATCH_LEN_MAX);
781 // Repeat len bytes from distance of rep0.
782 if (unlikely(dict_repeat(&dict, rep0, &len))) {
783 coder->sequence = SEQ_COPY;
788 rc_normalize(SEQ_NORMALIZE);
789 coder->sequence = SEQ_IS_MATCH;
794 // NOTE: Must not copy dict.limit.
795 dictptr->pos = dict.pos;
796 dictptr->full = dict.full;
798 rc_from_local(coder->rc, *in_pos);
800 coder->state = state;
806 coder->probs = probs;
807 coder->symbol = symbol;
808 coder->limit = limit;
809 coder->offset = offset;
812 // Update the remaining amount of uncompressed data if uncompressed
814 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
815 coder->uncompressed_size -= dict.pos - dict_start;
817 // Since there cannot be end of payload marker if the
818 // uncompressed size was known, we check here if we
819 // finished decoding.
820 if (coder->uncompressed_size == 0 && ret == LZMA_OK
821 && coder->sequence != SEQ_NORMALIZE)
822 ret = coder->sequence == SEQ_IS_MATCH
823 ? LZMA_STREAM_END : LZMA_DATA_ERROR;
826 // We can do an additional check in the range decoder to catch some
828 if (ret == LZMA_STREAM_END) {
829 if (!rc_is_finished(coder->rc))
830 ret = LZMA_DATA_ERROR;
832 // Reset the range decoder so that it is ready to reinitialize
833 // for a new LZMA2 chunk.
843 lzma_decoder_uncompressed(lzma_coder *coder, lzma_vli uncompressed_size)
845 coder->uncompressed_size = uncompressed_size;
850 lzma_lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
853 (*(lzma_coder **)(coder))->uncompressed_size = uncompressed_size;
858 lzma_decoder_reset(lzma_coder *coder, const void *opt)
860 const lzma_options_lzma *options = opt;
862 // NOTE: We assume that lc/lp/pb are valid since they were
863 // successfully decoded with lzma_lzma_decode_properties().
865 // Calculate pos_mask. We don't need pos_bits as is for anything.
866 coder->pos_mask = (1U << options->pb) - 1;
868 // Initialize the literal decoder.
869 literal_init(coder->literal, options->lc, options->lp);
871 coder->literal_context_bits = options->lc;
872 coder->literal_pos_mask = (1U << options->lp) - 1;
875 coder->state = STATE_LIT_LIT;
880 coder->pos_mask = (1U << options->pb) - 1;
885 // Bit and bittree decoders
886 for (uint32_t i = 0; i < STATES; ++i) {
887 for (uint32_t j = 0; j <= coder->pos_mask; ++j) {
888 bit_reset(coder->is_match[i][j]);
889 bit_reset(coder->is_rep0_long[i][j]);
892 bit_reset(coder->is_rep[i]);
893 bit_reset(coder->is_rep0[i]);
894 bit_reset(coder->is_rep1[i]);
895 bit_reset(coder->is_rep2[i]);
898 for (uint32_t i = 0; i < DIST_STATES; ++i)
899 bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS);
901 for (uint32_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)
902 bit_reset(coder->pos_special[i]);
904 bittree_reset(coder->pos_align, ALIGN_BITS);
906 // Len decoders (also bit/bittree)
907 const uint32_t num_pos_states = 1U << options->pb;
908 bit_reset(coder->match_len_decoder.choice);
909 bit_reset(coder->match_len_decoder.choice2);
910 bit_reset(coder->rep_len_decoder.choice);
911 bit_reset(coder->rep_len_decoder.choice2);
913 for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
914 bittree_reset(coder->match_len_decoder.low[pos_state],
916 bittree_reset(coder->match_len_decoder.mid[pos_state],
919 bittree_reset(coder->rep_len_decoder.low[pos_state],
921 bittree_reset(coder->rep_len_decoder.mid[pos_state],
925 bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
926 bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
928 coder->sequence = SEQ_IS_MATCH;
940 lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator,
941 const void *opt, lzma_lz_options *lz_options)
943 if (lz->coder == NULL) {
944 lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
945 if (lz->coder == NULL)
946 return LZMA_MEM_ERROR;
948 lz->code = &lzma_decode;
949 lz->reset = &lzma_decoder_reset;
950 lz->set_uncompressed = &lzma_decoder_uncompressed;
953 // All dictionary sizes are OK here. LZ decoder will take care of
954 // the special cases.
955 const lzma_options_lzma *options = opt;
956 lz_options->dict_size = options->dict_size;
957 lz_options->preset_dict = options->preset_dict;
958 lz_options->preset_dict_size = options->preset_dict_size;
964 /// Allocate and initialize LZMA decoder. This is used only via LZ
965 /// initialization (lzma_lzma_decoder_init() passes function pointer to
966 /// the LZ initialization).
968 lzma_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,
969 const void *options, lzma_lz_options *lz_options)
971 if (!is_lclppb_valid(options))
972 return LZMA_PROG_ERROR;
974 return_if_error(lzma_lzma_decoder_create(
975 lz, allocator, options, lz_options));
977 lzma_decoder_reset(lz->coder, options);
978 lzma_decoder_uncompressed(lz->coder, LZMA_VLI_UNKNOWN);
985 lzma_lzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
986 const lzma_filter_info *filters)
988 // LZMA can only be the last filter in the chain. This is enforced
989 // by the raw_decoder initialization.
990 assert(filters[1].init == NULL);
992 return lzma_lz_decoder_init(next, allocator, filters,
998 lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
1000 if (byte > (4 * 5 + 4) * 9 + 8)
1003 // See the file format specification to understand this.
1004 options->pb = byte / (9 * 5);
1005 byte -= options->pb * 9 * 5;
1006 options->lp = byte / 9;
1007 options->lc = byte - options->lp * 9;
1009 return options->lc + options->lp > LZMA_LCLP_MAX;
1014 lzma_lzma_decoder_memusage_nocheck(const void *options)
1016 const lzma_options_lzma *const opt = options;
1017 return sizeof(lzma_coder) + lzma_lz_decoder_memusage(opt->dict_size);
1022 lzma_lzma_decoder_memusage(const void *options)
1024 if (!is_lclppb_valid(options))
1027 return lzma_lzma_decoder_memusage_nocheck(options);
1032 lzma_lzma_props_decode(void **options, const lzma_allocator *allocator,
1033 const uint8_t *props, size_t props_size)
1035 if (props_size != 5)
1036 return LZMA_OPTIONS_ERROR;
1038 lzma_options_lzma *opt
1039 = lzma_alloc(sizeof(lzma_options_lzma), allocator);
1041 return LZMA_MEM_ERROR;
1043 if (lzma_lzma_lclppb_decode(opt, props[0]))
1046 // All dictionary sizes are accepted, including zero. LZ decoder
1047 // will automatically use a dictionary at least a few KiB even if
1048 // a smaller dictionary is requested.
1049 opt->dict_size = unaligned_read32le(props + 1);
1051 opt->preset_dict = NULL;
1052 opt->preset_dict_size = 0;
1059 lzma_free(opt, allocator);
1060 return LZMA_OPTIONS_ERROR;