xref: /freebsd/contrib/xz/src/liblzma/api/lzma/container.h (revision 81ad6265)
1 /**
2  * \file        lzma/container.h
3  * \brief       File formats
4  */
5 
6 /*
7  * Author: Lasse Collin
8  *
9  * This file has been put into the public domain.
10  * You can do whatever you want with this file.
11  *
12  * See ../lzma.h for information about liblzma as a whole.
13  */
14 
15 #ifndef LZMA_H_INTERNAL
16 #	error Never include this file directly. Use <lzma.h> instead.
17 #endif
18 
19 
20 /************
21  * Encoding *
22  ************/
23 
24 /**
25  * \brief       Default compression preset
26  *
27  * It's not straightforward to recommend a default preset, because in some
28  * cases keeping the resource usage relatively low is more important that
29  * getting the maximum compression ratio.
30  */
31 #define LZMA_PRESET_DEFAULT     UINT32_C(6)
32 
33 
34 /**
35  * \brief       Mask for preset level
36  *
37  * This is useful only if you need to extract the level from the preset
38  * variable. That should be rare.
39  */
40 #define LZMA_PRESET_LEVEL_MASK  UINT32_C(0x1F)
41 
42 
43 /*
44  * Preset flags
45  *
46  * Currently only one flag is defined.
47  */
48 
49 /**
50  * \brief       Extreme compression preset
51  *
52  * This flag modifies the preset to make the encoding significantly slower
53  * while improving the compression ratio only marginally. This is useful
54  * when you don't mind wasting time to get as small result as possible.
55  *
56  * This flag doesn't affect the memory usage requirements of the decoder (at
57  * least not significantly). The memory usage of the encoder may be increased
58  * a little but only at the lowest preset levels (0-3).
59  */
60 #define LZMA_PRESET_EXTREME       (UINT32_C(1) << 31)
61 
62 
63 /**
64  * \brief       Multithreading options
65  */
66 typedef struct {
67 	/**
68 	 * \brief       Flags
69 	 *
70 	 * Set this to zero if no flags are wanted.
71 	 *
72 	 * Encoder: No flags are currently supported.
73 	 *
74 	 * Decoder: Bitwise-or of zero or more of the decoder flags:
75 	 * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
76 	 * LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
77 	 * LZMA_CONCATENATED, LZMA_FAIL_FAST
78 	 */
79 	uint32_t flags;
80 
81 	/**
82 	 * \brief       Number of worker threads to use
83 	 */
84 	uint32_t threads;
85 
86 	/**
87 	 * \brief       Encoder only: Maximum uncompressed size of a Block
88 	 *
89 	 * The encoder will start a new .xz Block every block_size bytes.
90 	 * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
91 	 * the caller may tell liblzma to start a new Block earlier.
92 	 *
93 	 * With LZMA2, a recommended block size is 2-4 times the LZMA2
94 	 * dictionary size. With very small dictionaries, it is recommended
95 	 * to use at least 1 MiB block size for good compression ratio, even
96 	 * if this is more than four times the dictionary size. Note that
97 	 * these are only recommendations for typical use cases; feel free
98 	 * to use other values. Just keep in mind that using a block size
99 	 * less than the LZMA2 dictionary size is waste of RAM.
100 	 *
101 	 * Set this to 0 to let liblzma choose the block size depending
102 	 * on the compression options. For LZMA2 it will be 3*dict_size
103 	 * or 1 MiB, whichever is more.
104 	 *
105 	 * For each thread, about 3 * block_size bytes of memory will be
106 	 * allocated. This may change in later liblzma versions. If so,
107 	 * the memory usage will probably be reduced, not increased.
108 	 */
109 	uint64_t block_size;
110 
111 	/**
112 	 * \brief       Timeout to allow lzma_code() to return early
113 	 *
114 	 * Multithreading can make liblzma to consume input and produce
115 	 * output in a very bursty way: it may first read a lot of input
116 	 * to fill internal buffers, then no input or output occurs for
117 	 * a while.
118 	 *
119 	 * In single-threaded mode, lzma_code() won't return until it has
120 	 * either consumed all the input or filled the output buffer. If
121 	 * this is done in multithreaded mode, it may cause a call
122 	 * lzma_code() to take even tens of seconds, which isn't acceptable
123 	 * in all applications.
124 	 *
125 	 * To avoid very long blocking times in lzma_code(), a timeout
126 	 * (in milliseconds) may be set here. If lzma_code() would block
127 	 * longer than this number of milliseconds, it will return with
128 	 * LZMA_OK. Reasonable values are 100 ms or more. The xz command
129 	 * line tool uses 300 ms.
130 	 *
131 	 * If long blocking times are fine for you, set timeout to a special
132 	 * value of 0, which will disable the timeout mechanism and will make
133 	 * lzma_code() block until all the input is consumed or the output
134 	 * buffer has been filled.
135 	 *
136 	 * \note        Even with a timeout, lzma_code() might sometimes take
137 	 *              somewhat long time to return. No timing guarantees
138 	 *              are made.
139 	 */
140 	uint32_t timeout;
141 
142 	/**
143 	 * \brief       Encoder only: Compression preset
144 	 *
145 	 * The preset is set just like with lzma_easy_encoder().
146 	 * The preset is ignored if filters below is non-NULL.
147 	 */
148 	uint32_t preset;
149 
150 	/**
151 	 * \brief       Encoder only: Filter chain (alternative to a preset)
152 	 *
153 	 * If this is NULL, the preset above is used. Otherwise the preset
154 	 * is ignored and the filter chain specified here is used.
155 	 */
156 	const lzma_filter *filters;
157 
158 	/**
159 	 * \brief       Encoder only: Integrity check type
160 	 *
161 	 * See check.h for available checks. The xz command line tool
162 	 * defaults to LZMA_CHECK_CRC64, which is a good choice if you
163 	 * are unsure.
164 	 */
165 	lzma_check check;
166 
167 	/*
168 	 * Reserved space to allow possible future extensions without
169 	 * breaking the ABI. You should not touch these, because the names
170 	 * of these variables may change. These are and will never be used
171 	 * with the currently supported options, so it is safe to leave these
172 	 * uninitialized.
173 	 */
174 	lzma_reserved_enum reserved_enum1;
175 	lzma_reserved_enum reserved_enum2;
176 	lzma_reserved_enum reserved_enum3;
177 	uint32_t reserved_int1;
178 	uint32_t reserved_int2;
179 	uint32_t reserved_int3;
180 	uint32_t reserved_int4;
181 
182 	/**
183 	 * \brief       Memory usage limit to reduce the number of threads
184 	 *
185 	 * Encoder: Ignored.
186 	 *
187 	 * Decoder:
188 	 *
189 	 * If the number of threads has been set so high that more than
190 	 * memlimit_threading bytes of memory would be needed, the number
191 	 * of threads will be reduced so that the memory usage will not exceed
192 	 * memlimit_threading bytes. However, if memlimit_threading cannot
193 	 * be met even in single-threaded mode, then decoding will continue
194 	 * in single-threaded mode and memlimit_threading may be exceeded
195 	 * even by a large amount. That is, memlimit_threading will never make
196 	 * lzma_code() return LZMA_MEMLIMIT_ERROR. To truly cap the memory
197 	 * usage, see memlimit_stop below.
198 	 *
199 	 * Setting memlimit_threading to UINT64_MAX or a similar huge value
200 	 * means that liblzma is allowed to keep the whole compressed file
201 	 * and the whole uncompressed file in memory in addition to the memory
202 	 * needed by the decompressor data structures used by each thread!
203 	 * In other words, a reasonable value limit must be set here or it
204 	 * will cause problems sooner or later. If you have no idea what
205 	 * a reasonable value could be, try lzma_physmem() / 4 as a starting
206 	 * point. Setting this limit will never prevent decompression of
207 	 * a file; this will only reduce the number of threads.
208 	 *
209 	 * If memlimit_threading is greater than memlimit_stop, then the value
210 	 * of memlimit_stop will be used for both.
211 	 */
212 	uint64_t memlimit_threading;
213 
214 	/**
215 	 * \brief       Memory usage limit that should never be exceeded
216 	 *
217 	 * Encoder: Ignored.
218 	 *
219 	 * Decoder: If decompressing will need more than this amount of
220 	 * memory even in the single-threaded mode, then lzma_code() will
221 	 * return LZMA_MEMLIMIT_ERROR.
222 	 */
223 	uint64_t memlimit_stop;
224 
225 	uint64_t reserved_int7;
226 	uint64_t reserved_int8;
227 	void *reserved_ptr1;
228 	void *reserved_ptr2;
229 	void *reserved_ptr3;
230 	void *reserved_ptr4;
231 
232 } lzma_mt;
233 
234 
235 /**
236  * \brief       Calculate approximate memory usage of easy encoder
237  *
238  * This function is a wrapper for lzma_raw_encoder_memusage().
239  *
240  * \param       preset  Compression preset (level and possible flags)
241  *
242  * \return      Number of bytes of memory required for the given
243  *              preset when encoding. If an error occurs, for example
244  *              due to unsupported preset, UINT64_MAX is returned.
245  */
246 extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
247 		lzma_nothrow lzma_attr_pure;
248 
249 
250 /**
251  * \brief       Calculate approximate decoder memory usage of a preset
252  *
253  * This function is a wrapper for lzma_raw_decoder_memusage().
254  *
255  * \param       preset  Compression preset (level and possible flags)
256  *
257  * \return      Number of bytes of memory required to decompress a file
258  *              that was compressed using the given preset. If an error
259  *              occurs, for example due to unsupported preset, UINT64_MAX
260  *              is returned.
261  */
262 extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
263 		lzma_nothrow lzma_attr_pure;
264 
265 
266 /**
267  * \brief       Initialize .xz Stream encoder using a preset number
268  *
269  * This function is intended for those who just want to use the basic features
270  * if liblzma (that is, most developers out there).
271  *
272  * \param       strm    Pointer to lzma_stream that is at least initialized
273  *                      with LZMA_STREAM_INIT.
274  * \param       preset  Compression preset to use. A preset consist of level
275  *                      number and zero or more flags. Usually flags aren't
276  *                      used, so preset is simply a number [0, 9] which match
277  *                      the options -0 ... -9 of the xz command line tool.
278  *                      Additional flags can be be set using bitwise-or with
279  *                      the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
280  * \param       check   Integrity check type to use. See check.h for available
281  *                      checks. The xz command line tool defaults to
282  *                      LZMA_CHECK_CRC64, which is a good choice if you are
283  *                      unsure. LZMA_CHECK_CRC32 is good too as long as the
284  *                      uncompressed file is not many gigabytes.
285  *
286  * \return      - LZMA_OK: Initialization succeeded. Use lzma_code() to
287  *                encode your data.
288  *              - LZMA_MEM_ERROR: Memory allocation failed.
289  *              - LZMA_OPTIONS_ERROR: The given compression preset is not
290  *                supported by this build of liblzma.
291  *              - LZMA_UNSUPPORTED_CHECK: The given check type is not
292  *                supported by this liblzma build.
293  *              - LZMA_PROG_ERROR: One or more of the parameters have values
294  *                that will never be valid. For example, strm == NULL.
295  *
296  * If initialization fails (return value is not LZMA_OK), all the memory
297  * allocated for *strm by liblzma is always freed. Thus, there is no need
298  * to call lzma_end() after failed initialization.
299  *
300  * If initialization succeeds, use lzma_code() to do the actual encoding.
301  * Valid values for `action' (the second argument of lzma_code()) are
302  * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
303  * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
304  */
305 extern LZMA_API(lzma_ret) lzma_easy_encoder(
306 		lzma_stream *strm, uint32_t preset, lzma_check check)
307 		lzma_nothrow lzma_attr_warn_unused_result;
308 
309 
310 /**
311  * \brief       Single-call .xz Stream encoding using a preset number
312  *
313  * The maximum required output buffer size can be calculated with
314  * lzma_stream_buffer_bound().
315  *
316  * \param       preset      Compression preset to use. See the description
317  *                          in lzma_easy_encoder().
318  * \param       check       Type of the integrity check to calculate from
319  *                          uncompressed data.
320  * \param       allocator   lzma_allocator for custom allocator functions.
321  *                          Set to NULL to use malloc() and free().
322  * \param       in          Beginning of the input buffer
323  * \param       in_size     Size of the input buffer
324  * \param       out         Beginning of the output buffer
325  * \param       out_pos     The next byte will be written to out[*out_pos].
326  *                          *out_pos is updated only if encoding succeeds.
327  * \param       out_size    Size of the out buffer; the first byte into
328  *                          which no data is written to is out[out_size].
329  *
330  * \return      - LZMA_OK: Encoding was successful.
331  *              - LZMA_BUF_ERROR: Not enough output buffer space.
332  *              - LZMA_UNSUPPORTED_CHECK
333  *              - LZMA_OPTIONS_ERROR
334  *              - LZMA_MEM_ERROR
335  *              - LZMA_DATA_ERROR
336  *              - LZMA_PROG_ERROR
337  */
338 extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
339 		uint32_t preset, lzma_check check,
340 		const lzma_allocator *allocator,
341 		const uint8_t *in, size_t in_size,
342 		uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
343 
344 
345 /**
346  * \brief       Initialize .xz Stream encoder using a custom filter chain
347  *
348  * \param       strm    Pointer to properly prepared lzma_stream
349  * \param       filters Array of filters. This must be terminated with
350  *                      filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for
351  *                      more information.
352  * \param       check   Type of the integrity check to calculate from
353  *                      uncompressed data.
354  *
355  * \return      - LZMA_OK: Initialization was successful.
356  *              - LZMA_MEM_ERROR
357  *              - LZMA_UNSUPPORTED_CHECK
358  *              - LZMA_OPTIONS_ERROR
359  *              - LZMA_PROG_ERROR
360  */
361 extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
362 		const lzma_filter *filters, lzma_check check)
363 		lzma_nothrow lzma_attr_warn_unused_result;
364 
365 
366 /**
367  * \brief       Calculate approximate memory usage of multithreaded .xz encoder
368  *
369  * Since doing the encoding in threaded mode doesn't affect the memory
370  * requirements of single-threaded decompressor, you can use
371  * lzma_easy_decoder_memusage(options->preset) or
372  * lzma_raw_decoder_memusage(options->filters) to calculate
373  * the decompressor memory requirements.
374  *
375  * \param       options Compression options
376  *
377  * \return      Number of bytes of memory required for encoding with the
378  *              given options. If an error occurs, for example due to
379  *              unsupported preset or filter chain, UINT64_MAX is returned.
380  */
381 extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
382 		const lzma_mt *options) lzma_nothrow lzma_attr_pure;
383 
384 
385 /**
386  * \brief       Initialize multithreaded .xz Stream encoder
387  *
388  * This provides the functionality of lzma_easy_encoder() and
389  * lzma_stream_encoder() as a single function for multithreaded use.
390  *
391  * The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH,
392  * LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be
393  * added in the future.
394  *
395  * \param       strm    Pointer to properly prepared lzma_stream
396  * \param       options Pointer to multithreaded compression options
397  *
398  * \return      - LZMA_OK
399  *              - LZMA_MEM_ERROR
400  *              - LZMA_UNSUPPORTED_CHECK
401  *              - LZMA_OPTIONS_ERROR
402  *              - LZMA_PROG_ERROR
403  */
404 extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
405 		lzma_stream *strm, const lzma_mt *options)
406 		lzma_nothrow lzma_attr_warn_unused_result;
407 
408 
409 /**
410  * \brief       Initialize .lzma encoder (legacy file format)
411  *
412  * The .lzma format is sometimes called the LZMA_Alone format, which is the
413  * reason for the name of this function. The .lzma format supports only the
414  * LZMA1 filter. There is no support for integrity checks like CRC32.
415  *
416  * Use this function if and only if you need to create files readable by
417  * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
418  * is strongly recommended.
419  *
420  * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
421  * No kind of flushing is supported, because the file format doesn't make
422  * it possible.
423  *
424  * \return      - LZMA_OK
425  *              - LZMA_MEM_ERROR
426  *              - LZMA_OPTIONS_ERROR
427  *              - LZMA_PROG_ERROR
428  */
429 extern LZMA_API(lzma_ret) lzma_alone_encoder(
430 		lzma_stream *strm, const lzma_options_lzma *options)
431 		lzma_nothrow lzma_attr_warn_unused_result;
432 
433 
434 /**
435  * \brief       Calculate output buffer size for single-call Stream encoder
436  *
437  * When trying to compress uncompressible data, the encoded size will be
438  * slightly bigger than the input data. This function calculates how much
439  * output buffer space is required to be sure that lzma_stream_buffer_encode()
440  * doesn't return LZMA_BUF_ERROR.
441  *
442  * The calculated value is not exact, but it is guaranteed to be big enough.
443  * The actual maximum output space required may be slightly smaller (up to
444  * about 100 bytes). This should not be a problem in practice.
445  *
446  * If the calculated maximum size doesn't fit into size_t or would make the
447  * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
448  * zero is returned to indicate the error.
449  *
450  * \note        The limit calculated by this function applies only to
451  *              single-call encoding. Multi-call encoding may (and probably
452  *              will) have larger maximum expansion when encoding
453  *              uncompressible data. Currently there is no function to
454  *              calculate the maximum expansion of multi-call encoding.
455  */
456 extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size)
457 		lzma_nothrow;
458 
459 
460 /**
461  * \brief       Single-call .xz Stream encoder
462  *
463  * \param       filters     Array of filters. This must be terminated with
464  *                          filters[n].id = LZMA_VLI_UNKNOWN. See filter.h
465  *                          for more information.
466  * \param       check       Type of the integrity check to calculate from
467  *                          uncompressed data.
468  * \param       allocator   lzma_allocator for custom allocator functions.
469  *                          Set to NULL to use malloc() and free().
470  * \param       in          Beginning of the input buffer
471  * \param       in_size     Size of the input buffer
472  * \param       out         Beginning of the output buffer
473  * \param       out_pos     The next byte will be written to out[*out_pos].
474  *                          *out_pos is updated only if encoding succeeds.
475  * \param       out_size    Size of the out buffer; the first byte into
476  *                          which no data is written to is out[out_size].
477  *
478  * \return      - LZMA_OK: Encoding was successful.
479  *              - LZMA_BUF_ERROR: Not enough output buffer space.
480  *              - LZMA_UNSUPPORTED_CHECK
481  *              - LZMA_OPTIONS_ERROR
482  *              - LZMA_MEM_ERROR
483  *              - LZMA_DATA_ERROR
484  *              - LZMA_PROG_ERROR
485  */
486 extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
487 		lzma_filter *filters, lzma_check check,
488 		const lzma_allocator *allocator,
489 		const uint8_t *in, size_t in_size,
490 		uint8_t *out, size_t *out_pos, size_t out_size)
491 		lzma_nothrow lzma_attr_warn_unused_result;
492 
493 
494 /**
495  * \brief       MicroLZMA encoder
496  *
497  * The MicroLZMA format is a raw LZMA stream whose first byte (always 0x00)
498  * has been replaced with bitwise-negation of the LZMA properties (lc/lp/pb).
499  * This encoding ensures that the first byte of MicroLZMA stream is never
500  * 0x00. There is no end of payload marker and thus the uncompressed size
501  * must be stored separately. For the best error detection the dictionary
502  * size should be stored separately as well but alternatively one may use
503  * the uncompressed size as the dictionary size when decoding.
504  *
505  * With the MicroLZMA encoder, lzma_code() behaves slightly unusually.
506  * The action argument must be LZMA_FINISH and the return value will never be
507  * LZMA_OK. Thus the encoding is always done with a single lzma_code() after
508  * the initialization. The benefit of the combination of initialization
509  * function and lzma_code() is that memory allocations can be re-used for
510  * better performance.
511  *
512  * lzma_code() will try to encode as much input as is possible to fit into
513  * the given output buffer. If not all input can be encoded, the stream will
514  * be finished without encoding all the input. The caller must check both
515  * input and output buffer usage after lzma_code() (total_in and total_out
516  * in lzma_stream can be convenient). Often lzma_code() can fill the output
517  * buffer completely if there is a lot of input, but sometimes a few bytes
518  * may remain unused because the next LZMA symbol would require more space.
519  *
520  * lzma_stream.avail_out must be at least 6. Otherwise LZMA_PROG_ERROR
521  * will be returned.
522  *
523  * The LZMA dictionary should be reasonably low to speed up the encoder
524  * re-initialization. A good value is bigger than the resulting
525  * uncompressed size of most of the output chunks. For example, if output
526  * size is 4 KiB, dictionary size of 32 KiB or 64 KiB is good. If the
527  * data compresses extremely well, even 128 KiB may be useful.
528  *
529  * The MicroLZMA format and this encoder variant were made with the EROFS
530  * file system in mind. This format may be convenient in other embedded
531  * uses too where many small streams are needed. XZ Embedded includes a
532  * decoder for this format.
533  *
534  * \return      - LZMA_STREAM_END: All good. Check the amounts of input used
535  *                and output produced. Store the amount of input used
536  *                (uncompressed size) as it needs to be known to decompress
537  *                the data.
538  *              - LZMA_OPTIONS_ERROR
539  *              - LZMA_MEM_ERROR
540  *              - LZMA_PROG_ERROR: In addition to the generic reasons for this
541  *                error code, this may also be returned if there isn't enough
542  *                output space (6 bytes) to create a valid MicroLZMA stream.
543  */
544 extern LZMA_API(lzma_ret) lzma_microlzma_encoder(
545 		lzma_stream *strm, const lzma_options_lzma *options);
546 
547 
548 /************
549  * Decoding *
550  ************/
551 
552 /**
553  * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
554  * being decoded has no integrity check. Note that when used with
555  * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
556  * if LZMA_TELL_NO_CHECK is used.
557  */
558 #define LZMA_TELL_NO_CHECK              UINT32_C(0x01)
559 
560 
561 /**
562  * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
563  * stream has an integrity check, but the type of the integrity check is not
564  * supported by this liblzma version or build. Such files can still be
565  * decoded, but the integrity check cannot be verified.
566  */
567 #define LZMA_TELL_UNSUPPORTED_CHECK     UINT32_C(0x02)
568 
569 
570 /**
571  * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
572  * of the integrity check is known. The type can then be got with
573  * lzma_get_check().
574  */
575 #define LZMA_TELL_ANY_CHECK             UINT32_C(0x04)
576 
577 
578 /**
579  * This flag makes lzma_code() not calculate and verify the integrity check
580  * of the compressed data in .xz files. This means that invalid integrity
581  * check values won't be detected and LZMA_DATA_ERROR won't be returned in
582  * such cases.
583  *
584  * This flag only affects the checks of the compressed data itself; the CRC32
585  * values in the .xz headers will still be verified normally.
586  *
587  * Don't use this flag unless you know what you are doing. Possible reasons
588  * to use this flag:
589  *
590  *   - Trying to recover data from a corrupt .xz file.
591  *
592  *   - Speeding up decompression, which matters mostly with SHA-256
593  *     or with files that have compressed extremely well. It's recommended
594  *     to not use this flag for this purpose unless the file integrity is
595  *     verified externally in some other way.
596  *
597  * Support for this flag was added in liblzma 5.1.4beta.
598  */
599 #define LZMA_IGNORE_CHECK               UINT32_C(0x10)
600 
601 
602 /**
603  * This flag enables decoding of concatenated files with file formats that
604  * allow concatenating compressed files as is. From the formats currently
605  * supported by liblzma, only the .xz and .lz formats allow concatenated
606  * files. Concatenated files are not allowed with the legacy .lzma format.
607  *
608  * This flag also affects the usage of the `action' argument for lzma_code().
609  * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
610  * unless LZMA_FINISH is used as `action'. Thus, the application has to set
611  * LZMA_FINISH in the same way as it does when encoding.
612  *
613  * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
614  * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
615  */
616 #define LZMA_CONCATENATED               UINT32_C(0x08)
617 
618 
619 /**
620  * This flag makes the threaded decoder report errors (like LZMA_DATA_ERROR)
621  * as soon as they are detected. This saves time when the application has no
622  * interest in a partially decompressed truncated or corrupt file. Note that
623  * due to timing randomness, if the same truncated or corrupt input is
624  * decompressed multiple times with this flag, a different amount of output
625  * may be produced by different runs, and even the error code might vary.
626  *
627  * When using LZMA_FAIL_FAST, it is recommended to use LZMA_FINISH to tell
628  * the decoder when no more input will be coming because it can help fast
629  * detection and reporting of truncated files. Note that in this situation
630  * truncated files might be diagnosed with LZMA_DATA_ERROR instead of
631  * LZMA_OK or LZMA_BUF_ERROR!
632  *
633  * Without this flag the threaded decoder will provide as much output as
634  * possible at first and then report the pending error. This default behavior
635  * matches the single-threaded decoder and provides repeatable behavior
636  * with truncated or corrupt input. There are a few special cases where the
637  * behavior can still differ like memory allocation failures (LZMA_MEM_ERROR).
638  *
639  * Single-threaded decoders currently ignore this flag.
640  *
641  * Support for this flag was added in liblzma 5.3.3alpha. Note that in older
642  * versions this flag isn't supported (LZMA_OPTIONS_ERROR) even by functions
643  * that ignore this flag in newer liblzma versions.
644  */
645 #define LZMA_FAIL_FAST                  UINT32_C(0x20)
646 
647 
648 /**
649  * \brief       Initialize .xz Stream decoder
650  *
651  * \param       strm        Pointer to properly prepared lzma_stream
652  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
653  *                          to effectively disable the limiter. liblzma
654  *                          5.2.3 and earlier don't allow 0 here and return
655  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
656  *                          had been specified.
657  * \param       flags       Bitwise-or of zero or more of the decoder flags:
658  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
659  *                          LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
660  *                          LZMA_CONCATENATED, LZMA_FAIL_FAST
661  *
662  * \return      - LZMA_OK: Initialization was successful.
663  *              - LZMA_MEM_ERROR: Cannot allocate memory.
664  *              - LZMA_OPTIONS_ERROR: Unsupported flags
665  *              - LZMA_PROG_ERROR
666  */
667 extern LZMA_API(lzma_ret) lzma_stream_decoder(
668 		lzma_stream *strm, uint64_t memlimit, uint32_t flags)
669 		lzma_nothrow lzma_attr_warn_unused_result;
670 
671 
672 /**
673  * \brief       Initialize multithreaded .xz Stream decoder
674  *
675  * \param       strm        Pointer to properly prepared lzma_stream
676  * \param       options     Pointer to multithreaded compression options
677  *
678  * The decoder can decode multiple Blocks in parallel. This requires that each
679  * Block Header contains the Compressed Size and Uncompressed size fields
680  * which are added by the multi-threaded encoder, see lzma_stream_encoder_mt().
681  *
682  * A Stream with one Block will only utilize one thread. A Stream with multiple
683  * Blocks but without size information in Block Headers will be processed in
684  * single-threaded mode in the same way as done by lzma_stream_decoder().
685  * Concatenated Streams are processed one Stream at a time; no inter-Stream
686  * parallelization is done.
687  *
688  * This function behaves like lzma_stream_decoder() when options->threads == 1
689  * and options->memlimit_threading <= 1.
690  *
691  * \return      - LZMA_OK: Initialization was successful.
692  *              - LZMA_MEM_ERROR: Cannot allocate memory.
693  *              - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
694  *              - LZMA_OPTIONS_ERROR: Unsupported flags.
695  *              - LZMA_PROG_ERROR
696  */
697 extern LZMA_API(lzma_ret) lzma_stream_decoder_mt(
698 		lzma_stream *strm, const lzma_mt *options)
699 		lzma_nothrow lzma_attr_warn_unused_result;
700 
701 
702 /**
703  * \brief       Decode .xz, .lzma, and .lz (lzip) files with autodetection
704  *
705  * This decoder autodetects between the .xz, .lzma, and .lz file formats,
706  * and calls lzma_stream_decoder(), lzma_alone_decoder(), or
707  * lzma_lzip_decoder() once the type of the input file has been detected.
708  *
709  * Support for .lz was added in 5.4.0.
710  *
711  * If the flag LZMA_CONCATENATED is used and the input is a .lzma file:
712  * For historical reasons concatenated .lzma files aren't supported.
713  * If there is trailing data after one .lzma stream, lzma_code() will
714  * return LZMA_DATA_ERROR. (lzma_alone_decoder() doesn't have such a check
715  * as it doesn't support any decoder flags. It will return LZMA_STREAM_END
716  * after one .lzma stream.)
717  *
718  * \param       strm        Pointer to properly prepared lzma_stream
719  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
720  *                          to effectively disable the limiter. liblzma
721  *                          5.2.3 and earlier don't allow 0 here and return
722  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
723  *                          had been specified.
724  * \param       flags       Bitwise-or of zero or more of the decoder flags:
725  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
726  *                          LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
727  *                          LZMA_CONCATENATED, LZMA_FAIL_FAST
728  *
729  * \return      - LZMA_OK: Initialization was successful.
730  *              - LZMA_MEM_ERROR: Cannot allocate memory.
731  *              - LZMA_OPTIONS_ERROR: Unsupported flags
732  *              - LZMA_PROG_ERROR
733  */
734 extern LZMA_API(lzma_ret) lzma_auto_decoder(
735 		lzma_stream *strm, uint64_t memlimit, uint32_t flags)
736 		lzma_nothrow lzma_attr_warn_unused_result;
737 
738 
739 /**
740  * \brief       Initialize .lzma decoder (legacy file format)
741  *
742  * \param       strm        Pointer to properly prepared lzma_stream
743  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
744  *                          to effectively disable the limiter. liblzma
745  *                          5.2.3 and earlier don't allow 0 here and return
746  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
747  *                          had been specified.
748  *
749  * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
750  * There is no need to use LZMA_FINISH, but it's allowed because it may
751  * simplify certain types of applications.
752  *
753  * \return      - LZMA_OK
754  *              - LZMA_MEM_ERROR
755  *              - LZMA_PROG_ERROR
756  */
757 extern LZMA_API(lzma_ret) lzma_alone_decoder(
758 		lzma_stream *strm, uint64_t memlimit)
759 		lzma_nothrow lzma_attr_warn_unused_result;
760 
761 
762 /**
763  * \brief       Initialize .lz (lzip) decoder (a foreign file format)
764  *
765  * \param       strm        Pointer to properly prepared lzma_stream
766  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
767  *                          to effectively disable the limiter.
768  * \param       flags       Bitwise-or of flags, or zero for no flags.
769  *                          All decoder flags listed above are supported
770  *                          although only LZMA_CONCATENATED and (in very rare
771  *                          cases) LZMA_IGNORE_CHECK are actually useful.
772  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
773  *                          and LZMA_FAIL_FAST do nothing. LZMA_TELL_ANY_CHECK
774  *                          is supported for consistency only as CRC32 is
775  *                          always used in the .lz format.
776  *
777  * This decoder supports the .lz format version 0 and the unextended .lz
778  * format version 1:
779  *
780  *   - Files in the format version 0 were produced by lzip 1.3 and older.
781  *     Such files aren't common but may be found from file archives
782  *     as a few source packages were released in this format. People
783  *     might have old personal files in this format too. Decompression
784  *     support for the format version 0 was removed in lzip 1.18.
785  *
786  *   - lzip 1.3 added decompression support for .lz format version 1 files.
787  *     Compression support was added in lzip 1.4. In lzip 1.6 the .lz format
788  *     version 1 was extended to support the Sync Flush marker. This extension
789  *     is not supported by liblzma. lzma_code() will return LZMA_DATA_ERROR
790  *     at the location of the Sync Flush marker. In practice files with
791  *     the Sync Flush marker are very rare and thus liblzma can decompress
792  *     almost all .lz files.
793  *
794  * Just like with lzma_stream_decoder() for .xz files, LZMA_CONCATENATED
795  * should be used when decompressing normal standalone .lz files.
796  *
797  * The .lz format allows putting non-.lz data at the end of a file after at
798  * least one valid .lz member. That is, one can append custom data at the end
799  * of a .lz file and the decoder is required to ignore it. In liblzma this
800  * is relevant only when LZMA_CONCATENATED is used. In that case lzma_code()
801  * will return LZMA_STREAM_END and leave lzma_stream.next_in pointing to
802  * the first byte of the non-.lz data. An exception to this is if the first
803  * 1-3 bytes of the non-.lz data are identical to the .lz magic bytes
804  * (0x4C, 0x5A, 0x49, 0x50; "LZIP" in US-ASCII). In such a case the 1-3 bytes
805  * will have been ignored by lzma_code(). If one wishes to locate the non-.lz
806  * data reliably, one must ensure that the first byte isn't 0x4C. Actually
807  * one should ensure that none of the first four bytes of trailing data are
808  * equal to the magic bytes because lzip >= 1.20 requires it by default.
809  *
810  * \return      - LZMA_OK: Initialization was successful.
811  *              - LZMA_MEM_ERROR: Cannot allocate memory.
812  *              - LZMA_OPTIONS_ERROR: Unsupported flags
813  *              - LZMA_PROG_ERROR
814  */
815 extern LZMA_API(lzma_ret) lzma_lzip_decoder(
816 		lzma_stream *strm, uint64_t memlimit, uint32_t flags)
817 		lzma_nothrow lzma_attr_warn_unused_result;
818 
819 
820 /**
821  * \brief       Single-call .xz Stream decoder
822  *
823  * \param       memlimit    Pointer to how much memory the decoder is allowed
824  *                          to allocate. The value pointed by this pointer is
825  *                          modified if and only if LZMA_MEMLIMIT_ERROR is
826  *                          returned.
827  * \param       flags       Bitwise-or of zero or more of the decoder flags:
828  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
829  *                          LZMA_IGNORE_CHECK, LZMA_CONCATENATED,
830  *                          LZMA_FAIL_FAST. Note that LZMA_TELL_ANY_CHECK
831  *                          is not allowed and will return LZMA_PROG_ERROR.
832  * \param       allocator   lzma_allocator for custom allocator functions.
833  *                          Set to NULL to use malloc() and free().
834  * \param       in          Beginning of the input buffer
835  * \param       in_pos      The next byte will be read from in[*in_pos].
836  *                          *in_pos is updated only if decoding succeeds.
837  * \param       in_size     Size of the input buffer; the first byte that
838  *                          won't be read is in[in_size].
839  * \param       out         Beginning of the output buffer
840  * \param       out_pos     The next byte will be written to out[*out_pos].
841  *                          *out_pos is updated only if decoding succeeds.
842  * \param       out_size    Size of the out buffer; the first byte into
843  *                          which no data is written to is out[out_size].
844  *
845  * \return      - LZMA_OK: Decoding was successful.
846  *              - LZMA_FORMAT_ERROR
847  *              - LZMA_OPTIONS_ERROR
848  *              - LZMA_DATA_ERROR
849  *              - LZMA_NO_CHECK: This can be returned only if using
850  *                the LZMA_TELL_NO_CHECK flag.
851  *              - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
852  *                the LZMA_TELL_UNSUPPORTED_CHECK flag.
853  *              - LZMA_MEM_ERROR
854  *              - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
855  *                The minimum required memlimit value was stored to *memlimit.
856  *              - LZMA_BUF_ERROR: Output buffer was too small.
857  *              - LZMA_PROG_ERROR
858  */
859 extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
860 		uint64_t *memlimit, uint32_t flags,
861 		const lzma_allocator *allocator,
862 		const uint8_t *in, size_t *in_pos, size_t in_size,
863 		uint8_t *out, size_t *out_pos, size_t out_size)
864 		lzma_nothrow lzma_attr_warn_unused_result;
865 
866 
867 /**
868  * \brief       MicroLZMA decoder
869  *
870  * See lzma_microlzma_decoder() for more information.
871  *
872  * The lzma_code() usage with this decoder is completely normal. The
873  * special behavior of lzma_code() applies to lzma_microlzma_encoder() only.
874  *
875  * \param       strm        Pointer to properly prepared lzma_stream
876  * \param       comp_size   Compressed size of the MicroLZMA stream.
877  *                          The caller must somehow know this exactly.
878  * \param       uncomp_size Uncompressed size of the MicroLZMA stream.
879  *                          If the exact uncompressed size isn't known, this
880  *                          can be set to a value that is at most as big as
881  *                          the exact uncompressed size would be, but then the
882  *                          next argument uncomp_size_is_exact must be false.
883  * \param       uncomp_size_is_exact
884  *                          If true, uncomp_size must be exactly correct.
885  *                          This will improve error detection at the end of
886  *                          the stream. If the exact uncompressed size isn't
887  *                          known, this must be false. uncomp_size must still
888  *                          be at most as big as the exact uncompressed size
889  *                          is. Setting this to false when the exact size is
890  *                          known will work but error detection at the end of
891  *                          the stream will be weaker.
892  * \param       dict_size   LZMA dictionary size that was used when
893  *                          compressing the data. It is OK to use a bigger
894  *                          value too but liblzma will then allocate more
895  *                          memory than would actually be required and error
896  *                          detection will be slightly worse. (Note that with
897  *                          the implementation in XZ Embedded it doesn't
898  *                          affect the memory usage if one specifies bigger
899  *                          dictionary than actually required.)
900  */
901 extern LZMA_API(lzma_ret) lzma_microlzma_decoder(
902 		lzma_stream *strm, uint64_t comp_size,
903 		uint64_t uncomp_size, lzma_bool uncomp_size_is_exact,
904 		uint32_t dict_size);
905