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_MAX (UINT64_MAX / 2)
30 * \brief VLI value to denote that the value is unknown
32 #define LZMA_VLI_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_MAX]. Unknown value is indicated with LZMA_VLI_UNKNOWN,
51 * which is the maximum value of the underlaying integer type (this feature
52 * 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_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_MAX || (vli) == LZMA_VLI_UNKNOWN)
75 * \brief Encodes variable-length integer
77 * In the .lzma format, most integers are encoded in variable-length
78 * representation. This saves space when smaller values are more likely
81 * The encoding scheme encodes seven bits to every byte, using minimum
82 * number of bytes required to represent the given value. Encodings that use
83 * non-minimum number of bytes are invalid, thus every integer has exactly
84 * one encoded representation. The maximum number of bits in a VLI is 63,
85 * thus the vli argument must be at maximum of UINT64_MAX / 2. You should
86 * use LZMA_VLI_MAX for clarity.
88 * This function has two modes: single-call and multi-call. Single-call mode
89 * encodes the whole integer at once; it is an error if the output buffer is
90 * too small. Multi-call mode saves the position in *vli_pos, and thus it is
91 * possible to continue encoding if the buffer becomes full before the whole
92 * integer has been encoded.
94 * \param vli Integer to be encoded
95 * \param vli_pos How many VLI-encoded bytes have already been written
96 * out. When starting to encode a new integer, *vli_pos
97 * must be set to zero. To use single-call encoding,
98 * set vli_pos to NULL.
99 * \param out Beginning of the output buffer
100 * \param out_pos The next byte will be written to out[*out_pos].
101 * \param out_size Size of the out buffer; the first byte into
102 * which no data is written to is out[out_size].
104 * \return Slightly different return values are used in multi-call and
107 * Single-call (vli_pos == NULL):
108 * - LZMA_OK: Integer successfully encoded.
109 * - LZMA_PROG_ERROR: Arguments are not sane. This can be due
110 * to too little output space; single-call mode doesn't use
111 * LZMA_BUF_ERROR, since the application should have checked
112 * the encoded size with lzma_vli_size().
114 * Multi-call (vli_pos != NULL):
115 * - LZMA_OK: So far all OK, but the integer is not
116 * completely written out yet.
117 * - LZMA_STREAM_END: Integer successfully encoded.
118 * - LZMA_BUF_ERROR: No output space was provided.
119 * - LZMA_PROG_ERROR: Arguments are not sane.
121 extern lzma_ret lzma_vli_encode(
122 lzma_vli vli, size_t *lzma_restrict vli_pos,
123 uint8_t *lzma_restrict out, size_t *lzma_restrict out_pos,
128 * \brief Decodes variable-length integer
130 * Like lzma_vli_encode(), this function has single-call and multi-call modes.
132 * \param vli Pointer to decoded integer. The decoder will
133 * initialize it to zero when *vli_pos == 0, so
134 * application isn't required to initialize *vli.
135 * \param vli_pos How many bytes have already been decoded. When
136 * starting to decode a new integer, *vli_pos must
137 * be initialized to zero. To use single-call decoding,
139 * \param in Beginning of the input buffer
140 * \param in_pos The next byte will be read from in[*in_pos].
141 * \param in_size Size of the input buffer; the first byte that
142 * won't be read is in[in_size].
144 * \return Slightly different return values are used in multi-call and
147 * Single-call (vli_pos == NULL):
148 * - LZMA_OK: Integer successfully decoded.
149 * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
150 * the end of the input buffer before the whole integer was
151 * decoded; providing no input at all will use LZMA_DATA_ERROR.
152 * - LZMA_PROG_ERROR: Arguments are not sane.
154 * Multi-call (vli_pos != NULL):
155 * - LZMA_OK: So far all OK, but the integer is not
156 * completely decoded yet.
157 * - LZMA_STREAM_END: Integer successfully decoded.
158 * - LZMA_DATA_ERROR: Integer is corrupt.
159 * - LZMA_BUF_ERROR: No input was provided.
160 * - LZMA_PROG_ERROR: Arguments are not sane.
162 extern lzma_ret lzma_vli_decode(lzma_vli *lzma_restrict vli,
163 size_t *lzma_restrict vli_pos, const uint8_t *lzma_restrict in,
164 size_t *lzma_restrict in_pos, size_t in_size);
168 * \brief Gets the number of bytes required to encode vli
170 * \return Number of bytes on success (1-9). If vli isn't valid,
173 extern uint32_t lzma_vli_size(lzma_vli vli)