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() and mtbl_varint_encode64() hand you back the number of bytes written to dst. 🫢
  • mtbl_varint_decode32() and mtbl_varint_decode64() hand you back the number of bytes read from ptr. ⭐
  • mtbl_varint_length() returns the number of bytes that its argument value would 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 argument data. it will read at most len_buf bytes from data. and if a valid varint is not present? you get back the value 0. πŸ’€

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 πŸ§šπŸ’œ