MTBL_VARINT(3)
π MTBL_VARINT(3) π
okay bestie, so this is MTBL_VARINT(3) from the mtbl crew, and it's literally just vibes about squishing 32 and 64 bit integers into as few bytes as possible. variable-width encoding, no cap. π¦
β¨ NAME β¨
mtbl_varint β variable-width encoding and decoding of 32 and 64 bit integers
πΈ SYNOPSIS πΈ
#include <mtbl.h>
unsigned mtbl_varint_length(uint64_t value);
unsigned mtbl_varint_length_packed(const uint8_t *buf, size_t len_buf);
size_t mtbl_varint_encode32(uint8_t *ptr, uint32_t value);
size_t mtbl_varint_encode64(uint8_t *ptr, uint64_t value);
size_t mtbl_varint_decode32(const uint8_t *ptr, uint32_t *value);
size_t mtbl_varint_decode64(const uint8_t *ptr, uint64_t *value);
π DESCRIPTION π
so like, mtbl_varint_encode32() and mtbl_varint_encode64() take the 32 or 64 bit quantity (respectively) in the argument value and write it out to the buffer in the argument dst. and the way it writes it? variable-width encoding, bestie. it uses at most 5 bytes for a 32 bit quantity, or at most 10 bytes for a 64 bit quantity. tiny when it can be, big when it has to be. it's giving efficiency. π
then mtbl_varint_decode32() and mtbl_varint_decode64() do the reverse β they read the 32 or 64 bit varint quantity (respectively) sitting in the argument ptr, and the quantity they read gets placed into the argument value. π
β οΈ okay IMPORTANT one, don't skip this: bounds checking must be performed by the caller. that's you. you're the main character here, and the bounds checking is your job. the library is NOT doing it for you, periodt.
π RETURN VALUE π
mtbl_varint_encode32()andmtbl_varint_encode64()hand you back the number of bytes written todst. π«Άmtbl_varint_decode32()andmtbl_varint_decode64()hand you back the number of bytes read fromptr. βmtbl_varint_length()returns the number of bytes that its argumentvaluewould require in the variable-width encoding. it's the "how much room am i gonna need" function, iconic. πmtbl_varint_length_packed()returns the number of bytes consumed by the variable-width encoded quantity at its argumentdata. it will read at mostlen_bufbytes fromdata. and if a valid varint is not present? you get back the value0. π
that's the whole page, bestie β short and slay. this was MTBL_VARINT(3), dated 01/31/2014. go forth and encode responsibly, and remember: bounds check or it's over for you π§π
π¦ where this page comes from
- debian bookworm: libmtbl-dev 1.3.0-1 Β· Apache-2.0
- debian trixie: libmtbl-dev 1.6.1-1+b1 Β· Apache-2.0
- ubuntu jammy: libmtbl-dev 1.3.0-1 Β· Apache-2.0
- ubuntu noble: libmtbl-dev 1.3.0-1 Β· Apache-2.0
- ubuntu questing: libmtbl-dev 1.6.1-1
- ubuntu resolute: libmtbl-dev 1.6.1-1build1 Β· Apache-2.0
- ubuntu stonking: libmtbl-dev 1.6.1-1build1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms