From f2386ea01f36fd0bf2054dafb656c0826ad51e45 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 26 Sep 2021 18:49:05 +0100 Subject: Add the regex_pcre2 module. --- src/modules/extra/m_regex_pcre2.cpp | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/modules/extra/m_regex_pcre2.cpp (limited to 'src/modules') diff --git a/src/modules/extra/m_regex_pcre2.cpp b/src/modules/extra/m_regex_pcre2.cpp new file mode 100644 index 000000000..a8e9c535a --- /dev/null +++ b/src/modules/extra/m_regex_pcre2.cpp @@ -0,0 +1,92 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2021 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("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 PCRE2Pattern final + : public Regex::Pattern +{ + private: + pcre2_code* regex; + + public: + PCRE2Pattern(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(pattern, reinterpret_cast(errorstr), erroroffset); + } + } + + ~PCRE2Pattern() override + { + pcre2_code_free(regex); + } + + bool IsMatch(const std::string& text) override + { + pcre2_match_data* unused = pcre2_match_data_create(1, 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; + } +}; + +class ModuleRegexPCRE2 final + : public Module +{ + private: + Regex::SimpleEngine regex; + + public: + ModuleRegexPCRE2() + : Module(VF_VENDOR, "Provides the pcre2 regular expression engine which uses the PCRE2 library.") + , regex(this, "pcre2") + { + } +}; + +MODULE_INIT(ModuleRegexPCRE2) -- cgit v1.3.1-10-gc9f91