cdr

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cdr implements CDR/XCDR1 (Common Data Representation) encoding and decoding as specified by the OMG DDS-XTypes 1.3 standard.

CDR encodes DDS data types on the wire using little-endian byte order and natural alignment. The encapsulation header (4 bytes: {0x00,0x01,0x00,0x00} for CDR_LE) is prepended by the Encoder and consumed by the Decoder.

Alignment

Each primitive type is aligned to its own size (bool/int8: 1, int16: 2, int32/float32: 4, int64/float64: 8). Strings are encoded as a 4-byte length prefix (including null terminator) followed by the UTF-8 bytes and a null byte. Byte sequences use a 4-byte count prefix.

Usage

// Encode
e := cdr.NewEncoder()
e.WriteString("hello")
e.WriteInt32(42)
data := e.Bytes()

// Decode
d, err := cdr.NewDecoder(data)
s, _ := d.ReadString()
n, _ := d.ReadInt32()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

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

Decoder reads CDR/XCDR1 little-endian bytes from a buffer. The buffer must begin with the 4-byte CDR_LE encapsulation header.

func NewDecoder

func NewDecoder(data []byte) (*Decoder, error)

NewDecoder creates a Decoder for data. Returns an error if the encapsulation header is missing or has an unsupported scheme.

func (*Decoder) ReadBool

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

ReadBool decodes a boolean byte.

func (*Decoder) ReadBytes

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

ReadBytes decodes a byte sequence (uint32 count + bytes).

func (*Decoder) ReadFloat32

func (d *Decoder) ReadFloat32() (float32, error)

ReadFloat32 decodes a 32-bit IEEE 754 float.

func (*Decoder) ReadFloat64

func (d *Decoder) ReadFloat64() (float64, error)

ReadFloat64 decodes a 64-bit IEEE 754 float.

func (*Decoder) ReadInt8

func (d *Decoder) ReadInt8() (int8, error)

ReadInt8 decodes a signed byte.

func (*Decoder) ReadInt16

func (d *Decoder) ReadInt16() (int16, error)

ReadInt16 decodes a signed 16-bit integer.

func (*Decoder) ReadInt32

func (d *Decoder) ReadInt32() (int32, error)

ReadInt32 decodes a signed 32-bit integer.

func (*Decoder) ReadInt64

func (d *Decoder) ReadInt64() (int64, error)

ReadInt64 decodes a signed 64-bit integer.

func (*Decoder) ReadString

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

ReadString decodes a CDR string (uint32 length + bytes + null terminator).

func (*Decoder) ReadUint8

func (d *Decoder) ReadUint8() (uint8, error)

ReadUint8 decodes an unsigned byte.

func (*Decoder) ReadUint16

func (d *Decoder) ReadUint16() (uint16, error)

ReadUint16 decodes an unsigned 16-bit integer.

func (*Decoder) ReadUint32

func (d *Decoder) ReadUint32() (uint32, error)

ReadUint32 decodes an unsigned 32-bit integer.

func (*Decoder) ReadUint64

func (d *Decoder) ReadUint64() (uint64, error)

ReadUint64 decodes an unsigned 64-bit integer.

func (*Decoder) Remaining

func (d *Decoder) Remaining() int

Remaining returns the number of undecoded bytes.

type Encoder

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

Encoder writes CDR/XCDR1 little-endian bytes to an internal buffer. Call Bytes() to retrieve the complete encoded message including the 4-byte encapsulation header.

func NewEncoder

func NewEncoder() *Encoder

NewEncoder returns an Encoder pre-seeded with the CDR_LE encapsulation header.

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Bytes returns the complete encoded buffer including the encapsulation header.

func (*Encoder) Len

func (e *Encoder) Len() int

Len returns the current encoded length in bytes.

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(v bool)

WriteBool encodes a boolean (1 byte: 0 or 1).

func (*Encoder) WriteBytes

func (e *Encoder) WriteBytes(b []byte)

WriteBytes encodes a byte sequence: uint32 count + raw bytes.

func (*Encoder) WriteFloat32

func (e *Encoder) WriteFloat32(v float32)

WriteFloat32 encodes a 32-bit IEEE 754 float (4-byte aligned).

func (*Encoder) WriteFloat64

func (e *Encoder) WriteFloat64(v float64)

WriteFloat64 encodes a 64-bit IEEE 754 float (8-byte aligned).

func (*Encoder) WriteInt8

func (e *Encoder) WriteInt8(v int8)

WriteInt8 encodes a signed byte.

func (*Encoder) WriteInt16

func (e *Encoder) WriteInt16(v int16)

WriteInt16 encodes a signed 16-bit integer (2-byte aligned).

func (*Encoder) WriteInt32

func (e *Encoder) WriteInt32(v int32)

WriteInt32 encodes a signed 32-bit integer (4-byte aligned).

func (*Encoder) WriteInt64

func (e *Encoder) WriteInt64(v int64)

WriteInt64 encodes a signed 64-bit integer (8-byte aligned).

func (*Encoder) WriteString

func (e *Encoder) WriteString(s string)

WriteString encodes a CDR string: uint32 length (including null terminator), UTF-8 bytes, null byte. The length field is 4-byte aligned.

func (*Encoder) WriteUint8

func (e *Encoder) WriteUint8(v uint8)

WriteUint8 encodes an unsigned byte.

func (*Encoder) WriteUint16

func (e *Encoder) WriteUint16(v uint16)

WriteUint16 encodes an unsigned 16-bit integer (2-byte aligned).

func (*Encoder) WriteUint32

func (e *Encoder) WriteUint32(v uint32)

WriteUint32 encodes an unsigned 32-bit integer (4-byte aligned).

func (*Encoder) WriteUint64

func (e *Encoder) WriteUint64(v uint64)

WriteUint64 encodes an unsigned 64-bit integer (8-byte aligned).

Jump to

Keyboard shortcuts

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