11553Srgrimes/**
21553Srgrimes * \file        lzma/vli.h
31553Srgrimes * \brief       Variable-length integer handling
41553Srgrimes *
51553Srgrimes * In the .xz format, most integers are encoded in a variable-length
61553Srgrimes * representation, which is sometimes called little endian base-128 encoding.
71553Srgrimes * This saves space when smaller values are more likely than bigger values.
81553Srgrimes *
91553Srgrimes * The encoding scheme encodes seven bits to every byte, using minimum
101553Srgrimes * number of bytes required to represent the given value. Encodings that use
111553Srgrimes * non-minimum number of bytes are invalid, thus every integer has exactly
121553Srgrimes * one encoded representation. The maximum number of bits in a VLI is 63,
131553Srgrimes * thus the vli argument must be less than or equal to UINT64_MAX / 2. You
141553Srgrimes * should use LZMA_VLI_MAX for clarity.
151553Srgrimes */
161553Srgrimes
171553Srgrimes/*
181553Srgrimes * Author: Lasse Collin
191553Srgrimes *
201553Srgrimes * This file has been put into the public domain.
211553Srgrimes * You can do whatever you want with this file.
221553Srgrimes *
231553Srgrimes * See ../lzma.h for information about liblzma as a whole.
241553Srgrimes */
251553Srgrimes
261553Srgrimes#ifndef LZMA_H_INTERNAL
271553Srgrimes#	error Never include this file directly. Use <lzma.h> instead.
281553Srgrimes#endif
291553Srgrimes
301553Srgrimes
311553Srgrimes/**
321553Srgrimes * \brief       Maximum supported value of a variable-length integer
331553Srgrimes */
341553Srgrimes#define LZMA_VLI_MAX (UINT64_MAX / 2)
3531492Swollman
3615648Sjoerg/**
3731492Swollman * \brief       VLI value to denote that the value is unknown
3831492Swollman */
3935998Sjb#define LZMA_VLI_UNKNOWN UINT64_MAX
401553Srgrimes
411553Srgrimes/**
421553Srgrimes * \brief       Maximum supported encoded length of variable length integers
431553Srgrimes */
441553Srgrimes#define LZMA_VLI_BYTES_MAX 9
4531492Swollman
4631492Swollman/**
4731492Swollman * \brief       VLI constant suffix
4831492Swollman */
491553Srgrimes#define LZMA_VLI_C(n) UINT64_C(n)
501553Srgrimes
511553Srgrimes
521553Srgrimes/**
5331492Swollman * \brief       Variable-length integer type
5431492Swollman *
5531492Swollman * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is
5631492Swollman * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the
5731492Swollman * underlaying integer type.
5831492Swollman *
591553Srgrimes * lzma_vli will be uint64_t for the foreseeable future. If a bigger size
601553Srgrimes * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will
611553Srgrimes * not overflow lzma_vli. This simplifies integer overflow detection.
621553Srgrimes */
631553Srgrimestypedef uint64_t lzma_vli;
641553Srgrimes
651553Srgrimes
661553Srgrimes/**
671553Srgrimes * \brief       Validate a variable-length integer
681553Srgrimes *
691553Srgrimes * This is useful to test that application has given acceptable values
701553Srgrimes * for example in the uncompressed_size and compressed_size variables.
711553Srgrimes *
721553Srgrimes * \return      True if the integer is representable as VLI or if it
7327618Simp *              indicates unknown value.
7427618Simp */
751553Srgrimes#define lzma_vli_is_valid(vli) \
761553Srgrimes	((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
771553Srgrimes
781553Srgrimes
791553Srgrimes/**
801553Srgrimes * \brief       Encode a variable-length integer
811553Srgrimes *
821553Srgrimes * This function has two modes: single-call and multi-call. Single-call mode
831553Srgrimes * encodes the whole integer at once; it is an error if the output buffer is
841553Srgrimes * too small. Multi-call mode saves the position in *vli_pos, and thus it is
851553Srgrimes * possible to continue encoding if the buffer becomes full before the whole
861553Srgrimes * integer has been encoded.
8726844Sjoerg *
8831492Swollman * \param       vli       Integer to be encoded
8926844Sjoerg * \param       vli_pos   How many VLI-encoded bytes have already been written
901553Srgrimes *                        out. When starting to encode a new integer in
911553Srgrimes *                        multi-call mode, *vli_pos must be set to zero.
921553Srgrimes *                        To use single-call encoding, set vli_pos to NULL.
931553Srgrimes * \param       out       Beginning of the output buffer
9431492Swollman * \param       out_pos   The next byte will be written to out[*out_pos].
9531492Swollman * \param       out_size  Size of the out buffer; the first byte into
961553Srgrimes *                        which no data is written to is out[out_size].
971553Srgrimes *
981553Srgrimes * \return      Slightly different return values are used in multi-call and
9927618Simp *              single-call modes.
1001553Srgrimes *
1011553Srgrimes *              Single-call (vli_pos == NULL):
1021553Srgrimes *              - LZMA_OK: Integer successfully encoded.
1031553Srgrimes *              - LZMA_PROG_ERROR: Arguments are not sane. This can be due
10426844Sjoerg *                to too little output space; single-call mode doesn't use
1051553Srgrimes *                LZMA_BUF_ERROR, since the application should have checked
1061553Srgrimes *                the encoded size with lzma_vli_size().
1071553Srgrimes *
1081553Srgrimes *              Multi-call (vli_pos != NULL):
10931492Swollman *              - LZMA_OK: So far all OK, but the integer is not
11031492Swollman *                completely written out yet.
1111553Srgrimes *              - LZMA_STREAM_END: Integer successfully encoded.
11231492Swollman *              - LZMA_BUF_ERROR: No output space was provided.
11331492Swollman *              - LZMA_PROG_ERROR: Arguments are not sane.
1141553Srgrimes */
1151553Srgrimesextern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos,
1161553Srgrimes		uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
1171553Srgrimes
1181553Srgrimes
11927618Simp/**
12031492Swollman * \brief       Decode a variable-length integer
12131492Swollman *
12231492Swollman * Like lzma_vli_encode(), this function has single-call and multi-call modes.
12327618Simp *
12431492Swollman * \param       vli       Pointer to decoded integer. The decoder will
12531492Swollman *                        initialize it to zero when *vli_pos == 0, so
12627618Simp *                        application isn't required to initialize *vli.
12731492Swollman * \param       vli_pos   How many bytes have already been decoded. When
12827618Simp *                        starting to decode a new integer in multi-call
12927618Simp *                        mode, *vli_pos must be initialized to zero. To
13031492Swollman *                        use single-call decoding, set vli_pos to NULL.
13131492Swollman * \param       in        Beginning of the input buffer
1321553Srgrimes * \param       in_pos    The next byte will be read from in[*in_pos].
13331492Swollman * \param       in_size   Size of the input buffer; the first byte that
13427618Simp *                        won't be read is in[in_size].
13531492Swollman *
13627618Simp * \return      Slightly different return values are used in multi-call and
1371553Srgrimes *              single-call modes.
1381553Srgrimes *
1391553Srgrimes *              Single-call (vli_pos == NULL):
1401553Srgrimes *              - LZMA_OK: Integer successfully decoded.
1411553Srgrimes *              - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
1421553Srgrimes *                the end of the input buffer before the whole integer was
1431553Srgrimes *                decoded; providing no input at all will use LZMA_DATA_ERROR.
14431492Swollman *              - LZMA_PROG_ERROR: Arguments are not sane.
14531492Swollman *
1461553Srgrimes *              Multi-call (vli_pos != NULL):
14731492Swollman *              - LZMA_OK: So far all OK, but the integer is not
14831492Swollman *                completely decoded yet.
1491553Srgrimes *              - LZMA_STREAM_END: Integer successfully decoded.
1501553Srgrimes *              - LZMA_DATA_ERROR: Integer is corrupt.
1511553Srgrimes *              - LZMA_BUF_ERROR: No input was provided.
1521553Srgrimes *              - LZMA_PROG_ERROR: Arguments are not sane.
15327618Simp */
15431492Swollmanextern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,
15527618Simp		const uint8_t *in, size_t *in_pos, size_t in_size)
1561553Srgrimes		lzma_nothrow;
15731492Swollman
1581553Srgrimes
1591553Srgrimes/**
1601553Srgrimes * \brief       Get the number of bytes required to encode a VLI
16115648Sjoerg *
16215648Sjoerg * \return      Number of bytes on success (1-9). If vli isn't valid,
1631553Srgrimes *              zero is returned.
1641553Srgrimes */
16527618Simpextern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)
16627618Simp		lzma_nothrow lzma_attr_pure;
16727618Simp