Skip to content

Commit

Permalink
Implement yr_rules_foreach and use it where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Sep 18, 2014
1 parent 6a1c56a commit 55ee00d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
4 changes: 4 additions & 0 deletions libyara/include/yara/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 6 additions & 26 deletions libyara/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,20 @@ 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;
string->matches[tidx].tail = NULL;
string->unconfirmed_matches[tidx].count = 0;
string->unconfirmed_matches[tidx].head = NULL;
string->unconfirmed_matches[tidx].tail = NULL;
string++;
}

rule++;
}
}

Expand All @@ -186,26 +180,20 @@ 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(
"%s:%s: %li\n",
rule->ns->name,
rule->identifier,
clock_ticks);

rule++;
}

printf("================================\n");
Expand Down Expand Up @@ -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))
Expand All @@ -458,8 +440,6 @@ int yr_rules_scan_mem_blocks(
goto _exit;
}
}

rule++;
}

callback(CALLBACK_MSG_SCAN_FINISHED, NULL, user_data);
Expand Down
10 changes: 2 additions & 8 deletions yara-python/yara-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,26 +1162,20 @@ 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);

object = PyLong_FromLongLong(clock_ticks);
PyDict_SetItemString(result, key, object);
Py_DECREF(object);

rule++;
}

return result;
Expand Down

0 comments on commit 55ee00d

Please sign in to comment.