nginx-util: move to pcre2
authorChristian Marangi <redacted>
Fri, 22 Sep 2023 16:15:01 +0000 (18:15 +0200)
committerRosen Penev <redacted>
Mon, 25 Sep 2023 21:41:54 +0000 (14:41 -0700)
Convert to pcre2 library as pcre is EOL. No functional change intended.

Signed-off-by: Christian Marangi <redacted>
net/nginx-util/Makefile
net/nginx-util/src/CMakeLists.txt
net/nginx-util/src/regex-pcre.hpp

index 4a77e2f2059f4868d656990759a2fca0ca4d1dd0..b4f06aaae0af47ae8d37ab1828d902b895fc4a29 100644 (file)
@@ -2,7 +2,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=nginx-util
 PKG_VERSION:=1.6
-PKG_RELEASE:=19
+PKG_RELEASE:=20
 PKG_MAINTAINER:=Peter Stadler <peter.stadler@student.uibk.ac.at>
 
 include $(INCLUDE_DIR)/package.mk
@@ -30,7 +30,7 @@ endef
 define Package/nginx-ssl-util
   $(Package/nginx-ssl-util/default)
   TITLE+= (using PCRE)
-  DEPENDS+= +libpcre
+  DEPENDS+= +libpcre2
   CONFLICTS:=nginx-ssl-util-nopcre,
 endef
 
index 2adff1c71e397326498fd2ee582727230facce0d..e023f1eb6e2462e44dd6ef1f7f179449e2abc9f0 100644 (file)
@@ -27,7 +27,7 @@ FIND_LIBRARY(ubus NAMES ubus)
 INCLUDE_DIRECTORIES(${ubus_include_dir})
 
 ADD_EXECUTABLE(nginx-ssl-util nginx-util.cpp)
-TARGET_LINK_LIBRARIES(nginx-ssl-util ${uci} ${ubox} ${ubus} pthread ssl crypto pcre)
+TARGET_LINK_LIBRARIES(nginx-ssl-util ${uci} ${ubox} ${ubus} pthread ssl crypto pcre2-8)
 INSTALL(TARGETS nginx-ssl-util RUNTIME DESTINATION bin)
 
 ADD_EXECUTABLE(nginx-ssl-util-nopcre nginx-util.cpp)
@@ -51,7 +51,7 @@ INSTALL(TARGETS px5g RUNTIME DESTINATION bin)
 
 ADD_EXECUTABLE(nginx-ssl-util-noubus nginx-util.cpp)
 TARGET_COMPILE_DEFINITIONS(nginx-ssl-util-noubus PUBLIC -DNO_UBUS)
-TARGET_LINK_LIBRARIES(nginx-ssl-util-noubus ${uci} ${ubox} pthread ssl crypto pcre)
+TARGET_LINK_LIBRARIES(nginx-ssl-util-noubus ${uci} ${ubox} pthread ssl crypto pcre2-8)
 INSTALL(TARGETS nginx-ssl-util-noubus RUNTIME DESTINATION bin)
 
 ADD_EXECUTABLE(nginx-ssl-util-nopcre-noubus nginx-util.cpp)
index f63d5f90c0f47deb2417410efe3f0c2891fab524..ab255542b3d58380ea70885f744459861d0c5023 100644 (file)
@@ -1,7 +1,9 @@
 #ifndef __REGEXP_PCRE_HPP
 #define __REGEXP_PCRE_HPP
 
-#include <pcre.h>
+#define PCRE2_CODE_UNIT_WIDTH 8
+
+#include <pcre2.h>
 #include <array>
 #include <stdexcept>
 #include <string>
@@ -65,11 +67,9 @@ class regex {
   private:
     int errcode = 0;
 
-    const char* errptr = nullptr;
-
-    int erroffset = 0;
+    PCRE2_SIZE erroffset = 0;
 
-    pcre* const re = nullptr;
+    pcre2_code* const re = nullptr;
 
     static const std::array<regex_constants::error_type, 86> errcode_pcre2regex;
 
@@ -89,10 +89,18 @@ class regex {
     explicit regex(const std::string& str) : regex(str.c_str()) {}
 
     explicit regex(const char* const str)
-        : re{pcre_compile2(str, 0, &errcode, &errptr, &erroffset, nullptr)}
+        : re{pcre2_compile((PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, &errcode, &erroffset, nullptr)}
     {
         if (re == nullptr) {
-            std::string what = std::string("regex error: ") + errptr + '\n';
+            std::vector<PCRE2_UCHAR> buffer(256);
+            int errlen;
+
+            errlen = pcre2_get_error_message(errcode, buffer.data(), buffer.size());
+            if (errlen < 0)
+                throw regex_error(errcode_pcre2regex.at(errlen));
+
+            std::string what = std::string("regex error: ") +
+                std::string(buffer.data(), buffer.data() + errlen) + '\n';
             what += "    '" + std::string{str} + "'\n";
             what += "     " + std::string(erroffset, ' ') + '^';
 
@@ -103,11 +111,11 @@ class regex {
     ~regex()
     {
         if (re != nullptr) {
-            pcre_free(re);
+            pcre2_code_free(re);
         }
     }
 
-    inline auto operator()() const -> const pcre*
+    inline auto operator()() const -> const pcre2_code*
     {
         return re;
     }
@@ -187,11 +195,19 @@ auto regex_search(std::string::const_iterator begin,
 
 inline auto regex_search(const std::string& subj, const regex& rgx)
 {
+    pcre2_match_data *match_data;
+
     if (rgx() == nullptr) {
         throw std::runtime_error("regex_search error: no regex given");
     }
+
+    match_data = pcre2_match_data_create_from_pattern(rgx(), NULL);
+
     int n =
-        pcre_exec(rgx(), nullptr, subj.c_str(), static_cast<int>(subj.length()), 0, 0, nullptr, 0);
+        pcre2_match(rgx(), (PCRE2_SPTR)subj.c_str(), static_cast<int>(subj.length()), 0, 0, match_data, nullptr);
+
+    pcre2_match_data_free(match_data);
+
     return n >= 0;
 }
 
@@ -205,7 +221,7 @@ auto regex_search(const std::string::const_iterator begin,
     }
 
     int sz = 0;
-    pcre_fullinfo(rgx(), nullptr, PCRE_INFO_CAPTURECOUNT, &sz);
+    pcre2_pattern_info(rgx(), PCRE2_INFO_CAPTURECOUNT, &sz);
     sz = 3 * (sz + 1);
 
     match.vec.reserve(sz);
@@ -216,7 +232,9 @@ auto regex_search(const std::string::const_iterator begin,
     match.begin = begin;
     match.end = end;
 
-    match.n = pcre_exec(rgx(), nullptr, subj, len, 0, 0, &match.vec[0], sz);
+    pcre2_match_data *match_data = pcre2_match_data_create(sz, NULL);
+    match.n = pcre2_match(rgx(), (PCRE2_SPTR)subj, len, 0, 0, match_data, NULL);
+    pcre2_match_data_free(match_data);
 
     if (match.n < 0) {
         return false;
git clone https://git.99rst.org/PROJECT