使用带有attibute结构解析二进制数据时打包 [英] Using structs with attibute packed when parsing binary data

查看:110
本文介绍了使用带有attibute结构解析二进制数据时打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约有见过各种code,其中一个数据读入一个字符无效,然后
其转换为结构。实例解析是哪里数据固定偏移量的文件格式。

Have seen various code around where one read data into a char or void and then cast it to a struct. Example is parsing of file formats where data has fixed offsets.

例如:

struct some_format {
    char magic[4];
    uint32_t len;
    uint16_t foo;
};

struct some_format *sf = (struct some_format*) buf;

要肯定这始终是有效的一是需要对齐结构使用 __属性__((包装))

To be sure this is always valid one need to align the struct by using __attribute__((packed)).

struct test {
    uint8_t a;
    uint8_t b;
    uint32_t c;
    uint8_t d[128];
} __attribute__((packed));

在读大而复杂的文件格式,这无疑使事情变得更简单。通常情况
阅读媒体格式与结构有30多个成员等。

When reading big and complex file formats this surely makes things much simpler. Typically reading media format with structs having 30+ members etc.

这也很容易通过例如在一个巨大的缓冲和转换为正确的类型如下:

It is also easy to read in a huge buffer and cast to proper type by for example:

struct mother {
    uint8_t a;
    uint8_t b;
    uint32_t offset_child;
};

struct child {
     ...
}

m = (struct mother*) buf;
c = (struct child*) ((uint8_t*)buf + mother->offset_child);

或者

read_big_buf(buf, 4096);

a = (struct a*) buf;
b = (struct b*) (buf + sizeof(struct a));
c = (struct c*) (buf + SOME_DEF);
...

这也将是很容易快速地写出这样的结构到文件。

It would also be easy to quickly write such structures to file.

我的问题是如何好或坏编码这种方式。我在看各种数据
结构和会用最好的方式来处理这个问题。

My question is how good or bad this way of coding is. I am looking at various data structures and would use the best way to handle this.


  • 这个的如何完成的?的(如:这是常见的做法)

  • __ __属性((包装))总是安全的?

  • <击>是它更好地使用的sscanf 我在想什么?关于感谢的@Amardeep

  • 是更好地使功能,其中一个用石膏和比特移位启动结构。


  • Is this how it is done? (As in: is this common practice.)
  • Is __attribute__((packed)) always safe?
  • Is it better to use sscanf. What was I thinking about?, Thanks @Amardeep
  • Is it better to make functions where one initiates structure with casts and bit shifting.
  • etc.

截至目前,我用这主要表现在数据信息的工具。如清单的所有结构
在象例如一个文件格式的特定类型以及它们的值一个媒体流。
信息倾销的工具。

As of now I use this mainly in data information tools. Like listing all structures of a certain type with their values in a file format like e.g. a media stream. Information dumping tools.

推荐答案

这是它是如何做的有时。包装是安全的,只要你正确地使用它。使用的sscanf()将意味着你正在阅读的文本数据,这是一个不同的使用情况下,比从结构的二进制图像。

It is how it is sometimes done. Packed is safe as long as you use it correctly. Using sscanf() would imply you are reading text data, which is a different use case than a binary image from a structure.

如果您code并不需要跨编译器和/或平台(CPU架构)的便携性,以及你的编译器对压缩结构的支持,那么这是访问序列化数据的一个完全合法的方式。

If your code does not require portability across compilers and/or platforms (CPU architectures), and your compiler has support for packed structures, then this is a perfectly legitimate way of accessing serialized data.

然而,如果你尝试生成一个平台上的数据,并用它在另一个因可能出现的问题:

However, problems may arise if you try to generate data on one platform and use it on another due to:


  1. 主机字节顺序(小端/大端)

  2. 大小不同语言的基本类型(长例如可以是32位或64位)

  3. 在一边而不是其他。
  4. code变化
  1. Host Byte Order (Little Endian/Big Endian)
  2. Different sizes for language primitive types (long can be 32 or 64 bits for example)
  3. Code changes on one side but not the other.

有,简化了序列化/反序列化和处理这些问题多数库。这种操作的开销是必须跨越的进程和主机系统更容易有道理的。但是,如果你的结构是非常复杂的,采用SER / DES库可能是合理的只是由于易于维护。

There are libraries that simplify serialization/deserialization and handle most of these issues. The overhead of such operations is easier justified on systems that must span processes and hosts. However, if your structures are very complex, using a ser/des library may be justified simply due to ease of maintenance.

这篇关于使用带有attibute结构解析二进制数据时打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆