xref: /freebsd/lib/libc/string/memmem.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 <stdint.h>
26 #include <string.h>
27 
28 static char *
29 twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
30 {
31 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
32 	for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
33 		if (hw == nw)
34 			return (char *)h - 2;
35 	return hw == nw ? (char *)h - 2 : 0;
36 }
37 
38 static char *
39 threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
40 {
41 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
42 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
43 	for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8)
44 		if (hw == nw)
45 			return (char *)h - 3;
46 	return hw == nw ? (char *)h - 3 : 0;
47 }
48 
49 static char *
50 fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
51 {
52 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
53 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
54 	for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
55 		if (hw == nw)
56 			return (char *)h - 4;
57 	return hw == nw ? (char *)h - 4 : 0;
58 }
59 
60 #define MAX(a, b) ((a) > (b) ? (a) : (b))
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
62 
63 #define BITOP(a, b, op) \
64 	((a)[(size_t)(b) / (8 * sizeof *(a))] op \
65 	    (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
66 
67 /*
68  * Two Way string search algorithm, with a bad shift table applied to the last
69  * byte of the window. A bit array marks which entries in the shift table are
70  * initialized to avoid fully initializing a 1kb/2kb table.
71  *
72  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
73  * Journal of the ACM 38(3):651-675
74  */
75 static char *
76 twoway_memmem(const unsigned char *h, const unsigned char *z,
77     const unsigned char *n, size_t l)
78 {
79 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
80 	size_t byteset[32 / sizeof(size_t)] = { 0 };
81 	size_t shift[256];
82 
83 	/* Computing length of needle and fill shift table */
84 	for (i = 0; i < l; i++)
85 		BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
86 
87 	/* Compute maximal suffix */
88 	ip = -1;
89 	jp = 0;
90 	k = p = 1;
91 	while (jp + k < l) {
92 		if (n[ip + k] == n[jp + k]) {
93 			if (k == p) {
94 				jp += p;
95 				k = 1;
96 			} else
97 				k++;
98 		} else if (n[ip + k] > n[jp + k]) {
99 			jp += k;
100 			k = 1;
101 			p = jp - ip;
102 		} else {
103 			ip = jp++;
104 			k = p = 1;
105 		}
106 	}
107 	ms = ip;
108 	p0 = p;
109 
110 	/* And with the opposite comparison */
111 	ip = -1;
112 	jp = 0;
113 	k = p = 1;
114 	while (jp + k < l) {
115 		if (n[ip + k] == n[jp + k]) {
116 			if (k == p) {
117 				jp += p;
118 				k = 1;
119 			} else
120 				k++;
121 		} else if (n[ip + k] < n[jp + k]) {
122 			jp += k;
123 			k = 1;
124 			p = jp - ip;
125 		} else {
126 			ip = jp++;
127 			k = p = 1;
128 		}
129 	}
130 	if (ip + 1 > ms + 1)
131 		ms = ip;
132 	else
133 		p = p0;
134 
135 	/* Periodic needle? */
136 	if (memcmp(n, n + p, ms + 1)) {
137 		mem0 = 0;
138 		p = MAX(ms, l - ms - 1) + 1;
139 	} else
140 		mem0 = l - p;
141 	mem = 0;
142 
143 	/* Search loop */
144 	for (;;) {
145 		/* If remainder of haystack is shorter than needle, done */
146 		if (z - h < l)
147 			return 0;
148 
149 		/* Check last byte first; advance by shift on mismatch */
150 		if (BITOP(byteset, h[l - 1], &)) {
151 			k = l - shift[h[l - 1]];
152 			if (k) {
153 				if (k < mem)
154 					k = mem;
155 				h += k;
156 				mem = 0;
157 				continue;
158 			}
159 		} else {
160 			h += l;
161 			mem = 0;
162 			continue;
163 		}
164 
165 		/* Compare right half */
166 		for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
167 			;
168 		if (k < l) {
169 			h += k - ms;
170 			mem = 0;
171 			continue;
172 		}
173 		/* Compare left half */
174 		for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
175 			;
176 		if (k <= mem)
177 			return (char *)h;
178 		h += p;
179 		mem = mem0;
180 	}
181 }
182 
183 void *
184 memmem(const void *h0, size_t k, const void *n0, size_t l)
185 {
186 	const unsigned char *h = h0, *n = n0;
187 
188 	/* Return immediately on empty needle */
189 	if (!l)
190 		return (void *)h;
191 
192 	/* Return immediately when needle is longer than haystack */
193 	if (k < l)
194 		return 0;
195 
196 	/* Use faster algorithms for short needles */
197 	h = memchr(h0, *n, k);
198 	if (!h || l == 1)
199 		return (void *)h;
200 	k -= h - (const unsigned char *)h0;
201 	if (k < l)
202 		return 0;
203 	if (l == 2)
204 		return twobyte_memmem(h, k, n);
205 	if (l == 3)
206 		return threebyte_memmem(h, k, n);
207 	if (l == 4)
208 		return fourbyte_memmem(h, k, n);
209 
210 	return twoway_memmem(h, h + k, n, l);
211 }
212