Put the unstable APIs behind #ifdef LZMA_UNSTABLE.
[xz/debian.git] / src / liblzma / api / lzma / container.h
blob499d8b9aca3cedef4056e9ac8eed819b72daea3a
1 /**
2 * \file lzma/container.h
3 * \brief File formats
4 */
6 /*
7 * Author: Lasse Collin
9 * This file has been put into the public domain.
10 * You can do whatever you want with this file.
12 * See ../lzma.h for information about liblzma as a whole.
15 #ifndef LZMA_H_INTERNAL
16 # error Never include this file directly. Use <lzma.h> instead.
17 #endif
20 /************
21 * Encoding *
22 ************/
24 /**
25 * \brief Default compression preset
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.
31 #define LZMA_PRESET_DEFAULT UINT32_C(6)
34 /**
35 * \brief Mask for preset level
37 * This is useful only if you need to extract the level from the preset
38 * variable. That should be rare.
40 #define LZMA_PRESET_LEVEL_MASK UINT32_C(0x1F)
44 * Preset flags
46 * Currently only one flag is defined.
49 /**
50 * \brief Extreme compression preset
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.
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).
60 #define LZMA_PRESET_EXTREME (UINT32_C(1) << 31)
63 #ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
64 /**
65 * \brief Multithreading options
67 typedef struct {
68 /**
69 * \brief Flags
71 * Set this to zero if no flags are wanted.
73 * No flags are currently supported.
75 uint32_t flags;
77 /**
78 * \brief Number of worker threads to use
80 uint32_t threads;
82 /**
83 * \brief Maximum uncompressed size of a Block
85 * The encoder will start a new .xz Block every block_size bytes.
86 * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
87 * the caller may tell liblzma to start a new Block earlier.
89 * With LZMA2, a recommended block size is 2-4 times the LZMA2
90 * dictionary size. With very small dictionaries, it is recommended
91 * to use at least 1 MiB block size for good compression ratio, even
92 * if this is more than four times the dictionary size. Note that
93 * these are only recommendations for typical use cases; feel free
94 * to use other values. Just keep in mind that using a block size
95 * less than the LZMA2 dictionary size is waste of RAM.
97 * Set this to 0 to let liblzma choose the block size depending
98 * on the compression options. For LZMA2 it will be 3*dict_size
99 * or 1 MiB, whichever is more.
101 uint64_t block_size;
104 * \brief Timeout to allow lzma_code() to return early
106 * Multithreading can make liblzma to consume input and produce
107 * output in a very bursty way: it may first read a lot of input
108 * to fill internal buffers, then no input or output occurs for
109 * a while.
111 * In single-threaded mode, lzma_code() won't return until it has
112 * either consumed all the input or filled the output buffer. If
113 * this is done in multithreaded mode, it may cause a call
114 * lzma_code() to take even tens of seconds, which isn't acceptable
115 * in all applications.
117 * To avoid very long blocking times in lzma_code(), a timeout
118 * (in milliseconds) may be set here. If lzma_code() would block
119 * longer than this number of milliseconds, it will return with
120 * LZMA_OK. Reasonable values are 100 ms or more. The xz command
121 * line tool uses 300 ms.
123 * If long blocking times are fine for you, set timeout to a special
124 * value of 0, which will disable the timeout mechanism and will make
125 * lzma_code() block until all the input is consumed or the output
126 * buffer has been filled.
128 * \note Even with a timeout, lzma_code() might sometimes take
129 * somewhat long time to return. No timing guarantees
130 * are made.
132 uint32_t timeout;
135 * \brief Compression preset (level and possible flags)
137 * The preset is set just like with lzma_easy_encoder().
138 * The preset is ignored if filters below is non-NULL.
140 uint32_t preset;
143 * \brief Filter chain (alternative to a preset)
145 * If this is NULL, the preset above is used. Otherwise the preset
146 * is ignored and the filter chain specified here is used.
148 const lzma_filter *filters;
151 * \brief Integrity check type
153 * See check.h for available checks. The xz command line tool
154 * defaults to LZMA_CHECK_CRC64, which is a good choice if you
155 * are unsure.
157 lzma_check check;
160 * Reserved space to allow possible future extensions without
161 * breaking the ABI. You should not touch these, because the names
162 * of these variables may change. These are and will never be used
163 * with the currently supported options, so it is safe to leave these
164 * uninitialized.
166 lzma_reserved_enum reserved_enum1;
167 lzma_reserved_enum reserved_enum2;
168 lzma_reserved_enum reserved_enum3;
169 uint32_t reserved_int1;
170 uint32_t reserved_int2;
171 uint32_t reserved_int3;
172 uint32_t reserved_int4;
173 uint64_t reserved_int5;
174 uint64_t reserved_int6;
175 uint64_t reserved_int7;
176 uint64_t reserved_int8;
177 void *reserved_ptr1;
178 void *reserved_ptr2;
179 void *reserved_ptr3;
180 void *reserved_ptr4;
182 } lzma_mt;
183 #endif
187 * \brief Calculate approximate memory usage of easy encoder
189 * This function is a wrapper for lzma_raw_encoder_memusage().
191 * \param preset Compression preset (level and possible flags)
193 * \return Number of bytes of memory required for the given
194 * preset when encoding. If an error occurs, for example
195 * due to unsupported preset, UINT64_MAX is returned.
197 extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
198 lzma_nothrow lzma_attr_pure;
202 * \brief Calculate approximate decoder memory usage of a preset
204 * This function is a wrapper for lzma_raw_decoder_memusage().
206 * \param preset Compression preset (level and possible flags)
208 * \return Number of bytes of memory required to decompress a file
209 * that was compressed using the given preset. If an error
210 * occurs, for example due to unsupported preset, UINT64_MAX
211 * is returned.
213 extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
214 lzma_nothrow lzma_attr_pure;
218 * \brief Initialize .xz Stream encoder using a preset number
220 * This function is intended for those who just want to use the basic features
221 * if liblzma (that is, most developers out there).
223 * \param strm Pointer to lzma_stream that is at least initialized
224 * with LZMA_STREAM_INIT.
225 * \param preset Compression preset to use. A preset consist of level
226 * number and zero or more flags. Usually flags aren't
227 * used, so preset is simply a number [0, 9] which match
228 * the options -0 ... -9 of the xz command line tool.
229 * Additional flags can be be set using bitwise-or with
230 * the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
231 * \param check Integrity check type to use. See check.h for available
232 * checks. The xz command line tool defaults to
233 * LZMA_CHECK_CRC64, which is a good choice if you are
234 * unsure. LZMA_CHECK_CRC32 is good too as long as the
235 * uncompressed file is not many gigabytes.
237 * \return - LZMA_OK: Initialization succeeded. Use lzma_code() to
238 * encode your data.
239 * - LZMA_MEM_ERROR: Memory allocation failed.
240 * - LZMA_OPTIONS_ERROR: The given compression preset is not
241 * supported by this build of liblzma.
242 * - LZMA_UNSUPPORTED_CHECK: The given check type is not
243 * supported by this liblzma build.
244 * - LZMA_PROG_ERROR: One or more of the parameters have values
245 * that will never be valid. For example, strm == NULL.
247 * If initialization fails (return value is not LZMA_OK), all the memory
248 * allocated for *strm by liblzma is always freed. Thus, there is no need
249 * to call lzma_end() after failed initialization.
251 * If initialization succeeds, use lzma_code() to do the actual encoding.
252 * Valid values for `action' (the second argument of lzma_code()) are
253 * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
254 * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
256 extern LZMA_API(lzma_ret) lzma_easy_encoder(
257 lzma_stream *strm, uint32_t preset, lzma_check check)
258 lzma_nothrow lzma_attr_warn_unused_result;
262 * \brief Single-call .xz Stream encoding using a preset number
264 * The maximum required output buffer size can be calculated with
265 * lzma_stream_buffer_bound().
267 * \param preset Compression preset to use. See the description
268 * in lzma_easy_encoder().
269 * \param check Type of the integrity check to calculate from
270 * uncompressed data.
271 * \param allocator lzma_allocator for custom allocator functions.
272 * Set to NULL to use malloc() and free().
273 * \param in Beginning of the input buffer
274 * \param in_size Size of the input buffer
275 * \param out Beginning of the output buffer
276 * \param out_pos The next byte will be written to out[*out_pos].
277 * *out_pos is updated only if encoding succeeds.
278 * \param out_size Size of the out buffer; the first byte into
279 * which no data is written to is out[out_size].
281 * \return - LZMA_OK: Encoding was successful.
282 * - LZMA_BUF_ERROR: Not enough output buffer space.
283 * - LZMA_UNSUPPORTED_CHECK
284 * - LZMA_OPTIONS_ERROR
285 * - LZMA_MEM_ERROR
286 * - LZMA_DATA_ERROR
287 * - LZMA_PROG_ERROR
289 extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
290 uint32_t preset, lzma_check check,
291 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
292 uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
296 * \brief Initialize .xz Stream encoder using a custom filter chain
298 * \param strm Pointer to properly prepared lzma_stream
299 * \param filters Array of filters. This must be terminated with
300 * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for
301 * more information.
302 * \param check Type of the integrity check to calculate from
303 * uncompressed data.
305 * \return - LZMA_OK: Initialization was successful.
306 * - LZMA_MEM_ERROR
307 * - LZMA_UNSUPPORTED_CHECK
308 * - LZMA_OPTIONS_ERROR
309 * - LZMA_PROG_ERROR
311 extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
312 const lzma_filter *filters, lzma_check check)
313 lzma_nothrow lzma_attr_warn_unused_result;
316 #ifdef LZMA_UNSTABLE /* Unstable API that may change. Use only for testing. */
318 * \brief Calculate approximate memory usage of multithreaded .xz encoder
320 * Since doing the encoding in threaded mode doesn't affect the memory
321 * requirements of single-threaded decompressor, you can use
322 * lzma_easy_decoder_memusage(options->preset) or
323 * lzma_raw_decoder_memusage(options->filters) to calculate
324 * the decompressor memory requirements.
326 * \param options Compression options
328 * \return Number of bytes of memory required for encoding with the
329 * given options. If an error occurs, for example due to
330 * unsupported preset or filter chain, UINT64_MAX is returned.
332 extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
333 const lzma_mt *options) lzma_nothrow lzma_attr_pure;
337 * \brief Initialize multithreaded .xz Stream encoder
339 * This provides the functionality of lzma_easy_encoder() and
340 * lzma_stream_encoder() as a single function for multithreaded use.
342 * TODO: For lzma_code(), only LZMA_RUN and LZMA_FINISH are currently
343 * supported. Support for other actions has been planned.
345 * \param strm Pointer to properly prepared lzma_stream
346 * \param options Pointer to multithreaded compression options
348 * \return - LZMA_OK
349 * - LZMA_MEM_ERROR
350 * - LZMA_UNSUPPORTED_CHECK
351 * - LZMA_OPTIONS_ERROR
352 * - LZMA_PROG_ERROR
354 extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
355 lzma_stream *strm, const lzma_mt *options)
356 lzma_nothrow lzma_attr_warn_unused_result;
357 #endif
361 * \brief Initialize .lzma encoder (legacy file format)
363 * The .lzma format is sometimes called the LZMA_Alone format, which is the
364 * reason for the name of this function. The .lzma format supports only the
365 * LZMA1 filter. There is no support for integrity checks like CRC32.
367 * Use this function if and only if you need to create files readable by
368 * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
369 * is strongly recommended.
371 * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
372 * No kind of flushing is supported, because the file format doesn't make
373 * it possible.
375 * \return - LZMA_OK
376 * - LZMA_MEM_ERROR
377 * - LZMA_OPTIONS_ERROR
378 * - LZMA_PROG_ERROR
380 extern LZMA_API(lzma_ret) lzma_alone_encoder(
381 lzma_stream *strm, const lzma_options_lzma *options)
382 lzma_nothrow lzma_attr_warn_unused_result;
386 * \brief Calculate output buffer size for single-call Stream encoder
388 * When trying to compress uncompressible data, the encoded size will be
389 * slightly bigger than the input data. This function calculates how much
390 * output buffer space is required to be sure that lzma_stream_buffer_encode()
391 * doesn't return LZMA_BUF_ERROR.
393 * The calculated value is not exact, but it is guaranteed to be big enough.
394 * The actual maximum output space required may be slightly smaller (up to
395 * about 100 bytes). This should not be a problem in practice.
397 * If the calculated maximum size doesn't fit into size_t or would make the
398 * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
399 * zero is returned to indicate the error.
401 * \note The limit calculated by this function applies only to
402 * single-call encoding. Multi-call encoding may (and probably
403 * will) have larger maximum expansion when encoding
404 * uncompressible data. Currently there is no function to
405 * calculate the maximum expansion of multi-call encoding.
407 extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size)
408 lzma_nothrow;
412 * \brief Single-call .xz Stream encoder
414 * \param filters Array of filters. This must be terminated with
415 * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h
416 * for more information.
417 * \param check Type of the integrity check to calculate from
418 * uncompressed data.
419 * \param allocator lzma_allocator for custom allocator functions.
420 * Set to NULL to use malloc() and free().
421 * \param in Beginning of the input buffer
422 * \param in_size Size of the input buffer
423 * \param out Beginning of the output buffer
424 * \param out_pos The next byte will be written to out[*out_pos].
425 * *out_pos is updated only if encoding succeeds.
426 * \param out_size Size of the out buffer; the first byte into
427 * which no data is written to is out[out_size].
429 * \return - LZMA_OK: Encoding was successful.
430 * - LZMA_BUF_ERROR: Not enough output buffer space.
431 * - LZMA_UNSUPPORTED_CHECK
432 * - LZMA_OPTIONS_ERROR
433 * - LZMA_MEM_ERROR
434 * - LZMA_DATA_ERROR
435 * - LZMA_PROG_ERROR
437 extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
438 lzma_filter *filters, lzma_check check,
439 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
440 uint8_t *out, size_t *out_pos, size_t out_size)
441 lzma_nothrow lzma_attr_warn_unused_result;
444 /************
445 * Decoding *
446 ************/
449 * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
450 * being decoded has no integrity check. Note that when used with
451 * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
452 * if LZMA_TELL_NO_CHECK is used.
454 #define LZMA_TELL_NO_CHECK UINT32_C(0x01)
458 * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
459 * stream has an integrity check, but the type of the integrity check is not
460 * supported by this liblzma version or build. Such files can still be
461 * decoded, but the integrity check cannot be verified.
463 #define LZMA_TELL_UNSUPPORTED_CHECK UINT32_C(0x02)
467 * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
468 * of the integrity check is known. The type can then be got with
469 * lzma_get_check().
471 #define LZMA_TELL_ANY_CHECK UINT32_C(0x04)
475 * This flag enables decoding of concatenated files with file formats that
476 * allow concatenating compressed files as is. From the formats currently
477 * supported by liblzma, only the .xz format allows concatenated files.
478 * Concatenated files are not allowed with the legacy .lzma format.
480 * This flag also affects the usage of the `action' argument for lzma_code().
481 * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
482 * unless LZMA_FINISH is used as `action'. Thus, the application has to set
483 * LZMA_FINISH in the same way as it does when encoding.
485 * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
486 * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
488 #define LZMA_CONCATENATED UINT32_C(0x08)
492 * \brief Initialize .xz Stream decoder
494 * \param strm Pointer to properly prepared lzma_stream
495 * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
496 * to effectively disable the limiter.
497 * \param flags Bitwise-or of zero or more of the decoder flags:
498 * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
499 * LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
501 * \return - LZMA_OK: Initialization was successful.
502 * - LZMA_MEM_ERROR: Cannot allocate memory.
503 * - LZMA_OPTIONS_ERROR: Unsupported flags
504 * - LZMA_PROG_ERROR
506 extern LZMA_API(lzma_ret) lzma_stream_decoder(
507 lzma_stream *strm, uint64_t memlimit, uint32_t flags)
508 lzma_nothrow lzma_attr_warn_unused_result;
512 * \brief Decode .xz Streams and .lzma files with autodetection
514 * This decoder autodetects between the .xz and .lzma file formats, and
515 * calls lzma_stream_decoder() or lzma_alone_decoder() once the type
516 * of the input file has been detected.
518 * \param strm Pointer to properly prepared lzma_stream
519 * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
520 * to effectively disable the limiter.
521 * \param flags Bitwise-or of flags, or zero for no flags.
523 * \return - LZMA_OK: Initialization was successful.
524 * - LZMA_MEM_ERROR: Cannot allocate memory.
525 * - LZMA_OPTIONS_ERROR: Unsupported flags
526 * - LZMA_PROG_ERROR
528 extern LZMA_API(lzma_ret) lzma_auto_decoder(
529 lzma_stream *strm, uint64_t memlimit, uint32_t flags)
530 lzma_nothrow lzma_attr_warn_unused_result;
534 * \brief Initialize .lzma decoder (legacy file format)
536 * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
537 * There is no need to use LZMA_FINISH, but allowing it may simplify
538 * certain types of applications.
540 * \return - LZMA_OK
541 * - LZMA_MEM_ERROR
542 * - LZMA_PROG_ERROR
544 extern LZMA_API(lzma_ret) lzma_alone_decoder(
545 lzma_stream *strm, uint64_t memlimit)
546 lzma_nothrow lzma_attr_warn_unused_result;
550 * \brief Single-call .xz Stream decoder
552 * \param memlimit Pointer to how much memory the decoder is allowed
553 * to allocate. The value pointed by this pointer is
554 * modified if and only if LZMA_MEMLIMIT_ERROR is
555 * returned.
556 * \param flags Bitwise-or of zero or more of the decoder flags:
557 * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
558 * LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK
559 * is not allowed and will return LZMA_PROG_ERROR.
560 * \param allocator lzma_allocator for custom allocator functions.
561 * Set to NULL to use malloc() and free().
562 * \param in Beginning of the input buffer
563 * \param in_pos The next byte will be read from in[*in_pos].
564 * *in_pos is updated only if decoding succeeds.
565 * \param in_size Size of the input buffer; the first byte that
566 * won't be read is in[in_size].
567 * \param out Beginning of the output buffer
568 * \param out_pos The next byte will be written to out[*out_pos].
569 * *out_pos is updated only if decoding succeeds.
570 * \param out_size Size of the out buffer; the first byte into
571 * which no data is written to is out[out_size].
573 * \return - LZMA_OK: Decoding was successful.
574 * - LZMA_FORMAT_ERROR
575 * - LZMA_OPTIONS_ERROR
576 * - LZMA_DATA_ERROR
577 * - LZMA_NO_CHECK: This can be returned only if using
578 * the LZMA_TELL_NO_CHECK flag.
579 * - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
580 * the LZMA_TELL_UNSUPPORTED_CHECK flag.
581 * - LZMA_MEM_ERROR
582 * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
583 * The minimum required memlimit value was stored to *memlimit.
584 * - LZMA_BUF_ERROR: Output buffer was too small.
585 * - LZMA_PROG_ERROR
587 extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
588 uint64_t *memlimit, uint32_t flags, lzma_allocator *allocator,
589 const uint8_t *in, size_t *in_pos, size_t in_size,
590 uint8_t *out, size_t *out_pos, size_t out_size)
591 lzma_nothrow lzma_attr_warn_unused_result;