What are important points when designing a (binary) file format? [closed]

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:

  • 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?


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

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.


I agree that these are good ideas:

  1. Magic numbers at the beginning. Pretty much required in *nix:

  2. File version number for backwards compatibility.

  3. Endianness specification.

But your fourth one is overkill, because #2 lets you add fields as long as you change the version number (and as long as you don't need forward compatibility).

  • possibly reserve some space for further per-file attributes that might be necessary in the future?

Also, the idea of imposing a block-structure on your file, expressed in many other answers, seems less like a universal requirement for binary files than a solution to a problem with certain kinds of payloads.

In addition to 1-3 above, I'd add these:

  • simple checksum or other way of detecting that the contents are intact. Otherwise you can't trust magic bytes or version numbers. Be careful to spec which bytes are included in the checksum. Typically you would include all bytes in the file that don't already have error detection.

  • version of your software (including the most granular number you have, e.g. build number) that wrote the file. You're going to get a bug report with an attached file from someone who can't open it and they will have no clue when they wrote the file because the error didn't occur then. But the bug is in the version that wrote it, not in the one trying to read it.

  • Make it clear in the spec that this is a binary format, i.e. all values 0-255 are allowed for all bytes (except the magic numbers).

And here are some optional ones:

  • If you do need forward compatibility, you need some way of expressing which "chunks" are "optional" (like png does), so that a previous version of your software can skip over them gracefully.

  • If you expect these files to be found "in the wild", you might consider embedding some clue to find the spec. Imagine how helpful it would be to find the string http://www.w3.org/TR/PNG/ in a png file.


It all depends on the purpose of the format, of course.

One flexible approach is to structure entire file as TLV (Tag-Length-Value) triplets. For example, make your file comprized of records, each record beginning with a 4-byte header:

1 byte  = record type
3 bytes = record length
followed by record content

Regarding the endianness, if you store endianness indicator in the file, all your applications will have to support all endianness formats. On the other hand, if you specify a particular endianness for your files, only applications on platforms with non-matching endiannes will have to do additional work, and it can be decided at compile time (using conditional compilation).