-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbinpac_regex.h
72 lines (52 loc) · 1.81 KB
/
binpac_regex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef binpac_regex_h
#define binpac_regex_h
#include "zeek/RE.h"
#include "binpac.h"
namespace zeek {
class RE_Matcher;
}
namespace binpac {
// Must be called before any binpac functionality is used.
//
// Note, this must be declared/defined here, and inline, because the RE
// functionality can only be used when compiling from inside Zeek.
// A copy is made of any FlowBuffer policy struct data passed.
inline void init(FlowBuffer::Policy* fbp = 0);
// Internal vector recording not yet compiled matchers.
extern std::vector<zeek::RE_Matcher*>* uncompiled_re_matchers;
class RegExMatcher {
public:
RegExMatcher(const char* pattern) : pattern_(pattern) {
if ( ! uncompiled_re_matchers )
uncompiled_re_matchers = new std::vector<zeek::RE_Matcher*>;
re_matcher_ = new zeek::RE_Matcher(pattern_.c_str());
uncompiled_re_matchers->push_back(re_matcher_);
}
~RegExMatcher() { delete re_matcher_; }
// Returns the length of longest match, or -1 on mismatch.
int MatchPrefix(const_byteptr data, int len) { return re_matcher_->MatchPrefix(data, len); }
private:
friend void ::binpac::init(FlowBuffer::Policy*);
// Function, and state, for compiling matchers.
static void init();
string pattern_;
zeek::RE_Matcher* re_matcher_;
};
inline void RegExMatcher::init() {
if ( ! uncompiled_re_matchers )
return;
for ( size_t i = 0; i < uncompiled_re_matchers->size(); ++i ) {
if ( ! (*uncompiled_re_matchers)[i]->Compile() ) {
fprintf(stderr, "binpac: cannot compile regular expression\n");
exit(1);
}
}
uncompiled_re_matchers->clear();
}
inline void init(FlowBuffer::Policy* fbp) {
RegExMatcher::init();
if ( fbp )
FlowBuffer::init(*fbp);
}
} // namespace binpac
#endif // binpac_regex_h