-fix
[oweals/gnunet.git] / src / regex / regex.c
index ceaeedf6d863acc392ec49f50aeb1a9d67a88be0..4c0f288510fa9ec449fccad1c09eddc767d2ffe4 100644 (file)
 #define INITIAL_BITS 8
 
 
-/**
- * Context that contains an id counter for states and transitions as well as a
- * DLL of automatons used as a stack for NFA construction.
- */
-struct GNUNET_REGEX_Context
-{
-  /**
-   * Unique state id.
-   */
-  unsigned int state_id;
-
-  /**
-   * Unique transition id.
-   */
-  unsigned int transition_id;
-
-  /**
-   * DLL of GNUNET_REGEX_Automaton's used as a stack.
-   */
-  struct GNUNET_REGEX_Automaton *stack_head;
-
-  /**
-   * DLL of GNUNET_REGEX_Automaton's used as a stack.
-   */
-  struct GNUNET_REGEX_Automaton *stack_tail;
-};
-
-
 /**
  * Set of states.
  */
@@ -80,101 +52,23 @@ struct GNUNET_REGEX_StateSet
 };
 
 
-/*
- * Debug helper functions
- */
-
-/**
- * Print all the transitions of state 's'.
- *
- * @param s state for which to print it's transitions.
- */
-void
-debug_print_transitions (struct GNUNET_REGEX_State *s);
-
-
 /**
- * Print information of the given state 's'.
+ * Compare two strings for equality. If either is NULL they are not equal.
  *
- * @param s state for which debug information should be printed.
- */
-void
-debug_print_state (struct GNUNET_REGEX_State *s)
-{
-  char *proof;
-
-  if (NULL == s->proof)
-    proof = "NULL";
-  else
-    proof = s->proof;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
-              s->id, s->name, s->marked, s->accepting, s->scc_id,
-              s->transition_count, proof);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
-  debug_print_transitions (s);
-}
-
-
-/**
- * Print debug information for all states contained in the automaton 'a'.
- *
- * @param a automaton for which debug information of it's states should be printed.
- */
-void
-debug_print_states (struct GNUNET_REGEX_Automaton *a)
-{
-  struct GNUNET_REGEX_State *s;
-
-  for (s = a->states_head; NULL != s; s = s->next)
-    debug_print_state (s);
-}
-
-
-/**
- * Print debug information for given transition 't'.
+ * @param str1 first string for comparison.
+ * @param str2 second string for comparison.
  *
- * @param t transition for which to print debug info.
+ * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
  */
-void
-debug_print_transition (struct GNUNET_REGEX_Transition *t)
-{
-  char *to_state;
-  char *from_state;
-  char label;
-
-  if (NULL == t)
-    return;
-
-  if (0 == t->label)
-    label = '0';
-  else
-    label = t->label;
-
-  if (NULL == t->to_state)
-    to_state = "NULL";
-  else
-    to_state = t->to_state->name;
-
-  if (NULL == t->from_state)
-    from_state = "NULL";
-  else
-    from_state = t->from_state->name;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
-              t->id, from_state, label, to_state);
-}
-
-
-void
-debug_print_transitions (struct GNUNET_REGEX_State *s)
+static int
+nullstrcmp (const char *str1, const char *str2)
 {
-  struct GNUNET_REGEX_Transition *t;
+  if ((NULL == str1) != (NULL == str2))
+    return -1;
+  if ((NULL == str1) && (NULL == str2))
+    return 0;
 
-  for (t = s->transitions_head; NULL != t; t = t->next)
-    debug_print_transition (t);
+  return strcmp (str1, str2);
 }
 
 
@@ -189,7 +83,7 @@ debug_print_transitions (struct GNUNET_REGEX_State *s)
  */
 static void
 state_add_transition (struct GNUNET_REGEX_Context *ctx,
-                      struct GNUNET_REGEX_State *from_state, const char label,
+                      struct GNUNET_REGEX_State *from_state, const char *label,
                       struct GNUNET_REGEX_State *to_state)
 {
   int is_dup;
@@ -206,7 +100,7 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
   is_dup = GNUNET_NO;
   for (t = from_state->transitions_head; NULL != t; t = t->next)
   {
-    if (t->to_state == to_state && t->label == label &&
+    if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
         t->from_state == from_state)
     {
       is_dup = GNUNET_YES;
@@ -220,13 +114,17 @@ state_add_transition (struct GNUNET_REGEX_Context *ctx,
   // sort transitions by label
   for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
   {
-    if (oth->label > label)
+    if (0 < nullstrcmp (oth->label, label))
       break;
   }
 
   t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
-  t->id = ctx->transition_id++;
-  t->label = label;
+  if (NULL != ctx)
+    t->id = ctx->transition_id++;
+  if (NULL != label)
+    t->label = GNUNET_strdup (label);
+  else
+    t->label = NULL;
   t->to_state = to_state;
   t->from_state = from_state;
 
@@ -256,6 +154,7 @@ state_remove_transition (struct GNUNET_REGEX_State *state,
   state->transition_count--;
   GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
                                transition);
+  GNUNET_free_non_null (transition->label);
   GNUNET_free (transition);
 }
 
@@ -307,7 +206,7 @@ state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
   {
     if (NULL != t->to_state)
     {
-      edges[count].label = &t->label;
+      edges[count].label = t->label;
       edges[count].destination = t->to_state->hash;
       count++;
     }
@@ -408,6 +307,7 @@ automaton_destroy_state (struct GNUNET_REGEX_State *s)
   {
     next_t = t->next;
     GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
+    GNUNET_free_non_null (t->label);
     GNUNET_free (t);
   }
 
@@ -500,7 +400,7 @@ automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
         is_dup = GNUNET_NO;
         for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
         {
-          if (t->to_state == s1 && t_check->label == t->label)
+          if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
             is_dup = GNUNET_YES;
         }
         if (GNUNET_NO == is_dup)
@@ -547,55 +447,225 @@ automaton_add_state (struct GNUNET_REGEX_Automaton *a,
 
 
 /**
- * Depth-first traversal of all states that are reachable from state 's'. Expects the states to
- * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
- * state.
+ * Depth-first traversal (DFS) of all states that are reachable from state
+ * 's'. Performs 'action' on each visited state.
  *
  * @param s start state.
+ * @param marks an array of size a->state_count to remember which state was
+ *        already visited.
  * @param count current count of the state.
+ * @param check function that is checked before advancing on each transition
+ *              in the DFS.
+ * @param check_cls closure for check.
  * @param action action to be performed on each state.
- * @param action_cls closure for action
+ * @param action_cls closure for action.
  */
 static void
-automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
+automaton_state_traverse (struct GNUNET_REGEX_State *s, int *marks,
+                          unsigned int *count,
+                          GNUNET_REGEX_traverse_check check, void *check_cls,
                           GNUNET_REGEX_traverse_action action, void *action_cls)
 {
   struct GNUNET_REGEX_Transition *t;
 
-  if (GNUNET_NO != s->marked)
+  if (GNUNET_YES == marks[s->traversal_id])
     return;
-  s->marked = GNUNET_YES;
+
+  marks[s->traversal_id] = GNUNET_YES;
+
   if (NULL != action)
     action (action_cls, *count, s);
+
   (*count)++;
+
   for (t = s->transitions_head; NULL != t; t = t->next)
-    automaton_state_traverse (t->to_state, count, action, action_cls);
+  {
+    if (NULL == check ||
+        (NULL != check && GNUNET_YES == check (check_cls, s, t)))
+    {
+      automaton_state_traverse (t->to_state, marks, count, check, check_cls,
+                                action, action_cls);
+    }
+  }
 }
 
 
 /**
- * Traverses the given automaton from it's start state, visiting all reachable
- * states and calling 'action' on each one of them.
- *
- * @param a automaton.
+ * Traverses the given automaton using depth-first-search (DFS) from it's start
+ * state, visiting all reachable states and calling 'action' on each one of
+ * them.
+ *
+ * @param a automaton to be traversed.
+ * @param start start state, pass a->start or NULL to traverse the whole automaton.
+ * @param check function that is checked before advancing on each transition
+ *              in the DFS.
+ * @param check_cls closure for check.
  * @param action action to be performed on each state.
  * @param action_cls closure for action
  */
 void
-GNUNET_REGEX_automaton_traverse (struct GNUNET_REGEX_Automaton *a,
+GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
+                                 struct GNUNET_REGEX_State *start,
+                                 GNUNET_REGEX_traverse_check check,
+                                 void *check_cls,
                                  GNUNET_REGEX_traverse_action action,
                                  void *action_cls)
 {
   unsigned int count;
   struct GNUNET_REGEX_State *s;
+  int marks[a->state_count];
+
+  if (NULL == a || 0 == a->state_count)
+    return;
+
+  for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
+       s = s->next, count++)
+  {
+    s->traversal_id = count;
+    marks[s->traversal_id] = GNUNET_NO;
+  }
 
-  for (s = a->states_head; NULL != s; s = s->next)
-    s->marked = GNUNET_NO;
   count = 0;
-  automaton_state_traverse (a->start, &count, action, action_cls);
+
+  if (NULL == start)
+    s = a->start;
+  else
+    s = start;
+
+  automaton_state_traverse (s, marks, &count, check, check_cls, action,
+                            action_cls);
+}
+
+
+/**
+ * Context for adding strided transitions to a DFA.
+ */
+struct GNUNET_REGEX_Strided_Context
+{
+  /**
+   * Length of the strides.
+   */
+  const unsigned int stride;
+
+  /**
+   * Strided transitions DLL. New strided transitions will be stored in this DLL
+   * and afterwards added to the DFA.
+   */
+  struct GNUNET_REGEX_Transition *transitions_head;
+
+  /**
+   * Strided transitions DLL.
+   */
+  struct GNUNET_REGEX_Transition *transitions_tail;
+};
+
+
+/**
+ * Recursive helper function to add strides to a DFA.
+ *
+ * @param cls context, contains stride length and strided transitions DLL.
+ * @param depth current depth of the depth-first traversal of the graph.
+ * @param label current label, string that contains all labels on the path from
+ *        'start' to 's'.
+ * @param start start state for the depth-first traversal of the graph.
+ * @param s current state in the depth-first traversal
+ */
+void
+add_multi_strides_to_dfa_helper (void *cls, const unsigned int depth,
+                                 char *label, struct GNUNET_REGEX_State *start,
+                                 struct GNUNET_REGEX_State *s)
+{
+  struct GNUNET_REGEX_Strided_Context *ctx = cls;
+  struct GNUNET_REGEX_Transition *t;
+  char *new_label;
+
+  if (depth == ctx->stride)
+  {
+    t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
+    t->label = GNUNET_strdup (label);
+    t->to_state = s;
+    t->from_state = start;
+    GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
+                                 t);
+  }
+  else
+  {
+    for (t = s->transitions_head; NULL != t; t = t->next)
+    {
+      /* Do not consider self-loops, because it end's up in too many
+       * transitions */
+      if (t->to_state == t->from_state)
+        continue;
+
+      if (NULL != label)
+      {
+        GNUNET_asprintf (&new_label, "%s%s", label, t->label);
+      }
+      else
+        new_label = GNUNET_strdup (t->label);
+
+      add_multi_strides_to_dfa_helper (cls, (depth + 1), new_label, start,
+                                       t->to_state);
+    }
+  }
+  GNUNET_free_non_null (label);
+}
+
+
+/**
+ * Function called for each state in the DFA. Starts a traversal of depth set in
+ * context starting from state 's'.
+ *
+ * @param cls context.
+ * @param count not used.
+ * @param s current state.
+ */
+void
+add_multi_strides_to_dfa (void *cls, const unsigned int count,
+                          struct GNUNET_REGEX_State *s)
+{
+  add_multi_strides_to_dfa_helper (cls, 0, NULL, s, s);
 }
 
 
+/**
+ * Adds multi-strided transitions to the given 'dfa'.
+ *
+ * @param regex_ctx regex context needed to add transitions to the automaton.
+ * @param dfa DFA to which the multi strided transitions should be added.
+ * @param stride_len length of the strides.
+ */
+void
+GNUNET_REGEX_add_multi_strides_to_dfa (struct GNUNET_REGEX_Context *regex_ctx,
+                                       struct GNUNET_REGEX_Automaton *dfa,
+                                       const unsigned int stride_len)
+{
+  struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
+  struct GNUNET_REGEX_Transition *t;
+  struct GNUNET_REGEX_Transition *t_next;
+
+  if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
+    return;
+
+  // Compute the new transitions.
+  GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
+                                   &add_multi_strides_to_dfa, &ctx);
+
+  // Add all the new transitions to the automaton.
+  for (t = ctx.transitions_head; NULL != t; t = t_next)
+  {
+    t_next = t->next;
+    state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
+    GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
+    GNUNET_free_non_null (t->label);
+    GNUNET_free (t);
+  }
+
+  dfa->is_multistrided = GNUNET_YES;
+}
+
+
+
 /**
  * Check if the given string 'str' needs parentheses around it when
  * using it to generate a regex.
@@ -696,8 +766,8 @@ has_epsilon (const char *str)
  *
  * @param str string
  *
- * @return string without preceding epsilon, string 'str' if no preceding epsilon
- *         could be found, NULL if 'str' was NULL
+ * @return string without preceding epsilon, string 'str' if no preceding
+ *         epsilon could be found, NULL if 'str' was NULL
  */
 static char *
 remove_epsilon (const char *str)
@@ -735,38 +805,22 @@ strkcmp (const char *str1, const char *str2, size_t k)
 
 
 /**
- * Compare two strings for equality. If either is NULL (or if both are
- * NULL), they are not equal.
- *
- * @param str1 first string for comparison.
- * @param str2 second string for comparison.
- *
- * @return  0 if the strings are the same, 1 or -1 if not
- */
-static int
-nullstrcmp (const char *str1, const char *str2)
-{
-  if ((NULL == str1) || (NULL == str2))
-    return -1;
-  return strcmp (str1, str2);
-}
-
-
-/**
- * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse' function to create
- * the depth-first numbering of the states.
+ * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse'
+ * function to create the depth-first numbering of the states.
  *
  * @param cls states array.
  * @param count current state counter.
  * @param s current state.
  */
-static void
-number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
+void
+number_states (void *cls, const unsigned int count,
+               struct GNUNET_REGEX_State *s)
 {
   struct GNUNET_REGEX_State **states = cls;
 
-  s->proof_id = count;
-  states[count] = s;
+  s->dfs_id = count;
+  if (NULL != states)
+    states[count] = s;
 }
 
 
@@ -1163,9 +1217,19 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   unsigned int j;
   unsigned int k;
 
+  if (NULL == a)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not create proofs, automaton was NULL\n");
+    return;
+  }
 
   /* create depth-first numbering of the states, initializes 'state' */
-  GNUNET_REGEX_automaton_traverse (a, &number_states, states);
+  GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &number_states,
+                                   states);
+
+  for (i = 0; i < n; i++)
+    GNUNET_assert (NULL != states[i]);
 
   /* Compute regular expressions of length "1" between each pair of states */
   for (i = 0; i < n; i++)
@@ -1177,13 +1241,13 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
     }
     for (t = states[i]->transitions_head; NULL != t; t = t->next)
     {
-      j = t->to_state->proof_id;
+      j = t->to_state->dfs_id;
       if (NULL == R_last[i][j])
-        GNUNET_asprintf (&R_last[i][j], "%c", t->label);
+        GNUNET_asprintf (&R_last[i][j], "%s", t->label);
       else
       {
         temp = R_last[i][j];
-        GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
+        GNUNET_asprintf (&R_last[i][j], "%s|%s", R_last[i][j], t->label);
         GNUNET_free (temp);
       }
     }
@@ -1205,7 +1269,8 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
         GNUNET_free (temp);
       }
 
-  /* Compute regular expressions of length "k" between each pair of states per induction */
+  /* Compute regular expressions of length "k" between each pair of states per
+   * induction */
   for (k = 0; k < n; k++)
   {
     for (i = 0; i < n; i++)
@@ -1238,30 +1303,31 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
   // assign proofs and hashes
   for (i = 0; i < n; i++)
   {
-    if (NULL != R_last[a->start->proof_id][i])
+    if (NULL != R_last[a->start->dfs_id][i])
     {
-      states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
+      states[i]->proof = GNUNET_strdup (R_last[a->start->dfs_id][i]);
       GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
                           &states[i]->hash);
     }
   }
 
-  // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
+  // complete regex for whole DFA: union of all pairs (start state/accepting
+  // state(s)).
   complete_regex = NULL;
   for (i = 0; i < n; i++)
   {
     if (states[i]->accepting)
     {
-      if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
+      if (NULL == complete_regex && 0 < strlen (R_last[a->start->dfs_id][i]))
       {
-        GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
+        GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->dfs_id][i]);
       }
-      else if (NULL != R_last[a->start->proof_id][i] &&
-               0 < strlen (R_last[a->start->proof_id][i]))
+      else if (NULL != R_last[a->start->dfs_id][i] &&
+               0 < strlen (R_last[a->start->dfs_id][i]))
       {
         temp = complete_regex;
         GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
-                         R_last[a->start->proof_id][i]);
+                         R_last[a->start->dfs_id][i]);
         GNUNET_free (temp);
       }
     }
@@ -1300,7 +1366,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
   s->id = ctx->state_id++;
   s->accepting = 0;
-  s->marked = 0;
+  s->marked = GNUNET_NO;
   s->name = NULL;
   s->scc_id = 0;
   s->index = -1;
@@ -1319,7 +1385,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
   if (nfa_states->len < 1)
     return s;
 
-  // Create a name based on 'sset'
+  // Create a name based on 'nfa_states'
   s->name = GNUNET_malloc (sizeof (char) * 2);
   strcat (s->name, "{");
   name = NULL;
@@ -1341,7 +1407,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
     // Add a transition for each distinct label to NULL state
     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
     {
-      if (0 != ctran->label)
+      if (NULL != ctran->label)
         state_add_transition (ctx, s, ctran->label, NULL);
     }
 
@@ -1366,7 +1432,7 @@ dfa_state_create (struct GNUNET_REGEX_Context *ctx,
  * @return new state or NULL, if transition on label not possible
  */
 static struct GNUNET_REGEX_State *
-dfa_move (struct GNUNET_REGEX_State *s, const char label)
+dfa_move (struct GNUNET_REGEX_State *s, const char *label)
 {
   struct GNUNET_REGEX_Transition *t;
   struct GNUNET_REGEX_State *new_s;
@@ -1378,7 +1444,9 @@ dfa_move (struct GNUNET_REGEX_State *s, const char label)
 
   for (t = s->transitions_head; NULL != t; t = t->next)
   {
-    if (label == t->label)
+    // TODO: Use strstr to match substring and return number of char's that have
+    // been consumed'
+    if (0 == strcmp (label, t->label))
     {
       new_s = t->to_state;
       break;
@@ -1388,6 +1456,20 @@ dfa_move (struct GNUNET_REGEX_State *s, const char label)
   return new_s;
 }
 
+/**
+ * Set the given state 'marked' to GNUNET_YES. Used by the
+ * 'dfa_remove_unreachable_states' function to detect unreachable states in the
+ * automaton.
+ *
+ * @param cls closure, not used.
+ * @param count count, not used.
+ * @param s state where the marked attribute will be set to GNUNET_YES.
+ */
+void
+mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
+{
+  s->marked = GNUNET_YES;
+}
 
 /**
  * Remove all unreachable states from DFA 'a'. Unreachable states are those
@@ -1406,7 +1488,7 @@ dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
     s->marked = GNUNET_NO;
 
   // 2. traverse dfa from start state and mark all visited states
-  GNUNET_REGEX_automaton_traverse (a, NULL, NULL);
+  GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
 
   // 3. delete all states that were not visited
   for (s = a->states_head; NULL != s; s = s_next)
@@ -1467,7 +1549,6 @@ static void
 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
                                      struct GNUNET_REGEX_Automaton *a)
 {
-  unsigned int i;
   int table[a->state_count][a->state_count];
   struct GNUNET_REGEX_State *s1;
   struct GNUNET_REGEX_State *s2;
@@ -1477,6 +1558,7 @@ dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
   struct GNUNET_REGEX_State *s2_next;
   int change;
   unsigned int num_equal_edges;
+  unsigned int i;
 
   for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
        i++, s1 = s1->next)
@@ -1516,13 +1598,13 @@ dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
         {
           for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
           {
-            if (t1->label == t2->label)
+            if (0 == strcmp (t1->label, t2->label))
             {
               num_equal_edges++;
               if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
                   0 != table[t2->to_state->marked][t1->to_state->marked])
               {
-                table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
+                table[s1->marked][s2->marked] = 1;
                 change = 1;
               }
             }
@@ -1599,13 +1681,16 @@ nfa_fragment_create (struct GNUNET_REGEX_State *start,
   n->type = NFA;
   n->start = NULL;
   n->end = NULL;
+  n->state_count = 0;
 
-  if (NULL == start && NULL == end)
+  if (NULL == start || NULL == end)
     return n;
 
   automaton_add_state (n, end);
   automaton_add_state (n, start);
 
+  n->state_count = 2;
+
   n->start = start;
   n->end = end;
 
@@ -1667,7 +1752,7 @@ nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
   s->id = ctx->state_id++;
   s->accepting = accepting;
-  s->marked = 0;
+  s->marked = GNUNET_NO;
   s->contained = 0;
   s->index = -1;
   s->lowlink = -1;
@@ -1685,13 +1770,13 @@ nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
  * @param nfa the NFA containing 's'
  * @param s starting point state
  * @param label transitioning label on which to base the closure on,
- *                pass 0 for epsilon transition
+ *                pass NULL for epsilon transition
  *
- * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
+ * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
  */
 static struct GNUNET_REGEX_StateSet *
 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
-                    struct GNUNET_REGEX_State *s, const char label)
+                    struct GNUNET_REGEX_State *s, const char *label)
 {
   struct GNUNET_REGEX_StateSet *cls;
   struct GNUNET_REGEX_StateSet *cls_check;
@@ -1709,7 +1794,7 @@ nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
     clsstate->contained = 0;
 
   // Add start state to closure only for epsilon closure
-  if (0 == label)
+  if (NULL == label)
     GNUNET_array_append (cls->states, cls->len, s);
 
   GNUNET_array_append (cls_check->states, cls_check->len, s);
@@ -1721,7 +1806,7 @@ nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
     for (ctran = currentstate->transitions_head; NULL != ctran;
          ctran = ctran->next)
     {
-      if (NULL != ctran->to_state && label == ctran->label)
+      if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
       {
         clsstate = ctran->to_state;
 
@@ -1752,13 +1837,13 @@ nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
  * @param nfa the NFA containing 's'
  * @param states list of states on which to base the closure on
  * @param label transitioning label for which to base the closure on,
- *                pass 0 for epsilon transition
+ *                pass NULL for epsilon transition
  *
- * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
+ * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
  */
 static struct GNUNET_REGEX_StateSet *
 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
-                        struct GNUNET_REGEX_StateSet *states, const char label)
+                        struct GNUNET_REGEX_StateSet *states, const char *label)
 {
   struct GNUNET_REGEX_State *s;
   struct GNUNET_REGEX_StateSet *sset;
@@ -1813,7 +1898,7 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
 
   b = ctx->stack_tail;
   GNUNET_assert (NULL != b);
@@ -1822,19 +1907,20 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
   GNUNET_assert (NULL != a);
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
-  state_add_transition (ctx, a->end, 0, b->start);
+  state_add_transition (ctx, a->end, NULL, b->start);
   a->end->accepting = 0;
   b->end->accepting = 1;
 
-  new = nfa_fragment_create (NULL, NULL);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
-  new->start = a->start;
-  new->end = b->end;
+  new_nfa = nfa_fragment_create (NULL, NULL);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
+  new_nfa->start = a->start;
+  new_nfa->end = b->end;
+  new_nfa->state_count += a->state_count + b->state_count;
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1847,12 +1933,11 @@ static void
 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1861,22 +1946,24 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
-  state_add_transition (ctx, start, 0, a->start);
-  state_add_transition (ctx, start, 0, end);
-  state_add_transition (ctx, a->end, 0, a->start);
-  state_add_transition (ctx, a->end, 0, end);
+  state_add_transition (ctx, start, NULL, a->start);
+  state_add_transition (ctx, start, NULL, end);
+  state_add_transition (ctx, a->end, NULL, a->start);
+  state_add_transition (ctx, a->end, NULL, end);
 
   a->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1893,7 +1980,7 @@ nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
   a = ctx->stack_tail;
   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
-  state_add_transition (ctx, a->end, 0, a->start);
+  state_add_transition (ctx, a->end, NULL, a->start);
 
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
 }
@@ -1908,12 +1995,11 @@ static void
 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1922,20 +2008,22 @@ nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
-  state_add_transition (ctx, start, 0, a->start);
-  state_add_transition (ctx, start, 0, end);
-  state_add_transition (ctx, a->end, 0, end);
+  state_add_transition (ctx, start, NULL, a->start);
+  state_add_transition (ctx, start, NULL, end);
+  state_add_transition (ctx, a->end, NULL, end);
 
   a->end->accepting = 0;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1950,7 +2038,7 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
@@ -1963,23 +2051,23 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
 
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
-  state_add_transition (ctx, start, 0, a->start);
-  state_add_transition (ctx, start, 0, b->start);
+  state_add_transition (ctx, start, NULL, a->start);
+  state_add_transition (ctx, start, NULL, b->start);
 
-  state_add_transition (ctx, a->end, 0, end);
-  state_add_transition (ctx, b->end, 0, end);
+  state_add_transition (ctx, a->end, NULL, end);
+  state_add_transition (ctx, b->end, NULL, end);
 
   a->end->accepting = 0;
   b->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1987,10 +2075,10 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
  * Adds a new nfa fragment to the stack
  *
  * @param ctx context
- * @param lit label for nfa transition
+ * @param label label for nfa transition
  */
 static void
-nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
+nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
 {
   struct GNUNET_REGEX_Automaton *n;
   struct GNUNET_REGEX_State *start;
@@ -2000,7 +2088,7 @@ nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
 
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
-  state_add_transition (ctx, start, lit, end);
+  state_add_transition (ctx, start, label, end);
   n = nfa_fragment_create (start, end);
   GNUNET_assert (NULL != n);
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
@@ -2041,6 +2129,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
   struct GNUNET_REGEX_Context ctx;
   struct GNUNET_REGEX_Automaton *nfa;
   const char *regexp;
+  char curlabel[2];
   char *error_msg;
   unsigned int count;
   unsigned int altcount;
@@ -2055,6 +2144,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
   GNUNET_REGEX_context_init (&ctx);
 
   regexp = regex;
+  curlabel[1] = '\0';
   p = NULL;
   error_msg = NULL;
   altcount = 0;
@@ -2134,16 +2224,14 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
       }
       nfa_add_question_op (&ctx);
       break;
-    case 92:                   /* escape: \ */
-      regexp++;
-      count++;
     default:
       if (atomcount > 1)
       {
         --atomcount;
         nfa_add_concatenation (&ctx);
       }
-      nfa_add_label (&ctx, *regexp);
+      curlabel[0] = *regexp;
+      nfa_add_label (&ctx, curlabel);
       atomcount++;
       break;
     }
@@ -2169,8 +2257,15 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
     goto error;
   }
 
+  /* Remember the regex that was used to generate this NFA */
   nfa->regex = GNUNET_strdup (regex);
 
+  /* create depth-first numbering of the states for pretty printing */
+  GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
+
+  /* No multistriding added so far */
+  nfa->is_multistrided = GNUNET_NO;
+
   return nfa;
 
 error:
@@ -2214,7 +2309,7 @@ construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
 
   for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
   {
-    if (0 == ctran->label || NULL != ctran->to_state)
+    if (NULL == ctran->label || NULL != ctran->to_state)
       continue;
 
     tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
@@ -2258,7 +2353,7 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   struct GNUNET_REGEX_Context ctx;
   struct GNUNET_REGEX_Automaton *dfa;
   struct GNUNET_REGEX_Automaton *nfa;
-  struct GNUNET_REGEX_StateSet *nfa_set;
+  struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
 
   GNUNET_REGEX_context_init (&ctx);
 
@@ -2274,11 +2369,15 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
 
   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
   dfa->type = DFA;
+  dfa->state_count = 0;
+  dfa->states_head = NULL;
+  dfa->states_tail = NULL;
   dfa->regex = GNUNET_strdup (regex);
+  dfa->is_multistrided = GNUNET_NO;
 
   // Create DFA start state from epsilon closure
-  nfa_set = nfa_closure_create (nfa, nfa->start, 0);
-  dfa->start = dfa_state_create (&ctx, nfa_set);
+  nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, 0);
+  dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
   automaton_add_state (dfa, dfa->start);
 
   construct_dfa_states (&ctx, nfa, dfa, dfa->start);
@@ -2291,6 +2390,9 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   // Create proofs for all states
   automaton_create_proofs (dfa);
 
+  // Add strides to DFA
+  // GNUNET_REGEX_add_multi_strides_to_dfa (&ctx, dfa, 2);
+
   return dfa;
 }
 
@@ -2336,6 +2438,7 @@ static int
 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
 {
   const char *strp;
+  char str[2];
   struct GNUNET_REGEX_State *s;
 
   if (DFA != a->type)
@@ -2351,9 +2454,11 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   if ((NULL == string || 0 == strlen (string)) && s->accepting)
     return 0;
 
+  str[1] = '\0';
   for (strp = string; NULL != strp && *strp; strp++)
   {
-    s = dfa_move (s, *strp);
+    str[0] = *strp;
+    s = dfa_move (s, str);
     if (NULL == s)
       break;
   }
@@ -2377,6 +2482,7 @@ static int
 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
 {
   const char *strp;
+  char str[2];
   struct GNUNET_REGEX_State *s;
   struct GNUNET_REGEX_StateSet *sset;
   struct GNUNET_REGEX_StateSet *new_sset;
@@ -2397,9 +2503,11 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   result = 1;
   sset = nfa_closure_create (a, a->start, 0);
 
+  str[1] = '\0';
   for (strp = string; NULL != strp && *strp; strp++)
   {
-    new_sset = nfa_closure_set_create (a, sset, *strp);
+    str[0] = *strp;
+    new_sset = nfa_closure_set_create (a, sset, str);
     state_set_clear (sset);
     sset = nfa_closure_set_create (a, new_sset, 0);
     state_set_clear (new_sset);
@@ -2473,6 +2581,31 @@ GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
 }
 
 
+/**
+ * Get the number of transitions that are contained in the given automaton.
+ *
+ * @param a automaton for which the number of transitions should be returned.
+ *
+ * @return number of transitions in the given automaton.
+ */
+unsigned int
+GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
+{
+  unsigned int t_count;
+  struct GNUNET_REGEX_State *s;
+
+  if (NULL == a)
+    return 0;
+
+  for (t_count = 0, s = a->states_head; NULL != s; s = s->next)
+  {
+    t_count += s->transition_count;
+  }
+
+  return t_count;
+}
+
+
 /**
  * Get the first key for the given 'input_string'. This hashes the first x bits
  * of the 'input_string'.
@@ -2517,6 +2650,12 @@ GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
 {
   struct GNUNET_HashCode key_check;
 
+  if (NULL == proof || NULL == key)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
+    return GNUNET_NO;
+  }
+
   GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
   return (0 ==
           GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
@@ -2542,7 +2681,6 @@ iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
                       GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
 {
   unsigned int i;
-  char label[2];
   char *temp;
   struct GNUNET_REGEX_Transition *t;
   unsigned int num_edges = state->transition_count;
@@ -2553,9 +2691,7 @@ iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
   {
     for (i = 0, t = state->transitions_head; NULL != t; t = t->next, i++)
     {
-      label[0] = t->label;
-      label[1] = '\0';
-      edges[i].label = label;
+      edges[i].label = t->label;
       edges[i].destination = t->to_state->hash;
     }
 
@@ -2570,9 +2706,9 @@ iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
     for (t = state->transitions_head; NULL != t; t = t->next)
     {
       if (NULL != consumed_string)
-        GNUNET_asprintf (&temp, "%s%c", consumed_string, t->label);
+        GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
       else
-        GNUNET_asprintf (&temp, "%c", t->label);
+        GNUNET_asprintf (&temp, "%s", t->label);
 
       iterate_initial_edge (min_len, max_len, cur_len, temp, t->to_state,
                             iterator, iterator_cls);
@@ -2585,10 +2721,14 @@ iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
 /**
  * Iterate over all initial edges that aren't actually part of the automaton.
  * This is needed to find the initial states returned by
- * GNUNET_REGEX_get_first_key. Iteration will start at the first branch state (a
- * state that has more than one outgoing edge, can be the first state), because
- * all previous states will have the same proof and be iterated over in
- * iterate_all_edges.
+ * GNUNET_REGEX_get_first_key. Iteration will start at the first state that has
+ * more than one outgoing edge, i.e. the state that branches the graph.
+ * For example consider the following graph:
+ * a -> b -> c -> d -> ...
+ *            \-> e -> ...
+ *
+ * This function will not iterate over the edges leading to "c", because these
+ * will be covered by the iterate_edges function.
  *
  * @param a the automaton for which the initial states should be computed.
  * @param initial_len length of the initial state string.
@@ -2619,15 +2759,15 @@ iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
       if (NULL != consumed_string)
       {
         temp = consumed_string;
-        GNUNET_asprintf (&consumed_string, "%s%c", consumed_string,
+        GNUNET_asprintf (&consumed_string, "%s%s", consumed_string,
                          s->transitions_head->label);
         GNUNET_free (temp);
       }
       else
-        GNUNET_asprintf (&consumed_string, "%c", s->transitions_head->label);
+        GNUNET_asprintf (&consumed_string, "%s", s->transitions_head->label);
 
       s = s->transitions_head->to_state;
-      cur_len++;
+      cur_len += strlen (s->transitions_head->label);
     }
     while (cur_len < initial_len && 1 == s->transition_count);
   }
@@ -2641,7 +2781,7 @@ iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
 
 /**
  * Iterate over all edges helper function starting from state 's', calling
- * iterator on for each edge.
+ * iterator function for each edge.
  *
  * @param s state.
  * @param iterator iterator function called for each edge.
@@ -2661,7 +2801,7 @@ iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
 
     num_edges = state_get_edges (s, edges);
 
-    if (0 < strlen (s->proof) || s->accepting)
+    if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
       iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
                 edges);