bin

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

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

Go to latest
Published: Jan 16, 2024 License: Apache-2.0 Imports: 16 Imported by: 33

README

StreamingFast binary

reference License

Binary Encoder and Decoder library

Usage

Struct Field Tags
  • _ will skip the field when encoding & deocode a struc
  • sizeof= indicates this field is a number used to track the length of a another field.
  • little indicates this field will be encoded as little endian
  • big indicates this field will be encoded as big endian
  • optional indicates this field is optional. An optional field will have an initial byte either 0x00 or 0x01 indicating whether the field is present. If the field is present it will be followed by the value.
  • Bare values will be parsed as type and little endian when necessary
Supported Types
  • int8, int16, int32, int64, Int128
  • uint8, uint16,uint32,uint64, Uint128
  • float32, float64, Float128
  • string, bool
  • Varint16, Varint32
  • Varuint16, Varuint32
Custom Types

To implement custom types, your types would need to implement the MarshalerBinary & UnmarshalerBinary interfaces

Example

Basic Implementation
type Example struct {
    Var      uint32 `bin:"_"`
    Str      string
    IntCount uint32 `bin:"sizeof=Var"`
    Weird    [8]byte
    Var      []int
}
Custom Implementation
type Example struct {
	Prefix byte
	Value  uint32
}

func (e *Example) UnmarshalBinary(decoder *Decoder) (err error) {
	if e.Prefix, err = decoder.ReadByte(); err != nil {
		return err
	}
	if e.Value, err = decoder.ReadUint32(BE()); err != nil {
		return err
	}
	return nil
}

func (e *Example) MarshalBinary(encoder *Encoder) error {
	if err := encoder.WriteByte(e.Prefix); err != nil {
		return err
	}
	return encoder.WriteUint32(e.Value, BE())
}

Contributing

Issues and PR in this repo related strictly to the binary library

Report any protocol-specific issues in their respective repositories

Please first refer to the general StreamingFast contribution guide, if you wish to contribute to this code base.

This codebase uses unit tests extensively, please write and run tests.

License

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrVarIntBufferSize = errors.New("varint: invalid buffer size")
View Source
var TypeSize = struct {
	Bool int
	Byte int

	Int8  int
	Int16 int

	Uint8   int
	Uint16  int
	Uint32  int
	Uint64  int
	Uint128 int

	Float32 int
	Float64 int

	PublicKey int
	Signature int

	Tstamp         int
	BlockTimestamp int

	CurrencyName int
}{
	Byte: 1,
	Bool: 1,

	Int8:  1,
	Int16: 2,

	Uint8:   1,
	Uint16:  2,
	Uint32:  4,
	Uint64:  8,
	Uint128: 16,

	Float32: 4,
	Float64: 8,
}

Functions

func BE

func BE() binary.ByteOrder

func ByteCount

func ByteCount(v interface{}) (uint64, error)

ByteCount computes the byte count size for the received populated structure. The reported size is the one for the populated structure received in arguments. Depending on how serialization of your fields is performed, size could vary for different structure.

func LE

func LE() binary.ByteOrder

func MarshalBinary

func MarshalBinary(v interface{}) ([]byte, error)

func MustByteCount

func MustByteCount(v interface{}) uint64

MustByteCount acts just like ByteCount but panics if it encounters any encoding errors.

Types

type BaseVariant

type BaseVariant struct {
	TypeID uint32
	Impl   interface{}
}

func (*BaseVariant) Assign

func (a *BaseVariant) Assign(typeID uint32, impl interface{})

func (*BaseVariant) MarshalJSON

func (a *BaseVariant) MarshalJSON(def *VariantDefinition) ([]byte, error)

func (*BaseVariant) Obtain

func (a *BaseVariant) Obtain(def *VariantDefinition) (typeID uint32, typeName string, impl interface{})

func (*BaseVariant) UnmarshalBinaryVariant

func (a *BaseVariant) UnmarshalBinaryVariant(decoder *Decoder, def *VariantDefinition) (err error)

func (*BaseVariant) UnmarshalJSON

func (a *BaseVariant) UnmarshalJSON(data []byte, def *VariantDefinition) error

type Bool

type Bool bool

func (Bool) MarshalBinary

func (b Bool) MarshalBinary(encoder *Encoder) error

func (*Bool) UnmarshalBinary

func (b *Bool) UnmarshalBinary(decoder *Decoder) error

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

type Decoder

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

Decoder implements the EOS unpacking, similar to FC_BUFFER

func NewDecoder

func NewDecoder(data []byte) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) (err error)

func (*Decoder) HasRemaining

func (d *Decoder) HasRemaining() bool

func (*Decoder) Position

func (d *Decoder) Position() uint

func (*Decoder) ReadBool

func (d *Decoder) ReadBool() (out bool, err error)

func (*Decoder) ReadByte

func (d *Decoder) ReadByte() (out byte, err error)

func (*Decoder) ReadByteArray

func (d *Decoder) ReadByteArray() (out []byte, err error)

func (*Decoder) ReadFloat128

func (d *Decoder) ReadFloat128(order binary.ByteOrder) (out Float128, err error)

func (*Decoder) ReadFloat32

func (d *Decoder) ReadFloat32(order binary.ByteOrder) (out float32, err error)

func (*Decoder) ReadFloat64

func (d *Decoder) ReadFloat64(order binary.ByteOrder) (out float64, err error)

func (*Decoder) ReadInt128

func (d *Decoder) ReadInt128(order binary.ByteOrder) (out Int128, err error)

func (*Decoder) ReadInt16

func (d *Decoder) ReadInt16(order binary.ByteOrder) (out int16, err error)

func (*Decoder) ReadInt32

func (d *Decoder) ReadInt32(order binary.ByteOrder) (out int32, err error)

func (*Decoder) ReadInt64

func (d *Decoder) ReadInt64(order binary.ByteOrder) (out int64, err error)

func (*Decoder) ReadInt8

func (d *Decoder) ReadInt8() (out int8, err error)

func (*Decoder) ReadString

func (d *Decoder) ReadString() (out string, err error)

func (*Decoder) ReadUint128

func (d *Decoder) ReadUint128(order binary.ByteOrder) (out Uint128, err error)

func (*Decoder) ReadUint16

func (d *Decoder) ReadUint16(order binary.ByteOrder) (out uint16, err error)

func (*Decoder) ReadUint32

func (d *Decoder) ReadUint32(order binary.ByteOrder) (out uint32, err error)

func (*Decoder) ReadUint64

func (d *Decoder) ReadUint64(order binary.ByteOrder) (out uint64, err error)

func (*Decoder) ReadUint8

func (d *Decoder) ReadUint8() (out uint8, err error)

func (*Decoder) ReadUvarint16

func (d *Decoder) ReadUvarint16() (out uint16, err error)

func (*Decoder) ReadUvarint32

func (d *Decoder) ReadUvarint32() (out uint32, err error)

func (*Decoder) ReadUvarint64

func (d *Decoder) ReadUvarint64() (uint64, error)

func (*Decoder) ReadVarint16

func (d *Decoder) ReadVarint16() (out int16, err error)

func (*Decoder) ReadVarint32

func (d *Decoder) ReadVarint32() (out int32, err error)

func (*Decoder) ReadVarint64

func (d *Decoder) ReadVarint64() (out int64, err error)

func (*Decoder) Remaining

func (d *Decoder) Remaining() int

func (*Decoder) SafeReadUTF8String

func (d *Decoder) SafeReadUTF8String() (out string, err error)

func (*Decoder) SetPosition

func (d *Decoder) SetPosition(idx uint) error

func (*Decoder) SkipBytes

func (d *Decoder) SkipBytes(count uint) error

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) (err error)

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(b bool) (err error)

func (*Encoder) WriteByte

func (e *Encoder) WriteByte(b byte) (err error)

func (*Encoder) WriteByteArray

func (e *Encoder) WriteByteArray(b []byte, writeLength bool) error

func (*Encoder) WriteFloat32

func (e *Encoder) WriteFloat32(f float32, order binary.ByteOrder) (err error)

func (*Encoder) WriteFloat64

func (e *Encoder) WriteFloat64(f float64, order binary.ByteOrder) (err error)

func (*Encoder) WriteInt128

func (e *Encoder) WriteInt128(i Int128, order binary.ByteOrder) (err error)

func (*Encoder) WriteInt16

func (e *Encoder) WriteInt16(i int16, order binary.ByteOrder) (err error)

func (*Encoder) WriteInt32

func (e *Encoder) WriteInt32(i int32, order binary.ByteOrder) (err error)

func (*Encoder) WriteInt64

func (e *Encoder) WriteInt64(i int64, order binary.ByteOrder) (err error)

func (*Encoder) WriteRaw

func (e *Encoder) WriteRaw(b []byte) error

func (*Encoder) WriteString

func (e *Encoder) WriteString(s string) (err error)

func (*Encoder) WriteUVarInt

func (e *Encoder) WriteUVarInt(v int) (err error)

func (*Encoder) WriteUint128

func (e *Encoder) WriteUint128(i Uint128, order binary.ByteOrder) (err error)

func (*Encoder) WriteUint16

func (e *Encoder) WriteUint16(i uint16, order binary.ByteOrder) (err error)

func (*Encoder) WriteUint32

func (e *Encoder) WriteUint32(i uint32, order binary.ByteOrder) (err error)

func (*Encoder) WriteUint64

func (e *Encoder) WriteUint64(i uint64, order binary.ByteOrder) (err error)

func (*Encoder) WriteUint8

func (e *Encoder) WriteUint8(i uint8) (err error)

func (*Encoder) WriteVarInt

func (e *Encoder) WriteVarInt(v int) (err error)

type Float128

type Float128 Uint128

func (Float128) MarshalBinary

func (i Float128) MarshalBinary(enc *Encoder) error

func (Float128) MarshalJSON

func (i Float128) MarshalJSON() (data []byte, err error)

func (*Float128) UnmarshalBinary

func (i *Float128) UnmarshalBinary(dec *Decoder) error

func (*Float128) UnmarshalJSON

func (i *Float128) UnmarshalJSON(data []byte) error

type HexBytes

type HexBytes []byte

func (HexBytes) MarshalBinary

func (o HexBytes) MarshalBinary(encoder *Encoder) error

func (HexBytes) MarshalJSON

func (t HexBytes) MarshalJSON() ([]byte, error)

func (HexBytes) String

func (t HexBytes) String() string

func (*HexBytes) UnmarshalBinary

func (o *HexBytes) UnmarshalBinary(decoder *Decoder) error

func (*HexBytes) UnmarshalJSON

func (t *HexBytes) UnmarshalJSON(data []byte) (err error)

type Int128

type Int128 Uint128

Int128

func (Int128) BigInt

func (i Int128) BigInt() *big.Int

func (Int128) DecimalString

func (i Int128) DecimalString() string

func (Int128) MarshalBinary

func (i Int128) MarshalBinary(enc *Encoder) error

func (Int128) MarshalJSON

func (i Int128) MarshalJSON() (data []byte, err error)

func (Int128) String

func (i Int128) String() string

func (*Int128) UnmarshalBinary

func (i *Int128) UnmarshalBinary(dec *Decoder) error

func (*Int128) UnmarshalJSON

func (i *Int128) UnmarshalJSON(data []byte) error

type Int64

type Int64 int64

func (Int64) MarshalBinary

func (i Int64) MarshalBinary(enc *Encoder) error

func (Int64) MarshalJSON

func (i Int64) MarshalJSON() (data []byte, err error)

func (*Int64) UnmarshalBinary

func (i *Int64) UnmarshalBinary(dec *Decoder) error

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(data []byte) error

type InvalidDecoderError

type InvalidDecoderError struct {
	Type reflect.Type
}

An InvalidDecoderError describes an invalid argument passed to Decoder. (The argument to Decoder must be a non-nil pointer.)

func (*InvalidDecoderError) Error

func (e *InvalidDecoderError) Error() string

type JSONFloat64

type JSONFloat64 float64

func (JSONFloat64) MarshalBinary

func (f JSONFloat64) MarshalBinary(enc *Encoder) error

func (*JSONFloat64) UnmarshalBinary

func (f *JSONFloat64) UnmarshalBinary(dec *Decoder) error

func (*JSONFloat64) UnmarshalJSON

func (f *JSONFloat64) UnmarshalJSON(data []byte) error

type MarshalerBinary

type MarshalerBinary interface {
	MarshalBinary(encoder *Encoder) error
}

type OnVariant

type OnVariant = func(impl interface{}) error

type SafeString

type SafeString string

func (SafeString) MarshalBinary

func (ss SafeString) MarshalBinary(encoder *Encoder) error

func (*SafeString) UnmarshalBinary

func (ss *SafeString) UnmarshalBinary(d *Decoder) error

type TypeIDEncoding

type TypeIDEncoding uint32
const (
	Uvarint32TypeIDEncoding TypeIDEncoding = iota
	Uint32TypeIDEncoding
	Uint8TypeIDEncoding
)

type Uint128

type Uint128 struct {
	Lo uint64
	Hi uint64
}

uint128

func (Uint128) BigInt

func (i Uint128) BigInt() *big.Int

func (Uint128) DecimalString

func (i Uint128) DecimalString() string

func (Uint128) MarshalBinary

func (i Uint128) MarshalBinary(enc *Encoder) error

func (Uint128) MarshalJSON

func (i Uint128) MarshalJSON() (data []byte, err error)

func (Uint128) String

func (i Uint128) String() string

func (*Uint128) UnmarshalBinary

func (i *Uint128) UnmarshalBinary(dec *Decoder) error

func (*Uint128) UnmarshalJSON

func (i *Uint128) UnmarshalJSON(data []byte) error

type Uint64

type Uint64 uint64

func (Uint64) MarshalBinary

func (i Uint64) MarshalBinary(enc *Encoder) error

func (Uint64) MarshalJSON

func (i Uint64) MarshalJSON() (data []byte, err error)

func (*Uint64) UnmarshalBinary

func (i *Uint64) UnmarshalBinary(dec *Decoder) error

func (*Uint64) UnmarshalJSON

func (i *Uint64) UnmarshalJSON(data []byte) error

type UnmarshalerBinary

type UnmarshalerBinary interface {
	UnmarshalBinary(decoder *Decoder) error
}

type Variant

type Variant interface {
	Assign(typeID uint, impl interface{})
	Obtain() (typeID uint, impl interface{})
}

type VariantDefinition

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

func NewVariantDefinition

func NewVariantDefinition(typeIDEncoding TypeIDEncoding, types []VariantType) (out *VariantDefinition)

NewVariantDefinition creates a variant definition based on the *ordered* provided types. It's the ordering that defines the binary variant value just like in native `nodeos` C++ and in Smart Contract via the `std::variant` type. It's important to pass the entries in the right order!

This variant definition can now be passed to functions of `BaseVariant` to implement marshal/unmarshaling functionalities for binary & JSON.

func (*VariantDefinition) TypeID

func (d *VariantDefinition) TypeID(name string) uint32

type VariantImplFactory

type VariantImplFactory = func() interface{}

type VariantType

type VariantType struct {
	Name string
	Type interface{}
}

type Varint16

type Varint16 int16

func (Varint16) MarshalBinary

func (o Varint16) MarshalBinary(encoder *Encoder) error

func (*Varint16) UnmarshalBinary

func (o *Varint16) UnmarshalBinary(decoder *Decoder) error

type Varint32

type Varint32 int32

func (Varint32) MarshalBinary

func (o Varint32) MarshalBinary(encoder *Encoder) error

func (*Varint32) UnmarshalBinary

func (o *Varint32) UnmarshalBinary(decoder *Decoder) error

type Varuint16

type Varuint16 uint16

func (Varuint16) MarshalBinary

func (o Varuint16) MarshalBinary(encoder *Encoder) error

func (*Varuint16) UnmarshalBinary

func (o *Varuint16) UnmarshalBinary(decoder *Decoder) error

type Varuint32

type Varuint32 uint32

func (Varuint32) MarshalBinary

func (o Varuint32) MarshalBinary(encoder *Encoder) error

func (*Varuint32) UnmarshalBinary

func (o *Varuint32) UnmarshalBinary(decoder *Decoder) error

Jump to

Keyboard shortcuts

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