dfilter: Make regex matches case insensitive by default

This commit is contained in:
João Valverde 2022-06-08 11:38:35 +01:00
parent bd0f0cbbed
commit de103394fe
5 changed files with 30 additions and 4 deletions

View File

@ -1148,7 +1148,7 @@ check_relation_matches(dfwork_t *dfw, stnode_t *st_node,
patt = stnode_data(st_arg2);
ws_debug("Compile regex pattern: %s", patt);
pcre = ws_regex_compile(patt, &errmsg);
pcre = ws_regex_compile_ex(patt, &errmsg, WS_REGEX_CASELESS|WS_REGEX_NEVER_UTF);
if (errmsg) {
dfilter_fail(dfw, NULL, "Regex compilation error: %s.", errmsg);
g_free(errmsg);

View File

@ -428,6 +428,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_pipe_spawn_sync@Base 2.5.1
ws_read_string_from_pipe@Base 2.5.0
ws_regex_compile@Base 3.7.0
ws_regex_compile_ex@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

View File

@ -53,6 +53,11 @@ class case_syntax(unittest.TestCase):
dfilter = r'http.host matches r"update\.microsoft\.c.."'
checkDFilterCount(dfilter, 1)
def test_matches_5(self, checkDFilterSucceed):
# case insensitive
dfilter = 'http.request.method matches "^head"'
checkDFilterSucceed(dfilter)
def test_equal_1(self, checkDFilterCount):
dfilter = 'ip.addr == 10.0.0.5'
checkDFilterCount(dfilter, 1)

View File

@ -43,16 +43,22 @@ get_error_msg(int errorcode)
static pcre2_code *
compile_pcre2(const char *patt, char **errmsg)
compile_pcre2(const char *patt, char **errmsg, unsigned flags)
{
pcre2_code *code;
int errorcode;
PCRE2_SIZE erroroffset;
uint32_t options = 0;
if (flags & WS_REGEX_NEVER_UTF)
options |= PCRE2_NEVER_UTF;
if (flags & WS_REGEX_CASELESS)
options |= PCRE2_CASELESS;
/* By default UTF-8 is off. */
code = pcre2_compile_8((PCRE2_SPTR)patt,
PCRE2_ZERO_TERMINATED,
PCRE2_NEVER_UTF,
options,
&errorcode,
&erroroffset,
NULL);
@ -68,10 +74,16 @@ compile_pcre2(const char *patt, char **errmsg)
ws_regex_t *
ws_regex_compile(const char *patt, char **errmsg)
{
return ws_regex_compile_ex(patt, errmsg, 0);
}
ws_regex_t *
ws_regex_compile_ex(const char *patt, char **errmsg, unsigned flags)
{
ws_return_val_if_null(patt, NULL);
pcre2_code *code = compile_pcre2(patt, errmsg);
pcre2_code *code = compile_pcre2(patt, errmsg, flags);
if (code == NULL)
return NULL;

View File

@ -22,6 +22,14 @@ typedef struct _ws_regex ws_regex_t;
WS_DLL_PUBLIC ws_regex_t *
ws_regex_compile(const char *patt, char **errmsg);
#define WS_REGEX_CASELESS (1U << 0)
/* By default UTF-8 is off. This option also prevents it from being
* turned on using a pattern option. */
#define WS_REGEX_NEVER_UTF (1U << 1)
WS_DLL_PUBLIC ws_regex_t *
ws_regex_compile_ex(const char *patt, char **errmsg, unsigned flags);
/** Matches a null-terminated subject string. */
WS_DLL_PUBLIC bool
ws_regex_matches(const ws_regex_t *re, const char *subj);