1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2005-2014 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 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 #include <string.h> 29 #include <stdint.h> 30 31 static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) 32 { 33 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; 34 for (h++; *h && hw != nw; hw = hw<<8 | *++h); 35 return *h ? (char *)h-1 : 0; 36 } 37 38 static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) 39 { 40 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 41 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 42 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); 43 return *h ? (char *)h-2 : 0; 44 } 45 46 static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) 47 { 48 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 49 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 50 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); 51 return *h ? (char *)h-3 : 0; 52 } 53 54 #define MAX(a,b) ((a)>(b)?(a):(b)) 55 #define MIN(a,b) ((a)<(b)?(a):(b)) 56 57 #define BITOP(a,b,op) \ 58 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) 59 60 /* 61 * Two Way string search algorithm, with a bad shift table applied to the last 62 * byte of the window. A bit array marks which entries in the shift table are 63 * initialized to avoid fully initializing a 1kb/2kb table. 64 * 65 * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, 66 * Journal of the ACM 38(3):651-675 67 */ 68 static char *twoway_strstr(const unsigned char *h, const unsigned char *n) 69 { 70 const unsigned char *z; 71 size_t l, ip, jp, k, p, ms, p0, mem, mem0; 72 size_t byteset[32 / sizeof(size_t)] = { 0 }; 73 size_t shift[256]; 74 75 /* Computing length of needle and fill shift table */ 76 for (l=0; n[l] && h[l]; l++) 77 BITOP(byteset, n[l], |=), shift[n[l]] = l+1; 78 if (n[l]) return 0; /* hit the end of h */ 79 80 /* Compute maximal suffix */ 81 ip = -1; jp = 0; k = p = 1; 82 while (jp+k<l) { 83 if (n[ip+k] == n[jp+k]) { 84 if (k == p) { 85 jp += p; 86 k = 1; 87 } else k++; 88 } else if (n[ip+k] > n[jp+k]) { 89 jp += k; 90 k = 1; 91 p = jp - ip; 92 } else { 93 ip = jp++; 94 k = p = 1; 95 } 96 } 97 ms = ip; 98 p0 = p; 99 100 /* And with the opposite comparison */ 101 ip = -1; jp = 0; k = p = 1; 102 while (jp+k<l) { 103 if (n[ip+k] == n[jp+k]) { 104 if (k == p) { 105 jp += p; 106 k = 1; 107 } else k++; 108 } else if (n[ip+k] < n[jp+k]) { 109 jp += k; 110 k = 1; 111 p = jp - ip; 112 } else { 113 ip = jp++; 114 k = p = 1; 115 } 116 } 117 if (ip+1 > ms+1) ms = ip; 118 else p = p0; 119 120 /* Periodic needle? */ 121 if (memcmp(n, n+p, ms+1)) { 122 mem0 = 0; 123 p = MAX(ms, l-ms-1) + 1; 124 } else mem0 = l-p; 125 mem = 0; 126 127 /* Initialize incremental end-of-haystack pointer */ 128 z = h; 129 130 /* Search loop */ 131 for (;;) { 132 /* Update incremental end-of-haystack pointer */ 133 if (z-h < l) { 134 /* Fast estimate for MIN(l,63) */ 135 size_t grow = l | 63; 136 const unsigned char *z2 = memchr(z, 0, grow); 137 if (z2) { 138 z = z2; 139 if (z-h < l) return 0; 140 } else z += grow; 141 } 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 //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l); 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); n[k] && n[k] == h[k]; k++); 161 if (n[k]) { 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 (char *)h; 169 h += p; 170 mem = mem0; 171 } 172 } 173 174 char *strstr(const char *h, const char *n) 175 { 176 /* Return immediately on empty needle */ 177 if (!n[0]) return (char *)h; 178 179 /* Use faster algorithms for short needles */ 180 h = strchr(h, *n); 181 if (!h || !n[1]) return (char *)h; 182 if (!h[1]) return 0; 183 if (!n[2]) return twobyte_strstr((void *)h, (void *)n); 184 if (!h[2]) return 0; 185 if (!n[3]) return threebyte_strstr((void *)h, (void *)n); 186 if (!h[3]) return 0; 187 if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); 188 189 return twoway_strstr((void *)h, (void *)n); 190 } 191