cstruct

package
v1.66.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package cstruct provides a helper for decoding binary data that is in the form of a padded C structure.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reads and decodes padded fields from a slice of bytes. All fields are decoded with native endianness.

Methods of a Decoder do not return errors, but rather store any error within the Decoder. The first error can be obtained via the Err method; after the first error, methods will return the zero value for their type.

Example

This test provides a semi-realistic example of how you can use this package to decode a C structure.

// Our example C structure:
//    struct mystruct {
//      char *p;
//      char c;
//	/* implicit: char _pad[3]; */
//      int x;
//    };
//
// The Go structure definition:
type myStruct struct {
	Ptr    uintptr
	Ch     byte
	Intval uint32
}

// Our "in-memory" version of the above structure
buf := []byte{
	1, 2, 3, 4, 0, 0, 0, 0, // ptr
	5,          // ch
	99, 99, 99, // padding
	78, 6, 0, 0, // x
}
d := NewDecoder(buf)

// Decode the structure; if one of these function returns an error,
// then subsequent decoder functions will return the zero value.
var x myStruct
x.Ptr = d.Uintptr()
x.Ch = d.Byte()
x.Intval = d.Uint32()

// Note that per the Go language spec:
//    [...] when evaluating the operands of an expression, assignment,
//    or return statement, all function calls, method calls, and
//    (channel) communication operations are evaluated in lexical
//    left-to-right order
//
// Since each field is assigned via a function call, one could use the
// following snippet to decode the struct.
//     x := myStruct{
//         Ptr:    d.Uintptr(),
//         Ch:     d.Byte(),
//         Intval: d.Uint32(),
//     }
//
// However, this means that reordering the fields in the initialization
// statement–normally a semantically identical operation–would change
// the way the structure is parsed. Thus we do it as above with
// explicit ordering.

// After finishing with the decoder, check errors
if err := d.Err(); err != nil {
	panic(err)
}

// Print the decoder offset and structure
fmt.Printf("off=%d struct=%#v\n", d.Offset(), x)
Output:

off=16 struct=cstruct.myStruct{Ptr:0x4030201, Ch:0x5, Intval:0x64e}

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder creates a Decoder from a byte slice.

func (*Decoder) Byte

func (d *Decoder) Byte() byte

Byte returns a single byte from the buffer.

func (*Decoder) Bytes

func (d *Decoder) Bytes(b []byte)

Byte returns a number of bytes from the buffer based on the size of the input slice. No padding is applied.

If an error is encountered or this Decoder has previously encountered an error, no changes are made to the provided buffer.

func (*Decoder) Err

func (d *Decoder) Err() error

Err returns the first error that was encountered by this Decoder.

func (*Decoder) Int16

func (d *Decoder) Int16() int16

Int16 returns a int16 decoded from the buffer.

func (*Decoder) Int32

func (d *Decoder) Int32() int32

Int32 returns a int32 decoded from the buffer.

func (*Decoder) Int64

func (d *Decoder) Int64() int64

Int64 returns a int64 decoded from the buffer.

func (*Decoder) Offset

func (d *Decoder) Offset() int

Offset returns the current read offset for data in the buffer.

func (*Decoder) Uint16

func (d *Decoder) Uint16() uint16

Uint16 returns a uint16 decoded from the buffer.

func (*Decoder) Uint32

func (d *Decoder) Uint32() uint32

Uint32 returns a uint32 decoded from the buffer.

func (*Decoder) Uint64

func (d *Decoder) Uint64() uint64

Uint64 returns a uint64 decoded from the buffer.

func (*Decoder) Uintptr

func (d *Decoder) Uintptr() uintptr

Uintptr returns a uintptr decoded from the buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL