-
Notifications
You must be signed in to change notification settings - Fork 0
/
memhunter.h
77 lines (56 loc) · 1.94 KB
/
memhunter.h
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef MEMHUNTER_H
#define MEMHUNTER_H
#include <cstring>
#include <cstdio>
#include <cstdlib>
class MemHunter {
// TODO(Janitha): This NEEEEDDSSS some serious testing!!!
char *needle;
size_t needlelen;
char *lookback;
size_t lookbacklen;
public:
MemHunter(const char *needlebuf, size_t len) {
needlelen = len;
needle = new char[needlelen];
memcpy(needle, needlebuf, needlelen);
lookback = new char[needlelen*2];
lookbacklen = 0;
}
inline char* findend(char *haystack, size_t haylen) {
size_t cpylen = needlelen*2 - lookbacklen;
cpylen = (cpylen < haylen) ? cpylen : haylen;
memcpy(lookback+lookbacklen, haystack, cpylen);
char *found;
// Find it in lookback
found = (char*)memmem(lookback, lookbacklen+cpylen, needle, needlelen);
if(found) {
return haystack + (found - lookback) - lookbacklen + needlelen;
}
// Find it in the haystack
found = (char*)memmem(haystack, haylen, needle, needlelen);
if(found) {
return found + needlelen;
}
// Needle wasn't found, let's buffer up and prep for next time
if(lookbacklen + cpylen <= needlelen) {
// Lookback is still growing, let it be
lookbacklen += cpylen;
} else if(lookbacklen + cpylen < needlelen*2) {
// The haylen must have been short, shift the lookback until it's needlelen
size_t shift = lookbacklen + cpylen - needlelen;
memcpy(lookback, lookback + shift, needlelen);
lookbacklen = needlelen;
} else {
// Haystack was bigger than needle, so just save off the tail of that
memcpy(lookback, haystack + haylen - needlelen, needlelen);
lookbacklen = needlelen;
}
return nullptr;
}
~MemHunter() {
delete needle;
delete lookback;
}
};
#endif