xref: /linux/lib/xz/xz_dec_lzma2.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1836d13a6SLasse Collin // SPDX-License-Identifier: 0BSD
2836d13a6SLasse Collin 
324fa0402SLasse Collin /*
424fa0402SLasse Collin  * LZMA2 decoder
524fa0402SLasse Collin  *
624fa0402SLasse Collin  * Authors: Lasse Collin <lasse.collin@tukaani.org>
7d89775fcSAlexander A. Klimov  *          Igor Pavlov <https://7-zip.org/>
824fa0402SLasse Collin  */
924fa0402SLasse Collin 
1024fa0402SLasse Collin #include "xz_private.h"
1124fa0402SLasse Collin #include "xz_lzma2.h"
1224fa0402SLasse Collin 
1324fa0402SLasse Collin /*
1424fa0402SLasse Collin  * Range decoder initialization eats the first five bytes of each LZMA chunk.
1524fa0402SLasse Collin  */
1624fa0402SLasse Collin #define RC_INIT_BYTES 5
1724fa0402SLasse Collin 
1824fa0402SLasse Collin /*
1924fa0402SLasse Collin  * Minimum number of usable input buffer to safely decode one LZMA symbol.
2024fa0402SLasse Collin  * The worst case is that we decode 22 bits using probabilities and 26
2124fa0402SLasse Collin  * direct bits. This may decode at maximum of 20 bytes of input. However,
2224fa0402SLasse Collin  * lzma_main() does an extra normalization before returning, thus we
2324fa0402SLasse Collin  * need to put 21 here.
2424fa0402SLasse Collin  */
2524fa0402SLasse Collin #define LZMA_IN_REQUIRED 21
2624fa0402SLasse Collin 
2724fa0402SLasse Collin /*
2824fa0402SLasse Collin  * Dictionary (history buffer)
2924fa0402SLasse Collin  *
3024fa0402SLasse Collin  * These are always true:
3124fa0402SLasse Collin  *    start <= pos <= full <= end
3224fa0402SLasse Collin  *    pos <= limit <= end
3324fa0402SLasse Collin  *
3424fa0402SLasse Collin  * In multi-call mode, also these are true:
3524fa0402SLasse Collin  *    end == size
3624fa0402SLasse Collin  *    size <= size_max
3724fa0402SLasse Collin  *    allocated <= size
3824fa0402SLasse Collin  *
3924fa0402SLasse Collin  * Most of these variables are size_t to support single-call mode,
4024fa0402SLasse Collin  * in which the dictionary variables address the actual output
4124fa0402SLasse Collin  * buffer directly.
4224fa0402SLasse Collin  */
4324fa0402SLasse Collin struct dictionary {
4424fa0402SLasse Collin 	/* Beginning of the history buffer */
4524fa0402SLasse Collin 	uint8_t *buf;
4624fa0402SLasse Collin 
4724fa0402SLasse Collin 	/* Old position in buf (before decoding more data) */
4824fa0402SLasse Collin 	size_t start;
4924fa0402SLasse Collin 
5024fa0402SLasse Collin 	/* Position in buf */
5124fa0402SLasse Collin 	size_t pos;
5224fa0402SLasse Collin 
5324fa0402SLasse Collin 	/*
5424fa0402SLasse Collin 	 * How full dictionary is. This is used to detect corrupt input that
5524fa0402SLasse Collin 	 * would read beyond the beginning of the uncompressed stream.
5624fa0402SLasse Collin 	 */
5724fa0402SLasse Collin 	size_t full;
5824fa0402SLasse Collin 
5924fa0402SLasse Collin 	/* Write limit; we don't write to buf[limit] or later bytes. */
6024fa0402SLasse Collin 	size_t limit;
6124fa0402SLasse Collin 
6224fa0402SLasse Collin 	/*
6324fa0402SLasse Collin 	 * End of the dictionary buffer. In multi-call mode, this is
6424fa0402SLasse Collin 	 * the same as the dictionary size. In single-call mode, this
6524fa0402SLasse Collin 	 * indicates the size of the output buffer.
6624fa0402SLasse Collin 	 */
6724fa0402SLasse Collin 	size_t end;
6824fa0402SLasse Collin 
6924fa0402SLasse Collin 	/*
7024fa0402SLasse Collin 	 * Size of the dictionary as specified in Block Header. This is used
7124fa0402SLasse Collin 	 * together with "full" to detect corrupt input that would make us
7224fa0402SLasse Collin 	 * read beyond the beginning of the uncompressed stream.
7324fa0402SLasse Collin 	 */
7424fa0402SLasse Collin 	uint32_t size;
7524fa0402SLasse Collin 
7624fa0402SLasse Collin 	/*
7724fa0402SLasse Collin 	 * Maximum allowed dictionary size in multi-call mode.
7824fa0402SLasse Collin 	 * This is ignored in single-call mode.
7924fa0402SLasse Collin 	 */
8024fa0402SLasse Collin 	uint32_t size_max;
8124fa0402SLasse Collin 
8224fa0402SLasse Collin 	/*
8324fa0402SLasse Collin 	 * Amount of memory currently allocated for the dictionary.
8424fa0402SLasse Collin 	 * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
8524fa0402SLasse Collin 	 * size_max is always the same as the allocated size.)
8624fa0402SLasse Collin 	 */
8724fa0402SLasse Collin 	uint32_t allocated;
8824fa0402SLasse Collin 
8924fa0402SLasse Collin 	/* Operation mode */
9024fa0402SLasse Collin 	enum xz_mode mode;
9124fa0402SLasse Collin };
9224fa0402SLasse Collin 
9324fa0402SLasse Collin /* Range decoder */
9424fa0402SLasse Collin struct rc_dec {
9524fa0402SLasse Collin 	uint32_t range;
9624fa0402SLasse Collin 	uint32_t code;
9724fa0402SLasse Collin 
9824fa0402SLasse Collin 	/*
9924fa0402SLasse Collin 	 * Number of initializing bytes remaining to be read
10024fa0402SLasse Collin 	 * by rc_read_init().
10124fa0402SLasse Collin 	 */
10224fa0402SLasse Collin 	uint32_t init_bytes_left;
10324fa0402SLasse Collin 
10424fa0402SLasse Collin 	/*
10524fa0402SLasse Collin 	 * Buffer from which we read our input. It can be either
10624fa0402SLasse Collin 	 * temp.buf or the caller-provided input buffer.
10724fa0402SLasse Collin 	 */
10824fa0402SLasse Collin 	const uint8_t *in;
10924fa0402SLasse Collin 	size_t in_pos;
11024fa0402SLasse Collin 	size_t in_limit;
11124fa0402SLasse Collin };
11224fa0402SLasse Collin 
11324fa0402SLasse Collin /* Probabilities for a length decoder. */
11424fa0402SLasse Collin struct lzma_len_dec {
11524fa0402SLasse Collin 	/* Probability of match length being at least 10 */
11624fa0402SLasse Collin 	uint16_t choice;
11724fa0402SLasse Collin 
11824fa0402SLasse Collin 	/* Probability of match length being at least 18 */
11924fa0402SLasse Collin 	uint16_t choice2;
12024fa0402SLasse Collin 
12124fa0402SLasse Collin 	/* Probabilities for match lengths 2-9 */
12224fa0402SLasse Collin 	uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
12324fa0402SLasse Collin 
12424fa0402SLasse Collin 	/* Probabilities for match lengths 10-17 */
12524fa0402SLasse Collin 	uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
12624fa0402SLasse Collin 
12724fa0402SLasse Collin 	/* Probabilities for match lengths 18-273 */
12824fa0402SLasse Collin 	uint16_t high[LEN_HIGH_SYMBOLS];
12924fa0402SLasse Collin };
13024fa0402SLasse Collin 
13124fa0402SLasse Collin struct lzma_dec {
13224fa0402SLasse Collin 	/* Distances of latest four matches */
13324fa0402SLasse Collin 	uint32_t rep0;
13424fa0402SLasse Collin 	uint32_t rep1;
13524fa0402SLasse Collin 	uint32_t rep2;
13624fa0402SLasse Collin 	uint32_t rep3;
13724fa0402SLasse Collin 
13824fa0402SLasse Collin 	/* Types of the most recently seen LZMA symbols */
13924fa0402SLasse Collin 	enum lzma_state state;
14024fa0402SLasse Collin 
14124fa0402SLasse Collin 	/*
14224fa0402SLasse Collin 	 * Length of a match. This is updated so that dict_repeat can
14324fa0402SLasse Collin 	 * be called again to finish repeating the whole match.
14424fa0402SLasse Collin 	 */
14524fa0402SLasse Collin 	uint32_t len;
14624fa0402SLasse Collin 
14724fa0402SLasse Collin 	/*
14824fa0402SLasse Collin 	 * LZMA properties or related bit masks (number of literal
14905911c5dSZhen Lei 	 * context bits, a mask derived from the number of literal
15005911c5dSZhen Lei 	 * position bits, and a mask derived from the number
15124fa0402SLasse Collin 	 * position bits)
15224fa0402SLasse Collin 	 */
15324fa0402SLasse Collin 	uint32_t lc;
15424fa0402SLasse Collin 	uint32_t literal_pos_mask; /* (1 << lp) - 1 */
15524fa0402SLasse Collin 	uint32_t pos_mask;         /* (1 << pb) - 1 */
15624fa0402SLasse Collin 
15724fa0402SLasse Collin 	/* If 1, it's a match. Otherwise it's a single 8-bit literal. */
15824fa0402SLasse Collin 	uint16_t is_match[STATES][POS_STATES_MAX];
15924fa0402SLasse Collin 
16024fa0402SLasse Collin 	/* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
16124fa0402SLasse Collin 	uint16_t is_rep[STATES];
16224fa0402SLasse Collin 
16324fa0402SLasse Collin 	/*
16424fa0402SLasse Collin 	 * If 0, distance of a repeated match is rep0.
16524fa0402SLasse Collin 	 * Otherwise check is_rep1.
16624fa0402SLasse Collin 	 */
16724fa0402SLasse Collin 	uint16_t is_rep0[STATES];
16824fa0402SLasse Collin 
16924fa0402SLasse Collin 	/*
17024fa0402SLasse Collin 	 * If 0, distance of a repeated match is rep1.
17124fa0402SLasse Collin 	 * Otherwise check is_rep2.
17224fa0402SLasse Collin 	 */
17324fa0402SLasse Collin 	uint16_t is_rep1[STATES];
17424fa0402SLasse Collin 
17524fa0402SLasse Collin 	/* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
17624fa0402SLasse Collin 	uint16_t is_rep2[STATES];
17724fa0402SLasse Collin 
17824fa0402SLasse Collin 	/*
17924fa0402SLasse Collin 	 * If 1, the repeated match has length of one byte. Otherwise
18024fa0402SLasse Collin 	 * the length is decoded from rep_len_decoder.
18124fa0402SLasse Collin 	 */
18224fa0402SLasse Collin 	uint16_t is_rep0_long[STATES][POS_STATES_MAX];
18324fa0402SLasse Collin 
18424fa0402SLasse Collin 	/*
18524fa0402SLasse Collin 	 * Probability tree for the highest two bits of the match
18624fa0402SLasse Collin 	 * distance. There is a separate probability tree for match
18724fa0402SLasse Collin 	 * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
18824fa0402SLasse Collin 	 */
18924fa0402SLasse Collin 	uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
19024fa0402SLasse Collin 
19124fa0402SLasse Collin 	/*
19224fa0402SLasse Collin 	 * Probility trees for additional bits for match distance
19324fa0402SLasse Collin 	 * when the distance is in the range [4, 127].
19424fa0402SLasse Collin 	 */
19524fa0402SLasse Collin 	uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
19624fa0402SLasse Collin 
19724fa0402SLasse Collin 	/*
19824fa0402SLasse Collin 	 * Probability tree for the lowest four bits of a match
19924fa0402SLasse Collin 	 * distance that is equal to or greater than 128.
20024fa0402SLasse Collin 	 */
20124fa0402SLasse Collin 	uint16_t dist_align[ALIGN_SIZE];
20224fa0402SLasse Collin 
20324fa0402SLasse Collin 	/* Length of a normal match */
20424fa0402SLasse Collin 	struct lzma_len_dec match_len_dec;
20524fa0402SLasse Collin 
20624fa0402SLasse Collin 	/* Length of a repeated match */
20724fa0402SLasse Collin 	struct lzma_len_dec rep_len_dec;
20824fa0402SLasse Collin 
20924fa0402SLasse Collin 	/* Probabilities of literals */
21024fa0402SLasse Collin 	uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
21124fa0402SLasse Collin };
21224fa0402SLasse Collin 
21324fa0402SLasse Collin struct lzma2_dec {
21424fa0402SLasse Collin 	/* Position in xz_dec_lzma2_run(). */
21524fa0402SLasse Collin 	enum lzma2_seq {
21624fa0402SLasse Collin 		SEQ_CONTROL,
21724fa0402SLasse Collin 		SEQ_UNCOMPRESSED_1,
21824fa0402SLasse Collin 		SEQ_UNCOMPRESSED_2,
21924fa0402SLasse Collin 		SEQ_COMPRESSED_0,
22024fa0402SLasse Collin 		SEQ_COMPRESSED_1,
22124fa0402SLasse Collin 		SEQ_PROPERTIES,
22224fa0402SLasse Collin 		SEQ_LZMA_PREPARE,
22324fa0402SLasse Collin 		SEQ_LZMA_RUN,
22424fa0402SLasse Collin 		SEQ_COPY
22524fa0402SLasse Collin 	} sequence;
22624fa0402SLasse Collin 
22724fa0402SLasse Collin 	/* Next position after decoding the compressed size of the chunk. */
22824fa0402SLasse Collin 	enum lzma2_seq next_sequence;
22924fa0402SLasse Collin 
23024fa0402SLasse Collin 	/* Uncompressed size of LZMA chunk (2 MiB at maximum) */
23124fa0402SLasse Collin 	uint32_t uncompressed;
23224fa0402SLasse Collin 
23324fa0402SLasse Collin 	/*
23424fa0402SLasse Collin 	 * Compressed size of LZMA chunk or compressed/uncompressed
23524fa0402SLasse Collin 	 * size of uncompressed chunk (64 KiB at maximum)
23624fa0402SLasse Collin 	 */
23724fa0402SLasse Collin 	uint32_t compressed;
23824fa0402SLasse Collin 
23924fa0402SLasse Collin 	/*
24024fa0402SLasse Collin 	 * True if dictionary reset is needed. This is false before
24124fa0402SLasse Collin 	 * the first chunk (LZMA or uncompressed).
24224fa0402SLasse Collin 	 */
24324fa0402SLasse Collin 	bool need_dict_reset;
24424fa0402SLasse Collin 
24524fa0402SLasse Collin 	/*
24624fa0402SLasse Collin 	 * True if new LZMA properties are needed. This is false
24724fa0402SLasse Collin 	 * before the first LZMA chunk.
24824fa0402SLasse Collin 	 */
24924fa0402SLasse Collin 	bool need_props;
250aaa2975fSLasse Collin 
251aaa2975fSLasse Collin #ifdef XZ_DEC_MICROLZMA
252aaa2975fSLasse Collin 	bool pedantic_microlzma;
253aaa2975fSLasse Collin #endif
25424fa0402SLasse Collin };
25524fa0402SLasse Collin 
25624fa0402SLasse Collin struct xz_dec_lzma2 {
25724fa0402SLasse Collin 	/*
25824fa0402SLasse Collin 	 * The order below is important on x86 to reduce code size and
25924fa0402SLasse Collin 	 * it shouldn't hurt on other platforms. Everything up to and
26024fa0402SLasse Collin 	 * including lzma.pos_mask are in the first 128 bytes on x86-32,
26124fa0402SLasse Collin 	 * which allows using smaller instructions to access those
26224fa0402SLasse Collin 	 * variables. On x86-64, fewer variables fit into the first 128
26324fa0402SLasse Collin 	 * bytes, but this is still the best order without sacrificing
26424fa0402SLasse Collin 	 * the readability by splitting the structures.
26524fa0402SLasse Collin 	 */
26624fa0402SLasse Collin 	struct rc_dec rc;
26724fa0402SLasse Collin 	struct dictionary dict;
26824fa0402SLasse Collin 	struct lzma2_dec lzma2;
26924fa0402SLasse Collin 	struct lzma_dec lzma;
27024fa0402SLasse Collin 
27124fa0402SLasse Collin 	/*
27224fa0402SLasse Collin 	 * Temporary buffer which holds small number of input bytes between
27324fa0402SLasse Collin 	 * decoder calls. See lzma2_lzma() for details.
27424fa0402SLasse Collin 	 */
27524fa0402SLasse Collin 	struct {
27624fa0402SLasse Collin 		uint32_t size;
27724fa0402SLasse Collin 		uint8_t buf[3 * LZMA_IN_REQUIRED];
27824fa0402SLasse Collin 	} temp;
27924fa0402SLasse Collin };
28024fa0402SLasse Collin 
28124fa0402SLasse Collin /**************
28224fa0402SLasse Collin  * Dictionary *
28324fa0402SLasse Collin  **************/
28424fa0402SLasse Collin 
28524fa0402SLasse Collin /*
28624fa0402SLasse Collin  * Reset the dictionary state. When in single-call mode, set up the beginning
28724fa0402SLasse Collin  * of the dictionary to point to the actual output buffer.
28824fa0402SLasse Collin  */
dict_reset(struct dictionary * dict,struct xz_buf * b)28924fa0402SLasse Collin static void dict_reset(struct dictionary *dict, struct xz_buf *b)
29024fa0402SLasse Collin {
29124fa0402SLasse Collin 	if (DEC_IS_SINGLE(dict->mode)) {
29224fa0402SLasse Collin 		dict->buf = b->out + b->out_pos;
29324fa0402SLasse Collin 		dict->end = b->out_size - b->out_pos;
29424fa0402SLasse Collin 	}
29524fa0402SLasse Collin 
29624fa0402SLasse Collin 	dict->start = 0;
29724fa0402SLasse Collin 	dict->pos = 0;
29824fa0402SLasse Collin 	dict->limit = 0;
29924fa0402SLasse Collin 	dict->full = 0;
30024fa0402SLasse Collin }
30124fa0402SLasse Collin 
30224fa0402SLasse Collin /* Set dictionary write limit */
dict_limit(struct dictionary * dict,size_t out_max)30324fa0402SLasse Collin static void dict_limit(struct dictionary *dict, size_t out_max)
30424fa0402SLasse Collin {
30524fa0402SLasse Collin 	if (dict->end - dict->pos <= out_max)
30624fa0402SLasse Collin 		dict->limit = dict->end;
30724fa0402SLasse Collin 	else
30824fa0402SLasse Collin 		dict->limit = dict->pos + out_max;
30924fa0402SLasse Collin }
31024fa0402SLasse Collin 
31124fa0402SLasse Collin /* Return true if at least one byte can be written into the dictionary. */
dict_has_space(const struct dictionary * dict)31224fa0402SLasse Collin static inline bool dict_has_space(const struct dictionary *dict)
31324fa0402SLasse Collin {
31424fa0402SLasse Collin 	return dict->pos < dict->limit;
31524fa0402SLasse Collin }
31624fa0402SLasse Collin 
31724fa0402SLasse Collin /*
31824fa0402SLasse Collin  * Get a byte from the dictionary at the given distance. The distance is
31924fa0402SLasse Collin  * assumed to valid, or as a special case, zero when the dictionary is
32024fa0402SLasse Collin  * still empty. This special case is needed for single-call decoding to
32124fa0402SLasse Collin  * avoid writing a '\0' to the end of the destination buffer.
32224fa0402SLasse Collin  */
dict_get(const struct dictionary * dict,uint32_t dist)32324fa0402SLasse Collin static inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist)
32424fa0402SLasse Collin {
32524fa0402SLasse Collin 	size_t offset = dict->pos - dist - 1;
32624fa0402SLasse Collin 
32724fa0402SLasse Collin 	if (dist >= dict->pos)
32824fa0402SLasse Collin 		offset += dict->end;
32924fa0402SLasse Collin 
33024fa0402SLasse Collin 	return dict->full > 0 ? dict->buf[offset] : 0;
33124fa0402SLasse Collin }
33224fa0402SLasse Collin 
33324fa0402SLasse Collin /*
33424fa0402SLasse Collin  * Put one byte into the dictionary. It is assumed that there is space for it.
33524fa0402SLasse Collin  */
dict_put(struct dictionary * dict,uint8_t byte)33624fa0402SLasse Collin static inline void dict_put(struct dictionary *dict, uint8_t byte)
33724fa0402SLasse Collin {
33824fa0402SLasse Collin 	dict->buf[dict->pos++] = byte;
33924fa0402SLasse Collin 
34024fa0402SLasse Collin 	if (dict->full < dict->pos)
34124fa0402SLasse Collin 		dict->full = dict->pos;
34224fa0402SLasse Collin }
34324fa0402SLasse Collin 
34424fa0402SLasse Collin /*
34524fa0402SLasse Collin  * Repeat given number of bytes from the given distance. If the distance is
34624fa0402SLasse Collin  * invalid, false is returned. On success, true is returned and *len is
34724fa0402SLasse Collin  * updated to indicate how many bytes were left to be repeated.
34824fa0402SLasse Collin  */
dict_repeat(struct dictionary * dict,uint32_t * len,uint32_t dist)34924fa0402SLasse Collin static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
35024fa0402SLasse Collin {
35124fa0402SLasse Collin 	size_t back;
35224fa0402SLasse Collin 	uint32_t left;
35324fa0402SLasse Collin 
35424fa0402SLasse Collin 	if (dist >= dict->full || dist >= dict->size)
35524fa0402SLasse Collin 		return false;
35624fa0402SLasse Collin 
35724fa0402SLasse Collin 	left = min_t(size_t, dict->limit - dict->pos, *len);
35824fa0402SLasse Collin 	*len -= left;
35924fa0402SLasse Collin 
36024fa0402SLasse Collin 	back = dict->pos - dist - 1;
36124fa0402SLasse Collin 	if (dist >= dict->pos)
36224fa0402SLasse Collin 		back += dict->end;
36324fa0402SLasse Collin 
36424fa0402SLasse Collin 	do {
36524fa0402SLasse Collin 		dict->buf[dict->pos++] = dict->buf[back++];
36624fa0402SLasse Collin 		if (back == dict->end)
36724fa0402SLasse Collin 			back = 0;
36824fa0402SLasse Collin 	} while (--left > 0);
36924fa0402SLasse Collin 
37024fa0402SLasse Collin 	if (dict->full < dict->pos)
37124fa0402SLasse Collin 		dict->full = dict->pos;
37224fa0402SLasse Collin 
37324fa0402SLasse Collin 	return true;
37424fa0402SLasse Collin }
37524fa0402SLasse Collin 
37624fa0402SLasse Collin /* Copy uncompressed data as is from input to dictionary and output buffers. */
dict_uncompressed(struct dictionary * dict,struct xz_buf * b,uint32_t * left)37724fa0402SLasse Collin static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
37824fa0402SLasse Collin 			      uint32_t *left)
37924fa0402SLasse Collin {
38024fa0402SLasse Collin 	size_t copy_size;
38124fa0402SLasse Collin 
38224fa0402SLasse Collin 	while (*left > 0 && b->in_pos < b->in_size
38324fa0402SLasse Collin 			&& b->out_pos < b->out_size) {
38424fa0402SLasse Collin 		copy_size = min(b->in_size - b->in_pos,
38524fa0402SLasse Collin 				b->out_size - b->out_pos);
38624fa0402SLasse Collin 		if (copy_size > dict->end - dict->pos)
38724fa0402SLasse Collin 			copy_size = dict->end - dict->pos;
38824fa0402SLasse Collin 		if (copy_size > *left)
38924fa0402SLasse Collin 			copy_size = *left;
39024fa0402SLasse Collin 
39124fa0402SLasse Collin 		*left -= copy_size;
39224fa0402SLasse Collin 
39383d3c4f2SLasse Collin 		/*
39483d3c4f2SLasse Collin 		 * If doing in-place decompression in single-call mode and the
39583d3c4f2SLasse Collin 		 * uncompressed size of the file is larger than the caller
39683d3c4f2SLasse Collin 		 * thought (i.e. it is invalid input!), the buffers below may
39783d3c4f2SLasse Collin 		 * overlap and cause undefined behavior with memcpy().
39883d3c4f2SLasse Collin 		 * With valid inputs memcpy() would be fine here.
39983d3c4f2SLasse Collin 		 */
40083d3c4f2SLasse Collin 		memmove(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
40124fa0402SLasse Collin 		dict->pos += copy_size;
40224fa0402SLasse Collin 
40324fa0402SLasse Collin 		if (dict->full < dict->pos)
40424fa0402SLasse Collin 			dict->full = dict->pos;
40524fa0402SLasse Collin 
40624fa0402SLasse Collin 		if (DEC_IS_MULTI(dict->mode)) {
40724fa0402SLasse Collin 			if (dict->pos == dict->end)
40824fa0402SLasse Collin 				dict->pos = 0;
40924fa0402SLasse Collin 
41083d3c4f2SLasse Collin 			/*
41183d3c4f2SLasse Collin 			 * Like above but for multi-call mode: use memmove()
41283d3c4f2SLasse Collin 			 * to avoid undefined behavior with invalid input.
41383d3c4f2SLasse Collin 			 */
41483d3c4f2SLasse Collin 			memmove(b->out + b->out_pos, b->in + b->in_pos,
41524fa0402SLasse Collin 					copy_size);
41624fa0402SLasse Collin 		}
41724fa0402SLasse Collin 
41824fa0402SLasse Collin 		dict->start = dict->pos;
41924fa0402SLasse Collin 
42024fa0402SLasse Collin 		b->out_pos += copy_size;
42124fa0402SLasse Collin 		b->in_pos += copy_size;
42224fa0402SLasse Collin 	}
42324fa0402SLasse Collin }
42424fa0402SLasse Collin 
425aaa2975fSLasse Collin #ifdef XZ_DEC_MICROLZMA
426aaa2975fSLasse Collin #	define DICT_FLUSH_SUPPORTS_SKIPPING true
427aaa2975fSLasse Collin #else
428aaa2975fSLasse Collin #	define DICT_FLUSH_SUPPORTS_SKIPPING false
429aaa2975fSLasse Collin #endif
430aaa2975fSLasse Collin 
43124fa0402SLasse Collin /*
43224fa0402SLasse Collin  * Flush pending data from dictionary to b->out. It is assumed that there is
43324fa0402SLasse Collin  * enough space in b->out. This is guaranteed because caller uses dict_limit()
43424fa0402SLasse Collin  * before decoding data into the dictionary.
43524fa0402SLasse Collin  */
dict_flush(struct dictionary * dict,struct xz_buf * b)43624fa0402SLasse Collin static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
43724fa0402SLasse Collin {
43824fa0402SLasse Collin 	size_t copy_size = dict->pos - dict->start;
43924fa0402SLasse Collin 
44024fa0402SLasse Collin 	if (DEC_IS_MULTI(dict->mode)) {
44124fa0402SLasse Collin 		if (dict->pos == dict->end)
44224fa0402SLasse Collin 			dict->pos = 0;
44324fa0402SLasse Collin 
44483d3c4f2SLasse Collin 		/*
44583d3c4f2SLasse Collin 		 * These buffers cannot overlap even if doing in-place
44683d3c4f2SLasse Collin 		 * decompression because in multi-call mode dict->buf
44783d3c4f2SLasse Collin 		 * has been allocated by us in this file; it's not
44883d3c4f2SLasse Collin 		 * provided by the caller like in single-call mode.
449aaa2975fSLasse Collin 		 *
450aaa2975fSLasse Collin 		 * With MicroLZMA, b->out can be NULL to skip bytes that
451aaa2975fSLasse Collin 		 * the caller doesn't need. This cannot be done with XZ
452aaa2975fSLasse Collin 		 * because it would break BCJ filters.
45383d3c4f2SLasse Collin 		 */
454aaa2975fSLasse Collin 		if (!DICT_FLUSH_SUPPORTS_SKIPPING || b->out != NULL)
45524fa0402SLasse Collin 			memcpy(b->out + b->out_pos, dict->buf + dict->start,
45624fa0402SLasse Collin 					copy_size);
45724fa0402SLasse Collin 	}
45824fa0402SLasse Collin 
45924fa0402SLasse Collin 	dict->start = dict->pos;
46024fa0402SLasse Collin 	b->out_pos += copy_size;
46124fa0402SLasse Collin 	return copy_size;
46224fa0402SLasse Collin }
46324fa0402SLasse Collin 
46424fa0402SLasse Collin /*****************
46524fa0402SLasse Collin  * Range decoder *
46624fa0402SLasse Collin  *****************/
46724fa0402SLasse Collin 
46824fa0402SLasse Collin /* Reset the range decoder. */
rc_reset(struct rc_dec * rc)46924fa0402SLasse Collin static void rc_reset(struct rc_dec *rc)
47024fa0402SLasse Collin {
47124fa0402SLasse Collin 	rc->range = (uint32_t)-1;
47224fa0402SLasse Collin 	rc->code = 0;
47324fa0402SLasse Collin 	rc->init_bytes_left = RC_INIT_BYTES;
47424fa0402SLasse Collin }
47524fa0402SLasse Collin 
47624fa0402SLasse Collin /*
47724fa0402SLasse Collin  * Read the first five initial bytes into rc->code if they haven't been
47824fa0402SLasse Collin  * read already. (Yes, the first byte gets completely ignored.)
47924fa0402SLasse Collin  */
rc_read_init(struct rc_dec * rc,struct xz_buf * b)48024fa0402SLasse Collin static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
48124fa0402SLasse Collin {
48224fa0402SLasse Collin 	while (rc->init_bytes_left > 0) {
48324fa0402SLasse Collin 		if (b->in_pos == b->in_size)
48424fa0402SLasse Collin 			return false;
48524fa0402SLasse Collin 
48624fa0402SLasse Collin 		rc->code = (rc->code << 8) + b->in[b->in_pos++];
48724fa0402SLasse Collin 		--rc->init_bytes_left;
48824fa0402SLasse Collin 	}
48924fa0402SLasse Collin 
49024fa0402SLasse Collin 	return true;
49124fa0402SLasse Collin }
49224fa0402SLasse Collin 
49324fa0402SLasse Collin /* Return true if there may not be enough input for the next decoding loop. */
rc_limit_exceeded(const struct rc_dec * rc)49424fa0402SLasse Collin static inline bool rc_limit_exceeded(const struct rc_dec *rc)
49524fa0402SLasse Collin {
49624fa0402SLasse Collin 	return rc->in_pos > rc->in_limit;
49724fa0402SLasse Collin }
49824fa0402SLasse Collin 
49924fa0402SLasse Collin /*
50024fa0402SLasse Collin  * Return true if it is possible (from point of view of range decoder) that
50124fa0402SLasse Collin  * we have reached the end of the LZMA chunk.
50224fa0402SLasse Collin  */
rc_is_finished(const struct rc_dec * rc)50324fa0402SLasse Collin static inline bool rc_is_finished(const struct rc_dec *rc)
50424fa0402SLasse Collin {
50524fa0402SLasse Collin 	return rc->code == 0;
50624fa0402SLasse Collin }
50724fa0402SLasse Collin 
50824fa0402SLasse Collin /* Read the next input byte if needed. */
rc_normalize(struct rc_dec * rc)50924fa0402SLasse Collin static __always_inline void rc_normalize(struct rc_dec *rc)
51024fa0402SLasse Collin {
51124fa0402SLasse Collin 	if (rc->range < RC_TOP_VALUE) {
51224fa0402SLasse Collin 		rc->range <<= RC_SHIFT_BITS;
51324fa0402SLasse Collin 		rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
51424fa0402SLasse Collin 	}
51524fa0402SLasse Collin }
51624fa0402SLasse Collin 
51724fa0402SLasse Collin /*
51805911c5dSZhen Lei  * Decode one bit. In some versions, this function has been split in three
51924fa0402SLasse Collin  * functions so that the compiler is supposed to be able to more easily avoid
52024fa0402SLasse Collin  * an extra branch. In this particular version of the LZMA decoder, this
52124fa0402SLasse Collin  * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
5220a434e0aSLasse Collin  * on x86). Using a non-split version results in nicer looking code too.
52324fa0402SLasse Collin  *
52424fa0402SLasse Collin  * NOTE: This must return an int. Do not make it return a bool or the speed
52524fa0402SLasse Collin  * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
52624fa0402SLasse Collin  * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
52724fa0402SLasse Collin  */
rc_bit(struct rc_dec * rc,uint16_t * prob)52824fa0402SLasse Collin static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
52924fa0402SLasse Collin {
53024fa0402SLasse Collin 	uint32_t bound;
53124fa0402SLasse Collin 	int bit;
53224fa0402SLasse Collin 
53324fa0402SLasse Collin 	rc_normalize(rc);
53424fa0402SLasse Collin 	bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
53524fa0402SLasse Collin 	if (rc->code < bound) {
53624fa0402SLasse Collin 		rc->range = bound;
53724fa0402SLasse Collin 		*prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
53824fa0402SLasse Collin 		bit = 0;
53924fa0402SLasse Collin 	} else {
54024fa0402SLasse Collin 		rc->range -= bound;
54124fa0402SLasse Collin 		rc->code -= bound;
54224fa0402SLasse Collin 		*prob -= *prob >> RC_MOVE_BITS;
54324fa0402SLasse Collin 		bit = 1;
54424fa0402SLasse Collin 	}
54524fa0402SLasse Collin 
54624fa0402SLasse Collin 	return bit;
54724fa0402SLasse Collin }
54824fa0402SLasse Collin 
54924fa0402SLasse Collin /* Decode a bittree starting from the most significant bit. */
rc_bittree(struct rc_dec * rc,uint16_t * probs,uint32_t limit)55024fa0402SLasse Collin static __always_inline uint32_t rc_bittree(struct rc_dec *rc,
55124fa0402SLasse Collin 					   uint16_t *probs, uint32_t limit)
55224fa0402SLasse Collin {
55324fa0402SLasse Collin 	uint32_t symbol = 1;
55424fa0402SLasse Collin 
55524fa0402SLasse Collin 	do {
55624fa0402SLasse Collin 		if (rc_bit(rc, &probs[symbol]))
55724fa0402SLasse Collin 			symbol = (symbol << 1) + 1;
55824fa0402SLasse Collin 		else
55924fa0402SLasse Collin 			symbol <<= 1;
56024fa0402SLasse Collin 	} while (symbol < limit);
56124fa0402SLasse Collin 
56224fa0402SLasse Collin 	return symbol;
56324fa0402SLasse Collin }
56424fa0402SLasse Collin 
56524fa0402SLasse Collin /* Decode a bittree starting from the least significant bit. */
rc_bittree_reverse(struct rc_dec * rc,uint16_t * probs,uint32_t * dest,uint32_t limit)56624fa0402SLasse Collin static __always_inline void rc_bittree_reverse(struct rc_dec *rc,
56724fa0402SLasse Collin 					       uint16_t *probs,
56824fa0402SLasse Collin 					       uint32_t *dest, uint32_t limit)
56924fa0402SLasse Collin {
57024fa0402SLasse Collin 	uint32_t symbol = 1;
57124fa0402SLasse Collin 	uint32_t i = 0;
57224fa0402SLasse Collin 
57324fa0402SLasse Collin 	do {
57424fa0402SLasse Collin 		if (rc_bit(rc, &probs[symbol])) {
57524fa0402SLasse Collin 			symbol = (symbol << 1) + 1;
57624fa0402SLasse Collin 			*dest += 1 << i;
57724fa0402SLasse Collin 		} else {
57824fa0402SLasse Collin 			symbol <<= 1;
57924fa0402SLasse Collin 		}
58024fa0402SLasse Collin 	} while (++i < limit);
58124fa0402SLasse Collin }
58224fa0402SLasse Collin 
58324fa0402SLasse Collin /* Decode direct bits (fixed fifty-fifty probability) */
rc_direct(struct rc_dec * rc,uint32_t * dest,uint32_t limit)58424fa0402SLasse Collin static inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
58524fa0402SLasse Collin {
58624fa0402SLasse Collin 	uint32_t mask;
58724fa0402SLasse Collin 
58824fa0402SLasse Collin 	do {
58924fa0402SLasse Collin 		rc_normalize(rc);
59024fa0402SLasse Collin 		rc->range >>= 1;
59124fa0402SLasse Collin 		rc->code -= rc->range;
59224fa0402SLasse Collin 		mask = (uint32_t)0 - (rc->code >> 31);
59324fa0402SLasse Collin 		rc->code += rc->range & mask;
59424fa0402SLasse Collin 		*dest = (*dest << 1) + (mask + 1);
59524fa0402SLasse Collin 	} while (--limit > 0);
59624fa0402SLasse Collin }
59724fa0402SLasse Collin 
59824fa0402SLasse Collin /********
59924fa0402SLasse Collin  * LZMA *
60024fa0402SLasse Collin  ********/
60124fa0402SLasse Collin 
60224fa0402SLasse Collin /* Get pointer to literal coder probability array. */
lzma_literal_probs(struct xz_dec_lzma2 * s)60324fa0402SLasse Collin static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s)
60424fa0402SLasse Collin {
60524fa0402SLasse Collin 	uint32_t prev_byte = dict_get(&s->dict, 0);
60624fa0402SLasse Collin 	uint32_t low = prev_byte >> (8 - s->lzma.lc);
60724fa0402SLasse Collin 	uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
60824fa0402SLasse Collin 	return s->lzma.literal[low + high];
60924fa0402SLasse Collin }
61024fa0402SLasse Collin 
61124fa0402SLasse Collin /* Decode a literal (one 8-bit byte) */
lzma_literal(struct xz_dec_lzma2 * s)61224fa0402SLasse Collin static void lzma_literal(struct xz_dec_lzma2 *s)
61324fa0402SLasse Collin {
61424fa0402SLasse Collin 	uint16_t *probs;
61524fa0402SLasse Collin 	uint32_t symbol;
61624fa0402SLasse Collin 	uint32_t match_byte;
61724fa0402SLasse Collin 	uint32_t match_bit;
61824fa0402SLasse Collin 	uint32_t offset;
61924fa0402SLasse Collin 	uint32_t i;
62024fa0402SLasse Collin 
62124fa0402SLasse Collin 	probs = lzma_literal_probs(s);
62224fa0402SLasse Collin 
62324fa0402SLasse Collin 	if (lzma_state_is_literal(s->lzma.state)) {
62424fa0402SLasse Collin 		symbol = rc_bittree(&s->rc, probs, 0x100);
62524fa0402SLasse Collin 	} else {
62624fa0402SLasse Collin 		symbol = 1;
62724fa0402SLasse Collin 		match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
62824fa0402SLasse Collin 		offset = 0x100;
62924fa0402SLasse Collin 
63024fa0402SLasse Collin 		do {
63124fa0402SLasse Collin 			match_bit = match_byte & offset;
63224fa0402SLasse Collin 			match_byte <<= 1;
63324fa0402SLasse Collin 			i = offset + match_bit + symbol;
63424fa0402SLasse Collin 
63524fa0402SLasse Collin 			if (rc_bit(&s->rc, &probs[i])) {
63624fa0402SLasse Collin 				symbol = (symbol << 1) + 1;
63724fa0402SLasse Collin 				offset &= match_bit;
63824fa0402SLasse Collin 			} else {
63924fa0402SLasse Collin 				symbol <<= 1;
64024fa0402SLasse Collin 				offset &= ~match_bit;
64124fa0402SLasse Collin 			}
64224fa0402SLasse Collin 		} while (symbol < 0x100);
64324fa0402SLasse Collin 	}
64424fa0402SLasse Collin 
64524fa0402SLasse Collin 	dict_put(&s->dict, (uint8_t)symbol);
64624fa0402SLasse Collin 	lzma_state_literal(&s->lzma.state);
64724fa0402SLasse Collin }
64824fa0402SLasse Collin 
64924fa0402SLasse Collin /* Decode the length of the match into s->lzma.len. */
lzma_len(struct xz_dec_lzma2 * s,struct lzma_len_dec * l,uint32_t pos_state)65024fa0402SLasse Collin static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
65124fa0402SLasse Collin 		     uint32_t pos_state)
65224fa0402SLasse Collin {
65324fa0402SLasse Collin 	uint16_t *probs;
65424fa0402SLasse Collin 	uint32_t limit;
65524fa0402SLasse Collin 
65624fa0402SLasse Collin 	if (!rc_bit(&s->rc, &l->choice)) {
65724fa0402SLasse Collin 		probs = l->low[pos_state];
65824fa0402SLasse Collin 		limit = LEN_LOW_SYMBOLS;
65924fa0402SLasse Collin 		s->lzma.len = MATCH_LEN_MIN;
66024fa0402SLasse Collin 	} else {
66124fa0402SLasse Collin 		if (!rc_bit(&s->rc, &l->choice2)) {
66224fa0402SLasse Collin 			probs = l->mid[pos_state];
66324fa0402SLasse Collin 			limit = LEN_MID_SYMBOLS;
66424fa0402SLasse Collin 			s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
66524fa0402SLasse Collin 		} else {
66624fa0402SLasse Collin 			probs = l->high;
66724fa0402SLasse Collin 			limit = LEN_HIGH_SYMBOLS;
66824fa0402SLasse Collin 			s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
66924fa0402SLasse Collin 					+ LEN_MID_SYMBOLS;
67024fa0402SLasse Collin 		}
67124fa0402SLasse Collin 	}
67224fa0402SLasse Collin 
67324fa0402SLasse Collin 	s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
67424fa0402SLasse Collin }
67524fa0402SLasse Collin 
67624fa0402SLasse Collin /* Decode a match. The distance will be stored in s->lzma.rep0. */
lzma_match(struct xz_dec_lzma2 * s,uint32_t pos_state)67724fa0402SLasse Collin static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
67824fa0402SLasse Collin {
67924fa0402SLasse Collin 	uint16_t *probs;
68024fa0402SLasse Collin 	uint32_t dist_slot;
68124fa0402SLasse Collin 	uint32_t limit;
68224fa0402SLasse Collin 
68324fa0402SLasse Collin 	lzma_state_match(&s->lzma.state);
68424fa0402SLasse Collin 
68524fa0402SLasse Collin 	s->lzma.rep3 = s->lzma.rep2;
68624fa0402SLasse Collin 	s->lzma.rep2 = s->lzma.rep1;
68724fa0402SLasse Collin 	s->lzma.rep1 = s->lzma.rep0;
68824fa0402SLasse Collin 
68924fa0402SLasse Collin 	lzma_len(s, &s->lzma.match_len_dec, pos_state);
69024fa0402SLasse Collin 
69124fa0402SLasse Collin 	probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
69224fa0402SLasse Collin 	dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
69324fa0402SLasse Collin 
69424fa0402SLasse Collin 	if (dist_slot < DIST_MODEL_START) {
69524fa0402SLasse Collin 		s->lzma.rep0 = dist_slot;
69624fa0402SLasse Collin 	} else {
69724fa0402SLasse Collin 		limit = (dist_slot >> 1) - 1;
69824fa0402SLasse Collin 		s->lzma.rep0 = 2 + (dist_slot & 1);
69924fa0402SLasse Collin 
70024fa0402SLasse Collin 		if (dist_slot < DIST_MODEL_END) {
70124fa0402SLasse Collin 			s->lzma.rep0 <<= limit;
70224fa0402SLasse Collin 			probs = s->lzma.dist_special + s->lzma.rep0
70324fa0402SLasse Collin 					- dist_slot - 1;
70424fa0402SLasse Collin 			rc_bittree_reverse(&s->rc, probs,
70524fa0402SLasse Collin 					&s->lzma.rep0, limit);
70624fa0402SLasse Collin 		} else {
70724fa0402SLasse Collin 			rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
70824fa0402SLasse Collin 			s->lzma.rep0 <<= ALIGN_BITS;
70924fa0402SLasse Collin 			rc_bittree_reverse(&s->rc, s->lzma.dist_align,
71024fa0402SLasse Collin 					&s->lzma.rep0, ALIGN_BITS);
71124fa0402SLasse Collin 		}
71224fa0402SLasse Collin 	}
71324fa0402SLasse Collin }
71424fa0402SLasse Collin 
71524fa0402SLasse Collin /*
71624fa0402SLasse Collin  * Decode a repeated match. The distance is one of the four most recently
71724fa0402SLasse Collin  * seen matches. The distance will be stored in s->lzma.rep0.
71824fa0402SLasse Collin  */
lzma_rep_match(struct xz_dec_lzma2 * s,uint32_t pos_state)71924fa0402SLasse Collin static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
72024fa0402SLasse Collin {
72124fa0402SLasse Collin 	uint32_t tmp;
72224fa0402SLasse Collin 
72324fa0402SLasse Collin 	if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
72424fa0402SLasse Collin 		if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
72524fa0402SLasse Collin 				s->lzma.state][pos_state])) {
72624fa0402SLasse Collin 			lzma_state_short_rep(&s->lzma.state);
72724fa0402SLasse Collin 			s->lzma.len = 1;
72824fa0402SLasse Collin 			return;
72924fa0402SLasse Collin 		}
73024fa0402SLasse Collin 	} else {
73124fa0402SLasse Collin 		if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
73224fa0402SLasse Collin 			tmp = s->lzma.rep1;
73324fa0402SLasse Collin 		} else {
73424fa0402SLasse Collin 			if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
73524fa0402SLasse Collin 				tmp = s->lzma.rep2;
73624fa0402SLasse Collin 			} else {
73724fa0402SLasse Collin 				tmp = s->lzma.rep3;
73824fa0402SLasse Collin 				s->lzma.rep3 = s->lzma.rep2;
73924fa0402SLasse Collin 			}
74024fa0402SLasse Collin 
74124fa0402SLasse Collin 			s->lzma.rep2 = s->lzma.rep1;
74224fa0402SLasse Collin 		}
74324fa0402SLasse Collin 
74424fa0402SLasse Collin 		s->lzma.rep1 = s->lzma.rep0;
74524fa0402SLasse Collin 		s->lzma.rep0 = tmp;
74624fa0402SLasse Collin 	}
74724fa0402SLasse Collin 
74824fa0402SLasse Collin 	lzma_state_long_rep(&s->lzma.state);
74924fa0402SLasse Collin 	lzma_len(s, &s->lzma.rep_len_dec, pos_state);
75024fa0402SLasse Collin }
75124fa0402SLasse Collin 
75224fa0402SLasse Collin /* LZMA decoder core */
lzma_main(struct xz_dec_lzma2 * s)75324fa0402SLasse Collin static bool lzma_main(struct xz_dec_lzma2 *s)
75424fa0402SLasse Collin {
75524fa0402SLasse Collin 	uint32_t pos_state;
75624fa0402SLasse Collin 
75724fa0402SLasse Collin 	/*
75824fa0402SLasse Collin 	 * If the dictionary was reached during the previous call, try to
75924fa0402SLasse Collin 	 * finish the possibly pending repeat in the dictionary.
76024fa0402SLasse Collin 	 */
76124fa0402SLasse Collin 	if (dict_has_space(&s->dict) && s->lzma.len > 0)
76224fa0402SLasse Collin 		dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
76324fa0402SLasse Collin 
76424fa0402SLasse Collin 	/*
76524fa0402SLasse Collin 	 * Decode more LZMA symbols. One iteration may consume up to
76624fa0402SLasse Collin 	 * LZMA_IN_REQUIRED - 1 bytes.
76724fa0402SLasse Collin 	 */
76824fa0402SLasse Collin 	while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
76924fa0402SLasse Collin 		pos_state = s->dict.pos & s->lzma.pos_mask;
77024fa0402SLasse Collin 
77124fa0402SLasse Collin 		if (!rc_bit(&s->rc, &s->lzma.is_match[
77224fa0402SLasse Collin 				s->lzma.state][pos_state])) {
77324fa0402SLasse Collin 			lzma_literal(s);
77424fa0402SLasse Collin 		} else {
77524fa0402SLasse Collin 			if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
77624fa0402SLasse Collin 				lzma_rep_match(s, pos_state);
77724fa0402SLasse Collin 			else
77824fa0402SLasse Collin 				lzma_match(s, pos_state);
77924fa0402SLasse Collin 
78024fa0402SLasse Collin 			if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
78124fa0402SLasse Collin 				return false;
78224fa0402SLasse Collin 		}
78324fa0402SLasse Collin 	}
78424fa0402SLasse Collin 
78524fa0402SLasse Collin 	/*
78624fa0402SLasse Collin 	 * Having the range decoder always normalized when we are outside
78724fa0402SLasse Collin 	 * this function makes it easier to correctly handle end of the chunk.
78824fa0402SLasse Collin 	 */
78924fa0402SLasse Collin 	rc_normalize(&s->rc);
79024fa0402SLasse Collin 
79124fa0402SLasse Collin 	return true;
79224fa0402SLasse Collin }
79324fa0402SLasse Collin 
79424fa0402SLasse Collin /*
79505911c5dSZhen Lei  * Reset the LZMA decoder and range decoder state. Dictionary is not reset
79624fa0402SLasse Collin  * here, because LZMA state may be reset without resetting the dictionary.
79724fa0402SLasse Collin  */
lzma_reset(struct xz_dec_lzma2 * s)79824fa0402SLasse Collin static void lzma_reset(struct xz_dec_lzma2 *s)
79924fa0402SLasse Collin {
80024fa0402SLasse Collin 	uint16_t *probs;
80124fa0402SLasse Collin 	size_t i;
80224fa0402SLasse Collin 
80324fa0402SLasse Collin 	s->lzma.state = STATE_LIT_LIT;
80424fa0402SLasse Collin 	s->lzma.rep0 = 0;
80524fa0402SLasse Collin 	s->lzma.rep1 = 0;
80624fa0402SLasse Collin 	s->lzma.rep2 = 0;
80724fa0402SLasse Collin 	s->lzma.rep3 = 0;
808a98a2540SLasse Collin 	s->lzma.len = 0;
80924fa0402SLasse Collin 
81024fa0402SLasse Collin 	/*
81124fa0402SLasse Collin 	 * All probabilities are initialized to the same value. This hack
81224fa0402SLasse Collin 	 * makes the code smaller by avoiding a separate loop for each
81324fa0402SLasse Collin 	 * probability array.
81424fa0402SLasse Collin 	 *
81524fa0402SLasse Collin 	 * This could be optimized so that only that part of literal
81624fa0402SLasse Collin 	 * probabilities that are actually required. In the common case
81724fa0402SLasse Collin 	 * we would write 12 KiB less.
81824fa0402SLasse Collin 	 */
81924fa0402SLasse Collin 	probs = s->lzma.is_match[0];
82024fa0402SLasse Collin 	for (i = 0; i < PROBS_TOTAL; ++i)
82124fa0402SLasse Collin 		probs[i] = RC_BIT_MODEL_TOTAL / 2;
82224fa0402SLasse Collin 
82324fa0402SLasse Collin 	rc_reset(&s->rc);
82424fa0402SLasse Collin }
82524fa0402SLasse Collin 
82624fa0402SLasse Collin /*
82724fa0402SLasse Collin  * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
82824fa0402SLasse Collin  * from the decoded lp and pb values. On success, the LZMA decoder state is
82924fa0402SLasse Collin  * reset and true is returned.
83024fa0402SLasse Collin  */
lzma_props(struct xz_dec_lzma2 * s,uint8_t props)83124fa0402SLasse Collin static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
83224fa0402SLasse Collin {
83324fa0402SLasse Collin 	if (props > (4 * 5 + 4) * 9 + 8)
83424fa0402SLasse Collin 		return false;
83524fa0402SLasse Collin 
83624fa0402SLasse Collin 	s->lzma.pos_mask = 0;
83724fa0402SLasse Collin 	while (props >= 9 * 5) {
83824fa0402SLasse Collin 		props -= 9 * 5;
83924fa0402SLasse Collin 		++s->lzma.pos_mask;
84024fa0402SLasse Collin 	}
84124fa0402SLasse Collin 
84224fa0402SLasse Collin 	s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
84324fa0402SLasse Collin 
84424fa0402SLasse Collin 	s->lzma.literal_pos_mask = 0;
84524fa0402SLasse Collin 	while (props >= 9) {
84624fa0402SLasse Collin 		props -= 9;
84724fa0402SLasse Collin 		++s->lzma.literal_pos_mask;
84824fa0402SLasse Collin 	}
84924fa0402SLasse Collin 
85024fa0402SLasse Collin 	s->lzma.lc = props;
85124fa0402SLasse Collin 
85224fa0402SLasse Collin 	if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
85324fa0402SLasse Collin 		return false;
85424fa0402SLasse Collin 
85524fa0402SLasse Collin 	s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
85624fa0402SLasse Collin 
85724fa0402SLasse Collin 	lzma_reset(s);
85824fa0402SLasse Collin 
85924fa0402SLasse Collin 	return true;
86024fa0402SLasse Collin }
86124fa0402SLasse Collin 
86224fa0402SLasse Collin /*********
86324fa0402SLasse Collin  * LZMA2 *
86424fa0402SLasse Collin  *********/
86524fa0402SLasse Collin 
86624fa0402SLasse Collin /*
86724fa0402SLasse Collin  * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
86824fa0402SLasse Collin  * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
86924fa0402SLasse Collin  * wrapper function takes care of making the LZMA decoder's assumption safe.
87024fa0402SLasse Collin  *
87124fa0402SLasse Collin  * As long as there is plenty of input left to be decoded in the current LZMA
87224fa0402SLasse Collin  * chunk, we decode directly from the caller-supplied input buffer until
87324fa0402SLasse Collin  * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
87424fa0402SLasse Collin  * s->temp.buf, which (hopefully) gets filled on the next call to this
87524fa0402SLasse Collin  * function. We decode a few bytes from the temporary buffer so that we can
87624fa0402SLasse Collin  * continue decoding from the caller-supplied input buffer again.
87724fa0402SLasse Collin  */
lzma2_lzma(struct xz_dec_lzma2 * s,struct xz_buf * b)87824fa0402SLasse Collin static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
87924fa0402SLasse Collin {
88024fa0402SLasse Collin 	size_t in_avail;
88124fa0402SLasse Collin 	uint32_t tmp;
88224fa0402SLasse Collin 
88324fa0402SLasse Collin 	in_avail = b->in_size - b->in_pos;
88424fa0402SLasse Collin 	if (s->temp.size > 0 || s->lzma2.compressed == 0) {
88524fa0402SLasse Collin 		tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
88624fa0402SLasse Collin 		if (tmp > s->lzma2.compressed - s->temp.size)
88724fa0402SLasse Collin 			tmp = s->lzma2.compressed - s->temp.size;
88824fa0402SLasse Collin 		if (tmp > in_avail)
88924fa0402SLasse Collin 			tmp = in_avail;
89024fa0402SLasse Collin 
89124fa0402SLasse Collin 		memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
89224fa0402SLasse Collin 
89324fa0402SLasse Collin 		if (s->temp.size + tmp == s->lzma2.compressed) {
89424fa0402SLasse Collin 			memzero(s->temp.buf + s->temp.size + tmp,
89524fa0402SLasse Collin 					sizeof(s->temp.buf)
89624fa0402SLasse Collin 						- s->temp.size - tmp);
89724fa0402SLasse Collin 			s->rc.in_limit = s->temp.size + tmp;
89824fa0402SLasse Collin 		} else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
89924fa0402SLasse Collin 			s->temp.size += tmp;
90024fa0402SLasse Collin 			b->in_pos += tmp;
90124fa0402SLasse Collin 			return true;
90224fa0402SLasse Collin 		} else {
90324fa0402SLasse Collin 			s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
90424fa0402SLasse Collin 		}
90524fa0402SLasse Collin 
90624fa0402SLasse Collin 		s->rc.in = s->temp.buf;
90724fa0402SLasse Collin 		s->rc.in_pos = 0;
90824fa0402SLasse Collin 
90924fa0402SLasse Collin 		if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
91024fa0402SLasse Collin 			return false;
91124fa0402SLasse Collin 
91224fa0402SLasse Collin 		s->lzma2.compressed -= s->rc.in_pos;
91324fa0402SLasse Collin 
91424fa0402SLasse Collin 		if (s->rc.in_pos < s->temp.size) {
91524fa0402SLasse Collin 			s->temp.size -= s->rc.in_pos;
91624fa0402SLasse Collin 			memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
91724fa0402SLasse Collin 					s->temp.size);
91824fa0402SLasse Collin 			return true;
91924fa0402SLasse Collin 		}
92024fa0402SLasse Collin 
92124fa0402SLasse Collin 		b->in_pos += s->rc.in_pos - s->temp.size;
92224fa0402SLasse Collin 		s->temp.size = 0;
92324fa0402SLasse Collin 	}
92424fa0402SLasse Collin 
92524fa0402SLasse Collin 	in_avail = b->in_size - b->in_pos;
92624fa0402SLasse Collin 	if (in_avail >= LZMA_IN_REQUIRED) {
92724fa0402SLasse Collin 		s->rc.in = b->in;
92824fa0402SLasse Collin 		s->rc.in_pos = b->in_pos;
92924fa0402SLasse Collin 
93024fa0402SLasse Collin 		if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
93124fa0402SLasse Collin 			s->rc.in_limit = b->in_pos + s->lzma2.compressed;
93224fa0402SLasse Collin 		else
93324fa0402SLasse Collin 			s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
93424fa0402SLasse Collin 
93524fa0402SLasse Collin 		if (!lzma_main(s))
93624fa0402SLasse Collin 			return false;
93724fa0402SLasse Collin 
93824fa0402SLasse Collin 		in_avail = s->rc.in_pos - b->in_pos;
93924fa0402SLasse Collin 		if (in_avail > s->lzma2.compressed)
94024fa0402SLasse Collin 			return false;
94124fa0402SLasse Collin 
94224fa0402SLasse Collin 		s->lzma2.compressed -= in_avail;
94324fa0402SLasse Collin 		b->in_pos = s->rc.in_pos;
94424fa0402SLasse Collin 	}
94524fa0402SLasse Collin 
94624fa0402SLasse Collin 	in_avail = b->in_size - b->in_pos;
94724fa0402SLasse Collin 	if (in_avail < LZMA_IN_REQUIRED) {
94824fa0402SLasse Collin 		if (in_avail > s->lzma2.compressed)
94924fa0402SLasse Collin 			in_avail = s->lzma2.compressed;
95024fa0402SLasse Collin 
95124fa0402SLasse Collin 		memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
95224fa0402SLasse Collin 		s->temp.size = in_avail;
95324fa0402SLasse Collin 		b->in_pos += in_avail;
95424fa0402SLasse Collin 	}
95524fa0402SLasse Collin 
95624fa0402SLasse Collin 	return true;
95724fa0402SLasse Collin }
95824fa0402SLasse Collin 
95924fa0402SLasse Collin /*
96024fa0402SLasse Collin  * Take care of the LZMA2 control layer, and forward the job of actual LZMA
96124fa0402SLasse Collin  * decoding or copying of uncompressed chunks to other functions.
96224fa0402SLasse Collin  */
xz_dec_lzma2_run(struct xz_dec_lzma2 * s,struct xz_buf * b)963*c6f371baSLasse Collin enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
96424fa0402SLasse Collin {
96524fa0402SLasse Collin 	uint32_t tmp;
96624fa0402SLasse Collin 
96724fa0402SLasse Collin 	while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
96824fa0402SLasse Collin 		switch (s->lzma2.sequence) {
96924fa0402SLasse Collin 		case SEQ_CONTROL:
97024fa0402SLasse Collin 			/*
97124fa0402SLasse Collin 			 * LZMA2 control byte
97224fa0402SLasse Collin 			 *
97324fa0402SLasse Collin 			 * Exact values:
97424fa0402SLasse Collin 			 *   0x00   End marker
97524fa0402SLasse Collin 			 *   0x01   Dictionary reset followed by
97624fa0402SLasse Collin 			 *          an uncompressed chunk
97724fa0402SLasse Collin 			 *   0x02   Uncompressed chunk (no dictionary reset)
97824fa0402SLasse Collin 			 *
97924fa0402SLasse Collin 			 * Highest three bits (s->control & 0xE0):
98024fa0402SLasse Collin 			 *   0xE0   Dictionary reset, new properties and state
98124fa0402SLasse Collin 			 *          reset, followed by LZMA compressed chunk
98224fa0402SLasse Collin 			 *   0xC0   New properties and state reset, followed
98324fa0402SLasse Collin 			 *          by LZMA compressed chunk (no dictionary
98424fa0402SLasse Collin 			 *          reset)
98524fa0402SLasse Collin 			 *   0xA0   State reset using old properties,
98624fa0402SLasse Collin 			 *          followed by LZMA compressed chunk (no
98724fa0402SLasse Collin 			 *          dictionary reset)
98824fa0402SLasse Collin 			 *   0x80   LZMA chunk (no dictionary or state reset)
98924fa0402SLasse Collin 			 *
99024fa0402SLasse Collin 			 * For LZMA compressed chunks, the lowest five bits
99124fa0402SLasse Collin 			 * (s->control & 1F) are the highest bits of the
99224fa0402SLasse Collin 			 * uncompressed size (bits 16-20).
99324fa0402SLasse Collin 			 *
99424fa0402SLasse Collin 			 * A new LZMA2 stream must begin with a dictionary
99524fa0402SLasse Collin 			 * reset. The first LZMA chunk must set new
99624fa0402SLasse Collin 			 * properties and reset the LZMA state.
99724fa0402SLasse Collin 			 *
99824fa0402SLasse Collin 			 * Values that don't match anything described above
99924fa0402SLasse Collin 			 * are invalid and we return XZ_DATA_ERROR.
100024fa0402SLasse Collin 			 */
100124fa0402SLasse Collin 			tmp = b->in[b->in_pos++];
100224fa0402SLasse Collin 
1003646032e3SLasse Collin 			if (tmp == 0x00)
1004646032e3SLasse Collin 				return XZ_STREAM_END;
1005646032e3SLasse Collin 
100624fa0402SLasse Collin 			if (tmp >= 0xE0 || tmp == 0x01) {
100724fa0402SLasse Collin 				s->lzma2.need_props = true;
100824fa0402SLasse Collin 				s->lzma2.need_dict_reset = false;
100924fa0402SLasse Collin 				dict_reset(&s->dict, b);
101024fa0402SLasse Collin 			} else if (s->lzma2.need_dict_reset) {
101124fa0402SLasse Collin 				return XZ_DATA_ERROR;
101224fa0402SLasse Collin 			}
101324fa0402SLasse Collin 
101424fa0402SLasse Collin 			if (tmp >= 0x80) {
101524fa0402SLasse Collin 				s->lzma2.uncompressed = (tmp & 0x1F) << 16;
101624fa0402SLasse Collin 				s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
101724fa0402SLasse Collin 
101824fa0402SLasse Collin 				if (tmp >= 0xC0) {
101924fa0402SLasse Collin 					/*
102024fa0402SLasse Collin 					 * When there are new properties,
102124fa0402SLasse Collin 					 * state reset is done at
102224fa0402SLasse Collin 					 * SEQ_PROPERTIES.
102324fa0402SLasse Collin 					 */
102424fa0402SLasse Collin 					s->lzma2.need_props = false;
102524fa0402SLasse Collin 					s->lzma2.next_sequence
102624fa0402SLasse Collin 							= SEQ_PROPERTIES;
102724fa0402SLasse Collin 
102824fa0402SLasse Collin 				} else if (s->lzma2.need_props) {
102924fa0402SLasse Collin 					return XZ_DATA_ERROR;
103024fa0402SLasse Collin 
103124fa0402SLasse Collin 				} else {
103224fa0402SLasse Collin 					s->lzma2.next_sequence
103324fa0402SLasse Collin 							= SEQ_LZMA_PREPARE;
103424fa0402SLasse Collin 					if (tmp >= 0xA0)
103524fa0402SLasse Collin 						lzma_reset(s);
103624fa0402SLasse Collin 				}
103724fa0402SLasse Collin 			} else {
103824fa0402SLasse Collin 				if (tmp > 0x02)
103924fa0402SLasse Collin 					return XZ_DATA_ERROR;
104024fa0402SLasse Collin 
104124fa0402SLasse Collin 				s->lzma2.sequence = SEQ_COMPRESSED_0;
104224fa0402SLasse Collin 				s->lzma2.next_sequence = SEQ_COPY;
104324fa0402SLasse Collin 			}
104424fa0402SLasse Collin 
104524fa0402SLasse Collin 			break;
104624fa0402SLasse Collin 
104724fa0402SLasse Collin 		case SEQ_UNCOMPRESSED_1:
104824fa0402SLasse Collin 			s->lzma2.uncompressed
104924fa0402SLasse Collin 					+= (uint32_t)b->in[b->in_pos++] << 8;
105024fa0402SLasse Collin 			s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
105124fa0402SLasse Collin 			break;
105224fa0402SLasse Collin 
105324fa0402SLasse Collin 		case SEQ_UNCOMPRESSED_2:
105424fa0402SLasse Collin 			s->lzma2.uncompressed
105524fa0402SLasse Collin 					+= (uint32_t)b->in[b->in_pos++] + 1;
105624fa0402SLasse Collin 			s->lzma2.sequence = SEQ_COMPRESSED_0;
105724fa0402SLasse Collin 			break;
105824fa0402SLasse Collin 
105924fa0402SLasse Collin 		case SEQ_COMPRESSED_0:
106024fa0402SLasse Collin 			s->lzma2.compressed
106124fa0402SLasse Collin 					= (uint32_t)b->in[b->in_pos++] << 8;
106224fa0402SLasse Collin 			s->lzma2.sequence = SEQ_COMPRESSED_1;
106324fa0402SLasse Collin 			break;
106424fa0402SLasse Collin 
106524fa0402SLasse Collin 		case SEQ_COMPRESSED_1:
106624fa0402SLasse Collin 			s->lzma2.compressed
106724fa0402SLasse Collin 					+= (uint32_t)b->in[b->in_pos++] + 1;
106824fa0402SLasse Collin 			s->lzma2.sequence = s->lzma2.next_sequence;
106924fa0402SLasse Collin 			break;
107024fa0402SLasse Collin 
107124fa0402SLasse Collin 		case SEQ_PROPERTIES:
107224fa0402SLasse Collin 			if (!lzma_props(s, b->in[b->in_pos++]))
107324fa0402SLasse Collin 				return XZ_DATA_ERROR;
107424fa0402SLasse Collin 
107524fa0402SLasse Collin 			s->lzma2.sequence = SEQ_LZMA_PREPARE;
107624fa0402SLasse Collin 
10774c1ca831SNick Desaulniers 			fallthrough;
107884d517f3SLasse Collin 
107924fa0402SLasse Collin 		case SEQ_LZMA_PREPARE:
108024fa0402SLasse Collin 			if (s->lzma2.compressed < RC_INIT_BYTES)
108124fa0402SLasse Collin 				return XZ_DATA_ERROR;
108224fa0402SLasse Collin 
108324fa0402SLasse Collin 			if (!rc_read_init(&s->rc, b))
108424fa0402SLasse Collin 				return XZ_OK;
108524fa0402SLasse Collin 
108624fa0402SLasse Collin 			s->lzma2.compressed -= RC_INIT_BYTES;
108724fa0402SLasse Collin 			s->lzma2.sequence = SEQ_LZMA_RUN;
108824fa0402SLasse Collin 
10894c1ca831SNick Desaulniers 			fallthrough;
109084d517f3SLasse Collin 
109124fa0402SLasse Collin 		case SEQ_LZMA_RUN:
109224fa0402SLasse Collin 			/*
109324fa0402SLasse Collin 			 * Set dictionary limit to indicate how much we want
109424fa0402SLasse Collin 			 * to be encoded at maximum. Decode new data into the
109524fa0402SLasse Collin 			 * dictionary. Flush the new data from dictionary to
109624fa0402SLasse Collin 			 * b->out. Check if we finished decoding this chunk.
109724fa0402SLasse Collin 			 * In case the dictionary got full but we didn't fill
109824fa0402SLasse Collin 			 * the output buffer yet, we may run this loop
109924fa0402SLasse Collin 			 * multiple times without changing s->lzma2.sequence.
110024fa0402SLasse Collin 			 */
110124fa0402SLasse Collin 			dict_limit(&s->dict, min_t(size_t,
110224fa0402SLasse Collin 					b->out_size - b->out_pos,
110324fa0402SLasse Collin 					s->lzma2.uncompressed));
110424fa0402SLasse Collin 			if (!lzma2_lzma(s, b))
110524fa0402SLasse Collin 				return XZ_DATA_ERROR;
110624fa0402SLasse Collin 
110724fa0402SLasse Collin 			s->lzma2.uncompressed -= dict_flush(&s->dict, b);
110824fa0402SLasse Collin 
110924fa0402SLasse Collin 			if (s->lzma2.uncompressed == 0) {
111024fa0402SLasse Collin 				if (s->lzma2.compressed > 0 || s->lzma.len > 0
111124fa0402SLasse Collin 						|| !rc_is_finished(&s->rc))
111224fa0402SLasse Collin 					return XZ_DATA_ERROR;
111324fa0402SLasse Collin 
111424fa0402SLasse Collin 				rc_reset(&s->rc);
111524fa0402SLasse Collin 				s->lzma2.sequence = SEQ_CONTROL;
111624fa0402SLasse Collin 
111724fa0402SLasse Collin 			} else if (b->out_pos == b->out_size
111824fa0402SLasse Collin 					|| (b->in_pos == b->in_size
111924fa0402SLasse Collin 						&& s->temp.size
112024fa0402SLasse Collin 						< s->lzma2.compressed)) {
112124fa0402SLasse Collin 				return XZ_OK;
112224fa0402SLasse Collin 			}
112324fa0402SLasse Collin 
112424fa0402SLasse Collin 			break;
112524fa0402SLasse Collin 
112624fa0402SLasse Collin 		case SEQ_COPY:
112724fa0402SLasse Collin 			dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
112824fa0402SLasse Collin 			if (s->lzma2.compressed > 0)
112924fa0402SLasse Collin 				return XZ_OK;
113024fa0402SLasse Collin 
113124fa0402SLasse Collin 			s->lzma2.sequence = SEQ_CONTROL;
113224fa0402SLasse Collin 			break;
113324fa0402SLasse Collin 		}
113424fa0402SLasse Collin 	}
113524fa0402SLasse Collin 
113624fa0402SLasse Collin 	return XZ_OK;
113724fa0402SLasse Collin }
113824fa0402SLasse Collin 
xz_dec_lzma2_create(enum xz_mode mode,uint32_t dict_max)1139*c6f371baSLasse Collin struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, uint32_t dict_max)
114024fa0402SLasse Collin {
114124fa0402SLasse Collin 	struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL);
114224fa0402SLasse Collin 	if (s == NULL)
114324fa0402SLasse Collin 		return NULL;
114424fa0402SLasse Collin 
114524fa0402SLasse Collin 	s->dict.mode = mode;
114624fa0402SLasse Collin 	s->dict.size_max = dict_max;
114724fa0402SLasse Collin 
114824fa0402SLasse Collin 	if (DEC_IS_PREALLOC(mode)) {
114924fa0402SLasse Collin 		s->dict.buf = vmalloc(dict_max);
115024fa0402SLasse Collin 		if (s->dict.buf == NULL) {
115124fa0402SLasse Collin 			kfree(s);
115224fa0402SLasse Collin 			return NULL;
115324fa0402SLasse Collin 		}
115424fa0402SLasse Collin 	} else if (DEC_IS_DYNALLOC(mode)) {
115524fa0402SLasse Collin 		s->dict.buf = NULL;
115624fa0402SLasse Collin 		s->dict.allocated = 0;
115724fa0402SLasse Collin 	}
115824fa0402SLasse Collin 
115924fa0402SLasse Collin 	return s;
116024fa0402SLasse Collin }
116124fa0402SLasse Collin 
xz_dec_lzma2_reset(struct xz_dec_lzma2 * s,uint8_t props)1162*c6f371baSLasse Collin enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
116324fa0402SLasse Collin {
116424fa0402SLasse Collin 	/* This limits dictionary size to 3 GiB to keep parsing simpler. */
116524fa0402SLasse Collin 	if (props > 39)
116624fa0402SLasse Collin 		return XZ_OPTIONS_ERROR;
116724fa0402SLasse Collin 
116824fa0402SLasse Collin 	s->dict.size = 2 + (props & 1);
116924fa0402SLasse Collin 	s->dict.size <<= (props >> 1) + 11;
117024fa0402SLasse Collin 
117124fa0402SLasse Collin 	if (DEC_IS_MULTI(s->dict.mode)) {
117224fa0402SLasse Collin 		if (s->dict.size > s->dict.size_max)
117324fa0402SLasse Collin 			return XZ_MEMLIMIT_ERROR;
117424fa0402SLasse Collin 
117524fa0402SLasse Collin 		s->dict.end = s->dict.size;
117624fa0402SLasse Collin 
117724fa0402SLasse Collin 		if (DEC_IS_DYNALLOC(s->dict.mode)) {
117824fa0402SLasse Collin 			if (s->dict.allocated < s->dict.size) {
11798e20ba2eSLasse Collin 				s->dict.allocated = s->dict.size;
118024fa0402SLasse Collin 				vfree(s->dict.buf);
118124fa0402SLasse Collin 				s->dict.buf = vmalloc(s->dict.size);
118224fa0402SLasse Collin 				if (s->dict.buf == NULL) {
118324fa0402SLasse Collin 					s->dict.allocated = 0;
118424fa0402SLasse Collin 					return XZ_MEM_ERROR;
118524fa0402SLasse Collin 				}
118624fa0402SLasse Collin 			}
118724fa0402SLasse Collin 		}
118824fa0402SLasse Collin 	}
118924fa0402SLasse Collin 
119024fa0402SLasse Collin 	s->lzma2.sequence = SEQ_CONTROL;
119124fa0402SLasse Collin 	s->lzma2.need_dict_reset = true;
119224fa0402SLasse Collin 
119324fa0402SLasse Collin 	s->temp.size = 0;
119424fa0402SLasse Collin 
119524fa0402SLasse Collin 	return XZ_OK;
119624fa0402SLasse Collin }
119724fa0402SLasse Collin 
xz_dec_lzma2_end(struct xz_dec_lzma2 * s)1198*c6f371baSLasse Collin void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
119924fa0402SLasse Collin {
120024fa0402SLasse Collin 	if (DEC_IS_MULTI(s->dict.mode))
120124fa0402SLasse Collin 		vfree(s->dict.buf);
120224fa0402SLasse Collin 
120324fa0402SLasse Collin 	kfree(s);
120424fa0402SLasse Collin }
1205aaa2975fSLasse Collin 
1206aaa2975fSLasse Collin #ifdef XZ_DEC_MICROLZMA
1207aaa2975fSLasse Collin /* This is a wrapper struct to have a nice struct name in the public API. */
1208aaa2975fSLasse Collin struct xz_dec_microlzma {
1209aaa2975fSLasse Collin 	struct xz_dec_lzma2 s;
1210aaa2975fSLasse Collin };
1211aaa2975fSLasse Collin 
xz_dec_microlzma_run(struct xz_dec_microlzma * s_ptr,struct xz_buf * b)1212aaa2975fSLasse Collin enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s_ptr,
1213aaa2975fSLasse Collin 				 struct xz_buf *b)
1214aaa2975fSLasse Collin {
1215aaa2975fSLasse Collin 	struct xz_dec_lzma2 *s = &s_ptr->s;
1216aaa2975fSLasse Collin 
1217aaa2975fSLasse Collin 	/*
1218aaa2975fSLasse Collin 	 * sequence is SEQ_PROPERTIES before the first input byte,
1219aaa2975fSLasse Collin 	 * SEQ_LZMA_PREPARE until a total of five bytes have been read,
1220aaa2975fSLasse Collin 	 * and SEQ_LZMA_RUN for the rest of the input stream.
1221aaa2975fSLasse Collin 	 */
1222aaa2975fSLasse Collin 	if (s->lzma2.sequence != SEQ_LZMA_RUN) {
1223aaa2975fSLasse Collin 		if (s->lzma2.sequence == SEQ_PROPERTIES) {
1224aaa2975fSLasse Collin 			/* One byte is needed for the props. */
1225aaa2975fSLasse Collin 			if (b->in_pos >= b->in_size)
1226aaa2975fSLasse Collin 				return XZ_OK;
1227aaa2975fSLasse Collin 
1228aaa2975fSLasse Collin 			/*
1229aaa2975fSLasse Collin 			 * Don't increment b->in_pos here. The same byte is
1230aaa2975fSLasse Collin 			 * also passed to rc_read_init() which will ignore it.
1231aaa2975fSLasse Collin 			 */
1232aaa2975fSLasse Collin 			if (!lzma_props(s, ~b->in[b->in_pos]))
1233aaa2975fSLasse Collin 				return XZ_DATA_ERROR;
1234aaa2975fSLasse Collin 
1235aaa2975fSLasse Collin 			s->lzma2.sequence = SEQ_LZMA_PREPARE;
1236aaa2975fSLasse Collin 		}
1237aaa2975fSLasse Collin 
1238aaa2975fSLasse Collin 		/*
1239aaa2975fSLasse Collin 		 * xz_dec_microlzma_reset() doesn't validate the compressed
1240aaa2975fSLasse Collin 		 * size so we do it here. We have to limit the maximum size
1241aaa2975fSLasse Collin 		 * to avoid integer overflows in lzma2_lzma(). 3 GiB is a nice
1242aaa2975fSLasse Collin 		 * round number and much more than users of this code should
1243aaa2975fSLasse Collin 		 * ever need.
1244aaa2975fSLasse Collin 		 */
1245aaa2975fSLasse Collin 		if (s->lzma2.compressed < RC_INIT_BYTES
1246aaa2975fSLasse Collin 				|| s->lzma2.compressed > (3U << 30))
1247aaa2975fSLasse Collin 			return XZ_DATA_ERROR;
1248aaa2975fSLasse Collin 
1249aaa2975fSLasse Collin 		if (!rc_read_init(&s->rc, b))
1250aaa2975fSLasse Collin 			return XZ_OK;
1251aaa2975fSLasse Collin 
1252aaa2975fSLasse Collin 		s->lzma2.compressed -= RC_INIT_BYTES;
1253aaa2975fSLasse Collin 		s->lzma2.sequence = SEQ_LZMA_RUN;
1254aaa2975fSLasse Collin 
1255aaa2975fSLasse Collin 		dict_reset(&s->dict, b);
1256aaa2975fSLasse Collin 	}
1257aaa2975fSLasse Collin 
1258aaa2975fSLasse Collin 	/* This is to allow increasing b->out_size between calls. */
1259aaa2975fSLasse Collin 	if (DEC_IS_SINGLE(s->dict.mode))
1260aaa2975fSLasse Collin 		s->dict.end = b->out_size - b->out_pos;
1261aaa2975fSLasse Collin 
1262aaa2975fSLasse Collin 	while (true) {
1263aaa2975fSLasse Collin 		dict_limit(&s->dict, min_t(size_t, b->out_size - b->out_pos,
1264aaa2975fSLasse Collin 					   s->lzma2.uncompressed));
1265aaa2975fSLasse Collin 
1266aaa2975fSLasse Collin 		if (!lzma2_lzma(s, b))
1267aaa2975fSLasse Collin 			return XZ_DATA_ERROR;
1268aaa2975fSLasse Collin 
1269aaa2975fSLasse Collin 		s->lzma2.uncompressed -= dict_flush(&s->dict, b);
1270aaa2975fSLasse Collin 
1271aaa2975fSLasse Collin 		if (s->lzma2.uncompressed == 0) {
1272aaa2975fSLasse Collin 			if (s->lzma2.pedantic_microlzma) {
1273aaa2975fSLasse Collin 				if (s->lzma2.compressed > 0 || s->lzma.len > 0
1274aaa2975fSLasse Collin 						|| !rc_is_finished(&s->rc))
1275aaa2975fSLasse Collin 					return XZ_DATA_ERROR;
1276aaa2975fSLasse Collin 			}
1277aaa2975fSLasse Collin 
1278aaa2975fSLasse Collin 			return XZ_STREAM_END;
1279aaa2975fSLasse Collin 		}
1280aaa2975fSLasse Collin 
1281aaa2975fSLasse Collin 		if (b->out_pos == b->out_size)
1282aaa2975fSLasse Collin 			return XZ_OK;
1283aaa2975fSLasse Collin 
1284aaa2975fSLasse Collin 		if (b->in_pos == b->in_size
1285aaa2975fSLasse Collin 				&& s->temp.size < s->lzma2.compressed)
1286aaa2975fSLasse Collin 			return XZ_OK;
1287aaa2975fSLasse Collin 	}
1288aaa2975fSLasse Collin }
1289aaa2975fSLasse Collin 
xz_dec_microlzma_alloc(enum xz_mode mode,uint32_t dict_size)1290aaa2975fSLasse Collin struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode,
1291aaa2975fSLasse Collin 						uint32_t dict_size)
1292aaa2975fSLasse Collin {
1293aaa2975fSLasse Collin 	struct xz_dec_microlzma *s;
1294aaa2975fSLasse Collin 
1295aaa2975fSLasse Collin 	/* Restrict dict_size to the same range as in the LZMA2 code. */
1296aaa2975fSLasse Collin 	if (dict_size < 4096 || dict_size > (3U << 30))
1297aaa2975fSLasse Collin 		return NULL;
1298aaa2975fSLasse Collin 
1299aaa2975fSLasse Collin 	s = kmalloc(sizeof(*s), GFP_KERNEL);
1300aaa2975fSLasse Collin 	if (s == NULL)
1301aaa2975fSLasse Collin 		return NULL;
1302aaa2975fSLasse Collin 
1303aaa2975fSLasse Collin 	s->s.dict.mode = mode;
1304aaa2975fSLasse Collin 	s->s.dict.size = dict_size;
1305aaa2975fSLasse Collin 
1306aaa2975fSLasse Collin 	if (DEC_IS_MULTI(mode)) {
1307aaa2975fSLasse Collin 		s->s.dict.end = dict_size;
1308aaa2975fSLasse Collin 
1309aaa2975fSLasse Collin 		s->s.dict.buf = vmalloc(dict_size);
1310aaa2975fSLasse Collin 		if (s->s.dict.buf == NULL) {
1311aaa2975fSLasse Collin 			kfree(s);
1312aaa2975fSLasse Collin 			return NULL;
1313aaa2975fSLasse Collin 		}
1314aaa2975fSLasse Collin 	}
1315aaa2975fSLasse Collin 
1316aaa2975fSLasse Collin 	return s;
1317aaa2975fSLasse Collin }
1318aaa2975fSLasse Collin 
xz_dec_microlzma_reset(struct xz_dec_microlzma * s,uint32_t comp_size,uint32_t uncomp_size,int uncomp_size_is_exact)1319aaa2975fSLasse Collin void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, uint32_t comp_size,
1320aaa2975fSLasse Collin 			    uint32_t uncomp_size, int uncomp_size_is_exact)
1321aaa2975fSLasse Collin {
1322aaa2975fSLasse Collin 	/*
1323aaa2975fSLasse Collin 	 * comp_size is validated in xz_dec_microlzma_run().
1324aaa2975fSLasse Collin 	 * uncomp_size can safely be anything.
1325aaa2975fSLasse Collin 	 */
1326aaa2975fSLasse Collin 	s->s.lzma2.compressed = comp_size;
1327aaa2975fSLasse Collin 	s->s.lzma2.uncompressed = uncomp_size;
1328aaa2975fSLasse Collin 	s->s.lzma2.pedantic_microlzma = uncomp_size_is_exact;
1329aaa2975fSLasse Collin 
1330aaa2975fSLasse Collin 	s->s.lzma2.sequence = SEQ_PROPERTIES;
1331aaa2975fSLasse Collin 	s->s.temp.size = 0;
1332aaa2975fSLasse Collin }
1333aaa2975fSLasse Collin 
xz_dec_microlzma_end(struct xz_dec_microlzma * s)1334aaa2975fSLasse Collin void xz_dec_microlzma_end(struct xz_dec_microlzma *s)
1335aaa2975fSLasse Collin {
1336aaa2975fSLasse Collin 	if (DEC_IS_MULTI(s->s.dict.mode))
1337aaa2975fSLasse Collin 		vfree(s->s.dict.buf);
1338aaa2975fSLasse Collin 
1339aaa2975fSLasse Collin 	kfree(s);
1340aaa2975fSLasse Collin }
1341aaa2975fSLasse Collin #endif
1342