etc

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: MIT Imports: 21 Imported by: 62

README

Etc: Efficient Transfer Coding

license

Etc is a low-level binary encoding package.

It's similar to package bytes with some extra bells and whistles.

Etc in JavaScript

API demo

While being faster than encoding/json, it provides a very similar reflection-based API.

package main

import (
  "github.com/superp00t/etc"
  "github.com/superp00t/etc/yo"
)

type Packet struct {
  ID          uint64
  Location    string
  Coordinates [2]float32
}

func main() {
  pc := Packet{
    ID:       1,
    Location: "Trinity",
    Coordinates: [2]float32{
      33.677216,
      -106.476059,
    },
  }

  data, err := etc.Marshal(pc)
  if err != nil {
    panic(err)
  }

  // Hex dump of "data":
  //
  // 01                     - ID
  // 07                     - 7 byte long string
  //   54 72 69 6e 69 74 79    "Trinity"
  // 78 b5 06 42            - X coordinate float32
  // be f3 d4 c2            - Y coordinate float32
  yo.Spew(data)
  
  var out Packet

  err = etc.Unmarshal(data, &out)
  if err != nil {
    panic(err)
  }
}

Etc also includes cross-platform utilities for easy directory manipulation.

Manual API

// Allocate empty buffer
e := etc.NewBuffer()

// Allocate buffer from string
e := etc.FromString("test")

// Allocate buffer from Base64
e := etc.FromBase64("dGVzdA==")

// Allocate buffer from bytes
e := etc.FromBytes([]byte{'t', 'e', 's', 't'})

// Create buffer as an alias to a file, preserving RAM but costing speed at runtime with disk IO
// Quite useful for parsing large files
e, err := etc.FileController("/tmp/newFile")
if err != nil {
  panic(err)
} else {
  e.Write([]byte("test"))
}

// Load a string from a defined 0-4 sector in buffer, ignoring possible zero padding bytes. This is not recommended and only included to support certain protocols and formats.
e.ReadFixedString(4) // "test"

// Write 64-bit integer to Buffer (using LEB128 integer compression)
e.WriteInt(12345678)

e.ReadInt() // 12345678

e.Base64() // dGVzdM7C8QU= (url encoding)
           //   t     e     s     t     [    12345678 (LEB128) ]
e.Bytes()  // [ 0x74, 0x65, 0x73, 0x74, 0xce, 0xc2, 0xf1, 0x05 ]

Documentation

Index

Constants

View Source
const (
	B  = 1
	KB = 1024 * B
	MB = 1024 * KB
	GB = 1024 * MB
)

Variables

This section is empty.

Functions

func DecodeSignedVarint

func DecodeSignedVarint(input io.Reader, limit int) *big.Int

func DecodeUnsignedVarint

func DecodeUnsignedVarint(input io.Reader, limit int) *big.Int

func EncodeSignedVarint

func EncodeSignedVarint(output io.Writer, x *big.Int)

func Marshal

func Marshal(v interface{}) ([]byte, error)

func RandomBigInt

func RandomBigInt(min, max *big.Int) *big.Int

Random random intergers

func RandomDuration

func RandomDuration(min, max time.Duration) time.Duration

func RandomIndex

func RandomIndex(v interface{}) int

func RandomInt

func RandomInt(min, max int) int

func RandomInt64

func RandomInt64(min, max int64) int64

func Unmarshal

func Unmarshal(b []byte, v interface{}) error

Types

type Backend

type Backend interface {
	Read([]byte) (int, error)
	Write([]byte) (int, error)
	Erase() error
	Seek(offset int64, whence int) (int64, error)
	Size() int64
	Bytes() []byte
	Close() error
}

func FsBackend

func FsBackend(path string, readOnly bool) (Backend, error)

func MemBackend

func MemBackend() Backend

type Buffer

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

func DummyReadSeeker

func DummyReadSeeker(r io.ReadSeeker) *Buffer

func DummyReader

func DummyReader(r io.Reader, size int64) *Buffer

func DummyWriter

func DummyWriter(w io.Writer) *Buffer

DummyWriter creates a Buffer using a Backend that can only do simple write operations.

func FileController

func FileController(path string, readOnly ...bool) (*Buffer, error)

func FromBase64

func FromBase64(s string) *Buffer

func FromBytes

func FromBytes(b []byte) *Buffer

FromBytes creates a copy of the supplied slice

func FromReadSeeker

func FromReadSeeker(sk io.ReadSeeker) *Buffer

func FromString

func FromString(b string) *Buffer

func NewBuffer

func NewBuffer() *Buffer

func OfBytes

func OfBytes(b []byte) *Buffer

OfBytes creates a Buffer using the supplied slice as its backend reader

func (*Buffer) Attach

func (b *Buffer) Attach(f Backend)

func (*Buffer) Available

func (b *Buffer) Available() int

func (*Buffer) Base64

func (b *Buffer) Base64() string

func (*Buffer) Bytes

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

func (*Buffer) Close

func (e *Buffer) Close() error

func (*Buffer) DecodeSignedVarint

func (bf *Buffer) DecodeSignedVarint(limit int) *big.Int

func (*Buffer) DecodeUnsignedVarint

func (bf *Buffer) DecodeUnsignedVarint(limit int) *big.Int

func (*Buffer) Delete

func (e *Buffer) Delete() error

func (*Buffer) Digest

func (b *Buffer) Digest(hs func() hash.Hash) []byte

func (*Buffer) EncodeSignedVarint

func (b *Buffer) EncodeSignedVarint(x *big.Int)

func (*Buffer) EncodeUnsignedVarint

func (b *Buffer) EncodeUnsignedVarint(c *big.Int)

func (*Buffer) Erase

func (b *Buffer) Erase() error

func (*Buffer) Find

func (b *Buffer) Find(str []byte) (int64, error)

func (*Buffer) FlushBits

func (b *Buffer) FlushBits()

func (*Buffer) FlushReadBits added in v0.9.2

func (b *Buffer) FlushReadBits()

func (*Buffer) FlushWriteBits added in v0.9.2

func (b *Buffer) FlushWriteBits()

func (*Buffer) Jump

func (b *Buffer) Jump(offset int64)

func (*Buffer) Len

func (b *Buffer) Len() int

func (*Buffer) Pos

func (b *Buffer) Pos() int64

func (*Buffer) Read

func (b *Buffer) Read(buf []byte) (int, error)

func (*Buffer) ReadAt

func (b *Buffer) ReadAt(p []byte, off int64) (int, error)

func (*Buffer) ReadBigInt

func (b *Buffer) ReadBigInt() *big.Int

func (*Buffer) ReadBigInt16

func (b *Buffer) ReadBigInt16() int16

func (*Buffer) ReadBigInt32

func (b *Buffer) ReadBigInt32() int32

func (*Buffer) ReadBigInt64

func (b *Buffer) ReadBigInt64() int64

WARNING: Big refers to Big-endian, not as in BigInteger.

func (*Buffer) ReadBigUint16

func (b *Buffer) ReadBigUint16() uint16

func (*Buffer) ReadBigUint32

func (b *Buffer) ReadBigUint32() uint32

func (*Buffer) ReadBigUint64

func (b *Buffer) ReadBigUint64() uint64

func (*Buffer) ReadBool

func (b *Buffer) ReadBool() bool

func (*Buffer) ReadBoxKey

func (b *Buffer) ReadBoxKey() *[32]byte

func (*Buffer) ReadBoxNonce

func (b *Buffer) ReadBoxNonce() *[24]byte

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() uint8

func (*Buffer) ReadBytes

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

func (*Buffer) ReadCString

func (b *Buffer) ReadCString() string

func (*Buffer) ReadDate

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

ReadDate returns a timestamp based on milliseconds

func (*Buffer) ReadFixedString

func (b *Buffer) ReadFixedString(i int) string

func (*Buffer) ReadFloat32

func (b *Buffer) ReadFloat32() float32

Floats

func (*Buffer) ReadFloat64

func (b *Buffer) ReadFloat64() float64

func (*Buffer) ReadInt

func (b *Buffer) ReadInt() int64

func (*Buffer) ReadInt16

func (b *Buffer) ReadInt16() int16

func (*Buffer) ReadInt32

func (b *Buffer) ReadInt32() int32

func (*Buffer) ReadInt64

func (b *Buffer) ReadInt64() int64

func (*Buffer) ReadInt8

func (b *Buffer) ReadInt8() int8

func (*Buffer) ReadInvertedString

func (b *Buffer) ReadInvertedString(l int) string

func (*Buffer) ReadLSBit

func (b *Buffer) ReadLSBit() bool

func (*Buffer) ReadLimitedBytes

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

func (*Buffer) ReadMSBit

func (b *Buffer) ReadMSBit() bool

Etc uses most-significant-bit first encoding of bits. For 8-byte aligned boolean values, refer to ReadBool().

func (*Buffer) ReadRemainder

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

func (*Buffer) ReadRune

func (b *Buffer) ReadRune() (rune, int, error)

func (*Buffer) ReadString

func (b *Buffer) ReadString(delimiter byte) (string, error)

func (*Buffer) ReadStringUntil

func (b *Buffer) ReadStringUntil(delimiter rune) string

func (*Buffer) ReadTime

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

ReadTime returns a timestamp based on nanoseconds

func (*Buffer) ReadTypedInt

func (b *Buffer) ReadTypedInt(fixed bool, bits int, signed, big bool, v interface{})

Integer functions

func (*Buffer) ReadUString

func (b *Buffer) ReadUString() string

func (*Buffer) ReadUUID

func (e *Buffer) ReadUUID() UUID

func (*Buffer) ReadUint

func (b *Buffer) ReadUint() uint64

func (*Buffer) ReadUint16

func (b *Buffer) ReadUint16() uint16

func (*Buffer) ReadUint32

func (b *Buffer) ReadUint32() uint32

func (*Buffer) ReadUint64

func (b *Buffer) ReadUint64() uint64

func (*Buffer) ReadUntilToken

func (b *Buffer) ReadUntilToken(s string) (string, error)

func (*Buffer) ReadWChar

func (b *Buffer) ReadWChar(ln int) string

func (*Buffer) Reverse

func (b *Buffer) Reverse(offset int64)

func (*Buffer) Runes

func (b *Buffer) Runes() []rune

func (*Buffer) Seek

func (b *Buffer) Seek(offset int64, whence int) (int64, error)

func (*Buffer) Size

func (e *Buffer) Size() int64

func (*Buffer) Start

func (b *Buffer) Start()

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) ToString

func (b *Buffer) ToString() string

func (*Buffer) Write

func (b *Buffer) Write(buf []byte) (int, error)

func (*Buffer) WriteAt

func (b *Buffer) WriteAt(p []byte, off int64) (int, error)

func (*Buffer) WriteBigInt

func (b *Buffer) WriteBigInt(v *big.Int)

func (*Buffer) WriteBigInt16

func (b *Buffer) WriteBigInt16(v int16)

WARNING: Big refers to Big-endian, not as in BigInteger.

func (*Buffer) WriteBigInt32

func (b *Buffer) WriteBigInt32(v int32)

func (*Buffer) WriteBigInt64

func (b *Buffer) WriteBigInt64(v int64)

func (*Buffer) WriteBigUint16

func (b *Buffer) WriteBigUint16(v uint16)

func (*Buffer) WriteBigUint32

func (b *Buffer) WriteBigUint32(v uint32)

func (*Buffer) WriteBigUint64

func (b *Buffer) WriteBigUint64(v uint64)

func (*Buffer) WriteBool

func (b *Buffer) WriteBool(v bool)

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(v uint8)

func (*Buffer) WriteCString

func (b *Buffer) WriteCString(v string)

func (*Buffer) WriteDate

func (b *Buffer) WriteDate(t time.Time)

func (*Buffer) WriteFixedString

func (b *Buffer) WriteFixedString(i int, v string)

func (*Buffer) WriteFloat32

func (b *Buffer) WriteFloat32(v float32)

func (*Buffer) WriteFloat64

func (b *Buffer) WriteFloat64(v float64)

func (*Buffer) WriteInt

func (b *Buffer) WriteInt(v int64)

func (*Buffer) WriteInt16

func (b *Buffer) WriteInt16(v int16)

func (*Buffer) WriteInt32

func (b *Buffer) WriteInt32(v int32)

func (*Buffer) WriteInt64

func (b *Buffer) WriteInt64(v int64)

func (*Buffer) WriteInt8

func (b *Buffer) WriteInt8(v int8)

func (*Buffer) WriteInvertedString

func (b *Buffer) WriteInvertedString(l int, s string)

func (*Buffer) WriteLSBit

func (b *Buffer) WriteLSBit(bit bool)

func (*Buffer) WriteLimitedBytes

func (b *Buffer) WriteLimitedBytes(bf []byte)

func (*Buffer) WriteMSBit

func (b *Buffer) WriteMSBit(bit bool)

func (*Buffer) WriteRandom

func (b *Buffer) WriteRandom(i int) *Buffer

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune)

func (*Buffer) WriteTime

func (b *Buffer) WriteTime(t time.Time)

func (*Buffer) WriteTypedInt

func (b *Buffer) WriteTypedInt(fixed bool, bits int, signed, big bool, val interface{})

func (*Buffer) WriteUString

func (b *Buffer) WriteUString(u string)

func (*Buffer) WriteUUID

func (e *Buffer) WriteUUID(g UUID)

func (*Buffer) WriteUint

func (b *Buffer) WriteUint(v uint64)

func (*Buffer) WriteUint16

func (b *Buffer) WriteUint16(v uint16)

func (*Buffer) WriteUint32

func (b *Buffer) WriteUint32(v uint32)

func (*Buffer) WriteUint64

func (b *Buffer) WriteUint64(v uint64)

type Decoder

type Decoder struct {
	*Buffer
}

func NewDecoder

func NewDecoder(i io.Reader, size int64) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

type DiskStatus

type DiskStatus struct {
	All  uint64 `json:"all"`
	Used uint64 `json:"used"`
	Free uint64 `json:"free"`
}

func GetDiskStatus

func GetDiskStatus(path string) (*DiskStatus, error)

type Encoder

type Encoder struct {
	*Buffer
}

func NewEncoder

func NewEncoder(out io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

type FixedInt16

type FixedInt16 int16

type FixedInt16BE

type FixedInt16BE int16

type FixedInt32

type FixedInt32 int32

type FixedInt32BE

type FixedInt32BE int32

type FixedInt64

type FixedInt64 int64

type FixedInt64BE

type FixedInt64BE int64

type FixedUint16

type FixedUint16 uint16

type FixedUint16BE

type FixedUint16BE uint16

type FixedUint32

type FixedUint32 uint32

type FixedUint32BE

type FixedUint32BE uint32

type FixedUint64

type FixedUint64 uint64

type FixedUint64BE

type FixedUint64BE uint64

type Path

type Path []string

func Env

func Env(key string) Path

func Gopath

func Gopath() Path

Gopath returns the path of local Go folder. Panics if one cannot be found.

func Goroot

func Goroot() Path

Goroot attempts to finds your Go installation, panics if one cannot be found.

func HomeDirectory

func HomeDirectory() Path

func Import

func Import(path string) Path

func LocalDirectory

func LocalDirectory() Path

func ParseSystemPath

func ParseSystemPath(s string) Path

func ParseUnixPath

func ParseUnixPath(s string) Path

func ParseWindowsPath

func ParseWindowsPath(s string) Path

func Root

func Root() Path

func TmpDirectory

func TmpDirectory() Path

func (Path) Concat

func (d Path) Concat(s ...string) Path

Concat applies the path with a variadic list of path elements, and returns it.

func (Path) DiskStatus

func (d Path) DiskStatus() *DiskStatus

func (Path) DiskUsed

func (d Path) DiskUsed() uint64

func (Path) Exists

func (d Path) Exists(p string) bool

func (Path) ExistsPath

func (d Path) ExistsPath(p Path) bool

func (Path) Free

func (d Path) Free() uint64

func (Path) Get

func (d Path) Get(path string) (*Buffer, error)

func (Path) GetSub

func (d Path) GetSub(p Path) Path

func (Path) GetSubFile

func (d Path) GetSubFile(path Path) (*Buffer, error)

func (Path) GetSubPath

func (d Path) GetSubPath(p Path) string

func (Path) IsDirectory

func (d Path) IsDirectory() bool

func (Path) IsExtant

func (d Path) IsExtant() bool

func (Path) LRU

func (d Path) LRU() (string, error)

LRU returns the least-recently modified file in a directory.

func (Path) MakeDir

func (d Path) MakeDir() error

func (Path) MakeDirPath

func (d Path) MakeDirPath(path Path) error

func (Path) Mkdir

func (d Path) Mkdir(elements ...string) error

Mkdir accepts a variadic array of string elements, such as "dev", "random"

func (Path) Pop

func (d Path) Pop() (string, Path)

func (Path) Put

func (d Path) Put(path string, data io.Reader) error

func (Path) ReadAll

func (d Path) ReadAll() ([]byte, error)

func (Path) Remove

func (d Path) Remove() error

func (Path) Render

func (d Path) Render() string

func (Path) RenderUnix

func (d Path) RenderUnix() string

func (Path) RenderWin

func (d Path) RenderWin() string

func (Path) Size

func (d Path) Size() uint64

func (Path) Stat

func (d Path) Stat() os.FileInfo

func (Path) String

func (d Path) String() string

func (Path) Time

func (d Path) Time() time.Time

func (Path) WriteAll

func (d Path) WriteAll(b []byte) error

type UUID

type UUID [16]byte
var NullUUID UUID

func GenerateRandomUUID

func GenerateRandomUUID() UUID

func ParseUUID

func ParseUUID(s string) (UUID, error)

func (UUID) Bytes

func (g UUID) Bytes() []byte

func (UUID) Cmp

func (g UUID) Cmp(u UUID) bool

func (UUID) MarshalJSON

func (g UUID) MarshalJSON() ([]byte, error)

func (UUID) MarshalYAML

func (g UUID) MarshalYAML() (interface{}, error)

func (UUID) String

func (g UUID) String() string

func (UUID) UnmarshalJSON

func (g UUID) UnmarshalJSON(b []byte) error

func (UUID) UnmarshalYAML

func (g UUID) UnmarshalYAML(b interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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