The API for PCRE2 seems to have changed a lot.

According to https://redmine.openinfosecfoundation.org/issues/3194

- pcre_free_substring replaced straight by pcre2_substring_free
- pcre_get_substring seems replaced by pcre2_substring_get_bynumber
which does not have the same arguments...
- pcre_exec replaced by pcre2_match
- pcre_copy_substring replaced by pcre2_substring_copy_bynumber
- pcre_free
- pcre_compile replaced by pcre2_compile
- pcre_study obsolete in pcre2 : The new API is more extensible, and
it was simplified by abolishing the separate "study" optimizing
function
- pare jit stuff present in pcre2

There seems to be some rationale on the decisions of the new API at

http://washitake.com/mail/exim/mirror/pcre/Testing/PCRE2_proposal.pdf

Upstream hasn't ported the code yet.

I'm attaching my 1st draft of a patch. It builds, but I need to check
it carefully.

As I don't have much experience with pcre2, comments and suggestions
are welcome.

Thanks,
Miry
# See: https://github.com/wch/r-source/blob/af9a038e277d14b038ef877366652b6e5400c399/src/main/grep.c
# See: https://github.com/Sigil-Ebook/Sigil/issues/630
# See: https://github.com/PCRE2Project/pcre2/issues/51
# See: https://github.com/HaxeFoundation/haxe/issues/10491
# See: https://github.com/SWI-Prolog/packages-pcre/issues/2
# See: https://github.com/SWI-Prolog/packages-pcre/pull/3/files
# See: https://pcre.org/current/doc/html/
# See: https://github.com/luvit/pcre2/blob/master/src/pcre2demo.c
# See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=999923
# See: http://washitake.com/mail/exim/mirror/pcre/Testing/PCRE2_proposal.pdf
# See: https://redmine.openinfosecfoundation.org/issues/3194
# See: https://pcre.org/current/doc/html/pcre2api.html

Index: xmlcopyeditor-1.3.0.0/configure.ac
===================================================================
--- xmlcopyeditor-1.3.0.0.orig/configure.ac
+++ xmlcopyeditor-1.3.0.0/configure.ac
@@ -71,9 +71,15 @@ AC_ARG_ENABLE(debug,
   CXXFLAGS="${CXXFLAGS} -DNDEBUG -DwxDEBUG_LEVEL=0"
 ])
 
-# Check pcre is available
-AC_CHECK_HEADER(pcre.h, ,
-	AC_MSG_ERROR([PCRE headers not found]))
+# Check pcre2 is available
+AC_CHECK_HEADER([pcre2.h], [],
+	[AC_MSG_ERROR([PCRE2 headers not found])],
+	[#define PCRE2_CODE_UNIT_WIDTH 8
+	#include <pcre2.h>
+	])
+
+PCRE2_LIBS="-lpcre2-8"
+AC_SUBST(PCRE2_LIBS)
 
 # Check boost::shared_ptr is available
 AC_LANG(C++)
Index: xmlcopyeditor-1.3.0.0/src/wrapregex.h
===================================================================
--- xmlcopyeditor-1.3.0.0.orig/src/wrapregex.h
+++ xmlcopyeditor-1.3.0.0/src/wrapregex.h
@@ -21,10 +21,14 @@
 #ifndef WRAPREGEX_H
 #define WRAPREGEX_H
 
+#ifndef PCRE2_CODE_UNIT_WIDTH
+#define PCRE2_CODE_UNIT_WIDTH 8
+#endif
+
 #include <iostream>
 #include <string>
 #include <vector>
-#include <pcre.h>
+#include <pcre2.h>
 #include <boost/utility.hpp>
 #include "contexthandler.h"
 
@@ -53,8 +57,11 @@ class WrapRegex : private boost::noncopy
 		int returnValue;
 		bool disabled;
 
-		pcre *patternStructure;
-		pcre_extra *patternExtraStructure;
+		//~ pcre *patternStructure;
+		//~ pcre_extra *patternExtraStructure;
+		pcre2_code *patternCode;
+		pcre2_match_data *patternMatchData;
+		pcre2_match_context *patternMatchContext;
 		int *matchArray;
 
 		string getInterpolatedString_ ( const char *buffer,
Index: xmlcopyeditor-1.3.0.0/src/wrapregex.cpp
===================================================================
--- xmlcopyeditor-1.3.0.0.orig/src/wrapregex.cpp
+++ xmlcopyeditor-1.3.0.0/src/wrapregex.cpp
@@ -41,8 +41,11 @@ WrapRegex::WrapRegex (
 	{
 		disabled = true;
 		matchArray = NULL;
-		patternStructure = NULL;
-		patternExtraStructure = NULL;
+		//~ patternStructure = NULL;
+		//~ patternExtraStructure = NULL;
+		patternCode = NULL;
+		patternMatchData = NULL;
+		patternMatchContext = NULL;
 		return;
 	}
 	disabled = false;
@@ -50,21 +53,25 @@ WrapRegex::WrapRegex (
 	matchArray = new int[arrayLength];
 
 	// compile
-	int optionsFlag = ( matchCase ) ? PCRE_UTF8 : PCRE_CASELESS | PCRE_UTF8;
-	const char *errorPointer;
-	int errorOffset;
-
-	if ( ( patternStructure = pcre_compile (
-	                              pattern.c_str(),
-	                              optionsFlag,
-	                              &errorPointer,
-	                              &errorOffset,
-	                              NULL ) ) == NULL )
+	uint32_t optionsFlag = ( matchCase ) ? PCRE2_UTF | PCRE2_NO_UTF_CHECK : PCRE2_CASELESS | PCRE2_UTF | PCRE2_NO_UTF_CHECK;
+	int errorCode;
+	PCRE2_SIZE errorOffset;
+
+	if ( ( patternCode = pcre2_compile (
+		(PCRE2_SPTR)pattern.c_str(), // pattern
+		PCRE2_ZERO_TERMINATED, // pattern is zero-terminated
+		optionsFlag, // options
+		&errorCode, // error number
+		&errorOffset, // error offset
+		NULL ) ) == NULL ) // default compile context
 	{
-		throw runtime_error ( errorPointer );
+		char buf[256];
+		pcre2_get_error_message ( errorCode, (PCRE2_UCHAR *)buf, sizeof(buf) );
+		throw runtime_error ( string(buf) );
 	}
 
-	patternExtraStructure = pcre_study ( patternStructure, 0, &errorPointer );
+	patternMatchData = pcre2_match_data_create_from_pattern ( patternCode, NULL );
+	patternMatchContext = pcre2_match_context_create ( NULL );
 }
 
 WrapRegex::~WrapRegex()
@@ -72,8 +79,11 @@ WrapRegex::~WrapRegex()
 	if ( disabled )
 		return;
 
-	pcre_free ( patternStructure );
-	pcre_free ( patternExtraStructure );
+	//~ pcre_free ( patternStructure );
+	//~ pcre_free ( patternExtraStructure );
+	pcre2_match_data_free ( patternMatchData );
+	pcre2_code_free ( patternCode );
+	pcre2_match_context_free ( patternMatchContext );
 	delete[] matchArray;
 }
 
@@ -108,15 +118,14 @@ string WrapRegex::replaceGlobal (
 	string output, match;
 
 	output.reserve ( buffer.size() );
-	while ( ( returnValue = pcre_exec (
-	                            patternStructure,
-	                            patternExtraStructure,
-	                            s,
-	                            strlen ( s ),
-	                            0,
-	                            0,
-	                            matchArray,
-	                            arrayLength ) ) >= 0 )
+	while ( ( returnValue = pcre2_match (
+		patternCode, // compiled pattern
+		(PCRE2_SPTR)s, // subject string
+		strlen ( s ), // length of the subject
+		0, // start at offset 0 in the subject
+		0, // default options
+		patternMatchData, // block where results will be stored
+		patternMatchContext ) ) >= 0 ) // match context
 	{
 		++ ( *matchCount );
 
@@ -150,15 +159,14 @@ int WrapRegex::matchPatternGlobal_ (
 	matchcount = 0;
 	offset = 0;
 
-	while ( ( returnValue = pcre_exec (
-	                            patternStructure,
-	                            patternExtraStructure,
-	                            s,
-	                            buflen,
-	                            offset,
-	                            0,
-	                            matchArray,
-	                            arrayLength ) ) >= 0 )
+	while ( ( returnValue = pcre2_match (
+		patternCode, // compiled pattern
+		(PCRE2_SPTR)s, // subject string
+		buflen, // length of the subject
+		offset, // start at this offset in the subject
+		0, // default options
+		patternMatchData, // block where results will be stored
+		patternMatchContext ) ) >= 0 ) // match context
 	{
 		++matchcount;
 
@@ -255,11 +263,18 @@ string WrapRegex::getSubpattern_ ( const
 	if ( disabled )
 		return "";
 
-	const char *sub;
-	int ret = pcre_get_substring ( s, matchArray, returnValue, subpattern, &sub );
-	if ( ret == PCRE_ERROR_NOSUBSTRING || ret == PCRE_ERROR_NOMEMORY )
+	char *sub = NULL;
+	size_t sublen;
+	//~ int ret = pcre_get_substring ( s, matchArray, returnValue, subpattern, &sub );
+	int ret = pcre2_substring_get_bynumber (
+		patternMatchData,
+		subpattern,
+		(PCRE2_UCHAR **)sub,
+		&sublen
+	);
+	if ( ret == PCRE2_ERROR_NOMATCH || ret == PCRE2_ERROR_BADDATA )
 		return "";
 	string subString ( sub );
-	pcre_free_substring ( sub );
+	pcre2_substring_free ( (PCRE2_UCHAR *)sub );
 	return subString;
 }
Index: xmlcopyeditor-1.3.0.0/src/Makefile.am
===================================================================
--- xmlcopyeditor-1.3.0.0.orig/src/Makefile.am
+++ xmlcopyeditor-1.3.0.0/src/Makefile.am
@@ -83,7 +83,8 @@ xmlcopyeditor_LDADD = $(WX_LIBS) \
 	$(ENCHANT_LIBS) \
 	$(GTK_LIBS) \
 	$(XSLT_LIBS) \
-	-lexpat -lpcre -lxerces-c
+	$(PCRE2_LIBS) \
+	-lexpat -lxerces-c
 
 nobase_dist_xmlcopyeditor_DATA = $(srcdir)/catalog/catalog \
 	$(srcdir)/dtd/*.* \

Reply via email to