ExMqtt.Protocol.Varint (ex_mqtt_uv v0.1.0)

Copy Markdown View Source

MQTT Variable Byte Integer encoding and decoding (1 to 4 bytes, max 268,435,455).

Summary

Functions

Decodes a Variable Byte Integer from the front of binary.

Encodes a non-negative integer (0..268_435_455) into a Variable Byte Integer binary.

Same as encode/1 but returns the binary directly or raises ArgumentError.

Functions

decode(binary)

@spec decode(binary()) ::
  {:ok, non_neg_integer(), binary()} | :incomplete | {:error, :malformed_varint}

Decodes a Variable Byte Integer from the front of binary.

Returns:

  • {:ok, value, rest} on success
  • :incomplete when more bytes are needed
  • {:error, :malformed_varint} when the encoding exceeds 4 bytes

encode(value)

@spec encode(non_neg_integer()) :: {:ok, binary()} | {:error, :varint_out_of_range}

Encodes a non-negative integer (0..268_435_455) into a Variable Byte Integer binary.

encode!(value)

@spec encode!(non_neg_integer()) :: binary()

Same as encode/1 but returns the binary directly or raises ArgumentError.