1 19a8d9fe 2022-10-31 op /* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */
4 19a8d9fe 2022-10-31 op * Copyright (c) 2005-2020 Rich Felker, et al.
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:
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.
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.
26 2d0a2b21 2023-06-23 op #include "../config.h"
28 19a8d9fe 2022-10-31 op #include <string.h>
29 19a8d9fe 2022-10-31 op #include <stdint.h>
32 19a8d9fe 2022-10-31 op twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
41 19a8d9fe 2022-10-31 op threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
51 19a8d9fe 2022-10-31 op fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
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))
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))))
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.
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)
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];
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;
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]) {
90 19a8d9fe 2022-10-31 op } else if (n[ip+k] > n[jp+k]) {
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) {
110 19a8d9fe 2022-10-31 op } else if (n[ip+k] < n[jp+k]) {
119 19a8d9fe 2022-10-31 op if (ip+1 > ms+1) ms = ip;
122 19a8d9fe 2022-10-31 op /* Periodic needle? */
123 19a8d9fe 2022-10-31 op if (memcmp(n, n+p, ms+1)) {
125 19a8d9fe 2022-10-31 op p = MAX(ms, l-ms-1) + 1;
126 19a8d9fe 2022-10-31 op } else mem0 = l-p;
129 19a8d9fe 2022-10-31 op /* Search loop */
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;
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]];
138 19a8d9fe 2022-10-31 op if (k < mem) k = mem;
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++);
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;
165 19a8d9fe 2022-10-31 op memmem(const void *h0, size_t k, const void *n0, size_t l)
167 19a8d9fe 2022-10-31 op const unsigned char *h = h0, *n = n0;
169 19a8d9fe 2022-10-31 op /* Return immediately on empty needle */
170 19a8d9fe 2022-10-31 op if (!l) return (void *)h;
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;
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);
184 19a8d9fe 2022-10-31 op return twoway_memmem(h, h+k, n, l);