PNG has a well designed magic number (or FourCC)

— richardwb on Friday, April 03, 2009 @ 17:20

Even with all the XML floating around, binary formats still have their place. It's good practice to place an identifier at the beginning of the binary file, so that the file type can be determined even in the absence of a proper file extension.

The simplest route to take is to just stuff some plain text there:

[M Y F I L E][rest of file]

However, consider following the design of the PNG magic number, described in detail in the PNG book. With some really neat design they are able to quickly detect some common forms of corruption caused by an incompatible transfer mechanism. In that case your identifier may look like:

[\211 M Y F I L E \r \n \032 \n][rest of file]

With an identifier like this many common forms of transfer corruption can be quickly identified, and you can spit out a simple "corrupt file" message instead of trying to read a broken file.

comments powered by Disqus