Git Product home page Git Product logo

go-binpat's Introduction

Go-Binpat

Go Reference Scrutinizer quality (GitHub/Bitbucket) GitHub License

Drop-in replacement for the standard binary package that allows for defining more complex structures using struct tags.

Example: Read

package main

import (
	"bytes"
	"encoding/binary"
	"fmt"

	"github.com/xypwn/go-binpat"
)

func main() {
	r := bytes.NewReader([]byte{0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 'H', 'e', 'l', 'l', 'o', 0x00})
	var data struct {
		Size uint32
		Data []byte `binpat:"len=Size"`
		Str  string `binpat:"nt"`
	}
	if err := binpat.Read(r, binary.BigEndian, &data); err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", data)
}

Output:

{Size:8 Data:[0 1 2 3 4 5 6 7] Str:Hello}

Example: Write

package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"unicode"

	"github.com/xypwn/go-binpat"
)

func main() {
	var w bytes.Buffer
	data := struct {
		Size uint32
		Data []byte `binpat:"len=Size"`
		Str  string `binpat:"nt"`
	}{
		Size: 8, // must equal len(Data) or an error occurs
		Data: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
		Str:  "Hello",
	}
	if err := binpat.Write(&w, binary.BigEndian, data); err != nil {
		panic(err)
	}
	fmt.Printf("Data: %+v\n", data)

	b := w.Bytes()
	fmt.Print("Bytes: [")
	for i := range b {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Printf("0x%02x", b[i])
		if unicode.IsPrint(rune(b[i])) {
			fmt.Printf("('%c')", b[i])
		}
	}
	fmt.Println("]")
}

Output:

Data: {Size:8 Data:[0 1 2 3 4 5 6 7] Str:Hello}
Bytes: [0x00 0x00 0x00 0x08 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x48('H') 0x65('e') 0x6c('l') 0x6c('l') 0x6f('o') 0x00]

Struct tags

Format by example

struct {
	// Size is set to be the length of the Data array in Data's struct tag
	Size uint32
	// Data is coded as Big Endian and its length is set to be the value of Size
	Data []int32 `binpat:"be,len=Size"`
	// Str is null-terminated
	Str  string  `binpat:"nt"`
}

List of struct tags

Tag Field type Inherited Description
- any do not serialize field
le any ✔️ always interpret as little endian
be any ✔️ always interpret as big endian
ne any ✔️ always interpret as native endian
nt string string is null-terminated
len=<FieldName> []any | string slice/string length

go-binpat's People

Contributors

xypwn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.