Path: blob/master/Utilities/cmliblzma/liblzma/api/lzma/block.h
3158 views
/* SPDX-License-Identifier: 0BSD */12/**3* \file lzma/block.h4* \brief .xz Block handling5* \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 Options for the Block and Block Header encoders and decoders19*20* Different Block handling functions use different parts of this structure.21* Some read some members, other functions write, and some do both. Only the22* members listed for reading need to be initialized when the specified23* functions are called. The members marked for writing will be assigned24* new values at some point either by calling the given function or by25* later calls to lzma_code().26*/27typedef struct {28/**29* \brief Block format version30*31* To prevent API and ABI breakages when new features are needed,32* a version number is used to indicate which members in this33* structure are in use:34* - liblzma >= 5.0.0: version = 0 is supported.35* - liblzma >= 5.1.4beta: Support for version = 1 was added,36* which adds the ignore_check member.37*38* If version is greater than one, most Block related functions39* will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works40* with any version value).41*42* Read by:43* - lzma_block_header_size()44* - lzma_block_header_encode()45* - lzma_block_header_decode()46* - lzma_block_compressed_size()47* - lzma_block_unpadded_size()48* - lzma_block_total_size()49* - lzma_block_encoder()50* - lzma_block_decoder()51* - lzma_block_buffer_encode()52* - lzma_block_uncomp_encode()53* - lzma_block_buffer_decode()54*55* Written by:56* - lzma_block_header_decode()57*/58uint32_t version;5960/**61* \brief Size of the Block Header field in bytes62*63* This is always a multiple of four.64*65* Read by:66* - lzma_block_header_encode()67* - lzma_block_header_decode()68* - lzma_block_compressed_size()69* - lzma_block_unpadded_size()70* - lzma_block_total_size()71* - lzma_block_decoder()72* - lzma_block_buffer_decode()73*74* Written by:75* - lzma_block_header_size()76* - lzma_block_buffer_encode()77* - lzma_block_uncomp_encode()78*/79uint32_t header_size;80# define LZMA_BLOCK_HEADER_SIZE_MIN 881# define LZMA_BLOCK_HEADER_SIZE_MAX 10248283/**84* \brief Type of integrity Check85*86* The Check ID is not stored into the Block Header, thus its value87* must be provided also when decoding.88*89* Read by:90* - lzma_block_header_encode()91* - lzma_block_header_decode()92* - lzma_block_compressed_size()93* - lzma_block_unpadded_size()94* - lzma_block_total_size()95* - lzma_block_encoder()96* - lzma_block_decoder()97* - lzma_block_buffer_encode()98* - lzma_block_buffer_decode()99*/100lzma_check check;101102/**103* \brief Size of the Compressed Data in bytes104*105* Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder106* will store this value to the Block Header. Block encoder doesn't107* care about this value, but will set it once the encoding has been108* finished.109*110* Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will111* verify that the size of the Compressed Data field matches112* compressed_size.113*114* Usually you don't know this value when encoding in streamed mode,115* and thus cannot write this field into the Block Header.116*117* In non-streamed mode you can reserve space for this field before118* encoding the actual Block. After encoding the data, finish the119* Block by encoding the Block Header. Steps in detail:120*121* - Set compressed_size to some big enough value. If you don't know122* better, use LZMA_VLI_MAX, but remember that bigger values take123* more space in Block Header.124*125* - Call lzma_block_header_size() to see how much space you need to126* reserve for the Block Header.127*128* - Encode the Block using lzma_block_encoder() and lzma_code().129* It sets compressed_size to the correct value.130*131* - Use lzma_block_header_encode() to encode the Block Header.132* Because space was reserved in the first step, you don't need133* to call lzma_block_header_size() anymore, because due to134* reserving, header_size has to be big enough. If it is "too big",135* lzma_block_header_encode() will add enough Header Padding to136* make Block Header to match the size specified by header_size.137*138* Read by:139* - lzma_block_header_size()140* - lzma_block_header_encode()141* - lzma_block_compressed_size()142* - lzma_block_unpadded_size()143* - lzma_block_total_size()144* - lzma_block_decoder()145* - lzma_block_buffer_decode()146*147* Written by:148* - lzma_block_header_decode()149* - lzma_block_compressed_size()150* - lzma_block_encoder()151* - lzma_block_decoder()152* - lzma_block_buffer_encode()153* - lzma_block_uncomp_encode()154* - lzma_block_buffer_decode()155*/156lzma_vli compressed_size;157158/**159* \brief Uncompressed Size in bytes160*161* This is handled very similarly to compressed_size above.162*163* uncompressed_size is needed by fewer functions than164* compressed_size. This is because uncompressed_size isn't165* needed to validate that Block stays within proper limits.166*167* Read by:168* - lzma_block_header_size()169* - lzma_block_header_encode()170* - lzma_block_decoder()171* - lzma_block_buffer_decode()172*173* Written by:174* - lzma_block_header_decode()175* - lzma_block_encoder()176* - lzma_block_decoder()177* - lzma_block_buffer_encode()178* - lzma_block_uncomp_encode()179* - lzma_block_buffer_decode()180*/181lzma_vli uncompressed_size;182183/**184* \brief Array of filters185*186* There can be 1-4 filters. The end of the array is marked with187* .id = LZMA_VLI_UNKNOWN.188*189* Read by:190* - lzma_block_header_size()191* - lzma_block_header_encode()192* - lzma_block_encoder()193* - lzma_block_decoder()194* - lzma_block_buffer_encode()195* - lzma_block_buffer_decode()196*197* Written by:198* - lzma_block_header_decode(): Note that this does NOT free()199* the old filter options structures. All unused filters[] will200* have .id == LZMA_VLI_UNKNOWN and .options == NULL. If201* decoding fails, all filters[] are guaranteed to be202* LZMA_VLI_UNKNOWN and NULL.203*204* \note Because of the array is terminated with205* .id = LZMA_VLI_UNKNOWN, the actual array must206* have LZMA_FILTERS_MAX + 1 members or the Block207* Header decoder will overflow the buffer.208*/209lzma_filter *filters;210211/**212* \brief Raw value stored in the Check field213*214* After successful coding, the first lzma_check_size(check) bytes215* of this array contain the raw value stored in the Check field.216*217* Note that CRC32 and CRC64 are stored in little endian byte order.218* Take it into account if you display the Check values to the user.219*220* Written by:221* - lzma_block_encoder()222* - lzma_block_decoder()223* - lzma_block_buffer_encode()224* - lzma_block_uncomp_encode()225* - lzma_block_buffer_decode()226*/227uint8_t raw_check[LZMA_CHECK_SIZE_MAX];228229/*230* Reserved space to allow possible future extensions without231* breaking the ABI. You should not touch these, because the names232* of these variables may change. These are and will never be used233* with the currently supported options, so it is safe to leave these234* uninitialized.235*/236237/** \private Reserved member. */238void *reserved_ptr1;239240/** \private Reserved member. */241void *reserved_ptr2;242243/** \private Reserved member. */244void *reserved_ptr3;245246/** \private Reserved member. */247uint32_t reserved_int1;248249/** \private Reserved member. */250uint32_t reserved_int2;251252/** \private Reserved member. */253lzma_vli reserved_int3;254255/** \private Reserved member. */256lzma_vli reserved_int4;257258/** \private Reserved member. */259lzma_vli reserved_int5;260261/** \private Reserved member. */262lzma_vli reserved_int6;263264/** \private Reserved member. */265lzma_vli reserved_int7;266267/** \private Reserved member. */268lzma_vli reserved_int8;269270/** \private Reserved member. */271lzma_reserved_enum reserved_enum1;272273/** \private Reserved member. */274lzma_reserved_enum reserved_enum2;275276/** \private Reserved member. */277lzma_reserved_enum reserved_enum3;278279/** \private Reserved member. */280lzma_reserved_enum reserved_enum4;281282/**283* \brief A flag to Block decoder to not verify the Check field284*285* This member is supported by liblzma >= 5.1.4beta if .version >= 1.286*287* If this is set to true, the integrity check won't be calculated288* and verified. Unless you know what you are doing, you should289* leave this to false. (A reason to set this to true is when the290* file integrity is verified externally anyway and you want to291* speed up the decompression, which matters mostly when using292* SHA-256 as the integrity check.)293*294* If .version >= 1, read by:295* - lzma_block_decoder()296* - lzma_block_buffer_decode()297*298* Written by (.version is ignored):299* - lzma_block_header_decode() always sets this to false300*/301lzma_bool ignore_check;302303/** \private Reserved member. */304lzma_bool reserved_bool2;305306/** \private Reserved member. */307lzma_bool reserved_bool3;308309/** \private Reserved member. */310lzma_bool reserved_bool4;311312/** \private Reserved member. */313lzma_bool reserved_bool5;314315/** \private Reserved member. */316lzma_bool reserved_bool6;317318/** \private Reserved member. */319lzma_bool reserved_bool7;320321/** \private Reserved member. */322lzma_bool reserved_bool8;323324} lzma_block;325326327/**328* \brief Decode the Block Header Size field329*330* To decode Block Header using lzma_block_header_decode(), the size of the331* Block Header has to be known and stored into lzma_block.header_size.332* The size can be calculated from the first byte of a Block using this macro.333* Note that if the first byte is 0x00, it indicates beginning of Index; use334* this macro only when the byte is not 0x00.335*336* There is no encoding macro because lzma_block_header_size() and337* lzma_block_header_encode() should be used.338*/339#define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)340341342/**343* \brief Calculate Block Header Size344*345* Calculate the minimum size needed for the Block Header field using the346* settings specified in the lzma_block structure. Note that it is OK to347* increase the calculated header_size value as long as it is a multiple of348* four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size349* just means that lzma_block_header_encode() will add Header Padding.350*351* \note This doesn't check that all the options are valid i.e. this352* may return LZMA_OK even if lzma_block_header_encode() or353* lzma_block_encoder() would fail. If you want to validate the354* filter chain, consider using lzma_memlimit_encoder() which as355* a side-effect validates the filter chain.356*357* \param block Block options358*359* \return Possible lzma_ret values:360* - LZMA_OK: Size calculated successfully and stored to361* block->header_size.362* - LZMA_OPTIONS_ERROR: Unsupported version, filters or363* filter options.364* - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.365*/366extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)367lzma_nothrow lzma_attr_warn_unused_result;368369370/**371* \brief Encode Block Header372*373* The caller must have calculated the size of the Block Header already with374* lzma_block_header_size(). If a value larger than the one calculated by375* lzma_block_header_size() is used, the Block Header will be padded to the376* specified size.377*378* \param block Block options to be encoded.379* \param[out] out Beginning of the output buffer. This must be380* at least block->header_size bytes.381*382* \return Possible lzma_ret values:383* - LZMA_OK: Encoding was successful. block->header_size384* bytes were written to output buffer.385* - LZMA_OPTIONS_ERROR: Invalid or unsupported options.386* - LZMA_PROG_ERROR: Invalid arguments, for example387* block->header_size is invalid or block->filters is NULL.388*/389extern LZMA_API(lzma_ret) lzma_block_header_encode(390const lzma_block *block, uint8_t *out)391lzma_nothrow lzma_attr_warn_unused_result;392393394/**395* \brief Decode Block Header396*397* block->version should (usually) be set to the highest value supported398* by the application. If the application sets block->version to a value399* higher than supported by the current liblzma version, this function will400* downgrade block->version to the highest value supported by it. Thus one401* should check the value of block->version after calling this function if402* block->version was set to a non-zero value and the application doesn't403* otherwise know that the liblzma version being used is new enough to404* support the specified block->version.405*406* The size of the Block Header must have already been decoded with407* lzma_block_header_size_decode() macro and stored to block->header_size.408*409* The integrity check type from Stream Header must have been stored410* to block->check.411*412* block->filters must have been allocated, but they don't need to be413* initialized (possible existing filter options are not freed).414*415* \param[out] block Destination for Block options416* \param allocator lzma_allocator for custom allocator functions.417* Set to NULL to use malloc() (and also free()418* if an error occurs).419* \param in Beginning of the input buffer. This must be420* at least block->header_size bytes.421*422* \return Possible lzma_ret values:423* - LZMA_OK: Decoding was successful. block->header_size424* bytes were read from the input buffer.425* - LZMA_OPTIONS_ERROR: The Block Header specifies some426* unsupported options such as unsupported filters. This can427* happen also if block->version was set to a too low value428* compared to what would be required to properly represent429* the information stored in the Block Header.430* - LZMA_DATA_ERROR: Block Header is corrupt, for example,431* the CRC32 doesn't match.432* - LZMA_PROG_ERROR: Invalid arguments, for example433* block->header_size is invalid or block->filters is NULL.434*/435extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,436const lzma_allocator *allocator, const uint8_t *in)437lzma_nothrow lzma_attr_warn_unused_result;438439440/**441* \brief Validate and set Compressed Size according to Unpadded Size442*443* Block Header stores Compressed Size, but Index has Unpadded Size. If the444* application has already parsed the Index and is now decoding Blocks,445* it can calculate Compressed Size from Unpadded Size. This function does446* exactly that with error checking:447*448* - Compressed Size calculated from Unpadded Size must be positive integer,449* that is, Unpadded Size must be big enough that after Block Header and450* Check fields there's still at least one byte for Compressed Size.451*452* - If Compressed Size was present in Block Header, the new value453* calculated from Unpadded Size is compared against the value454* from Block Header.455*456* \note This function must be called _after_ decoding the Block Header457* field so that it can properly validate Compressed Size if it458* was present in Block Header.459*460* \param block Block options: block->header_size must461* already be set with lzma_block_header_size().462* \param unpadded_size Unpadded Size from the Index field in bytes463*464* \return Possible lzma_ret values:465* - LZMA_OK: block->compressed_size was set successfully.466* - LZMA_DATA_ERROR: unpadded_size is too small compared to467* block->header_size and lzma_check_size(block->check).468* - LZMA_PROG_ERROR: Some values are invalid. For example,469* block->header_size must be a multiple of four and470* between 8 and 1024 inclusive.471*/472extern LZMA_API(lzma_ret) lzma_block_compressed_size(473lzma_block *block, lzma_vli unpadded_size)474lzma_nothrow lzma_attr_warn_unused_result;475476477/**478* \brief Calculate Unpadded Size479*480* The Index field stores Unpadded Size and Uncompressed Size. The latter481* can be taken directly from the lzma_block structure after coding a Block,482* but Unpadded Size needs to be calculated from Block Header Size,483* Compressed Size, and size of the Check field. This is where this function484* is needed.485*486* \param block Block options: block->header_size must already be487* set with lzma_block_header_size().488*489* \return Unpadded Size on success, or zero on error.490*/491extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)492lzma_nothrow lzma_attr_pure;493494495/**496* \brief Calculate the total encoded size of a Block497*498* This is equivalent to lzma_block_unpadded_size() except that the returned499* value includes the size of the Block Padding field.500*501* \param block Block options: block->header_size must already be502* set with lzma_block_header_size().503*504* \return On success, total encoded size of the Block. On error,505* zero is returned.506*/507extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)508lzma_nothrow lzma_attr_pure;509510511/**512* \brief Initialize .xz Block encoder513*514* Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the515* filter chain supports it), and LZMA_FINISH.516*517* The Block encoder encodes the Block Data, Block Padding, and Check value.518* It does NOT encode the Block Header which can be encoded with519* lzma_block_header_encode().520*521* \param strm Pointer to lzma_stream that is at least initialized522* with LZMA_STREAM_INIT.523* \param block Block options: block->version, block->check,524* and block->filters must have been initialized.525*526* \return Possible lzma_ret values:527* - LZMA_OK: All good, continue with lzma_code().528* - LZMA_MEM_ERROR529* - LZMA_OPTIONS_ERROR530* - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID531* that is not supported by this build of liblzma. Initializing532* the encoder failed.533* - LZMA_PROG_ERROR534*/535extern LZMA_API(lzma_ret) lzma_block_encoder(536lzma_stream *strm, lzma_block *block)537lzma_nothrow lzma_attr_warn_unused_result;538539540/**541* \brief Initialize .xz Block decoder542*543* Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using544* LZMA_FINISH is not required. It is supported only for convenience.545*546* The Block decoder decodes the Block Data, Block Padding, and Check value.547* It does NOT decode the Block Header which can be decoded with548* lzma_block_header_decode().549*550* \param strm Pointer to lzma_stream that is at least initialized551* with LZMA_STREAM_INIT.552* \param block Block options553*554* \return Possible lzma_ret values:555* - LZMA_OK: All good, continue with lzma_code().556* - LZMA_PROG_ERROR557* - LZMA_MEM_ERROR558*/559extern LZMA_API(lzma_ret) lzma_block_decoder(560lzma_stream *strm, lzma_block *block)561lzma_nothrow lzma_attr_warn_unused_result;562563564/**565* \brief Calculate maximum output size for single-call Block encoding566*567* This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.568* See the documentation of lzma_stream_buffer_bound().569*570* \param uncompressed_size Size of the data to be encoded with the571* single-call Block encoder.572*573* \return Maximum output size in bytes for single-call Block encoding.574*/575extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)576lzma_nothrow;577578579/**580* \brief Single-call .xz Block encoder581*582* In contrast to the multi-call encoder initialized with583* lzma_block_encoder(), this function encodes also the Block Header. This584* is required to make it possible to write appropriate Block Header also585* in case the data isn't compressible, and different filter chain has to be586* used to encode the data in uncompressed form using uncompressed chunks587* of the LZMA2 filter.588*589* When the data isn't compressible, header_size, compressed_size, and590* uncompressed_size are set just like when the data was compressible, but591* it is possible that header_size is too small to hold the filter chain592* specified in block->filters, because that isn't necessarily the filter593* chain that was actually used to encode the data. lzma_block_unpadded_size()594* still works normally, because it doesn't read the filters array.595*596* \param block Block options: block->version, block->check,597* and block->filters must have been initialized.598* \param allocator lzma_allocator for custom allocator functions.599* Set to NULL to use malloc() and free().600* \param in Beginning of the input buffer601* \param in_size Size of the input buffer602* \param[out] out Beginning of the output buffer603* \param[out] out_pos The next byte will be written to out[*out_pos].604* *out_pos is updated only if encoding succeeds.605* \param out_size Size of the out buffer; the first byte into606* which no data is written to is out[out_size].607*608* \return Possible lzma_ret values:609* - LZMA_OK: Encoding was successful.610* - LZMA_BUF_ERROR: Not enough output buffer space.611* - LZMA_UNSUPPORTED_CHECK612* - LZMA_OPTIONS_ERROR613* - LZMA_MEM_ERROR614* - LZMA_DATA_ERROR615* - LZMA_PROG_ERROR616*/617extern LZMA_API(lzma_ret) lzma_block_buffer_encode(618lzma_block *block, const lzma_allocator *allocator,619const uint8_t *in, size_t in_size,620uint8_t *out, size_t *out_pos, size_t out_size)621lzma_nothrow lzma_attr_warn_unused_result;622623624/**625* \brief Single-call uncompressed .xz Block encoder626*627* This is like lzma_block_buffer_encode() except this doesn't try to628* compress the data and instead encodes the data using LZMA2 uncompressed629* chunks. The required output buffer size can be determined with630* lzma_block_buffer_bound().631*632* Since the data won't be compressed, this function ignores block->filters.633* This function doesn't take lzma_allocator because this function doesn't634* allocate any memory from the heap.635*636* \param block Block options: block->version, block->check,637* and block->filters must have been initialized.638* \param in Beginning of the input buffer639* \param in_size Size of the input buffer640* \param[out] out Beginning of the output buffer641* \param[out] out_pos The next byte will be written to out[*out_pos].642* *out_pos is updated only if encoding succeeds.643* \param out_size Size of the out buffer; the first byte into644* which no data is written to is out[out_size].645*646* \return Possible lzma_ret values:647* - LZMA_OK: Encoding was successful.648* - LZMA_BUF_ERROR: Not enough output buffer space.649* - LZMA_UNSUPPORTED_CHECK650* - LZMA_OPTIONS_ERROR651* - LZMA_MEM_ERROR652* - LZMA_DATA_ERROR653* - LZMA_PROG_ERROR654*/655extern LZMA_API(lzma_ret) lzma_block_uncomp_encode(lzma_block *block,656const uint8_t *in, size_t in_size,657uint8_t *out, size_t *out_pos, size_t out_size)658lzma_nothrow lzma_attr_warn_unused_result;659660661/**662* \brief Single-call .xz Block decoder663*664* This is single-call equivalent of lzma_block_decoder(), and requires that665* the caller has already decoded Block Header and checked its memory usage.666*667* \param block Block options668* \param allocator lzma_allocator for custom allocator functions.669* Set to NULL to use malloc() and free().670* \param in Beginning of the input buffer671* \param in_pos The next byte will be read from in[*in_pos].672* *in_pos is updated only if decoding succeeds.673* \param in_size Size of the input buffer; the first byte that674* won't be read is in[in_size].675* \param[out] out Beginning of the output buffer676* \param[out] out_pos The next byte will be written to out[*out_pos].677* *out_pos is updated only if encoding succeeds.678* \param out_size Size of the out buffer; the first byte into679* which no data is written to is out[out_size].680*681* \return Possible lzma_ret values:682* - LZMA_OK: Decoding was successful.683* - LZMA_OPTIONS_ERROR684* - LZMA_DATA_ERROR685* - LZMA_MEM_ERROR686* - LZMA_BUF_ERROR: Output buffer was too small.687* - LZMA_PROG_ERROR688*/689extern LZMA_API(lzma_ret) lzma_block_buffer_decode(690lzma_block *block, const lzma_allocator *allocator,691const uint8_t *in, size_t *in_pos, size_t in_size,692uint8_t *out, size_t *out_pos, size_t out_size)693lzma_nothrow;694695696