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
- Variables
- func AppendTable(dst []byte, v any) ([]byte, error)
- func AppendValue(dst []byte, name string, v any) ([]byte, error)
- func Decode(t Table, v any, opts MapOptions) error
- func Marshal(v any) ([]byte, error)
- func UnmarshalInto(b []byte, v any) error
- type Array
- type DecodeError
- type Decoder
- type Field
- type FieldError
- type MapOptions
- type Table
- func (t Table) Array(name string) (Array, bool)
- func (t Table) Bool(name string) (bool, bool)
- func (t Table) Float(name string) (float64, bool)
- func (t Table) Get(name string) (any, bool)
- func (t Table) Int(name string) (int64, bool)
- func (t Table) Map() (map[string]any, error)
- func (t Table) MapLastWins() map[string]any
- func (t Table) String(name string) (string, bool)
- func (t Table) Table(name string) (Table, bool)
- type Type
Constants ¶
const DefaultMaxDepth = 32
DefaultMaxDepth is the default nesting limit for encoding and decoding. The top-level table counts as depth 1.
const DefaultMaxSize = 16 << 20
DefaultMaxSize is the default input size limit of a Decoder.
const MaxNameLen = math.MaxUint16
MaxNameLen is the longest field name that can be encoded.
Variables ¶
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.
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.
var ErrMalformed = errors.New("blobmsg: malformed message")
ErrMalformed is wrapped by every decoding error caused by invalid input.
Functions ¶
func AppendTable ¶
AppendTable appends the encoding of the table v (see Marshal) to dst. On error dst is returned unchanged.
func AppendValue ¶
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 ¶
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 ¶
UnmarshalInto decodes the blobmsg table data b (see Unmarshal) and stores it in v with Decode and default (strict) options.
Types ¶
type DecodeError ¶
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 ¶
DecodeArray decodes b as the attribute list of an array. Element names, if present, are ignored.
func (*Decoder) DecodeTable ¶
DecodeTable decodes b as the attribute list of a table: every attribute must carry a non-empty name.
type FieldError ¶
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 ¶
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 ¶
Unmarshal decodes a blobmsg table attribute list (the payload of a ubus data attribute) with the default limits.
func (Table) Bool ¶
Bool returns the boolean field called name. Integer values of any width are accepted; non-zero is true.
func (Table) Get ¶
Get returns the value of the last field called name. The last occurrence wins, as with the C blobmsg parser.
func (Table) Map ¶
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 ¶
MapLastWins is like Map but keeps the last value of duplicate names.
type Type ¶
type Type uint8
Type is a blobmsg value type as carried in the attribute id.