xref: /freebsd-src/lib/libc/string/memmem.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
16050c8feSAndre Oppermann /*-
2d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: MIT
3d915a14eSPedro F. Giffuni  *
488521634SEd Maste  * Copyright (c) 2005-2014 Rich Felker, et al.
56050c8feSAndre Oppermann  *
688521634SEd Maste  * Permission is hereby granted, free of charge, to any person obtaining
788521634SEd Maste  * a copy of this software and associated documentation files (the
888521634SEd Maste  * "Software"), to deal in the Software without restriction, including
988521634SEd Maste  * without limitation the rights to use, copy, modify, merge, publish,
1088521634SEd Maste  * distribute, sublicense, and/or sell copies of the Software, and to
1188521634SEd Maste  * permit persons to whom the Software is furnished to do so, subject to
1288521634SEd Maste  * the following conditions:
136050c8feSAndre Oppermann  *
1488521634SEd Maste  * The above copyright notice and this permission notice shall be
1588521634SEd Maste  * included in all copies or substantial portions of the Software.
1688521634SEd Maste  *
1788521634SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1888521634SEd Maste  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1988521634SEd Maste  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2088521634SEd Maste  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
2188521634SEd Maste  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2288521634SEd Maste  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2388521634SEd Maste  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
246050c8feSAndre Oppermann  */
2588521634SEd Maste #include <stdint.h>
264874ddfdSEd Maste #include <string.h>
276050c8feSAndre Oppermann 
284874ddfdSEd Maste static char *
twobyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)294874ddfdSEd Maste twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
306050c8feSAndre Oppermann {
3188521634SEd Maste 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
329004dbddSGleb Smirnoff 	for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
334874ddfdSEd Maste 		if (hw == nw)
344874ddfdSEd Maste 			return (char *)h - 2;
359004dbddSGleb Smirnoff 	return hw == nw ? (char *)h - 2 : 0;
3688521634SEd Maste }
376050c8feSAndre Oppermann 
384874ddfdSEd Maste static char *
threebyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)394874ddfdSEd Maste threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
4088521634SEd Maste {
41*33482daeSEd Maste 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
42*33482daeSEd Maste 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
439004dbddSGleb Smirnoff 	for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8)
444874ddfdSEd Maste 		if (hw == nw)
454874ddfdSEd Maste 			return (char *)h - 3;
469004dbddSGleb Smirnoff 	return hw == nw ? (char *)h - 3 : 0;
4788521634SEd Maste }
486050c8feSAndre Oppermann 
494874ddfdSEd Maste static char *
fourbyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)504874ddfdSEd Maste fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
5188521634SEd Maste {
52*33482daeSEd Maste 	uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
53*33482daeSEd Maste 	uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
549004dbddSGleb Smirnoff 	for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
554874ddfdSEd Maste 		if (hw == nw)
564874ddfdSEd Maste 			return (char *)h - 4;
579004dbddSGleb Smirnoff 	return hw == nw ? (char *)h - 4 : 0;
5888521634SEd Maste }
596050c8feSAndre Oppermann 
6088521634SEd Maste #define MAX(a, b) ((a) > (b) ? (a) : (b))
6188521634SEd Maste #define MIN(a, b) ((a) < (b) ? (a) : (b))
626050c8feSAndre Oppermann 
6388521634SEd Maste #define BITOP(a, b, op) \
644874ddfdSEd Maste 	((a)[(size_t)(b) / (8 * sizeof *(a))] op \
654874ddfdSEd Maste 	    (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
666050c8feSAndre Oppermann 
6701dc206bSEd Maste /*
6801dc206bSEd Maste  * Two Way string search algorithm, with a bad shift table applied to the last
6901dc206bSEd Maste  * byte of the window. A bit array marks which entries in the shift table are
7001dc206bSEd Maste  * initialized to avoid fully initializing a 1kb/2kb table.
7101dc206bSEd Maste  *
7201dc206bSEd Maste  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
7301dc206bSEd Maste  * Journal of the ACM 38(3):651-675
7401dc206bSEd Maste  */
754874ddfdSEd Maste static char *
twoway_memmem(const unsigned char * h,const unsigned char * z,const unsigned char * n,size_t l)764874ddfdSEd Maste twoway_memmem(const unsigned char *h, const unsigned char *z,
774874ddfdSEd Maste     const unsigned char *n, size_t l)
7888521634SEd Maste {
7988521634SEd Maste 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
8088521634SEd Maste 	size_t byteset[32 / sizeof(size_t)] = { 0 };
8188521634SEd Maste 	size_t shift[256];
826050c8feSAndre Oppermann 
8388521634SEd Maste 	/* Computing length of needle and fill shift table */
8488521634SEd Maste 	for (i = 0; i < l; i++)
8588521634SEd Maste 		BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
8688521634SEd Maste 
8788521634SEd Maste 	/* Compute maximal suffix */
884874ddfdSEd Maste 	ip = -1;
894874ddfdSEd Maste 	jp = 0;
904874ddfdSEd Maste 	k = p = 1;
9188521634SEd Maste 	while (jp + k < l) {
9288521634SEd Maste 		if (n[ip + k] == n[jp + k]) {
9388521634SEd Maste 			if (k == p) {
9488521634SEd Maste 				jp += p;
9588521634SEd Maste 				k = 1;
964874ddfdSEd Maste 			} else
974874ddfdSEd Maste 				k++;
9888521634SEd Maste 		} else if (n[ip + k] > n[jp + k]) {
9988521634SEd Maste 			jp += k;
10088521634SEd Maste 			k = 1;
10188521634SEd Maste 			p = jp - ip;
10288521634SEd Maste 		} else {
10388521634SEd Maste 			ip = jp++;
10488521634SEd Maste 			k = p = 1;
10588521634SEd Maste 		}
10688521634SEd Maste 	}
10788521634SEd Maste 	ms = ip;
10888521634SEd Maste 	p0 = p;
10988521634SEd Maste 
11088521634SEd Maste 	/* And with the opposite comparison */
1114874ddfdSEd Maste 	ip = -1;
1124874ddfdSEd Maste 	jp = 0;
1134874ddfdSEd Maste 	k = p = 1;
11488521634SEd Maste 	while (jp + k < l) {
11588521634SEd Maste 		if (n[ip + k] == n[jp + k]) {
11688521634SEd Maste 			if (k == p) {
11788521634SEd Maste 				jp += p;
11888521634SEd Maste 				k = 1;
1194874ddfdSEd Maste 			} else
1204874ddfdSEd Maste 				k++;
12188521634SEd Maste 		} else if (n[ip + k] < n[jp + k]) {
12288521634SEd Maste 			jp += k;
12388521634SEd Maste 			k = 1;
12488521634SEd Maste 			p = jp - ip;
12588521634SEd Maste 		} else {
12688521634SEd Maste 			ip = jp++;
12788521634SEd Maste 			k = p = 1;
12888521634SEd Maste 		}
12988521634SEd Maste 	}
1304874ddfdSEd Maste 	if (ip + 1 > ms + 1)
1314874ddfdSEd Maste 		ms = ip;
1324874ddfdSEd Maste 	else
1334874ddfdSEd Maste 		p = p0;
13488521634SEd Maste 
13588521634SEd Maste 	/* Periodic needle? */
13688521634SEd Maste 	if (memcmp(n, n + p, ms + 1)) {
13788521634SEd Maste 		mem0 = 0;
13888521634SEd Maste 		p = MAX(ms, l - ms - 1) + 1;
1394874ddfdSEd Maste 	} else
1404874ddfdSEd Maste 		mem0 = l - p;
14188521634SEd Maste 	mem = 0;
14288521634SEd Maste 
14388521634SEd Maste 	/* Search loop */
14488521634SEd Maste 	for (;;) {
14588521634SEd Maste 		/* If remainder of haystack is shorter than needle, done */
1464874ddfdSEd Maste 		if (z - h < l)
1474874ddfdSEd Maste 			return 0;
14888521634SEd Maste 
14988521634SEd Maste 		/* Check last byte first; advance by shift on mismatch */
15088521634SEd Maste 		if (BITOP(byteset, h[l - 1], &)) {
15188521634SEd Maste 			k = l - shift[h[l - 1]];
15288521634SEd Maste 			if (k) {
1537dbcd06eSEd Maste 				if (k < mem)
1547dbcd06eSEd Maste 					k = mem;
15588521634SEd Maste 				h += k;
15688521634SEd Maste 				mem = 0;
15788521634SEd Maste 				continue;
15888521634SEd Maste 			}
15988521634SEd Maste 		} else {
16088521634SEd Maste 			h += l;
16188521634SEd Maste 			mem = 0;
16288521634SEd Maste 			continue;
16388521634SEd Maste 		}
16488521634SEd Maste 
16588521634SEd Maste 		/* Compare right half */
1664874ddfdSEd Maste 		for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
1674874ddfdSEd Maste 			;
16888521634SEd Maste 		if (k < l) {
16988521634SEd Maste 			h += k - ms;
17088521634SEd Maste 			mem = 0;
17188521634SEd Maste 			continue;
17288521634SEd Maste 		}
17388521634SEd Maste 		/* Compare left half */
1744874ddfdSEd Maste 		for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
1754874ddfdSEd Maste 			;
1764874ddfdSEd Maste 		if (k <= mem)
1774874ddfdSEd Maste 			return (char *)h;
17888521634SEd Maste 		h += p;
17988521634SEd Maste 		mem = mem0;
18088521634SEd Maste 	}
18188521634SEd Maste }
18288521634SEd Maste 
1834874ddfdSEd Maste void *
memmem(const void * h0,size_t k,const void * n0,size_t l)1844874ddfdSEd Maste memmem(const void *h0, size_t k, const void *n0, size_t l)
18588521634SEd Maste {
18688521634SEd Maste 	const unsigned char *h = h0, *n = n0;
18788521634SEd Maste 
18888521634SEd Maste 	/* Return immediately on empty needle */
1894874ddfdSEd Maste 	if (!l)
1904874ddfdSEd Maste 		return (void *)h;
19188521634SEd Maste 
19288521634SEd Maste 	/* Return immediately when needle is longer than haystack */
1934874ddfdSEd Maste 	if (k < l)
1944874ddfdSEd Maste 		return 0;
19588521634SEd Maste 
19688521634SEd Maste 	/* Use faster algorithms for short needles */
19788521634SEd Maste 	h = memchr(h0, *n, k);
1984874ddfdSEd Maste 	if (!h || l == 1)
1994874ddfdSEd Maste 		return (void *)h;
20088521634SEd Maste 	k -= h - (const unsigned char *)h0;
2014874ddfdSEd Maste 	if (k < l)
2024874ddfdSEd Maste 		return 0;
2034874ddfdSEd Maste 	if (l == 2)
2044874ddfdSEd Maste 		return twobyte_memmem(h, k, n);
2054874ddfdSEd Maste 	if (l == 3)
2064874ddfdSEd Maste 		return threebyte_memmem(h, k, n);
2074874ddfdSEd Maste 	if (l == 4)
2084874ddfdSEd Maste 		return fourbyte_memmem(h, k, n);
20988521634SEd Maste 
21088521634SEd Maste 	return twoway_memmem(h, h + k, n, l);
2116050c8feSAndre Oppermann }
212