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 pos_slot[LEN_TO_POS_STATES][POS_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 - END_POS_MODEL_INDEX];
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_TABLE_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),
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)
292 if (!rc_read_init(&coder->rc, in, in_pos, in_size))
299 // Making local copies of often-used variables improves both
300 // speed and readability.
302 lzma_dict dict = *dictptr;
304 const size_t dict_start = dict.pos;
307 rc_to_local(coder->rc, *in_pos);
310 uint32_t state = coder->state;
311 uint32_t rep0 = coder->rep0;
312 uint32_t rep1 = coder->rep1;
313 uint32_t rep2 = coder->rep2;
314 uint32_t rep3 = coder->rep3;
316 const uint32_t pos_mask = coder->pos_mask;
318 // These variables are actually needed only if we last time ran
319 // out of input in the middle of the decoder loop.
320 probability *probs = coder->probs;
321 uint32_t symbol = coder->symbol;
322 uint32_t limit = coder->limit;
323 uint32_t offset = coder->offset;
324 uint32_t len = coder->len;
326 const uint32_t literal_pos_mask = coder->literal_pos_mask;
327 const uint32_t literal_context_bits = coder->literal_context_bits;
329 // Temporary variables
330 uint32_t pos_state = dict.pos & pos_mask;
332 lzma_ret ret = LZMA_OK;
334 // If uncompressed size is known, there must be no end of payload
336 const bool no_eopm = coder->uncompressed_size
338 if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
339 dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
341 // The main decoder loop. The "switch" is used to restart the decoder at
342 // correct location. Once restarted, the "switch" is no longer used.
343 switch (coder->sequence)
345 // Calculate new pos_state. This is skipped on the first loop
346 // since we already calculated it when setting up the local
348 pos_state = dict.pos & pos_mask;
352 if (unlikely(no_eopm && dict.pos == dict.limit))
355 rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
356 rc_update_0(coder->is_match[state][pos_state]);
358 // It's a literal i.e. a single 8-bit byte.
360 probs = literal_subcoder(coder->literal,
361 literal_context_bits, literal_pos_mask,
362 dict.pos, dict_get(&dict, 0));
365 if (is_literal_state(state)) {
366 // Decode literal without match byte.
370 rc_bit(probs[symbol], , , SEQ_LITERAL);
371 } while (symbol < (1 << 8));
373 rc_bit_case(probs[symbol], , , SEQ_LITERAL0);
374 rc_bit_case(probs[symbol], , , SEQ_LITERAL1);
375 rc_bit_case(probs[symbol], , , SEQ_LITERAL2);
376 rc_bit_case(probs[symbol], , , SEQ_LITERAL3);
377 rc_bit_case(probs[symbol], , , SEQ_LITERAL4);
378 rc_bit_case(probs[symbol], , , SEQ_LITERAL5);
379 rc_bit_case(probs[symbol], , , SEQ_LITERAL6);
380 rc_bit_case(probs[symbol], , , SEQ_LITERAL7);
383 // Decode literal with match byte.
385 // We store the byte we compare against
386 // ("match byte") to "len" to minimize the
387 // number of variables we need to store
388 // between decoder calls.
389 len = dict_get(&dict, rep0) << 1;
391 // The usage of "offset" allows omitting some
392 // branches, which should give tiny speed
393 // improvement on some CPUs. "offset" gets
394 // set to zero if match_bit didn't match.
398 case SEQ_LITERAL_MATCHED:
400 const uint32_t match_bit
402 const uint32_t subcoder_index
406 rc_bit(probs[subcoder_index],
407 offset &= ~match_bit,
409 SEQ_LITERAL_MATCHED);
411 // It seems to be faster to do this
412 // here instead of putting it to the
413 // beginning of the loop and then
414 // putting the "case" in the middle
418 } while (symbol < (1 << 8));
422 uint32_t subcoder_index;
426 match_bit = len & offset; \
427 subcoder_index = offset + match_bit + symbol; \
428 rc_bit(probs[subcoder_index], \
429 offset &= ~match_bit, \
430 offset &= match_bit, \
433 d(SEQ_LITERAL_MATCHED0);
435 d(SEQ_LITERAL_MATCHED1);
437 d(SEQ_LITERAL_MATCHED2);
439 d(SEQ_LITERAL_MATCHED3);
441 d(SEQ_LITERAL_MATCHED4);
443 d(SEQ_LITERAL_MATCHED5);
445 d(SEQ_LITERAL_MATCHED6);
447 d(SEQ_LITERAL_MATCHED7);
452 //update_literal(state);
453 // Use a lookup table to update to literal state,
454 // since compared to other state updates, this would
455 // need two branches.
456 static const lzma_lzma_state next_state[] = {
463 STATE_SHORTREP_LIT_LIT,
470 state = next_state[state];
472 case SEQ_LITERAL_WRITE:
473 if (unlikely(dict_put(&dict, symbol))) {
474 coder->sequence = SEQ_LITERAL_WRITE;
481 // Instead of a new byte we are going to get a byte range
482 // (distance and length) which will be repeated from our
485 rc_update_1(coder->is_match[state][pos_state]);
488 rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
489 // Not a repeated match
490 rc_update_0(coder->is_rep[state]);
493 // The latest three match distances are kept in
494 // memory in case there are repeated matches.
499 // Decode the length of the match.
500 len_decode(len, coder->match_len_decoder,
501 pos_state, SEQ_MATCH_LEN);
503 // Prepare to decode the highest two bits of the
505 probs = coder->pos_slot[get_len_to_pos_state(len)];
511 rc_bit(probs[symbol], , , SEQ_POS_SLOT);
512 } while (symbol < POS_SLOTS);
514 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT0);
515 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT1);
516 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT2);
517 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT3);
518 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT4);
519 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT5);
521 // Get rid of the highest bit that was needed for
522 // indexing of the probability array.
524 assert(symbol <= 63);
526 if (symbol < START_POS_MODEL_INDEX) {
527 // Match distances [0, 3] have only two bits.
530 // Decode the lowest [1, 29] bits of
531 // the match distance.
532 limit = (symbol >> 1) - 1;
533 assert(limit >= 1 && limit <= 30);
534 rep0 = 2 + (symbol & 1);
536 if (symbol < END_POS_MODEL_INDEX) {
537 // Prepare to decode the low bits for
538 // a distance of [4, 127].
542 // -1 is fine, because we start
543 // decoding at probs[1], not probs[0].
544 // NOTE: This violates the C standard,
545 // since we are doing pointer
546 // arithmetic past the beginning of
548 assert((int32_t)(rep0 - symbol - 1)
550 assert((int32_t)(rep0 - symbol - 1)
552 probs = coder->pos_special + rep0
559 rc_bit(probs[symbol], ,
562 } while (++offset < limit);
567 rc_bit(probs[symbol], ,
573 rc_bit(probs[symbol], ,
579 rc_bit(probs[symbol], ,
585 rc_bit(probs[symbol], ,
591 // We need "symbol" only for
592 // indexing the probability
593 // array, thus we can use
594 // rc_bit_last() here to omit
595 // the unneeded updating of
597 rc_bit_last(probs[symbol], ,
603 // The distance is >= 128. Decode the
604 // lower bits without probabilities
605 // except the lowest four bits.
606 assert(symbol >= 14);
611 // Not worth manual unrolling
613 rc_direct(rep0, SEQ_DIRECT);
614 } while (--limit > 0);
616 // Decode the lowest four bits using
624 rc_bit(coder->pos_align[
628 } while (++offset < ALIGN_BITS);
631 rc_bit(coder->pos_align[symbol], ,
632 rep0 += 1, SEQ_ALIGN0);
634 rc_bit(coder->pos_align[symbol], ,
635 rep0 += 2, SEQ_ALIGN1);
637 rc_bit(coder->pos_align[symbol], ,
638 rep0 += 4, SEQ_ALIGN2);
640 // Like in SEQ_POS_MODEL, we don't
641 // need "symbol" for anything else
642 // than indexing the probability array.
643 rc_bit_last(coder->pos_align[symbol], ,
644 rep0 += 8, SEQ_ALIGN3);
647 if (rep0 == UINT32_MAX) {
648 // End of payload marker was
649 // found. It must not be
650 // present if uncompressed
652 if (coder->uncompressed_size
653 != LZMA_VLI_UNKNOWN) {
654 ret = LZMA_DATA_ERROR;
660 // end-of-payload marker.
661 rc_normalize(SEQ_EOPM);
662 ret = LZMA_STREAM_END;
668 // Validate the distance we just decoded.
669 if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
670 ret = LZMA_DATA_ERROR;
675 rc_update_1(coder->is_rep[state]);
679 // The match distance is a value that we have had
680 // earlier. The latest four match distances are
681 // available as rep0, rep1, rep2 and rep3. We will
682 // now decode which of them is the new distance.
684 // There cannot be a match if we haven't produced
685 // any output, so check that first.
686 if (unlikely(!dict_is_distance_valid(&dict, 0))) {
687 ret = LZMA_DATA_ERROR;
692 rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
693 rc_update_0(coder->is_rep0[state]);
694 // The distance is rep0.
696 case SEQ_IS_REP0_LONG:
697 rc_if_0(coder->is_rep0_long[state][pos_state],
699 rc_update_0(coder->is_rep0_long[
702 update_short_rep(state);
705 if (unlikely(dict_put(&dict, dict_get(
707 coder->sequence = SEQ_SHORTREP;
714 // Repeating more than one byte at
716 rc_update_1(coder->is_rep0_long[
720 rc_update_1(coder->is_rep0[state]);
723 // The distance is rep1, rep2 or rep3. Once
724 // we find out which one of these three, it
725 // is stored to rep0 and rep1, rep2 and rep3
726 // are updated accordingly.
727 rc_if_0(coder->is_rep1[state], SEQ_IS_REP1) {
728 rc_update_0(coder->is_rep1[state]);
730 const uint32_t distance = rep1;
735 rc_update_1(coder->is_rep1[state]);
737 rc_if_0(coder->is_rep2[state],
739 rc_update_0(coder->is_rep2[
742 const uint32_t distance = rep2;
748 rc_update_1(coder->is_rep2[
751 const uint32_t distance = rep3;
760 update_long_rep(state);
762 // Decode the length of the repeated match.
763 len_decode(len, coder->rep_len_decoder,
764 pos_state, SEQ_REP_LEN);
767 /////////////////////////////////
768 // Repeat from history buffer. //
769 /////////////////////////////////
771 // The length is always between these limits. There is no way
772 // to trigger the algorithm to set len outside this range.
773 assert(len >= MATCH_LEN_MIN);
774 assert(len <= MATCH_LEN_MAX);
777 // Repeat len bytes from distance of rep0.
778 if (unlikely(dict_repeat(&dict, rep0, &len))) {
779 coder->sequence = SEQ_COPY;
784 rc_normalize(SEQ_NORMALIZE);
785 coder->sequence = SEQ_IS_MATCH;
790 // NOTE: Must not copy dict.limit.
791 dictptr->pos = dict.pos;
792 dictptr->full = dict.full;
794 rc_from_local(coder->rc, *in_pos);
796 coder->state = state;
802 coder->probs = probs;
803 coder->symbol = symbol;
804 coder->limit = limit;
805 coder->offset = offset;
808 // Update the remaining amount of uncompressed data if uncompressed
810 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
811 coder->uncompressed_size -= dict.pos - dict_start;
813 // Since there cannot be end of payload marker if the
814 // uncompressed size was known, we check here if we
815 // finished decoding.
816 if (coder->uncompressed_size == 0 && ret == LZMA_OK
817 && coder->sequence != SEQ_NORMALIZE)
818 ret = coder->sequence == SEQ_IS_MATCH
819 ? LZMA_STREAM_END : LZMA_DATA_ERROR;
822 // We can do an additional check in the range decoder to catch some
824 if (ret == LZMA_STREAM_END) {
825 if (!rc_is_finished(coder->rc))
826 ret = LZMA_DATA_ERROR;
828 // Reset the range decoder so that it is ready to reinitialize
829 // for a new LZMA2 chunk.
839 lzma_decoder_uncompressed(lzma_coder *coder, lzma_vli uncompressed_size)
841 coder->uncompressed_size = uncompressed_size;
846 lzma_lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
849 (*(lzma_coder **)(coder))->uncompressed_size = uncompressed_size;
854 lzma_decoder_reset(lzma_coder *coder, const void *opt)
856 const lzma_options_lzma *options = opt;
858 // NOTE: We assume that lc/lp/pb are valid since they were
859 // successfully decoded with lzma_lzma_decode_properties().
861 // Calculate pos_mask. We don't need pos_bits as is for anything.
862 coder->pos_mask = (1U << options->pb) - 1;
864 // Initialize the literal decoder.
865 literal_init(coder->literal, options->lc, options->lp);
867 coder->literal_context_bits = options->lc;
868 coder->literal_pos_mask = (1U << options->lp) - 1;
871 coder->state = STATE_LIT_LIT;
876 coder->pos_mask = (1U << options->pb) - 1;
881 // Bit and bittree decoders
882 for (uint32_t i = 0; i < STATES; ++i) {
883 for (uint32_t j = 0; j <= coder->pos_mask; ++j) {
884 bit_reset(coder->is_match[i][j]);
885 bit_reset(coder->is_rep0_long[i][j]);
888 bit_reset(coder->is_rep[i]);
889 bit_reset(coder->is_rep0[i]);
890 bit_reset(coder->is_rep1[i]);
891 bit_reset(coder->is_rep2[i]);
894 for (uint32_t i = 0; i < LEN_TO_POS_STATES; ++i)
895 bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
897 for (uint32_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
898 bit_reset(coder->pos_special[i]);
900 bittree_reset(coder->pos_align, ALIGN_BITS);
902 // Len decoders (also bit/bittree)
903 const uint32_t num_pos_states = 1U << options->pb;
904 bit_reset(coder->match_len_decoder.choice);
905 bit_reset(coder->match_len_decoder.choice2);
906 bit_reset(coder->rep_len_decoder.choice);
907 bit_reset(coder->rep_len_decoder.choice2);
909 for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
910 bittree_reset(coder->match_len_decoder.low[pos_state],
912 bittree_reset(coder->match_len_decoder.mid[pos_state],
915 bittree_reset(coder->rep_len_decoder.low[pos_state],
917 bittree_reset(coder->rep_len_decoder.mid[pos_state],
921 bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
922 bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
924 coder->sequence = SEQ_IS_MATCH;
936 lzma_lzma_decoder_create(lzma_lz_decoder *lz, lzma_allocator *allocator,
937 const void *opt, lzma_lz_options *lz_options)
939 if (lz->coder == NULL) {
940 lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
941 if (lz->coder == NULL)
942 return LZMA_MEM_ERROR;
944 lz->code = &lzma_decode;
945 lz->reset = &lzma_decoder_reset;
946 lz->set_uncompressed = &lzma_decoder_uncompressed;
949 // All dictionary sizes are OK here. LZ decoder will take care of
950 // the special cases.
951 const lzma_options_lzma *options = opt;
952 lz_options->dict_size = options->dict_size;
953 lz_options->preset_dict = options->preset_dict;
954 lz_options->preset_dict_size = options->preset_dict_size;
960 /// Allocate and initialize LZMA decoder. This is used only via LZ
961 /// initialization (lzma_lzma_decoder_init() passes function pointer to
962 /// the LZ initialization).
964 lzma_decoder_init(lzma_lz_decoder *lz, lzma_allocator *allocator,
965 const void *options, lzma_lz_options *lz_options)
967 if (!is_lclppb_valid(options))
968 return LZMA_PROG_ERROR;
970 return_if_error(lzma_lzma_decoder_create(
971 lz, allocator, options, lz_options));
973 lzma_decoder_reset(lz->coder, options);
974 lzma_decoder_uncompressed(lz->coder, LZMA_VLI_UNKNOWN);
981 lzma_lzma_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
982 const lzma_filter_info *filters)
984 // LZMA can only be the last filter in the chain. This is enforced
985 // by the raw_decoder initialization.
986 assert(filters[1].init == NULL);
988 return lzma_lz_decoder_init(next, allocator, filters,
994 lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
996 if (byte > (4 * 5 + 4) * 9 + 8)
999 // See the file format specification to understand this.
1000 options->pb = byte / (9 * 5);
1001 byte -= options->pb * 9 * 5;
1002 options->lp = byte / 9;
1003 options->lc = byte - options->lp * 9;
1005 return options->lc + options->lp > LZMA_LCLP_MAX;
1010 lzma_lzma_decoder_memusage_nocheck(const void *options)
1012 const lzma_options_lzma *const opt = options;
1013 return sizeof(lzma_coder) + lzma_lz_decoder_memusage(opt->dict_size);
1018 lzma_lzma_decoder_memusage(const void *options)
1020 if (!is_lclppb_valid(options))
1023 return lzma_lzma_decoder_memusage_nocheck(options);
1028 lzma_lzma_props_decode(void **options, lzma_allocator *allocator,
1029 const uint8_t *props, size_t props_size)
1031 if (props_size != 5)
1032 return LZMA_OPTIONS_ERROR;
1034 lzma_options_lzma *opt
1035 = lzma_alloc(sizeof(lzma_options_lzma), allocator);
1037 return LZMA_MEM_ERROR;
1039 if (lzma_lzma_lclppb_decode(opt, props[0]))
1042 // All dictionary sizes are accepted, including zero. LZ decoder
1043 // will automatically use a dictionary at least a few KiB even if
1044 // a smaller dictionary is requested.
1045 opt->dict_size = unaligned_read32le(props + 1);
1047 opt->preset_dict = NULL;
1048 opt->preset_dict_size = 0;
1055 lzma_free(opt, allocator);
1056 return LZMA_OPTIONS_ERROR;