Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:02:55

0001 /* SPDX-License-Identifier: 0BSD */
0002 
0003 /**
0004  * \file        lzma/container.h
0005  * \brief       File formats
0006  * \note        Never include this file directly. Use <lzma.h> instead.
0007  */
0008 
0009 /*
0010  * Author: Lasse Collin
0011  */
0012 
0013 #ifndef LZMA_H_INTERNAL
0014 #   error Never include this file directly. Use <lzma.h> instead.
0015 #endif
0016 
0017 
0018 /************
0019  * Encoding *
0020  ************/
0021 
0022 /**
0023  * \brief       Default compression preset
0024  *
0025  * It's not straightforward to recommend a default preset, because in some
0026  * cases keeping the resource usage relatively low is more important that
0027  * getting the maximum compression ratio.
0028  */
0029 #define LZMA_PRESET_DEFAULT     UINT32_C(6)
0030 
0031 
0032 /**
0033  * \brief       Mask for preset level
0034  *
0035  * This is useful only if you need to extract the level from the preset
0036  * variable. That should be rare.
0037  */
0038 #define LZMA_PRESET_LEVEL_MASK  UINT32_C(0x1F)
0039 
0040 
0041 /*
0042  * Preset flags
0043  *
0044  * Currently only one flag is defined.
0045  */
0046 
0047 /**
0048  * \brief       Extreme compression preset
0049  *
0050  * This flag modifies the preset to make the encoding significantly slower
0051  * while improving the compression ratio only marginally. This is useful
0052  * when you don't mind spending time to get as small result as possible.
0053  *
0054  * This flag doesn't affect the memory usage requirements of the decoder (at
0055  * least not significantly). The memory usage of the encoder may be increased
0056  * a little but only at the lowest preset levels (0-3).
0057  */
0058 #define LZMA_PRESET_EXTREME       (UINT32_C(1) << 31)
0059 
0060 
0061 /**
0062  * \brief       Multithreading options
0063  */
0064 typedef struct {
0065     /**
0066      * \brief       Flags
0067      *
0068      * Set this to zero if no flags are wanted.
0069      *
0070      * Encoder: No flags are currently supported.
0071      *
0072      * Decoder: Bitwise-or of zero or more of the decoder flags:
0073      * - LZMA_TELL_NO_CHECK
0074      * - LZMA_TELL_UNSUPPORTED_CHECK
0075      * - LZMA_TELL_ANY_CHECK
0076      * - LZMA_IGNORE_CHECK
0077      * - LZMA_CONCATENATED
0078      * - LZMA_FAIL_FAST
0079      */
0080     uint32_t flags;
0081 
0082     /**
0083      * \brief       Number of worker threads to use
0084      */
0085     uint32_t threads;
0086 
0087     /**
0088      * \brief       Encoder only: Maximum uncompressed size of a Block
0089      *
0090      * The encoder will start a new .xz Block every block_size bytes.
0091      * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
0092      * the caller may tell liblzma to start a new Block earlier.
0093      *
0094      * With LZMA2, a recommended block size is 2-4 times the LZMA2
0095      * dictionary size. With very small dictionaries, it is recommended
0096      * to use at least 1 MiB block size for good compression ratio, even
0097      * if this is more than four times the dictionary size. Note that
0098      * these are only recommendations for typical use cases; feel free
0099      * to use other values. Just keep in mind that using a block size
0100      * less than the LZMA2 dictionary size is waste of RAM.
0101      *
0102      * Set this to 0 to let liblzma choose the block size depending
0103      * on the compression options. For LZMA2 it will be 3*dict_size
0104      * or 1 MiB, whichever is more.
0105      *
0106      * For each thread, about 3 * block_size bytes of memory will be
0107      * allocated. This may change in later liblzma versions. If so,
0108      * the memory usage will probably be reduced, not increased.
0109      */
0110     uint64_t block_size;
0111 
0112     /**
0113      * \brief       Timeout to allow lzma_code() to return early
0114      *
0115      * Multithreading can make liblzma consume input and produce
0116      * output in a very bursty way: it may first read a lot of input
0117      * to fill internal buffers, then no input or output occurs for
0118      * a while.
0119      *
0120      * In single-threaded mode, lzma_code() won't return until it has
0121      * either consumed all the input or filled the output buffer. If
0122      * this is done in multithreaded mode, it may cause a call
0123      * lzma_code() to take even tens of seconds, which isn't acceptable
0124      * in all applications.
0125      *
0126      * To avoid very long blocking times in lzma_code(), a timeout
0127      * (in milliseconds) may be set here. If lzma_code() would block
0128      * longer than this number of milliseconds, it will return with
0129      * LZMA_OK. Reasonable values are 100 ms or more. The xz command
0130      * line tool uses 300 ms.
0131      *
0132      * If long blocking times are acceptable, set timeout to a special
0133      * value of 0. This will disable the timeout mechanism and will make
0134      * lzma_code() block until all the input is consumed or the output
0135      * buffer has been filled.
0136      *
0137      * \note        Even with a timeout, lzma_code() might sometimes take
0138      *              a long time to return. No timing guarantees are made.
0139      */
0140     uint32_t timeout;
0141 
0142     /**
0143      * \brief       Encoder only: Compression preset
0144      *
0145      * The preset is set just like with lzma_easy_encoder().
0146      * The preset is ignored if filters below is non-NULL.
0147      */
0148     uint32_t preset;
0149 
0150     /**
0151      * \brief       Encoder only: Filter chain (alternative to a preset)
0152      *
0153      * If this is NULL, the preset above is used. Otherwise the preset
0154      * is ignored and the filter chain specified here is used.
0155      */
0156     const lzma_filter *filters;
0157 
0158     /**
0159      * \brief       Encoder only: Integrity check type
0160      *
0161      * See check.h for available checks. The xz command line tool
0162      * defaults to LZMA_CHECK_CRC64, which is a good choice if you
0163      * are unsure.
0164      */
0165     lzma_check check;
0166 
0167     /*
0168      * Reserved space to allow possible future extensions without
0169      * breaking the ABI. You should not touch these, because the names
0170      * of these variables may change. These are and will never be used
0171      * with the currently supported options, so it is safe to leave these
0172      * uninitialized.
0173      */
0174     /** \private     Reserved member. */
0175     lzma_reserved_enum reserved_enum1;
0176 
0177     /** \private     Reserved member. */
0178     lzma_reserved_enum reserved_enum2;
0179 
0180     /** \private     Reserved member. */
0181     lzma_reserved_enum reserved_enum3;
0182 
0183     /** \private     Reserved member. */
0184     uint32_t reserved_int1;
0185 
0186     /** \private     Reserved member. */
0187     uint32_t reserved_int2;
0188 
0189     /** \private     Reserved member. */
0190     uint32_t reserved_int3;
0191 
0192     /** \private     Reserved member. */
0193     uint32_t reserved_int4;
0194 
0195     /**
0196      * \brief       Memory usage limit to reduce the number of threads
0197      *
0198      * Encoder: Ignored.
0199      *
0200      * Decoder:
0201      *
0202      * If the number of threads has been set so high that more than
0203      * memlimit_threading bytes of memory would be needed, the number
0204      * of threads will be reduced so that the memory usage will not exceed
0205      * memlimit_threading bytes. However, if memlimit_threading cannot
0206      * be met even in single-threaded mode, then decoding will continue
0207      * in single-threaded mode and memlimit_threading may be exceeded
0208      * even by a large amount. That is, memlimit_threading will never make
0209      * lzma_code() return LZMA_MEMLIMIT_ERROR. To truly cap the memory
0210      * usage, see memlimit_stop below.
0211      *
0212      * Setting memlimit_threading to UINT64_MAX or a similar huge value
0213      * means that liblzma is allowed to keep the whole compressed file
0214      * and the whole uncompressed file in memory in addition to the memory
0215      * needed by the decompressor data structures used by each thread!
0216      * In other words, a reasonable value limit must be set here or it
0217      * will cause problems sooner or later. If you have no idea what
0218      * a reasonable value could be, try lzma_physmem() / 4 as a starting
0219      * point. Setting this limit will never prevent decompression of
0220      * a file; this will only reduce the number of threads.
0221      *
0222      * If memlimit_threading is greater than memlimit_stop, then the value
0223      * of memlimit_stop will be used for both.
0224      */
0225     uint64_t memlimit_threading;
0226 
0227     /**
0228      * \brief       Memory usage limit that should never be exceeded
0229      *
0230      * Encoder: Ignored.
0231      *
0232      * Decoder: If decompressing will need more than this amount of
0233      * memory even in the single-threaded mode, then lzma_code() will
0234      * return LZMA_MEMLIMIT_ERROR.
0235      */
0236     uint64_t memlimit_stop;
0237 
0238     /** \private     Reserved member. */
0239     uint64_t reserved_int7;
0240 
0241     /** \private     Reserved member. */
0242     uint64_t reserved_int8;
0243 
0244     /** \private     Reserved member. */
0245     void *reserved_ptr1;
0246 
0247     /** \private     Reserved member. */
0248     void *reserved_ptr2;
0249 
0250     /** \private     Reserved member. */
0251     void *reserved_ptr3;
0252 
0253     /** \private     Reserved member. */
0254     void *reserved_ptr4;
0255 
0256 } lzma_mt;
0257 
0258 
0259 /**
0260  * \brief       Calculate approximate memory usage of easy encoder
0261  *
0262  * This function is a wrapper for lzma_raw_encoder_memusage().
0263  *
0264  * \param       preset  Compression preset (level and possible flags)
0265  *
0266  * \return      Number of bytes of memory required for the given
0267  *              preset when encoding or UINT64_MAX on error.
0268  */
0269 extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
0270         lzma_nothrow lzma_attr_pure;
0271 
0272 
0273 /**
0274  * \brief       Calculate approximate decoder memory usage of a preset
0275  *
0276  * This function is a wrapper for lzma_raw_decoder_memusage().
0277  *
0278  * \param       preset  Compression preset (level and possible flags)
0279  *
0280  * \return      Number of bytes of memory required to decompress a file
0281  *              that was compressed using the given preset or UINT64_MAX
0282  *              on error.
0283  */
0284 extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
0285         lzma_nothrow lzma_attr_pure;
0286 
0287 
0288 /**
0289  * \brief       Initialize .xz Stream encoder using a preset number
0290  *
0291  * This function is intended for those who just want to use the basic features
0292  * of liblzma (that is, most developers out there).
0293  *
0294  * If initialization fails (return value is not LZMA_OK), all the memory
0295  * allocated for *strm by liblzma is always freed. Thus, there is no need
0296  * to call lzma_end() after failed initialization.
0297  *
0298  * If initialization succeeds, use lzma_code() to do the actual encoding.
0299  * Valid values for 'action' (the second argument of lzma_code()) are
0300  * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
0301  * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
0302  *
0303  * \param       strm    Pointer to lzma_stream that is at least initialized
0304  *                      with LZMA_STREAM_INIT.
0305  * \param       preset  Compression preset to use. A preset consist of level
0306  *                      number and zero or more flags. Usually flags aren't
0307  *                      used, so preset is simply a number [0, 9] which match
0308  *                      the options -0 ... -9 of the xz command line tool.
0309  *                      Additional flags can be set using bitwise-or with
0310  *                      the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
0311  * \param       check   Integrity check type to use. See check.h for available
0312  *                      checks. The xz command line tool defaults to
0313  *                      LZMA_CHECK_CRC64, which is a good choice if you are
0314  *                      unsure. LZMA_CHECK_CRC32 is good too as long as the
0315  *                      uncompressed file is not many gigabytes.
0316  *
0317  * \return      Possible lzma_ret values:
0318  *              - LZMA_OK: Initialization succeeded. Use lzma_code() to
0319  *                encode your data.
0320  *              - LZMA_MEM_ERROR: Memory allocation failed.
0321  *              - LZMA_OPTIONS_ERROR: The given compression preset is not
0322  *                supported by this build of liblzma.
0323  *              - LZMA_UNSUPPORTED_CHECK: The given check type is not
0324  *                supported by this liblzma build.
0325  *              - LZMA_PROG_ERROR: One or more of the parameters have values
0326  *                that will never be valid. For example, strm == NULL.
0327  */
0328 extern LZMA_API(lzma_ret) lzma_easy_encoder(
0329         lzma_stream *strm, uint32_t preset, lzma_check check)
0330         lzma_nothrow lzma_attr_warn_unused_result;
0331 
0332 
0333 /**
0334  * \brief       Single-call .xz Stream encoding using a preset number
0335  *
0336  * The maximum required output buffer size can be calculated with
0337  * lzma_stream_buffer_bound().
0338  *
0339  * \param       preset      Compression preset to use. See the description
0340  *                          in lzma_easy_encoder().
0341  * \param       check       Type of the integrity check to calculate from
0342  *                          uncompressed data.
0343  * \param       allocator   lzma_allocator for custom allocator functions.
0344  *                          Set to NULL to use malloc() and free().
0345  * \param       in          Beginning of the input buffer
0346  * \param       in_size     Size of the input buffer
0347  * \param[out]  out         Beginning of the output buffer
0348  * \param[out]  out_pos     The next byte will be written to out[*out_pos].
0349  *                          *out_pos is updated only if encoding succeeds.
0350  * \param       out_size    Size of the out buffer; the first byte into
0351  *                          which no data is written to is out[out_size].
0352  *
0353  * \return      Possible lzma_ret values:
0354  *              - LZMA_OK: Encoding was successful.
0355  *              - LZMA_BUF_ERROR: Not enough output buffer space.
0356  *              - LZMA_UNSUPPORTED_CHECK
0357  *              - LZMA_OPTIONS_ERROR
0358  *              - LZMA_MEM_ERROR
0359  *              - LZMA_DATA_ERROR
0360  *              - LZMA_PROG_ERROR
0361  */
0362 extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
0363         uint32_t preset, lzma_check check,
0364         const lzma_allocator *allocator,
0365         const uint8_t *in, size_t in_size,
0366         uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
0367 
0368 
0369 /**
0370  * \brief       Initialize .xz Stream encoder using a custom filter chain
0371  *
0372  * \param       strm    Pointer to lzma_stream that is at least initialized
0373  *                      with LZMA_STREAM_INIT.
0374  * \param       filters Array of filters terminated with
0375  *                      .id == LZMA_VLI_UNKNOWN. See filters.h for more
0376  *                      information.
0377  * \param       check   Type of the integrity check to calculate from
0378  *                      uncompressed data.
0379  *
0380  * \return      Possible lzma_ret values:
0381  *              - LZMA_OK: Initialization was successful.
0382  *              - LZMA_MEM_ERROR
0383  *              - LZMA_UNSUPPORTED_CHECK
0384  *              - LZMA_OPTIONS_ERROR
0385  *              - LZMA_PROG_ERROR
0386  */
0387 extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
0388         const lzma_filter *filters, lzma_check check)
0389         lzma_nothrow lzma_attr_warn_unused_result;
0390 
0391 
0392 /**
0393  * \brief       Calculate approximate memory usage of multithreaded .xz encoder
0394  *
0395  * Since doing the encoding in threaded mode doesn't affect the memory
0396  * requirements of single-threaded decompressor, you can use
0397  * lzma_easy_decoder_memusage(options->preset) or
0398  * lzma_raw_decoder_memusage(options->filters) to calculate
0399  * the decompressor memory requirements.
0400  *
0401  * \param       options Compression options
0402  *
0403  * \return      Number of bytes of memory required for encoding with the
0404  *              given options. If an error occurs, for example due to
0405  *              unsupported preset or filter chain, UINT64_MAX is returned.
0406  */
0407 extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
0408         const lzma_mt *options) lzma_nothrow lzma_attr_pure;
0409 
0410 
0411 /**
0412  * \brief       Initialize multithreaded .xz Stream encoder
0413  *
0414  * This provides the functionality of lzma_easy_encoder() and
0415  * lzma_stream_encoder() as a single function for multithreaded use.
0416  *
0417  * The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH,
0418  * LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be
0419  * added in the future.
0420  *
0421  * \param       strm    Pointer to lzma_stream that is at least initialized
0422  *                      with LZMA_STREAM_INIT.
0423  * \param       options Pointer to multithreaded compression options
0424  *
0425  * \return      Possible lzma_ret values:
0426  *              - LZMA_OK
0427  *              - LZMA_MEM_ERROR
0428  *              - LZMA_UNSUPPORTED_CHECK
0429  *              - LZMA_OPTIONS_ERROR
0430  *              - LZMA_PROG_ERROR
0431  */
0432 extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
0433         lzma_stream *strm, const lzma_mt *options)
0434         lzma_nothrow lzma_attr_warn_unused_result;
0435 
0436 
0437 /**
0438  * \brief       Calculate recommended Block size for multithreaded .xz encoder
0439  *
0440  * This calculates a recommended Block size for multithreaded encoding given
0441  * a filter chain. This is used internally by lzma_stream_encoder_mt() to
0442  * determine the Block size if the block_size member is not set to the
0443  * special value of 0 in the lzma_mt options struct.
0444  *
0445  * If one wishes to change the filters between Blocks, this function is
0446  * helpful to set the block_size member of the lzma_mt struct before calling
0447  * lzma_stream_encoder_mt(). Since the block_size member represents the
0448  * maximum possible Block size for the multithreaded .xz encoder, one can
0449  * use this function to find the maximum recommended Block size based on
0450  * all planned filter chains. Otherwise, the multithreaded encoder will
0451  * base its maximum Block size on the first filter chain used (if the
0452  * block_size member is not set), which may unnecessarily limit the Block
0453  * size for a later filter chain.
0454  *
0455  * \param       filters   Array of filters terminated with
0456  *                        .id == LZMA_VLI_UNKNOWN.
0457  *
0458  * \return      Recommended Block size in bytes, or UINT64_MAX if
0459  *              an error occurred.
0460  */
0461 extern LZMA_API(uint64_t) lzma_mt_block_size(const lzma_filter *filters)
0462         lzma_nothrow;
0463 
0464 
0465 /**
0466  * \brief       Initialize .lzma encoder (legacy file format)
0467  *
0468  * The .lzma format is sometimes called the LZMA_Alone format, which is the
0469  * reason for the name of this function. The .lzma format supports only the
0470  * LZMA1 filter. There is no support for integrity checks like CRC32.
0471  *
0472  * Use this function if and only if you need to create files readable by
0473  * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
0474  * is strongly recommended.
0475  *
0476  * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
0477  * No kind of flushing is supported, because the file format doesn't make
0478  * it possible.
0479  *
0480  * \param       strm    Pointer to lzma_stream that is at least initialized
0481  *                      with LZMA_STREAM_INIT.
0482  * \param       options Pointer to encoder options
0483  *
0484  * \return      Possible lzma_ret values:
0485  *              - LZMA_OK
0486  *              - LZMA_MEM_ERROR
0487  *              - LZMA_OPTIONS_ERROR
0488  *              - LZMA_PROG_ERROR
0489  */
0490 extern LZMA_API(lzma_ret) lzma_alone_encoder(
0491         lzma_stream *strm, const lzma_options_lzma *options)
0492         lzma_nothrow lzma_attr_warn_unused_result;
0493 
0494 
0495 /**
0496  * \brief       Calculate output buffer size for single-call Stream encoder
0497  *
0498  * When trying to compress incompressible data, the encoded size will be
0499  * slightly bigger than the input data. This function calculates how much
0500  * output buffer space is required to be sure that lzma_stream_buffer_encode()
0501  * doesn't return LZMA_BUF_ERROR.
0502  *
0503  * The calculated value is not exact, but it is guaranteed to be big enough.
0504  * The actual maximum output space required may be slightly smaller (up to
0505  * about 100 bytes). This should not be a problem in practice.
0506  *
0507  * If the calculated maximum size doesn't fit into size_t or would make the
0508  * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
0509  * zero is returned to indicate the error.
0510  *
0511  * \note        The limit calculated by this function applies only to
0512  *              single-call encoding. Multi-call encoding may (and probably
0513  *              will) have larger maximum expansion when encoding
0514  *              incompressible data. Currently there is no function to
0515  *              calculate the maximum expansion of multi-call encoding.
0516  *
0517  * \param       uncompressed_size   Size in bytes of the uncompressed
0518  *                                  input data
0519  *
0520  * \return      Maximum number of bytes needed to store the compressed data.
0521  */
0522 extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size)
0523         lzma_nothrow;
0524 
0525 
0526 /**
0527  * \brief       Single-call .xz Stream encoder
0528  *
0529  * \param       filters     Array of filters terminated with
0530  *                          .id == LZMA_VLI_UNKNOWN. See filters.h for more
0531  *                          information.
0532  * \param       check       Type of the integrity check to calculate from
0533  *                          uncompressed data.
0534  * \param       allocator   lzma_allocator for custom allocator functions.
0535  *                          Set to NULL to use malloc() and free().
0536  * \param       in          Beginning of the input buffer
0537  * \param       in_size     Size of the input buffer
0538  * \param[out]  out         Beginning of the output buffer
0539  * \param[out]  out_pos     The next byte will be written to out[*out_pos].
0540  *                          *out_pos is updated only if encoding succeeds.
0541  * \param       out_size    Size of the out buffer; the first byte into
0542  *                          which no data is written to is out[out_size].
0543  *
0544  * \return      Possible lzma_ret values:
0545  *              - LZMA_OK: Encoding was successful.
0546  *              - LZMA_BUF_ERROR: Not enough output buffer space.
0547  *              - LZMA_UNSUPPORTED_CHECK
0548  *              - LZMA_OPTIONS_ERROR
0549  *              - LZMA_MEM_ERROR
0550  *              - LZMA_DATA_ERROR
0551  *              - LZMA_PROG_ERROR
0552  */
0553 extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
0554         lzma_filter *filters, lzma_check check,
0555         const lzma_allocator *allocator,
0556         const uint8_t *in, size_t in_size,
0557         uint8_t *out, size_t *out_pos, size_t out_size)
0558         lzma_nothrow lzma_attr_warn_unused_result;
0559 
0560 
0561 /**
0562  * \brief       MicroLZMA encoder
0563  *
0564  * The MicroLZMA format is a raw LZMA stream whose first byte (always 0x00)
0565  * has been replaced with bitwise-negation of the LZMA properties (lc/lp/pb).
0566  * This encoding ensures that the first byte of MicroLZMA stream is never
0567  * 0x00. There is no end of payload marker and thus the uncompressed size
0568  * must be stored separately. For the best error detection the dictionary
0569  * size should be stored separately as well but alternatively one may use
0570  * the uncompressed size as the dictionary size when decoding.
0571  *
0572  * With the MicroLZMA encoder, lzma_code() behaves slightly unusually.
0573  * The action argument must be LZMA_FINISH and the return value will never be
0574  * LZMA_OK. Thus the encoding is always done with a single lzma_code() after
0575  * the initialization. The benefit of the combination of initialization
0576  * function and lzma_code() is that memory allocations can be re-used for
0577  * better performance.
0578  *
0579  * lzma_code() will try to encode as much input as is possible to fit into
0580  * the given output buffer. If not all input can be encoded, the stream will
0581  * be finished without encoding all the input. The caller must check both
0582  * input and output buffer usage after lzma_code() (total_in and total_out
0583  * in lzma_stream can be convenient). Often lzma_code() can fill the output
0584  * buffer completely if there is a lot of input, but sometimes a few bytes
0585  * may remain unused because the next LZMA symbol would require more space.
0586  *
0587  * lzma_stream.avail_out must be at least 6. Otherwise LZMA_PROG_ERROR
0588  * will be returned.
0589  *
0590  * The LZMA dictionary should be reasonably low to speed up the encoder
0591  * re-initialization. A good value is bigger than the resulting
0592  * uncompressed size of most of the output chunks. For example, if output
0593  * size is 4 KiB, dictionary size of 32 KiB or 64 KiB is good. If the
0594  * data compresses extremely well, even 128 KiB may be useful.
0595  *
0596  * The MicroLZMA format and this encoder variant were made with the EROFS
0597  * file system in mind. This format may be convenient in other embedded
0598  * uses too where many small streams are needed. XZ Embedded includes a
0599  * decoder for this format.
0600  *
0601  * \param       strm    Pointer to lzma_stream that is at least initialized
0602  *                      with LZMA_STREAM_INIT.
0603  * \param       options Pointer to encoder options
0604  *
0605  * \return      Possible lzma_ret values:
0606  *              - LZMA_STREAM_END: All good. Check the amounts of input used
0607  *                and output produced. Store the amount of input used
0608  *                (uncompressed size) as it needs to be known to decompress
0609  *                the data.
0610  *              - LZMA_OPTIONS_ERROR
0611  *              - LZMA_MEM_ERROR
0612  *              - LZMA_PROG_ERROR: In addition to the generic reasons for this
0613  *                error code, this may also be returned if there isn't enough
0614  *                output space (6 bytes) to create a valid MicroLZMA stream.
0615  */
0616 extern LZMA_API(lzma_ret) lzma_microlzma_encoder(
0617         lzma_stream *strm, const lzma_options_lzma *options)
0618         lzma_nothrow;
0619 
0620 
0621 /************
0622  * Decoding *
0623  ************/
0624 
0625 /**
0626  * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
0627  * being decoded has no integrity check. Note that when used with
0628  * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
0629  * if LZMA_TELL_NO_CHECK is used.
0630  */
0631 #define LZMA_TELL_NO_CHECK              UINT32_C(0x01)
0632 
0633 
0634 /**
0635  * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
0636  * stream has an integrity check, but the type of the integrity check is not
0637  * supported by this liblzma version or build. Such files can still be
0638  * decoded, but the integrity check cannot be verified.
0639  */
0640 #define LZMA_TELL_UNSUPPORTED_CHECK     UINT32_C(0x02)
0641 
0642 
0643 /**
0644  * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
0645  * of the integrity check is known. The type can then be got with
0646  * lzma_get_check().
0647  */
0648 #define LZMA_TELL_ANY_CHECK             UINT32_C(0x04)
0649 
0650 
0651 /**
0652  * This flag makes lzma_code() not calculate and verify the integrity check
0653  * of the compressed data in .xz files. This means that invalid integrity
0654  * check values won't be detected and LZMA_DATA_ERROR won't be returned in
0655  * such cases.
0656  *
0657  * This flag only affects the checks of the compressed data itself; the CRC32
0658  * values in the .xz headers will still be verified normally.
0659  *
0660  * Don't use this flag unless you know what you are doing. Possible reasons
0661  * to use this flag:
0662  *
0663  *   - Trying to recover data from a corrupt .xz file.
0664  *
0665  *   - Speeding up decompression, which matters mostly with SHA-256
0666  *     or with files that have compressed extremely well. It's recommended
0667  *     to not use this flag for this purpose unless the file integrity is
0668  *     verified externally in some other way.
0669  *
0670  * Support for this flag was added in liblzma 5.1.4beta.
0671  */
0672 #define LZMA_IGNORE_CHECK               UINT32_C(0x10)
0673 
0674 
0675 /**
0676  * This flag enables decoding of concatenated files with file formats that
0677  * allow concatenating compressed files as is. From the formats currently
0678  * supported by liblzma, only the .xz and .lz formats allow concatenated
0679  * files. Concatenated files are not allowed with the legacy .lzma format.
0680  *
0681  * This flag also affects the usage of the 'action' argument for lzma_code().
0682  * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
0683  * unless LZMA_FINISH is used as 'action'. Thus, the application has to set
0684  * LZMA_FINISH in the same way as it does when encoding.
0685  *
0686  * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
0687  * as 'action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
0688  */
0689 #define LZMA_CONCATENATED               UINT32_C(0x08)
0690 
0691 
0692 /**
0693  * This flag makes the threaded decoder report errors (like LZMA_DATA_ERROR)
0694  * as soon as they are detected. This saves time when the application has no
0695  * interest in a partially decompressed truncated or corrupt file. Note that
0696  * due to timing randomness, if the same truncated or corrupt input is
0697  * decompressed multiple times with this flag, a different amount of output
0698  * may be produced by different runs, and even the error code might vary.
0699  *
0700  * When using LZMA_FAIL_FAST, it is recommended to use LZMA_FINISH to tell
0701  * the decoder when no more input will be coming because it can help fast
0702  * detection and reporting of truncated files. Note that in this situation
0703  * truncated files might be diagnosed with LZMA_DATA_ERROR instead of
0704  * LZMA_OK or LZMA_BUF_ERROR!
0705  *
0706  * Without this flag the threaded decoder will provide as much output as
0707  * possible at first and then report the pending error. This default behavior
0708  * matches the single-threaded decoder and provides repeatable behavior
0709  * with truncated or corrupt input. There are a few special cases where the
0710  * behavior can still differ like memory allocation failures (LZMA_MEM_ERROR).
0711  *
0712  * Single-threaded decoders currently ignore this flag.
0713  *
0714  * Support for this flag was added in liblzma 5.3.3alpha. Note that in older
0715  * versions this flag isn't supported (LZMA_OPTIONS_ERROR) even by functions
0716  * that ignore this flag in newer liblzma versions.
0717  */
0718 #define LZMA_FAIL_FAST                  UINT32_C(0x20)
0719 
0720 
0721 /**
0722  * \brief       Initialize .xz Stream decoder
0723  *
0724  * \param       strm        Pointer to lzma_stream that is at least initialized
0725  *                          with LZMA_STREAM_INIT.
0726  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
0727  *                          to effectively disable the limiter. liblzma
0728  *                          5.2.3 and earlier don't allow 0 here and return
0729  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
0730  *                          had been specified.
0731  * \param       flags       Bitwise-or of zero or more of the decoder flags:
0732  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
0733  *                          LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
0734  *                          LZMA_CONCATENATED, LZMA_FAIL_FAST
0735  *
0736  * \return      Possible lzma_ret values:
0737  *              - LZMA_OK: Initialization was successful.
0738  *              - LZMA_MEM_ERROR: Cannot allocate memory.
0739  *              - LZMA_OPTIONS_ERROR: Unsupported flags
0740  *              - LZMA_PROG_ERROR
0741  */
0742 extern LZMA_API(lzma_ret) lzma_stream_decoder(
0743         lzma_stream *strm, uint64_t memlimit, uint32_t flags)
0744         lzma_nothrow lzma_attr_warn_unused_result;
0745 
0746 
0747 /**
0748  * \brief       Initialize multithreaded .xz Stream decoder
0749  *
0750  * The decoder can decode multiple Blocks in parallel. This requires that each
0751  * Block Header contains the Compressed Size and Uncompressed size fields
0752  * which are added by the multi-threaded encoder, see lzma_stream_encoder_mt().
0753  *
0754  * A Stream with one Block will only utilize one thread. A Stream with multiple
0755  * Blocks but without size information in Block Headers will be processed in
0756  * single-threaded mode in the same way as done by lzma_stream_decoder().
0757  * Concatenated Streams are processed one Stream at a time; no inter-Stream
0758  * parallelization is done.
0759  *
0760  * This function behaves like lzma_stream_decoder() when options->threads == 1
0761  * and options->memlimit_threading <= 1.
0762  *
0763  * \param       strm        Pointer to lzma_stream that is at least initialized
0764  *                          with LZMA_STREAM_INIT.
0765  * \param       options     Pointer to multithreaded compression options
0766  *
0767  * \return      Possible lzma_ret values:
0768  *              - LZMA_OK: Initialization was successful.
0769  *              - LZMA_MEM_ERROR: Cannot allocate memory.
0770  *              - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
0771  *              - LZMA_OPTIONS_ERROR: Unsupported flags.
0772  *              - LZMA_PROG_ERROR
0773  */
0774 extern LZMA_API(lzma_ret) lzma_stream_decoder_mt(
0775         lzma_stream *strm, const lzma_mt *options)
0776         lzma_nothrow lzma_attr_warn_unused_result;
0777 
0778 
0779 /**
0780  * \brief       Decode .xz, .lzma, and .lz (lzip) files with autodetection
0781  *
0782  * This decoder autodetects between the .xz, .lzma, and .lz file formats,
0783  * and calls lzma_stream_decoder(), lzma_alone_decoder(), or
0784  * lzma_lzip_decoder() once the type of the input file has been detected.
0785  *
0786  * Support for .lz was added in 5.4.0.
0787  *
0788  * If the flag LZMA_CONCATENATED is used and the input is a .lzma file:
0789  * For historical reasons concatenated .lzma files aren't supported.
0790  * If there is trailing data after one .lzma stream, lzma_code() will
0791  * return LZMA_DATA_ERROR. (lzma_alone_decoder() doesn't have such a check
0792  * as it doesn't support any decoder flags. It will return LZMA_STREAM_END
0793  * after one .lzma stream.)
0794  *
0795  * \param       strm        Pointer to lzma_stream that is at least initialized
0796  *                          with LZMA_STREAM_INIT.
0797  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
0798  *                          to effectively disable the limiter. liblzma
0799  *                          5.2.3 and earlier don't allow 0 here and return
0800  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
0801  *                          had been specified.
0802  * \param       flags       Bitwise-or of zero or more of the decoder flags:
0803  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
0804  *                          LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
0805  *                          LZMA_CONCATENATED, LZMA_FAIL_FAST
0806  *
0807  * \return      Possible lzma_ret values:
0808  *              - LZMA_OK: Initialization was successful.
0809  *              - LZMA_MEM_ERROR: Cannot allocate memory.
0810  *              - LZMA_OPTIONS_ERROR: Unsupported flags
0811  *              - LZMA_PROG_ERROR
0812  */
0813 extern LZMA_API(lzma_ret) lzma_auto_decoder(
0814         lzma_stream *strm, uint64_t memlimit, uint32_t flags)
0815         lzma_nothrow lzma_attr_warn_unused_result;
0816 
0817 
0818 /**
0819  * \brief       Initialize .lzma decoder (legacy file format)
0820  *
0821  * Valid 'action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
0822  * There is no need to use LZMA_FINISH, but it's allowed because it may
0823  * simplify certain types of applications.
0824  *
0825  * \param       strm        Pointer to lzma_stream that is at least initialized
0826  *                          with LZMA_STREAM_INIT.
0827  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
0828  *                          to effectively disable the limiter. liblzma
0829  *                          5.2.3 and earlier don't allow 0 here and return
0830  *                          LZMA_PROG_ERROR; later versions treat 0 as if 1
0831  *                          had been specified.
0832  *
0833  * \return      Possible lzma_ret values:
0834  *              - LZMA_OK
0835  *              - LZMA_MEM_ERROR
0836  *              - LZMA_PROG_ERROR
0837  */
0838 extern LZMA_API(lzma_ret) lzma_alone_decoder(
0839         lzma_stream *strm, uint64_t memlimit)
0840         lzma_nothrow lzma_attr_warn_unused_result;
0841 
0842 
0843 /**
0844  * \brief       Initialize .lz (lzip) decoder (a foreign file format)
0845  *
0846  * This decoder supports the .lz format version 0 and the unextended .lz
0847  * format version 1:
0848  *
0849  *   - Files in the format version 0 were produced by lzip 1.3 and older.
0850  *     Such files aren't common but may be found from file archives
0851  *     as a few source packages were released in this format. People
0852  *     might have old personal files in this format too. Decompression
0853  *     support for the format version 0 was removed in lzip 1.18.
0854  *
0855  *   - lzip 1.3 added decompression support for .lz format version 1 files.
0856  *     Compression support was added in lzip 1.4. In lzip 1.6 the .lz format
0857  *     version 1 was extended to support the Sync Flush marker. This extension
0858  *     is not supported by liblzma. lzma_code() will return LZMA_DATA_ERROR
0859  *     at the location of the Sync Flush marker. In practice files with
0860  *     the Sync Flush marker are very rare and thus liblzma can decompress
0861  *     almost all .lz files.
0862  *
0863  * Just like with lzma_stream_decoder() for .xz files, LZMA_CONCATENATED
0864  * should be used when decompressing normal standalone .lz files.
0865  *
0866  * The .lz format allows putting non-.lz data at the end of a file after at
0867  * least one valid .lz member. That is, one can append custom data at the end
0868  * of a .lz file and the decoder is required to ignore it. In liblzma this
0869  * is relevant only when LZMA_CONCATENATED is used. In that case lzma_code()
0870  * will return LZMA_STREAM_END and leave lzma_stream.next_in pointing to
0871  * the first byte of the non-.lz data. An exception to this is if the first
0872  * 1-3 bytes of the non-.lz data are identical to the .lz magic bytes
0873  * (0x4C, 0x5A, 0x49, 0x50; "LZIP" in US-ASCII). In such a case the 1-3 bytes
0874  * will have been ignored by lzma_code(). If one wishes to locate the non-.lz
0875  * data reliably, one must ensure that the first byte isn't 0x4C. Actually
0876  * one should ensure that none of the first four bytes of trailing data are
0877  * equal to the magic bytes because lzip >= 1.20 requires it by default.
0878  *
0879  * \param       strm        Pointer to lzma_stream that is at least initialized
0880  *                          with LZMA_STREAM_INIT.
0881  * \param       memlimit    Memory usage limit as bytes. Use UINT64_MAX
0882  *                          to effectively disable the limiter.
0883  * \param       flags       Bitwise-or of flags, or zero for no flags.
0884  *                          All decoder flags listed above are supported
0885  *                          although only LZMA_CONCATENATED and (in very rare
0886  *                          cases) LZMA_IGNORE_CHECK are actually useful.
0887  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
0888  *                          and LZMA_FAIL_FAST do nothing. LZMA_TELL_ANY_CHECK
0889  *                          is supported for consistency only as CRC32 is
0890  *                          always used in the .lz format.
0891  *
0892  * \return      Possible lzma_ret values:
0893  *              - LZMA_OK: Initialization was successful.
0894  *              - LZMA_MEM_ERROR: Cannot allocate memory.
0895  *              - LZMA_OPTIONS_ERROR: Unsupported flags
0896  *              - LZMA_PROG_ERROR
0897  */
0898 extern LZMA_API(lzma_ret) lzma_lzip_decoder(
0899         lzma_stream *strm, uint64_t memlimit, uint32_t flags)
0900         lzma_nothrow lzma_attr_warn_unused_result;
0901 
0902 
0903 /**
0904  * \brief       Single-call .xz Stream decoder
0905  *
0906  * \param       memlimit    Pointer to how much memory the decoder is allowed
0907  *                          to allocate. The value pointed by this pointer is
0908  *                          modified if and only if LZMA_MEMLIMIT_ERROR is
0909  *                          returned.
0910  * \param       flags       Bitwise-or of zero or more of the decoder flags:
0911  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
0912  *                          LZMA_IGNORE_CHECK, LZMA_CONCATENATED,
0913  *                          LZMA_FAIL_FAST. Note that LZMA_TELL_ANY_CHECK
0914  *                          is not allowed and will return LZMA_PROG_ERROR.
0915  * \param       allocator   lzma_allocator for custom allocator functions.
0916  *                          Set to NULL to use malloc() and free().
0917  * \param       in          Beginning of the input buffer
0918  * \param       in_pos      The next byte will be read from in[*in_pos].
0919  *                          *in_pos is updated only if decoding succeeds.
0920  * \param       in_size     Size of the input buffer; the first byte that
0921  *                          won't be read is in[in_size].
0922  * \param[out]  out         Beginning of the output buffer
0923  * \param[out]  out_pos     The next byte will be written to out[*out_pos].
0924  *                          *out_pos is updated only if decoding succeeds.
0925  * \param       out_size    Size of the out buffer; the first byte into
0926  *                          which no data is written to is out[out_size].
0927  *
0928  * \return      Possible lzma_ret values:
0929  *              - LZMA_OK: Decoding was successful.
0930  *              - LZMA_FORMAT_ERROR
0931  *              - LZMA_OPTIONS_ERROR
0932  *              - LZMA_DATA_ERROR
0933  *              - LZMA_NO_CHECK: This can be returned only if using
0934  *                the LZMA_TELL_NO_CHECK flag.
0935  *              - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
0936  *                the LZMA_TELL_UNSUPPORTED_CHECK flag.
0937  *              - LZMA_MEM_ERROR
0938  *              - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
0939  *                The minimum required memlimit value was stored to *memlimit.
0940  *              - LZMA_BUF_ERROR: Output buffer was too small.
0941  *              - LZMA_PROG_ERROR
0942  */
0943 extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
0944         uint64_t *memlimit, uint32_t flags,
0945         const lzma_allocator *allocator,
0946         const uint8_t *in, size_t *in_pos, size_t in_size,
0947         uint8_t *out, size_t *out_pos, size_t out_size)
0948         lzma_nothrow lzma_attr_warn_unused_result;
0949 
0950 
0951 /**
0952  * \brief       MicroLZMA decoder
0953  *
0954  * See lzma_microlzma_encoder() for more information.
0955  *
0956  * The lzma_code() usage with this decoder is completely normal. The
0957  * special behavior of lzma_code() applies to lzma_microlzma_encoder() only.
0958  *
0959  * \param       strm        Pointer to lzma_stream that is at least initialized
0960  *                          with LZMA_STREAM_INIT.
0961  * \param       comp_size   Compressed size of the MicroLZMA stream.
0962  *                          The caller must somehow know this exactly.
0963  * \param       uncomp_size Uncompressed size of the MicroLZMA stream.
0964  *                          If the exact uncompressed size isn't known, this
0965  *                          can be set to a value that is at most as big as
0966  *                          the exact uncompressed size would be, but then the
0967  *                          next argument uncomp_size_is_exact must be false.
0968  * \param       uncomp_size_is_exact
0969  *                          If true, uncomp_size must be exactly correct.
0970  *                          This will improve error detection at the end of
0971  *                          the stream. If the exact uncompressed size isn't
0972  *                          known, this must be false. uncomp_size must still
0973  *                          be at most as big as the exact uncompressed size
0974  *                          is. Setting this to false when the exact size is
0975  *                          known will work but error detection at the end of
0976  *                          the stream will be weaker.
0977  * \param       dict_size   LZMA dictionary size that was used when
0978  *                          compressing the data. It is OK to use a bigger
0979  *                          value too but liblzma will then allocate more
0980  *                          memory than would actually be required and error
0981  *                          detection will be slightly worse. (Note that with
0982  *                          the implementation in XZ Embedded it doesn't
0983  *                          affect the memory usage if one specifies bigger
0984  *                          dictionary than actually required.)
0985  *
0986  * \return      Possible lzma_ret values:
0987  *              - LZMA_OK
0988  *              - LZMA_MEM_ERROR
0989  *              - LZMA_OPTIONS_ERROR
0990  *              - LZMA_PROG_ERROR
0991  */
0992 extern LZMA_API(lzma_ret) lzma_microlzma_decoder(
0993         lzma_stream *strm, uint64_t comp_size,
0994         uint64_t uncomp_size, lzma_bool uncomp_size_is_exact,
0995         uint32_t dict_size) lzma_nothrow;