nginx-util: fix SEGFAULT from regex_search
[feed/packages.git] / net / nginx-util / src / regex-pcre.hpp
1 #ifndef __REGEXP_PCRE_HPP
2 #define __REGEXP_PCRE_HPP
3
4 #define PCRE2_CODE_UNIT_WIDTH 8
5
6 #include <pcre2.h>
7 #include <array>
8 #include <stdexcept>
9 #include <string>
10 #include <vector>
11
12 namespace rgx {
13 /* partially implement the std::regex interface using PCRE for performance
14 * (=> pass "match" as non-const reference)
15 */
16
17 namespace regex_constants {
18 enum error_type {
19 _enum_error_collate,
20 _enum_error_ctype,
21 _enum_error_escape,
22 _enum_error_backref,
23 _enum_error_brack,
24 _enum_error_paren,
25 _enum_error_brace,
26 _enum_error_badbrace,
27 _enum_error_range,
28 _enum_error_space,
29 _enum_error_badrepeat,
30 _enum_error_complexity,
31 _enum_error_stack,
32 _enum_error_last
33 };
34 static const error_type error_collate(_enum_error_collate);
35 static const error_type error_ctype(_enum_error_ctype);
36 static const error_type error_escape(_enum_error_escape);
37 static const error_type error_backref(_enum_error_backref);
38 static const error_type error_brack(_enum_error_brack);
39 static const error_type error_paren(_enum_error_paren);
40 static const error_type error_brace(_enum_error_brace);
41 static const error_type error_badbrace(_enum_error_badbrace);
42 static const error_type error_range(_enum_error_range);
43 static const error_type error_space(_enum_error_space);
44 static const error_type error_badrepeat(_enum_error_badrepeat);
45 static const error_type error_complexity(_enum_error_complexity);
46 static const error_type error_stack(_enum_error_stack);
47 } // namespace regex_constants
48
49 class regex_error : public std::runtime_error {
50 private:
51 regex_constants::error_type errcode;
52
53 public:
54 explicit regex_error(regex_constants::error_type code, const char* what = "regex error")
55 : runtime_error(what), errcode(code)
56 {}
57
58 [[nodiscard]] auto virtual code() const -> regex_constants::error_type;
59 };
60
61 [[nodiscard]] auto regex_error::code() const -> regex_constants::error_type
62 {
63 return errcode;
64 }
65
66 class regex {
67 private:
68 int errcode = 0;
69
70 PCRE2_SIZE erroffset = 0;
71
72 pcre2_code* const re = nullptr;
73
74 static const std::array<regex_constants::error_type, 86> errcode_pcre2regex;
75
76 static const auto BASE = 10;
77
78 public:
79 inline regex() = default;
80
81 inline regex(const regex&) = delete;
82
83 inline regex(regex&&) = default;
84
85 inline auto operator=(const regex&) -> regex& = delete;
86
87 inline auto operator=(regex &&) -> regex& = delete;
88
89 explicit regex(const std::string& str) : regex(str.c_str()) {}
90
91 explicit regex(const char* const str)
92 : re{pcre2_compile((PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, &errcode, &erroffset, nullptr)}
93 {
94 if (re == nullptr) {
95 std::vector<PCRE2_UCHAR> buffer(256);
96 int errlen;
97
98 errlen = pcre2_get_error_message(errcode, buffer.data(), buffer.size());
99 if (errlen < 0)
100 throw regex_error(errcode_pcre2regex.at(errlen));
101
102 std::string what = std::string("regex error: ") +
103 std::string(buffer.data(), buffer.data() + errlen) + '\n';
104 what += " '" + std::string{str} + "'\n";
105 what += " " + std::string(erroffset, ' ') + '^';
106
107 throw regex_error(errcode_pcre2regex.at(errcode), what.c_str());
108 }
109 }
110
111 ~regex()
112 {
113 if (re != nullptr) {
114 pcre2_code_free(re);
115 }
116 }
117
118 inline auto operator()() const -> const pcre2_code*
119 {
120 return re;
121 }
122 };
123
124 class smatch {
125 friend auto regex_search(std::string::const_iterator begin,
126 std::string::const_iterator end,
127 smatch& match, // NOLINT(google-runtime-references)
128 const regex& rgx); // match std::regex interface.
129
130 private:
131 std::string::const_iterator begin;
132
133 std::string::const_iterator end;
134
135 std::vector<PCRE2_SIZE> vec{};
136
137 int n = 0;
138
139 public:
140 [[nodiscard]] inline auto position(int i = 0) const
141 {
142 return (i < 0 || i >= n) ? std::string::npos : vec[2 * i];
143 }
144
145 [[nodiscard]] inline auto length(int i = 0) const
146 {
147 return (i < 0 || i >= n) ? 0 : vec[2 * i + 1] - vec[2 * i];
148 }
149
150 [[nodiscard]] auto str(int i = 0) const -> std::string
151 { // should we throw?
152 if (i < 0 || i >= n) {
153 return "";
154 }
155 int x = vec[2 * i];
156 if (x < 0) {
157 return "";
158 }
159 int y = vec[2 * i + 1];
160 return std::string{begin + x, begin + y};
161 }
162
163 [[nodiscard]] auto format(const std::string& fmt) const;
164
165 [[nodiscard]] auto size() const -> int
166 {
167 return n;
168 }
169
170 [[nodiscard]] inline auto empty() const
171 {
172 return n < 0;
173 }
174
175 [[nodiscard]] inline auto ready() const
176 {
177 return !vec.empty();
178 }
179 };
180
181 inline auto regex_search(const std::string& subj, const regex& rgx);
182
183 auto regex_replace(const std::string& subj, const regex& rgx, const std::string& insert);
184
185 inline auto regex_search(const std::string& subj,
186 smatch& match, // NOLINT(google-runtime-references)
187 const regex& rgx); // match std::regex interface.
188
189 auto regex_search(std::string::const_iterator begin,
190 std::string::const_iterator end,
191 smatch& match, // NOLINT(google-runtime-references)
192 const regex& rgx); // match std::regex interface.
193
194 // ------------------------- implementation: ----------------------------------
195
196 inline auto regex_search(const std::string& subj, const regex& rgx)
197 {
198 pcre2_match_data *match_data;
199
200 if (rgx() == nullptr) {
201 throw std::runtime_error("regex_search error: no regex given");
202 }
203
204 match_data = pcre2_match_data_create_from_pattern(rgx(), NULL);
205
206 int n =
207 pcre2_match(rgx(), (PCRE2_SPTR)subj.c_str(), static_cast<int>(subj.length()), 0, 0, match_data, nullptr);
208
209 pcre2_match_data_free(match_data);
210
211 return n >= 0;
212 }
213
214 auto regex_search(const std::string::const_iterator begin,
215 const std::string::const_iterator end,
216 smatch& match,
217 const regex& rgx)
218 {
219 if (rgx() == nullptr) {
220 throw std::runtime_error("regex_search error: no regex given");
221 }
222
223 int sz = 0;
224 pcre2_pattern_info(rgx(), PCRE2_INFO_CAPTURECOUNT, &sz);
225 sz = 3 * (sz + 1);
226
227 match.vec.reserve(sz);
228
229 const char* subj = &*begin;
230 int n, len = static_cast<int>(&*end - subj);
231 unsigned int ov_count;
232 PCRE2_SIZE *ov;
233
234 match.begin = begin;
235 match.end = end;
236
237 pcre2_match_data *match_data = pcre2_match_data_create(sz, NULL);
238 n = pcre2_match(rgx(), (PCRE2_SPTR)subj, len, 0, 0, match_data, NULL);
239 ov = pcre2_get_ovector_pointer(match_data);
240 ov_count = pcre2_get_ovector_count(match_data);
241
242 match.vec.assign(ov, ov + ov_count);
243 match.n = n;
244
245 pcre2_match_data_free(match_data);
246
247 if (match.n < 0) {
248 return false;
249 }
250 if (match.n == 0) {
251 match.n = sz / 3;
252 }
253
254 return true;
255 }
256
257 inline auto regex_search(const std::string& subj, smatch& match, const regex& rgx)
258 {
259 return regex_search(subj.begin(), subj.end(), match, rgx);
260 }
261
262 auto smatch::format(const std::string& fmt) const
263 {
264 std::string ret{};
265 size_t index = 0;
266
267 size_t pos = 0;
268 while ((pos = fmt.find('$', index)) != std::string::npos) {
269 ret.append(fmt, index, pos - index);
270 index = pos + 1;
271
272 char chr = fmt[index++];
273 switch (chr) {
274 case '&': // match
275 ret += str(0);
276 break;
277
278 case '`': // prefix
279 ret.append(begin, begin + vec[0]);
280 break;
281
282 case '\'': // suffix
283 ret.append(begin + vec[1], end);
284 break;
285
286 default:
287 if (isdigit(chr) != 0) { // one or two digits => submatch:
288 int num = chr - '0';
289 chr = fmt[index];
290 if (isdigit(chr) != 0) { // second digit:
291 ++index;
292 static const auto base = 10;
293 num = num * base + chr - '0';
294 }
295 ret += str(num);
296 break;
297 } // else:
298
299 ret += '$';
300 [[fallthrough]];
301
302 case '$': // escaped
303 ret += chr;
304 }
305 }
306 ret.append(fmt, index);
307 return ret;
308 }
309
310 auto regex_replace(const std::string& subj, const regex& rgx, const std::string& insert)
311 {
312 if (rgx() == nullptr) {
313 throw std::runtime_error("regex_replace error: no regex given");
314 }
315
316 std::string ret{};
317 auto pos = subj.begin();
318
319 for (smatch match; regex_search(pos, subj.end(), match, rgx);
320 pos += match.position(0) + match.length(0))
321 {
322 ret.append(pos, pos + match.position(0));
323 ret.append(match.format(insert));
324 }
325
326 ret.append(pos, subj.end());
327 return ret;
328 }
329
330 // ------------ There is only the translation table below : -------------------
331
332 const std::array<regex_constants::error_type, 86> regex::errcode_pcre2regex = {
333 // 0 no error
334 regex_constants::error_type::_enum_error_last,
335 // 1 \ at end of pattern
336 regex_constants::error_escape,
337 // 2 \c at end of pattern
338 regex_constants::error_escape,
339 // 3 unrecognized character follows \ .
340 regex_constants::error_escape,
341 // 4 numbers out of order in {} quantifier
342 regex_constants::error_badbrace,
343 // 5 number too big in {} quantifier
344 regex_constants::error_badbrace,
345 // 6 missing terminating for character class
346 regex_constants::error_brack,
347 // 7 invalid escape sequence in character class
348 regex_constants::error_escape,
349 // 8 range out of order in character class
350 regex_constants::error_range,
351 // 9 nothing to repeat
352 regex_constants::error_badrepeat,
353 // 10 [this code is not in use
354 regex_constants::error_type::_enum_error_last,
355 // 11 internal error: unexpected repeat
356 regex_constants::error_badrepeat,
357 // 12 unrecognized character after (? or (?-
358 regex_constants::error_backref,
359 // 13 POSIX named classes are supported only within a class
360 regex_constants::error_range,
361 // 14 missing )
362 regex_constants::error_paren,
363 // 15 reference to non-existent subpattern
364 regex_constants::error_backref,
365 // 16 erroffset passed as NULL
366 regex_constants::error_type::_enum_error_last,
367 // 17 unknown option bit(s) set
368 regex_constants::error_type::_enum_error_last,
369 // 18 missing ) after comment
370 regex_constants::error_paren,
371 // 19 [this code is not in use
372 regex_constants::error_type::_enum_error_last,
373 // 20 regular expression is too large
374 regex_constants::error_space,
375 // 21 failed to get memory
376 regex_constants::error_stack,
377 // 22 unmatched parentheses
378 regex_constants::error_paren,
379 // 23 internal error: code overflow
380 regex_constants::error_stack,
381 // 24 unrecognized character after (?<
382 regex_constants::error_backref,
383 // 25 lookbehind assertion is not fixed length
384 regex_constants::error_backref,
385 // 26 malformed number or name after (?(
386 regex_constants::error_backref,
387 // 27 conditional group contains more than two branches
388 regex_constants::error_backref,
389 // 28 assertion expected after (?(
390 regex_constants::error_backref,
391 // 29 (?R or (?[+-digits must be followed by )
392 regex_constants::error_backref,
393 // 30 unknown POSIX class name
394 regex_constants::error_ctype,
395 // 31 POSIX collating elements are not supported
396 regex_constants::error_collate,
397 // 32 this version of PCRE is compiled without UTF support
398 regex_constants::error_collate,
399 // 33 [this code is not in use
400 regex_constants::error_type::_enum_error_last,
401 // 34 character value in \x{} or \o{} is too large
402 regex_constants::error_escape,
403 // 35 invalid condition (?(0)
404 regex_constants::error_backref,
405 // 36 \C not allowed in lookbehind assertion
406 regex_constants::error_escape,
407 // 37 PCRE does not support \L, \l, \N{name}, \U, or \u
408 regex_constants::error_escape,
409 // 38 number after (?C is > 255
410 regex_constants::error_backref,
411 // 39 closing ) for (?C expected
412 regex_constants::error_paren,
413 // 40 recursive call could loop indefinitely
414 regex_constants::error_complexity,
415 // 41 unrecognized character after (?P
416 regex_constants::error_backref,
417 // 42 syntax error in subpattern name (missing terminator)
418 regex_constants::error_paren,
419 // 43 two named subpatterns have the same name
420 regex_constants::error_backref,
421 // 44 invalid UTF-8 string (specifically UTF-8)
422 regex_constants::error_collate,
423 // 45 support for \P, \p, and \X has not been compiled
424 regex_constants::error_escape,
425 // 46 malformed \P or \p sequence
426 regex_constants::error_escape,
427 // 47 unknown property name after \P or \p
428 regex_constants::error_escape,
429 // 48 subpattern name is too long (maximum 32 characters)
430 regex_constants::error_backref,
431 // 49 too many named subpatterns (maximum 10000)
432 regex_constants::error_complexity,
433 // 50 [this code is not in use
434 regex_constants::error_type::_enum_error_last,
435 // 51 octal value is greater than \377 in 8-bit non-UTF-8 mode
436 regex_constants::error_escape,
437 // 52 internal error: overran compiling workspace
438 regex_constants::error_type::_enum_error_last,
439 // 53 internal error: previously-checked referenced subpattern not found
440 regex_constants::error_type::_enum_error_last,
441 // 54 DEFINE group contains more than one branch
442 regex_constants::error_backref,
443 // 55 repeating a DEFINE group is not allowed
444 regex_constants::error_backref,
445 // 56 inconsistent NEWLINE options
446 regex_constants::error_escape,
447 // 57 \g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain
448 // number
449 regex_constants::error_backref,
450 // 58 a numbered reference must not be zero
451 regex_constants::error_backref,
452 // 59 an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)
453 regex_constants::error_complexity,
454 // 60 (*VERB) not recognized or malformed
455 regex_constants::error_complexity,
456 // 61 number is too big
457 regex_constants::error_complexity,
458 // 62 subpattern name expected
459 regex_constants::error_backref,
460 // 63 digit expected after (?+
461 regex_constants::error_backref,
462 // 64 is an invalid data character in JavaScript compatibility mode
463 regex_constants::error_escape,
464 // 65 different names for subpatterns of the same number are not allowed
465 regex_constants::error_backref,
466 // 66 (*MARK) must have an argument
467 regex_constants::error_complexity,
468 // 67 this version of PCRE is not compiled with Unicode property support
469 regex_constants::error_collate,
470 // 68 \c must be followed by an ASCII character
471 regex_constants::error_escape,
472 // 69 \k is not followed by a braced, angle-bracketed, or quoted name
473 regex_constants::error_backref,
474 // 70 internal error: unknown opcode in find_fixedlength()
475 regex_constants::error_type::_enum_error_last,
476 // 71 \N is not supported in a class
477 regex_constants::error_ctype,
478 // 72 too many forward references
479 regex_constants::error_backref,
480 // 73 disallowed Unicode code point (>= 0xd800 && <= 0xdfff)
481 regex_constants::error_escape,
482 // 74 invalid UTF-16 string (specifically UTF-16)
483 regex_constants::error_collate,
484 // 75 name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)
485 regex_constants::error_complexity,
486 // 76 character value in \u.... sequence is too large
487 regex_constants::error_escape,
488 // 77 invalid UTF-32 string (specifically UTF-32)
489 regex_constants::error_collate,
490 // 78 setting UTF is disabled by the application
491 regex_constants::error_collate,
492 // 79 non-hex character in \x{} (closing brace missing?)
493 regex_constants::error_escape,
494 // 80 non-octal character in \o{} (closing brace missing?)
495 regex_constants::error_escape,
496 // 81 missing opening brace after \o
497 regex_constants::error_brace,
498 // 82 parentheses are too deeply nested
499 regex_constants::error_complexity,
500 // 83 invalid range in character class
501 regex_constants::error_range,
502 // 84 group name must start with a non-digit
503 regex_constants::error_backref,
504 // 85 parentheses are too deeply nested (stack check)
505 regex_constants::error_stack};
506
507 } // namespace rgx
508
509 #endif