bytebuilder

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 4 Imported by: 1

README

bytebuilder

Go Reference

Golang API to manipulate byte slices.

Get:

go get github.com/g0rbe/go-bytebuilder@latest

Get the latest tag (if Go module proxy is not updated):

go get "github.com/g0rbe/go-bytebuilder@$(curl -s 'https://api.github.com/repos/g0rbe/go-bytebuilder/tags' | jq -r '.[0].name')"

Get the latest commit (if Go module proxy is not updated):

go get "github.com/g0rbe/go-bytebuilder@$(curl -s 'https://api.github.com/repos/g0rbe/go-bytebuilder/commits' | jq -r '.[0].sha')"

Author

System administrator service and Cybersecurity for small and medium-sized businesses in and around Győr.

Documentation

Index

Constants

View Source
const IntSize = intSize

IntSize is the size in bits of an int or uint value.

View Source
const NativeEndian = LittleEndian

Variables

This section is empty.

Functions

func ReadBigInt16 added in v0.8.0

func ReadBigInt16(b *[]byte) (int16, bool)

ReadBigInt16 removes the first bytes from b and returns it as an int16 in big-endian order. The bool indicates whether the read was successful.

func ReadBigInt24 added in v0.8.0

func ReadBigInt24(b *[]byte) (int32, bool)

ReadInt24 removes the first bytes from b and returns it as a int32 in big-endian order. The bool indicates whether the read was successful.

func ReadBigInt32 added in v0.8.0

func ReadBigInt32(b *[]byte) (int32, bool)

ReadBigInt32 removes the first bytes from b and returns it as an int32 in big-endian order. The bool indicates whether the read was successful.

func ReadBigInt64 added in v0.8.0

func ReadBigInt64(b *[]byte) (int64, bool)

ReadInt64 removes the first bytes from b and returns it as an int64 in big-endian order. The bool indicates whether the read was successful.

func ReadBigUint16 added in v0.8.0

func ReadBigUint16(b *[]byte) (uint16, bool)

Uint16BE removes the first bytes from b and returns it as an uint16 in big-endian order. The bool indicates whether the read was successful.

func ReadBigUint24 added in v0.8.0

func ReadBigUint24(b *[]byte) (uint32, bool)

ReadBigUint24 removes the first bytes from b and returns it as a uint32 in big-endian order. The bool indicates whether the read was successful.

func ReadBigUint32 added in v0.8.0

func ReadBigUint32(b *[]byte) (uint32, bool)

ReadBigUint32 removes the first bytes from b and returns it as an uint32 in big-endian order. The bool indicates whether the read was successful.

func ReadBigUint64 added in v0.8.0

func ReadBigUint64(b *[]byte) (uint64, bool)

ReadUint64 removes the first bytes from b and returns it as an uint64 in big-endian order. The bool indicates whether the read was successful.

func ReadByte added in v0.8.0

func ReadByte(b *[]byte) (byte, bool)

ReadByte removes the first byte from b and returns it as a byte. The bool indicates whether the read was successful.

func ReadBytes

func ReadBytes(b *[]byte, n int) []byte

ReadBytes removes the first n bytes from b and returns it. If the read failed, returns nil.

func ReadInt16

func ReadInt16(b *[]byte) (int16, bool)

ReadInt16 removes the first bytes from b and returns it as an int16 in native endian order. The bool indicates whether the read was successful.

func ReadInt24

func ReadInt24(b *[]byte) (int32, bool)

ReadInt24 removes the first bytes from b and returns it as a int32 in native endian order. The bool indicates whether the read was successful.

func ReadInt32

func ReadInt32(b *[]byte) (int32, bool)

ReadInt32 removes the first bytes from b and returns it as an int32 in native-endian order. The bool indicates whether the read was successful.

func ReadInt64

func ReadInt64(b *[]byte) (int64, bool)

ReadInt64 removes the first bytes from b and returns it as an int64 in native-endian order. The bool indicates whether the read was successful.

func ReadInt8

func ReadInt8(b *[]byte) (int8, bool)

ReadInt8 removes the first byte from b and returns it as an int8. The bool indicates whether the read was successful.

func ReadLittleInt16 added in v0.8.0

func ReadLittleInt16(b *[]byte) (int16, bool)

ReadLittleInt16 removes the first bytes from b and returns it as an int16 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleInt24 added in v0.8.0

func ReadLittleInt24(b *[]byte) (int32, bool)

ReadInt24 removes the first bytes from b and returns it as a int32 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleInt32 added in v0.8.0

func ReadLittleInt32(b *[]byte) (int32, bool)

ReadLittleInt32 removes the first bytes from b and returns it as an int32 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleInt64 added in v0.8.0

func ReadLittleInt64(b *[]byte) (int64, bool)

ReadInt64 removes the first bytes from b and returns it as an int64 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleUint16 added in v0.8.0

func ReadLittleUint16(b *[]byte) (uint16, bool)

ReadLittleUint16 removes the first bytes from b and returns it as an uint16 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleUint24 added in v0.8.0

func ReadLittleUint24(b *[]byte) (uint32, bool)

ReadLittleUint24 removes the first bytes from b and returns it as a uint32 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleUint32 added in v0.8.0

func ReadLittleUint32(b *[]byte) (uint32, bool)

ReadLittleUint32 removes the first bytes from b and returns it as an uint32 in little-endian order. The bool indicates whether the read was successful.

func ReadLittleUint64 added in v0.8.0

func ReadLittleUint64(b *[]byte) (uint64, bool)

ReadUint64 removes the first bytes from b and returns it as an uint64 in little-endian order. The bool indicates whether the read was successful.

func ReadReaderBytes

func ReadReaderBytes(in io.Reader, n int) ([]byte, error)

ReadReaderBytes reads n byte from in. At the end of the file, io.EOF is returned.

func ReadReaderUint16

func ReadReaderUint16(in io.Reader) (uint16, error)

ReadReaderUint16 reads bytes from in and returns it as an uint16. At the end of the file io.EOF returned.

func ReadReaderUint24

func ReadReaderUint24(in io.Reader) (uint32, error)

ReadReaderUint24 reads bytes from in and returns it as an uint32. At the end of the file, io.EOF is returned.

func ReadReaderUint32

func ReadReaderUint32(in io.Reader) (uint32, error)

ReadReaderUint32 reads bytes from in and returns it as an uint32. At the end of the file io.EOF returned.

func ReadReaderUint8

func ReadReaderUint8(in io.Reader) (uint8, error)

ReadReaderUint8 reads a byte from in and returns is as an uint8. At the end of the file, io.EOF is returned.

func ReadUint16

func ReadUint16(b *[]byte) (uint16, bool)

ReadUint16 removes the first bytes from b and returns it as an uint16 in native endian order. The bool indicates whether the read was successful.

func ReadUint24

func ReadUint24(b *[]byte) (uint32, bool)

ReadUint24 removes the first bytes from b and returns it as a uint32 in native endian order. The bool indicates whether the read was successful.

func ReadUint32

func ReadUint32(b *[]byte) (uint32, bool)

ReadUint32 removes the first bytes from b and returns it as an uint32 in native-endian order. The bool indicates whether the read was successful.

func ReadUint64

func ReadUint64(b *[]byte) (uint64, bool)

ReadUint64 removes the first bytes from b and returns it as an uint64 in native-endian order. The bool indicates whether the read was successful.

func ReadUint8

func ReadUint8(b *[]byte) (uint8, bool)

ReadUint8 removes the first byte from b and returns it as an uint8. The bool indicates whether the read was successful.

func Skip

func Skip(b *[]byte, n int) bool

Skip removes the first n bytes from b. Returns whether it was successful.

func SkipReader

func SkipReader(in io.Reader, n int) error

ReadReaderSkip skips n bytes in in. At the end of the file, io.EOF is returned.

func WriteBigInt16 added in v0.8.0

func WriteBigInt16(b *[]byte, v int16)

WriteBigInt16 appends v at the end of b in big-endian order.

func WriteBigInt24 added in v0.8.0

func WriteBigInt24(b *[]byte, v int32)

WriteInt24 appends v at the end of b in big-endian order.

func WriteBigInt32 added in v0.8.0

func WriteBigInt32(b *[]byte, v int32)

WriteInt32 appends v at the end of b in big-endian order.

func WriteBigInt64 added in v0.8.0

func WriteBigInt64(b *[]byte, v int64)

WriteInt64 appends v at the end of b in big-endian order.

func WriteBigUint16 added in v0.8.0

func WriteBigUint16(b *[]byte, v uint16)

WriteBigUint16 appends v at the end of b in big-endian order.

func WriteBigUint24 added in v0.8.0

func WriteBigUint24(b *[]byte, v uint32)

WriteBigUint24 appends v at the end of b in big-endian order.

func WriteBigUint32 added in v0.8.0

func WriteBigUint32(b *[]byte, v uint32)

WriteUint32 appends v at the end of b in big-endian order.

func WriteBigUint64 added in v0.8.0

func WriteBigUint64(b *[]byte, v uint64)

WriteUint64 appends v at the end of b in big-endian order.

func WriteByte added in v0.8.0

func WriteByte(b *[]byte, v byte)

WriteByte appends v at the end of b.

func WriteBytes

func WriteBytes(b *[]byte, v ...byte)

WriteBytes appends bytes at the end of b.

func WriteInt16

func WriteInt16(b *[]byte, v int16)

WriteInt16 appends v at the end of b in native-endian order.

func WriteInt24

func WriteInt24(b *[]byte, v int32)

WriteInt24 appends v at the end of b in native-endian order.

func WriteInt32

func WriteInt32(b *[]byte, v int32)

WriteInt32 appends v at the end of b in native-endian order.

func WriteInt64

func WriteInt64(b *[]byte, v int64)

WriteInt64 appends v at the end of b in native-endian order.

func WriteInt8

func WriteInt8(b *[]byte, v int8)

WriteInt8 appends v at the end of b.

func WriteLittleInt16 added in v0.8.0

func WriteLittleInt16(b *[]byte, v int16)

WriteLittleInt16 appends v at the end of b in little-endian order.

func WriteLittleInt24 added in v0.8.0

func WriteLittleInt24(b *[]byte, v int32)

WriteInt24 appends v at the end of b in little-endian order.

func WriteLittleInt32 added in v0.8.0

func WriteLittleInt32(b *[]byte, v int32)

WriteInt32 appends v at the end of b in little-endian order.

func WriteLittleInt64 added in v0.8.0

func WriteLittleInt64(b *[]byte, v int64)

WriteInt64 appends v at the end of b in little-endian order.

func WriteLittleUint16 added in v0.8.0

func WriteLittleUint16(b *[]byte, v uint16)

WriteLittleUint16 appends v at the end of b in little-endian order.

func WriteLittleUint24 added in v0.8.0

func WriteLittleUint24(b *[]byte, v uint32)

WriteLittleUint24 appends v at the end of b in little-endian order.

func WriteLittleUint32 added in v0.8.0

func WriteLittleUint32(b *[]byte, v uint32)

WriteUint32 appends v at the end of b in little-endian order.

func WriteLittleUint64 added in v0.8.0

func WriteLittleUint64(b *[]byte, v uint64)

WriteUint64 appends v at the end of b in little-endian order.

func WriteUint16

func WriteUint16(b *[]byte, v uint16)

WriteUint16 appends v at the end of b in native-endian order.

func WriteUint24

func WriteUint24(b *[]byte, v uint32)

WriteUint24 appends v at the end of b in native-endian order.

func WriteUint32

func WriteUint32(b *[]byte, v uint32)

WriteUint32 appends v at the end of b in native-endian order.

func WriteUint64

func WriteUint64(b *[]byte, v uint64)

WriteUint64 appends v at the end of b in native-endian order.

func WriteUint8

func WriteUint8(b *[]byte, v uint8)

WriteUint8 appends v at the end of b.

func WriteWriterBytes

func WriteWriterBytes(in io.Writer, bytes ...byte) error

WriteWriterBytes appends v at the end of in.

func WriteWriterUint16

func WriteWriterUint16(in io.Writer, v uint16) error

WriteWriterUint16 appends v at the end of in.

func WriteWriterUint24

func WriteWriterUint24(in io.Writer, v uint32) error

WriteWriterUint24 appends v at the end of in.

func WriteWriterUint32

func WriteWriterUint32(in io.Writer, v uint32) error

WriteWriterUint32 appends v at the end of in.

func WriteWriterUint8

func WriteWriterUint8(in io.Writer, v uint8) error

WriteWriterUint8 appends v at the end of in.

Types

type Buffer

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

func NewBuffer

func NewBuffer(bytes []byte) Buffer

func NewEmpty

func NewEmpty() Buffer

NewEmpty creates a Buffer with a zero length byte slice.

func NewWithSize

func NewWithSize(n int) Buffer

NewWithSize creates a Buffer with a byte slice of length n.

func ReadAll

func ReadAll(r io.Reader) (Buffer, error)

ReadAll reads from r until an error or EOF and returns a Buffer from the data it read. A successful call returns err == nil, not err == EOF. Because ReadAll is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.

func (*Buffer) Bytes

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

Bytes returns the underlying byte slice.

func (*Buffer) BytesPointer

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

BytesPointer returns a pointer to the underlying byte slice.

func (*Buffer) Empty

func (b *Buffer) Empty() bool

Empty returns whether b is empty.

func (*Buffer) ReadBytes

func (b *Buffer) ReadBytes(n int) []byte

ReadBytes removes the first n bytes from b and returns it. If the read failed, returns nil.

func (*Buffer) ReadGMTUnixTime32

func (b *Buffer) ReadGMTUnixTime32() (time.Time, bool)

ReadGMTUnixTime32 removes the first bytes from b and returns it as an unix time. The bool indicates whether the read was successful.

func (*Buffer) ReadInt

func (b *Buffer) ReadInt() (int, bool)

ReadInt removes the first bytes (depends on IntSize) from b and returns it as an int.

func (*Buffer) ReadInt16

func (b *Buffer) ReadInt16() (int16, bool)

ReadInt16 removes the first bytes from b and returns it as an int16. The bool indicates whether the read was successful.

func (*Buffer) ReadInt24

func (b *Buffer) ReadInt24() (int32, bool)

ReadInt24 removes the first bytes from b and returns it as a int32. The bool indicates whether the read was successful.

func (*Buffer) ReadInt32

func (b *Buffer) ReadInt32() (int32, bool)

ReadInt32 removes the first bytes from b and returns it as an int32. The bool indicates whether the read was successful.

func (*Buffer) ReadInt64

func (b *Buffer) ReadInt64() (int64, bool)

ReadInt64 removes the first bytes from b and returns it as an int64. The bool indicates whether the read was successful.

func (*Buffer) ReadInt8

func (b *Buffer) ReadInt8() (int8, bool)

ReadInt8 removes the first byte from b and returns it as an int8. The bool indicates whether the read was successful.

func (*Buffer) ReadUint16

func (b *Buffer) ReadUint16() (uint16, bool)

ReadUint16 removes the first bytes from b and returns it as an uint16. The bool indicates whether the read was successful.

func (*Buffer) ReadUint24

func (b *Buffer) ReadUint24() (uint32, bool)

ReadUint24 removes the first bytes from b and returns it as a uint32. The bool indicates whether the read was successful.

func (*Buffer) ReadUint32

func (b *Buffer) ReadUint32() (uint32, bool)

ReadUint32 removes the first bytes from b and returns it as an uint32. The bool indicates whether the read was successful.

func (*Buffer) ReadUint64

func (b *Buffer) ReadUint64() (uint64, bool)

ReadUint64 removes the first bytes from b and returns it as an uint64. The bool indicates whether the read was successful.

func (*Buffer) ReadUint8

func (b *Buffer) ReadUint8() (uint8, bool)

ReadUint8 removes the first byte from b and returns it as an uint8. The bool indicates whether the read was successful.

func (*Buffer) ReadVector

func (b *Buffer) ReadVector(bitSize int) ([]byte, bool)

ReadVector reads the length of bytes then the bytes itself. The length type is depend on bitSize (eg.: uint8, uint16, uint24, uint32, uint64). Therefore, bitSize must be 8/16/24/32/64. If bitSize is an invalid number, this function panics.

func (*Buffer) Size

func (b *Buffer) Size() int

Size returns the size of the underlying byte slice

func (*Buffer) Skip

func (b *Buffer) Skip(n int) bool

Skip removes the first n bytes from b. Returns whether it was successful.

func (*Buffer) WriteBytes

func (b *Buffer) WriteBytes(bytes ...byte)

WriteBytes appends bytes at the end of b.

func (*Buffer) WriteGMTUnixTime32

func (b *Buffer) WriteGMTUnixTime32(time time.Time)

WriteGMTUnixTime32 appends time to b in 32 bit unix format.

func (*Buffer) WriteInt

func (b *Buffer) WriteInt(v int)

WriteInt append v at the end of b with size bitSize. The number of bytes depends on IntSize.

func (*Buffer) WriteInt16

func (b *Buffer) WriteInt16(v int16)

WriteInt16 appends v at the end of b.

func (*Buffer) WriteInt24

func (b *Buffer) WriteInt24(v int32)

WriteInt24 appends v at the end of b.

func (*Buffer) WriteInt32

func (b *Buffer) WriteInt32(v int32)

WriteInt32 appends v at the end of b.

func (*Buffer) WriteInt64

func (b *Buffer) WriteInt64(v int64)

WriteInt64 appends v at the end of b.

func (*Buffer) WriteInt8

func (b *Buffer) WriteInt8(v int8)

WriteInt8 appends v at the end of b.

func (*Buffer) WriteRandom

func (b *Buffer) WriteRandom(n int)

WriteRandom appends n random bytes to b. If failed to read random, this function panics. This function gets random numbers from `crypto/rand` package.

func (*Buffer) WriteUint16

func (b *Buffer) WriteUint16(v uint16)

WriteUint16 appends v at the end of b.

func (*Buffer) WriteUint24

func (b *Buffer) WriteUint24(v uint32)

WriteUint24 appends v at the end of b.

func (*Buffer) WriteUint32

func (b *Buffer) WriteUint32(v uint32)

WriteUint32 appends v at the end of b.

func (*Buffer) WriteUint64

func (b *Buffer) WriteUint64(v uint64)

WriteUint64 appends v at the end of b.

func (*Buffer) WriteUint8

func (b *Buffer) WriteUint8(v uint8)

WriteUint8 appends v at the end of b.

func (*Buffer) WriteVector

func (b *Buffer) WriteVector(v []byte, bitSize int)

WriteVector appends the length of bytes then the bytes itself. The length type is depend on bitSize (eg.: uint8, uint16, uint24, uint32, uint64). Therefore, bitSize must be 8/16/24/32/64. If bitSize is an invalid number, this function panics.

type Endianness added in v0.8.0

type Endianness byte
const (
	LittleEndian Endianness = iota
	BigEndian
)

Jump to

Keyboard shortcuts

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