[Forensics-changes] [yara] 81/135: Implement yr_compiler_set_callback

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:27:34 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.1.0
in repository yara.

commit 425dadec8275712e7897bd15e14251e7c1b60221
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Fri Aug 8 11:05:31 2014 +0200

    Implement yr_compiler_set_callback
---
 libyara/compiler.c              | 10 ++++-
 libyara/include/yara/compiler.h | 84 ++++++++++++++++++++++-------------------
 libyara/lexer.c                 | 10 ++---
 libyara/lexer.l                 | 10 ++---
 libyara/parser.c                |  2 +-
 yara-python/yara-python.c       |  8 +++-
 yara.c                          |  3 +-
 yarac.c                         |  2 +-
 8 files changed, 74 insertions(+), 55 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index 3aa9614..5e6b6b6 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -39,7 +39,7 @@ int yr_compiler_create(
     return ERROR_INSUFICIENT_MEMORY;
 
   new_compiler->errors = 0;
-  new_compiler->error_report_function = NULL;
+  new_compiler->callback = NULL;
   new_compiler->last_error = ERROR_SUCCESS;
   new_compiler->last_error_line = 0;
   new_compiler->error_line = 0;
@@ -154,6 +154,14 @@ void yr_compiler_destroy(
 }
 
 
+void yr_compiler_set_callback(
+    YR_COMPILER* compiler,
+    YR_COMPILER_CALLBACK callback)
+{
+  compiler->callback = callback;
+}
+
+
 int _yr_compiler_push_file(
     YR_COMPILER* compiler,
     FILE* fh)
diff --git a/libyara/include/yara/compiler.h b/libyara/include/yara/compiler.h
index 51313c3..b647c56 100644
--- a/libyara/include/yara/compiler.h
+++ b/libyara/include/yara/compiler.h
@@ -29,7 +29,7 @@ limitations under the License.
 #define YARA_ERROR_LEVEL_WARNING 1
 
 
-typedef void (*YR_REPORT_FUNC)(
+typedef void (*YR_COMPILER_CALLBACK)(
     int error_level,
     const char* file_name,
     int line_number,
@@ -38,55 +38,56 @@ typedef void (*YR_REPORT_FUNC)(
 
 typedef struct _YR_COMPILER
 {
-  int                 last_result;
-  YR_REPORT_FUNC      error_report_function;
-  int                 errors;
-  int                 error_line;
-  int                 last_error;
-  int                 last_error_line;
+  int                   errors;
+  int                   error_line;
+  int                   last_error;
+  int                   last_error_line;
+  int                   last_result;
 
-  jmp_buf             error_recovery;
+  jmp_buf               error_recovery;
 
-  YR_ARENA*           sz_arena;
-  YR_ARENA*           rules_arena;
-  YR_ARENA*           strings_arena;
-  YR_ARENA*           code_arena;
-  YR_ARENA*           re_code_arena;
-  YR_ARENA*           automaton_arena;
-  YR_ARENA*           compiled_rules_arena;
-  YR_ARENA*           externals_arena;
-  YR_ARENA*           namespaces_arena;
-  YR_ARENA*           metas_arena;
+  YR_COMPILER_CALLBACK  callback;
 
-  YR_AC_AUTOMATON*    automaton;
-  YR_HASH_TABLE*      rules_table;
-  YR_HASH_TABLE*      objects_table;
-  YR_NAMESPACE*       current_namespace;
-  YR_STRING*          current_rule_strings;
+  YR_ARENA*             sz_arena;
+  YR_ARENA*             rules_arena;
+  YR_ARENA*             strings_arena;
+  YR_ARENA*             code_arena;
+  YR_ARENA*             re_code_arena;
+  YR_ARENA*             automaton_arena;
+  YR_ARENA*             compiled_rules_arena;
+  YR_ARENA*             externals_arena;
+  YR_ARENA*             namespaces_arena;
+  YR_ARENA*             metas_arena;
 
-  int                 current_rule_flags;
-  int                 namespaces_count;
+  YR_AC_AUTOMATON*      automaton;
+  YR_HASH_TABLE*        rules_table;
+  YR_HASH_TABLE*        objects_table;
+  YR_NAMESPACE*         current_namespace;
+  YR_STRING*            current_rule_strings;
 
-  int8_t*             loop_address[MAX_LOOP_NESTING];
-  char*               loop_identifier[MAX_LOOP_NESTING];
-  int                 loop_depth;
-  int                 loop_for_of_mem_offset;
+  int                   current_rule_flags;
+  int                   namespaces_count;
 
-  int                 allow_includes;
+  int8_t*               loop_address[MAX_LOOP_NESTING];
+  char*                 loop_identifier[MAX_LOOP_NESTING];
+  int                   loop_depth;
+  int                   loop_for_of_mem_offset;
 
-  char*               file_name_stack[MAX_INCLUDE_DEPTH];
-  int                 file_name_stack_ptr;
+  int                   allow_includes;
 
-  FILE*               file_stack[MAX_INCLUDE_DEPTH];
-  int                 file_stack_ptr;
+  char*                 file_name_stack[MAX_INCLUDE_DEPTH];
+  int                   file_name_stack_ptr;
 
-  char                last_error_extra_info[MAX_COMPILER_ERROR_EXTRA_INFO];
+  FILE*                 file_stack[MAX_INCLUDE_DEPTH];
+  int                   file_stack_ptr;
 
-  char                lex_buf[LEX_BUF_SIZE];
-  char*               lex_buf_ptr;
-  unsigned short      lex_buf_len;
+  char                  last_error_extra_info[MAX_COMPILER_ERROR_EXTRA_INFO];
 
-  char                include_base_dir[MAX_PATH];
+  char                  lex_buf[LEX_BUF_SIZE];
+  char*                 lex_buf_ptr;
+  unsigned short        lex_buf_len;
+
+  char                  include_base_dir[MAX_PATH];
 
 } YR_COMPILER;
 
@@ -117,6 +118,11 @@ void yr_compiler_destroy(
     YR_COMPILER* compiler);
 
 
+void yr_compiler_set_callback(
+    YR_COMPILER* compiler,
+    YR_COMPILER_CALLBACK callback);
+
+
 int yr_compiler_add_file(
     YR_COMPILER* compiler,
     FILE* rules_file,
diff --git a/libyara/lexer.c b/libyara/lexer.c
index 7d3ffc1..0d96481 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -2887,7 +2887,7 @@ void yywarning(
   else
     file_name = NULL;
 
-  compiler->error_report_function(
+  compiler->callback(
       YARA_ERROR_LEVEL_WARNING,
       file_name,
       yara_yyget_lineno(yyscanner),
@@ -2943,9 +2943,9 @@ void yyerror(
     yr_compiler_set_error_extra_info(compiler, error_message);
     compiler->last_error = ERROR_SYNTAX_ERROR;
 
-    if (compiler->error_report_function != NULL)
+    if (compiler->callback != NULL)
     {
-      compiler->error_report_function(
+      compiler->callback(
           YARA_ERROR_LEVEL_ERROR,
           file_name,
           compiler->last_error_line,
@@ -2956,11 +2956,11 @@ void yyerror(
   {
     compiler->last_error = compiler->last_result;
 
-    if (compiler->error_report_function != NULL)
+    if (compiler->callback != NULL)
     {
       yr_compiler_get_error_message(compiler, message, sizeof(message));
 
-      compiler->error_report_function(
+      compiler->callback(
         YARA_ERROR_LEVEL_ERROR,
         file_name,
         compiler->last_error_line,
diff --git a/libyara/lexer.l b/libyara/lexer.l
index a25e17d..ca77ec3 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -568,7 +568,7 @@ void yywarning(
   else
     file_name = NULL;
 
-  compiler->error_report_function(
+  compiler->callback(
       YARA_ERROR_LEVEL_WARNING,
       file_name,
       yyget_lineno(yyscanner),
@@ -624,9 +624,9 @@ void yyerror(
     yr_compiler_set_error_extra_info(compiler, error_message);
     compiler->last_error = ERROR_SYNTAX_ERROR;
 
-    if (compiler->error_report_function != NULL)
+    if (compiler->callback != NULL)
     {
-      compiler->error_report_function(
+      compiler->callback(
           YARA_ERROR_LEVEL_ERROR,
           file_name,
           compiler->last_error_line,
@@ -637,11 +637,11 @@ void yyerror(
   {
     compiler->last_error = compiler->last_result;
 
-    if (compiler->error_report_function != NULL)
+    if (compiler->callback != NULL)
     {
       yr_compiler_get_error_message(compiler, message, sizeof(message));
 
-      compiler->error_report_function(
+      compiler->callback(
         YARA_ERROR_LEVEL_ERROR,
         file_name,
         compiler->last_error_line,
diff --git a/libyara/parser.c b/libyara/parser.c
index dac670b..0d29928 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -586,7 +586,7 @@ YR_STRING* yr_parser_reduce_string_declaration(
       goto _exit;
   }
 
-  if (min_atom_length < 2 && compiler->error_report_function != NULL)
+  if (min_atom_length < 2 && compiler->callback != NULL)
   {
     snprintf(
         message,
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 8acf025..2a94433 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -1180,14 +1180,18 @@ static PyObject * yara_compile(
     if (error != ERROR_SUCCESS)
       return handle_error(error, NULL);
 
-    compiler->error_report_function = raise_exception_on_error;
+    yr_compiler_set_callback(compiler, raise_exception_on_error);
 
     if (error_on_warning != NULL)
     {
       if (PyBool_Check(error_on_warning))
       {
         if (PyObject_IsTrue(error_on_warning) == 1)
-          compiler->error_report_function = raise_exception_on_error_or_warning;
+        {
+          yr_compiler_set_callback(
+              compiler,
+              raise_exception_on_error_or_warning);
+        }
       }
       else
       {
diff --git a/yara.c b/yara.c
index b1fc760..fedb327 100644
--- a/yara.c
+++ b/yara.c
@@ -1057,7 +1057,8 @@ int main(
       external = external->next;
     }
 
-    compiler->error_report_function = print_compiler_error;
+    yr_compiler_set_callback(compiler, print_compiler_error);
+
     rule_file = fopen(argv[optind], "r");
 
     if (rule_file == NULL)
diff --git a/yarac.c b/yarac.c
index ed295d7..8b1f8de 100644
--- a/yarac.c
+++ b/yarac.c
@@ -194,7 +194,7 @@ int main(
     return EXIT_FAILURE;
   }
 
-  compiler->error_report_function = report_error;
+  yr_compiler_set_callback(compiler, report_error);
 
   for (i = optind; i < argc - 1; i++)
   {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list