ufcbor

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package ufcbor provides utilities for fast and reflect-less CBOR decoding and encoding.

Index

Constants

View Source
const (
	HeaderUintTinyMin = 0x00
	HeaderUintTinyMax = 0x17
	HeaderUint8       = 0x18
	HeaderUint16      = 0x19
	HeaderUint32      = 0x1a
	HeaderUint64      = 0x1b

	HeaderIntTinyMin = 0x20
	HeaderIntTinyMax = 0x37
	HeaderInt8       = 0x38
	HeaderInt16      = 0x39
	HeaderInt32      = 0x3a
	HeaderInt64      = 0x3b

	HeaderBytesTinyMin = 0x40
	HeaderBytesTinyMax = 0x57
	HeaderBytes8       = 0x58
	HeaderBytes16      = 0x59
	HeaderBytes32      = 0x5a
	HeaderBytes64      = 0x5b

	HeaderArrayTinyMin = 0x80
	HeaderArrayTinyMax = 0x97
	HeaderArray8       = 0x98
	HeaderArray16      = 0x99
	HeaderArray32      = 0x9a
	HeaderArray64      = 0x9b

	HeaderMapTinyMin = 0xa0
	HeaderMapTinyMax = 0xb7
	HeaderMap8       = 0xb8
	HeaderMap16      = 0xb9
	HeaderMap32      = 0xba
	HeaderMap64      = 0xbb
)

Header constants.

See https://www.rfc-editor.org/rfc/rfc8949.html#name-jump-table-for-initial-byte.

View Source
const (
	ValueIntTinyMin = -0x18
	ValueIntTinyMax = -0x01

	ValueUintTinyMin = 0x00
	ValueUintTinyMax = 0x17
)

Value constants.

See https://www.rfc-editor.org/rfc/rfc8949.html#name-jump-table-for-initial-byte.

View Source
const TinyMask = 0b0001_1111

TinyMask is the bit mask for obtaining tiny values in headers.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

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

Decoder is a simple, performant CBOR decoder. All methods save the first error they encounter, and the error should be checked by calling Decoder.Err. Decoder operates with byte slices as opposed to an io.Reader to improve performance by reducing the amount of virtual calls and buffer allocations on the client side.

func NewDecoder

func NewDecoder() (d Decoder)

NewDecoder returns a new properly initialized decoder. d is a value and not a pointer to spare allocations.

func (*Decoder) AppendBytes

func (d *Decoder) AppendBytes(orig, b []byte) (res, rest []byte)

AppendBytes decodes a CBOR byte string from b, appends it to orig and returns it. rest is the rest of b after consuming the byte string. If there are errors, res is orig and rest is b.

func (*Decoder) DecodeArrayStart

func (d *Decoder) DecodeArrayStart(b []byte) (l uint64, rest []byte)

DecodeArrayStart decodes a start of a CBOR array with the length l, if b contains it. rest is the rest of b after consuming the array start. If there are errors, l is 0 and rest is b.

func (*Decoder) DecodeInt64

func (d *Decoder) DecodeInt64(b []byte) (n int64, rest []byte)

DecodeInt64 decodes a CBOR signed integer, if b contains it. rest is the rest of b after consuming the integer. If there are errors, n is 0 and rest is b.

func (*Decoder) DecodeMapStart

func (d *Decoder) DecodeMapStart(b []byte) (l uint64, rest []byte)

DecodeMapStart decodes a start of a CBOR map with the length l, if b contains it. rest is the rest of b after consuming the array start. If there are errors, l is 0 and rest is b.

func (*Decoder) DecodeUint64

func (d *Decoder) DecodeUint64(b []byte) (u uint64, rest []byte)

DecodeUint64 decodes a CBOR unsigned integer, if b contains it. rest is the rest of b after consuming the unsigned integer. If there are errors, u is 0 and rest is b.

func (*Decoder) Err

func (d *Decoder) Err() (err error)

Err returns the first error the decoder has encountered, if any.

type Encoder

type Encoder struct{}

Encoder is a simple, performant CBOR encoder. It handles byte slices as opposed to an io.Writer to improve performance by reducing the amount of virtual calls and buffer allocations on the client side.

TODO(a.garipov): Consider turning into functions.

TODO(a.garipov): Move to golibs/cborutil.

func NewEncoder

func NewEncoder() (e Encoder)

NewEncoder returns a new properly initialized Encoder.

func (Encoder) EncodeArrayStart

func (e Encoder) EncodeArrayStart(orig []byte, l uint64) (res []byte)

EncodeArrayStart appends a start of a CBOR array with the length l to orig and returns it.

func (Encoder) EncodeBytes

func (e Encoder) EncodeBytes(orig, b []byte) (res []byte)

EncodeBytes appends b as a CBOR byte string to orig and returns it.

func (Encoder) EncodeInt64

func (e Encoder) EncodeInt64(orig []byte, n int64) (res []byte)

EncodeInt64 appends n as a CBOR signed integer to orig and returns it.

func (Encoder) EncodeMapStart

func (e Encoder) EncodeMapStart(orig []byte, l uint64) (res []byte)

EncodeMapStart appends a start of a CBOR map with the length l to orig and returns it.

func (Encoder) EncodeUint64

func (e Encoder) EncodeUint64(orig []byte, u uint64) (res []byte)

EncodeUint64 appends u as a CBOR unsigned integer to orig and returns it.

Jump to

Keyboard shortcuts

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