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