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