-
Notifications
You must be signed in to change notification settings - Fork 7
/
byteshift_memmem.c
227 lines (204 loc) · 7.88 KB
/
byteshift_memmem.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <limits.h>
#include <string.h>
#include "byteshift_memmem.h"
#include "make_big_endian.h"
void *byteshift_memmem(const void *haystack, size_t haystack_len,
const void *needle, size_t needle_len)
{
const unsigned char* needle_ptr;
const unsigned char* haystack_ptr;
int sums_diff;
size_t compare_len;
unsigned long last_needle_chars;
unsigned long last_haystack_chars;
unsigned int i;
switch (needle_len) {
case (0):
/* empty needle */
return (void *) haystack;
break;
case (1):
/* special case for single-character needles */
return memchr(haystack, *((unsigned char*) needle), haystack_len);
break;
}
/* start searching through haystack only from the first occurence of the
first character of needle */
haystack_ptr = (const unsigned char *) memchr(haystack, *((const unsigned char*) needle), haystack_len);
if (!haystack_ptr) {
/* the first character of needle isn't in haystack */
return NULL;
}
haystack_len -= (haystack_ptr - (const unsigned char*)haystack);
if (haystack_len < needle_len) {
/* the remaining haystack is smaller than needle */
return NULL;
}
haystack = (void *)haystack_ptr;
if (needle_len > LONG_INT_N_BYTES + 1)
{
needle_ptr = (const unsigned char *)needle;
sums_diff = 0;
#ifndef MAKE_ULONG_BIGENDIAN
for (i = needle_len - LONG_INT_N_BYTES; i > 0; --i) {
sums_diff -= *needle_ptr++;
sums_diff += *haystack_ptr++;
}
last_needle_chars = 0;
last_haystack_chars = 0;
for (i = LONG_INT_N_BYTES; i > 0; --i) {
last_needle_chars <<= 8;
last_needle_chars ^= *needle_ptr;
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr;
sums_diff -= *needle_ptr++;
sums_diff += *haystack_ptr++;
}
#else
for (i = needle_len; i > 0; --i) {
sums_diff -= *needle_ptr++;
sums_diff += *haystack_ptr++;
}
last_needle_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)needle_ptr) - 1));
last_haystack_chars = MAKE_ULONG_BIGENDIAN(*(((unsigned long *)haystack_ptr) - 1));
#endif /* MAKE_ULONG_BIGENDIAN */
/* we will call memcmp() only once we know that the sums are equal and
that LONG_INT_N_BYTES last chars are equal, so it will be enough to
compare all but the last LONG_INT_N_BYTES + 1 characters */
compare_len = needle_len - (LONG_INT_N_BYTES + 1);
/* At this point:
* needle is at least two characters long
* haystack is at least needle_len characters long (also at least two)
* the first characters of needle and haystack are identical
*/
if ( sums_diff == 0
&& last_haystack_chars == last_needle_chars
&& memcmp(haystack, needle, compare_len) == 0)
{
return (void *) haystack;
}
/* iterate through the remainder of haystack, updating the sums' difference
and checking for identity whenever the difference is zero */
for (i = haystack_len - needle_len; i > 0; --i)
{
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr;
sums_diff -= *(const unsigned char*)haystack++;
sums_diff += *haystack_ptr++;
/* if sums_diff == 0, we know that the sums are equal, so it is enough
to compare all but the last characters */
if ( sums_diff == 0
&& last_haystack_chars == last_needle_chars
&& memcmp(haystack, needle, compare_len) == 0)
{
return (void *) haystack;
}
}
}
else if (needle_len < LONG_INT_N_BYTES)
{
needle_ptr = (const unsigned char *)needle;
sums_diff = 0;
for (i = needle_len; i > 0; --i) {
sums_diff -= *needle_ptr++;
sums_diff += *haystack_ptr++;
}
/* we will call memcmp() only once we know that the sums are equal, so
it will be enough to compare all but the last characters */
compare_len = needle_len - 1;
/* At this point:
* needle is at least two characters long
* haystack is at least needle_len characters long (also at least two)
* the first characters of needle and haystack are identical
*/
if ( sums_diff == 0
&& memcmp(haystack, needle, compare_len) == 0)
{
return (void *) haystack;
}
/* iterate through the remainder of haystack, updating the sums' difference
and checking for identity whenever the difference is zero */
for (i = haystack_len - needle_len; i > 0; --i)
{
sums_diff -= *(const unsigned char *)haystack++;
sums_diff += *haystack_ptr++;
/* if sums_diff == 0, we know that the sums are equal, so it is enough
to compare all but the last characters */
if ( sums_diff == 0
&& memcmp(haystack, needle, compare_len) == 0)
{
return (void *) haystack;
}
}
}
else if (needle_len == LONG_INT_N_BYTES)
{
#ifndef MAKE_ULONG_BIGENDIAN
needle_ptr = (const unsigned char *)needle;
last_needle_chars = 0;
last_haystack_chars = 0;
for (i = needle_len; i > 0; --i) {
last_needle_chars <<= 8;
last_needle_chars ^= *needle_ptr++;
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr++;
}
#else
last_needle_chars = MAKE_ULONG_BIGENDIAN(*(unsigned long *)needle);
last_haystack_chars = MAKE_ULONG_BIGENDIAN(*(unsigned long *)haystack);
haystack_ptr += LONG_INT_N_BYTES;
#endif /* MAKE_ULONG_BIGENDIAN */
if (last_haystack_chars == last_needle_chars)
{
return (void *) haystack;
}
/* iterate through the remainder of haystack, updating the last char
data and checking for equality */
for (i = haystack_len - needle_len; i > 0; --i)
{
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr++;
if (last_haystack_chars == last_needle_chars)
{
return (void *) (haystack_ptr - needle_len);
}
}
}
else /* needle_len == LONG_INT_N_BYTES + 1 */
{
#ifndef MAKE_ULONG_BIGENDIAN
needle_ptr = (const unsigned char *)needle;
last_needle_chars = 0;
last_haystack_chars = 0;
for (i = LONG_INT_N_BYTES; i > 0; --i) {
last_needle_chars <<= 8;
last_needle_chars ^= *needle_ptr++;
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr++;
}
#else
last_needle_chars = MAKE_ULONG_BIGENDIAN(*(unsigned long *)needle);
last_haystack_chars = MAKE_ULONG_BIGENDIAN(*(unsigned long *)haystack);
haystack_ptr += LONG_INT_N_BYTES;
#endif /* MAKE_ULONG_BIGENDIAN */
unsigned char last_needle_char = *(((const unsigned char *)needle) + LONG_INT_N_BYTES);
if ( last_haystack_chars == last_needle_chars
&& *haystack_ptr == last_needle_char)
{
return (void *) haystack;
}
/* iterate through the remainder of haystack, updating the last char
data and checking for equality */
for (i = haystack_len - needle_len; i > 0; --i)
{
last_haystack_chars <<= 8;
last_haystack_chars ^= *haystack_ptr++;
if ( last_haystack_chars == last_needle_chars
&& *haystack_ptr == last_needle_char)
{
return (void *) (haystack_ptr - (needle_len - 1));
}
}
}
return NULL;
}