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