forked from axoflow/axosyslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr-regexp.c
138 lines (117 loc) · 3.8 KB
/
expr-regexp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2024 Attila Szakacs
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filterx/expr-regexp.h"
#include "filterx/object-primitive.h"
#include "filterx/object-extractor.h"
#include "filterx/object-string.h"
#include "filterx/object-list-interface.h"
#include "filterx/object-dict-interface.h"
#include "filterx/expr-function.h"
#include "filterx/filterx-object-istype.h"
#include "filterx/filterx-ref.h"
#include "compat/pcre.h"
#include "scratch-buffers.h"
#include "filterx/expr-regexp-common.h"
typedef struct FilterXExprRegexpMatch_
{
FilterXExpr super;
FilterXExpr *lhs;
pcre2_code_8 *pattern;
gboolean invert;
} FilterXExprRegexpMatch;
static FilterXObject *
_regexp_match_eval(FilterXExpr *s)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *) s;
FilterXObject *result = NULL;
FilterXReMatchState state;
filterx_expr_rematch_state_init(&state);
gboolean matched = filterx_regexp_match_eval(self->lhs, self->pattern, &state);
if (!state.match_data)
{
/* Error happened during matching. */
goto exit;
}
result = filterx_boolean_new(matched != self->invert);
exit:
filterx_expr_rematch_state_cleanup(&state);
return result;
}
static FilterXExpr *
_regexp_match_optimize(FilterXExpr *s)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *) s;
self->lhs = filterx_expr_optimize(self->lhs);
return NULL;
}
static gboolean
_regexp_match_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *) s;
if (!filterx_expr_init(self->lhs, cfg))
return FALSE;
return filterx_expr_init_method(s, cfg);
}
static void
_regexp_match_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *) s;
filterx_expr_deinit(self->lhs, cfg);
filterx_expr_deinit_method(s, cfg);
}
static void
_regexp_match_free(FilterXExpr *s)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *) s;
filterx_expr_unref(self->lhs);
if (self->pattern)
pcre2_code_free(self->pattern);
filterx_expr_free_method(s);
}
/* Takes reference of lhs */
FilterXExpr *
filterx_expr_regexp_match_new(FilterXExpr *lhs, const gchar *pattern)
{
FilterXExprRegexpMatch *self = g_new0(FilterXExprRegexpMatch, 1);
filterx_expr_init_instance(&self->super, "regexp_match");
self->super.eval = _regexp_match_eval;
self->super.optimize = _regexp_match_optimize;
self->super.init = _regexp_match_init;
self->super.deinit = _regexp_match_deinit;
self->super.free_fn = _regexp_match_free;
self->lhs = lhs;
self->pattern = filterx_regexp_compile_pattern_defaults(pattern);
if (!self->pattern)
{
filterx_expr_unref(&self->super);
return NULL;
}
return &self->super;
}
FilterXExpr *
filterx_expr_regexp_nomatch_new(FilterXExpr *lhs, const gchar *pattern)
{
FilterXExprRegexpMatch *self = (FilterXExprRegexpMatch *)filterx_expr_regexp_match_new(lhs, pattern);
self->invert = TRUE;
return &self->super;
}