1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_decoder.c
4 /// \brief LZMA decoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007-2008 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #include "lz_decoder.h"
22 #include "lzma_common.h"
23 #include "lzma_decoder.h"
24 #include "range_decoder.h"
29 // Macros for (somewhat) size-optimized code.
30 #define seq_4(seq) seq
32 #define seq_6(seq) seq
34 #define seq_8(seq) seq
36 #define seq_len(seq) \
41 #define len_decode(target, ld, pos_state, seq) \
43 case seq ## _CHOICE: \
44 rc_if_0(ld.choice, seq ## _CHOICE) { \
45 rc_update_0(ld.choice); \
46 probs = ld.low[pos_state];\
47 limit = LEN_LOW_SYMBOLS; \
48 target = MATCH_LEN_MIN; \
50 rc_update_1(ld.choice); \
51 case seq ## _CHOICE2: \
52 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
53 rc_update_0(ld.choice2); \
54 probs = ld.mid[pos_state]; \
55 limit = LEN_MID_SYMBOLS; \
56 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
58 rc_update_1(ld.choice2); \
60 limit = LEN_HIGH_SYMBOLS; \
61 target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \
66 case seq ## _BITTREE: \
68 rc_bit(probs[symbol], , , seq ## _BITTREE); \
69 } while (symbol < limit); \
70 target += symbol - limit; \
100 #define seq_len(seq) \
118 #define len_decode(target, ld, pos_state, seq) \
121 case seq ## _CHOICE: \
122 rc_if_0(ld.choice, seq ## _CHOICE) { \
123 rc_update_0(ld.choice); \
124 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW0); \
125 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW1); \
126 rc_bit_case(ld.low[pos_state][symbol], , , seq ## _LOW2); \
127 target = symbol - LEN_LOW_SYMBOLS + MATCH_LEN_MIN; \
129 rc_update_1(ld.choice); \
130 case seq ## _CHOICE2: \
131 rc_if_0(ld.choice2, seq ## _CHOICE2) { \
132 rc_update_0(ld.choice2); \
133 rc_bit_case(ld.mid[pos_state][symbol], , , \
135 rc_bit_case(ld.mid[pos_state][symbol], , , \
137 rc_bit_case(ld.mid[pos_state][symbol], , , \
139 target = symbol - LEN_MID_SYMBOLS \
140 + MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
142 rc_update_1(ld.choice2); \
143 rc_bit_case(ld.high[symbol], , , seq ## _HIGH0); \
144 rc_bit_case(ld.high[symbol], , , seq ## _HIGH1); \
145 rc_bit_case(ld.high[symbol], , , seq ## _HIGH2); \
146 rc_bit_case(ld.high[symbol], , , seq ## _HIGH3); \
147 rc_bit_case(ld.high[symbol], , , seq ## _HIGH4); \
148 rc_bit_case(ld.high[symbol], , , seq ## _HIGH5); \
149 rc_bit_case(ld.high[symbol], , , seq ## _HIGH6); \
150 rc_bit_case(ld.high[symbol], , , seq ## _HIGH7); \
151 target = symbol - LEN_HIGH_SYMBOLS \
153 + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; \
161 /// Length decoder probabilities; see comments in lzma_common.h.
165 probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
166 probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
167 probability high[LEN_HIGH_SYMBOLS];
168 } lzma_length_decoder;
171 struct lzma_coder_s {
176 /// Literals; see comments in lzma_common.h.
177 probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
179 /// If 1, it's a match. Otherwise it's a single 8-bit literal.
180 probability is_match[STATES][POS_STATES_MAX];
182 /// If 1, it's a repeated match. The distance is one of rep0 .. rep3.
183 probability is_rep[STATES];
185 /// If 0, distance of a repeated match is rep0.
186 /// Otherwise check is_rep1.
187 probability is_rep0[STATES];
189 /// If 0, distance of a repeated match is rep1.
190 /// Otherwise check is_rep2.
191 probability is_rep1[STATES];
193 /// If 0, distance of a repeated match is rep2. Otherwise it is rep3.
194 probability is_rep2[STATES];
196 /// If 1, the repeated match has length of one byte. Otherwise
197 /// the length is decoded from rep_len_decoder.
198 probability is_rep0_long[STATES][POS_STATES_MAX];
200 /// Probability tree for the highest two bits of the match distance.
201 /// There is a separate probability tree for match lengths of
202 /// 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
203 probability pos_slot[LEN_TO_POS_STATES][POS_SLOTS];
205 /// Probility trees for additional bits for match distance when the
206 /// distance is in the range [4, 127].
207 probability pos_special[FULL_DISTANCES - END_POS_MODEL_INDEX];
209 /// Probability tree for the lowest four bits of a match distance
210 /// that is equal to or greater than 128.
211 probability pos_align[ALIGN_TABLE_SIZE];
213 /// Length of a normal match
214 lzma_length_decoder match_len_decoder;
216 /// Length of a repeated match
217 lzma_length_decoder rep_len_decoder;
224 lzma_range_decoder rc;
226 // Types of the most recently seen LZMA symbols
227 lzma_lzma_state state;
229 uint32_t rep0; ///< Distance of the latest match
230 uint32_t rep1; ///< Distance of second latest match
231 uint32_t rep2; ///< Distance of third latest match
232 uint32_t rep3; ///< Distance of fourth latest match
234 uint32_t pos_mask; // (1U << pos_bits) - 1
235 uint32_t literal_context_bits;
236 uint32_t literal_pos_mask;
238 /// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of
239 /// payload marker is expected.
240 lzma_vli uncompressed_size;
242 ////////////////////////////////
243 // State of incomplete symbol //
244 ////////////////////////////////
246 /// Position where to continue the decoder loop
251 seq_8(SEQ_LITERAL_MATCHED),
254 seq_len(SEQ_MATCH_LEN),
265 seq_len(SEQ_REP_LEN),
269 /// Base of the current probability tree
272 /// Symbol being decoded. This is also used as an index variable in
273 /// bittree decoders: probs[symbol]
276 /// Used as a loop termination condition on bittree decoders and
277 /// direct bits decoder.
280 /// Matched literal decoder: 0x100 or 0 to help avoiding branches.
281 /// Bittree reverse decoders: Offset of the next bit: 1 << offset
284 /// If decoding a literal: match byte.
285 /// If decoding a match: length of the match.
291 lzma_decode(lzma_coder *restrict coder, lzma_dict *restrict dictptr,
292 const uint8_t *restrict in,
293 size_t *restrict in_pos, size_t in_size)
299 if (!rc_read_init(&coder->rc, in, in_pos, in_size))
306 // Making local copies of often-used variables improves both
307 // speed and readability.
309 lzma_dict dict = *dictptr;
311 const size_t dict_start = dict.pos;
314 rc_to_local(coder->rc, *in_pos);
317 uint32_t state = coder->state;
318 uint32_t rep0 = coder->rep0;
319 uint32_t rep1 = coder->rep1;
320 uint32_t rep2 = coder->rep2;
321 uint32_t rep3 = coder->rep3;
323 const uint32_t pos_mask = coder->pos_mask;
325 // These variables are actually needed only if we last time ran
326 // out of input in the middle of the decoder loop.
327 probability *probs = coder->probs;
328 uint32_t symbol = coder->symbol;
329 uint32_t limit = coder->limit;
330 uint32_t offset = coder->offset;
331 uint32_t len = coder->len;
333 const uint32_t literal_pos_mask = coder->literal_pos_mask;
334 const uint32_t literal_context_bits = coder->literal_context_bits;
336 // Temporary variables
337 uint32_t pos_state = dict.pos & pos_mask;
339 lzma_ret ret = LZMA_OK;
341 // If uncompressed size is known, there must be no end of payload
343 const bool no_eopm = coder->uncompressed_size
345 if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
346 dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
348 // The main decoder loop. The "switch" is used to restart the decoder at
349 // correct location. Once restarted, the "switch" is no longer used.
350 switch (coder->sequence)
352 // Calculate new pos_state. This is skipped on the first loop
353 // since we already calculated it when setting up the local
355 pos_state = dict.pos & pos_mask;
359 if (unlikely(no_eopm && dict.pos == dict.limit))
362 rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
363 rc_update_0(coder->is_match[state][pos_state]);
365 // It's a literal i.e. a single 8-bit byte.
367 probs = literal_subcoder(coder->literal,
368 literal_context_bits, literal_pos_mask,
369 dict.pos, dict_get(&dict, 0));
372 if (is_literal_state(state)) {
373 // Decode literal without match byte.
377 rc_bit(probs[symbol], , , SEQ_LITERAL);
378 } while (symbol < (1 << 8));
380 rc_bit_case(probs[symbol], , , SEQ_LITERAL0);
381 rc_bit_case(probs[symbol], , , SEQ_LITERAL1);
382 rc_bit_case(probs[symbol], , , SEQ_LITERAL2);
383 rc_bit_case(probs[symbol], , , SEQ_LITERAL3);
384 rc_bit_case(probs[symbol], , , SEQ_LITERAL4);
385 rc_bit_case(probs[symbol], , , SEQ_LITERAL5);
386 rc_bit_case(probs[symbol], , , SEQ_LITERAL6);
387 rc_bit_case(probs[symbol], , , SEQ_LITERAL7);
390 // Decode literal with match byte.
392 // We store the byte we compare against
393 // ("match byte") to "len" to minimize the
394 // number of variables we need to store
395 // between decoder calls.
396 len = dict_get(&dict, rep0) << 1;
398 // The usage of "offset" allows omitting some
399 // branches, which should give tiny speed
400 // improvement on some CPUs. "offset" gets
401 // set to zero if match_bit didn't match.
405 case SEQ_LITERAL_MATCHED:
407 const uint32_t match_bit
409 const uint32_t subcoder_index
413 rc_bit(probs[subcoder_index],
414 offset &= ~match_bit,
416 SEQ_LITERAL_MATCHED);
418 // It seems to be faster to do this
419 // here instead of putting it to the
420 // beginning of the loop and then
421 // putting the "case" in the middle
425 } while (symbol < (1 << 8));
429 uint32_t subcoder_index;
433 match_bit = len & offset; \
434 subcoder_index = offset + match_bit + symbol; \
435 rc_bit(probs[subcoder_index], \
436 offset &= ~match_bit, \
437 offset &= match_bit, \
440 d(SEQ_LITERAL_MATCHED0);
442 d(SEQ_LITERAL_MATCHED1);
444 d(SEQ_LITERAL_MATCHED2);
446 d(SEQ_LITERAL_MATCHED3);
448 d(SEQ_LITERAL_MATCHED4);
450 d(SEQ_LITERAL_MATCHED5);
452 d(SEQ_LITERAL_MATCHED6);
454 d(SEQ_LITERAL_MATCHED7);
459 //update_literal(state);
460 // Use a lookup table to update to literal state,
461 // since compared to other state updates, this would
462 // need two branches.
463 static const lzma_lzma_state next_state[] = {
470 STATE_SHORTREP_LIT_LIT,
477 state = next_state[state];
479 case SEQ_LITERAL_WRITE:
480 if (unlikely(dict_put(&dict, symbol))) {
481 coder->sequence = SEQ_LITERAL_WRITE;
488 // Instead of a new byte we are going to get a byte range
489 // (distance and length) which will be repeated from our
492 rc_update_1(coder->is_match[state][pos_state]);
495 rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
496 // Not a repeated match
497 rc_update_0(coder->is_rep[state]);
500 // The latest three match distances are kept in
501 // memory in case there are repeated matches.
506 // Decode the length of the match.
507 len_decode(len, coder->match_len_decoder,
508 pos_state, SEQ_MATCH_LEN);
510 // Prepare to decode the highest two bits of the
512 probs = coder->pos_slot[get_len_to_pos_state(len)];
518 rc_bit(probs[symbol], , , SEQ_POS_SLOT);
519 } while (symbol < POS_SLOTS);
521 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT0);
522 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT1);
523 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT2);
524 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT3);
525 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT4);
526 rc_bit_case(probs[symbol], , , SEQ_POS_SLOT5);
528 // Get rid of the highest bit that was needed for
529 // indexing of the probability array.
531 assert(symbol <= 63);
533 if (symbol < START_POS_MODEL_INDEX) {
534 // Match distances [0, 3] have only two bits.
537 // Decode the lowest [1, 29] bits of
538 // the match distance.
539 limit = (symbol >> 1) - 1;
540 assert(limit >= 1 && limit <= 30);
541 rep0 = 2 + (symbol & 1);
543 if (symbol < END_POS_MODEL_INDEX) {
544 // Prepare to decode the low bits for
545 // a distance of [4, 127].
549 // -1 is fine, because we start
550 // decoding at probs[1], not probs[0].
551 // NOTE: This violates the C standard,
552 // since we are doing pointer
553 // arithmetic past the beginning of
555 assert((int32_t)(rep0 - symbol - 1)
557 assert((int32_t)(rep0 - symbol - 1)
559 probs = coder->pos_special + rep0
566 rc_bit(probs[symbol], ,
569 } while (++offset < limit);
574 rc_bit(probs[symbol], ,
580 rc_bit(probs[symbol], ,
586 rc_bit(probs[symbol], ,
592 rc_bit(probs[symbol], ,
598 // We need "symbol" only for
599 // indexing the probability
600 // array, thus we can use
601 // rc_bit_last() here to omit
602 // the unneeded updating of
604 rc_bit_last(probs[symbol], ,
610 // The distace is >= 128. Decode the
611 // lower bits without probabilities
612 // except the lowest four bits.
613 assert(symbol >= 14);
618 // Not worth manual unrolling
620 rc_direct(rep0, SEQ_DIRECT);
621 } while (--limit > 0);
623 // Decode the lowest four bits using
631 rc_bit(coder->pos_align[
635 } while (++offset < ALIGN_BITS);
638 rc_bit(coder->pos_align[symbol], ,
639 rep0 += 1, SEQ_ALIGN0);
641 rc_bit(coder->pos_align[symbol], ,
642 rep0 += 2, SEQ_ALIGN1);
644 rc_bit(coder->pos_align[symbol], ,
645 rep0 += 4, SEQ_ALIGN2);
647 // Like in SEQ_POS_MODEL, we don't
648 // need "symbol" for anything else
649 // than indexing the probability array.
650 rc_bit_last(coder->pos_align[symbol], ,
651 rep0 += 8, SEQ_ALIGN3);
654 if (rep0 == UINT32_MAX) {
655 // End of payload marker was
656 // found. It must not be
657 // present if uncompressed
659 if (coder->uncompressed_size
660 != LZMA_VLI_UNKNOWN) {
661 ret = LZMA_DATA_ERROR;
667 rc_normalize(SEQ_EOPM);
668 ret = LZMA_STREAM_END;
674 // Validate the distance we just decoded.
675 if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
676 ret = LZMA_DATA_ERROR;
681 rc_update_1(coder->is_rep[state]);
685 // The match distance is a value that we have had
686 // earlier. The latest four match distances are
687 // available as rep0, rep1, rep2 and rep3. We will
688 // now decode which of them is the new distance.
690 // There cannot be a match if we haven't produced
691 // any output, so check that first.
692 if (unlikely(!dict_is_distance_valid(&dict, 0))) {
693 ret = LZMA_DATA_ERROR;
698 rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
699 rc_update_0(coder->is_rep0[state]);
700 // The distance is rep0.
702 case SEQ_IS_REP0_LONG:
703 rc_if_0(coder->is_rep0_long[state][pos_state],
705 rc_update_0(coder->is_rep0_long[
708 update_short_rep(state);
711 if (unlikely(dict_put(&dict, dict_get(
713 coder->sequence = SEQ_SHORTREP;
720 // Repeating more than one byte at
722 rc_update_1(coder->is_rep0_long[
726 rc_update_1(coder->is_rep0[state]);
729 // The distance is rep1, rep2 or rep3. Once
730 // we find out which one of these three, it
731 // is stored to rep0 and rep1, rep2 and rep3
732 // are updated accordingly.
733 rc_if_0(coder->is_rep1[state], SEQ_IS_REP1) {
734 rc_update_0(coder->is_rep1[state]);
736 const uint32_t distance = rep1;
741 rc_update_1(coder->is_rep1[state]);
743 rc_if_0(coder->is_rep2[state],
745 rc_update_0(coder->is_rep2[
748 const uint32_t distance = rep2;
754 rc_update_1(coder->is_rep2[
757 const uint32_t distance = rep3;
766 update_long_rep(state);
768 // Decode the length of the repeated match.
769 len_decode(len, coder->rep_len_decoder,
770 pos_state, SEQ_REP_LEN);
773 /////////////////////////////////
774 // Repeat from history buffer. //
775 /////////////////////////////////
777 // The length is always between these limits. There is no way
778 // to trigger the algorithm to set len outside this range.
779 assert(len >= MATCH_LEN_MIN);
780 assert(len <= MATCH_LEN_MAX);
783 // Repeat len bytes from distance of rep0.
784 if (unlikely(dict_repeat(&dict, rep0, &len))) {
785 coder->sequence = SEQ_COPY;
790 rc_normalize(SEQ_NORMALIZE);
791 coder->sequence = SEQ_IS_MATCH;
796 // NOTE: Must not copy dict.limit.
797 dictptr->pos = dict.pos;
798 dictptr->full = dict.full;
800 rc_from_local(coder->rc, *in_pos);
802 coder->state = state;
808 coder->probs = probs;
809 coder->symbol = symbol;
810 coder->limit = limit;
811 coder->offset = offset;
814 // Update the remaining amount of uncompressed data if uncompressed
816 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
817 coder->uncompressed_size -= dict.pos - dict_start;
819 // Since there cannot be end of payload marker if the
820 // uncompressed size was known, we check here if we
821 // finished decoding.
822 if (coder->uncompressed_size == 0 && ret == LZMA_OK
823 && coder->sequence != SEQ_NORMALIZE)
824 ret = coder->sequence == SEQ_IS_MATCH
825 ? LZMA_STREAM_END : LZMA_DATA_ERROR;
828 // We can do an additional check in the range decoder to catch some
830 if (ret == LZMA_STREAM_END) {
831 if (!rc_is_finished(coder->rc))
832 ret = LZMA_DATA_ERROR;
834 // Reset the range decoder so that it is ready to reinitialize
835 // for a new LZMA2 chunk.
845 lzma_decoder_uncompressed(lzma_coder *coder, lzma_vli uncompressed_size)
847 coder->uncompressed_size = uncompressed_size;
852 lzma_lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
855 (*(lzma_coder **)(coder))->uncompressed_size = uncompressed_size;
860 lzma_decoder_reset(lzma_coder *coder, const void *opt)
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().
868 // Calculate pos_mask. We don't need pos_bits as is for anything.
869 coder->pos_mask = (1U << options->pos_bits) - 1;
871 // Initialize the literal decoder.
872 literal_init(coder->literal, options->literal_context_bits,
873 options->literal_pos_bits);
875 coder->literal_context_bits = options->literal_context_bits;
876 coder->literal_pos_mask = (1 << options->literal_pos_bits) - 1;
879 coder->state = STATE_LIT_LIT;
884 coder->pos_mask = (1 << options->pos_bits) - 1;
889 // Bit and bittree decoders
890 for (uint32_t i = 0; i < STATES; ++i) {
891 for (uint32_t j = 0; j <= coder->pos_mask; ++j) {
892 bit_reset(coder->is_match[i][j]);
893 bit_reset(coder->is_rep0_long[i][j]);
896 bit_reset(coder->is_rep[i]);
897 bit_reset(coder->is_rep0[i]);
898 bit_reset(coder->is_rep1[i]);
899 bit_reset(coder->is_rep2[i]);
902 for (uint32_t i = 0; i < LEN_TO_POS_STATES; ++i)
903 bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
905 for (uint32_t i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
906 bit_reset(coder->pos_special[i]);
908 bittree_reset(coder->pos_align, ALIGN_BITS);
910 // Len decoders (also bit/bittree)
911 const uint32_t num_pos_states = 1 << options->pos_bits;
912 bit_reset(coder->match_len_decoder.choice);
913 bit_reset(coder->match_len_decoder.choice2);
914 bit_reset(coder->rep_len_decoder.choice);
915 bit_reset(coder->rep_len_decoder.choice2);
917 for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
918 bittree_reset(coder->match_len_decoder.low[pos_state],
920 bittree_reset(coder->match_len_decoder.mid[pos_state],
923 bittree_reset(coder->rep_len_decoder.low[pos_state],
925 bittree_reset(coder->rep_len_decoder.mid[pos_state],
929 bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
930 bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
932 coder->sequence = SEQ_IS_MATCH;
944 lzma_lzma_decoder_create(lzma_lz_decoder *lz, lzma_allocator *allocator,
945 const void *opt, size_t *dict_size)
947 if (lz->coder == NULL) {
948 lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
949 if (lz->coder == NULL)
950 return LZMA_MEM_ERROR;
952 lz->code = &lzma_decode;
953 lz->reset = &lzma_decoder_reset;
954 lz->set_uncompressed = &lzma_decoder_uncompressed;
957 // All dictionary sizes are OK here. LZ decoder will take care of
958 // the special cases.
959 const lzma_options_lzma *options = opt;
960 *dict_size = options->dictionary_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, lzma_allocator *allocator,
971 const void *options, size_t *dict_size)
973 if (!is_lclppb_valid(options))
974 return LZMA_PROG_ERROR;
976 return_if_error(lzma_lzma_decoder_create(
977 lz, allocator, options, dict_size));
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, 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->pos_bits = byte / (9 * 5);
1007 byte -= options->pos_bits * 9 * 5;
1008 options->literal_pos_bits = byte / 9;
1009 options->literal_context_bits = byte - options->literal_pos_bits * 9;
1011 return options->literal_context_bits + options->literal_pos_bits
1012 > LZMA_LITERAL_BITS_MAX;
1017 lzma_lzma_decoder_memusage(const void *options)
1019 const lzma_options_lzma *const opt = options;
1020 const uint64_t lz_memusage
1021 = lzma_lz_decoder_memusage(opt->dictionary_size);
1022 if (lz_memusage == UINT64_MAX)
1025 return sizeof(lzma_coder) + lz_memusage;
1030 lzma_lzma_props_decode(void **options, lzma_allocator *allocator,
1031 const uint8_t *props, size_t props_size)
1033 if (props_size != 5)
1034 return LZMA_OPTIONS_ERROR;
1036 lzma_options_lzma *opt
1037 = lzma_alloc(sizeof(lzma_options_lzma), allocator);
1039 return LZMA_MEM_ERROR;
1041 if (lzma_lzma_lclppb_decode(opt, props[0]))
1044 // All dictionary sizes are accepted, including zero. LZ decoder
1045 // will automatically use a dictionary at least a few KiB even if
1046 // a smaller dictionary is requested.
1047 opt->dictionary_size = integer_read_32(props + 1);
1049 opt->preset_dictionary = NULL;
1050 opt->preset_dictionary_size = 0;
1057 lzma_free(opt, allocator);
1058 return LZMA_OPTIONS_ERROR;