quic_varint (quic v1.3.0)

View Source

Variable-length integer encoding/decoding for QUIC.

QUIC uses a variable-length integer encoding scheme where the two most significant bits of the first byte indicate the length:

  • 2#00xxxxxx - 1 byte, 6-bit value (0-63)
  • 2#01xxxxxx - 2 bytes, 14-bit value (0-16383)
  • 2#10xxxxxx - 4 bytes, 30-bit value (0-1073741823)
  • 2#11xxxxxx - 8 bytes, 62-bit value (0-4611686018427387903)

Summary

Functions

Decode a QUIC variable-length integer from a binary. Returns {Value, Rest} where Rest is the remaining binary. Raises error:badarg for invalid input.

Encode an integer as a QUIC variable-length integer. Returns the encoded binary. Raises error:badarg for negative values or values > 2^62-1.

Return the number of bytes needed to encode a value.

Return the maximum value that can be encoded in N bytes.

Functions

decode(Bin)

-spec decode(binary()) -> {non_neg_integer(), binary()}.

Decode a QUIC variable-length integer from a binary. Returns {Value, Rest} where Rest is the remaining binary. Raises error:badarg for invalid input.

encode(V)

-spec encode(non_neg_integer()) -> binary().

Encode an integer as a QUIC variable-length integer. Returns the encoded binary. Raises error:badarg for negative values or values > 2^62-1.

encode_len(V)

-spec encode_len(non_neg_integer()) -> 1 | 2 | 4 | 8.

Return the number of bytes needed to encode a value.

max_value(_)

-spec max_value(1 | 2 | 4 | 8) -> non_neg_integer().

Return the maximum value that can be encoded in N bytes.