codec

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 5 Imported by: 42

Documentation

Index

Constants

View Source
const OFFSET_SIZE = 4

Variables

This section is empty.

Functions

func ContainerLength

func ContainerLength(values ...Serializable) (out uint64)

func Sum

func Sum(values ...ByteLength) (out uint64)

Types

type ByteLength

type ByteLength interface {
	ByteLength() uint64
}

type Decodable

type Decodable interface {
	Decode(x []byte) error
}

type DecodingReader

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

func NewDecodingReader

func NewDecodingReader(input io.Reader, scope uint64) *DecodingReader

func (*DecodingReader) BitList

func (dr *DecodingReader) BitList(dst *[]byte, bitLimit uint64) error

func (*DecodingReader) BitVector

func (dr *DecodingReader) BitVector(dst *[]byte, bitLength uint64) error

func (*DecodingReader) ByteList added in v0.1.2

func (dr *DecodingReader) ByteList(dst *[]byte, byteLimit uint64) error

func (*DecodingReader) ByteVector added in v0.1.2

func (dr *DecodingReader) ByteVector(dst *[]byte, byteLength uint64) error

func (*DecodingReader) Container

func (dr *DecodingReader) Container(fields ...Deserializable) error

func (*DecodingReader) FixedLenContainer

func (dr *DecodingReader) FixedLenContainer(fields ...Deserializable) error

FixedLenContainer just reads the concatednated fields, without opening smaller scopes. Much faster for simple structs, but use with caution.

func (*DecodingReader) Index

func (dr *DecodingReader) Index() uint64

how far we have read so far (scoped per container)

func (*DecodingReader) List

func (dr *DecodingReader) List(add func() Deserializable, fixedElemSize uint64, limit uint64) error

func (*DecodingReader) Max

func (dr *DecodingReader) Max() uint64

How far we can read (max - i = remaining bytes to read without error). Note: when a child element is not fixed length, the parent should set the scope, so that the child can infer its size from it.

func (*DecodingReader) Read

func (dr *DecodingReader) Read(p []byte) (int, error)

Read p fully, returns n read bytes. len(p) == n always if err == nil

func (*DecodingReader) ReadByte

func (dr *DecodingReader) ReadByte() (byte, error)

func (*DecodingReader) ReadOffset

func (dr *DecodingReader) ReadOffset() (uint32, error)

func (*DecodingReader) ReadUint16

func (dr *DecodingReader) ReadUint16() (uint16, error)

func (*DecodingReader) ReadUint32

func (dr *DecodingReader) ReadUint32() (uint32, error)

func (*DecodingReader) ReadUint64

func (dr *DecodingReader) ReadUint64() (uint64, error)

func (*DecodingReader) Scope

func (dr *DecodingReader) Scope() uint64

returns the remaining scope (amount of bytes that can be read)

func (*DecodingReader) Skip

func (dr *DecodingReader) Skip(count uint64) (int, error)

func (*DecodingReader) SubScope

func (dr *DecodingReader) SubScope(count uint64) (*DecodingReader, error)

SubScope returns a scope of the SSZ reader. Re-uses same scratchpad.

func (*DecodingReader) Union added in v0.1.7

func (dr *DecodingReader) Union(selectFn func(selector uint8) (Deserializable, error)) error

Deserialize a Union, the selectFn is called to retrieve a destination to deserialize into, and remember the selector. The selectFn can return a nil when there is nothing to decode (when Union is used as an Optional)

func (*DecodingReader) UpdateIndexFromScoped

func (dr *DecodingReader) UpdateIndexFromScoped(other *DecodingReader)

func (*DecodingReader) Vector

func (dr *DecodingReader) Vector(item func(i uint64) Deserializable, fixedElemSize uint64, length uint64) error

Deserialize vector. If fixedElemSize == 0, the item is regarded as dynamic length

type Deserializable

type Deserializable interface {
	Deserialize(dr *DecodingReader) error
	FixedLength
}

type Encodable

type Encodable interface {
	Encode() ([]byte, error)
}

type EncodingWriter

type EncodingWriter struct {
	Scratch [32]byte
	// contains filtered or unexported fields
}

func NewEncodingWriter

func NewEncodingWriter(w io.Writer) *EncodingWriter

func (*EncodingWriter) BitList

func (ew *EncodingWriter) BitList(bits []byte) error

func (*EncodingWriter) BitVector

func (ew *EncodingWriter) BitVector(bits []byte) error

func (*EncodingWriter) Container

func (ew *EncodingWriter) Container(fields ...Serializable) error

Container serialization. Fields with a non-zero .FixedLength() are considered fixed-length.

func (*EncodingWriter) FixedLenContainer

func (ew *EncodingWriter) FixedLenContainer(fields ...Serializable) error

func (*EncodingWriter) List

func (ew *EncodingWriter) List(item func(i uint64) Serializable, fixedElemSize uint64, length uint64) error

List serialization. If fixedElemSize == 0, then items are considered dynamic length, and will be encoded with offsets.

func (*EncodingWriter) Union added in v0.1.7

func (ew *EncodingWriter) Union(selector uint8, value Serializable) error

Serialize a Union. The value

func (*EncodingWriter) Vector

func (ew *EncodingWriter) Vector(item func(i uint64) Serializable, fixedElemSize uint64, length uint64) error

Vector serialization, works the same as encoding a List.

func (*EncodingWriter) Write

func (ew *EncodingWriter) Write(p []byte) error

Write writes len(p) bytes from p fully to the underlying accumulated buffer.

func (*EncodingWriter) WriteByte

func (ew *EncodingWriter) WriteByte(v byte) error

Write a single byte to the buffer.

func (*EncodingWriter) WriteOffset

func (ew *EncodingWriter) WriteOffset(prevOffset uint64, elemLen uint64) (offset uint64, err error)

Writes an offset for an element

func (*EncodingWriter) WriteUint16

func (ew *EncodingWriter) WriteUint16(v uint16) error

func (*EncodingWriter) WriteUint32

func (ew *EncodingWriter) WriteUint32(v uint32) error

func (*EncodingWriter) WriteUint64

func (ew *EncodingWriter) WriteUint64(v uint64) error

func (*EncodingWriter) Written

func (ew *EncodingWriter) Written() int

How many bytes were written to the underlying io.Writer before ending encoding (for handling errors)

type FixedLength

type FixedLength interface {
	FixedLength() uint64
}

type Serializable

type Serializable interface {
	Serialize(w *EncodingWriter) error
	ByteLength
	FixedLength
}

Jump to

Keyboard shortcuts

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