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