Blame


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