xref: /netbsd-src/common/lib/libc/string/memmem.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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 #if defined(LIBC_SCCS) && !defined(lint)
25 #if 0
26 __FBSDID("$FreeBSD: head/lib/libc/string/memmem.c 315468 2017-03-18 00:53:24Z emaste $");
27 #else
28 __RCSID("$NetBSD: memmem.c,v 1.1 2018/07/08 17:53:12 christos Exp $");
29 #endif
30 #endif /* LIBC_SCCS and not lint */
31 
32 #if !defined(_KERNEL) && !defined(_STANDALONE)
33 #include <string.h>
34 #include <stdint.h>
35 #else
36 #include <lib/libkern/libkern.h>
37 #endif
38 
39 static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
40 {
41 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
42 	for (h++, k--; k; k--, hw = hw<<8 | *++h)
43 		if (hw == nw) return __UNCONST(h-1);
44 	return 0;
45 }
46 
47 static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
48 {
49 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
50 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
51 	for (h+=2, k-=2; k; k--, hw = (hw|*++h)<<8)
52 		if (hw == nw) return __UNCONST(h-2);
53 	return 0;
54 }
55 
56 static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
57 {
58 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
59 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
60 	for (h+=3, k-=3; k; k--, hw = hw<<8 | *++h)
61 		if (hw == nw) return __UNCONST(h-3);
62 	return 0;
63 }
64 
65 #define MAX(a,b) ((a)>(b)?(a):(b))
66 #define MIN(a,b) ((a)<(b)?(a):(b))
67 
68 #define BITOP(a,b,op) \
69  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
70 
71 /*
72  * Two Way string search algorithm, with a bad shift table applied to the last
73  * byte of the window. A bit array marks which entries in the shift table are
74  * initialized to avoid fully initializing a 1kb/2kb table.
75  *
76  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
77  * Journal of the ACM 38(3):651-675
78  */
79 static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
80 {
81 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
82 	size_t byteset[32 / sizeof(size_t)] = { 0 };
83 	size_t shift[256];
84 
85 	/* Computing length of needle and fill shift table */
86 	for (i=0; i<l; i++)
87 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
88 
89 	/* Compute maximal suffix */
90 	ip = (size_t)-1; jp = 0; 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 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 = (size_t)-1; jp = 0; k = p = 1;
111 	while (jp+k<l) {
112 		if (n[ip+k] == n[jp+k]) {
113 			if (k == p) {
114 				jp += p;
115 				k = 1;
116 			} else k++;
117 		} else if (n[ip+k] < n[jp+k]) {
118 			jp += k;
119 			k = 1;
120 			p = jp - ip;
121 		} else {
122 			ip = jp++;
123 			k = p = 1;
124 		}
125 	}
126 	if (ip+1 > ms+1) ms = ip;
127 	else p = p0;
128 
129 	/* Periodic needle? */
130 	if (memcmp(n, n+p, ms+1)) {
131 		mem0 = 0;
132 		p = MAX(ms, l-ms-1) + 1;
133 	} else mem0 = l-p;
134 	mem = 0;
135 
136 	/* Search loop */
137 	for (;;) {
138 		/* If remainder of haystack is shorter than needle, done */
139 		if ((size_t)(z-h) < l) return 0;
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 (mem0 && mem && k < p) k = l-p;
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); k<l && n[k] == h[k]; k++);
158 		if (k < l) {
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 __UNCONST(h);
166 		h += p;
167 		mem = mem0;
168 	}
169 }
170 
171 void *memmem(const void *h0, size_t k, const void *n0, size_t l)
172 {
173 	const unsigned char *h = h0, *n = n0;
174 
175 	/* Return immediately on empty needle */
176 	if (!l) return __UNCONST(h);
177 
178 	/* Return immediately when needle is longer than haystack */
179 	if (k<l) return 0;
180 
181 	/* Use faster algorithms for short needles */
182 	h = memchr(h0, *n, k);
183 	if (!h || l==1) return __UNCONST(h);
184 	k -= h - (const unsigned char *)h0;
185 	if (k<l) return 0;
186 	if (l==2) return twobyte_memmem(h, k, n);
187 	if (l==3) return threebyte_memmem(h, k, n);
188 	if (l==4) return fourbyte_memmem(h, k, n);
189 
190 	return twoway_memmem(h, h+k, n, l);
191 }
192