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.
53 * In future, even if lzma_vli is typdefined to something else than uint64_t,
54 * it is guaranteed that 2 * LZMA_VLI_MAX will not overflow lzma_vli.
55 * This simplifies integer overflow detection.
57 typedef uint64_t lzma_vli;
61 * \brief Simple macro to validate variable-length integer
63 * This is useful to test that application has given acceptable values
64 * for example in the uncompressed_size and compressed_size variables.
66 * \return True if the integer is representable as VLI or if it
67 * indicates unknown value.
69 #define lzma_vli_is_valid(vli) \
70 ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
74 * \brief Encodes variable-length integer
76 * In the .xz format, most integers are encoded in a variable-length
77 * representation, which is sometimes called little endian base-128 encoding.
78 * This saves space when smaller values are more likely than bigger values.
80 * The encoding scheme encodes seven bits to every byte, using minimum
81 * number of bytes required to represent the given value. Encodings that use
82 * non-minimum number of bytes are invalid, thus every integer has exactly
83 * one encoded representation. The maximum number of bits in a VLI is 63,
84 * thus the vli argument must be at maximum of UINT64_MAX / 2. You should
85 * use LZMA_VLI_MAX for clarity.
87 * This function has two modes: single-call and multi-call. Single-call mode
88 * encodes the whole integer at once; it is an error if the output buffer is
89 * too small. Multi-call mode saves the position in *vli_pos, and thus it is
90 * possible to continue encoding if the buffer becomes full before the whole
91 * integer has been encoded.
93 * \param vli Integer to be encoded
94 * \param vli_pos How many VLI-encoded bytes have already been written
95 * out. When starting to encode a new integer, *vli_pos
96 * must be set to zero. To use single-call encoding,
97 * set vli_pos to NULL.
98 * \param out Beginning of the output buffer
99 * \param out_pos The next byte will be written to out[*out_pos].
100 * \param out_size Size of the out buffer; the first byte into
101 * which no data is written to is out[out_size].
103 * \return Slightly different return values are used in multi-call and
106 * Single-call (vli_pos == NULL):
107 * - LZMA_OK: Integer successfully encoded.
108 * - LZMA_PROG_ERROR: Arguments are not sane. This can be due
109 * to too little output space; single-call mode doesn't use
110 * LZMA_BUF_ERROR, since the application should have checked
111 * the encoded size with lzma_vli_size().
113 * Multi-call (vli_pos != NULL):
114 * - LZMA_OK: So far all OK, but the integer is not
115 * completely written out yet.
116 * - LZMA_STREAM_END: Integer successfully encoded.
117 * - LZMA_BUF_ERROR: No output space was provided.
118 * - LZMA_PROG_ERROR: Arguments are not sane.
120 extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli,
121 size_t *lzma_restrict vli_pos, uint8_t *lzma_restrict out,
122 size_t *lzma_restrict out_pos, size_t out_size);
126 * \brief Decodes variable-length integer
128 * Like lzma_vli_encode(), this function has single-call and multi-call modes.
130 * \param vli Pointer to decoded integer. The decoder will
131 * initialize it to zero when *vli_pos == 0, so
132 * application isn't required to initialize *vli.
133 * \param vli_pos How many bytes have already been decoded. When
134 * starting to decode a new integer, *vli_pos must
135 * be initialized to zero. To use single-call decoding,
137 * \param in Beginning of the input buffer
138 * \param in_pos The next byte will be read from in[*in_pos].
139 * \param in_size Size of the input buffer; the first byte that
140 * won't be read is in[in_size].
142 * \return Slightly different return values are used in multi-call and
145 * Single-call (vli_pos == NULL):
146 * - LZMA_OK: Integer successfully decoded.
147 * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
148 * the end of the input buffer before the whole integer was
149 * decoded; providing no input at all will use LZMA_DATA_ERROR.
150 * - LZMA_PROG_ERROR: Arguments are not sane.
152 * Multi-call (vli_pos != NULL):
153 * - LZMA_OK: So far all OK, but the integer is not
154 * completely decoded yet.
155 * - LZMA_STREAM_END: Integer successfully decoded.
156 * - LZMA_DATA_ERROR: Integer is corrupt.
157 * - LZMA_BUF_ERROR: No input was provided.
158 * - LZMA_PROG_ERROR: Arguments are not sane.
160 extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *lzma_restrict vli,
161 size_t *lzma_restrict vli_pos, const uint8_t *lzma_restrict in,
162 size_t *lzma_restrict in_pos, size_t in_size);
166 * \brief Get the number of bytes required to encode a VLI
168 * \return Number of bytes on success (1-9). If vli isn't valid,
171 extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) lzma_attr_pure;