Varint Frame Decoder

medium · parsing, protocols, performance, binary

Varint Frame Decoder

Decode a length‑prefixed frame from a byte buffer using unsigned varint length encoding.

Function signature

func DecodeFrame(buf []byte, maxLen int) (payload []byte, consumed int, err error)

Requirements

  • Length is encoded as unsigned varint (7 bits per byte, continuation bit 0x80).
  • If the varint is incomplete, return ErrNeedMore and consume nothing.
  • If the varint is malformed or overflows 64 bits, return ErrMalformedVarint.
  • If length > maxLen, return ErrFrameTooLarge.
  • If the payload is incomplete, return ErrNeedMore and consume nothing.
  • On success, return a slice of the input buffer (zero‑copy) and the number of bytes consumed.

Constraints

  • maxLen >= 0.
  • Do not allocate on the hot path (tests enforce zero allocations).

Notes

  • Do not convert to string.
  • consumed must be varintBytes + length.
Run tests to see results
No issues detected