[Forensics-changes] [yara] 175/415: Fix issues while compiling for Windows

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:02 UTC 2014


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

bengen pushed a commit to branch debian
in repository yara.

commit 8e0d372df1a04ea6ea15f71201c78d8e4f524f68
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Mon Jun 24 09:33:01 2013 +0000

    Fix issues while compiling for Windows
---
 libyara/ahocorasick.c           | 25 ++++++++++---------
 libyara/arena.c                 | 36 +++++++++++++--------------
 libyara/exec.c                  |  2 +-
 libyara/grammar.c               |  6 +++--
 libyara/grammar.h               |  2 +-
 libyara/grammar.y               |  2 +-
 libyara/regex/regex.h           | 55 +++++++++++++++++++++++++++++++++++++++++
 libyara/rules.c                 |  3 +++
 libyara/yara.h                  | 14 ++++++++---
 windows/libyara/libyara.vcxproj | 22 +++++++++--------
 windows/yara/yara.vcxproj       |  6 ++---
 yara-python/setupwin64.py       |  2 +-
 yara.c                          | 21 ++++++++++++----
 13 files changed, 139 insertions(+), 57 deletions(-)

diff --git a/libyara/ahocorasick.c b/libyara/ahocorasick.c
index 054c01b..9a818d7 100644
--- a/libyara/ahocorasick.c
+++ b/libyara/ahocorasick.c
@@ -27,8 +27,9 @@ limitations under the License.
 #define MAX_TOKEN 4
 
 
-#define min(x, y) (x < y)?(x):(y)
-
+#ifndef min
+#define min(x, y) ((x < y) ? (x) : (y))
+#endif
 
 typedef struct _QUEUE_NODE
 {
@@ -233,12 +234,12 @@ AC_STATE* _yr_ac_create_state(
 // returned tokens.
 //
 
-void* _yr_ac_gen_case_combinations(
+uint8_t* _yr_ac_gen_case_combinations(
     uint8_t* token,
     int token_length,
     int token_offset,
     int token_backtrack,
-    void* output_buffer)
+    uint8_t* output_buffer)
 {
   char c;
   char* new_token;
@@ -302,10 +303,10 @@ void* _yr_ac_gen_case_combinations(
 // which is shorter, or 00 00 00 00 which is more homogeneous.
 //
 
-void* _yr_ac_gen_hex_tokens(
+uint8_t* _yr_ac_gen_hex_tokens(
     STRING* string,
     int max_token_length,
-    void* output_buffer)
+    uint8_t* output_buffer)
 {
   int inside_or = 0;
   int token_length = 0;
@@ -453,10 +454,10 @@ void* _yr_ac_gen_hex_tokens(
 // Generates tokens for a regular expression.
 //
 
-void* _yr_ac_gen_regexp_tokens(
+uint8_t* _yr_ac_gen_regexp_tokens(
     STRING* string,
     int max_token_length,
-    void* output_buffer)
+    uint8_t* output_buffer)
 {
   uint8_t token[MAX_TOKEN];
   uint8_t first_bytes[256];
@@ -557,7 +558,7 @@ void* _yr_ac_gen_regexp_tokens(
 void _yr_ac_gen_tokens(
     STRING* string,
     int max_token_length,
-    void* output_buffer)
+    uint8_t* output_buffer)
 {
   int i, j;
   int token_length;
@@ -595,7 +596,7 @@ void _yr_ac_gen_tokens(
       str = output_buffer;
 
       memcpy(output_buffer, string->string, token_length);
-      output_buffer += token_length;
+      ((uint8_t*) output_buffer) += token_length;
 
       if (STRING_IS_NO_CASE(string))
       {
@@ -626,9 +627,9 @@ void _yr_ac_gen_tokens(
       while(i < token_length)
       {
         if (i % 2 == 0)
-          *((uint8_t*) output_buffer++) = string->string[j++];
+          *(((uint8_t*) output_buffer)++) = string->string[j++];
         else
-          *((uint8_t*) output_buffer++) = 0;
+          *(((uint8_t*) output_buffer)++) = 0;
         i++;
       }
 
diff --git a/libyara/arena.c b/libyara/arena.c
index a22598f..313708d 100644
--- a/libyara/arena.c
+++ b/libyara/arena.c
@@ -156,7 +156,7 @@ int _yr_arena_make_relocatable(
 
   assert(page != NULL);
 
-  base_offset = base - page->address;
+  base_offset = (uint8_t*) base - page->address;
   offset = va_arg(offsets, size_t);
 
   while (offset != -1)
@@ -338,10 +338,10 @@ void* yr_arena_next_address(
 
   assert(page != NULL);
 
-  if (address + increment >= page->address + page->used)
+  if ((uint8_t*) address + increment >= page->address + page->used)
     return page->next ? page->next->address : NULL;
   else
-    return address + increment;
+    return (uint8_t*) address + increment;
 }
 
 
@@ -366,8 +366,8 @@ int yr_arena_coalesce(
   ARENA_PAGE* next_page;
   RELOC* reloc;
 
-  void** reloc_address;
-  void* reloc_target;
+  uint8_t** reloc_address;
+  uint8_t* reloc_target;
   int total_size = 0;
 
   page = arena->page_list_head;
@@ -418,7 +418,7 @@ int yr_arena_coalesce(
 
   while (reloc != NULL)
   {
-    reloc_address = big_page->address + reloc->offset;
+	reloc_address = (uint8_t**) (big_page->address + reloc->offset);
     reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
@@ -734,8 +734,8 @@ int yr_arena_duplicate(
   ARENA_PAGE* page;
   ARENA_PAGE* new_page;
   ARENA* new_arena;
-  void** reloc_address;
-  void* reloc_target;
+  uint8_t** reloc_address;
+  uint8_t* reloc_target;
 
   // Only coalesced arenas can be duplicated.
   assert(arena->is_coalesced);
@@ -778,7 +778,7 @@ int yr_arena_duplicate(
 
     new_page->reloc_list_tail = new_reloc;
 
-    reloc_address = new_page->address + new_reloc->offset;
+    reloc_address = (uint8_t**) (new_page->address + new_reloc->offset);
     reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
@@ -828,8 +828,8 @@ int yr_arena_save(
   ARENA_FILE_HEADER header;
 
   int32_t end_marker = -1;
-  void** reloc_address;
-  void* reloc_target;
+  uint8_t** reloc_address;
+  uint8_t* reloc_target;
 
   // Only coalesced arenas can be saved.
   assert(arena->is_coalesced);
@@ -845,7 +845,7 @@ int yr_arena_save(
   // Convert pointers to offsets before saving.
   while (reloc != NULL)
   {
-    reloc_address = page->address + reloc->offset;
+    reloc_address = (uint8_t**) (page->address + reloc->offset);
     reloc_target = *reloc_address;
 
     if (reloc_target != NULL)
@@ -878,7 +878,7 @@ int yr_arena_save(
   {
     fwrite(&reloc->offset, sizeof(reloc->offset), 1, fh);
 
-    reloc_address = page->address + reloc->offset;
+    reloc_address = (uint8_t**) (page->address + reloc->offset);
     reloc_target = *reloc_address;
 
     if (reloc_target != (void*) (size_t) 0xFFFABADA)
@@ -922,8 +922,8 @@ int yr_arena_load(
   void* new_address;
   int result;
   int32_t reloc_offset;
-  void** reloc_address;
-  void* reloc_target;
+  uint8_t** reloc_address;
+  uint8_t* reloc_target;
   long file_size;
 
   fh = fopen(filename, "r");
@@ -991,10 +991,10 @@ int yr_arena_load(
   {
     yr_arena_make_relocatable(new_arena, page->address, reloc_offset, EOL);
 
-    reloc_address = page->address + reloc_offset;
-    reloc_target = (void*) *reloc_address;
+    reloc_address = (uint8_t**) (page->address + reloc_offset);
+    reloc_target = *reloc_address;
 
-    if (reloc_target != (void*) (size_t) 0xFFFABADA)
+    if (reloc_target != (uint8_t*) (size_t) 0xFFFABADA)
       *reloc_address += (size_t) page->address;
     else
       *reloc_address = 0;
diff --git a/libyara/exec.c b/libyara/exec.c
index d3f8fcd..7e66d12 100644
--- a/libyara/exec.c
+++ b/libyara/exec.c
@@ -24,7 +24,7 @@ limitations under the License.
 
 #define push(x)  \
     if (sp < STACK_SIZE) stack[sp++] = (x); \
-    else return ERROR_STACK_OVERFLOW
+    else return ERROR_EXEC_STACK_OVERFLOW
 
 
 #define pop(x)  x = stack[--sp]
diff --git a/libyara/grammar.c b/libyara/grammar.c
index 7f66f1e..4c6aeaa 100644
--- a/libyara/grammar.c
+++ b/libyara/grammar.c
@@ -200,6 +200,8 @@
 
 
 #include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
 #include <string.h>
 #include <limits.h>
 #include <stddef.h>
@@ -209,7 +211,7 @@
 #include "sizedstr.h"
 #include "mem.h"
 #include "lex.h"
-#include "regex.h"
+#include "regex/regex.h"
 #include "parser.h"
 #include "utils.h"
 #include "yara.h"
@@ -253,7 +255,7 @@ typedef union YYSTYPE
 {
   void*           sized_string;
   char*           c_string;
-  int64_t         integer;
+  __int64         integer;
   void*           string;
   void*           meta;
 }
diff --git a/libyara/grammar.h b/libyara/grammar.h
index da81090..e30d667 100644
--- a/libyara/grammar.h
+++ b/libyara/grammar.h
@@ -174,7 +174,7 @@ typedef union YYSTYPE
 {
   void*           sized_string;
   char*           c_string;
-  int64_t         integer;
+  __int64         integer;
   void*           string;
   void*           meta;
 }
diff --git a/libyara/grammar.y b/libyara/grammar.y
index d40078b..a82f17a 100644
--- a/libyara/grammar.y
+++ b/libyara/grammar.y
@@ -26,7 +26,7 @@ limitations under the License.
 #include "sizedstr.h"
 #include "mem.h"
 #include "lex.h"
-#include "regex.h"
+#include "regex/regex.h"
 #include "parser.h"
 #include "utils.h"
 #include "yara.h"
diff --git a/libyara/regex/regex.h b/libyara/regex/regex.h
new file mode 100644
index 0000000..38c6f94
--- /dev/null
+++ b/libyara/regex/regex.h
@@ -0,0 +1,55 @@
+/*
+Copyright(c) 2011, Google, Inc. [mjwiacek at google.com].
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#ifndef _REGEX_H
+#define _REGEX_H
+
+#include "../yara.h"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+void yr_regex_free(REGEXP* regex);
+
+
+int yr_regex_exec(
+    REGEXP* regex,
+    int anchored,
+    const char *buffer,
+    size_t buffer_size);
+
+
+int yr_regex_compile(
+    REGEXP* output,
+    const char* pattern,
+    int case_insensitive,
+    char* error_message,
+    size_t error_message_size,
+    int* error_offset);
+
+
+int yr_regex_get_first_bytes(
+    REGEXP* regex,
+    uint8_t* table);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libyara/rules.c b/libyara/rules.c
index b904cb5..6d25409 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -24,6 +24,9 @@ limitations under the License.
 #include "utils.h"
 #include "yara.h"
 
+#ifdef WIN32
+#define inline __inline
+#endif
 
 int _yr_scan_compare(
     uint8_t* str1,
diff --git a/libyara/yara.h b/libyara/yara.h
index 348e424..2acac0e 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -20,6 +20,14 @@ limitations under the License.
 #include <stdio.h>
 #include <stdint.h>
 
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#ifdef _MSC_VER
+#define snprintf _snprintf
+#endif
+
 #ifndef TRUE
 #define TRUE 1
 #endif
@@ -69,7 +77,7 @@ limitations under the License.
 #define ERROR_VECTOR_TOO_LONG                   31
 #define ERROR_INCLUDE_DEPTH_EXCEEDED            32
 #define ERROR_INVALID_OR_CORRUPT_FILE           33
-#define ERROR_STACK_OVERFLOW                    34
+#define ERROR_EXEC_STACK_OVERFLOW               34
 
 #define MAX_INCLUDE_DEPTH 16
 #define LEX_BUF_SIZE  1024
@@ -190,8 +198,8 @@ typedef struct _RELOC
 typedef struct _ARENA_PAGE
 {
 
-  void* new_address;
-  void* address;
+  uint8_t* new_address;
+  uint8_t* address;
 
   int32_t size;
   int32_t used;
diff --git a/windows/libyara/libyara.vcxproj b/windows/libyara/libyara.vcxproj
index 35abb80..52ca44a 100644
--- a/windows/libyara/libyara.vcxproj
+++ b/windows/libyara/libyara.vcxproj
@@ -69,8 +69,8 @@
   </PropertyGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <ClCompile>
-      <PreprocessorDefinitions>WIN32;PCRE_STATIC</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>..\..\windows\include;..\libyara;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <PreprocessorDefinitions>WIN32;PCRE_STATIC;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>..\libyara\regex;..\libyara;..\..\windows\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
     </ClCompile>
     <Lib>
@@ -98,8 +98,8 @@
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <ClCompile>
-      <PreprocessorDefinitions>WIN32;PCRE_STATIC</PreprocessorDefinitions>
-      <AdditionalIncludeDirectories>..\..\windows\include;..\libyara;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <PreprocessorDefinitions>WIN32;PCRE_STATIC;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>..\libyara\regex;..\libyara;..\..\windows\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
     </ClCompile>
     <Lib>
@@ -119,20 +119,22 @@
     </Lib>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="..\..\libyara\ast.c" />
-    <ClCompile Include="..\..\libyara\eval.c" />
-    <ClCompile Include="..\..\libyara\exe.c" />
+    <ClCompile Include="..\..\libyara\ahocorasick.c" />
+    <ClCompile Include="..\..\libyara\arena.c" />
+    <ClCompile Include="..\..\libyara\compiler.c" />
+    <ClCompile Include="..\..\libyara\exec.c" />
+    <ClCompile Include="..\..\libyara\exefiles.c" />
     <ClCompile Include="..\..\libyara\filemap.c" />
     <ClCompile Include="..\..\libyara\grammar.c" />
     <ClCompile Include="..\..\libyara\hash.c" />
     <ClCompile Include="..\..\libyara\lex.c" />
     <ClCompile Include="..\..\libyara\libyara.c" />
     <ClCompile Include="..\..\libyara\mem.c" />
+    <ClCompile Include="..\..\libyara\parser.c" />
     <ClCompile Include="..\..\libyara\proc.c" />
     <ClCompile Include="..\..\libyara\regex\regex-pcre.c" />
-    <ClCompile Include="..\..\libyara\scan.c" />
-    <ClCompile Include="..\..\libyara\weight.c" />
-    <ClCompile Include="..\..\libyara\xtoi.c" />
+    <ClCompile Include="..\..\libyara\rules.c" />
+    <ClCompile Include="..\..\libyara\utils.c" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
diff --git a/windows/yara/yara.vcxproj b/windows/yara/yara.vcxproj
index 8e89e9f..5d705f3 100644
--- a/windows/yara/yara.vcxproj
+++ b/windows/yara/yara.vcxproj
@@ -34,7 +34,7 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
@@ -45,7 +45,7 @@
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <CharacterSet>NotSet</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
@@ -90,7 +90,7 @@
       <PrecompiledHeader>NotUsing</PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>..\..\libyara;..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
     </ClCompile>
diff --git a/yara-python/setupwin64.py b/yara-python/setupwin64.py
index f8c88ab..e570f24 100644
--- a/yara-python/setupwin64.py
+++ b/yara-python/setupwin64.py
@@ -9,6 +9,6 @@ setup(name='yara-python',
         sources=['yara-python.c'],
         include_dirs=['../windows/include', '../libyara'],
         extra_objects=[
-          '../windows/yara/x64/Release/libyara64.lib',
+          '../windows/yara/Release/libyara64.lib',
           '../windows/lib/pcre64.lib']
         )])
diff --git a/yara.c b/yara.c
index 75f8bfb..2c58a0c 100644
--- a/yara.c
+++ b/yara.c
@@ -40,6 +40,11 @@ limitations under the License.
 #define MAX_PATH 255
 #endif
 
+#ifdef _MSC_VER
+#define snprintf _snprintf
+#endif
+
+
 int recursive_search = FALSE;
 int show_tags = FALSE;
 int show_specified_tags = FALSE;
@@ -140,7 +145,7 @@ int is_directory(
   }
 }
 
-void scan_dir(
+int scan_dir(
     const char* dir,
     int recursive,
     YARA_RULES* rules,
@@ -152,6 +157,8 @@ void scan_dir(
   char full_path[MAX_PATH];
   static char path_and_mask[MAX_PATH];
 
+  int result = ERROR_SUCCESS;
+
   snprintf(path_and_mask, sizeof(path_and_mask), "%s\\*", dir);
 
   hFind = FindFirstFile(path_and_mask, &FindFileData);
@@ -166,17 +173,22 @@ void scan_dir(
       if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
       {
         //printf("Processing %s...\n", FindFileData.cFileName);
-        yr_rules_scan_file(rules, full_path, callback, full_path);
+        result = yr_rules_scan_file(rules, full_path, callback, full_path);
       }
       else if (recursive && FindFileData.cFileName[0] != '.' )
       {
-        scan_dir(full_path, recursive, rules, callback);
+        result = scan_dir(full_path, recursive, rules, callback);
       }
 
+      if (result != ERROR_SUCCESS)
+        break;
+
     } while (FindNextFile(hFind, &FindFileData));
 
     FindClose(hFind);
   }
+
+  return result;
 }
 
 #else
@@ -451,7 +463,6 @@ int callback(RULE* rule, void* data)
 
 
 int process_cmd_line(
-    YARA_COMPILER* compiler,
     int argc,
     char const* argv[])
 {
@@ -655,7 +666,7 @@ int main(
   int errors;
   int result;
 
-  if (!process_cmd_line(compiler, argc, argv))
+  if (!process_cmd_line(argc, argv))
     return 0;
 
   if (argc == 1 || optind == argc)

-- 
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