Blame


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