fastgrep.c revision 1.1 1 1.1 joerg /* $OpenBSD: util.c,v 1.36 2007/10/02 17:59:18 otto Exp $ */
2 1.1 joerg /* $FreeBSD: head/usr.bin/grep/fastgrep.c 211496 2010-08-19 09:28:59Z des $ */
3 1.1 joerg
4 1.1 joerg /*-
5 1.1 joerg * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
6 1.1 joerg * Copyright (C) 2008 Gabor Kovesdan <gabor (at) FreeBSD.org>
7 1.1 joerg * All rights reserved.
8 1.1 joerg *
9 1.1 joerg * Redistribution and use in source and binary forms, with or without
10 1.1 joerg * modification, are permitted provided that the following conditions
11 1.1 joerg * are met:
12 1.1 joerg * 1. Redistributions of source code must retain the above copyright
13 1.1 joerg * notice, this list of conditions and the following disclaimer.
14 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 joerg * notice, this list of conditions and the following disclaimer in the
16 1.1 joerg * documentation and/or other materials provided with the distribution.
17 1.1 joerg *
18 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 joerg * SUCH DAMAGE.
29 1.1 joerg */
30 1.1 joerg
31 1.1 joerg /*
32 1.1 joerg * XXX: This file is a speed up for grep to cover the defects of the
33 1.1 joerg * regex library. These optimizations should practically be implemented
34 1.1 joerg * there keeping this code clean. This is a future TODO, but for the
35 1.1 joerg * meantime, we need to use this workaround.
36 1.1 joerg */
37 1.1 joerg
38 1.1 joerg #include <sys/cdefs.h>
39 1.1 joerg __RCSID("$NetBSD: fastgrep.c,v 1.1 2011/02/16 01:31:33 joerg Exp $");
40 1.1 joerg
41 1.1 joerg #include <limits.h>
42 1.1 joerg #include <stdbool.h>
43 1.1 joerg #include <stdlib.h>
44 1.1 joerg #include <string.h>
45 1.1 joerg #include <wchar.h>
46 1.1 joerg #include <wctype.h>
47 1.1 joerg
48 1.1 joerg #include "grep.h"
49 1.1 joerg
50 1.1 joerg static inline int grep_cmp(const unsigned char *, const unsigned char *, size_t);
51 1.1 joerg static inline void grep_revstr(unsigned char *, int);
52 1.1 joerg
53 1.1 joerg void
54 1.1 joerg fgrepcomp(fastgrep_t *fg, const char *pat)
55 1.1 joerg {
56 1.1 joerg unsigned int i;
57 1.1 joerg
58 1.1 joerg /* Initialize. */
59 1.1 joerg fg->len = strlen(pat);
60 1.1 joerg fg->bol = false;
61 1.1 joerg fg->eol = false;
62 1.1 joerg fg->reversed = false;
63 1.1 joerg
64 1.1 joerg fg->pattern = grep_malloc(strlen(pat) + 1);
65 1.1 joerg strcpy(fg->pattern, pat);
66 1.1 joerg
67 1.1 joerg /* Preprocess pattern. */
68 1.1 joerg for (i = 0; i <= UCHAR_MAX; i++)
69 1.1 joerg fg->qsBc[i] = fg->len;
70 1.1 joerg for (i = 1; i < fg->len; i++)
71 1.1 joerg fg->qsBc[fg->pattern[i]] = fg->len - i;
72 1.1 joerg }
73 1.1 joerg
74 1.1 joerg /*
75 1.1 joerg * Returns: -1 on failure, 0 on success
76 1.1 joerg */
77 1.1 joerg int
78 1.1 joerg fastcomp(fastgrep_t *fg, const char *pat)
79 1.1 joerg {
80 1.1 joerg unsigned int i;
81 1.1 joerg int firstHalfDot = -1;
82 1.1 joerg int firstLastHalfDot = -1;
83 1.1 joerg int hasDot = 0;
84 1.1 joerg int lastHalfDot = 0;
85 1.1 joerg int shiftPatternLen;
86 1.1 joerg bool bol = false;
87 1.1 joerg bool eol = false;
88 1.1 joerg
89 1.1 joerg /* Initialize. */
90 1.1 joerg fg->len = strlen(pat);
91 1.1 joerg fg->bol = false;
92 1.1 joerg fg->eol = false;
93 1.1 joerg fg->reversed = false;
94 1.1 joerg
95 1.1 joerg /* Remove end-of-line character ('$'). */
96 1.1 joerg if (fg->len > 0 && pat[fg->len - 1] == '$') {
97 1.1 joerg eol = true;
98 1.1 joerg fg->eol = true;
99 1.1 joerg fg->len--;
100 1.1 joerg }
101 1.1 joerg
102 1.1 joerg /* Remove beginning-of-line character ('^'). */
103 1.1 joerg if (pat[0] == '^') {
104 1.1 joerg bol = true;
105 1.1 joerg fg->bol = true;
106 1.1 joerg fg->len--;
107 1.1 joerg }
108 1.1 joerg
109 1.1 joerg if (fg->len >= 14 &&
110 1.1 joerg strncmp(pat + (fg->bol ? 1 : 0), "[[:<:]]", 7) == 0 &&
111 1.1 joerg strncmp(pat + (fg->bol ? 1 : 0) + fg->len - 7, "[[:>:]]", 7) == 0) {
112 1.1 joerg fg->len -= 14;
113 1.1 joerg /* Word boundary is handled separately in util.c */
114 1.1 joerg wflag = true;
115 1.1 joerg }
116 1.1 joerg
117 1.1 joerg /*
118 1.1 joerg * Copy pattern minus '^' and '$' characters as well as word
119 1.1 joerg * match character classes at the beginning and ending of the
120 1.1 joerg * string respectively.
121 1.1 joerg */
122 1.1 joerg fg->pattern = grep_malloc(fg->len + 1);
123 1.1 joerg strlcpy(fg->pattern, pat + (bol ? 1 : 0) + wflag, fg->len + 1);
124 1.1 joerg
125 1.1 joerg /* Look for ways to cheat...er...avoid the full regex engine. */
126 1.1 joerg for (i = 0; i < fg->len; i++) {
127 1.1 joerg /* Can still cheat? */
128 1.1 joerg if (fg->pattern[i] == '.') {
129 1.1 joerg hasDot = i;
130 1.1 joerg if (i < fg->len / 2) {
131 1.1 joerg if (firstHalfDot < 0)
132 1.1 joerg /* Closest dot to the beginning */
133 1.1 joerg firstHalfDot = i;
134 1.1 joerg } else {
135 1.1 joerg /* Closest dot to the end of the pattern. */
136 1.1 joerg lastHalfDot = i;
137 1.1 joerg if (firstLastHalfDot < 0)
138 1.1 joerg firstLastHalfDot = i;
139 1.1 joerg }
140 1.1 joerg } else {
141 1.1 joerg /* Free memory and let others know this is empty. */
142 1.1 joerg free(fg->pattern);
143 1.1 joerg fg->pattern = NULL;
144 1.1 joerg return (-1);
145 1.1 joerg }
146 1.1 joerg }
147 1.1 joerg
148 1.1 joerg /*
149 1.1 joerg * Determine if a reverse search would be faster based on the placement
150 1.1 joerg * of the dots.
151 1.1 joerg */
152 1.1 joerg if ((!(lflag || cflag)) && ((!(bol || eol)) &&
153 1.1 joerg ((lastHalfDot) && ((firstHalfDot < 0) ||
154 1.1 joerg ((fg->len - (lastHalfDot + 1)) < (size_t)firstHalfDot)))) &&
155 1.1 joerg !oflag && !color) {
156 1.1 joerg fg->reversed = true;
157 1.1 joerg hasDot = fg->len - (firstHalfDot < 0 ?
158 1.1 joerg firstLastHalfDot : firstHalfDot) - 1;
159 1.1 joerg grep_revstr(fg->pattern, fg->len);
160 1.1 joerg }
161 1.1 joerg
162 1.1 joerg /*
163 1.1 joerg * Normal Quick Search would require a shift based on the position the
164 1.1 joerg * next character after the comparison is within the pattern. With
165 1.1 joerg * wildcards, the position of the last dot effects the maximum shift
166 1.1 joerg * distance.
167 1.1 joerg * The closer to the end the wild card is the slower the search. A
168 1.1 joerg * reverse version of this algorithm would be useful for wildcards near
169 1.1 joerg * the end of the string.
170 1.1 joerg *
171 1.1 joerg * Examples:
172 1.1 joerg * Pattern Max shift
173 1.1 joerg * ------- ---------
174 1.1 joerg * this 5
175 1.1 joerg * .his 4
176 1.1 joerg * t.is 3
177 1.1 joerg * th.s 2
178 1.1 joerg * thi. 1
179 1.1 joerg */
180 1.1 joerg
181 1.1 joerg /* Adjust the shift based on location of the last dot ('.'). */
182 1.1 joerg shiftPatternLen = fg->len - hasDot;
183 1.1 joerg
184 1.1 joerg /* Preprocess pattern. */
185 1.1 joerg for (i = 0; i <= (signed)UCHAR_MAX; i++)
186 1.1 joerg fg->qsBc[i] = shiftPatternLen;
187 1.1 joerg for (i = hasDot + 1; i < fg->len; i++) {
188 1.1 joerg fg->qsBc[fg->pattern[i]] = fg->len - i;
189 1.1 joerg }
190 1.1 joerg
191 1.1 joerg /*
192 1.1 joerg * Put pattern back to normal after pre-processing to allow for easy
193 1.1 joerg * comparisons later.
194 1.1 joerg */
195 1.1 joerg if (fg->reversed)
196 1.1 joerg grep_revstr(fg->pattern, fg->len);
197 1.1 joerg
198 1.1 joerg return (0);
199 1.1 joerg }
200 1.1 joerg
201 1.1 joerg int
202 1.1 joerg grep_search(fastgrep_t *fg, const unsigned char *data, size_t len, regmatch_t *pmatch)
203 1.1 joerg {
204 1.1 joerg unsigned int j;
205 1.1 joerg int ret = REG_NOMATCH;
206 1.1 joerg
207 1.1 joerg if (pmatch->rm_so == (ssize_t)len)
208 1.1 joerg return (ret);
209 1.1 joerg
210 1.1 joerg if (fg->bol && pmatch->rm_so != 0) {
211 1.1 joerg pmatch->rm_so = len;
212 1.1 joerg pmatch->rm_eo = len;
213 1.1 joerg return (ret);
214 1.1 joerg }
215 1.1 joerg
216 1.1 joerg /* No point in going farther if we do not have enough data. */
217 1.1 joerg if (len < fg->len)
218 1.1 joerg return (ret);
219 1.1 joerg
220 1.1 joerg /* Only try once at the beginning or ending of the line. */
221 1.1 joerg if (fg->bol || fg->eol) {
222 1.1 joerg /* Simple text comparison. */
223 1.1 joerg /* Verify data is >= pattern length before searching on it. */
224 1.1 joerg if (len >= fg->len) {
225 1.1 joerg /* Determine where in data to start search at. */
226 1.1 joerg j = fg->eol ? len - fg->len : 0;
227 1.1 joerg if (!((fg->bol && fg->eol) && (len != fg->len)))
228 1.1 joerg if (grep_cmp(fg->pattern, data + j,
229 1.1 joerg fg->len) == -1) {
230 1.1 joerg pmatch->rm_so = j;
231 1.1 joerg pmatch->rm_eo = j + fg->len;
232 1.1 joerg ret = 0;
233 1.1 joerg }
234 1.1 joerg }
235 1.1 joerg } else if (fg->reversed) {
236 1.1 joerg /* Quick Search algorithm. */
237 1.1 joerg j = len;
238 1.1 joerg do {
239 1.1 joerg if (grep_cmp(fg->pattern, data + j - fg->len,
240 1.1 joerg fg->len) == -1) {
241 1.1 joerg pmatch->rm_so = j - fg->len;
242 1.1 joerg pmatch->rm_eo = j;
243 1.1 joerg ret = 0;
244 1.1 joerg break;
245 1.1 joerg }
246 1.1 joerg /* Shift if within bounds, otherwise, we are done. */
247 1.1 joerg if (j == fg->len)
248 1.1 joerg break;
249 1.1 joerg j -= fg->qsBc[data[j - fg->len - 1]];
250 1.1 joerg } while (j >= fg->len);
251 1.1 joerg } else {
252 1.1 joerg /* Quick Search algorithm. */
253 1.1 joerg j = pmatch->rm_so;
254 1.1 joerg do {
255 1.1 joerg if (grep_cmp(fg->pattern, data + j, fg->len) == -1) {
256 1.1 joerg pmatch->rm_so = j;
257 1.1 joerg pmatch->rm_eo = j + fg->len;
258 1.1 joerg ret = 0;
259 1.1 joerg break;
260 1.1 joerg }
261 1.1 joerg
262 1.1 joerg /* Shift if within bounds, otherwise, we are done. */
263 1.1 joerg if (j + fg->len == len)
264 1.1 joerg break;
265 1.1 joerg else
266 1.1 joerg j += fg->qsBc[data[j + fg->len]];
267 1.1 joerg } while (j <= (len - fg->len));
268 1.1 joerg }
269 1.1 joerg
270 1.1 joerg return (ret);
271 1.1 joerg }
272 1.1 joerg
273 1.1 joerg /*
274 1.1 joerg * Returns: i >= 0 on failure (position that it failed)
275 1.1 joerg * -1 on success
276 1.1 joerg */
277 1.1 joerg static inline int
278 1.1 joerg grep_cmp(const unsigned char *pat, const unsigned char *data, size_t len)
279 1.1 joerg {
280 1.1 joerg size_t size;
281 1.1 joerg wchar_t *wdata, *wpat;
282 1.1 joerg unsigned int i;
283 1.1 joerg
284 1.1 joerg if (iflag) {
285 1.1 joerg if ((size = mbstowcs(NULL, (const char *)data, 0)) ==
286 1.1 joerg ((size_t) - 1))
287 1.1 joerg return (-1);
288 1.1 joerg
289 1.1 joerg wdata = grep_malloc(size * sizeof(wint_t));
290 1.1 joerg
291 1.1 joerg if (mbstowcs(wdata, (const char *)data, size) ==
292 1.1 joerg ((size_t) - 1))
293 1.1 joerg return (-1);
294 1.1 joerg
295 1.1 joerg if ((size = mbstowcs(NULL, (const char *)pat, 0)) ==
296 1.1 joerg ((size_t) - 1))
297 1.1 joerg return (-1);
298 1.1 joerg
299 1.1 joerg wpat = grep_malloc(size * sizeof(wint_t));
300 1.1 joerg
301 1.1 joerg if (mbstowcs(wpat, (const char *)pat, size) == ((size_t) - 1))
302 1.1 joerg return (-1);
303 1.1 joerg for (i = 0; i < len; i++) {
304 1.1 joerg if ((towlower(wpat[i]) == towlower(wdata[i])) ||
305 1.1 joerg ((grepbehave != GREP_FIXED) && wpat[i] == L'.'))
306 1.1 joerg continue;
307 1.1 joerg free(wpat);
308 1.1 joerg free(wdata);
309 1.1 joerg return (i);
310 1.1 joerg }
311 1.1 joerg } else {
312 1.1 joerg for (i = 0; i < len; i++) {
313 1.1 joerg if ((pat[i] == data[i]) || ((grepbehave != GREP_FIXED) &&
314 1.1 joerg pat[i] == '.'))
315 1.1 joerg continue;
316 1.1 joerg return (i);
317 1.1 joerg }
318 1.1 joerg }
319 1.1 joerg return (-1);
320 1.1 joerg }
321 1.1 joerg
322 1.1 joerg static inline void
323 1.1 joerg grep_revstr(unsigned char *str, int len)
324 1.1 joerg {
325 1.1 joerg int i;
326 1.1 joerg char c;
327 1.1 joerg
328 1.1 joerg for (i = 0; i < len / 2; i++) {
329 1.1 joerg c = str[i];
330 1.1 joerg str[i] = str[len - i - 1];
331 1.1 joerg str[len - i - 1] = c;
332 1.1 joerg }
333 1.1 joerg }
334