In <bfocbv$gepau$> Walter Dnes <> writes:
> When I asked in another thread about string comparisons, I forgot
>about the chr(0) booby-trap in C strings.
It's not a booby-trap, it's a string terminator
>Since I want to compare
>random binary data, this is important to me. Someone correct me if I'm
>wrong; strstr stops at chr(0).
All the functions whose names start with str stop at the end of the
string.
>memcmp doesn't treat chr(0) as a
>delimiter, and can compare ranges (the word "strings" is incorrect here)
>that included embedded chr(0).
memcmp (like all the other functions whose names start with mem)
operates on memory blocks defined by their start address and length.
>I realize that memcmp won't
>automatically scan a larger string, but I can put it in a loop to sweep
>through a larger string.
I don't get you. memcmp will scan a memory block as large as specified
in its third argument.
>Too bad that memmem is not standard.
It's trivial to implement, using memchr and memcmp:
#include <string.h>
void *mymemmem(const void *s1, size_t n1, const void *s2, size_t n2)
{
const unsigned char *p, *p1 = s1, *p2 = s2;
if (n1 == 0 || n2 == 0) return NULL;
while ((p = memchr(p1, *p2, n1)) != NULL) {
n1 -= p - p1;
p1 = p;
if (n1 < n2) break;
if (memcmp(p1, p2, n2) == 0) return (void *)p1;
p1++;
n1--;
if (n1 < n2) break;
}
return NULL;
}
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: