Reputation: 823
I cannot use strstr, memchr because array can contains any number of \0 chars, is there are any efficient ways to do that? I have to find all positions (indexes) or pointers.
Upvotes: 0
Views: 3222
Reputation: 585
Works normally for me
bool BinScanner::FindSequence(const unsigned char * pSeq, unsigned long ulSeqSize)
{
const unsigned char* pFindRes = std::search(pCurr, pEnd, pSeq, pSeq+ulSeqSize);
if (pFindRes != pEnd)
{
return true;
}
}
Upvotes: 0
Reputation: 477070
Piece of cake in C++:
#include <string>
const std::string needle = get_byte_sequence();
const std::string haystack = get_data();
std::size_t pos = haystack.find(needle);
// found if pos != std::string::npos
Another option is to use a generic algorithm:
#include <algorithm>
std::string::const_iterator it = std::search(data.begin(), data.end(), needle.begin(), needle.end());
if (it != data.end())
{
// needle found at position std::distance(data.begin(), it);
}
else
{
// needle not found
}
Bear in mind that C++ string
objects can contain arbitrary data. To create a string from a byte buffer, pass its size to the constructor:
char buf[200]; // fill with your data; possibly full of zeros!
std::string s(buf, 200); // no problem
Upvotes: 6
Reputation: 968
If you expect to encounter null-characters, use the standard function memcmp(const void *, const void *, size_t).
Upvotes: 0
Reputation: 143099
Perhaps, you should look at std::search
? (there is also memem
, but I don't think it's extremely portable).
Upvotes: 1