Better proofs
authorMaximilian Szengel <gnunet@maxsz.de>
Wed, 6 Jun 2012 10:54:07 +0000 (10:54 +0000)
committerMaximilian Szengel <gnunet@maxsz.de>
Wed, 6 Jun 2012 10:54:07 +0000 (10:54 +0000)
src/regex/regex.c
src/regex/test_regex_iterate_api.c

index 551610baab8ab4a0096f417b81784a738d2fd65a..72ed0f81c8a5f367bb9ceb031cbf60760262bf95 100644 (file)
@@ -817,6 +817,7 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   char *R_last[a->state_count][a->state_count];
   char *R_cur[a->state_count][a->state_count];
   char *temp;
+  int length;
 
   k = 0;
   n = a->state_count;
@@ -849,16 +850,17 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
         }
       }
 
+      if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
+      {
+        temp = R_last[i][j];
+        GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
+        GNUNET_free (temp);
+      }
+
       if (i == j)
       {
         if (NULL == R_last[i][j])
           GNUNET_asprintf (&R_last[i][j], "");
-        else if (1 < strlen (R_last[i][j]))
-        {
-          temp = R_last[i][j];
-          GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
-          GNUNET_free (temp);
-        }
       }
     }
   }
@@ -870,33 +872,51 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
     {
       for (j = 0; j < n; j++)
       {
+        // 0*R = R*0 = 0
+        // 0+R = R+0 = R
         if (NULL == R_last[i][k] || NULL == R_last[k][j])
         {
           if (NULL != R_last[i][j])
             R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
         }
-        else if (NULL == R_last[i][j] ||
-                 0 == strcmp (R_last[i][j], R_last[i][k]))
-        {
-          if ((R_last[k][k][0] == '(' &&
-               R_last[k][k][strlen (R_last[k][k]) - 1] == ')') ||
-              (1 == strlen (R_last[k][k])))
-            GNUNET_asprintf (&R_cur[i][j], "(%s%s*%s)", R_last[i][k],
-                             R_last[k][k], R_last[k][j]);
-          else
-            GNUNET_asprintf (&R_cur[i][j], "(%s(%s)*%s)", R_last[i][k],
-                             R_last[k][k], R_last[k][j]);
-        }
         else
         {
-          if ((R_last[k][k][0] == '(' &&
-               R_last[k][k][strlen (R_last[k][k]) - 1] == ')') ||
-              (1 == strlen (R_last[k][k])))
-            GNUNET_asprintf (&R_cur[i][j], "(%s|%s%s*%s)", R_last[i][j],
-                             R_last[i][k], R_last[k][k], R_last[k][j]);
+          // R(k)ij = R(k-1)ij + R(k-1)ik (R(k-1)kk)* R(k-1)kj
+          length =
+              snprintf (NULL, 0, "(%s|%s(%s)*%s)", R_last[i][j], R_last[i][k],
+                        R_last[k][k], R_last[k][j]) + 1;
+          char *left = GNUNET_malloc (length);
+          char *right = GNUNET_malloc (length);
+
+          if (NULL != R_last[i][j])
+            strcat (left, R_last[i][j]);
+
+          if (NULL != R_last[i][k])
+            strcat (right, R_last[i][k]);
+
+          if (NULL != R_last[k][k] && 0 != strcmp (R_last[k][k], ""))
+          {
+            strcat (right, "(");
+            strcat (right, R_last[k][k]);
+            strcat (right, ")*");
+          }
+
+          if (NULL != R_last[k][j])
+            strcat (right, R_last[k][j]);
+
+          if (0 == strcmp (left, right) || 0 == strcmp (left, "") ||
+              0 == strcmp (right, ""))
+          {
+            if (0 == strcmp (left, ""))
+              GNUNET_asprintf (&R_cur[i][j], "%s", right);
+            else
+              GNUNET_asprintf (&R_cur[i][j], "%s", left);
+          }
           else
-            GNUNET_asprintf (&R_cur[i][j], "(%s|%s(%s)*%s)", R_last[i][j],
-                             R_last[i][k], R_last[k][k], R_last[k][j]);
+            GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", left, right);
+
+          GNUNET_free_non_null (left);
+          GNUNET_free_non_null (right);
         }
       }
     }
@@ -917,10 +937,12 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
     }
   }
 
-  // assign proofs
+  // assign proofs and hashes
   for (i = 0; i < n; i++)
   {
     states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
+    GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
+                        &states[i]->hash);
   }
 
   // cleanup
index 51ebbcd88678fc4968f948555a59995efcabfb33..6c5b0e55b2ab30ea3a7c61054e7ac9b51534d6a2 100644 (file)
@@ -61,10 +61,14 @@ main (int argc, char *argv[])
 
   error = 0;
   /*regex = "ab(c|d)+c*(a(b|c)+d)+(bla)+"; */
+  /*regex = "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*"; */
   /*regex = "z(abc|def)?xyz"; */
-  regex = "1*0(0|1)*";
+  /*regex = "1*0(0|1)*"; */
   /*regex = "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*"; */
-
+  /*regex = "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)"; */
+  /*regex = "abc(1|0)*def"; */
+  /*regex = "ab|ac"; */
+  regex = "(ab)(ab)*";
   dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex));
   GNUNET_REGEX_automaton_save_graph (dfa, "dfa.dot");
   GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, NULL);