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