compression

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HuffmanEOFSymbol  = protocol.HuffmanEOFSymbol
	HuffmanMaxSymbols = protocol.HuffmanMaxSymbols

	HuffmanMaxNodes        = (HuffmanMaxSymbols)*2 + 1 // +1 for additional EOF symbol
	HuffmanLookupTableBits = 10
	HuffmanLookupTableSize = (1 << HuffmanLookupTableBits)
	HuffmanLookupTableMask = (HuffmanLookupTableSize - 1)
)
View Source
const (
	// StringTerminator is the zero byte that terminates the string
	StringTerminator byte = 0

	// with how many bytes the packer is initialized
	PackerBufferSize = 1024 * 2
)
View Source
const (
	// max bytes that can be received for one integer
	MaxVarintLen32 = 5
)

Variables

View Source
var (
	ErrHuffmanCompress   = errors.New("compression error")
	ErrHuffmanDecompress = errors.New("decompression error")
)
View Source
var (
	// ErrNoDataToUnpack is returned if the compressed array does not have sufficient data to unpack
	ErrNoDataToUnpack = fmt.Errorf("%w: no data", io.EOF)

	// ErrNotAString if no separator after a string is found, the string cannot be unpacked, as there is no string
	ErrNotAString = errors.New("could not unpack string: terminator not found")

	// ErrNotEnoughData is used when the user tries to retrieve more data with NextBytes() than there is available.
	ErrNotEnoughData = errors.New("trying to read more data than available")
)

Functions

func AppendVarint added in v1.4.0

func AppendVarint(buf []byte, x int) []byte

AppendVarint appends the varint-encoded form of x, as generated by PutVarint, to buf and returns the extended buffer.

func PutVarint added in v1.4.0

func PutVarint(buf []byte, x int) int

PutVarint encodes an int32 into buf and returns the number of bytes written. If the buffer is too small, PutVarint will panic. Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign E: is next byte part of the current integer S: Sign of integer Data, Integer bits that follow the sign

func ReadVarint added in v1.4.0

func ReadVarint(r io.ByteReader) (int, error)

ReadVarint can decode a stream of bytes

func Varint added in v1.4.0

func Varint(buf []byte) (i int, n int)

Varint decodes an int from buf and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 with the following meaning:

n == 0: buf too small
n  < 0: value larger than 32 bits (overflow)
        and -n is the number of bytes read

Types

type Huffman added in v1.4.0

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

func NewHuffman added in v1.4.0

func NewHuffman(frequencyTable [HuffmanMaxSymbols]uint32) *Huffman

NewHuffman expects a frequency table aka index -> symbol You can use the default one that can be found under protocol.FrequencyTable You can put the frequency at index HuffmanMaxSymbols -1 to the value 1. It is the EOF value which will ne overwritten anyway.

func (*Huffman) Compress added in v1.4.0

func (h *Huffman) Compress(data, compressed []byte) (written int, err error)

Compress compresses in data to the compressed slice. 'compressed' must be preallocated with enough space to fit the result.

func (*Huffman) Decompress added in v1.4.0

func (h *Huffman) Decompress(data, decompressed []byte) (written int, err error)

Decompress decompresses 'data' and writes the result into 'decompressed'. The decompressed slice must be preallocated to fit the decompressed data.

type MsgPacker added in v1.4.0

type MsgPacker struct {
	Packer
}

func NewMsgPacker added in v1.4.0

func NewMsgPacker(msgType protocol.MsgType, system ...bool) *MsgPacker

NewMsgPacker creates a new message packer It initially sets the message type header and system flag

type Packer

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

Packer compresses data

func NewPacker added in v1.4.0

func NewPacker(buf ...[]byte) *Packer

NewPacker ceates a new Packer with a given default buffer size. You can provide ONE optional buffer that is used instead of the default one

func (*Packer) AddByte added in v1.4.0

func (p *Packer) AddByte(b byte)

func (*Packer) AddBytes added in v1.4.0

func (p *Packer) AddBytes(data []byte)

func (*Packer) AddInt added in v1.4.0

func (p *Packer) AddInt(i int)

func (*Packer) AddString added in v1.4.0

func (p *Packer) AddString(s string)

func (*Packer) Bytes

func (p *Packer) Bytes() []byte

Bytes returns the underlying buffer

func (*Packer) Reset

func (p *Packer) Reset()

Reset internal buffer

func (*Packer) Size

func (p *Packer) Size() int

Size len of the Buffer

type Unpacker

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

Unpacker unpacks received messages

func NewUnpacker added in v1.4.0

func NewUnpacker(data []byte) *Unpacker

NewUnpacker constructs a new Unpacker

func (*Unpacker) Bytes added in v1.4.0

func (u *Unpacker) Bytes() []byte

Bytes returns the not yet used bytes. This operation consumes the buffer leaving it empty

func (*Unpacker) NextByte added in v1.4.0

func (u *Unpacker) NextByte() (b byte, err error)

NextByte returns the next bytes.

func (*Unpacker) NextBytes

func (u *Unpacker) NextBytes(size int) (b []byte, err error)

NextBytes returns the next size bytes.

func (*Unpacker) NextInt

func (u *Unpacker) NextInt() (i int, err error)

NextInt unpacks the next integer

func (*Unpacker) NextString

func (u *Unpacker) NextString() (s string, err error)

NextString unpacks the next string from the message

func (*Unpacker) Reset

func (u *Unpacker) Reset(b []byte)

Reset resets the underlying byte slice to a new slice

func (*Unpacker) Size

func (u *Unpacker) Size() int

Size of the underlying buffer

Jump to

Keyboard shortcuts

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