Solved

Searching a binary file for an hex pattern

Posted on 2006-11-26
3
596 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 263
Delphi OLE Error 8 93
delphi parse string to params 3 97
Delphi: Connect to running MS Outlook 4 48
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now