Blob


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