GNUnet 0.22.2
buffer.c
Go to the documentation of this file.
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 under the
6 terms of the GNU Affero General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
12
13 You should have received a copy of the GNU Affero General Public License along with
14 GNUnet; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15*/
22#include "platform.h"
23#include "gnunet_util_lib.h"
24
34void
36 size_t capacity)
37{
38 /* Buffer should be zero-initialized */
39 GNUNET_assert (0 == buf->mem);
40 GNUNET_assert (0 == buf->capacity);
41 GNUNET_assert (0 == buf->position);
42 buf->mem = GNUNET_malloc (capacity);
43 buf->capacity = capacity;
44 buf->warn_grow = GNUNET_YES;
45}
46
47
54void
56 size_t n)
57{
58 size_t new_capacity = buf->position + n;
59
60 /* guard against overflow */
61 GNUNET_assert (new_capacity >= buf->position);
62 if (new_capacity <= buf->capacity)
63 return;
64 /* warn if calculation of expected size was wrong */
66 if (new_capacity < buf->capacity * 2)
67 new_capacity = buf->capacity * 2;
68 buf->capacity = new_capacity;
69 if (NULL != buf->mem)
70 buf->mem = GNUNET_realloc (buf->mem, new_capacity);
71 else
72 buf->mem = GNUNET_malloc (new_capacity);
73}
74
75
85void
87 const char *data,
88 size_t len)
89{
91 memcpy (buf->mem + buf->position, data, len);
92 buf->position += len;
93}
94
95
102void
104 const char *str)
105{
106 size_t len = strlen (str);
107
108 GNUNET_buffer_write (buf, str, len);
109}
110
111
122char *
124{
125 char *res;
126
127 /* ensure 0-termination */
128 if ( (0 == buf->position) || ('\0' != buf->mem[buf->position - 1]))
129 {
131 buf->mem[buf->position++] = '\0';
132 }
133 res = buf->mem;
134 memset (buf, 0, sizeof (struct GNUNET_Buffer));
135 return res;
136}
137
138
148void *
150{
151 void *res;
152 *size = buf->position;
153 res = buf->mem;
154 memset (buf, 0, sizeof (struct GNUNET_Buffer));
155 return res;
156}
157
158
164void
166{
167 GNUNET_free (buf->mem);
168 memset (buf, 0, sizeof (struct GNUNET_Buffer));
169}
170
171
180void
181GNUNET_buffer_write_path (struct GNUNET_Buffer *buf, const char *str)
182{
183 size_t len = strlen (str);
184
185 while ( (0 != len) && ('/' == str[0]) )
186 {
187 str++;
188 len--;
189 }
190 if ( (0 == buf->position) || ('/' != buf->mem[buf->position - 1]) )
191 {
193 buf->mem[buf->position++] = '/';
194 }
195 GNUNET_buffer_write (buf, str, len);
196}
197
198
209void
210GNUNET_buffer_write_fstr (struct GNUNET_Buffer *buf, const char *fmt, ...)
211{
212 va_list args;
213
214 va_start (args, fmt);
216 va_end (args);
217}
218
219
230void
232 const char *fmt,
233 va_list args)
234{
235 int res;
236 va_list args2;
237
238 va_copy (args2, args);
239 res = vsnprintf (NULL, 0, fmt, args2);
240 va_end (args2);
241
242 GNUNET_assert (res >= 0);
244
245 va_copy (args2, args);
246 res = vsnprintf (buf->mem + buf->position, res + 1, fmt, args2);
247 va_end (args2);
248
249 GNUNET_assert (res >= 0);
250 buf->position += res;
251 GNUNET_assert (buf->position <= buf->capacity);
252}
253
254
264void
266 const void *data,
267 size_t data_len)
268{
269 size_t outlen;
270
271 GNUNET_assert (data_len <= SIZE_MAX / 8);
272 outlen = data_len * 8;
273
274 // https://bugs.gnunet.org/view.php?id=9279#c23545
275 outlen = (outlen + 4) / 5;
277 outlen);
278 GNUNET_assert (NULL !=
280 data_len,
281 (buf->mem
282 + buf->position),
283 outlen));
284 buf->position += outlen;
285 GNUNET_assert (buf->position <= buf->capacity);
286}
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...)
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
Definition: buffer.c:210
static char * data
The data to insert into the dht.
static char * res
Currently read line or NULL on EOF.
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition: buffer.c:123
void GNUNET_buffer_write_data_encoded(struct GNUNET_Buffer *buf, const void *data, size_t data_len)
Write data encoded via GNUNET_STRINGS_data_to_string to the buffer.
Definition: buffer.c:265
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:55
void * GNUNET_buffer_reap(struct GNUNET_Buffer *buf, size_t *size)
Clear the buffer and return its contents.
Definition: buffer.c:149
void GNUNET_buffer_write_vfstr(struct GNUNET_Buffer *buf, const char *fmt, va_list args)
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
Definition: buffer.c:231
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
void GNUNET_buffer_prealloc(struct GNUNET_Buffer *buf, size_t capacity)
Initialize a buffer with the given capacity.
Definition: buffer.c:35
void GNUNET_buffer_write_path(struct GNUNET_Buffer *buf, const char *str)
Write a path component to a buffer, ensuring that there is exactly one slash between the previous con...
Definition: buffer.c:181
void GNUNET_buffer_write_str(struct GNUNET_Buffer *buf, const char *str)
Write a 0-terminated string to a buffer, excluding the 0-terminator.
Definition: buffer.c:103
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:165
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:732
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define SIZE_MAX
Definition: platform.h:208
Dynamically growing buffer.
size_t capacity
Capacity of the buffer.
int warn_grow
Log a warning if the buffer is grown over its initially allocated capacity.
size_t position
Current write position.
char * mem
Backing memory.