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