summaryrefslogtreecommitdiff
path: root/Build/source/utils/t1utils/t1utils-src/memmem.c
blob: a724dc6f51706737de6410c33e02363103bae841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Some operating systems might not have memmem. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stddef.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif

#define memmem my_memmem

void*
memmem(const void* haystack, size_t haystack_len,
       const void* needle, size_t needle_len)
{
    const unsigned char* s = (const unsigned char*) haystack;
    const unsigned char* ends = s + haystack_len - needle_len;
    const unsigned char* p = (const unsigned char*) needle;
    void* try;
    if (needle_len == 0)
        return (void*) haystack;
    while (s <= ends && (try = memchr(s, *p, ends - s + 1)))
        if (memcmp(try, p, needle_len) == 0)
            return (void*) try;
        else
            s = (const unsigned char*) try + 1;
    return 0;
}

#ifdef __cplusplus
}
#endif