Skip to content

Commit b075166

Browse files
e-linerA. Patrick Williams III
authored andcommitted
Adding files from xz-embedded repository
Change-Id: I57997bc90e8d25297ed034773855ae619c46e430 RTC:125550 Reviewed-on: http://gfw160.aus.stglabs.ibm.com:8080/gerrit/20109 Tested-by: Jenkins Server Tested-by: Jenkins OP Build CI Tested-by: Jenkins OP HW Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com> Reviewed-by: PRACHI GUPTA <pragupta@us.ibm.com> Reviewed-by: A. Patrick Williams III <iawillia@us.ibm.com>
1 parent 8338d88 commit b075166

File tree

9 files changed

+3001
-0
lines changed

9 files changed

+3001
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is an automatically generated prolog.
2+
3+
$SOURCE_BEGIN_TAG $filename $SOURCE_END_TAG
4+
5+
OpenPOWER $projectName Project
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
/* IBM_PROLOG_BEGIN_TAG */
2+
/* This is an automatically generated prolog. */
3+
/* */
4+
/* $Source: src/usr/hwpf/hwp/start_payload/xz/xz.h $ */
5+
/* */
6+
/* OpenPOWER HostBoot Project */
7+
/* */
8+
/* IBM_PROLOG_END_TAG */
9+
/*
10+
* XZ decompressor
11+
*
12+
* Authors: Lasse Collin <lasse.collin@tukaani.org>
13+
* Igor Pavlov <http://7-zip.org/>
14+
*
15+
* This file has been put into the public domain.
16+
* You can do whatever you want with this file.
17+
*/
18+
19+
#ifndef XZ_H
20+
#define XZ_H
21+
22+
#ifdef __KERNEL__
23+
#include <linux/stddef.h>
24+
#include <linux/types.h>
25+
#else
26+
#include <stddef.h>
27+
#include <stdint.h>
28+
#endif
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
/* In Linux, this is used to make extern functions static when needed. */
35+
#ifndef XZ_EXTERN
36+
#define XZ_EXTERN extern
37+
#endif
38+
39+
/**
40+
* enum xz_mode - Operation mode
41+
*
42+
* @XZ_SINGLE: Single-call mode. This uses less RAM than
43+
* than multi-call modes, because the LZMA2
44+
* dictionary doesn't need to be allocated as
45+
* part of the decoder state. All required data
46+
* structures are allocated at initialization,
47+
* so xz_dec_run() cannot return XZ_MEM_ERROR.
48+
* @XZ_PREALLOC: Multi-call mode with preallocated LZMA2
49+
* dictionary buffer. All data structures are
50+
* allocated at initialization, so xz_dec_run()
51+
* cannot return XZ_MEM_ERROR.
52+
* @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is
53+
* allocated once the required size has been
54+
* parsed from the stream headers. If the
55+
* allocation fails, xz_dec_run() will return
56+
* XZ_MEM_ERROR.
57+
*
58+
* It is possible to enable support only for a subset of the above
59+
* modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
60+
* or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
61+
* with support for all operation modes, but the preboot code may
62+
* be built with fewer features to minimize code size.
63+
*/
64+
enum xz_mode {
65+
XZ_SINGLE,
66+
XZ_PREALLOC,
67+
XZ_DYNALLOC
68+
};
69+
70+
/**
71+
* enum xz_ret - Return codes
72+
* @XZ_OK: Everything is OK so far. More input or more
73+
* output space is required to continue. This
74+
* return code is possible only in multi-call mode
75+
* (XZ_PREALLOC or XZ_DYNALLOC).
76+
* @XZ_STREAM_END: Operation finished successfully.
77+
* @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding
78+
* is still possible in multi-call mode by simply
79+
* calling xz_dec_run() again.
80+
* Note that this return value is used only if
81+
* XZ_DEC_ANY_CHECK was defined at build time,
82+
* which is not used in the kernel. Unsupported
83+
* check types return XZ_OPTIONS_ERROR if
84+
* XZ_DEC_ANY_CHECK was not defined at build time.
85+
* @XZ_MEM_ERROR: Allocating memory failed. This return code is
86+
* possible only if the decoder was initialized
87+
* with XZ_DYNALLOC. The amount of memory that was
88+
* tried to be allocated was no more than the
89+
* dict_max argument given to xz_dec_init().
90+
* @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than
91+
* allowed by the dict_max argument given to
92+
* xz_dec_init(). This return value is possible
93+
* only in multi-call mode (XZ_PREALLOC or
94+
* XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
95+
* ignores the dict_max argument.
96+
* @XZ_FORMAT_ERROR: File format was not recognized (wrong magic
97+
* bytes).
98+
* @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
99+
* compression options. In the decoder this means
100+
* that the header CRC32 matches, but the header
101+
* itself specifies something that we don't support.
102+
* @XZ_DATA_ERROR: Compressed data is corrupt.
103+
* @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
104+
* different between multi-call and single-call
105+
* mode; more information below.
106+
*
107+
* In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
108+
* to XZ code cannot consume any input and cannot produce any new output.
109+
* This happens when there is no new input available, or the output buffer
110+
* is full while at least one output byte is still pending. Assuming your
111+
* code is not buggy, you can get this error only when decoding a compressed
112+
* stream that is truncated or otherwise corrupt.
113+
*
114+
* In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
115+
* is too small or the compressed input is corrupt in a way that makes the
116+
* decoder produce more output than the caller expected. When it is
117+
* (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
118+
* is used instead of XZ_BUF_ERROR.
119+
*/
120+
enum xz_ret {
121+
XZ_OK,
122+
XZ_STREAM_END,
123+
XZ_UNSUPPORTED_CHECK,
124+
XZ_MEM_ERROR,
125+
XZ_MEMLIMIT_ERROR,
126+
XZ_FORMAT_ERROR,
127+
XZ_OPTIONS_ERROR,
128+
XZ_DATA_ERROR,
129+
XZ_BUF_ERROR
130+
};
131+
132+
/**
133+
* struct xz_buf - Passing input and output buffers to XZ code
134+
* @in: Beginning of the input buffer. This may be NULL if and only
135+
* if in_pos is equal to in_size.
136+
* @in_pos: Current position in the input buffer. This must not exceed
137+
* in_size.
138+
* @in_size: Size of the input buffer
139+
* @out: Beginning of the output buffer. This may be NULL if and only
140+
* if out_pos is equal to out_size.
141+
* @out_pos: Current position in the output buffer. This must not exceed
142+
* out_size.
143+
* @out_size: Size of the output buffer
144+
*
145+
* Only the contents of the output buffer from out[out_pos] onward, and
146+
* the variables in_pos and out_pos are modified by the XZ code.
147+
*/
148+
struct xz_buf {
149+
const uint8_t *in;
150+
size_t in_pos;
151+
size_t in_size;
152+
153+
uint8_t *out;
154+
size_t out_pos;
155+
size_t out_size;
156+
};
157+
158+
/**
159+
* struct xz_dec - Opaque type to hold the XZ decoder state
160+
*/
161+
struct xz_dec;
162+
163+
/**
164+
* xz_dec_init() - Allocate and initialize a XZ decoder state
165+
* @mode: Operation mode
166+
* @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for
167+
* multi-call decoding. This is ignored in single-call mode
168+
* (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
169+
* or 2^n + 2^(n-1) bytes (the latter sizes are less common
170+
* in practice), so other values for dict_max don't make sense.
171+
* In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
172+
* 512 KiB, and 1 MiB are probably the only reasonable values,
173+
* except for kernel and initramfs images where a bigger
174+
* dictionary can be fine and useful.
175+
*
176+
* Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
177+
* once. The caller must provide enough output space or the decoding will
178+
* fail. The output space is used as the dictionary buffer, which is why
179+
* there is no need to allocate the dictionary as part of the decoder's
180+
* internal state.
181+
*
182+
* Because the output buffer is used as the workspace, streams encoded using
183+
* a big dictionary are not a problem in single-call mode. It is enough that
184+
* the output buffer is big enough to hold the actual uncompressed data; it
185+
* can be smaller than the dictionary size stored in the stream headers.
186+
*
187+
* Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
188+
* of memory is preallocated for the LZMA2 dictionary. This way there is no
189+
* risk that xz_dec_run() could run out of memory, since xz_dec_run() will
190+
* never allocate any memory. Instead, if the preallocated dictionary is too
191+
* small for decoding the given input stream, xz_dec_run() will return
192+
* XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
193+
* decoded to avoid allocating excessive amount of memory for the dictionary.
194+
*
195+
* Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
196+
* dict_max specifies the maximum allowed dictionary size that xz_dec_run()
197+
* may allocate once it has parsed the dictionary size from the stream
198+
* headers. This way excessive allocations can be avoided while still
199+
* limiting the maximum memory usage to a sane value to prevent running the
200+
* system out of memory when decompressing streams from untrusted sources.
201+
*
202+
* On success, xz_dec_init() returns a pointer to struct xz_dec, which is
203+
* ready to be used with xz_dec_run(). If memory allocation fails,
204+
* xz_dec_init() returns NULL.
205+
*/
206+
XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max);
207+
208+
/**
209+
* xz_dec_run() - Run the XZ decoder
210+
* @s: Decoder state allocated using xz_dec_init()
211+
* @b: Input and output buffers
212+
*
213+
* The possible return values depend on build options and operation mode.
214+
* See enum xz_ret for details.
215+
*
216+
* Note that if an error occurs in single-call mode (return value is not
217+
* XZ_STREAM_END), b->in_pos and b->out_pos are not modified and the
218+
* contents of the output buffer from b->out[b->out_pos] onward are
219+
* undefined. This is true even after XZ_BUF_ERROR, because with some filter
220+
* chains, there may be a second pass over the output buffer, and this pass
221+
* cannot be properly done if the output buffer is truncated. Thus, you
222+
* cannot give the single-call decoder a too small buffer and then expect to
223+
* get that amount valid data from the beginning of the stream. You must use
224+
* the multi-call decoder if you don't want to uncompress the whole stream.
225+
*/
226+
XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b);
227+
228+
/**
229+
* xz_dec_reset() - Reset an already allocated decoder state
230+
* @s: Decoder state allocated using xz_dec_init()
231+
*
232+
* This function can be used to reset the multi-call decoder state without
233+
* freeing and reallocating memory with xz_dec_end() and xz_dec_init().
234+
*
235+
* In single-call mode, xz_dec_reset() is always called in the beginning of
236+
* xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
237+
* multi-call mode.
238+
*/
239+
XZ_EXTERN void xz_dec_reset(struct xz_dec *s);
240+
241+
/**
242+
* xz_dec_end() - Free the memory allocated for the decoder state
243+
* @s: Decoder state allocated using xz_dec_init(). If s is NULL,
244+
* this function does nothing.
245+
*/
246+
XZ_EXTERN void xz_dec_end(struct xz_dec *s);
247+
248+
/*
249+
* Standalone build (userspace build or in-kernel build for boot time use)
250+
* needs a CRC32 implementation. For normal in-kernel use, kernel's own
251+
* CRC32 module is used instead, and users of this module don't need to
252+
* care about the functions below.
253+
*/
254+
#ifndef XZ_INTERNAL_CRC32
255+
#ifdef __KERNEL__
256+
#define XZ_INTERNAL_CRC32 0
257+
#else
258+
#define XZ_INTERNAL_CRC32 1
259+
#endif
260+
#endif
261+
262+
/*
263+
* If CRC64 support has been enabled with XZ_USE_CRC64, a CRC64
264+
* implementation is needed too.
265+
*/
266+
#ifndef XZ_USE_CRC64
267+
#undef XZ_INTERNAL_CRC64
268+
#define XZ_INTERNAL_CRC64 0
269+
#endif
270+
#ifndef XZ_INTERNAL_CRC64
271+
#ifdef __KERNEL__
272+
#error Using CRC64 in the kernel has not been implemented.
273+
#else
274+
#define XZ_INTERNAL_CRC64 1
275+
#endif
276+
#endif
277+
278+
#if XZ_INTERNAL_CRC32
279+
/*
280+
* This must be called before any other xz_* function to initialize
281+
* the CRC32 lookup table.
282+
*/
283+
XZ_EXTERN void xz_crc32_init(void);
284+
285+
/*
286+
* Update CRC32 value using the polynomial from IEEE-802.3. To start a new
287+
* calculation, the third argument must be zero. To continue the calculation,
288+
* the previously returned value is passed as the third argument.
289+
*/
290+
XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc);
291+
#endif
292+
293+
#if XZ_INTERNAL_CRC64
294+
/*
295+
* This must be called before any other xz_* function (except xz_crc32_init())
296+
* to initialize the CRC64 lookup table.
297+
*/
298+
XZ_EXTERN void xz_crc64_init(void);
299+
300+
/*
301+
* Update CRC64 value using the polynomial from ECMA-182. To start a new
302+
* calculation, the third argument must be zero. To continue the calculation,
303+
* the previously returned value is passed as the third argument.
304+
*/
305+
XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc);
306+
#endif
307+
308+
#ifdef __cplusplus
309+
}
310+
#endif
311+
312+
#endif

0 commit comments

Comments
 (0)