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 __FBSDID("$FreeBSD$"); 25 26 #include <string.h> 27 #include <stdint.h> 28 29 static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) 30 { 31 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; 32 for (h++; *h && hw != nw; hw = hw<<8 | *++h); 33 return *h ? (char *)h-1 : 0; 34 } 35 36 static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) 37 { 38 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 39 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 40 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); 41 return *h ? (char *)h-2 : 0; 42 } 43 44 static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) 45 { 46 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 47 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 48 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); 49 return *h ? (char *)h-3 : 0; 50 } 51 52 #define MAX(a,b) ((a)>(b)?(a):(b)) 53 #define MIN(a,b) ((a)<(b)?(a):(b)) 54 55 #define BITOP(a,b,op) \ 56 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) 57 58 static char *twoway_strstr(const unsigned char *h, const unsigned char *n) 59 { 60 const unsigned char *z; 61 size_t l, ip, jp, k, p, ms, p0, mem, mem0; 62 size_t byteset[32 / sizeof(size_t)] = { 0 }; 63 size_t shift[256]; 64 65 /* Computing length of needle and fill shift table */ 66 for (l=0; n[l] && h[l]; l++) 67 BITOP(byteset, n[l], |=), shift[n[l]] = l+1; 68 if (n[l]) return 0; /* hit the end of h */ 69 70 /* Compute maximal suffix */ 71 ip = -1; jp = 0; k = p = 1; 72 while (jp+k<l) { 73 if (n[ip+k] == n[jp+k]) { 74 if (k == p) { 75 jp += p; 76 k = 1; 77 } else k++; 78 } else if (n[ip+k] > n[jp+k]) { 79 jp += k; 80 k = 1; 81 p = jp - ip; 82 } else { 83 ip = jp++; 84 k = p = 1; 85 } 86 } 87 ms = ip; 88 p0 = p; 89 90 /* And with the opposite comparison */ 91 ip = -1; jp = 0; k = p = 1; 92 while (jp+k<l) { 93 if (n[ip+k] == n[jp+k]) { 94 if (k == p) { 95 jp += p; 96 k = 1; 97 } else k++; 98 } else if (n[ip+k] < n[jp+k]) { 99 jp += k; 100 k = 1; 101 p = jp - ip; 102 } else { 103 ip = jp++; 104 k = p = 1; 105 } 106 } 107 if (ip+1 > ms+1) ms = ip; 108 else p = p0; 109 110 /* Periodic needle? */ 111 if (memcmp(n, n+p, ms+1)) { 112 mem0 = 0; 113 p = MAX(ms, l-ms-1) + 1; 114 } else mem0 = l-p; 115 mem = 0; 116 117 /* Initialize incremental end-of-haystack pointer */ 118 z = h; 119 120 /* Search loop */ 121 for (;;) { 122 /* Update incremental end-of-haystack pointer */ 123 if (z-h < l) { 124 /* Fast estimate for MIN(l,63) */ 125 size_t grow = l | 63; 126 const unsigned char *z2 = memchr(z, 0, grow); 127 if (z2) { 128 z = z2; 129 if (z-h < l) return 0; 130 } else z += grow; 131 } 132 133 /* Check last byte first; advance by shift on mismatch */ 134 if (BITOP(byteset, h[l-1], &)) { 135 k = l-shift[h[l-1]]; 136 //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l); 137 if (k) { 138 if (mem0 && mem && k < p) k = l-p; 139 h += k; 140 mem = 0; 141 continue; 142 } 143 } else { 144 h += l; 145 mem = 0; 146 continue; 147 } 148 149 /* Compare right half */ 150 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); 151 if (n[k]) { 152 h += k-ms; 153 mem = 0; 154 continue; 155 } 156 /* Compare left half */ 157 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); 158 if (k <= mem) return (char *)h; 159 h += p; 160 mem = mem0; 161 } 162 } 163 164 char *strstr(const char *h, const char *n) 165 { 166 /* Return immediately on empty needle */ 167 if (!n[0]) return (char *)h; 168 169 /* Use faster algorithms for short needles */ 170 h = strchr(h, *n); 171 if (!h || !n[1]) return (char *)h; 172 if (!h[1]) return 0; 173 if (!n[2]) return twobyte_strstr((void *)h, (void *)n); 174 if (!h[2]) return 0; 175 if (!n[3]) return threebyte_strstr((void *)h, (void *)n); 176 if (!h[3]) return 0; 177 if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); 178 179 return twoway_strstr((void *)h, (void *)n); 180 } 181