10578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
20578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * .xz Stream decoder
30578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
40578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Author: Lasse Collin <lasse.collin@tukaani.org>
50578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
60578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * This file has been put into the public domain.
70578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * You can do whatever you want with this file.
80578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
90578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#include "xz_private.h"
110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#include "xz_stream.h"
120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_USE_CRC64
140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#	define IS_CRC64(check_type) ((check_type) == XZ_CHECK_CRC64)
150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#else
160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#	define IS_CRC64(check_type) false
170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Hash used to validate the Index field */
200578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct xz_dec_hash {
210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	vli_type unpadded;
220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	vli_type uncompressed;
230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t crc32;
240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
260578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstruct xz_dec {
270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Position in dec_main() */
280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum {
290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_STREAM_HEADER,
300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_BLOCK_START,
310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_BLOCK_HEADER,
320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_BLOCK_UNCOMPRESS,
330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_BLOCK_PADDING,
340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_BLOCK_CHECK,
350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_INDEX,
360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_INDEX_PADDING,
370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_INDEX_CRC32,
380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		SEQ_STREAM_FOOTER
390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} sequence;
400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Position in variable-length integers and Check fields */
420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t pos;
430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Variable-length integer decoded by dec_vli() */
450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	vli_type vli;
460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Saved in_pos and out_pos */
480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_start;
490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t out_start;
500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_USE_CRC64
520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* CRC32 or CRC64 value in Block or CRC32 value in Index */
530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint64_t crc;
540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#else
550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* CRC32 value in Block or Index */
560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint32_t crc;
570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Type of the integrity check calculated from uncompressed data */
600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_check check_type;
610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Operation mode */
630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_mode mode;
640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * True if the next call to xz_dec_run() is allowed to return
670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * XZ_BUF_ERROR.
680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	bool allow_buf_error;
700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Information stored in Block Header */
720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct {
730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/*
740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * Value stored in the Compressed Size field, or
750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * VLI_UNKNOWN if Compressed Size is not present.
760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 */
770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type compressed;
780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/*
800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * Value stored in the Uncompressed Size field, or
810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * VLI_UNKNOWN if Uncompressed Size is not present.
820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 */
830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type uncompressed;
840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Size of the Block Header field */
860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		uint32_t size;
870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} block_header;
880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Information collected when decoding Blocks */
900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct {
910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Observed compressed size of the current Block */
920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type compressed;
930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Observed uncompressed size of the current Block */
950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type uncompressed;
960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Number of Blocks decoded so far */
980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type count;
990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/*
1010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * Hash calculated from the Block sizes. This is used to
1020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * validate the Index field.
1030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 */
1040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		struct xz_dec_hash hash;
1050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} block;
1060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Variables needed when verifying the Index field */
1080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct {
1090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Position in dec_index() */
1100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		enum {
1110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			SEQ_INDEX_COUNT,
1120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			SEQ_INDEX_UNPADDED,
1130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			SEQ_INDEX_UNCOMPRESSED
1140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		} sequence;
1150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Size of the Index in bytes */
1170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type size;
1180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/* Number of Records (matches block.count in valid files) */
1200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		vli_type count;
1210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/*
1230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * Hash calculated from the Records (matches block.hash in
1240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * valid files).
1250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 */
1260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		struct xz_dec_hash hash;
1270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} index;
1280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
1300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Temporary buffer needed to hold Stream Header, Block Header,
1310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * and Stream Footer. The Block Header is the biggest (1 KiB)
1320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * so we reserve space according to that. buf[] has to be aligned
1330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * to a multiple of four bytes; the size_t variables before it
1340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * should guarantee this.
1350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
1360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct {
1370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		size_t pos;
1380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		size_t size;
1390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		uint8_t buf[1024];
1400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} temp;
1410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct xz_dec_lzma2 *lzma2;
1430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
1450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct xz_dec_bcj *bcj;
1460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	bool bcj_active;
1470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
1480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
1490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_ANY_CHECK
1510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Sizes of the Check field with different Check IDs */
1520578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic const uint8_t check_sizes[16] = {
1530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	0,
1540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	4, 4, 4,
1550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	8, 8, 8,
1560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	16, 16, 16,
1570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	32, 32, 32,
1580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	64, 64, 64
1590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync};
1600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
1610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
1630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
1640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * must have set s->temp.pos to indicate how much data we are supposed
1650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * to copy into s->temp.buf. Return true once s->temp.pos has reached
1660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * s->temp.size.
1670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
1680578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool fill_temp(struct xz_dec *s, struct xz_buf *b)
1690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
1700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t copy_size = min_t(size_t,
1710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			b->in_size - b->in_pos, s->temp.size - s->temp.pos);
1720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
1740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	b->in_pos += copy_size;
1750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.pos += copy_size;
1760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.pos == s->temp.size) {
1780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->temp.pos = 0;
1790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return true;
1800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
1810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return false;
1830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
1840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode a variable-length integer (little-endian base-128 encoding) */
1860578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in,
1870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			   size_t *in_pos, size_t in_size)
1880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
1890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	uint8_t byte;
1900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->pos == 0)
1920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->vli = 0;
1930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (*in_pos < in_size) {
1950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		byte = in[*in_pos];
1960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++*in_pos;
1970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
1980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->vli |= (vli_type)(byte & 0x7F) << s->pos;
1990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if ((byte & 0x80) == 0) {
2010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/* Don't allow non-minimal encodings. */
2020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (byte == 0 && s->pos != 0)
2030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
2040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->pos = 0;
2060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_STREAM_END;
2070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
2080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->pos += 7;
2100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->pos == 7 * VLI_BYTES_MAX)
2110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
2120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
2130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_OK;
2150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
2160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
2180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Decode the Compressed Data field from a Block. Update and validate
2190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * the observed compressed and uncompressed sizes of the Block so that
2200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * they don't exceed the values possibly stored in the Block Header
2210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * (validation assumes that no integer overflow occurs, since vli_type
2220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * is normally uint64_t). Update the CRC32 or CRC64 value if presence of
2230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * the CRC32 or CRC64 field was indicated in Stream Header.
2240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
2250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Once the decoding is finished, validate that the observed sizes match
2260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * the sizes possibly stored in the Block Header. Update the hash and
2270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Block count, which are later used to validate the Index field.
2280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
2290578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
2300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
2310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_ret ret;
2320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->in_start = b->in_pos;
2340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->out_start = b->out_pos;
2350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
2370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->bcj_active)
2380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
2390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	else
2400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
2410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		ret = xz_dec_lzma2_run(s->lzma2, b);
2420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->block.compressed += b->in_pos - s->in_start;
2440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->block.uncompressed += b->out_pos - s->out_start;
2450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
2470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * There is no need to separately check for VLI_UNKNOWN, since
2480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * the observed sizes are always smaller than VLI_UNKNOWN.
2490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
2500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->block.compressed > s->block_header.compressed
2510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			|| s->block.uncompressed
2520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				> s->block_header.uncompressed)
2530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
2540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->check_type == XZ_CHECK_CRC32)
2560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->crc = xz_crc32(b->out + s->out_start,
2570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				b->out_pos - s->out_start, s->crc);
2580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_USE_CRC64
2590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	else if (s->check_type == XZ_CHECK_CRC64)
2600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->crc = xz_crc64(b->out + s->out_start,
2610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				b->out_pos - s->out_start, s->crc);
2620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
2630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (ret == XZ_STREAM_END) {
2650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->block_header.compressed != VLI_UNKNOWN
2660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				&& s->block_header.compressed
2670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					!= s->block.compressed)
2680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
2690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->block_header.uncompressed != VLI_UNKNOWN
2710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				&& s->block_header.uncompressed
2720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					!= s->block.uncompressed)
2730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
2740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block.hash.unpadded += s->block_header.size
2760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				+ s->block.compressed;
2770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_ANY_CHECK
2790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block.hash.unpadded += check_sizes[s->check_type];
2800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#else
2810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->check_type == XZ_CHECK_CRC32)
2820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->block.hash.unpadded += 4;
2830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		else if (IS_CRC64(s->check_type))
2840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->block.hash.unpadded += 8;
2850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
2860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block.hash.uncompressed += s->block.uncompressed;
2880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block.hash.crc32 = xz_crc32(
2890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				(const uint8_t *)&s->block.hash,
2900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				sizeof(s->block.hash), s->block.hash.crc32);
2910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++s->block.count;
2930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
2940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return ret;
2960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
2970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
2980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Update the Index size and the CRC32 value. */
2990578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic void index_update(struct xz_dec *s, const struct xz_buf *b)
3000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_used = b->in_pos - s->in_start;
3020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->index.size += in_used;
3030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->crc = xz_crc32(b->in + s->in_start, in_used, s->crc);
3040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Decode the Number of Records, Unpadded Size, and Uncompressed Size
3080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * fields from the Index field. That is, Index Padding and CRC32 are not
3090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * decoded by this function.
3100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
3110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
3120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
3130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3140578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
3150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_ret ret;
3170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
3190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
3200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (ret != XZ_STREAM_END) {
3210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			index_update(s, b);
3220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return ret;
3230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
3240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		switch (s->index.sequence) {
3260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX_COUNT:
3270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.count = s->vli;
3280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
3300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Validate that the Number of Records field
3310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * indicates the same number of Records as
3320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * there were Blocks in the Stream.
3330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
3340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->index.count != s->block.count)
3350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
3360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.sequence = SEQ_INDEX_UNPADDED;
3380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
3390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX_UNPADDED:
3410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.hash.unpadded += s->vli;
3420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
3430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
3440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX_UNCOMPRESSED:
3460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.hash.uncompressed += s->vli;
3470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.hash.crc32 = xz_crc32(
3480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					(const uint8_t *)&s->index.hash,
3490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					sizeof(s->index.hash),
3500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					s->index.hash.crc32);
3510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			--s->index.count;
3520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->index.sequence = SEQ_INDEX_UNPADDED;
3530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
3540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
3550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (s->index.count > 0);
3560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_STREAM_END;
3580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Validate that the next four or eight input bytes match the value
3620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * of s->crc. s->pos must be zero when starting to validate the first byte.
3630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * The "bits" argument allows using the same code for both CRC32 and CRC64.
3640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3650578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret crc_validate(struct xz_dec *s, struct xz_buf *b,
3660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				uint32_t bits)
3670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	do {
3690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (b->in_pos == b->in_size)
3700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_OK;
3710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (((s->crc >> s->pos) & 0xFF) != b->in[b->in_pos++])
3730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
3740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->pos += 8;
3760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} while (s->pos < bits);
3780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->crc = 0;
3800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->pos = 0;
3810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_STREAM_END;
3830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
3840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_ANY_CHECK
3860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
3870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Skip over the Check field when the Check ID is not supported.
3880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Returns true once the whole Check field has been skipped over.
3890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
3900578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic bool check_skip(struct xz_dec *s, struct xz_buf *b)
3910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
3920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (s->pos < check_sizes[s->check_type]) {
3930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (b->in_pos == b->in_size)
3940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return false;
3950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
3960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++b->in_pos;
3970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		++s->pos;
3980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
3990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->pos = 0;
4010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return true;
4030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
4050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
4070578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_stream_header(struct xz_dec *s)
4080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
4100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_FORMAT_ERROR;
4110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
4130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			!= get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
4140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)
4170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
4180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
4200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Of integrity checks, we support none (Check ID = 0),
4210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * CRC32 (Check ID = 1), and optionally CRC64 (Check ID = 4).
4220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * However, if XZ_DEC_ANY_CHECK is defined, we will accept other
4230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * check types too, but then the check won't be verified and
4240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * a warning (XZ_UNSUPPORTED_CHECK) will be given.
4250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
4260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
4270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_ANY_CHECK
4290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->check_type > XZ_CHECK_MAX)
4300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
4310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type))
4330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_UNSUPPORTED_CHECK;
4340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#else
4350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type))
4360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
4370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
4380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_OK;
4400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
4430578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_stream_footer(struct xz_dec *s)
4440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
4460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
4490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
4520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Validate Backward Size. Note that we never added the size of the
4530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Index CRC32 field to s->index.size, thus we use s->index.size / 4
4540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * instead of s->index.size / 4 - 1.
4550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
4560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
4570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)
4600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
4630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Use XZ_STREAM_END instead of XZ_OK to be more convenient
4640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * for the caller.
4650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
4660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_STREAM_END;
4670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
4680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* Decode the Block Header and initialize the filter chain. */
4700578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_block_header(struct xz_dec *s)
4710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
4720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_ret ret;
4730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
4750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Validate the CRC32. We know that the temp buffer is at least
4760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * eight bytes so this is safe.
4770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
4780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.size -= 4;
4790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (xz_crc32(s->temp.buf, s->temp.size, 0)
4800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			!= get_le32(s->temp.buf + s->temp.size))
4810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
4820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.pos = 2;
4840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
4860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Catch unsupported Block Flags. We support only one or two filters
4870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * in the chain, so we catch that with the same test.
4880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
4890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
4900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[1] & 0x3E)
4910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#else
4920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[1] & 0x3F)
4930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
4940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
4950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
4960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Compressed Size */
4970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[1] & 0x40) {
4980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
4990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					!= XZ_STREAM_END)
5000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
5010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block_header.compressed = s->vli;
5030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
5040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block_header.compressed = VLI_UNKNOWN;
5050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
5060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Uncompressed Size */
5080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[1] & 0x80) {
5090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
5100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				!= XZ_STREAM_END)
5110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_DATA_ERROR;
5120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block_header.uncompressed = s->vli;
5140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
5150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->block_header.uncompressed = VLI_UNKNOWN;
5160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
5170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
5190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* If there are two filters, the first one must be a BCJ filter. */
5200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->bcj_active = s->temp.buf[1] & 0x01;
5210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->bcj_active) {
5220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->temp.size - s->temp.pos < 2)
5230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_OPTIONS_ERROR;
5240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
5260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (ret != XZ_OK)
5270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return ret;
5280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		/*
5300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * We don't support custom start offset,
5310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 * so Size of Properties must be zero.
5320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		 */
5330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->temp.buf[s->temp.pos++] != 0x00)
5340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_OPTIONS_ERROR;
5350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
5360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
5370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Valid Filter Flags always take at least two bytes. */
5390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.size - s->temp.pos < 2)
5400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
5410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Filter ID = LZMA2 */
5430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[s->temp.pos++] != 0x21)
5440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
5450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Size of Properties = 1-byte Filter Properties */
5470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.buf[s->temp.pos++] != 0x01)
5480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_OPTIONS_ERROR;
5490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Filter Properties contains LZMA2 dictionary size. */
5510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->temp.size - s->temp.pos < 1)
5520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return XZ_DATA_ERROR;
5530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
5550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (ret != XZ_OK)
5560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return ret;
5570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* The rest must be Header Padding. */
5590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (s->temp.pos < s->temp.size)
5600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->temp.buf[s->temp.pos++] != 0x00)
5610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return XZ_OPTIONS_ERROR;
5620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.pos = 0;
5640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->block.compressed = 0;
5650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->block.uncompressed = 0;
5660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return XZ_OK;
5680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
5690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5700578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncstatic enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
5710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
5720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_ret ret;
5730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/*
5750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * Store the start position for the case when we are in the middle
5760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 * of the Index field.
5770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	 */
5780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->in_start = b->in_pos;
5790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	while (true) {
5810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		switch (s->sequence) {
5820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_STREAM_HEADER:
5830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
5840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Stream Header is copied to s->temp, and then
5850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * decoded from there. This way if the caller
5860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * gives us only little input at a time, we can
5870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * still keep the Stream Header decoding code
5880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * simple. Similar approach is used in many places
5890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * in this file.
5900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
5910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!fill_temp(s, b))
5920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
5930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
5940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
5950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * If dec_stream_header() returns
5960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * XZ_UNSUPPORTED_CHECK, it is still possible
5970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * to continue decoding if working in multi-call
5980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * mode. Thus, update s->sequence before calling
5990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * dec_stream_header().
6000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
6010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_START;
6020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = dec_stream_header(s);
6040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (ret != XZ_OK)
6050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return ret;
6060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_BLOCK_START:
6080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/* We need one byte of input to continue. */
6090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (b->in_pos == b->in_size)
6100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
6110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/* See if this is the beginning of the Index field. */
6130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (b->in[b->in_pos] == 0) {
6140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->in_start = b->in_pos++;
6150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				s->sequence = SEQ_INDEX;
6160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				break;
6170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
6200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Calculate the size of the Block Header and
6210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * prepare to decode it.
6220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
6230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->block_header.size
6240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				= ((uint32_t)b->in[b->in_pos] + 1) * 4;
6250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->temp.size = s->block_header.size;
6270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->temp.pos = 0;
6280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_HEADER;
6290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_BLOCK_HEADER:
6310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!fill_temp(s, b))
6320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
6330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = dec_block_header(s);
6350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (ret != XZ_OK)
6360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return ret;
6370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_UNCOMPRESS;
6390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_BLOCK_UNCOMPRESS:
6410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = dec_block(s, b);
6420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (ret != XZ_STREAM_END)
6430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return ret;
6440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_PADDING;
6460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_BLOCK_PADDING:
6480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/*
6490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * Size of Compressed Data + Block Padding
6500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * must be a multiple of four. We don't need
6510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * s->block.compressed for anything else
6520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * anymore, so we use it here to test the size
6530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 * of the Block Padding field.
6540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			 */
6550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			while (s->block.compressed & 3) {
6560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (b->in_pos == b->in_size)
6570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_OK;
6580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (b->in[b->in_pos++] != 0)
6600578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_DATA_ERROR;
6610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				++s->block.compressed;
6630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_CHECK;
6660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_BLOCK_CHECK:
6680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (s->check_type == XZ_CHECK_CRC32) {
6690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				ret = crc_validate(s, b, 32);
6700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (ret != XZ_STREAM_END)
6710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return ret;
6720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			else if (IS_CRC64(s->check_type)) {
6740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				ret = crc_validate(s, b, 64);
6750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (ret != XZ_STREAM_END)
6760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return ret;
6770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_ANY_CHECK
6790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			else if (!check_skip(s, b)) {
6800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
6810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
6820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
6830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_BLOCK_START;
6850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			break;
6860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX:
6880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = dec_index(s, b);
6890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (ret != XZ_STREAM_END)
6900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return ret;
6910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_INDEX_PADDING;
6930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
6940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX_PADDING:
6950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			while ((s->index.size + (b->in_pos - s->in_start))
6960578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					& 3) {
6970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (b->in_pos == b->in_size) {
6980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					index_update(s, b);
6990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_OK;
7000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				}
7010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				if (b->in[b->in_pos++] != 0)
7030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					return XZ_DATA_ERROR;
7040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			}
7050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/* Finish the CRC32 value and Index size. */
7070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			index_update(s, b);
7080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			/* Compare the hashes to validate the Index field. */
7100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!memeq(&s->block.hash, &s->index.hash,
7110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					sizeof(s->block.hash)))
7120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_DATA_ERROR;
7130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_INDEX_CRC32;
7150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_INDEX_CRC32:
7170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = crc_validate(s, b, 32);
7180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (ret != XZ_STREAM_END)
7190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return ret;
7200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->temp.size = STREAM_HEADER_SIZE;
7220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			s->sequence = SEQ_STREAM_FOOTER;
7230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		case SEQ_STREAM_FOOTER:
7250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			if (!fill_temp(s, b))
7260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync				return XZ_OK;
7270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			return dec_stream_footer(s);
7290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
7300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
7310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	/* Never reached */
7330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
7340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/*
7360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
7370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * multi-call and single-call decoding.
7380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
7390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
7400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * are not going to make any progress anymore. This is to prevent the caller
7410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * from calling us infinitely when the input file is truncated or otherwise
7420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * corrupt. Since zlib-style API allows that the caller fills the input buffer
7430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * only when the decoder doesn't produce any new output, we have to be careful
7440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
7450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * after the second consecutive call to xz_dec_run() that makes no progress.
7460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
7470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * In single-call mode, if we couldn't decode everything and no error
7480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * occurred, either the input is truncated or the output buffer is too small.
7490578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Since we know that the last input byte never produces any output, we know
7500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * that if all the input was consumed and decoding wasn't finished, the file
7510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * must be corrupt. Otherwise the output buffer has to be too small or the
7520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * file is corrupt in a way that decoding it produces too big output.
7530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync *
7540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
7550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * their original values. This is because with some filter chains there won't
7560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * be any valid uncompressed data in the output buffer unless the decoding
7570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * actually succeeds (that's the price to pay of using the output buffer as
7580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * the workspace).
7590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */
7600578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
7610578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
7620578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t in_start;
7630578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	size_t out_start;
7640578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	enum xz_ret ret;
7650578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7660578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_SINGLE(s->mode))
7670578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		xz_dec_reset(s);
7680578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7690578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	in_start = b->in_pos;
7700578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	out_start = b->out_pos;
7710578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	ret = dec_main(s, b);
7720578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7730578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (DEC_IS_SINGLE(s->mode)) {
7740578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (ret == XZ_OK)
7750578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = b->in_pos == b->in_size
7760578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync					? XZ_DATA_ERROR : XZ_BUF_ERROR;
7770578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7780578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (ret != XZ_STREAM_END) {
7790578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			b->in_pos = in_start;
7800578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			b->out_pos = out_start;
7810578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		}
7820578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7830578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else if (ret == XZ_OK && in_start == b->in_pos
7840578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			&& out_start == b->out_pos) {
7850578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		if (s->allow_buf_error)
7860578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync			ret = XZ_BUF_ERROR;
7870578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7880578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->allow_buf_error = true;
7890578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	} else {
7900578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		s->allow_buf_error = false;
7910578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
7920578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7930578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return ret;
7940578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
7950578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
7960578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)
7970578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
7980578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);
7990578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s == NULL)
8000578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		return NULL;
8010578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8020578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->mode = mode;
8030578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8040578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
8050578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));
8060578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->bcj == NULL)
8070578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		goto error_bcj;
8080578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
8090578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->lzma2 = xz_dec_lzma2_create(mode, dict_max);
8110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s->lzma2 == NULL)
8120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		goto error_lzma2;
8130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	xz_dec_reset(s);
8150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return s;
8160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8170578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncerror_lzma2:
8180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
8190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	xz_dec_bcj_end(s->bcj);
8200578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncerror_bcj:
8210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
8220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	kfree(s);
8230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	return NULL;
8240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
8250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8260578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN void xz_dec_reset(struct xz_dec *s)
8270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
8280578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->sequence = SEQ_STREAM_HEADER;
8290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->allow_buf_error = false;
8300578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->pos = 0;
8310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->crc = 0;
8320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	memzero(&s->block, sizeof(s->block));
8330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	memzero(&s->index, sizeof(s->index));
8340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.pos = 0;
8350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	s->temp.size = STREAM_HEADER_SIZE;
8360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
8370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync
8380578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN void xz_dec_end(struct xz_dec *s)
8390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{
8400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	if (s != NULL) {
8410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		xz_dec_lzma2_end(s->lzma2);
8420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifdef XZ_DEC_BCJ
8430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		xz_dec_bcj_end(s->bcj);
8440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif
8450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync		kfree(s);
8460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync	}
8470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync}
848