1 From fea1a27f5fbef28243620fa66909d2d04c81e140 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Thu, 2 Nov 2023 21:16:07 +0100
4 Subject: [PATCH] Move to PCRE2 from PCRE
6 Move to PCRE2 as PCRE is EOL and won't receive any security updates
9 Convert each function to PCRE2 equivalent and update configure.ac to
12 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
14 configure.ac | 20 +++++++++---------
15 src/config.h.in | 4 ++--
16 src/sipgrep.c | 56 ++++++++++++++++++++++++++++++-------------------
17 3 files changed, 47 insertions(+), 33 deletions(-)
21 @@ -26,8 +26,8 @@ AC_ARG_ENABLE(ssl,
31 AC_MSG_CHECKING([whether to use ncurses])
32 @@ -169,15 +169,15 @@ AC_SUBST(PCAP_LIBS)
36 -dnl check for pcre library
37 +dnl check for pcre2 library
41 -AC_CHECKING([for pcre Library and Header files])
42 -AC_CHECK_HEADER([pcre.h], ,AC_MSG_ERROR([Could not find pcre headers !]))
43 -AC_CHECK_LIB([pcre], [pcre_compile], ,[AC_MSG_ERROR([libpcre required])])
44 -AC_DEFINE(USE_PCRE, 1, [Use PCRE library])
46 +# Checks for libpcre2
47 +AC_CHECKING([for pcre2 Library and Header files])
48 +AC_CHECK_HEADER([pcre2.h], ,AC_MSG_ERROR([Could not find pcre2 headers !]), [#define PCRE2_CODE_UNIT_WIDTH 8])
49 +AC_CHECK_LIB([pcre2-8], [pcre2_compile_8], ,[AC_MSG_ERROR([libpcre2 required])])
50 +AC_DEFINE(USE_PCRE2, 1, [Use PCRE2 library])
55 @@ -271,6 +271,6 @@ echo Ncurses support............. : $use
58 echo Build with REDIS............ : $useRedis
59 -echo Build with PCRE............. : $usePCRE
60 +echo Build with PCRE............. : $usePCRE2
66 /* Use NCURSES library */
69 -/* Use PCRE library */
71 +/* Use PCRE2 library */
74 /* Use REDIS library */
83 +#define PCRE2_CODE_UNIT_WIDTH 8
87 #include "include/ipreasm.h"
88 @@ -149,17 +150,18 @@ struct statistics_table *statstable = NU
94 +PCRE2_UCHAR re_err[128];
95 +PCRE2_SIZE err_offset;
98 -pcre *pattern = NULL;
99 -pcre_extra *pattern_extra = NULL;
100 +pcre2_code *pattern = NULL;
106 -char *match_data = NULL, *bin_data = NULL;
107 +PCRE2_SPTR match_data = NULL;
108 +char *bin_data = NULL;
109 uint16_t match_len = 0;
110 int8_t (*match_func) () = &blank_match_func;
112 @@ -550,13 +552,13 @@ main (int argc, char **argv)
116 - uint32_t pcre_options = PCRE_UNGREEDY;
117 + uint32_t pcre2_options = PCRE2_UNGREEDY;
120 - pcre_options |= PCRE_CASELESS;
121 + pcre2_options |= PCRE2_CASELESS;
123 if (re_multiline_match)
124 - pcre_options |= PCRE_DOTALL;
125 + pcre2_options |= PCRE2_DOTALL;
128 char *word_regex = malloc (strlen (match_data) * 3 + strlen (WORD_REGEX));
129 @@ -564,14 +566,21 @@ main (int argc, char **argv)
130 match_data = word_regex;
133 - pattern = pcre_compile (match_data, pcre_options, (const char **) &re_err, &err_offset, 0);
134 + pattern = pcre2_compile (match_data, PCRE2_ZERO_TERMINATED, pcre2_options, &err_code, &err_offset, NULL);
137 + pcre2_get_error_message (err_code, re_err, 128);
138 fprintf (stderr, "compile failed: %s\n", re_err);
142 - pattern_extra = pcre_study (pattern, 0, (const char **) &re_err);
143 + err_code = pcre2_jit_compile (pattern, PCRE2_JIT_COMPLETE);
145 + if (err_code < 0) {
146 + pcre2_get_error_message(err_code, re_err, 128);
147 + fprintf (stderr, "compile failed: %s\n", re_err);
151 match_func = &re_match_func;
153 @@ -1653,21 +1662,28 @@ dump_packet (struct pcap_pkthdr *h, u_ch
155 re_match_func (unsigned char *data, uint32_t len)
157 + pcre2_match_data *match_data;
159 + match_data = pcre2_match_data_create_from_pattern(pattern, NULL);
161 - switch (pcre_exec (pattern, 0, (char *)data, (int32_t) len, 0, 0, 0, 0)) {
162 - case PCRE_ERROR_NULL:
163 - case PCRE_ERROR_BADOPTION:
164 - case PCRE_ERROR_BADMAGIC:
165 - case PCRE_ERROR_UNKNOWN_NODE:
166 - case PCRE_ERROR_NOMEMORY:
167 + switch (pcre2_match (pattern, (PCRE2_SPTR)data, len, 0, 0, match_data, 0)) {
168 + case PCRE2_ERROR_NULL:
169 + case PCRE2_ERROR_BADOPTION:
170 + case PCRE2_ERROR_BADMAGIC:
171 + case PCRE2_ERROR_INTERNAL:
172 + case PCRE2_ERROR_NOMEMORY:
173 + pcre2_match_data_free(match_data);
174 perror ("she's dead, jim\n");
178 - case PCRE_ERROR_NOMATCH:
179 + case PCRE2_ERROR_NOMATCH:
180 + pcre2_match_data_free(match_data);
184 + pcre2_match_data_free(match_data);
189 @@ -2125,9 +2141,7 @@ clean_exit (int32_t sig)
193 - pcre_free (pattern);
195 - pcre_free (pattern_extra);
196 + pcre2_code_free (pattern);