Debian Codegraph @

src/OVAL/results/oval_cmp_basic.c

raw symbols SCIP
1 /*
2 * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Tomas Heinrich <[email protected]>
21 * Šimon Lukašík
22 */
23
24 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
25 #include <config.h>cxx . . $ `<file>/build/config.h`/
26 #endif
27
28 #include <math.h>
29 #include <string.h>
30
31 #include "oval_types.h"cxx . . $ `<file>/src/OVAL/public/oval_types.h`/
32 #include "common/_error.h"cxx . . $ `<file>/src/common/_error.h`/
33 #include "common/oscap_pcre.h"cxx . . $ `<file>/src/common/oscap_pcre.h`/
34 #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
35 #include "oval_cmp_basic_impl.h"cxx . . $ `<file>/src/OVAL/results/oval_cmp_basic_impl.h`/
36
37 oval_result_tcxx . . $ oval_result_t# oval_boolean_cmpcxx . . $ oval_boolean_cmp(56d242478234f315).(const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! state, const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! syschar, oval_operation_tcxx . . $ oval_operation_t# operation)
38 {
39 switch (operation) {
40 case OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.:
41 return state == syschar ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
42 case OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.:
43 return state != syschar ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
44 default:
45 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OVALcxx . . $ `src/common/public/oscap_error.h:61:9`!, "Invalid type of operation in boolean evaluation: %d.", operation);
46 }
47 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
48 }
49
50 oval_result_tcxx . . $ oval_result_t# oval_int_cmpcxx . . $ oval_int_cmp(9cddff872de8d216).(const intmax_tcxx . . $ intmax_t# state, const intmax_tcxx . . $ intmax_t# syschar, oval_operation_tcxx . . $ oval_operation_t# operation)
51 {
52 switch (operation) {
53 case OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.:
54 return state == syschar ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
55 case OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.:
56 return state != syschar ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
57 case OVAL_OPERATION_GREATER_THANcxx . . $ OVAL_OPERATION_GREATER_THAN.:
58 return syschar > state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
59 case OVAL_OPERATION_GREATER_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_GREATER_THAN_OR_EQUAL.:
60 return syschar >= state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
61 case OVAL_OPERATION_LESS_THANcxx . . $ OVAL_OPERATION_LESS_THAN.:
62 return syschar < state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
63 case OVAL_OPERATION_LESS_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_LESS_THAN_OR_EQUAL.:
64 return syschar <= state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
65 case OVAL_OPERATION_BITWISE_ANDcxx . . $ OVAL_OPERATION_BITWISE_AND.:
66 return (syschar & state) == state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
67 case OVAL_OPERATION_BITWISE_ORcxx . . $ OVAL_OPERATION_BITWISE_OR.:
68 return (syschar | state) == state ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
69 default:
70 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OVALcxx . . $ `src/common/public/oscap_error.h:61:9`!, "Invalid type of operation in integer evaluation: %d.", operation);
71 }
72 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
73 }
74
75 static inline int cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(double a, double b)
76 {
77 double relative_err;
78 int r;
79
80 if (a == b)
81 return 0;
82 r = (a > b) ? 1 : -1;
83 if (fabs(a) > fabs(b)) {
84 relative_err = fabs((a - b) / a);
85 } else {
86 relative_err = fabs((a - b) / b);
87 }
88 if (relative_err <= 0.000000001)
89 return 0;
90
91 return r;
92 }
93
94 oval_result_tcxx . . $ oval_result_t# oval_float_cmpcxx . . $ oval_float_cmp(fdbee3ef524f2c1c).(const double state_val, const double sys_val, oval_operation_tcxx . . $ oval_operation_t# operation)
95 {
96 if (operation == OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.) {
97 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) == 0) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
98 } else if (operation == OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.) {
99 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) != 0) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
100 } else if (operation == OVAL_OPERATION_GREATER_THANcxx . . $ OVAL_OPERATION_GREATER_THAN.) {
101 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) == 1) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
102 } else if (operation == OVAL_OPERATION_GREATER_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_GREATER_THAN_OR_EQUAL.) {
103 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) >= 0) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
104 } else if (operation == OVAL_OPERATION_LESS_THANcxx . . $ OVAL_OPERATION_LESS_THAN.) {
105 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) == -1) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
106 } else if (operation == OVAL_OPERATION_LESS_THAN_OR_EQUALcxx . . $ OVAL_OPERATION_LESS_THAN_OR_EQUAL.) {
107 return ((cmp_floatcxx . . $ cmp_float(1a35796978658aa4).(sys_val, state_val) <= 0) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.);
108 } else {
109 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OVALcxx . . $ `src/common/public/oscap_error.h:61:9`!, "Invalid type of operation in float evaluation: %s.", oval_operation_get_text(operation));
110 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
111 }
112 }
113
114 static int istrcmpcxx . . $ istrcmp(5ae29d726ae57f88).(const char *st1, const char *st2)
115 {
116 if (st1 == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
117 st1 = "";
118 if (st2 == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)
119 st2 = "";
120 return oscap_strcasecmpcxx . . $ oscap_strcasecmp(5ae29d726ae57f88).(st1, st2);
121 }
122
123 static oval_result_tcxx . . $ oval_result_t# strregcompcxx . . $ strregcomp(773f45be5a39542f).(const char *pattern, const char *test_str)
124 {
125 int ret;
126 oval_result_tcxx . . $ oval_result_t# result = OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
127 oscap_pcre_tcxx . . $ oscap_pcre_t# *re;
128 char *err;
129 int errofs;
130
131 re = oscap_pcre_compilecxx . . $ oscap_pcre_compile(29ca11f2d36a957a).(pattern, OSCAP_PCRE_OPTS_UTF8cxx . . $ OSCAP_PCRE_OPTS_UTF8., &err, &errofs);
132 if (re == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
133 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to compile regex pattern '%s', "
134 "oscap_pcre_compile() returned error (offset: %d): '%s'.\n", pattern, errofs, err);
135 oscap_pcre_err_freecxx . . $ oscap_pcre_err_free(fda57c8427c60377).(err);
136 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
137 }
138
139 ret = oscap_pcre_execcxx . . $ oscap_pcre_exec(7630dfd25fd504ec).(re, test_str, strlencxx . . $ strlen(751346f8406fd082).(test_str), 0, 0, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, 0);
140 if (ret > OSCAP_PCRE_ERR_NOMATCHcxx . . $ OSCAP_PCRE_ERR_NOMATCH. ) {
141 result = OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
142 } else if (ret == OSCAP_PCRE_ERR_NOMATCHcxx . . $ OSCAP_PCRE_ERR_NOMATCH.) {
143 result = OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
144 } else {
145 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to match regex pattern '%s' on string '%s', "
146 "oscap_pcre_exec() returned error: %d.\n", pattern, test_str, ret);
147 result = OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
148 }
149
150 oscap_pcre_freecxx . . $ oscap_pcre_free(804bb72472b0846e).(re);
151 return result;
152 }
153
154 oval_result_tcxx . . $ oval_result_t# oval_string_cmpcxx . . $ oval_string_cmp(1cc2eda1f1bc9b56).(const char *state, const char *syschar, oval_operation_tcxx . . $ oval_operation_t# operation)
155 {
156 syschar = syschar ? syschar : "";
157 switch (operation) {
158 case OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.:
159 return oscap_strcmpcxx . . $ oscap_strcmp(5ae29d726ae57f88).(state, syschar) ? OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE. : OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
160 case OVAL_OPERATION_CASE_INSENSITIVE_EQUALScxx . . $ OVAL_OPERATION_CASE_INSENSITIVE_EQUALS.:
161 return istrcmpcxx . . $ istrcmp(5ae29d726ae57f88).(state, syschar) ? OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE. : OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.;
162 case OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.:
163 return oscap_strcmpcxx . . $ oscap_strcmp(5ae29d726ae57f88).(state, syschar) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
164 case OVAL_OPERATION_CASE_INSENSITIVE_NOT_EQUALcxx . . $ OVAL_OPERATION_CASE_INSENSITIVE_NOT_EQUAL.:
165 return istrcmpcxx . . $ istrcmp(5ae29d726ae57f88).(state, syschar) ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
166 case OVAL_OPERATION_PATTERN_MATCHcxx . . $ OVAL_OPERATION_PATTERN_MATCH.:
167 return strregcompcxx . . $ strregcomp(773f45be5a39542f).(state, syschar);
168 default:
169 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OVALcxx . . $ `src/common/public/oscap_error.h:61:9`!, "Invalid type of operation in string evaluation: %d.", operation);
170 }
171 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
172 }
173
174 oval_result_tcxx . . $ oval_result_t# oval_binary_cmpcxx . . $ oval_binary_cmp(1cc2eda1f1bc9b56).(const char *state, const char *syschar, oval_operation_tcxx . . $ oval_operation_t# operation)
175 {
176 // I'm going to use case insensitive compare here - don't know if it's necessary
177 switch (operation) {
178 case OVAL_OPERATION_EQUALScxx . . $ OVAL_OPERATION_EQUALS.:
179 return istrcmpcxx . . $ istrcmp(5ae29d726ae57f88).(state, syschar) == 0 ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
180 case OVAL_OPERATION_NOT_EQUALcxx . . $ OVAL_OPERATION_NOT_EQUAL.:
181 return istrcmpcxx . . $ istrcmp(5ae29d726ae57f88).(state, syschar) != 0 ? OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE. : OVAL_RESULT_FALSEcxx . . $ OVAL_RESULT_FALSE.;
182 default:
183 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OVALcxx . . $ `src/common/public/oscap_error.h:61:9`!, "Invalid type of operation in binary evaluation: %d.", operation);
184 }
185 return OVAL_RESULT_ERRORcxx . . $ OVAL_RESULT_ERROR.;
186 }