From: Fabian Keil Date: Tue, 11 Jul 2023 04:22:16 +0000 (+0200) Subject: Add regex_matches() to reduce HAVE_PCRE2 ifdefs X-Git-Url: http://www.privoxy.org/gitweb/%22http:/i.j.b/static/gitweb.css?a=commitdiff_plain;h=662426360b8d10202feabdcd3515d64ea8833798;p=privoxy.git Add regex_matches() to reduce HAVE_PCRE2 ifdefs --- diff --git a/actions.c b/actions.c index 379c5e97..9fd3249c 100644 --- a/actions.c +++ b/actions.c @@ -828,12 +828,8 @@ int update_action_bits_for_tag(struct client_state *csp, const char *tag) continue; } -#ifdef HAVE_PCRE2 - if (pcre2_pattern_matches(b->url->pattern.tag_regex, tag)) -#else /* and check if one of the tag patterns matches the tag, */ - if (0 == regexec(b->url->pattern.tag_regex, tag, 0, NULL, 0)) -#endif + if (regex_matches(b->url->pattern.tag_regex, tag)) { /* if it does, update the action bit map, */ if (merge_current_action(csp->action, b->action)) @@ -888,11 +884,7 @@ jb_err check_negative_tag_patterns(struct client_state *csp, unsigned int flag) } for (tag = csp->tags->first; NULL != tag; tag = tag->next) { -#ifdef HAVE_PCRE2 - if (pcre2_pattern_matches(b->url->pattern.tag_regex, tag->str)) -#else - if (0 == regexec(b->url->pattern.tag_regex, tag->str, 0, NULL, 0)) -#endif + if (regex_matches(b->url->pattern.tag_regex, tag->str)) { /* * The pattern matches at least one tag, thus the action diff --git a/client-tags.c b/client-tags.c index 474e5695..d8cbb350 100644 --- a/client-tags.c +++ b/client-tags.c @@ -659,11 +659,7 @@ int client_tag_match(const struct pattern_spec *pattern, for (tag = tags->first; tag != NULL; tag = tag->next) { -#ifdef HAVE_PCRE2 - if (pcre2_pattern_matches(pattern->pattern.tag_regex, tag->str)) -#else - if (0 == regexec(pattern->pattern.tag_regex, tag->str, 0, NULL, 0)) -#endif + if (regex_matches(pattern->pattern.tag_regex, tag->str)) { log_error(LOG_LEVEL_TAGGING, "Client tag '%s' matches.", tag->str); return 1; diff --git a/urlmatch.c b/urlmatch.c index 84e9d298..22feb9c8 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1160,7 +1160,7 @@ static int simplematch(const char *pattern, const char *text) * Returns : TRUE for yes, FALSE otherwise. * *********************************************************************/ -int pcre2_pattern_matches(const pcre2_code *pattern, const char *string) +static int pcre2_pattern_matches(const pcre2_code *pattern, const char *string) { PCRE2_SIZE offset; int ret; @@ -1189,6 +1189,29 @@ int pcre2_pattern_matches(const pcre2_code *pattern, const char *string) #endif +/********************************************************************* + * + * Function : regex_matches + * + * Description : Checks if a compiled regex pattern matches a string + * using either pcre2 or pcre1 code. + * + * Parameters : + * 1 : pattern = The compiled pattern + * 2 : string = The string to check + * + * Returns : TRUE for yes, FALSE otherwise. + * + *********************************************************************/ +int regex_matches(const REGEX_TYPE *pattern, const char *string) +{ +#ifdef HAVE_PCRE2 + return pcre2_pattern_matches(pattern, string); +#else + return (0 == regexec(pattern, string, 0, NULL, 0)); +#endif +} + /********************************************************************* * * Function : simple_domaincmp @@ -1483,13 +1506,7 @@ static int host_matches(const struct http_request *http, if (pattern->pattern.url_spec.host_regex_type == PCRE_HOST_PATTERN) { return ((NULL == pattern->pattern.url_spec.host_regex) -#ifdef HAVE_PCRE2 - || pcre2_pattern_matches(pattern->pattern.url_spec.host_regex, - http->host)); -#else - || (0 == regexec(pattern->pattern.url_spec.host_regex, - http->host, 0, NULL, 0))); -#endif + || regex_matches(pattern->pattern.url_spec.host_regex, http->host)); } #endif return ((NULL == pattern->pattern.url_spec.dbuffer) || (0 == domain_match(pattern, http))); @@ -1512,11 +1529,7 @@ static int host_matches(const struct http_request *http, static int path_matches(const char *path, const struct pattern_spec *pattern) { return ((NULL == pattern->pattern.url_spec.preg) -#ifdef HAVE_PCRE2 - || (pcre2_pattern_matches(pattern->pattern.url_spec.preg, path))); -#else - || (0 == regexec(pattern->pattern.url_spec.preg, path, 0, NULL, 0))); -#endif + || regex_matches(pattern->pattern.url_spec.preg, path)); } diff --git a/urlmatch.h b/urlmatch.h index 8643aa4e..543f2b88 100644 --- a/urlmatch.h +++ b/urlmatch.h @@ -50,9 +50,7 @@ extern int url_requires_percent_encoding(const char *url); extern int url_match(const struct pattern_spec *pattern, const struct http_request *http); -#ifdef HAVE_PCRE2 -extern int pcre2_pattern_matches(const pcre2_code *pattern, const char *string); -#endif +int regex_matches(const REGEX_TYPE *pattern, const char *string); extern jb_err create_pattern_spec(struct pattern_spec *url, char *buf); extern void free_pattern_spec(struct pattern_spec *url);