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