gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] branch master updated: clean up checksum logic


From: gnunet
Subject: [taler-anastasis] branch master updated: clean up checksum logic
Date: Mon, 21 Dec 2020 13:40:16 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new 46d787e  clean up checksum logic
46d787e is described below

commit 46d787e1995b6911a96ec9c76c299bcf7cad3331
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Mon Dec 21 13:40:14 2020 +0100

    clean up checksum logic
---
 src/lib/anastasis_api_redux.c | 102 ++++++++++++++----------------------------
 1 file changed, 34 insertions(+), 68 deletions(-)

diff --git a/src/lib/anastasis_api_redux.c b/src/lib/anastasis_api_redux.c
index 71f81ad..5444f2e 100644
--- a/src/lib/anastasis_api_redux.c
+++ b/src/lib/anastasis_api_redux.c
@@ -490,90 +490,56 @@ static bool
 validate_ahv (const char *ahv_number,
               const char *regexp)
 {
-  char num_to_check[12];
-  regex_t regex;
-  int regex_result;
-
-  // check if ahv has correct form
-  regex_result = regcomp (&regex,
-                          regexp,
-                          REG_EXTENDED);
-  if (0 < regex_result)
   {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Failed to compile regular expression.");
-    regfree (&regex);
-    return false;
-  }
-  regex_result = regexec (&regex,
-                          ahv_number,
-                          0,
-                          NULL,
-                          0);
-  if (0 != regex_result)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "AHV number `%s' does not match regex\n",
-                ahv_number);
-    regfree (&regex);
-    return false;
-  }
-  regfree (&regex);
+    regex_t regex;
 
-  // check if ahv contains points
-  if (strstr (ahv_number, "."))
-  {
-    for (unsigned int i = 0; i < 3; i++)
+    if (0 != regcomp (&regex,
+                      regexp,
+                      REG_EXTENDED))
     {
-      num_to_check[i] = ahv_number[i];
-    }
-    for (unsigned int i = 0; i < 4; i++)
-    {
-      num_to_check[3 + i] = ahv_number[4 + i];
-    }
-    for (unsigned int i = 0; i < 4; i++)
-    {
-      num_to_check[7 + i] = ahv_number[9 + i];
+      GNUNET_break (0);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Failed to compile regular expression.");
+      return false;
     }
-    num_to_check[11] = ahv_number[14];
-  }
-  else
-  {
-    for (unsigned int i = 0; i < 12; i++)
+    /* check if ahv has correct form */
+    if (0 != regexec (&regex,
+                      ahv_number,
+                      0,
+                      NULL,
+                      0))
     {
-      num_to_check[i] = ahv_number[i];
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "AHV number `%s' does not match regex\n",
+                  ahv_number);
+      regfree (&regex);
+      return false;
     }
+    regfree (&regex);
   }
 
   {
     unsigned int checknum;
-    unsigned int calculation = 0;
     unsigned int next_ten;
+    const char *pos = &ahv_number[strlen (ahv_number) - 1];
+    bool phase = true;
+    unsigned int calculation = 0;
 
-    checknum = (unsigned int) ahv_number[strlen (ahv_number) - 1] - 48;
-    for (size_t i = 0; i < strlen (num_to_check); i++)
+    checknum = *pos - 48;
+    while (pos > ahv_number)
     {
-      unsigned int x = num_to_check[strlen (num_to_check) - i - 1] - 48;
-      if (0 == i % 2)
-        calculation += (x * 3);
+      pos--;
+      if ('.' == *pos)
+        continue;
+      if (phase)
+        calculation += ((*pos - 48) * 3);
       else
-        calculation += x;
+        calculation += *pos - 48;
+      phase = ! phase;
     }
-    // round up to the next ten
-    next_ten = (calculation + 9) / 10;
-    next_ten = next_ten * 10;
-    // calculate check number
+    /* round up to the next ten */
+    next_ten = ((calculation + 9) / 10) * 10;
     calculation = next_ten - calculation;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Checksum calculation for AHV `%s' (%s) %s (%u/%u/%u)\n",
-                ahv_number,
-                num_to_check,
-                (checknum != calculation) ? "failed" : "succeeded",
-                next_ten,
-                calculation,
-                checknum);
     return (checknum == calculation);
   }
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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