varints
Variable-length integer (VLQ) encoding and decoding in C99
Macros | Functions
varints.h File Reference

Variable-length integer (VLQ) encoding and decoding. More...

Go to the source code of this file.

Macros

#define VARINTS_MAX_ENCODED_LEN   (10u)
 Maximum size of an encoded 64-bit VLQ/varint value.
 

Functions

int varint_encode_u64 (uint64_t input, uint8_t *output, int *bytes_generated)
 
int varint_decode_u64 (uint8_t *input, uint64_t *output, int *bytes_consumed)
 
int varint_encode_i64 (int64_t input, uint8_t *output, int *bytes_generated)
 
int varint_decode_i64 (uint8_t *input, int64_t *output, int *bytes_consumed)
 

Detailed Description

Variable-length integer (VLQ) encoding and decoding.

Author
Erik K. Nyquist

Function Documentation

◆ varint_decode_i64()

int varint_decode_i64 ( uint8_t *  input,
int64_t *  output,
int *  bytes_consumed 
)

Decode a VLQ/varint byte stream into a signed 64-bit integer.

Parameters
inputPointer to VLQ/varint byte stream to decode.
outputPointer to location to store decoded integer value.
bytes_consumedPointer to location to store number of input bytes consumed before the end of the VLQ/varint value was found, may be NULL.
Returns
0 if successful, 1 if the end of the VLQ/varint byte stream was not found after VARINTS_MAX_ENCODED_LEN bytes, and -1 if a NULL pointer was provided for 'input' or 'output'.

◆ varint_decode_u64()

int varint_decode_u64 ( uint8_t *  input,
uint64_t *  output,
int *  bytes_consumed 
)

Decode a VLQ/varint byte stream into an unsigned 64-bit integer.

Parameters
inputPointer to VLQ/varint byte stream to decode.
outputPointer to location to store decoded integer value.
bytes_consumedPointer to location to store number of input bytes consumed before the end of the VLQ/varint value was found, may be NULL.
Returns
0 if successful, 1 if the end of the VLQ/varint byte stream was not found after VARINTS_MAX_ENCODED_LEN bytes, and -1 if a NULL pointer was provided for 'input' or 'output'.

◆ varint_encode_i64()

int varint_encode_i64 ( int64_t  input,
uint8_t *  output,
int *  bytes_generated 
)

Encode a signed 64-bit integer into a VLQ/varint byte stream.

Parameters
inputInput value to encode.
outputPointer to location to store encoded output bytes. At least VARINTS_MAX_ENCODED_LEN bytes are expected to be available.
bytes_generatedPointer to location to store number of output bytes generated, may be NULL.
Returns
0 if successful, and -1 if a NULL pointer was provided for 'output'.

◆ varint_encode_u64()

int varint_encode_u64 ( uint64_t  input,
uint8_t *  output,
int *  bytes_generated 
)

Encode an unsigned 64-bit integer into a VLQ/varint byte stream.

Parameters
inputInput value to encode.
outputPointer to location to store encoded output bytes. At least VARINTS_MAX_ENCODED_LEN bytes are expected to be available.
bytes_generatedPointer to location to store number of output bytes generated, may be NULL.
Returns
0 if successful, and -1 if a NULL pointer was provided for 'output'.