]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/vli.h
6ab1d183abdfca33b2f3ea216252b3d784e69e31
[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_MAX (UINT64_MAX / 2)
28
29 /**
30  * \brief       VLI value to denote that the value is unknown
31  */
32 #define LZMA_VLI_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_MAX]. Unknown value is indicated with LZMA_VLI_UNKNOWN,
51  * which is the maximum value of the underlaying integer type.
52  *
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.
56  */
57 typedef uint64_t lzma_vli;
58
59
60 /**
61  * \brief       Simple macro to validate variable-length integer
62  *
63  * This is useful to test that application has given acceptable values
64  * for example in the uncompressed_size and compressed_size variables.
65  *
66  * \return      True if the integer is representable as VLI or if it
67  *              indicates unknown value.
68  */
69 #define lzma_vli_is_valid(vli) \
70         ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
71
72
73 /**
74  * \brief       Encodes variable-length integer
75  *
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.
79  *
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.
86  *
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.
92  *
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].
102  *
103  * \return      Slightly different return values are used in multi-call and
104  *              single-call modes.
105  *
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().
112  *
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.
119  */
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);
123
124
125 /**
126  * \brief       Decodes variable-length integer
127  *
128  * Like lzma_vli_encode(), this function has single-call and multi-call modes.
129  *
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,
136  *                        set this to NULL.
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].
141  *
142  * \return      Slightly different return values are used in multi-call and
143  *              single-call modes.
144  *
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.
151  *
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.
159  */
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);
163
164
165 /**
166  * \brief       Get the number of bytes required to encode a VLI
167  *
168  * \return      Number of bytes on success (1-9). If vli isn't valid,
169  *              zero is returned.
170  */
171 extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) lzma_attr_pure;