aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2021-12-21 00:04:33 +0000
committerJoão Valverde <j@v6e.pt>2021-12-21 00:40:02 +0000
commit36d5aad962f6c481a4aed09a0a8f7a575fcbdd12 (patch)
tree9952f0da08ff8633dac374776ddb60562bf01c55
parent392745c56f304dc851c02b91dde6f8ce9710c3b2 (diff)
wsutil: Split ws_regex_matches() into two functions
Split ws_regex_matches() into two functions with better semantics and remove the WS_REGEX_ZERO_TERMINATED symbol. ws_regex_matches() matches zero terminated strings. ws_regex_matches_length() matches a string length in code units.
-rw-r--r--debian/libwsutil0.symbols1
-rw-r--r--epan/ftypes/ftype-bytes.c2
-rw-r--r--epan/ftypes/ftype-protocol.c4
-rw-r--r--epan/ftypes/ftype-string.c2
-rw-r--r--wsutil/regex.c22
-rw-r--r--wsutil/regex.h10
6 files changed, 27 insertions, 14 deletions
diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols
index c11a8294ad..7da55031ff 100644
--- a/debian/libwsutil0.symbols
+++ b/debian/libwsutil0.symbols
@@ -424,6 +424,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_regex_compile@Base 3.7.0
ws_regex_free@Base 3.7.0
ws_regex_matches@Base 3.7.0
+ ws_regex_matches_length@Base 3.7.0
ws_regex_pattern@Base 3.7.0
ws_socket_ptoa@Base 3.1.1
ws_strcasestr@Base 3.7.0
diff --git a/epan/ftypes/ftype-bytes.c b/epan/ftypes/ftype-bytes.c
index 12c703b185..f03167e852 100644
--- a/epan/ftypes/ftype-bytes.c
+++ b/epan/ftypes/ftype-bytes.c
@@ -558,7 +558,7 @@ cmp_matches(const fvalue_t *fv, const ws_regex_t *regex)
{
GByteArray *a = fv->value.bytes;
- return ws_regex_matches(regex, a->data, a->len);
+ return ws_regex_matches_length(regex, a->data, a->len);
}
void
diff --git a/epan/ftypes/ftype-protocol.c b/epan/ftypes/ftype-protocol.c
index b50b52ef03..dba821d7d6 100644
--- a/epan/ftypes/ftype-protocol.c
+++ b/epan/ftypes/ftype-protocol.c
@@ -291,9 +291,9 @@ cmp_matches(const fvalue_t *fv, const ws_regex_t *regex)
if (a->tvb != NULL) {
tvb_len = tvb_captured_length(a->tvb);
data = (const char *)tvb_get_ptr(a->tvb, 0, tvb_len);
- rc = ws_regex_matches(regex, data, tvb_len);
+ rc = ws_regex_matches_length(regex, data, tvb_len);
} else {
- rc = ws_regex_matches(regex, a->proto_string, WS_REGEX_ZERO_TERMINATED);
+ rc = ws_regex_matches(regex, a->proto_string);
}
}
CATCH_ALL {
diff --git a/epan/ftypes/ftype-string.c b/epan/ftypes/ftype-string.c
index 133f5a9b38..606cdd0194 100644
--- a/epan/ftypes/ftype-string.c
+++ b/epan/ftypes/ftype-string.c
@@ -152,7 +152,7 @@ cmp_matches(const fvalue_t *fv, const ws_regex_t *regex)
if (! regex) {
return FALSE;
}
- return ws_regex_matches(regex, str, WS_REGEX_ZERO_TERMINATED);
+ return ws_regex_matches(regex, str);
}
void
diff --git a/wsutil/regex.c b/wsutil/regex.c
index 0698df9161..bb3bd0b3de 100644
--- a/wsutil/regex.c
+++ b/wsutil/regex.c
@@ -83,20 +83,17 @@ ws_regex_compile(const char *patt, char **errmsg)
static bool
-match_pcre2(pcre2_code *code, const char *subj, size_t subj_size)
+match_pcre2(pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length)
{
- PCRE2_SIZE length;
pcre2_match_data *match_data;
int rc;
- length = subj_size == WS_REGEX_ZERO_TERMINATED ? PCRE2_ZERO_TERMINATED : (PCRE2_SIZE)subj_size;
-
/* We don't use the matched substring but pcre2_match requires
* at least one pair of offsets. */
match_data = pcre2_match_data_create(1, NULL);
rc = pcre2_match(code,
- subj,
+ subject,
length,
0, /* start at offset zero of the subject */
0, /* default options */
@@ -123,12 +120,23 @@ match_pcre2(pcre2_code *code, const char *subj, size_t subj_size)
bool
-ws_regex_matches(const ws_regex_t *re, const char *subj, size_t subj_size)
+ws_regex_matches(const ws_regex_t *re, const char *subj)
+{
+ ws_return_val_if_null(re, FALSE);
+ ws_return_val_if_null(subj, FALSE);
+
+ return match_pcre2(re->code, (PCRE2_SPTR)subj, PCRE2_ZERO_TERMINATED);
+}
+
+
+bool
+ws_regex_matches_length(const ws_regex_t *re,
+ const char *subj, size_t subj_length)
{
ws_return_val_if_null(re, FALSE);
ws_return_val_if_null(subj, FALSE);
- return match_pcre2(re->code, subj, subj_size);
+ return match_pcre2(re->code, (PCRE2_SPTR)subj, (PCRE2_SIZE)subj_length);
}
diff --git a/wsutil/regex.h b/wsutil/regex.h
index eec51b7baf..df38ecae32 100644
--- a/wsutil/regex.h
+++ b/wsutil/regex.h
@@ -12,8 +12,6 @@
#include <wireshark.h>
-#define WS_REGEX_ZERO_TERMINATED SIZE_MAX
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -24,8 +22,14 @@ typedef struct _ws_regex ws_regex_t;
WS_DLL_PUBLIC ws_regex_t *
ws_regex_compile(const char *patt, char **errmsg);
+/** Matches a null-terminated subject string. */
+WS_DLL_PUBLIC bool
+ws_regex_matches(const ws_regex_t *re, const char *subj);
+
+/** Matches a subject string length in 8 bit code units. */
WS_DLL_PUBLIC bool
-ws_regex_matches(const ws_regex_t *re, const char *subj, size_t subj_size);
+ws_regex_matches_length(const ws_regex_t *re,
+ const char *subj, size_t subj_length);
WS_DLL_PUBLIC void
ws_regex_free(ws_regex_t *re);