windows - c++ cli comparing hexadecimal bytes from a file not working -


i have file called ab.exe contains in hexadecimal

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bbaae8cafdffff83c408000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054aae8cafdffff83c40800000000000000000000000000000000000000000000000000000000000000000000000000aae8cafdffff83c4088d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

i have code in c++ suppose detect if string of hexadecimal in file or not , if add list box.

array<byte>^ target1 = { 0xaa,0xe8,0xca,0xfd,0xff,0xff,0x83,0xc4,0x08,0x8d }; array<byte>^ target2 = { 0x54,0xaa,0xe8,0xca,0xfd,0xff,0xff,0x83,0xc4,0x08 }; array<byte>^ target3 = { 0xbb,0xaa,0xe8,0xca,0xfd,0xff,0xff,0x83,0xc4,0x08 };   int matched1 = 0; int matched2 = 0; int matched3 = 0; filestream^ fs2 = gcnew filestream(line, filemode::open, fileaccess::read, fileshare::readwrite);  int value; {     value = fs2->readbyte();     if (value == target1[matched1]) {         matched1++;     }     else         matched1 = 0;      if (value == target2[matched2]) {         matched2++;     }     else         matched2 = 0;      if (value == target3[matched3]) {         matched3++;     }     else         matched3 = 0;      if(matched1 == target1->length)     {         listbox1->items->add(line + "1");      }      if(matched2 == target2->length)     {         listbox1->items->add(line + "2");      }      if(matched3 == target3->length)     {         listbox1->items->add(line + "3");      } } while (value != -1);  fs2->close(); 

the problem adds line + 3 list box , not line + 1 or line + 2 list box

i not know why because 3 of strings in file should added list box. reason last 1 being added because tried having 2 , second 1 got added.can show me why not being added list box. thanks

update1

after playing around more not last target gets added each time, first string appears in file gets added. stepped through program using message boxes , happening lets 54aae8cafdffff83c408 first string appear in file line + 2 added, reason matched integer 3 stop counting = 0 rest of file. can explain me why , how fix it.

update2

here answer problem. needed add matched = 0; after each add list box command.

listbox1->items->add(line + "1"); matched1 = 0;  listbox1->items->add(line + "2"); matched2 = 0;  listbox1->items->add(line + "3"); matched3 = 0; 

it seems me after first matching of 1 pattern (here target3) read beyond last byte of target3 (because of matched3++), may cause undesired behavior.

update1:

if(matched1 == target1->length) {   matched1 = 0; // pattern matched reset counter   ... } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -