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 ¶
- type Decoder
- func (d *Decoder) ReadBool() (bool, error)
- func (d *Decoder) ReadBytes() ([]byte, error)
- func (d *Decoder) ReadFloat32() (float32, error)
- func (d *Decoder) ReadFloat64() (float64, error)
- func (d *Decoder) ReadInt8() (int8, error)
- func (d *Decoder) ReadInt16() (int16, error)
- func (d *Decoder) ReadInt32() (int32, error)
- func (d *Decoder) ReadInt64() (int64, error)
- func (d *Decoder) ReadString() (string, error)
- func (d *Decoder) ReadUint8() (uint8, error)
- func (d *Decoder) ReadUint16() (uint16, error)
- func (d *Decoder) ReadUint32() (uint32, error)
- func (d *Decoder) ReadUint64() (uint64, error)
- func (d *Decoder) Remaining() int
- type Encoder
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) Len() int
- func (e *Encoder) WriteBool(v bool)
- func (e *Encoder) WriteBytes(b []byte)
- func (e *Encoder) WriteFloat32(v float32)
- func (e *Encoder) WriteFloat64(v float64)
- func (e *Encoder) WriteInt8(v int8)
- func (e *Encoder) WriteInt16(v int16)
- func (e *Encoder) WriteInt32(v int32)
- func (e *Encoder) WriteInt64(v int64)
- func (e *Encoder) WriteString(s string)
- func (e *Encoder) WriteUint8(v uint8)
- func (e *Encoder) WriteUint16(v uint16)
- func (e *Encoder) WriteUint32(v uint32)
- func (e *Encoder) WriteUint64(v uint64)
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 ¶
NewDecoder creates a Decoder for data. Returns an error if the encapsulation header is missing or has an unsupported scheme.
func (*Decoder) ReadFloat32 ¶
ReadFloat32 decodes a 32-bit IEEE 754 float.
func (*Decoder) ReadFloat64 ¶
ReadFloat64 decodes a 64-bit IEEE 754 float.
func (*Decoder) ReadString ¶
ReadString decodes a CDR string (uint32 length + bytes + null terminator).
func (*Decoder) ReadUint16 ¶
ReadUint16 decodes an unsigned 16-bit integer.
func (*Decoder) ReadUint32 ¶
ReadUint32 decodes an unsigned 32-bit integer.
func (*Decoder) ReadUint64 ¶
ReadUint64 decodes an unsigned 64-bit integer.
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 ¶
Bytes returns the complete encoded buffer including the encapsulation header.
func (*Encoder) WriteBytes ¶
WriteBytes encodes a byte sequence: uint32 count + raw bytes.
func (*Encoder) WriteFloat32 ¶
WriteFloat32 encodes a 32-bit IEEE 754 float (4-byte aligned).
func (*Encoder) WriteFloat64 ¶
WriteFloat64 encodes a 64-bit IEEE 754 float (8-byte aligned).
func (*Encoder) WriteInt16 ¶
WriteInt16 encodes a signed 16-bit integer (2-byte aligned).
func (*Encoder) WriteInt32 ¶
WriteInt32 encodes a signed 32-bit integer (4-byte aligned).
func (*Encoder) WriteInt64 ¶
WriteInt64 encodes a signed 64-bit integer (8-byte aligned).
func (*Encoder) WriteString ¶
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 ¶
WriteUint8 encodes an unsigned byte.
func (*Encoder) WriteUint16 ¶
WriteUint16 encodes an unsigned 16-bit integer (2-byte aligned).
func (*Encoder) WriteUint32 ¶
WriteUint32 encodes an unsigned 32-bit integer (4-byte aligned).
func (*Encoder) WriteUint64 ¶
WriteUint64 encodes an unsigned 64-bit integer (8-byte aligned).