xref: /freebsd/contrib/xz/src/liblzma/api/lzma/vli.h (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1*3b35e7eeSXin LI /* SPDX-License-Identifier: 0BSD */
2*3b35e7eeSXin LI 
381ad8388SMartin Matuska /**
481ad8388SMartin Matuska  * \file        lzma/vli.h
581ad8388SMartin Matuska  * \brief       Variable-length integer handling
6c917796cSXin LI  * \note        Never include this file directly. Use <lzma.h> instead.
781ad8388SMartin Matuska  *
881ad8388SMartin Matuska  * In the .xz format, most integers are encoded in a variable-length
981ad8388SMartin Matuska  * representation, which is sometimes called little endian base-128 encoding.
1081ad8388SMartin Matuska  * This saves space when smaller values are more likely than bigger values.
1181ad8388SMartin Matuska  *
1281ad8388SMartin Matuska  * The encoding scheme encodes seven bits to every byte, using minimum
1381ad8388SMartin Matuska  * number of bytes required to represent the given value. Encodings that use
1481ad8388SMartin Matuska  * non-minimum number of bytes are invalid, thus every integer has exactly
1581ad8388SMartin Matuska  * one encoded representation. The maximum number of bits in a VLI is 63,
1681ad8388SMartin Matuska  * thus the vli argument must be less than or equal to UINT64_MAX / 2. You
1781ad8388SMartin Matuska  * should use LZMA_VLI_MAX for clarity.
1881ad8388SMartin Matuska  */
1981ad8388SMartin Matuska 
2081ad8388SMartin Matuska /*
2181ad8388SMartin Matuska  * Author: Lasse Collin
2281ad8388SMartin Matuska  */
2381ad8388SMartin Matuska 
2481ad8388SMartin Matuska #ifndef LZMA_H_INTERNAL
2581ad8388SMartin Matuska #	error Never include this file directly. Use <lzma.h> instead.
2681ad8388SMartin Matuska #endif
2781ad8388SMartin Matuska 
2881ad8388SMartin Matuska 
2981ad8388SMartin Matuska /**
30542aef48SMartin Matuska  * \brief       Maximum supported value of a variable-length integer
3181ad8388SMartin Matuska  */
3281ad8388SMartin Matuska #define LZMA_VLI_MAX (UINT64_MAX / 2)
3381ad8388SMartin Matuska 
3481ad8388SMartin Matuska /**
3581ad8388SMartin Matuska  * \brief       VLI value to denote that the value is unknown
3681ad8388SMartin Matuska  */
3781ad8388SMartin Matuska #define LZMA_VLI_UNKNOWN UINT64_MAX
3881ad8388SMartin Matuska 
3981ad8388SMartin Matuska /**
40542aef48SMartin Matuska  * \brief       Maximum supported encoded length of variable length integers
4181ad8388SMartin Matuska  */
4281ad8388SMartin Matuska #define LZMA_VLI_BYTES_MAX 9
4381ad8388SMartin Matuska 
4481ad8388SMartin Matuska /**
4581ad8388SMartin Matuska  * \brief       VLI constant suffix
4681ad8388SMartin Matuska  */
4781ad8388SMartin Matuska #define LZMA_VLI_C(n) UINT64_C(n)
4881ad8388SMartin Matuska 
4981ad8388SMartin Matuska 
5081ad8388SMartin Matuska /**
5181ad8388SMartin Matuska  * \brief       Variable-length integer type
5281ad8388SMartin Matuska  *
53542aef48SMartin Matuska  * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is
54542aef48SMartin Matuska  * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the
55a8675d92SXin LI  * underlying integer type.
5681ad8388SMartin Matuska  *
57542aef48SMartin Matuska  * lzma_vli will be uint64_t for the foreseeable future. If a bigger size
58542aef48SMartin Matuska  * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will
59542aef48SMartin Matuska  * not overflow lzma_vli. This simplifies integer overflow detection.
6081ad8388SMartin Matuska  */
6181ad8388SMartin Matuska typedef uint64_t lzma_vli;
6281ad8388SMartin Matuska 
6381ad8388SMartin Matuska 
6481ad8388SMartin Matuska /**
65542aef48SMartin Matuska  * \brief       Validate a variable-length integer
6681ad8388SMartin Matuska  *
6781ad8388SMartin Matuska  * This is useful to test that application has given acceptable values
6881ad8388SMartin Matuska  * for example in the uncompressed_size and compressed_size variables.
6981ad8388SMartin Matuska  *
70*3b35e7eeSXin LI  * \return      True if the integer is representable as a VLI or if it
71*3b35e7eeSXin LI  *              indicates an unknown value. False otherwise.
7281ad8388SMartin Matuska  */
7381ad8388SMartin Matuska #define lzma_vli_is_valid(vli) \
7481ad8388SMartin Matuska 	((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
7581ad8388SMartin Matuska 
7681ad8388SMartin Matuska 
7781ad8388SMartin Matuska /**
7881ad8388SMartin Matuska  * \brief       Encode a variable-length integer
7981ad8388SMartin Matuska  *
8081ad8388SMartin Matuska  * This function has two modes: single-call and multi-call. Single-call mode
8181ad8388SMartin Matuska  * encodes the whole integer at once; it is an error if the output buffer is
8281ad8388SMartin Matuska  * too small. Multi-call mode saves the position in *vli_pos, and thus it is
8381ad8388SMartin Matuska  * possible to continue encoding if the buffer becomes full before the whole
8481ad8388SMartin Matuska  * integer has been encoded.
8581ad8388SMartin Matuska  *
8681ad8388SMartin Matuska  * \param       vli       Integer to be encoded
87c917796cSXin LI  * \param[out]  vli_pos   How many VLI-encoded bytes have already been written
88542aef48SMartin Matuska  *                        out. When starting to encode a new integer in
89542aef48SMartin Matuska  *                        multi-call mode, *vli_pos must be set to zero.
90542aef48SMartin Matuska  *                        To use single-call encoding, set vli_pos to NULL.
91c917796cSXin LI  * \param[out]  out       Beginning of the output buffer
92c917796cSXin LI  * \param[out]  out_pos   The next byte will be written to out[*out_pos].
9381ad8388SMartin Matuska  * \param       out_size  Size of the out buffer; the first byte into
9481ad8388SMartin Matuska  *                        which no data is written to is out[out_size].
9581ad8388SMartin Matuska  *
9681ad8388SMartin Matuska  * \return      Slightly different return values are used in multi-call and
9781ad8388SMartin Matuska  *              single-call modes.
9881ad8388SMartin Matuska  *
9981ad8388SMartin Matuska  *              Single-call (vli_pos == NULL):
10081ad8388SMartin Matuska  *              - LZMA_OK: Integer successfully encoded.
10181ad8388SMartin Matuska  *              - LZMA_PROG_ERROR: Arguments are not sane. This can be due
10281ad8388SMartin Matuska  *                to too little output space; single-call mode doesn't use
10381ad8388SMartin Matuska  *                LZMA_BUF_ERROR, since the application should have checked
10481ad8388SMartin Matuska  *                the encoded size with lzma_vli_size().
10581ad8388SMartin Matuska  *
10681ad8388SMartin Matuska  *              Multi-call (vli_pos != NULL):
10781ad8388SMartin Matuska  *              - LZMA_OK: So far all OK, but the integer is not
10881ad8388SMartin Matuska  *                completely written out yet.
10981ad8388SMartin Matuska  *              - LZMA_STREAM_END: Integer successfully encoded.
11081ad8388SMartin Matuska  *              - LZMA_BUF_ERROR: No output space was provided.
11181ad8388SMartin Matuska  *              - LZMA_PROG_ERROR: Arguments are not sane.
11281ad8388SMartin Matuska  */
113e0f0e66dSMartin Matuska extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos,
114e0f0e66dSMartin Matuska 		uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
11581ad8388SMartin Matuska 
11681ad8388SMartin Matuska 
11781ad8388SMartin Matuska /**
11881ad8388SMartin Matuska  * \brief       Decode a variable-length integer
11981ad8388SMartin Matuska  *
12081ad8388SMartin Matuska  * Like lzma_vli_encode(), this function has single-call and multi-call modes.
12181ad8388SMartin Matuska  *
122c917796cSXin LI  * \param[out]  vli       Pointer to decoded integer. The decoder will
12381ad8388SMartin Matuska  *                        initialize it to zero when *vli_pos == 0, so
12481ad8388SMartin Matuska  *                        application isn't required to initialize *vli.
125c917796cSXin LI  * \param[out]  vli_pos   How many bytes have already been decoded. When
126542aef48SMartin Matuska  *                        starting to decode a new integer in multi-call
127542aef48SMartin Matuska  *                        mode, *vli_pos must be initialized to zero. To
128542aef48SMartin Matuska  *                        use single-call decoding, set vli_pos to NULL.
12981ad8388SMartin Matuska  * \param       in        Beginning of the input buffer
130c917796cSXin LI  * \param[out]  in_pos    The next byte will be read from in[*in_pos].
13181ad8388SMartin Matuska  * \param       in_size   Size of the input buffer; the first byte that
13281ad8388SMartin Matuska  *                        won't be read is in[in_size].
13381ad8388SMartin Matuska  *
13481ad8388SMartin Matuska  * \return      Slightly different return values are used in multi-call and
13581ad8388SMartin Matuska  *              single-call modes.
13681ad8388SMartin Matuska  *
13781ad8388SMartin Matuska  *              Single-call (vli_pos == NULL):
13881ad8388SMartin Matuska  *              - LZMA_OK: Integer successfully decoded.
13981ad8388SMartin Matuska  *              - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
14081ad8388SMartin Matuska  *                the end of the input buffer before the whole integer was
14181ad8388SMartin Matuska  *                decoded; providing no input at all will use LZMA_DATA_ERROR.
14281ad8388SMartin Matuska  *              - LZMA_PROG_ERROR: Arguments are not sane.
14381ad8388SMartin Matuska  *
14481ad8388SMartin Matuska  *              Multi-call (vli_pos != NULL):
14581ad8388SMartin Matuska  *              - LZMA_OK: So far all OK, but the integer is not
14681ad8388SMartin Matuska  *                completely decoded yet.
14781ad8388SMartin Matuska  *              - LZMA_STREAM_END: Integer successfully decoded.
14881ad8388SMartin Matuska  *              - LZMA_DATA_ERROR: Integer is corrupt.
14981ad8388SMartin Matuska  *              - LZMA_BUF_ERROR: No input was provided.
15081ad8388SMartin Matuska  *              - LZMA_PROG_ERROR: Arguments are not sane.
15181ad8388SMartin Matuska  */
152e0f0e66dSMartin Matuska extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,
153e0f0e66dSMartin Matuska 		const uint8_t *in, size_t *in_pos, size_t in_size)
154e0f0e66dSMartin Matuska 		lzma_nothrow;
15581ad8388SMartin Matuska 
15681ad8388SMartin Matuska 
15781ad8388SMartin Matuska /**
15881ad8388SMartin Matuska  * \brief       Get the number of bytes required to encode a VLI
15981ad8388SMartin Matuska  *
1609e6bbe47SXin LI  * \param       vli       Integer whose encoded size is to be determined
1619e6bbe47SXin LI  *
16281ad8388SMartin Matuska  * \return      Number of bytes on success (1-9). If vli isn't valid,
16381ad8388SMartin Matuska  *              zero is returned.
16481ad8388SMartin Matuska  */
16581ad8388SMartin Matuska extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)
16681ad8388SMartin Matuska 		lzma_nothrow lzma_attr_pure;
167