1 /* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */ 2 3 /* 4 * Copyright (c) 2005-2020 Rich Felker, et al. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 /* OPENBSD ORIGINAL: lib/libc/string/memmem.c */ 27 28 #include "includes.h" 29 30 #ifndef HAVE_MEMMEM 31 32 #include <string.h> 33 #ifdef HAVE_STDINT_H 34 #include <stdint.h> 35 #endif 36 37 static char * 38 twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) 39 { 40 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; 41 for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++) 42 if (hw == nw) return (char *)h-2; 43 return hw == nw ? (char *)h-2 : 0; 44 } 45 46 static char * 47 threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) 48 { 49 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 50 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 51 for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8) 52 if (hw == nw) return (char *)h-3; 53 return hw == nw ? (char *)h-3 : 0; 54 } 55 56 static char * 57 fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) 58 { 59 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 60 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 61 for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++) 62 if (hw == nw) return (char *)h-4; 63 return hw == nw ? (char *)h-4 : 0; 64 } 65 66 #if 0 67 /* In -portable, defines.h ensures that these are already defined. */ 68 #define MAX(a,b) ((a)>(b)?(a):(b)) 69 #define MIN(a,b) ((a)<(b)?(a):(b)) 70 #endif 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 * Maxime Crochemore and Dominique Perrin, Two-way string-matching, 77 * Journal of the ACM, 38(3):651-675, July 1991. 78 */ 79 static char * 80 twoway_memmem(const unsigned char *h, const unsigned char *z, 81 const unsigned char *n, size_t l) 82 { 83 size_t i, ip, jp, k, p, ms, p0, mem, mem0; 84 size_t byteset[32 / sizeof(size_t)] = { 0 }; 85 size_t shift[256]; 86 87 /* Computing length of needle and fill shift table */ 88 for (i=0; i<l; i++) 89 BITOP(byteset, n[i], |=), shift[n[i]] = i+1; 90 91 /* Compute maximal suffix */ 92 ip = -1; jp = 0; k = p = 1; 93 while (jp+k<l) { 94 if (n[ip+k] == n[jp+k]) { 95 if (k == p) { 96 jp += p; 97 k = 1; 98 } else k++; 99 } else if (n[ip+k] > n[jp+k]) { 100 jp += k; 101 k = 1; 102 p = jp - ip; 103 } else { 104 ip = jp++; 105 k = p = 1; 106 } 107 } 108 ms = ip; 109 p0 = p; 110 111 /* And with the opposite comparison */ 112 ip = -1; jp = 0; k = p = 1; 113 while (jp+k<l) { 114 if (n[ip+k] == n[jp+k]) { 115 if (k == p) { 116 jp += p; 117 k = 1; 118 } else k++; 119 } else if (n[ip+k] < n[jp+k]) { 120 jp += k; 121 k = 1; 122 p = jp - ip; 123 } else { 124 ip = jp++; 125 k = p = 1; 126 } 127 } 128 if (ip+1 > ms+1) ms = ip; 129 else p = p0; 130 131 /* Periodic needle? */ 132 if (memcmp(n, n+p, ms+1)) { 133 mem0 = 0; 134 p = MAX(ms, l-ms-1) + 1; 135 } else mem0 = l-p; 136 mem = 0; 137 138 /* Search loop */ 139 for (;;) { 140 /* If remainder of haystack is shorter than needle, done */ 141 if (z-h < l) return 0; 142 143 /* Check last byte first; advance by shift on mismatch */ 144 if (BITOP(byteset, h[l-1], &)) { 145 k = l-shift[h[l-1]]; 146 if (k) { 147 if (k < mem) k = mem; 148 h += k; 149 mem = 0; 150 continue; 151 } 152 } else { 153 h += l; 154 mem = 0; 155 continue; 156 } 157 158 /* Compare right half */ 159 for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++); 160 if (k < l) { 161 h += k-ms; 162 mem = 0; 163 continue; 164 } 165 /* Compare left half */ 166 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); 167 if (k <= mem) return (char *)h; 168 h += p; 169 mem = mem0; 170 } 171 } 172 173 void * 174 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 (void *)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 (void *)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 DEF_WEAK(memmem); 196 #endif /* HAVE_MEMMEM */ 197