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