0

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.

7
  • What did you try? There are some obvious solutions. Why don't they fit you? Commented Nov 24, 2011 at 13:15
  • 1
    It's not at all clear what you are looking for here. Every index into a byte array is the start of a set of sub-arrays, varying in length from 1 to (outer array length) - index. Commented Nov 24, 2011 at 13:15
  • Why you can't use memchr? There you give size of byte array so \0 bytes isn't any problem. Commented Nov 24, 2011 at 13:16
  • Oh, I think I've just understood. You want to find the offset of a particular pattern of data bytes within a larger array? Commented Nov 24, 2011 at 13:16
  • For C, if you're on a GNU system, try memmem. Commented Nov 24, 2011 at 13:19

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

7 Comments

I'd vote against the find, because it's not of the string nature it seems.
@MichaelKrelin-hacker: You think in such three-dimensional terms :-) Think of "string" as "string of bytes"; problem solved. Also, always think of "char" as "byte"; "char" is arguably the most inappropriately named data type...
I can think of char as byte, but I can't help thinking of char* as 0-terminated string ;-) But well, it all comes down to 3d terms - I used the word "nature" :) (and I used the words "vote against", though I didn't mean downvoting half of your answer:))
+1, except that char buf[200]; is not full of zeroes (add initializer please).
@avakar: Sorry, misunderstanding. I mean: "This is your data, possibly this is full of zeros."
|
1

Perhaps, you should look at std::search? (there is also memem, but I don't think it's extremely portable).

Comments

0

If you expect to encounter null-characters, use the standard function memcmp(const void *, const void *, size_t).

Comments

0

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.