From 55ee00dc63ff14f615f87ce9f318b55f34ce2463 Mon Sep 17 00:00:00 2001 From: Victor Manuel Alvarez Date: Thu, 18 Sep 2014 20:33:43 +0200 Subject: [PATCH] Implement yr_rules_foreach and use it where appropriate --- libyara/include/yara/rules.h | 4 ++++ libyara/rules.c | 32 ++++++-------------------------- yara-python/yara-python.c | 10 ++-------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/libyara/include/yara/rules.h b/libyara/include/yara/rules.h index 9114827f78..43cf163147 100644 --- a/libyara/include/yara/rules.h +++ b/libyara/include/yara/rules.h @@ -49,6 +49,10 @@ limitations under the License. for (match = STRING_MATCHES(string).head; match != NULL; match = match->next) +#define yr_rules_foreach(rules, rule) \ + for (rule = rules->rules_list_head; !RULE_IS_NULL(rule); rule++) + + int yr_rules_scan_mem( YR_RULES* rules, diff --git a/libyara/rules.c b/libyara/rules.c index 9d77a60acc..bea56cb9ca 100644 --- a/libyara/rules.c +++ b/libyara/rules.c @@ -151,15 +151,12 @@ void _yr_rules_clean_matches( int tidx = yr_get_tidx(); - rule = rules->rules_list_head; - - while (!RULE_IS_NULL(rule)) + yr_rules_foreach(rules, rule) { rule->t_flags[tidx] &= ~RULE_TFLAGS_MATCH; rule->ns->t_flags[tidx] &= ~NAMESPACE_TFLAGS_UNSATISFIED_GLOBAL; - string = rule->strings; - while (!STRING_IS_NULL(string)) + yr_rule_strings_foreach(rule, string) { string->matches[tidx].count = 0; string->matches[tidx].head = NULL; @@ -167,10 +164,7 @@ void _yr_rules_clean_matches( string->unconfirmed_matches[tidx].count = 0; string->unconfirmed_matches[tidx].head = NULL; string->unconfirmed_matches[tidx].tail = NULL; - string++; } - - rule++; } } @@ -186,17 +180,13 @@ void yr_rules_print_profiling_info( printf("===== PROFILING INFORMATION =====\n"); - rule = rules->rules_list_head; - - while (!RULE_IS_NULL(rule)) + yr_rules_foreach(rules, rule) { clock_ticks = rule->clock_ticks; - string = rule->strings; - while (!STRING_IS_NULL(string)) + yr_rule_strings_foreach(rule, string) { clock_ticks += string->clock_ticks; - string++; } printf( @@ -204,8 +194,6 @@ void yr_rules_print_profiling_info( rule->ns->name, rule->identifier, clock_ticks); - - rule++; } printf("================================\n"); @@ -419,21 +407,15 @@ int yr_rules_scan_mem_blocks( if (result != ERROR_SUCCESS) goto _exit; - rule = rules->rules_list_head; - - while (!RULE_IS_NULL(rule)) + yr_rules_foreach(rules, rule) { if (RULE_IS_GLOBAL(rule) && !(rule->t_flags[tidx] & RULE_TFLAGS_MATCH)) { rule->ns->t_flags[tidx] |= NAMESPACE_TFLAGS_UNSATISFIED_GLOBAL; } - - rule++; } - rule = rules->rules_list_head; - - while (!RULE_IS_NULL(rule)) + yr_rules_foreach(rules, rule) { if (rule->t_flags[tidx] & RULE_TFLAGS_MATCH && !(rule->ns->t_flags[tidx] & NAMESPACE_TFLAGS_UNSATISFIED_GLOBAL)) @@ -458,8 +440,6 @@ int yr_rules_scan_mem_blocks( goto _exit; } } - - rule++; } callback(CALLBACK_MSG_SCAN_FINISHED, NULL, user_data); diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c index 87a64f0512..e62cfc01fb 100644 --- a/yara-python/yara-python.c +++ b/yara-python/yara-python.c @@ -1162,17 +1162,13 @@ static PyObject * Rules_profiling_info( result = PyDict_New(); - rule = rules->rules_list_head; - - while (!RULE_IS_NULL(rule)) + yr_rules_foreach(rules, rule) { clock_ticks = rule->clock_ticks; - string = rule->strings; - while (!STRING_IS_NULL(string)) + yr_rule_strings_foreach(rule, string) { clock_ticks += string->clock_ticks; - string++; } snprintf(key, sizeof(key), "%s:%s", rule->ns->name, rule->identifier); @@ -1180,8 +1176,6 @@ static PyObject * Rules_profiling_info( object = PyLong_FromLongLong(clock_ticks); PyDict_SetItemString(result, key, object); Py_DECREF(object); - - rule++; } return result;