Categorygithub.com/o-murphy/pystruct-go
repositorypackage
1.0.0
Repository: https://github.com/o-murphy/pystruct-go.git
Documentation: pkg.go.dev

# README

PyStruct

Go lang implementation of python's struct module

Interpret bytes as packed binary data

[!TIP] If you can't find something in README.md you can search references in Python's struct documentation

[!NOTE] Some functionality not yet implemented, details there

[!NOTE] Tested only with small values

Contents

Installation

go get github.com/o-murphy/pystruct-go

Usage

Example

package main

import (
	"bytes"
	"fmt"

	pystruct "github.com/o-murphy/pystruct-go"
)

func main() {

	// unpack
	byteArray := []byte{97, 98, 99, 100, 101, 102, 103}
	intf, err := pystruct.Unpack(`<3sf`, byteArray)
	if err == nil {
		fmt.Println(intf...)
	}

	// pack
	byteArray2, err := pystruct.Pack(`<3sf`, intf...)
	if err == nil {
		fmt.Println(byteArray2)
	}

	if bytes.Equal(byteArray, byteArray2) {
		fmt.Println("Equal")
	}

	// or use struct
	s := pystruct.NewStruct(`<3sf`)
	byteArray, err := s.Pack(intf...)
	intf, err := s.Unpack(byteArray)

}

Byte Order, Size, and Alignment

Details By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler). This behavior is chosen so that the bytes of a packed struct correspond exactly to the memory layout of the corresponding C struct. Whether to use native byte ordering and padding or standard formats depends on the application.

Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:

CharacterByte orderSizeAlignment
@nativenativenative
=nativestandardnone
<little-endianstandardnone
>big-endianstandardnone
!network (= big-endian)standardnone

[!TIP] If the first character is not one of these, '@' is assumed.

[!NOTE] Note The number 1023 (0x3ff in hexadecimal) has the following byte representations:

03 ff in big-endian (>)
ff 03 in little-endian (<)

More info there...

Format Characters

Details Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.

[!NOTE] More info there...

FormatC TypeGo TypePython TypeStandard Size
ccharbytebytes of length 11
bsigned charint8integer1
Bunsigned charuint8integer1
?_Boolboolbool1
hshortint16integer2
Hunsigned shortuint16integer2
iintint32integer4
Iunsigned intuint32integer4
llongint32integer4
Lunsigned longuint32integer4
qlong longint64integer8
Qunsigned long longuint64integer8
ffloatfloat32 (float64)float4 (8)
ddoublefloat64 (float32)float8 (4)
schar[]stringbytesVariable
xpad byteN/A**no value
nssize_tN/A**integer
Nsize_tN/A**integer
efloat16N/A**float2
pchar[]N/A**bytes
Pvoid*N/A**integer

Functions

func CalcSize

func CalcSize(format string) (int, error)

Return the size of the struct (and hence of the bytes object produced by pack(format, ...)) corresponding to the format string format

size, err := pystruct.CalcSize(format)
if err == nil {
  fmt.Println(size)
}

func Pack

func Pack(format string, intf []interface{}) ([]byte, error)

Return a bytes object containing the values v1, v2, … packed according to the format string format. The arguments must match the values required by the format exactly.

intf := []interface{}{"abc", 1.01}
byteArray, err := pystruct.Pack(`<3sf`, intf)
if err == nil {
  fmt.Println(byteArray2)
}

func PackInto

func PackInto(format string, buffer []byte, offset int, intf ...interface{}) ([]byte, error)

Pack the values v1, v2, … according to the format string format and write the packed bytes into the writable buffer starting at position offset. Note that offset is a required argument.

intf := []interface{}{"abc", 1.01}
buffer := []byte{0xff, 0xff, 0xff, 0xff}
byteArray, err := pystruct.PackInto(`<3sf`, buffer, 3, intf)
if err == nil {
  fmt.Println(byteArray)
}

func Unpack

func Unpack(format string, buffer []byte) ([]interface{}, error)

Unpack from the buffer buffer (presumably packed by Pack(format, ...)) according to the format string format. The result is an []interface{} even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected by CalcSize().

byteArray := []byte{97, 98, 99, 100, 101, 102, 103}
intf, err := pystruct.Unpack(`<3sf`, byteArray)
if err == nil {
  fmt.Println(intf...)
}

func UnpackFrom

func UnpackFrom(format string, buffer []byte, offset int) ([]interface{}, error)

Unpack from buffer starting at position offset, according to the format string format. The result is an []interface{} even if it contains exactly one item. The buffer’s size in bytes, starting at position offset, must be at least the size required by the format, as reflected by CalcSize().

byteArray := []byte{0, 0, 0, 97, 98, 99, 100, 101, 102, 103}
intf, err := pystruct.UnpackFrom(`<3sf`, byteArray, 3)
if err == nil {
  fmt.Println(intf...)
}

func IterUnpack

func IterUnpack(format string, buffer []byte) (<-chan interface{}, <-chan error)

Iteratively unpack from the buffer buffer according to the format string format. This function returns an iterator which will read equally sized chunks from the buffer until all its contents have been consumed. The buffer’s size in bytes must be a multiple of the size required by the format, as reflected by CalcSize()

format := `<3si`
byteArray := []byte{97, 98, 99, 100, 101, 102, 103}
iterator, errs := pystruct.IterUnpack(format, byteArray)

i := 0
for value := range iterator {
	fmt.Println(i, value)
  i++
}

Types

type PyStruct

type PyStruct struct {
	format string
}

PyStruct(fmt) --> compiled PyStruct object

func NewStruct
NewStruct(format string) (PyStruct, error)

NewStruct(fmt) --> compiled PyStruct object Methods bellow this just binds for same named functions

func CalcSize

(⬆️CalcSize)

func (s *PyStruct) CalcSize() (int, error)
func Pack

(⬆️Pack)

func (s *PyStruct) Pack(intf ...interface{}) ([]byte, error)
func PackInto

(⬆️PackInto)

func (s *PyStruct) PackInto(buffer []byte, offset int, intf ...interface{}) ([]byte, error) 
func Unpack

(⬆️Unpack)

func (s *PyStruct) Unpack(buffer []byte) ([]interface{}, error)
func UnpackFrom

(⬆️UnpackFrom)

func (s *PyStruct) UnpackFrom(buffer []byte, offset int) ([]interface{}, error) 
func IterUnpack

(⬆️IterUnpack)

func (s *PyStruct) IterUnpack(format string, buffer []byte) (<-chan interface{}, <-chan error)

Not yet implemented

N/A
  • p char[] - Pascal string
  • P void*
  • x PadByte
  • n ssize_t
  • N size_t
Float16
  • e Float16 - (IEEE 754 binary16 half precision float)

RISK NOTICE

[!IMPORTANT] THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.