ghostiam / binstruct Goto Github PK
View Code? Open in Web Editor NEWGolang binary decoder for mapping data into the structure
License: MIT License
Golang binary decoder for mapping data into the structure
License: MIT License
Hi again, is there a plan to add possibility to reference fields of array of structs?
Or maybe it's way out of scope of this package? I mean it's achievable via custom function without much of a hassle.
I have a following example:
type ChunkHeader struct {
Size uint8
}
type Entry struct {
Count uint8
Headers []ChunkHeader `bin:"len:Count"`
Chunks [][]byte `bin:"ReadChunks"`
// desired ("_currentIndex" or maybe "$", not sure)
// Chunks [][]byte `bin:"len:Count,[len:Headers[_currentIndex].Size]"`
}
func (entry *Entry) ReadChunks(r binstruct.Reader) error {
entry.Chunks = make([][]byte, len(entry.Headers))
for i, header := range entry.Headers {
buf := make([]byte, header.Size)
read, err := r.Read(buf)
if err != nil {
return errors.Join(
fmt.Errorf("failed to read chunk %d", i),
err,
)
}
if read != int(header.Size) {
return fmt.Errorf("not enough data to read chunk %d", i)
}
entry.Chunks[i] = buf
}
return nil
}
func main() {
data := []byte{
0x02, // count of headers
0x01, // header 1 size=1
0x02, // header 2 size=2
0x10, // chunk 1 data byte 1
0x20, // chunk 2 data byte 1
0x21, // chunk 2 data byte 2
}
reader := binstruct.NewReaderFromBytes(data, binary.LittleEndian, false)
var entry Entry
err := reader.Unmarshal(&entry)
if err != nil {
fmt.Printf("failed to parse: %s\n", err)
}
fmt.Println("data", entry)
}
package main
import (
"fmt"
"log"
"github.com/ghostiam/binstruct"
)
func main() {
data := []byte{
0xbf, 0x81, 0xb7, 0xa, 0xd9, 0x5e, 0xd8, 0xb9, 0xff, 0x7f, 0xe9, 0x11, 0x85, 0xbd, 0x90, 0x29, 0xbc, 0x6, 0x80, 0x0, 0xaf,
}
type dataStruct struct {
First []byte `bin:"len:2"`
Mid []byte `bin:"len:6"`
Last []byte `bin:"len:"`
}
var actual dataStruct
err := binstruct.UnmarshalBE(data, &actual) // UnmarshalLE() or Unmarshal()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v", actual)
// Output: {Arr:[1 2 3 4]}
}
result :
main.dataStruct{First:[]uint8{0xbf, 0x81}, Mid:[]uint8{0xb7, 0xa, 0xd9, 0x5e, 0x
d8, 0xb9}, Last:[]uint8(nil)}
how UnmarshalBE above dataStruct with last of []byte with dont set lenght of []byte ?
i need Last is []byte{0x11, 0x85, 0xbd, 0x90, 0x29, 0xbc, 0x6, 0x80, 0x0, 0xaf}
Thanks.
Hi, thank you for this awesome package!
It would be great to add support for referencing inner fields (e.g. I have Header.EntriesCount
), currently hat requires to write helper method or helper field.
Related code:
type Bin struct {
...
Pin struct {
Pin1 []byte `bin:"len:4"` // LE
Pin1Unknown []byte `bin:"len:4"`
Pin1Checksum uint16 `bin:"Uint16l,len:2"`
Pin2 []byte `bin:"len:4"` // LE
Pin2Unknown []byte `bin:"len:4"`
Pin2Checksum uint16 `bin:"Uint16l,len:2"`
} // 20 bytes
...
}
// Uint16l returns a uint16 read as little endian
func (*Bin) Uint16l(r binstruct.Reader) (uint16, error) {
var out uint16
if err := binary.Read(r, binary.LittleEndian, &out); err != nil {
return 0, err
}
return out, nil
}
2021/12/25 16:05:41 main.go:21: failed set value to field "Pin": unmarshal struct: failed set value to field "Pin1Checksum": call custom func:
failed call method, expected methods:
func (*) Uint16l(r binstruct.Reader) error {}
or
func (*) Uint16l(r binstruct.Reader) (uint16, error) {}
exit status 1
Hi, thank you for the great package!
It would be nice to have a flag for offset options to be able to restore offset after reading referenced chunk.
I have an archive structure that have sparsely referenced buffers. I'm using the following reader method:
type File struct {
Offset uint64
Size uint32
InlineBuffer []byte `bin:"FileReadInlineBuffer"`
// desired
// InlineBuffer []byte `bin:"offsetStart:Offset, len:Size, restoreOffsetOrSomeBetterNameForThisFlag:true"`
}
func (file *File) FileReadInlineBuffer(r binstruct.Reader) error {
oldPos, err := r.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
_, err = r.Seek(int64(file.Offset), io.SeekStart)
if err != nil {
return err
}
size := int(file.Size)
file.InlineBuffer = make([]byte, size)
n, err := r.Read(file.InlineBuffer)
if err != nil {
return err
}
if n != size {
return fmt.Errorf("failed to read inline buffer: expected %d bytes, got: %d", size, n)
}
_, err = r.Seek(oldPos, io.SeekStart)
if err != nil {
return err
}
return nil
}
Would be nice to have a benchmark comparison with other libraries (e.g. lunixbochs/struc)
I'd like to know if this repo offers better performances
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.