Skip to content

Commit cd1a882

Browse files
committed
regex: Organizes the classes per file
moves Pcre to backends/pcre.cc moves PegexMatch to regex_match.h
1 parent f96c04d commit cd1a882

File tree

6 files changed

+214
-148
lines changed

6 files changed

+214
-148
lines changed

Diff for: build/pcre.m4

+36-15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ AC_ARG_WITH(
2323
2424
AC_MSG_CHECKING([for libpcre config script])
2525
26+
if test "x${with_pcre}" == "xno"; then
27+
AC_MSG_NOTICE([Support for PCRE was disabled by the utilization of --without-pcre or --with-pcre=no])
28+
PCRE_DISABLED=yes
29+
else
30+
if test "x${with_pcre}" == "xyes"; then
31+
PCRE_MANDATORY=yes
32+
AC_MSG_NOTICE([PCRE support was marked as mandatory by the utilization of --with-pcre=yes])
33+
test_paths="/usr/local/libpcre /usr/local/pcre /usr/local /opt/libpcre /opt/pcre /opt /usr /opt/local"
34+
fi
35+
2636
for x in ${test_paths}; do
2737
dnl # Determine if the script was specified and use it directly
2838
if test ! -d "$x" -a -e "$x"; then
@@ -55,7 +65,7 @@ if test -n "${pcre_path}"; then
5565
AC_MSG_RESULT([${PCRE_CONFIG}])
5666
PCRE_VERSION="`${PCRE_CONFIG} --version`"
5767
if test ! -z "${PCRE_VERSION}"; then AC_MSG_NOTICE(pcre VERSION: $PCRE_VERSION); fi
58-
PCRE_CFLAGS="`${PCRE_CONFIG} --cflags`"
68+
PCRE_CFLAGS="`${PCRE_CONFIG} --cflags` -DWITH_PCRE"
5969
if test ! -z "${PCRE_CFLAGS}"; then AC_MSG_NOTICE(pcre CFLAGS: $PCRE_CFLAGS); fi
6070
PCRE_LDADD="`${PCRE_CONFIG} --libs`"
6171
if test ! -z "${PCRE_LDADD}"; then AC_MSG_NOTICE(pcre LDADD: $PCRE_LDADD); fi
@@ -90,20 +100,31 @@ if test -n "${PCRE_VERSION}"; then
90100
LDFLAGS=$save_$LDFLAGS
91101
fi
92102
93-
AC_SUBST(PCRE_CONFIG)
94-
AC_SUBST(PCRE_VERSION)
95-
AC_SUBST(PCRE_CPPFLAGS)
96-
AC_SUBST(PCRE_CFLAGS)
97-
AC_SUBST(PCRE_LDFLAGS)
98-
AC_SUBST(PCRE_LDADD)
99-
AC_SUBST(PCRE_LD_PATH)
103+
fi
104+
100105
101-
if test -z "${PCRE_VERSION}"; then
102-
AC_MSG_NOTICE([*** pcre library not found.])
103-
ifelse([$2], , AC_MSG_ERROR([pcre library is required]), $2)
106+
if test -z "${PCRE_LDADD}"; then
107+
if test -z "${PCRE_MANDATORY}"; then
108+
if test -z "${PCRE_DISABLED}"; then
109+
PCRE_FOUND=0
110+
else
111+
PCRE_FOUND=2
112+
fi
113+
else
114+
AC_MSG_ERROR([PCRE was explicitly referenced but it was not found])
115+
PCRE_FOUND=-1
116+
fi
104117
else
105-
AC_MSG_NOTICE([using pcre v${PCRE_VERSION}])
106-
ifelse([$1], , , $1)
107-
PCRE_LDADD="${PCRE_LDADD} -lpcre"
108-
fi
118+
PCRE_FOUND=1
119+
AC_SUBST(PCRE_CONFIG)
120+
AC_SUBST(PCRE_VERSION)
121+
AC_SUBST(PCRE_CPPFLAGS)
122+
AC_SUBST(PCRE_CFLAGS)
123+
AC_SUBST(PCRE_LDFLAGS)
124+
AC_SUBST(PCRE_LDADD)
125+
AC_SUBST(PCRE_LD_PATH)
126+
PCRE_DISPLAY="${PCRE_LDADD}, ${PCRE_CFLAGS}"
127+
AC_SUBST(PCRE_DISPLAY)
128+
fi
129+
109130
])

Diff for: src/regex/backend/pcre.cc

+96-5
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,114 @@
1313
*
1414
*/
1515

16-
#include <pcre.h>
1716

1817
#include <iostream>
1918
#include <fstream>
2019
#include <string>
2120
#include <list>
21+
#include <pcre.h>
22+
23+
#include "src/regex/backend/pcre.h"
24+
#include "src/regex/regex_match.h"
2225

23-
#ifndef SRC_REGEX_BACKEND_PCRE_H_
24-
#define SRC_REGEX_BACKEND_PCRE_H_
2526

2627
namespace modsecurity {
2728
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+
2866

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;
2973

74+
do {
75+
rc = pcre_exec(m_pc, m_pce, subject,
76+
s.size(), offset, 0, ovector, OVECCOUNT);
3077

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));
89+
90+
if (len == 0) {
91+
rc = 0;
92+
break;
93+
}
94+
}
95+
} while (rc > 0);
96+
97+
return retList;
98+
}
99+
100+
101+
int Pcre::search(const std::string& s, RegexMatch *match) const {
102+
int ovector[OVECCOUNT];
103+
int ret = pcre_exec(m_pc, m_pce, s.c_str(),
104+
s.size(), 0, 0, ovector, OVECCOUNT) > 0;
105+
106+
if (ret > 0) {
107+
*match = RegexMatch(
108+
std::string(s, ovector[ret-1], ovector[ret] - ovector[ret-1]),
109+
0);
110+
}
111+
112+
return ret;
113+
}
114+
115+
116+
int Pcre::search(const std::string& s) const {
117+
int ovector[OVECCOUNT];
118+
return pcre_exec(m_pc, m_pce, s.c_str(),
119+
s.size(), 0, 0, ovector, OVECCOUNT) > 0;
120+
}
121+
122+
123+
} // namespace backend
31124
} // namespace regex
32125
} // namespace modsecurity
33126

34-
35-
#endif // SRC_REGEX_BACKEND_PCRE_H_

Diff for: src/regex/backend/pcre.h

+26
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,40 @@
2020
#include <string>
2121
#include <list>
2222

23+
#include "src/regex/regex_match.h"
24+
2325
#ifndef SRC_REGEX_BACKEND_PCRE_H_
2426
#define SRC_REGEX_BACKEND_PCRE_H_
2527

2628
namespace modsecurity {
2729
namespace regex {
30+
namespace backend {
31+
32+
33+
#define OVECCOUNT 30
34+
35+
36+
class Pcre {
37+
public:
38+
explicit Pcre(const std::string& pattern_);
39+
~Pcre();
40+
41+
// m_pc and m_pce can't be easily copied
42+
Pcre(const Pcre&) = delete;
43+
Pcre& operator=(const Pcre&) = delete;
44+
45+
std::list<RegexMatch> searchAll(const std::string& s) const;
46+
int search(const std::string &s, RegexMatch *m) const;
47+
int search(const std::string &s) const;
2848

49+
const std::string pattern;
50+
private:
51+
pcre *m_pc = NULL;
52+
pcre_extra *m_pce = NULL;
53+
};
2954

3055

56+
} // namespace backend
3157
} // namespace regex
3258
} // namespace modsecurity
3359

Diff for: src/regex/regex.cc

-88
Original file line numberDiff line numberDiff line change
@@ -26,98 +26,10 @@
2626
#include <fstream>
2727
#include <iostream>
2828

29-
#include "src/utils/geo_lookup.h"
30-
31-
#if PCRE_HAVE_JIT
32-
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
33-
#else
34-
#define pcre_study_opt 0
35-
#endif
3629

3730
namespace modsecurity {
3831
namespace regex {
3932

4033

41-
Regex::Regex(const std::string& pattern_)
42-
: pattern(pattern_.empty() ? ".*" : pattern_) {
43-
const char *errptr = NULL;
44-
int erroffset;
45-
46-
m_pc = pcre_compile(pattern.c_str(), PCRE_DOTALL|PCRE_MULTILINE,
47-
&errptr, &erroffset, NULL);
48-
49-
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
50-
}
51-
52-
53-
Regex::~Regex() {
54-
if (m_pc != NULL) {
55-
pcre_free(m_pc);
56-
m_pc = NULL;
57-
}
58-
if (m_pce != NULL) {
59-
#if PCRE_HAVE_JIT
60-
pcre_free_study(m_pce);
61-
#else
62-
pcre_free(m_pce);
63-
#endif
64-
m_pce = NULL;
65-
}
66-
}
67-
68-
69-
std::list<RegexMatch> Regex::searchAll(const std::string& s) const {
70-
const char *subject = s.c_str();
71-
const std::string tmpString = std::string(s.c_str(), s.size());
72-
int ovector[OVECCOUNT];
73-
int rc, i, offset = 0;
74-
std::list<RegexMatch> retList;
75-
76-
do {
77-
rc = pcre_exec(m_pc, m_pce, subject,
78-
s.size(), offset, 0, ovector, OVECCOUNT);
79-
80-
for (i = 0; i < rc; i++) {
81-
size_t start = ovector[2*i];
82-
size_t end = ovector[2*i+1];
83-
size_t len = end - start;
84-
if (end > s.size()) {
85-
rc = 0;
86-
break;
87-
}
88-
std::string match = std::string(tmpString, start, len);
89-
offset = start + len;
90-
retList.push_front(RegexMatch(match, start));
91-
92-
if (len == 0) {
93-
rc = 0;
94-
break;
95-
}
96-
}
97-
} while (rc > 0);
98-
99-
return retList;
100-
}
101-
102-
int Regex::search(const std::string& s, RegexMatch *match) const {
103-
int ovector[OVECCOUNT];
104-
int ret = pcre_exec(m_pc, m_pce, s.c_str(),
105-
s.size(), 0, 0, ovector, OVECCOUNT) > 0;
106-
107-
if (ret > 0) {
108-
*match = RegexMatch(
109-
std::string(s, ovector[ret-1], ovector[ret] - ovector[ret-1]),
110-
0);
111-
}
112-
113-
return ret;
114-
}
115-
116-
int Regex::search(const std::string& s) const {
117-
int ovector[OVECCOUNT];
118-
return pcre_exec(m_pc, m_pce, s.c_str(),
119-
s.size(), 0, 0, ovector, OVECCOUNT) > 0;
120-
}
121-
12234
} // namespace regex
12335
} // namespace modsecurity

0 commit comments

Comments
 (0)