Added '?' operator
[oweals/gnunet.git] / src / regex / regex.c
index 080e5d31e26f3c80f210b017b17ede0c209583d8..a240fcd8023a466919a9bcc90584d4c2b5f25501 100644 (file)
  */
 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
+   * 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;
 };
 
+/**
+ * Type of an automaton.
+ */
 enum GNUNET_REGEX_automaton_type
 {
   NFA,
@@ -50,20 +64,51 @@ enum GNUNET_REGEX_automaton_type
 };
 
 /**
- * Automaton representation
+ * Automaton representation.
  */
 struct GNUNET_REGEX_Automaton
 {
+  /**
+   * This is a linked list.
+   */
   struct GNUNET_REGEX_Automaton *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct GNUNET_REGEX_Automaton *next;
 
+  /**
+   * First state of the automaton. This is mainly
+   * used for constructing an NFA, where each NFA
+   * itself consists of one or more NFAs linked
+   * together.
+   */
   struct State *start;
+
+  /**
+   * End state of the automaton.
+   */
   struct State *end;
 
+  /**
+   * Number of states in the automaton.
+   */
   unsigned int state_count;
+
+  /**
+   * DLL of states.
+   */
   struct State *states_head;
+
+  /**
+   * DLL of states
+   */
   struct State *states_tail;
 
+  /**
+   * Type of the automaton.
+   */
   enum GNUNET_REGEX_automaton_type type;
 };
 
@@ -72,18 +117,58 @@ struct GNUNET_REGEX_Automaton
  */
 struct State
 {
+  /**
+   * This is a linked list.
+   */
   struct State *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct State *next;
 
+  /**
+   * Unique state id.
+   */
   unsigned int id;
+
+  /**
+   * If this is an accepting state or not.
+   */
   int accepting;
+
+  /**
+   * Marking of the state. This is used for marking all visited
+   * states when traversing all states of an automaton and for
+   * cases where the state id cannot be used (dfa minimization).
+   */
   int marked;
+
+  /**
+   * Human readable name of the automaton. Used for debugging
+   * and graph creation.
+   */
   char *name;
 
+  /**
+   * Number of transitions from this state to other states.
+   */
   unsigned int transition_count;
+
+  /**
+   * DLL of transitions.
+   */
   struct Transition *transitions_head;
+
+  /**
+   * DLL of transitions.
+   */
   struct Transition *transitions_tail;
 
+  /**
+   * Set of states on which this state is based on. Used when
+   * creating a DFA out of several NFA states.
+   */
   struct StateSet *nfa_set;
 };
 
@@ -93,23 +178,46 @@ struct State
  */
 struct Transition
 {
+  /**
+   * This is a linked list.
+   */
   struct Transition *prev;
+
+  /**
+   * This is a linked list.
+   */
   struct Transition *next;
 
+  /**
+   * Unique id of this transition.
+   */
   unsigned int id;
+
+  /**
+   * Literal for this transition. This is basically the edge label for
+   * the graph.
+   */
   char literal;
+
+  /**
+   * State to which this transition leads.
+   */
   struct State *state;
 };
 
 /**
- * Set of states
+ * Set of states.
  */
 struct StateSet
 {
   /**
-   * Array of states
+   * Array of states.
    */
   struct State **states;
+
+  /**
+   * Length of the 'states' array.
+   */
   unsigned int len;
 };
 
@@ -177,6 +285,16 @@ debug_print_transitions (struct State *s)
   }
 }
 
+/**
+ * Compare two states. Used for sorting.
+ *
+ * @param a first state
+ * @param b second state
+ *
+ * @return an integer less than, equal to, or greater than zero
+ *         if the first argument is considered to be respectively
+ *         less than, equal to, or greater than the second.
+ */
 static int
 state_compare (const void *a, const void *b)
 {
@@ -196,24 +314,29 @@ state_compare (const void *a, const void *b)
  * @param sset1 first state set
  * @param sset2 second state set
  *
- * @return 0 if they are equal, non 0 otherwise
+ * @return an integer less than, equal to, or greater than zero
+ *         if the first argument is considered to be respectively
+ *         less than, equal to, or greater than the second.
  */
 static int
 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
 {
+  int result;
   int i;
 
-  if (sset1->len != sset2->len)
+  if (NULL == sset1 || NULL == sset2)
     return 1;
 
+  result = sset1->len - sset2->len;
+
   for (i = 0; i < sset1->len; i++)
   {
-    if (sset1->states[i]->id != sset2->states[i]->id)
-    {
-      return 1;
-    }
+    if (0 != result)
+      break;
+
+    result = state_compare (&sset1->states[i], &sset2->states[i]);
   }
-  return 0;
+  return result;
 }
 
 /**
@@ -294,6 +417,9 @@ add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
 static void
 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
 {
+  if (NULL == a)
+    return;
+
   a->start = NULL;
   a->end = NULL;
   a->states_head = NULL;
@@ -313,6 +439,9 @@ automaton_destroy_state (struct State *s)
   struct Transition *t;
   struct Transition *next_t;
 
+  if (NULL == s)
+    return;
+
   if (NULL != s->name)
     GNUNET_free (s->name);
 
@@ -329,16 +458,113 @@ automaton_destroy_state (struct State *s)
   GNUNET_free (s);
 }
 
+/**
+ * Remove a state from the given automaton 'a'. Always use this function
+ * when altering the states of an automaton. Will also remove all transitions
+ * leading to this state, before destroying it.
+ *
+ * @param a automaton
+ * @param s state to remove
+ */
 static void
 automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
 {
   struct State *ss;
+  struct State *s_check;
+  struct Transition *t_check;
+
+  if (NULL == a || NULL == s)
+    return;
+
+  // remove state
   ss = s;
   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
   a->state_count--;
+
+  // remove all transitions leading to this state
+  for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
+  {
+    for (t_check = s_check->transitions_head; NULL != t_check;
+         t_check = t_check->next)
+    {
+      if (t_check->state == ss)
+      {
+        GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
+                                     s_check->transitions_tail, t_check);
+        s_check->transition_count--;
+      }
+    }
+  }
+
   automaton_destroy_state (ss);
 }
 
+/**
+ * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
+ *
+ * @param ctx context
+ * @param a automaton
+ * @param s1 first state
+ * @param s2 second state, will be destroyed
+ */
+static void
+automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
+                        struct GNUNET_REGEX_Automaton *a, struct State *s1,
+                        struct State *s2)
+{
+  struct State *s_check;
+  struct Transition *t_check;
+  struct Transition *t;
+  char *new_name;
+
+  GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
+
+  // 1. Make all transitions pointing to s2 point to s1
+  for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
+  {
+    for (t_check = s_check->transitions_head; NULL != t_check;
+         t_check = t_check->next)
+    {
+      if (s_check != s1 && s2 == t_check->state)
+        t_check->state = s1;
+    }
+  }
+
+  // 2. Add all transitions from s2 to sX to s1
+  for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
+  {
+    for (t = s1->transitions_head; NULL != t; t = t->next)
+    {
+      if (t_check->literal != t->literal && NULL != t_check->state &&
+          t_check->state != t->state && t_check->state != s2)
+      {
+        add_transition (ctx, s1, t_check->literal, t_check->state);
+      }
+    }
+  }
+
+  // 3. Rename s1 to {s1,s2}
+  new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
+  strncat (new_name, s1->name, strlen (s1->name));
+  strncat (new_name, s2->name, strlen (s2->name));
+  if (NULL != s1->name)
+    GNUNET_free (s1->name);
+  s1->name = new_name;
+
+  // remove state
+  s_check = s2;
+  GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
+  a->state_count--;
+  automaton_destroy_state (s_check);
+}
+
+/**
+ * Add a state to the automaton 'a', always use this function to
+ * alter the states DLL of the automaton.
+ *
+ * @param a automaton to add the state to
+ * @param s state that should be added
+ */
 static void
 automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
 {
@@ -493,17 +719,14 @@ dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
   stack_len++;
   while (stack_len > 0)
   {
-    s = stack[stack_len-1];
+    s = stack[stack_len - 1];
     stack_len--;
-    s->marked = 1; // mark s as visited
+    s->marked = 1;              // mark s as visited
     for (t = s->transitions_head; NULL != t; t = t->next)
     {
+      // add next states to stack
       if (NULL != t->state && 0 == t->state->marked)
-      {
-        // add next states to stack
-        stack[stack_len] = t->state;
-        stack_len++;
-      }
+        stack[++stack_len] = t->state;
     }
   }
 
@@ -525,9 +748,7 @@ static void
 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
 {
   struct State *s;
-  struct State *s_check;
   struct Transition *t;
-  struct Transition *t_check;
   int dead;
 
   GNUNET_assert (DFA == a->type);
@@ -551,20 +772,6 @@ dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
       continue;
 
     // state s is dead, remove it
-    // 1. remove all transitions to this state
-    for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
-    {
-      for (t_check = s_check->transitions_head; NULL != t_check;
-           t_check = t_check->next)
-      {
-        if (t_check->state == s)
-        {
-          GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
-                                       s_check->transitions_tail, t_check);
-        }
-      }
-    }
-    // 2. remove state
     automaton_remove_state (a, s);
   }
 }
@@ -572,23 +779,100 @@ dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
 /**
  * Merge all non distinguishable states in the DFA 'a'
  *
+ * @param ctx context
  * @param a DFA automaton
  */
 static void
-dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Automaton *a)
+dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
+                                     struct GNUNET_REGEX_Automaton *a)
 {
+  int i;
+  int table[a->state_count][a->state_count];
+  struct State *s1;
+  struct State *s2;
+  struct Transition *t1;
+  struct Transition *t2;
+  int change;
+
+  change = 1;
+  for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
+       i++, s1 = s1->next)
+    s1->marked = i;
+
+  // Mark all pairs of accepting/!accepting states
+  for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+  {
+    for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+    {
+      if ((s1->accepting && !s2->accepting) ||
+          (!s1->accepting && s2->accepting))
+      {
+        table[s1->marked][s2->marked] = 1;
+      }
+      else
+        table[s1->marked][s2->marked] = 0;
+    }
+  }
+
+  while (0 != change)
+  {
+    change = 0;
+    for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+    {
+      for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+      {
+        if (0 != table[s1->marked][s2->marked])
+          continue;
+
+        for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
+        {
+          for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
+          {
+            if (t1->literal == t2->literal && t1->state == t2->state &&
+                (0 != table[t1->state->marked][t2->state->marked] ||
+                 0 != table[t2->state->marked][t1->state->marked]))
+            {
+              table[s1->marked][s2->marked] = t1->literal;
+              change = 1;
+            }
+            else if (t1->literal != t2->literal && t1->state != t2->state)
+            {
+              table[s1->marked][s2->marked] = -1;
+              change = 1;
+            }
+          }
+        }
+      }
+    }
+  }
 
+  struct State *s2_next;
+
+  for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
+  {
+    for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
+    {
+      s2_next = s2->next;
+      if (s1 != s2 && table[s1->marked][s2->marked] == 0)
+        automaton_merge_states (ctx, a, s1, s2);
+    }
+  }
 }
 
 /**
  * Minimize the given DFA 'a' by removing all unreachable states,
  * removing all dead states and merging all non distinguishable states
  *
+ * @param ctx context
  * @param a DFA automaton
  */
 static void
-dfa_minimize (struct GNUNET_REGEX_Automaton *a)
+dfa_minimize (struct GNUNET_REGEX_Context *ctx,
+              struct GNUNET_REGEX_Automaton *a)
 {
+  if (NULL == a)
+    return;
+
   GNUNET_assert (DFA == a->type);
 
   // 1. remove unreachable states
@@ -598,7 +882,7 @@ dfa_minimize (struct GNUNET_REGEX_Automaton *a)
   dfa_remove_dead_states (a);
 
   // 3. Merge nondistinguishable states
-  dfa_merge_nondistinguishable_states (a);
+  dfa_merge_nondistinguishable_states (ctx, a);
 }
 
 /**
@@ -692,13 +976,13 @@ nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
 }
 
 /**
- * Calculates the NFA closure set for the given state
+ * Calculates the NFA closure set for the given state.
  *
  * @param s starting point state
  * @param literal transitioning literal on which to base the closure on,
  *                pass 0 for epsilon transition
  *
- * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
  */
 static struct StateSet *
 nfa_closure_create (struct State *s, const char literal)
@@ -757,7 +1041,7 @@ nfa_closure_create (struct State *s, const char literal)
  * @param literal transitioning literal for which to base the closure on,
  *                pass 0 for epsilon transition
  *
- * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
  */
 static struct StateSet *
 nfa_closure_set_create (struct StateSet *states, const char literal)
@@ -894,6 +1178,45 @@ nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
 }
 
+/**
+ * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
+ *
+ * @param ctx context
+ */
+static void
+nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
+{
+  struct GNUNET_REGEX_Automaton *a;
+  struct GNUNET_REGEX_Automaton *new;
+  struct State *start;
+  struct State *end;
+
+  a = ctx->stack_tail;
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
+  if (NULL == a)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "nfa_add_question_op failed, because there was no element on the stack");
+    return;
+  }
+
+  start = nfa_state_create (ctx, 0);
+  end = nfa_state_create (ctx, 1);
+
+  add_transition (ctx, start, 0, a->start);
+  add_transition (ctx, start, 0, end);
+  add_transition (ctx, a->end, 0, end);
+
+  a->end->accepting = 0;
+
+  new = nfa_fragment_create (start, end);
+  nfa_add_states (new, a->states_head, a->states_tail);
+  automaton_fragment_clear (a);
+
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+}
+
 /**
  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
  * that alternates between a and b (a|b)
@@ -1057,6 +1380,14 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
       }
       nfa_add_plus_op (&ctx);
       break;
+    case '?':
+      if (atomcount == 0)
+      {
+        error_msg = "Cannot append '?' to nothing";
+        goto error;
+      }
+      nfa_add_question_op (&ctx);
+      break;
     case 92:                   /* escape: \ */
       regexp++;
       count++;
@@ -1100,7 +1431,8 @@ error:
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
   if (NULL != error_msg)
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
-  GNUNET_free (p);
+  if (NULL != p)
+    GNUNET_free (p);
   while (NULL != ctx.stack_tail)
   {
     GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
@@ -1163,6 +1495,13 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   // Create NFA
   nfa = GNUNET_REGEX_construct_nfa (regex, len);
 
+  if (NULL == nfa)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Could not create DFA, because NFA creation failed\n");
+    return NULL;
+  }
+
   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
   dfa->type = DFA;
 
@@ -1172,6 +1511,8 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   dfa->start = dfa_state_create (&ctx, nfa_set);
   automaton_add_state (dfa, dfa->start);
   GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
+
+  // Create dfa states by combining nfa states
   while (dfa_stack->len > 0)
   {
     dfa_state = dfa_stack->states[dfa_stack->len - 1];
@@ -1214,7 +1555,7 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
   GNUNET_free (dfa_stack);
   GNUNET_REGEX_automaton_destroy (nfa);
 
-  dfa_minimize (dfa);
+  dfa_minimize (&ctx, dfa);
 
   return dfa;
 }
@@ -1390,7 +1731,6 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
   return result;
 }
 
-
 /**
  * Evaluates the given 'string' against the given compiled regex
  *