1 /*-
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2005-2014 Rich Felker, et al.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include <sys/cdefs.h>
26 #include <stdint.h>
27 #include <string.h>
28
29 static char *
twobyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)30 twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
31 {
32 uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
33 for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
34 if (hw == nw)
35 return (char *)h - 2;
36 return hw == nw ? (char *)h - 2 : 0;
37 }
38
39 static char *
threebyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)40 threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
41 {
42 uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
43 uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
44 for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8)
45 if (hw == nw)
46 return (char *)h - 3;
47 return hw == nw ? (char *)h - 3 : 0;
48 }
49
50 static char *
fourbyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)51 fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
52 {
53 uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
54 uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
55 for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
56 if (hw == nw)
57 return (char *)h - 4;
58 return hw == nw ? (char *)h - 4 : 0;
59 }
60
61 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63
64 #define BITOP(a, b, op) \
65 ((a)[(size_t)(b) / (8 * sizeof *(a))] op \
66 (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
67
68 /*
69 * Two Way string search algorithm, with a bad shift table applied to the last
70 * byte of the window. A bit array marks which entries in the shift table are
71 * initialized to avoid fully initializing a 1kb/2kb table.
72 *
73 * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
74 * Journal of the ACM 38(3):651-675
75 */
76 static char *
twoway_memmem(const unsigned char * h,const unsigned char * z,const unsigned char * n,size_t l)77 twoway_memmem(const unsigned char *h, const unsigned char *z,
78 const unsigned char *n, size_t l)
79 {
80 size_t i, ip, jp, k, p, ms, p0, mem, mem0;
81 size_t byteset[32 / sizeof(size_t)] = { 0 };
82 size_t shift[256];
83
84 /* Computing length of needle and fill shift table */
85 for (i = 0; i < l; i++)
86 BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
87
88 /* Compute maximal suffix */
89 ip = -1;
90 jp = 0;
91 k = p = 1;
92 while (jp + k < l) {
93 if (n[ip + k] == n[jp + k]) {
94 if (k == p) {
95 jp += p;
96 k = 1;
97 } else
98 k++;
99 } else if (n[ip + k] > n[jp + k]) {
100 jp += k;
101 k = 1;
102 p = jp - ip;
103 } else {
104 ip = jp++;
105 k = p = 1;
106 }
107 }
108 ms = ip;
109 p0 = p;
110
111 /* And with the opposite comparison */
112 ip = -1;
113 jp = 0;
114 k = p = 1;
115 while (jp + k < l) {
116 if (n[ip + k] == n[jp + k]) {
117 if (k == p) {
118 jp += p;
119 k = 1;
120 } else
121 k++;
122 } else if (n[ip + k] < n[jp + k]) {
123 jp += k;
124 k = 1;
125 p = jp - ip;
126 } else {
127 ip = jp++;
128 k = p = 1;
129 }
130 }
131 if (ip + 1 > ms + 1)
132 ms = ip;
133 else
134 p = p0;
135
136 /* Periodic needle? */
137 if (memcmp(n, n + p, ms + 1)) {
138 mem0 = 0;
139 p = MAX(ms, l - ms - 1) + 1;
140 } else
141 mem0 = l - p;
142 mem = 0;
143
144 /* Search loop */
145 for (;;) {
146 /* If remainder of haystack is shorter than needle, done */
147 if (z - h < l)
148 return 0;
149
150 /* Check last byte first; advance by shift on mismatch */
151 if (BITOP(byteset, h[l - 1], &)) {
152 k = l - shift[h[l - 1]];
153 if (k) {
154 if (k < mem)
155 k = mem;
156 h += k;
157 mem = 0;
158 continue;
159 }
160 } else {
161 h += l;
162 mem = 0;
163 continue;
164 }
165
166 /* Compare right half */
167 for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
168 ;
169 if (k < l) {
170 h += k - ms;
171 mem = 0;
172 continue;
173 }
174 /* Compare left half */
175 for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
176 ;
177 if (k <= mem)
178 return (char *)h;
179 h += p;
180 mem = mem0;
181 }
182 }
183
184 void *
memmem(const void * h0,size_t k,const void * n0,size_t l)185 memmem(const void *h0, size_t k, const void *n0, size_t l)
186 {
187 const unsigned char *h = h0, *n = n0;
188
189 /* Return immediately on empty needle */
190 if (!l)
191 return (void *)h;
192
193 /* Return immediately when needle is longer than haystack */
194 if (k < l)
195 return 0;
196
197 /* Use faster algorithms for short needles */
198 h = memchr(h0, *n, k);
199 if (!h || l == 1)
200 return (void *)h;
201 k -= h - (const unsigned char *)h0;
202 if (k < l)
203 return 0;
204 if (l == 2)
205 return twobyte_memmem(h, k, n);
206 if (l == 3)
207 return threebyte_memmem(h, k, n);
208 if (l == 4)
209 return fourbyte_memmem(h, k, n);
210
211 return twoway_memmem(h, h + k, n, l);
212 }
213