aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-03-05 13:48:14 +0000
committerChristian Grothoff <christian@grothoff.org>2013-03-05 13:48:14 +0000
commit9dd0d8bfcff777e2f53922eb10445ac29bc6afc2 (patch)
treeb581ce09f5551161b7370a8efc98a4a6e8493505 /src/util
parente5cc8dcd9c82d1736b6c4cfd0a52fc1dcf9ea154 (diff)
downloadgnunet-9dd0d8bfcff777e2f53922eb10445ac29bc6afc2.tar.gz
gnunet-9dd0d8bfcff777e2f53922eb10445ac29bc6afc2.zip
-improve strings API
Diffstat (limited to 'src/util')
-rw-r--r--src/util/strings.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/util/strings.c b/src/util/strings.c
index 4cef5b6fd..9856a15c4 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -758,7 +758,7 @@ getValue__ (unsigned char a)
758 * @return pointer to the next byte in 'out' or NULL on error. 758 * @return pointer to the next byte in 'out' or NULL on error.
759 */ 759 */
760char * 760char *
761GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out, size_t out_size) 761GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
762{ 762{
763 /** 763 /**
764 * 32 characters for encoding 764 * 32 characters for encoding
@@ -768,9 +768,11 @@ GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out
768 unsigned int rpos; 768 unsigned int rpos;
769 unsigned int bits; 769 unsigned int bits;
770 unsigned int vbit; 770 unsigned int vbit;
771 const unsigned char *udata;
771 772
772 GNUNET_assert (data != NULL); 773 GNUNET_assert (data != NULL);
773 GNUNET_assert (out != NULL); 774 GNUNET_assert (out != NULL);
775 udata = data;
774 if (out_size < (((size*8) + ((size*8) % 5)) % 5)) 776 if (out_size < (((size*8) + ((size*8) % 5)) % 5))
775 { 777 {
776 GNUNET_break (0); 778 GNUNET_break (0);
@@ -784,7 +786,7 @@ GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out
784 { 786 {
785 if ((rpos < size) && (vbit < 5)) 787 if ((rpos < size) && (vbit < 5))
786 { 788 {
787 bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */ 789 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
788 vbit += 8; 790 vbit += 8;
789 } 791 }
790 if (vbit < 5) 792 if (vbit < 5)
@@ -823,7 +825,7 @@ GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out
823 */ 825 */
824int 826int
825GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, 827GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
826 unsigned char *out, size_t out_size) 828 void *out, size_t out_size)
827{ 829{
828 unsigned int rpos; 830 unsigned int rpos;
829 unsigned int wpos; 831 unsigned int wpos;
@@ -831,7 +833,10 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
831 unsigned int vbit; 833 unsigned int vbit;
832 int ret; 834 int ret;
833 int shift; 835 int shift;
836 unsigned char *uout;
834 int encoded_len = out_size * 8; 837 int encoded_len = out_size * 8;
838
839 uout = out;
835 if (encoded_len % 5 > 0) 840 if (encoded_len % 5 > 0)
836 { 841 {
837 vbit = encoded_len % 5; /* padding! */ 842 vbit = encoded_len % 5; /* padding! */
@@ -859,13 +864,14 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
859 vbit += 5; 864 vbit += 5;
860 if (vbit >= 8) 865 if (vbit >= 8)
861 { 866 {
862 out[--wpos] = (unsigned char) bits; 867 uout[--wpos] = (unsigned char) bits;
863 bits >>= 8; 868 bits >>= 8;
864 vbit -= 8; 869 vbit -= 8;
865 } 870 }
866 } 871 }
867 GNUNET_assert (rpos == 0); 872 GNUNET_assert (rpos == 0);
868 GNUNET_assert (vbit == 0); 873 GNUNET_assert (vbit == 0);
874
869 return GNUNET_OK; 875 return GNUNET_OK;
870} 876}
871 877