diff options
Diffstat (limited to 'archival/libunarchive/unxz/xz.h')
-rw-r--r-- | archival/libunarchive/unxz/xz.h | 139 |
1 files changed, 94 insertions, 45 deletions
diff --git a/archival/libunarchive/unxz/xz.h b/archival/libunarchive/unxz/xz.h index eb82706b9..c6c071c4a 100644 --- a/archival/libunarchive/unxz/xz.h +++ b/archival/libunarchive/unxz/xz.h | |||
@@ -30,9 +30,42 @@ | |||
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * enum xz_mode - Operation mode | ||
34 | * | ||
35 | * @XZ_SINGLE: Single-call mode. This uses less RAM than | ||
36 | * than multi-call modes, because the LZMA2 | ||
37 | * dictionary doesn't need to be allocated as | ||
38 | * part of the decoder state. All required data | ||
39 | * structures are allocated at initialization, | ||
40 | * so xz_dec_run() cannot return XZ_MEM_ERROR. | ||
41 | * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2 | ||
42 | * dictionary buffer. All data structures are | ||
43 | * allocated at initialization, so xz_dec_run() | ||
44 | * cannot return XZ_MEM_ERROR. | ||
45 | * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is | ||
46 | * allocated once the required size has been | ||
47 | * parsed from the stream headers. If the | ||
48 | * allocation fails, xz_dec_run() will return | ||
49 | * XZ_MEM_ERROR. | ||
50 | * | ||
51 | * It is possible to enable support only for a subset of the above | ||
52 | * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC, | ||
53 | * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled | ||
54 | * with support for all operation modes, but the preboot code may | ||
55 | * be built with fewer features to minimize code size. | ||
56 | */ | ||
57 | enum xz_mode { | ||
58 | XZ_SINGLE, | ||
59 | XZ_PREALLOC, | ||
60 | XZ_DYNALLOC | ||
61 | }; | ||
62 | |||
63 | /** | ||
33 | * enum xz_ret - Return codes | 64 | * enum xz_ret - Return codes |
34 | * @XZ_OK: Everything is OK so far. More input or more | 65 | * @XZ_OK: Everything is OK so far. More input or more |
35 | * output space is required to continue. | 66 | * output space is required to continue. This |
67 | * return code is possible only in multi-call mode | ||
68 | * (XZ_PREALLOC or XZ_DYNALLOC). | ||
36 | * @XZ_STREAM_END: Operation finished successfully. | 69 | * @XZ_STREAM_END: Operation finished successfully. |
37 | * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding | 70 | * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding |
38 | * is still possible in multi-call mode by simply | 71 | * is still possible in multi-call mode by simply |
@@ -42,8 +75,17 @@ | |||
42 | * which is not used in the kernel. Unsupported | 75 | * which is not used in the kernel. Unsupported |
43 | * check types return XZ_OPTIONS_ERROR if | 76 | * check types return XZ_OPTIONS_ERROR if |
44 | * XZ_DEC_ANY_CHECK was not defined at build time. | 77 | * XZ_DEC_ANY_CHECK was not defined at build time. |
45 | * @XZ_MEMLIMIT_ERROR: Not enough memory was preallocated at decoder | 78 | * @XZ_MEM_ERROR: Allocating memory failed. This return code is |
46 | * initialization time. | 79 | * possible only if the decoder was initialized |
80 | * with XZ_DYNALLOC. The amount of memory that was | ||
81 | * tried to be allocated was no more than the | ||
82 | * dict_max argument given to xz_dec_init(). | ||
83 | * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than | ||
84 | * allowed by the dict_max argument given to | ||
85 | * xz_dec_init(). This return value is possible | ||
86 | * only in multi-call mode (XZ_PREALLOC or | ||
87 | * XZ_DYNALLOC); the single-call mode (XZ_SINGLE) | ||
88 | * ignores the dict_max argument. | ||
47 | * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic | 89 | * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic |
48 | * bytes). | 90 | * bytes). |
49 | * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested | 91 | * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested |
@@ -72,6 +114,7 @@ enum xz_ret { | |||
72 | XZ_OK, | 114 | XZ_OK, |
73 | XZ_STREAM_END, | 115 | XZ_STREAM_END, |
74 | XZ_UNSUPPORTED_CHECK, | 116 | XZ_UNSUPPORTED_CHECK, |
117 | XZ_MEM_ERROR, | ||
75 | XZ_MEMLIMIT_ERROR, | 118 | XZ_MEMLIMIT_ERROR, |
76 | XZ_FORMAT_ERROR, | 119 | XZ_FORMAT_ERROR, |
77 | XZ_OPTIONS_ERROR, | 120 | XZ_OPTIONS_ERROR, |
@@ -112,61 +155,67 @@ struct xz_dec; | |||
112 | 155 | ||
113 | /** | 156 | /** |
114 | * xz_dec_init() - Allocate and initialize a XZ decoder state | 157 | * xz_dec_init() - Allocate and initialize a XZ decoder state |
158 | * @mode: Operation mode | ||
115 | * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for | 159 | * @dict_max: Maximum size of the LZMA2 dictionary (history buffer) for |
116 | * multi-call decoding, or special value of zero to indicate | 160 | * multi-call decoding. This is ignored in single-call mode |
117 | * single-call decoding mode. | 161 | * (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes |
118 | * | 162 | * or 2^n + 2^(n-1) bytes (the latter sizes are less common |
119 | * If dict_max > 0, the decoder is initialized to work in multi-call mode. | 163 | * in practice), so other values for dict_max don't make sense. |
120 | * dict_max number of bytes of memory is preallocated for the LZMA2 | 164 | * In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB, |
121 | * dictionary. This way there is no risk that xz_dec_run() could run out | 165 | * 512 KiB, and 1 MiB are probably the only reasonable values, |
122 | * of memory, since xz_dec_run() will never allocate any memory. Instead, | 166 | * except for kernel and initramfs images where a bigger |
123 | * if the preallocated dictionary is too small for decoding the given input | 167 | * dictionary can be fine and useful. |
124 | * stream, xz_dec_run() will return XZ_MEMLIMIT_ERROR. Thus, it is important | 168 | * |
125 | * to know what kind of data will be decoded to avoid allocating excessive | 169 | * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at |
126 | * amount of memory for the dictionary. | 170 | * once. The caller must provide enough output space or the decoding will |
127 | * | 171 | * fail. The output space is used as the dictionary buffer, which is why |
128 | * LZMA2 dictionary is always 2^n bytes or 2^n + 2^(n-1) bytes (the latter | 172 | * there is no need to allocate the dictionary as part of the decoder's |
129 | * sizes are less common in practice). In the kernel, dictionary sizes of | 173 | * internal state. |
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 | * | 174 | * |
140 | * Because the output buffer is used as the workspace, streams encoded using | 175 | * 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 | 176 | * a big dictionary are not a problem in single-call mode. It is enough that |
142 | * output buffer is big enough to hold the actual uncompressed data; it | 177 | * the 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. | 178 | * can be smaller than the dictionary size stored in the stream headers. |
144 | * | 179 | * |
180 | * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes | ||
181 | * of memory is preallocated for the LZMA2 dictionary. This way there is no | ||
182 | * risk that xz_dec_run() could run out of memory, since xz_dec_run() will | ||
183 | * never allocate any memory. Instead, if the preallocated dictionary is too | ||
184 | * small for decoding the given input stream, xz_dec_run() will return | ||
185 | * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be | ||
186 | * decoded to avoid allocating excessive amount of memory for the dictionary. | ||
187 | * | ||
188 | * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC): | ||
189 | * dict_max specifies the maximum allowed dictionary size that xz_dec_run() | ||
190 | * may allocate once it has parsed the dictionary size from the stream | ||
191 | * headers. This way excessive allocations can be avoided while still | ||
192 | * limiting the maximum memory usage to a sane value to prevent running the | ||
193 | * system out of memory when decompressing streams from untrusted sources. | ||
194 | * | ||
145 | * On success, xz_dec_init() returns a pointer to struct xz_dec, which is | 195 | * 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. | 196 | * ready to be used with xz_dec_run(). If memory allocation fails, |
197 | * xz_dec_init() returns NULL. | ||
147 | */ | 198 | */ |
148 | XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(uint32_t dict_max); | 199 | XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init( |
200 | enum xz_mode mode, uint32_t dict_max); | ||
149 | 201 | ||
150 | /** | 202 | /** |
151 | * xz_dec_run() - Run the XZ decoder | 203 | * xz_dec_run() - Run the XZ decoder |
152 | * @s: Decoder state allocated using xz_dec_init() | 204 | * @s: Decoder state allocated using xz_dec_init() |
153 | * @b: Input and output buffers | 205 | * @b: Input and output buffers |
154 | * | 206 | * |
155 | * In multi-call mode, this function may return any of the values listed in | 207 | * The possible return values depend on build options and operation mode. |
156 | * enum xz_ret. | 208 | * See enum xz_ret for details. |
157 | * | 209 | * |
158 | * In single-call mode, this function never returns XZ_OK. If an error occurs | 210 | * NOTE: If an error occurs in single-call mode (return value is not |
159 | * in single-call mode (return value is not XZ_STREAM_END), b->in_pos and | 211 | * XZ_STREAM_END), b->in_pos and b->out_pos are not modified, and the |
160 | * b->out_pos are not modified, and the contents of the output buffer from | 212 | * contents of the output buffer from b->out[b->out_pos] onward are |
161 | * b->out[b->out_pos] onward are undefined. | 213 | * undefined. This is true even after XZ_BUF_ERROR, because with some filter |
162 | * | 214 | * chains, there may be a second pass over the output buffer, and this pass |
163 | * NOTE: In single-call mode, the contents of the output buffer are undefined | 215 | * cannot be properly done if the output buffer is truncated. Thus, you |
164 | * also after XZ_BUF_ERROR. This is because with some filter chains, there | 216 | * cannot give the single-call decoder a too small buffer and then expect to |
165 | * may be a second pass over the output buffer, and this pass cannot be | 217 | * get that amount valid data from the beginning of the stream. You must use |
166 | * properly done if the output buffer is truncated. Thus, you cannot give | 218 | * the multi-call decoder if you don't want to uncompress the whole stream. |
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 | */ | 219 | */ |
171 | XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b); | 220 | XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b); |
172 | 221 | ||