ttlv

package
v0.0.0-...-7e9f181 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package ttlv implements the TTLV serialization and deserialization as defined in the Oasis KMIP 1.4 specification, section 9.1. It also supports other encoding format like

This package is the low level part of KMIP protocol serialization and should mostly never be used directly except to extend the kmip protocol implementation or to perform testing or optimizing stuff.

Index

Constants

This section is empty.

Variables

View Source
var ErrEOF = Errorf("unexpected end of data")

ErrEOF is an ErrEncoding instance returned whensome data are missing while decoding.

Functions

func BitmaskStr

func BitmaskStr[T ~int32](value T, sep string) string

BitmaskStr returns the string representation of a bitmask value, consisting of a concatenation of all the flags values separated by `sep`. If it's known, the flag string value is the normalized name, otherwise it's the 0x prefixed hex value.

func CompareVersions

func CompareVersions[A, B Version](a A, b B) int

CompareVersions compare 2 protocol version and returns

-1 if a is less than b,
 0 if a equals b,
+1 if a is greater than b.

func EnumStr

func EnumStr[T ~uint32](value T) string

EnumStr returns the string representation of an enum. If it's known, the string is the normalized name, otherwise it's the 0x prefixed hex value.

func EnumValues

func EnumValues[T ~uint32]() iter.Seq2[T, string]

EnumValues returns an iterator over registered enum values and names for the given type T. If T is not a known enum, then the iterator will yield an empty result set.

func Errorf

func Errorf(format string, args ...any) error

Errorf creates a new ErrEncoding.

func IsErrEncoding

func IsErrEncoding(err error) bool

IsErrEncoding returns true if err is of type or wraps and error of type ErrEncoding.

func MarshalJSON

func MarshalJSON(data any) []byte

MarshalJSON serializes `data` into a json TTLV encoded byte string.

func MarshalTTLV

func MarshalTTLV(data any) []byte

MarshalTTLV serializes `data` into a binary TTLV encoded byte array.

func MarshalText

func MarshalText(data any) []byte

MarshalText serializes `data` into a textual and human-friendly form of TTLV. Useful mainly for debugging.

func MarshalXML

func MarshalXML(data any) []byte

MarshalXML serializes `data` into an xml TTLV encoded byte string.

func RegisterBitmask

func RegisterBitmask[T ~int32](tag int, names ...string)

RegisterBitmask registers a bitmask types with it's tag and string values.

func RegisterEnum

func RegisterEnum[T ~uint32](tag int, names map[T]string)

RegisterEnum registers an enum tag with its tag and all its string values.

func RegisterTag

func RegisterTag(name string, value int, ty ...reflect.Type)

RegisterTag registers a new named tag and save the mapping between name and interger value for later usage by encoder and decoders. An optional list of types can be provided to also register a mapping between the types and the default tag used for serializing/deserializing them.

func TagString

func TagString(tag int) string

func UnmarshalJSON

func UnmarshalJSON(data []byte, ptr any) error

UnmarshalJSON deserializes the json TTLV byte string into the object pointed by `ptr`.

`ptr` must be a pointer.

func UnmarshalTTLV

func UnmarshalTTLV(data []byte, ptr any) error

UnmarshalTTLV deserializes the binary TTLV byte string into the object pointed by `ptr`.

`ptr` must be a pointer.

func UnmarshalXML

func UnmarshalXML(data []byte, ptr any) error

UnmarshalXML deserializes the xml TTLV byte string into the object pointed by `ptr`.

`ptr` must be a pointer.

Types

type Decodable

type Decodable interface {
	DecodeTTLV(d *Decoder) error
}

TODO: Do we still need that ?

type Decoder

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

Decoder exposes methods to read TTLV tagged values to an internal buffer. It supports multiple formats like binary TTLV or xml TTLV.

func NewJSONDecoder

func NewJSONDecoder(bytes []byte) (Decoder, error)

NewJSONDecoder create a new Decoder to decode values from the json TTLV format.

func NewTTLVDecoder

func NewTTLVDecoder(bytes []byte) (Decoder, error)

NewTTLVDecoder create a new Decoder to decode values from the binary TTLV format.

func NewXMLDecoder

func NewXMLDecoder(bytes []byte) (Decoder, error)

NewXMLDecoder create a new Decoder to decode values from the xml TTLV format.

func NewXMLFromDecoder

func NewXMLFromDecoder(dec *xml.Decoder) (Decoder, error)

NewXMLDecoder create a new Decoder to decode xml TTLV values from the given encoding/xml.Decoder.

func (*Decoder) Any

func (dec *Decoder) Any(value any) error

Any decodes `value` by deserializing it from the buffer using value's type default tag. It panics if no tag can be found for `value` or if value does not implement Encodable. `value` must be a pointer.

func (*Decoder) BigInteger

func (dec *Decoder) BigInteger(tag int) (*big.Int, error)

BigInteger decodes a big integer and advance to the next value.

func (*Decoder) Bitmask

func (dec *Decoder) Bitmask(realtag, tag int) (int32, error)

Bitmaks reads a bitmask value from the internal buffer. While `tag` is the tag to write with the value, which may differ from the bitmask's default tag, `realtag` can optionally be set to non-zero to identify the real default tag associated to the bitmask type. It's useful for deserializing the bitmask value from its text representation.

func (*Decoder) Bool

func (dec *Decoder) Bool(tag int) (bool, error)

Bool reads a boolean to the internal buffer.

func (*Decoder) ByteString

func (dec *Decoder) ByteString(tag int) ([]byte, error)

ByteString reads a byte array and advance to the next value.

func (*Decoder) DateTime

func (dec *Decoder) DateTime(tag int) (time.Time, error)

DateTime reads a date-time and advance to the next value.

func (*Decoder) Enum

func (dec *Decoder) Enum(realtag, tag int) (uint32, error)

Enum reads an enum from the internal buffer. While `tag` is the tag to write with the value, which may differ from the enum's default tag, `realtag` can optionally be set to non-zero to identify the real default tag associated to the enum type. It's useful for deserializing the enum value from its text representation.

func (*Decoder) Integer

func (dec *Decoder) Integer(tag int) (int32, error)

Integer decodes an integer and advance to the next value.

func (*Decoder) Interval

func (dec *Decoder) Interval(tag int) (time.Duration, error)

DateTime reads a duration and advance to the next value.

func (*Decoder) LongInteger

func (dec *Decoder) LongInteger(tag int) (int64, error)

LongInteger decodes a long integer and advance to the next value.

func (*Decoder) Next

func (dec *Decoder) Next() error

Next advances to the next TTLV value.

func (*Decoder) Opt

func (dec *Decoder) Opt(tag int, value any) error

Opt optionally decodes a value if the current tag match the one given in parameter. If not, the deserialization is skipped.

gotcha: When skipped, the passed value is not zeroed.

func (*Decoder) Struct

func (dec *Decoder) Struct(tag int, f func(*Decoder) error) error

Struct reads a structure to the internal buffer. It calls the provided callback `f` with a Decoder to use for reading struct's fields.

func (*Decoder) Tag

func (dec *Decoder) Tag() int

Tag returns the tag of the current TTLV value being decoded. It returns 0 in case of EOF.

func (*Decoder) TagAny

func (dec *Decoder) TagAny(tag int, value any) (err error)

TagAny decodes `value` by deserializing it from the buffer with the given tag instead of value's type default one. It panics if value's type cannot be encoded or if it's not a pointer.

func (*Decoder) TextString

func (dec *Decoder) TextString(tag int) (string, error)

TextString reads a string and advance to the next value.

func (*Decoder) Type

func (dec *Decoder) Type() Type

Tag returns the type of the current TTLV value being decoded. It returns 0 in case of EOF.

type Encodable

type Encodable interface {
	EncodeTTLV(e *Encoder)
}

TODO: Do we still need that ?

type Encoder

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

Encoder exposes methods to write TTLV tagged values to an internal buffer. It supports multiple formats like binary TTLV or xml TTLV.

func NewJSONEncoder

func NewJSONEncoder() Encoder

NewJSONEncoder create a new Encoder to encode values to the json TTLV format.

func NewTTLVEncoder

func NewTTLVEncoder() Encoder

NewTTLVEncoder create a new Encoder to encode values to the binary TTLV format.

func NewTextEncoder

func NewTextEncoder() Encoder

NewTextEncoder create a new Encoder to print TTLV values into a textual and human-friendly form. Mainly useful for debugging.

func NewXMLEncoder

func NewXMLEncoder() Encoder

NewXMLEncoder create a new Encoder to encode values to the xml TTLV format.

func (*Encoder) Any

func (enc *Encoder) Any(value any)

Any encodes `value` and writes it to the internal buffer using value's type default tag. It panics if no tag can be found for `value` or if value does not implement Encodable.

func (*Encoder) BigInteger

func (enc *Encoder) BigInteger(tag int, value *big.Int)

BigInteger writes a big integer to the internal buffer.

func (*Encoder) Bitmask

func (enc *Encoder) Bitmask(realtag, tag int, value int32)

Bitmaks writes a bitmask value to the internal buffer. While `tag` is the tag to write with the value, which may differ from the bitmask's default tag, `realtag` can optionally be set to non-zero to identify the real default tag associated to the bitmask type. It's useful for serializing the bitmask value to its text representation.

func (*Encoder) Bool

func (enc *Encoder) Bool(tag int, value bool)

Bool writes a boolean to the internal buffer.

func (*Encoder) ByteString

func (enc *Encoder) ByteString(tag int, str []byte)

ByteString writes a byte array to the internal buffer.

func (*Encoder) Bytes

func (enc *Encoder) Bytes() []byte

Bytes returns the internal byte array holding the encoded content.

func (*Encoder) Clear

func (enc *Encoder) Clear()

Clear clears the internal buffer without deallocating it, making the encoder reusable for encoding another value.

func (*Encoder) DateTime

func (enc *Encoder) DateTime(tag int, date time.Time)

DateTime writes a date-time value to the internal buffer.

func (*Encoder) Enum

func (enc *Encoder) Enum(realtag, tag int, value uint32)

Enum writes an enum to the internal buffer. While `tag` is the tag to write with the value, which may differ from the enum's default tag, `realtag` can optionally be set to non-zero to identify the real default tag associated to the enum type. It's useful for serializing the enum value to its text representation.

func (*Encoder) Integer

func (enc *Encoder) Integer(tag int, value int32)

Integer writes an integer to the internal buffer.

func (*Encoder) Interval

func (enc *Encoder) Interval(tag int, interval time.Duration)

Interval writes a duration to the internal buffer.

func (*Encoder) LongInteger

func (enc *Encoder) LongInteger(tag int, value int64)

LongInteger writes a long integer to the internal buffer.

func (*Encoder) Struct

func (enc *Encoder) Struct(tag int, f func(*Encoder))

Struct writes a structure to the internal buffer. It calls the provided callback `f` with an Encoder to use for writing struct's fields.

func (*Encoder) TagAny

func (enc *Encoder) TagAny(tag int, value any)

TagAny encodes `value` and writes it to the internal buffer with the given tag instead of value's type default one. It panics if value's type cannot be encoded.

func (*Encoder) TextString

func (enc *Encoder) TextString(tag int, str string)

TextString writes a string to the internal buffer.

type Enum

type Enum uint32

Enum is a generic TTLV enum value.

type ErrEncoding

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

ErrEncoding is the error type returned when decoding data fails.

func (ErrEncoding) Error

func (err ErrEncoding) Error() string

func (ErrEncoding) Unwrap

func (err ErrEncoding) Unwrap() error

type Stream

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

Stream is a helper type to wrap io.ReadWrite stream to serialize and deserialize binary TTLV encoded golang types to / from the stream.

func NewStream

func NewStream(inner io.ReadWriteCloser, maxSize int) Stream

NewStream creates a new TTLV stream around the given I/O stream. If maxSize is greater than 0, then it will limit the maximum allowed size in bytes for a message to receive.

func (*Stream) Close

func (s *Stream) Close() error

Close wloses the inner stream.

func (*Stream) Recv

func (s *Stream) Recv(msg any) error

Recv reads the next TTLV binary payload from the inner stream, then deserialize it into the value pointed by `msg`. Note that `msg` must be a pointer.

func (*Stream) Roundtrip

func (s *Stream) Roundtrip(req, resp any) error

Roundtrip simply perform a Send() followed by a Recv(), sending `req` then receiving `resp`.

func (*Stream) Send

func (s *Stream) Send(msg any) error

Send serializes to TTLV binary the given `msg` then writes it to the inner stream.

type Struct

type Struct []Value

Struct is a generic TTLV struct container.

func (*Struct) TagDecodeTTLV

func (v *Struct) TagDecodeTTLV(d *Decoder, tag int) error

func (Struct) TagEncodeTTLV

func (v Struct) TagEncodeTTLV(e *Encoder, tag int)

type TagDecodable

type TagDecodable interface {
	TagDecodeTTLV(d *Decoder, tag int) error
}

TagEncodable is implemented by types implementing a custom deserialization logic, instead of relying on reflection.

type TagEncodable

type TagEncodable interface {
	TagEncodeTTLV(e *Encoder, tag int)
}

TagEncodable is implemented by types implementing a custom serialization logic, instead of relying on reflection.

type Type

type Type uint8

Type is a TTLV encoding type as defined in the KMIP 1.4 specification, section 9.1.1.2 for TTLV encoding.

const (
	TypeStructure Type = 0x01 + iota
	TypeInteger
	TypeLongInteger
	TypeBigInteger
	TypeEnumeration
	TypeBoolean
	TypeTextString
	TypeByteString
	TypeDateTime
	TypeInterval
)

func (Type) String

func (ty Type) String() string

type Value

type Value struct {
	// The value's TTLV tag.
	Tag int
	// The TTLV value.
	Value any
}

Value is a generic TTLV tagged value.

func (*Value) DecodeTTLV

func (v *Value) DecodeTTLV(d *Decoder) error

func (Value) EncodeTTLV

func (v Value) EncodeTTLV(e *Encoder)

func (*Value) TagDecodeTTLV

func (v *Value) TagDecodeTTLV(d *Decoder, tag int) error

func (Value) TagEncodeTTLV

func (v Value) TagEncodeTTLV(e *Encoder, tag int)

type Version

type Version interface {
	// Major returns the version major number
	Major() int
	// Minor returns the version minor number
	Minor() int
}

Version interface is implemented by types representing a protocol version when the protocol supports versioning like KMIP.

Jump to

Keyboard shortcuts

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