tlv

package
v0.0.0-...-1e60831 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: NIST-PD-fallback Imports: 6 Imported by: 2

Documentation

Overview

Package tlv implements NDN Type-Length-Value (TLV) encoding.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIncomplete = errors.New("incomplete input")
	ErrTail       = errors.New("junk after end of TLV")
	ErrType       = errors.New("TLV-TYPE out of range")
	ErrCritical   = errors.New("unrecognized critical TLV-TYPE")
	ErrRange      = errors.New("out of range")
	ErrErrorField = errors.New("Error(nil) field")
)

Error conditions.

Functions

func Decode

func Decode(wire []byte, u Unmarshaler) error

Decode unmarshals a buffer that contains a single TLV.

func Encode

func Encode(fields ...Field) (wire []byte, e error)

Encode encodes a sequence of Fields.

func EncodeFrom

func EncodeFrom(fields ...Fielder) (wire []byte, e error)

EncodeFrom encodes a sequence of Fielders.

func EncodeValueOnly

func EncodeValueOnly(f Fielder) ([]byte, error)

EncodeValueOnly returns TLV-VALUE of a Fielder created by TLV, TLVFrom, TLVBytes, or TLVNNI.

Types

type DecodingBuffer

type DecodingBuffer []byte

DecodingBuffer recognizes TLV elements.

func (DecodingBuffer) EOF

func (d DecodingBuffer) EOF() bool

EOF returns true if decoder is at end of input.

func (*DecodingBuffer) Element

func (d *DecodingBuffer) Element() (de DecodingElement, e error)

Element recognizes one TLV element from the buffer.

func (*DecodingBuffer) Elements

func (d *DecodingBuffer) Elements() (list []DecodingElement)

Elements recognizes TLV elements from the buffer. Bytes that cannot be recognized as TLV elements are left in the decoder.

func (DecodingBuffer) ErrUnlessEOF

func (d DecodingBuffer) ErrUnlessEOF() error

ErrUnlessEOF returns an error if there is unconsumed input.

func (DecodingBuffer) Rest

func (d DecodingBuffer) Rest() []byte

Rest returns unconsumed input.

type DecodingElement

type DecodingElement struct {
	Element
	Wire  []byte
	After []byte
}

DecodingElement represents an TLV element during decoding.

func (DecodingElement) IsCriticalType

func (de DecodingElement) IsCriticalType() bool

IsCriticalType returns true if the TLV-TYPE is considered critical for evolvability purpose.

func (DecodingElement) Unmarshal

func (de DecodingElement) Unmarshal(u Unmarshaler) error

Unmarshal unmarshals TLV into a value.

func (DecodingElement) UnmarshalNNI

func (de DecodingElement) UnmarshalNNI(max uint64, err *error, rangeErr error) (v uint64)

UnmarshalNNI unmarshals TLV-VALUE as NNI and checks it's within [0:max] range.

This function has an unusual set of parameters to allow for more compact calling code:

if pkt.Field = uint32(de.UnmarshalNNI(math.MaxUint32, &e, tlv.ErrRange)); e != nil {
  return e
}

func (DecodingElement) UnmarshalValue

func (de DecodingElement) UnmarshalValue(u encoding.BinaryUnmarshaler) error

UnmarshalValue unmarshals TLV-VALUE into a value.

func (DecodingElement) WireAfter

func (de DecodingElement) WireAfter() []byte

WireAfter returns Wire+After.

type Element

type Element struct {
	// Type is the TLV-TYPE.
	Type uint32
	// Value is the TLV-VALUE.
	Value []byte
}

Element represents a TLV element. Zero value is invalid.

func (Element) Field

func (element Element) Field() Field

Field implements Fielder interface.

func (Element) Length

func (element Element) Length() int

Length returns TLV-LENGTH.

func (Element) Size

func (element Element) Size() int

Size returns encoded size.

func (*Element) UnmarshalTLV

func (element *Element) UnmarshalTLV(typ uint32, value []byte) error

UnmarshalTLV implements Unmarshaler interface.

type EncodingBuffer

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

EncodingBuffer is an encoding buffer. Zero value is an empty buffer.

func (*EncodingBuffer) Append

func (eb *EncodingBuffer) Append(f Field)

Append appends a field. If there's an error, it is saved in the EncodingBuffer.

func (EncodingBuffer) Output

func (eb EncodingBuffer) Output() ([]byte, error)

Output returns encoding output and first error.

type Field

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

Field is an encodable field. Zero value encodes to nothing.

func Bytes

func Bytes(b []byte) Field

Bytes creates a Field that encodes to given bytes.

func FieldError

func FieldError(e error) Field

FieldError creates a Field that generates an error.

func FieldFunc

func FieldFunc(f func([]byte) ([]byte, error)) Field

FieldFunc creates a Field that calls a function to append to a slice.

func TLV

func TLV(typ uint32, values ...Field) Field

TLV creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE Fields.

func TLVBytes

func TLVBytes(typ uint32, value []byte) Field

TLVBytes creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE byte slice.

func TLVFrom

func TLVFrom(typ uint32, values ...Fielder) Field

TLVFrom creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE Fielders.

func TLVNNI

func TLVNNI[V constraints.Integer](typ uint32, v V) Field

TLVNNI creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE NonNegativeInteger.

func (Field) Encode

func (f Field) Encode(b []byte) ([]byte, error)

Encode appends to the byte slice. Returns modified slice and error.

func (Field) Field

func (f Field) Field() Field

Field implements Fielder interface.

type Fielder

type Fielder interface {
	Field() Field
}

Fielder is the interface implemented by an object that can encode itself to a Field.

type NNI

type NNI uint64

NNI is a non-negative integer.

func (NNI) Encode

func (n NNI) Encode(b []byte) []byte

Encode encodes this number, appending to buffer.

func (NNI) Field

func (n NNI) Field() Field

Field implements Fielder interface.

func (NNI) Size

func (n NNI) Size() int

Size returns the wire encoding size.

func (*NNI) UnmarshalBinary

func (n *NNI) UnmarshalBinary(wire []byte) error

UnmarshalBinary decodes this number.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalTLV(typ uint32, value []byte) error
}

Unmarshaler is the interface implemented by an object that can decode an TLV element representation of itself.

type VarNum

type VarNum uint64

VarNum represents a number in variable size encoding for TLV-TYPE or TLV-LENGTH.

func (*VarNum) Decode

func (n *VarNum) Decode(wire []byte) (rest []byte, e error)

Decode extracts a VarNum from the buffer.

func (VarNum) Encode

func (n VarNum) Encode(b []byte) []byte

Encode appends this number to a buffer.

func (VarNum) Size

func (n VarNum) Size() int

Size returns the wire encoding size.

Jump to

Keyboard shortcuts

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