From 7340659cc22e54107959b6abd7eee812f047e0c3 Mon Sep 17 00:00:00 2001 From: liuwentan Date: Mon, 25 Sep 2023 17:27:29 +0800 Subject: [PATCH] [PATCH]remove duplicate code for expr_matcher --- .../expr_matcher/adapter_hs/adapter_hs.cpp | 96 ++++++------ .../expr_matcher/adapter_rs/adapter_rs.cpp | 142 +++++++----------- 2 files changed, 107 insertions(+), 131 deletions(-) diff --git a/scanner/expr_matcher/adapter_hs/adapter_hs.cpp b/scanner/expr_matcher/adapter_hs/adapter_hs.cpp index d341c8d..001d160 100644 --- a/scanner/expr_matcher/adapter_hs/adapter_hs.cpp +++ b/scanner/expr_matcher/adapter_hs/adapter_hs.cpp @@ -756,6 +756,54 @@ static void adapter_hs_stream_reset(struct adapter_hs_stream *hs_stream) utarray_clear(hs_stream->matched_pat->pattern_ids); } +int adapter_hs_scan_match(struct bool_matcher *bm, UT_array *pattern_ids, + struct bool_expr_match *match_buff, size_t buff_size, + struct expr_scan_result *results, size_t n_result, + size_t *n_hit_result) +{ + size_t n_pattern_id = utarray_len(pattern_ids); + if (0 == n_pattern_id) { + *n_hit_result = 0; + return 0; + } + + utarray_sort(pattern_ids, compare_pattern_id); + + unsigned long long prev_pattern_id = 0xFFFFFFFFFFFFFFFF; + unsigned long long tmp_pattern_id = 0; + size_t n_unique_pattern_id = 0; + unsigned long long unique_pattern_ids[n_pattern_id]; + + for (size_t i = 0; i < n_pattern_id; i++) { + tmp_pattern_id = *(unsigned long long *)utarray_eltptr(pattern_ids, i); + if (tmp_pattern_id != prev_pattern_id) { + unique_pattern_ids[n_unique_pattern_id++] = tmp_pattern_id; + prev_pattern_id = tmp_pattern_id; + } + } + + int bool_matcher_ret = bool_matcher_match(bm, unique_pattern_ids, + n_unique_pattern_id, + match_buff, buff_size); + if (bool_matcher_ret < 0) { + goto next; + } + + if (bool_matcher_ret > (int)n_result) { + bool_matcher_ret = n_result; + } + + for (int index = 0; index < bool_matcher_ret; index++) { + results[index].rule_id = match_buff[index].expr_id; + results[index].user_tag = match_buff[index].user_tag; + } + *n_hit_result = bool_matcher_ret; + +next: + utarray_clear(pattern_ids); + return bool_matcher_ret; +} + int adapter_hs_scan_stream(void *hs_stream, const char *data, size_t data_len, struct expr_scan_result *results, size_t n_result, size_t *n_hit_result) @@ -822,51 +870,9 @@ int adapter_hs_scan_stream(void *hs_stream, const char *data, size_t data_len, return -1; } - size_t n_pattern_id = utarray_len(stream->matched_pat->pattern_ids); - if (0 == n_pattern_id) { - *n_hit_result = 0; - return 0; - } - - utarray_sort(stream->matched_pat->pattern_ids, compare_pattern_id); - - unsigned long long prev_pattern_id = 0xFFFFFFFFFFFFFFFF; - unsigned long long tmp_pattern_id = 0; - size_t n_unique_pattern_id = 0; - unsigned long long unique_pattern_ids[n_pattern_id]; - - for (size_t i = 0; i < n_pattern_id; i++) { - tmp_pattern_id = *(unsigned long long *)utarray_eltptr(stream->matched_pat->pattern_ids, i); - if (tmp_pattern_id != prev_pattern_id) { - unique_pattern_ids[n_unique_pattern_id++] = tmp_pattern_id; - prev_pattern_id = tmp_pattern_id; - } - } - - int ret = 0; - struct bool_expr_match *bool_matcher_results = scratch->bool_match_buffs[thread_id]; - int bool_matcher_ret = bool_matcher_match(stream->ref_hs_rt->bm, unique_pattern_ids, - n_unique_pattern_id, bool_matcher_results, - MAX_HIT_EXPR_NUM); - if (bool_matcher_ret < 0) { - ret = -1; - goto next; - } - - if (bool_matcher_ret > (int)n_result) { - bool_matcher_ret = n_result; - } - - for (int index = 0; index < bool_matcher_ret; index++) { - results[index].rule_id = bool_matcher_results[index].expr_id; - results[index].user_tag = bool_matcher_results[index].user_tag; - } - *n_hit_result = bool_matcher_ret; - -next: - utarray_clear(stream->matched_pat->pattern_ids); - - return ret; + return adapter_hs_scan_match(stream->ref_hs_rt->bm, stream->matched_pat->pattern_ids, + scratch->bool_match_buffs[thread_id], MAX_HIT_EXPR_NUM, + results, n_result, n_hit_result); } int adapter_hs_scan(void *hs_instance, int thread_id, const char *data, size_t data_len, diff --git a/scanner/expr_matcher/adapter_rs/adapter_rs.cpp b/scanner/expr_matcher/adapter_rs/adapter_rs.cpp index 42c47b2..04e05b2 100644 --- a/scanner/expr_matcher/adapter_rs/adapter_rs.cpp +++ b/scanner/expr_matcher/adapter_rs/adapter_rs.cpp @@ -562,6 +562,54 @@ void adapter_rs_stream_close(void *rs_stream) FREE(stream); } +int adapter_rs_scan_match(struct bool_matcher *bm, UT_array *pattern_ids, + struct bool_expr_match *match_buff, size_t buff_size, + struct expr_scan_result *results, size_t n_result, + size_t *n_hit_result) +{ + size_t n_pattern_id = utarray_len(pattern_ids); + if (0 == n_pattern_id) { + *n_hit_result = 0; + return 0; + } + + utarray_sort(pattern_ids, compare_pattern_id); + + unsigned long long prev_pattern_id = 0xFFFFFFFFFFFFFFFF; + unsigned long long tmp_pattern_id = 0; + size_t n_unique_pattern_id = 0; + unsigned long long unique_pattern_ids[n_pattern_id]; + + for (size_t i = 0; i < n_pattern_id; i++) { + tmp_pattern_id = *(unsigned long long *)utarray_eltptr(pattern_ids, i); + if (tmp_pattern_id != prev_pattern_id) { + unique_pattern_ids[n_unique_pattern_id++] = tmp_pattern_id; + prev_pattern_id = tmp_pattern_id; + } + } + + int bool_matcher_ret = bool_matcher_match(bm, unique_pattern_ids, + n_unique_pattern_id, + match_buff, buff_size); + if (bool_matcher_ret < 0) { + goto next; + } + + if (bool_matcher_ret > (int)n_result) { + bool_matcher_ret = n_result; + } + + for (int index = 0; index < bool_matcher_ret; index++) { + results[index].rule_id = match_buff[index].expr_id; + results[index].user_tag = match_buff[index].user_tag; + } + *n_hit_result = bool_matcher_ret; + +next: + utarray_clear(pattern_ids); + return bool_matcher_ret; +} + int adapter_rs_scan_stream(void *rs_stream, const char *data, size_t data_len, struct expr_scan_result *results, size_t n_result, size_t *n_hit_result) @@ -597,49 +645,10 @@ int adapter_rs_scan_stream(void *rs_stream, const char *data, size_t data_len, return -1; } - size_t n_pattern_id = utarray_len(matched_pat->pattern_ids); - if (0 == n_pattern_id) { - *n_hit_result = 0; - return 0; - } - - utarray_sort(matched_pat->pattern_ids, compare_pattern_id); - - unsigned long long prev_pattern_id = 0xFFFFFFFFFFFFFFFF; - unsigned long long tmp_pattern_id = 0; - size_t n_unique_pattern_id = 0; - unsigned long long unique_pattern_ids[n_pattern_id]; - - for (size_t i = 0; i < n_pattern_id; i++) { - tmp_pattern_id = *(unsigned long long *)utarray_eltptr(matched_pat->pattern_ids, i); - if (tmp_pattern_id != prev_pattern_id) { - unique_pattern_ids[n_unique_pattern_id++] = tmp_pattern_id; - prev_pattern_id = tmp_pattern_id; - } - } - - struct bool_expr_match *bool_matcher_results = rs_rt->bool_match_buffs[thread_id]; - int bool_matcher_ret = bool_matcher_match(rs_rt->bm, unique_pattern_ids, n_unique_pattern_id, - bool_matcher_results, MAX_HIT_EXPR_NUM); - if (bool_matcher_ret < 0) { - ret = -1; - goto next; - } - - if (bool_matcher_ret > (int)n_result) { - bool_matcher_ret = n_result; - } - - for (int index = 0; index < bool_matcher_ret; index++) { - results[index].rule_id = bool_matcher_results[index].expr_id; - results[index].user_tag = bool_matcher_results[index].user_tag; - } - *n_hit_result = bool_matcher_ret; - -next: - utarray_clear(matched_pat->pattern_ids); - - return ret; + return adapter_rs_scan_match(rs_rt->bm, matched_pat->pattern_ids, + rs_rt->bool_match_buffs[thread_id], + MAX_HIT_EXPR_NUM, results, n_result, + n_hit_result); } int adapter_rs_scan(void *rs_instance, int thread_id, const char *data, size_t data_len, @@ -675,47 +684,8 @@ int adapter_rs_scan(void *rs_instance, int thread_id, const char *data, size_t d return -1; } - size_t n_pattern_id = utarray_len(matched_pat->pattern_ids); - if (0 == n_pattern_id) { - *n_hit_result = 0; - return 0; - } - - utarray_sort(matched_pat->pattern_ids, compare_pattern_id); - - unsigned long long prev_pattern_id = 0xFFFFFFFFFFFFFFFF; - unsigned long long tmp_pattern_id = 0; - size_t n_unique_pattern_id = 0; - unsigned long long unique_pattern_ids[n_pattern_id]; - - for (size_t i = 0; i < n_pattern_id; i++) { - tmp_pattern_id = *(unsigned long long *)utarray_eltptr(matched_pat->pattern_ids, i); - if (tmp_pattern_id != prev_pattern_id) { - unique_pattern_ids[n_unique_pattern_id++] = tmp_pattern_id; - prev_pattern_id = tmp_pattern_id; - } - } - - struct bool_expr_match *bool_matcher_results = rs_rt->bool_match_buffs[thread_id]; - int bool_matcher_ret = bool_matcher_match(rs_rt->bm, unique_pattern_ids, n_unique_pattern_id, - bool_matcher_results, MAX_HIT_EXPR_NUM); - if (bool_matcher_ret < 0) { - ret = -1; - goto next; - } - - if (bool_matcher_ret > (int)n_result) { - bool_matcher_ret = n_result; - } - - for (int index = 0; index < bool_matcher_ret; index++) { - results[index].rule_id = bool_matcher_results[index].expr_id; - results[index].user_tag = bool_matcher_results[index].user_tag; - } - *n_hit_result = bool_matcher_ret; - -next: - utarray_clear(matched_pat->pattern_ids); - - return ret; + return adapter_rs_scan_match(rs_rt->bm, matched_pat->pattern_ids, + rs_rt->bool_match_buffs[thread_id], + MAX_HIT_EXPR_NUM, results, n_result, + n_hit_result); } \ No newline at end of file