Home | History | Annotate | Line # | Download | only in string
memmem.c revision 1.3.2.2
      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.3.2.2 2019/06/10 21:41:07 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,
     40     const unsigned char *n)
     41 {
     42 	uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
     43 	for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
     44 		if (hw == nw) return __UNCONST(h - 2);
     45 	return hw == nw ? __UNCONST(h - 2) : 0;
     46 }
     47 
     48 static char *threebyte_memmem(const unsigned char *h, size_t k,
     49     const unsigned char *n)
     50 {
     51 	uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
     52 	uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
     53 	for (h += 3, k -= 3; k; k--, hw = (hw|*h++) << 8)
     54 		if (hw == nw) return __UNCONST(h - 3);
     55 	return hw == nw ? __UNCONST(h - 3) : 0;
     56 }
     57 
     58 static char *fourbyte_memmem(const unsigned char *h, size_t k,
     59     const unsigned char *n)
     60 {
     61 	uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
     62 	uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
     63 	for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
     64 		if (hw == nw) return __UNCONST(h - 4);
     65 	return hw == nw ? __UNCONST(h - 4) : 0;
     66 	return 0;
     67 }
     68 
     69 #define MAX(a,b) ((a)>(b)?(a):(b))
     70 #define MIN(a,b) ((a)<(b)?(a):(b))
     71 
     72 #define BITOP(a,b,op) \
     73  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
     74 
     75 /*
     76  * Two Way string search algorithm, with a bad shift table applied to the last
     77  * byte of the window. A bit array marks which entries in the shift table are
     78  * initialized to avoid fully initializing a 1kb/2kb table.
     79  *
     80  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
     81  * Journal of the ACM 38(3):651-675
     82  */
     83 static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
     84 {
     85 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
     86 	size_t byteset[32 / sizeof(size_t)] = { 0 };
     87 	size_t shift[256];
     88 
     89 	/* Computing length of needle and fill shift table */
     90 	for (i=0; i<l; i++)
     91 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
     92 
     93 	/* Compute maximal suffix */
     94 	ip = (size_t)-1; jp = 0; k = p = 1;
     95 	while (jp+k<l) {
     96 		if (n[ip+k] == n[jp+k]) {
     97 			if (k == p) {
     98 				jp += p;
     99 				k = 1;
    100 			} else k++;
    101 		} else if (n[ip+k] > n[jp+k]) {
    102 			jp += k;
    103 			k = 1;
    104 			p = jp - ip;
    105 		} else {
    106 			ip = jp++;
    107 			k = p = 1;
    108 		}
    109 	}
    110 	ms = ip;
    111 	p0 = p;
    112 
    113 	/* And with the opposite comparison */
    114 	ip = (size_t)-1; jp = 0; k = p = 1;
    115 	while (jp+k<l) {
    116 		if (n[ip+k] == n[jp+k]) {
    117 			if (k == p) {
    118 				jp += p;
    119 				k = 1;
    120 			} else k++;
    121 		} else if (n[ip+k] < n[jp+k]) {
    122 			jp += k;
    123 			k = 1;
    124 			p = jp - ip;
    125 		} else {
    126 			ip = jp++;
    127 			k = p = 1;
    128 		}
    129 	}
    130 	if (ip+1 > ms+1) ms = ip;
    131 	else p = p0;
    132 
    133 	/* Periodic needle? */
    134 	if (memcmp(n, n+p, ms+1)) {
    135 		mem0 = 0;
    136 		p = MAX(ms, l-ms-1) + 1;
    137 	} else mem0 = l-p;
    138 	mem = 0;
    139 
    140 	/* Search loop */
    141 	for (;;) {
    142 		/* If remainder of haystack is shorter than needle, done */
    143 		if ((size_t)(z-h) < l) return 0;
    144 
    145 		/* Check last byte first; advance by shift on mismatch */
    146 		if (BITOP(byteset, h[l-1], &)) {
    147 			k = l-shift[h[l-1]];
    148 			if (k) {
    149 				if (mem0 && mem && k < p) k = l-p;
    150 				h += k;
    151 				mem = 0;
    152 				continue;
    153 			}
    154 		} else {
    155 			h += l;
    156 			mem = 0;
    157 			continue;
    158 		}
    159 
    160 		/* Compare right half */
    161 		for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
    162 		if (k < l) {
    163 			h += k-ms;
    164 			mem = 0;
    165 			continue;
    166 		}
    167 		/* Compare left half */
    168 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
    169 		if (k <= mem) return __UNCONST(h);
    170 		h += p;
    171 		mem = mem0;
    172 	}
    173 }
    174 
    175 void *memmem(const void *h0, size_t k, const void *n0, size_t l)
    176 {
    177 	const unsigned char *h = h0, *n = n0;
    178 
    179 	/* Return immediately on empty needle */
    180 	if (!l) return __UNCONST(h);
    181 
    182 	/* Return immediately when needle is longer than haystack */
    183 	if (k<l) return 0;
    184 
    185 	/* Use faster algorithms for short needles */
    186 	h = memchr(h0, *n, k);
    187 	if (!h || l==1) return __UNCONST(h);
    188 	k -= h - (const unsigned char *)h0;
    189 	if (k<l) return 0;
    190 	if (l==2) return twobyte_memmem(h, k, n);
    191 	if (l==3) return threebyte_memmem(h, k, n);
    192 	if (l==4) return fourbyte_memmem(h, k, n);
    193 
    194 	return twoway_memmem(h, h+k, n, l);
    195 }
    196