1 0d6fb46a 2022-01-09 op /* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */
4 0d6fb46a 2022-01-09 op * Copyright (c) 2005-2020 Rich Felker, et al.
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:
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.
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.
26 0d6fb46a 2022-01-09 op #include <string.h>
27 0d6fb46a 2022-01-09 op #include <stdint.h>
30 0d6fb46a 2022-01-09 op twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
39 0d6fb46a 2022-01-09 op threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
49 0d6fb46a 2022-01-09 op fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
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;
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))
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))))
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.
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)
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];
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;
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]) {
88 0d6fb46a 2022-01-09 op } else if (n[ip+k] > n[jp+k]) {
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) {
108 0d6fb46a 2022-01-09 op } else if (n[ip+k] < n[jp+k]) {
117 0d6fb46a 2022-01-09 op if (ip+1 > ms+1) ms = ip;
120 0d6fb46a 2022-01-09 op /* Periodic needle? */
121 0d6fb46a 2022-01-09 op if (memcmp(n, n+p, ms+1)) {
123 0d6fb46a 2022-01-09 op p = MAX(ms, l-ms-1) + 1;
124 0d6fb46a 2022-01-09 op } else mem0 = l-p;
127 0d6fb46a 2022-01-09 op /* Search loop */
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;
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]];
136 0d6fb46a 2022-01-09 op if (k < mem) k = mem;
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++);
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;
163 0d6fb46a 2022-01-09 op memmem(const void *h0, size_t k, const void *n0, size_t l)
165 0d6fb46a 2022-01-09 op const unsigned char *h = h0, *n = n0;
167 0d6fb46a 2022-01-09 op /* Return immediately on empty needle */
168 0d6fb46a 2022-01-09 op if (!l) return (void *)h;
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;
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);
182 0d6fb46a 2022-01-09 op return twoway_memmem(h, h+k, n, l);