From 194ebd377f27499a3010b2bbd7697cb91da510a8 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Thu, 16 Nov 2023 01:30:49 +0100 Subject: Clean the Python script converting the ROST grammar. --- src/analysis/scan/grammar.y | 52 ++++++++++++++++++++++++++------------------- src/analysis/scan/tokens.l | 25 ---------------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/analysis/scan/grammar.y b/src/analysis/scan/grammar.y index 8380a81..8298855 100644 --- a/src/analysis/scan/grammar.y +++ b/src/analysis/scan/grammar.y @@ -6,10 +6,10 @@ /* Affiche un message d'erreur suite à l'analyse en échec. */ -static int yyerror(GContentScanner *, yyscan_t, GScanRule **, sized_string_t *, sized_string_t *, void/*GBytesPattern*/ **, char *); +static int yyerror(GContentScanner *, yyscan_t, GScanRule **, sized_string_t *, sized_string_t *, char *); #define raise_error(msg) \ - yyerror(scanner, yyscanner, built_rule, tmp_0, tmp_1, NULL, msg) + yyerror(scanner, yyscanner, built_rule, tmp_0, tmp_1, msg) %} @@ -76,7 +76,6 @@ typedef void *yyscan_t; } masked; ScanRuleFlags rule_flags; /* Fanions pour règle */ - GScanRule *rule; /* Nouvelle règle à intégrer */ GScanTokenNode *node; /* Bribe de motif à intégrer */ GSearchPattern *pattern; /* Nouveau motif à considérer */ @@ -85,7 +84,6 @@ typedef void *yyscan_t; modifier_arg_t mod_arg; /* Argument pour modificateur */ ScanPlainNodeFlags str_flags; /* Fanions pour texte */ - GScanExpression *expr; /* Expression de condition */ struct { @@ -103,13 +101,13 @@ typedef void *yyscan_t; %define api.pure full -%parse-param { GContentScanner *scanner } { yyscan_t yyscanner } { GScanRule **built_rule } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { void /*GBytesPattern*/ **built_pattern } -%lex-param { yyscan_t yyscanner } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { void/*GBytesPattern*/ **built_pattern } +%parse-param { GContentScanner *scanner } { yyscan_t yyscanner } { GScanRule **built_rule } { sized_string_t *tmp_0} { sized_string_t *tmp_1} +%lex-param { yyscan_t yyscanner } { sized_string_t *tmp_0} { sized_string_t *tmp_1} %code provides { #define YY_DECL \ - int rost_lex(YYSTYPE *yylval_param, yyscan_t yyscanner, sized_string_t *tmp_0, sized_string_t *tmp_1, void/*GBytesPattern*/ **built_pattern) + int rost_lex(YYSTYPE *yylval_param, yyscan_t yyscanner, sized_string_t *tmp_0, sized_string_t *tmp_1) YY_DECL; @@ -246,7 +244,6 @@ YY_DECL; %type rule_flags %type rule_flag -%type rule %type PLAIN_TEXT %type ESCAPED_TEXT @@ -323,20 +320,36 @@ YY_DECL; %left HOOK_O HOOK_C + %destructor { g_object_unref(G_OBJECT($$)); } + + %destructor { g_object_unref(G_OBJECT($$)); } + + %destructor { g_object_unref(G_OBJECT($$)); } + + %destructor { g_object_unref(G_OBJECT($$)); } + + %destructor { + size_t __i; + + for (__i = 0; __i < $$.count; __i++) + g_object_unref(G_OBJECT($$.args[__i])); + if ($$.args != NULL) + free($$.args); -%destructor { printf("-------- Discarding symbol %p.\n", $$); } + } %% rules : /* empty */ | external rules - | rule rules + | rule { - g_content_scanner_add_rule(scanner, $1); - g_object_unref(G_OBJECT($1)); + g_content_scanner_add_rule(scanner, *built_rule); + g_clear_object(built_rule); } + rules ; @@ -368,12 +381,8 @@ YY_DECL; rule : rule_flags "rule" RULE_IDENTIFIER { *built_rule = g_scan_rule_new($1, $3.data); - $$ = *built_rule; } tags "{" meta bytes condition "}" - { - $$ = $4; - } ; @@ -1880,9 +1889,9 @@ relational_expr : cexpression "<" cexpression * * ******************************************************************************/ -static int yyerror(GContentScanner *scanner, yyscan_t yyscanner, GScanRule **built_rule, sized_string_t *tmp_0, sized_string_t *tmp_1, void/*GBytesPattern*/ **built_pattern, char *msg) +static int yyerror(GContentScanner *scanner, yyscan_t yyscanner, GScanRule **built_rule, sized_string_t *tmp_0, sized_string_t *tmp_1, char *msg) { - printf("YYERROR line %d: %s\n", yyget_lineno(yyscanner), msg); + //printf("YYERROR line %d: %s\n", yyget_lineno(yyscanner), msg); return 0; @@ -1909,7 +1918,6 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_ GScanRule *built_rule; /* Règle en construction */ sized_string_t tmp_0; /* Zone tampon #1 */ sized_string_t tmp_1; /* Zone tampon #2 */ - void /*GBytesPattern*/ *built_pattern; /* Motif en construction */ yyscan_t lexstate; /* Gestion d'analyse lexicale */ YY_BUFFER_STATE state; /* Contexte d'analyse */ int status; /* Bilan d'une analyse */ @@ -1924,13 +1932,11 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_ tmp_1.data = malloc((length + 1) * sizeof(bin_t)); tmp_1.len = 0; - built_pattern = NULL; - rost_lex_init(&lexstate); state = rost__scan_bytes(text, length, lexstate); - status = yyparse(scanner, lexstate, &built_rule, &tmp_0, &tmp_1, &built_pattern); + status = yyparse(scanner, lexstate, &built_rule, &tmp_0, &tmp_1); result = (status == EXIT_SUCCESS); @@ -1941,6 +1947,8 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_ exit_szstr(&tmp_0); exit_szstr(&tmp_1); + g_clear_object(&built_rule); + return result; } diff --git a/src/analysis/scan/tokens.l b/src/analysis/scan/tokens.l index b1543c9..0dff9f4 100644 --- a/src/analysis/scan/tokens.l +++ b/src/analysis/scan/tokens.l @@ -982,31 +982,6 @@ bytes_fuzzy_id [\*A-Za-z_][\*A-Za-z0-9_]* -"\"" { - POP_STATE; - // *built_pattern = g_bytes_pattern_new(); - PUSH_STATE(bytes_value_raw); - } - -"\"" { POP_STATE; /*yylval->pattern = *built_pattern*/; return 11111/*MASKED_STRING*/; } - -"\\\"" { }//g_bytes_pattern_append_data(*built_pattern, '"', 0xff); } -"\\t" { }//g_bytes_pattern_append_data(*built_pattern, '\t', 0xff); } -"\\r" { }//g_bytes_pattern_append_data(*built_pattern, '\r', 0xff); } -"\\n" { }//g_bytes_pattern_append_data(*built_pattern, '\n', 0xff); } -"\\\\" { }//g_bytes_pattern_append_data(*built_pattern, '\\', 0xff); } - -\\x[0-9a-fA-F]{2} { - uint8_t __ch; - __ch = strtol(yytext + 2, NULL, 16); - printf("__ch: %hhx\n", __ch); - //g_bytes_pattern_append_data(*built_pattern, __ch, 0xff); - } - -. { }//g_bytes_pattern_append_data(*built_pattern, *yytext, 0xff); } - - - -- cgit v0.11.2-87-g4458