1207753Smm/** 2207753Smm * \file lzma/vli.h 3207753Smm * \brief Variable-length integer handling 4207753Smm * 5207753Smm * In the .xz format, most integers are encoded in a variable-length 6207753Smm * representation, which is sometimes called little endian base-128 encoding. 7207753Smm * This saves space when smaller values are more likely than bigger values. 8207753Smm * 9207753Smm * The encoding scheme encodes seven bits to every byte, using minimum 10207753Smm * number of bytes required to represent the given value. Encodings that use 11207753Smm * non-minimum number of bytes are invalid, thus every integer has exactly 12207753Smm * one encoded representation. The maximum number of bits in a VLI is 63, 13207753Smm * thus the vli argument must be less than or equal to UINT64_MAX / 2. You 14207753Smm * should use LZMA_VLI_MAX for clarity. 15207753Smm */ 16207753Smm 17207753Smm/* 18207753Smm * Author: Lasse Collin 19207753Smm * 20207753Smm * This file has been put into the public domain. 21207753Smm * You can do whatever you want with this file. 22207753Smm * 23207753Smm * See ../lzma.h for information about liblzma as a whole. 24207753Smm */ 25207753Smm 26207753Smm#ifndef LZMA_H_INTERNAL 27207753Smm# error Never include this file directly. Use <lzma.h> instead. 28207753Smm#endif 29207753Smm 30207753Smm 31207753Smm/** 32215187Smm * \brief Maximum supported value of a variable-length integer 33207753Smm */ 34207753Smm#define LZMA_VLI_MAX (UINT64_MAX / 2) 35207753Smm 36207753Smm/** 37207753Smm * \brief VLI value to denote that the value is unknown 38207753Smm */ 39207753Smm#define LZMA_VLI_UNKNOWN UINT64_MAX 40207753Smm 41207753Smm/** 42215187Smm * \brief Maximum supported encoded length of variable length integers 43207753Smm */ 44207753Smm#define LZMA_VLI_BYTES_MAX 9 45207753Smm 46207753Smm/** 47207753Smm * \brief VLI constant suffix 48207753Smm */ 49207753Smm#define LZMA_VLI_C(n) UINT64_C(n) 50207753Smm 51207753Smm 52207753Smm/** 53207753Smm * \brief Variable-length integer type 54207753Smm * 55215187Smm * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is 56215187Smm * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the 57360523Sdelphij * underlying integer type. 58207753Smm * 59215187Smm * lzma_vli will be uint64_t for the foreseeable future. If a bigger size 60215187Smm * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will 61215187Smm * not overflow lzma_vli. This simplifies integer overflow detection. 62207753Smm */ 63207753Smmtypedef uint64_t lzma_vli; 64207753Smm 65207753Smm 66207753Smm/** 67215187Smm * \brief Validate a variable-length integer 68207753Smm * 69207753Smm * This is useful to test that application has given acceptable values 70207753Smm * for example in the uncompressed_size and compressed_size variables. 71207753Smm * 72207753Smm * \return True if the integer is representable as VLI or if it 73207753Smm * indicates unknown value. 74207753Smm */ 75207753Smm#define lzma_vli_is_valid(vli) \ 76207753Smm ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN) 77207753Smm 78207753Smm 79207753Smm/** 80207753Smm * \brief Encode a variable-length integer 81207753Smm * 82207753Smm * This function has two modes: single-call and multi-call. Single-call mode 83207753Smm * encodes the whole integer at once; it is an error if the output buffer is 84207753Smm * too small. Multi-call mode saves the position in *vli_pos, and thus it is 85207753Smm * possible to continue encoding if the buffer becomes full before the whole 86207753Smm * integer has been encoded. 87207753Smm * 88207753Smm * \param vli Integer to be encoded 89207753Smm * \param vli_pos How many VLI-encoded bytes have already been written 90215187Smm * out. When starting to encode a new integer in 91215187Smm * multi-call mode, *vli_pos must be set to zero. 92215187Smm * To use single-call encoding, set vli_pos to NULL. 93207753Smm * \param out Beginning of the output buffer 94207753Smm * \param out_pos The next byte will be written to out[*out_pos]. 95207753Smm * \param out_size Size of the out buffer; the first byte into 96207753Smm * which no data is written to is out[out_size]. 97207753Smm * 98207753Smm * \return Slightly different return values are used in multi-call and 99207753Smm * single-call modes. 100207753Smm * 101207753Smm * Single-call (vli_pos == NULL): 102207753Smm * - LZMA_OK: Integer successfully encoded. 103207753Smm * - LZMA_PROG_ERROR: Arguments are not sane. This can be due 104207753Smm * to too little output space; single-call mode doesn't use 105207753Smm * LZMA_BUF_ERROR, since the application should have checked 106207753Smm * the encoded size with lzma_vli_size(). 107207753Smm * 108207753Smm * Multi-call (vli_pos != NULL): 109207753Smm * - LZMA_OK: So far all OK, but the integer is not 110207753Smm * completely written out yet. 111207753Smm * - LZMA_STREAM_END: Integer successfully encoded. 112207753Smm * - LZMA_BUF_ERROR: No output space was provided. 113207753Smm * - LZMA_PROG_ERROR: Arguments are not sane. 114207753Smm */ 115213700Smmextern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos, 116213700Smm uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; 117207753Smm 118207753Smm 119207753Smm/** 120207753Smm * \brief Decode a variable-length integer 121207753Smm * 122207753Smm * Like lzma_vli_encode(), this function has single-call and multi-call modes. 123207753Smm * 124207753Smm * \param vli Pointer to decoded integer. The decoder will 125207753Smm * initialize it to zero when *vli_pos == 0, so 126207753Smm * application isn't required to initialize *vli. 127207753Smm * \param vli_pos How many bytes have already been decoded. When 128215187Smm * starting to decode a new integer in multi-call 129215187Smm * mode, *vli_pos must be initialized to zero. To 130215187Smm * use single-call decoding, set vli_pos to NULL. 131207753Smm * \param in Beginning of the input buffer 132207753Smm * \param in_pos The next byte will be read from in[*in_pos]. 133207753Smm * \param in_size Size of the input buffer; the first byte that 134207753Smm * won't be read is in[in_size]. 135207753Smm * 136207753Smm * \return Slightly different return values are used in multi-call and 137207753Smm * single-call modes. 138207753Smm * 139207753Smm * Single-call (vli_pos == NULL): 140207753Smm * - LZMA_OK: Integer successfully decoded. 141207753Smm * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting 142207753Smm * the end of the input buffer before the whole integer was 143207753Smm * decoded; providing no input at all will use LZMA_DATA_ERROR. 144207753Smm * - LZMA_PROG_ERROR: Arguments are not sane. 145207753Smm * 146207753Smm * Multi-call (vli_pos != NULL): 147207753Smm * - LZMA_OK: So far all OK, but the integer is not 148207753Smm * completely decoded yet. 149207753Smm * - LZMA_STREAM_END: Integer successfully decoded. 150207753Smm * - LZMA_DATA_ERROR: Integer is corrupt. 151207753Smm * - LZMA_BUF_ERROR: No input was provided. 152207753Smm * - LZMA_PROG_ERROR: Arguments are not sane. 153207753Smm */ 154213700Smmextern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos, 155213700Smm const uint8_t *in, size_t *in_pos, size_t in_size) 156213700Smm lzma_nothrow; 157207753Smm 158207753Smm 159207753Smm/** 160207753Smm * \brief Get the number of bytes required to encode a VLI 161207753Smm * 162207753Smm * \return Number of bytes on success (1-9). If vli isn't valid, 163207753Smm * zero is returned. 164207753Smm */ 165207753Smmextern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) 166207753Smm lzma_nothrow lzma_attr_pure; 167