[Forensics-changes] [yara] 38/415: Implemented includes enable/disabling parameter in yara.compile

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:42:41 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 aae781475b70a0dbb1ed05168813cc90a7c61374
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Sat Oct 24 13:51:56 2009 +0000

    Implemented includes enable/disabling parameter in yara.compile
---
 libyara/lex.c             | 260 +++++++++++++++++++++-------------------------
 libyara/lex.l             |  81 +++++++++------
 libyara/libyara.c         |   8 +-
 libyara/libyara.tmproj    |  80 ++++++++------
 libyara/yara.h            |   1 +
 yara-python/yara-python.c |  46 ++++++--
 6 files changed, 257 insertions(+), 219 deletions(-)

diff --git a/libyara/lex.c b/libyara/lex.c
index 2eaa0cd..1b8bd65 100644
--- a/libyara/lex.c
+++ b/libyara/lex.c
@@ -8,7 +8,7 @@
 #define FLEX_SCANNER
 #define YY_FLEX_MAJOR_VERSION 2
 #define YY_FLEX_MINOR_VERSION 5
-#define YY_FLEX_SUBMINOR_VERSION 35
+#define YY_FLEX_SUBMINOR_VERSION 33
 #if YY_FLEX_SUBMINOR_VERSION > 0
 #define FLEX_BETA
 #endif
@@ -30,7 +30,7 @@
 
 /* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */
 
-#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+#if __STDC_VERSION__ >= 199901L
 
 /* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
  * if you want the limit (max/min) macros for int types. 
@@ -93,12 +93,11 @@ typedef unsigned int flex_uint32_t;
 
 #else	/* ! __cplusplus */
 
-/* C99 requires __STDC__ to be defined as 1. */
-#if defined (__STDC__)
+#if __STDC__
 
 #define YY_USE_CONST
 
-#endif	/* defined (__STDC__) */
+#endif	/* __STDC__ */
 #endif	/* ! __cplusplus */
 
 #ifdef YY_USE_CONST
@@ -134,6 +133,8 @@ typedef void* yyscan_t;
 #define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column)
 #define yy_flex_debug yyg->yy_flex_debug_r
 
+int yylex_init (yyscan_t* scanner);
+
 /* Enter a start condition.  This macro really ought to take a parameter,
  * but we do it the disgusting crufty way forced on us by the ()-less
  * definition of BEGIN.
@@ -169,11 +170,6 @@ typedef void* yyscan_t;
 typedef struct yy_buffer_state *YY_BUFFER_STATE;
 #endif
 
-#ifndef YY_TYPEDEF_YY_SIZE_T
-#define YY_TYPEDEF_YY_SIZE_T
-typedef size_t yy_size_t;
-#endif
-
 #define EOB_ACT_CONTINUE_SCAN 0
 #define EOB_ACT_END_OF_FILE 1
 #define EOB_ACT_LAST_MATCH 2
@@ -209,6 +205,16 @@ typedef size_t yy_size_t;
 
 #define unput(c) yyunput( c, yyg->yytext_ptr , yyscanner )
 
+/* The following is because we cannot portably get our hands on size_t
+ * (without autoconf's help, which isn't available because we want
+ * flex-generated scanners to compile on their own).
+ */
+
+#ifndef YY_TYPEDEF_YY_SIZE_T
+#define YY_TYPEDEF_YY_SIZE_T
+typedef unsigned int yy_size_t;
+#endif
+
 #ifndef YY_STRUCT_YY_BUFFER_STATE
 #define YY_STRUCT_YY_BUFFER_STATE
 struct yy_buffer_state
@@ -226,7 +232,7 @@ struct yy_buffer_state
 	/* Number of characters read into yy_ch_buf, not including EOB
 	 * characters.
 	 */
-	yy_size_t yy_n_chars;
+	int yy_n_chars;
 
 	/* Whether we "own" the buffer - i.e., we know we created it,
 	 * and can realloc() it to grow it, and should free() it to
@@ -305,7 +311,7 @@ static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ,yyscan_t yyscanner );
 
 YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner );
 YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner );
-YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ,yyscan_t yyscanner );
+YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ,yyscan_t yyscanner );
 
 void *yyalloc (yy_size_t ,yyscan_t yyscanner );
 void *yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
@@ -641,7 +647,7 @@ static yyconst flex_int32_t yy_rule_can_match_eol[72] =
 
 
 
-#line 645 "lex.c"
+#line 651 "lex.c"
 
 #define INITIAL 0
 #define str 1
@@ -673,8 +679,8 @@ struct yyguts_t
     size_t yy_buffer_stack_max; /**< capacity of stack. */
     YY_BUFFER_STATE * yy_buffer_stack; /**< Stack as an array. */
     char yy_hold_char;
-    yy_size_t yy_n_chars;
-    yy_size_t yyleng_r;
+    int yy_n_chars;
+    int yyleng_r;
     char *yy_c_buf_p;
     int yy_init;
     int yy_start;
@@ -702,10 +708,6 @@ static int yy_init_globals (yyscan_t yyscanner );
      * from bison output in section 1.*/
     #    define yylval yyg->yylval_r
     
-int yylex_init (yyscan_t* scanner);
-
-int yylex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner);
-
 /* Accessor methods to globals.
    These are made visible to non-reentrant scanners for convenience. */
 
@@ -727,7 +729,7 @@ FILE *yyget_out (yyscan_t yyscanner );
 
 void yyset_out  (FILE * out_str ,yyscan_t yyscanner );
 
-yy_size_t yyget_leng (yyscan_t yyscanner );
+int yyget_leng (yyscan_t yyscanner );
 
 char *yyget_text (yyscan_t yyscanner );
 
@@ -781,7 +783,7 @@ static int input (yyscan_t yyscanner );
 /* This used to be an fputs(), but since the string might contain NUL's,
  * we now use fwrite().
  */
-#define ECHO fwrite( yytext, yyleng, 1, yyout )
+#define ECHO (void) fwrite( yytext, yyleng, 1, yyout )
 #endif
 
 /* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
@@ -792,7 +794,7 @@ static int input (yyscan_t yyscanner );
 	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
 		{ \
 		int c = '*'; \
-		yy_size_t n; \
+		size_t n; \
 		for ( n = 0; n < max_size && \
 			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
 			buf[n] = (char) c; \
@@ -846,11 +848,9 @@ static int input (yyscan_t yyscanner );
 #ifndef YY_DECL
 #define YY_DECL_IS_OURS 1
 
-extern int yylex \
-               (YYSTYPE * yylval_param ,yyscan_t yyscanner);
+extern int yylex (YYSTYPE * yylval_param ,yyscan_t yyscanner);
 
-#define YY_DECL int yylex \
-               (YYSTYPE * yylval_param , yyscan_t yyscanner)
+#define YY_DECL int yylex (YYSTYPE * yylval_param , yyscan_t yyscanner)
 #endif /* !YY_DECL */
 
 /* Code executed at the beginning of each rule, after yytext and yyleng
@@ -1210,56 +1210,75 @@ YY_RULE_SETUP
 #line 106 "lex.l"
 { 
 										char			buffer[1024];
+										char			*current_file_name;
 										char			*s = NULL;
 										char			*b = NULL;
 										char			*f;
 										FILE* 			fh;
 										YARA_CONTEXT* 	context = yyget_extra(yyscanner);
-					
-										*yyextra->lex_buf_ptr = '\0'; // null-terminate included file path
 										
-										// move path of current source file into buffer
-															
-										strncpy(buffer, yr_get_current_file_name(context), sizeof(buffer));
+										if (context->allow_includes)
+										{
+											*yyextra->lex_buf_ptr = '\0'; // null-terminate included file path
+										
+											// move path of current source file into buffer
+											
+											current_file_name = yr_get_current_file_name(context);
+											
+											if (current_file_name != NULL)
+											{				
+												strncpy(buffer, yr_get_current_file_name(context), sizeof(buffer));
+											}
+											else
+											{
+												buffer[0] = '\0';
+											}
 										
-										// make included file path relative to current source file
+											// make included file path relative to current source file
 										
-										s = strrchr(buffer, '/');
+											s = strrchr(buffer, '/');
 										
-										#ifdef WIN32
-										b = strrchr(buffer, '\\'); // in Windows both path delimiters are accepted
-										#endif
+											#ifdef WIN32
+											b = strrchr(buffer, '\\'); // in Windows both path delimiters are accepted
+											#endif
 									
-										if (s != NULL || b != NULL)
-										{
-											f = (b > s)? (b + 1): (s + 1);
+											if (s != NULL || b != NULL)
+											{
+												f = (b > s)? (b + 1): (s + 1);
 											
-											strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
+												strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
 											
-											fh = fopen(buffer, "r");
+												fh = fopen(buffer, "r");
 											
-											// if include file was not found relative to current source file, try to open it
-											// with path as specified by user (maybe user wrote a full path)
+												// if include file was not found relative to current source file, try to open it
+												// with path as specified by user (maybe user wrote a full path)
 											
-											if (fh == NULL) 
+												if (fh == NULL) 
+												{
+													fh = fopen(yyextra->lex_buf, "r");
+												}
+											}
+											else
 											{
 												fh = fopen(yyextra->lex_buf, "r");
 											}
-										}
-										else
-										{
-											fh = fopen(yyextra->lex_buf, "r");
-										}
 	
-										if (fh != NULL)
-										{						
-											yr_push_file_name(context, yyextra->lex_buf);			
-											yypush_buffer_state(yy_create_buffer(fh,YY_BUF_SIZE,yyscanner),yyscanner);										
-										}
-										else
+											if (fh != NULL)
+											{						
+												yr_push_file_name(context, yyextra->lex_buf);			
+												yypush_buffer_state(yy_create_buffer(fh,YY_BUF_SIZE,yyscanner),yyscanner);										
+											}
+											else
+											{
+												snprintf(buffer, sizeof(buffer), "can't open include file: %s", yyextra->lex_buf);
+												yyerror(yyscanner, buffer);
+											}
+											
+										} 
+										else // not allowing includes
 										{
-											snprintf(buffer, sizeof(buffer), "can't open include file: %s", yyextra->lex_buf);
-											yyerror(yyscanner, buffer);
+											yyerror(yyscanner, "includes are disabled");
+											yyterminate();
 										}
 												
 										BEGIN(INITIAL);
@@ -1269,7 +1288,7 @@ case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(str):
 case YY_STATE_EOF(regexp):
 case YY_STATE_EOF(include):
-#line 163 "lex.l"
+#line 182 "lex.l"
 {
 										YARA_CONTEXT* context = yyget_extra(yyscanner);
 						
@@ -1285,7 +1304,7 @@ case YY_STATE_EOF(include):
 	YY_BREAK
 case 46:
 YY_RULE_SETUP
-#line 177 "lex.l"
+#line 196 "lex.l"
 {
 			                       		yylval->c_string = (char*) yr_strdup(yytext);
 			                       		return _STRING_IDENTIFIER_WITH_WILDCARD_;      
@@ -1293,7 +1312,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 47:
 YY_RULE_SETUP
-#line 182 "lex.l"
+#line 201 "lex.l"
 {
 				                       		yylval->c_string = (char*) yr_strdup(yytext);
 				                       		return _STRING_IDENTIFIER_;      
@@ -1301,7 +1320,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 48:
 YY_RULE_SETUP
-#line 188 "lex.l"
+#line 207 "lex.l"
 {	
 				                       		yylval->c_string = (char*) yr_strdup(yytext);
 											yylval->c_string[0] = '$'; 						/* replace # by $*/
@@ -1310,7 +1329,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 49:
 YY_RULE_SETUP
-#line 194 "lex.l"
+#line 213 "lex.l"
 {	
 					                      	yylval->c_string = (char*) yr_strdup(yytext);
 											yylval->c_string[0] = '$'; 						/* replace @ by $*/
@@ -1319,7 +1338,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 50:
 YY_RULE_SETUP
-#line 200 "lex.l"
+#line 219 "lex.l"
 { 
 										if (strlen(yytext) > 128)
 										{
@@ -1332,7 +1351,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 51:
 YY_RULE_SETUP
-#line 210 "lex.l"
+#line 229 "lex.l"
 { 
 										yylval->integer = (unsigned int) atoi(yytext);
 						
@@ -1350,7 +1369,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 52:
 YY_RULE_SETUP
-#line 225 "lex.l"
+#line 244 "lex.l"
 {
 										yylval->integer = xtoi(yytext + 2);
 										return _NUMBER_;
@@ -1358,7 +1377,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 53:
 YY_RULE_SETUP
-#line 230 "lex.l"
+#line 249 "lex.l"
 { 	/* saw closing quote - all done */
 
 										SIZED_STRING* s;
@@ -1385,22 +1404,22 @@ YY_RULE_SETUP
 	YY_BREAK
 case 54:
 YY_RULE_SETUP
-#line 254 "lex.l"
+#line 273 "lex.l"
 { *yyextra->lex_buf_ptr++ = '\t'; yyextra->lex_buf_len++; }
 	YY_BREAK
 case 55:
 YY_RULE_SETUP
-#line 255 "lex.l"
+#line 274 "lex.l"
 { *yyextra->lex_buf_ptr++ = '\"'; yyextra->lex_buf_len++; }
 	YY_BREAK
 case 56:
 YY_RULE_SETUP
-#line 256 "lex.l"
+#line 275 "lex.l"
 { *yyextra->lex_buf_ptr++ = '\\'; yyextra->lex_buf_len++; }
 	YY_BREAK
 case 57:
 YY_RULE_SETUP
-#line 258 "lex.l"
+#line 277 "lex.l"
 {
 		        						int result;
 
@@ -1412,7 +1431,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 58:
 YY_RULE_SETUP
-#line 267 "lex.l"
+#line 286 "lex.l"
 {
 										YYTEXT_TO_BUFFER;
 									}
@@ -1420,7 +1439,7 @@ YY_RULE_SETUP
 case 59:
 /* rule 59 can match eol */
 YY_RULE_SETUP
-#line 271 "lex.l"
+#line 290 "lex.l"
 {
 										yyerror(yyscanner, "unterminated string");
 										yyterminate();
@@ -1429,14 +1448,14 @@ YY_RULE_SETUP
 case 60:
 /* rule 60 can match eol */
 YY_RULE_SETUP
-#line 276 "lex.l"
+#line 295 "lex.l"
 {
 										yyerror(yyscanner, "illegal escape sequence");
 									}
 	YY_BREAK
 case 61:
 YY_RULE_SETUP
-#line 281 "lex.l"
+#line 300 "lex.l"
 { 	
 										SIZED_STRING* s;
 
@@ -1461,7 +1480,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 62:
 YY_RULE_SETUP
-#line 303 "lex.l"
+#line 322 "lex.l"
 { 				
 										*yyextra->lex_buf_ptr++ = '/';
 										yyextra->lex_buf_len++ ;
@@ -1469,7 +1488,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 63:
 YY_RULE_SETUP
-#line 308 "lex.l"
+#line 327 "lex.l"
 { 				
 										*yyextra->lex_buf_ptr++ = yytext[0];
 										*yyextra->lex_buf_ptr++ = yytext[1];
@@ -1478,7 +1497,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 64:
 YY_RULE_SETUP
-#line 314 "lex.l"
+#line 333 "lex.l"
 {
 										YYTEXT_TO_BUFFER;
 									}
@@ -1486,7 +1505,7 @@ YY_RULE_SETUP
 case 65:
 /* rule 65 can match eol */
 YY_RULE_SETUP
-#line 318 "lex.l"
+#line 337 "lex.l"
 {
 										yyerror(yyscanner, "unterminated regular expression");
 										yyterminate();
@@ -1494,7 +1513,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 66:
 YY_RULE_SETUP
-#line 323 "lex.l"
+#line 342 "lex.l"
 {
 				 						yyextra->lex_buf_ptr = yyextra->lex_buf; 
 										yyextra->lex_buf_len = 0;
@@ -1503,7 +1522,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 67:
 YY_RULE_SETUP
-#line 329 "lex.l"
+#line 348 "lex.l"
 {
 				 						yyextra->lex_buf_ptr = yyextra->lex_buf; 
 										yyextra->lex_buf_len = 0;
@@ -1512,7 +1531,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 68:
 YY_RULE_SETUP
-#line 335 "lex.l"
+#line 354 "lex.l"
 { 
 										int len = strlen(yytext);
 										
@@ -1530,22 +1549,22 @@ YY_RULE_SETUP
 case 69:
 /* rule 69 can match eol */
 YY_RULE_SETUP
-#line 349 "lex.l"
+#line 368 "lex.l"
 /* skip whitespace */
 	YY_BREAK
 case 70:
 YY_RULE_SETUP
-#line 351 "lex.l"
+#line 370 "lex.l"
 { 
                        					return yytext[0];    
 									}
 	YY_BREAK
 case 71:
 YY_RULE_SETUP
-#line 354 "lex.l"
+#line 373 "lex.l"
 ECHO;
 	YY_BREAK
-#line 1549 "lex.c"
+#line 1568 "lex.c"
 
 	case YY_END_OF_BUFFER:
 		{
@@ -1730,7 +1749,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 
 	else
 		{
-			yy_size_t num_to_read =
+			int num_to_read =
 			YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
 
 		while ( num_to_read <= 0 )
@@ -1744,7 +1763,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 
 			if ( b->yy_is_our_buffer )
 				{
-				yy_size_t new_size = b->yy_buf_size * 2;
+				int new_size = b->yy_buf_size * 2;
 
 				if ( new_size <= 0 )
 					b->yy_buf_size += b->yy_buf_size / 8;
@@ -1799,14 +1818,6 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 	else
 		ret_val = EOB_ACT_CONTINUE_SCAN;
 
-	if ((yy_size_t) (yyg->yy_n_chars + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
-		/* Extend the array by 50%, plus the number we really need. */
-		yy_size_t new_size = yyg->yy_n_chars + number_to_move + (yyg->yy_n_chars >> 1);
-		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ,yyscanner );
-		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
-			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
-	}
-
 	yyg->yy_n_chars += number_to_move;
 	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars] = YY_END_OF_BUFFER_CHAR;
 	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[yyg->yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR;
@@ -1888,7 +1899,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 	if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
 		{ /* need to shift things up to make room */
 		/* +2 for EOB chars. */
-		register yy_size_t number_to_move = yyg->yy_n_chars + 2;
+		register int number_to_move = yyg->yy_n_chars + 2;
 		register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
 					YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
 		register char *source =
@@ -1942,7 +1953,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
 
 		else
 			{ /* need more input */
-			yy_size_t offset = yyg->yy_c_buf_p - yyg->yytext_ptr;
+			int offset = yyg->yy_c_buf_p - yyg->yytext_ptr;
 			++yyg->yy_c_buf_p;
 
 			switch ( yy_get_next_buffer( yyscanner ) )
@@ -2233,7 +2244,7 @@ void yypop_buffer_state (yyscan_t yyscanner)
  */
 static void yyensure_buffer_stack (yyscan_t yyscanner)
 {
-	yy_size_t num_to_alloc;
+	int num_to_alloc;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
 	if (!yyg->yy_buffer_stack) {
@@ -2246,9 +2257,7 @@ static void yyensure_buffer_stack (yyscan_t yyscanner)
 		yyg->yy_buffer_stack = (struct yy_buffer_state**)yyalloc
 								(num_to_alloc * sizeof(struct yy_buffer_state*)
 								, yyscanner);
-		if ( ! yyg->yy_buffer_stack )
-			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
-								  
+		
 		memset(yyg->yy_buffer_stack, 0, num_to_alloc * sizeof(struct yy_buffer_state*));
 				
 		yyg->yy_buffer_stack_max = num_to_alloc;
@@ -2266,8 +2275,6 @@ static void yyensure_buffer_stack (yyscan_t yyscanner)
 								(yyg->yy_buffer_stack,
 								num_to_alloc * sizeof(struct yy_buffer_state*)
 								, yyscanner);
-		if ( ! yyg->yy_buffer_stack )
-			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
 
 		/* zero only the new slots.*/
 		memset(yyg->yy_buffer_stack + yyg->yy_buffer_stack_max, 0, grow_size * sizeof(struct yy_buffer_state*));
@@ -2312,7 +2319,7 @@ YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size , yyscan_t yyscann
 
 /** Setup the input buffer state to scan a string. The next call to yylex() will
  * scan from a @e copy of @a str.
- * @param yystr a NUL-terminated string to scan
+ * @param str a NUL-terminated string to scan
  * @param yyscanner The scanner object.
  * @return the newly allocated buffer state object.
  * @note If you want to scan bytes that may contain NUL values, then use
@@ -2331,11 +2338,12 @@ YY_BUFFER_STATE yy_scan_string (yyconst char * yystr , yyscan_t yyscanner)
  * @param yyscanner The scanner object.
  * @return the newly allocated buffer state object.
  */
-YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, yy_size_t  _yybytes_len , yyscan_t yyscanner)
+YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, int  _yybytes_len , yyscan_t yyscanner)
 {
 	YY_BUFFER_STATE b;
 	char *buf;
-	yy_size_t n, i;
+	yy_size_t n;
+	int i;
     
 	/* Get memory for full buffer, including space for trailing EOB's. */
 	n = _yybytes_len + 2;
@@ -2445,7 +2453,7 @@ FILE *yyget_out  (yyscan_t yyscanner)
 /** Get the length of the current token.
  * @param yyscanner The scanner object.
  */
-yy_size_t yyget_leng  (yyscan_t yyscanner)
+int yyget_leng  (yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
     return yyleng;
@@ -2573,42 +2581,6 @@ int yylex_init(yyscan_t* ptr_yy_globals)
     return yy_init_globals ( *ptr_yy_globals );
 }
 
-/* yylex_init_extra has the same functionality as yylex_init, but follows the
- * convention of taking the scanner as the last argument. Note however, that
- * this is a *pointer* to a scanner, as it will be allocated by this call (and
- * is the reason, too, why this function also must handle its own declaration).
- * The user defined value in the first argument will be available to yyalloc in
- * the yyextra field.
- */
-
-int yylex_init_extra(YY_EXTRA_TYPE yy_user_defined,yyscan_t* ptr_yy_globals )
-
-{
-    struct yyguts_t dummy_yyguts;
-
-    yyset_extra (yy_user_defined, &dummy_yyguts);
-
-    if (ptr_yy_globals == NULL){
-        errno = EINVAL;
-        return 1;
-    }
-	
-    *ptr_yy_globals = (yyscan_t) yyalloc ( sizeof( struct yyguts_t ), &dummy_yyguts );
-	
-    if (*ptr_yy_globals == NULL){
-        errno = ENOMEM;
-        return 1;
-    }
-    
-    /* By setting to 0xAA, we expose bugs in
-    yy_init_globals. Leave at 0x00 for releases. */
-    memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t));
-    
-    yyset_extra (yy_user_defined, *ptr_yy_globals);
-    
-    return yy_init_globals ( *ptr_yy_globals );
-}
-
 static int yy_init_globals (yyscan_t yyscanner)
 {
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
@@ -2720,7 +2692,7 @@ void yyfree (void * ptr , yyscan_t yyscanner)
 
 #define YYTABLES_NAME "yytables"
 
-#line 354 "lex.l"
+#line 373 "lex.l"
 
 
 
diff --git a/libyara/lex.l b/libyara/lex.l
index ffd7143..085c2e3 100644
--- a/libyara/lex.l
+++ b/libyara/lex.l
@@ -105,56 +105,75 @@ include[ \t]+\"						{
 			
 <include>\"		    				{ 
 										char			buffer[1024];
+										char			*current_file_name;
 										char			*s = NULL;
 										char			*b = NULL;
 										char			*f;
 										FILE* 			fh;
 										YARA_CONTEXT* 	context = yyget_extra(yyscanner);
-					
-										*yyextra->lex_buf_ptr = '\0'; // null-terminate included file path
 										
-										// move path of current source file into buffer
-															
-										strncpy(buffer, yr_get_current_file_name(context), sizeof(buffer));
+										if (context->allow_includes)
+										{
+											*yyextra->lex_buf_ptr = '\0'; // null-terminate included file path
+										
+											// move path of current source file into buffer
+											
+											current_file_name = yr_get_current_file_name(context);
+											
+											if (current_file_name != NULL)
+											{				
+												strncpy(buffer, yr_get_current_file_name(context), sizeof(buffer));
+											}
+											else
+											{
+												buffer[0] = '\0';
+											}
 										
-										// make included file path relative to current source file
+											// make included file path relative to current source file
 										
-										s = strrchr(buffer, '/');
+											s = strrchr(buffer, '/');
 										
-										#ifdef WIN32
-										b = strrchr(buffer, '\\'); // in Windows both path delimiters are accepted
-										#endif
+											#ifdef WIN32
+											b = strrchr(buffer, '\\'); // in Windows both path delimiters are accepted
+											#endif
 									
-										if (s != NULL || b != NULL)
-										{
-											f = (b > s)? (b + 1): (s + 1);
+											if (s != NULL || b != NULL)
+											{
+												f = (b > s)? (b + 1): (s + 1);
 											
-											strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
+												strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
 											
-											fh = fopen(buffer, "r");
+												fh = fopen(buffer, "r");
 											
-											// if include file was not found relative to current source file, try to open it
-											// with path as specified by user (maybe user wrote a full path)
+												// if include file was not found relative to current source file, try to open it
+												// with path as specified by user (maybe user wrote a full path)
 											
-											if (fh == NULL) 
+												if (fh == NULL) 
+												{
+													fh = fopen(yyextra->lex_buf, "r");
+												}
+											}
+											else
 											{
 												fh = fopen(yyextra->lex_buf, "r");
 											}
-										}
-										else
-										{
-											fh = fopen(yyextra->lex_buf, "r");
-										}
 	
-										if (fh != NULL)
-										{						
-											yr_push_file_name(context, yyextra->lex_buf);			
-											yypush_buffer_state(yy_create_buffer(fh, YY_BUF_SIZE, yyscanner), yyscanner);										
-										}
-										else
+											if (fh != NULL)
+											{						
+												yr_push_file_name(context, yyextra->lex_buf);			
+												yypush_buffer_state(yy_create_buffer(fh, YY_BUF_SIZE, yyscanner), yyscanner);										
+											}
+											else
+											{
+												snprintf(buffer, sizeof(buffer), "can't open include file: %s", yyextra->lex_buf);
+												yyerror(yyscanner, buffer);
+											}
+											
+										} 
+										else // not allowing includes
 										{
-											snprintf(buffer, sizeof(buffer), "can't open include file: %s", yyextra->lex_buf);
-											yyerror(yyscanner, buffer);
+											yyerror(yyscanner, "includes are disabled");
+											yyterminate();
 										}
 												
 										BEGIN(INITIAL);
diff --git a/libyara/libyara.c b/libyara/libyara.c
index 96a75d9..b20817c 100644
--- a/libyara/libyara.c
+++ b/libyara/libyara.c
@@ -50,6 +50,7 @@ YARA_CONTEXT* yr_create_context()
     context->current_rule_strings = NULL;
     context->inside_for = 0;
 	context->namespaces = NULL;
+    context->allow_includes = TRUE;
 	context->current_namespace = yr_create_namespace(context, "default");
     
     memset(context->hash_table.hashed_strings, 0, sizeof(context->hash_table.hashed_strings));
@@ -138,6 +139,11 @@ void yr_destroy_context(YARA_CONTEXT* context)
 		
 		ns = next_ns;
 	}
+	
+	while (context->file_name_stack_ptr > 0)
+    {
+        yr_pop_file_name(context);
+    }
     
     clear_hash_table(&context->hash_table);
 	yr_free(context);
@@ -180,9 +186,9 @@ void yr_push_file_name(YARA_CONTEXT* context, const char* file_name)
 
 void yr_pop_file_name(YARA_CONTEXT* context)
 {  
-    context->file_name_stack_ptr--;
     if (context->file_name_stack_ptr > 0)
     {
+        context->file_name_stack_ptr--;
         yr_free(context->file_name_stack[context->file_name_stack_ptr]);
         context->file_name_stack[context->file_name_stack_ptr] = NULL;  
     }
diff --git a/libyara/libyara.tmproj b/libyara/libyara.tmproj
index f644a4b..dc4e963 100644
--- a/libyara/libyara.tmproj
+++ b/libyara/libyara.tmproj
@@ -3,7 +3,7 @@
 <plist version="1.0">
 <dict>
 	<key>currentDocument</key>
-	<string>lex.l</string>
+	<string>ast.c</string>
 	<key>documents</key>
 	<array>
 		<dict>
@@ -13,7 +13,7 @@
 					<key>filename</key>
 					<string>scan.c</string>
 					<key>lastUsed</key>
-					<date>2009-10-16T17:14:53Z</date>
+					<date>2009-10-23T14:08:40Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
@@ -31,27 +31,27 @@
 					<key>filename</key>
 					<string>eval.c</string>
 					<key>lastUsed</key>
-					<date>2009-10-18T14:52:55Z</date>
+					<date>2009-10-23T15:50:54Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
 					<string>ast.c</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T14:25:02Z</date>
+					<date>2009-10-23T15:50:54Z</date>
+					<key>selected</key>
+					<true/>
 				</dict>
 				<dict>
 					<key>filename</key>
 					<string>lex.l</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T14:32:32Z</date>
-					<key>selected</key>
-					<true/>
+					<date>2009-10-23T14:30:38Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
 					<string>grammar.y</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T12:13:46Z</date>
+					<date>2009-10-23T14:27:20Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
@@ -69,7 +69,7 @@
 					<key>filename</key>
 					<string>libyara.c</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T14:23:31Z</date>
+					<date>2009-10-23T14:19:39Z</date>
 				</dict>
 			</array>
 			<key>expanded</key>
@@ -84,7 +84,7 @@
 					<key>filename</key>
 					<string>yara.h</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T14:24:58Z</date>
+					<date>2009-10-23T14:08:35Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
@@ -102,19 +102,19 @@
 					<key>filename</key>
 					<string>filemap.h</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T12:07:15Z</date>
+					<date>2009-10-23T14:24:21Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
 					<string>eval.h</string>
 					<key>lastUsed</key>
-					<date>2009-10-18T14:53:35Z</date>
+					<date>2009-10-23T14:20:32Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
 					<string>ast.h</string>
 					<key>lastUsed</key>
-					<date>2009-10-22T12:52:46Z</date>
+					<date>2009-10-23T14:27:23Z</date>
 				</dict>
 				<dict>
 					<key>filename</key>
@@ -148,7 +148,7 @@
 		</dict>
 	</array>
 	<key>fileHierarchyDrawerWidth</key>
-	<integer>155</integer>
+	<integer>141</integer>
 	<key>metaData</key>
 	<dict>
 		<key>../yara.c</key>
@@ -170,42 +170,58 @@
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>22</integer>
+				<integer>20</integer>
 				<key>line</key>
-				<integer>105</integer>
+				<integer>80</integer>
 			</dict>
+			<key>columnSelection</key>
+			<false/>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>86</integer>
+			<integer>70</integer>
+			<key>selectFrom</key>
+			<dict>
+				<key>column</key>
+				<integer>4</integer>
+				<key>line</key>
+				<integer>80</integer>
+			</dict>
+			<key>selectTo</key>
+			<dict>
+				<key>column</key>
+				<integer>20</integer>
+				<key>line</key>
+				<integer>80</integer>
+			</dict>
 		</dict>
 		<key>ast.h</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>22</integer>
+				<integer>55</integer>
 				<key>line</key>
-				<integer>127</integer>
+				<integer>74</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>11</integer>
+			<integer>40</integer>
 		</dict>
 		<key>eval.c</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>8</integer>
+				<integer>0</integer>
 				<key>line</key>
-				<integer>228</integer>
+				<integer>124</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>0</integer>
+			<integer>272</integer>
 		</dict>
 		<key>eval.h</key>
 		<dict>
@@ -219,7 +235,7 @@
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>0</integer>
+			<integer>1</integer>
 		</dict>
 		<key>filemap.c</key>
 		<dict>
@@ -247,7 +263,7 @@
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>0</integer>
+			<integer>5</integer>
 		</dict>
 		<key>grammar.y</key>
 		<dict>
@@ -261,7 +277,7 @@
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>532</integer>
+			<integer>334</integer>
 		</dict>
 		<key>lex.h</key>
 		<dict>
@@ -289,7 +305,7 @@
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>7</integer>
+			<integer>76</integer>
 		</dict>
 		<key>libyara.c</key>
 		<dict>
@@ -305,11 +321,11 @@
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>116</integer>
+			<integer>140</integer>
 			<key>selectFrom</key>
 			<dict>
 				<key>column</key>
-				<integer>44</integer>
+				<integer>2</integer>
 				<key>line</key>
 				<integer>151</integer>
 			</dict>
@@ -454,14 +470,14 @@
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>47</integer>
+				<integer>0</integer>
 				<key>line</key>
 				<integer>228</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>184</integer>
+			<integer>218</integer>
 		</dict>
 	</dict>
 	<key>openDocuments</key>
@@ -490,6 +506,6 @@
 	<key>showFileHierarchyDrawer</key>
 	<true/>
 	<key>windowFrame</key>
-	<string>{{193, 108}, {1682, 1055}}</string>
+	<string>{{158, 51}, {1112, 727}}</string>
 </dict>
 </plist>
diff --git a/libyara/yara.h b/libyara/yara.h
index c511907..609f463 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -222,6 +222,7 @@ typedef struct _YARA_CONTEXT
     char*		    lex_buf_ptr;
     unsigned short  lex_buf_len;
     
+    int             allow_includes;
     char            include_base_dir[MAX_PATH];
 
 } YARA_CONTEXT;
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 5b31e71..19afaad 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -260,7 +260,7 @@ static PyTypeObject Rules_Type = {
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-static PyObject * Rules_new_from_file(FILE* file, const char* namespace, PyObject* rules)
+static PyObject * Rules_new_from_file(FILE* file, const char* filepath, const char* namespace, PyObject* rules, int allow_includes)
 { 
     YARA_CONTEXT* context;
     Rules* result;
@@ -290,6 +290,13 @@ static PyObject * Rules_new_from_file(FILE* file, const char* namespace, PyObjec
 	{
 		context->current_namespace = yr_create_namespace(context, namespace);
 	}
+	
+	if (filepath != NULL)
+	{
+        yr_push_file_name(context, filepath);
+	}
+	
+	context->allow_includes = allow_includes;
          
     errors = yr_compile_file(file, context);
        
@@ -320,7 +327,7 @@ static PyObject * Rules_new_from_file(FILE* file, const char* namespace, PyObjec
 }
 
 
-static PyObject * Rules_new_from_string(const char* string, const char* namespace, PyObject* rules)
+static PyObject * Rules_new_from_string(const char* string, const char* namespace, PyObject* rules, int allow_includes)
 { 
 	YARA_CONTEXT* context;
 	Rules* result;
@@ -346,6 +353,8 @@ static PyObject * Rules_new_from_string(const char* string, const char* namespac
 		context->current_namespace = yr_create_namespace(context, namespace);
 	}
 	
+    context->allow_includes = allow_includes;
+	
     errors = yr_compile_string(string, context);
        
     if (errors)   /* errors during compilation */
@@ -517,7 +526,7 @@ static PyObject * Rules_getattro(PyObject *self, PyObject *name)
 
 static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keywords)
 { 
-    static char *kwlist[] = {"filepath", "source", "file", "filepaths", "sources", NULL};
+    static char *kwlist[] = {"filepath", "source", "file", "filepaths", "sources", "includes", NULL};
     
     FILE* fh;
     
@@ -526,6 +535,7 @@ static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keyword
 	
 	PyObject *sources_dict = NULL;
 	PyObject *filepaths_dict = NULL;
+	PyObject *includes = NULL;
 	
 	PyObject *key, *value;
 	
@@ -534,16 +544,30 @@ static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keyword
     char* filepath = NULL;
     char* source = NULL;
 	char* namespace = NULL;
+	
+    int allow_includes = TRUE;
     
-    if (PyArg_ParseTupleAndKeywords(args, keywords, "|ssOOO", kwlist, &filepath, &source, &file, &filepaths_dict, &sources_dict))
+    if (PyArg_ParseTupleAndKeywords(args, keywords, "|ssOOOO", kwlist, &filepath, &source, &file, &filepaths_dict, &sources_dict, &includes))
     {
+        if (includes != NULL)
+        {
+            if (PyBool_Check(includes))
+            {
+                allow_includes = (PyObject_IsTrue(includes) == 1);  // PyObject_IsTrue can return -1 in case of error
+            }
+            else
+            {
+                result = PyErr_Format(PyExc_TypeError, "'includes' param must be of boolean type");
+            }
+        }
+        
         if (filepath != NULL)
         {            
             fh = fopen(filepath, "r");
             
             if (fh != NULL)
             {
-                result = Rules_new_from_file(fh, NULL, NULL);
+                result = Rules_new_from_file(fh, filepath, NULL, NULL, allow_includes);
                 fclose(fh);
             }
             else
@@ -553,12 +577,12 @@ static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keyword
         }
         else if (source != NULL)
         {
-            result = Rules_new_from_string(source, NULL, NULL);
+            result = Rules_new_from_string(source, NULL, NULL, allow_includes);
         }
         else if (file != NULL)
         {
             fh = PyFile_AsFile(file);   
-            result = Rules_new_from_file(fh, NULL, NULL);
+            result = Rules_new_from_file(fh, NULL, NULL, NULL, allow_includes);
         }
         else if (sources_dict != NULL)
         {
@@ -571,18 +595,18 @@ static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keyword
 					
 					if (source != NULL && namespace != NULL)
 					{
-						result = Rules_new_from_string(source, namespace, result);
+						result = Rules_new_from_string(source, namespace, result, allow_includes);
 					}
 					else
 					{
-						result = PyErr_Format(PyExc_TypeError, "keys and values of the sources dictionary must be of string type");
+						result = PyErr_Format(PyExc_TypeError, "keys and values of the 'sources' dictionary must be of string type");
 						break;
 					}
 				}
 			}
 			else
 			{
-				result = PyErr_Format(PyExc_TypeError, "sources must be a dictionary");
+				result = PyErr_Format(PyExc_TypeError, "'sources' must be a dictionary");
 			}
         }
         else if (filepaths_dict != NULL)
@@ -600,7 +624,7 @@ static PyObject * yara_compile(PyObject *self, PyObject *args, PyObject *keyword
             
             			if (fh != NULL)
             			{
-                			result = Rules_new_from_file(fh, namespace, result);
+                			result = Rules_new_from_file(fh, filepath, namespace, result, allow_includes);
                 			fclose(fh);
             			}
             			else

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