Merge pull request #861 from aduskett/dahdi-linux
[feed/telephony.git] / net / sipgrep / patches / 001-Move-to-PCRE2-from-PCRE.patch
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
5
6 Move to PCRE2 as PCRE is EOL and won't receive any security updates
7 anymore.
8
9 Convert each function to PCRE2 equivalent and update configure.ac to
10 USE_PCRE2.
11
12 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
13 ---
14 configure.ac | 20 +++++++++---------
15 src/config.h.in | 4 ++--
16 src/sipgrep.c | 56 ++++++++++++++++++++++++++++++-------------------
17 3 files changed, 47 insertions(+), 33 deletions(-)
18
19 --- a/configure.ac
20 +++ b/configure.ac
21 @@ -26,8 +26,8 @@ AC_ARG_ENABLE(ssl,
22 AC_MSG_RESULT([$SSL])
23 AC_SUBST([SSL])
24
25 -usePCRE=yes
26 -AC_SUBST([PCRE])
27 +usePCRE2=yes
28 +AC_SUBST([PCRE2])
29
30 useNCURSES=no
31 AC_MSG_CHECKING([whether to use ncurses])
32 @@ -169,15 +169,15 @@ AC_SUBST(PCAP_LIBS)
33
34
35 dnl
36 -dnl check for pcre library
37 +dnl check for pcre2 library
38 dnl
39
40 -# Checks for libpcre
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])
45 -AC_SUBST(PCRE_LIBS)
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])
51 +AC_SUBST(PCRE2_LIBS)
52
53
54 dnl
55 @@ -271,6 +271,6 @@ echo Ncurses support............. : $use
56
57 echo
58 echo Build with REDIS............ : $useRedis
59 -echo Build with PCRE............. : $usePCRE
60 +echo Build with PCRE............. : $usePCRE2
61 echo
62
63 --- a/src/config.h.in
64 +++ b/src/config.h.in
65 @@ -152,8 +152,8 @@
66 /* Use NCURSES library */
67 #undef USE_NCURSES
68
69 -/* Use PCRE library */
70 -#undef USE_PCRE
71 +/* Use PCRE2 library */
72 +#undef USE_PCRE2
73
74 /* Use REDIS library */
75 #undef USE_REDIS
76 --- a/src/sipgrep.c
77 +++ b/src/sipgrep.c
78 @@ -88,7 +88,8 @@
79
80 #include <netdb.h>
81
82 -#include <pcre.h>
83 +#define PCRE2_CODE_UNIT_WIDTH 8
84 +#include <pcre2.h>
85
86 /* reasambling */
87 #include "include/ipreasm.h"
88 @@ -149,17 +150,18 @@ struct statistics_table *statstable = NU
89 * GNU PCRE
90 */
91
92 -int32_t err_offset;
93 -char *re_err = NULL;
94 +PCRE2_UCHAR re_err[128];
95 +PCRE2_SIZE err_offset;
96 +uint32_t err_code;
97
98 -pcre *pattern = NULL;
99 -pcre_extra *pattern_extra = NULL;
100 +pcre2_code *pattern = NULL;
101
102 /*
103 * Matching
104 */
105
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;
111
112 @@ -550,13 +552,13 @@ main (int argc, char **argv)
113
114 if (match_data) {
115
116 - uint32_t pcre_options = PCRE_UNGREEDY;
117 + uint32_t pcre2_options = PCRE2_UNGREEDY;
118
119 if (re_ignore_case)
120 - pcre_options |= PCRE_CASELESS;
121 + pcre2_options |= PCRE2_CASELESS;
122
123 if (re_multiline_match)
124 - pcre_options |= PCRE_DOTALL;
125 + pcre2_options |= PCRE2_DOTALL;
126
127 if (re_match_word) {
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;
131 }
132
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);
135
136 if (!pattern) {
137 + pcre2_get_error_message (err_code, re_err, 128);
138 fprintf (stderr, "compile failed: %s\n", re_err);
139 clean_exit (-1);
140 }
141
142 - pattern_extra = pcre_study (pattern, 0, (const char **) &re_err);
143 + err_code = pcre2_jit_compile (pattern, PCRE2_JIT_COMPLETE);
144 +
145 + if (err_code < 0) {
146 + pcre2_get_error_message(err_code, re_err, 128);
147 + fprintf (stderr, "compile failed: %s\n", re_err);
148 + clean_exit (-1);
149 + }
150
151 match_func = &re_match_func;
152
153 @@ -1653,21 +1662,28 @@ dump_packet (struct pcap_pkthdr *h, u_ch
154 int8_t
155 re_match_func (unsigned char *data, uint32_t len)
156 {
157 + pcre2_match_data *match_data;
158 +
159 + match_data = pcre2_match_data_create_from_pattern(pattern, NULL);
160
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");
175 clean_exit (-2);
176 break;
177
178 - case PCRE_ERROR_NOMATCH:
179 + case PCRE2_ERROR_NOMATCH:
180 + pcre2_match_data_free(match_data);
181 return 0;
182 }
183
184 + pcre2_match_data_free(match_data);
185 +
186 if (max_matches)
187 matches++;
188
189 @@ -2125,9 +2141,7 @@ clean_exit (int32_t sig)
190 printf ("exit\n");
191
192 if (pattern)
193 - pcre_free (pattern);
194 - if (pattern_extra)
195 - pcre_free (pattern_extra);
196 + pcre2_code_free (pattern);
197
198 if (bin_data)
199 free (bin_data);