1*3b35e7eeSXin LI /* SPDX-License-Identifier: 0BSD */ 2*3b35e7eeSXin LI 381ad8388SMartin Matuska /** 481ad8388SMartin Matuska * \file lzma/base.h 581ad8388SMartin Matuska * \brief Data types and functions used in many places in liblzma API 6c917796cSXin LI * \note Never include this file directly. Use <lzma.h> instead. 781ad8388SMartin Matuska */ 881ad8388SMartin Matuska 981ad8388SMartin Matuska /* 1081ad8388SMartin Matuska * Author: Lasse Collin 1181ad8388SMartin Matuska */ 1281ad8388SMartin Matuska 1381ad8388SMartin Matuska #ifndef LZMA_H_INTERNAL 1481ad8388SMartin Matuska # error Never include this file directly. Use <lzma.h> instead. 1581ad8388SMartin Matuska #endif 1681ad8388SMartin Matuska 1781ad8388SMartin Matuska 1881ad8388SMartin Matuska /** 1981ad8388SMartin Matuska * \brief Boolean 2081ad8388SMartin Matuska * 2181ad8388SMartin Matuska * This is here because C89 doesn't have stdbool.h. To set a value for 2281ad8388SMartin Matuska * variables having type lzma_bool, you can use 23*3b35e7eeSXin LI * - C99's 'true' and 'false' from stdbool.h; 24*3b35e7eeSXin LI * - C++'s internal 'true' and 'false'; or 2581ad8388SMartin Matuska * - integers one (true) and zero (false). 2681ad8388SMartin Matuska */ 2781ad8388SMartin Matuska typedef unsigned char lzma_bool; 2881ad8388SMartin Matuska 2981ad8388SMartin Matuska 3081ad8388SMartin Matuska /** 3181ad8388SMartin Matuska * \brief Type of reserved enumeration variable in structures 3281ad8388SMartin Matuska * 3381ad8388SMartin Matuska * To avoid breaking library ABI when new features are added, several 3481ad8388SMartin Matuska * structures contain extra variables that may be used in future. Since 3581ad8388SMartin Matuska * sizeof(enum) can be different than sizeof(int), and sizeof(enum) may 3681ad8388SMartin Matuska * even vary depending on the range of enumeration constants, we specify 3781ad8388SMartin Matuska * a separate type to be used for reserved enumeration variables. All 3881ad8388SMartin Matuska * enumeration constants in liblzma API will be non-negative and less 3981ad8388SMartin Matuska * than 128, which should guarantee that the ABI won't break even when 4081ad8388SMartin Matuska * new constants are added to existing enumerations. 4181ad8388SMartin Matuska */ 4281ad8388SMartin Matuska typedef enum { 4381ad8388SMartin Matuska LZMA_RESERVED_ENUM = 0 4481ad8388SMartin Matuska } lzma_reserved_enum; 4581ad8388SMartin Matuska 4681ad8388SMartin Matuska 4781ad8388SMartin Matuska /** 4881ad8388SMartin Matuska * \brief Return values used by several functions in liblzma 4981ad8388SMartin Matuska * 5081ad8388SMartin Matuska * Check the descriptions of specific functions to find out which return 5181ad8388SMartin Matuska * values they can return. With some functions the return values may have 5281ad8388SMartin Matuska * more specific meanings than described here; those differences are 5381ad8388SMartin Matuska * described per-function basis. 5481ad8388SMartin Matuska */ 5581ad8388SMartin Matuska typedef enum { 5681ad8388SMartin Matuska LZMA_OK = 0, 5781ad8388SMartin Matuska /**< 5881ad8388SMartin Matuska * \brief Operation completed successfully 5981ad8388SMartin Matuska */ 6081ad8388SMartin Matuska 6181ad8388SMartin Matuska LZMA_STREAM_END = 1, 6281ad8388SMartin Matuska /**< 6381ad8388SMartin Matuska * \brief End of stream was reached 6481ad8388SMartin Matuska * 6581ad8388SMartin Matuska * In encoder, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or 6681ad8388SMartin Matuska * LZMA_FINISH was finished. In decoder, this indicates 6781ad8388SMartin Matuska * that all the data was successfully decoded. 6881ad8388SMartin Matuska * 6981ad8388SMartin Matuska * In all cases, when LZMA_STREAM_END is returned, the last 7081ad8388SMartin Matuska * output bytes should be picked from strm->next_out. 7181ad8388SMartin Matuska */ 7281ad8388SMartin Matuska 7381ad8388SMartin Matuska LZMA_NO_CHECK = 2, 7481ad8388SMartin Matuska /**< 7581ad8388SMartin Matuska * \brief Input stream has no integrity check 7681ad8388SMartin Matuska * 7781ad8388SMartin Matuska * This return value can be returned only if the 7881ad8388SMartin Matuska * LZMA_TELL_NO_CHECK flag was used when initializing 7981ad8388SMartin Matuska * the decoder. LZMA_NO_CHECK is just a warning, and 8081ad8388SMartin Matuska * the decoding can be continued normally. 8181ad8388SMartin Matuska * 8281ad8388SMartin Matuska * It is possible to call lzma_get_check() immediately after 8381ad8388SMartin Matuska * lzma_code has returned LZMA_NO_CHECK. The result will 8481ad8388SMartin Matuska * naturally be LZMA_CHECK_NONE, but the possibility to call 8581ad8388SMartin Matuska * lzma_get_check() may be convenient in some applications. 8681ad8388SMartin Matuska */ 8781ad8388SMartin Matuska 8881ad8388SMartin Matuska LZMA_UNSUPPORTED_CHECK = 3, 8981ad8388SMartin Matuska /**< 9081ad8388SMartin Matuska * \brief Cannot calculate the integrity check 9181ad8388SMartin Matuska * 9281ad8388SMartin Matuska * The usage of this return value is different in encoders 9381ad8388SMartin Matuska * and decoders. 9481ad8388SMartin Matuska * 9581ad8388SMartin Matuska * Encoders can return this value only from the initialization 9681ad8388SMartin Matuska * function. If initialization fails with this value, the 9781ad8388SMartin Matuska * encoding cannot be done, because there's no way to produce 9881ad8388SMartin Matuska * output with the correct integrity check. 9981ad8388SMartin Matuska * 10081ad8388SMartin Matuska * Decoders can return this value only from lzma_code() and 10181ad8388SMartin Matuska * only if the LZMA_TELL_UNSUPPORTED_CHECK flag was used when 10281ad8388SMartin Matuska * initializing the decoder. The decoding can still be 10381ad8388SMartin Matuska * continued normally even if the check type is unsupported, 10481ad8388SMartin Matuska * but naturally the check will not be validated, and possible 10581ad8388SMartin Matuska * errors may go undetected. 10681ad8388SMartin Matuska * 10781ad8388SMartin Matuska * With decoder, it is possible to call lzma_get_check() 10881ad8388SMartin Matuska * immediately after lzma_code() has returned 10981ad8388SMartin Matuska * LZMA_UNSUPPORTED_CHECK. This way it is possible to find 11081ad8388SMartin Matuska * out what the unsupported Check ID was. 11181ad8388SMartin Matuska */ 11281ad8388SMartin Matuska 11381ad8388SMartin Matuska LZMA_GET_CHECK = 4, 11481ad8388SMartin Matuska /**< 11581ad8388SMartin Matuska * \brief Integrity check type is now available 11681ad8388SMartin Matuska * 11781ad8388SMartin Matuska * This value can be returned only by the lzma_code() function 11881ad8388SMartin Matuska * and only if the decoder was initialized with the 11981ad8388SMartin Matuska * LZMA_TELL_ANY_CHECK flag. LZMA_GET_CHECK tells the 12081ad8388SMartin Matuska * application that it may now call lzma_get_check() to find 12181ad8388SMartin Matuska * out the Check ID. This can be used, for example, to 12281ad8388SMartin Matuska * implement a decoder that accepts only files that have 12381ad8388SMartin Matuska * strong enough integrity check. 12481ad8388SMartin Matuska */ 12581ad8388SMartin Matuska 12681ad8388SMartin Matuska LZMA_MEM_ERROR = 5, 12781ad8388SMartin Matuska /**< 12881ad8388SMartin Matuska * \brief Cannot allocate memory 12981ad8388SMartin Matuska * 13081ad8388SMartin Matuska * Memory allocation failed, or the size of the allocation 13181ad8388SMartin Matuska * would be greater than SIZE_MAX. 13281ad8388SMartin Matuska * 13381ad8388SMartin Matuska * Due to internal implementation reasons, the coding cannot 13481ad8388SMartin Matuska * be continued even if more memory were made available after 13581ad8388SMartin Matuska * LZMA_MEM_ERROR. 13681ad8388SMartin Matuska */ 13781ad8388SMartin Matuska 13881ad8388SMartin Matuska LZMA_MEMLIMIT_ERROR = 6, 139c917796cSXin LI /**< 14081ad8388SMartin Matuska * \brief Memory usage limit was reached 14181ad8388SMartin Matuska * 14281ad8388SMartin Matuska * Decoder would need more memory than allowed by the 14381ad8388SMartin Matuska * specified memory usage limit. To continue decoding, 14481ad8388SMartin Matuska * the memory usage limit has to be increased with 14581ad8388SMartin Matuska * lzma_memlimit_set(). 1469e6bbe47SXin LI * 1479e6bbe47SXin LI * liblzma 5.2.6 and earlier had a bug in single-threaded .xz 1489e6bbe47SXin LI * decoder (lzma_stream_decoder()) which made it impossible 1499e6bbe47SXin LI * to continue decoding after LZMA_MEMLIMIT_ERROR even if 1509e6bbe47SXin LI * the limit was increased using lzma_memlimit_set(). 1519e6bbe47SXin LI * Other decoders worked correctly. 15281ad8388SMartin Matuska */ 15381ad8388SMartin Matuska 15481ad8388SMartin Matuska LZMA_FORMAT_ERROR = 7, 15581ad8388SMartin Matuska /**< 15681ad8388SMartin Matuska * \brief File format not recognized 15781ad8388SMartin Matuska * 15881ad8388SMartin Matuska * The decoder did not recognize the input as supported file 15981ad8388SMartin Matuska * format. This error can occur, for example, when trying to 16081ad8388SMartin Matuska * decode .lzma format file with lzma_stream_decoder, 16181ad8388SMartin Matuska * because lzma_stream_decoder accepts only the .xz format. 16281ad8388SMartin Matuska */ 16381ad8388SMartin Matuska 16481ad8388SMartin Matuska LZMA_OPTIONS_ERROR = 8, 16581ad8388SMartin Matuska /**< 16681ad8388SMartin Matuska * \brief Invalid or unsupported options 16781ad8388SMartin Matuska * 16881ad8388SMartin Matuska * Invalid or unsupported options, for example 16981ad8388SMartin Matuska * - unsupported filter(s) or filter options; or 17081ad8388SMartin Matuska * - reserved bits set in headers (decoder only). 17181ad8388SMartin Matuska * 17281ad8388SMartin Matuska * Rebuilding liblzma with more features enabled, or 17381ad8388SMartin Matuska * upgrading to a newer version of liblzma may help. 17481ad8388SMartin Matuska */ 17581ad8388SMartin Matuska 17681ad8388SMartin Matuska LZMA_DATA_ERROR = 9, 17781ad8388SMartin Matuska /**< 17881ad8388SMartin Matuska * \brief Data is corrupt 17981ad8388SMartin Matuska * 18081ad8388SMartin Matuska * The usage of this return value is different in encoders 18181ad8388SMartin Matuska * and decoders. In both encoder and decoder, the coding 18281ad8388SMartin Matuska * cannot continue after this error. 18381ad8388SMartin Matuska * 18481ad8388SMartin Matuska * Encoders return this if size limits of the target file 18581ad8388SMartin Matuska * format would be exceeded. These limits are huge, thus 18681ad8388SMartin Matuska * getting this error from an encoder is mostly theoretical. 18781ad8388SMartin Matuska * For example, the maximum compressed and uncompressed 18881ad8388SMartin Matuska * size of a .xz Stream is roughly 8 EiB (2^63 bytes). 18981ad8388SMartin Matuska * 19081ad8388SMartin Matuska * Decoders return this error if the input data is corrupt. 19181ad8388SMartin Matuska * This can mean, for example, invalid CRC32 in headers 19281ad8388SMartin Matuska * or invalid check of uncompressed data. 19381ad8388SMartin Matuska */ 19481ad8388SMartin Matuska 19581ad8388SMartin Matuska LZMA_BUF_ERROR = 10, 19681ad8388SMartin Matuska /**< 19781ad8388SMartin Matuska * \brief No progress is possible 19881ad8388SMartin Matuska * 19981ad8388SMartin Matuska * This error code is returned when the coder cannot consume 20081ad8388SMartin Matuska * any new input and produce any new output. The most common 20181ad8388SMartin Matuska * reason for this error is that the input stream being 20281ad8388SMartin Matuska * decoded is truncated or corrupt. 20381ad8388SMartin Matuska * 20481ad8388SMartin Matuska * This error is not fatal. Coding can be continued normally 20581ad8388SMartin Matuska * by providing more input and/or more output space, if 20681ad8388SMartin Matuska * possible. 20781ad8388SMartin Matuska * 20881ad8388SMartin Matuska * Typically the first call to lzma_code() that can do no 20981ad8388SMartin Matuska * progress returns LZMA_OK instead of LZMA_BUF_ERROR. Only 21081ad8388SMartin Matuska * the second consecutive call doing no progress will return 21181ad8388SMartin Matuska * LZMA_BUF_ERROR. This is intentional. 21281ad8388SMartin Matuska * 21381ad8388SMartin Matuska * With zlib, Z_BUF_ERROR may be returned even if the 21481ad8388SMartin Matuska * application is doing nothing wrong, so apps will need 21581ad8388SMartin Matuska * to handle Z_BUF_ERROR specially. The above hack 21681ad8388SMartin Matuska * guarantees that liblzma never returns LZMA_BUF_ERROR 21781ad8388SMartin Matuska * to properly written applications unless the input file 21881ad8388SMartin Matuska * is truncated or corrupt. This should simplify the 21981ad8388SMartin Matuska * applications a little. 22081ad8388SMartin Matuska */ 22181ad8388SMartin Matuska 22281ad8388SMartin Matuska LZMA_PROG_ERROR = 11, 22381ad8388SMartin Matuska /**< 22481ad8388SMartin Matuska * \brief Programming error 22581ad8388SMartin Matuska * 22681ad8388SMartin Matuska * This indicates that the arguments given to the function are 22781ad8388SMartin Matuska * invalid or the internal state of the decoder is corrupt. 22881ad8388SMartin Matuska * - Function arguments are invalid or the structures 22981ad8388SMartin Matuska * pointed by the argument pointers are invalid 23081ad8388SMartin Matuska * e.g. if strm->next_out has been set to NULL and 23181ad8388SMartin Matuska * strm->avail_out > 0 when calling lzma_code(). 23281ad8388SMartin Matuska * - lzma_* functions have been called in wrong order 23381ad8388SMartin Matuska * e.g. lzma_code() was called right after lzma_end(). 23481ad8388SMartin Matuska * - If errors occur randomly, the reason might be flaky 23581ad8388SMartin Matuska * hardware. 23681ad8388SMartin Matuska * 23781ad8388SMartin Matuska * If you think that your code is correct, this error code 23881ad8388SMartin Matuska * can be a sign of a bug in liblzma. See the documentation 23981ad8388SMartin Matuska * how to report bugs. 24081ad8388SMartin Matuska */ 24173ed8e77SXin LI 24273ed8e77SXin LI LZMA_SEEK_NEEDED = 12, 24373ed8e77SXin LI /**< 24473ed8e77SXin LI * \brief Request to change the input file position 24573ed8e77SXin LI * 24673ed8e77SXin LI * Some coders can do random access in the input file. The 24773ed8e77SXin LI * initialization functions of these coders take the file size 24873ed8e77SXin LI * as an argument. No other coders can return LZMA_SEEK_NEEDED. 24973ed8e77SXin LI * 25073ed8e77SXin LI * When this value is returned, the application must seek to 25173ed8e77SXin LI * the file position given in lzma_stream.seek_pos. This value 25273ed8e77SXin LI * is guaranteed to never exceed the file size that was 25373ed8e77SXin LI * specified at the coder initialization. 25473ed8e77SXin LI * 25573ed8e77SXin LI * After seeking the application should read new input and 25673ed8e77SXin LI * pass it normally via lzma_stream.next_in and .avail_in. 25773ed8e77SXin LI */ 25873ed8e77SXin LI 25973ed8e77SXin LI /* 260*3b35e7eeSXin LI * These enumerations may be used internally by liblzma 26173ed8e77SXin LI * but they will never be returned to applications. 26273ed8e77SXin LI */ 26373ed8e77SXin LI LZMA_RET_INTERNAL1 = 101, 26473ed8e77SXin LI LZMA_RET_INTERNAL2 = 102, 26573ed8e77SXin LI LZMA_RET_INTERNAL3 = 103, 26673ed8e77SXin LI LZMA_RET_INTERNAL4 = 104, 26773ed8e77SXin LI LZMA_RET_INTERNAL5 = 105, 26873ed8e77SXin LI LZMA_RET_INTERNAL6 = 106, 26973ed8e77SXin LI LZMA_RET_INTERNAL7 = 107, 27073ed8e77SXin LI LZMA_RET_INTERNAL8 = 108 27181ad8388SMartin Matuska } lzma_ret; 27281ad8388SMartin Matuska 27381ad8388SMartin Matuska 27481ad8388SMartin Matuska /** 275*3b35e7eeSXin LI * \brief The 'action' argument for lzma_code() 27681ad8388SMartin Matuska * 27753200025SRui Paulo * After the first use of LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, LZMA_FULL_BARRIER, 278*3b35e7eeSXin LI * or LZMA_FINISH, the same 'action' must be used until lzma_code() returns 27953200025SRui Paulo * LZMA_STREAM_END. Also, the amount of input (that is, strm->avail_in) must 28053200025SRui Paulo * not be modified by the application until lzma_code() returns 281*3b35e7eeSXin LI * LZMA_STREAM_END. Changing the 'action' or modifying the amount of input 28253200025SRui Paulo * will make lzma_code() return LZMA_PROG_ERROR. 28381ad8388SMartin Matuska */ 28481ad8388SMartin Matuska typedef enum { 28581ad8388SMartin Matuska LZMA_RUN = 0, 28681ad8388SMartin Matuska /**< 28781ad8388SMartin Matuska * \brief Continue coding 28881ad8388SMartin Matuska * 28981ad8388SMartin Matuska * Encoder: Encode as much input as possible. Some internal 29081ad8388SMartin Matuska * buffering will probably be done (depends on the filter 29181ad8388SMartin Matuska * chain in use), which causes latency: the input used won't 29281ad8388SMartin Matuska * usually be decodeable from the output of the same 29381ad8388SMartin Matuska * lzma_code() call. 29481ad8388SMartin Matuska * 29581ad8388SMartin Matuska * Decoder: Decode as much input as possible and produce as 29681ad8388SMartin Matuska * much output as possible. 29781ad8388SMartin Matuska */ 29881ad8388SMartin Matuska 29981ad8388SMartin Matuska LZMA_SYNC_FLUSH = 1, 30081ad8388SMartin Matuska /**< 30181ad8388SMartin Matuska * \brief Make all the input available at output 30281ad8388SMartin Matuska * 30381ad8388SMartin Matuska * Normally the encoder introduces some latency. 30481ad8388SMartin Matuska * LZMA_SYNC_FLUSH forces all the buffered data to be 30581ad8388SMartin Matuska * available at output without resetting the internal 30681ad8388SMartin Matuska * state of the encoder. This way it is possible to use 30781ad8388SMartin Matuska * compressed stream for example for communication over 30881ad8388SMartin Matuska * network. 30981ad8388SMartin Matuska * 31081ad8388SMartin Matuska * Only some filters support LZMA_SYNC_FLUSH. Trying to use 31181ad8388SMartin Matuska * LZMA_SYNC_FLUSH with filters that don't support it will 31281ad8388SMartin Matuska * make lzma_code() return LZMA_OPTIONS_ERROR. For example, 31381ad8388SMartin Matuska * LZMA1 doesn't support LZMA_SYNC_FLUSH but LZMA2 does. 31481ad8388SMartin Matuska * 31581ad8388SMartin Matuska * Using LZMA_SYNC_FLUSH very often can dramatically reduce 31681ad8388SMartin Matuska * the compression ratio. With some filters (for example, 31781ad8388SMartin Matuska * LZMA2), fine-tuning the compression options may help 318542aef48SMartin Matuska * mitigate this problem significantly (for example, 319542aef48SMartin Matuska * match finder with LZMA2). 32081ad8388SMartin Matuska * 32181ad8388SMartin Matuska * Decoders don't support LZMA_SYNC_FLUSH. 32281ad8388SMartin Matuska */ 32381ad8388SMartin Matuska 32481ad8388SMartin Matuska LZMA_FULL_FLUSH = 2, 32581ad8388SMartin Matuska /**< 326542aef48SMartin Matuska * \brief Finish encoding of the current Block 32781ad8388SMartin Matuska * 328542aef48SMartin Matuska * All the input data going to the current Block must have 329542aef48SMartin Matuska * been given to the encoder (the last bytes can still be 330542aef48SMartin Matuska * pending in *next_in). Call lzma_code() with LZMA_FULL_FLUSH 331542aef48SMartin Matuska * until it returns LZMA_STREAM_END. Then continue normally 332542aef48SMartin Matuska * with LZMA_RUN or finish the Stream with LZMA_FINISH. 33381ad8388SMartin Matuska * 33481ad8388SMartin Matuska * This action is currently supported only by Stream encoder 33581ad8388SMartin Matuska * and easy encoder (which uses Stream encoder). If there is 33681ad8388SMartin Matuska * no unfinished Block, no empty Block is created. 33781ad8388SMartin Matuska */ 33881ad8388SMartin Matuska 33953200025SRui Paulo LZMA_FULL_BARRIER = 4, 34053200025SRui Paulo /**< 34153200025SRui Paulo * \brief Finish encoding of the current Block 34253200025SRui Paulo * 34353200025SRui Paulo * This is like LZMA_FULL_FLUSH except that this doesn't 34453200025SRui Paulo * necessarily wait until all the input has been made 34553200025SRui Paulo * available via the output buffer. That is, lzma_code() 34653200025SRui Paulo * might return LZMA_STREAM_END as soon as all the input 34753200025SRui Paulo * has been consumed (avail_in == 0). 34853200025SRui Paulo * 34953200025SRui Paulo * LZMA_FULL_BARRIER is useful with a threaded encoder if 35053200025SRui Paulo * one wants to split the .xz Stream into Blocks at specific 35153200025SRui Paulo * offsets but doesn't care if the output isn't flushed 35253200025SRui Paulo * immediately. Using LZMA_FULL_BARRIER allows keeping 35353200025SRui Paulo * the threads busy while LZMA_FULL_FLUSH would make 35453200025SRui Paulo * lzma_code() wait until all the threads have finished 35553200025SRui Paulo * until more data could be passed to the encoder. 35653200025SRui Paulo * 35753200025SRui Paulo * With a lzma_stream initialized with the single-threaded 35853200025SRui Paulo * lzma_stream_encoder() or lzma_easy_encoder(), 35953200025SRui Paulo * LZMA_FULL_BARRIER is an alias for LZMA_FULL_FLUSH. 36053200025SRui Paulo */ 36153200025SRui Paulo 36281ad8388SMartin Matuska LZMA_FINISH = 3 36381ad8388SMartin Matuska /**< 36481ad8388SMartin Matuska * \brief Finish the coding operation 36581ad8388SMartin Matuska * 366542aef48SMartin Matuska * All the input data must have been given to the encoder 367542aef48SMartin Matuska * (the last bytes can still be pending in next_in). 368542aef48SMartin Matuska * Call lzma_code() with LZMA_FINISH until it returns 369542aef48SMartin Matuska * LZMA_STREAM_END. Once LZMA_FINISH has been used, 370542aef48SMartin Matuska * the amount of input must no longer be changed by 371542aef48SMartin Matuska * the application. 37281ad8388SMartin Matuska * 37381ad8388SMartin Matuska * When decoding, using LZMA_FINISH is optional unless the 37481ad8388SMartin Matuska * LZMA_CONCATENATED flag was used when the decoder was 37581ad8388SMartin Matuska * initialized. When LZMA_CONCATENATED was not used, the only 37681ad8388SMartin Matuska * effect of LZMA_FINISH is that the amount of input must not 37781ad8388SMartin Matuska * be changed just like in the encoder. 37881ad8388SMartin Matuska */ 37981ad8388SMartin Matuska } lzma_action; 38081ad8388SMartin Matuska 38181ad8388SMartin Matuska 38281ad8388SMartin Matuska /** 38381ad8388SMartin Matuska * \brief Custom functions for memory handling 38481ad8388SMartin Matuska * 38581ad8388SMartin Matuska * A pointer to lzma_allocator may be passed via lzma_stream structure 38681ad8388SMartin Matuska * to liblzma, and some advanced functions take a pointer to lzma_allocator 38781ad8388SMartin Matuska * as a separate function argument. The library will use the functions 38881ad8388SMartin Matuska * specified in lzma_allocator for memory handling instead of the default 38981ad8388SMartin Matuska * malloc() and free(). C++ users should note that the custom memory 39081ad8388SMartin Matuska * handling functions must not throw exceptions. 39181ad8388SMartin Matuska * 39253200025SRui Paulo * Single-threaded mode only: liblzma doesn't make an internal copy of 39353200025SRui Paulo * lzma_allocator. Thus, it is OK to change these function pointers in 39453200025SRui Paulo * the middle of the coding process, but obviously it must be done 395*3b35e7eeSXin LI * carefully to make sure that the replacement 'free' can deallocate 396*3b35e7eeSXin LI * memory allocated by the earlier 'alloc' function(s). 39753200025SRui Paulo * 39853200025SRui Paulo * Multithreaded mode: liblzma might internally store pointers to the 39953200025SRui Paulo * lzma_allocator given via the lzma_stream structure. The application 40053200025SRui Paulo * must not change the allocator pointer in lzma_stream or the contents 40153200025SRui Paulo * of the pointed lzma_allocator structure until lzma_end() has been used 40253200025SRui Paulo * to free the memory associated with that lzma_stream. The allocation 40353200025SRui Paulo * functions might be called simultaneously from multiple threads, and 40453200025SRui Paulo * thus they must be thread safe. 40581ad8388SMartin Matuska */ 40681ad8388SMartin Matuska typedef struct { 40781ad8388SMartin Matuska /** 40881ad8388SMartin Matuska * \brief Pointer to a custom memory allocation function 40981ad8388SMartin Matuska * 41081ad8388SMartin Matuska * If you don't want a custom allocator, but still want 41181ad8388SMartin Matuska * custom free(), set this to NULL and liblzma will use 41281ad8388SMartin Matuska * the standard malloc(). 41381ad8388SMartin Matuska * 41481ad8388SMartin Matuska * \param opaque lzma_allocator.opaque (see below) 41581ad8388SMartin Matuska * \param nmemb Number of elements like in calloc(). liblzma 41681ad8388SMartin Matuska * will always set nmemb to 1, so it is safe to 41781ad8388SMartin Matuska * ignore nmemb in a custom allocator if you like. 41881ad8388SMartin Matuska * The nmemb argument exists only for 41981ad8388SMartin Matuska * compatibility with zlib and libbzip2. 42081ad8388SMartin Matuska * \param size Size of an element in bytes. 42181ad8388SMartin Matuska * liblzma never sets this to zero. 42281ad8388SMartin Matuska * 42381ad8388SMartin Matuska * \return Pointer to the beginning of a memory block of 424*3b35e7eeSXin LI * 'size' bytes, or NULL if allocation fails 42581ad8388SMartin Matuska * for some reason. When allocation fails, functions 42681ad8388SMartin Matuska * of liblzma return LZMA_MEM_ERROR. 42781ad8388SMartin Matuska * 42881ad8388SMartin Matuska * The allocator should not waste time zeroing the allocated buffers. 42981ad8388SMartin Matuska * This is not only about speed, but also memory usage, since the 43081ad8388SMartin Matuska * operating system kernel doesn't necessarily allocate the requested 43181ad8388SMartin Matuska * memory in physical memory until it is actually used. With small 43281ad8388SMartin Matuska * input files, liblzma may actually need only a fraction of the 43381ad8388SMartin Matuska * memory that it requested for allocation. 43481ad8388SMartin Matuska * 43581ad8388SMartin Matuska * \note LZMA_MEM_ERROR is also used when the size of the 43681ad8388SMartin Matuska * allocation would be greater than SIZE_MAX. Thus, 43781ad8388SMartin Matuska * don't assume that the custom allocator must have 43881ad8388SMartin Matuska * returned NULL if some function from liblzma 43981ad8388SMartin Matuska * returns LZMA_MEM_ERROR. 44081ad8388SMartin Matuska */ 44181ad8388SMartin Matuska void *(LZMA_API_CALL *alloc)(void *opaque, size_t nmemb, size_t size); 44281ad8388SMartin Matuska 44381ad8388SMartin Matuska /** 44481ad8388SMartin Matuska * \brief Pointer to a custom memory freeing function 44581ad8388SMartin Matuska * 44681ad8388SMartin Matuska * If you don't want a custom freeing function, but still 44781ad8388SMartin Matuska * want a custom allocator, set this to NULL and liblzma 44881ad8388SMartin Matuska * will use the standard free(). 44981ad8388SMartin Matuska * 45081ad8388SMartin Matuska * \param opaque lzma_allocator.opaque (see below) 45181ad8388SMartin Matuska * \param ptr Pointer returned by lzma_allocator.alloc(), 45281ad8388SMartin Matuska * or when it is set to NULL, a pointer returned 45381ad8388SMartin Matuska * by the standard malloc(). 45481ad8388SMartin Matuska */ 45581ad8388SMartin Matuska void (LZMA_API_CALL *free)(void *opaque, void *ptr); 45681ad8388SMartin Matuska 45781ad8388SMartin Matuska /** 45881ad8388SMartin Matuska * \brief Pointer passed to .alloc() and .free() 45981ad8388SMartin Matuska * 46081ad8388SMartin Matuska * opaque is passed as the first argument to lzma_allocator.alloc() 46181ad8388SMartin Matuska * and lzma_allocator.free(). This intended to ease implementing 46281ad8388SMartin Matuska * custom memory allocation functions for use with liblzma. 46381ad8388SMartin Matuska * 46481ad8388SMartin Matuska * If you don't need this, you should set this to NULL. 46581ad8388SMartin Matuska */ 46681ad8388SMartin Matuska void *opaque; 46781ad8388SMartin Matuska 46881ad8388SMartin Matuska } lzma_allocator; 46981ad8388SMartin Matuska 47081ad8388SMartin Matuska 47181ad8388SMartin Matuska /** 47281ad8388SMartin Matuska * \brief Internal data structure 47381ad8388SMartin Matuska * 47481ad8388SMartin Matuska * The contents of this structure is not visible outside the library. 47581ad8388SMartin Matuska */ 47681ad8388SMartin Matuska typedef struct lzma_internal_s lzma_internal; 47781ad8388SMartin Matuska 47881ad8388SMartin Matuska 47981ad8388SMartin Matuska /** 48081ad8388SMartin Matuska * \brief Passing data to and from liblzma 48181ad8388SMartin Matuska * 48281ad8388SMartin Matuska * The lzma_stream structure is used for 48381ad8388SMartin Matuska * - passing pointers to input and output buffers to liblzma; 4849e6bbe47SXin LI * - defining custom memory handler functions; and 48581ad8388SMartin Matuska * - holding a pointer to coder-specific internal data structures. 48681ad8388SMartin Matuska * 48781ad8388SMartin Matuska * Typical usage: 48881ad8388SMartin Matuska * 48981ad8388SMartin Matuska * - After allocating lzma_stream (on stack or with malloc()), it must be 49081ad8388SMartin Matuska * initialized to LZMA_STREAM_INIT (see LZMA_STREAM_INIT for details). 49181ad8388SMartin Matuska * 49281ad8388SMartin Matuska * - Initialize a coder to the lzma_stream, for example by using 49381ad8388SMartin Matuska * lzma_easy_encoder() or lzma_auto_decoder(). Some notes: 49481ad8388SMartin Matuska * - In contrast to zlib, strm->next_in and strm->next_out are 49581ad8388SMartin Matuska * ignored by all initialization functions, thus it is safe 49681ad8388SMartin Matuska * to not initialize them yet. 49781ad8388SMartin Matuska * - The initialization functions always set strm->total_in and 49881ad8388SMartin Matuska * strm->total_out to zero. 49981ad8388SMartin Matuska * - If the initialization function fails, no memory is left allocated 50081ad8388SMartin Matuska * that would require freeing with lzma_end() even if some memory was 50181ad8388SMartin Matuska * associated with the lzma_stream structure when the initialization 50281ad8388SMartin Matuska * function was called. 50381ad8388SMartin Matuska * 50481ad8388SMartin Matuska * - Use lzma_code() to do the actual work. 50581ad8388SMartin Matuska * 50681ad8388SMartin Matuska * - Once the coding has been finished, the existing lzma_stream can be 50781ad8388SMartin Matuska * reused. It is OK to reuse lzma_stream with different initialization 50881ad8388SMartin Matuska * function without calling lzma_end() first. Old allocations are 50981ad8388SMartin Matuska * automatically freed. 51081ad8388SMartin Matuska * 51181ad8388SMartin Matuska * - Finally, use lzma_end() to free the allocated memory. lzma_end() never 51281ad8388SMartin Matuska * frees the lzma_stream structure itself. 51381ad8388SMartin Matuska * 51481ad8388SMartin Matuska * Application may modify the values of total_in and total_out as it wants. 51581ad8388SMartin Matuska * They are updated by liblzma to match the amount of data read and 51653200025SRui Paulo * written but aren't used for anything else except as a possible return 51753200025SRui Paulo * values from lzma_get_progress(). 51881ad8388SMartin Matuska */ 51981ad8388SMartin Matuska typedef struct { 52081ad8388SMartin Matuska const uint8_t *next_in; /**< Pointer to the next input byte. */ 52181ad8388SMartin Matuska size_t avail_in; /**< Number of available input bytes in next_in. */ 52281ad8388SMartin Matuska uint64_t total_in; /**< Total number of bytes read by liblzma. */ 52381ad8388SMartin Matuska 52481ad8388SMartin Matuska uint8_t *next_out; /**< Pointer to the next output position. */ 52581ad8388SMartin Matuska size_t avail_out; /**< Amount of free space in next_out. */ 52681ad8388SMartin Matuska uint64_t total_out; /**< Total number of bytes written by liblzma. */ 52781ad8388SMartin Matuska 52881ad8388SMartin Matuska /** 52981ad8388SMartin Matuska * \brief Custom memory allocation functions 53081ad8388SMartin Matuska * 53181ad8388SMartin Matuska * In most cases this is NULL which makes liblzma use 53281ad8388SMartin Matuska * the standard malloc() and free(). 53353200025SRui Paulo * 53453200025SRui Paulo * \note In 5.0.x this is not a const pointer. 53581ad8388SMartin Matuska */ 53653200025SRui Paulo const lzma_allocator *allocator; 53781ad8388SMartin Matuska 53881ad8388SMartin Matuska /** Internal state is not visible to applications. */ 53981ad8388SMartin Matuska lzma_internal *internal; 54081ad8388SMartin Matuska 54181ad8388SMartin Matuska /* 54281ad8388SMartin Matuska * Reserved space to allow possible future extensions without 54381ad8388SMartin Matuska * breaking the ABI. Excluding the initialization of this structure, 54481ad8388SMartin Matuska * you should not touch these, because the names of these variables 54581ad8388SMartin Matuska * may change. 54681ad8388SMartin Matuska */ 547c917796cSXin LI 548c917796cSXin LI /** \private Reserved member. */ 54981ad8388SMartin Matuska void *reserved_ptr1; 550c917796cSXin LI 551c917796cSXin LI /** \private Reserved member. */ 55281ad8388SMartin Matuska void *reserved_ptr2; 553c917796cSXin LI 554c917796cSXin LI /** \private Reserved member. */ 555542aef48SMartin Matuska void *reserved_ptr3; 556c917796cSXin LI 557c917796cSXin LI /** \private Reserved member. */ 558542aef48SMartin Matuska void *reserved_ptr4; 55973ed8e77SXin LI 56073ed8e77SXin LI /** 56173ed8e77SXin LI * \brief New seek input position for LZMA_SEEK_NEEDED 56273ed8e77SXin LI * 56373ed8e77SXin LI * When lzma_code() returns LZMA_SEEK_NEEDED, the new input position 56473ed8e77SXin LI * needed by liblzma will be available seek_pos. The value is 56573ed8e77SXin LI * guaranteed to not exceed the file size that was specified when 56673ed8e77SXin LI * this lzma_stream was initialized. 56773ed8e77SXin LI * 56873ed8e77SXin LI * In all other situations the value of this variable is undefined. 56973ed8e77SXin LI */ 57073ed8e77SXin LI uint64_t seek_pos; 57173ed8e77SXin LI 572c917796cSXin LI /** \private Reserved member. */ 57381ad8388SMartin Matuska uint64_t reserved_int2; 574c917796cSXin LI 575c917796cSXin LI /** \private Reserved member. */ 576542aef48SMartin Matuska size_t reserved_int3; 577c917796cSXin LI 578c917796cSXin LI /** \private Reserved member. */ 579542aef48SMartin Matuska size_t reserved_int4; 580c917796cSXin LI 581c917796cSXin LI /** \private Reserved member. */ 58281ad8388SMartin Matuska lzma_reserved_enum reserved_enum1; 583c917796cSXin LI 584c917796cSXin LI /** \private Reserved member. */ 58581ad8388SMartin Matuska lzma_reserved_enum reserved_enum2; 58681ad8388SMartin Matuska 58781ad8388SMartin Matuska } lzma_stream; 58881ad8388SMartin Matuska 58981ad8388SMartin Matuska 59081ad8388SMartin Matuska /** 59181ad8388SMartin Matuska * \brief Initialization for lzma_stream 59281ad8388SMartin Matuska * 59381ad8388SMartin Matuska * When you declare an instance of lzma_stream, you can immediately 59481ad8388SMartin Matuska * initialize it so that initialization functions know that no memory 59581ad8388SMartin Matuska * has been allocated yet: 59681ad8388SMartin Matuska * 59781ad8388SMartin Matuska * lzma_stream strm = LZMA_STREAM_INIT; 59881ad8388SMartin Matuska * 59981ad8388SMartin Matuska * If you need to initialize a dynamically allocated lzma_stream, you can use 60081ad8388SMartin Matuska * memset(strm_pointer, 0, sizeof(lzma_stream)). Strictly speaking, this 60181ad8388SMartin Matuska * violates the C standard since NULL may have different internal 60281ad8388SMartin Matuska * representation than zero, but it should be portable enough in practice. 60381ad8388SMartin Matuska * Anyway, for maximum portability, you can use something like this: 60481ad8388SMartin Matuska * 60581ad8388SMartin Matuska * lzma_stream tmp = LZMA_STREAM_INIT; 60681ad8388SMartin Matuska * *strm = tmp; 60781ad8388SMartin Matuska */ 60881ad8388SMartin Matuska #define LZMA_STREAM_INIT \ 60981ad8388SMartin Matuska { NULL, 0, 0, NULL, 0, 0, NULL, NULL, \ 610542aef48SMartin Matuska NULL, NULL, NULL, NULL, 0, 0, 0, 0, \ 611542aef48SMartin Matuska LZMA_RESERVED_ENUM, LZMA_RESERVED_ENUM } 61281ad8388SMartin Matuska 61381ad8388SMartin Matuska 61481ad8388SMartin Matuska /** 61581ad8388SMartin Matuska * \brief Encode or decode data 61681ad8388SMartin Matuska * 61781ad8388SMartin Matuska * Once the lzma_stream has been successfully initialized (e.g. with 61881ad8388SMartin Matuska * lzma_stream_encoder()), the actual encoding or decoding is done 61981ad8388SMartin Matuska * using this function. The application has to update strm->next_in, 62081ad8388SMartin Matuska * strm->avail_in, strm->next_out, and strm->avail_out to pass input 62181ad8388SMartin Matuska * to and get output from liblzma. 62281ad8388SMartin Matuska * 62381ad8388SMartin Matuska * See the description of the coder-specific initialization function to find 624*3b35e7eeSXin LI * out what 'action' values are supported by the coder. 625c917796cSXin LI * 626c917796cSXin LI * \param strm Pointer to lzma_stream that is at least initialized 627c917796cSXin LI * with LZMA_STREAM_INIT. 628c917796cSXin LI * \param action Action for this function to take. Must be a valid 629c917796cSXin LI * lzma_action enum value. 630c917796cSXin LI * 631c917796cSXin LI * \return Any valid lzma_ret. See the lzma_ret enum description for more 632c917796cSXin LI * information. 63381ad8388SMartin Matuska */ 63481ad8388SMartin Matuska extern LZMA_API(lzma_ret) lzma_code(lzma_stream *strm, lzma_action action) 63581ad8388SMartin Matuska lzma_nothrow lzma_attr_warn_unused_result; 63681ad8388SMartin Matuska 63781ad8388SMartin Matuska 63881ad8388SMartin Matuska /** 63981ad8388SMartin Matuska * \brief Free memory allocated for the coder data structures 64081ad8388SMartin Matuska * 64181ad8388SMartin Matuska * After lzma_end(strm), strm->internal is guaranteed to be NULL. No other 64281ad8388SMartin Matuska * members of the lzma_stream structure are touched. 64381ad8388SMartin Matuska * 64481ad8388SMartin Matuska * \note zlib indicates an error if application end()s unfinished 64581ad8388SMartin Matuska * stream structure. liblzma doesn't do this, and assumes that 64681ad8388SMartin Matuska * application knows what it is doing. 647c917796cSXin LI * 648c917796cSXin LI * \param strm Pointer to lzma_stream that is at least initialized 649c917796cSXin LI * with LZMA_STREAM_INIT. 65081ad8388SMartin Matuska */ 65181ad8388SMartin Matuska extern LZMA_API(void) lzma_end(lzma_stream *strm) lzma_nothrow; 65281ad8388SMartin Matuska 65381ad8388SMartin Matuska 65481ad8388SMartin Matuska /** 65553200025SRui Paulo * \brief Get progress information 65653200025SRui Paulo * 65753200025SRui Paulo * In single-threaded mode, applications can get progress information from 65853200025SRui Paulo * strm->total_in and strm->total_out. In multi-threaded mode this is less 65953200025SRui Paulo * useful because a significant amount of both input and output data gets 66053200025SRui Paulo * buffered internally by liblzma. This makes total_in and total_out give 66153200025SRui Paulo * misleading information and also makes the progress indicator updates 66253200025SRui Paulo * non-smooth. 66353200025SRui Paulo * 66453200025SRui Paulo * This function gives realistic progress information also in multi-threaded 66553200025SRui Paulo * mode by taking into account the progress made by each thread. In 66653200025SRui Paulo * single-threaded mode *progress_in and *progress_out are set to 66753200025SRui Paulo * strm->total_in and strm->total_out, respectively. 668c917796cSXin LI * 669c917796cSXin LI * \param strm Pointer to lzma_stream that is at least 670c917796cSXin LI * initialized with LZMA_STREAM_INIT. 671c917796cSXin LI * \param[out] progress_in Pointer to the number of input bytes processed. 672c917796cSXin LI * \param[out] progress_out Pointer to the number of output bytes processed. 67353200025SRui Paulo */ 67453200025SRui Paulo extern LZMA_API(void) lzma_get_progress(lzma_stream *strm, 67553200025SRui Paulo uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow; 67653200025SRui Paulo 67753200025SRui Paulo 67853200025SRui Paulo /** 67981ad8388SMartin Matuska * \brief Get the memory usage of decoder filter chain 68081ad8388SMartin Matuska * 68181ad8388SMartin Matuska * This function is currently supported only when *strm has been initialized 68281ad8388SMartin Matuska * with a function that takes a memlimit argument. With other functions, you 68381ad8388SMartin Matuska * should use e.g. lzma_raw_encoder_memusage() or lzma_raw_decoder_memusage() 68481ad8388SMartin Matuska * to estimate the memory requirements. 68581ad8388SMartin Matuska * 68681ad8388SMartin Matuska * This function is useful e.g. after LZMA_MEMLIMIT_ERROR to find out how big 68781ad8388SMartin Matuska * the memory usage limit should have been to decode the input. Note that 68881ad8388SMartin Matuska * this may give misleading information if decoding .xz Streams that have 68981ad8388SMartin Matuska * multiple Blocks, because each Block can have different memory requirements. 69081ad8388SMartin Matuska * 691c917796cSXin LI * \param strm Pointer to lzma_stream that is at least initialized 692c917796cSXin LI * with LZMA_STREAM_INIT. 693c917796cSXin LI * 694542aef48SMartin Matuska * \return How much memory is currently allocated for the filter 695542aef48SMartin Matuska * decoders. If no filter chain is currently allocated, 696542aef48SMartin Matuska * some non-zero value is still returned, which is less than 697542aef48SMartin Matuska * or equal to what any filter chain would indicate as its 698542aef48SMartin Matuska * memory requirement. 69981ad8388SMartin Matuska * 70081ad8388SMartin Matuska * If this function isn't supported by *strm or some other error 70181ad8388SMartin Matuska * occurs, zero is returned. 70281ad8388SMartin Matuska */ 70381ad8388SMartin Matuska extern LZMA_API(uint64_t) lzma_memusage(const lzma_stream *strm) 70481ad8388SMartin Matuska lzma_nothrow lzma_attr_pure; 70581ad8388SMartin Matuska 70681ad8388SMartin Matuska 70781ad8388SMartin Matuska /** 70881ad8388SMartin Matuska * \brief Get the current memory usage limit 70981ad8388SMartin Matuska * 71081ad8388SMartin Matuska * This function is supported only when *strm has been initialized with 71181ad8388SMartin Matuska * a function that takes a memlimit argument. 71281ad8388SMartin Matuska * 713c917796cSXin LI * \param strm Pointer to lzma_stream that is at least initialized 714c917796cSXin LI * with LZMA_STREAM_INIT. 715c917796cSXin LI * 71681ad8388SMartin Matuska * \return On success, the current memory usage limit is returned 71781ad8388SMartin Matuska * (always non-zero). On error, zero is returned. 71881ad8388SMartin Matuska */ 71981ad8388SMartin Matuska extern LZMA_API(uint64_t) lzma_memlimit_get(const lzma_stream *strm) 72081ad8388SMartin Matuska lzma_nothrow lzma_attr_pure; 72181ad8388SMartin Matuska 72281ad8388SMartin Matuska 72381ad8388SMartin Matuska /** 72481ad8388SMartin Matuska * \brief Set the memory usage limit 72581ad8388SMartin Matuska * 72681ad8388SMartin Matuska * This function is supported only when *strm has been initialized with 72781ad8388SMartin Matuska * a function that takes a memlimit argument. 72881ad8388SMartin Matuska * 729b71a5db3SXin LI * liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes 730b71a5db3SXin LI * this function to do nothing (leaving the limit unchanged) and still 731b71a5db3SXin LI * return LZMA_OK. Later versions treat 0 as if 1 had been specified (so 732b71a5db3SXin LI * lzma_memlimit_get() will return 1 even if you specify 0 here). 733b71a5db3SXin LI * 7349e6bbe47SXin LI * liblzma 5.2.6 and earlier had a bug in single-threaded .xz decoder 7359e6bbe47SXin LI * (lzma_stream_decoder()) which made it impossible to continue decoding 7369e6bbe47SXin LI * after LZMA_MEMLIMIT_ERROR even if the limit was increased using 7379e6bbe47SXin LI * lzma_memlimit_set(). Other decoders worked correctly. 7389e6bbe47SXin LI * 739c917796cSXin LI * \return Possible lzma_ret values: 740c917796cSXin LI * - LZMA_OK: New memory usage limit successfully set. 74181ad8388SMartin Matuska * - LZMA_MEMLIMIT_ERROR: The new limit is too small. 74281ad8388SMartin Matuska * The limit was not changed. 74381ad8388SMartin Matuska * - LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't 744b71a5db3SXin LI * support memory usage limit. 74581ad8388SMartin Matuska */ 74681ad8388SMartin Matuska extern LZMA_API(lzma_ret) lzma_memlimit_set( 74781ad8388SMartin Matuska lzma_stream *strm, uint64_t memlimit) lzma_nothrow; 748