| 1 | /** |
| 2 | * @file oval_value.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * "David Niemoller" <[email protected]> |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <stdbool.h> |
| 38 | |
| 39 | #include "oval_definitions_impl.h" |
| 40 | #include "adt/oval_collection_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | #include "common/elements.h" |
| 44 | |
| 45 | typedef struct oval_value { |
| 46 | oval_datatype_t datatype; |
| 47 | char *text; |
| 48 | } oval_value_t; |
| 49 | |
| 50 | bool oval_value_iterator_has_more(struct oval_value_iterator *oc_value) |
| 51 | { |
| 52 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 53 | oc_value); |
| 54 | } |
| 55 | |
| 56 | struct oval_value *oval_value_iterator_next(struct oval_value_iterator |
| 57 | *oc_value) |
| 58 | { |
| 59 | return (struct oval_value *) |
| 60 | oval_collection_iterator_next((struct oval_iterator *)oc_value); |
| 61 | } |
| 62 | |
| 63 | void oval_value_iterator_free(struct oval_value_iterator |
| 64 | *oc_value) |
| 65 | { |
| 66 | oval_collection_iterator_free((struct oval_iterator *)oc_value); |
| 67 | } |
| 68 | |
| 69 | int oval_value_iterator_remaining(struct oval_value_iterator *iterator) |
| 70 | { |
| 71 | return oval_collection_iterator_remaining((struct oval_iterator *)iterator); |
| 72 | } |
| 73 | |
| 74 | oval_datatype_t oval_value_get_datatype(struct oval_value *value) |
| 75 | { |
| 76 | __attribute__nonnull__(value); |
| 77 | |
| 78 | return (value)->datatype; |
| 79 | } |
| 80 | |
| 81 | char *oval_value_get_text(struct oval_value *value) |
| 82 | { |
| 83 | __attribute__nonnull__(value); |
| 84 | |
| 85 | return value->text; |
| 86 | } |
| 87 | |
| 88 | unsigned char *oval_value_get_binary(struct oval_value *value) |
| 89 | { |
| 90 | return NULL; //TODO: implement oval_value_binary |
| 91 | } |
| 92 | |
| 93 | bool oval_value_get_boolean(struct oval_value * value) |
| 94 | { |
| 95 | __attribute__nonnull__(value); |
| 96 | |
| 97 | if (strcmp("false", (value)->text) == 0 |
| 98 | || strcmp("0", (value)->text) == 0) |
| 99 | return false; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | float oval_value_get_float(struct oval_value *value) |
| 104 | { |
| 105 | __attribute__nonnull__(value); |
| 106 | |
| 107 | char *endptr; |
| 108 | return strtof((const char *)value->text, &endptr); |
| 109 | } |
| 110 | |
| 111 | long long oval_value_get_integer(struct oval_value *value) |
| 112 | { |
| 113 | __attribute__nonnull__(value); |
| 114 | |
| 115 | char *endptr; |
| 116 | return strtoll((const char *)value->text, &endptr, 10); |
| 117 | } |
| 118 | |
| 119 | struct oval_value *oval_value_new(oval_datatype_t datatype, char *text_value) |
| 120 | { |
| 121 | oval_value_t *value = (oval_value_t *) malloc(sizeof(oval_value_t)); |
| 122 | if (value == NULL) |
| 123 | return NULL; |
| 124 | |
| 125 | value->datatype = datatype; |
| 126 | value->text = oscap_strdup(text_value); |
| 127 | return value; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | struct oval_value *oval_value_clone(struct oval_value *old_value) |
| 132 | { |
| 133 | __attribute__nonnull__(old_value); |
| 134 | |
| 135 | struct oval_value *new_value = oval_value_new(old_value->datatype, old_value->text); |
| 136 | return new_value; |
| 137 | } |
| 138 | |
| 139 | void oval_value_free(struct oval_value *value) |
| 140 | { |
| 141 | if (value == NULL) |
| 142 | return; |
| 143 | |
| 144 | free(value->text); |
| 145 | free(value); |
| 146 | } |
| 147 | |
| 148 | int oval_value_cast(struct oval_value *value, oval_datatype_t new_dt) |
| 149 | { |
| 150 | /* |
| 151 | oval_datatype_t old_dt; |
| 152 | |
| 153 | old_dt = oval_value_get_datatype(value); |
| 154 | */ |
| 155 | // todo: attempt a proper cast here |
| 156 | value->datatype = new_dt; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | void oval_value_set_datatype(struct oval_value *value, oval_datatype_t datatype) |
| 162 | { |
| 163 | value->datatype = datatype; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | void oval_value_set_text(struct oval_value *value, char *text) |
| 168 | { |
| 169 | if(value->text!=NULL) |
| 170 | free(value->text); |
| 171 | value->text = oscap_strdup(text); |
| 172 | } |
| 173 | */ |
| 174 | |
| 175 | static void oval_value_parse_tag_consume_text(char *string, void *text) |
| 176 | { |
| 177 | *(char **)text = oscap_strdup(string); |
| 178 | } |
| 179 | |
| 180 | int oval_value_parse_tag(xmlTextReaderPtr reader, |
| 181 | struct oval_parser_context *context, oval_value_consumer consumer, void *user) |
| 182 | { |
| 183 | int return_code; |
| 184 | oval_datatype_t datatype = oval_datatype_parse(reader, "datatype", OVAL_DATATYPE_STRING); |
| 185 | char *text = NULL; |
| 186 | int isNil = oval_parser_boolean_attribute(reader, "xsi:nil", 0); |
| 187 | if (isNil) { |
| 188 | return_code = 0; |
| 189 | } else { |
| 190 | return_code = oscap_parser_text_value(reader, &oval_value_parse_tag_consume_text, &text); |
| 191 | } |
| 192 | struct oval_value *value = oval_value_new(datatype, text ? text : ""); |
| 193 | free(text); |
| 194 | (*consumer) (value, user); |
| 195 | return return_code; |
| 196 | } |
| 197 | |
| 198 | xmlNode *oval_value_to_dom(struct oval_value *value, xmlDoc * doc, xmlNode * parent) { |
| 199 | return NULL; //TODO: implement oval_value_to_dom |
| 200 | } |