cbor

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: MIT Imports: 14 Imported by: 48

README

CBOR Library - Slideshow and Latest Docs.

CBOR library in Go

This is a generic CBOR encoder and decoder. It can encode integers and floats to their smallest forms (like float16) when values fit. Each release passes 375+ tests and 250+ million execs fuzzing with 1100+ CBOR files.

Build Status codecov Go Report Card Release License

What is CBOR? CBOR (RFC 7049) is a binary data format inspired by JSON and MessagePack. CBOR is used in IETF Internet Standards such as COSE (RFC 8152) and CWT (RFC 8392 CBOR Web Token). Even WebAuthn uses CBOR.

Why this CBOR library? It doesn't crash and it has well-balanced qualities: small, fast, safe and easy. It also supports "preferred serialization" by encoding integers and floats to their smallest forms when values fit.

  • Small apps. Same programs are 4-9 MB smaller by switching to this library. No code gen and the only imported pkg is x448/float16 which is maintained by the same team as this library.

  • Small data. The toarray, keyasint, and omitempty struct tags shrink size of Go structs encoded to CBOR. Integers encode to smallest form that fits. Floats can shrink from float64 -> float32 -> float16 if values can round-trip.

  • Fast. v1.3 became faster than a well-known library that uses unsafe optimizations and code gen. Faster libraries will always exist, but speed is only one factor. This library doesn't use unsafe optimizations or code gen.

  • Safe and reliable. It prevents crashes on malicious CBOR data by using extensive tests, coverage-guided fuzzing, data validation, and avoiding Go's unsafe pkg. Nested levels for CBOR arrays, maps, and tags are limited to 32.

  • Easy and saves time. It has the same API as Go's encoding/json when possible. Existing structs don't require changes. Go struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.

Predefined configs make it easier to comply with standards like Canonical CBOR, CTAP2 Canonical CBOR, etc.

Custom configs can be created by setting individual options. E.g., EncOptions.NaNConvert can be set to NaNConvertNone, NaNConvert7e00, NaNConvertQuiet, or NaNConvertPreserveSignal.

Struct tags like keyasint and toarray make compact CBOR data such as COSE, CWT, and SenML easier to use.


CBOR API


👉 ComparisonsStatusDesign GoalsFeaturesStandardsFuzzingUsageSecurity PolicyLicense

Comparisons

Comparisons are between this newer library and a well-known library that had 1,000+ stars before this library was created. Default build settings for each library were used for all comparisons.

This library is safer. Small malicious CBOR messages are rejected quickly before they exhaust system resources.

alt text

This library is smaller. Programs like senmlCat can be 4 MB smaller by switching to this library. Programs using more complex CBOR data types can be 9.2 MB smaller.

alt text

This library is faster for encoding and decoding CBOR Web Token (CWT). However, speed is only one factor and it can vary depending on data types and sizes. Unlike the other library, this one doesn't use Go's unsafe package or code gen.

alt text

The resource intensive codec.CborHandle initialization (in the other library) was placed outside the benchmark loop to make sure their library wasn't penalized.

Doing your own comparisons is highly recommended. Use your most common message sizes and data types.

Current Status

Version 1.x has:

  • Stable API – won't make breaking API changes except:
    • CoreDetEncOptions() is subject to change because it uses draft standard not yet approved.
    • PreferredUnsortedEncOptions() is subject to change because it uses draft standard not yet approved.
  • Stable requirements – will always support Go v1.12 (unless there's compelling reason).
  • Passed fuzzing – Fuzzing for v1.5 passed 2.75+ billion execs and is in progress. v1.4 passed 532+ million execs in coverage-guided fuzzing at the time of release and reached 4+ billion execs 18 days after release.

Each commit passes 375+ tests. Each release also passes 250+ million execs in coverage-guided fuzzing using 1,100+ CBOR files (corpus). See Fuzzing and Code Coverage.

Recent activity:

  • Release v1.3 -- Faster encoding and decoding.
  • Release v1.3 -- Add toarray struct tag to simplify using CBOR arrays.
  • Release v1.3 -- Add keyasint struct tag to simplify using CBOR maps with int keys.
  • Release v1.3.4 -- Bugfixes and refactoring. Limit nested levels to 32 for arrays, maps, tags.
  • Release v1.4 -- Deprecate bool encoding options and add int SortMode. Use float16 to float32 conversion func that had all 65536 results verified to be correct. Fix decoding of float16 subnormal numbers.
  • Release v1.5 -- Add option to shrink floating-point values to smaller sizes like float16 (if they preserve value).
  • Release v1.5 -- Add options for encoding floating-point NaN values: NaNConvertNone, NaNConvert7e00, NaNConvertQuiet, or NaNConvertPreserveSignal.

Coming soon: support for CBOR tags (major type 6). After that, options for handling duplicate map keys.

Design Goals

This library is designed to be a generic CBOR encoder and decoder. It was initially created for a WebAuthn (FIDO2) server library, because existing CBOR libraries (in Go) didn't meet certain criteria in 2019.

This library is designed to be:

  • Easy – API is like encoding/json plus keyasint and toarray struct tags.
  • Small – Programs in cisco/senml are 4 MB smaller by switching to this library. In extreme cases programs can be smaller by 9+ MB. No code gen and the only imported pkg is x448/float16 which is maintained by the same team.
  • Safe and reliable – No unsafe pkg, coverage >95%, coverage-guided fuzzing, and data validation to avoid crashes on malformed or malicious data.

Competing factors are balanced:

  • Speed vs safety vs size – to keep size small, avoid code generation. For safety, validate data and avoid Go's unsafe pkg. For speed, use safe optimizations such as caching struct metadata. v1.4 is faster than a well-known library that uses unsafe and code gen.
  • Standards compliance – CBOR (RFC 7049) with minor limitations. Encoder supports options for sorting, floating-point conversions, and more. Predefined configurations are also available so you can use "CTAP2 Canonical CBOR", etc. without knowing individual options. Decoder checks for well-formedness, validates data, and limits nested levels to defend against attacks. See Standards.

Avoiding unsafe package has benefits. The unsafe package warns:

Packages that import unsafe may be non-portable and are not protected by the Go 1 compatibility guidelines.

All releases prioritize reliability to avoid crashes on decoding malformed CBOR data. See Fuzzing and Coverage.

Features not in Go's standard library are usually not added. However, the toarray struct tag in ugorji/go was too useful to ignore. It was added in v1.3 when a project mentioned they were using it with CBOR to save disk space.

Features

  • API is like encoding/json plus extra struct tags:
    • cbor.Encoder writes CBOR to io.Writer.
    • cbor.Decoder reads CBOR from io.Reader.
    • cbor.Marshal writes CBOR to []byte.
    • cbor.Unmarshal reads CBOR from []byte.
  • Support "cbor" and "json" keys in Go's struct tags. If both are specified, then "cbor" is used.
  • toarray struct tag allows named struct fields for elements of CBOR arrays.
  • keyasint struct tag allows named struct fields for elements of CBOR maps with int keys.
  • Encoder has easy functions that create and return modifiable configurations:
    • func CanonicalEncOptions() EncOptions
    • func CTAP2EncOptions() EncOptions
    • func CoreDetEncOptions() EncOptions
    • func PreferredUnsortedEncOptions() EncOptions
  • For Go integers, encoder always uses "preferred serialization" which encodes their values to the smallest number of bytes.
  • Encoder floating-point option types: ShortestFloatMode, InfConvertMode, and NaNConvertMode.
    • ShortestFloatMode: ShortestFloatNone or ShortestFloat16 (IEEE 754 binary16, etc. if value fits).
    • InfConvertMode: InfConvertNone or InfConvertFloat16.
    • NaNConvertMode: NaNConvertNone, NaNConvert7e00, NaNConvertQuiet, or NaNConvertPreserveSignal
  • Encoder sort options: SortNone, SortBytewiseLexical, SortCanonical, SortCTAP2, SortCoreDeterministic
  • Support encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces.
  • Support cbor.RawMessage which can delay CBOR decoding or precompute CBOR encoding.
  • Support cbor.Marshaler and cbor.Unmarshaler interfaces to allow user-defined types to have custom CBOR encoding and decoding.
  • Support time.Time as RFC 3339 formatted text string or Unix time.
  • Support indefinite length CBOR data ("streaming"). For decoding, each indefinite length "container" must fit into memory to perform well-formedness checks that prevent exploits. Go's io.LimitReader can be used to limit sizes.
  • Encoder uses struct field visibility rules for anonymous struct fields (same rules as encoding/json.)
  • Encoder always uses smallest CBOR integer sizes for more compact data serialization.
  • Decoder always checks for invalid UTF-8 string errors.
  • Decoder always decodes in-place to slices, maps, and structs.
  • Decoder uses case-insensitive field name match when decoding to structs.
  • Both encoder and decoder correctly handles nil slice, map, pointer, and interface values.

Coming soon: support for CBOR tags (major type 6). After that, options for handling duplicate map keys.

Standards

This library implements CBOR as specified in RFC 7049 with minor limitations.

For Go integers, encoder always uses "preferred serialization" which encodes their values to the smallest number of bytes.

Encoder has options that can be set individually to create custom configurations. Easy functions are also provided to create and return modifiable configurations (EncOptions):

  • CanonicalEncOptions() -- Canonical CBOR (RFC 7049 Section 3.9).
  • CTAP2EncOptions() -- CTAP2 Canonical CBOR (FIDO2 CTAP2).
  • PreferredUnsortedEncOptions() -- Preferred Serialization (unsorted, shortest integer and floating-point forms that preserve values, NaN values encoded as 0xf97e00).
  • CoreDetEncOptions() -- Bytewise lexicographic sort order for map keys, plus options from PreferredUnsortedEncOptions()

EncOptions.Sort:

  • SortNone: no sorting for map keys.
  • SortLengthFirst: length-first map key ordering.
  • SortBytewiseLexical: bytewise lexicographic map key ordering.
  • SortCanonical: same as SortLengthFirst (RFC 7049 Section 3.9)
  • SortCTAP2Canonical: same as SortBytewiseLexical (CTAP2 Canonical CBOR).
  • SortCoreDeterministic: same as SortBytewiseLexical.

Encoder has 3 types of options for floating-point data: ShortestFloatMode, InfConvertMode, and NaNConvertMode.

EncOptions.ShortestFloat:

  • ShortestFloatNone: no conversion.
  • ShortestFloat16: uses float16 (IEEE 754 binary16) as the shortest form that preserves value.

With ShortestFloat16, each floating-point value (including subnormals) can encode float64 -> float32 -> float16 when values can round-trip. Conversions for infinity and NaN use InfConvert and NaNConvert settings.

EncOptions.InfConvert:

  • InfConvertNone: don't convert +- infinity to other representations -- used by CTAP2 Canonical CBOR
  • InfConvertFloat16: convert +- infinity to float16 since they always preserve value (recommended)

EncOptions.NaNConvert:

  • NaNConvertNone: don't convert NaN to other representations -- used by CTAP2 Canonical CBOR.
  • NaNConvert7e00: encode to 0xf97e00 (CBOR float16 = 0x7e00) -- used by RFC 7049 Canonical CBOR.
  • NaNConvertQuiet: force quiet bit = 1 and use shortest form that preserves NaN payload.
  • NaNConvertPreserveSignal: convert to smallest form that preserves value (quit bit unmodified and NaN payload preserved).

Float16 conversions use x448/float16 maintained by the same team as this library. All 4+ billion possible conversions are verified to be correct in that library.

Decoder checks for all required well-formedness errors, including all "subkinds" of syntax errors and too little data.

After well-formedness is verified, basic validity errors are handled as follows:

  • Invalid UTF-8 string: Decoder always checks and returns invalid UTF-8 string error.
  • Duplicate keys in a map: By default, decoder decodes to a map with duplicate keys by overwriting previous value with the same key. Options to handle duplicate map keys in different ways may be added as a feature.

When decoding well-formed CBOR arrays and maps, decoder saves the first error it encounters and continues with the next item. Options to handle this differently may be added in the future.

Limitations

CBOR tags (type 6) is being added in the next release (milestone v2.0) and is coming soon.

Known limitations:

  • Currently, CBOR tag numbers are ignored. Decoder simply decodes tag content. Work is in progress to add support.
  • Currently, duplicate map keys are not checked during decoding. Option to handle duplicate map keys in different ways will be added.
  • Nested levels for CBOR arrays, maps, and tags are limited to 32 to quickly reject potentially malicious data. This limit will be reconsidered upon request.
  • CBOR negative int (type 1) that cannot fit into Go's int64 are not supported, such as RFC 7049 example -18446744073709551616. Decoding these values returns cbor.UnmarshalTypeError like Go's encoding/json.
  • CBOR Undefined (0xf7) value decodes to Go's nil value. Use CBOR Null (0xf6) to round-trip with Go's nil.

Like Go's encoding/json, data validation checks the entire message to prevent partially filled (corrupted) data. This library also prevents crashes and resource exhaustion attacks from malicious CBOR data. Use Go's io.LimitReader when decoding very large data to limit size.

Fuzzing and Code Coverage

Over 375 tests must pass before tagging a release. They include all RFC 7049 examples, bugs found by fuzzing, 2 maliciously crafted CBOR data, and over 87 tests with malformed data.

Code coverage must not fall below 95% when tagging a release. Code coverage is 97.9% (go test -cover) for cbor v1.5 which is among the highest for libraries (in Go) of this type.

Coverage-guided fuzzing must pass 250+ million execs before tagging a release. E.g. v1.4 passed 532+ million execs in coverage-guided fuzzing at the time of release and reached 4+ billion execs 18 days later. Fuzzing uses fxamacker/cbor-fuzz. Default corpus has:

Over 1,100 files (corpus) are used for fuzzing because it includes fuzz-generated corpus.

System Requirements

  • Go 1.12 (or newer)
  • Tested and fuzzed on linux_amd64, but it should work on other little-endian platforms.

Versions and API Changes

This project uses Semantic Versioning, so the API is always backwards compatible unless the major version number changes.

API

The API is the same as encoding/json when possible.

In addition to the API, the keyasint and toarray struct tags are worth knowing. They can reduce programming effort, improve system performance, and reduce the size of serialized data.

package cbor // import "github.com/fxamacker/cbor"

func Marshal(v interface{}, encOpts EncOptions) ([]byte, error)
func Unmarshal(data []byte, v interface{}) error
func Valid(data []byte) (rest []byte, err error)
type Decoder struct{ ... }
    func NewDecoder(r io.Reader) *Decoder
    func (dec *Decoder) Decode(v interface{}) (err error)
    func (dec *Decoder) NumBytesRead() int
type EncOptions struct{ ... }
    func CTAP2EncOptions() EncOptions
    func CanonicalEncOptions() EncOptions
    func CoreDetEncOptions() EncOptions
    func PreferredUnsortedEncOptions() EncOptions
type Encoder struct{ ... }
    func NewEncoder(w io.Writer, encOpts EncOptions) *Encoder
    func (enc *Encoder) Encode(v interface{}) error
    func (enc *Encoder) StartIndefiniteByteString() error
    func (enc *Encoder) StartIndefiniteTextString() error
    func (enc *Encoder) StartIndefiniteArray() error
    func (enc *Encoder) StartIndefiniteMap() error
    func (enc *Encoder) EndIndefinite() error
type InfConvertMode int
    const InfConvertFloat16 InfConvertMode = iota ...
type InvalidUnmarshalError struct{ ... }
type Marshaler interface{ ... }
type NaNConvertMode int
    const NaNConvert7e00 NaNConvertMode = iota ...
type RawMessage []byte
type SemanticError struct{ ... }
type ShortestFloatMode int
    const ShortestFloatNone ShortestFloatMode = iota ...
type SortMode int
    const SortNone SortMode = 0 ...
type SyntaxError struct{ ... }
type UnmarshalTypeError struct{ ... }
type Unmarshaler interface{ ... }
type UnsupportedTypeError struct{ ... }

See API docs for more details.

Installation

go get github.com/fxamacker/cbor

Released versions benefit from longer fuzz tests.

Usage

👉 Use Go's io.LimitReader when decoding very large data to limit size.

The API is the same as encoding/json when possible:

  • cbor.Marshal writes CBOR to []byte
  • cbor.Unmarshal reads CBOR from []byte
  • cbor.Encoder writes CBOR to io.Writer
  • cbor.Decoder reads CBOR from io.Reader

The keyasint and toarray struct tags make it easy to use compact CBOR message formats. Internet standards often use CBOR arrays and CBOR maps with int keys to save space.

Using named struct fields instead of array elements or maps with int keys makes code more readable and less error prone.

Decoding CWT (CBOR Web Token) using keyasint and toarray struct tags:

// Signed CWT is defined in RFC 8392
type signedCWT struct {
	_           struct{} `cbor:",toarray"`
	Protected   []byte
	Unprotected coseHeader
	Payload     []byte
	Signature   []byte
}

// Part of COSE header definition
type coseHeader struct {
	Alg int    `cbor:"1,keyasint,omitempty"`
	Kid []byte `cbor:"4,keyasint,omitempty"`
	IV  []byte `cbor:"5,keyasint,omitempty"`
}

// data is []byte containing signed CWT

var v signedCWT
if err := cbor.Unmarshal(data, &v); err != nil {
	return err
}

Encoding CWT (CBOR Web Token) using keyasint and toarray struct tags:

// Use signedCWT struct defined in "Decoding CWT" example.

var v signedCWT
...
if data, err := cbor.Marshal(v, cbor.EncOptions{}); err != nil {
	return err
}

Decoding SenML using keyasint struct tag:

// RFC 8428 says, "The data is structured as a single array that 
// contains a series of SenML Records that can each contain fields"

type SenMLRecord struct {
	BaseName    string  `cbor:"-2,keyasint,omitempty"`
	BaseTime    float64 `cbor:"-3,keyasint,omitempty"`
	BaseUnit    string  `cbor:"-4,keyasint,omitempty"`
	BaseValue   float64 `cbor:"-5,keyasint,omitempty"`
	BaseSum     float64 `cbor:"-6,keyasint,omitempty"`
	BaseVersion int     `cbor:"-1,keyasint,omitempty"`
	Name        string  `cbor:"0,keyasint,omitempty"`
	Unit        string  `cbor:"1,keyasint,omitempty"`
	Value       float64 `cbor:"2,keyasint,omitempty"`
	ValueS      string  `cbor:"3,keyasint,omitempty"`
	ValueB      bool    `cbor:"4,keyasint,omitempty"`
	ValueD      string  `cbor:"8,keyasint,omitempty"`
	Sum         float64 `cbor:"5,keyasint,omitempty"`
	Time        float64 `cbor:"6,keyasint,omitempty"`
	UpdateTime  float64 `cbor:"7,keyasint,omitempty"`
}

// data is a []byte containing SenML

var v []SenMLRecord
if err := cbor.Unmarshal(data, &v); err != nil {
	return err
}

Encoding SenML using keyasint struct tag and ShortestFloat16 encoding option:

// use SenMLRecord struct defined in "Decoding SenML" example

var v []SenMLRecord
...
if data, err := cbor.Marshal(v, cbor.EncOptions{ShortestFloat: cbor.ShortestFloat16}); err != nil {
	return err
}

Decoding:

// create a decoder
dec := cbor.NewDecoder(reader)

// decode into empty interface
var i interface{}
err = dec.Decode(&i)

// decode into struct 
var stru ExampleStruct
err = dec.Decode(&stru)

// decode into map
var m map[string]string
err = dec.Decode(&m)

// decode into primitive
var f float32
err = dec.Decode(&f)

Encoding:

// create an encoder with canonical CBOR encoding enabled
enc := cbor.NewEncoder(writer, cbor.CanonicalEncOptions())

// encode struct
err = enc.Encode(stru)

// encode map
err = enc.Encode(m)

// encode primitive
err = enc.Encode(f)

Encoding indefinite length array:

enc := cbor.NewEncoder(writer, cbor.EncOptions{})

// start indefinite length array encoding
err = enc.StartIndefiniteArray()

// encode array element
err = enc.Encode(1)

// encode array element
err = enc.Encode([]int{2, 3})

// start nested indefinite length array as array element
err = enc.StartIndefiniteArray()

// encode nested array element
err = enc.Encode(4)

// encode nested array element
err = enc.Encode(5)

// end nested indefinite length array
err = enc.EndIndefinite()

// end indefinite length array
err = enc.EndIndefinite()

More examples.

Benchmarks

Go structs are faster than maps with string keys:

  • decoding into struct is >29% faster than decoding into map.
  • encoding struct is >35% faster than encoding map.

Go structs with keyasint struct tag are faster than maps with integer keys:

  • decoding into struct is >25% faster than decoding into map.
  • encoding struct is >32% faster than encoding map.

Go structs with toarray struct tag are faster than slice:

  • decoding into struct is >14% faster than decoding into slice.
  • encoding struct is >9% faster than encoding slice.

Doing your own benchmarks is highly recommended. Use your most common message sizes and data types.

See Benchmarks for fxamacker/cbor.

Code of Conduct

This project has adopted the Contributor Covenant Code of Conduct. Contact faye.github@gmail.com with any questions or comments.

Contributing

Please refer to How to Contribute.

Security Policy

For v1, security fixes are provided only for the latest released version since the API won't break compatibility.

To report security vulnerabilities, please email faye.github@gmail.com and allow time for the problem to be resolved before reporting it to the public.

Disclaimers

Phrases like "no crashes" or "doesn't crash" mean there are no known crash bugs in the latest version based on results of unit tests and coverage-guided fuzzing. It doesn't imply the software is 100% bug-free or 100% invulnerable to all known and unknown attacks.

Please read the license for additional disclaimers and terms.

Special Thanks

  • Carsten Bormann for RFC 7049 (CBOR), his fast confirmation to my RFC 7049 errata, approving my pull request to 7049bis, and his patience when I misread a line in 7049bis.
  • Montgomery Edwards⁴⁴⁸ for contributing float16 conversion code, updating the README.md, creating comparison charts & slideshow, and filing many helpful issues.
  • Keith Randall for fixing Go bugs and providing workarounds so we don't have to wait for new versions of Go.
  • Stefan Tatschner for being the 1st to discover my CBOR library, filing issues #1 and #2, and using it in sep.
  • Yawning Angel for replacing a library with this one in oasis-core, and filing issue #5.
  • Jernej Kos for filing issue #11 (add feature similar to json.RawMessage) and his kind words about this library.
  • Jeffrey Yasskin and Laurence Lundblade for their help clarifying 7049bis on the IETF mailing list.
  • Jakob Borg for his words of encouragement about this library at Go Forum.

License

Copyright (c) Faye Amacker. All rights reserved.

Licensed under the MIT License.


👉 ComparisonsStatusDesign GoalsFeaturesStandardsFuzzingUsageSecurity PolicyLicense

Documentation

Overview

Package cbor provides a fuzz-tested CBOR encoder and decoder with full support for float16, Canonical CBOR, CTAP2 Canonical CBOR, and custom settings.

THIS VERSION IS OUTDATED

V2 IS AVAILABLE

https://github.com/fxamacker/cbor/releases

Basics

Encoding options allow "preferred serialization" by encoding integers and floats to their smallest forms (like float16) when values fit.

Go struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected. If both struct tags are specified then `cbor` is used.

Struct tags like "keyasint", "toarray", and "omitempty" make it easy to use very compact formats like COSE and CWT (CBOR Web Tokens) with structs.

For example, the "toarray" struct tag encodes/decodes struct fields as array elements. And "keyasint" struct tag encodes/decodes struct fields to values of maps with specified int keys.

fxamacker/cbor-fuzz provides coverage-guided fuzzing for this package.

For latest API docs, see: https://github.com/fxamacker/cbor#api

Example (COSE)
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	// Use "keyasint" struct tag to encode/decode struct to/from CBOR map.
	// Use cbor.RawMessage to delay unmarshaling (CrvOrNOrK's data type depends on Kty's value).
	type coseKey struct {
		Kty       int             `cbor:"1,keyasint,omitempty"`
		Kid       []byte          `cbor:"2,keyasint,omitempty"`
		Alg       int             `cbor:"3,keyasint,omitempty"`
		KeyOpts   int             `cbor:"4,keyasint,omitempty"`
		IV        []byte          `cbor:"5,keyasint,omitempty"`
		CrvOrNOrK cbor.RawMessage `cbor:"-1,keyasint,omitempty"` // K for symmetric keys, Crv for elliptic curve keys, N for RSA modulus
		XOrE      cbor.RawMessage `cbor:"-2,keyasint,omitempty"` // X for curve x-coordinate, E for RSA public exponent
		Y         cbor.RawMessage `cbor:"-3,keyasint,omitempty"` // Y for curve y-cooridate
		D         []byte          `cbor:"-4,keyasint,omitempty"`
	}
	// Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.2
	// 128-Bit Symmetric Key
	cborData, _ := hex.DecodeString("a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963313238030a")
	var v coseKey
	if err := cbor.Unmarshal(cborData, &v); err != nil {
		fmt.Println("error:", err)
	}
	if _, err := cbor.Marshal(v, cbor.EncOptions{}); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", v)
}
Output:

{Kty:4 Kid:[83 121 109 109 101 116 114 105 99 49 50 56] Alg:10 KeyOpts:0 IV:[] CrvOrNOrK:[80 35 31 76 77 77 48 81 253 194 236 10 56 81 213 179 131] XOrE:[] Y:[] D:[]}
Example (CWT)
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	// Use "keyasint" struct tag to encode/decode struct to/from CBOR map.
	type claims struct {
		Iss string `cbor:"1,keyasint"`
		Sub string `cbor:"2,keyasint"`
		Aud string `cbor:"3,keyasint"`
		Exp int    `cbor:"4,keyasint"`
		Nbf int    `cbor:"5,keyasint"`
		Iat int    `cbor:"6,keyasint"`
		Cti []byte `cbor:"7,keyasint"`
	}
	// Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1
	cborData, _ := hex.DecodeString("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71")
	var v claims
	if err := cbor.Unmarshal(cborData, &v); err != nil {
		fmt.Println("error:", err)
	}
	if _, err := cbor.Marshal(v, cbor.EncOptions{}); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", v)
}
Output:

{Iss:coap://as.example.com Sub:erikw Aud:coap://light.example.com Exp:1444064944 Nbf:1443944944 Iat:1443944944 Cti:[11 113]}
Example (SenML)
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	// Use "keyasint" struct tag to encode/decode struct to/from CBOR map.
	type SenMLRecord struct {
		BaseName    string  `cbor:"-2,keyasint,omitempty"`
		BaseTime    float64 `cbor:"-3,keyasint,omitempty"`
		BaseUnit    string  `cbor:"-4,keyasint,omitempty"`
		BaseValue   float64 `cbor:"-5,keyasint,omitempty"`
		BaseSum     float64 `cbor:"-6,keyasint,omitempty"`
		BaseVersion int     `cbor:"-1,keyasint,omitempty"`
		Name        string  `cbor:"0,keyasint,omitempty"`
		Unit        string  `cbor:"1,keyasint,omitempty"`
		Time        float64 `cbor:"6,keyasint,omitempty"`
		UpdateTime  float64 `cbor:"7,keyasint,omitempty"`
		Value       float64 `cbor:"2,keyasint,omitempty"`
		ValueS      string  `cbor:"3,keyasint,omitempty"`
		ValueB      bool    `cbor:"4,keyasint,omitempty"`
		ValueD      string  `cbor:"8,keyasint,omitempty"`
		Sum         float64 `cbor:"5,keyasint,omitempty"`
	}
	// Data from https://tools.ietf.org/html/rfc8428#section-6
	cborData, _ := hex.DecodeString("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333")
	var v []SenMLRecord
	if err := cbor.Unmarshal(cborData, &v); err != nil {
		fmt.Println("error:", err)
	}
	// Encoder uses ShortestFloat16 option to use float16 as the shortest form that preserves floating-point value.
	if _, err := cbor.Marshal(v, cbor.EncOptions{ShortestFloat: cbor.ShortestFloat16}); err != nil {
		fmt.Println("error:", err)
	}
	for _, rec := range v {
		fmt.Printf("%+v\n", rec)
	}
}
Output:

{BaseName:urn:dev:ow:10e2073a0108006: BaseTime:1.276020076001e+09 BaseUnit:A BaseValue:0 BaseSum:0 BaseVersion:5 Name:voltage Unit:V Time:0 UpdateTime:0 Value:120.1 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:-5 UpdateTime:0 Value:1.2 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:-4 UpdateTime:0 Value:1.3 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:-3 UpdateTime:0 Value:1.4 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:-2 UpdateTime:0 Value:1.5 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:-1 UpdateTime:0 Value:1.6 ValueS: ValueB:false ValueD: Sum:0}
{BaseName: BaseTime:0 BaseUnit: BaseValue:0 BaseSum:0 BaseVersion:0 Name:current Unit: Time:0 UpdateTime:0 Value:1.7 ValueS: ValueB:false ValueD: Sum:0}
Example (SignedCWT)
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	// Use "keyasint" struct tag to encode/decode struct to/from CBOR map.
	// Partial COSE header definition
	type coseHeader struct {
		Alg int    `cbor:"1,keyasint,omitempty"`
		Kid []byte `cbor:"4,keyasint,omitempty"`
		IV  []byte `cbor:"5,keyasint,omitempty"`
	}
	// Use "toarray" struct tag to encode/decode struct to/from CBOR array.
	type signedCWT struct {
		_           struct{} `cbor:",toarray"`
		Protected   []byte
		Unprotected coseHeader
		Payload     []byte
		Signature   []byte
	}
	// Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.3
	cborData, _ := hex.DecodeString("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30")
	var v signedCWT
	if err := cbor.Unmarshal(cborData, &v); err != nil {
		fmt.Println("error:", err)
	}
	if _, err := cbor.Marshal(v, cbor.EncOptions{}); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", v)
}
Output:

{_:{} Protected:[161 1 38] Unprotected:{Alg:0 Kid:[65 115 121 109 109 101 116 114 105 99 69 67 68 83 65 50 53 54] IV:[]} Payload:[167 1 117 99 111 97 112 58 47 47 97 115 46 101 120 97 109 112 108 101 46 99 111 109 2 101 101 114 105 107 119 3 120 24 99 111 97 112 58 47 47 108 105 103 104 116 46 101 120 97 109 112 108 101 46 99 111 109 4 26 86 18 174 176 5 26 86 16 217 240 6 26 86 16 217 240 7 66 11 113] Signature:[84 39 193 255 40 210 63 186 209 242 156 76 124 106 85 94 96 29 111 162 159 145 121 188 61 116 56 186 202 202 90 205 8 200 212 212 249 97 49 104 12 66 154 1 248 89 81 236 238 116 58 82 185 182 54 50 197 114 9 18 14 28 158 48]}
Example (WebAuthn)
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	// Use cbor.RawMessage to delay unmarshaling (AttStmt's data type depends on Fmt's value).
	type attestationObject struct {
		AuthnData []byte          `cbor:"authData"`
		Fmt       string          `cbor:"fmt"`
		AttStmt   cbor.RawMessage `cbor:"attStmt"`
	}
	cborData, _ := hex.DecodeString("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3")
	var v attestationObject
	if err := cbor.Unmarshal(cborData, &v); err != nil {
		fmt.Println("error:", err)
	}
	if _, err := cbor.Marshal(v, cbor.EncOptions{}); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", v)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}, encOpts EncOptions) ([]byte, error)

Marshal returns the CBOR encoding of v.

Marshal uses the following type-dependent default encodings:

Boolean values encode as CBOR booleans (type 7).

Positive integer values encode as CBOR positive integers (type 0).

Negative integer values encode as CBOR negative integers (type 1).

Floating point values encode as CBOR floating points (type 7).

String values encode as CBOR text strings (type 3).

[]byte values encode as CBOR byte strings (type 2).

Array and slice values encode as CBOR arrays (type 4).

Map values encode as CBOR maps (type 5).

Struct values encode as CBOR maps (type 5). Each exported struct field becomes a pair with field name encoded as CBOR text string (type 3) and field value encoded based on its type.

Pointer values encode as the value pointed to.

Nil slice/map/pointer/interface values encode as CBOR nulls (type 7).

time.Time values encode as text strings specified in RFC3339 when EncOptions.TimeRFC3339 is true; otherwise, time.Time values encode as numerical representation of seconds since January 1, 1970 UTC.

If value implements the Marshaler interface, Marshal calls its MarshalCBOR method. If value implements encoding.BinaryMarshaler instead, Marhsal calls its MarshalBinary method and encode it as CBOR byte string.

Marshal supports format string stored under the "cbor" key in the struct field's tag. CBOR format string can specify the name of the field, "omitempty" and "keyasint" options, and special case "-" for field omission. If "cbor" key is absent, Marshal uses "json" key.

Struct field name is treated as integer if it has "keyasint" option in its format string. The format string must specify an integer as its field name.

Special struct field "_" is used to specify struct level options, such as "toarray". "toarray" option enables Go struct to be encoded as CBOR array. "omitempty" is disabled by "toarray" to ensure that the same number of elements are encoded every time.

Anonymous struct fields are usually marshalled as if their exported fields were fields in the outer struct. Marshal follows the same struct fields visibility rules used by JSON encoding package. An anonymous struct field with a name given in its CBOR tag is treated as having that name, rather than being anonymous. An anonymous struct field of interface type is treated the same as having that type as its name, rather than being anonymous.

Interface values encode as the value contained in the interface. A nil interface value encodes as the null CBOR value.

Channel, complex, and functon values cannot be encoded in CBOR. Attempting to encode such a value causes Marshal to return an UnsupportedTypeError.

Example
package main

import (
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Animal struct {
		Age    int
		Name   string
		Owners []string
		Male   bool
	}
	animal := Animal{Age: 4, Name: "Candy", Owners: []string{"Mary", "Joe"}}
	b, err := cbor.Marshal(animal, cbor.EncOptions{})
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
}
Output:

a46341676504644e616d656543616e6479664f776e65727382644d617279634a6f65644d616c65f4
Example (Canonical)

This example uses Marshal to encode struct and map in canonical form.

package main

import (
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Animal struct {
		Age      int
		Name     string
		Contacts map[string]string
		Male     bool
	}
	animal := Animal{Age: 4, Name: "Candy", Contacts: map[string]string{"Mary": "111-111-1111", "Joe": "222-222-2222"}}
	b, err := cbor.Marshal(animal, cbor.CanonicalEncOptions())
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
}
Output:

a46341676504644d616c65f4644e616d656543616e647968436f6e7461637473a2634a6f656c3232322d3232322d32323232644d6172796c3131312d3131312d31313131
Example (Keyasint)

This example uses "keyasint" struct tag to encode struct's fiele names as integer. This feautre is very useful in handling COSE, CWT, SenML data.

package main

import (
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Record struct {
		Name        string `cbor:"1,keyasint"`
		Unit        string `cbor:"2,keyasint"`
		Measurement int    `cbor:"3,keyasint"`
	}
	rec := Record{Name: "current", Unit: "V", Measurement: 1}
	b, err := cbor.Marshal(rec, cbor.EncOptions{})
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
}
Output:

a3016763757272656e740261560301
Example (Time)
package main

import (
	"fmt"
	"time"

	"github.com/fxamacker/cbor"
)

func main() {
	tm, _ := time.Parse(time.RFC3339Nano, "2013-03-21T20:04:00Z")
	// Encode time as string in RFC3339 format.
	b, err := cbor.Marshal(tm, cbor.EncOptions{TimeRFC3339: true})
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
	// Encode time as numerical representation of seconds since January 1, 1970 UTC.
	b, err = cbor.Marshal(tm, cbor.EncOptions{TimeRFC3339: false})
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
}
Output:

74323031332d30332d32315432303a30343a30305a
1a514b67b0
Example (Toarray)

This example uses "toarray" struct tag to encode struct as CBOR array.

package main

import (
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Record struct {
		_           struct{} `cbor:",toarray"`
		Name        string
		Unit        string
		Measurement int
	}
	rec := Record{Name: "current", Unit: "V", Measurement: 1}
	b, err := cbor.Marshal(rec, cbor.EncOptions{})
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", b)
}
Output:

836763757272656e74615601

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the CBOR-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an error.

Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary, with the following additional rules:

To unmarshal CBOR into a pointer, Unmarshal first handles the case of the CBOR being the CBOR literal null. In that case, Unmarshal sets the pointer to nil. Otherwise, Unmarshal unmarshals the CBOR into the value pointed at by the pointer. If the pointer is nil, Unmarshal allocates a new value for it to point to.

To unmarshal CBOR into an interface value, Unmarshal stores one of these in the interface value:

bool, for CBOR booleans
uint64, for CBOR positive integers
int64, for CBOR negative integers
float64, for CBOR floating points
[]byte, for CBOR byte strings
string, for CBOR text strings
[]interface{}, for CBOR arrays
map[interface{}]interface{}, for CBOR maps
nil, for CBOR null

To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice only if the CBOR array is empty or slice capacity is less than CBOR array length. Otherwise Unmarshal reuses the existing slice, overwriting existing elements. Unmarshal sets the slice length to CBOR array length.

To ummarshal a CBOR array into a Go array, Unmarshal decodes CBOR array elements into corresponding Go array elements. If the Go array is smaller than the CBOR array, the additional CBOR array elements are discarded. If the CBOR array is smaller than the Go array, the additional Go array elements are set to zero values.

To unmarshal a CBOR map into a map, Unmarshal allocates a new map only if the map is nil. Otherwise Unmarshal reuses the existing map, keeping existing entries. Unmarshal stores key-value pairs from the CBOR map into Go map.

To unmarshal a CBOR map into a struct, Unmarshal matches CBOR map keys to the keys in the following priority:

  1. "cbor" key in struct field tag,
  2. "json" key in struct field tag,
  3. struct field name.

Unmarshal prefers an exact match but also accepts a case-insensitive match. Map keys which don't have a corresponding struct field are ignored.

To unmarshal a CBOR text string into a time.Time value, Unmarshal parses text string formatted in RFC3339. To unmarshal a CBOR integer/float into a time.Time value, Unmarshal creates an unix time with integer/float as seconds and fractional seconds since January 1, 1970 UTC.

To unmarshal CBOR into a value implementing the Unmarshaler interface, Unmarshal calls that value's UnmarshalCBOR method.

Unmarshal decodes a CBOR byte string into a value implementing encoding.BinaryUnmarshaler.

If a CBOR value is not appropriate for a given Go type, or if a CBOR number overflows the Go type, Unmarshal skips that field and completes the unmarshalling as best as it can. If no more serious errors are encountered, unmarshal returns an UnmarshalTypeError describing the earliest such error. In any case, it's not guaranteed that all the remaining fields following the problematic one will be unmarshaled into the target object.

The CBOR null value unmarshals into a slice/map/pointer/interface by setting that Go value to nil. Because null is often used to mean "not present", unmarshalling a CBOR null into any other Go type has no effect on the value produces no error.

Unmarshal ignores CBOR tag data and parses tagged data following CBOR tag.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Animal struct {
		Age    int
		Name   string
		Owners []string
		Male   bool
	}
	cborData, _ := hex.DecodeString("a46341676504644e616d656543616e6479664f776e65727382644d617279634a6f65644d616c65f4")
	var animal Animal
	err := cbor.Unmarshal(cborData, &animal)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animal)
}
Output:

{Age:4 Name:Candy Owners:[Mary Joe] Male:false}
Example (Time)
package main

import (
	"encoding/hex"
	"fmt"
	"time"

	"github.com/fxamacker/cbor"
)

func main() {
	cborRFC3339Time, _ := hex.DecodeString("74323031332d30332d32315432303a30343a30305a")
	tm := time.Time{}
	if err := cbor.Unmarshal(cborRFC3339Time, &tm); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v\n", tm.UTC().Format(time.RFC3339Nano))
	cborUnixTime, _ := hex.DecodeString("1a514b67b0")
	tm = time.Time{}
	if err := cbor.Unmarshal(cborUnixTime, &tm); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v\n", tm.UTC().Format(time.RFC3339Nano))
}
Output:

2013-03-21T20:04:00Z
2013-03-21T20:04:00Z

func Valid

func Valid(data []byte) (rest []byte, err error)

Valid checks whether CBOR data is complete and well-formed.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reads and decodes CBOR values from an input stream.

Example
package main

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io"

	"github.com/fxamacker/cbor"
)

func main() {
	type Animal struct {
		Age    int
		Name   string
		Owners []string
		Male   bool
	}
	cborData, _ := hex.DecodeString("a46341676504644d616c65f4644e616d656543616e6479664f776e65727382644d617279634a6f65a46341676506644d616c65f5644e616d656452756479664f776e657273816543696e6479a46341676502644d616c65f5644e616d656444756b65664f776e65727381664e6f72746f6e")
	dec := cbor.NewDecoder(bytes.NewReader(cborData))
	for {
		var animal Animal
		if err := dec.Decode(&animal); err != nil {
			if err != io.EOF {
				fmt.Println("error:", err)
			}
			break
		}
		fmt.Printf("%+v\n", animal)
	}
}
Output:

{Age:4 Name:Candy Owners:[Mary Joe] Male:false}
{Age:6 Name:Rudy Owners:[Cindy] Male:true}
{Age:2 Name:Duke Owners:[Norton] Male:true}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode reads the next CBOR-encoded value from its input and stores it in the value pointed to by v.

func (*Decoder) NumBytesRead

func (dec *Decoder) NumBytesRead() int

NumBytesRead returns the number of bytes read.

type EncOptions

type EncOptions struct {
	// Sort specifies sorting order.
	Sort SortMode

	// ShortestFloat specifies the shortest floating-point encoding that preserves
	// the value being encoded.
	ShortestFloat ShortestFloatMode

	// NaNConvert specifies how to encode NaN and it overrides ShortestFloatMode.
	NaNConvert NaNConvertMode

	// InfConvert specifies how to encode Inf and it overrides ShortestFloatMode.
	InfConvert InfConvertMode

	// Canonical causes map and struct to be encoded in a predictable sequence
	// of bytes by sorting map keys or struct fields according to canonical rules:
	//     - If two keys have different lengths, the shorter one sorts earlier;
	//     - If two keys have the same length, the one with the lower value in
	//       (byte-wise) lexical order sorts earlier.
	//
	// Deprecated: Set Sort to SortCanonical instead.
	Canonical bool

	// CTAP2Canonical uses bytewise lexicographic order of map keys encodings:
	//     - If the major types are different, the one with the lower value in
	//       numerical order sorts earlier.
	//     - If two keys have different lengths, the shorter one sorts earlier;
	//     - If two keys have the same length, the one with the lower value in
	//       (byte-wise) lexical order sorts earlier.
	// Please note that when maps keys have the same data type, "canonical CBOR"
	// AND "CTAP2 canonical CBOR" render the same sort order.
	//
	// Deprecated: Set Sort to SortCTAP2 instead.
	CTAP2Canonical bool

	// TimeRFC3339 causes time.Time to be encoded as string in RFC3339 format;
	// otherwise, time.Time is encoded as numerical representation of seconds
	// since January 1, 1970 UTC.
	TimeRFC3339 bool
	// contains filtered or unexported fields
}

EncOptions specifies encoding options.

func CTAP2EncOptions added in v1.5.0

func CTAP2EncOptions() EncOptions

CTAP2EncOptions returns EncOptions for "CTAP2 Canonical CBOR" encoding, defined in CTAP specification, with the following rules:

  1. "Integers must be encoded as small as possible."
  2. "The representations of any floating-point values are not changed."
  3. "The expression of lengths in major types 2 through 5 must be as short as possible."
  4. "Indefinite-length items must be made into definite-length items.""
  5. The keys in every map must be sorted in bytewise lexicographic order. See SortBytewiseLexical for details.
  6. "Tags as defined in Section 2.4 in [RFC7049] MUST NOT be present."

func CanonicalEncOptions added in v1.5.0

func CanonicalEncOptions() EncOptions

CanonicalEncOptions returns EncOptions for "Canonical CBOR" encoding, defined in RFC 7049 Section 3.9 with the following rules:

  1. "Integers must be as small as possible."
  2. "The expression of lengths in major types 2 through 5 must be as short as possible."
  3. The keys in every map must be sorted in length-first sorting order. See SortLengthFirst for details.
  4. "Indefinite-length items must be made into definite-length items."
  5. "If a protocol allows for IEEE floats, then additional canonicalization rules might need to be added. One example rule might be to have all floats start as a 64-bit float, then do a test conversion to a 32-bit float; if the result is the same numeric value, use the shorter value and repeat the process with a test conversion to a 16-bit float. (This rule selects 16-bit float for positive and negative Infinity as well.) Also, there are many representations for NaN. If NaN is an allowed value, it must always be represented as 0xf97e00."

func CoreDetEncOptions added in v1.5.0

func CoreDetEncOptions() EncOptions

CoreDetEncOptions returns EncOptions for "Core Deterministic" encoding, defined in RFC 7049bis with the following rules:

  1. "Preferred serialization MUST be used. In particular, this means that arguments (see Section 3) for integers, lengths in major types 2 through 5, and tags MUST be as short as possible" "Floating point values also MUST use the shortest form that preserves the value"
  2. "Indefinite-length items MUST NOT appear."
  3. "The keys in every map MUST be sorted in the bytewise lexicographic order of their deterministic encodings."

func PreferredUnsortedEncOptions added in v1.5.0

func PreferredUnsortedEncOptions() EncOptions

PreferredUnsortedEncOptions returns EncOptions for "Preferred Serialization" encoding, defined in RFC 7049bis with the following rules:

  1. "The preferred serialization always uses the shortest form of representing the argument (Section 3);"
  2. "it also uses the shortest floating-point encoding that preserves the value being encoded (see Section 5.5)." "The preferred encoding for a floating-point value is the shortest floating-point encoding that preserves its value, e.g., 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the number 5555.5, unless the CBOR-based protocol specifically excludes the use of the shorter floating-point encodings. For NaN values, a shorter encoding is preferred if zero-padding the shorter significand towards the right reconstitutes the original NaN value (for many applications, the single NaN encoding 0xf97e00 will suffice)."
  3. "Definite length encoding is preferred whenever the length is known at the time the serialization of the item starts."

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder writes CBOR values to an output stream.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	type Animal struct {
		Age    int
		Name   string
		Owners []string
		Male   bool
	}
	animals := []Animal{
		{Age: 4, Name: "Candy", Owners: []string{"Mary", "Joe"}, Male: false},
		{Age: 6, Name: "Rudy", Owners: []string{"Cindy"}, Male: true},
		{Age: 2, Name: "Duke", Owners: []string{"Norton"}, Male: true},
	}
	var buf bytes.Buffer
	enc := cbor.NewEncoder(&buf, cbor.CanonicalEncOptions())
	for _, animal := range animals {
		err := enc.Encode(animal)
		if err != nil {
			fmt.Println("error:", err)
		}
	}
	fmt.Printf("%x\n", buf.Bytes())
}
Output:

a46341676504644d616c65f4644e616d656543616e6479664f776e65727382644d617279634a6f65a46341676506644d616c65f5644e616d656452756479664f776e657273816543696e6479a46341676502644d616c65f5644e616d656444756b65664f776e65727381664e6f72746f6e
Example (IndefiniteLengthArray)

ExampleEncoder_indefiniteLengthArray encodes a stream of elements as an indefinite length array. Encoder supports nested indefinite length values.

package main

import (
	"bytes"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	var buf bytes.Buffer
	enc := cbor.NewEncoder(&buf, cbor.EncOptions{})
	// Start indefinite length array encoding.
	if err := enc.StartIndefiniteArray(); err != nil {
		fmt.Println("error:", err)
	}
	// Encode array element.
	if err := enc.Encode(1); err != nil {
		fmt.Println("error:", err)
	}
	// Encode array element.
	if err := enc.Encode([]int{2, 3}); err != nil {
		fmt.Println("error:", err)
	}
	// Start a nested indefinite length array as array element.
	if err := enc.StartIndefiniteArray(); err != nil {
		fmt.Println("error:", err)
	}
	// Encode nested array element.
	if err := enc.Encode(4); err != nil {
		fmt.Println("error:", err)
	}
	// Encode nested array element.
	if err := enc.Encode(5); err != nil {
		fmt.Println("error:", err)
	}
	// Close nested indefinite length array.
	if err := enc.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	// Close outer indefinite length array.
	if err := enc.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", buf.Bytes())
}
Output:

9f018202039f0405ffff
Example (IndefiniteLengthByteString)

ExampleEncoder_indefiniteLengthByteString encodes a stream of definite length byte string ("chunks") as an indefinite length byte string.

package main

import (
	"bytes"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	var buf bytes.Buffer
	encoder := cbor.NewEncoder(&buf, cbor.EncOptions{})
	// Start indefinite length byte string encoding.
	if err := encoder.StartIndefiniteByteString(); err != nil {
		fmt.Println("error:", err)
	}
	// Encode definite length byte string.
	if err := encoder.Encode([]byte{1, 2}); err != nil {
		fmt.Println("error:", err)
	}
	// Encode definite length byte string.
	if err := encoder.Encode([3]byte{3, 4, 5}); err != nil {
		fmt.Println("error:", err)
	}
	// Close indefinite length byte string.
	if err := encoder.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", buf.Bytes())
}
Output:

5f42010243030405ff
Example (IndefiniteLengthMap)

ExampleEncoder_indefiniteLengthMap encodes a stream of elements as an indefinite length map. Encoder supports nested indefinite length values.

package main

import (
	"bytes"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	var buf bytes.Buffer
	enc := cbor.NewEncoder(&buf, cbor.EncOptions{Sort: cbor.SortCanonical})
	// Start indefinite length map encoding.
	if err := enc.StartIndefiniteMap(); err != nil {
		fmt.Println("error:", err)
	}
	// Encode map key.
	if err := enc.Encode("a"); err != nil {
		fmt.Println("error:", err)
	}
	// Encode map value.
	if err := enc.Encode(1); err != nil {
		fmt.Println("error:", err)
	}
	// Encode map key.
	if err := enc.Encode("b"); err != nil {
		fmt.Println("error:", err)
	}
	// Start an indefinite length array as map value.
	if err := enc.StartIndefiniteArray(); err != nil {
		fmt.Println("error:", err)
	}
	// Encoded array element.
	if err := enc.Encode(2); err != nil {
		fmt.Println("error:", err)
	}
	// Encoded array element.
	if err := enc.Encode(3); err != nil {
		fmt.Println("error:", err)
	}
	// Close indefinite length array.
	if err := enc.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	// Close indefinite length map.
	if err := enc.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", buf.Bytes())
}
Output:

bf61610161629f0203ffff
Example (IndefiniteLengthTextString)

ExampleEncoder_indefiniteLengthTextString encodes a stream of definite length text string ("chunks") as an indefinite length text string.

package main

import (
	"bytes"
	"fmt"

	"github.com/fxamacker/cbor"
)

func main() {
	var buf bytes.Buffer
	encoder := cbor.NewEncoder(&buf, cbor.EncOptions{})
	// Start indefinite length text string encoding.
	if err := encoder.StartIndefiniteTextString(); err != nil {
		fmt.Println("error:", err)
	}
	// Encode definite length text string.
	if err := encoder.Encode("strea"); err != nil {
		fmt.Println("error:", err)
	}
	// Encode definite length text string.
	if err := encoder.Encode("ming"); err != nil {
		fmt.Println("error:", err)
	}
	// Close indefinite length text string.
	if err := encoder.EndIndefinite(); err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%x\n", buf.Bytes())
}
Output:

7f657374726561646d696e67ff

func NewEncoder

func NewEncoder(w io.Writer, encOpts EncOptions) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(v interface{}) error

Encode writes the CBOR encoding of v to the stream.

func (*Encoder) EndIndefinite added in v0.2.0

func (enc *Encoder) EndIndefinite() error

EndIndefinite closes last opened indefinite length value.

func (*Encoder) StartIndefiniteArray added in v0.2.0

func (enc *Encoder) StartIndefiniteArray() error

StartIndefiniteArray starts array encoding of indefinite length. Subsequent calls of (*Encoder).Encode() encodes elements of the array until EndIndefinite is called.

func (*Encoder) StartIndefiniteByteString added in v0.2.0

func (enc *Encoder) StartIndefiniteByteString() error

StartIndefiniteByteString starts byte string encoding of indefinite length. Subsequent calls of (*Encoder).Encode() encodes definite length byte strings ("chunks") as one continguous string until EndIndefinite is called.

func (*Encoder) StartIndefiniteMap added in v0.2.0

func (enc *Encoder) StartIndefiniteMap() error

StartIndefiniteMap starts array encoding of indefinite length. Subsequent calls of (*Encoder).Encode() encodes elements of the map until EndIndefinite is called.

func (*Encoder) StartIndefiniteTextString added in v0.2.0

func (enc *Encoder) StartIndefiniteTextString() error

StartIndefiniteTextString starts text string encoding of indefinite length. Subsequent calls of (*Encoder).Encode() encodes definite length text strings ("chunks") as one continguous string until EndIndefinite is called.

type InfConvertMode added in v1.5.0

type InfConvertMode int

InfConvertMode specifies how to encode Infinity and overrides ShortestFloatMode. ShortestFloatMode is not used for encoding Infinity and NaN values.

const (
	// InfConvertFloat16 always converts Inf to lossless IEEE binary16 (float16).
	InfConvertFloat16 InfConvertMode = iota

	// InfConvertNone never converts (used by CTAP2 Canonical CBOR).
	InfConvertNone
)

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

InvalidUnmarshalError describes an invalid argument passed to Unmarshal.

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type Marshaler added in v1.2.0

type Marshaler interface {
	MarshalCBOR() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid CBOR.

type NaNConvertMode added in v1.5.0

type NaNConvertMode int

NaNConvertMode specifies how to encode NaN and overrides ShortestFloatMode. ShortestFloatMode is not used for encoding Infinity and NaN values.

const (
	// NaNConvert7e00 always encodes NaN to 0xf97e00 (CBOR float16 = 0x7e00).
	NaNConvert7e00 NaNConvertMode = iota

	// NaNConvertNone never modifies or converts NaN to other representations
	// (float64 NaN stays float64, etc. even if it can use float16 without losing
	// any bits).
	NaNConvertNone

	// NaNConvertPreserveSignal converts NaN to the smallest form that preserves
	// value (quiet bit + payload) as described in RFC 7049bis Draft 12.
	NaNConvertPreserveSignal

	// NaNConvertQuiet always forces quiet bit = 1 and shortest form that preserves
	// NaN payload.
	NaNConvertQuiet
)

type RawMessage added in v1.2.0

type RawMessage []byte

RawMessage is a raw encoded CBOR value. It implements Marshaler and Unmarshaler interfaces and can be used to delay CBOR decoding or precompute a CBOR encoding.

func (RawMessage) MarshalCBOR added in v1.2.0

func (m RawMessage) MarshalCBOR() ([]byte, error)

MarshalCBOR returns m as the CBOR encoding of m.

func (*RawMessage) UnmarshalCBOR added in v1.2.0

func (m *RawMessage) UnmarshalCBOR(data []byte) error

UnmarshalCBOR sets *m to a copy of data.

type SemanticError

type SemanticError struct {
	// contains filtered or unexported fields
}

SemanticError is a description of a CBOR semantic error.

func (*SemanticError) Error

func (e *SemanticError) Error() string

type ShortestFloatMode added in v1.5.0

type ShortestFloatMode int

ShortestFloatMode specifies which floating-point format should be used as the shortest possible format for CBOR encoding. It is not used for encoding Infinity and NaN values.

const (
	// ShortestFloatNone makes float values encode without any conversion.
	// This is the default for ShortestFloatMode in v1.
	// E.g. a float32 in Go will encode to CBOR float32.  And
	// a float64 in Go will encode to CBOR float64.
	ShortestFloatNone ShortestFloatMode = iota

	// ShortestFloat16 specifies float16 as the shortest form that preserves value.
	// E.g. if float64 can convert to float32 while perserving value, then
	// encoding will also try to convert float32 to float16.  So a float64 might
	// encode as CBOR float64, float32 or float16 depending on the value.
	ShortestFloat16
)

type SortMode added in v1.4.0

type SortMode int

SortMode identifies supported sorting order.

const (
	// SortNone means no sorting.
	SortNone SortMode = 0

	// SortLengthFirst causes map keys or struct fields to be sorted such that:
	//     - If two keys have different lengths, the shorter one sorts earlier;
	//     - If two keys have the same length, the one with the lower value in
	//       (byte-wise) lexical order sorts earlier.
	// It is used in "Canonical CBOR" encoding in RFC 7049 3.9.
	SortLengthFirst SortMode = 1

	// SortBytewiseLexical causes map keys or struct fields to be sorted in the
	// bytewise lexicographic order of their deterministic CBOR encodings.
	// It is used in "CTAP2 Canonical CBOR" and "Core Deterministic Encoding"
	// in RFC 7049bis.
	SortBytewiseLexical SortMode = 2

	// SortCanonical is used in "Canonical CBOR" encoding in RFC 7049 3.9.
	SortCanonical SortMode = SortLengthFirst

	// SortCTAP2 is used in "CTAP2 Canonical CBOR".
	SortCTAP2 SortMode = SortBytewiseLexical

	// SortCoreDeterministic is used in "Core Deterministic Encoding" in RFC 7049bis.
	SortCoreDeterministic SortMode = SortBytewiseLexical
)

type SyntaxError

type SyntaxError struct {
	// contains filtered or unexported fields
}

SyntaxError is a description of a CBOR syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // description of CBOR value
	Type   reflect.Type // type of Go value it could not be assigned to
	Struct string       // struct type containing the field
	Field  string       // name of the field holding the Go value
	// contains filtered or unexported fields
}

UnmarshalTypeError describes a CBOR value that was not appropriate for a Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type Unmarshaler added in v1.2.0

type Unmarshaler interface {
	UnmarshalCBOR([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a CBOR representation of themselves. The input can be assumed to be a valid encoding of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain the data after returning.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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