/
switch_regex.c
242 lines (208 loc) · 6.58 KB
/
switch_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2010, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Michael Jerris <mike@jerris.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Michael Jerris <mike@jerris.com>
*
*
* switch_regex.c -- PCRE wrapper
*
*/
#include <switch.h>
#include <pcre.h>
SWITCH_DECLARE(switch_regex_t *) switch_regex_compile(const char *pattern,
int options, const char **errorptr, int *erroroffset, const unsigned char *tables)
{
return pcre_compile(pattern, options, errorptr, erroroffset, tables);
}
SWITCH_DECLARE(int) switch_regex_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int size)
{
return pcre_copy_substring(subject, ovector, stringcount, stringnumber, buffer, size);
}
SWITCH_DECLARE(void) switch_regex_free(void *data)
{
pcre_free(data);
}
SWITCH_DECLARE(int) switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
{
const char *error = NULL;
int erroffset = 0;
pcre *re = NULL;
int match_count = 0;
char *tmp = NULL;
uint32_t flags = 0;
char abuf[256] = "";
if (!(field && expression)) {
return 0;
}
if (*expression == '_') {
if (switch_ast2regex(expression + 1, abuf, sizeof(abuf))) {
expression = abuf;
}
}
if (*expression == '/') {
char *opts = NULL;
tmp = strdup(expression + 1);
assert(tmp);
if ((opts = strrchr(tmp, '/'))) {
*opts++ = '\0';
} else {
goto end;
}
expression = tmp;
if (opts) {
if (strchr(opts, 'i')) {
flags |= PCRE_CASELESS;
}
if (strchr(opts, 's')) {
flags |= PCRE_DOTALL;
}
}
}
re = pcre_compile(expression, /* the pattern */
flags, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
if (error) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "COMPILE ERROR: %d [%s][%s]\n", erroffset, error, expression);
switch_regex_safe_free(re);
goto end;
}
match_count = pcre_exec(re, /* result of pcre_compile() */
NULL, /* we didn't study the pattern */
field, /* the subject string */
(int) strlen(field), /* the length of the subject string */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* vector of integers for substring information */
olen); /* number of elements (NOT size in bytes) */
if (match_count <= 0) {
switch_regex_safe_free(re);
match_count = 0;
}
*new_re = (switch_regex_t *) re;
end:
switch_safe_free(tmp);
return match_count;
}
SWITCH_DECLARE(void) switch_perform_substitution(switch_regex_t *re, int match_count, const char *data, const char *field_data,
char *substituted, switch_size_t len, int *ovector)
{
char index[10] = "";
char replace[1024] = "";
switch_size_t x, y = 0, z = 0;
int num = 0;
for (x = 0; x < (len - 1) && x < strlen(data);) {
if (data[x] == '$') {
x++;
if (!(data[x] > 47 && data[x] < 58)) {
substituted[y++] = data[x - 1];
continue;
}
while (data[x] > 47 && data[x] < 58) {
index[z++] = data[x];
x++;
}
index[z++] = '\0';
z = 0;
num = atoi(index);
if (num < 0 || num > 256) {
num = -1;
}
if (pcre_copy_substring(field_data, ovector, match_count, num, replace, sizeof(replace)) > 0) {
switch_size_t r;
for (r = 0; r < strlen(replace); r++) {
substituted[y++] = replace[r];
}
}
} else {
substituted[y++] = data[x];
x++;
}
}
substituted[y++] = '\0';
}
SWITCH_DECLARE(switch_status_t) switch_regex_match_partial(const char *target, const char *expression, int *partial)
{
const char *error = NULL; /* Used to hold any errors */
int error_offset = 0; /* Holds the offset of an error */
pcre *pcre_prepared = NULL; /* Holds the compiled regex */
int match_count = 0; /* Number of times the regex was matched */
int offset_vectors[255]; /* not used, but has to exist or pcre won't even try to find a match */
int pcre_flags = 0;
/* Compile the expression */
pcre_prepared = pcre_compile(expression, 0, &error, &error_offset, NULL);
/* See if there was an error in the expression */
if (error != NULL) {
/* Clean up after ourselves */
if (pcre_prepared) {
pcre_free(pcre_prepared);
pcre_prepared = NULL;
}
/* Note our error */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
"Regular Expression Error expression[%s] error[%s] location[%d]\n", expression, error, error_offset);
/* We definitely didn't match anything */
return SWITCH_STATUS_FALSE;
}
if (*partial) {
pcre_flags = PCRE_PARTIAL;
}
/* So far so good, run the regex */
match_count =
pcre_exec(pcre_prepared, NULL, target, (int) strlen(target), 0, pcre_flags, offset_vectors, sizeof(offset_vectors) / sizeof(offset_vectors[0]));
/* Clean up */
if (pcre_prepared) {
pcre_free(pcre_prepared);
pcre_prepared = NULL;
}
/* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "number of matches: %d\n", match_count); */
/* Was it a match made in heaven? */
if (match_count > 0) {
*partial = 0;
return SWITCH_STATUS_SUCCESS;
} else if (match_count == PCRE_ERROR_PARTIAL) {
/* yes it is already set, but the code is clearer this way */
*partial = 1;
return SWITCH_STATUS_SUCCESS;
} else {
return SWITCH_STATUS_FALSE;
}
}
SWITCH_DECLARE(switch_status_t) switch_regex_match(const char *target, const char *expression)
{
int partial = 0;
return switch_regex_match_partial(target, expression, &partial);
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/