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