tlv

package
v0.0.0-...-9dd16ae Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const MaxRecordSize = 65535 // 65KB

MaxRecordSize is the maximum size of a particular record that will be parsed by a stream decoder. This value is currently chosen to the be equal to the maximum message size permitted by BOLT 1, as no record should be bigger than an entire message.

Variables

View Source
var ErrRecordTooLarge = errors.New("record is too large")

ErrRecordTooLarge signals that a decoded record has a length that is too long to parse.

View Source
var ErrStreamNotCanonical = errors.New("tlv stream is not canonical")

ErrStreamNotCanonical signals that a decoded stream does not contain records sorting by monotonically-increasing type.

View Source
var ErrTUintNotMinimal = errors.New("truncated uint not minimally encoded")

ErrTUintNotMinimal signals that decoding a truncated uint failed because the value was not minimally encoded.

View Source
var ErrVarIntNotCanonical = errors.New("decoded varint is not canonical")

ErrVarIntNotCanonical signals that the decoded varint was not minimally encoded.

Functions

func DBytes32

func DBytes32(r io.Reader, val interface{}, _ *[8]byte, l uint64) error

DBytes32 is a Decoder for 32-byte arrays. An error is returned if val is not a *[32]byte.

func DBytes33

func DBytes33(r io.Reader, val interface{}, _ *[8]byte, l uint64) error

DBytes33 is a Decoder for 33-byte arrays. An error is returned if val is not a *[33]byte.

func DBytes64

func DBytes64(r io.Reader, val interface{}, _ *[8]byte, l uint64) error

DBytes64 is an Decoder for 64-byte arrays. An error is returned if val is not a *[64]byte.

func DNOP

func DNOP(io.Reader, interface{}, *[8]byte, uint64) error

DNOP is an encoder that doesn't modify the io.Reader and never fails.

func DPubKey

func DPubKey(r io.Reader, val interface{}, _ *[8]byte, l uint64) error

DPubKey is a Decoder for *btcec.PublicKey values. An error is returned if val is not a **btcec.PublicKey.

func DTUint16

func DTUint16(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DTUint16 is an Decoder for truncated uint16 values, where leading zeros will be resurrected. An error is returned if val is not a *uint16.

func DTUint32

func DTUint32(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DTUint32 is an Decoder for truncated uint32 values, where leading zeros will be resurrected. An error is returned if val is not a *uint32.

func DTUint64

func DTUint64(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DTUint64 is an Decoder for truncated uint64 values, where leading zeros will be resurrected. An error is returned if val is not a *uint64.

func DUint16

func DUint16(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DUint16 is a Decoder for uint16 values. An error is returned if val is not a *uint16.

func DUint32

func DUint32(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DUint32 is a Decoder for uint32 values. An error is returned if val is not a *uint32.

func DUint64

func DUint64(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DUint64 is a Decoder for uint64 values. An error is returned if val is not a *uint64.

func DUint8

func DUint8(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

DUint8 is a Decoder for uint8 values. An error is returned if val is not a *uint8.

func DVarBytes

func DVarBytes(r io.Reader, val interface{}, _ *[8]byte, l uint64) error

DVarBytes is a Decoder for variable byte slices. An error is returned if val is not *[]byte.

func EBytes32

func EBytes32(w io.Writer, val interface{}, _ *[8]byte) error

EBytes32 is an Encoder for 32-byte arrays. An error is returned if val is not a *[32]byte.

func EBytes33

func EBytes33(w io.Writer, val interface{}, _ *[8]byte) error

EBytes33 is an Encoder for 33-byte arrays. An error is returned if val is not a *[33]byte.

func EBytes64

func EBytes64(w io.Writer, val interface{}, _ *[8]byte) error

EBytes64 is an Encoder for 64-byte arrays. An error is returned if val is not a *[64]byte.

func ENOP

func ENOP(io.Writer, interface{}, *[8]byte) error

ENOP is an encoder that doesn't modify the io.Writer and never fails.

func EPubKey

func EPubKey(w io.Writer, val interface{}, _ *[8]byte) error

EPubKey is an Encoder for *btcec.PublicKey values. An error is returned if val is not a **btcec.PublicKey.

func ETUint16

func ETUint16(w io.Writer, val interface{}, buf *[8]byte) error

ETUint16 is an Encoder for truncated uint16 values, where leading zeros will be omitted. An error is returned if val is not a *uint16.

func ETUint16T

func ETUint16T(w io.Writer, val uint16, buf *[8]byte) error

ETUint16T is an Encoder for truncated uint16 values, where leading zeros will be omitted. An error is returned if val is not a *uint16.

func ETUint32

func ETUint32(w io.Writer, val interface{}, buf *[8]byte) error

ETUint32 is an Encoder for truncated uint32 values, where leading zeros will be omitted. An error is returned if val is not a *uint32.

func ETUint32T

func ETUint32T(w io.Writer, val uint32, buf *[8]byte) error

ETUint32T is an Encoder for truncated uint32 values, where leading zeros will be omitted. An error is returned if val is not a *uint32.

func ETUint64

func ETUint64(w io.Writer, val interface{}, buf *[8]byte) error

ETUint64 is an Encoder for truncated uint64 values, where leading zeros will be omitted. An error is returned if val is not a *uint64.

func ETUint64T

func ETUint64T(w io.Writer, val uint64, buf *[8]byte) error

ETUint64T is an Encoder for truncated uint64 values, where leading zeros will be omitted. An error is returned if val is not a *uint64.

func EUint16

func EUint16(w io.Writer, val interface{}, buf *[8]byte) error

EUint16 is an Encoder for uint16 values. An error is returned if val is not a *uint16.

func EUint16T

func EUint16T(w io.Writer, val uint16, buf *[8]byte) error

EUint16T encodes a uint16 val to the provided io.Writer. This method is exposed so that encodings for custom uint16-like types can be created without incurring an extra heap allocation.

func EUint32

func EUint32(w io.Writer, val interface{}, buf *[8]byte) error

EUint32 is an Encoder for uint32 values. An error is returned if val is not a *uint32.

func EUint32T

func EUint32T(w io.Writer, val uint32, buf *[8]byte) error

EUint32T encodes a uint32 val to the provided io.Writer. This method is exposed so that encodings for custom uint32-like types can be created without incurring an extra heap allocation.

func EUint64

func EUint64(w io.Writer, val interface{}, buf *[8]byte) error

EUint64 is an Encoder for uint64 values. An error is returned if val is not a *uint64.

func EUint64T

func EUint64T(w io.Writer, val uint64, buf *[8]byte) error

EUint64T encodes a uint64 val to the provided io.Writer. This method is exposed so that encodings for custom uint64-like types can be created without incurring an extra heap allocation.

func EUint8

func EUint8(w io.Writer, val interface{}, buf *[8]byte) error

EUint8 is an Encoder for uint8 values. An error is returned if val is not a *uint8.

func EUint8T

func EUint8T(w io.Writer, val uint8, buf *[8]byte) error

EUint8T encodes a uint8 val to the provided io.Writer. This method is exposed so that encodings for custom uint8-like types can be created without incurring an extra heap allocation.

func EVarBytes

func EVarBytes(w io.Writer, val interface{}, _ *[8]byte) error

EVarBytes is an Encoder for variable byte slices. An error is returned if val is not *[]byte.

func ReadVarInt

func ReadVarInt(r io.Reader, buf *[8]byte) (uint64, error)

ReadVarInt reads a variable length integer from r and returns it as a uint64.

func RecordsToMap

func RecordsToMap(records []Record) (map[uint64][]byte, error)

RecordsToMap encodes a series of TLV records as raw key-value pairs in the form of a map.

func SizeTUint16

func SizeTUint16(v uint16) uint64

SizeTUint16 returns the number of bytes remaining in a uint16 after truncating the leading zeros.

func SizeTUint32

func SizeTUint32(v uint32) uint64

SizeTUint32 returns the number of bytes remaining in a uint32 after truncating the leading zeros.

func SizeTUint64

func SizeTUint64(v uint64) uint64

SizeTUint64 returns the number of bytes remaining in a uint64 after truncating the leading zeros.

func SortRecords

func SortRecords(records []Record)

SortRecords is a helper function that will sort a slice of records in place according to their type.

func VarIntSize

func VarIntSize(val uint64) uint64

VarIntSize returns the required number of bytes to encode a var int.

func WriteVarInt

func WriteVarInt(w io.Writer, val uint64, buf *[8]byte) error

WriteVarInt serializes val to w using a variable number of bytes depending on its value.

Types

type Decoder

type Decoder func(r io.Reader, val interface{}, buf *[8]byte, l uint64) error

Decoder is a signature for methods that can decode TLV values. An error should be returned if the Decoder cannot support the underlying type of val. The provided scratch buffer must be non-nil.

type Encoder

type Encoder func(w io.Writer, val interface{}, buf *[8]byte) error

Encoder is a signature for methods that can encode TLV values. An error should be returned if the Encoder cannot support the underlying type of val. The provided scratch buffer must be non-nil.

func StubEncoder

func StubEncoder(v []byte) Encoder

StubEncoder is a factory function that makes a stub tlv.Encoder out of a raw value. We can use this to make a record that can be encoded when we don't actually know it's true underlying value, and only it serialization.

type ErrTypeForDecoding

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

ErrTypeForDecoding signals that an incorrect type was passed to a Decoder or that the expected length of the encoding is different from that required by the expected type.

func NewTypeForDecodingErr

func NewTypeForDecodingErr(val interface{}, expType string,
	valLength, expLength uint64) ErrTypeForDecoding

NewTypeForDecodingErr creates a new ErrTypeForDecoding given the incorrect val and expected type, or the mismatch in their expected lengths.

func (ErrTypeForDecoding) Error

func (e ErrTypeForDecoding) Error() string

Error returns a human-readable description of the type mismatch.

type ErrTypeForEncoding

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

ErrTypeForEncoding signals that an incorrect type was passed to an Encoder.

func NewTypeForEncodingErr

func NewTypeForEncodingErr(val interface{}, expType string) ErrTypeForEncoding

NewTypeForEncodingErr creates a new ErrTypeForEncoding given the incorrect val and the expected type.

func (ErrTypeForEncoding) Error

func (e ErrTypeForEncoding) Error() string

Error returns a human-readable description of the type mismatch.

type Record

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

Record holds the required information to encode or decode a TLV record.

func MakeDynamicRecord

func MakeDynamicRecord(typ Type, val interface{}, sizeFunc SizeFunc,
	encoder Encoder, decoder Decoder) Record

MakeDynamicRecord creates a record whose size may vary, and will be determined at the time of encoding via sizeFunc.

func MakePrimitiveRecord

func MakePrimitiveRecord(typ Type, val interface{}) Record

MakePrimitiveRecord creates a record for common types.

func MakeStaticRecord

func MakeStaticRecord(typ Type, val interface{}, size uint64, encoder Encoder,
	decoder Decoder) Record

MakeStaticRecord creates a record for a field of fixed-size

func MapToRecords

func MapToRecords(tlvMap map[uint64][]byte) []Record

MapToRecords encodes the passed TLV map as a series of regular tlv.Record instances. The resulting set of records will be returned in sorted order by their type.

func (*Record) Decode

func (f *Record) Decode(r io.Reader, l uint64) error

Decode read in the TLV record from the passed reader. This is useful when a caller wants decode a *single* TLV record, outside the context of the Stream struct.

func (*Record) Encode

func (f *Record) Encode(w io.Writer) error

Encode writes out the TLV record to the passed writer. This is useful when a caller wants to obtain the raw encoding of a *single* TLV record, outside the context of the Stream struct.

func (*Record) Size

func (f *Record) Size() uint64

Size returns the size of the Record's value. If no static size is known, the dynamic size will be evaluated.

func (*Record) Type

func (f *Record) Type() Type

Type returns the type of the underlying TLV record.

type RecordProducer

type RecordProducer interface {
	// Record returns a Record that can be used to encode or decode the
	// backing object.
	Record() Record
}

RecorderProducer is an interface for objects that can produce a Record object capable of encoding and/or decoding the RecordProducer as a Record.

type SizeFunc

type SizeFunc func() uint64

SizeFunc is a function that can compute the length of a given field. Since the size of the underlying field can change, this allows the size of the field to be evaluated at the time of encoding.

func SizeVarBytes

func SizeVarBytes(e *[]byte) SizeFunc

SizeVarBytes returns a SizeFunc that can compute the length of a byte slice.

type Stream

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

Stream defines a TLV stream that can be used for encoding or decoding a set of TLV Records.

func MustNewStream

func MustNewStream(records ...Record) *Stream

MustNewStream creates a new TLV Stream given an encoding codec, a decoding codec, and a set of known records. If an error is encountered in creating the stream, this method will panic instead of returning the error.

func NewStream

func NewStream(records ...Record) (*Stream, error)

NewStream creates a new TLV Stream given an encoding codec, a decoding codec, and a set of known records.

func (*Stream) Decode

func (s *Stream) Decode(r io.Reader) error

Decode deserializes TLV Stream from the passed io.Reader. The Stream will inspect each record that is parsed and check to see if it has a corresponding Record to facilitate deserialization of that field. If the record is unknown, the Stream will discard the record's bytes and proceed to the subsequent record.

Each record has the following format:

[varint: type]
[varint: length]
[length: value]

A series of (possibly zero) records are concatenated into a stream, this example contains two records:

(t: 0x01, l: 0x04, v: 0xff, 0xff, 0xff, 0xff)
(t: 0x02, l: 0x01, v: 0x01)

This method asserts that the byte stream is canonical, namely that each record is unique and that all records are sorted in ascending order. An ErrNotCanonicalStream error is returned if the encoded TLV stream is not.

We permit an io.EOF error only when reading the type byte which signals that the last record was read cleanly and we should stop parsing. All other io.EOF or io.ErrUnexpectedEOF errors are returned.

func (*Stream) DecodeWithParsedTypes

func (s *Stream) DecodeWithParsedTypes(r io.Reader) (TypeMap, error)

DecodeWithParsedTypes is identical to Decode, but if successful, returns a TypeMap containing the types of all records that were decoded or ignored from the stream.

func (*Stream) Encode

func (s *Stream) Encode(w io.Writer) error

Encode writes a Stream to the passed io.Writer. Each of the Records known to the Stream is written in ascending order of their type so as to be canonical.

The stream is constructed by concatenating the individual, serialized Records where each record has the following format:

[varint: type]
[varint: length]
[length: value]

An error is returned if the io.Writer fails to accept bytes from the encoding, and nothing else. The ordering of the Records is asserted upon the creation of a Stream, and thus the output will be by definition canonical.

type Type

type Type uint64

Type is an 64-bit identifier for a TLV Record.

type TypeMap

type TypeMap map[Type][]byte

TypeMap is a map of parsed Types. The map values are byte slices. If the byte slice is nil, the type was successfully parsed. Otherwise the value is byte slice containing the encoded data.

Jump to

Keyboard shortcuts

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