gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r26581 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r26581 - gnunet/src/util
Date: Fri, 22 Mar 2013 18:33:59 +0100

Author: grothoff
Date: 2013-03-22 18:33:59 +0100 (Fri, 22 Mar 2013)
New Revision: 26581

Modified:
   gnunet/src/util/strings.c
   gnunet/src/util/test_strings_to_data.c
Log:
-fixing string decoder issue for input sizes of a multiple of 5

Modified: gnunet/src/util/strings.c
===================================================================
--- gnunet/src/util/strings.c   2013-03-22 17:33:00 UTC (rev 26580)
+++ gnunet/src/util/strings.c   2013-03-22 17:33:59 UTC (rev 26581)
@@ -817,7 +817,7 @@
  * @param enc the encoding
  * @param enclen number of characters in 'enc' (without 0-terminator, which 
can be missing)
  * @param out location where to store the decoded data
- * @param out_size sizeof the output buffer
+ * @param out_size size of the output buffer
  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
  */
 int
@@ -831,31 +831,40 @@
   int ret;
   int shift;
   unsigned char *uout;
-  int encoded_len = out_size * 8;
+  unsigned int encoded_len = out_size * 8;
 
+  if (0 == enclen)
+  {
+    if (0 == out_size)
+      return GNUNET_OK;
+    return GNUNET_SYSERR;
+  }
   uout = out;
-  if (encoded_len % 5 > 0)
+  wpos = out_size;
+  rpos = enclen;
+  if ((encoded_len % 5) > 0)
   {
     vbit = encoded_len % 5; /* padding! */
     shift = 5 - vbit;
+    bits = (ret = getValue__ (enc[--rpos])) >> (5 - (encoded_len % 5));
   }
   else
   {
-    vbit = 0;
+    vbit = 5;
     shift = 0;
+    bits = (ret = getValue__ (enc[--rpos]));
   }
   if ((encoded_len + shift) / 5 != enclen)
     return GNUNET_SYSERR;
-
-  wpos = out_size;
-  rpos = enclen;
-  bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
   if (-1 == ret)
     return GNUNET_SYSERR;
   while (wpos > 0)
   {
     if (0 == rpos)
+    {
+      GNUNET_break (0);
       return GNUNET_SYSERR;
+    }
     bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
     if (-1 == ret)
       return GNUNET_SYSERR;

Modified: gnunet/src/util/test_strings_to_data.c
===================================================================
--- gnunet/src/util/test_strings_to_data.c      2013-03-22 17:33:00 UTC (rev 
26580)
+++ gnunet/src/util/test_strings_to_data.c      2013-03-22 17:33:59 UTC (rev 
26581)
@@ -30,30 +30,33 @@
 int
 main (int argc, char *argv[])
 {
-       GNUNET_log_setup ("util", "DEBUG", NULL);
-       char *conv;
-       char buf[255];
-       char *end;
-       struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded src;
-       struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded dest;
+  char buf[1024];
+  char *end;
+  char src[128];
+  char dst[128];
+  unsigned int i;
+  int ret = 0;
+  
+  GNUNET_log_setup ("util", "DEBUG", NULL);
+  for (i=0;i<sizeof(src);i++)
+  {
+    memset (src, i, sizeof (src));
+    memset (dst, i+1, sizeof (dst));
 
-       memset (&src, '\1', sizeof (src));
-       memset (&dest, '\2', sizeof (dest));
-
-       end = GNUNET_STRINGS_data_to_string (&src, sizeof (src), buf, sizeof 
(buf));
-       end[0] = '\0';
-       fprintf (stderr, "Key `%s'\n",buf);
-       GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_public_key_from_string 
(buf, strlen (buf), &dest));
-
-       conv = GNUNET_CRYPTO_ecc_public_key_to_string (&src);
-       GNUNET_assert (NULL != conv);
-       fprintf (stderr, "Key `%s'\n",conv);
-
-
-  GNUNET_assert (GNUNET_OK == GNUNET_STRINGS_string_to_data (conv, strlen 
(conv), (unsigned char *) &dest, sizeof (dest)));
-  GNUNET_assert (0 == memcmp (&src, &dest, sizeof (dest)));
-
-       return 0;
+    end = GNUNET_STRINGS_data_to_string (&src, i, buf, sizeof (buf));
+    end[0] = '\0';
+    if (GNUNET_OK != 
+       GNUNET_STRINGS_string_to_data (buf, strlen (buf), dst, i))
+    {
+      fprintf (stderr, "%u failed decode (%u bytes)\n", i, (unsigned int) 
strlen (buf));
+      ret = 1;
+    } else if (0 != memcmp (src, dst, i))
+    {
+      fprintf (stderr, "%u wrong decode (%u bytes)\n", i, (unsigned int) 
strlen (buf));
+      ret = 1;
+    }
+  }
+  return ret;
 }
 
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]