Click here to Skip to main content
Click here to Skip to main content

Binary Templates - A New Way of Editing Binary Files

, 14 Jan 2004
Rate this:
Please Sign up or sign in to vote.
Introduces Binary Templates, a new way of editing binary files. Binary Templates look like C/C++ structures but are run as a program, and thus are extremely flexible and can be used for parsing virtually any binary file.

Sample Image - binary_templates.gif

Introduction

Binary files have always been difficult to edit, usually involving scanning through an endless number of bytes using a hex editor. Binary Templates offer a new way to edit a binary file that is much easier and more intuitive. A Binary Template looks similar to a C/C++ structure definition; however, Binary Templates are much more powerful than structures since they are executed as a program. This difference allows complex expressions, functions, and control statements such as if-else or do-while, to be contained inside of the C/C++ structures. Thus Binary Templates are powerful and flexible enough to work with practically any binary file format, from WAV files up to Word documents.

Binary Templates are currently supported by 010 Editor, a professional hex editor by SweetScape Software. When a Binary Template is run in 010 Editor, a binary file is parsed into a hierarchal data structure that can be viewed and edited using the program's interface. To install 010 Editor and some sample Binary Templates for BMP and ZIP files, see the download link above.

Sample Code

The code below demonstrates a sample Binary Template that could be used to read your own custom image format. Whenever a variable is declared, that variable is mapped to a set of bytes in a binary file. For example, the variable header would be mapped to the first four bytes of the file, the variable version to the next 4 bytes, and so on.

    // Define a structure used for a color
    typedef struct
    {
        uchar   red;
        uchar   green;
        uchar   blue;
    } RGBTRIPLE;

    // Define the image file
    struct FILE
    {
        // Define the header information
        char    header[4];
        int     version;
        int     width;
        int     height;
        int     format;

        // Define the lines of the bitmap
        struct LINE
        {
            RGBTRIPLE colors[ file.width ];
        } 
        lines[ file.height ];
        
    } file;

Binary Templates can also contain control statements. For example, to read a different set of lines based on the version, you could use:

    // Define the lines of the bitmap
    struct LINE
    {
        if( file.version == 0 )
            RGBTRIPLE colors[file.width];
        else
            RGBQUAD   colors[file.width];
    } 
    lines[ file.height ];

Templates have a number of other powerful features. Local variables that are not mapped to a file can be declared using the local keyword. Over 90 different functions are available for reading data, manipulating strings, or calling mathematical functions. Coloring can be applied to different parts of the file and big and little endian data is easily handled. Most Templates parse a file starting at the beginning and move to the end; however, parsing a file in any order is possible by using the functions FSeek or FTell.

To see a Binary Template in action, download and install 010 Editor from the link at the top of the page. Then open a BMP or ZIP file and a Template will automatically be run. Next click on the 'Template' tab of the Inspector and the whole file will be presented in a data structure for easy editing.

For More Information

More information on Binary Templates or 010 Editor see:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

gsweet
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

 
Generalfinding out LOC from a Binary File Pinmemberd_s_kumar30-Sep-04 20:51 
GeneralRe: finding out LOC from a Binary File PinmemberMr.Prakash25-Nov-04 6:11 
GeneralPlz delete this article, as this is a total ad. PinmemberSnyp17-Jan-04 12:26 
GeneralRe: Plz delete this article, as this is a total ad. PinmemberJerry Jeremiah30-Apr-06 20:20 
GeneralRe: Plz delete this article, as this is a total ad. PinsitebuilderTroy Marchand2-May-06 11:27 
GeneralRe: Plz delete this article, as this is a total ad. PinmemberJerry Jeremiah2-May-06 20:28 
QuestionWhere is the source code? PinmemberWREY15-Jan-04 23:26 
AnswerRe: Where is the source code? PinsussIan Prest16-Jan-04 3:16 
GeneralRe: Where is the source code? Pinmembergsweet16-Jan-04 5:14 
GeneralRe: Where is the source code? PinmemberDarren Schroeder21-Jan-04 3:43 
GeneralRe: Where is the source code? PinmemberJerry Jeremiah30-Apr-06 20:34 
GeneralRe: Where is the source code? Pinmembergsweet2-May-06 11:41 
GeneralRe: Where is the source code? PinmemberAndrew Phillips7-Apr-08 19:45 
GeneralThis is great! PinmemberDaniel Turini15-Jan-04 9:59 
GeneralRe: This is great! Pinmemberleppie15-Jan-04 10:34 
GeneralHell, THE editor ! PinmemberKochise15-Jan-04 21:29 
GeneralRe: Hell, THE editor ! Pinmembergsweet16-Jan-04 5:36 
GeneralRe: This is great! PinmemberAndrew Phillips7-Apr-08 19:39 
QuestionAdvantages? Pinmemberdog_spawn15-Jan-04 9:02 
AnswerRe: Advantages? Pinmembergsweet16-Jan-04 4:52 
GeneralRe: Advantages? Pinmemberdog_spawn16-Jan-04 5:02 
GeneralRe: Advantages? PinmemberAndrew Phillips7-Apr-08 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 15 Jan 2004
Article Copyright 2004 by gsweet
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid