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.
This commit is contained in:
João Valverde 2021-12-21 00:04:33 +00:00
parent 392745c56f
commit 36d5aad962
6 changed files with 27 additions and 14 deletions

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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, subj, subj_size);
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, (PCRE2_SPTR)subj, (PCRE2_SIZE)subj_length);
}

View File

@ -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, size_t subj_size);
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_length(const ws_regex_t *re,
const char *subj, size_t subj_length);
WS_DLL_PUBLIC void
ws_regex_free(ws_regex_t *re);