dfilter: Clean up lexical scanning

This commit is contained in:
João Valverde 2022-04-06 14:58:10 +01:00
parent 6057d1a6e2
commit a6f37323e6
5 changed files with 35 additions and 33 deletions

View File

@ -123,9 +123,6 @@ DfilterTrace(FILE *TraceFILE, char *zTracePrompt);
header_field_info *
dfilter_resolve_unparsed(dfwork_t *dfw, const char *name);
char *
dfilter_literal_normalized(const char *token);
const char *tokenstr(int token);
#endif

View File

@ -86,9 +86,6 @@ dfilter_resolve_unparsed(dfwork_t *dfw, const char *name)
{
header_field_info *hfinfo;
if (*name == '.')
name += 1;
hfinfo = proto_registrar_get_byname(name);
if (hfinfo != NULL) {
/* It's a field name */
@ -106,17 +103,6 @@ dfilter_resolve_unparsed(dfwork_t *dfw, const char *name)
return NULL;
}
char *
dfilter_literal_normalized(const char *token)
{
if (*token == '<') {
char *end = strchr(token, '>');
return g_strndup(token + 1, end - (token + 1));
}
return g_strdup(token);
}
/* Initialize the dfilter module */
void
dfilter_init(void)

View File

@ -364,7 +364,7 @@ v6-cidr-prefix \/[[:digit:]]{1,3}
/* None of the patterns below can match ".." anywhere in the token string. */
{MacAddress}|{QuadMacAddress} {
/* MAC Address literal. */
/* MAC Address. */
return set_lval_str(TOKEN_UNPARSED, yytext);
}
@ -378,27 +378,31 @@ v6-cidr-prefix \/[[:digit:]]{1,3}
return set_lval_str(TOKEN_UNPARSED, yytext);
}
:?[[:xdigit:]]+:[[:xdigit:]:]* {
[[:xdigit:]]+:[[:xdigit:]:]* {
/* Bytes. */
if (yytext[0] == ':') {
/* Skip leading colon. */
return set_lval_str(TOKEN_LITERAL, yytext + 1);
}
return set_lval_str(TOKEN_UNPARSED, yytext);
}
:[[:xdigit:]:-]+(\.[[:xdigit:]:-]+)* {
/* Literal starting with colon. Bytes, integer or float. */
/* Skip leading colon. */
return set_lval_str(TOKEN_LITERAL, yytext + 1);
}
"<"[^>=]+">" {
/* Literal in-between angle brackets (cannot be parsed as a protocol field). */
return set_lval_str(TOKEN_LITERAL, yytext);
/* Strip brackets. */
char *end = strchr(yytext, '>');
*end = '\0';
df_lval->value = g_strdup(yytext + 1);
return TOKEN_LITERAL;
}
[:.]?[[:alnum:]_]{WORD_CHAR}*(\.{WORD_CHAR}+)* {
/* Identifier or literal or unparsed. */
if (yytext[0] == '.')
return set_lval_str(TOKEN_IDENTIFIER, yytext);
if (yytext[0] == ':') {
/* Skip leading colon. */
return set_lval_str(TOKEN_LITERAL, yytext + 1);
\.?[[:alnum:]_]{WORD_CHAR}*(\.{WORD_CHAR}+)* {
/* Identifier or unparsed. */
if (yytext[0] == '.') {
/* Skip leading dot. */
return set_lval_str(TOKEN_IDENTIFIER, yytext + 1);
}
return set_lval_str(TOKEN_UNPARSED, yytext);
}

View File

@ -185,8 +185,7 @@ stnode_new_unparsed(const char *str, char *token)
stnode_t *
stnode_new_literal(const char *str, char *token)
{
char *value = dfilter_literal_normalized(str);
return stnode_new(STTYPE_LITERAL, value, token);
return stnode_new(STTYPE_LITERAL, g_strdup(str), token);
}
stnode_t *

View File

@ -137,6 +137,22 @@ class case_equality(unittest.TestCase):
dfilter = "udp contains <ce:13>"
checkDFilterCount(dfilter, 1)
def test_literal_3(self, checkDFilterCount):
dfilter = "frame[0:10] contains :00:01:6c"
checkDFilterCount(dfilter, 1)
def test_literal_4(self, checkDFilterCount):
dfilter = "frame[0:10] contains :00016c"
checkDFilterCount(dfilter, 1)
def test_literal_5(self, checkDFilterCount):
dfilter = "frame[0:10] contains :00.01.6c"
checkDFilterCount(dfilter, 1)
def test_literal_6(self, checkDFilterCount):
dfilter = "frame[0:10] contains :00-01-6c"
checkDFilterCount(dfilter, 1)
@fixtures.uses_fixtures
class case_bitwise(unittest.TestCase):
trace_file = "http.pcap"