| 1 | /* |
| 2 | * Copyright 2009 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 | * Lukas Kuklinek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * @file |
| 25 | * @internal |
| 26 | * @{ |
| 27 | */ |
| 28 | #ifndef OSCAP_LIST_ |
| 29 | #define OSCAP_LIST_cxx . . $ `src/common/list.h:29:9`! |
| 30 | |
| 31 | #include <stdlib.h> |
| 32 | #include <stdbool.h> |
| 33 | |
| 34 | #include "util.h"cxx . . $ `<file>/src/common/util.h`/ |
| 35 | #include "public/oscap.h"cxx . . $ `<file>/src/common/public/oscap.h`/ |
| 36 | #include "public/oscap_text.h"cxx . . $ `<file>/src/common/public/oscap_text.h`/ |
| 37 | |
| 38 | |
| 39 | // list item dump function type |
| 40 | typedef void (*oscap_dump_funccxx . . $ oscap_dump_func#) (); |
| 41 | // generic comparison function type |
| 42 | typedef boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! (*oscap_cmp_funccxx . . $ oscap_cmp_func#) (void *, void *); |
| 43 | |
| 44 | /* |
| 45 | * Linear linked list. |
| 46 | */ |
| 47 | |
| 48 | struct oscap_list_itemcxx . . $ oscap_list_item# { |
| 49 | void *datacxx . . $ oscap_list_item#data.; |
| 50 | struct oscap_list_itemcxx . . $ oscap_list_item# *nextcxx . . $ oscap_list_item#next.; |
| 51 | }; |
| 52 | |
| 53 | struct oscap_listcxx . . $ oscap_list# { |
| 54 | struct oscap_list_itemcxx . . $ oscap_list_item# *firstcxx . . $ oscap_list#first.; |
| 55 | struct oscap_list_itemcxx . . $ oscap_list_item# *lastcxx . . $ oscap_list#last.; |
| 56 | size_tcxx . . $ size_t# itemcountcxx . . $ oscap_list#itemcount.; |
| 57 | }; |
| 58 | |
| 59 | // FIXME: SCE engine uses these |
| 60 | |
| 61 | struct oscap_listcxx . . $ oscap_list# *oscap_list_newcxx . . $ oscap_list_new(ea215f73c8361bff).(void); |
| 62 | void oscap_create_listscxx . . $ oscap_create_lists(ec4abf0a650ab9a0).(struct oscap_listcxx . . $ oscap_list# **first, ...); |
| 63 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_addcxx . . $ oscap_list_add(97f28760a5cf7823).(struct oscap_listcxx . . $ oscap_list# *list, void *value); |
| 64 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_pushcxx . . $ oscap_list_push(97f28760a5cf7823).(struct oscap_listcxx . . $ oscap_list# *list, void *value); |
| 65 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_prependcxx . . $ oscap_list_prepend(97f28760a5cf7823).(struct oscap_listcxx . . $ oscap_list# *list, void *value); |
| 66 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_popcxx . . $ oscap_list_pop(8038dc059d248e3c).(struct oscap_listcxx . . $ oscap_list# *list, oscap_destruct_funccxx . . $ oscap_destruct_func# destructor); |
| 67 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_removecxx . . $ oscap_list_remove(7e6762e164fcba91).(struct oscap_listcxx . . $ oscap_list# *list, void *value, oscap_cmp_funccxx . . $ oscap_cmp_func# compare, oscap_destruct_funccxx . . $ oscap_destruct_func# destructor); |
| 68 | struct oscap_listcxx . . $ oscap_list# *oscap_list_clonecxx . . $ oscap_list_clone(7e3970b01307df8e).(const struct oscap_listcxx . . $ oscap_list# * list, oscap_clone_funccxx . . $ oscap_clone_func# cloner); |
| 69 | void oscap_list_freecxx . . $ oscap_list_free(e68e105c3900fcc1).(struct oscap_listcxx . . $ oscap_list# *list, oscap_destruct_funccxx . . $ oscap_destruct_func# destructor); |
| 70 | void oscap_list_free0cxx . . $ oscap_list_free0(27ed2064e5aec1b1).(struct oscap_listcxx . . $ oscap_list# *list); |
| 71 | void oscap_list_dumpcxx . . $ oscap_list_dump(df0831e7fcc09688).(struct oscap_listcxx . . $ oscap_list# *list, oscap_dump_funccxx . . $ oscap_dump_func# dumper, int depth); |
| 72 | int oscap_list_get_itemcountcxx . . $ oscap_list_get_itemcount(5b766af325364e37).(struct oscap_listcxx . . $ oscap_list# *list); |
| 73 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_list_containscxx . . $ oscap_list_contains(810ee1985baa5ebe).(struct oscap_listcxx . . $ oscap_list# *list, void *what, oscap_cmp_funccxx . . $ oscap_cmp_func# compare); |
| 74 | struct oscap_listcxx . . $ oscap_list# *oscap_list_destructive_joincxx . . $ oscap_list_destructive_join(ff1de76ad37ec509).(struct oscap_listcxx . . $ oscap_list# *list1, struct oscap_listcxx . . $ oscap_list# *list2); |
| 75 | |
| 76 | |
| 77 | |
| 78 | /* Linked List iterator. */ |
| 79 | |
| 80 | typedef boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`!(*oscap_filter_funccxx . . $ oscap_filter_func#) (void *, void *); |
| 81 | |
| 82 | struct oscap_iteratorcxx . . $ oscap_iterator# { |
| 83 | struct oscap_list_itemcxx . . $ oscap_list_item# *curcxx . . $ oscap_iterator#cur.; |
| 84 | struct oscap_listcxx . . $ oscap_list# *listcxx . . $ oscap_iterator#list.; |
| 85 | oscap_filter_funccxx . . $ oscap_filter_func# filtercxx . . $ oscap_iterator#filter.; |
| 86 | void *user_datacxx . . $ oscap_iterator#user_data.; |
| 87 | }; |
| 88 | |
| 89 | // FIXME: SCE engine uses these |
| 90 | |
| 91 | void *oscap_iterator_newcxx . . $ oscap_iterator_new(f89fb456dfd589d7).(struct oscap_listcxx . . $ oscap_list# *list); |
| 92 | void *oscap_iterator_new_filtercxx . . $ oscap_iterator_new_filter(412a5e5f37b98023).(struct oscap_listcxx . . $ oscap_list# *list, oscap_filter_funccxx . . $ oscap_filter_func# filter, void *user_data); |
| 93 | void *oscap_iterator_nextcxx . . $ oscap_iterator_next(4c75051c806960eb).(struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 94 | size_tcxx . . $ size_t# oscap_iterator_get_itemcountcxx . . $ oscap_iterator_get_itemcount(88013f75f0376f74).(const struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 95 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_iterator_has_morecxx . . $ oscap_iterator_has_more(63b1de9a5adb9205).(struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 96 | void oscap_iterator_resetcxx . . $ oscap_iterator_reset(3cba44c1b7872358).(struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 97 | void *oscap_iterator_detachcxx . . $ oscap_iterator_detach(4c75051c806960eb).(struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 98 | void oscap_iterator_freecxx . . $ oscap_iterator_free(3cba44c1b7872358).(struct oscap_iteratorcxx . . $ oscap_iterator# *it); |
| 99 | |
| 100 | |
| 101 | void *oscap_list_findcxx . . $ oscap_list_find(56869e8d639c7a63).(struct oscap_listcxx . . $ oscap_list# *list, void *what, oscap_cmp_funccxx . . $ oscap_cmp_func# compare); |
| 102 | |
| 103 | /** |
| 104 | * Iterate over an array, given an iterator. |
| 105 | * Execute @a code for each array member stored in @a val. |
| 106 | * It is NOT safe to use return or goto inside of the @a code, |
| 107 | * the iterator would not be freed properly. |
| 108 | */ |
| 109 | #define OSCAP_FOREACH_GENERICcxx . . $ `src/common/list.h:109:9`!(itype, vtype, val, init_val, code) \ |
| 110 | { \ |
| 111 | struct itype##_iterator *val##_iter = (init_val); \ |
| 112 | vtype val; \ |
| 113 | while (itype##_iterator_has_more(val##_iter)) { \ |
| 114 | val = itype##_iterator_next(val##_iter); \ |
| 115 | code \ |
| 116 | } \ |
| 117 | itype##_iterator_free(val##_iter); \ |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Iterate over an array, given an iterator. |
| 122 | * @param type type of array elements (w/o the struct keyword) |
| 123 | * @param val name of an variable the member will be sequentially stored in |
| 124 | * @param init_val initial member value (i.e. an iterator pointing to the start element) |
| 125 | * @param code code to be executed for each element the iterator hits |
| 126 | * @see OSCAP_FOREACH_GENERIC |
| 127 | */ |
| 128 | #define OSCAP_FOREACHcxx . . $ `src/common/list.h:128:9`!(type, val, init_val, code) \ |
| 129 | OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code) |
| 130 | |
| 131 | /** |
| 132 | * Iterate over an array, given an iterator. |
| 133 | * It is generally not safe to use break, return or goto inside the loop |
| 134 | * (iterator wouldn't be properly freed otherwise). |
| 135 | * Two variables, named VAL and VAL_iter (substitute VAL for actual macro argument) |
| 136 | * will be added to current variable scope. You can free the iterator explicitly |
| 137 | * after previous unusual escape from the loop (e.g. using break). |
| 138 | * @param val name of an variable the string will be sequentially stored in |
| 139 | * @param init_val initial member value (i.e. an iterator pointing to the start element) |
| 140 | * @param code code to be executed for each string the iterator hits |
| 141 | */ |
| 142 | #define OSCAP_FOR_GENERICcxx . . $ `src/common/list.h:142:9`!(itype, vtype, val, init_val) \ |
| 143 | vtype val = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; struct itype##_iterator *val##_iter = (init_val); \ |
| 144 | while (itype##_iterator_has_more(val##_iter) \ |
| 145 | ? (val = itype##_iterator_next(val##_iter), truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!) \ |
| 146 | : (itype##_iterator_free(val##_iter), val##_iter = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!)) |
| 147 | |
| 148 | /** |
| 149 | * Iterate over an array, given an iterator. |
| 150 | * @param type type of array elements (w/o the struct keyword) |
| 151 | * @param val name of an variable the member will be sequentially stored in |
| 152 | * @param init_val initial member value (i.e. an iterator pointing to the start element) |
| 153 | * @see OSCAP_FOR_GENERIC |
| 154 | */ |
| 155 | #define OSCAP_FORcxx . . $ `src/common/list.h:155:9`!(type, val, init_val) OSCAP_FOR_GENERIC(type, struct type *, val, init_val) |
| 156 | |
| 157 | /** |
| 158 | * Iterate over an array of strings, given an iterator. |
| 159 | * @param val name of an variable the member will be sequentially stored in |
| 160 | * @param init_val initial member value (i.e. an iterator pointing to the start element) |
| 161 | * @see OSCAP_FOR_GENERIC |
| 162 | */ |
| 163 | #define OSCAP_FOR_STRcxx . . $ `src/common/list.h:163:9`!(val, init_val) OSCAP_FOR_GENERICcxx . . $ `src/common/list.h:142:9`!(oscap_string, const char *, val, init_val) |
| 164 | |
| 165 | /* |
| 166 | * Hash table |
| 167 | */ |
| 168 | |
| 169 | // Comparison function. |
| 170 | typedef int (*oscap_compare_funccxx . . $ oscap_compare_func#) (const char *, const char *); |
| 171 | // Hash table item. |
| 172 | struct oscap_htable_itemcxx . . $ oscap_htable_item# { |
| 173 | struct oscap_htable_itemcxx . . $ oscap_htable_item# *nextcxx . . $ oscap_htable_item#next.; // Next item. |
| 174 | char *keycxx . . $ oscap_htable_item#key.; // Item key. |
| 175 | void *valuecxx . . $ oscap_htable_item#value.; // Item value. |
| 176 | }; |
| 177 | |
| 178 | // Hash table. |
| 179 | struct oscap_htablecxx . . $ oscap_htable# { |
| 180 | size_tcxx . . $ size_t# hsizecxx . . $ oscap_htable#hsize.; // Size of the hash table. |
| 181 | size_tcxx . . $ size_t# itemcountcxx . . $ oscap_htable#itemcount.; // Number of elements in the hash table. |
| 182 | struct oscap_htable_itemcxx . . $ oscap_htable_item# **tablecxx . . $ oscap_htable#table.; // The table itself. |
| 183 | oscap_compare_funccxx . . $ oscap_compare_func# cmpcxx . . $ oscap_htable#cmp.; // Funcion used to compare keys (e.g. strcmp). |
| 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * Create a new hash table. |
| 188 | * @param cmp Pointer to a function used as the key comparator. |
| 189 | * @hsize Size of the hash table. |
| 190 | * @internal |
| 191 | * @return new hash table |
| 192 | */ |
| 193 | struct oscap_htablecxx . . $ oscap_htable# *oscap_htable_new1cxx . . $ oscap_htable_new1(65089fcacd1ec7ff).(oscap_compare_funccxx . . $ oscap_compare_func# cmp, size_tcxx . . $ size_t# hsize); |
| 194 | |
| 195 | /* |
| 196 | * Create a new hash table. |
| 197 | * |
| 198 | * The table will use strcmp() as the comparison function and will have default table size. |
| 199 | * @see oscap_htable_new1() |
| 200 | * @return new hash table |
| 201 | */ |
| 202 | struct oscap_htablecxx . . $ oscap_htable# *oscap_htable_newcxx . . $ oscap_htable_new(3c1cb2b6f390ad38).(void); |
| 203 | |
| 204 | /* |
| 205 | * Do a Deep Copy of a hashtable and all of its items |
| 206 | * |
| 207 | * @return deep copy of hash table |
| 208 | */ |
| 209 | struct oscap_htablecxx . . $ oscap_htable# * oscap_htable_clonecxx . . $ oscap_htable_clone(c459a691cacbd47f).(const struct oscap_htablecxx . . $ oscap_htable# * table, oscap_clone_funccxx . . $ oscap_clone_func# cloner); |
| 210 | |
| 211 | /* |
| 212 | * Add an item to the hash table. |
| 213 | * @return True on success, false if the key already exists. |
| 214 | */ |
| 215 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_htable_addcxx . . $ oscap_htable_add(d9abe78f59fa00eb).(struct oscap_htablecxx . . $ oscap_htable# *htable, const char *key, void *item); |
| 216 | |
| 217 | /* |
| 218 | * Get a hash table item. |
| 219 | * @return An item, NULL if item with specified key is not present in the hash table. |
| 220 | */ |
| 221 | void *oscap_htable_getcxx . . $ oscap_htable_get(f5c7e8ff6c970cd3).(struct oscap_htablecxx . . $ oscap_htable# *htable, const char *key); |
| 222 | |
| 223 | /** |
| 224 | * Get count of items in the hash table |
| 225 | * @param htable Hash table |
| 226 | * @return Count of items stored in the given hash table |
| 227 | */ |
| 228 | size_tcxx . . $ size_t# oscap_htable_itemcountcxx . . $ oscap_htable_itemcount(74d87088304a4a23).(struct oscap_htablecxx . . $ oscap_htable# *htable); |
| 229 | |
| 230 | void *oscap_htable_detachcxx . . $ oscap_htable_detach(f5c7e8ff6c970cd3).(struct oscap_htablecxx . . $ oscap_htable# *htable, const char *key); |
| 231 | |
| 232 | void oscap_htable_dumpcxx . . $ oscap_htable_dump(93d367da24ec0c3e).(struct oscap_htablecxx . . $ oscap_htable# *htable, oscap_dump_funccxx . . $ oscap_dump_func# dumper, int depth); |
| 233 | |
| 234 | /* |
| 235 | * Delete the hash table. |
| 236 | * @param htable Hash table to be deleted. |
| 237 | * @param destructor Function used to delete individual items. |
| 238 | */ |
| 239 | void oscap_htable_freecxx . . $ oscap_htable_free(efa5da5da5d523d).(struct oscap_htablecxx . . $ oscap_htable# *htable, oscap_destruct_funccxx . . $ oscap_destruct_func# destructor); |
| 240 | /* |
| 241 | * Dispose the hash table -- do not dispose individial items. |
| 242 | * @param htable Hash table to be deleted. |
| 243 | */ |
| 244 | void oscap_htable_free0cxx . . $ oscap_htable_free0(dbd8f9d05ba37fd5).(struct oscap_htablecxx . . $ oscap_htable# *htable); |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Iterator through htable structure. |
| 249 | */ |
| 250 | struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator#; |
| 251 | |
| 252 | /** |
| 253 | * Create new iterator through hash table. No ordering is defined for items. |
| 254 | * @param htable Hash table to iterate through. |
| 255 | * @return the iterator |
| 256 | */ |
| 257 | struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *oscap_htable_iterator_newcxx . . $ oscap_htable_iterator_new(81743c99830972e7).(struct oscap_htablecxx . . $ oscap_htable# *htable); |
| 258 | |
| 259 | /** |
| 260 | * Query if the given iterator has more hash table items. |
| 261 | * @param iterator iterator |
| 262 | * @return true is there is at least one more item. |
| 263 | */ |
| 264 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_htable_iterator_has_morecxx . . $ oscap_htable_iterator_has_more(eba4198ab3388c55).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 265 | |
| 266 | /** |
| 267 | * Get the next item from iterator. The behavior is undefined if the prior call |
| 268 | * of oscap_htable_iterator_has_more() returns false |
| 269 | * @param hit iterator |
| 270 | */ |
| 271 | const struct oscap_htable_itemcxx . . $ oscap_htable_item# *oscap_htable_iterator_nextcxx . . $ oscap_htable_iterator_next(98c5256d433bc020).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 272 | |
| 273 | /** |
| 274 | * Get the key of next item from the iterator. The behavior is undefined |
| 275 | * if the prior call of oscap_htable_iterator_has_more returns false. |
| 276 | * @param hit iterator |
| 277 | */ |
| 278 | const char *oscap_htable_iterator_next_keycxx . . $ oscap_htable_iterator_next_key(952d27177769fc02).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 279 | |
| 280 | /** |
| 281 | * Get the value of next item from the iterator. The behavior is undefined |
| 282 | * if the prior call of oscap_htable_iterator_has_more returns false. |
| 283 | * @param hit iterator |
| 284 | */ |
| 285 | void *oscap_htable_iterator_next_valuecxx . . $ oscap_htable_iterator_next_value(4e1156670e4376db).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 286 | |
| 287 | /** |
| 288 | * Get the key and value of next item from the iterator. The behavior is undefined |
| 289 | * if the prior call of oscap_htable_iterator_has_more returns false. |
| 290 | * @param hit iterator |
| 291 | * @param key pointer to pointer that will point towards the key pointer |
| 292 | * @param value pointer to pointer that will point towards the value pointer |
| 293 | */ |
| 294 | void oscap_htable_iterator_next_kvcxx . . $ oscap_htable_iterator_next_kv(6fdd88671b26854e).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit, const char **key, void **value); |
| 295 | |
| 296 | /** |
| 297 | * Reset the hashtable iterator to the begin |
| 298 | * @param hit iterator |
| 299 | */ |
| 300 | void oscap_htable_iterator_resetcxx . . $ oscap_htable_iterator_reset(e07ea5562ff119bd).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 301 | |
| 302 | /** |
| 303 | * Dispose the iterator |
| 304 | * @param hit the iterator to dispose |
| 305 | */ |
| 306 | void oscap_htable_iterator_freecxx . . $ oscap_htable_iterator_free(e07ea5562ff119bd).(struct oscap_htable_iteratorcxx . . $ oscap_htable_iterator# *hit); |
| 307 | |
| 308 | void oscap_print_depthcxx . . $ oscap_print_depth(d4f767463ce0a6b3).(int depth); |
| 309 | |
| 310 | |
| 311 | #endif |