12940b44dSPeter Avalos /** 22940b44dSPeter Avalos * \file lzma/vli.h 32940b44dSPeter Avalos * \brief Variable-length integer handling 42940b44dSPeter Avalos * 52940b44dSPeter Avalos * In the .xz format, most integers are encoded in a variable-length 62940b44dSPeter Avalos * representation, which is sometimes called little endian base-128 encoding. 72940b44dSPeter Avalos * This saves space when smaller values are more likely than bigger values. 82940b44dSPeter Avalos * 92940b44dSPeter Avalos * The encoding scheme encodes seven bits to every byte, using minimum 102940b44dSPeter Avalos * number of bytes required to represent the given value. Encodings that use 112940b44dSPeter Avalos * non-minimum number of bytes are invalid, thus every integer has exactly 122940b44dSPeter Avalos * one encoded representation. The maximum number of bits in a VLI is 63, 132940b44dSPeter Avalos * thus the vli argument must be less than or equal to UINT64_MAX / 2. You 142940b44dSPeter Avalos * should use LZMA_VLI_MAX for clarity. 152940b44dSPeter Avalos */ 162940b44dSPeter Avalos 172940b44dSPeter Avalos /* 182940b44dSPeter Avalos * Author: Lasse Collin 192940b44dSPeter Avalos * 202940b44dSPeter Avalos * This file has been put into the public domain. 212940b44dSPeter Avalos * You can do whatever you want with this file. 222940b44dSPeter Avalos * 232940b44dSPeter Avalos * See ../lzma.h for information about liblzma as a whole. 242940b44dSPeter Avalos */ 252940b44dSPeter Avalos 262940b44dSPeter Avalos #ifndef LZMA_H_INTERNAL 272940b44dSPeter Avalos # error Never include this file directly. Use <lzma.h> instead. 282940b44dSPeter Avalos #endif 292940b44dSPeter Avalos 302940b44dSPeter Avalos 312940b44dSPeter Avalos /** 322940b44dSPeter Avalos * \brief Maximum supported value of a variable-length integer 332940b44dSPeter Avalos */ 342940b44dSPeter Avalos #define LZMA_VLI_MAX (UINT64_MAX / 2) 352940b44dSPeter Avalos 362940b44dSPeter Avalos /** 372940b44dSPeter Avalos * \brief VLI value to denote that the value is unknown 382940b44dSPeter Avalos */ 392940b44dSPeter Avalos #define LZMA_VLI_UNKNOWN UINT64_MAX 402940b44dSPeter Avalos 412940b44dSPeter Avalos /** 422940b44dSPeter Avalos * \brief Maximum supported encoded length of variable length integers 432940b44dSPeter Avalos */ 442940b44dSPeter Avalos #define LZMA_VLI_BYTES_MAX 9 452940b44dSPeter Avalos 462940b44dSPeter Avalos /** 472940b44dSPeter Avalos * \brief VLI constant suffix 482940b44dSPeter Avalos */ 492940b44dSPeter Avalos #define LZMA_VLI_C(n) UINT64_C(n) 502940b44dSPeter Avalos 512940b44dSPeter Avalos 522940b44dSPeter Avalos /** 532940b44dSPeter Avalos * \brief Variable-length integer type 542940b44dSPeter Avalos * 552940b44dSPeter Avalos * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is 562940b44dSPeter Avalos * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the 57*e151908bSDaniel Fojt * underlying integer type. 582940b44dSPeter Avalos * 592940b44dSPeter Avalos * lzma_vli will be uint64_t for the foreseeable future. If a bigger size 602940b44dSPeter Avalos * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will 612940b44dSPeter Avalos * not overflow lzma_vli. This simplifies integer overflow detection. 622940b44dSPeter Avalos */ 632940b44dSPeter Avalos typedef uint64_t lzma_vli; 642940b44dSPeter Avalos 652940b44dSPeter Avalos 662940b44dSPeter Avalos /** 672940b44dSPeter Avalos * \brief Validate a variable-length integer 682940b44dSPeter Avalos * 692940b44dSPeter Avalos * This is useful to test that application has given acceptable values 702940b44dSPeter Avalos * for example in the uncompressed_size and compressed_size variables. 712940b44dSPeter Avalos * 722940b44dSPeter Avalos * \return True if the integer is representable as VLI or if it 732940b44dSPeter Avalos * indicates unknown value. 742940b44dSPeter Avalos */ 752940b44dSPeter Avalos #define lzma_vli_is_valid(vli) \ 762940b44dSPeter Avalos ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN) 772940b44dSPeter Avalos 782940b44dSPeter Avalos 792940b44dSPeter Avalos /** 802940b44dSPeter Avalos * \brief Encode a variable-length integer 812940b44dSPeter Avalos * 822940b44dSPeter Avalos * This function has two modes: single-call and multi-call. Single-call mode 832940b44dSPeter Avalos * encodes the whole integer at once; it is an error if the output buffer is 842940b44dSPeter Avalos * too small. Multi-call mode saves the position in *vli_pos, and thus it is 852940b44dSPeter Avalos * possible to continue encoding if the buffer becomes full before the whole 862940b44dSPeter Avalos * integer has been encoded. 872940b44dSPeter Avalos * 882940b44dSPeter Avalos * \param vli Integer to be encoded 892940b44dSPeter Avalos * \param vli_pos How many VLI-encoded bytes have already been written 902940b44dSPeter Avalos * out. When starting to encode a new integer in 912940b44dSPeter Avalos * multi-call mode, *vli_pos must be set to zero. 922940b44dSPeter Avalos * To use single-call encoding, set vli_pos to NULL. 932940b44dSPeter Avalos * \param out Beginning of the output buffer 942940b44dSPeter Avalos * \param out_pos The next byte will be written to out[*out_pos]. 952940b44dSPeter Avalos * \param out_size Size of the out buffer; the first byte into 962940b44dSPeter Avalos * which no data is written to is out[out_size]. 972940b44dSPeter Avalos * 982940b44dSPeter Avalos * \return Slightly different return values are used in multi-call and 992940b44dSPeter Avalos * single-call modes. 1002940b44dSPeter Avalos * 1012940b44dSPeter Avalos * Single-call (vli_pos == NULL): 1022940b44dSPeter Avalos * - LZMA_OK: Integer successfully encoded. 1032940b44dSPeter Avalos * - LZMA_PROG_ERROR: Arguments are not sane. This can be due 1042940b44dSPeter Avalos * to too little output space; single-call mode doesn't use 1052940b44dSPeter Avalos * LZMA_BUF_ERROR, since the application should have checked 1062940b44dSPeter Avalos * the encoded size with lzma_vli_size(). 1072940b44dSPeter Avalos * 1082940b44dSPeter Avalos * Multi-call (vli_pos != NULL): 1092940b44dSPeter Avalos * - LZMA_OK: So far all OK, but the integer is not 1102940b44dSPeter Avalos * completely written out yet. 1112940b44dSPeter Avalos * - LZMA_STREAM_END: Integer successfully encoded. 1122940b44dSPeter Avalos * - LZMA_BUF_ERROR: No output space was provided. 1132940b44dSPeter Avalos * - LZMA_PROG_ERROR: Arguments are not sane. 1142940b44dSPeter Avalos */ 1152940b44dSPeter Avalos extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos, 1162940b44dSPeter Avalos uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; 1172940b44dSPeter Avalos 1182940b44dSPeter Avalos 1192940b44dSPeter Avalos /** 1202940b44dSPeter Avalos * \brief Decode a variable-length integer 1212940b44dSPeter Avalos * 1222940b44dSPeter Avalos * Like lzma_vli_encode(), this function has single-call and multi-call modes. 1232940b44dSPeter Avalos * 1242940b44dSPeter Avalos * \param vli Pointer to decoded integer. The decoder will 1252940b44dSPeter Avalos * initialize it to zero when *vli_pos == 0, so 1262940b44dSPeter Avalos * application isn't required to initialize *vli. 1272940b44dSPeter Avalos * \param vli_pos How many bytes have already been decoded. When 1282940b44dSPeter Avalos * starting to decode a new integer in multi-call 1292940b44dSPeter Avalos * mode, *vli_pos must be initialized to zero. To 1302940b44dSPeter Avalos * use single-call decoding, set vli_pos to NULL. 1312940b44dSPeter Avalos * \param in Beginning of the input buffer 1322940b44dSPeter Avalos * \param in_pos The next byte will be read from in[*in_pos]. 1332940b44dSPeter Avalos * \param in_size Size of the input buffer; the first byte that 1342940b44dSPeter Avalos * won't be read is in[in_size]. 1352940b44dSPeter Avalos * 1362940b44dSPeter Avalos * \return Slightly different return values are used in multi-call and 1372940b44dSPeter Avalos * single-call modes. 1382940b44dSPeter Avalos * 1392940b44dSPeter Avalos * Single-call (vli_pos == NULL): 1402940b44dSPeter Avalos * - LZMA_OK: Integer successfully decoded. 1412940b44dSPeter Avalos * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting 1422940b44dSPeter Avalos * the end of the input buffer before the whole integer was 1432940b44dSPeter Avalos * decoded; providing no input at all will use LZMA_DATA_ERROR. 1442940b44dSPeter Avalos * - LZMA_PROG_ERROR: Arguments are not sane. 1452940b44dSPeter Avalos * 1462940b44dSPeter Avalos * Multi-call (vli_pos != NULL): 1472940b44dSPeter Avalos * - LZMA_OK: So far all OK, but the integer is not 1482940b44dSPeter Avalos * completely decoded yet. 1492940b44dSPeter Avalos * - LZMA_STREAM_END: Integer successfully decoded. 1502940b44dSPeter Avalos * - LZMA_DATA_ERROR: Integer is corrupt. 1512940b44dSPeter Avalos * - LZMA_BUF_ERROR: No input was provided. 1522940b44dSPeter Avalos * - LZMA_PROG_ERROR: Arguments are not sane. 1532940b44dSPeter Avalos */ 1542940b44dSPeter Avalos extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos, 1552940b44dSPeter Avalos const uint8_t *in, size_t *in_pos, size_t in_size) 1562940b44dSPeter Avalos lzma_nothrow; 1572940b44dSPeter Avalos 1582940b44dSPeter Avalos 1592940b44dSPeter Avalos /** 1602940b44dSPeter Avalos * \brief Get the number of bytes required to encode a VLI 1612940b44dSPeter Avalos * 1622940b44dSPeter Avalos * \return Number of bytes on success (1-9). If vli isn't valid, 1632940b44dSPeter Avalos * zero is returned. 1642940b44dSPeter Avalos */ 1652940b44dSPeter Avalos extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) 1662940b44dSPeter Avalos lzma_nothrow lzma_attr_pure; 167