Skip to content

Commit 18f0e6d

Browse files
zimmerleWGH-
authored andcommitted
regex: Organizes the classes per file
moves Pcre to backends/pcre.cc moves PegexMatch to regex_match.h
1 parent d03a293 commit 18f0e6d

File tree

6 files changed

+250
-179
lines changed

6 files changed

+250
-179
lines changed

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
])

src/regex/backend/pcre.cc

+117-5
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,135 @@
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+
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);
2877

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));
2989

90+
if (len == 0) {
91+
rc = 0;
92+
break;
93+
}
94+
}
95+
} while (rc > 0);
3096

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
31145
} // namespace regex
32146
} // namespace modsecurity
33147

34-
35-
#endif // SRC_REGEX_BACKEND_PCRE_H_

src/regex/backend/pcre.h

+28
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,43 @@
1919
#include <fstream>
2020
#include <string>
2121
#include <list>
22+
#include <vector>
23+
24+
#include "src/regex/regex_match.h"
2225

2326
#ifndef SRC_REGEX_BACKEND_PCRE_H_
2427
#define SRC_REGEX_BACKEND_PCRE_H_
2528

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

51+
const std::string pattern;
52+
private:
53+
pcre *m_pc = NULL;
54+
pcre_extra *m_pce = NULL;
55+
};
2956

3057

58+
} // namespace backend
3159
} // namespace regex
3260
} // namespace modsecurity
3361

src/regex/regex.cc

-108
Original file line numberDiff line numberDiff line change
@@ -22,118 +22,10 @@
2222
#include <fstream>
2323
#include <iostream>
2424

25-
#include "src/utils/geo_lookup.h"
26-
27-
#if PCRE_HAVE_JIT
28-
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
29-
#else
30-
#define pcre_study_opt 0
31-
#endif
3225

3326
namespace modsecurity {
3427
namespace regex {
3528

3629

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

0 commit comments

Comments
 (0)