byteparse

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

package byteparse provides a binary parser and utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tag added in v0.2.1

func Tag(tag []byte) parser.Parser[byte, []byte]

Tag initializes a parser that checks the input starts with the tag prefix.

Example
package main

import (
	"fmt"

	"github.com/Drumato/peachcomb/pkg/byteparse"
)

func main() {
	t := []byte{0x7f, 0x45, 0x4c, 0x46}

	b := byteparse.NewCompleteInput([]byte{0x7f, 0x45, 0x4c, 0x46, 0x02})
	_, o, err := byteparse.Tag(t)(b)
	fmt.Printf("%d\n", len(o))
	fmt.Printf("%x %x %x %x\n", o[0], o[1], o[2], o[3])
	fmt.Println(err)
}
Output:

4
7f 45 4c 46
<nil>

func UInt16

func UInt16(byteorder binary.ByteOrder) parser.Parser[byte, uint16]

Uint16 initializes a parser that parse 16-bit unsigned integer. user can determine the behavior of this parser by giving byteorder what you want to use.

Example
package main

import (
	"encoding/binary"
	"fmt"

	"github.com/Drumato/peachcomb/pkg/byteparse"
)

func main() {
	b := byteparse.NewCompleteInput([]byte{0x01, 0x02, 0x03})
	_, o, err := byteparse.UInt16(binary.BigEndian)(b)
	fmt.Printf("0x%x\n", o)
	fmt.Println(err)
}
Output:


0x102
<nil>

func UInt32 added in v0.1.5

func UInt32(byteorder binary.ByteOrder) parser.Parser[byte, uint32]

Uint32 initializes a parser that parse 32-bit unsigned integer. user can determine the behavior of this parser by giving byteorder what you want to use.

Example
package main

import (
	"encoding/binary"
	"fmt"

	"github.com/Drumato/peachcomb/pkg/byteparse"
)

func main() {
	b := byteparse.NewCompleteInput([]byte{0x01, 0x02, 0x03, 0x04})
	_, o, err := byteparse.UInt32(binary.BigEndian)(b)
	fmt.Printf("0x%x\n", o)
	fmt.Println(err)
}
Output:


0x1020304
<nil>

func UInt64 added in v0.1.5

func UInt64(byteorder binary.ByteOrder) parser.Parser[byte, uint64]

Uint64 initializes a parser that parse 64-bit unsigned integer. user can determine the behavior of this parser by giving byteorder what you want to use.

func UInt8

func UInt8() parser.Parser[byte, uint8]

Uint8 initializes a parser that parse 8-bit unsigned integer.

Example
package main

import (
	"fmt"

	"github.com/Drumato/peachcomb/pkg/byteparse"
)

func main() {
	b := byteparse.NewCompleteInput([]byte{0x01, 0x02, 0x03})
	_, o, err := byteparse.UInt8()(b)
	fmt.Println(o)
	fmt.Println(err)
}
Output:


1
<nil>

Types

type CompleteInput added in v0.3.0

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

CompleteInput holds the whole bytes.

func NewCompleteInput added in v0.3.0

func NewCompleteInput(bytes []byte) *CompleteInput

NewCompleteInput initialiizes a CompleteInput.

func (*CompleteInput) Read added in v0.3.0

func (c *CompleteInput) Read(buf []byte) (int, error)

Read implements parser.ParseInput interface.

func (*CompleteInput) Seek added in v0.3.0

func (c *CompleteInput) Seek(n int, mode parser.SeekMode) (int, error)

Seek implements parser.ParseInput interface.

type UnexpectedPrefixError added in v0.2.1

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

UnexpectedPrefixError notifies the prefix of the given input is unexpected.

func (*UnexpectedPrefixError) Error added in v0.2.1

func (e *UnexpectedPrefixError) Error() string

Error implements error interface.

Jump to

Keyboard shortcuts

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