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 __XEN__
14 #include <xen/kernel.h>
15 #include <asm/byteorder.h>
16 #endif
17
18 #define get_le32(p) le32_to_cpup((const uint32_t *)(p))
19
20 #if 1 /* ndef CONFIG_??? */
get_unaligned_le32(void * p)21 static inline u32 INIT get_unaligned_le32(void *p)
22 {
23 return le32_to_cpup(p);
24 }
25
put_unaligned_le32(u32 val,void * p)26 static inline void INIT put_unaligned_le32(u32 val, void *p)
27 {
28 *(__force __le32*)p = cpu_to_le32(val);
29 }
30 #else
31 #include <asm/unaligned.h>
32
get_unaligned_le32(void * p)33 static inline u32 INIT get_unaligned_le32(void *p)
34 {
35 return le32_to_cpu(__get_unaligned(p, 4));
36 }
37
put_unaligned_le32(u32 val,void * p)38 static inline void INIT put_unaligned_le32(u32 val, void *p)
39 {
40 __put_unaligned(cpu_to_le32(val), p, 4);
41 }
42 #endif
43
44 #define false 0
45 #define true 1
46
47 /**
48 * enum xz_mode - Operation mode
49 *
50 * @XZ_SINGLE: Single-call mode. This uses less RAM than
51 * than multi-call modes, because the LZMA2
52 * dictionary doesn't need to be allocated as
53 * part of the decoder state. All required data
54 * structures are allocated at initialization,
55 * so xz_dec_run() cannot return XZ_MEM_ERROR.
56 * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2
57 * dictionary buffer. All data structures are
58 * allocated at initialization, so xz_dec_run()
59 * cannot return XZ_MEM_ERROR.
60 * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is
61 * allocated once the required size has been
62 * parsed from the stream headers. If the
63 * allocation fails, xz_dec_run() will return
64 * XZ_MEM_ERROR.
65 *
66 * It is possible to enable support only for a subset of the above
67 * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
68 * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
69 * with support for all operation modes, but the preboot code may
70 * be built with fewer features to minimize code size.
71 */
72 enum xz_mode {
73 XZ_SINGLE,
74 XZ_PREALLOC,
75 XZ_DYNALLOC
76 };
77
78 /**
79 * enum xz_ret - Return codes
80 * @XZ_OK: Everything is OK so far. More input or more
81 * output space is required to continue. This
82 * return code is possible only in multi-call mode
83 * (XZ_PREALLOC or XZ_DYNALLOC).
84 * @XZ_STREAM_END: Operation finished successfully.
85 * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding
86 * is still possible in multi-call mode by simply
87 * calling xz_dec_run() again.
88 * Note that this return value is used only if
89 * XZ_DEC_ANY_CHECK was defined at build time,
90 * which is not used in the kernel. Unsupported
91 * check types return XZ_OPTIONS_ERROR if
92 * XZ_DEC_ANY_CHECK was not defined at build time.
93 * @XZ_MEM_ERROR: Allocating memory failed. This return code is
94 * possible only if the decoder was initialized
95 * with XZ_DYNALLOC. The amount of memory that was
96 * tried to be allocated was no more than the
97 * dict_max argument given to xz_dec_init().
98 * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than
99 * allowed by the dict_max argument given to
100 * xz_dec_init(). This return value is possible
101 * only in multi-call mode (XZ_PREALLOC or
102 * XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
103 * ignores the dict_max argument.
104 * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic
105 * bytes).
106 * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
107 * compression options. In the decoder this means
108 * that the header CRC32 matches, but the header
109 * itself specifies something that we don't support.
110 * @XZ_DATA_ERROR: Compressed data is corrupt.
111 * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
112 * different between multi-call and single-call
113 * mode; more information below.
114 *
115 * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
116 * to XZ code cannot consume any input and cannot produce any new output.
117 * This happens when there is no new input available, or the output buffer
118 * is full while at least one output byte is still pending. Assuming your
119 * code is not buggy, you can get this error only when decoding a compressed
120 * stream that is truncated or otherwise corrupt.
121 *
122 * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
123 * is too small or the compressed input is corrupt in a way that makes the
124 * decoder produce more output than the caller expected. When it is
125 * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
126 * is used instead of XZ_BUF_ERROR.
127 */
128 enum xz_ret {
129 XZ_OK,
130 XZ_STREAM_END,
131 XZ_UNSUPPORTED_CHECK,
132 XZ_MEM_ERROR,
133 XZ_MEMLIMIT_ERROR,
134 XZ_FORMAT_ERROR,
135 XZ_OPTIONS_ERROR,
136 XZ_DATA_ERROR,
137 XZ_BUF_ERROR
138 };
139
140 /**
141 * struct xz_buf - Passing input and output buffers to XZ code
142 * @in: Beginning of the input buffer. This may be NULL if and only
143 * if in_pos is equal to in_size.
144 * @in_pos: Current position in the input buffer. This must not exceed
145 * in_size.
146 * @in_size: Size of the input buffer
147 * @out: Beginning of the output buffer. This may be NULL if and only
148 * if out_pos is equal to out_size.
149 * @out_pos: Current position in the output buffer. This must not exceed
150 * out_size.
151 * @out_size: Size of the output buffer
152 *
153 * Only the contents of the output buffer from out[out_pos] onward, and
154 * the variables in_pos and out_pos are modified by the XZ code.
155 */
156 struct xz_buf {
157 const uint8_t *in;
158 size_t in_pos;
159 size_t in_size;
160
161 uint8_t *out;
162 size_t out_pos;
163 size_t out_size;
164 };
165
166 /**
167 * struct xz_dec - Opaque type to hold the XZ decoder state
168 */
169 struct xz_dec;
170
171 /* If no specific decoding mode is requested, enable support for all modes. */
172 #if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
173 && !defined(XZ_DEC_DYNALLOC)
174 # define XZ_DEC_SINGLE
175 # define XZ_DEC_PREALLOC
176 # define XZ_DEC_DYNALLOC
177 #endif
178
179 /*
180 * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
181 * of the supported modes are enabled, these macros will evaluate to true or
182 * false at compile time and thus allow the compiler to omit unneeded code.
183 */
184 #ifdef XZ_DEC_SINGLE
185 # define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
186 #else
187 # define DEC_IS_SINGLE(mode) (false)
188 #endif
189
190 #ifdef XZ_DEC_PREALLOC
191 # define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
192 #else
193 # define DEC_IS_PREALLOC(mode) (false)
194 #endif
195
196 #ifdef XZ_DEC_DYNALLOC
197 # define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
198 #else
199 # define DEC_IS_DYNALLOC(mode) (false)
200 #endif
201
202 #if !defined(XZ_DEC_SINGLE)
203 # define DEC_IS_MULTI(mode) (true)
204 #elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
205 # define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
206 #else
207 # define DEC_IS_MULTI(mode) (false)
208 #endif
209
210 /*
211 * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
212 * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
213 */
214 #ifndef XZ_DEC_BCJ
215 # if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
216 || defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
217 || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
218 || defined(XZ_DEC_SPARC)
219 # define XZ_DEC_BCJ
220 # endif
221 #endif
222
223 /*
224 * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
225 * before calling xz_dec_lzma2_run().
226 */
227 XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
228 uint32_t dict_max);
229
230 /*
231 * Decode the LZMA2 properties (one byte) and reset the decoder. Return
232 * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
233 * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
234 * decoder doesn't support.
235 */
236 XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
237 uint8_t props);
238
239 /* Decode raw LZMA2 stream from b->in to b->out. */
240 XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
241 struct xz_buf *b);
242
243 /* Free the memory allocated for the LZMA2 decoder. */
244 XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
245
246 #ifdef XZ_DEC_BCJ
247 /*
248 * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
249 * calling xz_dec_bcj_run().
250 */
251 XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool_t single_call);
252
253 /*
254 * Decode the Filter ID of a BCJ filter. This implementation doesn't
255 * support custom start offsets, so no decoding of Filter Properties
256 * is needed. Returns XZ_OK if the given Filter ID is supported.
257 * Otherwise XZ_OPTIONS_ERROR is returned.
258 */
259 XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
260
261 /*
262 * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
263 * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
264 * must be called directly.
265 */
266 XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
267 struct xz_dec_lzma2 *lzma2,
268 struct xz_buf *b);
269
270 /* Free the memory allocated for the BCJ filters. */
271 #define xz_dec_bcj_end(s) free(s)
272 #endif
273
274 #endif
275