3 * \brief Variable-length integer handling
5 * \author Copyright (C) 1999-2006 Igor Pavlov
6 * \author Copyright (C) 2007 Lasse Collin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
19 #ifndef LZMA_H_INTERNAL
20 # error Never include this file directly. Use <lzma.h> instead.
25 * \brief Maximum supported value of variable-length integer
27 #define LZMA_VLI_VALUE_MAX (UINT64_MAX / 2)
30 * \brief VLI value to denote that the value is unknown
32 #define LZMA_VLI_VALUE_UNKNOWN UINT64_MAX
35 * \brief Maximum supported length of variable length integers
37 #define LZMA_VLI_BYTES_MAX 9
41 * \brief VLI constant suffix
43 #define LZMA_VLI_C(n) UINT64_C(n)
47 * \brief Variable-length integer type
49 * This will always be unsigned integer. Valid VLI values are in the range
50 * [0, LZMA_VLI_VALUE_MAX]. Unknown value is indicated with
51 * LZMA_VLI_VALUE_UNKNOWN, which is the maximum value of the underlaying
52 * integer type (this feature is useful in several situations).
54 * In future, even if lzma_vli is typdefined to something else than uint64_t,
55 * it is guaranteed that 2 * LZMA_VLI_VALUE_MAX will not overflow lzma_vli.
56 * This simplifies integer overflow detection.
58 typedef uint64_t lzma_vli;
62 * \brief Simple macro to validate variable-length integer
64 * This is useful to test that application has given acceptable values
65 * for example in the uncompressed_size and compressed_size variables.
67 * \return True if the integer is representable as VLI or if it
68 * indicates unknown value.
70 #define lzma_vli_is_valid(vli) \
71 ((vli) <= LZMA_VLI_VALUE_MAX || (vli) == LZMA_VLI_VALUE_UNKNOWN)
75 * \brief Sets VLI to given value with error checking
77 * \param dest Target variable which must have type of lzma_vli.
78 * \param src New value to be stored to dest.
79 * \param limit Maximum allowed value for src.
81 * \return False on success, true on error. If an error occurred,
82 * dest is left in undefined state (i.e. it's possible that
83 * it will be different in newer liblzma versions).
85 #define lzma_vli_set_lim(dest, src, limit) \
86 ((src) > (limit) || ((dest) = (src)) > (limit))
91 #define lzma_vli_add_lim(dest, src, limit) \
92 ((src) > (limit) || ((dest) += (src)) > (limit))
94 #define lzma_vli_add2_lim(dest, src1, src2, limit) \
95 (lzma_vli_add_lim(dest, src1, limit) \
96 || lzma_vli_add_lim(dest, src2, limit))
98 #define lzma_vli_add3_lim(dest, src1, src2, src3, limit) \
99 (lzma_vli_add_lim(dest, src1, limit) \
100 || lzma_vli_add_lim(dest, src2, limit) \
101 || lzma_vli_add_lim(dest, src3, limit))
103 #define lzma_vli_add4_lim(dest, src1, src2, src3, src4, limit) \
104 (lzma_vli_add_lim(dest, src1, limit) \
105 || lzma_vli_add_lim(dest, src2, limit) \
106 || lzma_vli_add_lim(dest, src3, limit) \
107 || lzma_vli_add_lim(dest, src4, limit))
109 #define lzma_vli_sum_lim(dest, src1, src2, limit) \
110 (lzma_vli_set_lim(dest, src1, limit) \
111 || lzma_vli_add_lim(dest, src2, limit))
113 #define lzma_vli_sum3_lim(dest, src1, src2, src3, limit) \
114 (lzma_vli_set_lim(dest, src1, limit) \
115 || lzma_vli_add_lim(dest, src2, limit) \
116 || lzma_vli_add_lim(dest, src3, limit))
118 #define lzma_vli_sum4_lim(dest, src1, src2, src3, src4, limit) \
119 (lzma_vli_set_lim(dest, src1, limit) \
120 || lzma_vli_add_lim(dest, src2, limit) \
121 || lzma_vli_add_lim(dest, src3, limit) \
122 || lzma_vli_add_lim(dest, src4, limit))
124 #define lzma_vli_set(dest, src) lzma_vli_set_lim(dest, src, LZMA_VLI_VALUE_MAX)
126 #define lzma_vli_add(dest, src) lzma_vli_add_lim(dest, src, LZMA_VLI_VALUE_MAX)
128 #define lzma_vli_add2(dest, src1, src2) \
129 lzma_vli_add2_lim(dest, src1, src2, LZMA_VLI_VALUE_MAX)
131 #define lzma_vli_add3(dest, src1, src2, src3) \
132 lzma_vli_add3_lim(dest, src1, src2, src3, LZMA_VLI_VALUE_MAX)
134 #define lzma_vli_add4(dest, src1, src2, src3, src4) \
135 lzma_vli_add4_lim(dest, src1, src2, src3, src4, LZMA_VLI_VALUE_MAX)
137 #define lzma_vli_sum(dest, src1, src2) \
138 lzma_vli_sum_lim(dest, src1, src2, LZMA_VLI_VALUE_MAX)
140 #define lzma_vli_sum3(dest, src1, src2, src3) \
141 lzma_vli_sum3_lim(dest, src1, src2, src3, LZMA_VLI_VALUE_MAX)
143 #define lzma_vli_sum4(dest, src1, src2, src3, src4) \
144 lzma_vli_sum4_lim(dest, src1, src2, src3, src4, LZMA_VLI_VALUE_MAX)
148 * \brief Encodes variable-length integer
150 * In the new .lzma format, most integers are encoded in variable-length
151 * representation. This saves space when smaller values are more likely
152 * than bigger values.
154 * The encoding scheme encodes seven bits to every byte, using minimum
155 * number of bytes required to represent the given value. In other words,
156 * it puts 7-63 bits into 1-9 bytes. This implementation limits the number
157 * of bits used to 63, thus num must be at maximum of UINT64_MAX / 2. You
158 * may use LZMA_VLI_VALUE_MAX for clarity.
160 * \param vli Integer to be encoded
161 * \param vli_pos How many VLI-encoded bytes have already been written
162 * out. When starting to encode a new integer, *vli_pos
163 * must be set to zero. To use single-call encoding,
164 * set vli_pos to NULL.
165 * \param out Beginning of the output buffer
166 * \param out_pos The next byte will be written to out[*out_pos].
167 * \param out_size Size of the out buffer; the first byte into
168 * which no data is written to is out[out_size].
170 * \return Slightly different return values are used in multi-call and
173 * Multi-call (vli_pos != NULL):
174 * - LZMA_OK: So far all OK, but the integer is not
175 * completely written out yet.
176 * - LZMA_STREAM_END: Integer successfully encoded.
177 * - LZMA_PROG_ERROR: Arguments are not sane. This can be due
178 * to no *out_pos == out_size; this function doesn't use
181 * Single-call (vli_pos == NULL):
182 * - LZMA_OK: Integer successfully encoded.
183 * - LZMA_PROG_ERROR: Arguments are not sane. This can be due
184 * to too little output space; this function doesn't use
187 extern lzma_ret lzma_vli_encode(
188 lzma_vli vli, size_t *lzma_restrict vli_pos,
189 uint8_t *lzma_restrict out, size_t *lzma_restrict out_pos,
194 * \brief Decodes variable-length integer
196 * \param vli Pointer to decoded integer. The decoder will
197 * initialize it to zero when *vli_pos == 0, so
198 * application isn't required to initialize *vli.
199 * \param vli_pos How many bytes have already been decoded. When
200 * starting to decode a new integer, *vli_pos must
201 * be initialized to zero. To use single-call decoding,
203 * \param in Beginning of the input buffer
204 * \param in_pos The next byte will be read from in[*in_pos].
205 * \param in_size Size of the input buffer; the first byte that
206 * won't be read is in[in_size].
208 * \return Slightly different return values are used in multi-call and
211 * Multi-call (vli_pos != NULL):
212 * - LZMA_OK: So far all OK, but the integer is not
213 * completely decoded yet.
214 * - LZMA_STREAM_END: Integer successfully decoded.
215 * - LZMA_DATA_ERROR: Integer is corrupt.
216 * - LZMA_PROG_ERROR: Arguments are not sane. This can be
217 * due to *in_pos == in_size; this function doesn't use
220 * Single-call (vli_pos == NULL):
221 * - LZMA_OK: Integer successfully decoded.
222 * - LZMA_DATA_ERROR: Integer is corrupt.
223 * - LZMA_PROG_ERROR: Arguments are not sane. This can be due to
224 * too little input; this function doesn't use LZMA_BUF_ERROR.
226 extern lzma_ret lzma_vli_decode(lzma_vli *lzma_restrict vli,
227 size_t *lzma_restrict vli_pos, const uint8_t *lzma_restrict in,
228 size_t *lzma_restrict in_pos, size_t in_size);
232 * \brief Gets the number of bytes required to encode vli
234 * \return Number of bytes on success (1-9). If vli isn't valid,
237 extern uint32_t lzma_vli_size(lzma_vli vli);