-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregex_util.c
176 lines (139 loc) · 5.22 KB
/
regex_util.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
/*
* Copyright (C) 2015 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "regex_util.h"
#include "util.h"
#include "build_config.h"
static Status re_custom_error_msg(Status, const char *fmt, va_list);
Status ru_compile(RegexInstance *reg_inst, const Regex *regex)
{
assert(reg_inst != NULL);
assert(regex != NULL);
const char *error_str;
int error_offset;
reg_inst->regex = pcre_compile(regex->regex_pattern,
PCRE_UTF8 | regex->modifiers,
&error_str, &error_offset, NULL);
if (reg_inst == NULL) {
return st_get_error(ERR_INVALID_REGEX, "Invalid regex - %s - "
"at position %d", error_str, error_offset);
}
reg_inst->regex_study = pcre_study(reg_inst->regex, 0, &error_str);
return STATUS_SUCCESS;
}
static Status re_custom_error_msg(Status status, const char *fmt,
va_list arg_ptr)
{
if (STATUS_IS_SUCCESS(status)) {
return status;
}
char *new_fmt = concat(fmt, status.msg);
if (new_fmt == NULL) {
return status;
}
Status new_status = st_get_custom_error(status.error_code,
new_fmt, arg_ptr);
free(new_fmt);
st_free_status(status);
return new_status;
}
/* Add extra info to regex error */
Status ru_compile_custom_error_msg(RegexInstance *reg_inst, const Regex *regex,
const char *fmt, ...)
{
Status status = ru_compile(reg_inst, regex);
va_list arg_ptr;
va_start(arg_ptr, fmt);
status = re_custom_error_msg(status, fmt, arg_ptr);
va_end(arg_ptr);
return status;
}
void ru_free_instance(const RegexInstance *reg_inst)
{
if (reg_inst == NULL) {
return;
}
#if WED_PCRE_VERSION_GE_8_20 && !defined(__MACH__)
pcre_free_study(reg_inst->regex_study);
#endif
pcre_free(reg_inst->regex);
}
Status ru_exec(RegexResult *result, const RegexInstance *reg_inst,
const char *str, size_t str_len, size_t start)
{
assert(str != NULL);
assert(start < str_len);
memset(result, 0, sizeof(RegexResult));
result->return_code = pcre_exec(reg_inst->regex, reg_inst->regex_study,
str, str_len, start, 0,
result->output_vector,
RE_OUTPUT_VECTOR_SIZE);
if (result->return_code == 0) {
return st_get_error(ERR_REGEX_EXECUTION_FAILED,
"Regex contains too many capture groups");
} else if (result->return_code < 0) {
if (result->return_code == PCRE_ERROR_NOMATCH) {
return STATUS_SUCCESS;
}
return st_get_error(ERR_REGEX_EXECUTION_FAILED,
"Regex execution failed. PCRE exit code: %d",
result->return_code);
}
result->match_length = result->output_vector[1] - result->output_vector[0];
result->match = 1;
return STATUS_SUCCESS;
}
Status ru_exec_custom_error_msg(RegexResult *result,
const RegexInstance *reg_inst,
const char *str, size_t str_len, size_t start,
const char *fmt, ...)
{
Status status = ru_exec(result, reg_inst, str, str_len, start);
va_list arg_ptr;
va_start(arg_ptr, fmt);
status = re_custom_error_msg(status, fmt, arg_ptr);
va_end(arg_ptr);
return status;
}
/* Get captured group by number */
Status ru_get_group(const RegexResult *result, const char *str,
size_t str_len, size_t group, char **group_str_ptr)
{
assert(!is_null_or_empty(str));
if (!result->match ||
result->return_code <= 0 ||
group >= (size_t)result->return_code ||
(size_t)result->output_vector[(group * 2) + 1] > str_len) {
return st_get_error(ERR_INVALID_REGEX_GROUP,
"Regex group %zu is invalid for regex result",
group);
}
size_t group_start = result->output_vector[(group * 2)];
size_t group_end = result->output_vector[(group * 2) + 1];
size_t group_size = group_end - group_start;
char *group_str = malloc(group_size + 1);
if (group_str == NULL) {
return OUT_OF_MEMORY("Unable to get capture group");
}
memcpy(group_str + group_start, str, group_size);
*(group_str + group_end) = '\0';
*group_str_ptr = group_str;
return STATUS_SUCCESS;
}