fastgrep.c revision 1.3 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.3 joerg __RCSID("$NetBSD: fastgrep.c,v 1.3 2011/02/17 22:03:25 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.2 joerg fg->pattern = (unsigned char *)grep_strdup(pat);
65 1.1 joerg
66 1.1 joerg /* Preprocess pattern. */
67 1.1 joerg for (i = 0; i <= UCHAR_MAX; i++)
68 1.1 joerg fg->qsBc[i] = fg->len;
69 1.1 joerg for (i = 1; i < fg->len; i++)
70 1.1 joerg fg->qsBc[fg->pattern[i]] = fg->len - i;
71 1.1 joerg }
72 1.1 joerg
73 1.1 joerg /*
74 1.1 joerg * Returns: -1 on failure, 0 on success
75 1.1 joerg */
76 1.1 joerg int
77 1.1 joerg fastcomp(fastgrep_t *fg, const char *pat)
78 1.1 joerg {
79 1.1 joerg unsigned int i;
80 1.1 joerg int firstHalfDot = -1;
81 1.1 joerg int firstLastHalfDot = -1;
82 1.1 joerg int hasDot = 0;
83 1.1 joerg int lastHalfDot = 0;
84 1.1 joerg int shiftPatternLen;
85 1.1 joerg
86 1.1 joerg /* Initialize. */
87 1.1 joerg fg->len = strlen(pat);
88 1.1 joerg fg->bol = false;
89 1.1 joerg fg->eol = false;
90 1.1 joerg fg->reversed = false;
91 1.1 joerg
92 1.1 joerg /* Remove end-of-line character ('$'). */
93 1.1 joerg if (fg->len > 0 && pat[fg->len - 1] == '$') {
94 1.1 joerg fg->eol = true;
95 1.1 joerg fg->len--;
96 1.1 joerg }
97 1.1 joerg
98 1.1 joerg /* Remove beginning-of-line character ('^'). */
99 1.1 joerg if (pat[0] == '^') {
100 1.1 joerg fg->bol = true;
101 1.1 joerg fg->len--;
102 1.3 joerg pat++;
103 1.1 joerg }
104 1.1 joerg
105 1.1 joerg if (fg->len >= 14 &&
106 1.3 joerg memcmp(pat, "[[:<:]]", 7) == 0 &&
107 1.3 joerg memcmp(pat + fg->len - 7, "[[:>:]]", 7) == 0) {
108 1.1 joerg fg->len -= 14;
109 1.3 joerg pat += 7;
110 1.1 joerg /* Word boundary is handled separately in util.c */
111 1.1 joerg wflag = true;
112 1.1 joerg }
113 1.1 joerg
114 1.1 joerg /*
115 1.3 joerg * pat has been adjusted earlier to not include '^', '$' or
116 1.3 joerg * the word match character classes at the beginning and ending
117 1.3 joerg * of the string respectively.
118 1.1 joerg */
119 1.1 joerg fg->pattern = grep_malloc(fg->len + 1);
120 1.3 joerg memcpy(fg->pattern, pat, fg->len);
121 1.3 joerg fg->pattern[fg->len] = '\0';
122 1.1 joerg
123 1.1 joerg /* Look for ways to cheat...er...avoid the full regex engine. */
124 1.1 joerg for (i = 0; i < fg->len; i++) {
125 1.1 joerg /* Can still cheat? */
126 1.1 joerg if (fg->pattern[i] == '.') {
127 1.1 joerg hasDot = i;
128 1.1 joerg if (i < fg->len / 2) {
129 1.1 joerg if (firstHalfDot < 0)
130 1.1 joerg /* Closest dot to the beginning */
131 1.1 joerg firstHalfDot = i;
132 1.1 joerg } else {
133 1.1 joerg /* Closest dot to the end of the pattern. */
134 1.1 joerg lastHalfDot = i;
135 1.1 joerg if (firstLastHalfDot < 0)
136 1.1 joerg firstLastHalfDot = i;
137 1.1 joerg }
138 1.1 joerg } else {
139 1.1 joerg /* Free memory and let others know this is empty. */
140 1.1 joerg free(fg->pattern);
141 1.1 joerg fg->pattern = NULL;
142 1.1 joerg return (-1);
143 1.1 joerg }
144 1.1 joerg }
145 1.1 joerg
146 1.1 joerg /*
147 1.1 joerg * Determine if a reverse search would be faster based on the placement
148 1.1 joerg * of the dots.
149 1.1 joerg */
150 1.3 joerg if ((!(lflag || cflag)) && ((!(fg->bol || fg->eol)) &&
151 1.1 joerg ((lastHalfDot) && ((firstHalfDot < 0) ||
152 1.1 joerg ((fg->len - (lastHalfDot + 1)) < (size_t)firstHalfDot)))) &&
153 1.1 joerg !oflag && !color) {
154 1.1 joerg fg->reversed = true;
155 1.1 joerg hasDot = fg->len - (firstHalfDot < 0 ?
156 1.1 joerg firstLastHalfDot : firstHalfDot) - 1;
157 1.1 joerg grep_revstr(fg->pattern, fg->len);
158 1.1 joerg }
159 1.1 joerg
160 1.1 joerg /*
161 1.1 joerg * Normal Quick Search would require a shift based on the position the
162 1.1 joerg * next character after the comparison is within the pattern. With
163 1.1 joerg * wildcards, the position of the last dot effects the maximum shift
164 1.1 joerg * distance.
165 1.1 joerg * The closer to the end the wild card is the slower the search. A
166 1.1 joerg * reverse version of this algorithm would be useful for wildcards near
167 1.1 joerg * the end of the string.
168 1.1 joerg *
169 1.1 joerg * Examples:
170 1.1 joerg * Pattern Max shift
171 1.1 joerg * ------- ---------
172 1.1 joerg * this 5
173 1.1 joerg * .his 4
174 1.1 joerg * t.is 3
175 1.1 joerg * th.s 2
176 1.1 joerg * thi. 1
177 1.1 joerg */
178 1.1 joerg
179 1.1 joerg /* Adjust the shift based on location of the last dot ('.'). */
180 1.1 joerg shiftPatternLen = fg->len - hasDot;
181 1.1 joerg
182 1.1 joerg /* Preprocess pattern. */
183 1.1 joerg for (i = 0; i <= (signed)UCHAR_MAX; i++)
184 1.1 joerg fg->qsBc[i] = shiftPatternLen;
185 1.1 joerg for (i = hasDot + 1; i < fg->len; i++) {
186 1.1 joerg fg->qsBc[fg->pattern[i]] = fg->len - i;
187 1.1 joerg }
188 1.1 joerg
189 1.1 joerg /*
190 1.1 joerg * Put pattern back to normal after pre-processing to allow for easy
191 1.1 joerg * comparisons later.
192 1.1 joerg */
193 1.1 joerg if (fg->reversed)
194 1.1 joerg grep_revstr(fg->pattern, fg->len);
195 1.1 joerg
196 1.1 joerg return (0);
197 1.1 joerg }
198 1.1 joerg
199 1.1 joerg int
200 1.1 joerg grep_search(fastgrep_t *fg, const unsigned char *data, size_t len, regmatch_t *pmatch)
201 1.1 joerg {
202 1.1 joerg unsigned int j;
203 1.1 joerg int ret = REG_NOMATCH;
204 1.1 joerg
205 1.1 joerg if (pmatch->rm_so == (ssize_t)len)
206 1.1 joerg return (ret);
207 1.1 joerg
208 1.1 joerg if (fg->bol && pmatch->rm_so != 0) {
209 1.1 joerg pmatch->rm_so = len;
210 1.1 joerg pmatch->rm_eo = len;
211 1.1 joerg return (ret);
212 1.1 joerg }
213 1.1 joerg
214 1.1 joerg /* No point in going farther if we do not have enough data. */
215 1.1 joerg if (len < fg->len)
216 1.1 joerg return (ret);
217 1.1 joerg
218 1.1 joerg /* Only try once at the beginning or ending of the line. */
219 1.1 joerg if (fg->bol || fg->eol) {
220 1.1 joerg /* Simple text comparison. */
221 1.1 joerg /* Verify data is >= pattern length before searching on it. */
222 1.1 joerg if (len >= fg->len) {
223 1.1 joerg /* Determine where in data to start search at. */
224 1.1 joerg j = fg->eol ? len - fg->len : 0;
225 1.1 joerg if (!((fg->bol && fg->eol) && (len != fg->len)))
226 1.1 joerg if (grep_cmp(fg->pattern, data + j,
227 1.1 joerg fg->len) == -1) {
228 1.1 joerg pmatch->rm_so = j;
229 1.1 joerg pmatch->rm_eo = j + fg->len;
230 1.1 joerg ret = 0;
231 1.1 joerg }
232 1.1 joerg }
233 1.1 joerg } else if (fg->reversed) {
234 1.1 joerg /* Quick Search algorithm. */
235 1.1 joerg j = len;
236 1.1 joerg do {
237 1.1 joerg if (grep_cmp(fg->pattern, data + j - fg->len,
238 1.1 joerg fg->len) == -1) {
239 1.1 joerg pmatch->rm_so = j - fg->len;
240 1.1 joerg pmatch->rm_eo = j;
241 1.1 joerg ret = 0;
242 1.1 joerg break;
243 1.1 joerg }
244 1.1 joerg /* Shift if within bounds, otherwise, we are done. */
245 1.1 joerg if (j == fg->len)
246 1.1 joerg break;
247 1.1 joerg j -= fg->qsBc[data[j - fg->len - 1]];
248 1.1 joerg } while (j >= fg->len);
249 1.1 joerg } else {
250 1.1 joerg /* Quick Search algorithm. */
251 1.1 joerg j = pmatch->rm_so;
252 1.1 joerg do {
253 1.1 joerg if (grep_cmp(fg->pattern, data + j, fg->len) == -1) {
254 1.1 joerg pmatch->rm_so = j;
255 1.1 joerg pmatch->rm_eo = j + fg->len;
256 1.1 joerg ret = 0;
257 1.1 joerg break;
258 1.1 joerg }
259 1.1 joerg
260 1.1 joerg /* Shift if within bounds, otherwise, we are done. */
261 1.1 joerg if (j + fg->len == len)
262 1.1 joerg break;
263 1.1 joerg else
264 1.1 joerg j += fg->qsBc[data[j + fg->len]];
265 1.1 joerg } while (j <= (len - fg->len));
266 1.1 joerg }
267 1.1 joerg
268 1.1 joerg return (ret);
269 1.1 joerg }
270 1.1 joerg
271 1.1 joerg /*
272 1.1 joerg * Returns: i >= 0 on failure (position that it failed)
273 1.1 joerg * -1 on success
274 1.1 joerg */
275 1.1 joerg static inline int
276 1.1 joerg grep_cmp(const unsigned char *pat, const unsigned char *data, size_t len)
277 1.1 joerg {
278 1.1 joerg size_t size;
279 1.1 joerg wchar_t *wdata, *wpat;
280 1.1 joerg unsigned int i;
281 1.1 joerg
282 1.1 joerg if (iflag) {
283 1.1 joerg if ((size = mbstowcs(NULL, (const char *)data, 0)) ==
284 1.1 joerg ((size_t) - 1))
285 1.1 joerg return (-1);
286 1.1 joerg
287 1.1 joerg wdata = grep_malloc(size * sizeof(wint_t));
288 1.1 joerg
289 1.1 joerg if (mbstowcs(wdata, (const char *)data, size) ==
290 1.1 joerg ((size_t) - 1))
291 1.1 joerg return (-1);
292 1.1 joerg
293 1.1 joerg if ((size = mbstowcs(NULL, (const char *)pat, 0)) ==
294 1.1 joerg ((size_t) - 1))
295 1.1 joerg return (-1);
296 1.1 joerg
297 1.1 joerg wpat = grep_malloc(size * sizeof(wint_t));
298 1.1 joerg
299 1.1 joerg if (mbstowcs(wpat, (const char *)pat, size) == ((size_t) - 1))
300 1.1 joerg return (-1);
301 1.1 joerg for (i = 0; i < len; i++) {
302 1.1 joerg if ((towlower(wpat[i]) == towlower(wdata[i])) ||
303 1.1 joerg ((grepbehave != GREP_FIXED) && wpat[i] == L'.'))
304 1.1 joerg continue;
305 1.1 joerg free(wpat);
306 1.1 joerg free(wdata);
307 1.1 joerg return (i);
308 1.1 joerg }
309 1.1 joerg } else {
310 1.1 joerg for (i = 0; i < len; i++) {
311 1.1 joerg if ((pat[i] == data[i]) || ((grepbehave != GREP_FIXED) &&
312 1.1 joerg pat[i] == '.'))
313 1.1 joerg continue;
314 1.1 joerg return (i);
315 1.1 joerg }
316 1.1 joerg }
317 1.1 joerg return (-1);
318 1.1 joerg }
319 1.1 joerg
320 1.1 joerg static inline void
321 1.1 joerg grep_revstr(unsigned char *str, int len)
322 1.1 joerg {
323 1.1 joerg int i;
324 1.1 joerg char c;
325 1.1 joerg
326 1.1 joerg for (i = 0; i < len / 2; i++) {
327 1.1 joerg c = str[i];
328 1.1 joerg str[i] = str[len - i - 1];
329 1.1 joerg str[len - i - 1] = c;
330 1.1 joerg }
331 1.1 joerg }
332