设计(二进制)文件格式时要注意什么? [英] What are important points when designing a (binary) file format?

查看:177
本文介绍了设计(二进制)文件格式时要注意什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当设计用于记录二进制数据的文件格式时,您认为格式应该具有什么属性?到目前为止,我已经提出了以下重点:

When designing a file format for recording binary data, what attributes would you think the format should have? So far, I've come up with the following important points:


  • 开始时有一些魔术字节,能够识别文件(在具体情况下,这也有助于区分文件和遗留文件)

  • 在开头有一个文件版本号,以便文件格式可以更改后,不会破坏兼容性

  • 指定所有数据项的字节和大小;或者:包含一些空格来描述数据的字节/大小(我会倾向于前者)

  • 可能为将来可能需要的进一步的每个文件属性预留一些空间? / li>
  • have some "magic bytes" at the beginning, to be able to recognize the files (in my specific case, this should also help to distinguish the files from "legacy" files)
  • have a file version number at the beginning, so that the file format can be changed later without breaking compatibility
  • specify the endianness and size of all data items; or: include some space to describe endianness/size of data (I would tend towards the former)
  • possibly reserve some space for further per-file attributes that might be necessary in the future?

还有什么会使格式更具前瞻性,并尽量减少头痛?

What else would be useful to make the format more future-proof and minimize headache in the future?

推荐答案

查看 PNG规范。这种格式有一些非常好的理由背后。

Take a look at the PNG spec. This format has some very good rationale behind it.

另外,决定什么对您的未来格式很重要:紧凑性,兼容性,允许嵌入其他格式(不同的压缩算法)内它。另一个有趣的例子是 Google的协议缓冲区 ,其中传输的数据的大小是王。

Also, decide what's important for your future format: compactness, compatibility, allowing to embed other formats (different compression algorithms) inside it. Another interesting example would be the Google's protocol buffers, where size of the transferred data is the king.

关于字节顺序,我建议您选择一个选项并坚持使用,不允许不同的字节顺序。否则,读书和写书库只会变得更复杂和更慢。

As for endianness, I'd suggest you to pick one option and stick with it, not allowing different byte orders. Otherwise, reading and writing libraries will only get more complex and slower.

这篇关于设计(二进制)文件格式时要注意什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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