[Forensics-changes] [yara] 359/407: Allow to pass custom pointer to compiler callback as suggested in #220

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:28:45 UTC 2017


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

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

commit b90e8b5283b27e1158cf2f59faf15edc4a0d107b
Author: Victor Manuel Alvarez <vmalvarez at virustotal.com>
Date:   Thu Jan 29 22:33:18 2015 +0100

    Allow to pass custom pointer to compiler callback as suggested in #220
---
 docs/capi.rst                   | 15 +++++++++++----
 libyara/compiler.c              |  4 +++-
 libyara/include/yara/compiler.h |  7 +++++--
 libyara/lexer.c                 | 21 ++++++++++++++-------
 libyara/lexer.l                 |  9 ++++++---
 yara-python/yara-python.c       | 11 +++++++----
 yara.c                          |  5 +++--
 yarac.c                         |  5 +++--
 8 files changed, 52 insertions(+), 25 deletions(-)

diff --git a/docs/capi.rst b/docs/capi.rst
index 5549609..f64a67a 100644
--- a/docs/capi.rst
+++ b/docs/capi.rst
@@ -49,14 +49,18 @@ callback function has the following prototype:
       int error_level,
       const char* file_name,
       int line_number,
-      const char* message)
+      const char* message,
+      void* user_data)
+
+.. versionchanged:: 3.4.0
 
 Possible values for ``error_level`` are ``YARA_ERROR_LEVEL_ERROR`` and
 ``YARA_ERROR_LEVEL_WARNING``. The arguments ``file_name`` and ``line_number``
 contains the file name and line number where the error or warning occurs.
 ``file_name`` is the one passed to :c:func:`yr_compiler_add_file`. It can
 be ``NULL`` if you passed ``NULL`` or if you're using
-:c:func:`yr_compiler_add_string`.
+:c:func:`yr_compiler_add_string`. The ``user_data`` pointer is the same you
+passed to :c:func:`yr_compiler_set_callback`.
 
 After you successfully added some sources you can get the compiled rules
 using the :c:func:`yr_compiler_get_rules()` function. You'll get a pointer to
@@ -289,9 +293,12 @@ Functions
 
   Destroy a YARA compiler.
 
-.. c:function:: void yr_compiler_set_callback(YR_COMPILER* compiler, YR_COMPILER_CALLBACK_FUNC callback)
+.. c:function:: void yr_compiler_set_callback(YR_COMPILER* compiler, YR_COMPILER_CALLBACK_FUNC callback, void* user_data)
+
+  Set a callback for receiving error and warning information. The *user_data*
+  pointer is passed to the callback function.
 
-  Set a callback for receiving error and warning information.
+.. versionchanged:: 3.4.0
 
 .. c:function:: int yr_compiler_add_file(YR_COMPILER* compiler, FILE* file, const char* namespace, const char* file_name)
 
diff --git a/libyara/compiler.c b/libyara/compiler.c
index 27616a6..5cbe92b 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -158,9 +158,11 @@ YR_API void yr_compiler_destroy(
 
 YR_API void yr_compiler_set_callback(
     YR_COMPILER* compiler,
-    YR_COMPILER_CALLBACK_FUNC callback)
+    YR_COMPILER_CALLBACK_FUNC callback,
+    void* user_data)
 {
   compiler->callback = callback;
+  compiler->user_data = user_data;
 }
 
 
diff --git a/libyara/include/yara/compiler.h b/libyara/include/yara/compiler.h
index 8694641..c413056 100644
--- a/libyara/include/yara/compiler.h
+++ b/libyara/include/yara/compiler.h
@@ -34,7 +34,8 @@ typedef void (*YR_COMPILER_CALLBACK_FUNC)(
     int error_level,
     const char* file_name,
     int line_number,
-    const char* message);
+    const char* message,
+    void* user_data);
 
 
 typedef struct _YR_COMPILER
@@ -87,6 +88,7 @@ typedef struct _YR_COMPILER
   unsigned short    lex_buf_len;
 
   char              include_base_dir[MAX_PATH];
+  void*             user_data;
 
   YR_COMPILER_CALLBACK_FUNC  callback;
 
@@ -135,7 +137,8 @@ YR_API void yr_compiler_destroy(
 
 YR_API void yr_compiler_set_callback(
     YR_COMPILER* compiler,
-    YR_COMPILER_CALLBACK_FUNC callback);
+    YR_COMPILER_CALLBACK_FUNC callback,
+    void* user_data);
 
 
 YR_API int yr_compiler_add_file(
diff --git a/libyara/lexer.c b/libyara/lexer.c
index f2afba5..a25e7f6 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -47,6 +47,7 @@ typedef int16_t flex_int16_t;
 typedef uint16_t flex_uint16_t;
 typedef int32_t flex_int32_t;
 typedef uint32_t flex_uint32_t;
+typedef uint64_t flex_uint64_t;
 #else
 typedef signed char flex_int8_t;
 typedef short int flex_int16_t;
@@ -357,7 +358,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
  */
 #define YY_DO_BEFORE_ACTION \
 	yyg->yytext_ptr = yy_bp; \
-	yyleng = (size_t) (yy_cp - yy_bp); \
+	yyleng = (yy_size_t) (yy_cp - yy_bp); \
 	yyg->yy_hold_char = *yy_cp; \
 	*yy_cp = '\0'; \
 	yyg->yy_c_buf_p = yy_cp;
@@ -685,7 +686,7 @@ limitations under the License.
 
 
 
-#line 689 "lexer.c"
+#line 690 "lexer.c"
 
 #define INITIAL 0
 #define str 1
@@ -923,7 +924,7 @@ YY_DECL
 #line 83 "lexer.l"
 
 
-#line 927 "lexer.c"
+#line 928 "lexer.c"
 
     yylval = yylval_param;
 
@@ -1755,7 +1756,7 @@ YY_RULE_SETUP
 #line 591 "lexer.l"
 ECHO;
 	YY_BREAK
-#line 1759 "lexer.c"
+#line 1760 "lexer.c"
 
 	case YY_END_OF_BUFFER:
 		{
@@ -2897,6 +2898,9 @@ void yywarning(
   YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
   char* file_name;
 
+  if (compiler->callback == NULL)
+    return;
+
   if (compiler->file_name_stack_ptr > 0)
     file_name = compiler->file_name_stack[compiler->file_name_stack_ptr - 1];
   else
@@ -2906,7 +2910,8 @@ void yywarning(
       YARA_ERROR_LEVEL_WARNING,
       file_name,
       yara_yyget_lineno(yyscanner),
-      warning_message);
+      warning_message,
+      compiler->user_data);
 }
 
 
@@ -2964,7 +2969,8 @@ void yyerror(
           YARA_ERROR_LEVEL_ERROR,
           file_name,
           compiler->last_error_line,
-          error_message);
+          error_message,
+          compiler->user_data);
     }
   }
   else
@@ -2979,7 +2985,8 @@ void yyerror(
         YARA_ERROR_LEVEL_ERROR,
         file_name,
         compiler->last_error_line,
-        message);
+        message,
+        compiler->user_data);
     }
   }
 
diff --git a/libyara/lexer.l b/libyara/lexer.l
index 89aec7d..83ccd39 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -610,7 +610,8 @@ void yywarning(
       YARA_ERROR_LEVEL_WARNING,
       file_name,
       yyget_lineno(yyscanner),
-      warning_message);
+      warning_message,
+      compiler->user_data);
 }
 
 
@@ -668,7 +669,8 @@ void yyerror(
           YARA_ERROR_LEVEL_ERROR,
           file_name,
           compiler->last_error_line,
-          error_message);
+          error_message,
+          compiler->user_data);
     }
   }
   else
@@ -683,7 +685,8 @@ void yyerror(
         YARA_ERROR_LEVEL_ERROR,
         file_name,
         compiler->last_error_line,
-        message);
+        message,
+        compiler->user_data);
     }
   }
 
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 007f3f0..6368604 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -1200,7 +1200,8 @@ void raise_exception_on_error(
     int error_level,
     const char* file_name,
     int line_number,
-    const char* message)
+    const char* message,
+    void* user_data)
 {
   if (error_level == YARA_ERROR_LEVEL_ERROR)
   {
@@ -1224,7 +1225,8 @@ void raise_exception_on_error_or_warning(
     int error_level,
     const char* file_name,
     int line_number,
-    const char* message)
+    const char* message,
+    void* user_data)
 {
   if (error_level == YARA_ERROR_LEVEL_ERROR)
   {
@@ -1313,7 +1315,7 @@ static PyObject * yara_compile(
     if (error != ERROR_SUCCESS)
       return handle_error(error, NULL);
 
-    yr_compiler_set_callback(compiler, raise_exception_on_error);
+    yr_compiler_set_callback(compiler, raise_exception_on_error, NULL);
 
     if (error_on_warning != NULL)
     {
@@ -1323,7 +1325,8 @@ static PyObject * yara_compile(
         {
           yr_compiler_set_callback(
               compiler,
-              raise_exception_on_error_or_warning);
+              raise_exception_on_error_or_warning,
+              NULL);
         }
       }
       else
diff --git a/yara.c b/yara.c
index 6302522..75541a9 100644
--- a/yara.c
+++ b/yara.c
@@ -446,7 +446,8 @@ void print_compiler_error(
     int error_level,
     const char* file_name,
     int line_number,
-    const char* message)
+    const char* message,
+    void* user_data)
 {
   if (error_level == YARA_ERROR_LEVEL_ERROR)
   {
@@ -905,7 +906,7 @@ int main(
     if (!define_external_variables(NULL, compiler))
       exit_with_code(EXIT_FAILURE);
 
-    yr_compiler_set_callback(compiler, print_compiler_error);
+    yr_compiler_set_callback(compiler, print_compiler_error, NULL);
 
     FILE* rule_file = fopen(argv[0], "r");
 
diff --git a/yarac.c b/yarac.c
index ab92b22..7ab016e 100644
--- a/yarac.c
+++ b/yarac.c
@@ -88,7 +88,8 @@ void report_error(
     int error_level,
     const char* file_name,
     int line_number,
-    const char* message)
+    const char* message,
+    void* user_data)
 {
   if (error_level == YARA_ERROR_LEVEL_ERROR)
   {
@@ -203,7 +204,7 @@ int main(
   if (!define_external_variables(compiler))
     exit_with_code(EXIT_FAILURE);
 
-  yr_compiler_set_callback(compiler, report_error);
+  yr_compiler_set_callback(compiler, report_error, NULL);
 
   for (int i = 0; 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