There are some C objects like unions, structs that contain bitfields and structs whose alignment differs from Go's ABI, that cannot be accessed from Go. Some of these structures cannot be changed to be accessible from Go code as they are part of the API of an existing library.
To marshall such objects into Go structures we thus cannot really use Go code. Instead w have to write the marshalling code in C. This works fine but I have not found a feasible way to define C functions that operate on types defined in Go code. Right now I am defining the data types I am marshalling into on the C side and use these data types in my Go code.
This is really nasty if I want to expose the marshalled types as an API in my Go code, as I cannot expose a C type as a part of my package interface. My current approach involves remarshalling the already marshalled object into a type that is defined in Go code.
Is there a more elegant way to do what I want to do, i.e. marshalling C structs that cannot be accessed from Go code into data types defined in Go code?
As requested in the comment section, here is a collection of C objects that cannot be accessed from Go.
#include <complex.h>
#include <stdbool.h>
union foo {
int i;
float f;
};
struct bar {
bool x:1;
unsigned int y:3;
unsigned int z:4;
};
struct baz {
float f;
complex float c;
};
#pragma pack 1
struct quux {
char c;
short s;
int i;
};
The standard package encoding/binary
can be used for manipulating raw C structs.
You can extend Read and Write functions to support custom types :
func Read(r io.Reader, order binary.ByteOrder, data interface{}) error {
switch data := data.(type) {
case *foo:
return readFoo(r, order, data)
// (...)
default:
return binary.Read(r, order, data)
}
}
func Write(w io.Writer, order binary.ByteOrder, data interface{}) error {
switch data := data.(type) {
case foo:
return writeFoo(r, order, data)
// (...)
default:
return binary.Write(r, order, data)
}
}
Use a struct containing all the union's fields and use application context to decide which value to encode into the C union.
type foo struct {
is_i bool
i int32
f float32
}
// Read a foo from r into data
func readFoo(r io.Reader, order binary.ByteOrder, data *foo) error {
b := make([]byte, 4)
if _, err := io.ReadFull(r, b); err != nil {
return err
}
*data = foo{
i: int32(order.PutUint32(b)),
f: float32(order.PutUint32(b)),
}
return nil
}
// Write a foo from data into w
func writeFoo(w io.Writer, order binary.ByteOrder, data foo) error {
b := make([]byte, 4)
if data.is_i {
order.PutUint32(b, uint32(data.i))
} else {
order.PutUint32(b, uint32(data.f))
}
_, err := w.Write(b)
return err
}
(Alternatively, using getters and setters: http://pastebin.com/H1QW5AFb)
Use bitwise operations to marshal bitfields
type bar struct {
x bool
y uint
z uint
}
// Read a bar from r into data
func readBar(r io.Reader, order binary.ByteOrder, data *foo) error {
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err != nil {
return err
}
// Read from bitfield
*data = bar{
x: bool(b[0] >> 7), // bool x:1;
y: uint((b[0] & 0x70) >> 3), // unsigned int y:3;
z: uint(b[0] & 0x0f), // unsigned int z:4;
}
return nil
}
// Write a bar from data into w
func writeBar(w io.Writer, order binary.ByteOrder, data bar) error {
b := make([]byte, 1)
var x uint8
if data.x {
x = 1
}
// Create bitfield
b[0] = (x & 0x01 << 7) & // bool x:1;
(uint8(data.y) & 0x03 << 4) & // unsigned int y:3;
(uint8(data.z) & 0x04) // unsigned int z:4;
_, err := w.Write(b)
return err
}
The serialized form of baz depends on the compiler's internal definition of complex.
When using encoding.binary
, fields have a 1-byte alignement so quux can be marshaled directly.
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