Quality Software Solutions
  • Specializing in professional hex editor tools,
    including disk editing and process editing.




Binary Templates - Parsing Binary Files

A Binary Template is a file used to parse a binary file into a data structure. Instead of scanning through a series of hex bytes as with traditional hex editors, Templates allow data to be understood and edited in a much more powerful and intuitive way.

Templates are similar to structure definitions in C/C++, but are more flexible since they may include if, for, or while statements. A Template is executed as a program, starting from the first line of the file. Whenever a variable is declared, that variable is mapped to a set of bytes in a file. Data from that file can then be read from or written to by accessing the created variable. See below for an example of a Template.

010 Editor contains full support for editing and running Binary Templates. An integrated source code editor is provided with syntax highlighting (pictured above right). Templates can even be configured to run automatically when a file is opened.

Note: Some other editors provide a structure viewer using structs similar to C/C++; however, these viewers are not nearly as powerful as Binary Templates are not capable of parsing entire binary files.

Example Template

The following example demonstrates a simple Binary Template. This Template is designed for a binary file containing a series of employee records.


struct FILE {
    struct HEADER {
	char    type[4];
        int     version;
        int     numRecords;
    } header;

    struct RECORD {
        int     employeeId;
        char    name[40];
        float	salary;
        if( file.header.version > 1 )
            int      numChildren;
        if( file.header.version > 2 )
            time_t   birthDate;
    } record[ file.header.numRecords ];

} file;

When a variable is defined in the Template, that variable is mapped to a set of bytes in a file. In this case, the variable type would be mapped to the first four bytes of the file, version would be mapped to the next four bytes, and so on. When executing the Template, any of the variables defined can be accessed as soon as they are declared. Here file.header.version can be used to read data from the file even though file is not completely defined.

Templates are very flexible any may contain any of the regular C operators including +, -, *, /, &, |, ^, ~, %, ++, --, ?:, etc. A large number of functions are available to modify how Templates run. See Template Download below for more examples of Templates.

Editing the Template Results

Once a Template is run, the variables defined in the Template can be accessed in the Template Results panel displayed below the Hex Editor, or in the Template tab of the Inspector. The Template Results shows a full hierarchal view of the data as shown on the left. When a variable is selected from the list, the corresponding hex bytes will be selected in the main Hex Editor Window. Variables can be edited by clicking on the Value field, entering a new value and pressing Enter.

Another way of reading values from the Template variables is to position the mouse cursor over some bytes in the Hex Editor Window. A hint popup will be displayed that indicates the value of the variable at that position. To lookup which variable corresponds to a certain byte position, move the cursor to that position and press Ctrl+J (Jump to Template Variable) and 010 Editor will locate the variable in the Template Results.

Editing with Scripts

Another way of editing variables produced from a Template is to use a Script. Scripts have a syntax similar to C and define variables in the regular way. For example, to double every employee's salary enter the following Script in the Code Editor and execute it:


int i;
for( i = 0; i < file.header.numRecords; i++ )
    file.record[i].salary *= 2.0;

The Script can modify any of the variables defined in the Template. Undo and Redo are supported for Scripts as with any other editing operation.

Advanced Features

010 Editor includes some additional functionality that make Templates even more powerful. For example:

  • Define regular C variables in a Template using the local keyword.
  • Apply colors to Template variables so they will stand out in the editor (see the functions SetBackColor, SetForeColor, or SetColor in the documentation).
  • The endian can be switched in the Template, allowing big-endian or little-endian data to be read (see the BigEndian or LittleEndian functions in the documentation).
  • Template variables can be read in any order by using the functions FSeek or FTell to move around the file.
  • Define your own Custom Variables by writing special read and write functions. This syntax allows data to be read in practically any possible format.
  • Data can be read from a file without declaring a Template variable using the functions ReadByte, ReadShort, ReadInt, etc.
  • Change the format of the data displayed in the Template Results using the syntax <format=hex|decimal|octal|binary> after a variable declaration.
  • Both structs and unions are supported and can be used to define recursive data types.

Template Download

A number of example Templates are available in the online template repository, which can be accessed using the following link:

Download Templates

To submit Templates to the archive, use the Submit Template page. Feel free to submit any Templates you have which may be useful to other people.

For more information about 010 Editor, see the 010 Editor Home Page, Screen Shots, or the Full Feature List.





  010 Editor Menu

  Latest News
- 010 Editor
v2.1
is now available.
Download it now!
What's New in 2.1?

  Our Clients Say...
"...your package is simply the best on the net. I have downloaded most, and yours is the best interface, is fast, and gives me data that is very useful in my research."
    - Cameron Jones

More...

  Other Products

  Articles

  Newsletter
Join our newsletter to receive tips and tricks on hex editing, special offers, and news from SweetScape Software.

Sign-up Now...




SweetScape Software
148 Pownal Rd. RR#1
Pownal, PEI
C0A 1Z0, Canada

E-mail: