util

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseByte1

func ParseByte1(data []byte, bitStartIndex uint16) (byte, error)

ParseByte1 parses 1 bit of data from the data array, starting at the given index

func ParseByte2 added in v0.2.0

func ParseByte2(data []byte, bitStartIndex uint16) (byte, error)

ParseByte2 parses 2 bits of data from the data array, starting at the given index

func ParseByte4

func ParseByte4(data []byte, bitStartIndex uint16) (byte, error)

ParseByte4 parses 4 bits of data from the data array, starting at the given index

func ParseByte6

func ParseByte6(data []byte, bitStartIndex uint16) (byte, error)

ParseByte6 parses 6 bits of data from the data array, starting at the given index

func ParseByte8

func ParseByte8(data []byte, bitStartIndex uint16) (byte, error)

ParseByte8 parses 8 bits of data from the data array, starting at the given index

func ParseUInt12

func ParseUInt12(data []byte, bitStartIndex uint16) (uint16, error)

ParseUInt12 parses 12 bits of data from the data array, starting at the given index

func ParseUInt16

func ParseUInt16(data []byte, bitStartIndex uint16) (uint16, error)

ParseUInt16 parses a 16-bit integer from the data array, starting at the given index

Types

type BitStream

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

func NewBitStream

func NewBitStream(b []byte) *BitStream

NewBitStream creates a new bitstream object

func NewBitStreamForWrite added in v0.2.0

func NewBitStreamForWrite() *BitStream

func NewBitStreamFromBase64

func NewBitStreamFromBase64(encoded string) (*BitStream, error)

NewBitStreamFromBase64 creates a new bit stream object from a base64-url encoded string

func (*BitStream) Base64Encode added in v0.2.0

func (bs *BitStream) Base64Encode() []byte

Base64Encode applies base64 encoding on the data in buffer.

func (*BitStream) GetPosition

func (bs *BitStream) GetPosition() uint16

GetPosition reads out the position of the bit pointer in the bit stream

func (*BitStream) Len

func (bs *BitStream) Len() uint16

Len returns the number of bytes in the BitStream

func (*BitStream) ReadByte1

func (bs *BitStream) ReadByte1() (byte, error)

ReadByte1 reads 1 bit from the bitstream, advancing the pointer

func (*BitStream) ReadByte2 added in v0.2.0

func (bs *BitStream) ReadByte2() (byte, error)

ReadByte2 reads 2 bits fron the bitstream, advancing the pointer

func (*BitStream) ReadByte4

func (bs *BitStream) ReadByte4() (byte, error)

ReadByte4 reads 4 bits from the bitstream, advancing the pointer

func (*BitStream) ReadByte6

func (bs *BitStream) ReadByte6() (byte, error)

ReadByte6 reads 6 bits from the bitstream, advancing the pointer

func (*BitStream) ReadByte8

func (bs *BitStream) ReadByte8() (byte, error)

ReadByte8 reads 8 bits from the bitstream, advancing the pointer

func (*BitStream) ReadFibonacciInt

func (bs *BitStream) ReadFibonacciInt() (uint16, error)

ReadFibonacciInt parses 1 fibonacci encoded int from the data array, starting at the given index Note that 0 cannot be fibonacci encoded, and thus will only be seen if there is an error.

func (*BitStream) ReadFibonacciRange

func (bs *BitStream) ReadFibonacciRange() (*IntRange, error)

ReadFibonacciRange

func (*BitStream) ReadIntRange

func (bs *BitStream) ReadIntRange() (*IntRange, error)

ReadIntRange parses a Range(Int) and returns an IntRange struct

func (*BitStream) ReadTwoBitField added in v0.2.0

func (bs *BitStream) ReadTwoBitField(numFields int) ([]byte, error)

func (*BitStream) ReadUInt12

func (bs *BitStream) ReadUInt12() (uint16, error)

ReadUInt12 reads 12 bits from the bitstream, advancing the pointer

func (*BitStream) ReadUInt16

func (bs *BitStream) ReadUInt16() (uint16, error)

ReadUInt16 reads 16 bits from the bitstream, advancing the pointer

func (*BitStream) Reset added in v0.2.0

func (bs *BitStream) Reset()

Reset clears all the data a BitStream holds.

func (*BitStream) SetPosition

func (bs *BitStream) SetPosition(pos uint16)

SetPosition sets the position of the bit pointer in the bit stream

func (*BitStream) WriteByte1 added in v0.2.0

func (bs *BitStream) WriteByte1(b byte)

WriteByte1 writes the rightmost bit in b into the BitStream.

func (*BitStream) WriteByte2 added in v0.2.0

func (bs *BitStream) WriteByte2(b byte)

WriteByte2 writes the rightmost 2 bits in b into the BitStream.

func (*BitStream) WriteByte4 added in v0.2.0

func (bs *BitStream) WriteByte4(b byte)

WriteByte4 writes the rightmost 4 bits in b into the BitStream.

func (*BitStream) WriteByte6 added in v0.2.0

func (bs *BitStream) WriteByte6(b byte)

WriteByte6 writes the rightmost 6 bits in b into the BitStream.

func (*BitStream) WriteByte8 added in v0.2.0

func (bs *BitStream) WriteByte8(b byte)

WriteByte8 writes a full byte b into the BitStream.

func (*BitStream) WriteFibonacciInt added in v0.2.0

func (bs *BitStream) WriteFibonacciInt(num uint16) error
WriteFibonacciInt writes int based on Fibonacci Coding https://en.wikipedia.org/wiki/Fibonacci_coding.

By definition, Fibonacci numbers are numbers that are the sum of the two Fibonacci numbers that come before. The numbers are [excluding 0, 1,] 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3), 8 (3+5), etc. All of the integers that are larger and equal to 0 are able to split into a combination of at least one Fibonacci numbers. For instance, 6 refers to 1+5, while 7 refers to 2+5, where 1, 2, 5 are Fibonacci numbers.

To encode an integer using Fibonacci Coding, just follow these steps: 1.Find the largest Fibonacci number equal to or less than N; subtract this number from N, keeping track of the remainder. 2.If the number subtracted was the i-th Fibonacci number F(i), put a 1 in place i−2 in the code word (counting the left-most digit as place 0). 3.Repeat the previous steps, substituting the remainder for N, until a remainder of 0 is reached. 4.Place an additional 1 after the rightmost digit in the code word.

So a Fibonacci encoded bit sequence is all about marking the corresponding bit as 1 while leaving the others as 0. Here are some examples:

FibonacciInt(1) = 11			1-> (1*1)->1
FibonacciInt(2) = 011 			2-> (1*1+2*1)->01
FibonacciInt(3) = 0011			3-> (1*0+2*0+3*1)->001
FibonacciInt(4) = 1011			4-> (1*1+2*0+3*1)->101
FibonacciInt(12) = 101011		12->(1*1+2*0+3*1+5*0+8*1)->10101

According to the algorithm, no sequential ones can be found in the bit sequence, and the final bit is always a single 1, which makes it easy to extract several continuous Fibonacci encoded integers out from a bit stream if we place an additional 1 in the end, since 11 could be an end-of-sequence indicator. The advantage of this is, that a sequence of numbers can be encoded into a sequence of bits without the need to know the length of the bit sequences in advance.

func (*BitStream) WriteIntRange added in v0.2.0

func (bs *BitStream) WriteIntRange(intRange *IntRange) error

WriteIntRange writes an IntRange object into the BitStream.

The basic idea is 12 bits for size, 1 bit indicating if a range refers to a single number or not, followed by one or two Fibonacci encoded numbers, and then followed by other ranges fell behind.

For example, an IntRange consisting of [1, 1], [3, 4] would be encoded into 000000000010 0 11 1 0011 11. There are two ranges, so the size is encoded into 000000000010. The first range [1, 1] refers to a single integer 1, so the indicator is 0, followed by Fibonacci encoded 1, while the next range [3, 4] refers to a range containing not only one integer, so the indicator is 1, followed by Fibonacci encoded 3 and 1 (4-3).

func (*BitStream) WriteTwoBitField added in v0.2.0

func (bs *BitStream) WriteTwoBitField(bList []byte)

WriteTwoBitField encapsulates WriteByte2 to get convenience to some extent, when encoding GPP strings.

func (*BitStream) WriteUInt12 added in v0.2.0

func (bs *BitStream) WriteUInt12(b uint16)

WriteUInt12 writes the rightmost 12 bits in b into the BitStream. For instance, the input b is 0xff01, the effective part refers to the rightmost 12 bits, which should be 0xf01.

func (*BitStream) WriteUInt16 added in v0.2.0

func (bs *BitStream) WriteUInt16(b uint16)

WriteUInt16 writes a full uint16 (two bytes) into the BitStream.

type IRange

type IRange struct {
	StartID uint16
	EndID   uint16
}

func (IRange) Contains

func (r IRange) Contains(id uint16) bool

Contains checks to see if an ID is contained within a range

type IntRange

type IntRange struct {
	Size  uint16
	Range []IRange
	Max   uint16
}

func (*IntRange) IsSet

func (ir *IntRange) IsSet(id uint16) bool

IsSet checks to see if an ID is contained within a range set

Jump to

Keyboard shortcuts

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