]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/string/memmem.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / lib / libc / string / memmem.c
1 /*-
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright (c) 2005-2014 Rich Felker, et al.
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 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27
28 #include <stdint.h>
29 #include <string.h>
30
31 static char *
32 twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
33 {
34         uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
35         for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++)
36                 if (hw == nw)
37                         return (char *)h - 2;
38         return hw == nw ? (char *)h - 2 : 0;
39 }
40
41 static char *
42 threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
43 {
44         uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8;
45         uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8;
46         for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8)
47                 if (hw == nw)
48                         return (char *)h - 3;
49         return hw == nw ? (char *)h - 3 : 0;
50 }
51
52 static char *
53 fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
54 {
55         uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
56         uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
57         for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++)
58                 if (hw == nw)
59                         return (char *)h - 4;
60         return hw == nw ? (char *)h - 4 : 0;
61 }
62
63 #define MAX(a, b) ((a) > (b) ? (a) : (b))
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65
66 #define BITOP(a, b, op) \
67         ((a)[(size_t)(b) / (8 * sizeof *(a))] op \
68             (size_t)1 << ((size_t)(b) % (8 * sizeof *(a))))
69
70 /*
71  * Two Way string search algorithm, with a bad shift table applied to the last
72  * byte of the window. A bit array marks which entries in the shift table are
73  * initialized to avoid fully initializing a 1kb/2kb table.
74  *
75  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
76  * Journal of the ACM 38(3):651-675
77  */
78 static char *
79 twoway_memmem(const unsigned char *h, const unsigned char *z,
80     const unsigned char *n, size_t l)
81 {
82         size_t i, ip, jp, k, p, ms, p0, mem, mem0;
83         size_t byteset[32 / sizeof(size_t)] = { 0 };
84         size_t shift[256];
85
86         /* Computing length of needle and fill shift table */
87         for (i = 0; i < l; i++)
88                 BITOP(byteset, n[i], |=), shift[n[i]] = i + 1;
89
90         /* Compute maximal suffix */
91         ip = -1;
92         jp = 0;
93         k = p = 1;
94         while (jp + k < l) {
95                 if (n[ip + k] == n[jp + k]) {
96                         if (k == p) {
97                                 jp += p;
98                                 k = 1;
99                         } else
100                                 k++;
101                 } else if (n[ip + k] > n[jp + k]) {
102                         jp += k;
103                         k = 1;
104                         p = jp - ip;
105                 } else {
106                         ip = jp++;
107                         k = p = 1;
108                 }
109         }
110         ms = ip;
111         p0 = p;
112
113         /* And with the opposite comparison */
114         ip = -1;
115         jp = 0;
116         k = p = 1;
117         while (jp + k < l) {
118                 if (n[ip + k] == n[jp + k]) {
119                         if (k == p) {
120                                 jp += p;
121                                 k = 1;
122                         } else
123                                 k++;
124                 } else if (n[ip + k] < n[jp + k]) {
125                         jp += k;
126                         k = 1;
127                         p = jp - ip;
128                 } else {
129                         ip = jp++;
130                         k = p = 1;
131                 }
132         }
133         if (ip + 1 > ms + 1)
134                 ms = ip;
135         else
136                 p = p0;
137
138         /* Periodic needle? */
139         if (memcmp(n, n + p, ms + 1)) {
140                 mem0 = 0;
141                 p = MAX(ms, l - ms - 1) + 1;
142         } else
143                 mem0 = l - p;
144         mem = 0;
145
146         /* Search loop */
147         for (;;) {
148                 /* If remainder of haystack is shorter than needle, done */
149                 if (z - h < l)
150                         return 0;
151
152                 /* Check last byte first; advance by shift on mismatch */
153                 if (BITOP(byteset, h[l - 1], &)) {
154                         k = l - shift[h[l - 1]];
155                         if (k) {
156                                 if (k < mem)
157                                         k = mem;
158                                 h += k;
159                                 mem = 0;
160                                 continue;
161                         }
162                 } else {
163                         h += l;
164                         mem = 0;
165                         continue;
166                 }
167
168                 /* Compare right half */
169                 for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++)
170                         ;
171                 if (k < l) {
172                         h += k - ms;
173                         mem = 0;
174                         continue;
175                 }
176                 /* Compare left half */
177                 for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--)
178                         ;
179                 if (k <= mem)
180                         return (char *)h;
181                 h += p;
182                 mem = mem0;
183         }
184 }
185
186 void *
187 memmem(const void *h0, size_t k, const void *n0, size_t l)
188 {
189         const unsigned char *h = h0, *n = n0;
190
191         /* Return immediately on empty needle */
192         if (!l)
193                 return (void *)h;
194
195         /* Return immediately when needle is longer than haystack */
196         if (k < l)
197                 return 0;
198
199         /* Use faster algorithms for short needles */
200         h = memchr(h0, *n, k);
201         if (!h || l == 1)
202                 return (void *)h;
203         k -= h - (const unsigned char *)h0;
204         if (k < l)
205                 return 0;
206         if (l == 2)
207                 return twobyte_memmem(h, k, n);
208         if (l == 3)
209                 return threebyte_memmem(h, k, n);
210         if (l == 4)
211                 return fourbyte_memmem(h, k, n);
212
213         return twoway_memmem(h, h + k, n, l);
214 }