... | ... |
@@ -438,7 +438,9 @@ int cli_pcre_scanbuf(const unsigned char *buffer, uint32_t length, const struct |
438 | 438 |
do { |
439 | 439 |
/* TODO: performance metrics */ |
440 | 440 |
rc = cli_pcre_match(pd, buffer+adjbuffer, adjlength, offset, 0, ovector, OVECCOUNT); |
441 |
- cli_dbgmsg("cli_pcre_scanbuf: running regex /%s/ returns %d\n", pd->expression, rc); |
|
441 |
+ /* if debug, generate a match report */ |
|
442 |
+ if (cli_debug_flag) |
|
443 |
+ cli_pcre_report(pd, buffer+adjbuffer, adjlength, rc, ovector, OVECCOUNT); |
|
442 | 444 |
|
443 | 445 |
/* matched, rc shouldn't be >0 unless a full match occurs */ |
444 | 446 |
if (rc > 0) { |
... | ... |
@@ -149,11 +149,29 @@ int cli_pcre_compile(struct cli_pcre_data *pd, long long unsigned match_limit, l |
149 | 149 |
return CL_SUCCESS; |
150 | 150 |
} |
151 | 151 |
|
152 |
+int cli_pcre_match(struct cli_pcre_data *pd, const unsigned char *buffer, uint32_t buflen, int override_offset, int options, int *ovector, size_t ovlen) |
|
153 |
+{ |
|
154 |
+ int startoffset; |
|
155 |
+ |
|
156 |
+ if (ovlen % 3) { |
|
157 |
+ cli_dbgmsg("cli_pcre_match: ovector length is not a multiple of 3\n"); |
|
158 |
+ return CL_EARG; |
|
159 |
+ } |
|
160 |
+ |
|
161 |
+ /* set the startoffset, override if a value is specified */ |
|
162 |
+ startoffset = pd->search_offset; |
|
163 |
+ if (override_offset >= 0) |
|
164 |
+ startoffset = override_offset; |
|
165 |
+ |
|
166 |
+ /* execute the pcre and return */ |
|
167 |
+ return pcre_exec(pd->re, pd->ex, buffer, buflen, startoffset, options, ovector, ovlen); |
|
168 |
+} |
|
169 |
+ |
|
152 | 170 |
#define DISABLE_PCRE_REPORT 0 |
153 | 171 |
#define MATCH_MAXLEN 1028 /*because lolz*/ |
154 | 172 |
|
155 | 173 |
/* TODO: audit this function */ |
156 |
-static void named_substr_print(struct cli_pcre_data *pd, const unsigned char *buffer, int *ovector, size_t ovlen) |
|
174 |
+static void named_substr_print(const struct cli_pcre_data *pd, const unsigned char *buffer, int *ovector, size_t ovlen) |
|
157 | 175 |
{ |
158 | 176 |
int i, j, length, namecount, trunc; |
159 | 177 |
unsigned char *tabptr; |
... | ... |
@@ -165,10 +183,10 @@ static void named_substr_print(struct cli_pcre_data *pd, const unsigned char *bu |
165 | 165 |
/* determine if there are named substrings */ |
166 | 166 |
(void)pcre_fullinfo(pd->re, pd->ex, PCRE_INFO_NAMECOUNT, &namecount); |
167 | 167 |
if (namecount <= 0) { |
168 |
- cli_dbgmsg("named_substr: no named substrings\n"); |
|
168 |
+ cli_dbgmsg("cli_pcre_report: no named substrings\n"); |
|
169 | 169 |
} |
170 | 170 |
else { |
171 |
- cli_dbgmsg("named_substr: named substrings\n"); |
|
171 |
+ cli_dbgmsg("cli_pcre_report: named substrings\n"); |
|
172 | 172 |
|
173 | 173 |
/* extract named substring translation table */ |
174 | 174 |
(void)pcre_fullinfo(pd->re, pd->ex, PCRE_INFO_NAMETABLE, &name_table); |
... | ... |
@@ -191,7 +209,7 @@ static void named_substr_print(struct cli_pcre_data *pd, const unsigned char *bu |
191 | 191 |
for (j = 0; j < length; ++j) |
192 | 192 |
snprintf(outstr+(2*j), sizeof(outstr)-(2*j), "%02x", (unsigned int)*(start+j)); |
193 | 193 |
|
194 |
- cli_dbgmsg("named_substr: (%d) %*s: %s%s\n", n, name_entry_size - 3, tabptr + 2, |
|
194 |
+ cli_dbgmsg("cli_pcre_report: (%d) %*s: %s%s\n", n, name_entry_size - 3, tabptr + 2, |
|
195 | 195 |
outstr, trunc ? " (trunc)":""); |
196 | 196 |
/* |
197 | 197 |
cli_dbgmsg("named_substr: (%d) %*s: %.*s%s\n", n, name_entry_size - 3, tabptr + 2, |
... | ... |
@@ -203,35 +221,28 @@ static void named_substr_print(struct cli_pcre_data *pd, const unsigned char *bu |
203 | 203 |
} |
204 | 204 |
|
205 | 205 |
/* TODO: audit this function */ |
206 |
-int cli_pcre_match(struct cli_pcre_data *pd, const unsigned char *buffer, uint32_t buflen, int override_offset, int options, int *ovector, size_t ovlen) |
|
206 |
+void cli_pcre_report(const struct cli_pcre_data *pd, const unsigned char *buffer, uint32_t buflen, int rc, int *ovector, size_t ovlen) |
|
207 | 207 |
{ |
208 |
- int rc, startoffset, i, j, length, trunc; |
|
208 |
+ int i, j, length, trunc; |
|
209 | 209 |
const char *start; |
210 | 210 |
char outstr[2*MATCH_MAXLEN+1]; |
211 | 211 |
|
212 |
- if (ovlen % 3) { |
|
213 |
- cli_dbgmsg("cli_pcre_match: ovector length is not a multiple of 3\n"); |
|
214 |
- return CL_EARG; |
|
215 |
- } |
|
216 |
- |
|
217 |
- /* set the startoffset, override if a value is specified */ |
|
218 |
- startoffset = pd->search_offset; |
|
219 |
- if (override_offset >= 0) |
|
220 |
- startoffset = override_offset; |
|
221 |
- |
|
222 |
- /* execute the pcre */ |
|
223 |
- rc = pcre_exec(pd->re, pd->ex, buffer, buflen, startoffset, options, ovector, ovlen); |
|
224 |
- |
|
225 | 212 |
/* print out additional diagnostics if cli_debug_flag is set */ |
226 |
- if (!DISABLE_PCRE_REPORT && cli_debug_flag) { |
|
213 |
+ if (!DISABLE_PCRE_REPORT) { |
|
227 | 214 |
cli_dbgmsg("\n"); |
228 |
- cli_dbgmsg("cli_pcre_match: PCRE Execution Report:\n"); |
|
215 |
+ cli_dbgmsg("cli_pcre_report: PCRE Execution Report:\n"); |
|
216 |
+ cli_dbgmsg("cli_pcre_report: running regex /%s/ returns %d\n", pd->expression, rc); |
|
229 | 217 |
if (rc > 0) { |
230 | 218 |
/* print out full-match and capture groups */ |
231 | 219 |
for (i = 0; i < rc; ++i) { |
232 | 220 |
start = buffer + ovector[2*i]; |
233 | 221 |
length = ovector[2*i+1] - ovector[2*i]; |
234 | 222 |
|
223 |
+ if (ovector[2*i+1] >= buflen) { |
|
224 |
+ cli_warnmsg("cli_pcre_report: reported match goes outside buffer\n"); |
|
225 |
+ continue; |
|
226 |
+ } |
|
227 |
+ |
|
235 | 228 |
trunc = 0; |
236 | 229 |
if (length > MATCH_MAXLEN) { |
237 | 230 |
trunc = 1; |
... | ... |
@@ -241,24 +252,22 @@ int cli_pcre_match(struct cli_pcre_data *pd, const unsigned char *buffer, uint32 |
241 | 241 |
for (j = 0; j < length; ++j) |
242 | 242 |
snprintf(outstr+(2*j), sizeof(outstr)-(2*j), "%02x", (unsigned int)*(start+j)); |
243 | 243 |
|
244 |
- cli_dbgmsg("cli_pcre_match: %d: %s%s\n", i, outstr, trunc ? " (trunc)":""); |
|
245 |
- //cli_dbgmsg("cli_pcre_match: %d: %.*s%s\n", i, length, start, trunc ? " (trunc)":""); |
|
244 |
+ cli_dbgmsg("cli_pcre_report: %d: %s%s\n", i, outstr, trunc ? " (trunc)":""); |
|
245 |
+ //cli_dbgmsg("cli_pcre_report: %d: %.*s%s\n", i, length, start, trunc ? " (trunc)":""); |
|
246 | 246 |
} |
247 | 247 |
|
248 | 248 |
named_substr_print(pd, buffer, ovector, ovlen); |
249 | 249 |
} |
250 | 250 |
else if (rc == 0 || rc == PCRE_ERROR_NOMATCH) { |
251 |
- cli_dbgmsg("cli_pcre_match: no match found\n"); |
|
251 |
+ cli_dbgmsg("cli_pcre_report: no match found\n"); |
|
252 | 252 |
} |
253 | 253 |
else { |
254 |
- cli_dbgmsg("cli_pcre_match: error occurred in pcre_match: %d\n", rc); |
|
254 |
+ cli_dbgmsg("cli_pcre_report: error occurred in pcre_match: %d\n", rc); |
|
255 | 255 |
/* error handled by caller */ |
256 | 256 |
} |
257 |
- cli_dbgmsg("cli_pcre_match: PCRE Execution Report End\n"); |
|
257 |
+ cli_dbgmsg("cli_pcre_report: PCRE Execution Report End\n"); |
|
258 | 258 |
cli_dbgmsg("\n"); |
259 | 259 |
} |
260 |
- |
|
261 |
- return rc; |
|
262 | 260 |
} |
263 | 261 |
|
264 | 262 |
void cli_pcre_free_single(struct cli_pcre_data *pd) |
... | ... |
@@ -52,6 +52,7 @@ int cli_pcre_parse(struct cli_pcre_data *pd, const char *pattern); |
52 | 52 |
int cli_pcre_addoptions(struct cli_pcre_data *pd, const char **opt, int errout); |
53 | 53 |
int cli_pcre_compile(struct cli_pcre_data *pd, long long unsigned match_limit, long long unsigned match_limit_recursion, unsigned int options, int opt_override); |
54 | 54 |
int cli_pcre_match(struct cli_pcre_data *pd, const unsigned char *buffer, uint32_t buflen, int override_offset, int options, int *ovector, size_t ovlen); |
55 |
+void cli_pcre_report(const struct cli_pcre_data *pd, const unsigned char *buffer, uint32_t buflen, int rc, int *ovector, size_t ovlen); |
|
55 | 56 |
void cli_pcre_free_single(struct cli_pcre_data *pd); |
56 | 57 |
#endif /* HAVE_PCRE */ |
57 | 58 |
#endif /*_REGEX_PCRE_H_*/ |