]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/liblzma/api/lzma/vli.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / src / liblzma / api / lzma / vli.h
1 /**
2  * \file        lzma/vli.h
3  * \brief       Variable-length integer handling
4  *
5  * In the .xz format, most integers are encoded in a variable-length
6  * representation, which is sometimes called little endian base-128 encoding.
7  * This saves space when smaller values are more likely than bigger values.
8  *
9  * The encoding scheme encodes seven bits to every byte, using minimum
10  * number of bytes required to represent the given value. Encodings that use
11  * non-minimum number of bytes are invalid, thus every integer has exactly
12  * one encoded representation. The maximum number of bits in a VLI is 63,
13  * thus the vli argument must be less than or equal to UINT64_MAX / 2. You
14  * should use LZMA_VLI_MAX for clarity.
15  */
16
17 /*
18  * Author: Lasse Collin
19  *
20  * This file has been put into the public domain.
21  * You can do whatever you want with this file.
22  *
23  * See ../lzma.h for information about liblzma as a whole.
24  */
25
26 #ifndef LZMA_H_INTERNAL
27 #       error Never include this file directly. Use <lzma.h> instead.
28 #endif
29
30
31 /**
32  * \brief       Maximum supported value of variable-length integer
33  */
34 #define LZMA_VLI_MAX (UINT64_MAX / 2)
35
36 /**
37  * \brief       VLI value to denote that the value is unknown
38  */
39 #define LZMA_VLI_UNKNOWN UINT64_MAX
40
41 /**
42  * \brief       Maximum supported length of variable length integers
43  */
44 #define LZMA_VLI_BYTES_MAX 9
45
46
47 /**
48  * \brief       VLI constant suffix
49  */
50 #define LZMA_VLI_C(n) UINT64_C(n)
51
52
53 /**
54  * \brief       Variable-length integer type
55  *
56  * This will always be unsigned integer. Valid VLI values are in the range
57  * [0, LZMA_VLI_MAX]. Unknown value is indicated with LZMA_VLI_UNKNOWN,
58  * which is the maximum value of the underlaying integer type.
59  *
60  * In future, even if lzma_vli is defined to be something other than uint64_t,
61  * it is guaranteed that 2 * LZMA_VLI_MAX will not overflow lzma_vli.
62  * This simplifies integer overflow detection.
63  */
64 typedef uint64_t lzma_vli;
65
66
67 /**
68  * \brief       Simple macro to validate variable-length integer
69  *
70  * This is useful to test that application has given acceptable values
71  * for example in the uncompressed_size and compressed_size variables.
72  *
73  * \return      True if the integer is representable as VLI or if it
74  *              indicates unknown value.
75  */
76 #define lzma_vli_is_valid(vli) \
77         ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
78
79
80 /**
81  * \brief       Encode a variable-length integer
82  *
83  * This function has two modes: single-call and multi-call. Single-call mode
84  * encodes the whole integer at once; it is an error if the output buffer is
85  * too small. Multi-call mode saves the position in *vli_pos, and thus it is
86  * possible to continue encoding if the buffer becomes full before the whole
87  * integer has been encoded.
88  *
89  * \param       vli       Integer to be encoded
90  * \param       vli_pos   How many VLI-encoded bytes have already been written
91  *                        out. When starting to encode a new integer, *vli_pos
92  *                        must be set to zero. To use single-call encoding,
93  *                        set vli_pos to NULL.
94  * \param       out       Beginning of the output buffer
95  * \param       out_pos   The next byte will be written to out[*out_pos].
96  * \param       out_size  Size of the out buffer; the first byte into
97  *                        which no data is written to is out[out_size].
98  *
99  * \return      Slightly different return values are used in multi-call and
100  *              single-call modes.
101  *
102  *              Single-call (vli_pos == NULL):
103  *              - LZMA_OK: Integer successfully encoded.
104  *              - LZMA_PROG_ERROR: Arguments are not sane. This can be due
105  *                to too little output space; single-call mode doesn't use
106  *                LZMA_BUF_ERROR, since the application should have checked
107  *                the encoded size with lzma_vli_size().
108  *
109  *              Multi-call (vli_pos != NULL):
110  *              - LZMA_OK: So far all OK, but the integer is not
111  *                completely written out yet.
112  *              - LZMA_STREAM_END: Integer successfully encoded.
113  *              - LZMA_BUF_ERROR: No output space was provided.
114  *              - LZMA_PROG_ERROR: Arguments are not sane.
115  */
116 extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli,
117                 size_t *vli_pos, uint8_t *lzma_restrict out,
118                 size_t *lzma_restrict out_pos, size_t out_size) lzma_nothrow;
119
120
121 /**
122  * \brief       Decode a variable-length integer
123  *
124  * Like lzma_vli_encode(), this function has single-call and multi-call modes.
125  *
126  * \param       vli       Pointer to decoded integer. The decoder will
127  *                        initialize it to zero when *vli_pos == 0, so
128  *                        application isn't required to initialize *vli.
129  * \param       vli_pos   How many bytes have already been decoded. When
130  *                        starting to decode a new integer, *vli_pos must
131  *                        be initialized to zero. To use single-call decoding,
132  *                        set this to NULL.
133  * \param       in        Beginning of the input buffer
134  * \param       in_pos    The next byte will be read from in[*in_pos].
135  * \param       in_size   Size of the input buffer; the first byte that
136  *                        won't be read is in[in_size].
137  *
138  * \return      Slightly different return values are used in multi-call and
139  *              single-call modes.
140  *
141  *              Single-call (vli_pos == NULL):
142  *              - LZMA_OK: Integer successfully decoded.
143  *              - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
144  *                the end of the input buffer before the whole integer was
145  *                decoded; providing no input at all will use LZMA_DATA_ERROR.
146  *              - LZMA_PROG_ERROR: Arguments are not sane.
147  *
148  *              Multi-call (vli_pos != NULL):
149  *              - LZMA_OK: So far all OK, but the integer is not
150  *                completely decoded yet.
151  *              - LZMA_STREAM_END: Integer successfully decoded.
152  *              - LZMA_DATA_ERROR: Integer is corrupt.
153  *              - LZMA_BUF_ERROR: No input was provided.
154  *              - LZMA_PROG_ERROR: Arguments are not sane.
155  */
156 extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *lzma_restrict vli,
157                 size_t *vli_pos, const uint8_t *lzma_restrict in,
158                 size_t *lzma_restrict in_pos, size_t in_size) lzma_nothrow;
159
160
161 /**
162  * \brief       Get the number of bytes required to encode a VLI
163  *
164  * \return      Number of bytes on success (1-9). If vli isn't valid,
165  *              zero is returned.
166  */
167 extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)
168                 lzma_nothrow lzma_attr_pure;