| 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 | #ifndef OSCAP_UTIL_H_ |
| 25 | #define OSCAP_UTIL_H_cxx . . $ `src/common/util.h:25:9`! |
| 26 | |
| 27 | #include "oscap_platforms.h"cxx . . $ `<file>/compat/oscap_platforms.h`/ |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <assert.h> |
| 32 | #include "public/oscap.h"cxx . . $ `<file>/src/common/public/oscap.h`/ |
| 33 | #include <stdarg.h> |
| 34 | #include <string.h> |
| 35 | #include "oscap_export.h"cxx . . $ `<file>/src/common/public/oscap_export.h`/ |
| 36 | #include "oscap_pcre.h"cxx . . $ `<file>/src/common/oscap_pcre.h`/ |
| 37 | |
| 38 | #ifndef __attribute__nonnull__ |
| 39 | #define __attribute__nonnull__cxx . . $ `src/common/util.h:39:9`!(x) assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!((x) != NULL) |
| 40 | #endif |
| 41 | |
| 42 | /** |
| 43 | * Function pointer to an object destructor. |
| 44 | */ |
| 45 | typedef void (*oscap_destruct_funccxx . . $ oscap_destruct_func#) (void *); |
| 46 | |
| 47 | /** |
| 48 | * Pointer to a clone function. |
| 49 | */ |
| 50 | typedef void* (*oscap_clone_funccxx . . $ oscap_clone_func#) (void *); |
| 51 | |
| 52 | /** |
| 53 | * Function pointer to an object consumer. |
| 54 | */ |
| 55 | typedef void (*oscap_consumer_funccxx . . $ oscap_consumer_func#) (void *, void *); |
| 56 | |
| 57 | /** |
| 58 | * Generate a getter function with an optional conversion. |
| 59 | * Header of the generated function will look like this (substitute the capital letter sequences with the actual parametres): |
| 60 | * @code RTYPE SNAME_MNAME(const struct SNAME*); @endcode |
| 61 | * @param RTYPE return type |
| 62 | * @param CONV conversion expression, i.e. either a type conversion of form '(type)' or an unary function identifier |
| 63 | * @param SNAME name of the structure of which we are getting a member (without the struct keyword) |
| 64 | * @param MNAME member name affects how the resultant function will be called |
| 65 | * @param MEXP expression to get the member |
| 66 | */ |
| 67 | #define OSCAP_GENERIC_GETTER_CONVcxx . . $ `src/common/util.h:67:9`!(RTYPE,CONV,SNAME,MNAME,MEXP) \ |
| 68 | RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); } |
| 69 | |
| 70 | /** |
| 71 | * Generate a getter function. |
| 72 | * @param RTYPE return type |
| 73 | * @param SNAME name of the structure of which we are getting a member |
| 74 | * @param MNAME member name affects how the resultant function will be called |
| 75 | * @param MEXP expression to get the member |
| 76 | * @see OSCAP_GENERIC_GETTER_CONV |
| 77 | */ |
| 78 | #define OSCAP_GENERIC_GETTERcxx . . $ `src/common/util.h:78:9`!(RTYPE,SNAME,MNAME,MEXP) \ |
| 79 | OSCAP_GENERIC_GETTER_CONVcxx . . $ `src/common/util.h:67:9`!(RTYPE,,SNAME,MNAME,MEXP) |
| 80 | |
| 81 | /** |
| 82 | * Generate a getter function with an explicit conversion to the target data type. |
| 83 | * @param RTYPE return type |
| 84 | * @param SNAME name of the structure of which we are getting a member |
| 85 | * @param MNAME member name affects how the resultant function will be called |
| 86 | * @param MEXP expression to get the member |
| 87 | * @see OSCAP_GENERIC_GETTER_CONV |
| 88 | */ |
| 89 | #define OSCAP_GENERIC_GETTER_FORCEcxx . . $ `src/common/util.h:89:9`!(RTYPE,SNAME,MNAME,MEXP) \ |
| 90 | OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP) |
| 91 | |
| 92 | /** |
| 93 | * Generate a getter function with an explicit conversion to the target data type. |
| 94 | * Member name and second part of the generated function's name must match. |
| 95 | * @param RTYPE return type |
| 96 | * @param SNAME name of the structure of which we are getting a member |
| 97 | * @param MNAME member name affects how the resultant function will be called |
| 98 | * @see OSCAP_GENERIC_GETTER_FORCE |
| 99 | */ |
| 100 | #define OSCAP_GETTER_FORCEcxx . . $ `src/common/util.h:100:9`!(RTYPE,SNAME,MNAME) \ |
| 101 | OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME) |
| 102 | |
| 103 | /** |
| 104 | * Generate a getter function. |
| 105 | * Member name and second part of the generated function's name must match. |
| 106 | * @param RTYPE return type |
| 107 | * @param SNAME name of the structure of which we are getting a member |
| 108 | * @param MNAME member name affects how the resultant function will be called |
| 109 | * @see OSCAP_GENERIC_GETTER |
| 110 | */ |
| 111 | #define OSCAP_GETTERcxx . . $ `src/common/util.h:111:9`!(RTYPE,SNAME,MNAME) \ |
| 112 | OSCAP_GENERIC_GETTERcxx . . $ `src/common/util.h:78:9`!(RTYPE,SNAME,MNAME,MNAME) |
| 113 | |
| 114 | #define ITERATOR_CASTcxx . . $ `src/common/util.h:114:9`!(x) ((struct oscap_iterator*)(x)) |
| 115 | #define OSCAP_ITERATORcxx . . $ `src/common/util.h:115:9`!(n) struct n##_iterator* |
| 116 | #define OSCAP_ITERATOR_FWDcxx . . $ `src/common/util.h:116:9`!(n) struct n##_iterator; |
| 117 | #define OSCAP_ITERATOR_HAS_MOREcxx . . $ `src/common/util.h:117:9`!(n) boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! n##_iterator_has_more(OSCAP_ITERATORcxx . . $ `src/common/util.h:115:9`!(n) it) { return oscap_iterator_has_more(ITERATOR_CASTcxx . . $ `src/common/util.h:114:9`!(it)); } |
| 118 | #define OSCAP_ITERATOR_NEXTcxx . . $ `src/common/util.h:118:9`!(t,n) t n##_iterator_next(OSCAP_ITERATORcxx . . $ `src/common/util.h:115:9`!(n) it) { return oscap_iterator_next(ITERATOR_CASTcxx . . $ `src/common/util.h:114:9`!(it)); } |
| 119 | #define OSCAP_ITERATOR_FREEcxx . . $ `src/common/util.h:119:9`!(n) void n##_iterator_free(OSCAP_ITERATORcxx . . $ `src/common/util.h:115:9`!(n) it) { oscap_iterator_free(ITERATOR_CASTcxx . . $ `src/common/util.h:114:9`!(it)); } |
| 120 | #define OSCAP_ITERATOR_RESETcxx . . $ `src/common/util.h:120:9`!(n) void n##_iterator_reset(OSCAP_ITERATORcxx . . $ `src/common/util.h:115:9`!(n) it) { oscap_iterator_reset(ITERATOR_CASTcxx . . $ `src/common/util.h:114:9`!(it)); } |
| 121 | #define OSCAP_ITERATOR_DETACHcxx . . $ `src/common/util.h:121:9`!(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); } |
| 122 | #define OSCAP_ITERATOR_GEN_Tcxx . . $ `src/common/util.h:122:9`!(t,n) OSCAP_ITERATOR_FWDcxx . . $ `src/common/util.h:116:9`!(n) OSCAP_ITERATOR_HAS_MOREcxx . . $ `src/common/util.h:117:9`!(n) OSCAP_ITERATOR_RESETcxx . . $ `src/common/util.h:120:9`!(n) OSCAP_ITERATOR_NEXTcxx . . $ `src/common/util.h:118:9`!(t,n) OSCAP_ITERATOR_FREEcxx . . $ `src/common/util.h:119:9`!(n) |
| 123 | #define OSCAP_ITERATOR_GENcxx . . $ `src/common/util.h:123:9`!(n) OSCAP_ITERATOR_GEN_Tcxx . . $ `src/common/util.h:122:9`!(struct n*,n) |
| 124 | |
| 125 | #define OSCAP_ITERATOR_REMOVE_Tcxx . . $ `src/common/util.h:125:9`!(t,n,destructor) \ |
| 126 | void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); } |
| 127 | #define OSCAP_ITERATOR_REMOVEcxx . . $ `src/common/util.h:127:9`!(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor) |
| 128 | #define OSCAP_ITERATOR_REMOVE_Fcxx . . $ `src/common/util.h:128:9`!(n) OSCAP_ITERATOR_REMOVE(n, n##_free) |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Generate an iterator getter function. |
| 133 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 134 | * @code struct ITYPE_iterator* SNAME_MNAME(const struct SNAME*); @endcode |
| 135 | * @param ITYPE iterator type (without the '_iterator' suffix) |
| 136 | * @param SNAME name of type the structure containing the list |
| 137 | * @param MANME list itself |
| 138 | * @param CONV convert expression |
| 139 | */ |
| 140 | #define OSCAP_IGETTER_CONVcxx . . $ `src/common/util.h:140:9`!(ITYPE,SNAME,MNAME,CONV) \ |
| 141 | struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \ |
| 142 | { return oscap_iterator_new((CONV(item))->MNAME); } |
| 143 | |
| 144 | /** |
| 145 | * Generate an iterator getter function. |
| 146 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 147 | * @code struct ITYPE_iterator* SNAME_MNAME(const struct SNAME*); @endcode |
| 148 | * @param ITYPE iterator type (without the '_iterator' suffix) |
| 149 | * @param SNAME name of type the structure containing the list |
| 150 | * @param MANME list itself |
| 151 | */ |
| 152 | #define OSCAP_IGETTERcxx . . $ `src/common/util.h:152:9`!(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONVcxx . . $ `src/common/util.h:140:9`!(ITYPE,SNAME,MNAME,) |
| 153 | |
| 154 | /** |
| 155 | * Generate an iterator and its getter. |
| 156 | * @see OSCAP_IGETTER |
| 157 | * @see OSCAP_ITERATOR_GEN |
| 158 | */ |
| 159 | #define OSCAP_IGETTER_GENcxx . . $ `src/common/util.h:159:9`!(ITYPE,SNAME,MNAME) OSCAP_IGETTERcxx . . $ `src/common/util.h:152:9`!(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GENcxx . . $ `src/common/util.h:123:9`!(ITYPE) |
| 160 | |
| 161 | /** |
| 162 | * Generate a getter function from a hash table. |
| 163 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 164 | * @code RTYPE SNAME_MNAME(const struct SNAME* item, const char* key); @endcode |
| 165 | * @param RTYPE type of the hash table item |
| 166 | * @param SNAME name of the structure |
| 167 | * @param MNAME structure member name |
| 168 | * @param MEXP expression to get the member variable (i.e. the hash table) |
| 169 | */ |
| 170 | #define OSCAP_HGETTER_EXPcxx . . $ `src/common/util.h:170:9`!(RTYPE,SNAME,MNAME,MEXP) \ |
| 171 | RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \ |
| 172 | { return oscap_htable_get(item->MEXP, key); } |
| 173 | |
| 174 | /** |
| 175 | * Generate a getter function from a hash table. |
| 176 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 177 | * @code RTYPE SNAME_MNAME(const struct SNAME* item, const char* key); @endcode |
| 178 | * @param RTYPE type of the hash table item |
| 179 | * @param SNAME name of the structure |
| 180 | * @param MNAME structure member name |
| 181 | */ |
| 182 | #define OSCAP_HGETTERcxx . . $ `src/common/util.h:182:9`!(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME) |
| 183 | |
| 184 | /** |
| 185 | * Generate a getter function from a hash table. |
| 186 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 187 | * @code struct RTYPE* SNAME_MNAME(const struct SNAME* item, const char* key); @endcode |
| 188 | * @param RTYPE type of the hash table item |
| 189 | * @param SNAME name of the structure |
| 190 | * @param MNAME structure member name |
| 191 | */ |
| 192 | #define OSCAP_HGETTER_STRUCTcxx . . $ `src/common/util.h:192:9`!(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME) |
| 193 | |
| 194 | #define OSCAP_SETTER_HEADERcxx . . $ `src/common/util.h:194:9`!(SNAME, MTYPE, MNAME) boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval) |
| 195 | |
| 196 | /** |
| 197 | * Generate a setter function with a check. |
| 198 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 199 | * @code bool SNAME_get_MNAME(struct SNAME *obj, MTYPE *item); @endcode |
| 200 | * @param SNAME Name of the structure. |
| 201 | * @param MTYPE Type of the member item to set. |
| 202 | * @param MNAME Name of the member item to set. |
| 203 | * @param CHECK Code to check validity of the assignment (you can use obj and item variables here). |
| 204 | * @param DELETER Function used to delete the old value (or empty string). |
| 205 | * @param ASSIGNER Function used to assign value to the item. |
| 206 | */ |
| 207 | #define OSCAP_SETTER_GENERIC_CHECKcxx . . $ `src/common/util.h:207:9`!(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \ |
| 208 | OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \ |
| 209 | { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; } |
| 210 | |
| 211 | /** |
| 212 | * Generate a setter function without a check. |
| 213 | * @see OSCAP_SETTER_GENERIC_CHECK |
| 214 | */ |
| 215 | #define OSCAP_SETTER_GENERICcxx . . $ `src/common/util.h:215:9`!(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \ |
| 216 | OSCAP_SETTER_HEADERcxx . . $ `src/common/util.h:194:9`!(SNAME, MTYPE, MNAME) \ |
| 217 | { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; } |
| 218 | |
| 219 | /** |
| 220 | * Generate a setter function without a check that does not delete the previous value. |
| 221 | * @see OSCAP_SETTER_GENERIC_CHECK |
| 222 | */ |
| 223 | #define OSCAP_SETTER_GENERIC_NODELETEcxx . . $ `src/common/util.h:223:9`!(SNAME, MTYPE, MNAME, ASSIGNER) \ |
| 224 | OSCAP_SETTER_HEADERcxx . . $ `src/common/util.h:194:9`!(SNAME, MTYPE, MNAME) \ |
| 225 | { obj->MNAME = ASSIGNER(newval); return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; } |
| 226 | |
| 227 | /** |
| 228 | * Generate a setter function using a simple assignment. |
| 229 | * @see OSCAP_SETTER_GENERIC_CHECK |
| 230 | */ |
| 231 | #define OSCAP_SETTER_SIMPLEcxx . . $ `src/common/util.h:231:9`!(SNAME, MTYPE, MNAME) \ |
| 232 | OSCAP_SETTER_GENERIC_NODELETEcxx . . $ `src/common/util.h:223:9`!(SNAME, MTYPE, MNAME, ) |
| 233 | |
| 234 | /** |
| 235 | * Assign a string value to a structure member. |
| 236 | * @param SNAME Structure name |
| 237 | * @param MNAME Name of structure member containing the string to be set. |
| 238 | */ |
| 239 | #define OSCAP_SETTER_STRINGcxx . . $ `src/common/util.h:239:9`!(SNAME, MNAME) \ |
| 240 | OSCAP_SETTER_GENERICcxx . . $ `src/common/util.h:215:9`!(SNAME, const char *, MNAME, free, oscap_strdup) |
| 241 | |
| 242 | /** |
| 243 | * Define both, getter and setter for a string structure member. |
| 244 | */ |
| 245 | #define OSCAP_ACCESSOR_STRINGcxx . . $ `src/common/util.h:245:9`!(SNAME, MNAME) \ |
| 246 | OSCAP_GETTERcxx . . $ `src/common/util.h:111:9`!(const char*, SNAME, MNAME) OSCAP_SETTER_STRINGcxx . . $ `src/common/util.h:239:9`!(SNAME, MNAME) |
| 247 | |
| 248 | /** |
| 249 | * Define both, getter and setter for a text structure member. |
| 250 | */ |
| 251 | #define OSCAP_ACCESSOR_TEXTcxx . . $ `src/common/util.h:251:9`!(SNAME, MNAME) \ |
| 252 | OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \ |
| 253 | OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, ) |
| 254 | |
| 255 | /** |
| 256 | * Define both, getter and setter for a structure member using simple get/set. |
| 257 | */ |
| 258 | #define OSCAP_ACCESSOR_SIMPLEcxx . . $ `src/common/util.h:258:9`!(MTYPE, SNAME, MNAME) \ |
| 259 | OSCAP_GETTERcxx . . $ `src/common/util.h:111:9`!(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLEcxx . . $ `src/common/util.h:231:9`!(SNAME, MTYPE, MNAME) |
| 260 | |
| 261 | /** |
| 262 | * Define both, getter and setter for a structure member using simple get/set |
| 263 | * using different expression to het the member. |
| 264 | */ |
| 265 | #define OSCAP_ACCESSOR_EXPcxx . . $ `src/common/util.h:265:9`!(MTYPE, SNAME, MNAME, MEXP) \ |
| 266 | OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \ |
| 267 | OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; } |
| 268 | |
| 269 | /** |
| 270 | * Generate function to insert an item into a list. |
| 271 | * Signature of the generated function will be as follows (substitute uppercase strings with actual params): |
| 272 | * @code bool SNAME_add_FNAME(struct SNAME *obj, struct MTYPE *item); @endcode |
| 273 | * @param SNAME structure typename (w/o the struct keyword) |
| 274 | * @param FNAME how should function name part after the _add_ look like |
| 275 | * @param MTYPE list member type |
| 276 | * @param MNAME name of the list within the structure |
| 277 | */ |
| 278 | #define OSCAP_INSERTERcxx . . $ `src/common/util.h:278:9`!(SNAME, FNAME, MTYPE, MNAME) \ |
| 279 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \ |
| 280 | { oscap_list_add(obj->MNAME, item); return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; } |
| 281 | |
| 282 | /* Generate iterator getter and list inserter */ |
| 283 | #define OSCAP_IGETINScxx . . $ `src/common/util.h:283:9`!(ITYPE, SNAME, MNAME, FNAME) \ |
| 284 | OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME) |
| 285 | /* Generate iterator getter, list inserter, and iterator manipulation functions. */ |
| 286 | #define OSCAP_IGETINS_GENcxx . . $ `src/common/util.h:286:9`!(ITYPE, SNAME, MNAME, FNAME) \ |
| 287 | OSCAP_IGETTER_GENcxx . . $ `src/common/util.h:159:9`!(ITYPE, SNAME, MNAME) OSCAP_INSERTERcxx . . $ `src/common/util.h:278:9`!(SNAME, FNAME, ITYPE, MNAME) |
| 288 | |
| 289 | /** |
| 290 | * Define mapping between symbolic constant and its string representation. |
| 291 | * |
| 292 | * It is supposed to define array of these structures, ending with element |
| 293 | * with the @a string member set to NULL. Value of such member also defines |
| 294 | * the default value for strings not defined elsewhere. |
| 295 | */ |
| 296 | struct oscap_string_mapcxx . . $ oscap_string_map# { |
| 297 | const int valuecxx . . $ oscap_string_map#value.; /* integer/enum value */ |
| 298 | const char *stringcxx . . $ oscap_string_map#string.; /* string representation of the value */ |
| 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * Use strdup on string, if string is NULL, return NULL |
| 303 | * @param str String we want to duplicate |
| 304 | */ |
| 305 | static inline char *oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(const char *str) { |
| 306 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 307 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 308 | |
| 309 | #ifdef _MSC_VER |
| 310 | return _strdup(str); |
| 311 | #else |
| 312 | return strdupcxx . . $ strdup(a8b8e32347182212).(str); |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Removes all occurrences of substr from str by shifting the contents to |
| 318 | * the left. If string is NULL, does nothing. |
| 319 | * @param str String we want to process |
| 320 | * @param substr Sub-string we want to get rid of |
| 321 | */ |
| 322 | static inline void oscap_strrmcxx . . $ oscap_strrm(72c91e5e80d20bae).(char *str, const char *substr) { |
| 323 | if (str == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 324 | return; |
| 325 | |
| 326 | size_tcxx . . $ size_t# sublen = strlencxx . . $ strlen(751346f8406fd082).(substr); |
| 327 | char *ptr = strstrcxx . . $ strstr(a713fe7b9a57cf96).(str, substr); |
| 328 | while (ptr != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 329 | memmovecxx . . $ memmove(cc817cb175f282d).(ptr, ptr+sublen, strlencxx . . $ strlen(751346f8406fd082).(ptr) - sublen + 1); |
| 330 | ptr = strstrcxx . . $ strstr(a713fe7b9a57cf96).(str, substr); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /// Just like strcmp except it's NULL-safe. Use the standard strcmp directly if possible. |
| 335 | static inline int oscap_strcmpcxx . . $ oscap_strcmp(5ae29d726ae57f88).(const char *s1, const char *s2) { |
| 336 | if (s1 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) s1 = ""; |
| 337 | if (s2 == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) s2 = ""; |
| 338 | return strcmpcxx . . $ strcmp(5ae29d726ae57f88).(s1, s2); |
| 339 | } |
| 340 | |
| 341 | /// Check for string equality. Use the standard strcmp directly if possible. |
| 342 | static inline boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(const char *s1, const char *s2) { |
| 343 | return oscap_strcmpcxx . . $ oscap_strcmp(5ae29d726ae57f88).(s1, s2) == 0; |
| 344 | } |
| 345 | |
| 346 | /// Check whether str starts with "prefix" |
| 347 | static inline boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_str_startswithcxx . . $ oscap_str_startswith(b7a7aa4a824b7595).(const char *str, const char *prefix) { |
| 348 | return strncmpcxx . . $ strncmp(fd278af2f295fa2e).(str, prefix, strlencxx . . $ strlen(751346f8406fd082).(prefix)) == 0; |
| 349 | } |
| 350 | |
| 351 | /// Check whether str ends with "suffix" |
| 352 | static inline boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_str_endswithcxx . . $ oscap_str_endswith(b7a7aa4a824b7595).(const char *str, const char *suffix) { |
| 353 | const size_tcxx . . $ size_t# str_len = strlencxx . . $ strlen(751346f8406fd082).(str); |
| 354 | const size_tcxx . . $ size_t# suffix_len = strlencxx . . $ strlen(751346f8406fd082).(suffix); |
| 355 | if (suffix_len > str_len) |
| 356 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 357 | return strncmpcxx . . $ strncmp(fd278af2f295fa2e).(str + str_len - suffix_len, suffix, suffix_len) == 0; |
| 358 | } |
| 359 | |
| 360 | /// Allocate aligned memory |
| 361 | static inline void *oscap_aligned_malloccxx . . $ oscap_aligned_malloc(63a7bf2941bfcd7a).(size_tcxx . . $ size_t# size, size_tcxx . . $ size_t# alignment) { |
| 362 | #ifdef WIN32 |
| 363 | return _aligned_malloc(size, alignment); |
| 364 | #else |
| 365 | void *ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 366 | int ret = posix_memaligncxx . . $ posix_memalign(e974a8a87fa206f3).(&ptr, alignment, size); |
| 367 | if (ret == 0) |
| 368 | return ptr; |
| 369 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 370 | #endif |
| 371 | } |
| 372 | |
| 373 | /// Free aligned memory |
| 374 | static inline void oscap_aligned_freecxx . . $ oscap_aligned_free(5d22bdacc48458e8).(void *memblock) { |
| 375 | #ifdef WIN32 |
| 376 | _aligned_free(memblock); |
| 377 | #else |
| 378 | freecxx . . $ free(5d22bdacc48458e8).(memblock); |
| 379 | #endif |
| 380 | } |
| 381 | |
| 382 | /// Trim whitespace (modifies its argument!) |
| 383 | char *oscap_trimcxx . . $ oscap_trim(7a9722d75b371431).(char *str); |
| 384 | /// Print to a newly allocated string using a va_list. |
| 385 | char *oscap_vsprintfcxx . . $ oscap_vsprintf(db813db1ba4aba5a).(const char *fmt, va_listcxx . . $ va_list# ap); |
| 386 | |
| 387 | /** |
| 388 | * Generates a pseudorandom string of a given length. |
| 389 | * If charset string is not NULL and its length is greater than 0, |
| 390 | * it will be used as a dictionary, otherwise a default alphanumeric set |
| 391 | * will be the base for the generated string. |
| 392 | * Caller is responsible for freeing the returned string. |
| 393 | * @param len desired string length (must be greater than 0) |
| 394 | * @param charset a dictionary string, could be NULL |
| 395 | * @return A random string of desired length. |
| 396 | */ |
| 397 | char *oscap_generate_random_stringcxx . . $ oscap_generate_random_string(d2dea573f08ae648).(size_tcxx . . $ size_t# len, char *charset); |
| 398 | |
| 399 | /** |
| 400 | * Join 2 paths in an intelligent way. |
| 401 | * Both paths are allowed to be NULL. |
| 402 | * Caller is responsible for freeing the returned pointer. |
| 403 | * @param path1 first path |
| 404 | * @param path2 second path |
| 405 | * @return Join of path1 and path2. The first path is separated by the second |
| 406 | * path by exactly 1 slash separator. |
| 407 | */ |
| 408 | char *oscap_path_joincxx . . $ oscap_path_join(a713fe7b9a57cf96).(const char *path1, const char *path2); |
| 409 | |
| 410 | /// In a list of key-value pairs (odd indicies are keys, even values), find a value for given key |
| 411 | const char *oscap_strlist_find_valuecxx . . $ oscap_strlist_find_value(ce6e284d80333bb0).(char ** const kvalues, const char *key); |
| 412 | /// Right trim @a ch characters (modifies its first argument!) |
| 413 | char *oscap_rtrimcxx . . $ oscap_rtrim(dabe84363a524a9e).(char *str, char ch); |
| 414 | /// Convert string to uppercase (modifies its first argument!) |
| 415 | void oscap_strtouppercxx . . $ oscap_strtoupper(fda57c8427c60377).(char *str); |
| 416 | |
| 417 | // check pointer equality |
| 418 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_ptr_cmpcxx . . $ oscap_ptr_cmp(838b4d1787e4a177).(void *node1, void *node2); |
| 419 | |
| 420 | /** |
| 421 | * A helper function to expand given shorthand IPv6 |
| 422 | * |
| 423 | * example: |
| 424 | * oscap_expand_ipv6("::1") returns "0:0:0:0:0:0:0:1" |
| 425 | * |
| 426 | * @par |
| 427 | * Asset Identification specification requires fully expanded IPv6 addresses. |
| 428 | * Despite this being greatly discouraged by RFC 5952 we have to comply and |
| 429 | * expand given IPv6, otherwise the resulting AI content would be invalid. |
| 430 | */ |
| 431 | char *oscap_expand_ipv6cxx . . $ oscap_expand_ipv6(a8b8e32347182212).(const char *input); |
| 432 | |
| 433 | #ifndef OSCAP_CONCAT |
| 434 | # define OSCAP_CONCAT1cxx . . $ `src/common/util.h:434:10`!(a,b) a ## b |
| 435 | # define OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(a,b) OSCAP_CONCAT1cxx . . $ `src/common/util.h:434:10`!(a,b) |
| 436 | #endif |
| 437 | |
| 438 | /** |
| 439 | * Mark global symbols. |
| 440 | * This macro can be used to wrap global variables and helps distinguish |
| 441 | * global variables and local varibles. |
| 442 | * It adds '___G_' prefix to variable name. |
| 443 | */ |
| 444 | #define OSCAP_GSYMcxx . . $ `src/common/util.h:444:9`!(s) OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(___G_, s) |
| 445 | |
| 446 | #define protect_errnocxx . . $ `src/common/util.h:446:9`! \ |
| 447 | for (int OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(__e,__LINE__)=errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(__s,__LINE__)=1; OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(__s,__LINE__)--; errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!=OSCAP_CONCATcxx . . $ `src/common/util.h:435:10`!(__e,__LINE__)) |
| 448 | |
| 449 | /** |
| 450 | * Convert a string to an enumeration constant. |
| 451 | * @param map An array of oscap_string_map structures that defines mapping between constants and strings. |
| 452 | * @param str string to be converted |
| 453 | * @memberof oscap_string_map |
| 454 | */ |
| 455 | int oscap_string_to_enumcxx . . $ oscap_string_to_enum(851d6ce10579b7d8).(const struct oscap_string_mapcxx . . $ oscap_string_map# *map, const char *str); |
| 456 | |
| 457 | /** |
| 458 | * Convert an enumeration constant to its corresponding string representation. |
| 459 | * @param map An array of oscap_string_map structures that defines mapping between constants and strings. |
| 460 | * @param val value to be converted |
| 461 | * @memberof oscap_string_map |
| 462 | */ |
| 463 | const char *oscap_enum_to_stringcxx . . $ oscap_enum_to_string(64165c519d473d40).(const struct oscap_string_mapcxx . . $ oscap_string_map# *map, int val); |
| 464 | |
| 465 | /** |
| 466 | * Split a string. |
| 467 | * Split string using given delimiter. |
| 468 | * Produces NULL-terminated array of strings. |
| 469 | * Modifies its first argument! |
| 470 | * @param str String we want to split |
| 471 | * @param delim Delimiter of string parts |
| 472 | */ |
| 473 | char **oscap_splitcxx . . $ oscap_split(25e857aacd75b4b6).(char *str, const char *delim); |
| 474 | |
| 475 | /** |
| 476 | * compare two strings ignoring case |
| 477 | * @param s1 first string |
| 478 | * @param s2 second string |
| 479 | * @return an integer less than, equal to, or greater than zero if s1 is, |
| 480 | * after ignoring case, found to be less than, to match, or be greater |
| 481 | * than s2, respectively. |
| 482 | */ |
| 483 | int oscap_strcasecmpcxx . . $ oscap_strcasecmp(5ae29d726ae57f88).(const char *s1, const char *s2); |
| 484 | |
| 485 | /** |
| 486 | * compare two strings ignoring case |
| 487 | * @param s1 first string |
| 488 | * @param s2 second string |
| 489 | * @param n compare no more than n bytes of s1 and s2 |
| 490 | * @return an integer less than, equal to, or greater than zero if s1 is, |
| 491 | * after ignoring case, found to be less than, to match, or be greater |
| 492 | * than s2, respectively. |
| 493 | */ |
| 494 | int oscap_strncasecmpcxx . . $ oscap_strncasecmp(fd278af2f295fa2e).(const char *s1, const char *s2, size_tcxx . . $ size_t# n); |
| 495 | |
| 496 | /** |
| 497 | * Get string describing error number |
| 498 | * @param errnum error number |
| 499 | * @param buf buffer to hold error string |
| 500 | * @param buflen size of buffer |
| 501 | */ |
| 502 | char *oscap_strerror_rcxx . . $ oscap_strerror_r(c3cf521967a2a310).(int errnum, char *buf, size_tcxx . . $ size_t# buflen); |
| 503 | |
| 504 | #ifndef OS_WINDOWS |
| 505 | /** |
| 506 | * Open file for reading with prefix added to its name. |
| 507 | * Caller is responsible for closing the handle. |
| 508 | * @param prefix the prefix, might be NULL or empty |
| 509 | * @param path the file name with the path |
| 510 | * @return FILE* handle of opened file |
| 511 | * NULL on failure |
| 512 | */ |
| 513 | FILEcxx . . $ FILE# *oscap_fopen_with_prefixcxx . . $ oscap_fopen_with_prefix(82ec0787cd37e0a3).(const char *prefix, const char *path); |
| 514 | #endif |
| 515 | |
| 516 | #ifdef OS_WINDOWS |
| 517 | /** |
| 518 | * Convert wide character string to a C string (UTF-16 to UTF-8) |
| 519 | * It is a wrapper around Windows API WideCharToMultiByte(). |
| 520 | * Caller is responsible for freein the returned string. |
| 521 | * @param wstr wide string |
| 522 | * @return C string |
| 523 | */ |
| 524 | char *oscap_windows_wstr_to_str(const wchar_t *wstr); |
| 525 | |
| 526 | /** |
| 527 | * Convert C string to wide character string (UTF-8 to UTF-16) |
| 528 | * It is a wrapper around Windows API MultiByteToWideChar(). |
| 529 | * Caller is responsible for freeing the returned string. |
| 530 | * @param str C string |
| 531 | * @return wide string |
| 532 | */ |
| 533 | wchar_t *oscap_windows_str_to_wstr(const char *str); |
| 534 | |
| 535 | /** |
| 536 | * Get formated message about Windows API errors. |
| 537 | * It is a wrapper around Windows API FormatMessage(). |
| 538 | * Caller is responsible for freeing the returned string. |
| 539 | * @param error_code Error code returned by a Windows API call. |
| 540 | * @return Error message in a form of a C string. |
| 541 | */ |
| 542 | char *oscap_windows_error_message(unsigned long error_code); |
| 543 | #endif |
| 544 | |
| 545 | /** |
| 546 | * Open a file for writing. |
| 547 | * The main difference from fopen() is that if the file exists but its opening |
| 548 | * for writing fails as permission denied, it will attempt to open it again |
| 549 | * without the O_CREAT flag. This is useful when writing to world-writeable |
| 550 | * directories with sticky bit such as /tmp on systems with fs.protected_regular |
| 551 | * turned on. |
| 552 | * @param filename name of the file to be opened |
| 553 | * @return file descriptor or -1 on error |
| 554 | */ |
| 555 | int oscap_open_writablecxx . . $ oscap_open_writable(5c4306d8553b3696).(const char *filename); |
| 556 | |
| 557 | /** |
| 558 | * Check if a path starts with the given prefix |
| 559 | * @param path file system path |
| 560 | * @param prefix file system path that will be tested if it's a prefix of the path parameter |
| 561 | * @return true or false |
| 562 | */ |
| 563 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_path_startswithcxx . . $ oscap_path_startswith(b7a7aa4a824b7595).(const char *path, const char *prefix); |
| 564 | |
| 565 | /** |
| 566 | * Concatenate 2 strings |
| 567 | * Convenience wrapper over strncat. |
| 568 | * @param str1 string 1 |
| 569 | * @param str2 string 2 |
| 570 | * @return string 1 with string 2 appended inside |
| 571 | */ |
| 572 | char *oscap_concatcxx . . $ oscap_concat(5bd1a890f71b1efd).(char *str1, char *str2); |
| 573 | |
| 574 | /** |
| 575 | * Remove excess leading and trailing whitespace from a string |
| 576 | * Caller is responsible for freeing the returned string. |
| 577 | * @param str input string |
| 578 | * @return A new string with excess leading and trailing whitespace removed, or NULL if input is NULL |
| 579 | */ |
| 580 | char *oscap_remove_excess_whitespacecxx . . $ oscap_remove_excess_whitespace(a8b8e32347182212).(const char *str); |
| 581 | |
| 582 | #endif /* OSCAP_UTIL_H_ */ |