dodan override za lintian
[ossec-hids.git] / src / os_regex / os_regex_compile.c
index c5f1ea5..d961b2a 100755 (executable)
@@ -5,7 +5,7 @@
  *
  * This program is a free software; you can redistribute it
  * and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
  * Foundation
  */
 
  * Returns 1 on success or 0 on error.
  * The error code is set on reg->error.
  */
-int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
+int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags)
 {
-    int i = 0;
-    int count = 0;
+    size_t i = 0;
+    size_t count = 0;
     int end_of_string = 0;
     int parenthesis = 0;
-    int prts_size = 0;
-    int max_prts_size = 0;
-    
+    unsigned prts_size = 0;
+    unsigned max_prts_size = 0;
+
     char *pt;
     char *new_str;
     char *new_str_free = NULL;
 
-    
+
     /* Checking for references not initialized */
     if(reg == NULL)
     {
         return(0);
     }
-    
+
 
     /* Initializing OSRegex structure */
     reg->error = 0;
@@ -71,8 +71,8 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
         reg->error = OS_REGEX_MAXSIZE;
         goto compile_error;
     }
-    
-    
+
+
     /* Duping the pattern for our internal work */
     new_str = strdup(pattern);
     if(!new_str)
@@ -82,34 +82,14 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
     }
     new_str_free = new_str;
     pt = new_str;
-    
-    
+
+
     /* Getting the number of sub patterns */
     do
     {
         if(*pt == BACKSLASH)
         {
             pt++;
-            if(!((*pt == 'w') ||
-                 (*pt == 'W') || 
-                 (*pt == 's') || 
-                 (*pt == 'S') || 
-                 (*pt == 'd') || 
-                 (*pt == 'D') || 
-                 (*pt == '.') || 
-                 (*pt == '(') ||
-                 (*pt == ')') ||
-                 (*pt == 'p') ||
-                 (*pt == 't') ||
-                 (*pt == '$') ||
-                 (*pt == '|') ||
-                 (*pt == '<') ||
-                 (*pt == '\\')))
-            {
-                reg->error = OS_REGEX_BADREGEX;
-                goto compile_error;
-            }
-
             /* Giving the new values for each regex */
             switch(*pt)
             {
@@ -128,6 +108,9 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
                 case '$': *pt = 13;break;
                 case '|': *pt = 14;break;
                 case '<': *pt = 15;break;
+                default:
+                    reg->error = OS_REGEX_BADREGEX;
+                    goto compile_error;
             }
             pt++;
 
@@ -144,22 +127,22 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
             parenthesis--;
             prts_size++;
         }
-        
+
         /* We only allow one level of parenthesis */
         if(parenthesis != 0 && parenthesis != 1)
         {
             reg->error = OS_REGEX_BADPARENTHESIS;
             goto compile_error;
         }
-        
-        /* The pattern must be always lower case if 
+
+        /* The pattern must be always lower case if
          * case sensitive is set
          */
         if(!(flags & OS_CASE_SENSITIVE))
         {
-            *pt = charmap[(uchar)*pt];
+            *pt = (char) charmap[(uchar)*pt];
         }
-        
+
         if(*pt == OR)
         {
             /* Each sub pattern must be closed on parenthesis */
@@ -170,9 +153,9 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
             }
             count++;
         }
-        pt++;    
+        pt++;
     }while(*pt != '\0');
-    
+
 
     /* After the whole pattern is read, the parenthesis must all be closed */
     if(parenthesis != 0)
@@ -180,33 +163,33 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
         reg->error = OS_REGEX_BADPARENTHESIS;
         goto compile_error;
     }
-    
-                                                                     
+
+
     /* Allocating the memory for the sub patterns */
     count++;
-    reg->patterns = calloc(count +1, sizeof(char *));
-    reg->flags = calloc(count +1, sizeof(int));
-    
-    
+    reg->patterns = (char **) calloc(count +1, sizeof(char *));
+    reg->flags = (int *) calloc(count +1, sizeof(int));
+
+    /* Memory allocation error check */
+    if(!reg->patterns || !reg->flags)
+    {
+        reg->error = OS_REGEX_OUTOFMEMORY;
+        goto compile_error;
+    }
+
+
     /* For the substrings */
     if((prts_size > 0) && (flags & OS_RETURN_SUBSTRING))
     {
-        reg->prts_closure = calloc(count +1, sizeof(char **));
-        reg->prts_str = calloc(count +1, sizeof(char **));
+        reg->prts_closure = (const char ***) calloc(count +1, sizeof(const char **));
+        reg->prts_str = (const char ***) calloc(count +1, sizeof(const char **));
         if(!reg->prts_closure || !reg->prts_str)
         {
             reg->error = OS_REGEX_OUTOFMEMORY;
             goto compile_error;
         }
     }
-    
-    
-    /* Memory allocation error check */
-    if(!reg->patterns || !reg->flags)
-    {
-        reg->error = OS_REGEX_OUTOFMEMORY;
-        goto compile_error;
-    }
+
 
     /* Initializing each sub pattern */
     for(i = 0; i<=count; i++)
@@ -222,12 +205,12 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
         }
     }
     i = 0;
-    
-    
+
+
     /* Reassigning pt to the beginning of the string */
     pt = new_str;
 
-    
+
     /* Getting the sub patterns */
     do
     {
@@ -268,7 +251,7 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
             /* The parenthesis closure if set */
             if(reg->prts_closure)
             {
-                int tmp_int = 0;
+                unsigned tmp_int = 0;
                 char *tmp_str;
 
 
@@ -297,10 +280,10 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
                 {
                     max_prts_size = prts_size;
                 }
-                
+
                 /* Allocating the memory */
-                reg->prts_closure[i] = calloc(prts_size + 1, sizeof(char *));
-                reg->prts_str[i] = calloc(prts_size + 1, sizeof(char *));
+                reg->prts_closure[i] = (const char **) calloc(prts_size + 1, sizeof(const char *));
+                reg->prts_str[i] = (const char **) calloc(prts_size + 1, sizeof(const char *));
                 if((reg->prts_closure[i] == NULL)||(reg->prts_str[i] == NULL))
                 {
                     reg->error = OS_REGEX_OUTOFMEMORY;
@@ -345,26 +328,26 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
     }while(!end_of_string);
 
     /* Allocating sub string for the maximum number of parenthesis */
-    reg->sub_strings = calloc(max_prts_size + 1, sizeof(char *));
+    reg->sub_strings = (char **) calloc(max_prts_size + 1, sizeof(char *));
     if(reg->sub_strings == NULL)
     {
         reg->error = OS_REGEX_OUTOFMEMORY;
         goto compile_error;
     }
-    
+
     /* Success return */
     free(new_str_free);
     return(1);
-    
-    
+
+
     /* Error handling */
     compile_error:
-    
+
     if(new_str_free)
     {
         free(new_str_free);
     }
-    
+
     OSRegex_FreePattern(reg);
 
     return(0);