Skip to content

Commit

Permalink
[Core] Move project to PCRE2
Browse files Browse the repository at this point in the history
Move project to PCRE2 as PCRE is EOL and won't receive any security
updates anymore.

PCRE2 have different API compared to PCRE. Mainly PCRE2 have the concept
of match_data, no ovector needs to be passed, different handling for
error string and different handling for substring manipulation.

Update any user of PCRE library with the new API

Signed-off-by: Christian Marangi <[email protected]>
  • Loading branch information
Ansuel committed Nov 5, 2024
1 parent 83b3871 commit b87c179
Show file tree
Hide file tree
Showing 41 changed files with 488 additions and 340 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ CORE_LIBS+=libfreeswitch_libyuv.la
endif

lib_LTLIBRARIES = libfreeswitch.la
libfreeswitch_la_CFLAGS = $(CORE_CFLAGS) $(SQLITE_CFLAGS) $(GUMBO_CFLAGS) $(FVAD_CFLAGS) $(FREETYPE_CFLAGS) $(CURL_CFLAGS) $(PCRE_CFLAGS) $(SPEEX_CFLAGS) $(LIBEDIT_CFLAGS) $(openssl_CFLAGS) $(SOFIA_SIP_CFLAGS) $(AM_CFLAGS) $(TPL_CFLAGS)
libfreeswitch_la_CFLAGS = $(CORE_CFLAGS) $(SQLITE_CFLAGS) $(GUMBO_CFLAGS) $(FVAD_CFLAGS) $(FREETYPE_CFLAGS) $(CURL_CFLAGS) $(PCRE2_CFLAGS) $(SPEEX_CFLAGS) $(LIBEDIT_CFLAGS) $(openssl_CFLAGS) $(SOFIA_SIP_CFLAGS) $(AM_CFLAGS) $(TPL_CFLAGS)
libfreeswitch_la_LDFLAGS = -version-info 1:0:0 $(AM_LDFLAGS) $(PLATFORM_CORE_LDFLAGS) -no-undefined
libfreeswitch_la_LIBADD = $(CORE_LIBS) $(APR_LIBS) $(SQLITE_LIBS) $(GUMBO_LIBS) $(FVAD_LIBS) $(FREETYPE_LIBS) $(CURL_LIBS) $(PCRE_LIBS) $(SPEEX_LIBS) $(LIBEDIT_LIBS) $(SYSTEMD_LIBS) $(openssl_LIBS) $(PLATFORM_CORE_LIBS) $(TPL_LIBS) $(SPANDSP_LIBS) $(SOFIA_SIP_LIBS)
libfreeswitch_la_LIBADD = $(CORE_LIBS) $(APR_LIBS) $(SQLITE_LIBS) $(GUMBO_LIBS) $(FVAD_LIBS) $(FREETYPE_LIBS) $(CURL_LIBS) $(PCRE2_LIBS) $(SPEEX_LIBS) $(LIBEDIT_LIBS) $(SYSTEMD_LIBS) $(openssl_LIBS) $(PLATFORM_CORE_LIBS) $(TPL_LIBS) $(SPANDSP_LIBS) $(SOFIA_SIP_LIBS)
libfreeswitch_la_DEPENDENCIES = $(BUILT_SOURCES)

if HAVE_PNG
Expand Down
10 changes: 5 additions & 5 deletions build/Makefile.centos5
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DOWNLOAD=http://files.freeswitch.org/downloads/libs
JPEG=v8d
OPENSSL=1.0.1l
SQLITE=autoconf-3080403
PCRE=8.35
PCRE2=10.42
CURL=7.40.0
SPEEX=1.2rc1
LIBEDIT=20140618-3.1
Expand Down Expand Up @@ -45,7 +45,7 @@ has-git:
@git --version || (echo "please install git by running 'make install-git'" && false)

clean:
@rm -rf openssl* ldns* jpeg* pcre* perl* pkg-config* speex* sqlite* libedit* curl* *~
@rm -rf openssl* ldns* jpeg* pcre2* perl* pkg-config* speex* sqlite* libedit* curl* *~
(cd freeswitch.git && git clean -fdx && git reset --hard HEAD && git pull)

libjpeg: jpeg-8d/.done
Expand All @@ -66,9 +66,9 @@ sqlite-$(SQLITE):
(test -d $@) || (wget -4 -O [email protected] $(DOWNLOAD)/[email protected] && tar zxfv [email protected])
(cd $@ && ./configure --prefix=$(PREFIX) && make && sudo make install && touch .done_sqlite && touch .done)

pcre: pcre-$(PCRE)/.done
pcre-$(PCRE)/.done: pcre-$(PCRE)
pcre-$(PCRE):
pcre2: pcre2-$(PCRE2)/.done
pcre2-$(PCRE2)/.done: pcre2-$(PCRE2)
pcre2-$(PCRE2):
(test -d $@) || (wget -4 -O [email protected] $(DOWNLOAD)/[email protected] && tar zxfv [email protected])
(cd $@ && ./configure --prefix=$(PREFIX) && make && sudo make install && touch .done)

Expand Down
4 changes: 2 additions & 2 deletions build/Makefile.centos6
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# in that same directory.
#
#
RPMS=git gcc-c++ autoconf automake libtool wget python ncurses-devel zlib-devel libjpeg-devel openssl-devel e2fsprogs-devel sqlite-devel libcurl-devel pcre-devel speex-devel ldns-devel libedit-devel
DEBS=git build-essential automake autoconf 'libtool-bin|libtool' wget python uuid-dev zlib1g-dev 'libjpeg8-dev|libjpeg62-turbo-dev' libncurses5-dev libssl-dev libpcre3-dev libcurl4-openssl-dev libldns-dev libedit-dev libspeexdsp-dev libspeexdsp-dev libsqlite3-dev perl libgdbm-dev libdb-dev bison libvlc-dev pkg-config
RPMS=git gcc-c++ autoconf automake libtool wget python ncurses-devel zlib-devel libjpeg-devel openssl-devel e2fsprogs-devel sqlite-devel libcurl-devel pcre2-devel speex-devel ldns-devel libedit-devel
DEBS=git build-essential automake autoconf 'libtool-bin|libtool' wget python uuid-dev zlib1g-dev 'libjpeg8-dev|libjpeg62-turbo-dev' libncurses5-dev libssl-dev libpcre2-dev libcurl4-openssl-dev libldns-dev libedit-dev libspeexdsp-dev libspeexdsp-dev libsqlite3-dev perl libgdbm-dev libdb-dev bison libvlc-dev pkg-config

freeswitch: deps has-git freeswitch.git/Makefile
cd freeswitch.git && make
Expand Down
2 changes: 1 addition & 1 deletion build/Makefile.openbsd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
#

PKG=rsync-3.1.0 git automake-1.14.1 autoconf-2.69p1 libtool gmake bzip2 jpeg wget pcre speex libldns
PKG=rsync-3.1.0 git automake-1.14.1 autoconf-2.69p1 libtool gmake bzip2 jpeg wget pcre2 speex libldns
PREFIX=/usr/local/freeswitch
DOWNLOAD=http://files.freeswitch.org/downloads/libs
OPENSSL=1.0.1j
Expand Down
10 changes: 5 additions & 5 deletions build/Makefile.solaris11
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DOWNLOAD=http://files.freeswitch.org/downloads/libs
JP=v8d
SSL=1.0.1j
SQLITE=autoconf-3080403
PCRE=8.35
PCRE2=10.42
CURL=7.35.0
SPEEX=1.2rc1
LIBEDIT=20140618-3.1
Expand Down Expand Up @@ -43,7 +43,7 @@ has-git:
@git --version || (echo "please install git by running 'gmake install-git'" && false)

clean:
@rm -rf openssl* ldns* jpeg* pcre* perl* pkg-config* speex* sqlite* libedit* curl* *~
@rm -rf openssl* ldns* jpeg* pcre2* perl* pkg-config* speex* sqlite* libedit* curl* *~
(cd freeswitch.git && git clean -fdx && git reset --hard HEAD && git pull)

libjpeg: jpeg-8d/.done
Expand All @@ -64,9 +64,9 @@ sqlite-$(SQLITE):
(test -d $@) || (wget -4 -O [email protected] $(DOWNLOAD)/[email protected] && tar zxfv [email protected])
(cd $@ && CFLAGS=-m64 LDFLAGS=-m64 ./configure --prefix=$(PREFIX) && gmake && sudo gmake install && touch .done)

pcre: pcre-$(PCRE)/.done
pcre-$(PCRE)/.done: pcre-$(PCRE)
pcre-$(PCRE):
pcre2: pcre2-$(PCRE2)/.done
pcre2-$(PCRE2)/.done: pcre2-$(PCRE2)
pcre2-$(PCRE2):
(test -d $@) || (wget -4 -O [email protected] $(DOWNLOAD)/[email protected] && tar zxfv [email protected])
(cd $@ && CXXFLAGS=-m64 CFLAGS=-m64 LDFLAGS=-m64 ./configure --prefix=$(PREFIX) && gmake && sudo gmake install && touch .done)

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ PKG_CHECK_MODULES([TPL], [libtpl >= 1.5],[

PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.6.20])
PKG_CHECK_MODULES([CURL], [libcurl >= 7.19])
PKG_CHECK_MODULES([PCRE], [libpcre >= 7.8])
PKG_CHECK_MODULES([PCRE2], [libpcre2-8 >= 10.00])
PKG_CHECK_MODULES([SPEEX], [speex >= 1.2rc1 speexdsp >= 1.2rc1])
PKG_CHECK_MODULES([YAML], [yaml-0.1 >= 0.1.4],[
AM_CONDITIONAL([HAVE_YAML],[true])],[
Expand Down
4 changes: 2 additions & 2 deletions freeswitch.spec
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ BuildRequires: libtool >= 1.5.17
BuildRequires: openssl-devel >= 1.0.1e
BuildRequires: sofia-sip-devel >= 1.13.17
BuildRequires: spandsp3-devel >= 3.0
BuildRequires: pcre-devel
BuildRequires: pcre2-devel
BuildRequires: speex-devel
BuildRequires: sqlite-devel >= 3.6.20
BuildRequires: libtiff-devel
Expand All @@ -156,7 +156,7 @@ BuildRequires: zlib-devel
BuildRequires: libxml2-devel
BuildRequires: libsndfile-devel
Requires: curl >= 7.19
Requires: pcre
Requires: pcre2
Requires: speex
Requires: sqlite >= 3.6.20
Requires: libtiff
Expand Down
10 changes: 5 additions & 5 deletions libs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ opal
/win32/celt/*/*/libcelt.log
/win32/libg722_1/*/*/libg722_1.log
/win32/libshout/*/*/libshout.log
/win32/pcre/pcre_chartables.c
/win32/pcre2/pcre2_chartables.c
/win32/tmp*.bat
!/xmlrpc-c/include/xmlrpc-c/config.h.in
/xmlrpc-c/stamp-h2
Expand Down Expand Up @@ -610,9 +610,9 @@ opal
broadvoice/config/compile
ilbc/config/compile
libg722_1/config/compile
pcre/compile
pcre2/compile
srtp/build/compile
/pcre-*/
/pcre2-*/
/speex-*/
/curl-*/
/sqlite-*.zip
Expand All @@ -637,8 +637,8 @@ curl-*/
curl-*
flite-*/
flite-*
pcre-*/
pcre-*
pcre2-*/
pcre2-*
libsndfile-*/
libsndfile-*
opencv-*/
Expand Down
6 changes: 3 additions & 3 deletions libs/apr/build/prebuildNW.bat
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ copy ..\..\apr-util\xml\expat\lib\expat.h.in ..\..\apr-util\xml\expat\lib\expat.
copy ..\..\apr-util\xml\expat\lib\config.hnw ..\..\apr-util\xml\expat\lib\config.h
copy ..\..\apr-util\include\private\apu_select_dbm.hw ..\..\apr-util\include\private\apu_select_dbm.h

@echo Fixing up the pcre headers
copy ..\..\pcre\config.hw ..\..\pcre\config.h
copy ..\..\pcre\pcre.hw ..\..\pcre\pcre.h
@echo Fixing up the pcre2 headers
copy ..\..\pcre2\config.hw ..\..\pcre2\config.h
copy ..\..\pcre2\pcre2.hw ..\..\pcre2\pcre2.h

@echo Generating the import list...
set MWCIncludes=..\include;..\include\arch\netware;..\include\arch\unix;..\..\apr-util\include;+%NovellLibC%
Expand Down
2 changes: 1 addition & 1 deletion src/include/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
* - APR (http://apr.apache.org)
* - APR-Util (http://apr.apache.org)
* - SQLite (http://www.sqlite.org)
* - Pcre (http://www.pcre.org/)
* - Pcre2 (http://www.pcre.org/)
* - SRTP (http://srtp.sourceforge.net/srtp.html)
*
* Additionally, the various external modules make use of several external modules:
Expand Down
33 changes: 23 additions & 10 deletions src/include/switch_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* Michael Jerris <[email protected]>
*
* switch_regex.h -- pcre wrapper and extensions Header
* switch_regex.h -- pcre2 wrapper and extensions Header
*
*/
/*! \file switch_regex.h
Expand All @@ -40,18 +40,21 @@ SWITCH_BEGIN_EXTERN_C
* @ingroup FREESWITCH
* @{
*/
typedef struct real_pcre switch_regex_t;
typedef struct pcre2_real_code switch_regex_t;
typedef struct pcre2_real_match_data_8 switch_regex_match_data_t;
typedef struct pcre2_real_compile_context_8 switch_regex_compile_context_t;

SWITCH_DECLARE(switch_regex_t *) switch_regex_compile(const char *pattern, int options, const char **errorptr, int *erroroffset,
const unsigned char *tables);
SWITCH_DECLARE(switch_regex_t *) switch_regex_compile(const char *pattern, int options, int *errorcode, unsigned int *erroroffset,
switch_regex_compile_context_t *ccontext);

SWITCH_DECLARE(int) switch_regex_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int size);
SWITCH_DECLARE(int) switch_regex_copy_substring(switch_regex_match_data_t *match_data, int stringnumber, char *buffer, unsigned int *size);

SWITCH_DECLARE(void) switch_regex_match_free(void *data);
SWITCH_DECLARE(void) switch_regex_free(void *data);

SWITCH_DECLARE(int) switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen);
SWITCH_DECLARE(void) switch_perform_substitution(switch_regex_t *re, int match_count, const char *data, const char *field_data,
char *substituted, switch_size_t len, int *ovector);
SWITCH_DECLARE(int) switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, switch_regex_match_data_t **new_match_data);
SWITCH_DECLARE(void) switch_perform_substitution(switch_regex_match_data_t *match_data, const char *data,
char *substituted, switch_size_t len);

/*!
\brief Function to evaluate an expression against a string
Expand All @@ -70,17 +73,27 @@ SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const cha
*/
SWITCH_DECLARE(switch_status_t) switch_regex_match_partial(const char *target, const char *expression, int *partial_match);

SWITCH_DECLARE(void) switch_capture_regex(switch_regex_t *re, int match_count, const char *field_data,
int *ovector, const char *var, switch_cap_callback_t callback, void *user_data);
SWITCH_DECLARE(void) switch_capture_regex(switch_regex_match_data_t *match_data, int match_count,
const char *var, switch_cap_callback_t callback, void *user_data);

SWITCH_DECLARE_NONSTD(void) switch_regex_set_var_callback(const char *var, const char *val, void *user_data);
SWITCH_DECLARE_NONSTD(void) switch_regex_set_event_header_callback(const char *var, const char *val, void *user_data);

#define switch_match_data_safe_free(match_data) if (match_data) {\
switch_regex_match_free(match_data);\
match_data = NULL;\
}

#define switch_regex_safe_free(re) if (re) {\
switch_regex_free(re);\
re = NULL;\
}

#define switch_regex_and_match_data_safe_free(re, match_data) {\
switch_match_data_safe_free(match_data);\
switch_regex_safe_free(re);\
}


/** @} */

Expand Down
8 changes: 4 additions & 4 deletions src/mod/applications/mod_abstraction/mod_abstraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ SWITCH_STANDARD_API(api_abstraction_function)

int proceed;
switch_regex_t *re = NULL;
int ovector[30];
switch_regex_match_data_t *match_data = NULL;

if ((proceed = switch_regex_perform(cmd, parse, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
if ((proceed = switch_regex_perform(cmd, parse, &re, &match_data))) {
const char *api_args = NULL;
char *substituted = NULL;

Expand All @@ -78,7 +78,7 @@ SWITCH_STANDARD_API(api_abstraction_function)
goto end;
}
memset(substituted, 0, len);
switch_perform_substitution(re, proceed, arguments, cmd , substituted, len, ovector);
switch_perform_substitution(match_data, arguments, substituted, len);
api_args = substituted;
} else {
api_args = arguments;
Expand All @@ -89,7 +89,7 @@ SWITCH_STANDARD_API(api_abstraction_function)
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No match for API %s (%s != %s)\n", api_name, parse, cmd);
}
switch_regex_safe_free(re);
switch_regex_and_match_data_safe_free(re, match_data);

} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "API %s doesn't exist inside the xml structure. You might have forgot to reload the module after editing it\n", api_name);
Expand Down
8 changes: 4 additions & 4 deletions src/mod/applications/mod_commands/mod_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ SWITCH_STANDARD_API(replace_function)
SWITCH_STANDARD_API(regex_function)
{
switch_regex_t *re = NULL;
int ovector[30];
switch_regex_match_data_t *match_data = NULL;
int argc;
char *mydata = NULL, *argv[4];
size_t len = 0;
Expand Down Expand Up @@ -2054,7 +2054,7 @@ SWITCH_STANDARD_API(regex_function)
goto error;
}

proceed = switch_regex_perform(argv[0], argv[1], &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
proceed = switch_regex_perform(argv[0], argv[1], &re, &match_data);

if (argc > 2) {
char *flags = "";
Expand All @@ -2069,7 +2069,7 @@ SWITCH_STANDARD_API(regex_function)
switch_assert(substituted);
memset(substituted, 0, len);
switch_replace_char(argv[2], '%', '$', SWITCH_FALSE);
switch_perform_substitution(re, proceed, argv[2], argv[0], substituted, len, ovector);
switch_perform_substitution(match_data, argv[2], substituted, len);

stream->write_function(stream, "%s", substituted);
free(substituted);
Expand All @@ -2091,7 +2091,7 @@ SWITCH_STANDARD_API(regex_function)
error:
stream->write_function(stream, "-ERR");
ok:
switch_regex_safe_free(re);
switch_regex_and_match_data_safe_free(re, match_data);
switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;
}
Expand Down
8 changes: 4 additions & 4 deletions src/mod/applications/mod_dptools/mod_dptools.c
Original file line number Diff line number Diff line change
Expand Up @@ -3211,16 +3211,16 @@ SWITCH_STANDARD_APP(capture_function)
{
char *argv[3] = { 0 };
switch_regex_t *re = NULL;
int ovector[30] = {0};
switch_regex_match_data_t *match_data = NULL;
char *lbuf;
int proceed;

if (!zstr(data) && (lbuf = switch_core_session_strdup(session, data))
&& switch_separate_string(lbuf, '|', argv, (sizeof(argv) / sizeof(argv[0]))) == 3) {
if ((proceed = switch_regex_perform(argv[1], argv[2], &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
switch_capture_regex(re, proceed, argv[1], ovector, argv[0], switch_regex_set_var_callback, session);
if ((proceed = switch_regex_perform(argv[1], argv[2], &re, &match_data))) {
switch_capture_regex(match_data, proceed, argv[0], switch_regex_set_var_callback, session);
}
switch_regex_safe_free(re);
switch_regex_and_match_data_safe_free(re, match_data);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No data specified.\n");
}
Expand Down
Loading

0 comments on commit b87c179

Please sign in to comment.