cbor

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cbor implements the CBOR (RFC 8949) subset used by COSE structures: integers, byte strings, text strings, arrays, maps, tags, booleans, null, and undefined. Encoding is deterministic (definite lengths, minimal integer encoding, bytewise-sorted map keys). Decoding is strict: indefinite lengths, floating-point values, extended simple values, duplicate map keys, non-integer and non-string map keys, excessive nesting, and trailing data are rejected.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedValue = errors.New("unsupported value")
	ErrMalformed        = errors.New("malformed data")
)
View Source
var (
	ErrInvalidTarget = errors.New("invalid target")
	ErrTypeMismatch  = errors.New("type mismatch")
)

Functions

func Decode

func Decode(data []byte) (any, error)

Decode deserializes a single value, rejecting trailing data. Integers are returned as int64, byte strings as []byte, text strings as string, arrays as []any, maps as map[any]any (with int64 or string keys), and tagged data items as Tag.

func DecodeFirst added in v1.1.0

func DecodeFirst(data []byte) (any, []byte, error)

DecodeFirst is Decode, except trailing data is returned rather than rejected: it deserializes the first value in data and additionally returns the bytes that follow it. Used for formats that embed a CBOR item followed by other data whose start is not known in advance, such as the credential public key within WebAuthn authenticator data.

func DecodeNoCopy

func DecodeNoCopy(data []byte) (any, error)

DecodeNoCopy is Decode, except decoded byte strings alias data instead of being copied. The caller must keep data alive and unmodified for as long as the decoded byte strings are in use. The aliasing slices have their capacity capped, so appending to them does not modify data.

func Encode

func Encode(value any) ([]byte, error)

Encode serializes a value deterministically (RFC 8949, Section 4.2.1). Supported types: nil, Undefined, bool, int, int64, uint64, []byte, string, []any, map[int64]any, map[any]any, and Tag.

func Marshal

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

Marshal serializes a Go value deterministically. See MarshalValue.

func MarshalValue

func MarshalValue(value any) (any, error)

MarshalValue converts a Go value into the CBOR value model. Struct fields are named via the cborschema, jsonschema, or json tag (in that order of precedence); fields tagged omitempty or omitzero are omitted when zero, and []byte becomes a byte string.

func Unmarshal

func Unmarshal(data []byte, target any) error

Unmarshal decodes data and maps the resulting value into target. See UnmarshalValue.

func UnmarshalValue

func UnmarshalValue(value any, target any) error

UnmarshalValue maps a decoded CBOR value into target, which must be a non-nil pointer. Struct fields are matched by the cborschema, jsonschema, or json tag name (in that order of precedence), falling back to the field name; unknown map keys are ignored.

Types

type Tag

type Tag struct {
	Number  uint64
	Content any
}

Tag is a CBOR tagged data item.

type Undefined

type Undefined struct{}

Undefined is the CBOR "undefined" simple value.

Directories

Path Synopsis
Package schema implements validation of decoded CBOR values (the value model produced by github.com/Motmedel/utils_go/pkg/cbor) against schemas.
Package schema implements validation of decoded CBOR values (the value model produced by github.com/Motmedel/utils_go/pkg/cbor) against schemas.

Jump to

Keyboard shortcuts

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