10578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
20578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * LZMA2 decoder
30578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
40578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Authors: Lasse Collin <lasse.collin@tukaani.org>
50578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *          Igor Pavlov <http://7-zip.org/>
60578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
70578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * This file has been put into the public domain.
80578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * You can do whatever you want with this file.
90578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#include "xz_private.h"
120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#include "xz_lzma2.h"
130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Range decoder initialization eats the first five bytes of each LZMA chunk.
160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#define RC_INIT_BYTES 5
180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Minimum number of usable input buffer to safely decode one LZMA symbol.
210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * The worst case is that we decode 22 bits using probabilities and 26
220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * direct bits. This may decode at maximum of 20 bytes of input. However,
230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * lzma_main() does an extra normalization before returning, thus we
240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * need to put 21 here.
250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#define LZMA_IN_REQUIRED 21
270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Dictionary (history buffer)
300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * These are always true:
320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *    start <= pos <= full <= end
330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *    pos <= limit <= end
340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * In multi-call mode, also these are true:
360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *    end == size
370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *    size <= size_max
380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *    allocated <= size
390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Most of these variables are size_t to support single-call mode,
410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * in which the dictionary variables address the actual output
420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * buffer directly.
430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
440578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct dictionary {
450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Beginning of the history buffer */
460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint8_t *buf;
470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Old position in buf (before decoding more data) */
490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t start;
500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Position in buf */
520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t pos;
530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * How full dictionary is. This is used to detect corrupt input that
560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * would read beyond the beginning of the uncompressed stream.
570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t full;
590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Write limit; we don't write to buf[limit] or later bytes. */
610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t limit;
620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * End of the dictionary buffer. In multi-call mode, this is
650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * the same as the dictionary size. In single-call mode, this
660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * indicates the size of the output buffer.
670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t end;
690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Size of the dictionary as specified in Block Header. This is used
720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * together with "full" to detect corrupt input that would make us
730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * read beyond the beginning of the uncompressed stream.
740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t size;
760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Maximum allowed dictionary size in multi-call mode.
790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * This is ignored in single-call mode.
800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t size_max;
820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Amount of memory currently allocated for the dictionary.
850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * size_max is always the same as the allocated size.)
870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t allocated;
890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Operation mode */
910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_mode mode;
920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Range decoder */
950578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct rc_dec {
960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t range;
970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t code;
980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Number of initializing bytes remaining to be read
1010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * by rc_read_init().
1020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t init_bytes_left;
1040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Buffer from which we read our input. It can be either
1070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * temp.buf or the caller-provided input buffer.
1080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	const uint8_t *in;
1100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_pos;
1110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_limit;
1120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
1130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Probabilities for a length decoder. */
1150578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct lzma_len_dec {
1160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probability of match length being at least 10 */
1170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t choice;
1180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probability of match length being at least 18 */
1200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t choice2;
1210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probabilities for match lengths 2-9 */
1230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
1240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probabilities for match lengths 10-17 */
1260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
1270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probabilities for match lengths 18-273 */
1290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t high[LEN_HIGH_SYMBOLS];
1300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
1310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1320578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct lzma_dec {
1330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Distances of latest four matches */
1340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t rep0;
1350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t rep1;
1360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t rep2;
1370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t rep3;
1380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Types of the most recently seen LZMA symbols */
1400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum lzma_state state;
1410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Length of a match. This is updated so that dict_repeat can
1440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * be called again to finish repeating the whole match.
1450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t len;
1470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * LZMA properties or related bit masks (number of literal
1500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * context bits, a mask dervied from the number of literal
1510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * position bits, and a mask dervied from the number
1520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * position bits)
1530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t lc;
1550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t literal_pos_mask; /* (1 << lp) - 1 */
1560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t pos_mask;         /* (1 << pb) - 1 */
1570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* If 1, it's a match. Otherwise it's a single 8-bit literal. */
1590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_match[STATES][POS_STATES_MAX];
1600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
1620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_rep[STATES];
1630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * If 0, distance of a repeated match is rep0.
1660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Otherwise check is_rep1.
1670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_rep0[STATES];
1690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * If 0, distance of a repeated match is rep1.
1720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Otherwise check is_rep2.
1730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_rep1[STATES];
1750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
1770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_rep2[STATES];
1780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * If 1, the repeated match has length of one byte. Otherwise
1810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * the length is decoded from rep_len_decoder.
1820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t is_rep0_long[STATES][POS_STATES_MAX];
1840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Probability tree for the highest two bits of the match
1870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * distance. There is a separate probability tree for match
1880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
1890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
1910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Probility trees for additional bits for match distance
1940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * when the distance is in the range [4, 127].
1950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
1970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Probability tree for the lowest four bits of a match
2000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * distance that is equal to or greater than 128.
2010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t dist_align[ALIGN_SIZE];
2030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Length of a normal match */
2050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct lzma_len_dec match_len_dec;
2060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Length of a repeated match */
2080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct lzma_len_dec rep_len_dec;
2090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Probabilities of literals */
2110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
2120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
2130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2140578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct lzma2_dec {
2150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Position in xz_dec_lzma2_run(). */
2160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum lzma2_seq {
2170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_CONTROL,
2180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_UNCOMPRESSED_1,
2190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_UNCOMPRESSED_2,
2200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_COMPRESSED_0,
2210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_COMPRESSED_1,
2220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_PROPERTIES,
2230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_LZMA_PREPARE,
2240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_LZMA_RUN,
2250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_COPY
2260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} sequence;
2270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Next position after decoding the compressed size of the chunk. */
2290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum lzma2_seq next_sequence;
2300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Uncompressed size of LZMA chunk (2 MiB at maximum) */
2320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t uncompressed;
2330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Compressed size of LZMA chunk or compressed/uncompressed
2360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * size of uncompressed chunk (64 KiB at maximum)
2370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t compressed;
2390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * True if dictionary reset is needed. This is false before
2420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * the first chunk (LZMA or uncompressed).
2430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	bool need_dict_reset;
2450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * True if new LZMA properties are needed. This is false
2480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * before the first LZMA chunk.
2490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	bool need_props;
2510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
2520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2530578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct xz_dec_lzma2 {
2540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * The order below is important on x86 to reduce code size and
2560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * it shouldn't hurt on other platforms. Everything up to and
2570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * including lzma.pos_mask are in the first 128 bytes on x86-32,
2580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * which allows using smaller instructions to access those
2590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * variables. On x86-64, fewer variables fit into the first 128
2600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * bytes, but this is still the best order without sacrificing
2610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * the readability by splitting the structures.
2620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct rc_dec rc;
2640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct dictionary dict;
2650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct lzma2_dec lzma2;
2660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct lzma_dec lzma;
2670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Temporary buffer which holds small number of input bytes between
2700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * decoder calls. See lzma2_lzma() for details.
2710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct {
2730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		uint32_t size;
2740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		uint8_t buf[3 * LZMA_IN_REQUIRED];
2750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} temp;
2760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
2770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/**************
2790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Dictionary *
2800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync **************/
2810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
2830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Reset the dictionary state. When in single-call mode, set up the beginning
2840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * of the dictionary to point to the actual output buffer.
2850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
2860578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void dict_reset(struct dictionary *dict, struct xz_buf *b)
2870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
2880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_SINGLE(dict->mode)) {
2890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->buf = b->out + b->out_pos;
2900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->end = b->out_size - b->out_pos;
2910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
2920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->start = 0;
2940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->pos = 0;
2950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->limit = 0;
2960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->full = 0;
2970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
2980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Set dictionary write limit */
3000578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void dict_limit(struct dictionary *dict, size_t out_max)
3010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dict->end - dict->pos <= out_max)
3030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->limit = dict->end;
3040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	else
3050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->limit = dict->pos + out_max;
3060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Return true if at least one byte can be written into the dictionary. */
3090578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline bool dict_has_space(const struct dictionary *dict)
3100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return dict->pos < dict->limit;
3120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Get a byte from the dictionary at the given distance. The distance is
3160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * assumed to valid, or as a special case, zero when the dictionary is
3170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * still empty. This special case is needed for single-call decoding to
3180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * avoid writing a '\0' to the end of the destination buffer.
3190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3200578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist)
3210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t offset = dict->pos - dist - 1;
3230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dist >= dict->pos)
3250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		offset += dict->end;
3260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return dict->full > 0 ? dict->buf[offset] : 0;
3280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Put one byte into the dictionary. It is assumed that there is space for it.
3320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3330578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline void dict_put(struct dictionary *dict, uint8_t byte)
3340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->buf[dict->pos++] = byte;
3360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dict->full < dict->pos)
3380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->full = dict->pos;
3390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Repeat given number of bytes from the given distance. If the distance is
3430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * invalid, false is returned. On success, true is returned and *len is
3440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * updated to indicate how many bytes were left to be repeated.
3450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3460578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
3470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t back;
3490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t left;
3500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dist >= dict->full || dist >= dict->size)
3520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return false;
3530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	left = min_t(size_t, dict->limit - dict->pos, *len);
3550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	*len -= left;
3560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	back = dict->pos - dist - 1;
3580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dist >= dict->pos)
3590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		back += dict->end;
3600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
3620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->buf[dict->pos++] = dict->buf[back++];
3630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (back == dict->end)
3640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			back = 0;
3650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (--left > 0);
3660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dict->full < dict->pos)
3680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->full = dict->pos;
3690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
3710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Copy uncompressed data as is from input to dictionary and output buffers. */
3740578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
3750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			      uint32_t *left)
3760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t copy_size;
3780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (*left > 0 && b->in_pos < b->in_size
3800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			&& b->out_pos < b->out_size) {
3810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		copy_size = min(b->in_size - b->in_pos,
3820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				b->out_size - b->out_pos);
3830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (copy_size > dict->end - dict->pos)
3840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			copy_size = dict->end - dict->pos;
3850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (copy_size > *left)
3860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			copy_size = *left;
3870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		*left -= copy_size;
3890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
3910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->pos += copy_size;
3920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (dict->full < dict->pos)
3940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			dict->full = dict->pos;
3950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (DEC_IS_MULTI(dict->mode)) {
3970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (dict->pos == dict->end)
3980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				dict->pos = 0;
3990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			memcpy(b->out + b->out_pos, b->in + b->in_pos,
4010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					copy_size);
4020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
4030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict->start = dict->pos;
4050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		b->out_pos += copy_size;
4070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		b->in_pos += copy_size;
4080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
4090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
4120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Flush pending data from dictionary to b->out. It is assumed that there is
4130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * enough space in b->out. This is guaranteed because caller uses dict_limit()
4140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * before decoding data into the dictionary.
4150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
4160578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
4170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t copy_size = dict->pos - dict->start;
4190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_MULTI(dict->mode)) {
4210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (dict->pos == dict->end)
4220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			dict->pos = 0;
4230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		memcpy(b->out + b->out_pos, dict->buf + dict->start,
4250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				copy_size);
4260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
4270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict->start = dict->pos;
4290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	b->out_pos += copy_size;
4300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return copy_size;
4310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*****************
4340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Range decoder *
4350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *****************/
4360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Reset the range decoder. */
4380578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void rc_reset(struct rc_dec *rc)
4390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc->range = (uint32_t)-1;
4410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc->code = 0;
4420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc->init_bytes_left = RC_INIT_BYTES;
4430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
4460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Read the first five initial bytes into rc->code if they haven't been
4470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * read already. (Yes, the first byte gets completely ignored.)
4480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
4490578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
4500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (rc->init_bytes_left > 0) {
4520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (b->in_pos == b->in_size)
4530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return false;
4540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->code = (rc->code << 8) + b->in[b->in_pos++];
4560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		--rc->init_bytes_left;
4570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
4580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
4600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Return true if there may not be enough input for the next decoding loop. */
4630578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline bool rc_limit_exceeded(const struct rc_dec *rc)
4640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return rc->in_pos > rc->in_limit;
4660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
4690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Return true if it is possible (from point of view of range decoder) that
4700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * we have reached the end of the LZMA chunk.
4710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
4720578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline bool rc_is_finished(const struct rc_dec *rc)
4730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return rc->code == 0;
4750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Read the next input byte if needed. */
4780578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic __always_inline void rc_normalize(struct rc_dec *rc)
4790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (rc->range < RC_TOP_VALUE) {
4810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->range <<= RC_SHIFT_BITS;
4820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
4830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
4840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
4870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Decode one bit. In some versions, this function has been splitted in three
4880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * functions so that the compiler is supposed to be able to more easily avoid
4890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * an extra branch. In this particular version of the LZMA decoder, this
4900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
4910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * on x86). Using a non-splitted version results in nicer looking code too.
4920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
4930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * NOTE: This must return an int. Do not make it return a bool or the speed
4940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
4950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
4960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
4970578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
4980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t bound;
5000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	int bit;
5010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc_normalize(rc);
5030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
5040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (rc->code < bound) {
5050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->range = bound;
5060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		*prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
5070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		bit = 0;
5080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
5090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->range -= bound;
5100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->code -= bound;
5110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		*prob -= *prob >> RC_MOVE_BITS;
5120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		bit = 1;
5130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
5140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return bit;
5160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode a bittree starting from the most significant bit. */
5190578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic __always_inline uint32_t rc_bittree(struct rc_dec *rc,
5200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					   uint16_t *probs, uint32_t limit)
5210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t symbol = 1;
5230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
5250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (rc_bit(rc, &probs[symbol]))
5260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			symbol = (symbol << 1) + 1;
5270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		else
5280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			symbol <<= 1;
5290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (symbol < limit);
5300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return symbol;
5320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode a bittree starting from the least significant bit. */
5350578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic __always_inline void rc_bittree_reverse(struct rc_dec *rc,
5360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					       uint16_t *probs,
5370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					       uint32_t *dest, uint32_t limit)
5380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t symbol = 1;
5400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t i = 0;
5410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
5430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (rc_bit(rc, &probs[symbol])) {
5440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			symbol = (symbol << 1) + 1;
5450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			*dest += 1 << i;
5460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
5470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			symbol <<= 1;
5480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
5490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (++i < limit);
5500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode direct bits (fixed fifty-fifty probability) */
5530578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
5540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t mask;
5560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
5580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc_normalize(rc);
5590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->range >>= 1;
5600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->code -= rc->range;
5610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		mask = (uint32_t)0 - (rc->code >> 31);
5620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		rc->code += rc->range & mask;
5630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		*dest = (*dest << 1) + (mask + 1);
5640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (--limit > 0);
5650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/********
5680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * LZMA *
5690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync ********/
5700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Get pointer to literal coder probability array. */
5720578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s)
5730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t prev_byte = dict_get(&s->dict, 0);
5750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t low = prev_byte >> (8 - s->lzma.lc);
5760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
5770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return s->lzma.literal[low + high];
5780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode a literal (one 8-bit byte) */
5810578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void lzma_literal(struct xz_dec_lzma2 *s)
5820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t *probs;
5840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t symbol;
5850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t match_byte;
5860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t match_bit;
5870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t offset;
5880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t i;
5890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	probs = lzma_literal_probs(s);
5910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (lzma_state_is_literal(s->lzma.state)) {
5930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		symbol = rc_bittree(&s->rc, probs, 0x100);
5940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
5950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		symbol = 1;
5960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
5970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		offset = 0x100;
5980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		do {
6000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			match_bit = match_byte & offset;
6010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			match_byte <<= 1;
6020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			i = offset + match_bit + symbol;
6030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (rc_bit(&s->rc, &probs[i])) {
6050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				symbol = (symbol << 1) + 1;
6060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				offset &= match_bit;
6070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			} else {
6080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				symbol <<= 1;
6090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				offset &= ~match_bit;
6100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} while (symbol < 0x100);
6120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
6130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dict_put(&s->dict, (uint8_t)symbol);
6150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_state_literal(&s->lzma.state);
6160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
6170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode the length of the match into s->lzma.len. */
6190578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
6200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		     uint32_t pos_state)
6210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
6220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t *probs;
6230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t limit;
6240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (!rc_bit(&s->rc, &l->choice)) {
6260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		probs = l->low[pos_state];
6270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		limit = LEN_LOW_SYMBOLS;
6280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma.len = MATCH_LEN_MIN;
6290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
6300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!rc_bit(&s->rc, &l->choice2)) {
6310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			probs = l->mid[pos_state];
6320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			limit = LEN_MID_SYMBOLS;
6330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
6340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
6350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			probs = l->high;
6360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			limit = LEN_HIGH_SYMBOLS;
6370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
6380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					+ LEN_MID_SYMBOLS;
6390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
6400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
6410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
6430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
6440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode a match. The distance will be stored in s->lzma.rep0. */
6460578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
6470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
6480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t *probs;
6490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t dist_slot;
6500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t limit;
6510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_state_match(&s->lzma.state);
6530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep3 = s->lzma.rep2;
6550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep2 = s->lzma.rep1;
6560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep1 = s->lzma.rep0;
6570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_len(s, &s->lzma.match_len_dec, pos_state);
6590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
6610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
6620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dist_slot < DIST_MODEL_START) {
6640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma.rep0 = dist_slot;
6650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
6660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		limit = (dist_slot >> 1) - 1;
6670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma.rep0 = 2 + (dist_slot & 1);
6680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (dist_slot < DIST_MODEL_END) {
6700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.rep0 <<= limit;
6710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			probs = s->lzma.dist_special + s->lzma.rep0
6720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					- dist_slot - 1;
6730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			rc_bittree_reverse(&s->rc, probs,
6740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					&s->lzma.rep0, limit);
6750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
6760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
6770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.rep0 <<= ALIGN_BITS;
6780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			rc_bittree_reverse(&s->rc, s->lzma.dist_align,
6790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					&s->lzma.rep0, ALIGN_BITS);
6800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
6810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
6820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
6830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
6850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Decode a repeated match. The distance is one of the four most recently
6860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * seen matches. The distance will be stored in s->lzma.rep0.
6870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
6880578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
6890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
6900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t tmp;
6910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
6930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
6940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma.state][pos_state])) {
6950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			lzma_state_short_rep(&s->lzma.state);
6960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.len = 1;
6970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return;
6980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
6990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
7000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
7010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			tmp = s->lzma.rep1;
7020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
7030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
7040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				tmp = s->lzma.rep2;
7050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			} else {
7060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				tmp = s->lzma.rep3;
7070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma.rep3 = s->lzma.rep2;
7080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
7090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma.rep2 = s->lzma.rep1;
7110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
7120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma.rep1 = s->lzma.rep0;
7140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma.rep0 = tmp;
7150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
7160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_state_long_rep(&s->lzma.state);
7180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_len(s, &s->lzma.rep_len_dec, pos_state);
7190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
7200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* LZMA decoder core */
7220578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool lzma_main(struct xz_dec_lzma2 *s)
7230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
7240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t pos_state;
7250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
7270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * If the dictionary was reached during the previous call, try to
7280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * finish the possibly pending repeat in the dictionary.
7290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
7300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (dict_has_space(&s->dict) && s->lzma.len > 0)
7310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
7320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
7340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Decode more LZMA symbols. One iteration may consume up to
7350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * LZMA_IN_REQUIRED - 1 bytes.
7360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
7370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
7380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		pos_state = s->dict.pos & s->lzma.pos_mask;
7390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!rc_bit(&s->rc, &s->lzma.is_match[
7410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma.state][pos_state])) {
7420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			lzma_literal(s);
7430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
7440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
7450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				lzma_rep_match(s, pos_state);
7460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			else
7470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				lzma_match(s, pos_state);
7480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
7500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return false;
7510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
7520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
7530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
7550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Having the range decoder always normalized when we are outside
7560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * this function makes it easier to correctly handle end of the chunk.
7570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
7580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc_normalize(&s->rc);
7590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
7610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
7620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
7640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Reset the LZMA decoder and range decoder state. Dictionary is nore reset
7650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * here, because LZMA state may be reset without resetting the dictionary.
7660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
7670578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void lzma_reset(struct xz_dec_lzma2 *s)
7680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
7690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint16_t *probs;
7700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t i;
7710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.state = STATE_LIT_LIT;
7730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep0 = 0;
7740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep1 = 0;
7750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep2 = 0;
7760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.rep3 = 0;
7770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
7790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * All probabilities are initialized to the same value. This hack
7800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * makes the code smaller by avoiding a separate loop for each
7810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * probability array.
7820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 *
7830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * This could be optimized so that only that part of literal
7840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * probabilities that are actually required. In the common case
7850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * we would write 12 KiB less.
7860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
7870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	probs = s->lzma.is_match[0];
7880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	for (i = 0; i < PROBS_TOTAL; ++i)
7890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		probs[i] = RC_BIT_MODEL_TOTAL / 2;
7900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	rc_reset(&s->rc);
7920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
7930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
7950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
7960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * from the decoded lp and pb values. On success, the LZMA decoder state is
7970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * reset and true is returned.
7980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
7990578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
8000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
8010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (props > (4 * 5 + 4) * 9 + 8)
8020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return false;
8030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.pos_mask = 0;
8050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (props >= 9 * 5) {
8060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		props -= 9 * 5;
8070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++s->lzma.pos_mask;
8080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
8090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
8110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.literal_pos_mask = 0;
8130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (props >= 9) {
8140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		props -= 9;
8150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++s->lzma.literal_pos_mask;
8160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
8170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.lc = props;
8190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
8210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return false;
8220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
8240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	lzma_reset(s);
8260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
8280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
8290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*********
8310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * LZMA2 *
8320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *********/
8330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
8350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
8360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
8370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * wrapper function takes care of making the LZMA decoder's assumption safe.
8380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
8390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * As long as there is plenty of input left to be decoded in the current LZMA
8400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * chunk, we decode directly from the caller-supplied input buffer until
8410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
8420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * s->temp.buf, which (hopefully) gets filled on the next call to this
8430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * function. We decode a few bytes from the temporary buffer so that we can
8440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * continue decoding from the caller-supplied input buffer again.
8450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
8460578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
8470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
8480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_avail;
8490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t tmp;
8500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	in_avail = b->in_size - b->in_pos;
8520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.size > 0 || s->lzma2.compressed == 0) {
8530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
8540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (tmp > s->lzma2.compressed - s->temp.size)
8550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			tmp = s->lzma2.compressed - s->temp.size;
8560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (tmp > in_avail)
8570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			tmp = in_avail;
8580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
8600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->temp.size + tmp == s->lzma2.compressed) {
8620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			memzero(s->temp.buf + s->temp.size + tmp,
8630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					sizeof(s->temp.buf)
8640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						- s->temp.size - tmp);
8650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->rc.in_limit = s->temp.size + tmp;
8660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
8670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->temp.size += tmp;
8680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			b->in_pos += tmp;
8690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return true;
8700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} else {
8710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
8720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
8730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->rc.in = s->temp.buf;
8750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->rc.in_pos = 0;
8760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
8780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return false;
8790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma2.compressed -= s->rc.in_pos;
8810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->rc.in_pos < s->temp.size) {
8830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->temp.size -= s->rc.in_pos;
8840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
8850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->temp.size);
8860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return true;
8870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
8880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		b->in_pos += s->rc.in_pos - s->temp.size;
8900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->temp.size = 0;
8910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
8920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	in_avail = b->in_size - b->in_pos;
8940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (in_avail >= LZMA_IN_REQUIRED) {
8950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->rc.in = b->in;
8960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->rc.in_pos = b->in_pos;
8970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
8990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->rc.in_limit = b->in_pos + s->lzma2.compressed;
9000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		else
9010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
9020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (!lzma_main(s))
9040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return false;
9050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		in_avail = s->rc.in_pos - b->in_pos;
9070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (in_avail > s->lzma2.compressed)
9080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return false;
9090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->lzma2.compressed -= in_avail;
9110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		b->in_pos = s->rc.in_pos;
9120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
9130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	in_avail = b->in_size - b->in_pos;
9150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (in_avail < LZMA_IN_REQUIRED) {
9160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (in_avail > s->lzma2.compressed)
9170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			in_avail = s->lzma2.compressed;
9180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
9200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->temp.size = in_avail;
9210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		b->in_pos += in_avail;
9220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
9230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
9250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
9260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
9280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Take care of the LZMA2 control layer, and forward the job of actual LZMA
9290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * decoding or copying of uncompressed chunks to other functions.
9300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
9310578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
9320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				       struct xz_buf *b)
9330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
9340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t tmp;
9350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
9370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		switch (s->lzma2.sequence) {
9380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_CONTROL:
9390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
9400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * LZMA2 control byte
9410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *
9420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Exact values:
9430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0x00   End marker
9440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0x01   Dictionary reset followed by
9450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          an uncompressed chunk
9460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0x02   Uncompressed chunk (no dictionary reset)
9470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *
9480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Highest three bits (s->control & 0xE0):
9490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0xE0   Dictionary reset, new properties and state
9500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          reset, followed by LZMA compressed chunk
9510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0xC0   New properties and state reset, followed
9520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          by LZMA compressed chunk (no dictionary
9530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          reset)
9540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0xA0   State reset using old properties,
9550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          followed by LZMA compressed chunk (no
9560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *          dictionary reset)
9570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *   0x80   LZMA chunk (no dictionary or state reset)
9580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *
9590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * For LZMA compressed chunks, the lowest five bits
9600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * (s->control & 1F) are the highest bits of the
9610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * uncompressed size (bits 16-20).
9620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *
9630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * A new LZMA2 stream must begin with a dictionary
9640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * reset. The first LZMA chunk must set new
9650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * properties and reset the LZMA state.
9660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 *
9670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Values that don't match anything described above
9680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * are invalid and we return XZ_DATA_ERROR.
9690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
9700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			tmp = b->in[b->in_pos++];
9710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (tmp == 0x00)
9730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_STREAM_END;
9740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (tmp >= 0xE0 || tmp == 0x01) {
9760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.need_props = true;
9770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.need_dict_reset = false;
9780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				dict_reset(&s->dict, b);
9790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			} else if (s->lzma2.need_dict_reset) {
9800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
9810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
9820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (tmp >= 0x80) {
9840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.uncompressed = (tmp & 0x1F) << 16;
9850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
9860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (tmp >= 0xC0) {
9880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					/*
9890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					 * When there are new properties,
9900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					 * state reset is done at
9910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					 * SEQ_PROPERTIES.
9920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					 */
9930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->lzma2.need_props = false;
9940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->lzma2.next_sequence
9950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync							= SEQ_PROPERTIES;
9960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
9970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				} else if (s->lzma2.need_props) {
9980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_DATA_ERROR;
9990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				} else {
10010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->lzma2.next_sequence
10020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync							= SEQ_LZMA_PREPARE;
10030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					if (tmp >= 0xA0)
10040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						lzma_reset(s);
10050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				}
10060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			} else {
10070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (tmp > 0x02)
10080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_DATA_ERROR;
10090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.sequence = SEQ_COMPRESSED_0;
10110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.next_sequence = SEQ_COPY;
10120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
10130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_UNCOMPRESSED_1:
10170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.uncompressed
10180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					+= (uint32_t)b->in[b->in_pos++] << 8;
10190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
10200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_UNCOMPRESSED_2:
10230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.uncompressed
10240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					+= (uint32_t)b->in[b->in_pos++] + 1;
10250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_COMPRESSED_0;
10260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_COMPRESSED_0:
10290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.compressed
10300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					= (uint32_t)b->in[b->in_pos++] << 8;
10310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_COMPRESSED_1;
10320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_COMPRESSED_1:
10350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.compressed
10360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					+= (uint32_t)b->in[b->in_pos++] + 1;
10370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = s->lzma2.next_sequence;
10380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_PROPERTIES:
10410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!lzma_props(s, b->in[b->in_pos++]))
10420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
10430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_LZMA_PREPARE;
10450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_LZMA_PREPARE:
10470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->lzma2.compressed < RC_INIT_BYTES)
10480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
10490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!rc_read_init(&s->rc, b))
10510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
10520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.compressed -= RC_INIT_BYTES;
10540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_LZMA_RUN;
10550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_LZMA_RUN:
10570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
10580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Set dictionary limit to indicate how much we want
10590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * to be encoded at maximum. Decode new data into the
10600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * dictionary. Flush the new data from dictionary to
10610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * b->out. Check if we finished decoding this chunk.
10620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * In case the dictionary got full but we didn't fill
10630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * the output buffer yet, we may run this loop
10640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * multiple times without changing s->lzma2.sequence.
10650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
10660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			dict_limit(&s->dict, min_t(size_t,
10670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					b->out_size - b->out_pos,
10680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->lzma2.uncompressed));
10690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!lzma2_lzma(s, b))
10700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
10710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.uncompressed -= dict_flush(&s->dict, b);
10730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->lzma2.uncompressed == 0) {
10750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (s->lzma2.compressed > 0 || s->lzma.len > 0
10760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						|| !rc_is_finished(&s->rc))
10770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_DATA_ERROR;
10780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				rc_reset(&s->rc);
10800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->lzma2.sequence = SEQ_CONTROL;
10810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			} else if (b->out_pos == b->out_size
10830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					|| (b->in_pos == b->in_size
10840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						&& s->temp.size
10850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						< s->lzma2.compressed)) {
10860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
10870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
10880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_COPY:
10920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
10930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->lzma2.compressed > 0)
10940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
10950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
10960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->lzma2.sequence = SEQ_CONTROL;
10970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
10980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
10990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
11000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_OK;
11020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
11030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11040578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
11050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync						   uint32_t dict_max)
11060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
11070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL);
11080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s == NULL)
11090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return NULL;
11100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->dict.mode = mode;
11120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->dict.size_max = dict_max;
11130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_PREALLOC(mode)) {
11150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->dict.buf = vmalloc(dict_max);
11160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->dict.buf == NULL) {
11170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			kfree(s);
11180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return NULL;
11190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
11200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else if (DEC_IS_DYNALLOC(mode)) {
11210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->dict.buf = NULL;
11220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->dict.allocated = 0;
11230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
11240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return s;
11260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
11270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11280578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
11290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
11300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* This limits dictionary size to 3 GiB to keep parsing simpler. */
11310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (props > 39)
11320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
11330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->dict.size = 2 + (props & 1);
11350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->dict.size <<= (props >> 1) + 11;
11360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_MULTI(s->dict.mode)) {
11380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->dict.size > s->dict.size_max)
11390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_MEMLIMIT_ERROR;
11400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->dict.end = s->dict.size;
11420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (DEC_IS_DYNALLOC(s->dict.mode)) {
11440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->dict.allocated < s->dict.size) {
11450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				vfree(s->dict.buf);
11460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->dict.buf = vmalloc(s->dict.size);
11470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (s->dict.buf == NULL) {
11480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->dict.allocated = 0;
11490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_MEM_ERROR;
11500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				}
11510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
11520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
11530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
11540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma.len = 0;
11560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma2.sequence = SEQ_CONTROL;
11580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma2.need_dict_reset = true;
11590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.size = 0;
11610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_OK;
11630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
11640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11650578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
11660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
11670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_MULTI(s->dict.mode))
11680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vfree(s->dict.buf);
11690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
11700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	kfree(s);
11710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
1172