• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 #include <string.h>
3 #include <stdint.h>
4 
twobyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)5 static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
6 {
7 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
8 	for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
9 		if (hw == nw) return (char *)h-2;
10 	return hw == nw ? (char *)h-2 : 0;
11 }
12 
threebyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)13 static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
14 {
15 	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8;
16 	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8;
17 	for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
18 		if (hw == nw) return (char *)h-3;
19 	return hw == nw ? (char *)h-3 : 0;
20 }
21 
fourbyte_memmem(const unsigned char * h,size_t k,const unsigned char * n)22 static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
23 {
24 	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
25 	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
26 	for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
27 		if (hw == nw) return (char *)h-4;
28 	return hw == nw ? (char *)h-4 : 0;
29 }
30 
31 #define MAX(a,b) ((a)>(b)?(a):(b))
32 #define MIN(a,b) ((a)<(b)?(a):(b))
33 
34 #define BITOP(a,b,op) \
35  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
36 
twoway_memmem(const unsigned char * h,const unsigned char * z,const unsigned char * n,size_t l)37 static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
38 {
39 	size_t i, ip, jp, k, p, ms, p0, mem, mem0;
40 	size_t byteset[32 / sizeof(size_t)] = { 0 };
41 	size_t shift[256];
42 
43 	/* Computing length of needle and fill shift table */
44 	for (i=0; i<l; i++)
45 		BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
46 
47 	/* Compute maximal suffix */
48 	ip = -1; jp = 0; k = p = 1;
49 	while (jp+k<l) {
50 		if (n[ip+k] == n[jp+k]) {
51 			if (k == p) {
52 				jp += p;
53 				k = 1;
54 			} else k++;
55 		} else if (n[ip+k] > n[jp+k]) {
56 			jp += k;
57 			k = 1;
58 			p = jp - ip;
59 		} else {
60 			ip = jp++;
61 			k = p = 1;
62 		}
63 	}
64 	ms = ip;
65 	p0 = p;
66 
67 	/* And with the opposite comparison */
68 	ip = -1; jp = 0; k = p = 1;
69 	while (jp+k<l) {
70 		if (n[ip+k] == n[jp+k]) {
71 			if (k == p) {
72 				jp += p;
73 				k = 1;
74 			} else k++;
75 		} else if (n[ip+k] < n[jp+k]) {
76 			jp += k;
77 			k = 1;
78 			p = jp - ip;
79 		} else {
80 			ip = jp++;
81 			k = p = 1;
82 		}
83 	}
84 	if (ip+1 > ms+1) ms = ip;
85 	else p = p0;
86 
87 	/* Periodic needle? */
88 	if (memcmp(n, n+p, ms+1)) {
89 		mem0 = 0;
90 		p = MAX(ms, l-ms-1) + 1;
91 	} else mem0 = l-p;
92 	mem = 0;
93 
94 	/* Search loop */
95 	for (;;) {
96 		/* If remainder of haystack is shorter than needle, done */
97 		if (z-h < l) return 0;
98 
99 		/* Check last byte first; advance by shift on mismatch */
100 		if (BITOP(byteset, h[l-1], &)) {
101 			k = l-shift[h[l-1]];
102 			if (k) {
103 				if (k < mem) k = mem;
104 				h += k;
105 				mem = 0;
106 				continue;
107 			}
108 		} else {
109 			h += l;
110 			mem = 0;
111 			continue;
112 		}
113 
114 		/* Compare right half */
115 		for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
116 		if (k < l) {
117 			h += k-ms;
118 			mem = 0;
119 			continue;
120 		}
121 		/* Compare left half */
122 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
123 		if (k <= mem) return (char *)h;
124 		h += p;
125 		mem = mem0;
126 	}
127 }
128 
memmem(const void * h0,size_t k,const void * n0,size_t l)129 void *memmem(const void *h0, size_t k, const void *n0, size_t l)
130 {
131 	const unsigned char *h = h0, *n = n0;
132 
133 	/* Return immediately on empty needle */
134 	if (!l) return (void *)h;
135 
136 	/* Return immediately when needle is longer than haystack */
137 	if (k<l) return 0;
138 
139 	/* Use faster algorithms for short needles */
140 	h = memchr(h0, *n, k);
141 	if (!h || l==1) return (void *)h;
142 	k -= h - (const unsigned char *)h0;
143 	if (k<l) return 0;
144 	if (l==2) return twobyte_memmem(h, k, n);
145 	if (l==3) return threebyte_memmem(h, k, n);
146 	if (l==4) return fourbyte_memmem(h, k, n);
147 
148 	return twoway_memmem(h, h+k, n, l);
149 }
150