Debian Codegraph @

src/OVAL/oval_filter.c

raw symbols SCIP
1 /**
2 * @file oval_filter.c
3 * @brief OVAL filter data type implementation
4 * @author "Tomas Heinrich" <[email protected]>
5 *
6 * @addtogroup OVALDEF
7 * @{
8 */
9 /*
10 * Copyright 2010--2014 Red Hat Inc., Durham, North Carolina.
11 * All Rights Reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * Authors:
28 * "Tomas Heinrich" <[email protected]>
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "common/debug_priv.h"
39 #include "common/elements.h"
40 #include "oval_agent_api_impl.h"
41 #include "oval_definitions_impl.h"
42
43 struct oval_filter {
44 struct oval_definition_model *model;
45 struct oval_state *state;
46 oval_filter_action_t action;
47 };
48
49 struct oval_filter *oval_filter_new(struct oval_definition_model *model)
50 {
51 struct oval_filter *filter;
52
53 filter = (struct oval_filter *) malloc(sizeof (struct oval_filter));
54 if (filter == NULL)
55 return NULL;
56
57 filter->model = model;
58 filter->state = NULL;
59 filter->action = OVAL_FILTER_ACTION_UNKNOWN;
60 return filter;
61 }
62
63 void oval_filter_free(struct oval_filter *filter)
64 {
65 __attribute__nonnull__(filter);
66
67 filter->model = NULL;
68 filter->state = NULL;
69 free(filter);
70 }
71
72 struct oval_filter *oval_filter_clone(struct oval_definition_model *new_model,
73 struct oval_filter *old_filter)
74 {
75 struct oval_filter *new_filter;
76 struct oval_state *ste;
77 oval_filter_action_t fa;
78
79 new_filter = oval_filter_new(new_model);
80 ste = oval_filter_get_state(old_filter);
81 ste = oval_state_clone(new_model, ste);
82 oval_filter_set_state(new_filter, ste);
83 fa = oval_filter_get_filter_action(old_filter);
84 oval_filter_set_filter_action(new_filter, fa);
85
86 return new_filter;
87 }
88
89 bool oval_filter_iterator_has_more(struct oval_filter_iterator *oc_filter)
90 {
91 return oval_collection_iterator_has_more((struct oval_iterator *) oc_filter);
92 }
93
94 struct oval_filter *oval_filter_iterator_next(struct oval_filter_iterator *oc_filter)
95 {
96 return (struct oval_filter *)
97 oval_collection_iterator_next((struct oval_iterator *) oc_filter);
98 }
99
100 void oval_filter_iterator_free(struct oval_filter_iterator *oc_filter)
101 {
102 oval_collection_iterator_free((struct oval_iterator *) oc_filter);
103 }
104
105 struct oval_state *oval_filter_get_state(struct oval_filter *filter)
106 {
107 __attribute__nonnull__(filter);
108
109 return filter->state;
110 }
111
112 oval_filter_action_t oval_filter_get_filter_action(struct oval_filter *filter)
113 {
114 __attribute__nonnull__(filter);
115
116 return filter->action;
117 }
118
119 void oval_filter_set_state(struct oval_filter *filter, struct oval_state *state)
120 {
121 __attribute__nonnull__(filter);
122 filter->state = state;
123 }
124
125 void oval_filter_set_filter_action(struct oval_filter *filter, oval_filter_action_t action)
126 {
127 __attribute__nonnull__(filter);
128 filter->action = action;
129 }
130
131 static void _oval_filter_consume_ste_ref(char *steref, void *user)
132 {
133 struct oval_filter *filter;
134 struct oval_state *ste;
135
136 filter = (struct oval_filter *) user;
137 ste = oval_definition_model_get_new_state(filter->model, steref);
138 oval_filter_set_state(filter, ste);
139 }
140
141 int oval_filter_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context,
142 oval_filter_consumer consumer, void *user)
143 {
144 struct oval_filter *filter;
145 oval_filter_action_t fa;
146 int return_code;
147
148 filter = oval_filter_new(context->definition_model);
149 fa = oval_filter_action_parse(reader, "action", OVAL_FILTER_ACTION_EXCLUDE);
150 oval_filter_set_filter_action(filter, fa);
151 return_code = oscap_parser_text_value(reader, &_oval_filter_consume_ste_ref, filter);
152
153 (*consumer) (filter, user);
154
155 return return_code;
156 }
157
158 xmlNode *oval_filter_to_dom(struct oval_filter *filter, xmlDoc *doc, xmlNode *parent)
159 {
160 struct oval_state *ste;
161 char *ste_id;
162 oval_filter_action_t fact;
163 xmlNs *ns_definitions;
164 xmlNode *filter_node;
165
166 ste = oval_filter_get_state(filter);
167 ste_id = oval_state_get_id(ste);
168 fact = oval_filter_get_filter_action(filter);
169 ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE);
170 filter_node = xmlNewTextChild(parent, ns_definitions, BAD_CAST "filter", BAD_CAST ste_id);
171 if (fact != OVAL_FILTER_ACTION_EXCLUDE)
172 xmlNewProp(filter_node, BAD_CAST "action", BAD_CAST oval_filter_action_get_text(fact));
173
174 return filter_node;
175 }
176
177 /// @}