forked from c-icap/c-icap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.c
182 lines (164 loc) · 5.19 KB
/
regex.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "common.h"
#include "debug.h"
#include "array.h"
#include "ci_regex.h"
#ifdef HAVE_PCRE
#include <pcre.h>
#else
#include <regex.h>
#endif
char *ci_regex_parse(const char *str, int *flags, int *recursive)
{
int slen;
const char *e;
char *s;
if (*str != '/')
return NULL;
++str;
slen = strlen(str);
e = str + slen;
while (*e != '/' && e != str) --e;
if (*e != '/')
return NULL;
slen = e - str;
s = malloc( (slen + 1) * sizeof(char));
strncpy(s, str, slen);
s[slen] = '\0';
*flags = 0;
#ifdef HAVE_PCRE
*flags |= PCRE_NEWLINE_ANY;
*flags |= PCRE_NEWLINE_ANYCRLF;
#else
*flags |= REG_EXTENDED;
// *flags |= REG_NOSUB;
#endif
while (*e != '\0') {
#ifdef HAVE_PCRE
if (*e == 'i')
*flags = *flags | PCRE_CASELESS;
else if (*e == 'm')
*flags |= PCRE_MULTILINE;
else if (*e == 's')
*flags |= PCRE_DOTALL;
else if (*e == 'x')
*flags |= PCRE_EXTENDED;
else if (*e == 'A')
*flags |= PCRE_ANCHORED;
else if (*e == 'D')
*flags |= PCRE_DOLLAR_ENDONLY;
else if (*e == 'U')
*flags |= PCRE_UNGREEDY;
else if (*e == 'X')
*flags |= PCRE_EXTRA;
else if (*e == 'D')
*flags |= PCRE_DOLLAR_ENDONLY;
else if (*e == 'u')
*flags |= PCRE_UTF8;
#else
if (*e == 'i')
*flags = *flags | REG_ICASE;
else if (*e == 'm')
*flags |= REG_NEWLINE;
#endif
else if (*e == 'g')
*recursive = 1;
++e;
}
return s;
}
ci_regex_t ci_regex_build(const char *regex_str, int regex_flags)
{
#ifdef HAVE_PCRE
pcre *re;
const char *error;
int erroffset;
re = pcre_compile(regex_str, regex_flags, &error, &erroffset, NULL);
if (re == NULL) {
ci_debug_printf(2, "PCRE compilation failed at offset %d: %s\n", erroffset, error);
return NULL;
}
return re;
#else
int retcode;
regex_t *regex = malloc(sizeof(regex_t));
/*reset regex_struct*/
memset(regex, 0, sizeof(regex_t));
retcode = regcomp(regex, regex_str, regex_flags);
if (retcode) {
free(regex);
regex = NULL;
}
return regex;
#endif
}
void ci_regex_free(ci_regex_t regex)
{
#ifdef HAVE_PCRE
pcre_free((pcre *)regex);
#else
regfree((regex_t *)regex);
free(regex);
#endif
}
#ifdef HAVE_PCRE
#define OVECCOUNT 30 /* should be a multiple of 3 */
#endif
int ci_regex_apply(const ci_regex_t regex, const char *str, int len, int recurs, ci_list_t *matches, const void *user_data)
{
int count = 0, i;
ci_regex_replace_part_t parts;
if (!str)
return 0;
#ifdef HAVE_PCRE
int ovector[OVECCOUNT];
int rc;
int offset = 0;
int str_length = len >=0 ? len : strlen(str);
do {
memset(ovector, 0, sizeof(ovector));
rc = pcre_exec(regex, NULL, str, str_length, offset, 0, ovector, OVECCOUNT);
if (rc >= 0 && ovector[0] != ovector[1]) {
++count;
ci_debug_printf(9, "Match pattern (pos:%d-%d): '%.*s'\n",
ovector[0], ovector[1], ovector[1]-ovector[0], str+ovector[0]);
offset = ovector[1];
if (matches) {
parts.user_data = user_data;
memset(parts.matches, 0, sizeof(ci_regex_matches_t));
for (i = 0; i < 10 && ovector[2*i+1] > ovector[2*i]; ++i) {
ci_debug_printf(9, "\t sub-match pattern (pos:%d-%d): '%.*s'\n", ovector[2*i], ovector[2*i+1], ovector[2*i + 1] - ovector[2*i], str+ovector[2*i]);
parts.matches[i].s = ovector[2*i];
parts.matches[i].e = ovector[2*i+1];
}
ci_list_push_back(matches, (void *)&parts);
}
}
} while (recurs && rc >=0 && offset < str_length);
#else
int retcode;
regmatch_t pmatch[10];
do {
if ((retcode = regexec(regex, str, 10, pmatch, 0)) == 0) {
++count;
ci_debug_printf(9, "Match pattern (pos:%d-%d): '%.*s'\n", pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[0].rm_eo - pmatch[0].rm_so, str+pmatch[0].rm_so);
if (matches) {
parts.user_data = user_data;
memset(parts.matches, 0, sizeof(ci_regex_matches_t));
for (i = 0; i < 10 && pmatch[i].rm_eo > pmatch[i].rm_so; ++i) {
ci_debug_printf(9, "\t sub-match pattern (pos:%d-%d): '%.*s'\n", pmatch[i].rm_so, pmatch[i].rm_eo, pmatch[i].rm_eo - pmatch[i].rm_so, str+pmatch[i].rm_so);
parts.matches[i].s = pmatch[i].rm_so;
parts.matches[i].e = pmatch[i].rm_eo;
}
ci_list_push_back(matches, (void *)&parts);
}
if (pmatch[0].rm_so >= 0 && pmatch[0].rm_eo >= 0 && pmatch[0].rm_so != pmatch[0].rm_eo) {
str += pmatch[0].rm_eo;
ci_debug_printf(8, "I will check again starting from: %s\n", str);
} else /*stop here*/
str = NULL;
}
} while (recurs && str && *str != '\0' && retcode == 0);
#endif
ci_debug_printf(5, "ci_regex_apply matches count: %d\n", count);
return count;
}