bidirectional variable-length integers
A bdvarint works similar to a regular varint but with a different format that allows for reading the integer forward and reverse. It can start at the index of the first byte and reading in a forward direction, or start at the index one past the last byte and reading in a reverse direction.
// Encode an integer and return the number of bytes written to data.
// Make sure data has at least 10 bytes available.
int bdvarint_encode_u64(void *data, uint64_t x);
int bdvarint_encode_i64(void *data, int64_t x);
// Decode an integer and return the number of bytes read.
// Returns zero if there not enough data available
// Returns -1 if the data is invalid
int bdvarint_decode_u64(const void *data, size_t len, uint64_t *x);
int bdvarint_decode_u64_reverse(const void *data, size_t len, uint64_t *x);
int bdvarint_decode_i64(const void *data, size_t len, int64_t *x);
int bdvarint_decode_i64_reverse(const void *data, size_t len, int64_t *x);
// Decode an integer and return the number of bytes read.
// WARNING: These are specialized operations that expect that data to be
// well-formed and valid. Any errors are undefined.
int bdvarint_decode_u64_nocheck(const void *data, uint64_t *x);
int bdvarint_decode_u64_reverse_nocheck(const void *data, uint64_t *x);
int bdvarint_decode_i64_nocheck(const void *data, int64_t *x);
int bdvarint_decode_i64_reverse_nocheck(const void *data, int64_t *x);
// Similar to the above functions, but allows for an optional 1 bit flag.
// Do not intermix these with the above functions, their format are slightly
// different.
int bdvarint_encode(void *data, uint64_t x, int flag);
int bdvarint_encode_calc_len(uint64_t x);
int bdvarint_decode(const void *src, size_t len, uint64_t *x, int *flag);
int bdvarint_decode_nocheck(const void *src, uint64_t *x, int *flag);
int bdvarint_decode_rev(const void *src, size_t len, uint64_t *x, int *flag);
int bdvarint_decode_rev_nocheck(const void *src, uint64_t *x, int *flag);Here's a visualization of the byte layout for a bdvarint at various data sizes.
standard (no flag)
with bit flag

