aboutsummaryrefslogtreecommitdiff
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
parent57c2b8f499605815aad123549db8edb8566fa370 (diff)
downloadgnunet-f225e4d9567118fdb0209a4bc8dfdfe76cb85ae0.tar.gz
gnunet-f225e4d9567118fdb0209a4bc8dfdfe76cb85ae0.zip
add GNUNET_STRINGS_data_to_string_alloc from TALER
-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,
297 297
298 298
299/** 299/**
300 * Return the base32crockford encoding of the given buffer.
301 *
302 * The returned string will be freshly allocated, and must be free'd
303 * with #GNUNET_free().
304 *
305 * @param buffer with data
306 * @param size size of the buffer
307 * @return freshly allocated, null-terminated string
308 */
309char *
310GNUNET_STRINGS_data_to_string_alloc (const void *buf,
311 size_t size);
312
313
314/**
300 * Convert Base32hex encoding back to data. 315 * Convert Base32hex encoding back to data.
301 * @a out_size must match exactly the size of the data before it was encoded. 316 * @a out_size must match exactly the size of the data before it was encoded.
302 * 317 *
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)
846 * @param out buffer to fill 846 * @param out buffer to fill
847 * @param out_size size of the buffer. Must be large enough to hold 847 * @param out_size size of the buffer. Must be large enough to hold
848 * (size * 8 + 4) / 5 bytes 848 * (size * 8 + 4) / 5 bytes
849 * @return pointer to the next byte in 'out' or NULL on error. 849 * @return pointer to the next byte in @a out or NULL on error.
850 */ 850 */
851char * 851char *
852GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size) 852GNUNET_STRINGS_data_to_string (const void *data,
853 size_t size,
854 char *out,
855 size_t out_size)
853{ 856{
854 /** 857 /**
855 * 32 characters for encoding 858 * 32 characters for encoding
@@ -861,8 +864,6 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t
861 unsigned int vbit; 864 unsigned int vbit;
862 const unsigned char *udata; 865 const unsigned char *udata;
863 866
864 GNUNET_assert (data != NULL);
865 GNUNET_assert (out != NULL);
866 udata = data; 867 udata = data;
867 if (out_size < (size * 8 + 4) / 5) 868 if (out_size < (size * 8 + 4) / 5)
868 { 869 {
@@ -902,6 +903,39 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t
902 903
903 904
904/** 905/**
906 * Return the base32crockford encoding of the given buffer.
907 *
908 * The returned string will be freshly allocated, and must be free'd
909 * with GNUNET_free().
910 *
911 * @param buffer with data
912 * @param size size of the buffer
913 * @return freshly allocated, null-terminated string
914 */
915char *
916GNUNET_STRINGS_data_to_string_alloc (const void *buf,
917 size_t size)
918{
919 char *str_buf;
920 size_t len = size * 8;
921 char *end;
922
923 if (len % 5 > 0)
924 len += 5 - len % 5;
925 len /= 5;
926 str_buf = GNUNET_malloc (len + 1);
927 end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len);
928 if (NULL == end)
929 {
930 GNUNET_free (str_buf);
931 return NULL;
932 }
933 *end = '\0';
934 return str_buf;
935}
936
937
938/**
905 * Convert Crockford Base32hex encoding back to data. 939 * Convert Crockford Base32hex encoding back to data.
906 * @a out_size must match exactly the size of the data before it was encoded. 940 * @a out_size must match exactly the size of the data before it was encoded.
907 * 941 *