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