Documentation ¶
Overview ¶
Package cstruct provides a helper for decoding binary data that is in the form of a padded C structure.
Index ¶
- type Decoder
- func (d *Decoder) Byte() byte
- func (d *Decoder) Bytes(b []byte)
- func (d *Decoder) Err() error
- func (d *Decoder) Int16() int16
- func (d *Decoder) Int32() int32
- func (d *Decoder) Int64() int64
- func (d *Decoder) Offset() int
- func (d *Decoder) Uint16() uint16
- func (d *Decoder) Uint32() uint32
- func (d *Decoder) Uint64() uint64
- func (d *Decoder) Uintptr() uintptr
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 ¶
NewDecoder creates a Decoder from a byte slice.
func (*Decoder) Bytes ¶
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.