-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
36 lines (28 loc) · 810 Bytes
/
utils.c
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
35
36
#include <linux/printk.h>
#include <linux/string.h>
#include "utils.h"
void *memmem(void *haystack, size_t haystack_len, void *needle, size_t needle_len)
{
if (haystack_len < needle_len) {
eprintln("haystack length is smaller than needle length");
return NULL;
}
void *addr = haystack;
int scan_length = haystack_len;
do {
addr = memchr(addr, *(char *)needle, scan_length);
if (!addr) {
break;
}
scan_length = haystack_len - (addr - haystack);
if (scan_length < needle_len) {
break;
}
if (0 == memcmp(addr + 1, needle + 1, needle_len - 1)) {
return addr;
}
addr += needle_len;
scan_length -= needle_len;
} while (scan_length);
return NULL;
}