|
| 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 [email protected]. |
| 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 | + : pattern(pattern_.empty() ? ".*" : pattern_), |
| 40 | + re(pattern, get_re2_options()) |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +std::list<RegexMatch> Re2::searchAll(const std::string& s) const { |
| 45 | + std::list<RegexMatch> retList; |
| 46 | + |
| 47 | + re2::StringPiece subject(s); |
| 48 | + |
| 49 | + size_t offset = 0; |
| 50 | + while (offset <= s.size()) { |
| 51 | + int ngroups = re.NumberOfCapturingGroups() + 1; |
| 52 | + re2::StringPiece submatches[ngroups]; |
| 53 | + |
| 54 | + if (!re.Match(subject, offset, s.size(), RE2::UNANCHORED, |
| 55 | + &submatches[0], ngroups)) { |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + for (int i = 0; i < ngroups; i++) { |
| 60 | + // N.B. StringPiece::as_string returns value, not reference |
| 61 | + auto match_string = submatches[i].as_string(); |
| 62 | + auto start = &submatches[i][0] - &subject[0]; |
| 63 | + retList.push_front(RegexMatch(std::move(match_string), start)); |
| 64 | + } |
| 65 | + |
| 66 | + offset = (&submatches[0][0] - &subject[0]) + submatches[0].length(); |
| 67 | + if (submatches[0].size() == 0) { |
| 68 | + offset++; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return retList; |
| 73 | +} |
| 74 | + |
| 75 | +bool Re2::searchOneMatch(const std::string& s, std::vector<RegexMatchCapture>& captures) const { |
| 76 | + re2::StringPiece subject(s); |
| 77 | + int ngroups = re.NumberOfCapturingGroups() + 1; |
| 78 | + re2::StringPiece submatches[ngroups]; |
| 79 | + |
| 80 | + if (re.Match(subject, 0, s.size(), RE2::UNANCHORED, &submatches[0], ngroups)) { |
| 81 | + for (int i = 0; i < ngroups; i++) { |
| 82 | + auto len = submatches[i].length(); |
| 83 | + auto start = len != 0 ? &submatches[i][0] - &subject[0] : 0; |
| 84 | + captures.push_back(RegexMatchCapture(i, start, len)); |
| 85 | + } |
| 86 | + return true; |
| 87 | + } else { |
| 88 | + return false; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +int Re2::search(const std::string& s, RegexMatch *match) const { |
| 93 | + re2::StringPiece subject(s); |
| 94 | + re2::StringPiece submatches[1]; |
| 95 | + if (re.Match(subject, 0, s.size(), RE2::UNANCHORED, &submatches[0], 1)) { |
| 96 | + // N.B. StringPiece::as_string returns value, not reference |
| 97 | + auto match_string = submatches[0].as_string(); |
| 98 | + auto start = &submatches[0][0] - &subject[0]; |
| 99 | + *match = RegexMatch(std::move(match_string), start); |
| 100 | + return 1; |
| 101 | + } else { |
| 102 | + return 0; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +int Re2::search(const std::string& s) const { |
| 107 | + re2::StringPiece subject(s); |
| 108 | + return re.Match(subject, 0, s.size(), RE2::UNANCHORED, NULL, 0); |
| 109 | +} |
| 110 | +#endif |
| 111 | + |
| 112 | +} // namespace backend |
| 113 | +} // namespace regex |
| 114 | +} // namespace modsecurity |
| 115 | + |
0 commit comments