|
| 1 | +/* |
| 2 | + * ModSecurity, http://www.modsecurity.org/ |
| 3 | + * Copyright (c) 2019 |
| 4 | + * |
| 5 | + * You may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * If any of the files related to licensing are missing or if you have any |
| 11 | + * other questions related to licensing please contact Trustwave Holdings, Inc. |
| 12 | + * directly using the email address security@modsecurity.org. |
| 13 | + * |
| 14 | + */ |
| 15 | +#include <iostream> |
| 16 | +#include <fstream> |
| 17 | +#include <string> |
| 18 | +#include <list> |
| 19 | + |
| 20 | +#include "src/regex/backend/re2.h" |
| 21 | +#include "src/regex/regex_match.h" |
| 22 | + |
| 23 | +namespace modsecurity { |
| 24 | +namespace regex { |
| 25 | +namespace backend { |
| 26 | + |
| 27 | +#ifdef WITH_RE2 |
| 28 | + |
| 29 | +static RE2::Options get_re2_options() { |
| 30 | + RE2::Options res; |
| 31 | + |
| 32 | + res.set_dot_nl(true); |
| 33 | + |
| 34 | + return res; |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +Re2::Re2(const std::string& pattern_) |
| 39 | + : re(pattern_.empty() ? ".*" : pattern_, get_re2_options()) |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +static bool do_match( |
| 44 | + const RE2 &re, |
| 45 | + const char *s, |
| 46 | + size_t n, |
| 47 | + RegexMatch *m, |
| 48 | + ssize_t max_groups, |
| 49 | + size_t offset) |
| 50 | +{ |
| 51 | + if (m == nullptr) { |
| 52 | + max_groups = 0; |
| 53 | + } |
| 54 | + |
| 55 | + // "+1" is required for full match (aka group 0) |
| 56 | + size_t ngroups = re.NumberOfCapturingGroups() + 1; |
| 57 | + if (max_groups >= 0 && max_groups < ngroups) { |
| 58 | + ngroups = max_groups; |
| 59 | + } |
| 60 | + re2::StringPiece submatches[ngroups]; |
| 61 | + |
| 62 | + if (re.Match(re2::StringPiece(s, n), offset, n, RE2::UNANCHORED, |
| 63 | + &submatches[0], ngroups)) { |
| 64 | + if (ngroups != 0) { |
| 65 | + RegexMatch::MatchGroupContainer groups; |
| 66 | + groups.reserve(ngroups); |
| 67 | + for (size_t i = 0; i < ngroups; i++) { |
| 68 | + size_t start = submatches[i].data() - s; |
| 69 | + std::string group = submatches[i].as_string(); |
| 70 | + groups.push_back(MatchGroup{start, std::move(group)}); |
| 71 | + } |
| 72 | + *m = RegexMatch(std::move(groups)); |
| 73 | + } |
| 74 | + return true; |
| 75 | + } |
| 76 | + return false; |
| 77 | +} |
| 78 | + |
| 79 | +std::vector<RegexMatch> Re2::searchAll(const std::string& s, bool overlapping) const { |
| 80 | + std::vector<RegexMatch> res; |
| 81 | + size_t offset = 0; |
| 82 | + |
| 83 | + while (1) { |
| 84 | + RegexMatch m; |
| 85 | + bool match = do_match(re, s.data(), s.size(), &m, -1, offset); |
| 86 | + if (!match) break; |
| 87 | + |
| 88 | + if (overlapping) { |
| 89 | + // start just after the beginning of the last match |
| 90 | + offset = m.group(0).offset + 1; |
| 91 | + } else { |
| 92 | + // start just at the end of the last match |
| 93 | + offset = m.group(0).offset + m.group(0).string.size(); |
| 94 | + if (offset == m.group(0).offset) { |
| 95 | + // empty match - advance by one to not match empty string repeatedly |
| 96 | + offset++; |
| 97 | + } |
| 98 | + } |
| 99 | + res.push_back(std::move(m)); |
| 100 | + } |
| 101 | + return res; |
| 102 | +} |
| 103 | + |
| 104 | +bool Re2::search(const std::string &s, RegexMatch *m, ssize_t max_groups) const { |
| 105 | + return do_match(re, s.data(), s.size(), m, max_groups, 0); |
| 106 | +} |
| 107 | + |
| 108 | +#endif |
| 109 | + |
| 110 | +} // namespace backend |
| 111 | +} // namespace regex |
| 112 | +} // namespace modsecurity |
| 113 | + |
0 commit comments