xref: /aosp_15_r20/external/xz-embedded/linux/lib/decompress_unxz.c (revision d2c16535d139cb185e89120452531bba6b36d3c6)
1*d2c16535SElliott Hughes // SPDX-License-Identifier: 0BSD
2*d2c16535SElliott Hughes 
3*d2c16535SElliott Hughes /*
4*d2c16535SElliott Hughes  * Wrapper for decompressing XZ-compressed kernel, initramfs, and initrd
5*d2c16535SElliott Hughes  *
6*d2c16535SElliott Hughes  * Author: Lasse Collin <[email protected]>
7*d2c16535SElliott Hughes  */
8*d2c16535SElliott Hughes 
9*d2c16535SElliott Hughes /*
10*d2c16535SElliott Hughes  * Important notes about in-place decompression
11*d2c16535SElliott Hughes  *
12*d2c16535SElliott Hughes  * At least on x86, the kernel is decompressed in place: the compressed data
13*d2c16535SElliott Hughes  * is placed to the end of the output buffer, and the decompressor overwrites
14*d2c16535SElliott Hughes  * most of the compressed data. There must be enough safety margin to
15*d2c16535SElliott Hughes  * guarantee that the write position is always behind the read position.
16*d2c16535SElliott Hughes  *
17*d2c16535SElliott Hughes  * The safety margin for XZ with LZMA2 or BCJ+LZMA2 is calculated below.
18*d2c16535SElliott Hughes  * Note that the margin with XZ is bigger than with Deflate (gzip)!
19*d2c16535SElliott Hughes  *
20*d2c16535SElliott Hughes  * The worst case for in-place decompression is that the beginning of
21*d2c16535SElliott Hughes  * the file is compressed extremely well, and the rest of the file is
22*d2c16535SElliott Hughes  * incompressible. Thus, we must look for worst-case expansion when the
23*d2c16535SElliott Hughes  * compressor is encoding incompressible data.
24*d2c16535SElliott Hughes  *
25*d2c16535SElliott Hughes  * The structure of the .xz file in case of a compressed kernel is as follows.
26*d2c16535SElliott Hughes  * Sizes (as bytes) of the fields are in parenthesis.
27*d2c16535SElliott Hughes  *
28*d2c16535SElliott Hughes  *    Stream Header (12)
29*d2c16535SElliott Hughes  *    Block Header:
30*d2c16535SElliott Hughes  *      Block Header (8-12)
31*d2c16535SElliott Hughes  *      Compressed Data (N)
32*d2c16535SElliott Hughes  *      Block Padding (0-3)
33*d2c16535SElliott Hughes  *      CRC32 (4)
34*d2c16535SElliott Hughes  *    Index (8-20)
35*d2c16535SElliott Hughes  *    Stream Footer (12)
36*d2c16535SElliott Hughes  *
37*d2c16535SElliott Hughes  * Normally there is exactly one Block, but let's assume that there are
38*d2c16535SElliott Hughes  * 2-4 Blocks just in case. Because Stream Header and also Block Header
39*d2c16535SElliott Hughes  * of the first Block don't make the decompressor produce any uncompressed
40*d2c16535SElliott Hughes  * data, we can ignore them from our calculations. Block Headers of possible
41*d2c16535SElliott Hughes  * additional Blocks have to be taken into account still. With these
42*d2c16535SElliott Hughes  * assumptions, it is safe to assume that the total header overhead is
43*d2c16535SElliott Hughes  * less than 128 bytes.
44*d2c16535SElliott Hughes  *
45*d2c16535SElliott Hughes  * Compressed Data contains LZMA2 or BCJ+LZMA2 encoded data. Since BCJ
46*d2c16535SElliott Hughes  * doesn't change the size of the data, it is enough to calculate the
47*d2c16535SElliott Hughes  * safety margin for LZMA2.
48*d2c16535SElliott Hughes  *
49*d2c16535SElliott Hughes  * LZMA2 stores the data in chunks. Each chunk has a header whose size is
50*d2c16535SElliott Hughes  * a maximum of 6 bytes, but to get round 2^n numbers, let's assume that
51*d2c16535SElliott Hughes  * the maximum chunk header size is 8 bytes. After the chunk header, there
52*d2c16535SElliott Hughes  * may be up to 64 KiB of actual payload in the chunk. Often the payload is
53*d2c16535SElliott Hughes  * quite a bit smaller though; to be safe, let's assume that an average
54*d2c16535SElliott Hughes  * chunk has only 32 KiB of payload.
55*d2c16535SElliott Hughes  *
56*d2c16535SElliott Hughes  * The maximum uncompressed size of the payload is 2 MiB. The minimum
57*d2c16535SElliott Hughes  * uncompressed size of the payload is in practice never less than the
58*d2c16535SElliott Hughes  * payload size itself. The LZMA2 format would allow uncompressed size
59*d2c16535SElliott Hughes  * to be less than the payload size, but no sane compressor creates such
60*d2c16535SElliott Hughes  * files. LZMA2 supports storing incompressible data in uncompressed form,
61*d2c16535SElliott Hughes  * so there's never a need to create payloads whose uncompressed size is
62*d2c16535SElliott Hughes  * smaller than the compressed size.
63*d2c16535SElliott Hughes  *
64*d2c16535SElliott Hughes  * The assumption, that the uncompressed size of the payload is never
65*d2c16535SElliott Hughes  * smaller than the payload itself, is valid only when talking about
66*d2c16535SElliott Hughes  * the payload as a whole. It is possible that the payload has parts where
67*d2c16535SElliott Hughes  * the decompressor consumes more input than it produces output. Calculating
68*d2c16535SElliott Hughes  * the worst case for this would be tricky. Instead of trying to do that,
69*d2c16535SElliott Hughes  * let's simply make sure that the decompressor never overwrites any bytes
70*d2c16535SElliott Hughes  * of the payload which it is currently reading.
71*d2c16535SElliott Hughes  *
72*d2c16535SElliott Hughes  * Now we have enough information to calculate the safety margin. We need
73*d2c16535SElliott Hughes  *   - 128 bytes for the .xz file format headers;
74*d2c16535SElliott Hughes  *   - 8 bytes per every 32 KiB of uncompressed size (one LZMA2 chunk header
75*d2c16535SElliott Hughes  *     per chunk, each chunk having average payload size of 32 KiB); and
76*d2c16535SElliott Hughes  *   - 64 KiB (biggest possible LZMA2 chunk payload size) to make sure that
77*d2c16535SElliott Hughes  *     the decompressor never overwrites anything from the LZMA2 chunk
78*d2c16535SElliott Hughes  *     payload it is currently reading.
79*d2c16535SElliott Hughes  *
80*d2c16535SElliott Hughes  * We get the following formula:
81*d2c16535SElliott Hughes  *
82*d2c16535SElliott Hughes  *    safety_margin = 128 + uncompressed_size * 8 / 32768 + 65536
83*d2c16535SElliott Hughes  *                  = 128 + (uncompressed_size >> 12) + 65536
84*d2c16535SElliott Hughes  *
85*d2c16535SElliott Hughes  * For comparison, according to arch/x86/boot/compressed/misc.c, the
86*d2c16535SElliott Hughes  * equivalent formula for Deflate is this:
87*d2c16535SElliott Hughes  *
88*d2c16535SElliott Hughes  *    safety_margin = 18 + (uncompressed_size >> 12) + 32768
89*d2c16535SElliott Hughes  *
90*d2c16535SElliott Hughes  * Thus, when updating Deflate-only in-place kernel decompressor to
91*d2c16535SElliott Hughes  * support XZ, the fixed overhead has to be increased from 18+32768 bytes
92*d2c16535SElliott Hughes  * to 128+65536 bytes.
93*d2c16535SElliott Hughes  */
94*d2c16535SElliott Hughes 
95*d2c16535SElliott Hughes /*
96*d2c16535SElliott Hughes  * STATIC is defined to "static" if we are being built for kernel
97*d2c16535SElliott Hughes  * decompression (pre-boot code). <linux/decompress/mm.h> will define
98*d2c16535SElliott Hughes  * STATIC to empty if it wasn't already defined. Since we will need to
99*d2c16535SElliott Hughes  * know later if we are being used for kernel decompression, we define
100*d2c16535SElliott Hughes  * XZ_PREBOOT here.
101*d2c16535SElliott Hughes  */
102*d2c16535SElliott Hughes #ifdef STATIC
103*d2c16535SElliott Hughes #	define XZ_PREBOOT
104*d2c16535SElliott Hughes #else
105*d2c16535SElliott Hughes #	include <linux/decompress/unxz.h>
106*d2c16535SElliott Hughes #endif
107*d2c16535SElliott Hughes #ifdef __KERNEL__
108*d2c16535SElliott Hughes #	include <linux/decompress/mm.h>
109*d2c16535SElliott Hughes #endif
110*d2c16535SElliott Hughes #define XZ_EXTERN STATIC
111*d2c16535SElliott Hughes 
112*d2c16535SElliott Hughes #ifndef XZ_PREBOOT
113*d2c16535SElliott Hughes #	include <linux/slab.h>
114*d2c16535SElliott Hughes #	include <linux/xz.h>
115*d2c16535SElliott Hughes #else
116*d2c16535SElliott Hughes /*
117*d2c16535SElliott Hughes  * Use the internal CRC32 code instead of kernel's CRC32 module, which
118*d2c16535SElliott Hughes  * is not available in early phase of booting.
119*d2c16535SElliott Hughes  */
120*d2c16535SElliott Hughes #define XZ_INTERNAL_CRC32 1
121*d2c16535SElliott Hughes 
122*d2c16535SElliott Hughes /*
123*d2c16535SElliott Hughes  * For boot time use, we enable only the BCJ filter of the current
124*d2c16535SElliott Hughes  * architecture or none if no BCJ filter is available for the architecture.
125*d2c16535SElliott Hughes  */
126*d2c16535SElliott Hughes #ifdef CONFIG_X86
127*d2c16535SElliott Hughes #	define XZ_DEC_X86
128*d2c16535SElliott Hughes #endif
129*d2c16535SElliott Hughes #if defined(CONFIG_PPC) && defined(CONFIG_CPU_BIG_ENDIAN)
130*d2c16535SElliott Hughes #	define XZ_DEC_POWERPC
131*d2c16535SElliott Hughes #endif
132*d2c16535SElliott Hughes #ifdef CONFIG_ARM
133*d2c16535SElliott Hughes #	ifdef CONFIG_THUMB2_KERNEL
134*d2c16535SElliott Hughes #		define XZ_DEC_ARMTHUMB
135*d2c16535SElliott Hughes #	else
136*d2c16535SElliott Hughes #		define XZ_DEC_ARM
137*d2c16535SElliott Hughes #	endif
138*d2c16535SElliott Hughes #endif
139*d2c16535SElliott Hughes #ifdef CONFIG_ARM64
140*d2c16535SElliott Hughes #	define XZ_DEC_ARM64
141*d2c16535SElliott Hughes #endif
142*d2c16535SElliott Hughes #ifdef CONFIG_RISCV
143*d2c16535SElliott Hughes #	define XZ_DEC_RISCV
144*d2c16535SElliott Hughes #endif
145*d2c16535SElliott Hughes #ifdef CONFIG_SPARC
146*d2c16535SElliott Hughes #	define XZ_DEC_SPARC
147*d2c16535SElliott Hughes #endif
148*d2c16535SElliott Hughes 
149*d2c16535SElliott Hughes /*
150*d2c16535SElliott Hughes  * This will get the basic headers so that memeq() and others
151*d2c16535SElliott Hughes  * can be defined.
152*d2c16535SElliott Hughes  */
153*d2c16535SElliott Hughes #include "xz/xz_private.h"
154*d2c16535SElliott Hughes 
155*d2c16535SElliott Hughes /*
156*d2c16535SElliott Hughes  * Replace the normal allocation functions with the versions from
157*d2c16535SElliott Hughes  * <linux/decompress/mm.h>. vfree() needs to support vfree(NULL)
158*d2c16535SElliott Hughes  * when XZ_DYNALLOC is used, but the pre-boot free() doesn't support it.
159*d2c16535SElliott Hughes  * Workaround it here because the other decompressors don't need it.
160*d2c16535SElliott Hughes  */
161*d2c16535SElliott Hughes #undef kmalloc
162*d2c16535SElliott Hughes #undef kfree
163*d2c16535SElliott Hughes #undef vmalloc
164*d2c16535SElliott Hughes #undef vfree
165*d2c16535SElliott Hughes #define kmalloc(size, flags) malloc(size)
166*d2c16535SElliott Hughes #define kfree(ptr) free(ptr)
167*d2c16535SElliott Hughes #define vmalloc(size) malloc(size)
168*d2c16535SElliott Hughes #define vfree(ptr) do { if (ptr != NULL) free(ptr); } while (0)
169*d2c16535SElliott Hughes 
170*d2c16535SElliott Hughes /*
171*d2c16535SElliott Hughes  * FIXME: Not all basic memory functions are provided in architecture-specific
172*d2c16535SElliott Hughes  * files (yet). We define our own versions here for now, but this should be
173*d2c16535SElliott Hughes  * only a temporary solution.
174*d2c16535SElliott Hughes  *
175*d2c16535SElliott Hughes  * memeq and memzero are not used much and any remotely sane implementation
176*d2c16535SElliott Hughes  * is fast enough. memcpy/memmove speed matters in multi-call mode, but
177*d2c16535SElliott Hughes  * the kernel image is decompressed in single-call mode, in which only
178*d2c16535SElliott Hughes  * memmove speed can matter and only if there is a lot of incompressible data
179*d2c16535SElliott Hughes  * (LZMA2 stores incompressible chunks in uncompressed form). Thus, the
180*d2c16535SElliott Hughes  * functions below should just be kept small; it's probably not worth
181*d2c16535SElliott Hughes  * optimizing for speed.
182*d2c16535SElliott Hughes  */
183*d2c16535SElliott Hughes 
184*d2c16535SElliott Hughes #ifndef memeq
memeq(const void * a,const void * b,size_t size)185*d2c16535SElliott Hughes static bool memeq(const void *a, const void *b, size_t size)
186*d2c16535SElliott Hughes {
187*d2c16535SElliott Hughes 	const uint8_t *x = a;
188*d2c16535SElliott Hughes 	const uint8_t *y = b;
189*d2c16535SElliott Hughes 	size_t i;
190*d2c16535SElliott Hughes 
191*d2c16535SElliott Hughes 	for (i = 0; i < size; ++i)
192*d2c16535SElliott Hughes 		if (x[i] != y[i])
193*d2c16535SElliott Hughes 			return false;
194*d2c16535SElliott Hughes 
195*d2c16535SElliott Hughes 	return true;
196*d2c16535SElliott Hughes }
197*d2c16535SElliott Hughes #endif
198*d2c16535SElliott Hughes 
199*d2c16535SElliott Hughes #ifndef memzero
memzero(void * buf,size_t size)200*d2c16535SElliott Hughes static void memzero(void *buf, size_t size)
201*d2c16535SElliott Hughes {
202*d2c16535SElliott Hughes 	uint8_t *b = buf;
203*d2c16535SElliott Hughes 	uint8_t *e = b + size;
204*d2c16535SElliott Hughes 
205*d2c16535SElliott Hughes 	while (b != e)
206*d2c16535SElliott Hughes 		*b++ = '\0';
207*d2c16535SElliott Hughes }
208*d2c16535SElliott Hughes #endif
209*d2c16535SElliott Hughes 
210*d2c16535SElliott Hughes #ifndef memmove
211*d2c16535SElliott Hughes /* Not static to avoid a conflict with the prototype in the Linux headers. */
memmove(void * dest,const void * src,size_t size)212*d2c16535SElliott Hughes void *memmove(void *dest, const void *src, size_t size)
213*d2c16535SElliott Hughes {
214*d2c16535SElliott Hughes 	uint8_t *d = dest;
215*d2c16535SElliott Hughes 	const uint8_t *s = src;
216*d2c16535SElliott Hughes 	size_t i;
217*d2c16535SElliott Hughes 
218*d2c16535SElliott Hughes 	if (d < s) {
219*d2c16535SElliott Hughes 		for (i = 0; i < size; ++i)
220*d2c16535SElliott Hughes 			d[i] = s[i];
221*d2c16535SElliott Hughes 	} else if (d > s) {
222*d2c16535SElliott Hughes 		i = size;
223*d2c16535SElliott Hughes 		while (i-- > 0)
224*d2c16535SElliott Hughes 			d[i] = s[i];
225*d2c16535SElliott Hughes 	}
226*d2c16535SElliott Hughes 
227*d2c16535SElliott Hughes 	return dest;
228*d2c16535SElliott Hughes }
229*d2c16535SElliott Hughes #endif
230*d2c16535SElliott Hughes 
231*d2c16535SElliott Hughes /*
232*d2c16535SElliott Hughes  * Since we need memmove anyway, we could use it as memcpy too.
233*d2c16535SElliott Hughes  * Commented out for now to avoid breaking things.
234*d2c16535SElliott Hughes  */
235*d2c16535SElliott Hughes /*
236*d2c16535SElliott Hughes #ifndef memcpy
237*d2c16535SElliott Hughes #	define memcpy memmove
238*d2c16535SElliott Hughes #endif
239*d2c16535SElliott Hughes */
240*d2c16535SElliott Hughes 
241*d2c16535SElliott Hughes #include "xz/xz_crc32.c"
242*d2c16535SElliott Hughes #include "xz/xz_dec_stream.c"
243*d2c16535SElliott Hughes #include "xz/xz_dec_lzma2.c"
244*d2c16535SElliott Hughes #include "xz/xz_dec_bcj.c"
245*d2c16535SElliott Hughes 
246*d2c16535SElliott Hughes #endif /* XZ_PREBOOT */
247*d2c16535SElliott Hughes 
248*d2c16535SElliott Hughes /* Size of the input and output buffers in multi-call mode */
249*d2c16535SElliott Hughes #define XZ_IOBUF_SIZE 4096
250*d2c16535SElliott Hughes 
251*d2c16535SElliott Hughes /*
252*d2c16535SElliott Hughes  * This function implements the API defined in <linux/decompress/generic.h>.
253*d2c16535SElliott Hughes  *
254*d2c16535SElliott Hughes  * This wrapper will automatically choose single-call or multi-call mode
255*d2c16535SElliott Hughes  * of the native XZ decoder API. The single-call mode can be used only when
256*d2c16535SElliott Hughes  * both input and output buffers are available as a single chunk, i.e. when
257*d2c16535SElliott Hughes  * fill() and flush() won't be used.
258*d2c16535SElliott Hughes  */
unxz(unsigned char * in,long in_size,long (* fill)(void * dest,unsigned long size),long (* flush)(void * src,unsigned long size),unsigned char * out,long * in_used,void (* error)(char * x))259*d2c16535SElliott Hughes STATIC int INIT unxz(unsigned char *in, long in_size,
260*d2c16535SElliott Hughes 		     long (*fill)(void *dest, unsigned long size),
261*d2c16535SElliott Hughes 		     long (*flush)(void *src, unsigned long size),
262*d2c16535SElliott Hughes 		     unsigned char *out, long *in_used,
263*d2c16535SElliott Hughes 		     void (*error)(char *x))
264*d2c16535SElliott Hughes {
265*d2c16535SElliott Hughes 	struct xz_buf b;
266*d2c16535SElliott Hughes 	struct xz_dec *s;
267*d2c16535SElliott Hughes 	enum xz_ret ret;
268*d2c16535SElliott Hughes 	bool must_free_in = false;
269*d2c16535SElliott Hughes 
270*d2c16535SElliott Hughes #if XZ_INTERNAL_CRC32
271*d2c16535SElliott Hughes 	xz_crc32_init();
272*d2c16535SElliott Hughes #endif
273*d2c16535SElliott Hughes 
274*d2c16535SElliott Hughes 	if (in_used != NULL)
275*d2c16535SElliott Hughes 		*in_used = 0;
276*d2c16535SElliott Hughes 
277*d2c16535SElliott Hughes 	if (fill == NULL && flush == NULL)
278*d2c16535SElliott Hughes 		s = xz_dec_init(XZ_SINGLE, 0);
279*d2c16535SElliott Hughes 	else
280*d2c16535SElliott Hughes 		s = xz_dec_init(XZ_DYNALLOC, (uint32_t)-1);
281*d2c16535SElliott Hughes 
282*d2c16535SElliott Hughes 	if (s == NULL)
283*d2c16535SElliott Hughes 		goto error_alloc_state;
284*d2c16535SElliott Hughes 
285*d2c16535SElliott Hughes 	if (flush == NULL) {
286*d2c16535SElliott Hughes 		b.out = out;
287*d2c16535SElliott Hughes 		b.out_size = (size_t)-1;
288*d2c16535SElliott Hughes 	} else {
289*d2c16535SElliott Hughes 		b.out_size = XZ_IOBUF_SIZE;
290*d2c16535SElliott Hughes 		b.out = malloc(XZ_IOBUF_SIZE);
291*d2c16535SElliott Hughes 		if (b.out == NULL)
292*d2c16535SElliott Hughes 			goto error_alloc_out;
293*d2c16535SElliott Hughes 	}
294*d2c16535SElliott Hughes 
295*d2c16535SElliott Hughes 	if (in == NULL) {
296*d2c16535SElliott Hughes 		must_free_in = true;
297*d2c16535SElliott Hughes 		in = malloc(XZ_IOBUF_SIZE);
298*d2c16535SElliott Hughes 		if (in == NULL)
299*d2c16535SElliott Hughes 			goto error_alloc_in;
300*d2c16535SElliott Hughes 	}
301*d2c16535SElliott Hughes 
302*d2c16535SElliott Hughes 	b.in = in;
303*d2c16535SElliott Hughes 	b.in_pos = 0;
304*d2c16535SElliott Hughes 	b.in_size = in_size;
305*d2c16535SElliott Hughes 	b.out_pos = 0;
306*d2c16535SElliott Hughes 
307*d2c16535SElliott Hughes 	if (fill == NULL && flush == NULL) {
308*d2c16535SElliott Hughes 		ret = xz_dec_run(s, &b);
309*d2c16535SElliott Hughes 	} else {
310*d2c16535SElliott Hughes 		do {
311*d2c16535SElliott Hughes 			if (b.in_pos == b.in_size && fill != NULL) {
312*d2c16535SElliott Hughes 				if (in_used != NULL)
313*d2c16535SElliott Hughes 					*in_used += b.in_pos;
314*d2c16535SElliott Hughes 
315*d2c16535SElliott Hughes 				b.in_pos = 0;
316*d2c16535SElliott Hughes 
317*d2c16535SElliott Hughes 				in_size = fill(in, XZ_IOBUF_SIZE);
318*d2c16535SElliott Hughes 				if (in_size < 0) {
319*d2c16535SElliott Hughes 					/*
320*d2c16535SElliott Hughes 					 * This isn't an optimal error code
321*d2c16535SElliott Hughes 					 * but it probably isn't worth making
322*d2c16535SElliott Hughes 					 * a new one either.
323*d2c16535SElliott Hughes 					 */
324*d2c16535SElliott Hughes 					ret = XZ_BUF_ERROR;
325*d2c16535SElliott Hughes 					break;
326*d2c16535SElliott Hughes 				}
327*d2c16535SElliott Hughes 
328*d2c16535SElliott Hughes 				b.in_size = in_size;
329*d2c16535SElliott Hughes 			}
330*d2c16535SElliott Hughes 
331*d2c16535SElliott Hughes 			ret = xz_dec_run(s, &b);
332*d2c16535SElliott Hughes 
333*d2c16535SElliott Hughes 			if (flush != NULL && (b.out_pos == b.out_size
334*d2c16535SElliott Hughes 					|| (ret != XZ_OK && b.out_pos > 0))) {
335*d2c16535SElliott Hughes 				/*
336*d2c16535SElliott Hughes 				 * Setting ret here may hide an error
337*d2c16535SElliott Hughes 				 * returned by xz_dec_run(), but probably
338*d2c16535SElliott Hughes 				 * it's not too bad.
339*d2c16535SElliott Hughes 				 */
340*d2c16535SElliott Hughes 				if (flush(b.out, b.out_pos) != (long)b.out_pos)
341*d2c16535SElliott Hughes 					ret = XZ_BUF_ERROR;
342*d2c16535SElliott Hughes 
343*d2c16535SElliott Hughes 				b.out_pos = 0;
344*d2c16535SElliott Hughes 			}
345*d2c16535SElliott Hughes 		} while (ret == XZ_OK);
346*d2c16535SElliott Hughes 
347*d2c16535SElliott Hughes 		if (must_free_in)
348*d2c16535SElliott Hughes 			free(in);
349*d2c16535SElliott Hughes 
350*d2c16535SElliott Hughes 		if (flush != NULL)
351*d2c16535SElliott Hughes 			free(b.out);
352*d2c16535SElliott Hughes 	}
353*d2c16535SElliott Hughes 
354*d2c16535SElliott Hughes 	if (in_used != NULL)
355*d2c16535SElliott Hughes 		*in_used += b.in_pos;
356*d2c16535SElliott Hughes 
357*d2c16535SElliott Hughes 	xz_dec_end(s);
358*d2c16535SElliott Hughes 
359*d2c16535SElliott Hughes 	switch (ret) {
360*d2c16535SElliott Hughes 	case XZ_STREAM_END:
361*d2c16535SElliott Hughes 		return 0;
362*d2c16535SElliott Hughes 
363*d2c16535SElliott Hughes 	case XZ_MEM_ERROR:
364*d2c16535SElliott Hughes 		/* This can occur only in multi-call mode. */
365*d2c16535SElliott Hughes 		error("XZ decompressor ran out of memory");
366*d2c16535SElliott Hughes 		break;
367*d2c16535SElliott Hughes 
368*d2c16535SElliott Hughes 	case XZ_FORMAT_ERROR:
369*d2c16535SElliott Hughes 		error("Input is not in the XZ format (wrong magic bytes)");
370*d2c16535SElliott Hughes 		break;
371*d2c16535SElliott Hughes 
372*d2c16535SElliott Hughes 	case XZ_OPTIONS_ERROR:
373*d2c16535SElliott Hughes 		error("Input was encoded with settings that are not "
374*d2c16535SElliott Hughes 				"supported by this XZ decoder");
375*d2c16535SElliott Hughes 		break;
376*d2c16535SElliott Hughes 
377*d2c16535SElliott Hughes 	case XZ_DATA_ERROR:
378*d2c16535SElliott Hughes 	case XZ_BUF_ERROR:
379*d2c16535SElliott Hughes 		error("XZ-compressed data is corrupt");
380*d2c16535SElliott Hughes 		break;
381*d2c16535SElliott Hughes 
382*d2c16535SElliott Hughes 	default:
383*d2c16535SElliott Hughes 		error("Bug in the XZ decompressor");
384*d2c16535SElliott Hughes 		break;
385*d2c16535SElliott Hughes 	}
386*d2c16535SElliott Hughes 
387*d2c16535SElliott Hughes 	return -1;
388*d2c16535SElliott Hughes 
389*d2c16535SElliott Hughes error_alloc_in:
390*d2c16535SElliott Hughes 	if (flush != NULL)
391*d2c16535SElliott Hughes 		free(b.out);
392*d2c16535SElliott Hughes 
393*d2c16535SElliott Hughes error_alloc_out:
394*d2c16535SElliott Hughes 	xz_dec_end(s);
395*d2c16535SElliott Hughes 
396*d2c16535SElliott Hughes error_alloc_state:
397*d2c16535SElliott Hughes 	error("XZ decompressor ran out of memory");
398*d2c16535SElliott Hughes 	return -1;
399*d2c16535SElliott Hughes }
400*d2c16535SElliott Hughes 
401*d2c16535SElliott Hughes /*
402*d2c16535SElliott Hughes  * This function is used by architecture-specific files to decompress
403*d2c16535SElliott Hughes  * the kernel image.
404*d2c16535SElliott Hughes  */
405*d2c16535SElliott Hughes #ifdef XZ_PREBOOT
__decompress(unsigned char * in,long in_size,long (* fill)(void * dest,unsigned long size),long (* flush)(void * src,unsigned long size),unsigned char * out,long out_size,long * in_used,void (* error)(char * x))406*d2c16535SElliott Hughes STATIC int INIT __decompress(unsigned char *in, long in_size,
407*d2c16535SElliott Hughes 			     long (*fill)(void *dest, unsigned long size),
408*d2c16535SElliott Hughes 			     long (*flush)(void *src, unsigned long size),
409*d2c16535SElliott Hughes 			     unsigned char *out, long out_size,
410*d2c16535SElliott Hughes 			     long *in_used,
411*d2c16535SElliott Hughes 			     void (*error)(char *x))
412*d2c16535SElliott Hughes {
413*d2c16535SElliott Hughes 	return unxz(in, in_size, fill, flush, out, in_used, error);
414*d2c16535SElliott Hughes }
415*d2c16535SElliott Hughes #endif
416