| 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 | * Maros Barabas <[email protected]> |
| 21 | * Lukas Kuklinek <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 25 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 26 | #endif |
| 27 | |
| 28 | #include <time.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <string.h> |
| 31 | #include <ctype.h> |
| 32 | #include <limits.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <math.h> |
| 35 | #include <sys/stat.h> |
| 36 | |
| 37 | #include "util.h"cxx . . $ `<file>/src/common/util.h`/ |
| 38 | #include "_error.h"cxx . . $ `<file>/src/common/_error.h`/ |
| 39 | #include "oscap.h"cxx . . $ `<file>/src/common/public/oscap.h`/ |
| 40 | #include "oscap_helpers.h"cxx . . $ `<file>/src/common/public/oscap_helpers.h`/ |
| 41 | #include "oscap_pcre.h"cxx . . $ `<file>/src/common/oscap_pcre.h`/ |
| 42 | #include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/ |
| 43 | |
| 44 | #ifdef OS_WINDOWS |
| 45 | #include <stdlib.h> |
| 46 | #include <windows.h> |
| 47 | #else |
| 48 | #include <libgen.h> |
| 49 | #include <strings.h> |
| 50 | #endif |
| 51 | |
| 52 | #define PATH_SEPARATORcxx . . $ `src/common/util.c:52:9`! '/' |
| 53 | |
| 54 | char *oscap_generate_random_stringcxx . . $ oscap_generate_random_string(d2dea573f08ae648).(size_t len, char *charset) |
| 55 | { |
| 56 | char default_charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 57 | char *res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 58 | charset = (charset != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && strlencxx . . $ strlen(751346f8406fd082).(charset) > 0) ? charset : default_charset; |
| 59 | size_t charset_len = strlencxx . . $ strlen(751346f8406fd082).(charset); |
| 60 | if (len > 0) { |
| 61 | srandcxx . . $ srand(cb2f890c2fdad230).(timecxx . . $ time(7f38134db6521d23).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)); |
| 62 | res = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(len+1); |
| 63 | res[len] = 0; |
| 64 | while (len-- > 0) { |
| 65 | size_t index = (double) randcxx . . $ rand(b126dc7c1de90089).() / RAND_MAXcxx . . $ `<external>/10696550936717951914/stdlib.h:87:9`! * (charset_len-1); |
| 66 | res[len] = charset[index]; |
| 67 | } |
| 68 | } |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | int oscap_string_to_enumcxx . . $ oscap_string_to_enum(851d6ce10579b7d8).(const struct oscap_string_mapcxx . . $ oscap_string_map# *map, const char *str) |
| 73 | { |
| 74 | __attribute__nonnull__cxx . . $ `src/common/util.h:39:9`!(map); |
| 75 | |
| 76 | while (map->stringcxx . . $ oscap_string_map#string. && (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || strcmpcxx . . $ strcmp(5ae29d726ae57f88).(map->stringcxx . . $ oscap_string_map#string., str) != 0)) |
| 77 | ++map; |
| 78 | return map->valuecxx . . $ oscap_string_map#value.; |
| 79 | } |
| 80 | |
| 81 | const char *oscap_enum_to_stringcxx . . $ oscap_enum_to_string(64165c519d473d40).(const struct oscap_string_mapcxx . . $ oscap_string_map# *map, int val) |
| 82 | { |
| 83 | __attribute__nonnull__cxx . . $ `src/common/util.h:39:9`!(map); |
| 84 | |
| 85 | while (map->stringcxx . . $ oscap_string_map#string. && map->valuecxx . . $ oscap_string_map#value. != val) |
| 86 | ++map; |
| 87 | return map->stringcxx . . $ oscap_string_map#string.; |
| 88 | } |
| 89 | |
| 90 | static const size_t CPE_SPLIT_INIT_ALLOCcxx . . $ CPE_SPLIT_INIT_ALLOC. = 8; |
| 91 | |
| 92 | char **oscap_splitcxx . . $ oscap_split(25e857aacd75b4b6).(char *str, const char *delim) |
| 93 | { |
| 94 | |
| 95 | __attribute__nonnull__cxx . . $ `src/common/util.h:39:9`!(str); |
| 96 | |
| 97 | char **stringp = &str; |
| 98 | int alloc = CPE_SPLIT_INIT_ALLOCcxx . . $ CPE_SPLIT_INIT_ALLOC.; |
| 99 | char **fields = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(alloc * sizeof(char *)); |
| 100 | if (!fields) |
| 101 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 102 | |
| 103 | int i = 0; |
| 104 | while (*stringp) { |
| 105 | if (i + 2 > alloc) { |
| 106 | void *old = fields; |
| 107 | alloc *= 2; |
| 108 | fields = realloccxx . . $ realloc(417ab160f6baadc6).(fields, alloc * sizeof(char *)); |
| 109 | if (fields == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 110 | freecxx . . $ free(5d22bdacc48458e8).(old); |
| 111 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 112 | } |
| 113 | } |
| 114 | fields[i++] = strsepcxx . . $ strsep(f4782bac5b8a260e).(stringp, delim); |
| 115 | } |
| 116 | fields[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 117 | |
| 118 | return fields; |
| 119 | } |
| 120 | |
| 121 | char *oscap_trimcxx . . $ oscap_trim(7a9722d75b371431).(char *str) |
| 122 | { |
| 123 | int off, i = 0; |
| 124 | |
| 125 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 126 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 127 | |
| 128 | /* left trim */ |
| 129 | while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(str[i])) ++i; |
| 130 | |
| 131 | off = i; |
| 132 | |
| 133 | /* move */ |
| 134 | while (1) { |
| 135 | str[i - off] = str[i]; |
| 136 | if (str[i] == '\0') |
| 137 | break; |
| 138 | ++i; |
| 139 | } |
| 140 | |
| 141 | i -= off; |
| 142 | |
| 143 | /* right trim */ |
| 144 | while (i > 0) { |
| 145 | if (!isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(str[--i])) { |
| 146 | str[i + 1] = '\0'; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return str; |
| 152 | } |
| 153 | |
| 154 | char *oscap_vsprintfcxx . . $ oscap_vsprintf(db813db1ba4aba5a).(const char *fmt, va_listcxx . . $ va_list# ap) |
| 155 | { |
| 156 | if (fmt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 157 | |
| 158 | char *ret = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 159 | va_listcxx . . $ va_list# args; |
| 160 | va_copycxx . . $ `<external>/8105065126723003039/stdarg.h:56:9`!(args, ap); |
| 161 | |
| 162 | char foo[2]; |
| 163 | int length = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(foo, 1, fmt, ap); |
| 164 | if (length < 0) goto cleanup; |
| 165 | |
| 166 | ret = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(char) * (length + 1)); |
| 167 | vsprintfcxx . . $ vsprintf(d8921982374e5df0).(ret, fmt, args); |
| 168 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(ret[length] == '\0'); |
| 169 | |
| 170 | cleanup: |
| 171 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | char *oscap_sprintfcxx . . $ oscap_sprintf(a849077ff1b8d812).(const char *fmt, ...) |
| 176 | { |
| 177 | char *ret = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 178 | va_listcxx . . $ va_list# ap; |
| 179 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(ap, fmt); |
| 180 | ret = oscap_vsprintfcxx . . $ oscap_vsprintf(db813db1ba4aba5a).(fmt, ap); |
| 181 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(ap); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | char *oscap_rtrimcxx . . $ oscap_rtrim(dabe84363a524a9e).(char *str, char ch) |
| 186 | { |
| 187 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 188 | for (char *pos = str + strlencxx . . $ strlen(751346f8406fd082).(str) - 1; pos >= str; --pos) { |
| 189 | if (*pos == ch) *pos = '\0'; |
| 190 | else break; |
| 191 | } |
| 192 | return str; |
| 193 | } |
| 194 | |
| 195 | void oscap_strtouppercxx . . $ oscap_strtoupper(fda57c8427c60377).(char *str) |
| 196 | { |
| 197 | if (!str) return; |
| 198 | for (; *str; ++str) *str = touppercxx . . $ `<external>/17904084882347303253/ctype.h:221:11`!(*str); |
| 199 | } |
| 200 | |
| 201 | char *oscap_expand_ipv6cxx . . $ oscap_expand_ipv6(a8b8e32347182212).(const char *input) |
| 202 | { |
| 203 | // we have to do a double pass because we need to know the number of |
| 204 | // specified components, :: will fill up to a total of 8 if encountered |
| 205 | |
| 206 | const char* input_it = input; |
| 207 | unsigned short component_count = 1; // fencepost, we start with 1 |
| 208 | while (*input_it) { |
| 209 | if (*input_it == ':') { |
| 210 | // don't count ::1 as 2 components |
| 211 | if (input_it != input) |
| 212 | component_count++; |
| 213 | |
| 214 | // the next character is either a part of a component or another ':' |
| 215 | // in any case we don't want to count it |
| 216 | input_it++; |
| 217 | } |
| 218 | |
| 219 | input_it++; |
| 220 | } |
| 221 | |
| 222 | // IPv6 is at most eight 4-tuples of [0-9a-f] with 7 separators, plus \0 |
| 223 | char* ret = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(8 * 4 * sizeof(char) + 7 + 1); |
| 224 | char* output_it = ret; |
| 225 | |
| 226 | input_it = input; |
| 227 | while (*input_it) { |
| 228 | // signifies that we have finished writing a component this iteration |
| 229 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! closed_component = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 230 | |
| 231 | if (*input_it == ':') { |
| 232 | const char next = *(input_it + 1 * sizeof(char)); |
| 233 | if (next == ':') { |
| 234 | // skip one extra char to skip over the whole :: |
| 235 | input_it++; |
| 236 | |
| 237 | for (unsigned short i = component_count; i < 8; ++i) { |
| 238 | // don't add leading separators |
| 239 | if (output_it != ret) |
| 240 | *output_it++ = ':'; |
| 241 | |
| 242 | *output_it++ = '0'; |
| 243 | closed_component = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 244 | } |
| 245 | } |
| 246 | else { |
| 247 | closed_component = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 248 | } |
| 249 | } |
| 250 | else { |
| 251 | *output_it++ = *input_it; |
| 252 | } |
| 253 | |
| 254 | input_it++; |
| 255 | |
| 256 | // if this was not the last char of the input add a separator |
| 257 | if (closed_component && *input_it) |
| 258 | *output_it++ = ':'; |
| 259 | } |
| 260 | |
| 261 | *output_it = '\0'; |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | char *oscap_realpathcxx . . $ oscap_realpath(ed8193f262e4eda).(const char *path, char *resolved_path) |
| 267 | { |
| 268 | #ifdef OS_WINDOWS |
| 269 | return _fullpath(resolved_path, path, PATH_MAX); |
| 270 | #else |
| 271 | return realpathcxx . . $ realpath(ed8193f262e4eda).(path, resolved_path); |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | char *oscap_basenamecxx . . $ oscap_basename(7a9722d75b371431).(char *path) |
| 276 | { |
| 277 | #ifdef OS_WINDOWS |
| 278 | char fname[_MAX_FNAME]; |
| 279 | char ext[_MAX_EXT]; |
| 280 | _splitpath_s(path, NULL, 0, NULL, 0, fname, _MAX_FNAME, ext, _MAX_EXT); |
| 281 | size_t base_len = strlen(fname) + strlen(ext) + 1; |
| 282 | char *base = malloc(base_len); |
| 283 | strncpy(base, fname, base_len); |
| 284 | strncat(base, ext, base_len - strlen(base) - 1); |
| 285 | return base; |
| 286 | #else |
| 287 | char *base = basenamecxx . . $ `<external>/6296865783241590524/libgen.h:35:9`!(path); |
| 288 | return oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(base); |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | #ifdef OS_WINDOWS |
| 293 | char *oscap_dirname(char *path) |
| 294 | { |
| 295 | if (path == NULL || *path == '\0' || (strchr(path, '/') == NULL && strchr(path, '\\') == NULL)) { |
| 296 | return strdup("."); |
| 297 | } |
| 298 | char dir[_MAX_DIR]; |
| 299 | char drive[_MAX_DRIVE]; |
| 300 | char dirname[_MAX_PATH]; |
| 301 | _splitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, NULL, 0, NULL, 0); |
| 302 | _makepath_s(dirname, _MAX_PATH, drive, dir, NULL, NULL); |
| 303 | oscap_rtrim(dirname, '/'); |
| 304 | oscap_rtrim(dirname, '\\'); |
| 305 | return oscap_strdup(dirname); |
| 306 | } |
| 307 | #else |
| 308 | char *oscap_dirnamecxx . . $ oscap_dirname(7a9722d75b371431).(char *path) |
| 309 | { |
| 310 | char *dirpath = dirnamecxx . . $ dirname(7a9722d75b371431).(path); |
| 311 | return oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(dirpath); |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | int oscap_strcasecmpcxx . . $ oscap_strcasecmp(5ae29d726ae57f88).(const char *s1, const char *s2) |
| 316 | { |
| 317 | #ifdef OS_WINDOWS |
| 318 | return _stricmp(s1, s2); |
| 319 | #else |
| 320 | return strcasecmpcxx . . $ strcasecmp(5ae29d726ae57f88).(s1, s2); |
| 321 | #endif |
| 322 | } |
| 323 | |
| 324 | int oscap_strncasecmpcxx . . $ oscap_strncasecmp(fd278af2f295fa2e).(const char *s1, const char *s2, size_t n) |
| 325 | { |
| 326 | #ifdef OS_WINDOWS |
| 327 | return _strnicmp(s1, s2, n); |
| 328 | #else |
| 329 | return strncasecmpcxx . . $ strncasecmp(fd278af2f295fa2e).(s1, s2, n); |
| 330 | #endif |
| 331 | } |
| 332 | |
| 333 | char *oscap_strtok_rcxx . . $ oscap_strtok_r(a42394c85d6397dd).(char *str, const char *delim, char **saveptr) |
| 334 | { |
| 335 | #ifdef OS_WINDOWS |
| 336 | return strtok_s(str, delim, saveptr); |
| 337 | #else |
| 338 | return strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(str, delim, saveptr); |
| 339 | #endif |
| 340 | } |
| 341 | |
| 342 | char *oscap_strerror_rcxx . . $ oscap_strerror_r(c3cf521967a2a310).(int errnum, char *buf, size_t buflen) |
| 343 | { |
| 344 | #ifdef OS_WINDOWS |
| 345 | strerror_s(buf, buflen, errnum); |
| 346 | return buf; |
| 347 | #elif defined(OS_APPLE) || defined(OS_FREEBSD) |
| 348 | strerror_r(errnum, buf, buflen); |
| 349 | return buf; |
| 350 | #else |
| 351 | return strerror_rcxx . . $ strerror_r(c3cf521967a2a310).(errnum, buf, buflen); |
| 352 | #endif |
| 353 | } |
| 354 | |
| 355 | char *oscap_path_joincxx . . $ oscap_path_join(a713fe7b9a57cf96).(const char *path1, const char *path2) |
| 356 | { |
| 357 | if (path1 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 358 | return oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(path2); |
| 359 | } |
| 360 | if (path2 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 361 | return oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(path1); |
| 362 | } |
| 363 | size_t path1_len = strlencxx . . $ strlen(751346f8406fd082).(path1); |
| 364 | size_t path2_len = strlencxx . . $ strlen(751346f8406fd082).(path2); |
| 365 | size_t path2_shift = 0; |
| 366 | while (path1_len >= 1 && path1[path1_len - 1] == PATH_SEPARATORcxx . . $ `src/common/util.c:52:9`!) { |
| 367 | path1_len--; |
| 368 | } |
| 369 | while (path2_shift < path2_len && path2[path2_shift] == PATH_SEPARATORcxx . . $ `src/common/util.c:52:9`!) { |
| 370 | path2_shift++; |
| 371 | } |
| 372 | path2_len -= path2_shift; |
| 373 | const size_t joined_path_len = path1_len + 1 + path2_len; |
| 374 | char *joined_path = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(joined_path_len + 1); |
| 375 | strncpycxx . . $ strncpy(255158a415b673b0).(joined_path, path1, path1_len); |
| 376 | joined_path[path1_len++] = PATH_SEPARATORcxx . . $ `src/common/util.c:52:9`!; |
| 377 | strncpycxx . . $ strncpy(255158a415b673b0).(joined_path + path1_len, path2 + path2_shift, path2_len); |
| 378 | joined_path[joined_path_len] = '\0'; |
| 379 | return joined_path; |
| 380 | } |
| 381 | |
| 382 | #ifndef OS_WINDOWS |
| 383 | FILEcxx . . $ FILE# *oscap_fopen_with_prefixcxx . . $ oscap_fopen_with_prefix(82ec0787cd37e0a3).(const char *prefix, const char *path) |
| 384 | { |
| 385 | FILEcxx . . $ FILE# *fp; |
| 386 | if (prefix != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 387 | char *path_with_prefix = oscap_sprintfcxx . . $ oscap_sprintf(a849077ff1b8d812).("%s%s", prefix, path); |
| 388 | fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(path_with_prefix, "r"); |
| 389 | freecxx . . $ free(5d22bdacc48458e8).(path_with_prefix); |
| 390 | } else { |
| 391 | fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, "r"); |
| 392 | } |
| 393 | return fp; |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #ifdef OS_WINDOWS |
| 398 | char *oscap_windows_wstr_to_str(const wchar_t *wstr) |
| 399 | { |
| 400 | if (wstr == NULL) { |
| 401 | return NULL; |
| 402 | } |
| 403 | const int required_size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); |
| 404 | char *str = malloc(required_size); |
| 405 | WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, required_size, NULL, NULL); |
| 406 | return str; |
| 407 | } |
| 408 | |
| 409 | wchar_t *oscap_windows_str_to_wstr(const char *str) |
| 410 | { |
| 411 | if (str == NULL) { |
| 412 | return NULL; |
| 413 | } |
| 414 | const int required_size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); |
| 415 | wchar_t *wstr = malloc(required_size * sizeof(wchar_t)); |
| 416 | MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, required_size); |
| 417 | return wstr; |
| 418 | } |
| 419 | |
| 420 | char *oscap_windows_error_message(unsigned long error_code) |
| 421 | { |
| 422 | wchar_t *buffer = NULL; |
| 423 | /* According to FormatMessage documentation, if FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 424 | * flag is set, the buffer must be freed by LocalFree and must be casted to LPWSTR. |
| 425 | */ |
| 426 | FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 427 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 428 | NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 429 | (LPWSTR) &buffer, 0, NULL); |
| 430 | char *error_message = oscap_windows_wstr_to_str(buffer); |
| 431 | LocalFree(buffer); |
| 432 | return error_message; |
| 433 | } |
| 434 | #endif |
| 435 | |
| 436 | int oscap_open_writablecxx . . $ oscap_open_writable(5c4306d8553b3696).(const char *filename) |
| 437 | { |
| 438 | #ifdef OS_WINDOWS |
| 439 | int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE); |
| 440 | #else |
| 441 | int fd = opencxx . . $ open(e156e3aa1e5c4ad1).(filename, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!|O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!|O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`!, |
| 442 | S_IRUSRcxx . . $ `<external>/531885356569766532/stat.h:168:9`!|S_IWUSRcxx . . $ `<external>/531885356569766532/stat.h:169:9`!|S_IRGRPcxx . . $ `<external>/531885356569766532/stat.h:180:9`!|S_IWGRPcxx . . $ `<external>/531885356569766532/stat.h:181:9`!|S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`!|S_IWOTHcxx . . $ `<external>/531885356569766532/stat.h:187:9`!); |
| 443 | #endif |
| 444 | if (fd == -1) { |
| 445 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!) { |
| 446 | /* File already exists and we aren't allowed to create a new one |
| 447 | with the same name */ |
| 448 | #ifdef OS_WINDOWS |
| 449 | fd = open(filename, O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE); |
| 450 | #else |
| 451 | fd = opencxx . . $ open(e156e3aa1e5c4ad1).(filename, O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!|O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`!, |
| 452 | S_IRUSRcxx . . $ `<external>/531885356569766532/stat.h:168:9`!|S_IWUSRcxx . . $ `<external>/531885356569766532/stat.h:169:9`!|S_IRGRPcxx . . $ `<external>/531885356569766532/stat.h:180:9`!|S_IWGRPcxx . . $ `<external>/531885356569766532/stat.h:181:9`!|S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`!|S_IWOTHcxx . . $ `<external>/531885356569766532/stat.h:187:9`!); |
| 453 | #endif |
| 454 | } |
| 455 | if (fd == -1) { |
| 456 | oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, |
| 457 | "Could not open output file '%s': %s", |
| 458 | filename, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 459 | return -1; |
| 460 | } |
| 461 | } |
| 462 | return fd; |
| 463 | } |
| 464 | |
| 465 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_path_startswithcxx . . $ oscap_path_startswith(b7a7aa4a824b7595).(const char *path, const char *prefix) |
| 466 | { |
| 467 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! res = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 468 | const char *del = "/"; |
| 469 | char *path_dup = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(path); |
| 470 | char **path_split = oscap_splitcxx . . $ oscap_split(25e857aacd75b4b6).(path_dup, del); |
| 471 | char *prefix_dup = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(prefix); |
| 472 | char **prefix_split = oscap_splitcxx . . $ oscap_split(25e857aacd75b4b6).(prefix_dup, del); |
| 473 | int i = 0, j = 0; |
| 474 | while (prefix_split[i] && path_split[j]) { |
| 475 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(prefix_split[i], "")) { |
| 476 | ++i; |
| 477 | continue; |
| 478 | } |
| 479 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(path_split[j], "")) { |
| 480 | ++j; |
| 481 | continue; |
| 482 | } |
| 483 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(prefix_split[i], path_split[j])) { |
| 484 | res = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 485 | break; |
| 486 | } |
| 487 | ++i; |
| 488 | ++j; |
| 489 | } |
| 490 | freecxx . . $ free(5d22bdacc48458e8).(path_dup); |
| 491 | freecxx . . $ free(5d22bdacc48458e8).(path_split); |
| 492 | freecxx . . $ free(5d22bdacc48458e8).(prefix_dup); |
| 493 | freecxx . . $ free(5d22bdacc48458e8).(prefix_split); |
| 494 | return res; |
| 495 | } |
| 496 | |
| 497 | char *oscap_concatcxx . . $ oscap_concat(5bd1a890f71b1efd).(char *str1, char *str2) |
| 498 | { |
| 499 | if (str1 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || str2 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 500 | return str1; |
| 501 | size_t str1_len = strlencxx . . $ strlen(751346f8406fd082).(str1); |
| 502 | size_t str2_len = strlencxx . . $ strlen(751346f8406fd082).(str2); |
| 503 | str1 = realloccxx . . $ realloc(417ab160f6baadc6).(str1, str1_len + str2_len + 1); |
| 504 | strncatcxx . . $ strncat(255158a415b673b0).(str1, str2, str2_len); |
| 505 | return str1; |
| 506 | } |
| 507 | |
| 508 | char *oscap_indentcxx . . $ oscap_indent(2050848944e00d54).(const char *str, int indent) |
| 509 | { |
| 510 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 511 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 512 | } |
| 513 | if (indent <= 0) { |
| 514 | return oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(str); |
| 515 | } |
| 516 | |
| 517 | size_t line_count = 1; |
| 518 | for (const char *p = str; *p; p++) { |
| 519 | if (*p == '\n' && *(p + 1) != '\0') { |
| 520 | line_count++; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | size_t str_len = strlencxx . . $ strlen(751346f8406fd082).(str); |
| 525 | size_t result_len = str_len + (indent * line_count); |
| 526 | char *result = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(result_len + 1); |
| 527 | if (result == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 528 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 529 | } |
| 530 | |
| 531 | char *dst = result; |
| 532 | const char *src = str; |
| 533 | |
| 534 | if (*src != '\n') { |
| 535 | for (int i = 0; i < indent; i++) { |
| 536 | *dst++ = ' '; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | while (*src) { |
| 541 | *dst++ = *src; |
| 542 | if (*src == '\n' && *(src + 1) != '\0' && *(src + 1) != '\n') { |
| 543 | // Add indent after newline (unless it's the last character) |
| 544 | for (int i = 0; i < indent; i++) { |
| 545 | *dst++ = ' '; |
| 546 | } |
| 547 | } |
| 548 | src++; |
| 549 | } |
| 550 | |
| 551 | *dst = '\0'; |
| 552 | return result; |
| 553 | } |
| 554 | |
| 555 | char *oscap_remove_excess_whitespacecxx . . $ oscap_remove_excess_whitespace(a8b8e32347182212).(const char *str) |
| 556 | { |
| 557 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 558 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 559 | } |
| 560 | |
| 561 | size_t str_len = strlencxx . . $ strlen(751346f8406fd082).(str); |
| 562 | char *result = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(str_len + 1); |
| 563 | if (result == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 564 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 565 | } |
| 566 | |
| 567 | const char *src = str; |
| 568 | char *dst = result; |
| 569 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! at_line_start = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 570 | int trailing_spaces = 0; |
| 571 | |
| 572 | while (*src) { |
| 573 | if (*src == '\n') { |
| 574 | trailing_spaces = 0; |
| 575 | if (!at_line_start) { |
| 576 | *dst++ = '\n'; |
| 577 | } |
| 578 | at_line_start = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 579 | src++; |
| 580 | } else if (at_line_start && isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*src)) { |
| 581 | src++; |
| 582 | } else if (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*src)) { |
| 583 | trailing_spaces++; |
| 584 | src++; |
| 585 | } else { |
| 586 | at_line_start = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 587 | for (int i = 0; i < trailing_spaces; i++) { |
| 588 | *dst++ = ' '; |
| 589 | } |
| 590 | trailing_spaces = 0; |
| 591 | *dst++ = *src++; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | *dst = '\0'; |
| 596 | return result; |
| 597 | } |