Categorygithub.com/mixcode/binarystruct
modulepackage
0.0.11
Repository: https://github.com/mixcode/binarystruct.git
Documentation: pkg.go.dev

# README

Go Reference

binarystruct : binary data encoder/decoder for native Go structs

Package binarystruct is an automatic type-converting binary data encoder/decoder(or marshaller/unmarshaller) for go-language structs.

Go's built-in binary encoding package, "encoding/binary" is the preferred method to deal with binary data structures. The binary package is quite easy to use, but some cases require additional type conversions when values are tightly packed. For example, an integer value in raw binary structure may be stored as a word or a byte, but the decoded value would be type-casted to an architecture-dependent integer for easy of use in the Go context.

This package simplifies the typecasting burdens by automatically handling conversion of struct fields using field tags.

A Quick Example

Assume we have a binary data structure with a magic header and three integers, byte, word, dword each, like below. By writing binary data types to field tags in Go struct definition, the values are automatically recognized and converted to proper encoding types.

// source binary data
blob := []byte { 0x61, 0x62, 0x63, 0x64,
	0x01,
	0x00, 0x02,
	0x00, 0x00, 0x00, 0x03 }
// [ "abcd", 0x01, 0x0002, 0x00000003 ]

// Go struct, with field types specified in tags
strc := struct {
	Header       string `binary:"[4]byte"` // maps to 4 bytes
	ValueInt8    int    `binary:"int8"`    // maps to single signed byte
	ValueUint16  int    `binary:"uint16"`  // maps to two bytes
	ValueDword32 int    `binary:"dword"`   // maps to four bytes
}{}

// Unmarshal binary data into the struct
readsz, err := binarystruct.Unmarshal(blob, binarystruct.BigEndian, &strc)

// the structure have proper values now
fmt.Println(strc)
// {abcd 1 2 3}

// Marshal a struct to []byte
output, err := binarystruct.Marshal(&strc, binarystruct.BigEndian)
// output == blob

Features

  • Automatic type conversion and range check based on field tag descriptions
  • Variable-length Array and String handling with references to other struct member variables
  • Text encoding support with "golang.org/x/text/encoding" interface

See also

See go reference doc for details.

# Functions

Marshal encodes a go value into binary data and return it as []byte.
Read reads binary data from r and decode it into a Go value.
Unmarshal decodes binary images into a Go value.
Write encodes a go value into binary stream and writes to w.

# Constants

If a field is tagged with Any, or no tag is set, then the the value's default encoding will be used.
{size Uint8, string [size]byte} `binary:"bstring"`.
8-bit byte.
32-bit double word.
{size Uint32, string [size]byte} `binary:"dwstring"`.
`binary:"float32"`.
`binary:"float64"`.
Values with Ignore tag are ignored.
`binary:"int16"`.
`binary:"int32"`.
`binary:"int64"`.
`binary:"int8"`.
Pad is padding zero bytes.
64-bit quad word.
[]byte.
`binary:"uint16"`.
`binary:"uint32"`.
`binary:"uint64"`.
`binary:"uint8"`.
16-bit word.
{size Uint16, string [size]byte} `binary:"wstring"`.
zero-word-terminated word string.
zero-terminated byte string, or C-style string.

# Variables

byte orders.
supplied value must be a pointer or an interface.
possibly an invalid encoding type appears in a tag.
the field is tagged as array but underlying value is not.
cannot determine the length of an array or slice.
No description provided by the author

# Structs

Marshaller is go-type to binary-type encoder with environmental values.

# Type aliases

type ByteOrder is an alias of encoding/binary.ByteOrder.