bytestream

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

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

Go to latest
Published: Aug 25, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

Package bytestream

This package is 100% compatible with the stdlib and mainly works with the bytes.Buffer and bytes.Reader interfaces.
Installation
go get -u github.com/amaanq/bytestream
Usage
import (
    "github.com/amaanq/bytestream"
)

func main() {
	Writer := &bytestream.Writer{
		buffer: bytes.NewBuffer([]byte{}),
	}
	Writer.WriteCompressedString("hello there!")
	Writer.WriteInt32(40, bytestream.BigEndian)
	// You've now packed hello there! and 40 into a very small byte array to do with what you please..
}
NOTE: This package is a custom protocol defined by a game company for their TCP data streams.

As such, I do not recommend using it to detect/use with protocols already implemented. However, you can use this for your own protocol for whatever it may be.

LogicLongs are now in the public domain seeing as several other public repositories explain them and showcase how to pack/unpack them.

Enjoy using this!

Documentation

Index

Constants

View Source
const (
	ByteSize      = iota + 1
	BoolSize      = ByteSize
	Int8Size      = ByteSize
	Int16Size     = Int8Size * 2
	Int24Size     = Int16Size + Int8Size
	Int32Size     = Int16Size * 2
	Int64Size     = Int32Size * 2
	LongSize      = 8 // Not tested if this works on 32-bit machines...
	LogicLongSize = LongSize
	LongLongSize  = LongSize
)
View Source
const Is64Bit = uint64(^uintptr(0)) == ^uint64(0)

Variables

This section is empty.

Functions

This section is empty.

Types

type Endianness

type Endianness bool
const (
	LittleEndian Endianness = true
	BigEndian    Endianness = false
)

type Reader

type Reader struct {
	Reader *bytes.Buffer
}

func NewReader

func NewReader(data []byte) *Reader

func (*Reader) ReadBool

func (r *Reader) ReadBool() (bool, int8, error)

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(length int) ([]byte, error)

func (*Reader) ReadCompressedString

func (r *Reader) ReadCompressedString() (string, error)

func (*Reader) ReadInt16

func (r *Reader) ReadInt16(endianness Endianness) (int16, error)

func (*Reader) ReadInt24

func (r *Reader) ReadInt24(endianness Endianness) (int32, error)

We are using an int32 to represent an int24 since the stdlib doesn't provide a type for this. However, this int32 will only read 3 bytes and cannot go above the max size for an int24 (8388607 or 0x7FFFFF) :)

func (*Reader) ReadInt32

func (r *Reader) ReadInt32(endianness Endianness) (int32, error)

func (*Reader) ReadInt64

func (r *Reader) ReadInt64(endianness Endianness) (int64, error)

func (*Reader) ReadInt8

func (r *Reader) ReadInt8() (int8, error)

func (*Reader) ReadIntSize

func (r *Reader) ReadIntSize(size uint8, endianness Endianness) (int64, error)

func (*Reader) ReadLong

func (r *Reader) ReadLong(endianness Endianness) (int, error)

func (*Reader) ReadLongLong

func (r *Reader) ReadLongLong(endianness Endianness) (int64, error)

func (*Reader) ReadString

func (r *Reader) ReadString() (string, error)

func (*Reader) ReadStringSize

func (r *Reader) ReadStringSize(ssize_t int) (string, error)

func (*Reader) ReadUInt16

func (r *Reader) ReadUInt16(endianness Endianness) (uint16, error)

func (*Reader) ReadUInt24

func (r *Reader) ReadUInt24(endianness Endianness) (uint32, error)

We are using a uint32 to represent a uint24 since the stdlib doesn't provide a type for this. However, this uint32 will only read 3 bytes and cannot go above the max size for a uint24 (16777215 or 0xFFFFFF) :)

func (*Reader) ReadUInt32

func (r *Reader) ReadUInt32(endianness Endianness) (uint32, error)

func (*Reader) ReadUInt64

func (r *Reader) ReadUInt64(endianness Endianness) (uint64, error)

func (*Reader) ReadUInt8

func (r *Reader) ReadUInt8() (uint8, error)

func (*Reader) ReadUIntSize

func (r *Reader) ReadUIntSize(size uint8, endianness Endianness) (int64, error)

TODO allow reading any int with size from int8 to int64 (mostly allow 40/48/56...)

func (*Reader) ReadUVarInt

func (r *Reader) ReadUVarInt() (uint64, error)

func (*Reader) ReadUnsignedLong

func (r *Reader) ReadUnsignedLong(endianness Endianness) (uint, error)

func (*Reader) ReadUnsignedLongLong

func (r *Reader) ReadUnsignedLongLong(endianness Endianness) (uint64, error)

func (*Reader) ReadVarInt

func (r *Reader) ReadVarInt() (int64, error)

type Sign

type Sign bool
const (
	Signed   Sign = true
	Unsigned Sign = false
)

type Writer

type Writer struct {
	Buffer *bytes.Buffer
}

func NewWriter

func NewWriter() *Writer

func (*Writer) WriteBool

func (w *Writer) WriteBool(data bool, count int8) error

func (*Writer) WriteBytes

func (w *Writer) WriteBytes(bytes []byte) error

func (*Writer) WriteCompressedString

func (w *Writer) WriteCompressedString(data string) error

func (*Writer) WriteInt16

func (w *Writer) WriteInt16(data int16, endianness Endianness) error

func (*Writer) WriteInt24

func (w *Writer) WriteInt24(data int32, endianness Endianness) error

func (*Writer) WriteInt32

func (w *Writer) WriteInt32(data int32, endianness Endianness) error

func (*Writer) WriteInt64

func (w *Writer) WriteInt64(data int64, endianness Endianness) error

func (*Writer) WriteInt8

func (w *Writer) WriteInt8(data int8) error

func (*Writer) WriteIntSize

func (w *Writer) WriteIntSize(data int64, size uint8, endianness Endianness) error

func (*Writer) WriteLong

func (w *Writer) WriteLong(data int64, endianness Endianness) error

func (*Writer) WriteLongLong

func (w *Writer) WriteLongLong(data int64, endianness Endianness) error

func (*Writer) WriteString

func (w *Writer) WriteString(data string) error

func (*Writer) WriteStringSize

func (w *Writer) WriteStringSize(data string, bytesize int8) error

This implementation writes the size of the string as a signed int of size bytesize, -1 will write 0xFFs for bytesize, and not write the string at all.

func (*Writer) WriteUInt16

func (w *Writer) WriteUInt16(data uint16, endianness Endianness) error

func (*Writer) WriteUInt24

func (w *Writer) WriteUInt24(data uint32, endianness Endianness) error

func (*Writer) WriteUInt32

func (w *Writer) WriteUInt32(data uint32, endianness Endianness) error

func (*Writer) WriteUInt64

func (w *Writer) WriteUInt64(data uint64, endianness Endianness) error

func (*Writer) WriteUInt8

func (w *Writer) WriteUInt8(data uint8) error

func (*Writer) WriteUIntSize

func (w *Writer) WriteUIntSize(data int64, size uint8, endianness Endianness) error

TODO allow writing any int with size from int8 to int64 (mostly allow 40/48/56...)

func (*Writer) WriteUVarInt

func (w *Writer) WriteUVarInt(data uint64) error

func (*Writer) WriteUnsignedLong

func (w *Writer) WriteUnsignedLong(data uint64, endianness Endianness) error

func (*Writer) WriteUnsignedLongLong

func (w *Writer) WriteUnsignedLongLong(data uint64, endianness Endianness) error

func (*Writer) WriteVarInt

func (w *Writer) WriteVarInt(data int64) error

Jump to

Keyboard shortcuts

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