libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Core.Varint

Description

Unsigned LEB128 varint encoding/decoding.

Used throughout libp2p for length-prefixed framing, protocol codes, and multiaddr/multihash encoding.

Follows the multiformats unsigned-varint spec (https:/github.commultiformats/unsigned-varint): varints are restricted to a maximum of 9 bytes (63 bits), and non-minimal (zero-padded) encodings are rejected on decode.

Synopsis

Documentation

encodeUvarint :: Word64 -> ByteString Source #

Encode a Word64 as an unsigned LEB128 varint. Calls error for values >= 2^63, which the spec makes unrepresentable (go-varint's PutUvarint panics identically).

decodeUvarint :: ByteString -> Either String (Word64, ByteString) Source #

Decode an unsigned LEB128 varint from a ByteString. Returns the decoded value and remaining bytes, or an error message. Rejects varints longer than 9 bytes and non-minimal encodings (a multi-byte varint whose final byte is 0x00).

maxVarintBytes :: Int Source #

Maximum number of bytes for a valid unsigned varint. The spec mandates: "Implementations MUST restrict the size of the varint to a max of 9 bytes (63 bits)."