1226035Sgabor/* $FreeBSD: stable/11/usr.bin/grep/regex/fastmatch.c 330449 2018-03-05 07:26:05Z eadler $ */
2226035Sgabor
3226035Sgabor/*-
4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5330449Seadler *
6226035Sgabor * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
7226035Sgabor * All rights reserved.
8226035Sgabor *
9226035Sgabor * Redistribution and use in source and binary forms, with or without
10226035Sgabor * modification, are permitted provided that the following conditions
11226035Sgabor * are met:
12226035Sgabor * 1. Redistributions of source code must retain the above copyright
13226035Sgabor *    notice, this list of conditions and the following disclaimer.
14226035Sgabor * 2. Redistributions in binary form must reproduce the above copyright
15226035Sgabor *    notice, this list of conditions and the following disclaimer in the
16226035Sgabor *    documentation and/or other materials provided with the distribution.
17226035Sgabor *
18226035Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19226035Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20226035Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21226035Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22226035Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23226035Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24226035Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25226035Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26226035Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27226035Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28226035Sgabor * SUCH DAMAGE.
29226035Sgabor */
30226035Sgabor
31226035Sgabor#include "glue.h"
32226035Sgabor
33226035Sgabor#include <errno.h>
34226035Sgabor#include <fastmatch.h>
35226035Sgabor#include <regex.h>
36226035Sgabor#include <string.h>
37226035Sgabor
38226035Sgabor#include "tre-fastmatch.h"
39226035Sgabor
40226035Sgaborint
41226035Sgabortre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
42226035Sgabor{
43226035Sgabor  int ret;
44226035Sgabor  tre_char_t *wregex;
45226035Sgabor  size_t wlen;
46226035Sgabor
47226035Sgabor  if (n != 0)
48226035Sgabor    {
49226035Sgabor      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
50226035Sgabor      if (ret != REG_OK)
51226035Sgabor	return ret;
52226035Sgabor      else
53226035Sgabor	ret = tre_compile_literal(preg, wregex, wlen, cflags);
54226035Sgabor      tre_free_pattern(wregex);
55226035Sgabor      return ret;
56226035Sgabor    }
57226035Sgabor  else
58226035Sgabor    return tre_compile_literal(preg, NULL, 0, cflags);
59226035Sgabor}
60226035Sgabor
61226035Sgaborint
62226035Sgabortre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
63226035Sgabor{
64226035Sgabor  int ret;
65226035Sgabor  tre_char_t *wregex;
66226035Sgabor  size_t wlen;
67226035Sgabor
68226035Sgabor  if (n != 0)
69226035Sgabor    {
70226035Sgabor      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
71226035Sgabor      if (ret != REG_OK)
72226035Sgabor	return ret;
73226035Sgabor      else
74226035Sgabor	ret = (cflags & REG_LITERAL)
75226035Sgabor	      ? tre_compile_literal(preg, wregex, wlen, cflags)
76226035Sgabor	      : tre_compile_fast(preg, wregex, wlen, cflags);
77226035Sgabor      tre_free_pattern(wregex);
78226035Sgabor      return ret;
79226035Sgabor    }
80226035Sgabor  else
81226035Sgabor    return tre_compile_literal(preg, NULL, 0, cflags);
82226035Sgabor}
83226035Sgabor
84226035Sgabor
85226035Sgaborint
86226035Sgabortre_fixcomp(fastmatch_t *preg, const char *regex, int cflags)
87226035Sgabor{
88226035Sgabor  return tre_fixncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
89226035Sgabor}
90226035Sgabor
91226035Sgaborint
92226035Sgabortre_fastcomp(fastmatch_t *preg, const char *regex, int cflags)
93226035Sgabor{
94226035Sgabor  return tre_fastncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
95226035Sgabor}
96226035Sgabor
97226035Sgaborint
98226035Sgabortre_fixwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
99226035Sgabor{
100226035Sgabor  return tre_compile_literal(preg, regex, n, cflags);
101226035Sgabor}
102226035Sgabor
103226035Sgaborint
104226035Sgabortre_fastwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
105226035Sgabor{
106226035Sgabor  return (cflags & REG_LITERAL) ?
107226035Sgabor    tre_compile_literal(preg, regex, n, cflags) :
108226035Sgabor    tre_compile_fast(preg, regex, n, cflags);
109226035Sgabor}
110226035Sgabor
111226035Sgaborint
112226035Sgabortre_fixwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
113226035Sgabor{
114226035Sgabor  return tre_fixwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
115226035Sgabor}
116226035Sgabor
117226035Sgaborint
118226035Sgabortre_fastwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
119226035Sgabor{
120226035Sgabor  return tre_fastwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
121226035Sgabor}
122226035Sgabor
123226035Sgaborvoid
124226035Sgabortre_fastfree(fastmatch_t *preg)
125226035Sgabor{
126226035Sgabor  tre_free_fast(preg);
127226035Sgabor}
128226035Sgabor
129226035Sgaborint
130226035Sgabortre_fastnexec(const fastmatch_t *preg, const char *string, size_t len,
131226035Sgabor         size_t nmatch, regmatch_t pmatch[], int eflags)
132226035Sgabor{
133226035Sgabor  tre_str_type_t type = (TRE_MB_CUR_MAX == 1) ? STR_BYTE : STR_MBS;
134226035Sgabor
135226035Sgabor  if (eflags & REG_STARTEND)
136226035Sgabor    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
137226035Sgabor		     type, nmatch, pmatch, eflags));
138226035Sgabor  else
139226035Sgabor    return tre_match_fast(preg, string, len, type, nmatch,
140226035Sgabor      pmatch, eflags);
141226035Sgabor}
142226035Sgabor
143226035Sgaborint
144226035Sgabortre_fastexec(const fastmatch_t *preg, const char *string, size_t nmatch,
145226035Sgabor	     regmatch_t pmatch[], int eflags)
146226035Sgabor{
147226035Sgabor  return tre_fastnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
148226035Sgabor}
149226035Sgabor
150226035Sgaborint
151226035Sgabortre_fastwnexec(const fastmatch_t *preg, const wchar_t *string, size_t len,
152226035Sgabor          size_t nmatch, regmatch_t pmatch[], int eflags)
153226035Sgabor{
154226035Sgabor  tre_str_type_t type = STR_WIDE;
155226035Sgabor
156226035Sgabor  if (eflags & REG_STARTEND)
157226035Sgabor    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
158226035Sgabor		     type, nmatch, pmatch, eflags));
159226035Sgabor  else
160226035Sgabor    return tre_match_fast(preg, string, len, type, nmatch,
161226035Sgabor      pmatch, eflags);
162226035Sgabor}
163226035Sgabor
164226035Sgaborint
165226035Sgabortre_fastwexec(const fastmatch_t *preg, const wchar_t *string,
166226035Sgabor         size_t nmatch, regmatch_t pmatch[], int eflags)
167226035Sgabor{
168226035Sgabor  return tre_fastwnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
169226035Sgabor}
170226035Sgabor
171