gnunet_buffer_lib.h (5842B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2020 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 21 /** 22 * @addtogroup libgnunetutil 23 * Multi-function utilities library for GNUnet programs 24 * @{ 25 * 26 * Common buffer management functions. 27 * 28 * @author Florian Dold 29 */ 30 31 #if ! defined (__GNUNET_UTIL_LIB_H_INSIDE__) 32 #error "Only <gnunet_util_lib.h> can be included directly." 33 #endif 34 35 #ifndef GNUNET_BUFFER_LIB_H 36 #define GNUNET_BUFFER_LIB_H 37 38 /** 39 * Dynamically growing buffer. Can be used to construct 40 * strings and other objects with dynamic size. 41 * 42 * This structure should, in most cases, be stack-allocated and 43 * zero-initialized, like: 44 * 45 * struct GNUNET_Buffer my_buffer = { 0 }; 46 */ 47 struct GNUNET_Buffer 48 { 49 /** 50 * Capacity of the buffer. 51 */ 52 size_t capacity; 53 54 /** 55 * Current write position. 56 */ 57 size_t position; 58 59 /** 60 * Backing memory. 61 */ 62 char *mem; 63 64 /** 65 * Log a warning if the buffer is grown over its initially allocated capacity. 66 */ 67 int warn_grow; 68 }; 69 70 71 /** 72 * Initialize a buffer with the given capacity. 73 * 74 * When a buffer is allocated with this function, a warning is logged 75 * when the buffer exceeds the initial capacity. 76 * 77 * @param buf the buffer to initialize 78 * @param capacity the capacity (in bytes) to allocate for @a buf 79 */ 80 void 81 GNUNET_buffer_prealloc (struct GNUNET_Buffer *buf, 82 size_t capacity); 83 84 85 /** 86 * Informs the buffer library to expect this buffer to be potentially 87 * very large (exceeding #GNUNET_malloc() limits). 88 * 89 * @param[in,out] buf buffer to set as large buffer 90 */ 91 void 92 GNUNET_buffer_large (struct GNUNET_Buffer *buf); 93 94 95 /** 96 * Make sure that at least @a n bytes remaining in the buffer. 97 * 98 * @param buf buffer to potentially grow 99 * @param n number of bytes that should be available to write 100 */ 101 void 102 GNUNET_buffer_ensure_remaining (struct GNUNET_Buffer *buf, 103 size_t n); 104 105 106 /** 107 * Write bytes to the buffer. 108 * 109 * Grows the buffer if necessary. 110 * 111 * @param buf buffer to write to 112 * @param data data to read from 113 * @param len number of bytes to copy from @a data to @a buf 114 * 115 */ 116 void 117 GNUNET_buffer_write (struct GNUNET_Buffer *buf, 118 const char *data, 119 size_t len); 120 121 122 /** 123 * Write a 0-terminated string to a buffer, excluding the 0-terminator. 124 * 125 * Grows the buffer if necessary. 126 * 127 * @param buf the buffer to write to 128 * @param str the string to write to @a buf 129 */ 130 void 131 GNUNET_buffer_write_str (struct GNUNET_Buffer *buf, 132 const char *str); 133 134 135 /** 136 * Write data encoded via #GNUNET_STRINGS_data_to_string to the buffer. 137 * 138 * Grows the buffer if necessary. 139 * 140 * @param buf buffer to write to 141 * @param data data to read from 142 * @param data_len number of bytes to copy from @a data to @a buf 143 */ 144 void 145 GNUNET_buffer_write_data_encoded (struct GNUNET_Buffer *buf, 146 const void *data, 147 size_t data_len); 148 149 150 /** 151 * Write a path component to a buffer, ensuring that 152 * there is exactly one slash between the previous contents 153 * of the buffer and the new string. 154 * 155 * @param buf buffer to write to 156 * @param str string containing the new path component 157 */ 158 void 159 GNUNET_buffer_write_path (struct GNUNET_Buffer *buf, 160 const char *str); 161 162 163 /** 164 * Write a 0-terminated formatted string to a buffer, excluding the 165 * 0-terminator. 166 * 167 * Grows the buffer if necessary. 168 * 169 * @param buf the buffer to write to 170 * @param fmt format string 171 * @param ... format arguments 172 */ 173 void 174 GNUNET_buffer_write_fstr (struct GNUNET_Buffer *buf, 175 const char *fmt, 176 ...) 177 __attribute__ ((format (printf, 2, 3))); 178 179 180 /** 181 * Write a 0-terminated formatted string to a buffer, excluding the 182 * 0-terminator. 183 * 184 * Grows the buffer if necessary. 185 * 186 * @param buf the buffer to write to 187 * @param fmt format string 188 * @param args format argument list 189 */ 190 void 191 GNUNET_buffer_write_vfstr (struct GNUNET_Buffer *buf, 192 const char *fmt, 193 va_list args); 194 195 196 /** 197 * Clear the buffer and return the string it contained. 198 * The caller is responsible to eventually #GNUNET_free 199 * the returned string. 200 * 201 * The returned string is always 0-terminated. 202 * 203 * @param buf the buffer to reap the string from 204 * @returns the buffer contained in the string 205 */ 206 char * 207 GNUNET_buffer_reap_str (struct GNUNET_Buffer *buf); 208 209 210 /** 211 * Clear the buffer and return its contents. 212 * The caller is responsible to eventually #GNUNET_free 213 * the returned data. 214 * 215 * @param buf the buffer to reap the contents from 216 * @param size where to store the size of the returned data 217 * @returns the data contained in the string 218 */ 219 void * 220 GNUNET_buffer_reap (struct GNUNET_Buffer *buf, 221 size_t *size); 222 223 224 /** 225 * Free the backing memory of the given buffer. 226 * Does not free the memory of the buffer control structure, 227 * which is typically stack-allocated. 228 * 229 * @param[in] buf buffer to clear 230 */ 231 void 232 GNUNET_buffer_clear (struct GNUNET_Buffer *buf); 233 234 235 #endif 236 237 /** @} */ /* end of group addition */