Coder Social home page Coder Social logo

binstruct's People

Contributors

ghostiam avatar rdhite avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

binstruct's Issues

Reference array of structs subfield

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)
}

a question

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.

function tag on nested struct

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

Read value on a given offset and return pointer back.

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
}

Benchmark

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

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.