encoding

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxPayloadSize is the maximum bytes a message can be regardless of other
	// individual limits imposed by messages themselves.
	MaxPayloadSize = 1024 * 1024 * 32 // 32MB

)

Variables

View Source
var (
	ErrOverflow     = errors.New("overflow")
	ErrNonCanonical = errors.New("non canonical")
)

Functions

func ReadElement

func ReadElement(r io.Reader, elm any) error

ReadElement reads the next sequence of bytes from r using little endian depending on the concrete type of element pointed to.

func ReadElements

func ReadElements(r io.Reader, elms ...any) error

ReadElements reads multiple items from r. It is equivalent to multiple calls to readElement.

func ReadVarBytes

func ReadVarBytes(r io.Reader) ([]byte, error)

ReadVarBytes reads a variable length byte array. A byte array is encoded as a varInt containing the length of the array followed by the bytes themselves. An error is returned if the length is greater than the maximum payload size since it helps protect against memory exhaustion attacks and forced panics through malformed messages.

func ReadVarInt

func ReadVarInt(r io.Reader) (uint64, error)

ReadVarInt reads a variable length integer from r and returns it as a uint64.

func ReadVarString

func ReadVarString(r io.Reader) (string, error)

ReadVarString reads a variable length string from r and returns it as a Go string. A variable length string is encoded as a variable length integer containing the length of the string followed by the bytes that represent the string itself. An error is returned if the length is greater than the maximum payload size since it helps protect against memory exhaustion attacks and forced panics through malformed messages.

func VarBytesSerializeSize

func VarBytesSerializeSize(bytes []byte) int

VarBytesSerializeSize returns the number of bytes it would take to serialize val as a byte array.

func VarIntSerializeSize

func VarIntSerializeSize(val uint64) int

VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func VarStringSerializeSize

func VarStringSerializeSize(str string) int

VarStringSerializeSize returns the number of bytes it would take to serialize val as a string.

func WriteElement

func WriteElement(w io.Writer, elm any) error

WriteElement writes the little endian representation of element to w.

func WriteElements

func WriteElements(w io.Writer, elements ...any) error

WriteElements writes multiple items to w. It is equivalent to multiple calls to writeElement.

func WriteVarBytes

func WriteVarBytes(w io.Writer, bytes []byte) error

WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.

func WriteVarInt

func WriteVarInt(w io.Writer, val uint64) error

WriteVarInt serializes val to w using a variable number of bytes depending on its value.

func WriteVarString

func WriteVarString(w io.Writer, str string) error

WriteVarString serializes str to w as a variable length integer containing the length of the string followed by the bytes that represent the string itself.

Types

This section is empty.

Jump to

Keyboard shortcuts

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