memmem.c revision 1.2 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2005-2014 Rich Felker, et al.
3 1.1 christos *
4 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining
5 1.1 christos * a copy of this software and associated documentation files (the
6 1.1 christos * "Software"), to deal in the Software without restriction, including
7 1.1 christos * without limitation the rights to use, copy, modify, merge, publish,
8 1.1 christos * distribute, sublicense, and/or sell copies of the Software, and to
9 1.1 christos * permit persons to whom the Software is furnished to do so, subject to
10 1.1 christos * the following conditions:
11 1.1 christos *
12 1.1 christos * The above copyright notice and this permission notice shall be
13 1.1 christos * included in all copies or substantial portions of the Software.
14 1.1 christos *
15 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 1.1 christos * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 1.1 christos * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 1.1 christos * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 1.1 christos * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 1.1 christos * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 1.1 christos * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 1.1 christos */
23 1.1 christos #include <sys/cdefs.h>
24 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
25 1.1 christos #if 0
26 1.1 christos __FBSDID("$FreeBSD: head/lib/libc/string/memmem.c 315468 2017-03-18 00:53:24Z emaste $");
27 1.1 christos #else
28 1.2 christos __RCSID("$NetBSD: memmem.c,v 1.2 2018/10/15 18:37:19 christos Exp $");
29 1.1 christos #endif
30 1.1 christos #endif /* LIBC_SCCS and not lint */
31 1.1 christos
32 1.1 christos #if !defined(_KERNEL) && !defined(_STANDALONE)
33 1.1 christos #include <string.h>
34 1.1 christos #include <stdint.h>
35 1.1 christos #else
36 1.1 christos #include <lib/libkern/libkern.h>
37 1.1 christos #endif
38 1.1 christos
39 1.2 christos static char *twobyte_memmem(const unsigned char *h, size_t k,
40 1.2 christos const unsigned char *n)
41 1.1 christos {
42 1.2 christos uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1];
43 1.2 christos for (h += 2, k -= 2; k; k--, hw = hw << 8 | *++h)
44 1.2 christos if (hw == nw) return __UNCONST(h - 2);
45 1.2 christos return hw == nw ? __UNCONST(h - 2) : 0;
46 1.1 christos }
47 1.1 christos
48 1.2 christos static char *threebyte_memmem(const unsigned char *h, size_t k,
49 1.2 christos const unsigned char *n)
50 1.1 christos {
51 1.2 christos uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
52 1.2 christos uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
53 1.2 christos for (h += 3, k -= 3; k; k--, hw = (hw|*++h) << 8)
54 1.2 christos if (hw == nw) return __UNCONST(h - 3);
55 1.2 christos return hw == nw ? __UNCONST(h - 3) : 0;
56 1.1 christos }
57 1.1 christos
58 1.2 christos static char *fourbyte_memmem(const unsigned char *h, size_t k,
59 1.2 christos const unsigned char *n)
60 1.1 christos {
61 1.2 christos uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
62 1.2 christos uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
63 1.2 christos for (h += 4, k -= 4; k; k--, hw = hw << 8 | *++h)
64 1.2 christos if (hw == nw) return __UNCONST(h - 4);
65 1.2 christos return hw == nw ? __UNCONST(h - 4) : 0;
66 1.1 christos return 0;
67 1.1 christos }
68 1.1 christos
69 1.1 christos #define MAX(a,b) ((a)>(b)?(a):(b))
70 1.1 christos #define MIN(a,b) ((a)<(b)?(a):(b))
71 1.1 christos
72 1.1 christos #define BITOP(a,b,op) \
73 1.1 christos ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
74 1.1 christos
75 1.1 christos /*
76 1.1 christos * Two Way string search algorithm, with a bad shift table applied to the last
77 1.1 christos * byte of the window. A bit array marks which entries in the shift table are
78 1.1 christos * initialized to avoid fully initializing a 1kb/2kb table.
79 1.1 christos *
80 1.1 christos * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
81 1.1 christos * Journal of the ACM 38(3):651-675
82 1.1 christos */
83 1.1 christos static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
84 1.1 christos {
85 1.1 christos size_t i, ip, jp, k, p, ms, p0, mem, mem0;
86 1.1 christos size_t byteset[32 / sizeof(size_t)] = { 0 };
87 1.1 christos size_t shift[256];
88 1.1 christos
89 1.1 christos /* Computing length of needle and fill shift table */
90 1.1 christos for (i=0; i<l; i++)
91 1.1 christos BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
92 1.1 christos
93 1.1 christos /* Compute maximal suffix */
94 1.1 christos ip = (size_t)-1; jp = 0; k = p = 1;
95 1.1 christos while (jp+k<l) {
96 1.1 christos if (n[ip+k] == n[jp+k]) {
97 1.1 christos if (k == p) {
98 1.1 christos jp += p;
99 1.1 christos k = 1;
100 1.1 christos } else k++;
101 1.1 christos } else if (n[ip+k] > n[jp+k]) {
102 1.1 christos jp += k;
103 1.1 christos k = 1;
104 1.1 christos p = jp - ip;
105 1.1 christos } else {
106 1.1 christos ip = jp++;
107 1.1 christos k = p = 1;
108 1.1 christos }
109 1.1 christos }
110 1.1 christos ms = ip;
111 1.1 christos p0 = p;
112 1.1 christos
113 1.1 christos /* And with the opposite comparison */
114 1.1 christos ip = (size_t)-1; jp = 0; k = p = 1;
115 1.1 christos while (jp+k<l) {
116 1.1 christos if (n[ip+k] == n[jp+k]) {
117 1.1 christos if (k == p) {
118 1.1 christos jp += p;
119 1.1 christos k = 1;
120 1.1 christos } else k++;
121 1.1 christos } else if (n[ip+k] < n[jp+k]) {
122 1.1 christos jp += k;
123 1.1 christos k = 1;
124 1.1 christos p = jp - ip;
125 1.1 christos } else {
126 1.1 christos ip = jp++;
127 1.1 christos k = p = 1;
128 1.1 christos }
129 1.1 christos }
130 1.1 christos if (ip+1 > ms+1) ms = ip;
131 1.1 christos else p = p0;
132 1.1 christos
133 1.1 christos /* Periodic needle? */
134 1.1 christos if (memcmp(n, n+p, ms+1)) {
135 1.1 christos mem0 = 0;
136 1.1 christos p = MAX(ms, l-ms-1) + 1;
137 1.1 christos } else mem0 = l-p;
138 1.1 christos mem = 0;
139 1.1 christos
140 1.1 christos /* Search loop */
141 1.1 christos for (;;) {
142 1.1 christos /* If remainder of haystack is shorter than needle, done */
143 1.1 christos if ((size_t)(z-h) < l) return 0;
144 1.1 christos
145 1.1 christos /* Check last byte first; advance by shift on mismatch */
146 1.1 christos if (BITOP(byteset, h[l-1], &)) {
147 1.1 christos k = l-shift[h[l-1]];
148 1.1 christos if (k) {
149 1.1 christos if (mem0 && mem && k < p) k = l-p;
150 1.1 christos h += k;
151 1.1 christos mem = 0;
152 1.1 christos continue;
153 1.1 christos }
154 1.1 christos } else {
155 1.1 christos h += l;
156 1.1 christos mem = 0;
157 1.1 christos continue;
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* Compare right half */
161 1.1 christos for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
162 1.1 christos if (k < l) {
163 1.1 christos h += k-ms;
164 1.1 christos mem = 0;
165 1.1 christos continue;
166 1.1 christos }
167 1.1 christos /* Compare left half */
168 1.1 christos for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
169 1.1 christos if (k <= mem) return __UNCONST(h);
170 1.1 christos h += p;
171 1.1 christos mem = mem0;
172 1.1 christos }
173 1.1 christos }
174 1.1 christos
175 1.1 christos void *memmem(const void *h0, size_t k, const void *n0, size_t l)
176 1.1 christos {
177 1.1 christos const unsigned char *h = h0, *n = n0;
178 1.1 christos
179 1.1 christos /* Return immediately on empty needle */
180 1.1 christos if (!l) return __UNCONST(h);
181 1.1 christos
182 1.1 christos /* Return immediately when needle is longer than haystack */
183 1.1 christos if (k<l) return 0;
184 1.1 christos
185 1.1 christos /* Use faster algorithms for short needles */
186 1.1 christos h = memchr(h0, *n, k);
187 1.1 christos if (!h || l==1) return __UNCONST(h);
188 1.1 christos k -= h - (const unsigned char *)h0;
189 1.1 christos if (k<l) return 0;
190 1.1 christos if (l==2) return twobyte_memmem(h, k, n);
191 1.1 christos if (l==3) return threebyte_memmem(h, k, n);
192 1.1 christos if (l==4) return fourbyte_memmem(h, k, n);
193 1.1 christos
194 1.1 christos return twoway_memmem(h, h+k, n, l);
195 1.1 christos }
196