aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-07-30 15:15:59 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-07-30 15:15:59 +0530
commitd335baac87c2c59796a543fc2df44e2db33f5e8e (patch)
tree4bb38fadb157887c4dec1c08b4a55afa35e51a68 /src
parent740355cd63dcd248d7cfd75129dc67e7667ecfe9 (diff)
downloadgnunet-d335baac87c2c59796a543fc2df44e2db33f5e8e.tar.gz
gnunet-d335baac87c2c59796a543fc2df44e2db33f5e8e.zip
implement GNUNET_buffer_write_data_encoded
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_buffer_lib.h15
-rw-r--r--src/util/buffer.c32
2 files changed, 47 insertions, 0 deletions
diff --git a/src/include/gnunet_buffer_lib.h b/src/include/gnunet_buffer_lib.h
index e09ec130a..046aee72b 100644
--- a/src/include/gnunet_buffer_lib.h
+++ b/src/include/gnunet_buffer_lib.h
@@ -110,6 +110,21 @@ GNUNET_buffer_write_str (struct GNUNET_Buffer *buf, const char *str);
110 110
111 111
112/** 112/**
113 * Write data encoded via #GNUNET_STRINGS_data_to_string to the buffer.
114 *
115 * Grows the buffer if necessary.
116 *
117 * @param buf buffer to write to
118 * @param data data to read from
119 * @param len number of bytes to copy from @a data to @a buf
120 */
121void
122GNUNET_buffer_write_data_encoded (struct GNUNET_Buffer *buf,
123 const char *data,
124 size_t len);
125
126
127/**
113 * Write a path component to a buffer, ensuring that 128 * Write a path component to a buffer, ensuring that
114 * there is exactly one slash between the previous contents 129 * there is exactly one slash between the previous contents
115 * of the buffer and the new string. 130 * of the buffer and the new string.
diff --git a/src/util/buffer.c b/src/util/buffer.c
index d0261889e..2af972413 100644
--- a/src/util/buffer.c
+++ b/src/util/buffer.c
@@ -248,3 +248,35 @@ GNUNET_buffer_write_vfstr (struct GNUNET_Buffer *buf,
248 buf->position += res; 248 buf->position += res;
249 GNUNET_assert (buf->position <= buf->capacity); 249 GNUNET_assert (buf->position <= buf->capacity);
250} 250}
251
252
253/**
254 * Write data encoded via #GNUNET_STRINGS_data_to_string to the buffer.
255 *
256 * Grows the buffer if necessary.
257 *
258 * @param buf buffer to write to
259 * @param data data to read from
260 * @param len number of bytes to copy from @a data to @a buf
261 */
262void
263GNUNET_buffer_write_data_encoded (struct GNUNET_Buffer *buf,
264 const char *data,
265 size_t len)
266{
267 size_t outlen = len * 8;
268 char *p = buf->mem + buf->position;
269
270 if (outlen % 5 > 0)
271 outlen += 5 - outlen % 5;
272 outlen /= 5;
273
274 GNUNET_buffer_ensure_remaining (buf, outlen);
275 GNUNET_assert (NULL !=
276 GNUNET_STRINGS_data_to_string (data,
277 len,
278 p,
279 outlen));
280 buf->position += outlen;
281 GNUNET_assert (buf->position <= buf->capacity);
282}