简体   繁体   English

(gcc)go 中的打包结构

[英]Packed Structs in (gcc)go

I have some old C code that makes somewhat heavy use of packed structures.我有一些旧的 C 代码在某种程度上使用了打包结构。 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.我正在研究使用 Go 作为此代码的包装器,但我很难找到一种方法来传递甚至为这些结构编写定义。

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.返回 12 而不是 1+2+2+4 = 9 我想要的压缩/未对齐结构。

I know that I could just create a byte array and do the parsing manually, but that seems very brittle and error prone...我知道我可以创建一个字节数组并手动进行解析,但这似乎非常脆弱且容易出错......

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).您可能需要重新考虑您的架构 - 尝试将二进制输入向下传递到 C 层并使用现有结构(您不会破坏您不更改的内容)。 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.然后,所有底层或第 3 方库正在做的是获取一些 void* 或 const char* 并将其类型转换为这些。 So if possible, try forwarding that data into a C layer (where you can get pointers) and don't expose the structures at all.因此,如果可能,请尝试将该数据转发到 C 层(您可以在其中获取指针)并且根本不公开结构。

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}

There's no way to tell gccgo to compile packed structures.没有办法告诉 gccgo 编译打包结构。 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: Output:

12

9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM