codec

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: GPL-3.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufferPool = sync.Pool{
	New: func() any {
		buf := make([]byte, 0, 256)
		return &buf
	},
}

BufferPool provides reusable byte buffers for encoding.

Functions

func GetBuffer

func GetBuffer() *[]byte

GetBuffer returns a buffer from the pool.

func PutBuffer

func PutBuffer(buf *[]byte)

PutBuffer returns a buffer to the pool.

Types

type BatchDecodeResult

type BatchDecodeResult struct {
	Results []*DecodeResult
	Errors  []error
}

BatchDecodeResult holds results from batch decoding.

type BitReader

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

BitReader reads bits from a byte slice. Bits are read MSB-first within each byte.

func NewBitReader

func NewBitReader(data []byte) *BitReader

NewBitReader creates a new BitReader from a byte slice.

func (*BitReader) BitOffset

func (r *BitReader) BitOffset() int

BitOffset returns the current bit offset within the current byte (0-7).

func (*BitReader) ByteOffset

func (r *BitReader) ByteOffset() int

ByteOffset returns the current byte offset.

func (*BitReader) ReadBits

func (r *BitReader) ReadBits(numBits int, order schema.ByteOrder) (uint64, error)

ReadBits reads numBits bits (1-64) in the specified byte order and returns the value.

func (*BitReader) ReadBytes

func (r *BitReader) ReadBytes(n int) ([]byte, error)

ReadBytes reads n bytes. The reader must be byte-aligned.

func (*BitReader) ReadRemainingBytes

func (r *BitReader) ReadRemainingBytes() []byte

ReadRemainingBytes reads all remaining bytes. Must be byte-aligned.

func (*BitReader) RemainingBits

func (r *BitReader) RemainingBits() int

RemainingBits returns the number of remaining bits.

type BitWriter

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

BitWriter accumulates bits into a byte buffer. Bits are written MSB-first within each byte.

func NewBitWriter

func NewBitWriter() *BitWriter

NewBitWriter creates a new BitWriter.

func (*BitWriter) BitOffset

func (w *BitWriter) BitOffset() int

BitOffset returns the current bit offset within the current byte (0-7).

func (*BitWriter) Bytes

func (w *BitWriter) Bytes() []byte

Bytes returns the written bytes, flushing any partial byte with zero padding.

func (*BitWriter) Len

func (w *BitWriter) Len() int

Len returns the number of complete bytes written so far.

func (*BitWriter) SetBytesAt

func (w *BitWriter) SetBytesAt(offset int, data []byte)

SetBytesAt overwrites bytes at a specific offset in the buffer. Used for checksum backfill after encoding.

func (*BitWriter) WriteBits

func (w *BitWriter) WriteBits(value uint64, numBits int, order schema.ByteOrder) error

WriteBits writes numBits bits (1-64) of value in the specified byte order. For big-endian: most significant bits are written first. For little-endian multi-byte values: least significant byte is written first.

func (*BitWriter) WriteByteSlice

func (w *BitWriter) WriteByteSlice(data []byte) error

WriteByteSlice writes raw bytes. The writer must be byte-aligned.

type CodecEngine

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

CodecEngine provides a unified encode/decode interface backed by Encoder and Decoder with shared registries.

func NewCodecEngine

func NewCodecEngine(cr *checksum.ChecksumRegistry, fr *format.FormatRegistry) *CodecEngine

NewCodecEngine creates a new CodecEngine with the given registries.

func (*CodecEngine) BatchDecode

func (e *CodecEngine) BatchDecode(s interface{ GetSchema() interface{} }, packets [][]byte) *BatchDecodeResult

BatchDecode decodes multiple packets in sequence.

func (*CodecEngine) Decode

func (e *CodecEngine) Decode(s *schema.ProtocolSchema, data []byte) (*DecodeResult, error)

Decode deserialises data into a DecodeResult according to the schema.

func (*CodecEngine) Encode

func (e *CodecEngine) Encode(s *schema.ProtocolSchema, packet map[string]any) ([]byte, error)

Encode serialises packet into []byte according to the schema.

type DecodeResult

type DecodeResult struct {
	Packet    map[string]any
	BytesRead int
}

DecodeResult holds the decoded packet and the number of bytes consumed.

type Decoder

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

Decoder decodes raw bytes into a Packet (map[string]any) according to a ProtocolSchema.

func NewDecoder

NewDecoder creates a new Decoder with the given registries.

func (*Decoder) Decode

func (dec *Decoder) Decode(s *schema.ProtocolSchema, data []byte) (*DecodeResult, error)

Decode deserialises data into a packet according to the schema.

type Encoder

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

Encoder encodes a Packet (map[string]any) into raw bytes according to a ProtocolSchema.

func NewEncoder

NewEncoder creates a new Encoder with the given registries.

func (*Encoder) Encode

func (enc *Encoder) Encode(s *schema.ProtocolSchema, packet map[string]any) ([]byte, error)

Encode serialises packet into []byte according to the schema.

Jump to

Keyboard shortcuts

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