Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:19:34

0001 /* SPDX-License-Identifier: 0BSD */
0002 
0003 /**
0004  * \file        lzma/vli.h
0005  * \brief       Variable-length integer handling
0006  * \note        Never include this file directly. Use <lzma.h> instead.
0007  *
0008  * In the .xz format, most integers are encoded in a variable-length
0009  * representation, which is sometimes called little endian base-128 encoding.
0010  * This saves space when smaller values are more likely than bigger values.
0011  *
0012  * The encoding scheme encodes seven bits to every byte, using minimum
0013  * number of bytes required to represent the given value. Encodings that use
0014  * non-minimum number of bytes are invalid, thus every integer has exactly
0015  * one encoded representation. The maximum number of bits in a VLI is 63,
0016  * thus the vli argument must be less than or equal to UINT64_MAX / 2. You
0017  * should use LZMA_VLI_MAX for clarity.
0018  */
0019 
0020 /*
0021  * Author: Lasse Collin
0022  */
0023 
0024 #ifndef LZMA_H_INTERNAL
0025 #   error Never include this file directly. Use <lzma.h> instead.
0026 #endif
0027 
0028 
0029 /**
0030  * \brief       Maximum supported value of a variable-length integer
0031  */
0032 #define LZMA_VLI_MAX (UINT64_MAX / 2)
0033 
0034 /**
0035  * \brief       VLI value to denote that the value is unknown
0036  */
0037 #define LZMA_VLI_UNKNOWN UINT64_MAX
0038 
0039 /**
0040  * \brief       Maximum supported encoded length of variable length integers
0041  */
0042 #define LZMA_VLI_BYTES_MAX 9
0043 
0044 /**
0045  * \brief       VLI constant suffix
0046  */
0047 #define LZMA_VLI_C(n) UINT64_C(n)
0048 
0049 
0050 /**
0051  * \brief       Variable-length integer type
0052  *
0053  * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is
0054  * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the
0055  * underlying integer type.
0056  *
0057  * lzma_vli will be uint64_t for the foreseeable future. If a bigger size
0058  * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will
0059  * not overflow lzma_vli. This simplifies integer overflow detection.
0060  */
0061 typedef uint64_t lzma_vli;
0062 
0063 
0064 /**
0065  * \brief       Validate a variable-length integer
0066  *
0067  * This is useful to test that application has given acceptable values
0068  * for example in the uncompressed_size and compressed_size variables.
0069  *
0070  * \return      True if the integer is representable as a VLI or if it
0071  *              indicates an unknown value. False otherwise.
0072  */
0073 #define lzma_vli_is_valid(vli) \
0074     ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
0075 
0076 
0077 /**
0078  * \brief       Encode a variable-length integer
0079  *
0080  * This function has two modes: single-call and multi-call. Single-call mode
0081  * encodes the whole integer at once; it is an error if the output buffer is
0082  * too small. Multi-call mode saves the position in *vli_pos, and thus it is
0083  * possible to continue encoding if the buffer becomes full before the whole
0084  * integer has been encoded.
0085  *
0086  * \param       vli       Integer to be encoded
0087  * \param[out]  vli_pos   How many VLI-encoded bytes have already been written
0088  *                        out. When starting to encode a new integer in
0089  *                        multi-call mode, *vli_pos must be set to zero.
0090  *                        To use single-call encoding, set vli_pos to NULL.
0091  * \param[out]  out       Beginning of the output buffer
0092  * \param[out]  out_pos   The next byte will be written to out[*out_pos].
0093  * \param       out_size  Size of the out buffer; the first byte into
0094  *                        which no data is written to is out[out_size].
0095  *
0096  * \return      Slightly different return values are used in multi-call and
0097  *              single-call modes.
0098  *
0099  *              Single-call (vli_pos == NULL):
0100  *              - LZMA_OK: Integer successfully encoded.
0101  *              - LZMA_PROG_ERROR: Arguments are not sane. This can be due
0102  *                to too little output space; single-call mode doesn't use
0103  *                LZMA_BUF_ERROR, since the application should have checked
0104  *                the encoded size with lzma_vli_size().
0105  *
0106  *              Multi-call (vli_pos != NULL):
0107  *              - LZMA_OK: So far all OK, but the integer is not
0108  *                completely written out yet.
0109  *              - LZMA_STREAM_END: Integer successfully encoded.
0110  *              - LZMA_BUF_ERROR: No output space was provided.
0111  *              - LZMA_PROG_ERROR: Arguments are not sane.
0112  */
0113 extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos,
0114         uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
0115 
0116 
0117 /**
0118  * \brief       Decode a variable-length integer
0119  *
0120  * Like lzma_vli_encode(), this function has single-call and multi-call modes.
0121  *
0122  * \param[out]  vli       Pointer to decoded integer. The decoder will
0123  *                        initialize it to zero when *vli_pos == 0, so
0124  *                        application isn't required to initialize *vli.
0125  * \param[out]  vli_pos   How many bytes have already been decoded. When
0126  *                        starting to decode a new integer in multi-call
0127  *                        mode, *vli_pos must be initialized to zero. To
0128  *                        use single-call decoding, set vli_pos to NULL.
0129  * \param       in        Beginning of the input buffer
0130  * \param[out]  in_pos    The next byte will be read from in[*in_pos].
0131  * \param       in_size   Size of the input buffer; the first byte that
0132  *                        won't be read is in[in_size].
0133  *
0134  * \return      Slightly different return values are used in multi-call and
0135  *              single-call modes.
0136  *
0137  *              Single-call (vli_pos == NULL):
0138  *              - LZMA_OK: Integer successfully decoded.
0139  *              - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
0140  *                the end of the input buffer before the whole integer was
0141  *                decoded; providing no input at all will use LZMA_DATA_ERROR.
0142  *              - LZMA_PROG_ERROR: Arguments are not sane.
0143  *
0144  *              Multi-call (vli_pos != NULL):
0145  *              - LZMA_OK: So far all OK, but the integer is not
0146  *                completely decoded yet.
0147  *              - LZMA_STREAM_END: Integer successfully decoded.
0148  *              - LZMA_DATA_ERROR: Integer is corrupt.
0149  *              - LZMA_BUF_ERROR: No input was provided.
0150  *              - LZMA_PROG_ERROR: Arguments are not sane.
0151  */
0152 extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,
0153         const uint8_t *in, size_t *in_pos, size_t in_size)
0154         lzma_nothrow;
0155 
0156 
0157 /**
0158  * \brief       Get the number of bytes required to encode a VLI
0159  *
0160  * \param       vli       Integer whose encoded size is to be determined
0161  *
0162  * \return      Number of bytes on success (1-9). If vli isn't valid,
0163  *              zero is returned.
0164  */
0165 extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)
0166         lzma_nothrow lzma_attr_pure;