Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * .xz Stream decoder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Lasse Collin <lasse.collin@tukaani.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This file has been put into the public domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * You can do whatever you want with this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "xz_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "xz_stream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /* Hash used to validate the Index field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) struct xz_dec_hash {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	vli_type unpadded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	vli_type uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	uint32_t crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct xz_dec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	/* Position in dec_main() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		SEQ_STREAM_HEADER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		SEQ_BLOCK_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		SEQ_BLOCK_HEADER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		SEQ_BLOCK_UNCOMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		SEQ_BLOCK_PADDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		SEQ_BLOCK_CHECK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		SEQ_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		SEQ_INDEX_PADDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		SEQ_INDEX_CRC32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		SEQ_STREAM_FOOTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	} sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/* Position in variable-length integers and Check fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	uint32_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	/* Variable-length integer decoded by dec_vli() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	vli_type vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* Saved in_pos and out_pos */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	size_t in_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	size_t out_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	/* CRC32 value in Block or Index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	uint32_t crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* Type of the integrity check calculated from uncompressed data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	enum xz_check check_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	/* Operation mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	enum xz_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 * True if the next call to xz_dec_run() is allowed to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 * XZ_BUF_ERROR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	bool allow_buf_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* Information stored in Block Header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		 * Value stored in the Compressed Size field, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		 * VLI_UNKNOWN if Compressed Size is not present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		vli_type compressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		 * Value stored in the Uncompressed Size field, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		 * VLI_UNKNOWN if Uncompressed Size is not present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		vli_type uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		/* Size of the Block Header field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		uint32_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	} block_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/* Information collected when decoding Blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		/* Observed compressed size of the current Block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		vli_type compressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* Observed uncompressed size of the current Block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		vli_type uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		/* Number of Blocks decoded so far */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		vli_type count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		 * Hash calculated from the Block sizes. This is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		 * validate the Index field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		struct xz_dec_hash hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	} block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* Variables needed when verifying the Index field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		/* Position in dec_index() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			SEQ_INDEX_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			SEQ_INDEX_UNPADDED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			SEQ_INDEX_UNCOMPRESSED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		} sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		/* Size of the Index in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		vli_type size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		/* Number of Records (matches block.count in valid files) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		vli_type count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		 * Hash calculated from the Records (matches block.hash in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		 * valid files).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		struct xz_dec_hash hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	} index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * Temporary buffer needed to hold Stream Header, Block Header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * and Stream Footer. The Block Header is the biggest (1 KiB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * so we reserve space according to that. buf[] has to be aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * to a multiple of four bytes; the size_t variables before it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 * should guarantee this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		size_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		uint8_t buf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	} temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct xz_dec_lzma2 *lzma2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct xz_dec_bcj *bcj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	bool bcj_active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #ifdef XZ_DEC_ANY_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Sizes of the Check field with different Check IDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const uint8_t check_sizes[16] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	4, 4, 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	8, 8, 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	16, 16, 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	32, 32, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	64, 64, 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * must have set s->temp.pos to indicate how much data we are supposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * to copy into s->temp.buf. Return true once s->temp.pos has reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * s->temp.size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	size_t copy_size = min_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			b->in_size - b->in_pos, s->temp.size - s->temp.pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	b->in_pos += copy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	s->temp.pos += copy_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (s->temp.pos == s->temp.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		s->temp.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Decode a variable-length integer (little-endian base-128 encoding) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			   size_t *in_pos, size_t in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	uint8_t byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (s->pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		s->vli = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	while (*in_pos < in_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		byte = in[*in_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		++*in_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		s->vli |= (vli_type)(byte & 0x7F) << s->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		if ((byte & 0x80) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			/* Don't allow non-minimal encodings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			if (byte == 0 && s->pos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			s->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			return XZ_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		s->pos += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		if (s->pos == 7 * VLI_BYTES_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * Decode the Compressed Data field from a Block. Update and validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * the observed compressed and uncompressed sizes of the Block so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * they don't exceed the values possibly stored in the Block Header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * (validation assumes that no integer overflow occurs, since vli_type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * is normally uint64_t). Update the CRC32 if presence of the CRC32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * field was indicated in Stream Header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * Once the decoding is finished, validate that the observed sizes match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * the sizes possibly stored in the Block Header. Update the hash and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * Block count, which are later used to validate the Index field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	enum xz_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	s->in_start = b->in_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	s->out_start = b->out_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (s->bcj_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ret = xz_dec_lzma2_run(s->lzma2, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	s->block.compressed += b->in_pos - s->in_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	s->block.uncompressed += b->out_pos - s->out_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * There is no need to separately check for VLI_UNKNOWN, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * the observed sizes are always smaller than VLI_UNKNOWN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (s->block.compressed > s->block_header.compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			|| s->block.uncompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				> s->block_header.uncompressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (s->check_type == XZ_CHECK_CRC32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		s->crc32 = xz_crc32(b->out + s->out_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				b->out_pos - s->out_start, s->crc32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (ret == XZ_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (s->block_header.compressed != VLI_UNKNOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				&& s->block_header.compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 					!= s->block.compressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (s->block_header.uncompressed != VLI_UNKNOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				&& s->block_header.uncompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 					!= s->block.uncompressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		s->block.hash.unpadded += s->block_header.size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				+ s->block.compressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #ifdef XZ_DEC_ANY_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		s->block.hash.unpadded += check_sizes[s->check_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (s->check_type == XZ_CHECK_CRC32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			s->block.hash.unpadded += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		s->block.hash.uncompressed += s->block.uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		s->block.hash.crc32 = xz_crc32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				(const uint8_t *)&s->block.hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				sizeof(s->block.hash), s->block.hash.crc32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		++s->block.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* Update the Index size and the CRC32 value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static void index_update(struct xz_dec *s, const struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	size_t in_used = b->in_pos - s->in_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	s->index.size += in_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * Decode the Number of Records, Unpadded Size, and Uncompressed Size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * fields from the Index field. That is, Index Padding and CRC32 are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * decoded by this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	enum xz_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		if (ret != XZ_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			index_update(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		switch (s->index.sequence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		case SEQ_INDEX_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			s->index.count = s->vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			 * Validate that the Number of Records field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			 * indicates the same number of Records as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			 * there were Blocks in the Stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			if (s->index.count != s->block.count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			s->index.sequence = SEQ_INDEX_UNPADDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		case SEQ_INDEX_UNPADDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			s->index.hash.unpadded += s->vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		case SEQ_INDEX_UNCOMPRESSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			s->index.hash.uncompressed += s->vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			s->index.hash.crc32 = xz_crc32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 					(const uint8_t *)&s->index.hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 					sizeof(s->index.hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 					s->index.hash.crc32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			--s->index.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			s->index.sequence = SEQ_INDEX_UNPADDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	} while (s->index.count > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return XZ_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * Validate that the next four input bytes match the value of s->crc32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * s->pos must be zero when starting to validate the first byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static enum xz_ret crc32_validate(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (b->in_pos == b->in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		s->pos += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	} while (s->pos < 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	s->crc32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	s->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return XZ_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #ifdef XZ_DEC_ANY_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * Skip over the Check field when the Check ID is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * Returns true once the whole Check field has been skipped over.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static bool check_skip(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	while (s->pos < check_sizes[s->check_type]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (b->in_pos == b->in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		++b->in_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		++s->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	s->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static enum xz_ret dec_stream_header(struct xz_dec *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return XZ_FORMAT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			!= get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	 * Of integrity checks, we support only none (Check ID = 0) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	 * CRC32 (Check ID = 1). However, if XZ_DEC_ANY_CHECK is defined,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	 * we will accept other check types too, but then the check won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	 * be verified and a warning (XZ_UNSUPPORTED_CHECK) will be given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (s->temp.buf[HEADER_MAGIC_SIZE + 1] > XZ_CHECK_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) #ifdef XZ_DEC_ANY_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (s->check_type > XZ_CHECK_CRC32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return XZ_UNSUPPORTED_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (s->check_type > XZ_CHECK_CRC32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static enum xz_ret dec_stream_footer(struct xz_dec *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * Validate Backward Size. Note that we never added the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 * Index CRC32 field to s->index.size, thus we use s->index.size / 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * instead of s->index.size / 4 - 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	 * Use XZ_STREAM_END instead of XZ_OK to be more convenient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	 * for the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	return XZ_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /* Decode the Block Header and initialize the filter chain. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static enum xz_ret dec_block_header(struct xz_dec *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	enum xz_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * Validate the CRC32. We know that the temp buffer is at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 * eight bytes so this is safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	s->temp.size -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (xz_crc32(s->temp.buf, s->temp.size, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			!= get_le32(s->temp.buf + s->temp.size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	s->temp.pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	 * Catch unsupported Block Flags. We support only one or two filters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	 * in the chain, so we catch that with the same test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (s->temp.buf[1] & 0x3E)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (s->temp.buf[1] & 0x3F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	/* Compressed Size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (s->temp.buf[1] & 0x40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 					!= XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		s->block_header.compressed = s->vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		s->block_header.compressed = VLI_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	/* Uncompressed Size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (s->temp.buf[1] & 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				!= XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		s->block_header.uncompressed = s->vli;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		s->block_header.uncompressed = VLI_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	/* If there are two filters, the first one must be a BCJ filter. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	s->bcj_active = s->temp.buf[1] & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (s->bcj_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (s->temp.size - s->temp.pos < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (ret != XZ_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		 * We don't support custom start offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		 * so Size of Properties must be zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		if (s->temp.buf[s->temp.pos++] != 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 			return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/* Valid Filter Flags always take at least two bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (s->temp.size - s->temp.pos < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	/* Filter ID = LZMA2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (s->temp.buf[s->temp.pos++] != 0x21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/* Size of Properties = 1-byte Filter Properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (s->temp.buf[s->temp.pos++] != 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	/* Filter Properties contains LZMA2 dictionary size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (s->temp.size - s->temp.pos < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (ret != XZ_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	/* The rest must be Header Padding. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	while (s->temp.pos < s->temp.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		if (s->temp.buf[s->temp.pos++] != 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			return XZ_OPTIONS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	s->temp.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	s->block.compressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	s->block.uncompressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	enum xz_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	 * Store the start position for the case when we are in the middle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	 * of the Index field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	s->in_start = b->in_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		switch (s->sequence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		case SEQ_STREAM_HEADER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			 * Stream Header is copied to s->temp, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			 * decoded from there. This way if the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			 * gives us only little input at a time, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			 * still keep the Stream Header decoding code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			 * simple. Similar approach is used in many places
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			 * in this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			if (!fill_temp(s, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 				return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			 * If dec_stream_header() returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			 * XZ_UNSUPPORTED_CHECK, it is still possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			 * to continue decoding if working in multi-call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			 * mode. Thus, update s->sequence before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			 * dec_stream_header().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			s->sequence = SEQ_BLOCK_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			ret = dec_stream_header(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			if (ret != XZ_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		case SEQ_BLOCK_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			/* We need one byte of input to continue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			if (b->in_pos == b->in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 				return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			/* See if this is the beginning of the Index field. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			if (b->in[b->in_pos] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 				s->in_start = b->in_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 				s->sequence = SEQ_INDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 			 * Calculate the size of the Block Header and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			 * prepare to decode it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			s->block_header.size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 				= ((uint32_t)b->in[b->in_pos] + 1) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			s->temp.size = s->block_header.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			s->temp.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			s->sequence = SEQ_BLOCK_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		case SEQ_BLOCK_HEADER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			if (!fill_temp(s, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 				return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			ret = dec_block_header(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			if (ret != XZ_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 			s->sequence = SEQ_BLOCK_UNCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		case SEQ_BLOCK_UNCOMPRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			ret = dec_block(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			if (ret != XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 			s->sequence = SEQ_BLOCK_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		case SEQ_BLOCK_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			 * Size of Compressed Data + Block Padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			 * must be a multiple of four. We don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			 * s->block.compressed for anything else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			 * anymore, so we use it here to test the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			 * of the Block Padding field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			while (s->block.compressed & 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 				if (b->in_pos == b->in_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 					return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 				if (b->in[b->in_pos++] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 					return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 				++s->block.compressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			s->sequence = SEQ_BLOCK_CHECK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		case SEQ_BLOCK_CHECK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			if (s->check_type == XZ_CHECK_CRC32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 				ret = crc32_validate(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 				if (ret != XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) #ifdef XZ_DEC_ANY_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 			else if (!check_skip(s, b)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 				return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			s->sequence = SEQ_BLOCK_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		case SEQ_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 			ret = dec_index(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			if (ret != XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 			s->sequence = SEQ_INDEX_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		case SEQ_INDEX_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 			while ((s->index.size + (b->in_pos - s->in_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 					& 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 				if (b->in_pos == b->in_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 					index_update(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 					return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 				if (b->in[b->in_pos++] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 					return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 			/* Finish the CRC32 value and Index size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 			index_update(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			/* Compare the hashes to validate the Index field. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			if (!memeq(&s->block.hash, &s->index.hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 					sizeof(s->block.hash)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 				return XZ_DATA_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			s->sequence = SEQ_INDEX_CRC32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		case SEQ_INDEX_CRC32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			ret = crc32_validate(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			if (ret != XZ_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			s->temp.size = STREAM_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			s->sequence = SEQ_STREAM_FOOTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		case SEQ_STREAM_FOOTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 			if (!fill_temp(s, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 				return XZ_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			return dec_stream_footer(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	/* Never reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)  * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)  * multi-call and single-call decoding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)  * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)  * are not going to make any progress anymore. This is to prevent the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)  * from calling us infinitely when the input file is truncated or otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)  * corrupt. Since zlib-style API allows that the caller fills the input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)  * only when the decoder doesn't produce any new output, we have to be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)  * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)  * after the second consecutive call to xz_dec_run() that makes no progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)  * In single-call mode, if we couldn't decode everything and no error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)  * occurred, either the input is truncated or the output buffer is too small.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)  * Since we know that the last input byte never produces any output, we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)  * that if all the input was consumed and decoding wasn't finished, the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)  * must be corrupt. Otherwise the output buffer has to be too small or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)  * file is corrupt in a way that decoding it produces too big output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)  * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)  * their original values. This is because with some filter chains there won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)  * be any valid uncompressed data in the output buffer unless the decoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)  * actually succeeds (that's the price to pay of using the output buffer as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)  * the workspace).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	size_t in_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	size_t out_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	enum xz_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	if (DEC_IS_SINGLE(s->mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		xz_dec_reset(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	in_start = b->in_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	out_start = b->out_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	ret = dec_main(s, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	if (DEC_IS_SINGLE(s->mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		if (ret == XZ_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 			ret = b->in_pos == b->in_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 					? XZ_DATA_ERROR : XZ_BUF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		if (ret != XZ_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 			b->in_pos = in_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 			b->out_pos = out_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	} else if (ret == XZ_OK && in_start == b->in_pos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			&& out_start == b->out_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		if (s->allow_buf_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 			ret = XZ_BUF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		s->allow_buf_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		s->allow_buf_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	if (s == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	s->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	if (s->bcj == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		goto error_bcj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	s->lzma2 = xz_dec_lzma2_create(mode, dict_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	if (s->lzma2 == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		goto error_lzma2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	xz_dec_reset(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) error_lzma2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	xz_dec_bcj_end(s->bcj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) error_bcj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	kfree(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) XZ_EXTERN void xz_dec_reset(struct xz_dec *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	s->sequence = SEQ_STREAM_HEADER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	s->allow_buf_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	s->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	s->crc32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	memzero(&s->block, sizeof(s->block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	memzero(&s->index, sizeof(s->index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	s->temp.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	s->temp.size = STREAM_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) XZ_EXTERN void xz_dec_end(struct xz_dec *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 		xz_dec_lzma2_end(s->lzma2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) #ifdef XZ_DEC_BCJ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		xz_dec_bcj_end(s->bcj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		kfree(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) }