novi upstream verzije 2.8.3
[ossec-hids.git] / src / os_regex / os_regex_compile.c
index 10ae154..d961b2a 100755 (executable)
  * 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;
@@ -90,26 +90,6 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
         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++;
 
@@ -157,7 +140,7 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
          */
         if(!(flags & OS_CASE_SENSITIVE))
         {
-            *pt = charmap[(uchar)*pt];
+            *pt = (char) charmap[(uchar)*pt];
         }
 
         if(*pt == OR)
@@ -184,15 +167,22 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
 
     /* 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;
@@ -201,13 +191,6 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
     }
 
 
-    /* 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++)
     {
@@ -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;
 
 
@@ -299,8 +282,8 @@ int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
                 }
 
                 /* 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,7 +328,7 @@ 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;