From dad26e6cd219f2572a30df657b0560d55de68a29 Mon Sep 17 00:00:00 2001 From: John Johansen Date: Sat, 16 Oct 2021 03:00:26 -0700 Subject: [PATCH] parser: add a method for profiles to do rule merging In preparation for file rules converting to use rule_t add a method to do rule merging. Signed-off-by: John Johansen --- parser/profile.cc | 37 +++++++++++++++++++++++++++++++++++++ parser/profile.h | 6 ++++++ 2 files changed, 43 insertions(+) diff --git a/parser/profile.cc b/parser/profile.cc index 54ca6b9b5..27fe570ee 100644 --- a/parser/profile.cc +++ b/parser/profile.cc @@ -18,6 +18,8 @@ #include #include +#include +#include const char *profile_mode_table[] = { "", @@ -119,6 +121,41 @@ Profile::~Profile() free(net.quiet); } +static bool comp (rule_t *lhs, rule_t *rhs) { return (*lhs < *rhs); } + +bool Profile::merge_rules(void) +{ + int count = 0; + + for (RuleList::iterator i = rule_ents.begin(); i != rule_ents.end(); ) { + if ((*i)->is_mergeable()) + count++; + } + if (count < 2) + return 0; + + std::vector table(count); + int n = 0; + for (RuleList::iterator i = rule_ents.begin(); i != rule_ents.end(); ) { + if ((*i)->is_mergeable()) + table[n++] = *i; + } + + std::sort(table.begin(), table.end(), comp); + + for (int i = 0, j = 1; j < count; j++) { + if (table[i]->cmp(*table[j]) == 0) { + if (!table[i]->merge(*table[j])) + return false; + continue; + } + i = j; + } + + return true; +} + + int add_entry_to_x_table(Profile *prof, char *name) { int i; diff --git a/parser/profile.h b/parser/profile.h index e324d9f1d..80ffd20e1 100644 --- a/parser/profile.h +++ b/parser/profile.h @@ -251,6 +251,12 @@ public: return strcmp(name, rhs.name) < 0; } + /* + * Requires the merged rules have customized methods + * cmp(), is_mergeable() and merge() + */ + virtual bool merge_rules(void); + void dump(void) { if (ns)