aboutsummaryrefslogtreecommitdiff
path: root/src/util/strings.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/strings.c')
-rw-r--r--src/util/strings.c42
1 files changed, 38 insertions, 4 deletions
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 *