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