1/* Return the offset of one string within another.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _LIBC
20# include <config.h>
21#endif
22
23#include <string.h>
24
25#define RETURN_TYPE char *
26#define AVAILABLE(h, h_l, j, n_l) \
27 (((j) + (n_l) <= (h_l)) \
28 || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
29 (j) + (n_l) <= (h_l)))
30#include "str-two-way.h"
31
32#undef strstr
33
34#ifndef STRSTR
35#define STRSTR strstr
36#endif
37
38static inline char *
39strstr2 (const unsigned char *hs, const unsigned char *ne)
40{
41 uint32_t h1 = (ne[0] << 16) | ne[1];
42 uint32_t h2 = 0;
43 for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
44 h2 = (h2 << 16) | c;
45 return h1 == h2 ? (char *)hs - 2 : NULL;
46}
47
48static inline char *
49strstr3 (const unsigned char *hs, const unsigned char *ne)
50{
51 uint32_t h1 = ((uint32_t)ne[0] << 24) | (ne[1] << 16) | (ne[2] << 8);
52 uint32_t h2 = 0;
53 for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
54 h2 = (h2 | c) << 8;
55 return h1 == h2 ? (char *)hs - 3 : NULL;
56}
57
58/* Hash character pairs so a small shift table can be used. All bits of
59 p[0] are included, but not all bits from p[-1]. So if two equal hashes
60 match on p[-1], p[0] matches too. Hash collisions are harmless and result
61 in smaller shifts. */
62#define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
63
64/* Fast strstr algorithm with guaranteed linear-time performance.
65 Small needles up to size 3 use a dedicated linear search. Longer needles
66 up to size 256 use a novel modified Horspool algorithm. It hashes pairs
67 of characters to quickly skip past mismatches. The main search loop only
68 exits if the last 2 characters match, avoiding unnecessary calls to memcmp
69 and allowing for a larger skip if there is no match. A self-adapting
70 filtering check is used to quickly detect mismatches in long needles.
71 By limiting the needle length to 256, the shift table can be reduced to 8
72 bits per entry, lowering preprocessing overhead and minimizing cache effects.
73 The limit also implies worst-case performance is linear.
74 Needles larger than 256 characters use the linear-time Two-Way algorithm. */
75char *
76STRSTR (const char *haystack, const char *needle)
77{
78 const unsigned char *hs = (const unsigned char *) haystack;
79 const unsigned char *ne = (const unsigned char *) needle;
80
81 /* Handle short needle special cases first. */
82 if (ne[0] == '\0')
83 return (char *)hs;
84 hs = (const unsigned char *)strchr ((const char*)hs, ne[0]);
85 if (hs == NULL || ne[1] == '\0')
86 return (char*)hs;
87 if (ne[2] == '\0')
88 return strstr2 (hs, ne);
89 if (ne[3] == '\0')
90 return strstr3 (hs, ne);
91
92 /* Ensure haystack length is at least as long as needle length.
93 Since a match may occur early on in a huge haystack, use strnlen
94 and read ahead a few cachelines for improved performance. */
95 size_t ne_len = strlen ((const char*)ne);
96 size_t hs_len = __strnlen ((const char*)hs, ne_len | 512);
97 if (hs_len < ne_len)
98 return NULL;
99
100 /* Check whether we have a match. This improves performance since we
101 avoid initialization overheads. */
102 if (memcmp (hs, ne, ne_len) == 0)
103 return (char *) hs;
104
105 /* Use Two-Way algorithm for very long needles. */
106 if (__glibc_unlikely (ne_len > 256))
107 return two_way_long_needle (hs, hs_len, ne, ne_len);
108
109 const unsigned char *end = hs + hs_len - ne_len;
110 uint8_t shift[256];
111 size_t tmp, shift1;
112 size_t m1 = ne_len - 1;
113 size_t offset = 0;
114
115 /* Initialize bad character shift hash table. */
116 memset (shift, 0, sizeof (shift));
117 for (int i = 1; i < m1; i++)
118 shift[hash2 (ne + i)] = i;
119 /* Shift1 is the amount we can skip after matching the hash of the
120 needle end but not the full needle. */
121 shift1 = m1 - shift[hash2 (ne + m1)];
122 shift[hash2 (ne + m1)] = m1;
123
124 while (1)
125 {
126 if (__glibc_unlikely (hs > end))
127 {
128 end += __strnlen ((const char*)end + m1 + 1, 2048);
129 if (hs > end)
130 return NULL;
131 }
132
133 /* Skip past character pairs not in the needle. */
134 do
135 {
136 hs += m1;
137 tmp = shift[hash2 (hs)];
138 }
139 while (tmp == 0 && hs <= end);
140
141 /* If the match is not at the end of the needle, shift to the end
142 and continue until we match the hash of the needle end. */
143 hs -= tmp;
144 if (tmp < m1)
145 continue;
146
147 /* Hash of the last 2 characters matches. If the needle is long,
148 try to quickly filter out mismatches. */
149 if (m1 < 15 || memcmp (hs + offset, ne + offset, 8) == 0)
150 {
151 if (memcmp (hs, ne, m1) == 0)
152 return (void *) hs;
153
154 /* Adjust filter offset when it doesn't find the mismatch. */
155 offset = (offset >= 8 ? offset : m1) - 8;
156 }
157
158 /* Skip based on matching the hash of the needle end. */
159 hs += shift1;
160 }
161}
162libc_hidden_builtin_def (strstr)
163

source code of glibc/string/strstr.c