How to bind a structure to every occurrence of a pattern

You can exchange the structure definitions in this forum.

Moderator: HHD Software Support Team

How to bind a structure to every occurrence of a pattern

Postby Barfy » Wed Feb 11, 2009 9:16 pm

Recently we have received a question: a customer wanted to bind a structure to each occurrence of a pattern in a document. He asked us for a solution.

And although Hex Editor Neo does not (yet) support macros, we were able to offer him a simple yet powerful solution.

Below is a sample structure definition file among with accompaning script file.

First, we create a structure.h file:

Code: Select all
structure.h:

#include "stddefs.h"

#pragma script("script.js")    // connect our script file here

struct StructureToBindAtSignatureLocation    // this is a structure to be bound on each occurrence of a signature
{
   char str[2];
};

public struct BindMeToStartOfTheFile
{
   [noindex] struct
   {
       var GapSize = LocateSignature(current_offset);
       if (GapSize == -1)
           $break_array(false);    // no more occurrences in a document, exit
hidden:
       char Reserved[GapSize];    // skip GapSize bytes
visible:
       StructureToBindAtSignatureLocation stb;
   } array[*];
};


And a script file:

Code: Select all
script.js:

function LocateSignature(starting_offset)
{
// Create an empty sequence object
   var sequence = document.CreateSequence();
   sequence.AddData(0,0x0d,0x0a);    // specify your signature here

// Create an empty selection object
   var selection = document.CreateEmptySelection();
   selection.AddRange(0, document.FileSize);

   var signature_offset;
   try
   {
       signature_offset = document.ToNumber(document.FindS(sequence, selection, starting_offset, false, false));
   } catch (e)
   {
       return -1;
   }

   return signature_offset - starting_offset;
}


Now, if you bind a BindMeToStartOfTheFile structure to the beginning of the document, you will get an array of bound StructureToBindAtSignatureLocation structures at correct locations!

The magic here lies in the LocateSignature script function, which is called by the Structure Viewer. The purpose of this function is to find the next occurrence of a pattern and return a size of a "gap" between the previous occurrence of a pattern (or the beginning of the file) and the next one.

This gap size is then used as an array argument to "skip" the gap, and then the StructureToBindAtSignatureLocation structure is instantiated.

Extending a structure definitions with a user created functions is a very powerful technique, as a scripting function has a full access to the underlying document as well as a Structure Viewer module itself. For example, it may call Structure Viewer to evaluate expressions, add late bound structures, print debug messages and so on.

Several usage examples are provided in the sample structure definition files, installed with Hex Editor Neo.
Barfy
Site Admin
 
Posts: 197
Joined: Thu Mar 09, 2006 2:07 pm
Location: Support Department

Postby pksublime » Wed Aug 12, 2009 2:35 pm

Thank you for the example.

However, I'm at a loss as to how to find my signature to bind to. The signature I'm trying to locate is two bytes: first byte is 0x01, second byte has the LSb set.
pksublime
 
Posts: 1
Joined: Wed Aug 12, 2009 2:31 pm

Postby Barfy » Fri Aug 14, 2009 9:19 am

pksublime wrote:However, I'm at a loss as to how to find my signature to bind to. The signature I'm trying to locate is two bytes: first byte is 0x01, second byte has the LSb set.


As the find function cannot find such a sequence, use it to locate the first byte (which is constant) and then manually check the next byte if its bit is set, using FileDocument.ReadS method and Javascript
Barfy
Site Admin
 
Posts: 197
Joined: Thu Mar 09, 2006 2:07 pm
Location: Support Department


Return to Structure Library

Who is online

Users browsing this forum: No registered users and 0 guests

cron