Documentation
¶
Overview ¶
Package cursor reads CBOR from a byte slice in a single pass. A Cursor knows CBOR but no Go types, so a decoder built on top decides what to read and the Cursor reads it.
A general-purpose CBOR decoder checks that the whole input is well-formed and then decodes it, accepting every form the standard allows. A Cursor checks as it reads, and reads only the forms Juno stores. These are integers, strings, felts as four limbs and struct fields by number, with Skip for what is not wanted. Anything else, and anything malformed, is an error.
Its primitives are pinned to the examples of RFC 8949 Appendix A, and its felts to literal bytes. The examples are at https://www.rfc-editor.org/rfc/rfc8949.html#appendix-A
Index ¶
- Constants
- Variables
- func Felt[F ~[Limbs]uint64](c *Cursor, dst *F) error
- func LimbSlice[F ~[Limbs]uint64](c *Cursor) ([]F, error)
- type Cursor
- func (c *Cursor) ArrayLen() (int, error)
- func (c *Cursor) BigInt() (*big.Int, error)
- func (c *Cursor) Bool() (bool, error)
- func (c *Cursor) Bytes() ([]byte, error)
- func (c *Cursor) BytesBorrowed() ([]byte, error)
- func (c *Cursor) BytesInto(dst []byte) error
- func (c *Cursor) Int64() (int64, error)
- func (c *Cursor) Key() (key []byte, isText bool, err error)
- func (c *Cursor) Leave()
- func (c *Cursor) Len() int
- func (c *Cursor) Limbs(dst *[Limbs]uint64) error
- func (c *Cursor) MapLen() (int, error)
- func (c *Cursor) NextField(s *Struct) (int, error)
- func (c *Cursor) NextWanted(s *Struct) (int, error)
- func (c *Cursor) Null() bool
- func (c *Cursor) OpenStruct(fields Fields) (Struct, error)
- func (c *Cursor) Peek() (byte, error)
- func (c *Cursor) Pos() int
- func (c *Cursor) Remaining() []byte
- func (c *Cursor) Scratch() unsafe.Pointer
- func (c *Cursor) Skip() error
- func (c *Cursor) String() (string, error)
- func (c *Cursor) Tag() (uint64, error)
- func (c *Cursor) Uint64() (uint64, error)
- type Fields
- type Struct
Constants ¶
const ( MajorUint = 0 << 5 MajorTag = 6 << 5 )
Major types (RFC 8949, section 3.1); only two are exported.
const ( Done = -1 Unknown = -2 )
Done is returned by NextField and NextWanted when the struct's map has no pairs left, and is closed. Unknown is returned by NextField when the key names no field, and its value is next, to skip.
const (
Limbs = 4
)
Limbs is the number of limbs in a felt, each a 64-bit value. feltHead is the head of its array, and feltLen the length of the uint64 form Juno writes, which is the array head and four limbs, each an eight-byte integer with its own head byte.
Variables ¶
Functions ¶
Types ¶
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
A Cursor walks a CBOR buffer one item at a time. It is not safe for concurrent use. depth is how many arrays and maps are open around the next item, and scratch is room for a value that is read and then copied, such as a map key.
func (*Cursor) ArrayLen ¶
ArrayLen reads an array head and returns its element count, capped by the bytes left. The caller must call Leave after reading the elements.
func (*Cursor) BigInt ¶
BigInt reads an integer of any width, either a plain integer or a tag 2 or 3 bignum.
func (*Cursor) Bytes ¶
Bytes reads a byte string into a new slice. An empty string gives an empty, non-nil slice.
func (*Cursor) BytesBorrowed ¶
BytesBorrowed is Bytes without the copy, so the result aliases the input.
func (*Cursor) Key ¶
Key reads a map key. A text key is checked as UTF-8 and returned as a sub-slice of the input; an integer key is consumed and reported as not text.
func (*Cursor) Leave ¶
func (c *Cursor) Leave()
Leave closes the array or map that ArrayLen or MapLen opened.
func (*Cursor) Limbs ¶
Limbs reads a felt, an array of four unsigned integers that are the limbs of its Montgomery form. dst is written only on success.
func (*Cursor) MapLen ¶
MapLen reads a map head and returns its pair count, capped by the bytes left. The caller must call Leave after reading the elements.
func (*Cursor) NextField ¶
NextField returns the field the next key names, Unknown if it names none, or Done once no pairs are left. A field named twice is ErrUnsupported. Stored maps are canonical, so the key expected next is compared first.
func (*Cursor) NextWanted ¶
NextWanted is NextField for a struct read in part. It skips the pairs of other keys, in any order, and the first of two keys naming a field wins.
func (*Cursor) Null ¶
Null consumes the next item if it is null or undefined, and reports whether it did.
func (*Cursor) OpenStruct ¶
OpenStruct reads the head of a struct's map. A null struct has no pairs.
func (*Cursor) Remaining ¶
Remaining returns the unread suffix of the input. It aliases the input buffer.
func (*Cursor) Scratch ¶
Scratch returns room for four words. Its contents are lost when the Cursor is reused.
func (*Cursor) Skip ¶
Skip consumes the next item and checks its syntax, but not UTF-8. Indefinite-length items and tags 0-3, whose content only a reader that returns the value can check, are ErrUnsupported. It consumes nothing on error.
type Fields ¶
type Fields struct {
// contains filtered or unexported fields
}
Fields matches the keys of a struct's map to field numbers, by an exact name first and then ignoring case. order holds each key as encoded, in canonical order (shorter first, then bytewise), names the wire name of each field by number, and all has the bit of every field set. The zero Fields has no fields.