I have some old C code that makes somewhat heavy use of packed structures. I'm looking into using Go as a wrapper for this code, but am having difficulty finding a way to pass or even write definitions for these structures.
Example:
import "unsafe";
type AlignTest struct {
    c byte;
    y int16;
    z int16;
    q int32;
}
func main() {
    vr := new(AlignTest);
    fmt.Println(unsafe.Sizeof(*vr),  "\n");
}
Returns 12 rather than the 1+2+2+4 = 9 that I would want with a packed/unaligned struct.
I know that I could just create a byte array and do the parsing manually, but that seems very brittle and error prone...
You could try something like this.
package main
import (
    "encoding/binary"
    "bytes"
    "fmt"
)
type Unpacked struct {
    C byte
    Y int16
    Z int16
    Q int32
}
type Packed struct {
    B [9]byte
}
func main() {
    var u Unpacked
    var p Packed
    var buf = bytes.NewBuffer(make([]byte, 0, len(p.B)))
    // Unpacked to Packed 
    u = Unpacked{1, 2, 3, 4}
    if err := binary.Write(buf, binary.BigEndian, &u); err != nil {
        fmt.Println(err)
    }
    if err := binary.Read(buf, binary.BigEndian, &p); err != nil {
        fmt.Println(err)
    }
    fmt.Println("u", u, "to", "p", p)
    // Packed to Unpacked
    p = Packed{[...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}}
    if err := binary.Write(buf, binary.BigEndian, &p); err != nil {
        fmt.Println(err)
    }
    if err := binary.Read(buf, binary.BigEndian, &u); err != nil {
        fmt.Println(err)
    }
    fmt.Println("p", p, "to", "u", u)
}
.
Output:
u {1 2 3 4} to p {[1 0 2 0 3 0 0 0 4]}
p {[1 2 3 4 5 6 7 8 9]} to u {1 515 1029 101124105}
                        You may want to rethink your architecture- try passing the binary input down to the C layer and use the existing structures (you won't break what you don't change). I'm assuming the structure packing looks something like this:
#ifdef WINDOWS
#pragma pack(push)
#endif
#pragma pack(BYTEALIGNMENT) // e.g. "#pragma pack(1)" or "#pragma pack(8)"
//--- Some packed structs
#ifdef WINDOWS
#pragma pack(pop)
#endif
#ifdef POSIX
#pragma pack()
#endif
All the underlying or 3rd Party libs are then doing is taking some void* or const char* and typecasting it to these. So if possible, try forwarding that data into a C layer (where you can get pointers) and don't expose the structures at all.
There's no way to tell gccgo to compile packed structures. The best solution I can think of is to manually add padding:
type AlignTest struct {
    c byte
    _ [3]byte // anonymous padding
    y int16
    z int16
    q int32
}
                        This works:
package main
import "unsafe"
import "fmt"
/*
struct test {
    char c;
    short i;
    short j;
    int k;
} __attribute__((packed));
*/
import "C"
type AlignTest struct {
    c byte
    y int16
    z int16
    q int32
}
func main() {
    vr := new(AlignTest)
    v := new(C.struct_test)
    fmt.Println(unsafe.Sizeof(*vr), "\n")
    fmt.Println(unsafe.Sizeof(*v), "\n")
}
Output:
12
9
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With