fragments

package
v0.0.0-...-072f331 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

package fragments provides low-level encoding and decoding helpers to construct and parse DBus message.

The provided encoder and decoder are low level tools, and do not ensure that all outputs are valid DBus messages.

You should not need to use this package at all, unless you are writing your own codeberg.org/dbus-go/dbus.Marshaler or codeberg.org/dbus-go/dbus.Unmarshaler, in which case your code will be handed an Encoder or Decoder and expected to produce correct DBus wire data with it.

Index

Constants

This section is empty.

Variables

View Source
var (
	BigEndian    = wrapStd{binary.BigEndian}
	LittleEndian = wrapStd{binary.LittleEndian}
	NativeEndian = wrapStd{binary.NativeEndian}
)

Functions

This section is empty.

Types

type ByteOrder

type ByteOrder interface {
	// contains filtered or unexported methods
}

type Decoder

type Decoder struct {
	// Order is the initial byte order to use when reading multi-byte
	// values. It can be changed during decoding by
	// [Decoder.ByteOrderFlag].
	Order ByteOrder
	// Mapper provides DecoderFuncs for types given to
	// Decoder.Value. If mapper is nil, the Decoder functions
	// normally except that Decoder.Value always returns an error.
	Mapper func(reflect.Type) (DecoderFunc, error)
	// In is the input stream to read.
	In io.Reader
	// contains filtered or unexported fields
}

A Decoder provides utilities to read a DBus wire format message from an io.Reader.

Methods advance the read cursor as needed to account for the padding required by DBus alignment rules, except for Decoder.Read which reads bytes verbatim.

func (*Decoder) Array

func (d *Decoder) Array(containsStructs bool, readElement func(int) error) (int, error)

Array reads an array.

readElement is called repeatedly while there is array data remaining to process, passing in the array index of the element to be decoded. readElement must completely consume all array bytes from the input, and must not read beyond the end of the array data.

Array returns the total number of array elements that were processed.

containsStructs indicates whether the array's elements are structs, so that the decoder consumes array header padding appropriately even if the array contains no elements.

containsStructs only affects the size and alignment of the struct header. When reading an array of structs, the caller must also use Decoder.Struct appropriately to align the reads of each element.

func (*Decoder) ByteOrderFlag

func (d *Decoder) ByteOrderFlag() error

ByteOrderFlag reads a DBus byte order flag byte, and sets the decoder's Order to match it.

func (*Decoder) Bytes

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

Bytes reads a DBus byte array.

func (*Decoder) Pad

func (d *Decoder) Pad(align int) error

Pad consumes padding bytes as needed to make the next read happen at a multiple of align bytes. If the decoder is already correctly aligned, no bytes are consumed.

func (*Decoder) Read

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

Read reads n bytes, with no framing or padding.

func (*Decoder) Signature

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

Signature reads a DBus signature.

func (*Decoder) String

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

String reads a DBus string.

func (*Decoder) Struct

func (d *Decoder) Struct(fields func() error) error

Struct reads a struct.

Struct fields must be read within the provided fields function.

func (*Decoder) Uint16

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

Uint16 reads a uint16.

func (*Decoder) Uint32

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

Uint32 reads a uint32.

func (*Decoder) Uint64

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

Uint64 reads a uint64.

func (*Decoder) Uint8

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

Uint8 reads a uint8.

func (*Decoder) Value

func (d *Decoder) Value(ctx context.Context, v any) error

Value reads a value into v, using the DecoderFunc provided by [Decoder.Mapper]. v must be a non-nil pointer.

type DecoderFunc

type DecoderFunc func(ctx context.Context, dec *Decoder, val reflect.Value) error

A DecoderFunc reads a value into val.

type Encoder

type Encoder struct {
	// Order is the byte order to use when encoding multi-byte values.
	Order ByteOrder
	// Mapper provides EncoderFuncs for types given to
	// Encoder.Value. If mapper is nil, the Encoder functions normally
	// except that Encoder.Value always returns an error.
	Mapper func(reflect.Type) (EncoderFunc, error)
	// Out is the encoded output.
	Out []byte
}

An Encoder provides utilities to write a DBus wire format message to a byte slice.

Methods insert padding as needed to conform to DBus alignment rules, except for Encoder.Write which outputs bytes verbatim.

func (*Encoder) Array

func (e *Encoder) Array(containsStructs bool, elements func() error) error

Array writes an array to the output.

Array elements must be added within the provided elements function. The elements function is responsible for padding each array element to the correct alignment for the element type.

containsStructs indicates whether the array's elements are structs, so that the array header can be padded accordingly.

func (*Encoder) ByteOrderFlag

func (e *Encoder) ByteOrderFlag()

ByteOrderFlag writes the DBus byte order flag byte ('l' or 'B') that matches [Encoder.Order].

func (*Encoder) Bytes

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

Bytes writes a DBus byte array.

func (*Encoder) Pad

func (e *Encoder) Pad(align int)

Pad inserts padding bytes as needed to make the next write start at a multiple of align bytes. If the message is already correctly aligned, no padding is inserted.

func (*Encoder) Signature

func (e *Encoder) Signature(s string)

Signature writes a DBus signature.

func (*Encoder) String

func (e *Encoder) String(s string)

String writes a DBus string.

func (*Encoder) Struct

func (e *Encoder) Struct(elements func() error) error

Struct writes a struct to the output.

Struct fields must be added within the provided elements function.

func (*Encoder) Uint16

func (e *Encoder) Uint16(u16 uint16)

Uint16 writes uint16.

func (*Encoder) Uint32

func (e *Encoder) Uint32(u32 uint32)

Uint32 writes uint32.

func (*Encoder) Uint64

func (e *Encoder) Uint64(u64 uint64)

Uint64 writes uint64.

func (*Encoder) Uint8

func (e *Encoder) Uint8(u8 uint8)

Uint8 writes a uint8.

func (*Encoder) Value

func (e *Encoder) Value(ctx context.Context, v any) error

Value writes v to the output, using the EncoderFunc provided by the encoder's Mapper.

func (*Encoder) Write

func (e *Encoder) Write(bs []byte)

Write writes bs as-is to the output. It is the caller's responsibility to ensure correct padding and encoding.

type EncoderFunc

type EncoderFunc func(ctx context.Context, enc *Encoder, val reflect.Value) error

An EncoderFunc writes a value to the given encoder.

Jump to

Keyboard shortcuts

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