| 1 | /** |
| 2 | * @file oval_string_map.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 | * Šimon Lukašík |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 32 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 33 | #endif |
| 34 | |
| 35 | #include <string.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | |
| 39 | #include "oval_string_map_impl.h"cxx . . $ `<file>/src/OVAL/adt/oval_string_map_impl.h`/ |
| 40 | #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/ |
| 41 | #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/ |
| 42 | |
| 43 | #if defined(OVAL_STRINGMAP_OLD) |
| 44 | struct _oval_string_map_entry_s; |
| 45 | |
| 46 | typedef struct _oval_string_map_entry { |
| 47 | struct _oval_string_map_entry *next; |
| 48 | char *key; |
| 49 | void *item; |
| 50 | } _oval_string_map_entry_t; |
| 51 | |
| 52 | typedef struct oval_string_map { |
| 53 | struct _oval_string_map_entry *entries; |
| 54 | } oval_string_map_t; |
| 55 | |
| 56 | /* failed - NULL |
| 57 | * success - oval_definition_model |
| 58 | * */ |
| 59 | struct oval_string_map *oval_string_map_new() |
| 60 | { |
| 61 | struct oval_string_map *map = (struct oval_string_map *)malloc(sizeof(oval_string_map_t)); |
| 62 | if (map == NULL) |
| 63 | return NULL; |
| 64 | |
| 65 | map->entries = NULL; |
| 66 | return map; |
| 67 | } |
| 68 | |
| 69 | static struct _oval_string_map_entry *_oval_string_map_entry_new(struct |
| 70 | _oval_string_map_entry |
| 71 | *after, struct |
| 72 | _oval_string_map_entry |
| 73 | *before) |
| 74 | { |
| 75 | struct _oval_string_map_entry *entry = |
| 76 | (struct _oval_string_map_entry *)malloc(sizeof(_oval_string_map_entry_t)); |
| 77 | if (entry == NULL) |
| 78 | return NULL; |
| 79 | |
| 80 | entry->next = before; |
| 81 | if (after != NULL) |
| 82 | after->next = entry; |
| 83 | return entry; |
| 84 | } |
| 85 | |
| 86 | void oval_string_map_put(struct oval_string_map *map, const char *key, void *item) |
| 87 | { |
| 88 | __attribute__nonnull__(map); |
| 89 | |
| 90 | char *temp = (char *)malloc((strlen(key) + 1) * sizeof(char) + 1); |
| 91 | char *usekey = strcpy(temp, key); |
| 92 | |
| 93 | /* SEARCH FOR INSERTION POINT */ |
| 94 | struct _oval_string_map_entry *insert_before = map->entries, *insert_after = NULL, *insertion; |
| 95 | if (insert_before == NULL) { |
| 96 | map->entries = insertion = _oval_string_map_entry_new(NULL, NULL); |
| 97 | } else { |
| 98 | int compare; |
| 99 | while (insert_before != NULL && ((compare = strcmp(usekey, insert_before->key)) < 0)) { |
| 100 | insert_after = insert_before; |
| 101 | insert_before = insert_after->next; |
| 102 | } |
| 103 | if (insert_before == NULL) { |
| 104 | insertion = _oval_string_map_entry_new(insert_after, NULL); |
| 105 | } else if (compare == 0) { |
| 106 | insertion = insert_before; |
| 107 | } else { |
| 108 | insertion = _oval_string_map_entry_new(insert_after, insert_before); |
| 109 | if (insert_after == NULL) |
| 110 | map->entries = insertion; |
| 111 | } |
| 112 | } |
| 113 | if (insertion == NULL) { |
| 114 | free(temp); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | insertion->key = usekey; |
| 119 | insertion->item = item; |
| 120 | } |
| 121 | |
| 122 | void oval_string_map_put_string(struct oval_string_map *map, const char *key, const char *item) |
| 123 | { |
| 124 | char *temp = (char *)malloc((strlen(item) + 1) * sizeof(char) + 1); |
| 125 | char *useval = strcpy(temp, item); |
| 126 | oval_string_map_put(map, key, useval); |
| 127 | } |
| 128 | |
| 129 | struct oval_iterator *oval_string_map_keys(struct oval_string_map *map) |
| 130 | { |
| 131 | __attribute__nonnull__(map); |
| 132 | |
| 133 | struct oval_iterator *iterator = oval_collection_iterator_new(); |
| 134 | struct _oval_string_map_entry *entry = map->entries; |
| 135 | while (entry != NULL) { |
| 136 | oval_collection_iterator_add(iterator, (void *)entry->key); |
| 137 | entry = entry->next; |
| 138 | } |
| 139 | return iterator; |
| 140 | } |
| 141 | |
| 142 | struct oval_iterator *oval_string_map_values(struct oval_string_map *map) |
| 143 | { |
| 144 | __attribute__nonnull__(map); |
| 145 | |
| 146 | struct oval_iterator *iterator = oval_collection_iterator_new(); |
| 147 | struct _oval_string_map_entry *entry = map->entries; |
| 148 | int count; |
| 149 | for (count = 0; entry != NULL; count++) { |
| 150 | oval_collection_iterator_add(iterator, entry->item); |
| 151 | entry = entry->next; |
| 152 | } |
| 153 | return iterator; |
| 154 | } |
| 155 | |
| 156 | void *oval_string_map_get_value(struct oval_string_map *map, const char *key) |
| 157 | { |
| 158 | __attribute__nonnull__(map); |
| 159 | |
| 160 | if (key == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | struct _oval_string_map_entry *entry; |
| 164 | for (entry = map->entries; (entry != NULL) && (strcmp(key, entry->key) != 0); entry = entry->next) { |
| 165 | } |
| 166 | return (entry == NULL) ? NULL : entry->item; |
| 167 | } |
| 168 | |
| 169 | void oval_string_map_free(struct oval_string_map *map, oscap_destruct_func free_func) |
| 170 | { |
| 171 | __attribute__nonnull__(map); |
| 172 | |
| 173 | struct _oval_string_map_entry *entry = map->entries; |
| 174 | struct _oval_string_map_entry *next; |
| 175 | while (entry != NULL) { |
| 176 | if (free_func != NULL) |
| 177 | if (entry->item) |
| 178 | (*free_func) (entry->item); |
| 179 | next = entry->next; |
| 180 | free(entry->key); |
| 181 | entry->item = NULL; |
| 182 | entry->key = NULL; |
| 183 | entry->next = NULL; |
| 184 | free(entry); |
| 185 | entry = next; |
| 186 | } |
| 187 | free(map); |
| 188 | } |
| 189 | |
| 190 | void oval_string_map_free_string(struct oval_string_map *map) |
| 191 | { |
| 192 | oval_string_map_free(map, free); |
| 193 | } |
| 194 | #else |
| 195 | # include "probes/SEAP/generic/rbt/rbt.h"cxx . . $ `<file>/src/OVAL/probes/SEAP/generic/rbt/rbt.h`/ |
| 196 | |
| 197 | struct oval_string_mapcxx . . $ oval_string_map# *oval_string_map_newcxx . . $ oval_string_map_new(c786b134e882bd29).(void) |
| 198 | { |
| 199 | return (struct oval_string_mapcxx . . $ oval_string_map# *)(rbt_str_newcxx . . $ rbt_str_new(557e4aca7c153cb).()); |
| 200 | } |
| 201 | |
| 202 | void oval_string_map_putcxx . . $ oval_string_map_put(95b46de789826acc).(struct oval_string_mapcxx . . $ oval_string_map# *map, const char *key, void *val) |
| 203 | { |
| 204 | char *key_copy; |
| 205 | |
| 206 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || key == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (rbt_str_addcxx . . $ rbt_str_add(d471093ac4c699c2).((rbt_tcxx . . $ rbt_t# *)map, key_copy = strdupcxx . . $ strdup(a8b8e32347182212).(key), val) != 0) { |
| 211 | dDcxx . . $ `src/common/debug_priv.h:64:9`!("rbt_str_add: non-zero return code"); |
| 212 | freecxx . . $ free(5d22bdacc48458e8).(key_copy); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void oval_string_map_put_stringcxx . . $ oval_string_map_put_string(9c717b37723dda1b).(struct oval_string_mapcxx . . $ oval_string_map# *map, const char *key, const char *val) |
| 217 | { |
| 218 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || key == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 219 | return; |
| 220 | } |
| 221 | char *str = strdupcxx . . $ strdup(a8b8e32347182212).(val), *key_copy; |
| 222 | |
| 223 | if (rbt_str_addcxx . . $ rbt_str_add(d471093ac4c699c2).((rbt_tcxx . . $ rbt_t# *)map, key_copy = strdupcxx . . $ strdup(a8b8e32347182212).(key), str) == 0) |
| 224 | return; |
| 225 | else { |
| 226 | freecxx . . $ free(5d22bdacc48458e8).(str); |
| 227 | freecxx . . $ free(5d22bdacc48458e8).(key_copy); |
| 228 | } |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | void *oval_string_map_get_valuecxx . . $ oval_string_map_get_value(c9e9819de236f3d5).(struct oval_string_mapcxx . . $ oval_string_map# *map, const char *key) |
| 233 | { |
| 234 | void *val = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 235 | |
| 236 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || key == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 237 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 238 | } |
| 239 | |
| 240 | if (rbt_str_getcxx . . $ rbt_str_get(3cd4f904f7b02e6c).((rbt_tcxx . . $ rbt_t# *)map, key, &val) != 0) |
| 241 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 242 | else |
| 243 | return (val); |
| 244 | } |
| 245 | |
| 246 | static void __oval_string_map_node_freecxx . . $ __oval_string_map_node_free(5b7a8a44cc1adbfb).(struct rbt_str_nodecxx . . $ rbt_str_node# *n, oscap_destruct_funccxx . . $ oscap_destruct_func# destroy) |
| 247 | { |
| 248 | if (destroy != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 249 | destroy(n->datacxx . . $ rbt_str_node#data.); |
| 250 | freecxx . . $ free(5d22bdacc48458e8).(n->keycxx . . $ rbt_str_node#key.); |
| 251 | } |
| 252 | |
| 253 | void oval_string_map_freecxx . . $ oval_string_map_free(e4310474f4dc2f59).(struct oval_string_mapcxx . . $ oval_string_map# *map, oscap_destruct_funccxx . . $ oscap_destruct_func# destroy) |
| 254 | { |
| 255 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 256 | return; |
| 257 | } |
| 258 | rbt_str_free_cb2cxx . . $ rbt_str_free_cb2(57a8905ddba3f9a3).((rbt_tcxx . . $ rbt_t# *)map, |
| 259 | (void(*)(struct rbt_str_nodecxx . . $ rbt_str_node# *, void *))__oval_string_map_node_freecxx . . $ __oval_string_map_node_free(5b7a8a44cc1adbfb)., |
| 260 | (void *)destroy); |
| 261 | } |
| 262 | |
| 263 | void oval_string_map_free0cxx . . $ oval_string_map_free0(92dc6c01d132bc35).(struct oval_string_mapcxx . . $ oval_string_map# *map) |
| 264 | { |
| 265 | oval_string_map_freecxx . . $ oval_string_map_free(e4310474f4dc2f59).(map, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 266 | } |
| 267 | |
| 268 | void oval_string_map_free_stringcxx . . $ oval_string_map_free_string(92dc6c01d132bc35).(struct oval_string_mapcxx . . $ oval_string_map# *map) |
| 269 | { |
| 270 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 271 | return; |
| 272 | } |
| 273 | oval_string_map_freecxx . . $ oval_string_map_free(e4310474f4dc2f59).(map, freecxx . . $ free(5d22bdacc48458e8).); |
| 274 | } |
| 275 | |
| 276 | static int __oval_iterator_addkeycxx . . $ __oval_iterator_addkey(216f1646fc7fe5da).(struct rbt_str_nodecxx . . $ rbt_str_node# *n, void *u) |
| 277 | { |
| 278 | struct oval_iteratorcxx . . $ oval_iterator# *it = (struct oval_iteratorcxx . . $ oval_iterator# *)u; |
| 279 | |
| 280 | oval_collection_iterator_addcxx . . $ oval_collection_iterator_add(a59eba6ba5efb2ef).(it, (void *)n->keycxx . . $ rbt_str_node#key.); |
| 281 | |
| 282 | return (0); |
| 283 | } |
| 284 | |
| 285 | static int __oval_iterator_addvalcxx . . $ __oval_iterator_addval(216f1646fc7fe5da).(struct rbt_str_nodecxx . . $ rbt_str_node# *n, void *u) |
| 286 | { |
| 287 | struct oval_iteratorcxx . . $ oval_iterator# *it = (struct oval_iteratorcxx . . $ oval_iterator# *)u; |
| 288 | |
| 289 | oval_collection_iterator_addcxx . . $ oval_collection_iterator_add(a59eba6ba5efb2ef).(it, n->datacxx . . $ rbt_str_node#data.); |
| 290 | |
| 291 | return (0); |
| 292 | } |
| 293 | |
| 294 | static int __oval_collection_addvalcxx . . $ __oval_collection_addval(216f1646fc7fe5da).(struct rbt_str_nodecxx . . $ rbt_str_node# *n, void *u) |
| 295 | { |
| 296 | struct oval_collectioncxx . . $ oval_collection# *c = (struct oval_collectioncxx . . $ oval_collection# *)u; |
| 297 | |
| 298 | oval_collection_addcxx . . $ oval_collection_add(3c6bf54730c0284).(c, n->datacxx . . $ rbt_str_node#data.); |
| 299 | |
| 300 | return (0); |
| 301 | } |
| 302 | |
| 303 | struct oval_iteratorcxx . . $ oval_iterator# *oval_string_map_keyscxx . . $ oval_string_map_keys(52dd2feb99c28df0).(struct oval_string_mapcxx . . $ oval_string_map# *map) |
| 304 | { |
| 305 | struct oval_iteratorcxx . . $ oval_iterator# *it; |
| 306 | |
| 307 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 308 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 309 | } |
| 310 | |
| 311 | it = oval_collection_iterator_newcxx . . $ oval_collection_iterator_new(b73131c625ff44d1).(); |
| 312 | rbt_str_walk_inorder2cxx . . $ rbt_str_walk_inorder2(1f9f5f59823fc556).((rbt_tcxx . . $ rbt_t# *)map, __oval_iterator_addkeycxx . . $ __oval_iterator_addkey(216f1646fc7fe5da)., it, 0); |
| 313 | |
| 314 | return (it); |
| 315 | } |
| 316 | |
| 317 | struct oval_iteratorcxx . . $ oval_iterator# *oval_string_map_valuescxx . . $ oval_string_map_values(52dd2feb99c28df0).(struct oval_string_mapcxx . . $ oval_string_map# *map) |
| 318 | { |
| 319 | struct oval_iteratorcxx . . $ oval_iterator# *it; |
| 320 | |
| 321 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 322 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 323 | } |
| 324 | |
| 325 | it = oval_collection_iterator_newcxx . . $ oval_collection_iterator_new(b73131c625ff44d1).(); |
| 326 | rbt_str_walk_inorder2cxx . . $ rbt_str_walk_inorder2(1f9f5f59823fc556).((rbt_tcxx . . $ rbt_t# *)map, __oval_iterator_addvalcxx . . $ __oval_iterator_addval(216f1646fc7fe5da)., it, 0); |
| 327 | |
| 328 | return (it); |
| 329 | } |
| 330 | |
| 331 | struct oval_collectioncxx . . $ oval_collection# *oval_string_map_collect_valuescxx . . $ oval_string_map_collect_values(5121c6e4df28049d).(struct oval_string_mapcxx . . $ oval_string_map# *map, struct oval_collectioncxx . . $ oval_collection# *collection) |
| 332 | { |
| 333 | if (map == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 334 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 335 | } |
| 336 | |
| 337 | if (collection == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 338 | collection = oval_collection_newcxx . . $ oval_collection_new(18476da3f812812d).(); |
| 339 | rbt_str_walk_inorder2cxx . . $ rbt_str_walk_inorder2(1f9f5f59823fc556).((rbt_tcxx . . $ rbt_t# *)map, __oval_collection_addvalcxx . . $ __oval_collection_addval(216f1646fc7fe5da)., collection, 0); |
| 340 | |
| 341 | return (collection); |
| 342 | } |
| 343 | |
| 344 | #endif /* OVAL_STRINGMAP_LINEAR */ |