liblzma (XZ Utils) 5.4.3
Macros | Typedefs | Functions
vli.h File Reference

Variable-length integer handling. More...

Macros

#define LZMA_VLI_MAX   (UINT64_MAX / 2)
 Maximum supported value of a variable-length integer.
 
#define LZMA_VLI_UNKNOWN   UINT64_MAX
 VLI value to denote that the value is unknown.
 
#define LZMA_VLI_BYTES_MAX   9
 Maximum supported encoded length of variable length integers.
 
#define LZMA_VLI_C(n)   UINT64_C(n)
 VLI constant suffix.
 
#define lzma_vli_is_valid(vli)    ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
 Validate a variable-length integer.
 

Typedefs

typedef uint64_t lzma_vli
 Variable-length integer type.
 

Functions

lzma_ret lzma_vli_encode (lzma_vli vli, size_t *vli_pos, uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow
 Encode a variable-length integer.
 
lzma_ret lzma_vli_decode (lzma_vli *vli, size_t *vli_pos, const uint8_t *in, size_t *in_pos, size_t in_size) lzma_nothrow
 Decode a variable-length integer.
 
uint32_t lzma_vli_size (lzma_vli vli) lzma_nothrow lzma_attr_pure
 Get the number of bytes required to encode a VLI.
 

Detailed Description

Variable-length integer handling.

Note
Never include this file directly. Use <lzma.h> instead.

In the .xz format, most integers are encoded in a variable-length representation, which is sometimes called little endian base-128 encoding. This saves space when smaller values are more likely than bigger values.

The encoding scheme encodes seven bits to every byte, using minimum number of bytes required to represent the given value. Encodings that use non-minimum number of bytes are invalid, thus every integer has exactly one encoded representation. The maximum number of bits in a VLI is 63, thus the vli argument must be less than or equal to UINT64_MAX / 2. You should use LZMA_VLI_MAX for clarity.

Macro Definition Documentation

◆ lzma_vli_is_valid

#define lzma_vli_is_valid (   vli)     ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)

Validate a variable-length integer.

This is useful to test that application has given acceptable values for example in the uncompressed_size and compressed_size variables.

Returns
True if the integer is representable as VLI or if it indicates unknown value. False if the integer cannot be represented as VLI.

Typedef Documentation

◆ lzma_vli

typedef uint64_t lzma_vli

Variable-length integer type.

Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the underlying integer type.

lzma_vli will be uint64_t for the foreseeable future. If a bigger size is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will not overflow lzma_vli. This simplifies integer overflow detection.

Function Documentation

◆ lzma_vli_encode()

lzma_ret lzma_vli_encode ( lzma_vli  vli,
size_t *  vli_pos,
uint8_t *  out,
size_t *  out_pos,
size_t  out_size 
)

Encode a variable-length integer.

This function has two modes: single-call and multi-call. Single-call mode encodes the whole integer at once; it is an error if the output buffer is too small. Multi-call mode saves the position in *vli_pos, and thus it is possible to continue encoding if the buffer becomes full before the whole integer has been encoded.

Parameters
vliInteger to be encoded
[out]vli_posHow many VLI-encoded bytes have already been written out. When starting to encode a new integer in multi-call mode, *vli_pos must be set to zero. To use single-call encoding, set vli_pos to NULL.
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos].
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Slightly different return values are used in multi-call and single-call modes.

Single-call (vli_pos == NULL):

  • LZMA_OK: Integer successfully encoded.
  • LZMA_PROG_ERROR: Arguments are not sane. This can be due to too little output space; single-call mode doesn't use LZMA_BUF_ERROR, since the application should have checked the encoded size with lzma_vli_size().

Multi-call (vli_pos != NULL):

  • LZMA_OK: So far all OK, but the integer is not completely written out yet.
  • LZMA_STREAM_END: Integer successfully encoded.
  • LZMA_BUF_ERROR: No output space was provided.
  • LZMA_PROG_ERROR: Arguments are not sane.

◆ lzma_vli_decode()

lzma_ret lzma_vli_decode ( lzma_vli vli,
size_t *  vli_pos,
const uint8_t *  in,
size_t *  in_pos,
size_t  in_size 
)

Decode a variable-length integer.

Like lzma_vli_encode(), this function has single-call and multi-call modes.

Parameters
[out]vliPointer to decoded integer. The decoder will initialize it to zero when *vli_pos == 0, so application isn't required to initialize *vli.
[out]vli_posHow many bytes have already been decoded. When starting to decode a new integer in multi-call mode, *vli_pos must be initialized to zero. To use single-call decoding, set vli_pos to NULL.
inBeginning of the input buffer
[out]in_posThe next byte will be read from in[*in_pos].
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
Slightly different return values are used in multi-call and single-call modes.

Single-call (vli_pos == NULL):

  • LZMA_OK: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt. This includes hitting the end of the input buffer before the whole integer was decoded; providing no input at all will use LZMA_DATA_ERROR.
  • LZMA_PROG_ERROR: Arguments are not sane.

Multi-call (vli_pos != NULL):

  • LZMA_OK: So far all OK, but the integer is not completely decoded yet.
  • LZMA_STREAM_END: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt.
  • LZMA_BUF_ERROR: No input was provided.
  • LZMA_PROG_ERROR: Arguments are not sane.

◆ lzma_vli_size()

uint32_t lzma_vli_size ( lzma_vli  vli)

Get the number of bytes required to encode a VLI.

Parameters
vliInteger whose encoded size is to be determined
Returns
Number of bytes on success (1-9). If vli isn't valid, zero is returned.