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