1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file       lzma_decoder.c
4/// \brief      LZMA decoder
5///
6//  Authors:    Igor Pavlov
7//              Lasse Collin
8//
9//  This file has been put into the public domain.
10//  You can do whatever you want with this file.
11//
12///////////////////////////////////////////////////////////////////////////////
13
14#include "lz_decoder.h"
15#include "lzma_common.h"
16#include "lzma_decoder.h"
17#include "range_decoder.h"
18
19
20#ifdef HAVE_SMALL
21
22// Macros for (somewhat) size-optimized code.
23#define seq_4(seq) seq
24
25#define seq_6(seq) seq
26
27#define seq_8(seq) seq
28
29#define seq_len(seq) \
30	seq ## _CHOICE, \
31	seq ## _CHOICE2, \
32	seq ## _BITTREE
33
34#define len_decode(target, ld, pos_state, seq) \
35do { \
36case 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; \
42	} else { \
43		rc_update_1(ld.choice); \
44case 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; \
50		} else { \
51			rc_update_1(ld.choice2); \
52			probs = ld.high; \
53			limit = LEN_HIGH_SYMBOLS; \
54			target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \
55					+ LEN_MID_SYMBOLS; \
56		} \
57	} \
58	symbol = 1; \
59case seq ## _BITTREE: \
60	do { \
61		rc_bit(probs[symbol], , , seq ## _BITTREE); \
62	} while (symbol < limit); \
63	target += symbol - limit; \
64} while (0)
65
66#else // HAVE_SMALL
67
68// Unrolled versions
69#define seq_4(seq) \
70	seq ## 0, \
71	seq ## 1, \
72	seq ## 2, \
73	seq ## 3
74
75#define seq_6(seq) \
76	seq ## 0, \
77	seq ## 1, \
78	seq ## 2, \
79	seq ## 3, \
80	seq ## 4, \
81	seq ## 5
82
83#define seq_8(seq) \
84	seq ## 0, \
85	seq ## 1, \
86	seq ## 2, \
87	seq ## 3, \
88	seq ## 4, \
89	seq ## 5, \
90	seq ## 6, \
91	seq ## 7
92
93#define seq_len(seq) \
94	seq ## _CHOICE, \
95	seq ## _LOW0, \
96	seq ## _LOW1, \
97	seq ## _LOW2, \
98	seq ## _CHOICE2, \
99	seq ## _MID0, \
100	seq ## _MID1, \
101	seq ## _MID2, \
102	seq ## _HIGH0, \
103	seq ## _HIGH1, \
104	seq ## _HIGH2, \
105	seq ## _HIGH3, \
106	seq ## _HIGH4, \
107	seq ## _HIGH5, \
108	seq ## _HIGH6, \
109	seq ## _HIGH7
110
111#define len_decode(target, ld, pos_state, seq) \
112do { \
113	symbol = 1; \
114case 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; \
121	} else { \
122		rc_update_1(ld.choice); \
123case 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], , , \
127					seq ## _MID0); \
128			rc_bit_case(ld.mid[pos_state][symbol], , , \
129					seq ## _MID1); \
130			rc_bit_case(ld.mid[pos_state][symbol], , , \
131					seq ## _MID2); \
132			target = symbol - LEN_MID_SYMBOLS \
133					+ MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
134		} else { \
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 \
145					+ MATCH_LEN_MIN \
146					+ LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; \
147		} \
148	} \
149} while (0)
150
151#endif // HAVE_SMALL
152
153
154/// Length decoder probabilities; see comments in lzma_common.h.
155typedef struct {
156	probability choice;
157	probability choice2;
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;
162
163
164typedef struct {
165	///////////////////
166	// Probabilities //
167	///////////////////
168
169	/// Literals; see comments in lzma_common.h.
170	probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
171
172	/// If 1, it's a match. Otherwise it's a single 8-bit literal.
173	probability is_match[STATES][POS_STATES_MAX];
174
175	/// If 1, it's a repeated match. The distance is one of rep0 .. rep3.
176	probability is_rep[STATES];
177
178	/// If 0, distance of a repeated match is rep0.
179	/// Otherwise check is_rep1.
180	probability is_rep0[STATES];
181
182	/// If 0, distance of a repeated match is rep1.
183	/// Otherwise check is_rep2.
184	probability is_rep1[STATES];
185
186	/// If 0, distance of a repeated match is rep2. Otherwise it is rep3.
187	probability is_rep2[STATES];
188
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];
192
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];
197
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];
201
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];
205
206	/// Length of a normal match
207	lzma_length_decoder match_len_decoder;
208
209	/// Length of a repeated match
210	lzma_length_decoder rep_len_decoder;
211
212	///////////////////
213	// Decoder state //
214	///////////////////
215
216	// Range coder
217	lzma_range_decoder rc;
218
219	// Types of the most recently seen LZMA symbols
220	lzma_lzma_state state;
221
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
226
227	uint32_t pos_mask; // (1U << pb) - 1
228	uint32_t literal_context_bits;
229	uint32_t literal_pos_mask;
230
231	/// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of
232	/// payload marker is expected.
233	lzma_vli uncompressed_size;
234
235	////////////////////////////////
236	// State of incomplete symbol //
237	////////////////////////////////
238
239	/// Position where to continue the decoder loop
240	enum {
241		SEQ_NORMALIZE,
242		SEQ_IS_MATCH,
243		seq_8(SEQ_LITERAL),
244		seq_8(SEQ_LITERAL_MATCHED),
245		SEQ_LITERAL_WRITE,
246		SEQ_IS_REP,
247		seq_len(SEQ_MATCH_LEN),
248		seq_6(SEQ_DIST_SLOT),
249		SEQ_DIST_MODEL,
250		SEQ_DIRECT,
251		seq_4(SEQ_ALIGN),
252		SEQ_EOPM,
253		SEQ_IS_REP0,
254		SEQ_SHORTREP,
255		SEQ_IS_REP0_LONG,
256		SEQ_IS_REP1,
257		SEQ_IS_REP2,
258		seq_len(SEQ_REP_LEN),
259		SEQ_COPY,
260	} sequence;
261
262	/// Base of the current probability tree
263	probability *probs;
264
265	/// Symbol being decoded. This is also used as an index variable in
266	/// bittree decoders: probs[symbol]
267	uint32_t symbol;
268
269	/// Used as a loop termination condition on bittree decoders and
270	/// direct bits decoder.
271	uint32_t limit;
272
273	/// Matched literal decoder: 0x100 or 0 to help avoiding branches.
274	/// Bittree reverse decoders: Offset of the next bit: 1 << offset
275	uint32_t offset;
276
277	/// If decoding a literal: match byte.
278	/// If decoding a match: length of the match.
279	uint32_t len;
280} lzma_lzma1_decoder;
281
282
283static lzma_ret
284lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
285		const uint8_t *restrict in,
286		size_t *restrict in_pos, size_t in_size)
287{
288	lzma_lzma1_decoder *restrict coder = coder_ptr;
289
290	////////////////////
291	// Initialization //
292	////////////////////
293
294	{
295		const lzma_ret ret = rc_read_init(
296				&coder->rc, in, in_pos, in_size);
297		if (ret != LZMA_STREAM_END)
298			return ret;
299	}
300
301	///////////////
302	// Variables //
303	///////////////
304
305	// Making local copies of often-used variables improves both
306	// speed and readability.
307
308	lzma_dict dict = *dictptr;
309
310	const size_t dict_start = dict.pos;
311
312	// Range decoder
313	rc_to_local(coder->rc, *in_pos);
314
315	// State
316	uint32_t state = coder->state;
317	uint32_t rep0 = coder->rep0;
318	uint32_t rep1 = coder->rep1;
319	uint32_t rep2 = coder->rep2;
320	uint32_t rep3 = coder->rep3;
321
322	const uint32_t pos_mask = coder->pos_mask;
323
324	// These variables are actually needed only if we last time ran
325	// out of input in the middle of the decoder loop.
326	probability *probs = coder->probs;
327	uint32_t symbol = coder->symbol;
328	uint32_t limit = coder->limit;
329	uint32_t offset = coder->offset;
330	uint32_t len = coder->len;
331
332	const uint32_t literal_pos_mask = coder->literal_pos_mask;
333	const uint32_t literal_context_bits = coder->literal_context_bits;
334
335	// Temporary variables
336	uint32_t pos_state = dict.pos & pos_mask;
337
338	lzma_ret ret = LZMA_OK;
339
340	// If uncompressed size is known, there must be no end of payload
341	// marker.
342	const bool no_eopm = coder->uncompressed_size
343			!= LZMA_VLI_UNKNOWN;
344	if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
345		dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
346
347	// The main decoder loop. The "switch" is used to restart the decoder at
348	// correct location. Once restarted, the "switch" is no longer used.
349	switch (coder->sequence)
350	while (true) {
351		// Calculate new pos_state. This is skipped on the first loop
352		// since we already calculated it when setting up the local
353		// variables.
354		pos_state = dict.pos & pos_mask;
355
356	case SEQ_NORMALIZE:
357	case SEQ_IS_MATCH:
358		if (unlikely(no_eopm && dict.pos == dict.limit))
359			break;
360
361		rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
362			rc_update_0(coder->is_match[state][pos_state]);
363
364			// It's a literal i.e. a single 8-bit byte.
365
366			probs = literal_subcoder(coder->literal,
367					literal_context_bits, literal_pos_mask,
368					dict.pos, dict_get(&dict, 0));
369			symbol = 1;
370
371			if (is_literal_state(state)) {
372				// Decode literal without match byte.
373#ifdef HAVE_SMALL
374	case SEQ_LITERAL:
375				do {
376					rc_bit(probs[symbol], , , SEQ_LITERAL);
377				} while (symbol < (1 << 8));
378#else
379				rc_bit_case(probs[symbol], , , SEQ_LITERAL0);
380				rc_bit_case(probs[symbol], , , SEQ_LITERAL1);
381				rc_bit_case(probs[symbol], , , SEQ_LITERAL2);
382				rc_bit_case(probs[symbol], , , SEQ_LITERAL3);
383				rc_bit_case(probs[symbol], , , SEQ_LITERAL4);
384				rc_bit_case(probs[symbol], , , SEQ_LITERAL5);
385				rc_bit_case(probs[symbol], , , SEQ_LITERAL6);
386				rc_bit_case(probs[symbol], , , SEQ_LITERAL7);
387#endif
388			} else {
389				// Decode literal with match byte.
390				//
391				// We store the byte we compare against
392				// ("match byte") to "len" to minimize the
393				// number of variables we need to store
394				// between decoder calls.
395				len = dict_get(&dict, rep0) << 1;
396
397				// The usage of "offset" allows omitting some
398				// branches, which should give tiny speed
399				// improvement on some CPUs. "offset" gets
400				// set to zero if match_bit didn't match.
401				offset = 0x100;
402
403#ifdef HAVE_SMALL
404	case SEQ_LITERAL_MATCHED:
405				do {
406					const uint32_t match_bit
407							= len & offset;
408					const uint32_t subcoder_index
409							= offset + match_bit
410							+ symbol;
411
412					rc_bit(probs[subcoder_index],
413							offset &= ~match_bit,
414							offset &= match_bit,
415							SEQ_LITERAL_MATCHED);
416
417					// It seems to be faster to do this
418					// here instead of putting it to the
419					// beginning of the loop and then
420					// putting the "case" in the middle
421					// of the loop.
422					len <<= 1;
423
424				} while (symbol < (1 << 8));
425#else
426				// Unroll the loop.
427				uint32_t match_bit;
428				uint32_t subcoder_index;
429
430#	define d(seq) \
431		case seq: \
432			match_bit = len & offset; \
433			subcoder_index = offset + match_bit + symbol; \
434			rc_bit(probs[subcoder_index], \
435					offset &= ~match_bit, \
436					offset &= match_bit, \
437					seq)
438
439				d(SEQ_LITERAL_MATCHED0);
440				len <<= 1;
441				d(SEQ_LITERAL_MATCHED1);
442				len <<= 1;
443				d(SEQ_LITERAL_MATCHED2);
444				len <<= 1;
445				d(SEQ_LITERAL_MATCHED3);
446				len <<= 1;
447				d(SEQ_LITERAL_MATCHED4);
448				len <<= 1;
449				d(SEQ_LITERAL_MATCHED5);
450				len <<= 1;
451				d(SEQ_LITERAL_MATCHED6);
452				len <<= 1;
453				d(SEQ_LITERAL_MATCHED7);
454#	undef d
455#endif
456			}
457
458			//update_literal(state);
459			// Use a lookup table to update to literal state,
460			// since compared to other state updates, this would
461			// need two branches.
462			static const lzma_lzma_state next_state[] = {
463				STATE_LIT_LIT,
464				STATE_LIT_LIT,
465				STATE_LIT_LIT,
466				STATE_LIT_LIT,
467				STATE_MATCH_LIT_LIT,
468				STATE_REP_LIT_LIT,
469				STATE_SHORTREP_LIT_LIT,
470				STATE_MATCH_LIT,
471				STATE_REP_LIT,
472				STATE_SHORTREP_LIT,
473				STATE_MATCH_LIT,
474				STATE_REP_LIT
475			};
476			state = next_state[state];
477
478	case SEQ_LITERAL_WRITE:
479			if (unlikely(dict_put(&dict, symbol))) {
480				coder->sequence = SEQ_LITERAL_WRITE;
481				goto out;
482			}
483
484			continue;
485		}
486
487		// Instead of a new byte we are going to get a byte range
488		// (distance and length) which will be repeated from our
489		// output history.
490
491		rc_update_1(coder->is_match[state][pos_state]);
492
493	case SEQ_IS_REP:
494		rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
495			// Not a repeated match
496			rc_update_0(coder->is_rep[state]);
497			update_match(state);
498
499			// The latest three match distances are kept in
500			// memory in case there are repeated matches.
501			rep3 = rep2;
502			rep2 = rep1;
503			rep1 = rep0;
504
505			// Decode the length of the match.
506			len_decode(len, coder->match_len_decoder,
507					pos_state, SEQ_MATCH_LEN);
508
509			// Prepare to decode the highest two bits of the
510			// match distance.
511			probs = coder->dist_slot[get_dist_state(len)];
512			symbol = 1;
513
514#ifdef HAVE_SMALL
515	case SEQ_DIST_SLOT:
516			do {
517				rc_bit(probs[symbol], , , SEQ_DIST_SLOT);
518			} while (symbol < DIST_SLOTS);
519#else
520			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT0);
521			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT1);
522			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT2);
523			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT3);
524			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT4);
525			rc_bit_case(probs[symbol], , , SEQ_DIST_SLOT5);
526#endif
527			// Get rid of the highest bit that was needed for
528			// indexing of the probability array.
529			symbol -= DIST_SLOTS;
530			assert(symbol <= 63);
531
532			if (symbol < DIST_MODEL_START) {
533				// Match distances [0, 3] have only two bits.
534				rep0 = symbol;
535			} else {
536				// Decode the lowest [1, 29] bits of
537				// the match distance.
538				limit = (symbol >> 1) - 1;
539				assert(limit >= 1 && limit <= 30);
540				rep0 = 2 + (symbol & 1);
541
542				if (symbol < DIST_MODEL_END) {
543					// Prepare to decode the low bits for
544					// a distance of [4, 127].
545					assert(limit <= 5);
546					rep0 <<= limit;
547					assert(rep0 <= 96);
548					// -1 is fine, because we start
549					// decoding at probs[1], not probs[0].
550					// NOTE: This violates the C standard,
551					// since we are doing pointer
552					// arithmetic past the beginning of
553					// the array.
554					assert((int32_t)(rep0 - symbol - 1)
555							>= -1);
556					assert((int32_t)(rep0 - symbol - 1)
557							<= 82);
558					probs = coder->pos_special + rep0
559							- symbol - 1;
560					symbol = 1;
561					offset = 0;
562	case SEQ_DIST_MODEL:
563#ifdef HAVE_SMALL
564					do {
565						rc_bit(probs[symbol], ,
566							rep0 += 1 << offset,
567							SEQ_DIST_MODEL);
568					} while (++offset < limit);
569#else
570					switch (limit) {
571					case 5:
572						assert(offset == 0);
573						rc_bit(probs[symbol], ,
574							rep0 += 1,
575							SEQ_DIST_MODEL);
576						++offset;
577						--limit;
578					case 4:
579						rc_bit(probs[symbol], ,
580							rep0 += 1 << offset,
581							SEQ_DIST_MODEL);
582						++offset;
583						--limit;
584					case 3:
585						rc_bit(probs[symbol], ,
586							rep0 += 1 << offset,
587							SEQ_DIST_MODEL);
588						++offset;
589						--limit;
590					case 2:
591						rc_bit(probs[symbol], ,
592							rep0 += 1 << offset,
593							SEQ_DIST_MODEL);
594						++offset;
595						--limit;
596					case 1:
597						// We need "symbol" only for
598						// indexing the probability
599						// array, thus we can use
600						// rc_bit_last() here to omit
601						// the unneeded updating of
602						// "symbol".
603						rc_bit_last(probs[symbol], ,
604							rep0 += 1 << offset,
605							SEQ_DIST_MODEL);
606					}
607#endif
608				} else {
609					// The distance is >= 128. Decode the
610					// lower bits without probabilities
611					// except the lowest four bits.
612					assert(symbol >= 14);
613					assert(limit >= 6);
614					limit -= ALIGN_BITS;
615					assert(limit >= 2);
616	case SEQ_DIRECT:
617					// Not worth manual unrolling
618					do {
619						rc_direct(rep0, SEQ_DIRECT);
620					} while (--limit > 0);
621
622					// Decode the lowest four bits using
623					// probabilities.
624					rep0 <<= ALIGN_BITS;
625					symbol = 1;
626#ifdef HAVE_SMALL
627					offset = 0;
628	case SEQ_ALIGN:
629					do {
630						rc_bit(coder->pos_align[
631								symbol], ,
632							rep0 += 1 << offset,
633							SEQ_ALIGN);
634					} while (++offset < ALIGN_BITS);
635#else
636	case SEQ_ALIGN0:
637					rc_bit(coder->pos_align[symbol], ,
638							rep0 += 1, SEQ_ALIGN0);
639	case SEQ_ALIGN1:
640					rc_bit(coder->pos_align[symbol], ,
641							rep0 += 2, SEQ_ALIGN1);
642	case SEQ_ALIGN2:
643					rc_bit(coder->pos_align[symbol], ,
644							rep0 += 4, SEQ_ALIGN2);
645	case SEQ_ALIGN3:
646					// Like in SEQ_DIST_MODEL, we don't
647					// need "symbol" for anything else
648					// than indexing the probability array.
649					rc_bit_last(coder->pos_align[symbol], ,
650							rep0 += 8, SEQ_ALIGN3);
651#endif
652
653					if (rep0 == UINT32_MAX) {
654						// End of payload marker was
655						// found. It must not be
656						// present if uncompressed
657						// size is known.
658						if (coder->uncompressed_size
659						!= LZMA_VLI_UNKNOWN) {
660							ret = LZMA_DATA_ERROR;
661							goto out;
662						}
663
664	case SEQ_EOPM:
665						// LZMA1 stream with
666						// end-of-payload marker.
667						rc_normalize(SEQ_EOPM);
668						ret = LZMA_STREAM_END;
669						goto out;
670					}
671				}
672			}
673
674			// Validate the distance we just decoded.
675			if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
676				ret = LZMA_DATA_ERROR;
677				goto out;
678			}
679
680		} else {
681			rc_update_1(coder->is_rep[state]);
682
683			// Repeated match
684			//
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.
689			//
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;
694				goto out;
695			}
696
697	case SEQ_IS_REP0:
698			rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
699				rc_update_0(coder->is_rep0[state]);
700				// The distance is rep0.
701
702	case SEQ_IS_REP0_LONG:
703				rc_if_0(coder->is_rep0_long[state][pos_state],
704						SEQ_IS_REP0_LONG) {
705					rc_update_0(coder->is_rep0_long[
706							state][pos_state]);
707
708					update_short_rep(state);
709
710	case SEQ_SHORTREP:
711					if (unlikely(dict_put(&dict, dict_get(
712							&dict, rep0)))) {
713						coder->sequence = SEQ_SHORTREP;
714						goto out;
715					}
716
717					continue;
718				}
719
720				// Repeating more than one byte at
721				// distance of rep0.
722				rc_update_1(coder->is_rep0_long[
723						state][pos_state]);
724
725			} else {
726				rc_update_1(coder->is_rep0[state]);
727
728	case SEQ_IS_REP1:
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]);
735
736					const uint32_t distance = rep1;
737					rep1 = rep0;
738					rep0 = distance;
739
740				} else {
741					rc_update_1(coder->is_rep1[state]);
742	case SEQ_IS_REP2:
743					rc_if_0(coder->is_rep2[state],
744							SEQ_IS_REP2) {
745						rc_update_0(coder->is_rep2[
746								state]);
747
748						const uint32_t distance = rep2;
749						rep2 = rep1;
750						rep1 = rep0;
751						rep0 = distance;
752
753					} else {
754						rc_update_1(coder->is_rep2[
755								state]);
756
757						const uint32_t distance = rep3;
758						rep3 = rep2;
759						rep2 = rep1;
760						rep1 = rep0;
761						rep0 = distance;
762					}
763				}
764			}
765
766			update_long_rep(state);
767
768			// Decode the length of the repeated match.
769			len_decode(len, coder->rep_len_decoder,
770					pos_state, SEQ_REP_LEN);
771		}
772
773		/////////////////////////////////
774		// Repeat from history buffer. //
775		/////////////////////////////////
776
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);
781
782	case SEQ_COPY:
783		// Repeat len bytes from distance of rep0.
784		if (unlikely(dict_repeat(&dict, rep0, &len))) {
785			coder->sequence = SEQ_COPY;
786			goto out;
787		}
788	}
789
790	rc_normalize(SEQ_NORMALIZE);
791	coder->sequence = SEQ_IS_MATCH;
792
793out:
794	// Save state
795
796	// NOTE: Must not copy dict.limit.
797	dictptr->pos = dict.pos;
798	dictptr->full = dict.full;
799
800	rc_from_local(coder->rc, *in_pos);
801
802	coder->state = state;
803	coder->rep0 = rep0;
804	coder->rep1 = rep1;
805	coder->rep2 = rep2;
806	coder->rep3 = rep3;
807
808	coder->probs = probs;
809	coder->symbol = symbol;
810	coder->limit = limit;
811	coder->offset = offset;
812	coder->len = len;
813
814	// Update the remaining amount of uncompressed data if uncompressed
815	// size was known.
816	if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
817		coder->uncompressed_size -= dict.pos - dict_start;
818
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;
826	}
827
828	// We can do an additional check in the range decoder to catch some
829	// corrupted files.
830	if (ret == LZMA_STREAM_END) {
831		if (!rc_is_finished(coder->rc))
832			ret = LZMA_DATA_ERROR;
833
834		// Reset the range decoder so that it is ready to reinitialize
835		// for a new LZMA2 chunk.
836		rc_reset(coder->rc);
837	}
838
839	return ret;
840}
841
842
843
844static void
845lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
846{
847	lzma_lzma1_decoder *coder = coder_ptr;
848	coder->uncompressed_size = uncompressed_size;
849}
850
851
852static void
853lzma_decoder_reset(void *coder_ptr, const void *opt)
854{
855	lzma_lzma1_decoder *coder = coder_ptr;
856	const lzma_options_lzma *options = opt;
857
858	// NOTE: We assume that lc/lp/pb are valid since they were
859	// successfully decoded with lzma_lzma_decode_properties().
860
861	// Calculate pos_mask. We don't need pos_bits as is for anything.
862	coder->pos_mask = (1U << options->pb) - 1;
863
864	// Initialize the literal decoder.
865	literal_init(coder->literal, options->lc, options->lp);
866
867	coder->literal_context_bits = options->lc;
868	coder->literal_pos_mask = (1U << options->lp) - 1;
869
870	// State
871	coder->state = STATE_LIT_LIT;
872	coder->rep0 = 0;
873	coder->rep1 = 0;
874	coder->rep2 = 0;
875	coder->rep3 = 0;
876	coder->pos_mask = (1U << options->pb) - 1;
877
878	// Range decoder
879	rc_reset(coder->rc);
880
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]);
886		}
887
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]);
892	}
893
894	for (uint32_t i = 0; i < DIST_STATES; ++i)
895		bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS);
896
897	for (uint32_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)
898		bit_reset(coder->pos_special[i]);
899
900	bittree_reset(coder->pos_align, ALIGN_BITS);
901
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);
908
909	for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
910		bittree_reset(coder->match_len_decoder.low[pos_state],
911				LEN_LOW_BITS);
912		bittree_reset(coder->match_len_decoder.mid[pos_state],
913				LEN_MID_BITS);
914
915		bittree_reset(coder->rep_len_decoder.low[pos_state],
916				LEN_LOW_BITS);
917		bittree_reset(coder->rep_len_decoder.mid[pos_state],
918				LEN_MID_BITS);
919	}
920
921	bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
922	bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
923
924	coder->sequence = SEQ_IS_MATCH;
925	coder->probs = NULL;
926	coder->symbol = 0;
927	coder->limit = 0;
928	coder->offset = 0;
929	coder->len = 0;
930
931	return;
932}
933
934
935extern lzma_ret
936lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator,
937		const void *opt, lzma_lz_options *lz_options)
938{
939	if (lz->coder == NULL) {
940		lz->coder = lzma_alloc(sizeof(lzma_lzma1_decoder), allocator);
941		if (lz->coder == NULL)
942			return LZMA_MEM_ERROR;
943
944		lz->code = &lzma_decode;
945		lz->reset = &lzma_decoder_reset;
946		lz->set_uncompressed = &lzma_decoder_uncompressed;
947	}
948
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;
955
956	return LZMA_OK;
957}
958
959
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).
963static lzma_ret
964lzma_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,
965		const void *options, lzma_lz_options *lz_options)
966{
967	if (!is_lclppb_valid(options))
968		return LZMA_PROG_ERROR;
969
970	return_if_error(lzma_lzma_decoder_create(
971			lz, allocator, options, lz_options));
972
973	lzma_decoder_reset(lz->coder, options);
974	lzma_decoder_uncompressed(lz->coder, LZMA_VLI_UNKNOWN);
975
976	return LZMA_OK;
977}
978
979
980extern lzma_ret
981lzma_lzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
982		const lzma_filter_info *filters)
983{
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);
987
988	return lzma_lz_decoder_init(next, allocator, filters,
989			&lzma_decoder_init);
990}
991
992
993extern bool
994lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
995{
996	if (byte > (4 * 5 + 4) * 9 + 8)
997		return true;
998
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;
1004
1005	return options->lc + options->lp > LZMA_LCLP_MAX;
1006}
1007
1008
1009extern uint64_t
1010lzma_lzma_decoder_memusage_nocheck(const void *options)
1011{
1012	const lzma_options_lzma *const opt = options;
1013	return sizeof(lzma_lzma1_decoder)
1014			+ lzma_lz_decoder_memusage(opt->dict_size);
1015}
1016
1017
1018extern uint64_t
1019lzma_lzma_decoder_memusage(const void *options)
1020{
1021	if (!is_lclppb_valid(options))
1022		return UINT64_MAX;
1023
1024	return lzma_lzma_decoder_memusage_nocheck(options);
1025}
1026
1027
1028extern lzma_ret
1029lzma_lzma_props_decode(void **options, const lzma_allocator *allocator,
1030		const uint8_t *props, size_t props_size)
1031{
1032	if (props_size != 5)
1033		return LZMA_OPTIONS_ERROR;
1034
1035	lzma_options_lzma *opt
1036			= lzma_alloc(sizeof(lzma_options_lzma), allocator);
1037	if (opt == NULL)
1038		return LZMA_MEM_ERROR;
1039
1040	if (lzma_lzma_lclppb_decode(opt, props[0]))
1041		goto error;
1042
1043	// All dictionary sizes are accepted, including zero. LZ decoder
1044	// will automatically use a dictionary at least a few KiB even if
1045	// a smaller dictionary is requested.
1046	opt->dict_size = unaligned_read32le(props + 1);
1047
1048	opt->preset_dict = NULL;
1049	opt->preset_dict_size = 0;
1050
1051	*options = opt;
1052
1053	return LZMA_OK;
1054
1055error:
1056	lzma_free(opt, allocator);
1057	return LZMA_OPTIONS_ERROR;
1058}
1059