]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/vli.h
Sort of garbage collection commit. :-| Many things are still
[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       Encodes variable-length integer
76  *
77  * In the .lzma format, most integers are encoded in variable-length
78  * representation. This saves space when smaller values are more likely
79  * than bigger values.
80  *
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_VALUE_MAX for clarity.
87  *
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.
93  *
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].
103  *
104  * \return      Slightly different return values are used in multi-call and
105  *              single-call modes.
106  *
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().
113  *
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.
120  */
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,
124                 size_t out_size);
125
126
127 /**
128  * \brief       Decodes variable-length integer
129  *
130  * Like lzma_vli_encode(), this function has single-call and multi-call modes.
131  *
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,
138  *                        set this to NULL.
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].
143  *
144  * \return      Slightly different return values are used in multi-call and
145  *              single-call modes.
146  *
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.
153  *
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.
161  */
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);
165
166
167 /**
168  * \brief       Gets the number of bytes required to encode vli
169  *
170  * \return      Number of bytes on success (1-9). If vli isn't valid,
171  *              zero is returned.
172  */
173 extern uint32_t lzma_vli_size(lzma_vli vli)
174                 lzma_attr_pure;