Magic Number

From Sigma Design Wiki

Jump to: navigation, search

In computer programming, a magic number is a constant used to identify the file or data type employed. The term was initially found in early Seventh Edition source code of the Unix operating system and, although it has lost its original meaning, it has become part of computer industry lexicon.


Magic number origin

When Unix was first being developed, it would load a program into memory and then execute it. However if something went wrong in the load process, or in the determination of where in memory to execute the program, then bad results would appear. So a magic number was placed at the beginning of the program.

This would assure that:

  1. The program had been read properly from the disk.
  2. The program was an executable in the proper format.
  3. Also, the magic number could be used to identify different versions of a program, or different executable mode.

Use in ARRIS CAD

As ARRIS was being developed in UNIX, errors would occur in either in reading and writing the disk, or in memory allocation, so magic numbers were often placed at the beginning of files, and at the beginning of data stored in the file to assure that the file and data was loaded properly.

For instance, an RI library is stored in a file, such as user.ri. If this file becomes corrupted on the disk - either because of a disk error, or because some other process overwrites it with a file of the wrong type, then the magic number would be messed up and ARRIS can easily determine that it should try to read and interpret the file.

Also, files are sometimes only partially written - because a process fails while writing the file, or (especially years ago), because people would try to save a file on a floppy disk when there was not room for it. So an additional magic number was placed at the end of the file, and of that magic number was not found, then, again, ARRIS would know that the file was corrupted.

Use in Multi Platforms

Because ARRIS supports data files created on various platforms. (UNIX and Window/DOS store data backwards, and have other format changes), ARRIS developed a "magic number" scheme to not only determine the type of file, but also what platform it was created on.

For instance, the two first lines of the main Sigmac file (smfile.sys) look like this (when displayed as ascii text):

  • ÿÿff72 80BSL
  • ÿÿÿÿff64 80BSL

Each line has a magic number (ÿÿff72 is how this binary number is displayed). The one in the first line means this is a Sigmac File. The one on the second line is used to identify an actual Sigmac withing the file.

The ASCII numbers after the magic number identify the platform on which it was created.

  • 80 means that the platform has 8 byte structures. (Some older platforms, had 1, 2 or 4 byte structures. This means that data written on one platform may be shorter or longer than the same data written on a different platform.)
  • BSL tells whether bytes, short integers and long integers are reversed on this platform.

(Capital letters mean that all 3 are reversed from UNIX. On a UNIX platform, this same "magic string" would be in lower case, "bsl", meaning they are not reversed.)

Personal tools