| 1 | /* |
| 2 | * Copyright 2015 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 | * Jan Černý <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 24 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 25 | #endif |
| 26 | |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include "oscap_buffer.h"cxx . . $ `<file>/src/common/oscap_buffer.h`/ |
| 31 | |
| 32 | #define INITIAL_CAPACITYcxx . . $ `src/common/oscap_buffer.c:32:9`! 64 |
| 33 | |
| 34 | /** |
| 35 | * Buffer with unlimited length |
| 36 | * contains: |
| 37 | * - pointer to data, |
| 38 | * - actual length of string, |
| 39 | * - capacity of allocated memory |
| 40 | */ |
| 41 | struct oscap_buffercxx . . $ oscap_buffer# { |
| 42 | char *datacxx . . $ oscap_buffer#data.; |
| 43 | size_t lengthcxx . . $ oscap_buffer#length.; |
| 44 | size_t capacitycxx . . $ oscap_buffer#capacity.; |
| 45 | }; |
| 46 | |
| 47 | struct oscap_buffercxx . . $ oscap_buffer# *oscap_buffer_newcxx . . $ oscap_buffer_new(78ca002e694d04d).() |
| 48 | { |
| 49 | struct oscap_buffercxx . . $ oscap_buffer# *s; |
| 50 | s = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(struct oscap_buffercxx . . $ oscap_buffer#)); |
| 51 | s->datacxx . . $ oscap_buffer#data. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(INITIAL_CAPACITYcxx . . $ `src/common/oscap_buffer.c:32:9`!); |
| 52 | s->datacxx . . $ oscap_buffer#data.[0] = '\0'; |
| 53 | s->lengthcxx . . $ oscap_buffer#length. = 0; |
| 54 | s->capacitycxx . . $ oscap_buffer#capacity. = INITIAL_CAPACITYcxx . . $ `src/common/oscap_buffer.c:32:9`!; |
| 55 | return s; |
| 56 | } |
| 57 | |
| 58 | void oscap_buffer_clearcxx . . $ oscap_buffer_clear(952d67516e6b9978).(struct oscap_buffercxx . . $ oscap_buffer# *s) |
| 59 | { |
| 60 | s->datacxx . . $ oscap_buffer#data.[0] = '\0'; |
| 61 | s->lengthcxx . . $ oscap_buffer#length. = 0; |
| 62 | } |
| 63 | |
| 64 | void oscap_buffer_freecxx . . $ oscap_buffer_free(952d67516e6b9978).(struct oscap_buffercxx . . $ oscap_buffer# *s) |
| 65 | { |
| 66 | if (s != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 67 | freecxx . . $ free(5d22bdacc48458e8).(s->datacxx . . $ oscap_buffer#data.); |
| 68 | freecxx . . $ free(5d22bdacc48458e8).(s); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | char* oscap_buffer_bequeathcxx . . $ oscap_buffer_bequeath(2ad8c279d2fffb0f).(struct oscap_buffercxx . . $ oscap_buffer# *s){ |
| 73 | char* str = s->datacxx . . $ oscap_buffer#data.; |
| 74 | freecxx . . $ free(5d22bdacc48458e8).(s); |
| 75 | return str; |
| 76 | } |
| 77 | |
| 78 | void oscap_buffer_append_binary_datacxx . . $ oscap_buffer_append_binary_data(ba3f817601beb05d).(struct oscap_buffercxx . . $ oscap_buffer# *s, const char *data, const size_t append_length) |
| 79 | { |
| 80 | if (s == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 81 | return; |
| 82 | if (s->lengthcxx . . $ oscap_buffer#length. + append_length + 1 > s->capacitycxx . . $ oscap_buffer#capacity.) { |
| 83 | /* Aligning allocated memory to multiples of INITIAL_CAPACITY. |
| 84 | * We pass to realloc the nearest greater muliple of INITIAL_CAPACITY |
| 85 | * rather than only needed capacity in order to not fragment |
| 86 | * the memory. |
| 87 | */ |
| 88 | size_t current_capacity = s->capacitycxx . . $ oscap_buffer#capacity.; |
| 89 | s->capacitycxx . . $ oscap_buffer#capacity. = ((s->capacitycxx . . $ oscap_buffer#capacity. + append_length - 1) / INITIAL_CAPACITYcxx . . $ `src/common/oscap_buffer.c:32:9`! + 1) * INITIAL_CAPACITYcxx . . $ `src/common/oscap_buffer.c:32:9`!; |
| 90 | void *new_data = realloccxx . . $ realloc(417ab160f6baadc6).(s->datacxx . . $ oscap_buffer#data., s->capacitycxx . . $ oscap_buffer#capacity.); |
| 91 | if (new_data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 92 | s->capacitycxx . . $ oscap_buffer#capacity. = current_capacity; |
| 93 | return; |
| 94 | } |
| 95 | s->datacxx . . $ oscap_buffer#data. = new_data; |
| 96 | } |
| 97 | |
| 98 | memcpycxx . . $ memcpy(cc817cb175f282d).(s->datacxx . . $ oscap_buffer#data. + s->lengthcxx . . $ oscap_buffer#length., data, append_length); |
| 99 | s->lengthcxx . . $ oscap_buffer#length. += append_length; |
| 100 | s->datacxx . . $ oscap_buffer#data.[s->lengthcxx . . $ oscap_buffer#length.] = '\0'; |
| 101 | } |
| 102 | |
| 103 | void oscap_buffer_append_stringcxx . . $ oscap_buffer_append_string(df7c25e1c8c8328c).(struct oscap_buffercxx . . $ oscap_buffer# *s, const char *t) |
| 104 | { |
| 105 | if (t == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 106 | return; |
| 107 | size_t append_length = strlencxx . . $ strlen(751346f8406fd082).(t); |
| 108 | oscap_buffer_append_binary_datacxx . . $ oscap_buffer_append_binary_data(ba3f817601beb05d).(s, t, append_length); |
| 109 | } |
| 110 | |
| 111 | char *oscap_buffer_get_rawcxx . . $ oscap_buffer_get_raw(3cd9678435dc2626).(const struct oscap_buffercxx . . $ oscap_buffer# *s) |
| 112 | { |
| 113 | return s->datacxx . . $ oscap_buffer#data.; |
| 114 | } |
| 115 | |
| 116 | size_t oscap_buffer_get_lengthcxx . . $ oscap_buffer_get_length(6e8c5100821ac051).(const struct oscap_buffercxx . . $ oscap_buffer# *s){ |
| 117 | return s->lengthcxx . . $ oscap_buffer#length.; |
| 118 | } |
| 119 | |
| 120 |