1 /* $FreeBSD: head/usr.bin/grep/regex/fastmatch.c 316492 2017-04-04 16:08:51Z cem $ */
2 
3 /*-
4  * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "glue.h"
30 
31 #include <errno.h>
32 #include <fastmatch.h>
33 #include <regex.h>
34 #include <string.h>
35 
36 #include "tre-fastmatch.h"
37 
38 int
tre_fixncomp(fastmatch_t * preg,const char * regex,size_t n,int cflags)39 tre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
40 {
41   int ret;
42   tre_char_t *wregex;
43   size_t wlen;
44 
45   if (n != 0)
46     {
47       ret = tre_convert_pattern(regex, n, &wregex, &wlen);
48       if (ret != REG_OK)
49 	return ret;
50       else
51 	ret = tre_compile_literal(preg, wregex, wlen, cflags);
52       tre_free_pattern(wregex);
53       return ret;
54     }
55   else
56     return tre_compile_literal(preg, NULL, 0, cflags);
57 }
58 
59 int
tre_fastncomp(fastmatch_t * preg,const char * regex,size_t n,int cflags)60 tre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
61 {
62   int ret;
63   tre_char_t *wregex;
64   size_t wlen;
65 
66   if (n != 0)
67     {
68       ret = tre_convert_pattern(regex, n, &wregex, &wlen);
69       if (ret != REG_OK)
70 	return ret;
71       else
72 	ret = (cflags & REG_LITERAL)
73 	      ? tre_compile_literal(preg, wregex, wlen, cflags)
74 	      : tre_compile_fast(preg, wregex, wlen, cflags);
75       tre_free_pattern(wregex);
76       return ret;
77     }
78   else
79     return tre_compile_literal(preg, NULL, 0, cflags);
80 }
81 
82 
83 int
tre_fixcomp(fastmatch_t * preg,const char * regex,int cflags)84 tre_fixcomp(fastmatch_t *preg, const char *regex, int cflags)
85 {
86   return tre_fixncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
87 }
88 
89 int
tre_fastcomp(fastmatch_t * preg,const char * regex,int cflags)90 tre_fastcomp(fastmatch_t *preg, const char *regex, int cflags)
91 {
92   return tre_fastncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
93 }
94 
95 int
tre_fixwncomp(fastmatch_t * preg,const wchar_t * regex,size_t n,int cflags)96 tre_fixwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
97 {
98   return tre_compile_literal(preg, regex, n, cflags);
99 }
100 
101 int
tre_fastwncomp(fastmatch_t * preg,const wchar_t * regex,size_t n,int cflags)102 tre_fastwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
103 {
104   return (cflags & REG_LITERAL) ?
105     tre_compile_literal(preg, regex, n, cflags) :
106     tre_compile_fast(preg, regex, n, cflags);
107 }
108 
109 int
tre_fixwcomp(fastmatch_t * preg,const wchar_t * regex,int cflags)110 tre_fixwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
111 {
112   return tre_fixwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
113 }
114 
115 int
tre_fastwcomp(fastmatch_t * preg,const wchar_t * regex,int cflags)116 tre_fastwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
117 {
118   return tre_fastwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
119 }
120 
121 void
tre_fastfree(fastmatch_t * preg)122 tre_fastfree(fastmatch_t *preg)
123 {
124   tre_free_fast(preg);
125 }
126 
127 int
tre_fastnexec(const fastmatch_t * preg,const char * string,size_t len,size_t nmatch,regmatch_t pmatch[],int eflags)128 tre_fastnexec(const fastmatch_t *preg, const char *string, size_t len,
129          size_t nmatch, regmatch_t pmatch[], int eflags)
130 {
131   tre_str_type_t type = (TRE_MB_CUR_MAX == 1) ? STR_BYTE : STR_MBS;
132 
133   if (eflags & REG_STARTEND)
134     CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
135 		     type, nmatch, pmatch, eflags));
136   else
137     return tre_match_fast(preg, string, len, type, nmatch,
138       pmatch, eflags);
139 }
140 
141 int
tre_fastexec(const fastmatch_t * preg,const char * string,size_t nmatch,regmatch_t pmatch[],int eflags)142 tre_fastexec(const fastmatch_t *preg, const char *string, size_t nmatch,
143 	     regmatch_t pmatch[], int eflags)
144 {
145   return tre_fastnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
146 }
147 
148 int
tre_fastwnexec(const fastmatch_t * preg,const wchar_t * string,size_t len,size_t nmatch,regmatch_t pmatch[],int eflags)149 tre_fastwnexec(const fastmatch_t *preg, const wchar_t *string, size_t len,
150           size_t nmatch, regmatch_t pmatch[], int eflags)
151 {
152   tre_str_type_t type = STR_WIDE;
153 
154   if (eflags & REG_STARTEND)
155     CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
156 		     type, nmatch, pmatch, eflags));
157   else
158     return tre_match_fast(preg, string, len, type, nmatch,
159       pmatch, eflags);
160 }
161 
162 int
tre_fastwexec(const fastmatch_t * preg,const wchar_t * string,size_t nmatch,regmatch_t pmatch[],int eflags)163 tre_fastwexec(const fastmatch_t *preg, const wchar_t *string,
164          size_t nmatch, regmatch_t pmatch[], int eflags)
165 {
166   return tre_fastwnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
167 }
168 
169