| 1 |
// RegExp class - search text using regular expressions |
| 2 |
|
| 3 |
// For all support, instructions and copyright go to: |
| 4 |
// http://e2guardian.org/ |
| 5 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 6 |
|
| 7 |
// INCLUDES |
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H |
| 10 |
#include "e2config.h" |
| 11 |
#endif |
| 12 |
#include "RegExp.hpp" |
| 13 |
#include "Logger.hpp" |
| 14 |
|
| 15 |
#include <cstring> |
| 16 |
#include <iostream> |
| 17 |
|
| 18 |
|
| 19 |
RegResult::RegResult() |
| 20 |
: imatched(false) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
// destructor - free regex if compiled |
| 26 |
RegResult::~RegResult() |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
// return the i'th match result |
| 31 |
std::string RegResult::result(int i) |
| 32 |
{ |
| 33 |
if (i >= (signed)results.size() || i < 0) { // reality check |
| 34 |
return ""; // maybe exception? |
| 35 |
} |
| 36 |
return results[i]; |
| 37 |
} |
| 38 |
|
| 39 |
// get the position of the i'th match result in the overall text |
| 40 |
unsigned int RegResult::offset(int i) |
| 41 |
{ |
| 42 |
if (i >= (signed)offsets.size() || i < 0) { // reality check |
| 43 |
return 0; // maybe exception? |
| 44 |
} |
| 45 |
return offsets[i]; |
| 46 |
} |
| 47 |
|
| 48 |
// get the length of the i'th match |
| 49 |
unsigned int RegResult::length(int i) |
| 50 |
{ |
| 51 |
if (i >= (signed)lengths.size() || i < 0) { // reality check |
| 52 |
return 0; // maybe exception? |
| 53 |
} |
| 54 |
return lengths[i]; |
| 55 |
} |
| 56 |
|
| 57 |
// how many matches did the last run generate? |
| 58 |
int RegResult::numberOfMatches() |
| 59 |
{ |
| 60 |
int i = (signed)results.size(); |
| 61 |
return i; |
| 62 |
} |
| 63 |
|
| 64 |
// did it, in fact, generate any? |
| 65 |
bool RegResult::matched() |
| 66 |
{ |
| 67 |
return imatched; // regexp matches only - not search/replace |
| 68 |
} |
| 69 |
|
| 70 |
// constructor - set defaults |
| 71 |
RegExp::RegExp() |
| 72 |
: reg(), wascompiled(false) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
// copy constructor |
| 77 |
RegExp::RegExp(const RegExp &r) |
| 78 |
{ |
| 79 |
// rs.results.clear(); |
| 80 |
//nrs.offsets.clear(); |
| 81 |
//nrs.lengths.clear(); |
| 82 |
//unsigned int i; |
| 83 |
//for (i = 0; i < rs.results.size(); i++) { |
| 84 |
//nrs.results.push_back(rs.results[i]); |
| 85 |
//} |
| 86 |
//for (i = 0; i < rs.offsets.size(); i++) { |
| 87 |
//nrs.offsets.push_back(rs.offsets[i]); |
| 88 |
//} |
| 89 |
//for (i = 0; i < rs.lengths.size(); i++) { |
| 90 |
//nrs.lengths.push_back(rs.lengths[i]); |
| 91 |
//} |
| 92 |
|
| 93 |
//nrs.imatched = rs.imatched; |
| 94 |
wascompiled = r.wascompiled; |
| 95 |
searchstring = r.searchstring; |
| 96 |
if (wascompiled == true) { |
| 97 |
#ifdef HAVE_PCRE |
| 98 |
if (regcomp(®, searchstring.c_str(), REG_ICASE | REG_EXTENDED | REG_DOTALL) != 0) { |
| 99 |
#else |
| 100 |
if (regcomp(®, searchstring.c_str(), REG_ICASE | REG_EXTENDED) != 0) { |
| 101 |
#endif |
| 102 |
regfree(®); |
| 103 |
//rs.imatched = false; |
| 104 |
wascompiled = false; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// destructor - free regex if compiled |
| 110 |
RegExp::~RegExp() |
| 111 |
{ |
| 112 |
if (wascompiled) { |
| 113 |
regfree(®); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// compile the given regular expression |
| 118 |
bool RegExp::comp(const char *exp) |
| 119 |
{ |
| 120 |
if (wascompiled) { |
| 121 |
regfree(®); |
| 122 |
wascompiled = false; |
| 123 |
} |
| 124 |
DEBUG_regexp("Compiling ", exp); |
| 125 |
#ifdef HAVE_PCRE |
| 126 |
DEBUG_regexp("...with PCRE "); |
| 127 |
if (regcomp(®, exp, REG_ICASE | REG_EXTENDED | REG_DOTALL) != 0) { // compile regex |
| 128 |
#else |
| 129 |
DEBUG_regexp("...without PCRE "); |
| 130 |
if (regcomp(®, exp, REG_ICASE | REG_EXTENDED) != 0) { |
| 131 |
#endif |
| 132 |
regfree(®); |
| 133 |
|
| 134 |
return false; // need exception? |
| 135 |
} |
| 136 |
wascompiled = true; |
| 137 |
searchstring = exp; |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
// match the given text against the pre-compiled expression |
| 142 |
bool RegExp::match(const char *text, RegResult &rs) |
| 143 |
{ |
| 144 |
rs.results.clear(); |
| 145 |
rs.offsets.clear(); |
| 146 |
rs.lengths.clear(); |
| 147 |
rs.imatched = false; |
| 148 |
|
| 149 |
if (!wascompiled) { |
| 150 |
return false; // need exception? |
| 151 |
} |
| 152 |
|
| 153 |
if( text == NULL || *text == 0) // false if text is empty |
| 154 |
return false; |
| 155 |
|
| 156 |
char *pos = (char *)text; |
| 157 |
int i; |
| 158 |
unsigned int num_sub_expressions = MAX_SUB_EXPRESSIONS; |
| 159 |
if (reg.re_nsub < num_sub_expressions) |
| 160 |
num_sub_expressions = reg.re_nsub; |
| 161 |
regmatch_t *pmatch = new regmatch_t[num_sub_expressions + 1]; // to hold result |
| 162 |
if (!pmatch) { // if it failed |
| 163 |
delete[] pmatch; |
| 164 |
rs.imatched = false; |
| 165 |
return false; |
| 166 |
// exception? |
| 167 |
} |
| 168 |
if (regexec(®, pos, num_sub_expressions + 1, pmatch, 0)) { // run regexdelete[]pmatch; |
| 169 |
delete[] pmatch; |
| 170 |
rs.imatched = false; |
| 171 |
DEBUG_regexp("no match for:", searchstring); |
| 172 |
return false; // if no match |
| 173 |
} |
| 174 |
size_t matchlen; |
| 175 |
char *submatch; |
| 176 |
unsigned int largestoffset; |
| 177 |
int error = 0; |
| 178 |
while (error == 0) { |
| 179 |
largestoffset = 0; |
| 180 |
for (i = 0; i <= (signed)num_sub_expressions; i++) { |
| 181 |
if (pmatch[i].rm_so != -1) { |
| 182 |
matchlen = pmatch[i].rm_eo - pmatch[i].rm_so; |
| 183 |
submatch = new char[matchlen + 1]; |
| 184 |
strncpy(submatch, pos + pmatch[i].rm_so, matchlen); |
| 185 |
submatch[matchlen] = '\0'; |
| 186 |
rs.results.push_back(std::string(submatch)); |
| 187 |
rs.offsets.push_back(pmatch[i].rm_so + (pos - text)); |
| 188 |
rs.lengths.push_back(matchlen); |
| 189 |
delete[] submatch; |
| 190 |
if ((pmatch[i].rm_so + matchlen) > largestoffset) { |
| 191 |
largestoffset = pmatch[i].rm_so + matchlen; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
if (largestoffset > 0) { |
| 196 |
pos += largestoffset; |
| 197 |
error = regexec(®, pos, num_sub_expressions + 1, pmatch, REG_NOTBOL); |
| 198 |
} else { |
| 199 |
error = -1; |
| 200 |
} |
| 201 |
} |
| 202 |
rs.imatched = true; |
| 203 |
delete[] pmatch; |
| 204 |
DEBUG_regexp("match(s) for:", searchstring); |
| 205 |
return true; // match(s) found |
| 206 |
} |
| 207 |
|
| 208 |
// My own version of STL::search() which seems to be 5-6 times faster |
| 209 |
char *RegExp::search(char *file, char *fileend, char *phrase, char *phraseend) |
| 210 |
{ |
| 211 |
|
| 212 |
int j, l; // counters |
| 213 |
int p; // to hold precalcuated value for speed |
| 214 |
bool match; // flag |
| 215 |
int qsBc[256]; // Quick Search Boyer Moore shift table (256 alphabet) |
| 216 |
char *k; // pointer used in matching |
| 217 |
|
| 218 |
int pl = phraseend - phrase; // phrase length |
| 219 |
int fl = (int)(fileend - file) - pl; // file length that could match |
| 220 |
|
| 221 |
if (fl < pl) |
| 222 |
return fileend; // reality checking |
| 223 |
if (pl > 126) |
| 224 |
return fileend; // reality checking |
| 225 |
|
| 226 |
// For speed we append the phrase to the end of the memory block so it |
| 227 |
// is always found, thus eliminating some checking. This is possible as |
| 228 |
// we know an extra 127 bytes have been provided by NaughtyFilter.cpp |
| 229 |
// and also the OptionContainer does not allow phrase lengths greater |
| 230 |
// than 126 chars |
| 231 |
|
| 232 |
for (j = 0; j < pl; j++) { |
| 233 |
fileend[j] = phrase[j]; |
| 234 |
} |
| 235 |
|
| 236 |
// Next we need to make the Quick Search Boyer Moore shift table |
| 237 |
|
| 238 |
p = pl + 1; |
| 239 |
for (j = 0; j < 256; j++) { // Preprocessing |
| 240 |
qsBc[j] = p; |
| 241 |
} |
| 242 |
for (j = 0; j < pl; j++) { // Preprocessing |
| 243 |
qsBc[(unsigned char)phrase[j]] = pl - j; |
| 244 |
} |
| 245 |
|
| 246 |
// Now do the searching! |
| 247 |
|
| 248 |
for (j = 0;;) { |
| 249 |
k = file + j; |
| 250 |
match = true; |
| 251 |
for (l = 0; l < pl; l++) { // quiv, but faster, memcmp() |
| 252 |
if (k[l] != phrase[l]) { |
| 253 |
match = false; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
if (match) { |
| 258 |
return (j + file); // match found at offset j (but could be the |
| 259 |
// copy put at fileend) |
| 260 |
} |
| 261 |
j += qsBc[(unsigned char)file[j + pl]]; // shift |
| 262 |
} |
| 263 |
return fileend; // should never get here as it should always match |
| 264 |
} |