Home | History | Annotate | Line # | Download | only in string
memmem.c revision 1.4
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2005-2014 Rich Felker, et al.
      3  1.1  christos  *
      4  1.1  christos  * Permission is hereby granted, free of charge, to any person obtaining
      5  1.1  christos  * a copy of this software and associated documentation files (the
      6  1.1  christos  * "Software"), to deal in the Software without restriction, including
      7  1.1  christos  * without limitation the rights to use, copy, modify, merge, publish,
      8  1.1  christos  * distribute, sublicense, and/or sell copies of the Software, and to
      9  1.1  christos  * permit persons to whom the Software is furnished to do so, subject to
     10  1.1  christos  * the following conditions:
     11  1.1  christos  *
     12  1.1  christos  * The above copyright notice and this permission notice shall be
     13  1.1  christos  * included in all copies or substantial portions of the Software.
     14  1.1  christos  *
     15  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     16  1.1  christos  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17  1.1  christos  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     19  1.1  christos  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     20  1.1  christos  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     21  1.1  christos  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  1.1  christos  */
     23  1.1  christos #include <sys/cdefs.h>
     24  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     25  1.1  christos #if 0
     26  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/string/memmem.c 315468 2017-03-18 00:53:24Z emaste $");
     27  1.1  christos #else
     28  1.4    rillig __RCSID("$NetBSD: memmem.c,v 1.4 2021/05/16 09:43:39 rillig Exp $");
     29  1.1  christos #endif
     30  1.1  christos #endif /* LIBC_SCCS and not lint */
     31  1.1  christos 
     32  1.1  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
     33  1.1  christos #include <string.h>
     34  1.1  christos #include <stdint.h>
     35  1.1  christos #else
     36  1.1  christos #include <lib/libkern/libkern.h>
     37  1.1  christos #endif
     38  1.1  christos 
     39  1.2  christos static char *twobyte_memmem(const unsigned char *h, size_t k,
     40  1.2  christos     const unsigned char *n)
     41  1.1  christos {
     42  1.2  christos 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
     43  1.3  christos 	for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
     44  1.2  christos 		if (hw == nw) return __UNCONST(h - 2);
     45  1.2  christos 	return hw == nw ? __UNCONST(h - 2) : 0;
     46  1.1  christos }
     47  1.1  christos 
     48  1.2  christos static char *threebyte_memmem(const unsigned char *h, size_t k,
     49  1.2  christos     const unsigned char *n)
     50  1.1  christos {
     51  1.2  christos 	uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
     52  1.2  christos 	uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
     53  1.3  christos 	for (h += 3, k -= 3; k; k--, hw = (hw|*h++) << 8)
     54  1.2  christos 		if (hw == nw) return __UNCONST(h - 3);
     55  1.2  christos 	return hw == nw ? __UNCONST(h - 3) : 0;
     56  1.1  christos }
     57  1.1  christos 
     58  1.2  christos static char *fourbyte_memmem(const unsigned char *h, size_t k,
     59  1.2  christos     const unsigned char *n)
     60  1.1  christos {
     61  1.2  christos 	uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
     62  1.2  christos 	uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
     63  1.3  christos 	for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
     64  1.2  christos 		if (hw == nw) return __UNCONST(h - 4);
     65  1.2  christos 	return hw == nw ? __UNCONST(h - 4) : 0;
     66  1.1  christos }
     67  1.1  christos 
     68  1.1  christos #define MAX(a,b) ((a)>(b)?(a):(b))
     69  1.1  christos #define MIN(a,b) ((a)<(b)?(a):(b))
     70  1.1  christos 
     71  1.1  christos #define BITOP(a,b,op) \
     72  1.1  christos  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
     73  1.1  christos 
     74  1.1  christos /*
     75  1.1  christos  * Two Way string search algorithm, with a bad shift table applied to the last
     76  1.1  christos  * byte of the window. A bit array marks which entries in the shift table are
     77  1.1  christos  * initialized to avoid fully initializing a 1kb/2kb table.
     78  1.1  christos  *
     79  1.1  christos  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
     80  1.1  christos  * Journal of the ACM 38(3):651-675
     81  1.1  christos  */
     82  1.1  christos static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
     83  1.1  christos {
     84  1.1  christos 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
     85  1.1  christos 	size_t byteset[32 / sizeof(size_t)] = { 0 };
     86  1.1  christos 	size_t shift[256];
     87  1.1  christos 
     88  1.1  christos 	/* Computing length of needle and fill shift table */
     89  1.1  christos 	for (i=0; i<l; i++)
     90  1.1  christos 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
     91  1.1  christos 
     92  1.1  christos 	/* Compute maximal suffix */
     93  1.1  christos 	ip = (size_t)-1; jp = 0; k = p = 1;
     94  1.1  christos 	while (jp+k<l) {
     95  1.1  christos 		if (n[ip+k] == n[jp+k]) {
     96  1.1  christos 			if (k == p) {
     97  1.1  christos 				jp += p;
     98  1.1  christos 				k = 1;
     99  1.1  christos 			} else k++;
    100  1.1  christos 		} else if (n[ip+k] > n[jp+k]) {
    101  1.1  christos 			jp += k;
    102  1.1  christos 			k = 1;
    103  1.1  christos 			p = jp - ip;
    104  1.1  christos 		} else {
    105  1.1  christos 			ip = jp++;
    106  1.1  christos 			k = p = 1;
    107  1.1  christos 		}
    108  1.1  christos 	}
    109  1.1  christos 	ms = ip;
    110  1.1  christos 	p0 = p;
    111  1.1  christos 
    112  1.1  christos 	/* And with the opposite comparison */
    113  1.1  christos 	ip = (size_t)-1; jp = 0; k = p = 1;
    114  1.1  christos 	while (jp+k<l) {
    115  1.1  christos 		if (n[ip+k] == n[jp+k]) {
    116  1.1  christos 			if (k == p) {
    117  1.1  christos 				jp += p;
    118  1.1  christos 				k = 1;
    119  1.1  christos 			} else k++;
    120  1.1  christos 		} else if (n[ip+k] < n[jp+k]) {
    121  1.1  christos 			jp += k;
    122  1.1  christos 			k = 1;
    123  1.1  christos 			p = jp - ip;
    124  1.1  christos 		} else {
    125  1.1  christos 			ip = jp++;
    126  1.1  christos 			k = p = 1;
    127  1.1  christos 		}
    128  1.1  christos 	}
    129  1.1  christos 	if (ip+1 > ms+1) ms = ip;
    130  1.1  christos 	else p = p0;
    131  1.1  christos 
    132  1.1  christos 	/* Periodic needle? */
    133  1.1  christos 	if (memcmp(n, n+p, ms+1)) {
    134  1.1  christos 		mem0 = 0;
    135  1.1  christos 		p = MAX(ms, l-ms-1) + 1;
    136  1.1  christos 	} else mem0 = l-p;
    137  1.1  christos 	mem = 0;
    138  1.1  christos 
    139  1.1  christos 	/* Search loop */
    140  1.1  christos 	for (;;) {
    141  1.1  christos 		/* If remainder of haystack is shorter than needle, done */
    142  1.1  christos 		if ((size_t)(z-h) < l) return 0;
    143  1.1  christos 
    144  1.1  christos 		/* Check last byte first; advance by shift on mismatch */
    145  1.1  christos 		if (BITOP(byteset, h[l-1], &)) {
    146  1.1  christos 			k = l-shift[h[l-1]];
    147  1.1  christos 			if (k) {
    148  1.1  christos 				if (mem0 && mem && k < p) k = l-p;
    149  1.1  christos 				h += k;
    150  1.1  christos 				mem = 0;
    151  1.1  christos 				continue;
    152  1.1  christos 			}
    153  1.1  christos 		} else {
    154  1.1  christos 			h += l;
    155  1.1  christos 			mem = 0;
    156  1.1  christos 			continue;
    157  1.1  christos 		}
    158  1.1  christos 
    159  1.1  christos 		/* Compare right half */
    160  1.1  christos 		for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
    161  1.1  christos 		if (k < l) {
    162  1.1  christos 			h += k-ms;
    163  1.1  christos 			mem = 0;
    164  1.1  christos 			continue;
    165  1.1  christos 		}
    166  1.1  christos 		/* Compare left half */
    167  1.1  christos 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
    168  1.1  christos 		if (k <= mem) return __UNCONST(h);
    169  1.1  christos 		h += p;
    170  1.1  christos 		mem = mem0;
    171  1.1  christos 	}
    172  1.1  christos }
    173  1.1  christos 
    174  1.1  christos void *memmem(const void *h0, size_t k, const void *n0, size_t l)
    175  1.1  christos {
    176  1.1  christos 	const unsigned char *h = h0, *n = n0;
    177  1.1  christos 
    178  1.1  christos 	/* Return immediately on empty needle */
    179  1.1  christos 	if (!l) return __UNCONST(h);
    180  1.1  christos 
    181  1.1  christos 	/* Return immediately when needle is longer than haystack */
    182  1.1  christos 	if (k<l) return 0;
    183  1.1  christos 
    184  1.1  christos 	/* Use faster algorithms for short needles */
    185  1.1  christos 	h = memchr(h0, *n, k);
    186  1.1  christos 	if (!h || l==1) return __UNCONST(h);
    187  1.1  christos 	k -= h - (const unsigned char *)h0;
    188  1.1  christos 	if (k<l) return 0;
    189  1.1  christos 	if (l==2) return twobyte_memmem(h, k, n);
    190  1.1  christos 	if (l==3) return threebyte_memmem(h, k, n);
    191  1.1  christos 	if (l==4) return fourbyte_memmem(h, k, n);
    192  1.1  christos 
    193  1.1  christos 	return twoway_memmem(h, h+k, n, l);
    194  1.1  christos }
    195