xref: /netbsd-src/lib/libc/string/bm.c (revision 1394f01b4a9e99092957ca5d824d67219565d9b5)
1 /*	$NetBSD: bm.c,v 1.5 1997/07/13 20:24:13 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Andrew Hume of AT&T Bell Laboratories.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)bm.c	8.7 (Berkeley) 6/21/94";
43 #else
44 __RCSID("$NetBSD: bm.c,v 1.5 1997/07/13 20:24:13 christos Exp $");
45 #endif
46 #endif /* LIBC_SCCS && not lint */
47 
48 #include <sys/types.h>
49 
50 #include <bm.h>
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 /*
56  * XXX
57  * The default frequency table starts at 99 and counts down.  The default
58  * table should probably be oriented toward text, and will necessarily be
59  * locale specific.  This one is for English.  It was derived from the
60  * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb
61  * of email and random text.  Change it if you can find something better.
62  */
63 static u_char const freq_def[256] = {
64 	 0,  0,  0,  0,  0,  0,  0,  0,
65 	 0, 77, 90,  0,  0,  0,  0,  0,
66 	 0,  0,  0,  0,  0,  0,  0,  0,
67 	 0,  0,  0,  0,  0,  0,  0,  0,
68 	99, 28, 42, 27, 16, 14, 20, 51,
69 	66, 65, 59, 24, 75, 76, 84, 56,
70 	72, 74, 64, 55, 54, 47, 41, 37,
71 	44, 61, 70, 43, 23, 53, 49, 22,
72 	33, 58, 40, 46, 45, 57, 60, 26,
73 	30, 63, 21, 12, 32, 50, 38, 39,
74 	34, 11, 48, 67, 62, 35, 15, 29,
75 	71, 18,  9, 17, 25, 13, 10, 52,
76 	36, 95, 78, 86, 87, 98, 82, 80,
77 	88, 94, 19, 68, 89, 83, 93, 96,
78 	81,  7, 91, 92, 97, 85, 69, 73,
79 	31, 79,  8,  5,  4,  6,  3,  0,
80 	 0,  0,  0,  0,  0,  0,  0,  0,
81 	 0,  0,  0,  0,  0,  0,  0,  0,
82 	 0,  0,  0,  0,  0,  0,  0,  0,
83 	 0,  0,  0,  0,  0,  0,  0,  0,
84 	 0,  0,  0,  0,  0,  0,  0,  0,
85 	 0,  0,  0,  0,  0,  0,  0,  0,
86 	 0,  0,  0,  0,  0,  0,  0,  0,
87 	 0,  0,  0,  0,  0,  0,  0,  0,
88 	 0,  0,  0,  0,  0,  0,  0,  0,
89 	 0,  0,  0,  0,  0,  0,  0,  0,
90 	 0,  0,  0,  0,  0,  0,  0,  0,
91 	 0,  0,  0,  0,  0,  0,  0,  0,
92 	 0,  0,  0,  0,  0,  0,  0,  0,
93 	 0,  0,  0,  0,  0,  0,  0,  0,
94 	 0,  0,  0,  0,  0,  0,  0,  0,
95 	 0,  0,  0,  0,  0,  0,  0,  0,
96 };
97 
98 bm_pat *
99 bm_comp(pb, len, freq)
100 	u_char const *pb;
101 	size_t len;
102 	u_char const *freq;
103 {
104 	register u_char const *pe, *p;
105 	register size_t *d, r;
106 	register int j;
107 	int sv_errno;
108 	bm_pat *pat;
109 
110 	if (len == 0) {
111 		errno = EINVAL;
112 		return (NULL);
113 	}
114 	if ((pat = malloc(sizeof(*pat))) == NULL)
115 		return (NULL);
116 	pat->pat = NULL;
117 	pat->delta = NULL;
118 
119 	pat->patlen = len;			/* copy pattern */
120 	if ((pat->pat = malloc(pat->patlen)) == NULL)
121 		goto mem;
122 	memcpy(pat->pat, pb, pat->patlen);
123 						/* get skip delta */
124 	if ((pat->delta = malloc(256 * sizeof(*d))) == NULL)
125 		goto mem;
126 	for (j = 0, d = pat->delta; j < 256; j++)
127 		d[j] = pat->patlen;
128 	for (pe = pb + pat->patlen - 1; pb <= pe; pb++)
129 		d[*pb] = pe - pb;
130 
131 	if (freq == NULL)			/* default freq table */
132 		freq = freq_def;
133 	r = 0;					/* get guard */
134 	for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++)
135 		if (freq[*pb] < freq[pat->pat[r]])
136 			r = pb - pat->pat;
137 	pat->rarec = pat->pat[r];
138 	pat->rareoff = r - (pat->patlen - 1);
139 
140 						/* get md2 shift */
141 	for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--)
142 		if (*p == *pe)
143 			break;
144 
145 	/* *p is first leftward reoccurrence of *pe */
146 	pat->md2 = pe - p;
147 	return (pat);
148 
149 mem:	sv_errno = errno;
150 	bm_free(pat);
151 	errno = sv_errno;
152 	return (NULL);
153 }
154 
155 void
156 bm_free(pat)
157 	bm_pat *pat;
158 {
159 	if (pat->pat != NULL)
160 		free(pat->pat);
161 	if (pat->delta != NULL)
162 		free(pat->delta);
163 	free(pat);
164 }
165 
166 u_char *
167 bm_exec(pat, base, n)
168 	bm_pat *pat;
169 	u_char *base;
170 	size_t n;
171 {
172 	register u_char *e, *ep, *p, *q, *s;
173 	register size_t *d0, k, md2, n1, ro;
174 	register int rc;
175 
176 	if (n == 0)
177 		return (NULL);
178 
179 	d0 = pat->delta;
180 	n1 = pat->patlen - 1;
181 	md2 = pat->md2;
182 	ro = pat->rareoff;
183 	rc = pat->rarec;
184 	ep = pat->pat + pat->patlen - 1;
185 	s = base + (pat->patlen - 1);
186 
187 	/* fast loop up to n - 3 * patlen */
188 	e = base + n - 3 * pat->patlen;
189 	while (s < e) {
190 		k = d0[*s];		/* ufast skip loop */
191 		while (k) {
192 			k = d0[*(s += k)];
193 			k = d0[*(s += k)];
194 		}
195 		if (s >= e)
196 			break;
197 		if (s[ro] != rc)	/* guard test */
198 			goto mismatch1;
199 					/* fwd match */
200 		for (p = pat->pat, q = s - n1; p < ep;)
201 			if (*q++ != *p++)
202 				goto mismatch1;
203 		return (s - n1);
204 
205 mismatch1:	s += md2;		/* md2 shift */
206 	}
207 
208 	/* slow loop up to end */
209 	e = base + n;
210 	while (s < e) {
211 		s += d0[*s];		/* step */
212 		if (s >= e)
213 			break;
214 		if (s[ro] != rc)	/* guard test */
215 			goto mismatch2;
216 					/* fwd match */
217 		for (p = pat->pat, q = s - n1; p <= ep;)
218 			if (*q++ != *p++)
219 				goto mismatch2;
220 		return (s - n1);
221 
222 mismatch2:	s += md2;		/* md2 shift */
223 	}
224 
225 	return (NULL);
226 }
227