Click here to Skip to main content
Licence 
First Posted 14 Jan 2004
Views 62,676
Bookmarked 24 times

Binary Templates - A New Way of Editing Binary Files

By | 14 Jan 2004 | Article
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

About the Author

gsweet

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalfinding out LOC from a Binary File Pinmemberd_s_kumar19:51 30 Sep '04  
GeneralRe: finding out LOC from a Binary File PinmemberMr.Prakash5:11 25 Nov '04  
GeneralPlz delete this article, as this is a total ad. PinmemberSnyp11:26 17 Jan '04  
GeneralRe: Plz delete this article, as this is a total ad. PinmemberJerry Jeremiah19:20 30 Apr '06  
GeneralRe: Plz delete this article, as this is a total ad. PinsitebuilderTroy Marchand10:27 2 May '06  
GeneralRe: Plz delete this article, as this is a total ad. PinmemberJerry Jeremiah19:28 2 May '06  
QuestionWhere is the source code? PinmemberWREY22:26 15 Jan '04  
AnswerRe: Where is the source code? PinsussIan Prest2:16 16 Jan '04  
GeneralRe: Where is the source code? Pinmembergsweet4:14 16 Jan '04  
GeneralRe: Where is the source code? PinmemberDarren Schroeder2:43 21 Jan '04  
GeneralRe: Where is the source code? PinmemberJerry Jeremiah19:34 30 Apr '06  
GeneralRe: Where is the source code? Pinmembergsweet10:41 2 May '06  
GeneralRe: Where is the source code? PinmemberAndrew Phillips18:45 7 Apr '08  
GeneralThis is great! PinmemberDaniel Turini8:59 15 Jan '04  
GeneralRe: This is great! Pinmemberleppie9:34 15 Jan '04  
GeneralHell, THE editor ! PinmemberKochise20:29 15 Jan '04  
GeneralRe: Hell, THE editor ! Pinmembergsweet4:36 16 Jan '04  
GeneralRe: This is great! PinmemberAndrew Phillips18:39 7 Apr '08  
QuestionAdvantages? Pinmemberdog_spawn8:02 15 Jan '04  
AnswerRe: Advantages? Pinmembergsweet3:52 16 Jan '04  
GeneralRe: Advantages? Pinmemberdog_spawn4:02 16 Jan '04  
GeneralRe: Advantages? PinmemberAndrew Phillips18:33 7 Apr '08  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Jan 2004
Article Copyright 2004 by gsweet
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid