msgpack

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package msgpack implements the subset of MessagePack that the memberlist wire protocol uses, byte-for-byte compatible with the encoder the protocol was defined by: github.com/hashicorp/go-msgpack/v2 with a default MsgpackHandle (WriteExt false).

Encoding follows that handle exactly:

  • strings and byte slices use the legacy raw family: fixstr (< 32 bytes), str16 (< 65536), str32; never str8 or bin. A nil byte slice is nil.
  • unsigned integers use the smallest of positive fixint, uint8, uint16, uint32, uint64.
  • signed integers use positive fixint for 0..127, int16/int32/int64 for larger positive values (never uint*), negative fixint for -32..-1, and the smallest of int8/int16/int32/int64 below that.
  • maps use fixmap (< 16 entries), map16, map32.

Decoding accepts every encoding a MessagePack encoder can produce for the target type: any integer format whose value is in range, str and bin families for strings and bytes, 0/1 fixints for booleans, and nil for the zero value. It is deliberately stricter than go-msgpack's decoder, which also accepts arrays of integers as bytes or strings and wraps out-of-range integers; no memberlist encoder produces either. Map counts above 2^31-1 and skipped values nested deeper than 32 levels are refused. It never panics and never allocates more than the input holds: in slice mode lengths are checked against the remaining input, and in stream mode buffers grow with the bytes actually received.

Index

Constants

This section is empty.

Variables

View Source
var ErrTruncated = errors.New("msgpack: truncated input")

ErrTruncated is returned when the input ends inside a value.

Functions

func AppendBool

func AppendBool(b []byte, v bool) []byte

AppendBool appends a boolean.

func AppendBytes

func AppendBytes(b []byte, p []byte) []byte

AppendBytes appends a byte slice in the legacy raw family; nil encodes as nil, an empty non-nil slice as an empty string.

func AppendInt

func AppendInt(b []byte, v int64) []byte

AppendInt appends a signed integer the way go-msgpack's EncodeInt does (PositiveIntUnsigned false): positive values above 127 use the signed formats.

func AppendMapHeader

func AppendMapHeader(b []byte, n int) []byte

AppendMapHeader appends the header of a map with n entries.

func AppendNil

func AppendNil(b []byte) []byte

AppendNil appends a nil value.

func AppendString

func AppendString(b []byte, s string) []byte

AppendString appends a string in the legacy raw family.

func AppendUint

func AppendUint(b []byte, v uint64) []byte

AppendUint appends an unsigned integer in its smallest format.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reads values from a byte slice or a Source.

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder returns a decoder over b. Slices it returns alias b only where documented (ReadKey); byte and string values are copies.

func NewStreamDecoder

func NewStreamDecoder(src Source) *Decoder

NewStreamDecoder returns a decoder over src.

func (*Decoder) ReadBool

func (d *Decoder) ReadBool() (bool, error)

ReadBool reads a boolean; like go-msgpack it accepts fixint 0 and 1, and nil reads as false.

func (*Decoder) ReadBytes

func (d *Decoder) ReadBytes(limit int) ([]byte, error)

ReadBytes reads a str or bin value as a new byte slice. nil reads as nil; an empty value as an empty non-nil slice. Values longer than limit are refused before any allocation.

func (*Decoder) ReadInt

func (d *Decoder) ReadInt(lo, hi int64) (int64, error)

ReadInt reads a signed integer within [lo, hi]; nil reads as 0.

func (*Decoder) ReadKey

func (d *Decoder) ReadKey() ([]byte, error)

ReadKey reads a map key (str or bin). The returned slice is valid until the next call; keys longer than 64 bytes are consumed and returned as nil, which matches no field.

func (*Decoder) ReadMapHeader

func (d *Decoder) ReadMapHeader() (int, error)

ReadMapHeader reads a map header and returns its entry count. A nil value reads as an empty map (go-msgpack decodes nil into a zero struct).

func (*Decoder) ReadRaw

func (d *Decoder) ReadRaw(n int) ([]byte, error)

ReadRaw reads the next n raw bytes (not a MessagePack value), such as a payload that follows the encoded headers.

func (*Decoder) ReadString

func (d *Decoder) ReadString(limit int) (string, error)

ReadString reads a str or bin value as a string; nil reads as "".

func (*Decoder) ReadUint

func (d *Decoder) ReadUint(limit uint64) (uint64, error)

ReadUint reads an unsigned integer no larger than limit; nil reads as 0.

func (*Decoder) Remaining

func (d *Decoder) Remaining() int

Remaining reports the unread input in slice mode; -1 in stream mode.

func (*Decoder) Reset

func (d *Decoder) Reset(b []byte)

Reset points d at a new slice input.

func (*Decoder) Skip

func (d *Decoder) Skip() error

Skip consumes one value of any type.

type Source

type Source interface {
	io.Reader
	io.ByteReader
}

Source is a stream the decoder reads from without read-ahead: it takes exactly the bytes of each value, so the caller can read what follows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL