b64buff

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package b64buff implements a bit buffer that can encode bit vectors as URL string.

The bit buffer allows to read and write any number of bits. The encoding is similar to Base64 but different. It has no padding. Currently it cannot be changed.

Example
buffer := Buffer{}
buffer.WriteUInt32(0x57, 7)
a, _ := buffer.ReadUInt32(3)
b, _ := buffer.ReadUInt32(4)
fmt.Println(a, b)
Output:

5 7

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	WrongNbBits   = errors.New("Impossible number of bits")
	NotEnoughData = errors.New("No enough data to read")
	NotAligned    = errors.New("Buffer not aligned")
	EncodingError = errors.New("Error in encoding")
)

Functions

func RandomString

func RandomString(length uint32) (ret string, err error)

RandomString produces a random readable string of the given length. The returned string is a valid B64 encoded random value.

func RandomUInt32

func RandomUInt32(nbBits uint8) (uint32, error)

RandomUInt32 returns a uniform randow value between 0 and 2^nbBits - 1.

Types

type Buffer

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

Buffer is a bit buffer.

func NewRandom

func NewRandom(nbBits uint32) (ret *Buffer, err error)

Random creates a Buffer containing at least nbBits random bits.

func (*Buffer) AlignRead

func (self *Buffer) AlignRead() (ret byte)

AlignRead forces read to be aligned, by discarding surnumerous bits.

Read is aligned when the number of bits read from the buffer can be divided by 6. The discarded bits are the less significant bits that would have been returned by a call to ReadUInt32. Those bits are returned by the method.

func (*Buffer) B64Reader

func (self *Buffer) B64Reader() io.Reader

B64Reader returns an encoded reader on the Buffer.

Calling Read on the returned value fails if read is not aligned. Read is aligned when the number of bits read from the buffer can be divided by 6.

func (*Buffer) Len

func (self *Buffer) Len() uint32

Len returns the number of bits in the buffer.

func (*Buffer) ReadAllB64

func (self *Buffer) ReadAllB64() (ret string, err error)

ReadAllB64 reads the whole buffer to the string.

The call fails if read is not aligned. Read is aligned when the number of bits read from the buffer can be divided by 6.

func (*Buffer) ReadUInt32

func (self *Buffer) ReadUInt32(nbBits uint8) (ret uint32, err error)

ReadUInt32 reads nbBits of the buffer into the lower bits of ret.

func (*Buffer) WriteB64

func (self *Buffer) WriteB64(str string) (err error)

WriteB64 writes the whole string into the buffer.

The string must have been correctly encoded by a call to ReadAllB64.

The call fails if write is not aligned. Write is aligned when the number of bits written to the buffer can be divided by 6.

func (*Buffer) WriteUInt32

func (self *Buffer) WriteUInt32(data uint32, nbBits uint8) error

WriteUInt32 writes the lower nbBits of data to the buffer.

Jump to

Keyboard shortcuts

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