xref: /aosp_15_r20/external/xz-embedded/linux/lib/xz/xz_private.h (revision d2c16535d139cb185e89120452531bba6b36d3c6)
1*d2c16535SElliott Hughes /* SPDX-License-Identifier: 0BSD */
2*d2c16535SElliott Hughes 
3*d2c16535SElliott Hughes /*
4*d2c16535SElliott Hughes  * Private includes and definitions
5*d2c16535SElliott Hughes  *
6*d2c16535SElliott Hughes  * Author: Lasse Collin <[email protected]>
7*d2c16535SElliott Hughes  */
8*d2c16535SElliott Hughes 
9*d2c16535SElliott Hughes #ifndef XZ_PRIVATE_H
10*d2c16535SElliott Hughes #define XZ_PRIVATE_H
11*d2c16535SElliott Hughes 
12*d2c16535SElliott Hughes #ifdef __KERNEL__
13*d2c16535SElliott Hughes #	include <linux/xz.h>
14*d2c16535SElliott Hughes #	include <linux/kernel.h>
15*d2c16535SElliott Hughes #	include <asm/unaligned.h>
16*d2c16535SElliott Hughes 	/* XZ_PREBOOT may be defined only via decompress_unxz.c. */
17*d2c16535SElliott Hughes #	ifndef XZ_PREBOOT
18*d2c16535SElliott Hughes #		include <linux/slab.h>
19*d2c16535SElliott Hughes #		include <linux/vmalloc.h>
20*d2c16535SElliott Hughes #		include <linux/string.h>
21*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_X86
22*d2c16535SElliott Hughes #			define XZ_DEC_X86
23*d2c16535SElliott Hughes #		endif
24*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_POWERPC
25*d2c16535SElliott Hughes #			define XZ_DEC_POWERPC
26*d2c16535SElliott Hughes #		endif
27*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_IA64
28*d2c16535SElliott Hughes #			define XZ_DEC_IA64
29*d2c16535SElliott Hughes #		endif
30*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_ARM
31*d2c16535SElliott Hughes #			define XZ_DEC_ARM
32*d2c16535SElliott Hughes #		endif
33*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_ARMTHUMB
34*d2c16535SElliott Hughes #			define XZ_DEC_ARMTHUMB
35*d2c16535SElliott Hughes #		endif
36*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_SPARC
37*d2c16535SElliott Hughes #			define XZ_DEC_SPARC
38*d2c16535SElliott Hughes #		endif
39*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_ARM64
40*d2c16535SElliott Hughes #			define XZ_DEC_ARM64
41*d2c16535SElliott Hughes #		endif
42*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_RISCV
43*d2c16535SElliott Hughes #			define XZ_DEC_RISCV
44*d2c16535SElliott Hughes #		endif
45*d2c16535SElliott Hughes #		ifdef CONFIG_XZ_DEC_MICROLZMA
46*d2c16535SElliott Hughes #			define XZ_DEC_MICROLZMA
47*d2c16535SElliott Hughes #		endif
48*d2c16535SElliott Hughes #		define memeq(a, b, size) (memcmp(a, b, size) == 0)
49*d2c16535SElliott Hughes #		define memzero(buf, size) memset(buf, 0, size)
50*d2c16535SElliott Hughes #	endif
51*d2c16535SElliott Hughes #	define get_le32(p) le32_to_cpup((const uint32_t *)(p))
52*d2c16535SElliott Hughes #else
53*d2c16535SElliott Hughes 	/*
54*d2c16535SElliott Hughes 	 * For userspace builds, use a separate header to define the required
55*d2c16535SElliott Hughes 	 * macros and functions. This makes it easier to adapt the code into
56*d2c16535SElliott Hughes 	 * different environments and avoids clutter in the Linux kernel tree.
57*d2c16535SElliott Hughes 	 */
58*d2c16535SElliott Hughes #	include "xz_config.h"
59*d2c16535SElliott Hughes #endif
60*d2c16535SElliott Hughes 
61*d2c16535SElliott Hughes /* If no specific decoding mode is requested, enable support for all modes. */
62*d2c16535SElliott Hughes #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
63*d2c16535SElliott Hughes 		&& !defined(XZ_DEC_DYNALLOC)
64*d2c16535SElliott Hughes #	define XZ_DEC_SINGLE
65*d2c16535SElliott Hughes #	define XZ_DEC_PREALLOC
66*d2c16535SElliott Hughes #	define XZ_DEC_DYNALLOC
67*d2c16535SElliott Hughes #endif
68*d2c16535SElliott Hughes 
69*d2c16535SElliott Hughes /*
70*d2c16535SElliott Hughes  * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
71*d2c16535SElliott Hughes  * of the supported modes are enabled, these macros will evaluate to true or
72*d2c16535SElliott Hughes  * false at compile time and thus allow the compiler to omit unneeded code.
73*d2c16535SElliott Hughes  */
74*d2c16535SElliott Hughes #ifdef XZ_DEC_SINGLE
75*d2c16535SElliott Hughes #	define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
76*d2c16535SElliott Hughes #else
77*d2c16535SElliott Hughes #	define DEC_IS_SINGLE(mode) (false)
78*d2c16535SElliott Hughes #endif
79*d2c16535SElliott Hughes 
80*d2c16535SElliott Hughes #ifdef XZ_DEC_PREALLOC
81*d2c16535SElliott Hughes #	define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
82*d2c16535SElliott Hughes #else
83*d2c16535SElliott Hughes #	define DEC_IS_PREALLOC(mode) (false)
84*d2c16535SElliott Hughes #endif
85*d2c16535SElliott Hughes 
86*d2c16535SElliott Hughes #ifdef XZ_DEC_DYNALLOC
87*d2c16535SElliott Hughes #	define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
88*d2c16535SElliott Hughes #else
89*d2c16535SElliott Hughes #	define DEC_IS_DYNALLOC(mode) (false)
90*d2c16535SElliott Hughes #endif
91*d2c16535SElliott Hughes 
92*d2c16535SElliott Hughes #if !defined(XZ_DEC_SINGLE)
93*d2c16535SElliott Hughes #	define DEC_IS_MULTI(mode) (true)
94*d2c16535SElliott Hughes #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
95*d2c16535SElliott Hughes #	define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
96*d2c16535SElliott Hughes #else
97*d2c16535SElliott Hughes #	define DEC_IS_MULTI(mode) (false)
98*d2c16535SElliott Hughes #endif
99*d2c16535SElliott Hughes 
100*d2c16535SElliott Hughes /*
101*d2c16535SElliott Hughes  * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
102*d2c16535SElliott Hughes  * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
103*d2c16535SElliott Hughes  */
104*d2c16535SElliott Hughes #ifndef XZ_DEC_BCJ
105*d2c16535SElliott Hughes #	if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
106*d2c16535SElliott Hughes 			|| defined(XZ_DEC_IA64) \
107*d2c16535SElliott Hughes 			|| defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
108*d2c16535SElliott Hughes 			|| defined(XZ_DEC_SPARC) || defined(XZ_DEC_ARM64) \
109*d2c16535SElliott Hughes 			|| defined(XZ_DEC_RISCV)
110*d2c16535SElliott Hughes #		define XZ_DEC_BCJ
111*d2c16535SElliott Hughes #	endif
112*d2c16535SElliott Hughes #endif
113*d2c16535SElliott Hughes 
114*d2c16535SElliott Hughes /*
115*d2c16535SElliott Hughes  * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
116*d2c16535SElliott Hughes  * before calling xz_dec_lzma2_run().
117*d2c16535SElliott Hughes  */
118*d2c16535SElliott Hughes XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
119*d2c16535SElliott Hughes 						   uint32_t dict_max);
120*d2c16535SElliott Hughes 
121*d2c16535SElliott Hughes /*
122*d2c16535SElliott Hughes  * Decode the LZMA2 properties (one byte) and reset the decoder. Return
123*d2c16535SElliott Hughes  * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
124*d2c16535SElliott Hughes  * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
125*d2c16535SElliott Hughes  * decoder doesn't support.
126*d2c16535SElliott Hughes  */
127*d2c16535SElliott Hughes XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
128*d2c16535SElliott Hughes 					 uint8_t props);
129*d2c16535SElliott Hughes 
130*d2c16535SElliott Hughes /* Decode raw LZMA2 stream from b->in to b->out. */
131*d2c16535SElliott Hughes XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
132*d2c16535SElliott Hughes 				       struct xz_buf *b);
133*d2c16535SElliott Hughes 
134*d2c16535SElliott Hughes /* Free the memory allocated for the LZMA2 decoder. */
135*d2c16535SElliott Hughes XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
136*d2c16535SElliott Hughes 
137*d2c16535SElliott Hughes #ifdef XZ_DEC_BCJ
138*d2c16535SElliott Hughes /*
139*d2c16535SElliott Hughes  * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
140*d2c16535SElliott Hughes  * calling xz_dec_bcj_run().
141*d2c16535SElliott Hughes  */
142*d2c16535SElliott Hughes XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool single_call);
143*d2c16535SElliott Hughes 
144*d2c16535SElliott Hughes /*
145*d2c16535SElliott Hughes  * Decode the Filter ID of a BCJ filter. This implementation doesn't
146*d2c16535SElliott Hughes  * support custom start offsets, so no decoding of Filter Properties
147*d2c16535SElliott Hughes  * is needed. Returns XZ_OK if the given Filter ID is supported.
148*d2c16535SElliott Hughes  * Otherwise XZ_OPTIONS_ERROR is returned.
149*d2c16535SElliott Hughes  */
150*d2c16535SElliott Hughes XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
151*d2c16535SElliott Hughes 
152*d2c16535SElliott Hughes /*
153*d2c16535SElliott Hughes  * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
154*d2c16535SElliott Hughes  * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
155*d2c16535SElliott Hughes  * must be called directly.
156*d2c16535SElliott Hughes  */
157*d2c16535SElliott Hughes XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
158*d2c16535SElliott Hughes 				     struct xz_dec_lzma2 *lzma2,
159*d2c16535SElliott Hughes 				     struct xz_buf *b);
160*d2c16535SElliott Hughes 
161*d2c16535SElliott Hughes /* Free the memory allocated for the BCJ filters. */
162*d2c16535SElliott Hughes #define xz_dec_bcj_end(s) kfree(s)
163*d2c16535SElliott Hughes #endif
164*d2c16535SElliott Hughes 
165*d2c16535SElliott Hughes #endif
166