Blame


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