158f0484fSRodney W. Grimes /*-
2*2e9bc9d1SEd Maste * SPDX-License-Identifier: MIT
38a16b7a1SPedro F. Giffuni *
488521634SEd Maste * Copyright (c) 2005-2014 Rich Felker, et al.
558f0484fSRodney W. Grimes *
688521634SEd Maste * Permission is hereby granted, free of charge, to any person obtaining
788521634SEd Maste * a copy of this software and associated documentation files (the
888521634SEd Maste * "Software"), to deal in the Software without restriction, including
988521634SEd Maste * without limitation the rights to use, copy, modify, merge, publish,
1088521634SEd Maste * distribute, sublicense, and/or sell copies of the Software, and to
1188521634SEd Maste * permit persons to whom the Software is furnished to do so, subject to
1288521634SEd Maste * the following conditions:
1358f0484fSRodney W. Grimes *
1488521634SEd Maste * The above copyright notice and this permission notice shall be
1588521634SEd Maste * included in all copies or substantial portions of the Software.
1658f0484fSRodney W. Grimes *
1788521634SEd Maste * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1888521634SEd Maste * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1988521634SEd Maste * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2088521634SEd Maste * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
2188521634SEd Maste * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2288521634SEd Maste * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2388521634SEd Maste * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2458f0484fSRodney W. Grimes */
2588521634SEd Maste #include <stdint.h>
264874ddfdSEd Maste #include <string.h>
2758f0484fSRodney W. Grimes
284874ddfdSEd Maste static char *
twobyte_strstr(const unsigned char * h,const unsigned char * n)294874ddfdSEd Maste twobyte_strstr(const unsigned char *h, const unsigned char *n)
3058f0484fSRodney W. Grimes {
3188521634SEd Maste uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
324874ddfdSEd Maste for (h++; *h && hw != nw; hw = hw << 8 | *++h)
334874ddfdSEd Maste ;
3488521634SEd Maste return *h ? (char *)h - 1 : 0;
3558f0484fSRodney W. Grimes }
3688521634SEd Maste
374874ddfdSEd Maste static char *
threebyte_strstr(const unsigned char * h,const unsigned char * n)384874ddfdSEd Maste threebyte_strstr(const unsigned char *h, const unsigned char *n)
3988521634SEd Maste {
4033482daeSEd Maste uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
4133482daeSEd Maste uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
424874ddfdSEd Maste for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8)
434874ddfdSEd Maste ;
4488521634SEd Maste return *h ? (char *)h - 2 : 0;
4588521634SEd Maste }
4688521634SEd Maste
474874ddfdSEd Maste static char *
fourbyte_strstr(const unsigned char * h,const unsigned char * n)484874ddfdSEd Maste fourbyte_strstr(const unsigned char *h, const unsigned char *n)
4988521634SEd Maste {
5033482daeSEd Maste uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
5133482daeSEd Maste uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
524874ddfdSEd Maste for (h += 3; *h && hw != nw; hw = hw << 8 | *++h)
534874ddfdSEd Maste ;
5488521634SEd Maste return *h ? (char *)h - 3 : 0;
5588521634SEd Maste }
5688521634SEd Maste
5788521634SEd Maste #define MAX(a, b) ((a) > (b) ? (a) : (b))
5888521634SEd Maste #define MIN(a, b) ((a) < (b) ? (a) : (b))
5988521634SEd Maste
6088521634SEd Maste #define BITOP(a, b, op) \
614874ddfdSEd Maste ((a)[(size_t)(b) / (8 * sizeof *(a))] op \
624874ddfdSEd Maste (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
6388521634SEd Maste
6401dc206bSEd Maste /*
6501dc206bSEd Maste * Two Way string search algorithm, with a bad shift table applied to the last
6601dc206bSEd Maste * byte of the window. A bit array marks which entries in the shift table are
6701dc206bSEd Maste * initialized to avoid fully initializing a 1kb/2kb table.
6801dc206bSEd Maste *
6901dc206bSEd Maste * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
7001dc206bSEd Maste * Journal of the ACM 38(3):651-675
7101dc206bSEd Maste */
724874ddfdSEd Maste static char *
twoway_strstr(const unsigned char * h,const unsigned char * n)734874ddfdSEd Maste twoway_strstr(const unsigned char *h, const unsigned char *n)
7488521634SEd Maste {
7588521634SEd Maste const unsigned char *z;
7688521634SEd Maste size_t l, ip, jp, k, p, ms, p0, mem, mem0;
7788521634SEd Maste size_t byteset[32 / sizeof(size_t)] = { 0 };
7888521634SEd Maste size_t shift[256];
7988521634SEd Maste
8088521634SEd Maste /* Computing length of needle and fill shift table */
8188521634SEd Maste for (l = 0; n[l] && h[l]; l++)
8288521634SEd Maste BITOP(byteset, n[l], |=), shift[n[l]] = l + 1;
834874ddfdSEd Maste if (n[l])
844874ddfdSEd Maste return 0; /* hit the end of h */
8588521634SEd Maste
8688521634SEd Maste /* Compute maximal suffix */
874874ddfdSEd Maste ip = -1;
884874ddfdSEd Maste jp = 0;
894874ddfdSEd Maste k = p = 1;
9088521634SEd Maste while (jp + k < l) {
9188521634SEd Maste if (n[ip + k] == n[jp + k]) {
9288521634SEd Maste if (k == p) {
9388521634SEd Maste jp += p;
9488521634SEd Maste k = 1;
954874ddfdSEd Maste } else
964874ddfdSEd Maste k++;
9788521634SEd Maste } else if (n[ip + k] > n[jp + k]) {
9888521634SEd Maste jp += k;
9988521634SEd Maste k = 1;
10088521634SEd Maste p = jp - ip;
10188521634SEd Maste } else {
10288521634SEd Maste ip = jp++;
10388521634SEd Maste k = p = 1;
10488521634SEd Maste }
10588521634SEd Maste }
10688521634SEd Maste ms = ip;
10788521634SEd Maste p0 = p;
10888521634SEd Maste
10988521634SEd Maste /* And with the opposite comparison */
1104874ddfdSEd Maste ip = -1;
1114874ddfdSEd Maste jp = 0;
1124874ddfdSEd Maste k = p = 1;
11388521634SEd Maste while (jp + k < l) {
11488521634SEd Maste if (n[ip + k] == n[jp + k]) {
11588521634SEd Maste if (k == p) {
11688521634SEd Maste jp += p;
11788521634SEd Maste k = 1;
1184874ddfdSEd Maste } else
1194874ddfdSEd Maste k++;
12088521634SEd Maste } else if (n[ip + k] < n[jp + k]) {
12188521634SEd Maste jp += k;
12288521634SEd Maste k = 1;
12388521634SEd Maste p = jp - ip;
12488521634SEd Maste } else {
12588521634SEd Maste ip = jp++;
12688521634SEd Maste k = p = 1;
12788521634SEd Maste }
12888521634SEd Maste }
1294874ddfdSEd Maste if (ip + 1 > ms + 1)
1304874ddfdSEd Maste ms = ip;
1314874ddfdSEd Maste else
1324874ddfdSEd Maste p = p0;
13388521634SEd Maste
13488521634SEd Maste /* Periodic needle? */
13588521634SEd Maste if (memcmp(n, n + p, ms + 1)) {
13688521634SEd Maste mem0 = 0;
13788521634SEd Maste p = MAX(ms, l - ms - 1) + 1;
1384874ddfdSEd Maste } else
1394874ddfdSEd Maste mem0 = l - p;
14088521634SEd Maste mem = 0;
14188521634SEd Maste
14288521634SEd Maste /* Initialize incremental end-of-haystack pointer */
14388521634SEd Maste z = h;
14488521634SEd Maste
14588521634SEd Maste /* Search loop */
14688521634SEd Maste for (;;) {
14788521634SEd Maste /* Update incremental end-of-haystack pointer */
14888521634SEd Maste if (z - h < l) {
149c6750f07SEd Maste /* Fast estimate for MAX(l,63) */
15088521634SEd Maste size_t grow = l | 63;
15188521634SEd Maste const unsigned char *z2 = memchr(z, 0, grow);
15288521634SEd Maste if (z2) {
15388521634SEd Maste z = z2;
1544874ddfdSEd Maste if (z - h < l)
1554874ddfdSEd Maste return 0;
1564874ddfdSEd Maste } else
1574874ddfdSEd Maste z += grow;
15888521634SEd Maste }
15988521634SEd Maste
16088521634SEd Maste /* Check last byte first; advance by shift on mismatch */
16188521634SEd Maste if (BITOP(byteset, h[l - 1], &)) {
16288521634SEd Maste k = l - shift[h[l - 1]];
16388521634SEd Maste if (k) {
1644874ddfdSEd Maste if (k < mem)
1654874ddfdSEd Maste k = mem;
16688521634SEd Maste h += k;
16788521634SEd Maste mem = 0;
16888521634SEd Maste continue;
16988521634SEd Maste }
17088521634SEd Maste } else {
17188521634SEd Maste h += l;
17288521634SEd Maste mem = 0;
17388521634SEd Maste continue;
17488521634SEd Maste }
17588521634SEd Maste
17688521634SEd Maste /* Compare right half */
1774874ddfdSEd Maste for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++)
1784874ddfdSEd Maste ;
17988521634SEd Maste if (n[k]) {
18088521634SEd Maste h += k - ms;
18188521634SEd Maste mem = 0;
18288521634SEd Maste continue;
18388521634SEd Maste }
18488521634SEd Maste /* Compare left half */
1854874ddfdSEd Maste for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
1864874ddfdSEd Maste ;
1874874ddfdSEd Maste if (k <= mem)
1884874ddfdSEd Maste return (char *)h;
18988521634SEd Maste h += p;
19088521634SEd Maste mem = mem0;
19188521634SEd Maste }
19288521634SEd Maste }
19388521634SEd Maste
1944874ddfdSEd Maste char *
strstr(const char * h,const char * n)1954874ddfdSEd Maste strstr(const char *h, const char *n)
19688521634SEd Maste {
19788521634SEd Maste /* Return immediately on empty needle */
1984874ddfdSEd Maste if (!n[0])
1994874ddfdSEd Maste return (char *)h;
20088521634SEd Maste
20188521634SEd Maste /* Use faster algorithms for short needles */
20288521634SEd Maste h = strchr(h, *n);
2034874ddfdSEd Maste if (!h || !n[1])
2044874ddfdSEd Maste return (char *)h;
2054874ddfdSEd Maste if (!h[1])
2064874ddfdSEd Maste return 0;
2074874ddfdSEd Maste if (!n[2])
2084874ddfdSEd Maste return twobyte_strstr((void *)h, (void *)n);
2094874ddfdSEd Maste if (!h[2])
2104874ddfdSEd Maste return 0;
2114874ddfdSEd Maste if (!n[3])
2124874ddfdSEd Maste return threebyte_strstr((void *)h, (void *)n);
2134874ddfdSEd Maste if (!h[3])
2144874ddfdSEd Maste return 0;
2154874ddfdSEd Maste if (!n[4])
2164874ddfdSEd Maste return fourbyte_strstr((void *)h, (void *)n);
21788521634SEd Maste
21888521634SEd Maste return twoway_strstr((void *)h, (void *)n);
21958f0484fSRodney W. Grimes }
220