rye

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package rye is a helper for writing serializing and deserializing logic.

There are two related compact formats. One for Uint64 and one for []byte. The []byte format is PrefixSlice. If the slice is one byte and that byte is less than 129, it is written directly. So all one byte values upto 128 are encoded in a single byte. The byte 129 is reseverved for a nil slice. For any slice up to 121 bytes, the length is encoded in a single slice as len+129. For values longer than 121 bytes, a meta length is encoded as metalength+250. Then the data length is encoded with that many bytes little-endian.

Compact Uint64s are encoded similarly. Any value up to and including 128 is encoded as a single byte. Values above 128 have their length encoded as 121+len and then the value is encoded little-endian. It would be slightly more efficient to have length encoding start at 247, allowing for more single byte values. But this strategy was chosen for consistency. Converting a Uint64 to a little-endian byte slice and encoding that will produce the same output as encoding the Uint64 directly.

For signed ints, simply casting them will not work well. Twos compliment means that for any negative value the most significant bit will be 1 and so all 8 bytes would always be written. Instead, the absolute value is bit shifted left one and the sign is placed in the least significant bit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inverse

func Inverse(bs []byte)

func Reverse

func Reverse(b []byte)

Types

type Bits

type Bits struct {
	Data []byte
	Idx  int
	Ln   int
}

Bits supports reading and writing individual bits.

func (*Bits) Copy

func (b *Bits) Copy() *Bits

Copy the Bits.

func (*Bits) Read

func (b *Bits) Read() byte

Read a single bit.

func (*Bits) ReadSubBits

func (b *Bits) ReadSubBits(bitLn byte) *Bits

ReadSubBits reads a *Bits using bitLn to decode the length of the Bits.

func (*Bits) ReadUint

func (b *Bits) ReadUint(n byte) uint64

ReadUint of n bits.

func (*Bits) Reset

func (b *Bits) Reset() *Bits

Reset the Idx to 0. Syntactic sugar.

func (*Bits) ShallowCopy

func (b *Bits) ShallowCopy() *Bits

ShallowCopy shares the underlying Data, but not the Ln or Idx values.

func (*Bits) Write

func (b *Bits) Write(bit byte) *Bits

Write a single bit to Bits.

func (*Bits) WriteBits

func (b *Bits) WriteBits(from *Bits) *Bits

WriteBits takes all the bits in "from" starting at from.Idx to from.Ln and writes them to b. The value of from.Idx is reset to 0 after this.

func (*Bits) WriteSubBits

func (b *Bits) WriteSubBits(sub *Bits, bitLn byte)

WriteSubBits writes contents of sub with the length encoded with bitLn bits. This can be used to combine multiple *Bits all encoded with a common bitLn.

func (*Bits) WriteUint

func (b *Bits) WriteUint(u uint64, n byte)

WriteUint u as n bits.

type D

type D byte
var Deserialize D

func (D) Float64

func (D) Float64(b []byte) float64

func (D) Float64Ordered

func (d D) Float64Ordered(b []byte) float64

func (D) Int16

func (d D) Int16(b []byte) int16

func (D) Int16Ordered

func (d D) Int16Ordered(b []byte) int16

func (D) Int32

func (d D) Int32(b []byte) int32

func (D) Int32Ordered

func (d D) Int32Ordered(b []byte) int32

func (D) Int64

func (d D) Int64(b []byte) int64

func (D) Int64Ordered

func (d D) Int64Ordered(b []byte) int64

func (D) Uint16

func (D) Uint16(b []byte) uint16

Uint16 returns a uint16 from the Deserializer and increases the index.

func (D) Uint32

func (D) Uint32(b []byte) uint32

Uint32 returns a uint32 from the Deserializer and increases the index.

func (D) Uint64

func (D) Uint64(b []byte) uint64

Uint64 returns a uint64 from the Deserializer and increases the index.

type Deserializer

type Deserializer struct {
	Data []byte
	Idx  int
}

Deserializer provides a helper for deserializing binary data

func NewDeserializer

func NewDeserializer(data []byte) *Deserializer

NewDeserializer returns a Deserializer prepared to deserialize the provided data

func (*Deserializer) Byte

func (d *Deserializer) Byte() byte

Byte returns one byte from the Deserializer and increases the index.

func (*Deserializer) Done

func (d *Deserializer) Done() bool

Done checks if the Index it at or past the end of the Data.

func (*Deserializer) Float32

func (d *Deserializer) Float32() float32

Float32 is read

func (*Deserializer) Float64

func (d *Deserializer) Float64() float64

Float64 is read

func (*Deserializer) Int16

func (d *Deserializer) Int16() int16

Int16 is read

func (*Deserializer) Int32

func (d *Deserializer) Int32() int32

Int32 is read

func (*Deserializer) Int64

func (d *Deserializer) Int64() int64

Int64 is read

func (*Deserializer) Int8

func (d *Deserializer) Int8() int8

Int8 is read

func (*Deserializer) Slice

func (d *Deserializer) Slice(ln int) []byte

Slice of a known length is returned.

func (*Deserializer) String

func (d *Deserializer) String(ln int) string

String is read

func (*Deserializer) Sub

func (d *Deserializer) Sub(ln int) *Deserializer

Sub returns a Sub-Deserializer of a given length. The index of the parent is placed at the end of the data allocated to the Sub-Deserializer.

func (*Deserializer) Uint

func (d *Deserializer) Uint(size byte) uint64

Uint of a known number of bytes is returned.

func (*Deserializer) Uint16

func (d *Deserializer) Uint16() uint16

Uint16 returns a uint16 from the Deserializer and increases the index.

func (*Deserializer) Uint32

func (d *Deserializer) Uint32() uint32

Uint32 returns a uint32 from the Deserializer and increases the index.

func (*Deserializer) Uint64

func (d *Deserializer) Uint64() uint64

Uint64 returns a uint64 from the Deserializer and increases the index.

func (*Deserializer) Uint8

func (d *Deserializer) Uint8() uint8

Uint8 returns a uint8 from the Deserializer and increases the index.

type S

type S byte

TODO: why isn't this struct{}

var Serialize S

func (S) Any

func (s S) Any(i any, buf []byte) []byte

func (S) Float64

func (s S) Float64(b []byte, f float64)

func (S) Float64Ordered

func (s S) Float64Ordered(b []byte, f float64)

func (S) Int16

func (s S) Int16(b []byte, x int16)

func (S) Int16Ordered

func (s S) Int16Ordered(b []byte, x int16)

func (S) Int32

func (s S) Int32(b []byte, x int32)

func (S) Int32Ordered

func (s S) Int32Ordered(b []byte, x int32)

func (S) Int64

func (s S) Int64(b []byte, x int64)

func (S) Int64Ordered

func (s S) Int64Ordered(b []byte, x int64)

func (S) Uint16

func (S) Uint16(b []byte, x uint16)

Uint16 writes a uint16 to the Serializer and increases the index.

func (S) Uint32

func (S) Uint32(b []byte, x uint32)

Uint32 writes a uint32 to the Serializer and increases the index.

func (S) Uint64

func (S) Uint64(b []byte, x uint64)

Uint64 writes a uint64 to the Serializer and increases the index.

type Serializer

type Serializer struct {
	Data []byte
	Size int
	Idx  int
}

Serializer is used to Serialize into the Data field.

func (*Serializer) Byte

func (s *Serializer) Byte(b byte)

Byte writes a byte to the Serializer and increases the index.

func (*Serializer) CheckFree

func (s *Serializer) CheckFree(ln int)

CheckFree guarentees that there is at least ln free bytes after index, appending more bytes if necessary.

func (*Serializer) Float32

func (s *Serializer) Float32(f float32)

Float32 writes a float32 to the Serializer and increases the index.

func (*Serializer) Float64

func (s *Serializer) Float64(f float64)

Float64 writes a float64 to the Serializer and increases the index.

func (*Serializer) Int16

func (s *Serializer) Int16(x int16)

Int16 writes a int16 to the Serializer and increases the index.

func (*Serializer) Int32

func (s *Serializer) Int32(x int32)

Int32 writes a int32 to the Serializer and increases the index.

func (*Serializer) Int64

func (s *Serializer) Int64(x int64)

Int64 writes a int64 to the Serializer and increases the index.

func (*Serializer) Int8

func (s *Serializer) Int8(x int8)

Int8 writes a int8 to the Serializer and increases the index.

func (*Serializer) Make

func (s *Serializer) Make() *Serializer

Make will set Data to the length of Size. If Data is already populated, it will be appeded to.

func (*Serializer) Slice

func (s *Serializer) Slice(data []byte)

Slice writes a byte slice to the Serializer and increases the index.

func (*Serializer) String

func (s *Serializer) String(data string)

String writes a string to the Serializer and increases the index.

func (*Serializer) Sub

func (s *Serializer) Sub(ln int) *Serializer

Sub serializer of a specific length.

func (*Serializer) Uint

func (s *Serializer) Uint(size byte, value uint64) byte

Uint serializes the value little-endian. If size is >0, that number of bytes will be written. If size==0, it will write the value with fewest bytes, omitting leading zeros. The returned byte indicates the number of bytes written. Size should be between 0 and 8, but it is not checked. This does not increase the Idx.

func (*Serializer) Uint16

func (s *Serializer) Uint16(x uint16)

Uint16 writes a uint16 to the Serializer and increases the index.

func (*Serializer) Uint32

func (s *Serializer) Uint32(x uint32)

Uint32 writes a uint32 to the Serializer and increases the index.

func (*Serializer) Uint64

func (s *Serializer) Uint64(x uint64)

Uint64 writes a uint64 to the Serializer and increases the index.

func (*Serializer) Uint8

func (s *Serializer) Uint8(x uint8)

Uint8 writes a uint8 to the Serializer and increases the index.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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