xref: /freebsd/lib/libc/string/memmem.c (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
1 /*-
2  * Copyright (c) 2005-2014 Rich Felker, et al.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25 
26 #include <string.h>
27 #include <stdint.h>
28 
29 static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
30 {
31 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
32 	for (h++, k--; k; k--, hw = hw<<8 | *++h)
33 		if (hw == nw) return (char *)h-1;
34 	return 0;
35 }
36 
37 static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
38 {
39 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
40 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
41 	for (h+=2, k-=2; k; k--, hw = (hw|*++h)<<8)
42 		if (hw == nw) return (char *)h-2;
43 	return 0;
44 }
45 
46 static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
47 {
48 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
49 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
50 	for (h+=3, k-=3; k; k--, hw = hw<<8 | *++h)
51 		if (hw == nw) return (char *)h-3;
52 	return 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  * Two Way string search algorithm, with a bad shift table applied to the last
63  * byte of the window. A bit array marks which entries in the shift table are
64  * initialized to avoid fully initializing a 1kb/2kb table.
65  *
66  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
67  * Journal of the ACM 38(3):651-675
68  */
69 static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
70 {
71 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
72 	size_t byteset[32 / sizeof(size_t)] = { 0 };
73 	size_t shift[256];
74 
75 	/* Computing length of needle and fill shift table */
76 	for (i=0; i<l; i++)
77 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
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 	/* Search loop */
127 	for (;;) {
128 		/* If remainder of haystack is shorter than needle, done */
129 		if (z-h < l) return 0;
130 
131 		/* Check last byte first; advance by shift on mismatch */
132 		if (BITOP(byteset, h[l-1], &)) {
133 			k = l-shift[h[l-1]];
134 			if (k) {
135 				if (mem0 && mem && k < p) k = l-p;
136 				h += k;
137 				mem = 0;
138 				continue;
139 			}
140 		} else {
141 			h += l;
142 			mem = 0;
143 			continue;
144 		}
145 
146 		/* Compare right half */
147 		for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
148 		if (k < l) {
149 			h += k-ms;
150 			mem = 0;
151 			continue;
152 		}
153 		/* Compare left half */
154 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
155 		if (k <= mem) return (char *)h;
156 		h += p;
157 		mem = mem0;
158 	}
159 }
160 
161 void *memmem(const void *h0, size_t k, const void *n0, size_t l)
162 {
163 	const unsigned char *h = h0, *n = n0;
164 
165 	/* Return immediately on empty needle */
166 	if (!l) return (void *)h;
167 
168 	/* Return immediately when needle is longer than haystack */
169 	if (k<l) return 0;
170 
171 	/* Use faster algorithms for short needles */
172 	h = memchr(h0, *n, k);
173 	if (!h || l==1) return (void *)h;
174 	k -= h - (const unsigned char *)h0;
175 	if (k<l) return 0;
176 	if (l==2) return twobyte_memmem(h, k, n);
177 	if (l==3) return threebyte_memmem(h, k, n);
178 	if (l==4) return fourbyte_memmem(h, k, n);
179 
180 	return twoway_memmem(h, h+k, n, l);
181 }
182