diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2014-06-17 18:47:00 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2014-06-17 18:47:00 +0000 |
commit | eb9dc235bb0d2825791d09315cdbe6351b72dd02 (patch) | |
tree | deecd8e36ba28b2ffcdb5894830a2b122eb0ba19 /src/util | |
parent | 93f06975c5b579a688132af13eb451522d6f0632 (diff) | |
download | gnunet-eb9dc235bb0d2825791d09315cdbe6351b72dd02.tar.gz gnunet-eb9dc235bb0d2825791d09315cdbe6351b72dd02.zip |
Use Crockford Base32 encoding instead of RFC 4648.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strings.c | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index e0196108a..3dbcaf2c0 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -783,7 +783,8 @@ GNUNET_STRINGS_get_short_name (const char *filename) | |||
783 | 783 | ||
784 | 784 | ||
785 | /** | 785 | /** |
786 | * Get the numeric value corresponding to a character. | 786 | * Get the decoded value corresponding to a character according to Crockford |
787 | * Base32 encoding. | ||
787 | * | 788 | * |
788 | * @param a a character | 789 | * @param a a character |
789 | * @return corresponding numeric value | 790 | * @return corresponding numeric value |
@@ -791,18 +792,52 @@ GNUNET_STRINGS_get_short_name (const char *filename) | |||
791 | static unsigned int | 792 | static unsigned int |
792 | getValue__ (unsigned char a) | 793 | getValue__ (unsigned char a) |
793 | { | 794 | { |
795 | unsigned int dec; | ||
796 | |||
797 | switch (a) | ||
798 | { | ||
799 | case 'O': | ||
800 | case 'o': | ||
801 | a = '0'; | ||
802 | break; | ||
803 | case 'i': | ||
804 | case 'I': | ||
805 | case 'l': | ||
806 | case 'L': | ||
807 | a = '1'; | ||
808 | break; | ||
809 | /* also consider U to be V */ | ||
810 | case 'u': | ||
811 | case 'U': | ||
812 | a = 'V'; | ||
813 | break; | ||
814 | default: | ||
815 | break; | ||
816 | } | ||
794 | if ((a >= '0') && (a <= '9')) | 817 | if ((a >= '0') && (a <= '9')) |
795 | return a - '0'; | 818 | return a - '0'; |
796 | if ((a >= 'A') && (a <= 'V')) | 819 | if ((a >= 'a') && (a <= 'z')) |
797 | return (a - 'A' + 10); | 820 | a = toupper (a); |
798 | if ((a >= 'a') && (a <= 'v')) | 821 | /* return (a - 'a' + 10); */ |
799 | return (a - 'a' + 10); | 822 | dec = 0; |
823 | if ((a >= 'A') && (a <= 'Z')) | ||
824 | { | ||
825 | if ('I' < a) | ||
826 | dec++; | ||
827 | if ('L' < a) | ||
828 | dec++; | ||
829 | if ('O' < a) | ||
830 | dec++; | ||
831 | if ('U' < a) | ||
832 | dec++; | ||
833 | return (a - 'A' + 10 - dec); | ||
834 | } | ||
800 | return -1; | 835 | return -1; |
801 | } | 836 | } |
802 | 837 | ||
803 | 838 | ||
804 | /** | 839 | /** |
805 | * Convert binary data to ASCII encoding using Base32Hex (RFC 4648). | 840 | * Convert binary data to ASCII encoding using Crockford Base32 encoding. |
806 | * Does not append 0-terminator, but returns a pointer to the place where | 841 | * Does not append 0-terminator, but returns a pointer to the place where |
807 | * it should be placed, if needed. | 842 | * it should be placed, if needed. |
808 | * | 843 | * |
@@ -819,7 +854,7 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t | |||
819 | /** | 854 | /** |
820 | * 32 characters for encoding | 855 | * 32 characters for encoding |
821 | */ | 856 | */ |
822 | static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; | 857 | static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; |
823 | unsigned int wpos; | 858 | unsigned int wpos; |
824 | unsigned int rpos; | 859 | unsigned int rpos; |
825 | unsigned int bits; | 860 | unsigned int bits; |
@@ -867,7 +902,7 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t | |||
867 | 902 | ||
868 | 903 | ||
869 | /** | 904 | /** |
870 | * Convert Base32hex encoding back to data. | 905 | * Convert Crockford Base32hex encoding back to data. |
871 | * @a out_size must match exactly the size of the data before it was encoded. | 906 | * @a out_size must match exactly the size of the data before it was encoded. |
872 | * | 907 | * |
873 | * @param enc the encoding | 908 | * @param enc the encoding |
@@ -902,7 +937,7 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, | |||
902 | { | 937 | { |
903 | vbit = encoded_len % 5; /* padding! */ | 938 | vbit = encoded_len % 5; /* padding! */ |
904 | shift = 5 - vbit; | 939 | shift = 5 - vbit; |
905 | bits = (ret = getValue__ (enc[--rpos])) >> (5 - (encoded_len % 5)); | 940 | bits = (ret = getValue__ (enc[--rpos])) >> shift; |
906 | } | 941 | } |
907 | else | 942 | else |
908 | { | 943 | { |