From: Michelle on
Peter,

Thank you for the explanation. It's more difficult then I thought.
I thought that searching through binary data a common issue was (you can do
this with every hex editor) and that there were plenty of examples
available.
For me there is only a single pattern to match, I don't need to search for
multiple patterns in a single pass.
Speed is not the main issue (although it's important), but finding
everything is the main issue.

To try your example, I created a console application (using Visual Studio
2008)
Maybe it's a little bit silly, but how can I add my pattern (hex value) as
an argument to FindByteString -> byte[] rgbPattern?

> int FindByteString(string strInputFile, byte[] rgbPattern)

Get an error on this and don't know how to solve it:
> if (++ibOffset < cbBlockCur.Length)
>

'int' does not contain a definition for 'Length' and no extension method
'Length' accepting a first argument of type 'int' could be found (are you
missing a using directive or an assembly reference?)

As mentioned before, I'm a rookie using C#, so please be patient.
But I'm willing to learn.

Regards,

Michelle





From: Peter Duniho on
On Fri, 11 Sep 2009 23:21:24 -0700, Michelle <michelle(a)notvalid.nomail>
wrote:

> Peter,
>
> Thank you for the explanation. It's more difficult then I thought.
> I thought that searching through binary data a common issue was (you can
> do
> this with every hex editor) and that there were plenty of examples
> available.

I don't know about the relative quantity of examples one way or the
other. As far as it being a common feature in hex editors, well...a) not
all hex editors are created equal (any editor that requires the entire
file data to be loaded into memory at once, which is a lot of them, can
trivially search the entire file contents without worrying about the
block-level i/o), and b) for someone with enough programming experience to
be able to write a good hex editor, this really isn't a very difficult
problem.

> [...]
> To try your example, I created a console application (using Visual
> Studio 2008)
> Maybe it's a little bit silly, but how can I add my pattern (hex value)
> as
> an argument to FindByteString -> byte[] rgbPattern?

The same way you'd initialize any array. For example:

byte[] rgbPattern = { 0xff, 0x56, 0x13, 0x1a };

If you want to provide a text string as initialization, that's possible as
well. You'll just have to parse each pair of hex digits in the string as
a byte and add them to an appropriate array. For example:

string strPattern = "ff56131a";
byte[] rgbPattern = RgbFromString(strPattern);

where:

byte[] RgbFromString(string strPattern)
{
byte[] rgbRet = new byte[strPattern.Length / 2];

for (int ib = 0; ib < rgbRet.Length; ib++)
{
rgbRet[ib] = byte.Parse(strPattern.Substring(ib * 2, 2),
NumberStyles.HexNumber);
}

return rgbRet;
}

>
>> int FindByteString(string strInputFile, byte[] rgbPattern)
>
> Get an error on this and don't know how to solve it:
>> if (++ibOffset < cbBlockCur.Length)
>>
>
> 'int' does not contain a definition for 'Length' and no extension method
> 'Length' accepting a first argument of type 'int' could be found (are you
> missing a using directive or an assembly reference?)
>
> As mentioned before, I'm a rookie using C#, so please be patient.
> But I'm willing to learn.

For what it's worth, it's my opinion that one of the best ways to learn
programming is to read other people's code and figure out how it works.
And of course, if you do that, in the process you should be able to figure
out how to fix any parts of the code that might not be exactly correct.

As far as that specific error goes, the purpose of that line of code is
simply to continue the loop if there are still unprocessed bytes left in
the current block (all of the rest of the code in the loop, after that
"if" statement and its block, handles updating the in-memory blocks
holding the data being processed to the next block-size part of the
file). Knowing that, it should be clear that the correct statement is:

if (++ibOffset < cbBlockCur)

That is, just remove the ".Length"...it's superfluous and left over from a
variation of the code I had before I changed it and posted my message.

Hope that helps.

Pete
From: Michelle on
Rossum,

I spend some time reading about the mentioned algorithms.
The "Boyer-Moore Algorithm" seems to be the most popular and fastest. (Also
used in several hex editors).
Unfortunately (so far) I couldn't find a c# example for searching binary
files.
Because I've not much experience using C#, it's difficult to translate the
'theory' to a working example.
So if you know one, I appreciate a link to it.
I'd like to read other people's code and figure out how it works to learn
about. (It's for education only :-)) )

Regards,

Michelle


From: Michelle on
Peter,

It's a shame, but i can't get it to work.
I'm stuck with this error: "An object reference is required for the
non-static field, method, or property
'binSearch.Program.FindByteString(string, byte[])'"
I tried several solutions found, but no luck so far.

namespace binSearch
{
class Program
{
static void Main(string[] args)
{
byte[] rgbPattern = { 0xFF, 0x56, 0x13, 0x1A, 0x1B, 0x08, 0x7B,
0x15, 0x61, 0x08, 0x00, 0x15, 0x1E };
FindByteString(@"D:\BinarySearch\myfile.bin");
}
int FindByteString(string strInputFile, byte[] rgbPattern)
{
byte[] rgbBlockCur = new byte[4096],
[. . .]

Michelle


From: Tom Spink on
Michelle wrote:

> Peter,
>
> It's a shame, but i can't get it to work.
> I'm stuck with this error: "An object reference is required for the
> non-static field, method, or property
> 'binSearch.Program.FindByteString(string, byte[])'"
> I tried several solutions found, but no luck so far.
>
> namespace binSearch
> {
> class Program
> {
> static void Main(string[] args)
> {
> byte[] rgbPattern = { 0xFF, 0x56, 0x13, 0x1A, 0x1B, 0x08, 0x7B,
> 0x15, 0x61, 0x08, 0x00, 0x15, 0x1E };
> FindByteString(@"D:\BinarySearch\myfile.bin");
> }
> int FindByteString(string strInputFile, byte[] rgbPattern)
> {
> byte[] rgbBlockCur = new byte[4096],
> [. . .]
>
> Michelle

You need to qualify the method definition with the static keyword:

///
static int FindByteString(string strInputFile, byte[] rgbPattern)
///

Put the keyword 'static' before the 'int', and it should remove that
error. It's probably best if you know why... so I suggest a bit of
research on the 'static' keyword and what static members are.

--
Tom