LEB128(3) Library Functions Manual LEB128(3)

KS_uleb128_decode, KS_leb128_decode, KS_uleb128_encode, KS_leb128_encodelittle endian base 128 utility functions

#include <libks/leb128.h>

size_t
KS_uleb128_decode(const char *buf, size_t buflen, uint64_t *num);

size_t
KS_leb128_decode(const char *buf, size_t buflen, int64_t *num);

ssize_t
KS_uleb128_encode(char *buf, size_t buflen, uint64_t num);

ssize_t
KS_leb128_encode(char *buf, size_t buflen, int64_t num);

The LEB128 API provides functions for encoding and decoding integers expressed in little endian base 128.

The () function decodes an unsigned LEB128 integer by consuming at most buflen number of bytes from buf. The decoded integer is stored in num and will be saturated at UINT64_MAX.

The () function decodes a signed LEB128 integer by consuming at most buflen number of bytes from buf. The decoded integer is stored in num and will be saturated at either INT64_MIN or INT64_MAX.

The () function encodes num as an unsigned LEB128 integer by writing at most buflen number of bytes to buf.

The () function encodes num as an signed LEB128 integer by writing at most buflen number of bytes to buf.

The KS_uleb128_decode() and KS_leb128_decode() functions returns the number of bytes consumed from buf.

The KS_uleb128_encode() and KS_leb128_encode() functions returns the number of written bytes to buf. If the buffer is too short, -1 is returned.

Anton Lindqvist <anton@basename.se>

OpenBSD 7.8 January 22, 2025 LEB128(3)