package cstruct
import "tailscale.com/util/cstruct"
Package cstruct provides a helper for decoding binary data that is in the form of a padded C structure.
Index ¶
- type Decoder
- func NewDecoder(b []byte) *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 ¶
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.
This test provides a semi-realistic example of how you can
use this package to decode a C structure.
Code:
Output:Example¶
{
// 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}
}
off=16 struct=cstruct.myStruct{Ptr:0x4030201, Ch:0x5, Intval:0x64e}
func NewDecoder ¶
NewDecoder creates a Decoder from a byte slice.
func (*Decoder) Byte ¶
Byte returns a single byte from the buffer.
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.
func (*Decoder) Err ¶
Err returns the first error that was encountered by this Decoder.
func (*Decoder) Int16 ¶
Int16 returns a int16 decoded from the buffer.
func (*Decoder) Int32 ¶
Int32 returns a int32 decoded from the buffer.
func (*Decoder) Int64 ¶
Int64 returns a int64 decoded from the buffer.
func (*Decoder) Offset ¶
Offset returns the current read offset for data in the buffer.
func (*Decoder) Uint16 ¶
Uint16 returns a uint16 decoded from the buffer.
func (*Decoder) Uint32 ¶
Uint32 returns a uint32 decoded from the buffer.
func (*Decoder) Uint64 ¶
Uint64 returns a uint64 decoded from the buffer.
func (*Decoder) Uintptr ¶
Uintptr returns a uintptr decoded from the buffer.
Source Files ¶
cstruct.go
- Version
- v1.84.0 (latest)
- Published
- May 21, 2025
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 1 day ago –
Tools for package owners.