bin

package
v0.0.0-...-87e9d67 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package bin implements binary serialization and deserialization for TL, providing Buffer that can decode and encode basic Type Language types.

This package is not intended to be used directly.

Ref:

Index

Constants

View Source
const (
	TypeIntID    = 0xa8509bda // int = Int (0xa8509bda)
	TypeLongID   = 0x22076cba // long = Long (0x22076cba)
	TypeDoubleID = 0x2210c154 // double = Double (0x2210c154)
	TypeStringID = 0xb5286e24 // string = String (0xb5286e24)
	TypeVector   = 0x1cb5c415 // vector {t:Type} # [ t ] = Vector t
	TypeBytes    = 0xe937bb82 // bytes#e937bb82 = Bytes

	TypeTrue  = 0x997275b5 // boolTrue#997275b5 = Bool
	TypeFalse = 0xbc799737 // boolFalse#bc799737 = Bool
)

Basic TL types.

View Source
const PreallocateLimit = 1024

PreallocateLimit is a vector pre-allocation limit.

View Source
const Word = 4

Word represents 4-byte sequence. Values in TL are generally aligned to Word.

Variables

This section is empty.

Functions

func NewUnexpectedID

func NewUnexpectedID(id uint32) error

NewUnexpectedID return new UnexpectedIDErr.

Types

type BareDecoder

type BareDecoder interface {
	DecodeBare(b *Buffer) error
}

BareDecoder can decode it's binary form from Buffer. BareEncoder is like Encoder, but decodes object as bare.

type BareEncoder

type BareEncoder interface {
	EncodeBare(b *Buffer) error
}

BareEncoder can encode it's binary form to Buffer. BareEncoder is like Encoder, but encodes object as bare.

type Buffer

type Buffer struct {
	Buf []byte
}

Buffer implements low level binary (de-)serialization for TL.

func (*Buffer) Bool

func (b *Buffer) Bool() (bool, error)

Bool decodes bare boolean from Buffer.

func (*Buffer) Bytes

func (b *Buffer) Bytes() ([]byte, error)

Bytes decodes byte slice from Buffer.

NB: returning value is a copy, it's safe to modify it.

func (*Buffer) ConsumeID

func (b *Buffer) ConsumeID(id uint32) error

ConsumeID decodes type id from Buffer. If id differs from provided, the *UnexpectedIDErr{ID: gotID} will be returned and buffer will be not consumed.

func (*Buffer) ConsumeN

func (b *Buffer) ConsumeN(target []byte, n int) error

ConsumeN consumes n bytes from buffer, writing them to target.

Returns io.ErrUnexpectedEOF if buffer contains less that n bytes. Expects that len(target) >= n.

func (*Buffer) Copy

func (b *Buffer) Copy() []byte

Copy returns new copy of buffer.

func (*Buffer) Decode

func (b *Buffer) Decode(d Decoder) error

Decode wrapper.

func (*Buffer) Double

func (b *Buffer) Double() (float64, error)

Double decodes 64-bit floating point from Buffer.

func (*Buffer) Encode

func (b *Buffer) Encode(e Encoder) error

Encode wrapper.

func (*Buffer) Expand

func (b *Buffer) Expand(n int)

Expand expands buffer to add n bytes.

func (*Buffer) ID

func (b *Buffer) ID() (uint32, error)

ID decodes type id from Buffer.

func (*Buffer) Int

func (b *Buffer) Int() (int, error)

Int decodes integer from Buffer.

func (*Buffer) Int128

func (b *Buffer) Int128() (Int128, error)

Int128 decodes 128-bit signed integer from Buffer.

func (*Buffer) Int256

func (b *Buffer) Int256() (Int256, error)

Int256 decodes 256-bit signed integer from Buffer.

func (*Buffer) Int32

func (b *Buffer) Int32() (int32, error)

Int32 decodes signed 32-bit integer from Buffer.

func (*Buffer) Int53

func (b *Buffer) Int53() (int64, error)

Int53 decodes 53-bit signed integer from Buffer.

func (Buffer) Len

func (b Buffer) Len() int

Len returns length of internal buffer.

func (*Buffer) Long

func (b *Buffer) Long() (int64, error)

Long decodes 64-bit signed integer from Buffer.

func (*Buffer) PeekID

func (b *Buffer) PeekID() (uint32, error)

PeekID returns next type id in Buffer, but does not consume it.

func (*Buffer) PeekN

func (b *Buffer) PeekN(target []byte, n int) error

PeekN returns n bytes from Buffer to target, but does not consume it.

Returns io.ErrUnexpectedEOF if buffer contains less that n bytes. Expects that len(target) >= n.

func (*Buffer) Put

func (b *Buffer) Put(raw []byte)

Put appends raw bytes to buffer.

Buffer does not retain raw.

func (*Buffer) PutBool

func (b *Buffer) PutBool(v bool)

PutBool serializes bare boolean.

func (*Buffer) PutBytes

func (b *Buffer) PutBytes(v []byte)

PutBytes serializes bare byte string.

func (*Buffer) PutDouble

func (b *Buffer) PutDouble(v float64)

PutDouble serializes v as 64-bit floating point.

func (*Buffer) PutID

func (b *Buffer) PutID(id uint32)

PutID serializes type definition id, like a8509bda.

func (*Buffer) PutInt

func (b *Buffer) PutInt(v int)

PutInt serializes v as signed 32-bit integer.

If v is bigger than 32-bit, `behavior` is undefined.

func (*Buffer) PutInt128

func (b *Buffer) PutInt128(v Int128)

PutInt128 serializes v as 128-bit signed integer.

func (*Buffer) PutInt256

func (b *Buffer) PutInt256(v Int256)

PutInt256 serializes v as 256-bit signed integer.

func (*Buffer) PutInt32

func (b *Buffer) PutInt32(v int32)

PutInt32 serializes signed 32-bit integer.

func (*Buffer) PutInt53

func (b *Buffer) PutInt53(v int64)

PutInt53 serializes v as signed integer.

func (*Buffer) PutLong

func (b *Buffer) PutLong(v int64)

PutLong serializes v as signed integer.

func (*Buffer) PutString

func (b *Buffer) PutString(s string)

PutString serializes bare string.

func (*Buffer) PutUint16

func (b *Buffer) PutUint16(v uint16)

PutUint16 serializes unsigned 16-bit integer.

func (*Buffer) PutUint32

func (b *Buffer) PutUint32(v uint32)

PutUint32 serializes unsigned 32-bit integer.

func (*Buffer) PutUint64

func (b *Buffer) PutUint64(v uint64)

PutUint64 serializes v as unsigned 64-bit integer.

func (*Buffer) PutVectorHeader

func (b *Buffer) PutVectorHeader(length int)

PutVectorHeader serializes vector header with provided length.

func (Buffer) Raw

func (b Buffer) Raw() []byte

Raw returns internal byte slice.

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read implements io.Reader.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset buffer to zero length.

func (*Buffer) ResetN

func (b *Buffer) ResetN(n int)

ResetN resets buffer and expands it to fit n bytes.

func (*Buffer) ResetTo

func (b *Buffer) ResetTo(buf []byte)

ResetTo sets internal buffer exactly to provided value.

Buffer will retain buf, so user should not modify or read it concurrently.

func (*Buffer) Skip

func (b *Buffer) Skip(n int)

Skip moves cursor for next n bytes.

func (*Buffer) String

func (b *Buffer) String() (string, error)

String decodes string from Buffer.

func (*Buffer) Uint32

func (b *Buffer) Uint32() (uint32, error)

Uint32 decodes unsigned 32-bit integer from Buffer.

func (*Buffer) Uint64

func (b *Buffer) Uint64() (uint64, error)

Uint64 decodes 64-bit unsigned integer from Buffer.

func (*Buffer) VectorHeader

func (b *Buffer) VectorHeader() (int, error)

VectorHeader decodes vector length from Buffer.

type Decoder

type Decoder interface {
	Decode(b *Buffer) error
}

Decoder can decode it's binary form from Buffer.

type Encoder

type Encoder interface {
	Encode(b *Buffer) error
}

Encoder can encode it's binary form to Buffer.

type Fields

type Fields uint32

Fields represent a bitfield value that compactly encodes information about provided conditional fields, e.g. says that fields "1", "5" and "10" were set.

func (*Fields) Decode

func (f *Fields) Decode(b *Buffer) error

Decode implements Decoder.

func (Fields) Encode

func (f Fields) Encode(b *Buffer) error

Encode implements Encoder.

func (Fields) Has

func (f Fields) Has(n int) bool

Has reports whether field with index n was set.

func (*Fields) Set

func (f *Fields) Set(n int)

Set sets field with index n.

func (Fields) String

func (f Fields) String() string

String implement fmt.Stringer

func (*Fields) Unset

func (f *Fields) Unset(n int)

Unset unsets field with index n.

func (Fields) Zero

func (f Fields) Zero() bool

Zero returns true, if all bits are equal to zero.

type Int128

type Int128 [16]byte

Int128 represents signed 128-bit integer.

func (Int128) BigInt

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

BigInt returns corresponding big.Int value.

func (*Int128) Decode

func (i *Int128) Decode(buf *Buffer) error

Decode implements bin.Decoder.

func (Int128) Encode

func (i Int128) Encode(b *Buffer) error

Encode implements bin.Encoder.

type Int256

type Int256 [32]byte

Int256 represents signed 256-bit integer.

func (Int256) BigInt

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

BigInt returns corresponding big.Int value.

func (*Int256) Decode

func (i *Int256) Decode(buf *Buffer) error

Decode implements bin.Decoder.

func (Int256) Encode

func (i Int256) Encode(b *Buffer) error

Encode implements bin.Encoder.

type InvalidLengthError

type InvalidLengthError struct {
	Length int
	Where  string
}

InvalidLengthError is returned when decoder reads invalid length.

func (*InvalidLengthError) Error

func (i *InvalidLengthError) Error() string

type Object

type Object interface {
	Decoder
	Encoder
}

Object wraps Decoder and Encoder interface and represents TL Object.

type Pool

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

Pool is a bin.Buffer pool.

func NewPool

func NewPool(length int) *Pool

NewPool creates new Pool. Length is initial buffer length.

func (*Pool) Get

func (b *Pool) Get() *Buffer

Get takes buffer from pool.

func (*Pool) GetSize

func (b *Pool) GetSize(length int) *Buffer

GetSize takes buffer with given size from pool.

func (*Pool) Put

func (b *Pool) Put(buf *Buffer)

Put returns buffer to pool.

type UnexpectedIDErr

type UnexpectedIDErr struct {
	ID uint32
}

UnexpectedIDErr means that unknown or unexpected type id was decoded.

func (*UnexpectedIDErr) Error

func (e *UnexpectedIDErr) Error() string

Jump to

Keyboard shortcuts

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