blob: 37432bf2e9ae3bb4dfb28a999084656bf341995a (
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
|
#include "compat.h"
#ifdef NO_STRDUP
#include <stdlib.h>
#include <string.h>
char *strdup(const char *s) {
char *s2 = malloc(strlen(s) + 1);
if (!s2)
return NULL;
strcpy(s2, s);
return s2;
}
#endif // NO_STRDUP
#ifdef NO_MEMMEM
void *memmem(const void *h, size_t h_size, const void *n, size_t n_size) {
if (n_size == 0)
return (void *)h;
if (n_size > h_size)
return NULL;
const unsigned char *p = h;
const unsigned char *end = p + h_size - n_size + 1;
unsigned char c = *(unsigned char *)n;
while ((p = memchr(p, c, end - p))) {
if (memcmp(p, n, n_size) == 0)
return (void *)p;
p++;
}
return NULL;
}
#endif // NO_MEMMEM
|