|
13 | 13 | *
|
14 | 14 | */
|
15 | 15 |
|
16 |
| -#include <pcre.h> |
17 | 16 |
|
18 | 17 | #include <iostream>
|
19 | 18 | #include <fstream>
|
20 | 19 | #include <string>
|
21 | 20 | #include <list>
|
| 21 | +#include <pcre.h> |
| 22 | + |
| 23 | +#include "src/regex/backend/pcre.h" |
| 24 | +#include "src/regex/regex_match.h" |
22 | 25 |
|
23 |
| -#ifndef SRC_REGEX_BACKEND_PCRE_H_ |
24 |
| -#define SRC_REGEX_BACKEND_PCRE_H_ |
25 | 26 |
|
26 | 27 | namespace modsecurity {
|
27 | 28 | namespace regex {
|
| 29 | +namespace backend { |
| 30 | + |
| 31 | + |
| 32 | +#if PCRE_HAVE_JIT |
| 33 | +#define pcre_study_opt PCRE_STUDY_JIT_COMPILE |
| 34 | +#else |
| 35 | +#define pcre_study_opt 0 |
| 36 | +#endif |
| 37 | + |
| 38 | + |
| 39 | +Pcre::Pcre(const std::string& pattern_) |
| 40 | + : pattern(pattern_.empty() ? ".*" : pattern_) { |
| 41 | + const char *errptr = NULL; |
| 42 | + int erroffset; |
| 43 | + |
| 44 | + m_pc = pcre_compile(pattern.c_str(), PCRE_DOTALL|PCRE_MULTILINE, |
| 45 | + &errptr, &erroffset, NULL); |
| 46 | + |
| 47 | + m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +Pcre::~Pcre() { |
| 52 | + if (m_pc != NULL) { |
| 53 | + pcre_free(m_pc); |
| 54 | + m_pc = NULL; |
| 55 | + } |
| 56 | + if (m_pce != NULL) { |
| 57 | +#if PCRE_HAVE_JIT |
| 58 | + pcre_free_study(m_pce); |
| 59 | +#else |
| 60 | + pcre_free(m_pce); |
| 61 | +#endif |
| 62 | + m_pce = NULL; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +std::list<RegexMatch> Pcre::searchAll(const std::string& s) const { |
| 68 | + const char *subject = s.c_str(); |
| 69 | + const std::string tmpString = std::string(s.c_str(), s.size()); |
| 70 | + int ovector[OVECCOUNT]; |
| 71 | + int rc, i, offset = 0; |
| 72 | + std::list<RegexMatch> retList; |
| 73 | + |
| 74 | + do { |
| 75 | + rc = pcre_exec(m_pc, m_pce, subject, |
| 76 | + s.size(), offset, 0, ovector, OVECCOUNT); |
28 | 77 |
|
| 78 | + for (i = 0; i < rc; i++) { |
| 79 | + size_t start = ovector[2*i]; |
| 80 | + size_t end = ovector[2*i+1]; |
| 81 | + size_t len = end - start; |
| 82 | + if (end > s.size()) { |
| 83 | + rc = 0; |
| 84 | + break; |
| 85 | + } |
| 86 | + std::string match = std::string(tmpString, start, len); |
| 87 | + offset = start + len; |
| 88 | + retList.push_front(RegexMatch(match, start)); |
29 | 89 |
|
| 90 | + if (len == 0) { |
| 91 | + rc = 0; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } while (rc > 0); |
30 | 96 |
|
| 97 | + return retList; |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +bool Pcre::searchOneMatch(const std::string& s, std::vector<RegexMatchCapture>& captures) const { |
| 102 | + const char *subject = s.c_str(); |
| 103 | + int ovector[OVECCOUNT]; |
| 104 | + |
| 105 | + int rc = pcre_exec(m_pc, m_pce, subject, s.size(), 0, 0, ovector, OVECCOUNT); |
| 106 | + |
| 107 | + for (int i = 0; i < rc; i++) { |
| 108 | + size_t start = ovector[2*i]; |
| 109 | + size_t end = ovector[2*i+1]; |
| 110 | + size_t len = end - start; |
| 111 | + if (end > s.size()) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + RegexMatchCapture capture(i, start, len); |
| 115 | + captures.push_back(capture); |
| 116 | + } |
| 117 | + |
| 118 | + return (rc > 0); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +int Pcre::search(const std::string& s, RegexMatch *match) const { |
| 123 | + int ovector[OVECCOUNT]; |
| 124 | + int ret = pcre_exec(m_pc, m_pce, s.c_str(), |
| 125 | + s.size(), 0, 0, ovector, OVECCOUNT) > 0; |
| 126 | + |
| 127 | + if (ret > 0) { |
| 128 | + *match = RegexMatch( |
| 129 | + std::string(s, ovector[ret-1], ovector[ret] - ovector[ret-1]), |
| 130 | + 0); |
| 131 | + } |
| 132 | + |
| 133 | + return ret; |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +int Pcre::search(const std::string& s) const { |
| 138 | + int ovector[OVECCOUNT]; |
| 139 | + return pcre_exec(m_pc, m_pce, s.c_str(), |
| 140 | + s.size(), 0, 0, ovector, OVECCOUNT) > 0; |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +} // namespace backend |
31 | 145 | } // namespace regex
|
32 | 146 | } // namespace modsecurity
|
33 | 147 |
|
34 |
| - |
35 |
| -#endif // SRC_REGEX_BACKEND_PCRE_H_ |
|
0 commit comments