Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Searching a binary file for an hex pattern

Posted on 2006-11-26
3
616 Views
Last Modified: 2010-04-05
Greetings,

One of the applications I'm currently trying to integrate with uses a thing called Btrieve, for which almost no support is left. I decided then to read the (if we may call it so) database BTR files into a memory stream and perform a manual search for what I need (there's an index-like structure inside and I have all the info I need to start with).

My approach is to read this binary file into a TMemoryStream (no problem here) and perform a search into it using StrPos. What happens is that if my search string has a #0, StrPos always returns nil... Any hints how to get this done ?!?! Should I search by chars or bytes ? Is this approach the best one ? Thank you for any help on this ! Using D7.

type
  TFormMain = class(TForm)
...
  private
      { Private declarations }
      Ms1, Ms2: TMemoryStream;
...
end;
...

const
  search_arg: const = #0#0#0#128#0#5;

procedure TFormMain.Ms1LoadFromFileClick(Sender: TObject);
begin
  try
    Ms1.LoadFromFile(my_file[0].name);
    HowToReturnOffsetFromStreamHere(Ms1, search_arg);  <---  ? ? ?
  except
    Showmessage('Couldn''t read the file !' + #13 + SysErrorMessage(GetLastError));
  end;
end;
0
Comment
Question by:FK4
3 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 18015977
Hi,
StrPos is for handling null terminated strings.
So the first occurabce of #0 terminates the string and the rest is ignored.
To allow searching for #0 use Pos() function.
0
 
LVL 4

Accepted Solution

by:
Meldrachaun earned 300 total points
ID: 18017955
There's a SearchBuf function in StrUtils that does what you want:


procedure TForm1.Button1Click(Sender: TObject);
const
  DATA = 'testing' + #0 + 'one' + #0 + 'two' + #0 + 'three' + #0;

var
  stream: TStringStream;
  ms: TMemoryStream;
  p1, p2: pointer;
  x, size, gap: integer;
  locations: string;

begin
  stream := TStringStream.Create(DATA);
  ms := TMemoryStream.Create;
  try
    ms.LoadFromStream(stream);
    p1 := ms.Memory;
    locations := '';
    size := ms.Size;
    repeat
      p2 := Pointer(SearchBuf(PChar(p1), size,0,0,#0, [soDown]));
      if p2 <> nil then
      begin
        gap := integer(p2) - integer(p1) + 1;
        dec(Size, gap);
        p1 := pointer(integer(p2)+1);

        x := integer(p2) - integer(ms.Memory);
        locations := locations + IntToStr(x)+ '  ';
      end;
    until p2 = nil;
  finally
    stream.free;
    ms.free;
  end;
  ShowMessage(locations);
end;
0
 

Author Comment

by:FK4
ID: 18021813
Meldrachaun, your base sample is 98% correct but fit me 100%.

The ms in yor sample does not actually load from 'stream', but from my source file, and the argument SearchString in SearchBuf is actually your DATA constant, not #0... but again, solved my problem.

Thanks,

Fernando
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question