As was mentioned before, strstr is the closest thing, but it is limited in that NULL characters terminate the buffers. So I have used this code:
#include <string.h>
char * _memmem(const char *mem, size_t mem_size, const char *sub, size_t sub_size)
{
// returns a pointer of the first occurance of a sub-buffer in a memory buffer
// returns NULL if the sub-buffer was not found
// is not limited by embedded NULLs in either the memory buffer or the sub-buffer
//
// Parameters:
// mem - The memory buffer to scan
// mem_size - The size, in bytes, of the memory buffer
// sub - The sub buffer to find
// sub_size - The size, in bytes, of the sub buffer
char \*ret = NULL;
char \*ptr = const\_cast<char \*>(mem);
while (ptr && !ret)
{
ptr = reinterpret\_cast<char \*>(memchr(ptr, \*sub, mem\_size - (sub\_size - 1) - (int)(ptr - mem)));
if (ptr)
{
if (!memcmp(ptr, sub, sub\_size))
ret = ptr;
++ptr;
}
}
return ret;
}
[edit] cleaned up the code a little [/edit]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it! -- modified at 18:12 Monday 21st November, 2005