Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struc: support Custom slice types #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mewmew
Copy link
Contributor

@mewmew mewmew commented Jul 14, 2018

Useful for implementing e.g. NULL-terminated slices
whose length is unknown and cannot be determined by
a field in the struct.

Useful for implementing e.g. NULL-terminated slices
whose length is unknown and cannot be determined by
a field in the struct.
@mewmew
Copy link
Contributor Author

mewmew commented Jul 14, 2018

Without this PR, I was running into the following error when trying to parse SYM files.

2018/07/14 09:49:45 struc: field `Dims` is a slice with no length or sizeof field

Where the type of Dims implements the struc.Custom interface.

// Dimensions specifies array dimensions.
type Dimensions []uint16

func (dims *Dimensions) Pack(p []byte, opt *struc.Options) (int, error) {
	panic("not yet implemented")
}

func (dims *Dimensions) Unpack(r io.Reader, length int, opt *struc.Options) error {
	for {
		var dim uint16
		if err := binary.Read(r, binary.LittleEndian, &dim); err != nil {
			if errors.Cause(err) == io.EOF {
				return errors.WithStack(io.ErrUnexpectedEOF)
			}
			return errors.WithStack(err)
		}
		*dims = append(*dims, dim)
		if dim == 0 {
			break
		}
	}
	return nil
}

func (dims *Dimensions) Size(opt *struc.Options) int {
	return 2 * len(*dims)
}

func (dims *Dimensions) String() string {
	var s string
	for i, dim := range *dims {
		if dim == 0 {
			break
		}
		if i != 0 {
			s += ","
		}
		s += strconv.Itoa(int(dim))
	}
	return s
}

@lunixbochs
Copy link
Owner

lunixbochs commented Jul 14, 2018 via email

@mewmew
Copy link
Contributor Author

mewmew commented Jul 14, 2018

Cool, can you add a test case for double round trip encoding/decoding such a slice? Ideally with a field after it to ensure the length stuff is ok.

I actually tried, but couldn't get size to work. I was going to ask you if there is something in particular that needs to be changed to support it.

Would you mind taking a look? :)

Cheers,
/u

@lunixbochs
Copy link
Owner

lunixbochs commented Jul 15, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants