xref: /openbsd-src/lib/libc/string/strstr.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: strstr.c,v 1.7 2017/04/12 16:06:12 millert Exp $ */
2 
3 /*
4  * Copyright (c) 2005-2014 Rich Felker
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 
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 
30 static char *
31 twobyte_strstr(const unsigned char *h, const unsigned char *n)
32 {
33 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
34 	for (h++; *h && hw != nw; hw = hw<<8 | *++h);
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 = n[0]<<24 | n[1]<<16 | n[2]<<8;
42 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
43 	for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
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 = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
51 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
52 	for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
53 	return *h ? (char *)h-3 : 0;
54 }
55 
56 #define MAX(a,b) ((a)>(b)?(a):(b))
57 #define MIN(a,b) ((a)<(b)?(a):(b))
58 
59 #define BITOP(a,b,op) \
60  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
61 
62 /*
63  * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
64  * Journal of the ACM, 38(3):651-675, July 1991.
65  */
66 static char *
67 twoway_strstr(const unsigned char *h, const unsigned char *n)
68 {
69 	const unsigned char *z;
70 	size_t l, ip, jp, k, p, ms, p0, mem, mem0;
71 	size_t byteset[32 / sizeof(size_t)] = { 0 };
72 	size_t shift[256];
73 
74 	/* Computing length of needle and fill shift table */
75 	for (l=0; n[l] && h[l]; l++)
76 		BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
77 	if (n[l]) return 0; /* hit the end of h */
78 
79 	/* Compute maximal suffix */
80 	ip = -1; jp = 0; k = p = 1;
81 	while (jp+k<l) {
82 		if (n[ip+k] == n[jp+k]) {
83 			if (k == p) {
84 				jp += p;
85 				k = 1;
86 			} else k++;
87 		} else if (n[ip+k] > n[jp+k]) {
88 			jp += k;
89 			k = 1;
90 			p = jp - ip;
91 		} else {
92 			ip = jp++;
93 			k = p = 1;
94 		}
95 	}
96 	ms = ip;
97 	p0 = p;
98 
99 	/* And with the opposite comparison */
100 	ip = -1; jp = 0; k = p = 1;
101 	while (jp+k<l) {
102 		if (n[ip+k] == n[jp+k]) {
103 			if (k == p) {
104 				jp += p;
105 				k = 1;
106 			} else k++;
107 		} else if (n[ip+k] < n[jp+k]) {
108 			jp += k;
109 			k = 1;
110 			p = jp - ip;
111 		} else {
112 			ip = jp++;
113 			k = p = 1;
114 		}
115 	}
116 	if (ip+1 > ms+1) ms = ip;
117 	else p = p0;
118 
119 	/* Periodic needle? */
120 	if (memcmp(n, n+p, ms+1)) {
121 		mem0 = 0;
122 		p = MAX(ms, l-ms-1) + 1;
123 	} else mem0 = l-p;
124 	mem = 0;
125 
126 	/* Initialize incremental end-of-haystack pointer */
127 	z = h;
128 
129 	/* Search loop */
130 	for (;;) {
131 		/* Update incremental end-of-haystack pointer */
132 		if (z-h < l) {
133 			/* Fast estimate for MIN(l,63) */
134 			size_t grow = l | 63;
135 			const unsigned char *z2 = memchr(z, 0, grow);
136 			if (z2) {
137 				z = z2;
138 				if (z-h < l) return 0;
139 			} else z += grow;
140 		}
141 
142 		/* Check last byte first; advance by shift on mismatch */
143 		if (BITOP(byteset, h[l-1], &)) {
144 			k = l-shift[h[l-1]];
145 #ifdef DEBUG
146 			printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
147 #endif
148 			if (k) {
149 				if (mem0 && mem && k < p) k = l-p;
150 				h += k;
151 				mem = 0;
152 				continue;
153 			}
154 		} else {
155 			h += l;
156 			mem = 0;
157 			continue;
158 		}
159 
160 		/* Compare right half */
161 		for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
162 		if (n[k]) {
163 			h += k-ms;
164 			mem = 0;
165 			continue;
166 		}
167 		/* Compare left half */
168 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
169 		if (k <= mem) return (char *)h;
170 		h += p;
171 		mem = mem0;
172 	}
173 }
174 
175 char *
176 strstr(const char *h, const char *n)
177 {
178 	/* Return immediately on empty needle */
179 	if (!n[0]) return (char *)h;
180 
181 	/* Use faster algorithms for short needles */
182 	h = strchr(h, *n);
183 	if (!h || !n[1]) return (char *)h;
184 	if (!h[1]) return 0;
185 	if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
186 	if (!h[2]) return 0;
187 	if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
188 	if (!h[3]) return 0;
189 	if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
190 
191 	return twoway_strstr((void *)h, (void *)n);
192 }
193 DEF_STRONG(strstr);
194