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