Since the strstr
function won't work here for an arbitrary binary data (it is working only for strings with \0
. termination), I can see three approaches here:
1) Naive approach: iterate over one array of bytes, and use memcmp
with the other array starting at different positions each time. Easy, but consumes O(k*n)
time (k, n - sizes of the data).
2) Using the KMP algorithm. Requires some work on understanding and coding, but giving the best time complexity O(k+n)
.
3) If the performance is not important, and you don't want to mess with ANY somewhat non-trivial algorithms:
-- Convert your binary datas to strings, representing each byte with it's two digits HEX value.
-- Use strstr
.
Update: After a little thinking about the third approach, there might be a case when it won't work right. Consider that you want to find the data represented by AA AA
inside 1A AA A1
. It shouldn't be found, since it is not there. But, if you represent the data as concatenated characters without delimiters, it will be like find AAAA
in 1AAAA1
, which will succeed. So adding some delimiter would be a good idea here.