diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-03-22 17:33:59 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-03-22 17:33:59 +0000 |
commit | 728a3f8c8a1098dea35732c3ad59fb8d60ee8e0f (patch) | |
tree | 40c5157956a84df91ed09c8d620cf2d83626941e /src/util | |
parent | 8bcc7b48e6e2e07ee5ba4da0e0c93714c6698c4d (diff) | |
download | gnunet-728a3f8c8a1098dea35732c3ad59fb8d60ee8e0f.tar.gz gnunet-728a3f8c8a1098dea35732c3ad59fb8d60ee8e0f.zip |
-fixing string decoder issue for input sizes of a multiple of 5
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strings.c | 25 | ||||
-rw-r--r-- | src/util/test_strings_to_data.c | 51 |
2 files changed, 44 insertions, 32 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index e9b5b3c21..8bbc904bc 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -817,7 +817,7 @@ GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t | |||
817 | * @param enc the encoding | 817 | * @param enc the encoding |
818 | * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) | 818 | * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing) |
819 | * @param out location where to store the decoded data | 819 | * @param out location where to store the decoded data |
820 | * @param out_size sizeof the output buffer | 820 | * @param out_size size of the output buffer |
821 | * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding | 821 | * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding |
822 | */ | 822 | */ |
823 | int | 823 | int |
@@ -831,31 +831,40 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, | |||
831 | int ret; | 831 | int ret; |
832 | int shift; | 832 | int shift; |
833 | unsigned char *uout; | 833 | unsigned char *uout; |
834 | int encoded_len = out_size * 8; | 834 | unsigned int encoded_len = out_size * 8; |
835 | 835 | ||
836 | if (0 == enclen) | ||
837 | { | ||
838 | if (0 == out_size) | ||
839 | return GNUNET_OK; | ||
840 | return GNUNET_SYSERR; | ||
841 | } | ||
836 | uout = out; | 842 | uout = out; |
837 | if (encoded_len % 5 > 0) | 843 | wpos = out_size; |
844 | rpos = enclen; | ||
845 | if ((encoded_len % 5) > 0) | ||
838 | { | 846 | { |
839 | vbit = encoded_len % 5; /* padding! */ | 847 | vbit = encoded_len % 5; /* padding! */ |
840 | shift = 5 - vbit; | 848 | shift = 5 - vbit; |
849 | bits = (ret = getValue__ (enc[--rpos])) >> (5 - (encoded_len % 5)); | ||
841 | } | 850 | } |
842 | else | 851 | else |
843 | { | 852 | { |
844 | vbit = 0; | 853 | vbit = 5; |
845 | shift = 0; | 854 | shift = 0; |
855 | bits = (ret = getValue__ (enc[--rpos])); | ||
846 | } | 856 | } |
847 | if ((encoded_len + shift) / 5 != enclen) | 857 | if ((encoded_len + shift) / 5 != enclen) |
848 | return GNUNET_SYSERR; | 858 | return GNUNET_SYSERR; |
849 | |||
850 | wpos = out_size; | ||
851 | rpos = enclen; | ||
852 | bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5); | ||
853 | if (-1 == ret) | 859 | if (-1 == ret) |
854 | return GNUNET_SYSERR; | 860 | return GNUNET_SYSERR; |
855 | while (wpos > 0) | 861 | while (wpos > 0) |
856 | { | 862 | { |
857 | if (0 == rpos) | 863 | if (0 == rpos) |
864 | { | ||
865 | GNUNET_break (0); | ||
858 | return GNUNET_SYSERR; | 866 | return GNUNET_SYSERR; |
867 | } | ||
859 | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; | 868 | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; |
860 | if (-1 == ret) | 869 | if (-1 == ret) |
861 | return GNUNET_SYSERR; | 870 | return GNUNET_SYSERR; |
diff --git a/src/util/test_strings_to_data.c b/src/util/test_strings_to_data.c index d98cd8578..739773558 100644 --- a/src/util/test_strings_to_data.c +++ b/src/util/test_strings_to_data.c | |||
@@ -30,30 +30,33 @@ | |||
30 | int | 30 | int |
31 | main (int argc, char *argv[]) | 31 | main (int argc, char *argv[]) |
32 | { | 32 | { |
33 | GNUNET_log_setup ("util", "DEBUG", NULL); | 33 | char buf[1024]; |
34 | char *conv; | 34 | char *end; |
35 | char buf[255]; | 35 | char src[128]; |
36 | char *end; | 36 | char dst[128]; |
37 | struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded src; | 37 | unsigned int i; |
38 | struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded dest; | 38 | int ret = 0; |
39 | 39 | ||
40 | memset (&src, '\1', sizeof (src)); | 40 | GNUNET_log_setup ("util", "DEBUG", NULL); |
41 | memset (&dest, '\2', sizeof (dest)); | 41 | for (i=0;i<sizeof(src);i++) |
42 | 42 | { | |
43 | end = GNUNET_STRINGS_data_to_string (&src, sizeof (src), buf, sizeof (buf)); | 43 | memset (src, i, sizeof (src)); |
44 | end[0] = '\0'; | 44 | memset (dst, i+1, sizeof (dst)); |
45 | fprintf (stderr, "Key `%s'\n",buf); | 45 | |
46 | GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_public_key_from_string (buf, strlen (buf), &dest)); | 46 | end = GNUNET_STRINGS_data_to_string (&src, i, buf, sizeof (buf)); |
47 | 47 | end[0] = '\0'; | |
48 | conv = GNUNET_CRYPTO_ecc_public_key_to_string (&src); | 48 | if (GNUNET_OK != |
49 | GNUNET_assert (NULL != conv); | 49 | GNUNET_STRINGS_string_to_data (buf, strlen (buf), dst, i)) |
50 | fprintf (stderr, "Key `%s'\n",conv); | 50 | { |
51 | 51 | fprintf (stderr, "%u failed decode (%u bytes)\n", i, (unsigned int) strlen (buf)); | |
52 | 52 | ret = 1; | |
53 | GNUNET_assert (GNUNET_OK == GNUNET_STRINGS_string_to_data (conv, strlen (conv), (unsigned char *) &dest, sizeof (dest))); | 53 | } else if (0 != memcmp (src, dst, i)) |
54 | GNUNET_assert (0 == memcmp (&src, &dest, sizeof (dest))); | 54 | { |
55 | 55 | fprintf (stderr, "%u wrong decode (%u bytes)\n", i, (unsigned int) strlen (buf)); | |
56 | return 0; | 56 | ret = 1; |
57 | } | ||
58 | } | ||
59 | return ret; | ||
57 | } | 60 | } |
58 | 61 | ||
59 | 62 | ||