Path: blob/master/Utilities/cmliblzma/liblzma/api/lzma/lzma12.h
3158 views
/* SPDX-License-Identifier: 0BSD */12/**3* \file lzma/lzma12.h4* \brief LZMA1 and LZMA2 filters5* \note Never include this file directly. Use <lzma.h> instead.6*/78/*9* Author: Lasse Collin10*/1112#ifndef LZMA_H_INTERNAL13# error Never include this file directly. Use <lzma.h> instead.14#endif151617/**18* \brief LZMA1 Filter ID (for raw encoder/decoder only, not in .xz)19*20* LZMA1 is the very same thing as what was called just LZMA in LZMA Utils,21* 7-Zip, and LZMA SDK. It's called LZMA1 here to prevent developers from22* accidentally using LZMA when they actually want LZMA2.23*/24#define LZMA_FILTER_LZMA1 LZMA_VLI_C(0x4000000000000001)2526/**27* \brief LZMA1 Filter ID with extended options (for raw encoder/decoder)28*29* This is like LZMA_FILTER_LZMA1 but with this ID a few extra options30* are supported in the lzma_options_lzma structure:31*32* - A flag to tell the encoder if the end of payload marker (EOPM) alias33* end of stream (EOS) marker must be written at the end of the stream.34* In contrast, LZMA_FILTER_LZMA1 always writes the end marker.35*36* - Decoder needs to be told the uncompressed size of the stream37* or that it is unknown (using the special value UINT64_MAX).38* If the size is known, a flag can be set to allow the presence of39* the end marker anyway. In contrast, LZMA_FILTER_LZMA1 always40* behaves as if the uncompressed size was unknown.41*42* This allows handling file formats where LZMA1 streams are used but where43* the end marker isn't allowed or where it might not (always) be present.44* This extended LZMA1 functionality is provided as a Filter ID for raw45* encoder and decoder instead of adding new encoder and decoder initialization46* functions because this way it is possible to also use extra filters,47* for example, LZMA_FILTER_X86 in a filter chain with LZMA_FILTER_LZMA1EXT,48* which might be needed to handle some file formats.49*/50#define LZMA_FILTER_LZMA1EXT LZMA_VLI_C(0x4000000000000002)5152/**53* \brief LZMA2 Filter ID54*55* Usually you want this instead of LZMA1. Compared to LZMA1, LZMA2 adds56* support for LZMA_SYNC_FLUSH, uncompressed chunks (smaller expansion57* when trying to compress incompressible data), possibility to change58* lc/lp/pb in the middle of encoding, and some other internal improvements.59*/60#define LZMA_FILTER_LZMA2 LZMA_VLI_C(0x21)616263/**64* \brief Match finders65*66* Match finder has major effect on both speed and compression ratio.67* Usually hash chains are faster than binary trees.68*69* If you will use LZMA_SYNC_FLUSH often, the hash chains may be a better70* choice, because binary trees get much higher compression ratio penalty71* with LZMA_SYNC_FLUSH.72*73* The memory usage formulas are only rough estimates, which are closest to74* reality when dict_size is a power of two. The formulas are more complex75* in reality, and can also change a little between liblzma versions. Use76* lzma_raw_encoder_memusage() to get more accurate estimate of memory usage.77*/78typedef enum {79LZMA_MF_HC3 = 0x03,80/**<81* \brief Hash Chain with 2- and 3-byte hashing82*83* Minimum nice_len: 384*85* Memory usage:86* - dict_size <= 16 MiB: dict_size * 7.587* - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB88*/8990LZMA_MF_HC4 = 0x04,91/**<92* \brief Hash Chain with 2-, 3-, and 4-byte hashing93*94* Minimum nice_len: 495*96* Memory usage:97* - dict_size <= 32 MiB: dict_size * 7.598* - dict_size > 32 MiB: dict_size * 6.599*/100101LZMA_MF_BT2 = 0x12,102/**<103* \brief Binary Tree with 2-byte hashing104*105* Minimum nice_len: 2106*107* Memory usage: dict_size * 9.5108*/109110LZMA_MF_BT3 = 0x13,111/**<112* \brief Binary Tree with 2- and 3-byte hashing113*114* Minimum nice_len: 3115*116* Memory usage:117* - dict_size <= 16 MiB: dict_size * 11.5118* - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB119*/120121LZMA_MF_BT4 = 0x14122/**<123* \brief Binary Tree with 2-, 3-, and 4-byte hashing124*125* Minimum nice_len: 4126*127* Memory usage:128* - dict_size <= 32 MiB: dict_size * 11.5129* - dict_size > 32 MiB: dict_size * 10.5130*/131} lzma_match_finder;132133134/**135* \brief Test if given match finder is supported136*137* It is safe to call this with a value that isn't listed in138* lzma_match_finder enumeration; the return value will be false.139*140* There is no way to list which match finders are available in this141* particular liblzma version and build. It would be useless, because142* a new match finder, which the application developer wasn't aware,143* could require giving additional options to the encoder that the older144* match finders don't need.145*146* \param match_finder Match finder ID147*148* \return lzma_bool:149* - true if the match finder is supported by this liblzma build.150* - false otherwise.151*/152extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)153lzma_nothrow lzma_attr_const;154155156/**157* \brief Compression modes158*159* This selects the function used to analyze the data produced by the match160* finder.161*/162typedef enum {163LZMA_MODE_FAST = 1,164/**<165* \brief Fast compression166*167* Fast mode is usually at its best when combined with168* a hash chain match finder.169*/170171LZMA_MODE_NORMAL = 2172/**<173* \brief Normal compression174*175* This is usually notably slower than fast mode. Use this176* together with binary tree match finders to expose the177* full potential of the LZMA1 or LZMA2 encoder.178*/179} lzma_mode;180181182/**183* \brief Test if given compression mode is supported184*185* It is safe to call this with a value that isn't listed in lzma_mode186* enumeration; the return value will be false.187*188* There is no way to list which modes are available in this particular189* liblzma version and build. It would be useless, because a new compression190* mode, which the application developer wasn't aware, could require giving191* additional options to the encoder that the older modes don't need.192*193* \param mode Mode ID.194*195* \return lzma_bool:196* - true if the compression mode is supported by this liblzma197* build.198* - false otherwise.199*/200extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)201lzma_nothrow lzma_attr_const;202203204/**205* \brief Options specific to the LZMA1 and LZMA2 filters206*207* Since LZMA1 and LZMA2 share most of the code, it's simplest to share208* the options structure too. For encoding, all but the reserved variables209* need to be initialized unless specifically mentioned otherwise.210* lzma_lzma_preset() can be used to get a good starting point.211*212* For raw decoding, both LZMA1 and LZMA2 need dict_size, preset_dict, and213* preset_dict_size (if preset_dict != NULL). LZMA1 needs also lc, lp, and pb.214*/215typedef struct {216/**217* \brief Dictionary size in bytes218*219* Dictionary size indicates how many bytes of the recently processed220* uncompressed data is kept in memory. One method to reduce size of221* the uncompressed data is to store distance-length pairs, which222* indicate what data to repeat from the dictionary buffer. Thus,223* the bigger the dictionary, the better the compression ratio224* usually is.225*226* Maximum size of the dictionary depends on multiple things:227* - Memory usage limit228* - Available address space (not a problem on 64-bit systems)229* - Selected match finder (encoder only)230*231* Currently the maximum dictionary size for encoding is 1.5 GiB232* (i.e. (UINT32_C(1) << 30) + (UINT32_C(1) << 29)) even on 64-bit233* systems for certain match finder implementation reasons. In the234* future, there may be match finders that support bigger235* dictionaries.236*237* Decoder already supports dictionaries up to 4 GiB - 1 B (i.e.238* UINT32_MAX), so increasing the maximum dictionary size of the239* encoder won't cause problems for old decoders.240*241* Because extremely small dictionaries sizes would have unneeded242* overhead in the decoder, the minimum dictionary size is 4096 bytes.243*244* \note When decoding, too big dictionary does no other harm245* than wasting memory.246*/247uint32_t dict_size;248# define LZMA_DICT_SIZE_MIN UINT32_C(4096)249# define LZMA_DICT_SIZE_DEFAULT (UINT32_C(1) << 23)250251/**252* \brief Pointer to an initial dictionary253*254* It is possible to initialize the LZ77 history window using255* a preset dictionary. It is useful when compressing many256* similar, relatively small chunks of data independently from257* each other. The preset dictionary should contain typical258* strings that occur in the files being compressed. The most259* probable strings should be near the end of the preset dictionary.260*261* This feature should be used only in special situations. For262* now, it works correctly only with raw encoding and decoding.263* Currently none of the container formats supported by264* liblzma allow preset dictionary when decoding, thus if265* you create a .xz or .lzma file with preset dictionary, it266* cannot be decoded with the regular decoder functions. In the267* future, the .xz format will likely get support for preset268* dictionary though.269*/270const uint8_t *preset_dict;271272/**273* \brief Size of the preset dictionary274*275* Specifies the size of the preset dictionary. If the size is276* bigger than dict_size, only the last dict_size bytes are277* processed.278*279* This variable is read only when preset_dict is not NULL.280* If preset_dict is not NULL but preset_dict_size is zero,281* no preset dictionary is used (identical to only setting282* preset_dict to NULL).283*/284uint32_t preset_dict_size;285286/**287* \brief Number of literal context bits288*289* How many of the highest bits of the previous uncompressed290* eight-bit byte (also known as 'literal') are taken into291* account when predicting the bits of the next literal.292*293* E.g. in typical English text, an upper-case letter is294* often followed by a lower-case letter, and a lower-case295* letter is usually followed by another lower-case letter.296* In the US-ASCII character set, the highest three bits are 010297* for upper-case letters and 011 for lower-case letters.298* When lc is at least 3, the literal coding can take advantage of299* this property in the uncompressed data.300*301* There is a limit that applies to literal context bits and literal302* position bits together: lc + lp <= 4. Without this limit the303* decoding could become very slow, which could have security related304* results in some cases like email servers doing virus scanning.305* This limit also simplifies the internal implementation in liblzma.306*307* There may be LZMA1 streams that have lc + lp > 4 (maximum possible308* lc would be 8). It is not possible to decode such streams with309* liblzma.310*/311uint32_t lc;312# define LZMA_LCLP_MIN 0313# define LZMA_LCLP_MAX 4314# define LZMA_LC_DEFAULT 3315316/**317* \brief Number of literal position bits318*319* lp affects what kind of alignment in the uncompressed data is320* assumed when encoding literals. A literal is a single 8-bit byte.321* See pb below for more information about alignment.322*/323uint32_t lp;324# define LZMA_LP_DEFAULT 0325326/**327* \brief Number of position bits328*329* pb affects what kind of alignment in the uncompressed data is330* assumed in general. The default means four-byte alignment331* (2^ pb =2^2=4), which is often a good choice when there's332* no better guess.333*334* When the alignment is known, setting pb accordingly may reduce335* the file size a little. E.g. with text files having one-byte336* alignment (US-ASCII, ISO-8859-*, UTF-8), setting pb=0 can337* improve compression slightly. For UTF-16 text, pb=1 is a good338* choice. If the alignment is an odd number like 3 bytes, pb=0339* might be the best choice.340*341* Even though the assumed alignment can be adjusted with pb and342* lp, LZMA1 and LZMA2 still slightly favor 16-byte alignment.343* It might be worth taking into account when designing file formats344* that are likely to be often compressed with LZMA1 or LZMA2.345*/346uint32_t pb;347# define LZMA_PB_MIN 0348# define LZMA_PB_MAX 4349# define LZMA_PB_DEFAULT 2350351/** Compression mode */352lzma_mode mode;353354/**355* \brief Nice length of a match356*357* This determines how many bytes the encoder compares from the match358* candidates when looking for the best match. Once a match of at359* least nice_len bytes long is found, the encoder stops looking for360* better candidates and encodes the match. (Naturally, if the found361* match is actually longer than nice_len, the actual length is362* encoded; it's not truncated to nice_len.)363*364* Bigger values usually increase the compression ratio and365* compression time. For most files, 32 to 128 is a good value,366* which gives very good compression ratio at good speed.367*368* The exact minimum value depends on the match finder. The maximum369* is 273, which is the maximum length of a match that LZMA1 and370* LZMA2 can encode.371*/372uint32_t nice_len;373374/** Match finder ID */375lzma_match_finder mf;376377/**378* \brief Maximum search depth in the match finder379*380* For every input byte, match finder searches through the hash chain381* or binary tree in a loop, each iteration going one step deeper in382* the chain or tree. The searching stops if383* - a match of at least nice_len bytes long is found;384* - all match candidates from the hash chain or binary tree have385* been checked; or386* - maximum search depth is reached.387*388* Maximum search depth is needed to prevent the match finder from389* wasting too much time in case there are lots of short match390* candidates. On the other hand, stopping the search before all391* candidates have been checked can reduce compression ratio.392*393* Setting depth to zero tells liblzma to use an automatic default394* value, that depends on the selected match finder and nice_len.395* The default is in the range [4, 200] or so (it may vary between396* liblzma versions).397*398* Using a bigger depth value than the default can increase399* compression ratio in some cases. There is no strict maximum value,400* but high values (thousands or millions) should be used with care:401* the encoder could remain fast enough with typical input, but402* malicious input could cause the match finder to slow down403* dramatically, possibly creating a denial of service attack.404*/405uint32_t depth;406407/**408* \brief For LZMA_FILTER_LZMA1EXT: Extended flags409*410* This is used only with LZMA_FILTER_LZMA1EXT.411*412* Currently only one flag is supported, LZMA_LZMA1EXT_ALLOW_EOPM:413*414* - Encoder: If the flag is set, then end marker is written just415* like it is with LZMA_FILTER_LZMA1. Without this flag the416* end marker isn't written and the application has to store417* the uncompressed size somewhere outside the compressed stream.418* To decompress streams without the end marker, the application419* has to set the correct uncompressed size in ext_size_low and420* ext_size_high.421*422* - Decoder: If the uncompressed size in ext_size_low and423* ext_size_high is set to the special value UINT64_MAX424* (indicating unknown uncompressed size) then this flag is425* ignored and the end marker must always be present, that is,426* the behavior is identical to LZMA_FILTER_LZMA1.427*428* Otherwise, if this flag isn't set, then the input stream429* must not have the end marker; if the end marker is detected430* then it will result in LZMA_DATA_ERROR. This is useful when431* it is known that the stream must not have the end marker and432* strict validation is wanted.433*434* If this flag is set, then it is autodetected if the end marker435* is present after the specified number of uncompressed bytes436* has been decompressed (ext_size_low and ext_size_high). The437* end marker isn't allowed in any other position. This behavior438* is useful when uncompressed size is known but the end marker439* may or may not be present. This is the case, for example,440* in .7z files (valid .7z files that have the end marker in441* LZMA1 streams are rare but they do exist).442*/443uint32_t ext_flags;444# define LZMA_LZMA1EXT_ALLOW_EOPM UINT32_C(0x01)445446/**447* \brief For LZMA_FILTER_LZMA1EXT: Uncompressed size (low bits)448*449* The 64-bit uncompressed size is needed for decompression with450* LZMA_FILTER_LZMA1EXT. The size is ignored by the encoder.451*452* The special value UINT64_MAX indicates that the uncompressed size453* is unknown and that the end of payload marker (also known as454* end of stream marker) must be present to indicate the end of455* the LZMA1 stream. Any other value indicates the expected456* uncompressed size of the LZMA1 stream. (If LZMA1 was used together457* with filters that change the size of the data then the uncompressed458* size of the LZMA1 stream could be different than the final459* uncompressed size of the filtered stream.)460*461* ext_size_low holds the least significant 32 bits of the462* uncompressed size. The most significant 32 bits must be set463* in ext_size_high. The macro lzma_ext_size_set(opt_lzma, u64size)464* can be used to set these members.465*466* The 64-bit uncompressed size is split into two uint32_t variables467* because there were no reserved uint64_t members and using the468* same options structure for LZMA_FILTER_LZMA1, LZMA_FILTER_LZMA1EXT,469* and LZMA_FILTER_LZMA2 was otherwise more convenient than having470* a new options structure for LZMA_FILTER_LZMA1EXT. (Replacing two471* uint32_t members with one uint64_t changes the ABI on some systems472* as the alignment of this struct can increase from 4 bytes to 8.)473*/474uint32_t ext_size_low;475476/**477* \brief For LZMA_FILTER_LZMA1EXT: Uncompressed size (high bits)478*479* This holds the most significant 32 bits of the uncompressed size.480*/481uint32_t ext_size_high;482483/*484* Reserved space to allow possible future extensions without485* breaking the ABI. You should not touch these, because the names486* of these variables may change. These are and will never be used487* with the currently supported options, so it is safe to leave these488* uninitialized.489*/490491/** \private Reserved member. */492uint32_t reserved_int4;493494/** \private Reserved member. */495uint32_t reserved_int5;496497/** \private Reserved member. */498uint32_t reserved_int6;499500/** \private Reserved member. */501uint32_t reserved_int7;502503/** \private Reserved member. */504uint32_t reserved_int8;505506/** \private Reserved member. */507lzma_reserved_enum reserved_enum1;508509/** \private Reserved member. */510lzma_reserved_enum reserved_enum2;511512/** \private Reserved member. */513lzma_reserved_enum reserved_enum3;514515/** \private Reserved member. */516lzma_reserved_enum reserved_enum4;517518/** \private Reserved member. */519void *reserved_ptr1;520521/** \private Reserved member. */522void *reserved_ptr2;523524} lzma_options_lzma;525526527/**528* \brief Macro to set the 64-bit uncompressed size in ext_size_*529*530* This might be convenient when decoding using LZMA_FILTER_LZMA1EXT.531* This isn't used with LZMA_FILTER_LZMA1 or LZMA_FILTER_LZMA2.532*/533#define lzma_set_ext_size(opt_lzma2, u64size) \534do { \535(opt_lzma2).ext_size_low = (uint32_t)(u64size); \536(opt_lzma2).ext_size_high = (uint32_t)((uint64_t)(u64size) >> 32); \537} while (0)538539540/**541* \brief Set a compression preset to lzma_options_lzma structure542*543* 0 is the fastest and 9 is the slowest. These match the switches -0 .. -9544* of the xz command line tool. In addition, it is possible to bitwise-or545* flags to the preset. Currently only LZMA_PRESET_EXTREME is supported.546* The flags are defined in container.h, because the flags are used also547* with lzma_easy_encoder().548*549* The preset levels are subject to changes between liblzma versions.550*551* This function is available only if LZMA1 or LZMA2 encoder has been enabled552* when building liblzma.553*554* If features (like certain match finders) have been disabled at build time,555* then the function may return success (false) even though the resulting556* LZMA1/LZMA2 options may not be usable for encoder initialization557* (LZMA_OPTIONS_ERROR).558*559* \param[out] options Pointer to LZMA1 or LZMA2 options to be filled560* \param preset Preset level bitwse-ORed with preset flags561*562* \return lzma_bool:563* - true if the preset is not supported (failure).564* - false otherwise (success).565*/566extern LZMA_API(lzma_bool) lzma_lzma_preset(567lzma_options_lzma *options, uint32_t preset) lzma_nothrow;568569570