| 1 | /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | // vim: expandtab:ts=8:sw=4:softtabstop=4: |
| 3 | /** |
| 4 | * \file lzma/vli.h |
| 5 | * \brief Variable-length integer handling |
| 6 | * |
| 7 | * In the .xz format, most integers are encoded in a variable-length |
| 8 | * 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 minimum |
| 12 | * number of bytes required to represent the given value. Encodings that use |
| 13 | * non-minimum number of bytes are invalid, thus every integer has exactly |
| 14 | * one encoded representation. The maximum number of bits in a VLI is 63, |
| 15 | * thus the vli argument must be at maximum of UINT64_MAX / 2. You should |
| 16 | * use LZMA_VLI_MAX for clarity. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Author: Lasse Collin |
| 21 | * |
| 22 | * This file has been put into the public domain. |
| 23 | * You can do whatever you want with this file. |
| 24 | * |
| 25 | * See ../lzma.h for information about liblzma as a whole. |
| 26 | */ |
| 27 | |
| 28 | #ifndef LZMA_H_INTERNAL |
| 29 | # error Never include this file directly. Use <lzma.h> instead. |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * \brief Maximum supported value of variable-length integer |
| 35 | */ |
| 36 | #define LZMA_VLI_MAX (UINT64_MAX / 2) |
| 37 | |
| 38 | /** |
| 39 | * \brief VLI value to denote that the value is unknown |
| 40 | */ |
| 41 | #define LZMA_VLI_UNKNOWN UINT64_MAX |
| 42 | |
| 43 | /** |
| 44 | * \brief Maximum supported length of variable length integers |
| 45 | */ |
| 46 | #define LZMA_VLI_BYTES_MAX 9 |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * \brief VLI constant suffix |
| 51 | */ |
| 52 | #define LZMA_VLI_C(n) UINT64_C(n) |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * \brief Variable-length integer type |
| 57 | * |
| 58 | * This will always be unsigned integer. Valid VLI values are in the range |
| 59 | * [0, LZMA_VLI_MAX]. Unknown value is indicated with LZMA_VLI_UNKNOWN, |
| 60 | * which is the maximum value of the underlaying integer type. |
| 61 | * |
| 62 | * In future, even if lzma_vli is typdefined to something else than uint64_t, |
| 63 | * it is guaranteed that 2 * LZMA_VLI_MAX will not overflow lzma_vli. |
| 64 | * This simplifies integer overflow detection. |
| 65 | */ |
| 66 | typedef uint64_t lzma_vli; |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * \brief Simple macro to validate variable-length integer |
| 71 | * |
| 72 | * This is useful to test that application has given acceptable values |
| 73 | * for example in the uncompressed_size and compressed_size variables. |
| 74 | * |
| 75 | * \return True if the integer is representable as VLI or if it |
| 76 | * indicates unknown value. |
| 77 | */ |
| 78 | #define lzma_vli_is_valid(vli) \ |
| 79 | ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN) |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * \brief Encode a variable-length integer |
| 84 | * |
| 85 | * This function has two modes: single-call and multi-call. Single-call mode |
| 86 | * encodes the whole integer at once; it is an error if the output buffer is |
| 87 | * too small. Multi-call mode saves the position in *vli_pos, and thus it is |
| 88 | * possible to continue encoding if the buffer becomes full before the whole |
| 89 | * integer has been encoded. |
| 90 | * |
| 91 | * \param vli Integer to be encoded |
| 92 | * \param vli_pos How many VLI-encoded bytes have already been written |
| 93 | * out. When starting to encode a new integer, *vli_pos |
| 94 | * must be set to zero. To use single-call encoding, |
| 95 | * set vli_pos to NULL. |
| 96 | * \param out Beginning of the output buffer |
| 97 | * \param out_pos The next byte will be written to out[*out_pos]. |
| 98 | * \param out_size Size of the out buffer; the first byte into |
| 99 | * which no data is written to is out[out_size]. |
| 100 | * |
| 101 | * \return Slightly different return values are used in multi-call and |
| 102 | * single-call modes. |
| 103 | * |
| 104 | * Single-call (vli_pos == NULL): |
| 105 | * - LZMA_OK: Integer successfully encoded. |
| 106 | * - LZMA_PROG_ERROR: Arguments are not sane. This can be due |
| 107 | * to too little output space; single-call mode doesn't use |
| 108 | * LZMA_BUF_ERROR, since the application should have checked |
| 109 | * the encoded size with lzma_vli_size(). |
| 110 | * |
| 111 | * Multi-call (vli_pos != NULL): |
| 112 | * - LZMA_OK: So far all OK, but the integer is not |
| 113 | * completely written out yet. |
| 114 | * - LZMA_STREAM_END: Integer successfully encoded. |
| 115 | * - LZMA_BUF_ERROR: No output space was provided. |
| 116 | * - LZMA_PROG_ERROR: Arguments are not sane. |
| 117 | */ |
| 118 | extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, |
| 119 | size_t *vli_pos, uint8_t *lzma_restrict out, |
| 120 | size_t *lzma_restrict out_pos, size_t out_size) lzma_nothrow; |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * \brief Decode a variable-length integer |
| 125 | * |
| 126 | * Like lzma_vli_encode(), this function has single-call and multi-call modes. |
| 127 | * |
| 128 | * \param vli Pointer to decoded integer. The decoder will |
| 129 | * initialize it to zero when *vli_pos == 0, so |
| 130 | * application isn't required to initialize *vli. |
| 131 | * \param vli_pos How many bytes have already been decoded. When |
| 132 | * starting to decode a new integer, *vli_pos must |
| 133 | * be initialized to zero. To use single-call decoding, |
| 134 | * set this to NULL. |
| 135 | * \param in Beginning of the input buffer |
| 136 | * \param in_pos The next byte will be read from in[*in_pos]. |
| 137 | * \param in_size Size of the input buffer; the first byte that |
| 138 | * won't be read is in[in_size]. |
| 139 | * |
| 140 | * \return Slightly different return values are used in multi-call and |
| 141 | * single-call modes. |
| 142 | * |
| 143 | * Single-call (vli_pos == NULL): |
| 144 | * - LZMA_OK: Integer successfully decoded. |
| 145 | * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting |
| 146 | * the end of the input buffer before the whole integer was |
| 147 | * decoded; providing no input at all will use LZMA_DATA_ERROR. |
| 148 | * - LZMA_PROG_ERROR: Arguments are not sane. |
| 149 | * |
| 150 | * Multi-call (vli_pos != NULL): |
| 151 | * - LZMA_OK: So far all OK, but the integer is not |
| 152 | * completely decoded yet. |
| 153 | * - LZMA_STREAM_END: Integer successfully decoded. |
| 154 | * - LZMA_DATA_ERROR: Integer is corrupt. |
| 155 | * - LZMA_BUF_ERROR: No input was provided. |
| 156 | * - LZMA_PROG_ERROR: Arguments are not sane. |
| 157 | */ |
| 158 | extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *lzma_restrict vli, |
| 159 | size_t *vli_pos, const uint8_t *lzma_restrict in, |
| 160 | size_t *lzma_restrict in_pos, size_t in_size) lzma_nothrow; |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * \brief Get the number of bytes required to encode a VLI |
| 165 | * |
| 166 | * \return Number of bytes on success (1-9). If vli isn't valid, |
| 167 | * zero is returned. |
| 168 | */ |
| 169 | extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) |
| 170 | lzma_nothrow lzma_attr_pure; |
| 171 | |