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