Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I choose a good magic number for my file format?

I am designing a binary file format from scratch, and I would like to include some magic bytes at the beginning so that it can be identified easily. How do I go about choosing which bytes? I am not aware of any central registry of magic numbers, so is it just a matter of picking something fairly random that isn't already identified by, say, the file command on a nearby UNIX box?

like image 384
jl6 Avatar asked Aug 25 '10 12:08

jl6


People also ask

What is magic number in file format?

ANSWER: A magic number is a numeric or string constant that indicates the file type. This number is in the first 512 bytes of the file. By default the localized magic file /usr/lib/locale/locale/LC_MESSAGES/magic is used to identify files that have a magic number.

What is magic number in file upload?

A magic number is a number embedded at or near the beginning of a file that indicates its file format (i.e. the type of file it is). This number is not visible to us. Every file has a number that represents the name of file types which is hexadecimal format.


1 Answers

Stay away from super-short magic numbers. Just because you're designing a binary format doesn't mean you can't use a text string for identifier. Follow that by an EOF char, and as an added bonus people who cat or type your binary file won't get a mangled terminal.

like image 173
snemarch Avatar answered Sep 19 '22 18:09

snemarch