[Forensics-changes] [yara] 20/415: Changed malloc and free by HeapAlloc and HeapFree under Windows

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:42:38 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 745b852803ba346067d74354123f5c293bbada6b
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Fri Mar 6 14:16:07 2009 +0000

    Changed malloc and free by HeapAlloc and HeapFree under Windows
---
 libyara/Makefile               |   7 +-
 libyara/Makefile.am            |   4 +-
 libyara/Makefile.in            |   7 +-
 libyara/ast.c                  |  67 +++++++-------
 libyara/grammar.c              | 201 +++++++++++++++++++++--------------------
 libyara/grammar.h              |   2 +-
 libyara/grammar.y              |  27 +++---
 libyara/lex.c                  | 168 +++++++++++++++++-----------------
 libyara/lex.l                  |  20 ++--
 libyara/mem.c                  |  69 ++++++++++++++
 libyara/mem.h                  |  21 +++++
 libyara/scan.c                 |  25 ++---
 windows/libyara/libyara.vcproj |   4 +
 13 files changed, 360 insertions(+), 262 deletions(-)

diff --git a/libyara/Makefile b/libyara/Makefile
index 780aa06..f977dfb 100644
--- a/libyara/Makefile
+++ b/libyara/Makefile
@@ -59,7 +59,7 @@ libLTLIBRARIES_INSTALL = $(INSTALL)
 LTLIBRARIES = $(lib_LTLIBRARIES)
 libyara_la_LIBADD =
 am_libyara_la_OBJECTS = grammar.lo lex.lo ast.lo scan.lo filemap.lo \
-	error.lo compile.lo eval.lo pefile.lo xtoi.lo
+	error.lo compile.lo eval.lo pefile.lo xtoi.lo mem.lo
 libyara_la_OBJECTS = $(am_libyara_la_OBJECTS)
 DEFAULT_INCLUDES = -I.
 depcomp = $(SHELL) $(top_srcdir)/depcomp
@@ -220,6 +220,7 @@ libyara_la_SOURCES = \
   eval.c \
   pefile.c \
   xtoi.c \
+  mem.c \
   ast.h \
   error.h \
   compile.h \
@@ -228,7 +229,8 @@ libyara_la_SOURCES = \
   pe.h \
   xtoi.h \
   pefile.h \
-  sizedstr.h
+  sizedstr.h \
+  mem.h
 
 libyara_la_LIBDADD = /usr/local/lib/libpcre.la
 all: config.h
@@ -334,6 +336,7 @@ include ./$(DEPDIR)/eval.Plo
 include ./$(DEPDIR)/filemap.Plo
 include ./$(DEPDIR)/grammar.Plo
 include ./$(DEPDIR)/lex.Plo
+include ./$(DEPDIR)/mem.Plo
 include ./$(DEPDIR)/pefile.Plo
 include ./$(DEPDIR)/scan.Plo
 include ./$(DEPDIR)/xtoi.Plo
diff --git a/libyara/Makefile.am b/libyara/Makefile.am
index 879cfb7..00c5ff1 100644
--- a/libyara/Makefile.am
+++ b/libyara/Makefile.am
@@ -15,6 +15,7 @@ libyara_la_SOURCES = \
   eval.c \
   pefile.c \
   xtoi.c \
+  mem.c \
   ast.h \
   error.h \
   compile.h \
@@ -23,6 +24,7 @@ libyara_la_SOURCES = \
   pe.h \
   xtoi.h \
   pefile.h \
-  sizedstr.h
+  sizedstr.h \
+  mem.h
 
 libyara_la_LIBDADD = /usr/local/lib/libpcre.la
\ No newline at end of file
diff --git a/libyara/Makefile.in b/libyara/Makefile.in
index 11714c1..3ce2fac 100644
--- a/libyara/Makefile.in
+++ b/libyara/Makefile.in
@@ -59,7 +59,7 @@ libLTLIBRARIES_INSTALL = $(INSTALL)
 LTLIBRARIES = $(lib_LTLIBRARIES)
 libyara_la_LIBADD =
 am_libyara_la_OBJECTS = grammar.lo lex.lo ast.lo scan.lo filemap.lo \
-	error.lo compile.lo eval.lo pefile.lo xtoi.lo
+	error.lo compile.lo eval.lo pefile.lo xtoi.lo mem.lo
 libyara_la_OBJECTS = $(am_libyara_la_OBJECTS)
 DEFAULT_INCLUDES = -I. at am__isrc@
 depcomp = $(SHELL) $(top_srcdir)/depcomp
@@ -220,6 +220,7 @@ libyara_la_SOURCES = \
   eval.c \
   pefile.c \
   xtoi.c \
+  mem.c \
   ast.h \
   error.h \
   compile.h \
@@ -228,7 +229,8 @@ libyara_la_SOURCES = \
   pe.h \
   xtoi.h \
   pefile.h \
-  sizedstr.h
+  sizedstr.h \
+  mem.h
 
 libyara_la_LIBDADD = /usr/local/lib/libpcre.la
 all: config.h
@@ -334,6 +336,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/filemap.Plo at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/grammar.Plo at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/lex.Plo at am__quote@
+ at AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/mem.Plo at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/pefile.Plo at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/scan.Plo at am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/xtoi.Plo at am__quote@
diff --git a/libyara/ast.c b/libyara/ast.c
index c228535..1393f86 100644
--- a/libyara/ast.c
+++ b/libyara/ast.c
@@ -22,13 +22,10 @@ GNU General Public License for more details.
 #include "yara.h"
 #include "ast.h"
 #include "error.h"
+#include "mem.h"
 
 #define todigit(x)  ((x) >='A'&& (x) <='F')? ((unsigned char) (x - 'A' + 10)) : ((unsigned char) (x - '0'))
 
-#ifdef WIN32
-#define strdup _strdup
-#endif
-
 RULE* lookup_rule(RULE_LIST* rules, char* identifier)
 {
     RULE* rule = rules->head;
@@ -121,7 +118,7 @@ int new_rule(RULE_LIST* rules, char* identifier, int flags, TAG* tag_list_head,
     
     if (lookup_rule(rules, identifier) == NULL)  /* do not allow rules with the same identifier */
     {
-        new_rule = (RULE*) malloc(sizeof(RULE));
+        new_rule = (RULE*) yr_malloc(sizeof(RULE));
     
         if (new_rule != NULL)
         {
@@ -188,13 +185,13 @@ int new_hex_string(SIZED_STRING* charstr, unsigned char** hexstr, unsigned char*
     
     //assert(charstr[0] == '{' && charstr[len - 1] == '}');
     
-    *hexstr = hex = (unsigned char*) malloc(len / 2);
-    *maskstr = mask = (unsigned char*) malloc(len);
+    *hexstr = hex = (unsigned char*) yr_malloc(len / 2);
+    *maskstr = mask = (unsigned char*) yr_malloc(len);
     
     if (hex == NULL || mask == NULL)
     {
-        if (hex) free(hex);
-        if (mask) free(mask);
+        if (hex) yr_free(hex);
+        if (mask) yr_free(mask);
         
         return ERROR_INSUFICIENT_MEMORY;
     }
@@ -418,8 +415,8 @@ int new_hex_string(SIZED_STRING* charstr, unsigned char** hexstr, unsigned char*
     
     if (result != ERROR_SUCCESS)
     {
-        free(*hexstr);
-        free(*maskstr); 
+        yr_free(*hexstr);
+        yr_free(*maskstr); 
         *hexstr = NULL;
         *maskstr = NULL;
     }
@@ -438,7 +435,7 @@ int new_text_string(SIZED_STRING* charstr, int flags, unsigned char** hexstr, RE
     //assert(charstr && hexstr && regexp && length);
     
     *length = charstr->length;
-    *hexstr = malloc(charstr->length);
+    *hexstr = yr_malloc(charstr->length);
     
     if (*hexstr == NULL)
     {
@@ -483,7 +480,7 @@ int new_string(char* identifier, SIZED_STRING* charstr, int flags, STRING** stri
     STRING* new_string;
     int result = ERROR_SUCCESS;
         
-    new_string = (STRING*) malloc(sizeof(STRING));
+    new_string = (STRING*) yr_malloc(sizeof(STRING));
     
     if(new_string != NULL)
     {
@@ -506,7 +503,7 @@ int new_string(char* identifier, SIZED_STRING* charstr, int flags, STRING** stri
         
         if (result != ERROR_SUCCESS)
         {
-            free(new_string);
+            yr_free(new_string);
             new_string = NULL;
         }   
     }
@@ -524,7 +521,7 @@ int new_simple_term(int type, TERM** term)
     TERM* new_term;
     int result = ERROR_SUCCESS;
     
-    new_term = (TERM*) malloc(sizeof(TERM));
+    new_term = (TERM*) yr_malloc(sizeof(TERM));
     
     if (new_term != NULL)
     {
@@ -545,7 +542,7 @@ int new_unary_operation(int type, TERM* op, TERM_UNARY_OPERATION** term)
     TERM_UNARY_OPERATION* new_term;
     int result = ERROR_SUCCESS;
     
-    new_term = (TERM_UNARY_OPERATION*) malloc(sizeof(TERM_UNARY_OPERATION));
+    new_term = (TERM_UNARY_OPERATION*) yr_malloc(sizeof(TERM_UNARY_OPERATION));
     
     if (new_term != NULL)
     {
@@ -566,7 +563,7 @@ int new_binary_operation(int type, TERM* op1, TERM* op2, TERM_BINARY_OPERATION**
     TERM_BINARY_OPERATION* new_term;
     int result = ERROR_SUCCESS;
     
-    new_term = (TERM_BINARY_OPERATION*) malloc(sizeof(TERM_BINARY_OPERATION));
+    new_term = (TERM_BINARY_OPERATION*) yr_malloc(sizeof(TERM_BINARY_OPERATION));
     
     if (new_term != NULL)
     {
@@ -588,7 +585,7 @@ int new_ternary_operation(int type, TERM* op1, TERM* op2, TERM* op3, TERM_TERNAR
     TERM_TERNARY_OPERATION* new_term;
     int result = ERROR_SUCCESS;
     
-    new_term = (TERM_TERNARY_OPERATION*) malloc(sizeof(TERM_TERNARY_OPERATION));
+    new_term = (TERM_TERNARY_OPERATION*) yr_malloc(sizeof(TERM_TERNARY_OPERATION));
     
     if (new_term != NULL)
     {
@@ -611,7 +608,7 @@ int new_constant(unsigned int constant, TERM_CONST** term)
     TERM_CONST* new_term;
     int result = ERROR_SUCCESS;
     
-    new_term = (TERM_CONST*) malloc(sizeof(TERM_CONST));
+    new_term = (TERM_CONST*) yr_malloc(sizeof(TERM_CONST));
 
     if (new_term != NULL)
     {
@@ -642,7 +639,7 @@ int new_string_identifier(int type, STRING* defined_strings, char* identifier, T
     		/* the string has been used in an expression, mark it as referenced */
     		string->flags |= STRING_FLAGS_REFERENCED;  
 	
-            new_term = (TERM_STRING*) malloc(sizeof(TERM_STRING));
+            new_term = (TERM_STRING*) yr_malloc(sizeof(TERM_STRING));
 
             if (new_term != NULL)
             {
@@ -658,7 +655,7 @@ int new_string_identifier(int type, STRING* defined_strings, char* identifier, T
     }
     else  /* anonymous strings */
     {
-        new_term = (TERM_STRING*) malloc(sizeof(TERM_STRING));
+        new_term = (TERM_STRING*) yr_malloc(sizeof(TERM_STRING));
 
         if (new_term != NULL)
         {
@@ -695,7 +692,7 @@ void free_term(TERM* term)
         while (next != NULL)
         {
             tmp = next->next;
-            free(next);
+            yr_free(next);
             next = tmp;     
         }
     
@@ -714,7 +711,7 @@ void free_term(TERM* term)
 
 	case TERM_TYPE_STRING_IN_SECTION_BY_NAME:
 	    
-	    free(((TERM_STRING*)term)->section_name);
+	    yr_free(((TERM_STRING*)term)->section_name);
 		break;
                     
     case TERM_TYPE_AND:          
@@ -754,12 +751,12 @@ void free_term(TERM* term)
         break;
     }
     
-    free(term);
+    yr_free(term);
 }
 
 RULE_LIST* alloc_rule_list()
 {
-	RULE_LIST* rule_list = (RULE_LIST*) malloc(sizeof(RULE_LIST));
+	RULE_LIST* rule_list = (RULE_LIST*) yr_malloc(sizeof(RULE_LIST));
 
 	rule_list->head = NULL;
 	rule_list->tail = NULL;
@@ -801,12 +798,12 @@ void free_rule_list(RULE_LIST* rule_list)
         {
             next_string = string->next;
             
-			free(string->identifier);
-            free(string->string);
+			yr_free(string->identifier);
+            yr_free(string->string);
             
             if (IS_HEX(string))
             {   
-                free(string->mask);
+                yr_free(string->mask);
             }
             else if (IS_REGEXP(string))
             {
@@ -819,11 +816,11 @@ void free_rule_list(RULE_LIST* rule_list)
             while (match != NULL)
             {
                 next_match = match->next;
-                free(match);
+                yr_free(match);
                 match = next_match;
             }
             
-            free(string);
+            yr_free(string);
             string = next_string;
         }
 
@@ -833,18 +830,18 @@ void free_rule_list(RULE_LIST* rule_list)
 		{
 			next_tag = tag->next;
 			
-			free(tag->identifier);
-			free(tag);
+			yr_free(tag->identifier);
+			yr_free(tag);
 			
 			tag = next_tag;
 		}
         
         free_term(rule->condition);
-        free(rule->identifier);     
-        free(rule);
+        yr_free(rule->identifier);     
+        yr_free(rule);
         rule = next_rule;
     }
 
-	free(rule_list);
+	yr_free(rule_list);
 }
 
diff --git a/libyara/grammar.c b/libyara/grammar.c
index eee2486..48a847b 100644
--- a/libyara/grammar.c
+++ b/libyara/grammar.c
@@ -181,6 +181,7 @@
 #include "error.h"
 #include "compile.h"
 #include "sizedstr.h"
+#include "mem.h"
 
 #define YYERROR_VERBOSE
 //#define YYDEBUG 1
@@ -201,7 +202,7 @@
 #endif
 
 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
-#line 93 "grammar.y"
+#line 94 "grammar.y"
 typedef union YYSTYPE {
     
     void*           sized_string;
@@ -213,7 +214,7 @@ typedef union YYSTYPE {
 
 } YYSTYPE;
 /* Line 190 of yacc.c.  */
-#line 217 "grammar.c"
+#line 218 "grammar.c"
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
 # define YYSTYPE_IS_DECLARED 1
 # define YYSTYPE_IS_TRIVIAL 1
@@ -222,7 +223,7 @@ typedef union YYSTYPE {
 
 
 /* Copy the second part of user declarations.  */
-#line 107 "grammar.y"
+#line 108 "grammar.y"
  
     
 /* Global variables */
@@ -260,7 +261,7 @@ int count_strings(TERM_STRING* st);
 
 
 /* Line 213 of yacc.c.  */
-#line 264 "grammar.c"
+#line 265 "grammar.c"
 
 #if ! defined (yyoverflow) || YYERROR_VERBOSE
 
@@ -471,15 +472,15 @@ static const yysigned_char yyrhs[] =
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
 static const unsigned short int yyrline[] =
 {
-       0,   145,   145,   146,   147,   150,   159,   170,   171,   174,
-     175,   178,   179,   182,   192,   204,   214,   226,   237,   248,
-     261,   262,   265,   266,   267,   268,   271,   272,   273,   284,
-     295,   306,   310,   321,   333,   332,   350,   349,   367,   366,
-     383,   394,   405,   416,   417,   418,   419,   420,   421,   422,
-     423,   424,   425,   426,   427,   428,   432,   433,   436,   437,
-     443,   454,   468,   469,   470,   471,   472,   473,   474,   475,
-     476,   487,   498,   499,   500,   501,   502,   503,   506,   509,
-     510,   511
+       0,   146,   146,   147,   148,   151,   160,   171,   172,   175,
+     176,   179,   180,   183,   193,   205,   215,   227,   238,   249,
+     262,   263,   266,   267,   268,   269,   272,   273,   274,   285,
+     296,   307,   311,   322,   334,   333,   351,   350,   368,   367,
+     384,   395,   406,   417,   418,   419,   420,   421,   422,   423,
+     424,   425,   426,   427,   428,   429,   433,   434,   437,   438,
+     444,   455,   469,   470,   471,   472,   473,   474,   475,   476,
+     477,   488,   499,   500,   501,   502,   503,   504,   507,   510,
+     511,   512
 };
 #endif
 
@@ -1348,7 +1349,7 @@ yyreduce:
   switch (yyn)
     {
         case 5:
-#line 151 "grammar.y"
+#line 152 "grammar.y"
     { 
                 if (reduce_rule_declaration((yyvsp[-6].c_string),(yyvsp[-8].integer),(yyvsp[-5].tag),0,(yyvsp[-1].term)) != ERROR_SUCCESS)
                 {
@@ -1360,7 +1361,7 @@ yyreduce:
     break;
 
   case 6:
-#line 160 "grammar.y"
+#line 161 "grammar.y"
     { 
                 if (reduce_rule_declaration((yyvsp[-9].c_string),(yyvsp[-11].integer),(yyvsp[-8].tag),(yyvsp[-4].string),(yyvsp[-1].term)) != ERROR_SUCCESS)
                 {
@@ -1372,37 +1373,37 @@ yyreduce:
     break;
 
   case 7:
-#line 170 "grammar.y"
+#line 171 "grammar.y"
     { (yyval.integer) = 0;  }
     break;
 
   case 8:
-#line 171 "grammar.y"
+#line 172 "grammar.y"
     { (yyval.integer) = (yyvsp[-1].integer) | (yyvsp[0].integer); }
     break;
 
   case 9:
-#line 174 "grammar.y"
+#line 175 "grammar.y"
     { (yyval.integer) = RULE_FLAGS_PRIVATE; }
     break;
 
   case 10:
-#line 175 "grammar.y"
+#line 176 "grammar.y"
     { (yyval.integer) = RULE_FLAGS_GLOBAL; }
     break;
 
   case 11:
-#line 178 "grammar.y"
+#line 179 "grammar.y"
     { (yyval.tag) = NULL; }
     break;
 
   case 12:
-#line 179 "grammar.y"
+#line 180 "grammar.y"
     { (yyval.tag) = (yyvsp[0].tag);   }
     break;
 
   case 13:
-#line 182 "grammar.y"
+#line 183 "grammar.y"
     { 
                                                 (yyval.tag) = reduce_tags(NULL,(yyvsp[0].c_string)); 
                                                 
@@ -1416,7 +1417,7 @@ yyreduce:
     break;
 
   case 14:
-#line 192 "grammar.y"
+#line 193 "grammar.y"
     {   
                                                 (yyval.tag) = reduce_tags((yyvsp[-1].tag),(yyvsp[0].c_string)); 
                                                 
@@ -1430,7 +1431,7 @@ yyreduce:
     break;
 
   case 15:
-#line 204 "grammar.y"
+#line 205 "grammar.y"
     { 
                                                 (yyval.string) = reduce_strings(NULL,(yyvsp[0].string)); 
                                                 
@@ -1444,7 +1445,7 @@ yyreduce:
     break;
 
   case 16:
-#line 214 "grammar.y"
+#line 215 "grammar.y"
     { 
                                                 (yyval.string) = reduce_strings((yyvsp[-1].string),(yyvsp[0].string));
                                                 
@@ -1458,7 +1459,7 @@ yyreduce:
     break;
 
   case 17:
-#line 227 "grammar.y"
+#line 228 "grammar.y"
     { 
                             (yyval.string) = reduce_string_declaration((yyvsp[-3].term), (yyvsp[-1].sized_string), (yyvsp[0].integer)); 
                 
@@ -1472,7 +1473,7 @@ yyreduce:
     break;
 
   case 18:
-#line 238 "grammar.y"
+#line 239 "grammar.y"
     { 
                            (yyval.string) = reduce_string_declaration((yyvsp[-3].term), (yyvsp[-1].sized_string), (yyvsp[0].integer) | STRING_FLAGS_REGEXP); 
 
@@ -1486,7 +1487,7 @@ yyreduce:
     break;
 
   case 19:
-#line 249 "grammar.y"
+#line 250 "grammar.y"
     {
                             (yyval.string) = reduce_string_declaration((yyvsp[-2].term), (yyvsp[0].sized_string), STRING_FLAGS_HEXADECIMAL);
             
@@ -1500,47 +1501,47 @@ yyreduce:
     break;
 
   case 20:
-#line 261 "grammar.y"
+#line 262 "grammar.y"
     { (yyval.integer) = 0;  }
     break;
 
   case 21:
-#line 262 "grammar.y"
+#line 263 "grammar.y"
     { (yyval.integer) = (yyvsp[-1].integer) | (yyvsp[0].integer); }
     break;
 
   case 22:
-#line 265 "grammar.y"
+#line 266 "grammar.y"
     { (yyval.integer) = STRING_FLAGS_WIDE; }
     break;
 
   case 23:
-#line 266 "grammar.y"
+#line 267 "grammar.y"
     { (yyval.integer) = STRING_FLAGS_ASCII; }
     break;
 
   case 24:
-#line 267 "grammar.y"
+#line 268 "grammar.y"
     { (yyval.integer) = STRING_FLAGS_NO_CASE; }
     break;
 
   case 25:
-#line 268 "grammar.y"
+#line 269 "grammar.y"
     { (yyval.integer) = STRING_FLAGS_FULL_WORD; }
     break;
 
   case 26:
-#line 271 "grammar.y"
+#line 272 "grammar.y"
     { (yyval.term) = reduce_constant(1); }
     break;
 
   case 27:
-#line 272 "grammar.y"
+#line 273 "grammar.y"
     { (yyval.term) = reduce_constant(0); }
     break;
 
   case 28:
-#line 274 "grammar.y"
+#line 275 "grammar.y"
     { 
                         (yyval.term) = reduce_rule((yyvsp[0].c_string));
                         
@@ -1554,7 +1555,7 @@ yyreduce:
     break;
 
   case 29:
-#line 285 "grammar.y"
+#line 286 "grammar.y"
     {  
                         (yyval.term) = reduce_string((yyvsp[0].term));
                         
@@ -1568,7 +1569,7 @@ yyreduce:
     break;
 
   case 30:
-#line 296 "grammar.y"
+#line 297 "grammar.y"
     {          
                         (yyval.term) = reduce_string_at((yyvsp[-2].term), (yyvsp[0].term));
                         
@@ -1582,14 +1583,14 @@ yyreduce:
     break;
 
   case 31:
-#line 307 "grammar.y"
+#line 308 "grammar.y"
     { 
                         (yyval.term) = NULL; 
                      }
     break;
 
   case 32:
-#line 311 "grammar.y"
+#line 312 "grammar.y"
     {          
                         (yyval.term) = reduce_string_in_range((yyvsp[-7].term), (yyvsp[-4].term), (yyvsp[-1].term));
                         
@@ -1603,7 +1604,7 @@ yyreduce:
     break;
 
   case 33:
-#line 322 "grammar.y"
+#line 323 "grammar.y"
     {          
                         (yyval.term) = reduce_string_in_section_by_name((yyvsp[-5].term), (yyvsp[-1].sized_string));
 
@@ -1617,14 +1618,14 @@ yyreduce:
     break;
 
   case 34:
-#line 333 "grammar.y"
+#line 334 "grammar.y"
     { 
                           inside_for++; 
                       }
     break;
 
   case 35:
-#line 337 "grammar.y"
+#line 338 "grammar.y"
     { 
                            inside_for--; 
                            
@@ -1640,14 +1641,14 @@ yyreduce:
     break;
 
   case 36:
-#line 350 "grammar.y"
+#line 351 "grammar.y"
     { 
                          inside_for++; 
                      }
     break;
 
   case 37:
-#line 354 "grammar.y"
+#line 355 "grammar.y"
     { 
                           inside_for--; 
                           
@@ -1663,14 +1664,14 @@ yyreduce:
     break;
 
   case 38:
-#line 367 "grammar.y"
+#line 368 "grammar.y"
     { 
                            inside_for++; 
                      }
     break;
 
   case 39:
-#line 371 "grammar.y"
+#line 372 "grammar.y"
     { 
                           inside_for--; 
                                                     
@@ -1686,7 +1687,7 @@ yyreduce:
     break;
 
   case 40:
-#line 384 "grammar.y"
+#line 385 "grammar.y"
     { 
                          (yyval.term) = reduce_term(TERM_TYPE_OF, (yyvsp[-2].term), (yyvsp[0].term), NULL); 
                          
@@ -1700,7 +1701,7 @@ yyreduce:
     break;
 
   case 41:
-#line 395 "grammar.y"
+#line 396 "grammar.y"
     { 
                          (yyval.term) = reduce_term(TERM_TYPE_OF, reduce_constant(count_strings((yyvsp[0].term))), (yyvsp[0].term), NULL); 
                          
@@ -1714,7 +1715,7 @@ yyreduce:
     break;
 
   case 42:
-#line 406 "grammar.y"
+#line 407 "grammar.y"
     { 
                          (yyval.term) = reduce_term(TERM_TYPE_OF, reduce_constant(1), (yyvsp[0].term), NULL); 
                          
@@ -1728,89 +1729,89 @@ yyreduce:
     break;
 
   case 43:
-#line 416 "grammar.y"
+#line 417 "grammar.y"
     { (yyval.term) = NULL; }
     break;
 
   case 44:
-#line 417 "grammar.y"
+#line 418 "grammar.y"
     { (yyval.term) = (yyvsp[-1].term); }
     break;
 
   case 45:
-#line 418 "grammar.y"
+#line 419 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_NOT, (yyvsp[0].term), NULL, NULL); }
     break;
 
   case 46:
-#line 419 "grammar.y"
+#line 420 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_AND, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 47:
-#line 420 "grammar.y"
+#line 421 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_OR, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 48:
-#line 421 "grammar.y"
+#line 422 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_EQ, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 49:
-#line 422 "grammar.y"
+#line 423 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_LT, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 50:
-#line 423 "grammar.y"
+#line 424 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_GT, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 51:
-#line 424 "grammar.y"
+#line 425 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_LE, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 52:
-#line 425 "grammar.y"
+#line 426 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_GE, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 53:
-#line 426 "grammar.y"
+#line 427 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_EQ, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 54:
-#line 427 "grammar.y"
+#line 428 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_EQ, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 55:
-#line 428 "grammar.y"
+#line 429 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_NOT_EQ, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 56:
-#line 432 "grammar.y"
+#line 433 "grammar.y"
     { (yyval.term) = (yyvsp[-1].term); }
     break;
 
   case 57:
-#line 433 "grammar.y"
-    { (yyval.term) = reduce_string_with_wildcard(strdup("$*")); }
+#line 434 "grammar.y"
+    { (yyval.term) = reduce_string_with_wildcard(yr_strdup("$*")); }
     break;
 
   case 59:
-#line 438 "grammar.y"
+#line 439 "grammar.y"
     {
                          (yyval.term) = reduce_string_enumeration((yyvsp[-2].term),(yyvsp[0].term));
                       }
     break;
 
   case 60:
-#line 444 "grammar.y"
+#line 445 "grammar.y"
     {  
                               (yyval.term) = reduce_string((yyvsp[0].term));
 
@@ -1824,7 +1825,7 @@ yyreduce:
     break;
 
   case 61:
-#line 455 "grammar.y"
+#line 456 "grammar.y"
     { 
                               (yyval.term) = reduce_string_with_wildcard((yyvsp[0].term)); 
                               
@@ -1838,47 +1839,47 @@ yyreduce:
     break;
 
   case 62:
-#line 468 "grammar.y"
+#line 469 "grammar.y"
     { (yyval.term) = reduce_filesize(); }
     break;
 
   case 63:
-#line 469 "grammar.y"
+#line 470 "grammar.y"
     { (yyval.term) = reduce_entrypoint(); }
     break;
 
   case 64:
-#line 470 "grammar.y"
+#line 471 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_INT8_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 65:
-#line 471 "grammar.y"
+#line 472 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_INT16_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 66:
-#line 472 "grammar.y"
+#line 473 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_INT32_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 67:
-#line 473 "grammar.y"
+#line 474 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_UINT8_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 68:
-#line 474 "grammar.y"
+#line 475 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_UINT16_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 69:
-#line 475 "grammar.y"
+#line 476 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_UINT32_AT_OFFSET, (yyvsp[-1].term), NULL, NULL); }
     break;
 
   case 70:
-#line 477 "grammar.y"
+#line 478 "grammar.y"
     { 
                     (yyval.term) = reduce_string_count((yyvsp[0].term)); 
                     
@@ -1892,7 +1893,7 @@ yyreduce:
     break;
 
   case 71:
-#line 488 "grammar.y"
+#line 489 "grammar.y"
     { 
                     (yyval.term) = reduce_string_offset((yyvsp[0].term)); 
 
@@ -1906,32 +1907,32 @@ yyreduce:
     break;
 
   case 72:
-#line 498 "grammar.y"
+#line 499 "grammar.y"
     { (yyval.term) = (yyvsp[-1].term); }
     break;
 
   case 73:
-#line 499 "grammar.y"
+#line 500 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_ADD, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 74:
-#line 500 "grammar.y"
+#line 501 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_SUB, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 75:
-#line 501 "grammar.y"
+#line 502 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_MUL, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 76:
-#line 502 "grammar.y"
+#line 503 "grammar.y"
     { (yyval.term) = reduce_term(TERM_TYPE_DIV, (yyvsp[-2].term), (yyvsp[0].term), NULL); }
     break;
 
   case 78:
-#line 506 "grammar.y"
+#line 507 "grammar.y"
     { (yyval.term) = reduce_constant((yyvsp[0].integer)); }
     break;
 
@@ -1939,7 +1940,7 @@ yyreduce:
     }
 
 /* Line 1037 of yacc.c.  */
-#line 1943 "grammar.c"
+#line 1944 "grammar.c"
 

   yyvsp -= yylen;
   yyssp -= yylen;
@@ -2167,7 +2168,7 @@ yyreturn:
 }
 
 
-#line 514 "grammar.y"
+#line 515 "grammar.y"
 
 
 
@@ -2236,7 +2237,7 @@ STRING* reduce_string_declaration(char* identifier, SIZED_STRING* str, int flags
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(str);
+    yr_free(str);
             
     return string;
 }
@@ -2266,7 +2267,7 @@ TAG* reduce_tags(TAG* tag_list_head, char* identifier)
 
     if (lookup_tag(tag_list_head, identifier) == NULL) /* no tags with the same identifier */
     {
-        tag = malloc(sizeof(TAG));
+        tag = yr_malloc(sizeof(TAG));
         
         if (tag != NULL)
         {
@@ -2351,7 +2352,7 @@ TERM* reduce_string(char* identifier)
         last_result = ERROR_MISPLACED_ANONYMOUS_STRING;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -2390,7 +2391,7 @@ TERM* reduce_string_with_wildcard(char* identifier)
         string = string->next;
     }
     
-    free(identifier);
+    yr_free(identifier);
     return (TERM*) term;  
 }
 
@@ -2416,7 +2417,7 @@ TERM* reduce_string_at(char* identifier, TERM* offset)
         last_result = ERROR_MISPLACED_ANONYMOUS_STRING;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -2436,7 +2437,7 @@ TERM* reduce_string_in_range(char* identifier, TERM* lower_offset, TERM* upper_o
         term->upper_offset = upper_offset;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -2452,11 +2453,11 @@ TERM* reduce_string_in_section_by_name(char* identifier, SIZED_STRING* section_n
     }
     else
     {
-        term->section_name = strdup(section_name->c_string);
+        term->section_name = yr_strdup(section_name->c_string);
     }
     
-    free(section_name);
-    free(identifier);   
+    yr_free(section_name);
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -2471,7 +2472,7 @@ TERM* reduce_string_count(char* identifier)
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(identifier);           
+    yr_free(identifier);           
     return (TERM*) term;
 }
 
@@ -2486,7 +2487,7 @@ TERM* reduce_string_offset(char* identifier)
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(identifier);           
+    yr_free(identifier);           
     return (TERM*) term;
 }
 
@@ -2508,7 +2509,7 @@ TERM* reduce_rule(char* identifier)
         term = NULL;
     }
     
-    free(identifier);
+    yr_free(identifier);
     return (TERM*) term;
 }
 
diff --git a/libyara/grammar.h b/libyara/grammar.h
index 60a8df7..a022f63 100644
--- a/libyara/grammar.h
+++ b/libyara/grammar.h
@@ -146,7 +146,7 @@
 
 
 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
-#line 93 "grammar.y"
+#line 94 "grammar.y"
 typedef union YYSTYPE {
     
     void*           sized_string;
diff --git a/libyara/grammar.y b/libyara/grammar.y
index 060b264..09b63bd 100644
--- a/libyara/grammar.y
+++ b/libyara/grammar.y
@@ -9,6 +9,7 @@
 #include "error.h"
 #include "compile.h"
 #include "sizedstr.h"
+#include "mem.h"
 
 #define YYERROR_VERBOSE
 //#define YYDEBUG 1
@@ -430,7 +431,7 @@ boolean_expression : _TRUE_                                 { $$ = reduce_consta
                   
 
 string_set  : '(' string_enumeration ')'            { $$ = $2; }
-            | _THEM_                                { $$ = reduce_string_with_wildcard(strdup("$*")); }
+            | _THEM_                                { $$ = reduce_string_with_wildcard(yr_strdup("$*")); }
             ;                           
                             
 string_enumeration  : string_enumeration_item
@@ -579,7 +580,7 @@ STRING* reduce_string_declaration(char* identifier, SIZED_STRING* str, int flags
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(str);
+    yr_free(str);
             
     return string;
 }
@@ -609,7 +610,7 @@ TAG* reduce_tags(TAG* tag_list_head, char* identifier)
 
     if (lookup_tag(tag_list_head, identifier) == NULL) /* no tags with the same identifier */
     {
-        tag = malloc(sizeof(TAG));
+        tag = yr_malloc(sizeof(TAG));
         
         if (tag != NULL)
         {
@@ -694,7 +695,7 @@ TERM* reduce_string(char* identifier)
         last_result = ERROR_MISPLACED_ANONYMOUS_STRING;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -733,7 +734,7 @@ TERM* reduce_string_with_wildcard(char* identifier)
         string = string->next;
     }
     
-    free(identifier);
+    yr_free(identifier);
     return (TERM*) term;  
 }
 
@@ -759,7 +760,7 @@ TERM* reduce_string_at(char* identifier, TERM* offset)
         last_result = ERROR_MISPLACED_ANONYMOUS_STRING;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -779,7 +780,7 @@ TERM* reduce_string_in_range(char* identifier, TERM* lower_offset, TERM* upper_o
         term->upper_offset = upper_offset;
     }
     
-    free(identifier);   
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -795,11 +796,11 @@ TERM* reduce_string_in_section_by_name(char* identifier, SIZED_STRING* section_n
     }
     else
     {
-        term->section_name = strdup(section_name->c_string);
+        term->section_name = yr_strdup(section_name->c_string);
     }
     
-    free(section_name);
-    free(identifier);   
+    yr_free(section_name);
+    yr_free(identifier);   
     return (TERM*) term;
 }
 
@@ -814,7 +815,7 @@ TERM* reduce_string_count(char* identifier)
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(identifier);           
+    yr_free(identifier);           
     return (TERM*) term;
 }
 
@@ -829,7 +830,7 @@ TERM* reduce_string_offset(char* identifier)
         strcpy(last_error_extra_info, identifier);
     }
     
-    free(identifier);           
+    yr_free(identifier);           
     return (TERM*) term;
 }
 
@@ -851,7 +852,7 @@ TERM* reduce_rule(char* identifier)
         term = NULL;
     }
     
-    free(identifier);
+    yr_free(identifier);
     return (TERM*) term;
 }
 
diff --git a/libyara/lex.c b/libyara/lex.c
index 4399889..ca40532 100644
--- a/libyara/lex.c
+++ b/libyara/lex.c
@@ -520,21 +520,19 @@ char *yytext;
 #include "compile.h"
 #include "error.h"
 #include "xtoi.h"
+#include "mem.h"
 
 
 char string_buf[256];
 char *string_buf_ptr;
 unsigned short string_buf_len;
 
-#ifdef WIN32
-#define strdup _strdup
-#endif
 
 #define str 1
 
 #define regexp 2
 
-#line 538 "lex.c"
+#line 536 "lex.c"
 
 /* Macros after this point can all be overridden by user definitions in
  * section 1.
@@ -685,10 +683,10 @@ YY_DECL
 	register char *yy_cp, *yy_bp;
 	register int yy_act;
 
-#line 33 "lex.l"
+#line 31 "lex.l"
 
 
-#line 692 "lex.c"
+#line 690 "lex.c"
 
 	if ( yy_init )
 		{
@@ -773,207 +771,207 @@ do_action:	/* This label is used only to access EOF actions. */
 
 case 1:
 YY_RULE_SETUP
-#line 35 "lex.l"
+#line 33 "lex.l"
 { return _LT_;	        }
 	YY_BREAK
 case 2:
 YY_RULE_SETUP
-#line 36 "lex.l"
+#line 34 "lex.l"
 { return _GT_;	        }
 	YY_BREAK
 case 3:
 YY_RULE_SETUP
-#line 37 "lex.l"
+#line 35 "lex.l"
 { return _LE_;	        }
 	YY_BREAK
 case 4:
 YY_RULE_SETUP
-#line 38 "lex.l"
+#line 36 "lex.l"
 { return _GE_;	        }
 	YY_BREAK
 case 5:
 YY_RULE_SETUP
-#line 39 "lex.l"
+#line 37 "lex.l"
 { return _EQ_;		    }
 	YY_BREAK
 case 6:
 YY_RULE_SETUP
-#line 40 "lex.l"
+#line 38 "lex.l"
 { return _NEQ_;	    }
 	YY_BREAK
 case 7:
 YY_RULE_SETUP
-#line 41 "lex.l"
+#line 39 "lex.l"
 { return _PRIVATE_;    }
 	YY_BREAK
 case 8:
 YY_RULE_SETUP
-#line 42 "lex.l"
+#line 40 "lex.l"
 { return _GLOBAL_;     }
 	YY_BREAK
 case 9:
 YY_RULE_SETUP
-#line 43 "lex.l"
+#line 41 "lex.l"
 { return _RULE_;       }
 	YY_BREAK
 case 10:
 YY_RULE_SETUP
-#line 44 "lex.l"
+#line 42 "lex.l"
 { return _STRINGS_;    }
 	YY_BREAK
 case 11:
 YY_RULE_SETUP
-#line 45 "lex.l"
+#line 43 "lex.l"
 { return _ASCII_;      }
 	YY_BREAK
 case 12:
 YY_RULE_SETUP
-#line 46 "lex.l"
+#line 44 "lex.l"
 { return _WIDE_;       }
 	YY_BREAK
 case 13:
 YY_RULE_SETUP
-#line 47 "lex.l"
+#line 45 "lex.l"
 { return _FULLWORD_;   }
 	YY_BREAK
 case 14:
 YY_RULE_SETUP
-#line 48 "lex.l"
+#line 46 "lex.l"
 { return _NOCASE_;     }
 	YY_BREAK
 case 15:
 YY_RULE_SETUP
-#line 49 "lex.l"
+#line 47 "lex.l"
 { return _CONDITION_;  }
 	YY_BREAK
 case 16:
 YY_RULE_SETUP
-#line 50 "lex.l"
+#line 48 "lex.l"
 { return _TRUE_;       }
 	YY_BREAK
 case 17:
 YY_RULE_SETUP
-#line 51 "lex.l"
+#line 49 "lex.l"
 { return _FALSE_;      }
 	YY_BREAK
 case 18:
 YY_RULE_SETUP
-#line 52 "lex.l"
+#line 50 "lex.l"
 { return _NOT_;        }
 	YY_BREAK
 case 19:
 YY_RULE_SETUP
-#line 53 "lex.l"
+#line 51 "lex.l"
 { return _AND_;        }
 	YY_BREAK
 case 20:
 YY_RULE_SETUP
-#line 54 "lex.l"
+#line 52 "lex.l"
 { return _OR_;         }
 	YY_BREAK
 case 21:
 YY_RULE_SETUP
-#line 55 "lex.l"
+#line 53 "lex.l"
 { return _AT_;         }
 	YY_BREAK
 case 22:
 YY_RULE_SETUP
-#line 56 "lex.l"
+#line 54 "lex.l"
 { return _IN_;         }
 	YY_BREAK
 case 23:
 YY_RULE_SETUP
-#line 57 "lex.l"
+#line 55 "lex.l"
 { return _OF_;         }
 	YY_BREAK
 case 24:
 YY_RULE_SETUP
-#line 58 "lex.l"
+#line 56 "lex.l"
 { return _THEM_;		}
 	YY_BREAK
 case 25:
 YY_RULE_SETUP
-#line 59 "lex.l"
+#line 57 "lex.l"
 { return _FOR_;        }
 	YY_BREAK
 case 26:
 YY_RULE_SETUP
-#line 60 "lex.l"
+#line 58 "lex.l"
 { return _ALL_;		}
 	YY_BREAK
 case 27:
 YY_RULE_SETUP
-#line 61 "lex.l"
+#line 59 "lex.l"
 { return _ANY_;		}
 	YY_BREAK
 case 28:
 YY_RULE_SETUP
-#line 62 "lex.l"
+#line 60 "lex.l"
 { return _ENTRYPOINT_; }
 	YY_BREAK
 case 29:
 YY_RULE_SETUP
-#line 63 "lex.l"
+#line 61 "lex.l"
 { return _SIZE_;       }
 	YY_BREAK
 case 30:
 YY_RULE_SETUP
-#line 64 "lex.l"
+#line 62 "lex.l"
 { return _RVA_;   	    }
 	YY_BREAK
 case 31:
 YY_RULE_SETUP
-#line 65 "lex.l"
+#line 63 "lex.l"
 { return _OFFSET_;     }
 	YY_BREAK
 case 32:
 YY_RULE_SETUP
-#line 66 "lex.l"
+#line 64 "lex.l"
 { return _FILE_;       }
 	YY_BREAK
 case 33:
 YY_RULE_SETUP
-#line 67 "lex.l"
+#line 65 "lex.l"
 { return _IS_;         }
 	YY_BREAK
 case 34:
 YY_RULE_SETUP
-#line 68 "lex.l"
+#line 66 "lex.l"
 { return _SECTION_;    }
 	YY_BREAK
 case 35:
 YY_RULE_SETUP
-#line 69 "lex.l"
+#line 67 "lex.l"
 { return _UINT8_;    	}
 	YY_BREAK
 case 36:
 YY_RULE_SETUP
-#line 70 "lex.l"
+#line 68 "lex.l"
 { return _UINT16_;    	}
 	YY_BREAK
 case 37:
 YY_RULE_SETUP
-#line 71 "lex.l"
+#line 69 "lex.l"
 { return _UINT32_;    	}
 	YY_BREAK
 case 38:
 YY_RULE_SETUP
-#line 72 "lex.l"
+#line 70 "lex.l"
 { return _INT8_;    	}
 	YY_BREAK
 case 39:
 YY_RULE_SETUP
-#line 73 "lex.l"
+#line 71 "lex.l"
 { return _INT16_;    	}
 	YY_BREAK
 case 40:
 YY_RULE_SETUP
-#line 74 "lex.l"
+#line 72 "lex.l"
 { return _INT32_;    	}
 	YY_BREAK
 case 41:
 YY_RULE_SETUP
-#line 78 "lex.l"
+#line 76 "lex.l"
 {	/* skip comments */
 								
 								char* line_break = strchr( yytext, '\n' );
@@ -987,7 +985,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 42:
 YY_RULE_SETUP
-#line 89 "lex.l"
+#line 87 "lex.l"
 {   /* skip single-line comments */
 
 								line_number++;
@@ -995,41 +993,41 @@ YY_RULE_SETUP
 	YY_BREAK
 case 43:
 YY_RULE_SETUP
-#line 94 "lex.l"
+#line 92 "lex.l"
 {
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
                        		return _STRING_IDENTIFIER_WITH_WILDCARD_;      
 					 	}
 	YY_BREAK
 case 44:
 YY_RULE_SETUP
-#line 99 "lex.l"
+#line 97 "lex.l"
 {
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
                        		return _STRING_IDENTIFIER_;      
 					 }
 	YY_BREAK
 case 45:
 YY_RULE_SETUP
-#line 105 "lex.l"
+#line 103 "lex.l"
 {	
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
 							yylval.c_string[0] = '$'; 					/* replace # by $*/
 		                    return _STRING_COUNT_;      
 					 }
 	YY_BREAK
 case 46:
 YY_RULE_SETUP
-#line 111 "lex.l"
+#line 109 "lex.l"
 {	
-	                      	yylval.c_string = (char*) strdup(yytext);
+	                      	yylval.c_string = (char*) yr_strdup(yytext);
 							yylval.c_string[0] = '$'; 					/* replace @ by $*/
 		                    return _STRING_OFFSET_;      
 					 }					
 	YY_BREAK
 case 47:
 YY_RULE_SETUP
-#line 117 "lex.l"
+#line 115 "lex.l"
 { 
 										if (strlen(yytext) > 128)
 										{
@@ -1037,13 +1035,13 @@ YY_RULE_SETUP
 											yynerrs++;
 										}
 										
-										yylval.c_string = (char*) strdup(yytext);
+										yylval.c_string = (char*) yr_strdup(yytext);
                    						return _IDENTIFIER_;
 									}
 	YY_BREAK
 case 48:
 YY_RULE_SETUP
-#line 128 "lex.l"
+#line 126 "lex.l"
 { 
 						yylval.integer = (unsigned int) atoi(yytext);
 						
@@ -1061,7 +1059,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 49:
 YY_RULE_SETUP
-#line 143 "lex.l"
+#line 141 "lex.l"
 {
 						yylval.integer = xtoi(yytext + 2);
 						return _NUMBER_;
@@ -1069,7 +1067,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 50:
 YY_RULE_SETUP
-#line 148 "lex.l"
+#line 146 "lex.l"
 { 	/* saw closing quote - all done */
 
 						SIZED_STRING* s;
@@ -1084,7 +1082,7 @@ YY_RULE_SETUP
 
 						BEGIN(INITIAL);
 						
-						s = (SIZED_STRING*) malloc(string_buf_len + sizeof(SIZED_STRING));
+						s = (SIZED_STRING*) yr_malloc(string_buf_len + sizeof(SIZED_STRING));
 						
 						s->length = string_buf_len;
 						
@@ -1097,22 +1095,22 @@ YY_RULE_SETUP
 	YY_BREAK
 case 51:
 YY_RULE_SETUP
-#line 173 "lex.l"
+#line 171 "lex.l"
 { *string_buf_ptr++ = '\t'; string_buf_len++; }
 	YY_BREAK
 case 52:
 YY_RULE_SETUP
-#line 174 "lex.l"
+#line 172 "lex.l"
 { *string_buf_ptr++ = '\"'; string_buf_len++; }
 	YY_BREAK
 case 53:
 YY_RULE_SETUP
-#line 175 "lex.l"
+#line 173 "lex.l"
 { *string_buf_ptr++ = '\\'; string_buf_len++; }
 	YY_BREAK
 case 54:
 YY_RULE_SETUP
-#line 177 "lex.l"
+#line 175 "lex.l"
 {
         						int result;
 
@@ -1124,7 +1122,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 55:
 YY_RULE_SETUP
-#line 186 "lex.l"
+#line 184 "lex.l"
 {
 						char *yptr = yytext;
 
@@ -1137,7 +1135,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 56:
 YY_RULE_SETUP
-#line 196 "lex.l"
+#line 194 "lex.l"
 {
 						yyerror("unterminated string");
 						yyterminate();
@@ -1146,7 +1144,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 57:
 YY_RULE_SETUP
-#line 202 "lex.l"
+#line 200 "lex.l"
 {
 						yyerror("illegal escape sequence");
 						yynerrs++;
@@ -1154,7 +1152,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 58:
 YY_RULE_SETUP
-#line 208 "lex.l"
+#line 206 "lex.l"
 { 	
 						SIZED_STRING* s;
 
@@ -1168,7 +1166,7 @@ YY_RULE_SETUP
 
 						BEGIN(INITIAL);
 
-						s = (SIZED_STRING*) malloc(string_buf_len + sizeof(SIZED_STRING));
+						s = (SIZED_STRING*) yr_malloc(string_buf_len + sizeof(SIZED_STRING));
 
 						s->length = string_buf_len;
 
@@ -1181,7 +1179,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 59:
 YY_RULE_SETUP
-#line 232 "lex.l"
+#line 230 "lex.l"
 { 				
 						*string_buf_ptr++ = '/';
 						string_buf_len++ ;
@@ -1189,7 +1187,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 60:
 YY_RULE_SETUP
-#line 237 "lex.l"
+#line 235 "lex.l"
 { 				
 						*string_buf_ptr++ = yytext[0];
 						*string_buf_ptr++ = yytext[1];
@@ -1198,7 +1196,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 61:
 YY_RULE_SETUP
-#line 243 "lex.l"
+#line 241 "lex.l"
 {
 						char *yptr = yytext;
 
@@ -1211,7 +1209,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 62:
 YY_RULE_SETUP
-#line 253 "lex.l"
+#line 251 "lex.l"
 {
 						yyerror("unterminated regular expression");
 						yyterminate();
@@ -1220,7 +1218,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 63:
 YY_RULE_SETUP
-#line 259 "lex.l"
+#line 257 "lex.l"
 {
  						string_buf_ptr = string_buf; 
 						string_buf_len = 0;
@@ -1229,7 +1227,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 64:
 YY_RULE_SETUP
-#line 265 "lex.l"
+#line 263 "lex.l"
 {
  						string_buf_ptr = string_buf; 
 						string_buf_len = 0;
@@ -1238,11 +1236,11 @@ YY_RULE_SETUP
 	YY_BREAK
 case 65:
 YY_RULE_SETUP
-#line 272 "lex.l"
+#line 270 "lex.l"
 { 
 										int len = strlen(yytext);
 										
-										SIZED_STRING* s = (SIZED_STRING*) malloc(len + sizeof(SIZED_STRING));
+										SIZED_STRING* s = (SIZED_STRING*) yr_malloc(len + sizeof(SIZED_STRING));
 
 										s->length = len;
 
@@ -1255,12 +1253,12 @@ YY_RULE_SETUP
 	YY_BREAK
 case 66:
 YY_RULE_SETUP
-#line 287 "lex.l"
+#line 285 "lex.l"
 /* skip whitespace */
 	YY_BREAK
 case 67:
 YY_RULE_SETUP
-#line 289 "lex.l"
+#line 287 "lex.l"
 {
 						line_number++;
 						
@@ -1268,17 +1266,17 @@ YY_RULE_SETUP
 	YY_BREAK
 case 68:
 YY_RULE_SETUP
-#line 294 "lex.l"
+#line 292 "lex.l"
 { 
                        	return yytext[0];    
 					}
 	YY_BREAK
 case 69:
 YY_RULE_SETUP
-#line 297 "lex.l"
+#line 295 "lex.l"
 ECHO;
 	YY_BREAK
-#line 1282 "lex.c"
+#line 1280 "lex.c"
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(str):
 case YY_STATE_EOF(regexp):
@@ -2166,7 +2164,7 @@ int main()
 	return 0;
 	}
 #endif
-#line 297 "lex.l"
+#line 295 "lex.l"
 
 
 
diff --git a/libyara/lex.l b/libyara/lex.l
index 0e36eaa..33fb1c4 100644
--- a/libyara/lex.l
+++ b/libyara/lex.l
@@ -9,15 +9,13 @@
 #include "compile.h"
 #include "error.h"
 #include "xtoi.h"
+#include "mem.h"
 
 
 char string_buf[256];
 char *string_buf_ptr;
 unsigned short string_buf_len;
 
-#ifdef WIN32
-#define strdup _strdup
-#endif
 
 %}
 
@@ -92,24 +90,24 @@ hexdigit      [a-fA-F0-9]
 							}
 							
 $({letter}|{digit}|_)*"*" {
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
                        		return _STRING_IDENTIFIER_WITH_WILDCARD_;      
 					 	}
 
 $({letter}|{digit}|_)* {
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
                        		return _STRING_IDENTIFIER_;      
 					 }
 				
 					
 #({letter}|{digit}|_)* {	
-                       		yylval.c_string = (char*) strdup(yytext);
+                       		yylval.c_string = (char*) yr_strdup(yytext);
 							yylval.c_string[0] = '$'; 					/* replace # by $*/
 		                    return _STRING_COUNT_;      
 					 }
 					
 @({letter}|{digit}|_)* {	
-	                      	yylval.c_string = (char*) strdup(yytext);
+	                      	yylval.c_string = (char*) yr_strdup(yytext);
 							yylval.c_string[0] = '$'; 					/* replace @ by $*/
 		                    return _STRING_OFFSET_;      
 					 }					
@@ -121,7 +119,7 @@ $({letter}|{digit}|_)* {
 											yynerrs++;
 										}
 										
-										yylval.c_string = (char*) strdup(yytext);
+										yylval.c_string = (char*) yr_strdup(yytext);
                    						return _IDENTIFIER_;
 									}
 							
@@ -159,7 +157,7 @@ $({letter}|{digit}|_)* {
 
 						BEGIN(INITIAL);
 						
-						s = (SIZED_STRING*) malloc(string_buf_len + sizeof(SIZED_STRING));
+						s = (SIZED_STRING*) yr_malloc(string_buf_len + sizeof(SIZED_STRING));
 						
 						s->length = string_buf_len;
 						
@@ -218,7 +216,7 @@ $({letter}|{digit}|_)* {
 
 						BEGIN(INITIAL);
 
-						s = (SIZED_STRING*) malloc(string_buf_len + sizeof(SIZED_STRING));
+						s = (SIZED_STRING*) yr_malloc(string_buf_len + sizeof(SIZED_STRING));
 
 						s->length = string_buf_len;
 
@@ -272,7 +270,7 @@ $({letter}|{digit}|_)* {
 \{({hexdigit}|[ \-|\?\[\]\(\)])+\}		{ 
 										int len = strlen(yytext);
 										
-										SIZED_STRING* s = (SIZED_STRING*) malloc(len + sizeof(SIZED_STRING));
+										SIZED_STRING* s = (SIZED_STRING*) yr_malloc(len + sizeof(SIZED_STRING));
 
 										s->length = len;
 
diff --git a/libyara/mem.c b/libyara/mem.c
new file mode 100644
index 0000000..67ba00b
--- /dev/null
+++ b/libyara/mem.c
@@ -0,0 +1,69 @@
+/*
+
+Copyright(c) 2007. Victor M. Alvarez [plusvic at gmail.com].
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+*/
+
+#ifdef WIN32
+
+#include <windows.h>
+
+void* yr_malloc(size_t size)
+{
+    return (void*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
+}
+
+
+void yr_free(void* ptr)
+{
+    HeapFree(GetProcessHeap(), 0, ptr);
+}
+
+
+char* yr_strdup(const char *s)
+{
+    size_t len;
+    char *r;
+    
+    len = strlen(s);
+    
+    r = yr_malloc(len + 1);
+    
+    strcpy(r, s);
+    
+    return r;
+}
+
+#else
+
+#include <stdlib.h>
+#include <string.h>
+
+void* yr_malloc(size_t size)
+{
+    return malloc(size);
+}
+
+
+void yr_free(void *ptr)
+{
+    free(ptr);
+}
+
+
+char* yr_strdup(const char *s)
+{
+    return strdup(s);
+}
+
+#endif
\ No newline at end of file
diff --git a/libyara/mem.h b/libyara/mem.h
new file mode 100644
index 0000000..9d34fc2
--- /dev/null
+++ b/libyara/mem.h
@@ -0,0 +1,21 @@
+/*
+
+Copyright(c) 2007. Victor M. Alvarez [plusvic at gmail.com].
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+*/
+
+void* yr_malloc(size_t size);
+void yr_free(void *ptr);
+char* yr_strdup(const char *s);
+
+
diff --git a/libyara/scan.c b/libyara/scan.c
index 41aa7fc..ebd1972 100644
--- a/libyara/scan.c
+++ b/libyara/scan.c
@@ -24,6 +24,7 @@ GNU General Public License for more details.
 #include "ast.h"
 #include "pefile.h"
 #include "error.h"
+#include "mem.h"
 
 #ifndef TRUE
 #define TRUE 1
@@ -348,7 +349,7 @@ int init_hash_table(RULE_LIST* rule_list)
 				
 				/* both lowercases */
 				
-				entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+				entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
     			    return ERROR_INSUFICIENT_MEMORY;
@@ -361,7 +362,7 @@ int init_hash_table(RULE_LIST* rule_list)
     			
                 x = toupper(x);
 				
-				entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+				entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
                     return ERROR_INSUFICIENT_MEMORY;
@@ -374,7 +375,7 @@ int init_hash_table(RULE_LIST* rule_list)
     			
     			y = toupper(y);  
     			    
-    			entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+    			entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
                     return ERROR_INSUFICIENT_MEMORY;
@@ -387,7 +388,7 @@ int init_hash_table(RULE_LIST* rule_list)
     			    
                 x = tolower(x);
  
-    			entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+    			entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
                     return ERROR_INSUFICIENT_MEMORY;
@@ -399,7 +400,7 @@ int init_hash_table(RULE_LIST* rule_list)
 			}
 			else if (hashable)
 			{
-				entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+				entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
                     return ERROR_INSUFICIENT_MEMORY;
@@ -410,7 +411,7 @@ int init_hash_table(RULE_LIST* rule_list)
 			}
 			else /* non hashable */
 			{
-			    entry = (STRING_LIST_ENTRY*) malloc(sizeof(STRING_LIST_ENTRY));
+			    entry = (STRING_LIST_ENTRY*) yr_malloc(sizeof(STRING_LIST_ENTRY));
 				
 				if (entry == NULL)
                     return ERROR_INSUFICIENT_MEMORY;
@@ -444,7 +445,7 @@ void free_hash_table(RULE_LIST* rule_list)
 			while (entry != NULL)
 			{
 				next_entry = entry->next;
-				free(entry);
+				yr_free(entry);
 				entry = next_entry;
 			}
 			
@@ -457,7 +458,7 @@ void free_hash_table(RULE_LIST* rule_list)
     while (entry != NULL)
 	{
 		next_entry = entry->next;
-		free(entry);
+		yr_free(entry);
 		entry = next_entry;
 	}
 	
@@ -487,7 +488,7 @@ void clear_marks(RULE_LIST* rule_list)
 			while (match != NULL)
 			{
 				next_match = match->next;
-				free(match);
+				yr_free(match);
 				match = next_match;
 			}
 			
@@ -523,7 +524,7 @@ int string_match(unsigned char* buffer, unsigned int buffer_size, STRING* string
 			}
 						
 			len = i/2;
-			tmp = malloc(len);
+			tmp = yr_malloc(len);
             i = 0;
 			
 			if (tmp != NULL)
@@ -536,7 +537,7 @@ int string_match(unsigned char* buffer, unsigned int buffer_size, STRING* string
 								
 				match = regexp_match(tmp, len, string->string, string->length, string->re, (negative_size > 2));
 			
-				free(tmp);			
+				yr_free(tmp);			
 				return match * 2;
 			}
 			
@@ -662,7 +663,7 @@ int find_matches_for_strings(   STRING_LIST_ENTRY* first_string,
 		    if (!overlap)
 		    {		    
     			string->flags |= STRING_FLAGS_FOUND;
-    			match = (MATCH*) malloc(sizeof(MATCH));
+    			match = (MATCH*) yr_malloc(sizeof(MATCH));
 
     			if (match != NULL)
     			{
diff --git a/windows/libyara/libyara.vcproj b/windows/libyara/libyara.vcproj
index 85f7ee7..3df77bd 100644
--- a/windows/libyara/libyara.vcproj
+++ b/windows/libyara/libyara.vcproj
@@ -179,6 +179,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\libyara\mem.c"
+				>
+			</File>
+			<File
 				RelativePath="..\..\libyara\pefile.c"
 				>
 			</File>

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