cborwire

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 10 Imported by: 0 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 cborwire provides bounded CBOR decoding and deterministic encoding.

Duplicate keys, indefinite-length items, and tags are rejected by default. Tags and indefinite lengths require explicit interoperability options. Encoding defaults to RFC 7049 canonical form, with explicit Core Deterministic and CTAP2 profiles available. Decoder nesting and collection limits remain bounded even when the byte limit is raised. CBOR is excluded from wire.DetectFormat because arbitrary binary data cannot be identified reliably.

Use Decode/Encode with an explicit profile where peer byte identity matters. The pinned authorities are RFC 7049 section 3.9, RFC 8949 core deterministic encoding and CTAP 2.2 section 8; these are different profiles, not generic signing policy. Reader calls buffer one bounded item and never close the reader. Borrowed values/custom codecs require caller synchronization; failed targets are indeterminate. No context API, transport, cgo or workers are introduced.

Index

Constants

View Source
const DefaultMaxBytes int64 = 1 << 20

DefaultMaxBytes is the default maximum CBOR item size.

Variables

View Source
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")

ErrPayloadTooLarge identifies CBOR items over the configured byte limit.

Functions

func Decode

func Decode(payload []byte, target any, options DecodeOptions) error

Decode borrows payload and parses one complete item into a non-nil pointer. Untyped integers use the driver's IntDecConvertNone policy (unsigned positives and signed negatives); this is not a JSON-number mapping. Duplicate keys are refused, tags/indefinite items need opt-in, and failed targets are indeterminate.

func DecodeReader

func DecodeReader(reader io.Reader, target any, options DecodeOptions) error

DecodeReader buffers a borrowed reader through EOF or size refusal before target decoding. Reader is never closed and there is no context API. Target must be a non-nil pointer; codec/strictness failures can leave it mutated. Keep custom codecs bounded and do not reuse a failed target without resetting it.

func Encode

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

Encode borrows a stable acyclic value and returns owned bytes for Profile. Zero selects Canonical; tags are forbidden unless enabled, and TimeTag needs AllowTags. Time uses the driver Unix-time mode. Output limits do not bound intermediate codec allocation or custom marshalers; errors return no payload.

func EncodeWriter

func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error

EncodeWriter completes serialization before touching a borrowed writer, which it never closes. Encoding failure writes nothing; a later write error can leave a prefix visible and matches wire.ErrWrite. It is not an atomic publication API.

Types

type DecodeOptions

type DecodeOptions struct {
	// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// MaxNestedLevels selects the pinned decoder nesting limit; zero retains its finite
	// default and invalid values fail mode construction.
	MaxNestedLevels int
	// MaxArrayElements selects the pinned decoder per-array bound; zero retains its finite
	// default.
	MaxArrayElements int
	// MaxMapPairs selects the pinned decoder per-map bound; zero retains its finite default.
	MaxMapPairs int
	// DisallowUnknownFields requests errors for unrecognized struct fields.
	DisallowUnknownFields bool
	// AllowTags allows tagged items; default forbids tags independently of byte/profile
	// policy.
	AllowTags bool
	// AllowIndefiniteLength allows indefinite-length items without disabling structural
	// budgets.
	AllowIndefiniteLength bool
}

DecodeOptions controls CBOR interoperability and resource constraints. Zero resource limits use fxamacker/cbor's bounded defaults.

type DeterministicProfile

type DeterministicProfile uint8

DeterministicProfile identifies a defined deterministic CBOR encoding.

const (
	// Canonical uses the canonical encoding defined by RFC 7049 section 3.9.
	Canonical DeterministicProfile = iota
	// CoreDeterministic uses RFC 8949 core deterministic encoding.
	CoreDeterministic
	// CTAP2Deterministic uses the CTAP2 canonical CBOR profile.
	CTAP2Deterministic
)

type EncodeOptions

type EncodeOptions struct {
	// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// Profile selects Canonical (zero), CoreDeterministic or CTAP2Deterministic; unknown
	// profiles fail.
	Profile DeterministicProfile
	// AllowTags allows tagged items; default forbids tags independently of byte/profile
	// policy.
	AllowTags bool
	// TimeTag requires a time tag for Unix-time encoding and therefore requires AllowTags.
	TimeTag bool
}

EncodeOptions controls the deterministic profile and tag behavior.

Jump to

Keyboard shortcuts

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