From c18499a33208aa92f627e4710ae0bfafea0e31ed Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Fri, 7 Jun 2024 19:19:19 +0100 Subject: Rename regex_pcre back to regex_pcre2. This rename happened before we had config compatibility and it doesn't make sense to keep it anymore. --- src/modules/extra/README | 2 +- src/modules/extra/m_regex_pcre.cpp | 148 ------------------------------------ src/modules/extra/m_regex_pcre2.cpp | 148 ++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 149 deletions(-) delete mode 100644 src/modules/extra/m_regex_pcre.cpp create mode 100644 src/modules/extra/m_regex_pcre2.cpp (limited to 'src/modules') diff --git a/src/modules/extra/README b/src/modules/extra/README index 7e3c3e99c..5cdb675d7 100644 --- a/src/modules/extra/README +++ b/src/modules/extra/README @@ -1,5 +1,5 @@ This directory stores modules which require external libraries to compile. -For example, m_regex_pcre requires the PCRE2 library. +For example, m_regex_pcre2 requires the PCRE2 library. To compile any of these modules first ensure you have the required dependencies (read the online documentation at https://docs.inspircd.org) and then symlink diff --git a/src/modules/extra/m_regex_pcre.cpp b/src/modules/extra/m_regex_pcre.cpp deleted file mode 100644 index 35bc39b0f..000000000 --- a/src/modules/extra/m_regex_pcre.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2021-2023 Sadie Powell - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/// $CompilerFlags: find_compiler_flags("libpcre2-8") -/// $LinkerFlags: find_linker_flags("libpcre2-8") - -/// $PackageInfo: require_system("arch") pcre2 -/// $PackageInfo: require_system("centos") pcre2-devel -/// $PackageInfo: require_system("darwin") pcre2 -/// $PackageInfo: require_system("debian") libpcre2-dev -/// $PackageInfo: require_system("rocky") pcre2-devel -/// $PackageInfo: require_system("ubuntu") libpcre2-dev - - -#include "inspircd.h" -#include "modules/regex.h" - -#define PCRE2_CODE_UNIT_WIDTH 8 -#include - -#ifdef _WIN32 -# pragma comment(lib, "pcre2-8.lib") -#endif - -class PCREPattern final - : public Regex::Pattern -{ -private: - pcre2_code* regex; - -public: - PCREPattern(const Module* mod, const std::string& pattern, uint8_t options) - : Regex::Pattern(pattern, options) - { - int flags = 0; - if (options & Regex::OPT_CASE_INSENSITIVE) - flags |= PCRE2_CASELESS; - - int errorcode; - PCRE2_SIZE erroroffset; - regex = pcre2_compile(reinterpret_cast(pattern.c_str()), pattern.length(), flags, &errorcode, &erroroffset, nullptr); - if (!regex) - { - PCRE2_UCHAR errorstr[128]; - pcre2_get_error_message(errorcode, errorstr, sizeof errorstr); - throw Regex::Exception(mod, pattern, reinterpret_cast(errorstr), erroroffset); - } - } - - ~PCREPattern() override - { - pcre2_code_free(regex); - } - - bool IsMatch(const std::string& text) override - { - pcre2_match_data* unused = pcre2_match_data_create_from_pattern(regex, nullptr); - int result = pcre2_match(regex, reinterpret_cast(text.c_str()), text.length(), 0, 0, unused, nullptr); - pcre2_match_data_free(unused); - return result >= 0; - } - - std::optional Matches(const std::string& text) override - { - pcre2_match_data* data = pcre2_match_data_create_from_pattern(regex, nullptr); - int result = pcre2_match(regex, reinterpret_cast(text.c_str()), text.length(), 0, 0, data, nullptr); - if (result < 0) - return std::nullopt; - - PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(data); - - uint32_t capturecount; - Regex::Captures captures; - if (!pcre2_pattern_info(regex, PCRE2_INFO_CAPTURECOUNT, &capturecount) && capturecount) - { - for (uint32_t idx = 0; idx <= capturecount; ++idx) - { - PCRE2_UCHAR* bufferptr; - PCRE2_SIZE bufferlen; - if (!pcre2_substring_get_bynumber(data, idx, &bufferptr, &bufferlen)) - captures.emplace_back(reinterpret_cast(bufferptr), bufferlen); - } - } - - uint32_t namedcapturecount; - Regex::NamedCaptures namedcaptures; - if (!pcre2_pattern_info(regex, PCRE2_INFO_NAMECOUNT, &namedcapturecount) && namedcapturecount) - { - uint32_t nameentrysize; - PCRE2_SPTR nametable; - if (!pcre2_pattern_info(regex, PCRE2_INFO_NAMEENTRYSIZE, &nameentrysize) - && !pcre2_pattern_info(regex, PCRE2_INFO_NAMETABLE, &nametable)) - { - for (uint32_t idx = 0; idx < namedcapturecount; ++idx) - { - int matchidx = (nametable[0] << 8) | nametable[1]; - const std::string matchname(reinterpret_cast(nametable + 2), nameentrysize - 3); - const std::string matchvalue(text.c_str() + ovector[2 * matchidx], ovector[ 2 * matchidx + 1] - ovector[2 * matchidx]); - namedcaptures.emplace(matchname, matchvalue); - nametable += nameentrysize; - } - } - } - - return Regex::MatchCollection(captures, namedcaptures); - } -}; - -class ModuleRegexPCRE final - : public Module -{ -private: - Regex::SimpleEngine regex; - -public: - ModuleRegexPCRE() - : Module(VF_VENDOR, "Provides the pcre regular expression engine which uses the PCRE2 library.") - , regex(this, "pcre") - { - } - - void init() override - { - std::vector version(16); - if (pcre2_config(PCRE2_CONFIG_VERSION, version.data()) < 0) - return; - - ServerInstance->Logs.Normal(MODNAME, "Module was compiled against PCRE2 version {}.{} and is running against version {}", - PCRE2_MAJOR, PCRE2_MINOR, version.data()); - } -}; - -MODULE_INIT(ModuleRegexPCRE) diff --git a/src/modules/extra/m_regex_pcre2.cpp b/src/modules/extra/m_regex_pcre2.cpp new file mode 100644 index 000000000..35bc39b0f --- /dev/null +++ b/src/modules/extra/m_regex_pcre2.cpp @@ -0,0 +1,148 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2021-2023 Sadie Powell + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/// $CompilerFlags: find_compiler_flags("libpcre2-8") +/// $LinkerFlags: find_linker_flags("libpcre2-8") + +/// $PackageInfo: require_system("arch") pcre2 +/// $PackageInfo: require_system("centos") pcre2-devel +/// $PackageInfo: require_system("darwin") pcre2 +/// $PackageInfo: require_system("debian") libpcre2-dev +/// $PackageInfo: require_system("rocky") pcre2-devel +/// $PackageInfo: require_system("ubuntu") libpcre2-dev + + +#include "inspircd.h" +#include "modules/regex.h" + +#define PCRE2_CODE_UNIT_WIDTH 8 +#include + +#ifdef _WIN32 +# pragma comment(lib, "pcre2-8.lib") +#endif + +class PCREPattern final + : public Regex::Pattern +{ +private: + pcre2_code* regex; + +public: + PCREPattern(const Module* mod, const std::string& pattern, uint8_t options) + : Regex::Pattern(pattern, options) + { + int flags = 0; + if (options & Regex::OPT_CASE_INSENSITIVE) + flags |= PCRE2_CASELESS; + + int errorcode; + PCRE2_SIZE erroroffset; + regex = pcre2_compile(reinterpret_cast(pattern.c_str()), pattern.length(), flags, &errorcode, &erroroffset, nullptr); + if (!regex) + { + PCRE2_UCHAR errorstr[128]; + pcre2_get_error_message(errorcode, errorstr, sizeof errorstr); + throw Regex::Exception(mod, pattern, reinterpret_cast(errorstr), erroroffset); + } + } + + ~PCREPattern() override + { + pcre2_code_free(regex); + } + + bool IsMatch(const std::string& text) override + { + pcre2_match_data* unused = pcre2_match_data_create_from_pattern(regex, nullptr); + int result = pcre2_match(regex, reinterpret_cast(text.c_str()), text.length(), 0, 0, unused, nullptr); + pcre2_match_data_free(unused); + return result >= 0; + } + + std::optional Matches(const std::string& text) override + { + pcre2_match_data* data = pcre2_match_data_create_from_pattern(regex, nullptr); + int result = pcre2_match(regex, reinterpret_cast(text.c_str()), text.length(), 0, 0, data, nullptr); + if (result < 0) + return std::nullopt; + + PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(data); + + uint32_t capturecount; + Regex::Captures captures; + if (!pcre2_pattern_info(regex, PCRE2_INFO_CAPTURECOUNT, &capturecount) && capturecount) + { + for (uint32_t idx = 0; idx <= capturecount; ++idx) + { + PCRE2_UCHAR* bufferptr; + PCRE2_SIZE bufferlen; + if (!pcre2_substring_get_bynumber(data, idx, &bufferptr, &bufferlen)) + captures.emplace_back(reinterpret_cast(bufferptr), bufferlen); + } + } + + uint32_t namedcapturecount; + Regex::NamedCaptures namedcaptures; + if (!pcre2_pattern_info(regex, PCRE2_INFO_NAMECOUNT, &namedcapturecount) && namedcapturecount) + { + uint32_t nameentrysize; + PCRE2_SPTR nametable; + if (!pcre2_pattern_info(regex, PCRE2_INFO_NAMEENTRYSIZE, &nameentrysize) + && !pcre2_pattern_info(regex, PCRE2_INFO_NAMETABLE, &nametable)) + { + for (uint32_t idx = 0; idx < namedcapturecount; ++idx) + { + int matchidx = (nametable[0] << 8) | nametable[1]; + const std::string matchname(reinterpret_cast(nametable + 2), nameentrysize - 3); + const std::string matchvalue(text.c_str() + ovector[2 * matchidx], ovector[ 2 * matchidx + 1] - ovector[2 * matchidx]); + namedcaptures.emplace(matchname, matchvalue); + nametable += nameentrysize; + } + } + } + + return Regex::MatchCollection(captures, namedcaptures); + } +}; + +class ModuleRegexPCRE final + : public Module +{ +private: + Regex::SimpleEngine regex; + +public: + ModuleRegexPCRE() + : Module(VF_VENDOR, "Provides the pcre regular expression engine which uses the PCRE2 library.") + , regex(this, "pcre") + { + } + + void init() override + { + std::vector version(16); + if (pcre2_config(PCRE2_CONFIG_VERSION, version.data()) < 0) + return; + + ServerInstance->Logs.Normal(MODNAME, "Module was compiled against PCRE2 version {}.{} and is running against version {}", + PCRE2_MAJOR, PCRE2_MINOR, version.data()); + } +}; + +MODULE_INIT(ModuleRegexPCRE) -- cgit v1.3.1-10-gc9f91