encoding

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 12 Imported by: 19

Documentation

Index

Constants

View Source
const (
	ParamShaNameConvention  = "params-sha256"
	DigestShaNameConvention = "sha256digest"
)

Variables

View Source
var ErrBufferOverflow = errors.New("buffer overflow when parsing. One of the TLV Length is wrong")
View Source
var ErrIncorrectDigest = errors.New("the sha256 digest is missing or incorrect")

Functions

func IsAlphabet added in v0.0.2

func IsAlphabet(r rune) bool

Types

type Buffer

type Buffer []byte

Buffer is a buffer of bytes

func ShrinkLength

func ShrinkLength(buf Buffer, shrink int) Buffer

Shrink length reduce the L by `shrink“ in a TLV encoded buffer `buf`

Precondition:
  `buf` starts with proper Type and Length numbers.
  Length > `shrink`.
  May crash otherwise.

Returns the new buffer containing reduced TL header. May start from the middle of original buffer, but always goes to the end.

type BufferReader

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

func NewBufferReader

func NewBufferReader(buf Buffer) *BufferReader

func (*BufferReader) Delegate

func (r *BufferReader) Delegate(l int) ParseReader

func (*BufferReader) Length

func (r *BufferReader) Length() int

func (*BufferReader) Pos

func (r *BufferReader) Pos() int

func (*BufferReader) Range

func (r *BufferReader) Range(start, end int) Wire

func (*BufferReader) Read

func (r *BufferReader) Read(b []byte) (int, error)

func (*BufferReader) ReadBuf

func (r *BufferReader) ReadBuf(l int) (Buffer, error)

func (*BufferReader) ReadByte

func (r *BufferReader) ReadByte() (byte, error)

func (*BufferReader) ReadWire

func (r *BufferReader) ReadWire(l int) (Wire, error)

func (*BufferReader) Seek

func (r *BufferReader) Seek(offset int64, whence int) (int64, error)

func (*BufferReader) Skip

func (r *BufferReader) Skip(n int) error

func (*BufferReader) UnreadByte

func (r *BufferReader) UnreadByte() error

type Component

type Component struct {
	Typ TLNum
	Val []byte
}

func ComponentFromBytes

func ComponentFromBytes(buf []byte) (Component, error)

func ComponentFromStr

func ComponentFromStr(s string) (Component, error)

func NewByteOffsetComponent

func NewByteOffsetComponent(off uint64) Component

func NewBytesComponent

func NewBytesComponent(typ TLNum, val []byte) Component

func NewNumberComponent

func NewNumberComponent(typ TLNum, val uint64) Component

func NewSegmentComponent

func NewSegmentComponent(seg uint64) Component

func NewSequenceNumComponent

func NewSequenceNumComponent(seq uint64) Component

func NewStringComponent

func NewStringComponent(typ TLNum, val string) Component

func NewTimestampComponent

func NewTimestampComponent(t uint64) Component

func NewVersionComponent

func NewVersionComponent(v uint64) Component

func ParseComponent

func ParseComponent(buf Buffer) (Component, int)

func ReadComponent

func ReadComponent(r ParseReader) (Component, error)

func (Component) Bytes

func (c Component) Bytes() []byte

func (Component) CanonicalString added in v0.0.2

func (c Component) CanonicalString() string

func (Component) Clone added in v0.0.8

func (c Component) Clone() Component

func (Component) Compare

func (c Component) Compare(rhs ComponentPattern) int

func (Component) ComponentPatternTrait

func (c Component) ComponentPatternTrait() ComponentPattern

func (Component) EncodeInto

func (c Component) EncodeInto(buf Buffer) int

func (Component) EncodingLength

func (c Component) EncodingLength() int

func (Component) Equal

func (c Component) Equal(rhs ComponentPattern) bool

func (Component) FromMatching

func (c Component) FromMatching(m Matching) (*Component, error)

func (Component) Hash added in v0.0.2

func (c Component) Hash() uint64

Hash returns the hash of the component

func (Component) HashInto added in v0.0.3

func (c Component) HashInto(h hash.Hash)

HashInto hashes the current component into the hasher

func (Component) IsMatch

func (c Component) IsMatch(value Component) bool

func (Component) Length

func (c Component) Length() TLNum

func (Component) Match

func (Component) Match(value Component, m Matching)

func (Component) NumberVal added in v0.0.2

func (c Component) NumberVal() uint64

NumberVal returns the value of the component as a number

func (Component) String

func (c Component) String() string

type ComponentPattern

type ComponentPattern interface {
	// ComponentPatternTrait returns the type trait of Component or Pattern
	// This is used to make ComponentPattern a union type of Component or Pattern
	// Component | Pattern does not work because we need a mixed list NamePattern
	ComponentPatternTrait() ComponentPattern

	// String returns the string of the component, with naming conventions.
	// Since naming conventions are not standardized, this should not be used for purposes other than logging.
	// please use CanonicalString() for stable string representation.
	String() string

	// CanonicalString returns the string representation of the component without naming conventions.
	CanonicalString() string

	// Compare returns an integer comparing two components lexicographically.
	// It compares the type number first, and then its value.
	// A component is always less than a pattern.
	// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
	Compare(ComponentPattern) int

	// Equal returns the two components/patterns are the same.
	Equal(ComponentPattern) bool

	// IsMatch returns if the Component value matches with the current component/pattern.
	IsMatch(value Component) bool

	// Match matches the current pattern/component with the value, and put the matching into the Matching map.
	Match(value Component, m Matching)

	// FromMatching initiates the pattern from the Matching map.
	FromMatching(m Matching) (*Component, error)
}

func ComponentPatternFromStr

func ComponentPatternFromStr(s string) (ComponentPattern, error)

type ErrFailToParse

type ErrFailToParse struct {
	TypeNum TLNum
	Err     error
}

func (ErrFailToParse) Error

func (e ErrFailToParse) Error() string

func (ErrFailToParse) Unwrap

func (e ErrFailToParse) Unwrap() error

type ErrFormat

type ErrFormat struct {
	Msg string
}

func (ErrFormat) Error

func (e ErrFormat) Error() string

type ErrIncompatibleType

type ErrIncompatibleType struct {
	Name    string
	ValType string
	TypeNum TLNum
	Value   any
}

func (ErrIncompatibleType) Error

func (e ErrIncompatibleType) Error() string

type ErrNotFound

type ErrNotFound struct {
	Key string
}

func (ErrNotFound) Error

func (e ErrNotFound) Error() string

type ErrSkipRequired

type ErrSkipRequired struct {
	Name    string
	TypeNum TLNum
}

func (ErrSkipRequired) Error

func (e ErrSkipRequired) Error() string

type ErrUnexpected

type ErrUnexpected struct {
	Err error
}

func (ErrUnexpected) Error

func (e ErrUnexpected) Error() string

func (ErrUnexpected) Unwrap

func (e ErrUnexpected) Unwrap() error

type ErrUnrecognizedField

type ErrUnrecognizedField struct {
	TypeNum TLNum
}

func (ErrUnrecognizedField) Error

func (e ErrUnrecognizedField) Error() string

type Matching

type Matching map[string][]byte

type Name

type Name []Component

func NameFromBytes

func NameFromBytes(buf []byte) (Name, error)

func NameFromStr

func NameFromStr(s string) (Name, error)

func ReadName

func ReadName(r ParseReader) (Name, error)

ReadName reads a Name from a Wire **excluding** the TL prefix.

func (Name) Bytes

func (n Name) Bytes() []byte

Bytes returns the encoded bytes of a Name

func (Name) Clone added in v0.0.8

func (n Name) Clone() Name

Clone returns a deep copy of a Name

func (Name) Compare

func (n Name) Compare(rhs Name) int

func (Name) EncodeInto

func (n Name) EncodeInto(buf Buffer) int

EncodeInto encodes a Name into a Buffer **excluding** the TL prefix. Please use Bytes() to get the fully encoded name.

func (Name) EncodingLength

func (n Name) EncodingLength() int

EncodingLength computes a Name's length after encoding **excluding** the TL prefix.

func (Name) Equal

func (n Name) Equal(rhs Name) bool

func (Name) Hash added in v0.0.2

func (n Name) Hash() uint64

Hash returns the hash of the name

func (Name) IsPrefix

func (n Name) IsPrefix(rhs Name) bool

func (Name) PrefixHash added in v0.0.2

func (n Name) PrefixHash() []uint64

PrefixHash returns the hash value of all prefixes of the name ret[n] means the hash of the prefix of length n. ret[0] is the same for all names.

func (Name) String

func (n Name) String() string

func (Name) ToFullName

func (n Name) ToFullName(rawData Wire) Name

type NamePattern

type NamePattern []ComponentPattern

func NamePatternFromStr

func NamePatternFromStr(s string) (NamePattern, error)

func (NamePattern) Compare

func (n NamePattern) Compare(rhs NamePattern) int

func (NamePattern) Equal

func (n NamePattern) Equal(rhs NamePattern) bool

func (NamePattern) FromMatching

func (n NamePattern) FromMatching(m Matching) (Name, error)

func (NamePattern) IsPrefix

func (n NamePattern) IsPrefix(rhs NamePattern) bool

func (NamePattern) Match

func (n NamePattern) Match(name Name, m Matching)

func (NamePattern) String

func (n NamePattern) String() string

type Nat

type Nat uint64

Nat is a TLV natural number

func ParseNat

func ParseNat(buf Buffer) (val Nat, pos int, err error)

func (Nat) Bytes

func (v Nat) Bytes() []byte

func (Nat) EncodeInto

func (v Nat) EncodeInto(buf Buffer) int

func (Nat) EncodingLength

func (v Nat) EncodingLength() int

type ParseReader

type ParseReader interface {
	io.Reader
	io.ByteScanner

	// ReadWire reads a list of buffers in place without copy.
	// It always tries to read the required length of bytes.
	ReadWire(l int) (Wire, error)

	// ReadBuf reads a continuous buffer, trying to avoid copy.
	ReadBuf(l int) (Buffer, error)

	// Range returns a wire that contains the bytes between start and end, without copy.
	Range(start, end int) Wire

	// Pos returns the current position in the buffer/wire.
	Pos() int

	// Length returns the length of the buffer/wire.
	Length() int

	// Skip skips the next n bytes.
	Skip(n int) error

	// Delegate returns a new ParseReader that starts from the current position with length l.
	// The result is equivalent to the following:
	//
	//   start := r.Pos()
	//   r.Skip(l)
	//   return r.Range(start, r.Pos())
	Delegate(l int) ParseReader
}

ParseReader is an interface operating on Buffer and Wire

type Pattern

type Pattern struct {
	Typ TLNum
	Tag string
}

func (Pattern) CanonicalString added in v0.0.2

func (p Pattern) CanonicalString() string

func (Pattern) Compare

func (p Pattern) Compare(rhs ComponentPattern) int

func (Pattern) ComponentPatternTrait

func (p Pattern) ComponentPatternTrait() ComponentPattern

func (Pattern) Equal

func (p Pattern) Equal(rhs ComponentPattern) bool

func (Pattern) FromMatching

func (p Pattern) FromMatching(m Matching) (*Component, error)

func (Pattern) IsMatch

func (p Pattern) IsMatch(value Component) bool

func (Pattern) Match

func (p Pattern) Match(value Component, m Matching)

func (Pattern) String

func (p Pattern) String() string

type PlaceHolder

type PlaceHolder struct{}

PlaceHolder is an empty structure that used to give names of procedure arguments.

type TLNum

type TLNum uint64

TLNum is a TLV Type or Length number

const (
	TypeInvalidComponent                TLNum = 0x00
	TypeImplicitSha256DigestComponent   TLNum = 0x01
	TypeParametersSha256DigestComponent TLNum = 0x02
	TypeGenericNameComponent            TLNum = 0x08
	TypeKeywordNameComponent            TLNum = 0x20
	TypeSegmentNameComponent            TLNum = 0x32
	TypeByteOffsetNameComponent         TLNum = 0x34
	TypeVersionNameComponent            TLNum = 0x36
	TypeTimestampNameComponent          TLNum = 0x38
	TypeSequenceNumNameComponent        TLNum = 0x3a
)
const TypeName TLNum = 0x07

func ParseTLNum

func ParseTLNum(buf Buffer) (val TLNum, pos int)

ParseTLNum parses a TLNum from a buffer. It is supposed to be used internally, so panic on index out of bounds.

func ReadTLNum

func ReadTLNum(r io.ByteReader) (val TLNum, err error)

ReadTLNum reads a TLNum from a io.Reader.

func (TLNum) EncodeInto

func (v TLNum) EncodeInto(buf Buffer) int

func (TLNum) EncodingLength

func (v TLNum) EncodingLength() int

type Wire

type Wire []Buffer

Wire is a collection of Buffer. May be allocated in non-contiguous memory.

func (Wire) Join

func (w Wire) Join() []byte

func (Wire) Length

func (w Wire) Length() uint64

type WireReader

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

WireReader is used for reading from a Wire. It is used when parsing a fragmented packet.

func NewWireReader

func NewWireReader(w Wire) *WireReader

func (*WireReader) Delegate

func (r *WireReader) Delegate(l int) ParseReader

func (*WireReader) Length

func (r *WireReader) Length() int

func (*WireReader) Pos

func (r *WireReader) Pos() int

func (*WireReader) Range

func (r *WireReader) Range(start, end int) Wire

func (*WireReader) Read

func (r *WireReader) Read(b []byte) (int, error)

func (*WireReader) ReadBuf

func (r *WireReader) ReadBuf(l int) (Buffer, error)

func (*WireReader) ReadByte

func (r *WireReader) ReadByte() (byte, error)

func (*WireReader) ReadWire

func (r *WireReader) ReadWire(l int) (Wire, error)

func (*WireReader) Skip

func (r *WireReader) Skip(n int) error

func (*WireReader) UnreadByte

func (r *WireReader) UnreadByte() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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