Imported Upstream version 2.7
[ossec-hids.git] / src / os_regex / os_match.c
1 /*   $OSSEC, os_regex.c, v0.4, 2006/01/02, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "os_regex.h"
17
18
19
20 /** int OS_Match2(char *pattern, char *str) v0.4
21  *
22  *  This function is a wrapper around the compile/execute
23  *  functions. It should only be used when the pattern is
24  *  only going to be used once.
25  *  Returns 1 on success or 0 on failure.
26  */
27 int OS_Match2(char *pattern, char *str)
28 {
29     int r_code = 0;
30     OSMatch reg;
31
32     /* If the compilation failed, we don't need to free anything */
33     if(OSMatch_Compile(pattern, &reg, 0))
34     {
35         if(OSMatch_Execute(str,strlen(str), &reg))
36         {
37             r_code = 1;
38         }
39
40         OSMatch_FreePattern(&reg);
41     }
42
43     return(r_code);
44 }
45
46
47 #ifdef NOTHINGEMPTY
48 /** int OS_Match3(char *pattern, char *str) v2.6
49  *
50  *  This function is used
51  *  to match any values from a delimited string
52  *  e.g. match pattern "abc" from string "123,abc,xyz"
53  */
54 int OS_Match3(char *pattern, char *str, char *delimiter)
55 {
56     int r_code = 0;
57     char *token = NULL;
58     char *dupstr = NULL;
59     char *saveptr = NULL;
60
61     /* debug2("1. str [%s], dupstr [%s], token[%s], delim [%s]", str, dupstr, token, delimiter); */
62
63     os_strdup(str, dupstr);
64     /* debug2("2. str [%s], dupstr [%s], token[%s], delim [%s]", str, dupstr, token, delimiter); */
65
66     token = strtok_r(dupstr, delimiter, &saveptr);
67     /* debug2("3. str [%s], dupstr [%s], token[%s], delim [%s]", str, dupstr, token, delimiter); */
68
69     while (token != NULL)
70     {
71         debug2("Matching [%s] with [%s]", pattern, token);
72         if (!strcmp(pattern, token))
73         {
74             r_code = 1;
75             break;
76         }
77
78         token = strtok_r(NULL, delimiter, &saveptr);
79     }
80
81     /* debug2("4. str [%s], dupstr [%s], token[%s], delim [%s]", str, dupstr, token, delimiter); */
82     free(dupstr);
83     return(r_code);
84 }
85 #endif
86
87
88 /* EOF */