Edit Anything

Professional text and hex editing
with Binary Templates technology.





Binary Templates

Helping you Understand Binary Data
Editing binary data is hard. You have to know exactly which bytes to edit in a file and editing the wrong bytes can render a file unusable. Binary Templates take the mystery out of binary data by allowing you to understand data and edit it in powerful, intuitive way.

The image on the right displays a ZIP file opened in 010 Editor. Most hex editors just display the raw hex bytes of the file (1) but 010 Editor goes beyond this by showing the file parsed into a hierarchical data structure for easy editing (2). The data structure is make up of a series of variables and selecting a variable in the list shows which hex bytes correspond to that variable (in the image, the variable 'frVersion' corresponds to the hex bytes '14 00').

010 Editor can automatically apply a Binary Template to a file when the file is opened. Binary Templates for ZIP, BMP, and WAV files are included with 010 Editor and a whole range of other templates are available for download in our Template Archive. If you are working with your own binary format, writing your own Binary Template is not difficult.


Writing Binary Templates

Binary Templates look similar to structure definitions in C/C++, but are more flexible since they may include if, for, or while statements as well as functions or complex expressions. 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. For an example of how a Template works, see the next section.

010 Editor contains full support for editing, running and debugging Binary Templates. An integrated source code editor is provided with syntax highlighting (pictured left).

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;
        ushort  numRecords;
    } header <bgcolor=cLtGray>;

    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, numRecords the next two bytes and so on (see the image below). 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 complex expressions including any of the regular C operators such as +, -, *, /, &, |, ^, ~, %, ++, --, ?:, 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 Variables 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.

To lookup which variable corresponds to a certain byte position in the Hex Editor, click on a byte in the Hex Editor and press Ctrl+J (Jump to Template Variable). 010 Editor will select the variable in the Template Results which contains that byte.

Struct Outlining

Another way of reading values from the Template variables is to position the mouse cursor over some bytes in the Hex Editor Window. If the bytes belong to a struct in the Template Results, lines will be drawn around the struct to indicate where the variables exist. This is called struct outlining. A hint popup will also be displayed which shows the value of the variable at that position.

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 the following Script could be used:


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

The Script can automatically 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 from the same file (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 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.
  • Templates can be written containing millions upon millions of variables by using On-Demand Structures.
  • Can call functions in external DLLs.
  • Integrated disassembler for X86, ARM, MIPS, PowerPC, SPARC, SystemZ, and XCore.
  • A full debugger is included for Templates and Scripts including breakpoints, stepping, watches and call stack.

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, see the Submitting Files 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, Online Tour or the Full Feature List.





Newsletter - Receive special offers, tips, tricks and news. Join now



010 Editor v15.0 is here!
What's new?


Navigation


Products

010 Editor














E-mail: info@sweetscape.com