aboutsummaryrefslogtreecommitdiff
path: root/net/privoxy/patches/101-Add-regex_matches-to-reduce-HAVE_PCRE2-ifdefs.patch
blob: a77ddac058ad87e440beb4b2e33f56111ee844a3 (plain)
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
From 662426360b8d10202feabdcd3515d64ea8833798 Mon Sep 17 00:00:00 2001
From: Fabian Keil <fk@fabiankeil.de>
Date: Tue, 11 Jul 2023 06:22:16 +0200
Subject: [PATCH] Add regex_matches() to reduce HAVE_PCRE2 ifdefs

---
 actions.c     | 12 ++----------
 client-tags.c |  6 +-----
 urlmatch.c    | 39 ++++++++++++++++++++++++++-------------
 urlmatch.h    |  4 +---
 4 files changed, 30 insertions(+), 31 deletions(-)

--- a/actions.c
+++ b/actions.c
@@ -828,12 +828,8 @@ int update_action_bits_for_tag(struct cl
             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(struc
          }
          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
--- a/client-tags.c
+++ b/client-tags.c
@@ -659,11 +659,7 @@ int client_tag_match(const struct patter
 
    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;
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -1160,7 +1160,7 @@ static int simplematch(const char *patte
  * 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;
@@ -1191,6 +1191,29 @@ int pcre2_pattern_matches(const pcre2_co
 
 /*********************************************************************
  *
+ * 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
  *
  * Description :  Domain-wise Compare fqdn's.  The comparison is
@@ -1483,13 +1506,7 @@ static int host_matches(const struct htt
    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 htt
 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));
 }
 
 
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -50,9 +50,7 @@ extern int url_requires_percent_encoding
 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);