使用定义结构的 C 头文件解析数据

发布于 2025-01-07 04:43:01 字数 999 浏览 0 评论 0原文

我有一个像这样的 C 头文件:

#define NAME_LEN 8
#define DEV_MAX  4

typedef struct __device
{
    int iDevID;
    int iDevSN;
}DEVICE;

typedef struct __person
{
    int iID;
    char acName[NAME_LEN];
    DEVICE aDevices[DEV_MAX];
}PERSON;

和一个可能像这样的二进制数据文件:

0000000 01 00 08 00 4a 61 63 6b 00 00 00 00 0a 00 00 00
0000020 11 11 11 11 0b 00 00 00 22 22 22 22 0c 00 00 00
0000040 33 33 33 33 0d 00 00 00 44 44 44 44

我所需要的只是使用上面的 C 头文件来可视化带有字段名称的数据表示...... 这样会更好...

m--iID : 0x80001
m--acName : Jack
m--aDevices[]
|--aDevices[0]
|--|--iDevID : 0xa
|--|--iDevSN : 0x11111111
|--aDevices[1]
|--|--iDevID : 0xb
|--|--iDevSN : 0x22222222
|--aDevices[2]
|--|--iDevID : 0xc
|--|--iDevSN : 0x33333333
|--aDevices[3]
|--|--iDevID : 0xd
|--|--iDevSN : 0x44444444

或者其他结构化数据.. xml / python pickle / json strings /无论什么

当然,我面对的头文件要复杂得多,其中会有一个 msgtype 和一个 msglenth 字段数据,这样我就可以找出哪个是正确的结构以及它有多长。

I have a C header file like this:

#define NAME_LEN 8
#define DEV_MAX  4

typedef struct __device
{
    int iDevID;
    int iDevSN;
}DEVICE;

typedef struct __person
{
    int iID;
    char acName[NAME_LEN];
    DEVICE aDevices[DEV_MAX];
}PERSON;

and a binary data file maybe like this:

0000000 01 00 08 00 4a 61 63 6b 00 00 00 00 0a 00 00 00
0000020 11 11 11 11 0b 00 00 00 22 22 22 22 0c 00 00 00
0000040 33 33 33 33 0d 00 00 00 44 44 44 44

All that what I need is to visulized data representation with field names using the C header file above....
It'll be better like this...

m--iID : 0x80001
m--acName : Jack
m--aDevices[]
|--aDevices[0]
|--|--iDevID : 0xa
|--|--iDevSN : 0x11111111
|--aDevices[1]
|--|--iDevID : 0xb
|--|--iDevSN : 0x22222222
|--aDevices[2]
|--|--iDevID : 0xc
|--|--iDevSN : 0x33333333
|--aDevices[3]
|--|--iDevID : 0xd
|--|--iDevSN : 0x44444444

or other structured data .. xml / python pickle / json strings / whatever

Of course, the header file which I faced is far more complicated, there will be a msgtype and a msglenth field in the data, so I can find out which is the correct structure and how long is it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久夏青 2025-01-14 04:43:01

你有多需要它?

一个可能的解决方案可能是制作一个 GCC 插件或 MELT 扩展(MELT 是一种用于扩展 GCC 的领域特定语言) ),但要做到这一点,您需要了解 GCC 的内部表示的一些细节(特别是 Tree,也许还有 Gimple),这将花费您一些时间(几天,而不是几小时)。

如果您的声明更简单,也许可以考虑使用 SWIG (或者可能是 RPCXDR 解析器),但这假设您是能够改变或简化它们。

How badly do you need that?

A possible solution might be to make a GCC plugin or a MELT extension (MELT is a domain specific language to extend GCC), but to do that you'll need to understand in some details the internal representation of GCC (notably Tree, and perhaps Gimple), and that will take you some time (days, not hours).

If your declarations are simpler, perhaps consider using SWIG (or maybe the RPCXDR parser), but that supposes that you are able to change or simplify them.

找个人就嫁了吧 2025-01-14 04:43:01

如果二进制格式与结构的内存布局相同,您可以直接转换它,不需要解析(有一些注意事项)。但是,这显然不是您的意思,因为您的十六进制转储和示例输出与该解释不匹配。

不过,您需要实际解释您的格式:如下所述,它并不明显。


您似乎有小端顺序的固定长度 4 八位字节整数,好吧。

如果我假设带有空终止符的可变长度字符串,则 4a 61 63 6b 00 = acName:"Jack"0a 00 00 00 = iDevID:0x0a 看起来不错,但是它们之间有一个 3 个八位字节的序列,我不知道其含义。

或者 Jack 不是以 null 结尾的,在这种情况下,它的长度固定为 4 个字符,而不是您为 NAME_LEN 定义的 8 个字符?这将使 00 6f 70 65 成为另一个 4 字节整数,但我仍然不知道它意味着什么。

...

If the binary format were identical to the memory layout of your structure, you could just cast it, no parsing required (with some caveats). However, that evidently isn't what you mean, since your hex dump and sample output don't match that interpretation.

You'll need to actually explain your format though: as described below, it isn't obvious.


You seem to have fixed-length 4-octet integers in little-endian order, OK.

If I assume variable length strings with a nul-terminator, 4a 61 63 6b 00 = acName:"Jack" and 0a 00 00 00 = iDevID:0x0a looks ok, but there is a 3-octet sequence between them I don't know the meaning of.

Or is Jack not nul-terminated, in which case it's fixed at 4 characters long and not the 8 you defined for NAME_LEN? That would make 00 6f 70 65 another 4-byte integer, but I still don't know what it means.

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文