C/C++将二进制数据映射到结构成员

发布于 2024-10-16 23:31:06 字数 1122 浏览 1 评论 0原文

假设我有以下文本文件:

清单 1:

Endianess=little
AddressModel=32

typedef struct{
  int    x;
  int    y;
  float  f;
  double d;
} A;

instance1:0x0000000100000002000048C19A99999999993C40
instance2:0x00100257000000090000000FBA99359976992397

其中,instance1 对应于结构 A 的一个实例,例如:

清单 2:

A->x = 0x00000001         = 1
A->y = 0x00000002         = 2
A->f = 0x000048C1         = -12.5
A->d = 0x9A99999999993C40 = 28.6

任务:< /strong>

编写一个应用程序,该应用程序采用文本文件、任意 C 数据结构和任意内存转储,并以易于阅读的格式打印出该结构实例的重建(例如如清单 2 所示)。

问题

  1. 最好的方法是什么?
  2. 与其重新发明轮子,是否有任何开源解决方案可以使这个问题受益?

需要考虑的事项:

解决方案必须

  1. 考虑数据类型的长度。
  2. 处理不同的地址模型和字节顺序。
  3. 显示十六进制以及该特定数据类型的本机显示。
  4. 采用未链接到您正在编写的程序中的 C 结构。

额外问题:

处理带有嵌入式结构的案例:

typedef struct{
  int    q;
  int    p;
  A      a;  //embedded struct of type A defined in listing 1
} B;

提前感谢大家!

Lets say, I have the following text file:

Listing 1:

Endianess=little
AddressModel=32

typedef struct{
  int    x;
  int    y;
  float  f;
  double d;
} A;

instance1:0x0000000100000002000048C19A99999999993C40
instance2:0x00100257000000090000000FBA99359976992397

Where instance1 corresponds to an instance of struct A, such as:

Listing 2:

A->x = 0x00000001         = 1
A->y = 0x00000002         = 2
A->f = 0x000048C1         = -12.5
A->d = 0x9A99999999993C40 = 28.6

The Task:

Write an application that takes as a text file, an arbitrary C data structure, and an arbitrary memory dump, and print out in an easy-to-read format, a reconstruction of the instance of that struct (such as what is seen in Listing 2).

The Questions:

  1. What is the best way to do this?
  2. Rather than re-invent the wheel, is(are) there any Open-source solutions that this problem might benefit from?

Things to consider:

The solution will have to

  1. take into account the length of the datatype.
  2. handle different address models and endianness.
  3. display both hex, and a native display for that particular data type.
  4. take C structs that were NOT linked into the program you are writing.

Bonus Question:

Handle a case with embedded structs:

typedef struct{
  int    q;
  int    p;
  A      a;  //embedded struct of type A defined in listing 1
} B;

Thanks in advance everyone!

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

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

发布评论

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

评论(1

微暖i 2024-10-23 23:31:06

你不能在 C 或 C++ 中使用反射,所以这是不可能的。这意味着您需要编写一个可以读取 C 结构定义的解析器。真的一点也不难。

如果您愿意,您可以自己做:标记和解析 C 并不难。或者您可以使用 lex 和 yacc 等工具。或者您可以使用 C++ 库,例如 Boost Spirit。

一旦有了解析器,您就可以用 C 语言构建数据读取器。您不想在 C 代码中重现该结构。您只想能够正确阅读它。

我会在解析步骤中编写一个数据类型和名称的数组。在二进制读取步骤中,逐步遍历数组并读取需要读取的字节数,然后生成输出行。重复此操作,直到用完数组中的数据类型。

You cannot use reflection in C or C++ so that's out. That means you will need to write a parser than can read C struct definitions. Not hard at all really.

You can do it yourself if you want: tokenizing and parsing C is not hard. Or you could use tools like lex and yacc. Or you could use a C++ library like Boost Spirit.

Once you've got a parser then you can build the data reader in C. You don't want to reproduce the struct in your C code. You just want to be able to read it properly.

I would write an array of data types and names during the parse step. During the binary read step, step through the array and read however many bytes you need to read, then produce the output line. Repeat until you run out of data types in the array.

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