| 1 | /** |
| 2 | * @file oval_reference.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2013 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 | |
| 38 | #include "oval_definitions_impl.h" |
| 39 | #include "adt/oval_collection_impl.h" |
| 40 | #include "common/debug_priv.h" |
| 41 | #include "common/util.h" |
| 42 | |
| 43 | typedef struct oval_reference { |
| 44 | struct oval_definition_model *model; |
| 45 | char *source; |
| 46 | char *id; |
| 47 | char *url; |
| 48 | } oval_reference_t; |
| 49 | |
| 50 | bool oval_reference_iterator_has_more(struct oval_reference_iterator |
| 51 | *oc_reference) |
| 52 | { |
| 53 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 54 | oc_reference); |
| 55 | } |
| 56 | |
| 57 | struct oval_reference *oval_reference_iterator_next(struct |
| 58 | oval_reference_iterator |
| 59 | *oc_reference) |
| 60 | { |
| 61 | return (struct oval_reference *) |
| 62 | oval_collection_iterator_next((struct oval_iterator *)oc_reference); |
| 63 | } |
| 64 | |
| 65 | void oval_reference_iterator_free(struct |
| 66 | oval_reference_iterator |
| 67 | *oc_reference) |
| 68 | { |
| 69 | oval_collection_iterator_free((struct oval_iterator *)oc_reference); |
| 70 | } |
| 71 | |
| 72 | char *oval_reference_get_source(struct oval_reference *ref) |
| 73 | { |
| 74 | __attribute__nonnull__(ref); |
| 75 | |
| 76 | return ((struct oval_reference *)ref)->source; |
| 77 | } |
| 78 | |
| 79 | char *oval_reference_get_id(struct oval_reference *ref) |
| 80 | { |
| 81 | __attribute__nonnull__(ref); |
| 82 | |
| 83 | return ((struct oval_reference *)ref)->id; |
| 84 | } |
| 85 | |
| 86 | char *oval_reference_get_url(struct oval_reference *ref) |
| 87 | { |
| 88 | __attribute__nonnull__(ref); |
| 89 | |
| 90 | return ((struct oval_reference *)ref)->url; |
| 91 | } |
| 92 | |
| 93 | struct oval_reference *oval_reference_new(struct oval_definition_model *model) |
| 94 | { |
| 95 | struct oval_reference *ref = (struct oval_reference *)malloc(sizeof(oval_reference_t)); |
| 96 | if (ref == NULL) |
| 97 | return NULL; |
| 98 | |
| 99 | ref->id = NULL; |
| 100 | ref->source = NULL; |
| 101 | ref->url = NULL; |
| 102 | ref->model = model; |
| 103 | return ref; |
| 104 | } |
| 105 | |
| 106 | struct oval_reference *oval_reference_clone |
| 107 | (struct oval_definition_model *new_model, struct oval_reference *old_reference) { |
| 108 | struct oval_reference *new_reference = oval_reference_new(new_model); |
| 109 | char *id = oval_reference_get_id(old_reference); |
| 110 | oval_reference_set_id(new_reference, id); |
| 111 | char *source = oval_reference_get_source(old_reference); |
| 112 | oval_reference_set_source(new_reference, source); |
| 113 | char *url = oval_reference_get_url(old_reference); |
| 114 | oval_reference_set_url(new_reference, url); |
| 115 | return new_reference; |
| 116 | } |
| 117 | |
| 118 | void oval_reference_free(struct oval_reference *ref) |
| 119 | { |
| 120 | __attribute__nonnull__(ref); |
| 121 | |
| 122 | if (ref->id != NULL) |
| 123 | free(ref->id); |
| 124 | if (ref->source != NULL) |
| 125 | free(ref->source); |
| 126 | if (ref->url != NULL) |
| 127 | free(ref->url); |
| 128 | ref->id = NULL; |
| 129 | ref->source = NULL; |
| 130 | ref->url = NULL; |
| 131 | free(ref); |
| 132 | } |
| 133 | |
| 134 | void oval_reference_set_source(struct oval_reference *ref, char *source) |
| 135 | { |
| 136 | __attribute__nonnull__(ref); |
| 137 | if (ref->source != NULL) |
| 138 | free(ref->source); |
| 139 | ref->source = (source == NULL) ? NULL : oscap_strdup(source); |
| 140 | } |
| 141 | |
| 142 | void oval_reference_set_id(struct oval_reference *ref, char *id) |
| 143 | { |
| 144 | __attribute__nonnull__(ref); |
| 145 | if (ref->id != NULL) |
| 146 | free(ref->id); |
| 147 | ref->id = (id == NULL) ? NULL : oscap_strdup(id); |
| 148 | } |
| 149 | |
| 150 | void oval_reference_set_url(struct oval_reference *ref, char *url) |
| 151 | { |
| 152 | __attribute__nonnull__(ref); |
| 153 | if (ref->url != NULL) |
| 154 | free(ref->url); |
| 155 | ref->url = (url == NULL) ? NULL : oscap_strdup(url); |
| 156 | } |
| 157 | |
| 158 | /*typedef void (*oval_reference_consumer)(struct oval_reference*, void*);*/ |
| 159 | int oval_reference_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, oval_reference_consumer consumer, void *user) |
| 160 | { |
| 161 | __attribute__nonnull__(context); |
| 162 | |
| 163 | struct oval_reference *ref = oval_reference_new(context->definition_model); |
| 164 | char *ref_id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "ref_id"); |
| 165 | if (ref_id != NULL) { |
| 166 | oval_reference_set_id(ref, ref_id); |
| 167 | free(ref_id); |
| 168 | ref_id = NULL; |
| 169 | } |
| 170 | char *ref_url = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "ref_url"); |
| 171 | if (ref_url != NULL) { |
| 172 | oval_reference_set_url(ref, ref_url); |
| 173 | free(ref_url); |
| 174 | ref_url = NULL; |
| 175 | } |
| 176 | char *source = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "source"); |
| 177 | if (source != NULL) { |
| 178 | oval_reference_set_source(ref, source); |
| 179 | free(source); |
| 180 | source = NULL; |
| 181 | } |
| 182 | (*consumer) (ref, user); |
| 183 | return 0; |
| 184 | } |
| 185 |