new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_regex / os_match_execute.c
old mode 100755 (executable)
new mode 100644 (file)
index 0bc8377..77b5ed6
@@ -1,5 +1,3 @@
-/*   $OSSEC, os_match_execute.c, v0.1, 2006/04/18, Daniel B. Cid$   */
-
 /* Copyright (C) 2009 Trend Micro Inc.
  * All right reserved.
  *
  * Foundation
  */
 
-
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 #include "os_regex.h"
-#include "os_regex_internal.h"
 
-
-/** Internal matching **/
-int _OS_Match(const char *pattern, const char *str, size_t str_len, size_t size)
+/* Compare an already compiled pattern with a not NULL string.
+ * Returns 1 on success or 0 on error.
+ * The error code is set on reg->error.
+ */
+int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg)
 {
-    size_t i = 0,j;
-    const char *pt = pattern;
-
-    if(str_len < size)
-        return(FALSE);
-
-    size = str_len - size;
-
-    /* Look to match the first pattern */
-    do
-    {
-        /* Match */
-        if(charmap[(uchar)str[i]] == *pt)
-        {
-            pt++;
-            j = i+1;
-
-            while(*pt != '\0')
-            {
-                if(str[j] == '\0')
-                    return(FALSE);
-
-                else if(*pt != charmap[(uchar)str[j]])
-                {
-                    pt = pattern;
-                    goto nnext;
-                }
-                j++;pt++;
-            }
-            return(TRUE);
-            nnext:
-            continue;
-        }
-    }while(++i <= size);
-
-    return(FALSE);
+    return reg->exec_function(str, str_len, reg);
 }
 
+int OSMatch_Execute_true(const char *subject, size_t len, OSMatch *match)
+{
+    (void)subject;
+    (void)len;
+    (void)match;
+    return (1);
+}
 
-/** Internal matching **/
-int _os_strncmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, size_t size)
+int OSMatch_Execute_pcre2_match(const char *str, size_t str_len, OSMatch * reg)
 {
-    if(strncasecmp(pattern, str, size) == 0)
-        return(TRUE);
+    int rc = 0;
+
+#ifdef USE_PCRE2_JIT
+    rc = pcre2_jit_match(reg->regex, (PCRE2_SPTR)str, str_len, 0, 0, reg->match_data, NULL);
+#else
+    rc = pcre2_match(reg->regex, (PCRE2_SPTR)str, str_len, 0, 0, reg->match_data, NULL);
+#endif
 
-    return(FALSE);
+    return (rc >= 0);
 }
 
-/** Internal matching **/
-int _os_strcmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
+int OSMatch_Execute_strcmp(const char *subject, size_t len, OSMatch *match)
 {
-    if(strcasecmp(pattern, str) == 0)
-        return(TRUE);
-
-    return(FALSE);
+    //^literal$
+    (void)len;
+    return !strcmp(match->pattern, subject);
 }
 
-int _os_strmatch(__attribute__((unused)) const char *pattern, __attribute__((unused)) const char *str,
-        __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
+int OSMatch_Execute_strncmp(const char *subject, size_t len, OSMatch *match)
 {
-    return(TRUE);
+    //^literal
+    (void)len;
+    return !strncmp(match->pattern, subject, match->pattern_len);
 }
 
-int _os_strstr(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size)
+int OSMatch_Execute_strrcmp(const char *subject, size_t len, OSMatch *match)
 {
-    if(strstr(str, pattern) != NULL)
-    {
-        return(TRUE);
+    // literal$
+    if (len >= match->pattern_len) {
+        return !strcmp(match->pattern, &subject[len - match->pattern_len]);
     }
-    return(FALSE);
+    return (0);
 }
 
-
-/** Internal matching **/
-int _os_strcmp_last(const char *pattern, const char *str, size_t str_len, size_t size)
+int OSMatch_Execute_strcasecmp(const char *subject, size_t len, OSMatch *match)
 {
-    /* Size of the string must be bigger */
-    if(str_len < size)
-        return(FALSE);
-
-    if(strcasecmp(pattern, str + (str_len - size)) == 0)
-        return(TRUE);
-
-    return(FALSE);
+    return (len == match->pattern_len && !strcasecmp(match->pattern, subject));
 }
 
-
-/** int OSMatch_Execute(char *str, int str_len, OSMatch *reg) v0.1
- * Compare an already compiled pattern with
- * a not NULL string.
- * Returns 1 on success or 0 on error.
- * The error code is set on reg->error.
- */
-int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg)
+int OSMatch_Execute_strncasecmp(const char *subject, size_t len, OSMatch *match)
 {
-    short int i = 0;
-
-    /* The string can't be NULL */
-    if(str == NULL)
-    {
-        reg->error = OS_REGEX_STR_NULL;
-        return(0);
-    }
-
+    (void)len;
+    return !strncasecmp(match->pattern, subject, match->pattern_len);
+}
 
-    /* Looping on all sub patterns */
-    while(reg->patterns[i])
-    {
-        if(reg->match_fp[i](reg->patterns[i],
-                            str,
-                            str_len,
-                            reg->size[i]) == TRUE)
-        {
-            return(1);
-        }
-        i++;
+int OSMatch_Execute_strrcasecmp(const char *subject, size_t len, OSMatch *match) {
+    if (len >= match->pattern_len) {
+        return !strcasecmp(match->pattern, &subject[len - match->pattern_len]);
     }
-
-    return(0);
+    return (0);
 }
 
-
-/* EOF */