This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathregex_functions.h
executable file
·145 lines (135 loc) · 4.41 KB
/
regex_functions.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef REGEX_H
#define REGEX_H
#include <regex.h>
#include <stdio.h>
#include "Config.h"
#include "Debug.h"
#include "Patterns.h"
/* Text versions of regexps */
typedef struct {
std::string ip;
std::string agent;
std::string url;
std::string traffic;
std::string referer;
std::string code;
std::string google;
std::string bing;
std::string baidu;
std::string yandex;
} LogRegexTexts;
/* Compiled regexps */
typedef struct {
regex_t ip;
regex_t agent;
regex_t url;
regex_t traffic;
regex_t referer;
regex_t code;
regex_t google;
regex_t bing;
regex_t baidu;
regex_t yandex;
} LogRegexCompiled;
/*
* Text regexps representations
* for different log line parts.
*/
static void logRegexTextsFill(LogRegexTexts *lrt, Config * config) {
(*lrt).ip = (config->ipPattern.length() > 4) ? config->ipPattern : IP_PATTERN;
(*lrt).agent = (config->agentPattern.length() > 4) ? config->agentPattern : AGENT_PATTERN;
(*lrt).url = (config->urlPattern.length() > 4) ? config->urlPattern : URL_PATTERN;
(*lrt).traffic = (config->trafficPattern.length() > 4) ? config->trafficPattern : TRAFFIC_PATTERN;
(*lrt).referer = (config->refPattern.length() > 4) ? config->refPattern : REF_PATTERN;
(*lrt).code = (config->codePattern.length() > 4) ? config->codePattern : CODE_PATTERN;
(*lrt).google = GOOGLE_PATTERN;
(*lrt).bing = BING_PATTERN;
(*lrt).baidu = BAIDU_PATTERN;
(*lrt).yandex = YANDEX_PATTERN;
}
/*
* Compiled representations
* of text regexps.
*/
static void logRegexForCompileFill(LogRegexCompiled *lrc) {
regex_t ipRegex;
regex_t agentRegex;
regex_t urlRegex;
regex_t trafficRegex;
regex_t refRegex;
regex_t codeRegex;
regex_t googleRegex;
regex_t bingRegex;
regex_t baiduRegex;
regex_t yandexRegex;
(*lrc).ip = ipRegex;
(*lrc).agent = agentRegex;
(*lrc).url = urlRegex;
(*lrc).traffic = trafficRegex;
(*lrc).referer = refRegex;
(*lrc).code = codeRegex;
(*lrc).google = googleRegex;
(*lrc).bing = bingRegex;
(*lrc).baidu = baiduRegex;
(*lrc).yandex = yandexRegex;
}
/*
* Compiles text regexp to regex_t.
*/
static int compileRegex(regex_t * regex, const char * regexText, Config * config) {
int error = regcomp (regex, regexText, REG_EXTENDED|REG_NEWLINE);
if (error != 0) {
if (config->debugMode) {
Debug::print("compileRegex: Regex error compiling");
}
return 1;
}
return 0;
}
/*
* Compiles all texts regexps into regex_t.
*/
static LogRegexCompiled logRegexCompileAll(const LogRegexTexts * regexps, LogRegexCompiled *lrComp, Config * config) {
compileRegex(&(*lrComp).ip, (*regexps).ip.c_str(), config);
compileRegex(&(*lrComp).agent, (*regexps).agent.c_str(), config);
compileRegex(&(*lrComp).url, (*regexps).url.c_str(), config);
compileRegex(&(*lrComp).traffic, (*regexps).traffic.c_str(), config);
compileRegex(&(*lrComp).referer, (*regexps).referer.c_str(), config);
compileRegex(&(*lrComp).code, (*regexps).code.c_str(), config);
compileRegex(&(*lrComp).google, (*regexps).google.c_str(), config);
compileRegex(&(*lrComp).bing, (*regexps).bing.c_str(), config);
compileRegex(&(*lrComp).baidu, (*regexps).baidu.c_str(), config);
compileRegex(&(*lrComp).yandex, (*regexps).yandex.c_str(), config);
}
/*
* Compares text with regexp.
*/
static std::string matchRegex(regex_t * r, const char * to_match, Config * config) {
const char * p = to_match;
regmatch_t m[2]; // matches found 0 - whole match, 1 - submatch
int nomatch = regexec(r, p, 2, m, 0);
if (nomatch) {
if (config->debugMode) {
Debug::print("matchRegex: No matches");
}
return "";
}
int start;
int finish;
if (m[1].rm_so == -1) {
if (config->debugMode) {
Debug::print("matchRegex: error, matched.rm_so == -1");
}
return "";
}
start = m[1].rm_so + (p - to_match);
finish = m[1].rm_eo + (p - to_match);
char buffer[(finish - start)+100];
sprintf(buffer, "%.*s", (finish - start), (to_match + start));
std::string result(buffer);
if (config->debugMode) {
Debug::print("matchRegex: found <<" + result + ">>");
}
return result;
}
#endif /* REGEX_H */