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