aboutsummaryrefslogtreecommitdiff
path: root/src/modules/extra/m_regex_posix.cpp
blob: f387ddc4f09e410659d71ac067f9a4b6e96d1706 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2020-2023 Sadie Powell <sadie@witchery.services>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"
#include "modules/regex.h"

#ifdef _WIN32
# include "pcre2posix.h"
# pragma comment(lib, "pcre2-posix.lib")
#else
# include <regex.h>
# include <sys/types.h>
#endif

class POSIXPattern final
	: public Regex::Pattern
{
private:
	regex_t regex;

public:
	POSIXPattern(const Module* mod, const std::string& pattern, uint8_t options)
		: Regex::Pattern(pattern, options)
	{
		int flags = REG_EXTENDED;
		if (options & Regex::OPT_CASE_INSENSITIVE)
			flags |= REG_ICASE;

		int error = regcomp(&regex, pattern.c_str(), flags);
		if (!error)
			return;

		// Retrieve the size of the error message and allocate a buffer.
		size_t errorsize = regerror(error, &regex, nullptr, 0);
		std::vector<char> errormsg(errorsize);

		// Retrieve the error message and free the buffer.
		regerror(error, &regex, errormsg.data(), errormsg.size());
		regfree(&regex);

		throw Regex::Exception(mod, pattern, std::string(errormsg.data(), errormsg.size() - 1));
	}

	~POSIXPattern() override
	{
		regfree(&regex);
	}

	bool IsMatch(const std::string& text) override
	{
		return !regexec(&regex, text.c_str(), 0, nullptr, 0);
	}

	std::optional<Regex::MatchCollection> Matches(const std::string& text) override
	{
		std::vector<regmatch_t> matches(32);
		int result = regexec(&regex, text.c_str(), matches.size(), matches.data(), 0);
		if (result)
			return std::nullopt;

		Regex::Captures captures;
		for (const auto& match : matches)
		{
			if (match.rm_so == -1 || match.rm_eo == -1)
				break;

			captures.emplace_back(text.c_str() + match.rm_so, match.rm_eo - match.rm_so);
		}
		captures.shrink_to_fit();

		// The posix engine does not support named captures.
		static const Regex::NamedCaptures unusednc;

		return Regex::MatchCollection(captures, unusednc);
	}
};

class ModuleRegexPOSIX final
	: public Module
{
private:
	Regex::SimpleEngine<POSIXPattern> regex;

public:
	ModuleRegexPOSIX()
		: Module(VF_VENDOR, "Provides the posix regular expression engine which uses the POSIX.2 regular expression matching system.")
		, regex(this, "posix")
	{
	}
};

MODULE_INIT(ModuleRegexPOSIX)