Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Iterators (VirusTotal#1141)
Browse files Browse the repository at this point in the history
This introduces a change in the way in which "for-in" loops work by introducing the concept of iterators. These kind of loops now accept an iterator that will return a sequence of values. Besides the integer ranges (N..M) and integer enumerations (X,Y,Z), this kind of loops now accepts other kinds of iterables, like arrays. This allows conditions like the following one:

for any section in pe.sections: ( section.name == "foo" )

Until now the same expression was written as:

for any i in (0..pe.number_of_sections - 1): ( pe.sections[i].name == "foo" )

The new syntax is more legible and opens door for more powerful features in the future. Backward compatibility is maintained.
  • Loading branch information
plusvic authored Nov 12, 2019
1 parent 8948b35 commit f9e8f62
Show file tree
Hide file tree
Showing 19 changed files with 1,818 additions and 1,172 deletions.
17 changes: 15 additions & 2 deletions libyara/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ YR_API int yr_compiler_create(
new_compiler->file_name_stack_ptr = 0;
new_compiler->fixup_stack_head = NULL;
new_compiler->loop_depth = 0;
new_compiler->loop_for_of_mem_offset = -1;
new_compiler->loop_for_of_var_index = -1;
new_compiler->compiled_rules_arena = NULL;
new_compiler->namespaces_count = 0;
new_compiler->current_rule = NULL;
Expand Down Expand Up @@ -473,6 +473,19 @@ void _yr_compiler_pop_file_name(
}


int _yr_compiler_get_var_frame(
YR_COMPILER* compiler)
{
int i, result = 0;

for (i = 0; i < compiler->loop_depth; i++)
result += compiler->loop[i].vars_count +
compiler->loop[i].vars_internal_count;

return result;
}


YR_API char* yr_compiler_get_current_file_name(
YR_COMPILER* compiler)
{
Expand Down Expand Up @@ -869,7 +882,7 @@ YR_API int yr_compiler_get_rules(
return ERROR_SUCCESS;
}

int _yr_compiler_define_variable(
static int _yr_compiler_define_variable(
YR_COMPILER* compiler,
YR_EXTERNAL_VARIABLE* external)
{
Expand Down
Loading

0 comments on commit f9e8f62

Please sign in to comment.