xref: /freebsd/lib/libc/string/strstr.c (revision 6fd84a627fa066a98777c7ab1e65bdd885407516)
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 __FBSDID("$FreeBSD$");
27 
28 #include <stdint.h>
29 #include <string.h>
30 
31 static char *
32 twobyte_strstr(const unsigned char *h, const unsigned char *n)
33 {
34 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
35 	for (h++; *h && hw != nw; hw = hw << 8 | *++h)
36 		;
37 	return *h ? (char *)h - 1 : 0;
38 }
39 
40 static char *
41 threebyte_strstr(const unsigned char *h, const unsigned char *n)
42 {
43 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
44 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
45 	for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8)
46 		;
47 	return *h ? (char *)h - 2 : 0;
48 }
49 
50 static char *
51 fourbyte_strstr(const unsigned char *h, 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 += 3; *h && hw != nw; hw = hw << 8 | *++h)
56 		;
57 	return *h ? (char *)h - 3 : 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_strstr(const unsigned char *h, const unsigned char *n)
77 {
78 	const unsigned char *z;
79 	size_t l, 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 (l = 0; n[l] && h[l]; l++)
85 		BITOP(byteset, n[l], |=), shift[n[l]] = l + 1;
86 	if (n[l])
87 		return 0; /* hit the end of h */
88 
89 	/* Compute maximal suffix */
90 	ip = -1;
91 	jp = 0;
92 	k = p = 1;
93 	while (jp + k < l) {
94 		if (n[ip + k] == n[jp + k]) {
95 			if (k == p) {
96 				jp += p;
97 				k = 1;
98 			} else
99 				k++;
100 		} else if (n[ip + k] > n[jp + k]) {
101 			jp += k;
102 			k = 1;
103 			p = jp - ip;
104 		} else {
105 			ip = jp++;
106 			k = p = 1;
107 		}
108 	}
109 	ms = ip;
110 	p0 = p;
111 
112 	/* And with the opposite comparison */
113 	ip = -1;
114 	jp = 0;
115 	k = p = 1;
116 	while (jp + k < l) {
117 		if (n[ip + k] == n[jp + k]) {
118 			if (k == p) {
119 				jp += p;
120 				k = 1;
121 			} else
122 				k++;
123 		} else if (n[ip + k] < n[jp + k]) {
124 			jp += k;
125 			k = 1;
126 			p = jp - ip;
127 		} else {
128 			ip = jp++;
129 			k = p = 1;
130 		}
131 	}
132 	if (ip + 1 > ms + 1)
133 		ms = ip;
134 	else
135 		p = p0;
136 
137 	/* Periodic needle? */
138 	if (memcmp(n, n + p, ms + 1)) {
139 		mem0 = 0;
140 		p = MAX(ms, l - ms - 1) + 1;
141 	} else
142 		mem0 = l - p;
143 	mem = 0;
144 
145 	/* Initialize incremental end-of-haystack pointer */
146 	z = h;
147 
148 	/* Search loop */
149 	for (;;) {
150 		/* Update incremental end-of-haystack pointer */
151 		if (z - h < l) {
152 			/* Fast estimate for MAX(l,63) */
153 			size_t grow = l | 63;
154 			const unsigned char *z2 = memchr(z, 0, grow);
155 			if (z2) {
156 				z = z2;
157 				if (z - h < l)
158 					return 0;
159 			} else
160 				z += grow;
161 		}
162 
163 		/* Check last byte first; advance by shift on mismatch */
164 		if (BITOP(byteset, h[l - 1], &)) {
165 			k = l - shift[h[l - 1]];
166 			if (k) {
167 				if (k < mem)
168 					k = mem;
169 				h += k;
170 				mem = 0;
171 				continue;
172 			}
173 		} else {
174 			h += l;
175 			mem = 0;
176 			continue;
177 		}
178 
179 		/* Compare right half */
180 		for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
181 			;
182 		if (n[k]) {
183 			h += k - ms;
184 			mem = 0;
185 			continue;
186 		}
187 		/* Compare left half */
188 		for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
189 			;
190 		if (k <= mem)
191 			return (char *)h;
192 		h += p;
193 		mem = mem0;
194 	}
195 }
196 
197 char *
198 strstr(const char *h, const char *n)
199 {
200 	/* Return immediately on empty needle */
201 	if (!n[0])
202 		return (char *)h;
203 
204 	/* Use faster algorithms for short needles */
205 	h = strchr(h, *n);
206 	if (!h || !n[1])
207 		return (char *)h;
208 	if (!h[1])
209 		return 0;
210 	if (!n[2])
211 		return twobyte_strstr((void *)h, (void *)n);
212 	if (!h[2])
213 		return 0;
214 	if (!n[3])
215 		return threebyte_strstr((void *)h, (void *)n);
216 	if (!h[3])
217 		return 0;
218 	if (!n[4])
219 		return fourbyte_strstr((void *)h, (void *)n);
220 
221 	return twoway_strstr((void *)h, (void *)n);
222 }
223