---
name: "mtbl_varint"
section: "3"
platform: "linux"
package: "libmtbl-dev"
version: "1.6.1-1+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mtbl_varint"
---

# mtbl\_varint(3)

Variable-width encoding and decoding of 32 and 64 bit integers

## 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

**mtbl\_varint\_encode32**() and **mtbl\_varint\_encode64**() write the 32 or 64 bit quantity, respectively, in the argument *value* to the buffer in the argument *dst*. The quantity will be written in using a variable-width encoding that uses at most 5 bytes for a 32 bit quantity or 10 bytes for a 64 bit quantity.

**mtbl\_varint\_decode32**() and **mtbl\_varint\_decode64**() read the 32 or 64 bit varint quantity, respectively, in the argument *ptr*. The quantity read will be placed in the argument *value*.

Bounds checking must be performed by the caller.

## RETURN VALUE

**mtbl\_varint\_encode32**() and **mtbl\_varint\_encode64**() return the number of bytes written to *dst*.

**mtbl\_varint\_decode32**() and **mtbl\_varint\_decode64**() return 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.

**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*. The value 0 is returned if a valid varint is not present.
