summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclaudio <>2020-04-16 12:39:28 +0000
committerclaudio <>2020-04-16 12:39:28 +0000
commit99252e97a3a44664558cb9dd91ea8da0aae2c719 (patch)
tree1c5fffdb42d7c5d18e2261eb49132cb8779b398a
parent6728bc88015cac4fc489f77e6be70489786696e4 (diff)
downloadopenbsd-99252e97a3a44664558cb9dd91ea8da0aae2c719.tar.gz
openbsd-99252e97a3a44664558cb9dd91ea8da0aae2c719.tar.bz2
openbsd-99252e97a3a44664558cb9dd91ea8da0aae2c719.zip
Replace the simple memmem() implementation with a version that is O(n)
based on code from musl and now similar to our strstr(). OK tb@ millert@
-rw-r--r--src/lib/libc/string/memmem.c214
1 files changed, 167 insertions, 47 deletions
diff --git a/src/lib/libc/string/memmem.c b/src/lib/libc/string/memmem.c
index 823443b08a..3b180b4a62 100644
--- a/src/lib/libc/string/memmem.c
+++ b/src/lib/libc/string/memmem.c
@@ -1,64 +1,184 @@
1/* $OpenBSD: memmem.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */ 1/* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */
2/*- 2
3 * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com> 3/*
4 * Copyright (c) 2005-2020 Rich Felker, et al.
4 * 5 *
5 * Redistribution and use in source and binary forms, with or without 6 * Permission is hereby granted, free of charge, to any person obtaining
6 * modification, are permitted provided that the following conditions 7 * a copy of this software and associated documentation files (the
7 * are met: 8 * "Software"), to deal in the Software without restriction, including
8 * 1. Redistributions of source code must retain the above copyright 9 * without limitation the rights to use, copy, modify, merge, publish,
9 * notice, this list of conditions and the following disclaimer. 10 * distribute, sublicense, and/or sell copies of the Software, and to
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * permit persons to whom the Software is furnished to do so, subject to
11 * notice, this list of conditions and the following disclaimer in the 12 * the following conditions:
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 * 13 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * The above copyright notice and this permission notice shall be
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * included in all copies or substantial portions of the Software.
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 *
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * SUCH DAMAGE.
28 */ 24 */
29 25
30#include <string.h> 26#include <string.h>
27#include <stdint.h>
28
29static char *
30twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
31{
32 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
33 for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
34 if (hw == nw) return (char *)h-2;
35 return hw == nw ? (char *)h-2 : 0;
36}
37
38static char *
39threebyte_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+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
44 if (hw == nw) return (char *)h-3;
45 return hw == nw ? (char *)h-3 : 0;
46}
47
48static char *
49fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
50{
51 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
52 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
53 for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
54 if (hw == nw) return (char *)h-4;
55 return hw == nw ? (char *)h-4 : 0;
56}
57
58#define MAX(a,b) ((a)>(b)?(a):(b))
59#define MIN(a,b) ((a)<(b)?(a):(b))
60
61#define BITOP(a,b,op) \
62 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
31 63
32/* 64/*
33 * Find the first occurrence of the byte string s in byte string l. 65 * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
66 * Journal of the ACM, 38(3):651-675, July 1991.
34 */ 67 */
35 68static char *
36void * 69twoway_memmem(const unsigned char *h, const unsigned char *z,
37memmem(const void *l, size_t l_len, const void *s, size_t s_len) 70 const unsigned char *n, size_t l)
38{ 71{
39 const char *cur, *last; 72 size_t i, ip, jp, k, p, ms, p0, mem, mem0;
40 const char *cl = l; 73 size_t byteset[32 / sizeof(size_t)] = { 0 };
41 const char *cs = s; 74 size_t shift[256];
75
76 /* Computing length of needle and fill shift table */
77 for (i=0; i<l; i++)
78 BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
79
80 /* Compute maximal suffix */
81 ip = -1; jp = 0; k = p = 1;
82 while (jp+k<l) {
83 if (n[ip+k] == n[jp+k]) {
84 if (k == p) {
85 jp += p;
86 k = 1;
87 } else k++;
88 } else if (n[ip+k] > n[jp+k]) {
89 jp += k;
90 k = 1;
91 p = jp - ip;
92 } else {
93 ip = jp++;
94 k = p = 1;
95 }
96 }
97 ms = ip;
98 p0 = p;
99
100 /* And with the opposite comparison */
101 ip = -1; jp = 0; k = p = 1;
102 while (jp+k<l) {
103 if (n[ip+k] == n[jp+k]) {
104 if (k == p) {
105 jp += p;
106 k = 1;
107 } else k++;
108 } else if (n[ip+k] < n[jp+k]) {
109 jp += k;
110 k = 1;
111 p = jp - ip;
112 } else {
113 ip = jp++;
114 k = p = 1;
115 }
116 }
117 if (ip+1 > ms+1) ms = ip;
118 else p = p0;
42 119
43 /* a zero length needle should just return the haystack */ 120 /* Periodic needle? */
44 if (s_len == 0) 121 if (memcmp(n, n+p, ms+1)) {
45 return (void *)cl; 122 mem0 = 0;
123 p = MAX(ms, l-ms-1) + 1;
124 } else mem0 = l-p;
125 mem = 0;
46 126
47 /* "s" must be smaller or equal to "l" */ 127 /* Search loop */
48 if (l_len < s_len) 128 for (;;) {
49 return NULL; 129 /* If remainder of haystack is shorter than needle, done */
130 if (z-h < l) return 0;
131
132 /* Check last byte first; advance by shift on mismatch */
133 if (BITOP(byteset, h[l-1], &)) {
134 k = l-shift[h[l-1]];
135 if (k) {
136 if (k < mem) k = mem;
137 h += k;
138 mem = 0;
139 continue;
140 }
141 } else {
142 h += l;
143 mem = 0;
144 continue;
145 }
146
147 /* Compare right half */
148 for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
149 if (k < l) {
150 h += k-ms;
151 mem = 0;
152 continue;
153 }
154 /* Compare left half */
155 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
156 if (k <= mem) return (char *)h;
157 h += p;
158 mem = mem0;
159 }
160}
161
162void *
163memmem(const void *h0, size_t k, const void *n0, size_t l)
164{
165 const unsigned char *h = h0, *n = n0;
50 166
51 /* special case where s_len == 1 */ 167 /* Return immediately on empty needle */
52 if (s_len == 1) 168 if (!l) return (void *)h;
53 return memchr(l, *cs, l_len);
54 169
55 /* the last position where its possible to find "s" in "l" */ 170 /* Return immediately when needle is longer than haystack */
56 last = cl + l_len - s_len; 171 if (k<l) return 0;
57 172
58 for (cur = cl; cur <= last; cur++) 173 /* Use faster algorithms for short needles */
59 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) 174 h = memchr(h0, *n, k);
60 return (void *)cur; 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);
61 181
62 return NULL; 182 return twoway_memmem(h, h+k, n, l);
63} 183}
64DEF_WEAK(memmem); 184DEF_WEAK(memmem);