summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-01-28 19:32:56 +0000
committerChristian Grothoff <christian@grothoff.org>2015-01-28 19:32:56 +0000
commitf225e4d9567118fdb0209a4bc8dfdfe76cb85ae0 (patch)
treee1a2d93d258189edd65b5a9d2acda1527197b52a /src
parent57c2b8f499605815aad123549db8edb8566fa370 (diff)
add GNUNET_STRINGS_data_to_string_alloc from TALER
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_strings_lib.h15
-rw-r--r--src/util/strings.c42
2 files changed, 53 insertions, 4 deletions
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h
index c7d16c947..387f3a135 100644
--- a/src/include/gnunet_strings_lib.h
+++ b/src/include/gnunet_strings_lib.h
@@ -297,6 +297,21 @@ GNUNET_STRINGS_data_to_string (const void *data,
/**
+ * Return the base32crockford encoding of the given buffer.
+ *
+ * The returned string will be freshly allocated, and must be free'd
+ * with #GNUNET_free().
+ *
+ * @param buffer with data
+ * @param size size of the buffer
+ * @return freshly allocated, null-terminated string
+ */
+char *
+GNUNET_STRINGS_data_to_string_alloc (const void *buf,
+ size_t size);
+
+
+/**
* Convert Base32hex encoding back to data.
* @a out_size must match exactly the size of the data before it was encoded.
*
diff --git a/src/util/strings.c b/src/util/strings.c
index ba1a91641..3700a7af9 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -846,10 +846,13 @@ getValue__ (unsigned char a)
* @param out buffer to fill
* @param out_size size of the buffer. Must be large enough to hold
* (size * 8 + 4) / 5 bytes
- * @return pointer to the next byte in 'out' or NULL on error.
+ * @return pointer to the next byte in @a out or NULL on error.
*/
char *
-GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
+GNUNET_STRINGS_data_to_string (const void *data,
+ size_t size,
+ char *out,
+ size_t out_size)
{
/**
* 32 characters for encoding
@@ -861,8 +864,6 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t
unsigned int vbit;
const unsigned char *udata;
- GNUNET_assert (data != NULL);
- GNUNET_assert (out != NULL);
udata = data;
if (out_size < (size * 8 + 4) / 5)
{
@@ -902,6 +903,39 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t
/**
+ * Return the base32crockford encoding of the given buffer.
+ *
+ * The returned string will be freshly allocated, and must be free'd
+ * with GNUNET_free().
+ *
+ * @param buffer with data
+ * @param size size of the buffer
+ * @return freshly allocated, null-terminated string
+ */
+char *
+GNUNET_STRINGS_data_to_string_alloc (const void *buf,
+ size_t size)
+{
+ char *str_buf;
+ size_t len = size * 8;
+ char *end;
+
+ if (len % 5 > 0)
+ len += 5 - len % 5;
+ len /= 5;
+ str_buf = GNUNET_malloc (len + 1);
+ end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len);
+ if (NULL == end)
+ {
+ GNUNET_free (str_buf);
+ return NULL;
+ }
+ *end = '\0';
+ return str_buf;
+}
+
+
+/**
* Convert Crockford Base32hex encoding back to data.
* @a out_size must match exactly the size of the data before it was encoded.
*