Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search in a BYTE array for a pattern?

I have a Byte array :

BYTE Buffer[20000]; this array contains the following data:

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C4C2050323231300A2020000000FD00384B1E5310000A20202020202000FA

My question is how can i search this array for a pattern like "000000FC"? I don't really think it is important, but i need the index where i can find my pattern too. Could someone provide an example for this, because i don't really understand this :(

like image 970
kampi Avatar asked Sep 06 '11 11:09

kampi


1 Answers

It is possible to use raw pointers with STL std::search().

For example

#include <algorithm>

BYTE Buffer[20000] = { 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFC };
PBYTE pBufferLast = Buffer + sizeof(Buffer);
BYTE Pattern[] = { 0x00, 0x00, 0x00, 0xFC };
PBYTE pPatternLast = Pattern + sizeof(Pattern);

PBYTE pOccurrence = std::search(Buffer, pBufferLast, Pattern, pPatternLast);

BOOL fFound = (pOccurrence != pBufferLast);

Since C++17, std::search() can use Boyer-Moore search (boyer_moore_searcher), etc.

like image 109
beyeriii Avatar answered Oct 10 '22 07:10

beyeriii