
|
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 and running 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.
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 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.
|
 |
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.
|

|







|