]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/vli.h
Imported to git.
[icculus/xz.git] / src / liblzma / api / lzma / vli.h
1 /**
2  * \file        lzma/vli.h
3  * \brief       Variable-length integer handling
4  *
5  * \author      Copyright (C) 1999-2006 Igor Pavlov
6  * \author      Copyright (C) 2007 Lasse Collin
7  *
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.
12  *
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.
17  */
18
19 #ifndef LZMA_H_INTERNAL
20 #       error Never include this file directly. Use <lzma.h> instead.
21 #endif
22
23
24 /**
25  * \brief       Maximum supported value of variable-length integer
26  */
27 #define LZMA_VLI_VALUE_MAX (UINT64_MAX / 2)
28
29 /**
30  * \brief       VLI value to denote that the value is unknown
31  */
32 #define LZMA_VLI_VALUE_UNKNOWN UINT64_MAX
33
34 /**
35  * \brief       Maximum supported length of variable length integers
36  */
37 #define LZMA_VLI_BYTES_MAX 9
38
39
40 /**
41  * \brief       VLI constant suffix
42  */
43 #define LZMA_VLI_C(n) UINT64_C(n)
44
45
46 /**
47  * \brief       Variable-length integer type
48  *
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).
53  *
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.
57  */
58 typedef uint64_t lzma_vli;
59
60
61 /**
62  * \brief       Simple macro to validate variable-length integer
63  *
64  * This is useful to test that application has given acceptable values
65  * for example in the uncompressed_size and compressed_size variables.
66  *
67  * \return      True if the integer is representable as VLI or if it
68  *              indicates unknown value.
69  */
70 #define lzma_vli_is_valid(vli) \
71         ((vli) <= LZMA_VLI_VALUE_MAX || (vli) == LZMA_VLI_VALUE_UNKNOWN)
72
73
74 /**
75  * \brief       Sets VLI to given value with error checking
76  *
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.
80  *
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).
84  */
85 #define lzma_vli_set_lim(dest, src, limit) \
86         ((src) > (limit) || ((dest) = (src)) > (limit))
87
88 /**
89  * \brief
90  */
91 #define lzma_vli_add_lim(dest, src, limit) \
92         ((src) > (limit) || ((dest) += (src)) > (limit))
93
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))
97
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))
102
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))
108
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))
112
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))
117
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))
123
124 #define lzma_vli_set(dest, src) lzma_vli_set_lim(dest, src, LZMA_VLI_VALUE_MAX)
125
126 #define lzma_vli_add(dest, src) lzma_vli_add_lim(dest, src, LZMA_VLI_VALUE_MAX)
127
128 #define lzma_vli_add2(dest, src1, src2) \
129         lzma_vli_add2_lim(dest, src1, src2, LZMA_VLI_VALUE_MAX)
130
131 #define lzma_vli_add3(dest, src1, src2, src3) \
132         lzma_vli_add3_lim(dest, src1, src2, src3, LZMA_VLI_VALUE_MAX)
133
134 #define lzma_vli_add4(dest, src1, src2, src3, src4) \
135         lzma_vli_add4_lim(dest, src1, src2, src3, src4, LZMA_VLI_VALUE_MAX)
136
137 #define lzma_vli_sum(dest, src1, src2) \
138         lzma_vli_sum_lim(dest, src1, src2, LZMA_VLI_VALUE_MAX)
139
140 #define lzma_vli_sum3(dest, src1, src2, src3) \
141         lzma_vli_sum3_lim(dest, src1, src2, src3, LZMA_VLI_VALUE_MAX)
142
143 #define lzma_vli_sum4(dest, src1, src2, src3, src4) \
144         lzma_vli_sum4_lim(dest, src1, src2, src3, src4, LZMA_VLI_VALUE_MAX)
145
146
147 /**
148  * \brief       Encodes variable-length integer
149  *
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.
153  *
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 INT64_MAX / 2. You
158  * may use LZMA_VLI_VALUE_MAX for clarity.
159  *
160  * \param       vli       Integer to be encoded
161  * \param       vli_pos   How many bytes have already been written out. This
162  *                        must be less than 9 before calling this function.
163  * \param       vli_size  Minimum size that the variable-length representation
164  *                        must take. This is useful if you want to use
165  *                        variable-length integers as padding. Usually you want
166  *                        to set this to zero. The maximum allowed value is 9.
167  * \param       out       Beginning of the output buffer
168  * \param       out_pos   The next byte will be written to out[*out_pos].
169  * \param       out_size  Size of the out buffer; the first byte into
170  *                        which no data is written to is out[out_size].
171  *
172  * \return      - LZMA_OK: So far all OK, but the integer is not
173  *                completely written out yet.
174  *              - LZMA_STREAM_END: Integer successfully encoded.
175  *              - LZMA_BUF_ERROR: No output space (*out_pos == out_size)
176  *              - LZMA_PROG_ERROR: Arguments are not sane.
177  */
178 extern lzma_ret lzma_vli_encode(
179                 lzma_vli vli, size_t *lzma_restrict vli_pos, size_t vli_size,
180                 uint8_t *lzma_restrict out, size_t *lzma_restrict out_pos,
181                 size_t out_size);
182
183
184 /**
185  * \brief       Decodes variable-length integer
186  *
187  * \param       vli       Pointer to decoded integer. The decoder will
188  *                        initialize it to zero when *vli_pos == 0, so
189  *                        application isn't required to initialize *vli.
190  * \param       vli_pos   How many bytes have already been decoded. When
191  *                        starting to decode a new integer, *vli_pos must
192  *                        be initialized to zero.
193  * \param       in        Beginning of the input buffer
194  * \param       in_pos    The next byte will be read from in[*in_pos].
195  * \param       in_size   Size of the input buffer; the first byte that
196  *                        won't be read is in[in_size].
197  *
198  * \return      - LZMA_OK: So far all OK, but the integer is not
199  *                completely decoded yet.
200  *              - LZMA_STREAM_END: Integer successfully decoded.
201  *              - LZMA_BUF_ERROR: No input data (*in_pos == in_size)
202  *              - LZMA_DATA_ERROR: Integer is longer than nine bytes.
203  *              - LZMA_PROG_ERROR: Arguments are not sane.
204  */
205 extern lzma_ret lzma_vli_decode(lzma_vli *lzma_restrict vli,
206                 size_t *lzma_restrict vli_pos, const uint8_t *lzma_restrict in,
207                 size_t *lzma_restrict in_pos, size_t in_size);
208
209
210 /**
211  * \brief       Decodes variable-length integer reading buffer backwards
212  *
213  * The variable-length integer encoding is designed so that it can be read
214  * either from the beginning to the end, or from the end to the beginning.
215  * This feature is needed to make the Stream parseable backwards;
216  * specifically, to read the Backward Size field in Stream Footer.
217  *
218  * \param       vli       Pointer to variable to hold the decoded integer.
219  * \param       in        Beginning of the input buffer
220  * \param       in_size   Number of bytes available in the in[] buffer.
221  *                        On successful decoding, this is updated to match
222  *                        the number of bytes used. (in[*in_size - 1] is the
223  *                        first byte to process. After successful decoding,
224  *                        in[*in_size] will point to the first byte of the
225  *                        variable-length integer.)
226  *
227  * \return      - LZMA_OK: Decoding successful
228  *              - LZMA_DATA_ERROR: No valid variable-length integer was found.
229  *              - LZMA_BUF_ERROR: Not enough input. Note that in practice,
230  *                this tends to be a sign of broken input, because the
231  *                applications usually do give as much input to this function
232  *                as the applications have available.
233  */
234 extern lzma_ret lzma_vli_reverse_decode(
235                 lzma_vli *vli, const uint8_t *in, size_t *in_size);
236
237
238 /**
239  * \brief       Gets the minimum number of bytes required to encode vli
240  *
241  * \return      Number of bytes on success (1-9). If vli isn't valid,
242  *              zero is returned.
243  */
244 extern size_t lzma_vli_size(lzma_vli vli);