xref: /freebsd/lib/libc/string/strstr.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 *
30 twobyte_strstr(const unsigned char *h, const unsigned char *n)
31 {
32 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
33 	for (h++; *h && hw != nw; hw = hw << 8 | *++h)
34 		;
35 	return *h ? (char *)h - 1 : 0;
36 }
37 
38 static char *
39 threebyte_strstr(const unsigned char *h, 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 += 2; *h && hw != nw; hw = (hw | *++h) << 8)
44 		;
45 	return *h ? (char *)h - 2 : 0;
46 }
47 
48 static char *
49 fourbyte_strstr(const unsigned char *h, const unsigned char *n)
50 {
51 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
52 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
53 	for (h += 3; *h && hw != nw; hw = hw << 8 | *++h)
54 		;
55 	return *h ? (char *)h - 3 : 0;
56 }
57 
58 #define MAX(a, b) ((a) > (b) ? (a) : (b))
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 
61 #define BITOP(a, b, op) \
62 	((a)[(size_t)(b) / (8 * sizeof *(a))] op \
63 	    (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
64 
65 /*
66  * Two Way string search algorithm, with a bad shift table applied to the last
67  * byte of the window. A bit array marks which entries in the shift table are
68  * initialized to avoid fully initializing a 1kb/2kb table.
69  *
70  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
71  * Journal of the ACM 38(3):651-675
72  */
73 static char *
74 twoway_strstr(const unsigned char *h, const unsigned char *n)
75 {
76 	const unsigned char *z;
77 	size_t l, ip, jp, k, p, ms, p0, mem, mem0;
78 	size_t byteset[32 / sizeof(size_t)] = { 0 };
79 	size_t shift[256];
80 
81 	/* Computing length of needle and fill shift table */
82 	for (l = 0; n[l] && h[l]; l++)
83 		BITOP(byteset, n[l], |=), shift[n[l]] = l + 1;
84 	if (n[l])
85 		return 0; /* hit the end of h */
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 	/* Initialize incremental end-of-haystack pointer */
144 	z = h;
145 
146 	/* Search loop */
147 	for (;;) {
148 		/* Update incremental end-of-haystack pointer */
149 		if (z - h < l) {
150 			/* Fast estimate for MAX(l,63) */
151 			size_t grow = l | 63;
152 			const unsigned char *z2 = memchr(z, 0, grow);
153 			if (z2) {
154 				z = z2;
155 				if (z - h < l)
156 					return 0;
157 			} else
158 				z += grow;
159 		}
160 
161 		/* Check last byte first; advance by shift on mismatch */
162 		if (BITOP(byteset, h[l - 1], &)) {
163 			k = l - shift[h[l - 1]];
164 			if (k) {
165 				if (k < mem)
166 					k = mem;
167 				h += k;
168 				mem = 0;
169 				continue;
170 			}
171 		} else {
172 			h += l;
173 			mem = 0;
174 			continue;
175 		}
176 
177 		/* Compare right half */
178 		for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
179 			;
180 		if (n[k]) {
181 			h += k - ms;
182 			mem = 0;
183 			continue;
184 		}
185 		/* Compare left half */
186 		for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
187 			;
188 		if (k <= mem)
189 			return (char *)h;
190 		h += p;
191 		mem = mem0;
192 	}
193 }
194 
195 char *
196 strstr(const char *h, const char *n)
197 {
198 	/* Return immediately on empty needle */
199 	if (!n[0])
200 		return (char *)h;
201 
202 	/* Use faster algorithms for short needles */
203 	h = strchr(h, *n);
204 	if (!h || !n[1])
205 		return (char *)h;
206 	if (!h[1])
207 		return 0;
208 	if (!n[2])
209 		return twobyte_strstr((void *)h, (void *)n);
210 	if (!h[2])
211 		return 0;
212 	if (!n[3])
213 		return threebyte_strstr((void *)h, (void *)n);
214 	if (!h[3])
215 		return 0;
216 	if (!n[4])
217 		return fourbyte_strstr((void *)h, (void *)n);
218 
219 	return twoway_strstr((void *)h, (void *)n);
220 }
221