Wednesday, April 4, 2007

Learn from PNG file header

When you designing your own file format, you should consider to use the PNG file header.  This is the smartest file header.

Here is the first 8 bytes of a PNG file:

13780787113102610
PNG\r\nCtrl-Z\n

Each byte has it's own usage:
 
137
This is a non-ASCII character to serve two purposes:
- Text editor will treat it as binary file
- To catch the error on FTP ASCII transfer types

80 78 71 (PNG)
Human readable file type

13 10 (\r \n)
To catch the the file is converted from Windows line encoding to Unix line encoding

26 (Ctrl-Z)
This is end-of-file marker to prevent dispalying lots of garbage characters if user "type" file in DOS

10
To catch the the file is converted from unix Line encoding to Windows line encoding

The PNG file header can detect common transfer errors and is readable if type the file in DOS terminal.