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