mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-16 07:56:12 +03:00
Using pcre (with JIT) instead of pcrecpp
This commit is contained in:
parent
ed86c24df6
commit
2451bf05d7
@ -29,8 +29,7 @@ namespace operators {
|
|||||||
bool Rx::evaluate(Assay *assay, const std::string& input) {
|
bool Rx::evaluate(Assay *assay, const std::string& input) {
|
||||||
SMatch match;
|
SMatch match;
|
||||||
|
|
||||||
Regex re(MacroExpansion::expand(param, assay));
|
if (regex_search(input, &match, *m_re) && match.size() >= 1) {
|
||||||
if (regex_search(input, &match, re) && match.size() >= 1) {
|
|
||||||
this->matched.push_back(match.match);
|
this->matched.push_back(match.match);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,17 @@ class Rx : public Operator {
|
|||||||
/** @ingroup ModSecurity_Operator */
|
/** @ingroup ModSecurity_Operator */
|
||||||
Rx(std::string op, std::string param, bool negation)
|
Rx(std::string op, std::string param, bool negation)
|
||||||
: Operator(op, param, negation),
|
: Operator(op, param, negation),
|
||||||
m_param(param) { }
|
m_param(param) {
|
||||||
|
Regex r(param);
|
||||||
|
m_re = &r;
|
||||||
|
}
|
||||||
|
|
||||||
bool evaluate(Assay *assay, const std::string &input);
|
bool evaluate(Assay *assay, const std::string &input);
|
||||||
|
|
||||||
std::list<std::string> matched;
|
std::list<std::string> matched;
|
||||||
private:
|
private:
|
||||||
std::string m_param;
|
std::string m_param;
|
||||||
|
Regex *m_re;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
#include "utils/regex.h"
|
#include "utils/regex.h"
|
||||||
|
|
||||||
#include <pcrecpp.h>
|
#include <pcre.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
@ -33,28 +33,28 @@ namespace Utils {
|
|||||||
|
|
||||||
Regex::Regex(const std::string& pattern_)
|
Regex::Regex(const std::string& pattern_)
|
||||||
: pattern(pattern_) {
|
: pattern(pattern_) {
|
||||||
|
const char *errptr = NULL;
|
||||||
|
int erroffset;
|
||||||
|
|
||||||
if (pattern.empty() == true) {
|
if (pattern.empty() == true) {
|
||||||
pattern.assign(".*");
|
pattern.assign(".*");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_pc = pcre_compile(pattern.c_str(), PCRE_DOTALL|PCRE_MULTILINE, &errptr, &erroffset, NULL);
|
||||||
|
m_pce = pcre_study(m_pc, PCRE_STUDY_JIT_COMPILE, &errptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int regex_search(const std::string& s, SMatch *match,
|
int regex_search(const std::string& s, SMatch *match,
|
||||||
const Regex& regex) {
|
const Regex& regex) {
|
||||||
std::string m;
|
int *ovector = 0;
|
||||||
pcrecpp::RE re(regex.pattern,
|
int ovecsize = 0;
|
||||||
pcrecpp::RE_Options(PCRE_DOTALL|PCRE_MULTILINE));
|
return pcre_exec(regex.m_pc, regex.m_pce, s.c_str(), s.size(), 0, 0, ovector, ovecsize) > 0;
|
||||||
|
|
||||||
/** FIXME: Should be not necessary to call PartialMatch twice here. */
|
|
||||||
match->size_ = re.PartialMatch(s);
|
|
||||||
re.PartialMatch(s, &m);
|
|
||||||
match->match = m;
|
|
||||||
|
|
||||||
return match->size_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int regex_search(const std::string& s, Regex regex) {
|
int regex_search(const std::string& s, const Regex& regex) {
|
||||||
pcrecpp::RE re(regex.pattern);
|
int *ovector = 0;
|
||||||
return re.PartialMatch(s);
|
int ovecsize = 0;
|
||||||
|
return pcre_exec(regex.m_pc, regex.m_pce, s.c_str(), s.size(), 0, 0, ovector, ovecsize) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <pcre.h>
|
||||||
|
|
||||||
#ifndef SRC_UTILS_REGEX_H_
|
#ifndef SRC_UTILS_REGEX_H_
|
||||||
#define SRC_UTILS_REGEX_H_
|
#define SRC_UTILS_REGEX_H_
|
||||||
@ -29,6 +30,9 @@ class Regex {
|
|||||||
public:
|
public:
|
||||||
explicit Regex(const std::string& pattern_);
|
explicit Regex(const std::string& pattern_);
|
||||||
std::string pattern;
|
std::string pattern;
|
||||||
|
pcre *m_pc = NULL;
|
||||||
|
pcre_extra *m_pce = NULL;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -44,7 +48,7 @@ class SMatch {
|
|||||||
int regex_search(const std::string& s, SMatch *m,
|
int regex_search(const std::string& s, SMatch *m,
|
||||||
const Regex& regex);
|
const Regex& regex);
|
||||||
|
|
||||||
int regex_search(const std::string& s, Regex r);
|
int regex_search(const std::string& s, const Regex& r);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user