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