amf0

package module
v0.0.0-...-c554739 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 8 Imported by: 30

README

amf0 GoDoc Build Status

General-purpose encoder and decoder for amf0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NotFoundError is returned when trying to get a key that doesn't
	// exist.
	NotFoundError = errors.New("Item not found in the object.")

	// WrongTypeError is returned when a key exists, but its type is not the
	// one requested.
	WrongTypeError = errors.New("Item not found in the object.")
)
View Source
var (
	// DefaultIdentifier is the default implementation of the Identifier
	// type. It holds knowledge of all implemented amf0 types in this
	// package.
	DefaultIdentifier = NewIdentifier(
		func() AmfType { return &Array{NewPaired()} },
		func() AmfType { return new(Null) },
		func() AmfType { return new(Undefined) },
		func() AmfType { return new(Bool) },
		func() AmfType { return new(Number) },
		func() AmfType { return &Object{NewPaired()} },
		func() AmfType { return new(String) },
		func() AmfType { return new(LongString) },
	)
)
View Source
var (
	ObjectEndSeq = []byte{0x00, 0x00, 0x09}
)

Functions

func EncodeToBytes

func EncodeToBytes(t AmfType) ([]byte, error)

EncodeToBytes uses the default Encoder to marshal the given AmfType `t` to a []byte, instead of writing to an io.Writer. Any error returned above will be returned here.

Types

type AmfType

type AmfType interface {
	// Decodes information for the type from the reader. This expects
	// the reader to return starting from the first byte _after_ the
	// type marker.
	Decode(io.Reader) error

	// Encodes and writes the type to the reader. Returns an error
	// if one occurred on the reader.
	Encode(io.Writer) (int, error)

	// Gets the associated marker byte for the type.
	Marker() byte

	// Native returns the native Golang type assosciated with this AmfType.
	Native() reflect.Type
}

AMF Types (strings, numbers, etc) implement the AmfType interface, which specifies that they have methods available to decode and encode data.

type Array

type Array struct {
	*Paired
}

func NewArray

func NewArray() *Array

func (*Array) Decode

func (a *Array) Decode(r io.Reader) error

Implements AmfType.Decode

func (*Array) Encode

func (a *Array) Encode(w io.Writer) (int, error)

Implements AmfType.Encode

func (*Array) EncodeBytes

func (a *Array) EncodeBytes() []byte

Implements AmfType.EncodeBytes

func (*Array) Marker

func (a *Array) Marker() byte

Implements AmfType.Marker

func (*Array) Native

func (a *Array) Native() reflect.Type

type Bodyless

type Bodyless interface {
	IsBodyless() bool
}

type Bool

type Bool bool

func (*Bool) Decode

func (b *Bool) Decode(r io.Reader) error

Implements AmfType.Decode

func (*Bool) Encode

func (b *Bool) Encode(w io.Writer) (int, error)

Implements AmfType.Encode

func (*Bool) Marker

func (b *Bool) Marker() byte

Implements AmfType.Marker

func (*Bool) Native

func (b *Bool) Native() reflect.Type

Implements AmfType.Native

type Decoder

type Decoder func(r io.Reader) (AmfType, error)
var (
	Decode Decoder = func(r io.Reader) (AmfType, error) {
		var typeId [1]byte
		if _, err := io.ReadFull(r, typeId[:]); err != nil {
			return nil, err
		}

		typ := DefaultIdentifier.TypeOf(typeId[0])
		if typ == nil {
			return nil, UnknownPacketError(typeId[0])
		}

		if err := typ.Decode(r); err != nil {
			return nil, err
		}

		return typ, nil
	}
)

type Encoder

type Encoder func(t AmfType, w io.Writer) (int, error)

Encoder represents a func capable of writing a representation of the AmfType "t" to the io.Writer "w". By contract, this type must obey the rules of amf0 encoding, which means there must be a marker, and a payload.

var (
	// Encode serves as a default implementation of the Encoder type. It is
	// compliant with the amf0 specification. It returns the number of bytes
	// that it wrote (usually 1+len(payload)), and any errors that it
	// encountered along the way.
	Encode Encoder = func(t AmfType, w io.Writer) (int, error) {
		buf := new(bytes.Buffer)

		buf.Write([]byte{t.Marker()})
		if _, err := t.Encode(buf); err != nil {
			return 0, err
		}

		n, err := io.Copy(w, buf)
		return int(n), err
	}
)

type Identifier

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

Identifier is a type capable of preforming bi-direcitonal lookups with respect to the various AmfTypes implemented here. It has two discrete responsibilities:

  • Fetch the AmfType assosciated with a given marker ID. ``` typ := DefaultIdentifier.TypeOf(0x01) #=> (reflect.TypeOf(new(amf0.Bool)).Elem()) ```

  • Fetch the AmfType assosciated with a given native type. (i.e., going from a `bool` to an `amf0.Bool`.) ``` typ := DefaultIdentifier.AmfType(true) #=> (reflect.TypeOf(new(amf0.Bool)).Elem()) ```

func NewIdentifier

func NewIdentifier(types ...TypeFactory) *Identifier

NewIdentifier returns a pointer to a new instance of the Identifier type. By calling this method, all of the TypeOf and AmfType permutations are precomputed, saving tiem in the future.

func (*Identifier) NewMatchingType

func (i *Identifier) NewMatchingType(v interface{}) AmfType

NewMatchingType returns a new instance of an AmfType in the same kind as given by v. If no matching type is found, nil is returned instead.

func (*Identifier) NewMatchingTypeFromValue

func (i *Identifier) NewMatchingTypeFromValue(val reflect.Value) AmfType

NewMatchingTypeFromValue returns a new instance of an AmfType in the same kind as given by v. If no matching type is found, nil is returned instead.

func (*Identifier) TypeOf

func (i *Identifier) TypeOf(id byte) AmfType

TypeOf returns the AmfType assosciated with a given marker ID.

type LongString

type LongString string

func NewLongString

func NewLongString(str string) *LongString

func (*LongString) Decode

func (l *LongString) Decode(r io.Reader) error

func (*LongString) Encode

func (l *LongString) Encode(w io.Writer) (int, error)

func (*LongString) Marker

func (l *LongString) Marker() byte

func (*LongString) Native

func (l *LongString) Native() reflect.Type

type Null

type Null struct{}

func (*Null) Decode

func (n *Null) Decode(r io.Reader) error

func (*Null) Encode

func (n *Null) Encode(w io.Writer) (int, error)

func (*Null) IsBodyless

func (n *Null) IsBodyless() bool

func (*Null) Marker

func (n *Null) Marker() byte

func (*Null) Native

func (n *Null) Native() reflect.Type

type Number

type Number float64

func NewNumber

func NewNumber(n float64) *Number

func (*Number) Decode

func (n *Number) Decode(r io.Reader) error

Implements AmfType.Decode

func (*Number) Encode

func (n *Number) Encode(w io.Writer) (int, error)

Implements AmfType.Encode

func (*Number) Marker

func (n *Number) Marker() byte

func (*Number) Native

func (n *Number) Native() reflect.Type

type Object

type Object struct {
	*Paired
}

func NewObject

func NewObject() *Object

func (*Object) Decode

func (o *Object) Decode(r io.Reader) error

Implements AmfType.Decode

func (*Object) Encode

func (o *Object) Encode(w io.Writer) (int, error)

Implements AmfType.Encode

func (*Object) Marker

func (o *Object) Marker() byte

Implements AmfType.Marker

func (*Object) Native

func (o *Object) Native() reflect.Type

Implements AmfType.Native

type Paired

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

func NewPaired

func NewPaired() *Paired

func (*Paired) Add

func (p *Paired) Add(key string, value AmfType)

Adds a new pair to the object.

func (*Paired) Bool

func (p *Paired) Bool(key string) (*Bool, error)

Returns a boolean type AMF specified by the key. If the key isn't found it returns a NotFoundError. If it is found but is of the wrong type, this returns a WrongTypeError.

func (*Paired) Get

func (p *Paired) Get(key string) (AmfType, error)

Returns an item specified by the key, or returns a NotFoundError.

func (*Paired) Len

func (p *Paired) Len() int

Returns the number of kv pairs in the object.

func (*Paired) String

func (p *Paired) String(key string) (*String, error)

Returns a string type AMF specified by the key. If the key isn't found it returns a NotFoundError. If it is found but is of the wrong type, this returns a WrongTypeError.

type String

type String string

func NewString

func NewString(str string) *String

func (*String) Decode

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

func (*String) Encode

func (s *String) Encode(w io.Writer) (int, error)

func (*String) Marker

func (s *String) Marker() byte

func (*String) Native

func (s *String) Native() reflect.Type

type TypeFactory

type TypeFactory func() AmfType

TypeFactory is a factory type that returns new instances of a specific AmfType.

type Undefined

type Undefined struct{}

func (*Undefined) Decode

func (u *Undefined) Decode(r io.Reader) error

func (*Undefined) Encode

func (u *Undefined) Encode(w io.Writer) (int, error)

func (*Undefined) IsBodyless

func (u *Undefined) IsBodyless() bool

func (*Undefined) Marker

func (u *Undefined) Marker() byte

func (*Undefined) Native

func (u *Undefined) Native() reflect.Type

type UnknownPacketError

type UnknownPacketError byte

func (UnknownPacketError) Error

func (e UnknownPacketError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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