Path: blob/master/Utilities/cmliblzma/liblzma/api/lzma/vli.h
3158 views
/* SPDX-License-Identifier: 0BSD */12/**3* \file lzma/vli.h4* \brief Variable-length integer handling5* \note Never include this file directly. Use <lzma.h> instead.6*7* In the .xz format, most integers are encoded in a variable-length8* representation, which is sometimes called little endian base-128 encoding.9* This saves space when smaller values are more likely than bigger values.10*11* The encoding scheme encodes seven bits to every byte, using minimum12* number of bytes required to represent the given value. Encodings that use13* non-minimum number of bytes are invalid, thus every integer has exactly14* one encoded representation. The maximum number of bits in a VLI is 63,15* thus the vli argument must be less than or equal to UINT64_MAX / 2. You16* should use LZMA_VLI_MAX for clarity.17*/1819/*20* Author: Lasse Collin21*/2223#ifndef LZMA_H_INTERNAL24# error Never include this file directly. Use <lzma.h> instead.25#endif262728/**29* \brief Maximum supported value of a variable-length integer30*/31#define LZMA_VLI_MAX (UINT64_MAX / 2)3233/**34* \brief VLI value to denote that the value is unknown35*/36#define LZMA_VLI_UNKNOWN UINT64_MAX3738/**39* \brief Maximum supported encoded length of variable length integers40*/41#define LZMA_VLI_BYTES_MAX 94243/**44* \brief VLI constant suffix45*/46#define LZMA_VLI_C(n) UINT64_C(n)474849/**50* \brief Variable-length integer type51*52* Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is53* indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the54* underlying integer type.55*56* lzma_vli will be uint64_t for the foreseeable future. If a bigger size57* is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will58* not overflow lzma_vli. This simplifies integer overflow detection.59*/60typedef uint64_t lzma_vli;616263/**64* \brief Validate a variable-length integer65*66* This is useful to test that application has given acceptable values67* for example in the uncompressed_size and compressed_size variables.68*69* \return True if the integer is representable as a VLI or if it70* indicates an unknown value. False otherwise.71*/72#define lzma_vli_is_valid(vli) \73((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)747576/**77* \brief Encode a variable-length integer78*79* This function has two modes: single-call and multi-call. Single-call mode80* encodes the whole integer at once; it is an error if the output buffer is81* too small. Multi-call mode saves the position in *vli_pos, and thus it is82* possible to continue encoding if the buffer becomes full before the whole83* integer has been encoded.84*85* \param vli Integer to be encoded86* \param[out] vli_pos How many VLI-encoded bytes have already been written87* out. When starting to encode a new integer in88* multi-call mode, *vli_pos must be set to zero.89* To use single-call encoding, set vli_pos to NULL.90* \param[out] out Beginning of the output buffer91* \param[out] out_pos The next byte will be written to out[*out_pos].92* \param out_size Size of the out buffer; the first byte into93* which no data is written to is out[out_size].94*95* \return Slightly different return values are used in multi-call and96* single-call modes.97*98* Single-call (vli_pos == NULL):99* - LZMA_OK: Integer successfully encoded.100* - LZMA_PROG_ERROR: Arguments are not sane. This can be due101* to too little output space; single-call mode doesn't use102* LZMA_BUF_ERROR, since the application should have checked103* the encoded size with lzma_vli_size().104*105* Multi-call (vli_pos != NULL):106* - LZMA_OK: So far all OK, but the integer is not107* completely written out yet.108* - LZMA_STREAM_END: Integer successfully encoded.109* - LZMA_BUF_ERROR: No output space was provided.110* - LZMA_PROG_ERROR: Arguments are not sane.111*/112extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos,113uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;114115116/**117* \brief Decode a variable-length integer118*119* Like lzma_vli_encode(), this function has single-call and multi-call modes.120*121* \param[out] vli Pointer to decoded integer. The decoder will122* initialize it to zero when *vli_pos == 0, so123* application isn't required to initialize *vli.124* \param[out] vli_pos How many bytes have already been decoded. When125* starting to decode a new integer in multi-call126* mode, *vli_pos must be initialized to zero. To127* use single-call decoding, set vli_pos to NULL.128* \param in Beginning of the input buffer129* \param[out] in_pos The next byte will be read from in[*in_pos].130* \param in_size Size of the input buffer; the first byte that131* won't be read is in[in_size].132*133* \return Slightly different return values are used in multi-call and134* single-call modes.135*136* Single-call (vli_pos == NULL):137* - LZMA_OK: Integer successfully decoded.138* - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting139* the end of the input buffer before the whole integer was140* decoded; providing no input at all will use LZMA_DATA_ERROR.141* - LZMA_PROG_ERROR: Arguments are not sane.142*143* Multi-call (vli_pos != NULL):144* - LZMA_OK: So far all OK, but the integer is not145* completely decoded yet.146* - LZMA_STREAM_END: Integer successfully decoded.147* - LZMA_DATA_ERROR: Integer is corrupt.148* - LZMA_BUF_ERROR: No input was provided.149* - LZMA_PROG_ERROR: Arguments are not sane.150*/151extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,152const uint8_t *in, size_t *in_pos, size_t in_size)153lzma_nothrow;154155156/**157* \brief Get the number of bytes required to encode a VLI158*159* \param vli Integer whose encoded size is to be determined160*161* \return Number of bytes on success (1-9). If vli isn't valid,162* zero is returned.163*/164extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)165lzma_nothrow lzma_attr_pure;166167168