blobmsg

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 9 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

Overview

Package blobmsg encodes and decodes the blobmsg format, the typed, self-describing message encoding used by ubus.

Wire format

A blobmsg value is a blob attribute whose 7-bit id carries the value type (see Type). Attributes inside a table carry a name: the "extended" header bit is set and the payload starts with a 16-bit big-endian name length, the name bytes and a NUL, padded to 4 bytes. Integers and doubles are big-endian; strings are NUL-terminated; every attribute is padded to a 4-byte boundary.

Go representation

Decoded values use these Go types:

TypeUnspec -> nil
TypeArray  -> Array
TypeTable  -> Table
TypeString -> string
TypeInt64  -> int64
TypeInt32  -> int32
TypeInt16  -> int16
TypeInt8   -> int8   (also used on the wire for booleans)
TypeDouble -> float64

A Table keeps the field order, the exact wire type of every value and any duplicate names found on the wire, so decoding is lossless (except for the payload of UNSPEC values, which carries no meaning).

Higher-level views are strict by default: Table.Map converts to map[string]any (INT8 becomes bool) and Decode fills Go structs; both reject duplicate names unless last-wins is requested explicitly. Decode never maps to Go int: destinations use explicit widths and values that do not fit are errors.

When encoding, Go values are mapped as follows: bool -> INT8, int8 -> INT8, int16 -> INT16, int32 -> INT32, int64 -> INT64, other integer types -> INT32 if the value fits, else INT64; float32/float64 -> DOUBLE; string -> STRING; nil -> UNSPEC; Table, map[string]any and other maps with string keys -> TABLE (maps are encoded in sorted key order); structs -> TABLE (see Encode); Array, []any and slices of the supported scalar types -> ARRAY.

All multi-byte values are read and written with encoding/binary, so alignment of the input buffer does not matter on any architecture.

Index

Constants

View Source
const DefaultMaxDepth = 32

DefaultMaxDepth is the default nesting limit for encoding and decoding. The top-level table counts as depth 1.

View Source
const DefaultMaxSize = 16 << 20

DefaultMaxSize is the default input size limit of a Decoder.

View Source
const MaxNameLen = math.MaxUint16

MaxNameLen is the longest field name that can be encoded.

Variables

View Source
var (
	ErrUnsupportedType = errors.New("blobmsg: unsupported Go type")
	ErrNameTooLong     = errors.New("blobmsg: name too long")
	ErrNUL             = errors.New("blobmsg: string or name contains NUL")
	ErrTooLarge        = errors.New("blobmsg: attribute exceeds maximum length")
	ErrTooDeep         = errors.New("blobmsg: nesting too deep")
	ErrIntRange        = errors.New("blobmsg: integer out of range")
)

Encoding errors. Errors never include encoded values.

View Source
var (
	// ErrDuplicateKey reports a table with the same field name more than
	// once where a unique name is required.
	ErrDuplicateKey = errors.New("blobmsg: duplicate key")
	// ErrTypeMismatch reports a value whose wire type does not fit the
	// Go destination.
	ErrTypeMismatch = errors.New("blobmsg: type mismatch")
	// ErrOverflow reports an integer that does not fit the destination.
	ErrOverflow = errors.New("blobmsg: integer overflow")
	// ErrUnknownField reports a table field without a matching struct
	// field when unknown fields are disallowed.
	ErrUnknownField = errors.New("blobmsg: unknown field")
)

Errors of the strict mappings between tables and Go values.

View Source
var ErrMalformed = errors.New("blobmsg: malformed message")

ErrMalformed is wrapped by every decoding error caused by invalid input.

Functions

func AppendTable

func AppendTable(dst []byte, v any) ([]byte, error)

AppendTable appends the encoding of the table v (see Marshal) to dst. On error dst is returned unchanged.

func AppendValue

func AppendValue(dst []byte, name string, v any) ([]byte, error)

AppendValue appends a single blobmsg attribute with the given name and value to dst. An empty name produces an unnamed attribute, as used for array elements. On error dst is returned unchanged.

func Decode

func Decode(t Table, v any, opts MapOptions) error

Decode stores the table t in the struct (or map[string]T) pointed to by v. Struct fields are matched by the `blobmsg:"name"` tag, else by the field name; "-" skips a field, ",omitempty" only affects encoding.

Supported destination types: string; bool (INT8); int8, int16, int32, int64 (from any integer wire type, with range check); float64 (DOUBLE); structs and map[string]T (TABLE); slices (ARRAY); pointers to these; blobmsg.Table, blobmsg.Array and any (raw decoded value). Go int, uint types and float32 are not supported, so that value ranges are always explicit. Missing fields leave the destination unchanged.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal encodes v as the attribute list of a blobmsg table, the form used for ubus message data. v must be nil, a Table or a map with string keys.

func UnmarshalInto

func UnmarshalInto(b []byte, v any) error

UnmarshalInto decodes the blobmsg table data b (see Unmarshal) and stores it in v with Decode and default (strict) options.

Types

type Array

type Array []any

Array is an ordered list of unnamed values (a blobmsg array).

func (Array) Slice

func (a Array) Slice() ([]any, error)

Slice converts the array to []any recursively, like Table.Map.

type DecodeError

type DecodeError struct {
	Offset int
	Reason string
}

DecodeError describes invalid input. It carries the byte offset of the offending attribute (relative to the start of the decoded buffer) and a short reason; it never contains payload bytes or field names.

func (*DecodeError) Error

func (e *DecodeError) Error() string

func (*DecodeError) Unwrap

func (e *DecodeError) Unwrap() error

Unwrap makes errors.Is(err, ErrMalformed) true.

type Decoder

type Decoder struct {
	// MaxDepth limits nesting; the top-level table or array is depth 1.
	MaxDepth int
	// MaxSize limits the input length in bytes.
	MaxSize int
}

Decoder decodes blobmsg data with configurable limits. The zero value uses DefaultMaxDepth and DefaultMaxSize.

func (*Decoder) DecodeArray

func (d *Decoder) DecodeArray(b []byte) (Array, error)

DecodeArray decodes b as the attribute list of an array. Element names, if present, are ignored.

func (*Decoder) DecodeTable

func (d *Decoder) DecodeTable(b []byte) (Table, error)

DecodeTable decodes b as the attribute list of a table: every attribute must carry a non-empty name.

func (*Decoder) DecodeValue

func (d *Decoder) DecodeValue(b []byte) (string, any, error)

DecodeValue decodes a single complete blobmsg attribute, returning its name and value. Trailing bytes after the attribute and its padding are an error.

type Field

type Field struct {
	Name  string
	Value any
}

Field is a named value inside a Table.

type FieldError

type FieldError struct {
	Path string
	Err  error
}

FieldError locates a mapping error. Path is the dotted field path (with [i] for array elements); it never contains values.

func (*FieldError) Error

func (e *FieldError) Error() string

func (*FieldError) Unwrap

func (e *FieldError) Unwrap() error

type MapOptions

type MapOptions struct {
	// LastWins accepts duplicate field names, keeping the last value (the
	// behaviour of the C blobmsg parser). By default duplicates are an
	// error.
	LastWins bool
	// DisallowUnknownFields makes table fields without a matching struct
	// field an error. By default they are ignored.
	DisallowUnknownFields bool
}

MapOptions controls the conversion of tables to Go values.

type Table

type Table []Field

Table is an ordered list of named values (a blobmsg table). It may contain duplicate names when decoded from the wire.

func Encode

func Encode(v any) (Table, error)

Encode converts a struct (or pointer to one) to a Table using the same field rules as Decode. Fields tagged ",omitempty" are left out when they hold their zero value.

func Unmarshal

func Unmarshal(b []byte) (Table, error)

Unmarshal decodes a blobmsg table attribute list (the payload of a ubus data attribute) with the default limits.

func (Table) Array

func (t Table) Array(name string) (Array, bool)

Array returns the nested array field called name.

func (Table) Bool

func (t Table) Bool(name string) (bool, bool)

Bool returns the boolean field called name. Integer values of any width are accepted; non-zero is true.

func (Table) Float

func (t Table) Float(name string) (float64, bool)

Float returns the double field called name.

func (Table) Get

func (t Table) Get(name string) (any, bool)

Get returns the value of the last field called name. The last occurrence wins, as with the C blobmsg parser.

func (Table) Int

func (t Table) Int(name string) (int64, bool)

Int returns the integer field called name, whatever its width.

func (Table) Map

func (t Table) Map() (map[string]any, error)

Map converts the table to map[string]any recursively: nested tables become map[string]any, arrays []any and INT8 values bool. A duplicate field name anywhere is an error (ErrDuplicateKey); see MapLastWins.

func (Table) MapLastWins

func (t Table) MapLastWins() map[string]any

MapLastWins is like Map but keeps the last value of duplicate names.

func (Table) String

func (t Table) String(name string) (string, bool)

String returns the string field called name.

func (Table) Table

func (t Table) Table(name string) (Table, bool)

Table returns the nested table field called name.

type Type

type Type uint8

Type is a blobmsg value type as carried in the attribute id.

const (
	TypeUnspec Type = 0
	TypeArray  Type = 1
	TypeTable  Type = 2
	TypeString Type = 3
	TypeInt64  Type = 4
	TypeInt32  Type = 5
	TypeInt16  Type = 6
	TypeInt8   Type = 7
	TypeDouble Type = 8

	// TypeBool is the wire type used for booleans.
	TypeBool = TypeInt8
)

Blobmsg value types.

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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