From f225e4d9567118fdb0209a4bc8dfdfe76cb85ae0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 28 Jan 2015 19:32:56 +0000 Subject: add GNUNET_STRINGS_data_to_string_alloc from TALER --- src/include/gnunet_strings_lib.h | 15 ++++++++++++++ src/util/strings.c | 42 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) (limited to 'src') 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 @@ -296,6 +296,21 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t out_size); +/** + * 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) { @@ -901,6 +902,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. -- cgit v1.2.3