diff options
Diffstat (limited to 'archival/libunarchive/unxz/xz.h')
-rw-r--r-- | archival/libunarchive/unxz/xz.h | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/archival/libunarchive/unxz/xz.h b/archival/libunarchive/unxz/xz.h new file mode 100644 index 000000000..eb82706b9 --- /dev/null +++ b/archival/libunarchive/unxz/xz.h | |||
@@ -0,0 +1,222 @@ | |||
1 | /* | ||
2 | * XZ decompressor | ||
3 | * | ||
4 | * Authors: Lasse Collin <lasse.collin@tukaani.org> | ||
5 | * Igor Pavlov <http://7-zip.org/> | ||
6 | * | ||
7 | * This file has been put into the public domain. | ||
8 | * You can do whatever you want with this file. | ||
9 | */ | ||
10 | |||
11 | #ifndef XZ_H | ||
12 | #define XZ_H | ||
13 | |||
14 | #ifdef __KERNEL__ | ||
15 | # include <linux/stddef.h> | ||
16 | # include <linux/types.h> | ||
17 | #else | ||
18 | # include <stddef.h> | ||
19 | # include <stdint.h> | ||
20 | #endif | ||
21 | |||
22 | /* In Linux, this is used to make extern functions static when needed. */ | ||
23 | #ifndef XZ_EXTERN | ||
24 | # define XZ_EXTERN extern | ||
25 | #endif | ||
26 | |||
27 | /* In Linux, this is used to mark the functions with __init when needed. */ | ||
28 | #ifndef XZ_FUNC | ||
29 | # define XZ_FUNC | ||
30 | #endif | ||
31 | |||
32 | /** | ||
33 | * enum xz_ret - Return codes | ||
34 | * @XZ_OK: Everything is OK so far. More input or more | ||
35 | * output space is required to continue. | ||
36 | * @XZ_STREAM_END: Operation finished successfully. | ||
37 | * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding | ||
38 | * is still possible in multi-call mode by simply | ||
39 | * calling xz_dec_run() again. | ||
40 | * NOTE: This return value is used only if | ||
41 | * XZ_DEC_ANY_CHECK was defined at build time, | ||
42 | * which is not used in the kernel. Unsupported | ||
43 | * check types return XZ_OPTIONS_ERROR if | ||
44 | * XZ_DEC_ANY_CHECK was not defined at build time. | ||
45 | * @XZ_MEMLIMIT_ERROR: Not enough memory was preallocated at decoder | ||
46 | * initialization time. | ||
47 | * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic | ||
48 | * bytes). | ||
49 | * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested | ||
50 | * compression options. In the decoder this means | ||
51 | * that the header CRC32 matches, but the header | ||
52 | * itself specifies something that we don't support. | ||
53 | * @XZ_DATA_ERROR: Compressed data is corrupt. | ||
54 | * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly | ||
55 | * different between multi-call and single-call | ||
56 | * mode; more information below. | ||
57 | * | ||
58 | * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls | ||
59 | * to XZ code cannot consume any input and cannot produce any new output. | ||
60 | * This happens when there is no new input available, or the output buffer | ||
61 | * is full while at least one output byte is still pending. Assuming your | ||
62 | * code is not buggy, you can get this error only when decoding a compressed | ||
63 | * stream that is truncated or otherwise corrupt. | ||
64 | * | ||
65 | * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer | ||
66 | * is too small, or the compressed input is corrupt in a way that makes the | ||
67 | * decoder produce more output than the caller expected. When it is | ||
68 | * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR | ||
69 | * is used instead of XZ_BUF_ERROR. | ||
70 | */ | ||
71 | enum xz_ret { | ||
72 | XZ_OK, | ||
73 | XZ_STREAM_END, | ||
74 | XZ_UNSUPPORTED_CHECK, | ||
75 | XZ_MEMLIMIT_ERROR, | ||
76 | XZ_FORMAT_ERROR, | ||
77 | XZ_OPTIONS_ERROR, | ||
78 | XZ_DATA_ERROR, | ||
79 | XZ_BUF_ERROR | ||
80 | }; | ||
81 | |||
82 | /** | ||
83 | * struct xz_buf - Passing input and output buffers to XZ code | ||
84 | * @in: Beginning of the input buffer. This may be NULL if and only | ||
85 | * if in_pos is equal to in_size. | ||
86 | * @in_pos: Current position in the input buffer. This must not exceed | ||
87 | * in_size. | ||
88 | * @in_size: Size of the input buffer | ||
89 | * @out: Beginning of the output buffer. This may be NULL if and only | ||
90 | * if out_pos is equal to out_size. | ||
91 | * @out_pos: Current position in the output buffer. This must not exceed | ||
92 | * out_size. | ||
93 | * @out_size: Size of the output buffer | ||
94 | * | ||
95 | * Only the contents of the output buffer from out[out_pos] onward, and | ||
96 | * the variables in_pos and out_pos are modified by the XZ code. | ||
97 | */ | ||
98 | struct xz_buf { | ||
99 | const uint8_t *in; | ||
100 | size_t in_pos; | ||
101 | size_t in_size; | ||
102 | |||
103 | uint8_t *out; | ||
104 | size_t out_pos; | ||
105 | size_t out_size; | ||
106 | }; | ||
107 | |||
108 | /** | ||
109 | * struct xz_dec - Opaque type to hold the XZ decoder state | ||
110 | */ | ||
111 | struct xz_dec; | ||
112 | |||
113 | /** | ||
114 | * xz_dec_init() - Allocate and initialize a XZ decoder state | ||
115 | * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for | ||
116 | * multi-call decoding, or special value of zero to indicate | ||
117 | * single-call decoding mode. | ||
118 | * | ||
119 | * If dict_max > 0, the decoder is initialized to work in multi-call mode. | ||
120 | * dict_max number of bytes of memory is preallocated for the LZMA2 | ||
121 | * dictionary. This way there is no risk that xz_dec_run() could run out | ||
122 | * of memory, since xz_dec_run() will never allocate any memory. Instead, | ||
123 | * if the preallocated dictionary is too small for decoding the given input | ||
124 | * stream, xz_dec_run() will return XZ_MEMLIMIT_ERROR. Thus, it is important | ||
125 | * to know what kind of data will be decoded to avoid allocating excessive | ||
126 | * amount of memory for the dictionary. | ||
127 | * | ||
128 | * LZMA2 dictionary is always 2^n bytes or 2^n + 2^(n-1) bytes (the latter | ||
129 | * sizes are less common in practice). In the kernel, dictionary sizes of | ||
130 | * 64 KiB, 128 KiB, 256 KiB, 512 KiB, and 1 MiB are probably the only | ||
131 | * reasonable values. | ||
132 | * | ||
133 | * If dict_max == 0, the decoder is initialized to work in single-call mode. | ||
134 | * In single-call mode, xz_dec_run() decodes the whole stream at once. The | ||
135 | * caller must provide enough output space or the decoding will fail. The | ||
136 | * output space is used as the dictionary buffer, which is why there is | ||
137 | * no need to allocate the dictionary as part of the decoder's internal | ||
138 | * state. | ||
139 | * | ||
140 | * Because the output buffer is used as the workspace, streams encoded using | ||
141 | * a big dictionary are not a problem in single-call. It is enough that the | ||
142 | * output buffer is big enough to hold the actual uncompressed data; it | ||
143 | * can be smaller than the dictionary size stored in the stream headers. | ||
144 | * | ||
145 | * On success, xz_dec_init() returns a pointer to struct xz_dec, which is | ||
146 | * ready to be used with xz_dec_run(). On error, xz_dec_init() returns NULL. | ||
147 | */ | ||
148 | XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(uint32_t dict_max); | ||
149 | |||
150 | /** | ||
151 | * xz_dec_run() - Run the XZ decoder | ||
152 | * @s: Decoder state allocated using xz_dec_init() | ||
153 | * @b: Input and output buffers | ||
154 | * | ||
155 | * In multi-call mode, this function may return any of the values listed in | ||
156 | * enum xz_ret. | ||
157 | * | ||
158 | * In single-call mode, this function never returns XZ_OK. If an error occurs | ||
159 | * in single-call mode (return value is not XZ_STREAM_END), b->in_pos and | ||
160 | * b->out_pos are not modified, and the contents of the output buffer from | ||
161 | * b->out[b->out_pos] onward are undefined. | ||
162 | * | ||
163 | * NOTE: In single-call mode, the contents of the output buffer are undefined | ||
164 | * also after XZ_BUF_ERROR. This is because with some filter chains, there | ||
165 | * may be a second pass over the output buffer, and this pass cannot be | ||
166 | * properly done if the output buffer is truncated. Thus, you cannot give | ||
167 | * the single-call decoder a too small buffer and then expect to get that | ||
168 | * amount valid data from the beginning of the stream. You must use the | ||
169 | * multi-call decoder if you don't want to uncompress the whole stream. | ||
170 | */ | ||
171 | XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b); | ||
172 | |||
173 | /** | ||
174 | * xz_dec_reset() - Reset an already allocated decoder state | ||
175 | * @s: Decoder state allocated using xz_dec_init() | ||
176 | * | ||
177 | * This function can be used to reset the multi-call decoder state without | ||
178 | * freeing and reallocating memory with xz_dec_end() and xz_dec_init(). | ||
179 | * | ||
180 | * In single-call mode, xz_dec_reset() is always called in the beginning of | ||
181 | * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in | ||
182 | * multi-call mode. | ||
183 | */ | ||
184 | XZ_EXTERN void XZ_FUNC xz_dec_reset(struct xz_dec *s); | ||
185 | |||
186 | /** | ||
187 | * xz_dec_end() - Free the memory allocated for the decoder state | ||
188 | * @s: Decoder state allocated using xz_dec_init(). If s is NULL, | ||
189 | * this function does nothing. | ||
190 | */ | ||
191 | XZ_EXTERN void XZ_FUNC xz_dec_end(struct xz_dec *s); | ||
192 | |||
193 | /* | ||
194 | * Standalone build (userspace build or in-kernel build for boot time use) | ||
195 | * needs a CRC32 implementation. For normal in-kernel use, kernel's own | ||
196 | * CRC32 module is used instead, and users of this module don't need to | ||
197 | * care about the functions below. | ||
198 | */ | ||
199 | #ifndef XZ_INTERNAL_CRC32 | ||
200 | # ifdef __KERNEL__ | ||
201 | # define XZ_INTERNAL_CRC32 0 | ||
202 | # else | ||
203 | # define XZ_INTERNAL_CRC32 1 | ||
204 | # endif | ||
205 | #endif | ||
206 | |||
207 | #if XZ_INTERNAL_CRC32 | ||
208 | /* | ||
209 | * This must be called before any other xz_* function to initialize | ||
210 | * the CRC32 lookup table. | ||
211 | */ | ||
212 | XZ_EXTERN void XZ_FUNC xz_crc32_init(void); | ||
213 | |||
214 | /* | ||
215 | * Update CRC32 value using the polynomial from IEEE-802.3. To start a new | ||
216 | * calculation, the third argument must be zero. To continue the calculation, | ||
217 | * the previously returned value is passed as the third argument. | ||
218 | */ | ||
219 | XZ_EXTERN uint32_t XZ_FUNC xz_crc32( | ||
220 | const uint8_t *buf, size_t size, uint32_t crc); | ||
221 | #endif | ||
222 | #endif | ||