Codebase list fdm / 136bf1e
Use PCRE2, from ben at bvnf dot space; GitHub issue 111. Nicholas Marriott 1 year, 9 months ago
4 changed file(s) with 51 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
3838 fi
3939
4040 AC_ARG_ENABLE(
41 pcre,
42 AC_HELP_STRING(--enable-pcre, use PCRE),
43 found_pcre=$enable_pcre
41 pcre2,
42 AC_HELP_STRING(--enable-pcre2, use PCRE2),
43 found_pcre2=$enable_pcre2
4444 )
45 if test "x$found_pcre" = xyes; then
46 CPPFLAGS="$CPPFLAGS -DPCRE"
47 LIBS="$LIBS -lpcre"
45 if test "x$found_pcre2" = xyes; then
46 CPPFLAGS="$CPPFLAGS -DPCRE2"
47
48 AC_SEARCH_LIBS(
49 pcre2_compile_8,
50 [pcre2-8],
51 found_pcre=yes,
52 found_pcre=no
53 )
54 if test "x$found_pcre" = xno; then
55 AC_MSG_ERROR("libpcre2 not found")
56 fi
4857 fi
4958
5059 AC_CHECK_HEADERS(
4343 #include <tdb.h>
4444 #include <regex.h>
4545
46 #ifdef PCRE
47 #include <pcre.h>
46 #ifdef PCRE2
47 #define PCRE2_CODE_UNIT_WIDTH 8
48 #include <pcre2.h>
4849 #endif
4950
5051 #include <openssl/ssl.h>
290291 /* Regexp wrapper structs. */
291292 struct re {
292293 char *str;
293 #ifndef PCRE
294 #ifndef PCRE2
294295 regex_t re;
295296 #else
296 pcre *pcre;
297 pcre2_code *pcre2;
297298 #endif
298299 int flags;
299300 };
1515 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1616 */
1717
18 #ifdef PCRE
18 #ifdef PCRE2
1919
2020 #include <sys/types.h>
2121
22 #include <pcre.h>
22 #define PCRE2_CODE_UNIT_WIDTH 8
23
24 #include <pcre2.h>
2325 #include <string.h>
2426
2527 #include "fdm.h"
2729 int
2830 re_compile(struct re *re, const char *s, int flags, char **cause)
2931 {
30 const char *error;
31 int off;
32 char error[256];
33 PCRE2_SIZE off;
34 int errorcode;
3235
3336 if (s == NULL)
3437 fatalx("null regexp");
3740 return (0);
3841 re->flags = flags;
3942
40 flags = PCRE_MULTILINE;
43 flags = PCRE2_MULTILINE;
4144 if (re->flags & RE_IGNCASE)
42 flags |= PCRE_CASELESS;
45 flags |= PCRE2_CASELESS;
4346
44 if ((re->pcre = pcre_compile(s, flags, &error, &off, NULL)) == NULL) {
47 re->pcre2 = pcre2_compile(s, PCRE2_ZERO_TERMINATED, flags, &errorcode,
48 &off, NULL);
49 if (re->pcre2 == NULL) {
50 pcre2_get_error_message(errorcode, error, sizeof(error));
4551 *cause = xstrdup(error);
4652 return (-1);
4753 }
5965 re_block(struct re *re, const void *buf, size_t len, struct rmlist *rml,
6066 char **cause)
6167 {
62 int res, pm[NPMATCH * 3];
63 u_int i, j;
68 int res;
69 pcre2_match_data *pmd;
70 PCRE2_SIZE *ovector;
71 u_int i, j;
6472
6573 if (len > INT_MAX)
6674 fatalx("buffer too big");
7583 return (0);
7684 }
7785
78 res = pcre_exec(re->pcre, NULL, buf, len, 0, 0, pm, NPMATCH * 3);
79 if (res < 0 && res != PCRE_ERROR_NOMATCH) {
86 pmd = pcre2_match_data_create_from_pattern(re->pcre2, NULL);
87 res = pcre2_match(re->pcre2, buf, len, 0, 0, pmd, NULL);
88 if (res < 0 && res != PCRE2_ERROR_NOMATCH) {
8089 xasprintf(cause, "%s: regexec failed", re->str);
90 pcre2_match_data_free(pmd);
8191 return (-1);
8292 }
8393
8494 if (rml != NULL) {
85 for (i = 0; i < NPMATCH; i++) {
95 ovector = pcre2_get_ovector_pointer(pmd);
96 for (i = 0; i < res; i++) {
8697 j = i * 2;
87 if (pm[j + 1] <= pm[j])
98 if (ovector[j + 1] <= ovector[j])
8899 break;
89100 rml->list[i].valid = 1;
90 rml->list[i].so = pm[j];
91 rml->list[i].eo = pm[j + 1];
101 rml->list[i].so = ovector[j];
102 rml->list[i].eo = ovector[j + 1];
92103 }
93104 rml->valid = 1;
94105 }
95106
96 return (res != PCRE_ERROR_NOMATCH);
107 return (res != PCRE2_ERROR_NOMATCH);
97108 }
98109
99110 void
100111 re_free(struct re *re)
101112 {
102113 xfree(re->str);
103 pcre_free(re->pcre);
114 pcre2_code_free(re->pcre2);
104115 }
105116
106 #endif /* PCRE */
117 #endif /* PCRE2 */
1515 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1616 */
1717
18 #ifndef PCRE
18 #ifndef PCRE2
1919
2020 #include <sys/types.h>
2121
108108 regfree(&re->re);
109109 }
110110
111 #endif /* !PCRE */
111 #endif /* !PCRE2 */