xref: /openbsd-src/lib/libc/string/memmem.c (revision f61003b04bf8dba5511779d655c4a010132df24d)
1*f61003b0Sclaudio /*	$OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */
2*f61003b0Sclaudio 
3*f61003b0Sclaudio /*
4*f61003b0Sclaudio  * Copyright (c) 2005-2020 Rich Felker, et al.
52eb94c6cSajacoutot  *
6*f61003b0Sclaudio  * Permission is hereby granted, free of charge, to any person obtaining
7*f61003b0Sclaudio  * a copy of this software and associated documentation files (the
8*f61003b0Sclaudio  * "Software"), to deal in the Software without restriction, including
9*f61003b0Sclaudio  * without limitation the rights to use, copy, modify, merge, publish,
10*f61003b0Sclaudio  * distribute, sublicense, and/or sell copies of the Software, and to
11*f61003b0Sclaudio  * permit persons to whom the Software is furnished to do so, subject to
12*f61003b0Sclaudio  * the following conditions:
132eb94c6cSajacoutot  *
14*f61003b0Sclaudio  * The above copyright notice and this permission notice shall be
15*f61003b0Sclaudio  * included in all copies or substantial portions of the Software.
16*f61003b0Sclaudio  *
17*f61003b0Sclaudio  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18*f61003b0Sclaudio  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19*f61003b0Sclaudio  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20*f61003b0Sclaudio  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21*f61003b0Sclaudio  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22*f61003b0Sclaudio  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23*f61003b0Sclaudio  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242eb94c6cSajacoutot  */
252eb94c6cSajacoutot 
262eb94c6cSajacoutot #include <string.h>
27*f61003b0Sclaudio #include <stdint.h>
28*f61003b0Sclaudio 
29*f61003b0Sclaudio static char *
twobyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)30*f61003b0Sclaudio twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
31*f61003b0Sclaudio {
32*f61003b0Sclaudio 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
33*f61003b0Sclaudio 	for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
34*f61003b0Sclaudio 		if (hw == nw) return (char *)h-2;
35*f61003b0Sclaudio 	return hw == nw ? (char *)h-2 : 0;
36*f61003b0Sclaudio }
37*f61003b0Sclaudio 
38*f61003b0Sclaudio static char *
threebyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)39*f61003b0Sclaudio threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
40*f61003b0Sclaudio {
41*f61003b0Sclaudio 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
42*f61003b0Sclaudio 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
43*f61003b0Sclaudio 	for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
44*f61003b0Sclaudio 		if (hw == nw) return (char *)h-3;
45*f61003b0Sclaudio 	return hw == nw ? (char *)h-3 : 0;
46*f61003b0Sclaudio }
47*f61003b0Sclaudio 
48*f61003b0Sclaudio static char *
fourbyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)49*f61003b0Sclaudio fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
50*f61003b0Sclaudio {
51*f61003b0Sclaudio 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
52*f61003b0Sclaudio 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
53*f61003b0Sclaudio 	for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
54*f61003b0Sclaudio 		if (hw == nw) return (char *)h-4;
55*f61003b0Sclaudio 	return hw == nw ? (char *)h-4 : 0;
56*f61003b0Sclaudio }
57*f61003b0Sclaudio 
58*f61003b0Sclaudio #define MAX(a,b) ((a)>(b)?(a):(b))
59*f61003b0Sclaudio #define MIN(a,b) ((a)<(b)?(a):(b))
60*f61003b0Sclaudio 
61*f61003b0Sclaudio #define BITOP(a,b,op) \
62*f61003b0Sclaudio  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
632eb94c6cSajacoutot 
642eb94c6cSajacoutot /*
65*f61003b0Sclaudio  * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
66*f61003b0Sclaudio  * Journal of the ACM, 38(3):651-675, July 1991.
672eb94c6cSajacoutot  */
68*f61003b0Sclaudio static char *
twoway_memmem(const unsigned char * h,const unsigned char * z,const unsigned char * n,size_t l)69*f61003b0Sclaudio twoway_memmem(const unsigned char *h, const unsigned char *z,
70*f61003b0Sclaudio     const unsigned char *n, size_t l)
71*f61003b0Sclaudio {
72*f61003b0Sclaudio 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
73*f61003b0Sclaudio 	size_t byteset[32 / sizeof(size_t)] = { 0 };
74*f61003b0Sclaudio 	size_t shift[256];
75*f61003b0Sclaudio 
76*f61003b0Sclaudio 	/* Computing length of needle and fill shift table */
77*f61003b0Sclaudio 	for (i=0; i<l; i++)
78*f61003b0Sclaudio 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
79*f61003b0Sclaudio 
80*f61003b0Sclaudio 	/* Compute maximal suffix */
81*f61003b0Sclaudio 	ip = -1; jp = 0; k = p = 1;
82*f61003b0Sclaudio 	while (jp+k<l) {
83*f61003b0Sclaudio 		if (n[ip+k] == n[jp+k]) {
84*f61003b0Sclaudio 			if (k == p) {
85*f61003b0Sclaudio 				jp += p;
86*f61003b0Sclaudio 				k = 1;
87*f61003b0Sclaudio 			} else k++;
88*f61003b0Sclaudio 		} else if (n[ip+k] > n[jp+k]) {
89*f61003b0Sclaudio 			jp += k;
90*f61003b0Sclaudio 			k = 1;
91*f61003b0Sclaudio 			p = jp - ip;
92*f61003b0Sclaudio 		} else {
93*f61003b0Sclaudio 			ip = jp++;
94*f61003b0Sclaudio 			k = p = 1;
95*f61003b0Sclaudio 		}
96*f61003b0Sclaudio 	}
97*f61003b0Sclaudio 	ms = ip;
98*f61003b0Sclaudio 	p0 = p;
99*f61003b0Sclaudio 
100*f61003b0Sclaudio 	/* And with the opposite comparison */
101*f61003b0Sclaudio 	ip = -1; jp = 0; k = p = 1;
102*f61003b0Sclaudio 	while (jp+k<l) {
103*f61003b0Sclaudio 		if (n[ip+k] == n[jp+k]) {
104*f61003b0Sclaudio 			if (k == p) {
105*f61003b0Sclaudio 				jp += p;
106*f61003b0Sclaudio 				k = 1;
107*f61003b0Sclaudio 			} else k++;
108*f61003b0Sclaudio 		} else if (n[ip+k] < n[jp+k]) {
109*f61003b0Sclaudio 			jp += k;
110*f61003b0Sclaudio 			k = 1;
111*f61003b0Sclaudio 			p = jp - ip;
112*f61003b0Sclaudio 		} else {
113*f61003b0Sclaudio 			ip = jp++;
114*f61003b0Sclaudio 			k = p = 1;
115*f61003b0Sclaudio 		}
116*f61003b0Sclaudio 	}
117*f61003b0Sclaudio 	if (ip+1 > ms+1) ms = ip;
118*f61003b0Sclaudio 	else p = p0;
119*f61003b0Sclaudio 
120*f61003b0Sclaudio 	/* Periodic needle? */
121*f61003b0Sclaudio 	if (memcmp(n, n+p, ms+1)) {
122*f61003b0Sclaudio 		mem0 = 0;
123*f61003b0Sclaudio 		p = MAX(ms, l-ms-1) + 1;
124*f61003b0Sclaudio 	} else mem0 = l-p;
125*f61003b0Sclaudio 	mem = 0;
126*f61003b0Sclaudio 
127*f61003b0Sclaudio 	/* Search loop */
128*f61003b0Sclaudio 	for (;;) {
129*f61003b0Sclaudio 		/* If remainder of haystack is shorter than needle, done */
130*f61003b0Sclaudio 		if (z-h < l) return 0;
131*f61003b0Sclaudio 
132*f61003b0Sclaudio 		/* Check last byte first; advance by shift on mismatch */
133*f61003b0Sclaudio 		if (BITOP(byteset, h[l-1], &)) {
134*f61003b0Sclaudio 			k = l-shift[h[l-1]];
135*f61003b0Sclaudio 			if (k) {
136*f61003b0Sclaudio 				if (k < mem) k = mem;
137*f61003b0Sclaudio 				h += k;
138*f61003b0Sclaudio 				mem = 0;
139*f61003b0Sclaudio 				continue;
140*f61003b0Sclaudio 			}
141*f61003b0Sclaudio 		} else {
142*f61003b0Sclaudio 			h += l;
143*f61003b0Sclaudio 			mem = 0;
144*f61003b0Sclaudio 			continue;
145*f61003b0Sclaudio 		}
146*f61003b0Sclaudio 
147*f61003b0Sclaudio 		/* Compare right half */
148*f61003b0Sclaudio 		for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
149*f61003b0Sclaudio 		if (k < l) {
150*f61003b0Sclaudio 			h += k-ms;
151*f61003b0Sclaudio 			mem = 0;
152*f61003b0Sclaudio 			continue;
153*f61003b0Sclaudio 		}
154*f61003b0Sclaudio 		/* Compare left half */
155*f61003b0Sclaudio 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
156*f61003b0Sclaudio 		if (k <= mem) return (char *)h;
157*f61003b0Sclaudio 		h += p;
158*f61003b0Sclaudio 		mem = mem0;
159*f61003b0Sclaudio 	}
160*f61003b0Sclaudio }
1612eb94c6cSajacoutot 
1622eb94c6cSajacoutot void *
memmem(const void * h0,size_t k,const void * n0,size_t l)163*f61003b0Sclaudio memmem(const void *h0, size_t k, const void *n0, size_t l)
1642eb94c6cSajacoutot {
165*f61003b0Sclaudio 	const unsigned char *h = h0, *n = n0;
1662eb94c6cSajacoutot 
167*f61003b0Sclaudio 	/* Return immediately on empty needle */
168*f61003b0Sclaudio 	if (!l) return (void *)h;
1692eb94c6cSajacoutot 
170*f61003b0Sclaudio 	/* Return immediately when needle is longer than haystack */
171*f61003b0Sclaudio 	if (k<l) return 0;
1722eb94c6cSajacoutot 
173*f61003b0Sclaudio 	/* Use faster algorithms for short needles */
174*f61003b0Sclaudio 	h = memchr(h0, *n, k);
175*f61003b0Sclaudio 	if (!h || l==1) return (void *)h;
176*f61003b0Sclaudio 	k -= h - (const unsigned char *)h0;
177*f61003b0Sclaudio 	if (k<l) return 0;
178*f61003b0Sclaudio 	if (l==2) return twobyte_memmem(h, k, n);
179*f61003b0Sclaudio 	if (l==3) return threebyte_memmem(h, k, n);
180*f61003b0Sclaudio 	if (l==4) return fourbyte_memmem(h, k, n);
1812eb94c6cSajacoutot 
182*f61003b0Sclaudio 	return twoway_memmem(h, h+k, n, l);
1832eb94c6cSajacoutot }
1849b9d2a55Sguenther DEF_WEAK(memmem);
185