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