GNUnet debian-0.26.1
 
Loading...
Searching...
No Matches
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
25void
27 size_t capacity)
28{
29 /* Buffer should be zero-initialized */
30 GNUNET_assert (0 == buf->mem);
31 GNUNET_assert (0 == buf->capacity);
32 GNUNET_assert (0 == buf->position);
33 buf->mem = GNUNET_malloc (capacity);
34 buf->capacity = capacity;
35 buf->warn_grow = GNUNET_YES;
36}
37
38
39void
41 size_t n)
42{
43 size_t new_capacity = buf->position + n;
44
45 /* guard against overflow */
46 GNUNET_assert (new_capacity >= buf->position);
47 if (new_capacity <= buf->capacity)
48 return;
49 /* warn if calculation of expected size was wrong */
51 if (new_capacity < buf->capacity * 2)
52 new_capacity = buf->capacity * 2;
53 buf->capacity = new_capacity;
54 if (NULL != buf->mem)
55 buf->mem = GNUNET_realloc (buf->mem,
56 new_capacity);
57 else
58 buf->mem = GNUNET_malloc (new_capacity);
59}
60
61
62void
64 const char *data,
65 size_t len)
66{
68 len);
69 memcpy (buf->mem + buf->position,
70 data,
71 len);
72 buf->position += len;
73}
74
75
76void
78 const char *str)
79{
80 size_t len = strlen (str);
81
83 str,
84 len);
85}
86
87
88char *
90{
91 char *res;
92
93 /* ensure 0-termination */
94 if ( (0 == buf->position) ||
95 ('\0' != buf->mem[buf->position - 1]) )
96 {
98 buf->mem[buf->position++] = '\0';
99 }
100 res = buf->mem;
101 memset (buf,
102 0,
103 sizeof (struct GNUNET_Buffer));
104 return res;
105}
106
107
108void *
110 size_t *size)
111{
112 void *res = buf->mem;
113
114 *size = buf->position;
115 memset (buf,
116 0,
117 sizeof (struct GNUNET_Buffer));
118 return res;
119}
120
121
122void
124{
125 GNUNET_free (buf->mem);
126 memset (buf,
127 0,
128 sizeof (struct GNUNET_Buffer));
129}
130
131
132void
134 const char *str)
135{
136 size_t len = strlen (str);
137
138 while ( (0 != len) && ('/' == str[0]) )
139 {
140 str++;
141 len--;
142 }
143 if ( (0 == buf->position) ||
144 ('/' != buf->mem[buf->position - 1]) )
145 {
147 buf->mem[buf->position++] = '/';
148 }
149 GNUNET_buffer_write (buf, str, len);
150}
151
152
153void
155 const char *fmt,
156 ...)
157{
158 va_list args;
159
160 va_start (args, fmt);
162 fmt,
163 args);
164 va_end (args);
165}
166
167
168void
170 const char *fmt,
171 va_list args)
172{
173 int res;
174 va_list args2;
175
176 va_copy (args2, args);
177 res = vsnprintf (NULL,
178 0,
179 fmt,
180 args2);
181 va_end (args2);
182
183 GNUNET_assert (res >= 0);
185 res + 1);
186
187 va_copy (args2, args);
188 res = vsnprintf (buf->mem + buf->position,
189 res + 1,
190 fmt,
191 args2);
192 va_end (args2);
193
194 GNUNET_assert (res >= 0);
195 buf->position += res;
196 GNUNET_assert (buf->position <= buf->capacity);
197}
198
199
200void
202 const void *data,
203 size_t data_len)
204{
205 size_t outlen;
206
207 GNUNET_assert (data_len <= SIZE_MAX / 8);
208 outlen = data_len * 8;
209
210 // https://bugs.gnunet.org/view.php?id=9279#c23545
211 outlen = (outlen + 4) / 5;
213 outlen);
214 GNUNET_assert (NULL !=
216 data_len,
217 (buf->mem
218 + buf->position),
219 outlen));
220 buf->position += outlen;
221 GNUNET_assert (buf->position <= buf->capacity);
222}
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...)
Definition buffer.c:154
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:89
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:201
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:40
void * GNUNET_buffer_reap(struct GNUNET_Buffer *buf, size_t *size)
Clear the buffer and return its contents.
Definition buffer.c:109
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:169
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition buffer.c:63
void GNUNET_buffer_prealloc(struct GNUNET_Buffer *buf, size_t capacity)
Initialize a buffer with the given capacity.
Definition buffer.c:26
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:133
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:77
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition buffer.c:123
@ 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:752
static unsigned int size
Size of the "table".
Definition peer.c:68
#define SIZE_MAX
Definition platform.h:209
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.
const char * str
Definition time.c:1252