/src/wazuh/src/shared/regex_op.c
Line | Count | Source |
1 | | /* Copyright (C) 2015, Wazuh Inc. |
2 | | * Copyright (C) 2009 Trend Micro Inc. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This program is free software; you can redistribute it |
6 | | * and/or modify it under the terms of the GNU General Public |
7 | | * License (version 2) as published by the FSF - Free Software |
8 | | * Foundation |
9 | | */ |
10 | | |
11 | | #ifndef WIN32 |
12 | | |
13 | | #include "shared.h" |
14 | | |
15 | | |
16 | | int OS_PRegex(const char *str, const char *regex) |
17 | 0 | { |
18 | 0 | regex_t preg; |
19 | |
|
20 | 0 | if (!str || !regex) { |
21 | 0 | return (0); |
22 | 0 | } |
23 | | |
24 | 0 | if (regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB) != 0) { |
25 | 0 | merror("Posix Regex compile error (%s).", regex); |
26 | 0 | return (0); |
27 | 0 | } |
28 | | |
29 | 0 | if (regexec(&preg, str, 0, NULL, 0) != 0) { |
30 | | /* Didn't match */ |
31 | 0 | regfree(&preg); |
32 | 0 | return (0); |
33 | 0 | } |
34 | | |
35 | 0 | regfree(&preg); |
36 | 0 | return (1); |
37 | 0 | } |
38 | | |
39 | | |
40 | 0 | int w_regexec(const char * pattern, const char * string, size_t nmatch, regmatch_t * pmatch) { |
41 | 0 | regex_t regex; |
42 | 0 | int result; |
43 | |
|
44 | 0 | if (!(pattern && string)) { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | |
48 | 0 | if (regcomp(®ex, pattern, REG_EXTENDED)) { |
49 | 0 | merror("Couldn't compile regular expression '%s'", pattern); |
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | 0 | result = regexec(®ex, string, nmatch, pmatch, 0); |
54 | 0 | regfree(®ex); |
55 | 0 | return !result; |
56 | 0 | } |
57 | | |
58 | 0 | void w_sql_regex(sqlite3_context *context, int argc, sqlite3_value **argv) { |
59 | 0 | char *pattern; |
60 | 0 | char *to_match; |
61 | 0 | regex_t regex; |
62 | 0 | char *error_msg; |
63 | |
|
64 | 0 | if (argc != 2) { |
65 | 0 | sqlite3_result_error(context, "regexp(): invalid arguments.\n", -1); |
66 | 0 | return; |
67 | 0 | } |
68 | | |
69 | 0 | pattern = (char*)sqlite3_value_text(argv[0]); |
70 | 0 | to_match = (char*)sqlite3_value_text(argv[1]); |
71 | |
|
72 | 0 | if (!pattern || !to_match) { |
73 | 0 | if (pattern == to_match) { |
74 | 0 | sqlite3_result_int(context, 1); |
75 | 0 | } else { |
76 | 0 | sqlite3_result_int(context, 0); |
77 | 0 | } |
78 | |
|
79 | 0 | return; |
80 | 0 | } |
81 | | |
82 | 0 | if (regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB)) { |
83 | 0 | os_calloc(OS_SIZE_1024, sizeof(char), error_msg); |
84 | 0 | snprintf(error_msg, OS_SIZE_1024, "regexp(): could not compile '%s'.\n", pattern); |
85 | 0 | sqlite3_result_error(context, error_msg, -1); |
86 | 0 | free(error_msg); |
87 | 0 | return; |
88 | 0 | } |
89 | | |
90 | 0 | sqlite3_result_int(context, !regexec(®ex, to_match , 0, NULL, 0)); |
91 | 0 | regfree(®ex); |
92 | 0 | } |
93 | | |
94 | | #endif /* !WIN32 */ |