13aee54a1Schristos /*- 23aee54a1Schristos * Copyright (c) 2005-2014 Rich Felker, et al. 33aee54a1Schristos * 43aee54a1Schristos * Permission is hereby granted, free of charge, to any person obtaining 53aee54a1Schristos * a copy of this software and associated documentation files (the 63aee54a1Schristos * "Software"), to deal in the Software without restriction, including 73aee54a1Schristos * without limitation the rights to use, copy, modify, merge, publish, 83aee54a1Schristos * distribute, sublicense, and/or sell copies of the Software, and to 93aee54a1Schristos * permit persons to whom the Software is furnished to do so, subject to 103aee54a1Schristos * the following conditions: 113aee54a1Schristos * 123aee54a1Schristos * The above copyright notice and this permission notice shall be 133aee54a1Schristos * included in all copies or substantial portions of the Software. 143aee54a1Schristos * 153aee54a1Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 163aee54a1Schristos * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 173aee54a1Schristos * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 183aee54a1Schristos * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 193aee54a1Schristos * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 203aee54a1Schristos * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 213aee54a1Schristos * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 223aee54a1Schristos */ 233aee54a1Schristos #include <sys/cdefs.h> 243aee54a1Schristos #if defined(LIBC_SCCS) && !defined(lint) 253aee54a1Schristos #if 0 263aee54a1Schristos __FBSDID("$FreeBSD: head/lib/libc/string/memmem.c 315468 2017-03-18 00:53:24Z emaste $"); 273aee54a1Schristos #else 28*47247797Sriastradh __RCSID("$NetBSD: memmem.c,v 1.6 2024/12/14 16:48:13 riastradh Exp $"); 293aee54a1Schristos #endif 303aee54a1Schristos #endif /* LIBC_SCCS and not lint */ 313aee54a1Schristos 323aee54a1Schristos #if !defined(_KERNEL) && !defined(_STANDALONE) 333aee54a1Schristos #include <string.h> 343aee54a1Schristos #include <stdint.h> 353aee54a1Schristos #else 363aee54a1Schristos #include <lib/libkern/libkern.h> 373aee54a1Schristos #endif 383aee54a1Schristos 39124c8231Schristos static char *twobyte_memmem(const unsigned char *h, size_t k, 40124c8231Schristos const unsigned char *n) 413aee54a1Schristos { 423aee54a1Schristos uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; 431689d3d9Schristos for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++) 44124c8231Schristos if (hw == nw) return __UNCONST(h - 2); 45124c8231Schristos return hw == nw ? __UNCONST(h - 2) : 0; 463aee54a1Schristos } 473aee54a1Schristos 48124c8231Schristos static char *threebyte_memmem(const unsigned char *h, size_t k, 49124c8231Schristos const unsigned char *n) 503aee54a1Schristos { 51*47247797Sriastradh uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8; 52*47247797Sriastradh uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8; 531689d3d9Schristos for (h += 3, k -= 3; k; k--, hw = (hw|*h++) << 8) 54124c8231Schristos if (hw == nw) return __UNCONST(h - 3); 55124c8231Schristos return hw == nw ? __UNCONST(h - 3) : 0; 563aee54a1Schristos } 573aee54a1Schristos 58124c8231Schristos static char *fourbyte_memmem(const unsigned char *h, size_t k, 59124c8231Schristos const unsigned char *n) 603aee54a1Schristos { 61*47247797Sriastradh uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; 62*47247797Sriastradh uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; 631689d3d9Schristos for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++) 64124c8231Schristos if (hw == nw) return __UNCONST(h - 4); 65124c8231Schristos return hw == nw ? __UNCONST(h - 4) : 0; 663aee54a1Schristos } 673aee54a1Schristos 683aee54a1Schristos #define MAX(a,b) ((a)>(b)?(a):(b)) 693aee54a1Schristos #define MIN(a,b) ((a)<(b)?(a):(b)) 703aee54a1Schristos 713aee54a1Schristos #define BITOP(a,b,op) \ 723aee54a1Schristos ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) 733aee54a1Schristos 743aee54a1Schristos /* 753aee54a1Schristos * Two Way string search algorithm, with a bad shift table applied to the last 763aee54a1Schristos * byte of the window. A bit array marks which entries in the shift table are 773aee54a1Schristos * initialized to avoid fully initializing a 1kb/2kb table. 783aee54a1Schristos * 793aee54a1Schristos * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, 803aee54a1Schristos * Journal of the ACM 38(3):651-675 813aee54a1Schristos */ 823aee54a1Schristos static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) 833aee54a1Schristos { 843aee54a1Schristos size_t i, ip, jp, k, p, ms, p0, mem, mem0; 853aee54a1Schristos size_t byteset[32 / sizeof(size_t)] = { 0 }; 863aee54a1Schristos size_t shift[256]; 873aee54a1Schristos 883aee54a1Schristos /* Computing length of needle and fill shift table */ 893aee54a1Schristos for (i=0; i<l; i++) 903aee54a1Schristos BITOP(byteset, n[i], |=), shift[n[i]] = i+1; 913aee54a1Schristos 923aee54a1Schristos /* Compute maximal suffix */ 933aee54a1Schristos ip = (size_t)-1; jp = 0; k = p = 1; 943aee54a1Schristos while (jp+k<l) { 953aee54a1Schristos if (n[ip+k] == n[jp+k]) { 963aee54a1Schristos if (k == p) { 973aee54a1Schristos jp += p; 983aee54a1Schristos k = 1; 993aee54a1Schristos } else k++; 1003aee54a1Schristos } else if (n[ip+k] > n[jp+k]) { 1013aee54a1Schristos jp += k; 1023aee54a1Schristos k = 1; 1033aee54a1Schristos p = jp - ip; 1043aee54a1Schristos } else { 1053aee54a1Schristos ip = jp++; 1063aee54a1Schristos k = p = 1; 1073aee54a1Schristos } 1083aee54a1Schristos } 1093aee54a1Schristos ms = ip; 1103aee54a1Schristos p0 = p; 1113aee54a1Schristos 1123aee54a1Schristos /* And with the opposite comparison */ 1133aee54a1Schristos ip = (size_t)-1; jp = 0; k = p = 1; 1143aee54a1Schristos while (jp+k<l) { 1153aee54a1Schristos if (n[ip+k] == n[jp+k]) { 1163aee54a1Schristos if (k == p) { 1173aee54a1Schristos jp += p; 1183aee54a1Schristos k = 1; 1193aee54a1Schristos } else k++; 1203aee54a1Schristos } else if (n[ip+k] < n[jp+k]) { 1213aee54a1Schristos jp += k; 1223aee54a1Schristos k = 1; 1233aee54a1Schristos p = jp - ip; 1243aee54a1Schristos } else { 1253aee54a1Schristos ip = jp++; 1263aee54a1Schristos k = p = 1; 1273aee54a1Schristos } 1283aee54a1Schristos } 1293aee54a1Schristos if (ip+1 > ms+1) ms = ip; 1303aee54a1Schristos else p = p0; 1313aee54a1Schristos 1323aee54a1Schristos /* Periodic needle? */ 1333aee54a1Schristos if (memcmp(n, n+p, ms+1)) { 1343aee54a1Schristos mem0 = 0; 1353aee54a1Schristos p = MAX(ms, l-ms-1) + 1; 1363aee54a1Schristos } else mem0 = l-p; 1373aee54a1Schristos mem = 0; 1383aee54a1Schristos 1393aee54a1Schristos /* Search loop */ 1403aee54a1Schristos for (;;) { 1413aee54a1Schristos /* If remainder of haystack is shorter than needle, done */ 1423aee54a1Schristos if ((size_t)(z-h) < l) return 0; 1433aee54a1Schristos 1443aee54a1Schristos /* Check last byte first; advance by shift on mismatch */ 1453aee54a1Schristos if (BITOP(byteset, h[l-1], &)) { 1463aee54a1Schristos k = l-shift[h[l-1]]; 1473aee54a1Schristos if (k) { 1483aee54a1Schristos if (mem0 && mem && k < p) k = l-p; 1493aee54a1Schristos h += k; 1503aee54a1Schristos mem = 0; 1513aee54a1Schristos continue; 1523aee54a1Schristos } 1533aee54a1Schristos } else { 1543aee54a1Schristos h += l; 1553aee54a1Schristos mem = 0; 1563aee54a1Schristos continue; 1573aee54a1Schristos } 1583aee54a1Schristos 1593aee54a1Schristos /* Compare right half */ 1603aee54a1Schristos for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++); 1613aee54a1Schristos if (k < l) { 1623aee54a1Schristos h += k-ms; 1633aee54a1Schristos mem = 0; 1643aee54a1Schristos continue; 1653aee54a1Schristos } 1663aee54a1Schristos /* Compare left half */ 1673aee54a1Schristos for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); 1683aee54a1Schristos if (k <= mem) return __UNCONST(h); 1693aee54a1Schristos h += p; 1703aee54a1Schristos mem = mem0; 1713aee54a1Schristos } 1723aee54a1Schristos } 1733aee54a1Schristos 1743aee54a1Schristos void *memmem(const void *h0, size_t k, const void *n0, size_t l) 1753aee54a1Schristos { 1763aee54a1Schristos const unsigned char *h = h0, *n = n0; 1773aee54a1Schristos 1783aee54a1Schristos /* Return immediately on empty needle */ 1793aee54a1Schristos if (!l) return __UNCONST(h); 1803aee54a1Schristos 1813aee54a1Schristos /* Return immediately when needle is longer than haystack */ 1823aee54a1Schristos if (k<l) return 0; 1833aee54a1Schristos 1843aee54a1Schristos /* Use faster algorithms for short needles */ 1853aee54a1Schristos h = memchr(h0, *n, k); 1863aee54a1Schristos if (!h || l==1) return __UNCONST(h); 1873aee54a1Schristos k -= h - (const unsigned char *)h0; 1883aee54a1Schristos if (k<l) return 0; 1893aee54a1Schristos if (l==2) return twobyte_memmem(h, k, n); 1903aee54a1Schristos if (l==3) return threebyte_memmem(h, k, n); 1913aee54a1Schristos if (l==4) return fourbyte_memmem(h, k, n); 1923aee54a1Schristos 1933aee54a1Schristos return twoway_memmem(h, h+k, n, l); 1943aee54a1Schristos } 195