gnunet_buffer_lib.h (5316B)
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, size_t capacity); 82 83 84 /** 85 * Make sure that at least @a n bytes remaining in the buffer. 86 * 87 * @param buf buffer to potentially grow 88 * @param n number of bytes that should be available to write 89 */ 90 void 91 GNUNET_buffer_ensure_remaining (struct GNUNET_Buffer *buf, size_t n); 92 93 94 /** 95 * Write bytes to the buffer. 96 * 97 * Grows the buffer if necessary. 98 * 99 * @param buf buffer to write to 100 * @param data data to read from 101 * @param len number of bytes to copy from @a data to @a buf 102 * 103 */ 104 void 105 GNUNET_buffer_write (struct GNUNET_Buffer *buf, const char *data, size_t len); 106 107 108 /** 109 * Write a 0-terminated string to a buffer, excluding the 0-terminator. 110 * 111 * Grows the buffer if necessary. 112 * 113 * @param buf the buffer to write to 114 * @param str the string to write to @a buf 115 */ 116 void 117 GNUNET_buffer_write_str (struct GNUNET_Buffer *buf, const char *str); 118 119 120 /** 121 * Write data encoded via #GNUNET_STRINGS_data_to_string to the buffer. 122 * 123 * Grows the buffer if necessary. 124 * 125 * @param buf buffer to write to 126 * @param data data to read from 127 * @param data_len number of bytes to copy from @a data to @a buf 128 */ 129 void 130 GNUNET_buffer_write_data_encoded (struct GNUNET_Buffer *buf, 131 const void *data, 132 size_t data_len); 133 134 135 /** 136 * Write a path component to a buffer, ensuring that 137 * there is exactly one slash between the previous contents 138 * of the buffer and the new string. 139 * 140 * @param buf buffer to write to 141 * @param str string containing the new path component 142 */ 143 void 144 GNUNET_buffer_write_path (struct GNUNET_Buffer *buf, const char *str); 145 146 147 /** 148 * Write a 0-terminated formatted string to a buffer, excluding the 149 * 0-terminator. 150 * 151 * Grows the buffer if necessary. 152 * 153 * @param buf the buffer to write to 154 * @param fmt format string 155 * @param ... format arguments 156 */ 157 void 158 GNUNET_buffer_write_fstr (struct GNUNET_Buffer *buf, const char *fmt, ...) 159 __attribute__ ((format (printf, 2, 3))); 160 161 162 /** 163 * Write a 0-terminated formatted string to a buffer, excluding the 164 * 0-terminator. 165 * 166 * Grows the buffer if necessary. 167 * 168 * @param buf the buffer to write to 169 * @param fmt format string 170 * @param args format argument list 171 */ 172 void 173 GNUNET_buffer_write_vfstr (struct GNUNET_Buffer *buf, const char *fmt, va_list 174 args); 175 176 177 /** 178 * Clear the buffer and return the string it contained. 179 * The caller is responsible to eventually #GNUNET_free 180 * the returned string. 181 * 182 * The returned string is always 0-terminated. 183 * 184 * @param buf the buffer to reap the string from 185 * @returns the buffer contained in the string 186 */ 187 char * 188 GNUNET_buffer_reap_str (struct GNUNET_Buffer *buf); 189 190 191 /** 192 * Clear the buffer and return its contents. 193 * The caller is responsible to eventually #GNUNET_free 194 * the returned data. 195 * 196 * @param buf the buffer to reap the contents from 197 * @param size where to store the size of the returned data 198 * @returns the data contained in the string 199 */ 200 void * 201 GNUNET_buffer_reap (struct GNUNET_Buffer *buf, size_t *size); 202 203 204 /** 205 * Free the backing memory of the given buffer. 206 * Does not free the memory of the buffer control structure, 207 * which is typically stack-allocated. 208 */ 209 void 210 GNUNET_buffer_clear (struct GNUNET_Buffer *buf); 211 212 213 #endif 214 215 /** @} */ /* end of group addition */