FK4
asked on
Searching a binary file for an hex pattern
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.Ms1LoadFromFileC lick(Sende r: TObject);
begin
try
Ms1.LoadFromFile(my_file[0 ].name);
HowToReturnOffsetFromStrea mHere(Ms1, search_arg); <--- ? ? ?
except
Showmessage('Couldn''t read the file !' + #13 + SysErrorMessage(GetLastErr or));
end;
end;
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.Ms1LoadFromFileC
begin
try
Ms1.LoadFromFile(my_file[0
HowToReturnOffsetFromStrea
except
Showmessage('Couldn''t read the file !' + #13 + SysErrorMessage(GetLastErr
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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.