2152167dc3944e57bb44bd7f9e97326fd4e584bd
[oweals/gnunet.git] / src / regex / regex.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file src/regex/regex.c
22  * @brief library to create automatons from regular expressions
23  * @author Maximilian Szengel
24  */
25 #include "platform.h"
26 #include "gnunet_container_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_regex_lib.h"
29 #include "regex_internal.h"
30
31
32 /**
33  * Constant for how many bits the initial string regex should have.
34  */
35 #define INITIAL_BITS 8
36
37
38 /**
39  * Context that contains an id counter for states and transitions as well as a
40  * DLL of automatons used as a stack for NFA construction.
41  */
42 struct GNUNET_REGEX_Context
43 {
44   /**
45    * Unique state id.
46    */
47   unsigned int state_id;
48
49   /**
50    * Unique transition id.
51    */
52   unsigned int transition_id;
53
54   /**
55    * DLL of GNUNET_REGEX_Automaton's used as a stack.
56    */
57   struct GNUNET_REGEX_Automaton *stack_head;
58
59   /**
60    * DLL of GNUNET_REGEX_Automaton's used as a stack.
61    */
62   struct GNUNET_REGEX_Automaton *stack_tail;
63 };
64
65
66 /**
67  * Set of states.
68  */
69 struct GNUNET_REGEX_StateSet
70 {
71   /**
72    * Array of states.
73    */
74   struct GNUNET_REGEX_State **states;
75
76   /**
77    * Length of the 'states' array.
78    */
79   unsigned int len;
80 };
81
82
83 /*
84  * Debug helper functions
85  */
86
87 /**
88  * Print all the transitions of state 's'.
89  *
90  * @param s state for which to print it's transitions.
91  */
92 void
93 debug_print_transitions (struct GNUNET_REGEX_State *s);
94
95
96 /**
97  * Print information of the given state 's'.
98  *
99  * @param s state for which debug information should be printed.
100  */
101 void
102 debug_print_state (struct GNUNET_REGEX_State *s)
103 {
104   char *proof;
105
106   if (NULL == s->proof)
107     proof = "NULL";
108   else
109     proof = s->proof;
110
111   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112               "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
113               s->id, s->name, s->marked, s->accepting, s->scc_id,
114               s->transition_count, proof);
115
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
117   debug_print_transitions (s);
118 }
119
120
121 /**
122  * Print debug information for all states contained in the automaton 'a'.
123  *
124  * @param a automaton for which debug information of it's states should be printed.
125  */
126 void
127 debug_print_states (struct GNUNET_REGEX_Automaton *a)
128 {
129   struct GNUNET_REGEX_State *s;
130
131   for (s = a->states_head; NULL != s; s = s->next)
132     debug_print_state (s);
133 }
134
135
136 /**
137  * Print debug information for given transition 't'.
138  *
139  * @param t transition for which to print debug info.
140  */
141 void
142 debug_print_transition (struct GNUNET_REGEX_Transition *t)
143 {
144   char *to_state;
145   char *from_state;
146   char label;
147
148   if (NULL == t)
149     return;
150
151   if (0 == t->label)
152     label = '0';
153   else
154     label = t->label;
155
156   if (NULL == t->to_state)
157     to_state = "NULL";
158   else
159     to_state = t->to_state->name;
160
161   if (NULL == t->from_state)
162     from_state = "NULL";
163   else
164     from_state = t->from_state->name;
165
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
167               t->id, from_state, label, to_state);
168 }
169
170
171 void
172 debug_print_transitions (struct GNUNET_REGEX_State *s)
173 {
174   struct GNUNET_REGEX_Transition *t;
175
176   for (t = s->transitions_head; NULL != t; t = t->next)
177     debug_print_transition (t);
178 }
179
180
181 /**
182  * Adds a transition from one state to another on 'label'. Does not add
183  * duplicate states.
184  *
185  * @param ctx context
186  * @param from_state starting state for the transition
187  * @param label transition label
188  * @param to_state state to where the transition should point to
189  */
190 static void
191 state_add_transition (struct GNUNET_REGEX_Context *ctx,
192                       struct GNUNET_REGEX_State *from_state, const char label,
193                       struct GNUNET_REGEX_State *to_state)
194 {
195   int is_dup;
196   struct GNUNET_REGEX_Transition *t;
197   struct GNUNET_REGEX_Transition *oth;
198
199   if (NULL == from_state)
200   {
201     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
202     return;
203   }
204
205   // Do not add duplicate state transitions
206   is_dup = GNUNET_NO;
207   for (t = from_state->transitions_head; NULL != t; t = t->next)
208   {
209     if (t->to_state == to_state && t->label == label &&
210         t->from_state == from_state)
211     {
212       is_dup = GNUNET_YES;
213       break;
214     }
215   }
216
217   if (GNUNET_YES == is_dup)
218     return;
219
220   // sort transitions by label
221   for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
222   {
223     if (oth->label > label)
224       break;
225   }
226
227   t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
228   t->id = ctx->transition_id++;
229   t->label = label;
230   t->to_state = to_state;
231   t->from_state = from_state;
232
233   // Add outgoing transition to 'from_state'
234   from_state->transition_count++;
235   GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
236                                       from_state->transitions_tail, oth, t);
237 }
238
239
240 /**
241  * Remove a 'transition' from 'state'.
242  *
243  * @param state state from which the to-be-removed transition originates.
244  * @param transition transition that should be removed from state 'state'.
245  */
246 static void
247 state_remove_transition (struct GNUNET_REGEX_State *state,
248                          struct GNUNET_REGEX_Transition *transition)
249 {
250   if (NULL == state || NULL == transition)
251     return;
252
253   if (transition->from_state != state)
254     return;
255
256   state->transition_count--;
257   GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
258                                transition);
259   GNUNET_free (transition);
260 }
261
262
263 /**
264  * Compare two states. Used for sorting.
265  *
266  * @param a first state
267  * @param b second state
268  *
269  * @return an integer less than, equal to, or greater than zero
270  *         if the first argument is considered to be respectively
271  *         less than, equal to, or greater than the second.
272  */
273 static int
274 state_compare (const void *a, const void *b)
275 {
276   struct GNUNET_REGEX_State **s1;
277   struct GNUNET_REGEX_State **s2;
278
279   s1 = (struct GNUNET_REGEX_State **) a;
280   s2 = (struct GNUNET_REGEX_State **) b;
281
282   return (*s1)->id - (*s2)->id;
283 }
284
285
286 /**
287  * Get all edges leaving state 's'.
288  *
289  * @param s state.
290  * @param edges all edges leaving 's', expected to be allocated and have enough
291  *        space for s->transitions_count elements.
292  *
293  * @return number of edges.
294  */
295 static unsigned int
296 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
297 {
298   struct GNUNET_REGEX_Transition *t;
299   unsigned int count;
300
301   if (NULL == s)
302     return 0;
303
304   count = 0;
305
306   for (t = s->transitions_head; NULL != t; t = t->next)
307   {
308     if (NULL != t->to_state)
309     {
310       edges[count].label = &t->label;
311       edges[count].destination = t->to_state->hash;
312       count++;
313     }
314   }
315   return count;
316 }
317
318
319 /**
320  * Compare to state sets by comparing the id's of the states that are contained
321  * in each set. Both sets are expected to be sorted by id!
322  *
323  * @param sset1 first state set
324  * @param sset2 second state set
325  *
326  * @return an integer less than, equal to, or greater than zero
327  *         if the first argument is considered to be respectively
328  *         less than, equal to, or greater than the second.
329  */
330 static int
331 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
332                    struct GNUNET_REGEX_StateSet *sset2)
333 {
334   int result;
335   unsigned int i;
336
337   if (NULL == sset1 || NULL == sset2)
338     return 1;
339
340   result = sset1->len - sset2->len;
341
342   for (i = 0; i < sset1->len; i++)
343   {
344     if (0 != result)
345       break;
346
347     result = state_compare (&sset1->states[i], &sset2->states[i]);
348   }
349   return result;
350 }
351
352
353 /**
354  * Clears the given StateSet 'set'
355  *
356  * @param set set to be cleared
357  */
358 static void
359 state_set_clear (struct GNUNET_REGEX_StateSet *set)
360 {
361   if (NULL != set)
362   {
363     GNUNET_free_non_null (set->states);
364     GNUNET_free (set);
365   }
366 }
367
368
369 /**
370  * Clears an automaton fragment. Does not destroy the states inside the
371  * automaton.
372  *
373  * @param a automaton to be cleared
374  */
375 static void
376 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
377 {
378   if (NULL == a)
379     return;
380
381   a->start = NULL;
382   a->end = NULL;
383   a->states_head = NULL;
384   a->states_tail = NULL;
385   a->state_count = 0;
386   GNUNET_free (a);
387 }
388
389
390 /**
391  * Frees the memory used by State 's'
392  *
393  * @param s state that should be destroyed
394  */
395 static void
396 automaton_destroy_state (struct GNUNET_REGEX_State *s)
397 {
398   struct GNUNET_REGEX_Transition *t;
399   struct GNUNET_REGEX_Transition *next_t;
400
401   if (NULL == s)
402     return;
403
404   GNUNET_free_non_null (s->name);
405   GNUNET_free_non_null (s->proof);
406
407   for (t = s->transitions_head; NULL != t; t = next_t)
408   {
409     next_t = t->next;
410     GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
411     GNUNET_free (t);
412   }
413
414   state_set_clear (s->nfa_set);
415
416   GNUNET_free (s);
417 }
418
419
420 /**
421  * Remove a state from the given automaton 'a'. Always use this function when
422  * altering the states of an automaton. Will also remove all transitions leading
423  * to this state, before destroying it.
424  *
425  * @param a automaton
426  * @param s state to remove
427  */
428 static void
429 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
430                         struct GNUNET_REGEX_State *s)
431 {
432   struct GNUNET_REGEX_State *ss;
433   struct GNUNET_REGEX_State *s_check;
434   struct GNUNET_REGEX_Transition *t_check;
435
436   if (NULL == a || NULL == s)
437     return;
438
439   // remove state
440   ss = s;
441   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
442   a->state_count--;
443
444   // remove all transitions leading to this state
445   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
446   {
447     for (t_check = s_check->transitions_head; NULL != t_check;
448          t_check = t_check->next)
449     {
450       if (t_check->to_state == ss)
451       {
452         GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
453                                      s_check->transitions_tail, t_check);
454         s_check->transition_count--;
455       }
456     }
457   }
458
459   automaton_destroy_state (ss);
460 }
461
462
463 /**
464  * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
465  * 's2'.
466  *
467  * @param ctx context
468  * @param a automaton
469  * @param s1 first state
470  * @param s2 second state, will be destroyed
471  */
472 static void
473 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
474                         struct GNUNET_REGEX_Automaton *a,
475                         struct GNUNET_REGEX_State *s1,
476                         struct GNUNET_REGEX_State *s2)
477 {
478   struct GNUNET_REGEX_State *s_check;
479   struct GNUNET_REGEX_Transition *t_check;
480   struct GNUNET_REGEX_Transition *t;
481   struct GNUNET_REGEX_Transition *t_next;
482   char *new_name;
483   int is_dup;
484
485   GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
486
487   if (s1 == s2)
488     return;
489
490   // 1. Make all transitions pointing to s2 point to s1, unless this transition
491   // does not already exists, if it already exists remove transition.
492   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
493   {
494     for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
495     {
496       t_next = t_check->next;
497
498       if (s2 == t_check->to_state)
499       {
500         is_dup = GNUNET_NO;
501         for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
502         {
503           if (t->to_state == s1 && t_check->label == t->label)
504             is_dup = GNUNET_YES;
505         }
506         if (GNUNET_NO == is_dup)
507           t_check->to_state = s1;
508         else
509           state_remove_transition (t_check->from_state, t_check);
510       }
511     }
512   }
513
514   // 2. Add all transitions from s2 to sX to s1
515   for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
516   {
517     if (t_check->to_state != s1)
518       state_add_transition (ctx, s1, t_check->label, t_check->to_state);
519   }
520
521   // 3. Rename s1 to {s1,s2}
522   new_name = s1->name;
523   GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
524   GNUNET_free (new_name);
525
526   // remove state
527   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
528   a->state_count--;
529   automaton_destroy_state (s2);
530 }
531
532
533 /**
534  * Add a state to the automaton 'a', always use this function to alter the
535  * states DLL of the automaton.
536  *
537  * @param a automaton to add the state to
538  * @param s state that should be added
539  */
540 static void
541 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
542                      struct GNUNET_REGEX_State *s)
543 {
544   GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
545   a->state_count++;
546 }
547
548
549 /**
550  * Depth-first traversal of all states that are reachable from state 's'. Expects the states to
551  * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
552  * state.
553  *
554  * @param s start state.
555  * @param count current count of the state.
556  * @param action action to be performed on each state.
557  * @param action_cls closure for action
558  */
559 static void
560 automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
561                           GNUNET_REGEX_traverse_action action, void *action_cls)
562 {
563   struct GNUNET_REGEX_Transition *t;
564
565   if (GNUNET_NO != s->marked)
566     return;
567   s->marked = GNUNET_YES;
568   if (NULL != action)
569     action (action_cls, *count, s);
570   (*count)++;
571   for (t = s->transitions_head; NULL != t; t = t->next)
572     automaton_state_traverse (t->to_state, count, action, action_cls);
573 }
574
575
576 /**
577  * Traverses the given automaton from it's start state, visiting all reachable
578  * states and calling 'action' on each one of them.
579  *
580  * @param a automaton.
581  * @param action action to be performed on each state.
582  * @param action_cls closure for action
583  */
584 void
585 GNUNET_REGEX_automaton_traverse (struct GNUNET_REGEX_Automaton *a,
586                                  GNUNET_REGEX_traverse_action action,
587                                  void *action_cls)
588 {
589   unsigned int count;
590   struct GNUNET_REGEX_State *s;
591
592   for (s = a->states_head; NULL != s; s = s->next)
593     s->marked = GNUNET_NO;
594   count = 0;
595   automaton_state_traverse (a->start, &count, action, action_cls);
596 }
597
598
599 /**
600  * Check if the given string 'str' needs parentheses around it when
601  * using it to generate a regex.
602  *
603  * @param str string
604  *
605  * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
606  */
607 static int
608 needs_parentheses (const char *str)
609 {
610   size_t slen;
611   const char *op;
612   const char *cl;
613   const char *pos;
614   unsigned int cnt;
615
616   if ((NULL == str) || ((slen = strlen (str)) < 2))
617     return GNUNET_NO;
618
619   if ('(' != str[0])
620     return GNUNET_YES;
621   cnt = 1;
622   pos = &str[1];
623   while (cnt > 0)
624   {
625     cl = strchr (pos, ')');
626     if (NULL == cl)
627     {
628       GNUNET_break (0);
629       return GNUNET_YES;
630     }
631     op = strchr (pos, '(');
632     if ((NULL != op) && (op < cl))
633     {
634       cnt++;
635       pos = op + 1;
636       continue;
637     }
638     /* got ')' first */
639     cnt--;
640     pos = cl + 1;
641   }
642   return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
643 }
644
645
646 /**
647  * Remove parentheses surrounding string 'str'.
648  * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
649  * You need to GNUNET_free the returned string.
650  *
651  * @param str string, free'd or re-used by this function, can be NULL
652  *
653  * @return string without surrounding parentheses, string 'str' if no preceding
654  *         epsilon could be found, NULL if 'str' was NULL
655  */
656 static char *
657 remove_parentheses (char *str)
658 {
659   size_t slen;
660   const char *pos;
661
662   if ((NULL == str) || ('(' != str[0]) ||
663       (str[(slen = strlen (str)) - 1] != ')'))
664     return str;
665
666   pos = strchr (&str[1], ')');
667   if (pos == &str[slen - 1])
668   {
669     memmove (str, &str[1], slen - 2);
670     str[slen - 2] = '\0';
671   }
672   return str;
673 }
674
675
676 /**
677  * Check if the string 'str' starts with an epsilon (empty string).
678  * Example: "(|a)" is starting with an epsilon.
679  *
680  * @param str string to test
681  *
682  * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
683  */
684 static int
685 has_epsilon (const char *str)
686 {
687   return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
688       (')' == str[strlen (str) - 1]);
689 }
690
691
692 /**
693  * Remove an epsilon from the string str. Where epsilon is an empty string
694  * Example: str = "(|a|b|c)", result: "a|b|c"
695  * The returned string needs to be freed.
696  *
697  * @param str string
698  *
699  * @return string without preceding epsilon, string 'str' if no preceding epsilon
700  *         could be found, NULL if 'str' was NULL
701  */
702 static char *
703 remove_epsilon (const char *str)
704 {
705   size_t len;
706
707   if (NULL == str)
708     return NULL;
709   if (('(' == str[0]) && ('|' == str[1]))
710   {
711     len = strlen (str);
712     if (')' == str[len - 1])
713       return GNUNET_strndup (&str[2], len - 3);
714   }
715   return GNUNET_strdup (str);
716 }
717
718
719 /**
720  * Compare 'str1', starting from position 'k',  with whole 'str2'
721  *
722  * @param str1 first string to compare, starting from position 'k'
723  * @param str2 second string for comparison
724  * @param k starting position in 'str1'
725  *
726  * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
727  */
728 static int
729 strkcmp (const char *str1, const char *str2, size_t k)
730 {
731   if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
732     return -1;
733   return strcmp (&str1[k], str2);
734 }
735
736
737 /**
738  * Compare two strings for equality. If either is NULL (or if both are
739  * NULL), they are not equal.
740  *
741  * @param str1 first string for comparison.
742  * @param str2 second string for comparison.
743  *
744  * @return  0 if the strings are the same, 1 or -1 if not
745  */
746 static int
747 nullstrcmp (const char *str1, const char *str2)
748 {
749   if ((NULL == str1) || (NULL == str2))
750     return -1;
751   return strcmp (str1, str2);
752 }
753
754
755 /**
756  * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse' function to create
757  * the depth-first numbering of the states.
758  *
759  * @param cls states array.
760  * @param count current state counter.
761  * @param s current state.
762  */
763 static void
764 number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
765 {
766   struct GNUNET_REGEX_State **states = cls;
767
768   s->proof_id = count;
769   states[count] = s;
770 }
771
772
773 /**
774  * Construct the regular expression given the inductive step,
775  * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
776  * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
777  *
778  * @param R_last_ij value of  $R^{(k-1)_{ij}.
779  * @param R_last_ik value of  $R^{(k-1)_{ik}.
780  * @param R_last_kk value of  $R^{(k-1)_{kk}.
781  * @param R_last_kj value of  $R^{(k-1)_{kj}.
782  * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
783  *                 is expected to be NULL when called!
784  */
785 static void
786 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
787                                   char *R_last_kk, char *R_last_kj,
788                                   char **R_cur_ij)
789 {
790   char *R_cur_l;
791   char *R_cur_r;
792   char *temp_a;
793   char *temp_b;
794   char *R_temp_ij;
795   char *R_temp_ik;
796   char *R_temp_kj;
797   char *R_temp_kk;
798
799   int eps_check;
800   int ij_ik_cmp;
801   int ij_kj_cmp;
802
803   int ik_kk_cmp;
804   int kk_kj_cmp;
805   int clean_ik_kk_cmp;
806   int clean_kk_kj_cmp;
807   unsigned int cnt;
808
809   size_t length;
810   size_t length_l;
811   size_t length_r;
812
813   GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
814
815   // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
816   // R_last == R^{(k-1)}, R_cur == R^{(k)}
817   // R_cur_ij = R_cur_l | R_cur_r
818   // R_cur_l == R^{(k-1)}_{ij}
819   // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
820
821   if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) ||     /* technically cannot happen, but looks saner */
822                               (NULL == R_last_kj)))
823   {
824     /* R^{(k)}_{ij} = N | N */
825     *R_cur_ij = NULL;
826     return;
827   }
828
829   if ((NULL == R_last_ik) || (NULL == R_last_kk) ||     /* technically cannot happen, but looks saner */
830       (NULL == R_last_kj))
831   {
832     /*  R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
833     *R_cur_ij = GNUNET_strdup (R_last_ij);
834     return;
835   }
836
837   // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
838   // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
839
840   R_cur_r = NULL;
841   R_cur_l = NULL;
842
843   // cache results from strcmp, we might need these many times
844   ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
845   ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
846   ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
847   kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
848
849   // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
850   // as parentheses, so we can better compare the contents
851   R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
852   R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
853   R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
854
855   clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
856   clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
857
858   // construct R_cur_l (and, if necessary R_cur_r)
859   if (NULL != R_last_ij)
860   {
861     // Assign R_temp_ij to R_last_ij and remove epsilon as well
862     // as parentheses, so we can better compare the contents
863     R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
864
865     if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
866         && 0 == strcmp (R_temp_kk, R_temp_kj))
867     {
868       if (0 == strlen (R_temp_ij))
869       {
870         R_cur_r = GNUNET_strdup ("");
871       }
872       else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
873                (0 == strncmp (R_last_ik, "(|", 2) &&
874                 0 == strncmp (R_last_kj, "(|", 2)))
875       {
876         // a|(e|a)a*(e|a) = a*
877         // a|(e|a)(e|a)*(e|a) = a*
878         // (e|a)|aa*a = a*
879         // (e|a)|aa*(e|a) = a*
880         // (e|a)|(e|a)a*a = a*
881         // (e|a)|(e|a)a*(e|a) = a*
882         // (e|a)|(e|a)(e|a)*(e|a) = a*
883         if (GNUNET_YES == needs_parentheses (R_temp_ij))
884           GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
885         else
886           GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
887       }
888       else
889       {
890         // a|aa*a = a+
891         // a|(e|a)a*a = a+
892         // a|aa*(e|a) = a+
893         // a|(e|a)(e|a)*a = a+
894         // a|a(e|a)*(e|a) = a+
895         if (GNUNET_YES == needs_parentheses (R_temp_ij))
896           GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
897         else
898           GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
899       }
900     }
901     else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
902     {
903       // a|ab*b = ab*
904       if (strlen (R_last_kk) < 1)
905         R_cur_r = GNUNET_strdup (R_last_ij);
906       else if (GNUNET_YES == needs_parentheses (R_temp_kk))
907         GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
908       else
909         GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
910
911       R_cur_l = NULL;
912     }
913     else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
914     {
915       // a|bb*a = b*a
916       if (strlen (R_last_kk) < 1)
917         R_cur_r = GNUNET_strdup (R_last_kj);
918       else if (GNUNET_YES == needs_parentheses (R_temp_kk))
919         GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
920       else
921         GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
922
923       R_cur_l = NULL;
924     }
925     else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
926              has_epsilon (R_last_kk))
927     {
928       // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
929       if (needs_parentheses (R_temp_kk))
930         GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
931       else
932         GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
933
934       R_cur_l = NULL;
935     }
936     else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
937              has_epsilon (R_last_kk))
938     {
939       // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|...  = b*a
940       if (needs_parentheses (R_temp_kk))
941         GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
942       else
943         GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
944
945       R_cur_l = NULL;
946     }
947     else
948     {
949       temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
950       temp_a = remove_parentheses (temp_a);
951       R_cur_l = temp_a;
952     }
953
954     GNUNET_free_non_null (R_temp_ij);
955   }
956   else
957   {
958     // we have no left side
959     R_cur_l = NULL;
960   }
961
962   // construct R_cur_r, if not already constructed
963   if (NULL == R_cur_r)
964   {
965     length = strlen (R_temp_kk) - strlen (R_last_ik);
966
967     // a(ba)*bx = (ab)+x
968     if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
969         NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
970         0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
971         0 == strncmp (R_temp_kk, R_last_kj, length))
972     {
973       temp_a = GNUNET_malloc (length + 1);
974       temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
975
976       length_l = 0;
977       length_r = 0;
978
979       for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
980       {
981         if (cnt < length)
982         {
983           temp_a[length_l] = R_last_kj[cnt];
984           length_l++;
985         }
986         else
987         {
988           temp_b[length_r] = R_last_kj[cnt];
989           length_r++;
990         }
991       }
992       temp_a[length_l] = '\0';
993       temp_b[length_r] = '\0';
994
995       // e|(ab)+ = (ab)*
996       if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
997       {
998         GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
999         GNUNET_free (R_cur_l);
1000         R_cur_l = NULL;
1001       }
1002       else
1003       {
1004         GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
1005       }
1006       GNUNET_free (temp_a);
1007       GNUNET_free (temp_b);
1008     }
1009     else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
1010              0 == strcmp (R_temp_kk, R_temp_kj))
1011     {
1012       // (e|a)a*(e|a) = a*
1013       // (e|a)(e|a)*(e|a) = a*
1014       if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1015       {
1016         if (needs_parentheses (R_temp_kk))
1017           GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
1018         else
1019           GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
1020       }
1021       // aa*a = a+a
1022       else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
1023                !has_epsilon (R_last_ik))
1024       {
1025         if (needs_parentheses (R_temp_kk))
1026           GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1027         else
1028           GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1029       }
1030       // (e|a)a*a = a+
1031       // aa*(e|a) = a+
1032       // a(e|a)*(e|a) = a+
1033       // (e|a)a*a = a+
1034       else
1035       {
1036         eps_check =
1037             (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1038              has_epsilon (R_last_kj));
1039
1040         if (eps_check == 1)
1041         {
1042           if (needs_parentheses (R_temp_kk))
1043             GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
1044           else
1045             GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
1046         }
1047       }
1048     }
1049     // aa*b = a+b
1050     // (e|a)(e|a)*b = a*b
1051     else if (0 == strcmp (R_temp_ik, R_temp_kk))
1052     {
1053       if (has_epsilon (R_last_ik))
1054       {
1055         if (needs_parentheses (R_temp_kk))
1056           GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
1057         else
1058           GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
1059       }
1060       else
1061       {
1062         if (needs_parentheses (R_temp_kk))
1063           GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
1064         else
1065           GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
1066       }
1067     }
1068     // ba*a = ba+
1069     // b(e|a)*(e|a) = ba*
1070     else if (0 == strcmp (R_temp_kk, R_temp_kj))
1071     {
1072       if (has_epsilon (R_last_kj))
1073       {
1074         if (needs_parentheses (R_temp_kk))
1075           GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
1076         else
1077           GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
1078       }
1079       else
1080       {
1081         if (needs_parentheses (R_temp_kk))
1082           GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
1083         else
1084           GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
1085       }
1086     }
1087     else
1088     {
1089       if (strlen (R_temp_kk) > 0)
1090       {
1091         if (needs_parentheses (R_temp_kk))
1092         {
1093           GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1094                            R_last_kj);
1095         }
1096         else
1097         {
1098           GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1099                            R_last_kj);
1100         }
1101       }
1102       else
1103       {
1104         GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1105       }
1106     }
1107   }
1108
1109   GNUNET_free_non_null (R_temp_ik);
1110   GNUNET_free_non_null (R_temp_kk);
1111   GNUNET_free_non_null (R_temp_kj);
1112
1113   if (NULL == R_cur_l && NULL == R_cur_r)
1114   {
1115     *R_cur_ij = NULL;
1116     return;
1117   }
1118
1119   if (NULL != R_cur_l && NULL == R_cur_r)
1120   {
1121     *R_cur_ij = R_cur_l;
1122     return;
1123   }
1124
1125   if (NULL == R_cur_l && NULL != R_cur_r)
1126   {
1127     *R_cur_ij = R_cur_r;
1128     return;
1129   }
1130
1131   if (0 == nullstrcmp (R_cur_l, R_cur_r))
1132   {
1133     *R_cur_ij = R_cur_l;
1134     GNUNET_free (R_cur_r);
1135     return;
1136   }
1137
1138   GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1139
1140   GNUNET_free (R_cur_l);
1141   GNUNET_free (R_cur_r);
1142 }
1143
1144
1145 /**
1146  * create proofs for all states in the given automaton. Implementation of the
1147  * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1148  * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1149  *
1150  * @param a automaton.
1151  */
1152 static void
1153 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1154 {
1155   unsigned int n = a->state_count;
1156   struct GNUNET_REGEX_State *states[n];
1157   char *R_last[n][n];
1158   char *R_cur[n][n];
1159   char *temp;
1160   struct GNUNET_REGEX_Transition *t;
1161   char *complete_regex;
1162   unsigned int i;
1163   unsigned int j;
1164   unsigned int k;
1165
1166
1167   /* create depth-first numbering of the states, initializes 'state' */
1168   GNUNET_REGEX_automaton_traverse (a, &number_states, states);
1169
1170   /* Compute regular expressions of length "1" between each pair of states */
1171   for (i = 0; i < n; i++)
1172   {
1173     for (j = 0; j < n; j++)
1174     {
1175       R_cur[i][j] = NULL;
1176       R_last[i][j] = NULL;
1177     }
1178     for (t = states[i]->transitions_head; NULL != t; t = t->next)
1179     {
1180       j = t->to_state->proof_id;
1181       if (NULL == R_last[i][j])
1182         GNUNET_asprintf (&R_last[i][j], "%c", t->label);
1183       else
1184       {
1185         temp = R_last[i][j];
1186         GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
1187         GNUNET_free (temp);
1188       }
1189     }
1190     if (NULL == R_last[i][i])
1191       GNUNET_asprintf (&R_last[i][i], "");
1192     else
1193     {
1194       temp = R_last[i][i];
1195       GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
1196       GNUNET_free (temp);
1197     }
1198   }
1199   for (i = 0; i < n; i++)
1200     for (j = 0; j < n; j++)
1201       if (needs_parentheses (R_last[i][j]))
1202       {
1203         temp = R_last[i][j];
1204         GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
1205         GNUNET_free (temp);
1206       }
1207
1208   /* Compute regular expressions of length "k" between each pair of states per induction */
1209   for (k = 0; k < n; k++)
1210   {
1211     for (i = 0; i < n; i++)
1212     {
1213       for (j = 0; j < n; j++)
1214       {
1215         // Basis for the recursion:
1216         // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1217         // R_last == R^{(k-1)}, R_cur == R^{(k)}
1218
1219         // Create R_cur[i][j] and simplify the expression
1220         automaton_create_proofs_simplify (R_last[i][j], R_last[i][k],
1221                                           R_last[k][k], R_last[k][j],
1222                                           &R_cur[i][j]);
1223       }
1224     }
1225
1226     // set R_last = R_cur
1227     for (i = 0; i < n; i++)
1228     {
1229       for (j = 0; j < n; j++)
1230       {
1231         GNUNET_free_non_null (R_last[i][j]);
1232         R_last[i][j] = R_cur[i][j];
1233         R_cur[i][j] = NULL;
1234       }
1235     }
1236   }
1237
1238   // assign proofs and hashes
1239   for (i = 0; i < n; i++)
1240   {
1241     if (NULL != R_last[a->start->proof_id][i])
1242     {
1243       states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
1244       GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1245                           &states[i]->hash);
1246     }
1247   }
1248
1249   // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
1250   complete_regex = NULL;
1251   for (i = 0; i < n; i++)
1252   {
1253     if (states[i]->accepting)
1254     {
1255       if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
1256       {
1257         GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
1258       }
1259       else if (NULL != R_last[a->start->proof_id][i] &&
1260                0 < strlen (R_last[a->start->proof_id][i]))
1261       {
1262         temp = complete_regex;
1263         GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1264                          R_last[a->start->proof_id][i]);
1265         GNUNET_free (temp);
1266       }
1267     }
1268   }
1269   a->canonical_regex = complete_regex;
1270
1271   // cleanup
1272   for (i = 0; i < n; i++)
1273   {
1274     for (j = 0; j < n; j++)
1275       GNUNET_free_non_null (R_last[i][j]);
1276   }
1277 }
1278
1279
1280 /**
1281  * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1282  * automaton_destroy_state.
1283  *
1284  * @param ctx context
1285  * @param nfa_states set of NFA states on which the DFA should be based on
1286  *
1287  * @return new DFA state
1288  */
1289 static struct GNUNET_REGEX_State *
1290 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1291                   struct GNUNET_REGEX_StateSet *nfa_states)
1292 {
1293   struct GNUNET_REGEX_State *s;
1294   char *name;
1295   int len = 0;
1296   struct GNUNET_REGEX_State *cstate;
1297   struct GNUNET_REGEX_Transition *ctran;
1298   unsigned int i;
1299
1300   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1301   s->id = ctx->state_id++;
1302   s->accepting = 0;
1303   s->marked = 0;
1304   s->name = NULL;
1305   s->scc_id = 0;
1306   s->index = -1;
1307   s->lowlink = -1;
1308   s->contained = 0;
1309   s->proof = NULL;
1310
1311   if (NULL == nfa_states)
1312   {
1313     GNUNET_asprintf (&s->name, "s%i", s->id);
1314     return s;
1315   }
1316
1317   s->nfa_set = nfa_states;
1318
1319   if (nfa_states->len < 1)
1320     return s;
1321
1322   // Create a name based on 'sset'
1323   s->name = GNUNET_malloc (sizeof (char) * 2);
1324   strcat (s->name, "{");
1325   name = NULL;
1326
1327   for (i = 0; i < nfa_states->len; i++)
1328   {
1329     cstate = nfa_states->states[i];
1330     GNUNET_asprintf (&name, "%i,", cstate->id);
1331
1332     if (NULL != name)
1333     {
1334       len = strlen (s->name) + strlen (name) + 1;
1335       s->name = GNUNET_realloc (s->name, len);
1336       strcat (s->name, name);
1337       GNUNET_free (name);
1338       name = NULL;
1339     }
1340
1341     // Add a transition for each distinct label to NULL state
1342     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1343     {
1344       if (0 != ctran->label)
1345         state_add_transition (ctx, s, ctran->label, NULL);
1346     }
1347
1348     // If the nfa_states contain an accepting state, the new dfa state is also
1349     // accepting
1350     if (cstate->accepting)
1351       s->accepting = 1;
1352   }
1353
1354   s->name[strlen (s->name) - 1] = '}';
1355
1356   return s;
1357 }
1358
1359
1360 /**
1361  * Move from the given state 's' to the next state on transition 'label'
1362  *
1363  * @param s starting state
1364  * @param label edge label to follow
1365  *
1366  * @return new state or NULL, if transition on label not possible
1367  */
1368 static struct GNUNET_REGEX_State *
1369 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1370 {
1371   struct GNUNET_REGEX_Transition *t;
1372   struct GNUNET_REGEX_State *new_s;
1373
1374   if (NULL == s)
1375     return NULL;
1376
1377   new_s = NULL;
1378
1379   for (t = s->transitions_head; NULL != t; t = t->next)
1380   {
1381     if (label == t->label)
1382     {
1383       new_s = t->to_state;
1384       break;
1385     }
1386   }
1387
1388   return new_s;
1389 }
1390
1391
1392 /**
1393  * Remove all unreachable states from DFA 'a'. Unreachable states are those
1394  * states that are not reachable from the starting state.
1395  *
1396  * @param a DFA automaton
1397  */
1398 static void
1399 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1400 {
1401   struct GNUNET_REGEX_State *s;
1402   struct GNUNET_REGEX_State *s_next;
1403
1404   // 1. unmark all states
1405   for (s = a->states_head; NULL != s; s = s->next)
1406     s->marked = GNUNET_NO;
1407
1408   // 2. traverse dfa from start state and mark all visited states
1409   GNUNET_REGEX_automaton_traverse (a, NULL, NULL);
1410
1411   // 3. delete all states that were not visited
1412   for (s = a->states_head; NULL != s; s = s_next)
1413   {
1414     s_next = s->next;
1415     if (GNUNET_NO == s->marked)
1416       automaton_remove_state (a, s);
1417   }
1418 }
1419
1420
1421 /**
1422  * Remove all dead states from the DFA 'a'. Dead states are those states that do
1423  * not transition to any other state but themselves.
1424  *
1425  * @param a DFA automaton
1426  */
1427 static void
1428 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1429 {
1430   struct GNUNET_REGEX_State *s;
1431   struct GNUNET_REGEX_Transition *t;
1432   int dead;
1433
1434   GNUNET_assert (DFA == a->type);
1435
1436   for (s = a->states_head; NULL != s; s = s->next)
1437   {
1438     if (s->accepting)
1439       continue;
1440
1441     dead = 1;
1442     for (t = s->transitions_head; NULL != t; t = t->next)
1443     {
1444       if (NULL != t->to_state && t->to_state != s)
1445       {
1446         dead = 0;
1447         break;
1448       }
1449     }
1450
1451     if (0 == dead)
1452       continue;
1453
1454     // state s is dead, remove it
1455     automaton_remove_state (a, s);
1456   }
1457 }
1458
1459
1460 /**
1461  * Merge all non distinguishable states in the DFA 'a'
1462  *
1463  * @param ctx context
1464  * @param a DFA automaton
1465  */
1466 static void
1467 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1468                                      struct GNUNET_REGEX_Automaton *a)
1469 {
1470   unsigned int i;
1471   int table[a->state_count][a->state_count];
1472   struct GNUNET_REGEX_State *s1;
1473   struct GNUNET_REGEX_State *s2;
1474   struct GNUNET_REGEX_Transition *t1;
1475   struct GNUNET_REGEX_Transition *t2;
1476   struct GNUNET_REGEX_State *s1_next;
1477   struct GNUNET_REGEX_State *s2_next;
1478   int change;
1479   unsigned int num_equal_edges;
1480
1481   for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1482        i++, s1 = s1->next)
1483   {
1484     s1->marked = i;
1485   }
1486
1487   // Mark all pairs of accepting/!accepting states
1488   for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1489   {
1490     for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1491     {
1492       table[s1->marked][s2->marked] = 0;
1493
1494       if ((s1->accepting && !s2->accepting) ||
1495           (!s1->accepting && s2->accepting))
1496       {
1497         table[s1->marked][s2->marked] = 1;
1498       }
1499     }
1500   }
1501
1502   // Find all equal states
1503   change = 1;
1504   while (0 != change)
1505   {
1506     change = 0;
1507     for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1508     {
1509       for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1510       {
1511         if (0 != table[s1->marked][s2->marked])
1512           continue;
1513
1514         num_equal_edges = 0;
1515         for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1516         {
1517           for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1518           {
1519             if (t1->label == t2->label)
1520             {
1521               num_equal_edges++;
1522               if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1523                   0 != table[t2->to_state->marked][t1->to_state->marked])
1524               {
1525                 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1526                 change = 1;
1527               }
1528             }
1529           }
1530         }
1531         if (num_equal_edges != s1->transition_count ||
1532             num_equal_edges != s2->transition_count)
1533         {
1534           // Make sure ALL edges of possible equal states are the same
1535           table[s1->marked][s2->marked] = -2;
1536         }
1537       }
1538     }
1539   }
1540
1541   // Merge states that are equal
1542   for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1543   {
1544     s1_next = s1->next;
1545     for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1546     {
1547       s2_next = s2->next;
1548       if (table[s1->marked][s2->marked] == 0)
1549         automaton_merge_states (ctx, a, s1, s2);
1550     }
1551   }
1552 }
1553
1554
1555 /**
1556  * Minimize the given DFA 'a' by removing all unreachable states, removing all
1557  * dead states and merging all non distinguishable states
1558  *
1559  * @param ctx context
1560  * @param a DFA automaton
1561  */
1562 static void
1563 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1564               struct GNUNET_REGEX_Automaton *a)
1565 {
1566   if (NULL == a)
1567     return;
1568
1569   GNUNET_assert (DFA == a->type);
1570
1571   // 1. remove unreachable states
1572   dfa_remove_unreachable_states (a);
1573
1574   // 2. remove dead states
1575   dfa_remove_dead_states (a);
1576
1577   // 3. Merge nondistinguishable states
1578   dfa_merge_nondistinguishable_states (ctx, a);
1579 }
1580
1581
1582 /**
1583  * Creates a new NFA fragment. Needs to be cleared using
1584  * automaton_fragment_clear.
1585  *
1586  * @param start starting state
1587  * @param end end state
1588  *
1589  * @return new NFA fragment
1590  */
1591 static struct GNUNET_REGEX_Automaton *
1592 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1593                      struct GNUNET_REGEX_State *end)
1594 {
1595   struct GNUNET_REGEX_Automaton *n;
1596
1597   n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1598
1599   n->type = NFA;
1600   n->start = NULL;
1601   n->end = NULL;
1602
1603   if (NULL == start || NULL == end)
1604     return n;
1605
1606   automaton_add_state (n, end);
1607   automaton_add_state (n, start);
1608
1609   n->start = start;
1610   n->end = end;
1611
1612   return n;
1613 }
1614
1615
1616 /**
1617  * Adds a list of states to the given automaton 'n'.
1618  *
1619  * @param n automaton to which the states should be added
1620  * @param states_head head of the DLL of states
1621  * @param states_tail tail of the DLL of states
1622  */
1623 static void
1624 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1625                 struct GNUNET_REGEX_State *states_head,
1626                 struct GNUNET_REGEX_State *states_tail)
1627 {
1628   struct GNUNET_REGEX_State *s;
1629
1630   if (NULL == n || NULL == states_head)
1631   {
1632     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1633     return;
1634   }
1635
1636   if (NULL == n->states_head)
1637   {
1638     n->states_head = states_head;
1639     n->states_tail = states_tail;
1640     return;
1641   }
1642
1643   if (NULL != states_head)
1644   {
1645     n->states_tail->next = states_head;
1646     n->states_tail = states_tail;
1647   }
1648
1649   for (s = states_head; NULL != s; s = s->next)
1650     n->state_count++;
1651 }
1652
1653
1654 /**
1655  * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1656  *
1657  * @param ctx context
1658  * @param accepting is it an accepting state or not
1659  *
1660  * @return new NFA state
1661  */
1662 static struct GNUNET_REGEX_State *
1663 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1664 {
1665   struct GNUNET_REGEX_State *s;
1666
1667   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1668   s->id = ctx->state_id++;
1669   s->accepting = accepting;
1670   s->marked = 0;
1671   s->contained = 0;
1672   s->index = -1;
1673   s->lowlink = -1;
1674   s->scc_id = 0;
1675   s->name = NULL;
1676   GNUNET_asprintf (&s->name, "s%i", s->id);
1677
1678   return s;
1679 }
1680
1681
1682 /**
1683  * Calculates the NFA closure set for the given state.
1684  *
1685  * @param nfa the NFA containing 's'
1686  * @param s starting point state
1687  * @param label transitioning label on which to base the closure on,
1688  *                pass 0 for epsilon transition
1689  *
1690  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1691  */
1692 static struct GNUNET_REGEX_StateSet *
1693 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1694                     struct GNUNET_REGEX_State *s, const char label)
1695 {
1696   struct GNUNET_REGEX_StateSet *cls;
1697   struct GNUNET_REGEX_StateSet *cls_check;
1698   struct GNUNET_REGEX_State *clsstate;
1699   struct GNUNET_REGEX_State *currentstate;
1700   struct GNUNET_REGEX_Transition *ctran;
1701
1702   if (NULL == s)
1703     return NULL;
1704
1705   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1706   cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1707
1708   for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1709     clsstate->contained = 0;
1710
1711   // Add start state to closure only for epsilon closure
1712   if (0 == label)
1713     GNUNET_array_append (cls->states, cls->len, s);
1714
1715   GNUNET_array_append (cls_check->states, cls_check->len, s);
1716   while (cls_check->len > 0)
1717   {
1718     currentstate = cls_check->states[cls_check->len - 1];
1719     GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1720
1721     for (ctran = currentstate->transitions_head; NULL != ctran;
1722          ctran = ctran->next)
1723     {
1724       if (NULL != ctran->to_state && label == ctran->label)
1725       {
1726         clsstate = ctran->to_state;
1727
1728         if (NULL != clsstate && 0 == clsstate->contained)
1729         {
1730           GNUNET_array_append (cls->states, cls->len, clsstate);
1731           GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1732           clsstate->contained = 1;
1733         }
1734       }
1735     }
1736   }
1737   GNUNET_assert (0 == cls_check->len);
1738   GNUNET_free (cls_check);
1739
1740   // sort the states
1741   if (cls->len > 1)
1742     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1743            state_compare);
1744
1745   return cls;
1746 }
1747
1748
1749 /**
1750  * Calculates the closure set for the given set of states.
1751  *
1752  * @param nfa the NFA containing 's'
1753  * @param states list of states on which to base the closure on
1754  * @param label transitioning label for which to base the closure on,
1755  *                pass 0 for epsilon transition
1756  *
1757  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1758  */
1759 static struct GNUNET_REGEX_StateSet *
1760 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1761                         struct GNUNET_REGEX_StateSet *states, const char label)
1762 {
1763   struct GNUNET_REGEX_State *s;
1764   struct GNUNET_REGEX_StateSet *sset;
1765   struct GNUNET_REGEX_StateSet *cls;
1766   unsigned int i;
1767   unsigned int j;
1768   unsigned int k;
1769   unsigned int contains;
1770
1771   if (NULL == states)
1772     return NULL;
1773
1774   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1775
1776   for (i = 0; i < states->len; i++)
1777   {
1778     s = states->states[i];
1779     sset = nfa_closure_create (nfa, s, label);
1780
1781     for (j = 0; j < sset->len; j++)
1782     {
1783       contains = 0;
1784       for (k = 0; k < cls->len; k++)
1785       {
1786         if (sset->states[j]->id == cls->states[k]->id)
1787         {
1788           contains = 1;
1789           break;
1790         }
1791       }
1792       if (!contains)
1793         GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1794     }
1795     state_set_clear (sset);
1796   }
1797
1798   if (cls->len > 1)
1799     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1800            state_compare);
1801
1802   return cls;
1803 }
1804
1805
1806 /**
1807  * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1808  *
1809  * @param ctx context
1810  */
1811 static void
1812 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1813 {
1814   struct GNUNET_REGEX_Automaton *a;
1815   struct GNUNET_REGEX_Automaton *b;
1816   struct GNUNET_REGEX_Automaton *new_nfa;
1817
1818   b = ctx->stack_tail;
1819   GNUNET_assert (NULL != b);
1820   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1821   a = ctx->stack_tail;
1822   GNUNET_assert (NULL != a);
1823   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1824
1825   state_add_transition (ctx, a->end, 0, b->start);
1826   a->end->accepting = 0;
1827   b->end->accepting = 1;
1828
1829   new_nfa = nfa_fragment_create (NULL, NULL);
1830   nfa_add_states (new_nfa, a->states_head, a->states_tail);
1831   nfa_add_states (new_nfa, b->states_head, b->states_tail);
1832   new_nfa->start = a->start;
1833   new_nfa->end = b->end;
1834   automaton_fragment_clear (a);
1835   automaton_fragment_clear (b);
1836
1837   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1838 }
1839
1840
1841 /**
1842  * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1843  *
1844  * @param ctx context
1845  */
1846 static void
1847 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1848 {
1849   struct GNUNET_REGEX_Automaton *a;
1850   struct GNUNET_REGEX_Automaton *new_nfa;
1851   struct GNUNET_REGEX_State *start;
1852   struct GNUNET_REGEX_State *end;
1853
1854   a = ctx->stack_tail;
1855
1856   if (NULL == a)
1857   {
1858     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1859                 "nfa_add_star_op failed, because there was no element on the stack");
1860     return;
1861   }
1862
1863   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1864
1865   start = nfa_state_create (ctx, 0);
1866   end = nfa_state_create (ctx, 1);
1867
1868   state_add_transition (ctx, start, 0, a->start);
1869   state_add_transition (ctx, start, 0, end);
1870   state_add_transition (ctx, a->end, 0, a->start);
1871   state_add_transition (ctx, a->end, 0, end);
1872
1873   a->end->accepting = 0;
1874   end->accepting = 1;
1875
1876   new_nfa = nfa_fragment_create (start, end);
1877   nfa_add_states (new_nfa, a->states_head, a->states_tail);
1878   automaton_fragment_clear (a);
1879
1880   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1881 }
1882
1883
1884 /**
1885  * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1886  *
1887  * @param ctx context
1888  */
1889 static void
1890 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1891 {
1892   struct GNUNET_REGEX_Automaton *a;
1893
1894   a = ctx->stack_tail;
1895   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1896
1897   state_add_transition (ctx, a->end, 0, a->start);
1898
1899   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1900 }
1901
1902
1903 /**
1904  * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1905  *
1906  * @param ctx context
1907  */
1908 static void
1909 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1910 {
1911   struct GNUNET_REGEX_Automaton *a;
1912   struct GNUNET_REGEX_Automaton *new_nfa;
1913   struct GNUNET_REGEX_State *start;
1914   struct GNUNET_REGEX_State *end;
1915
1916   a = ctx->stack_tail;
1917   
1918   if (NULL == a)
1919   {
1920     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1921                 "nfa_add_question_op failed, because there was no element on the stack");
1922     return;
1923   }
1924
1925   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1926
1927   start = nfa_state_create (ctx, 0);
1928   end = nfa_state_create (ctx, 1);
1929
1930   state_add_transition (ctx, start, 0, a->start);
1931   state_add_transition (ctx, start, 0, end);
1932   state_add_transition (ctx, a->end, 0, end);
1933
1934   a->end->accepting = 0;
1935
1936   new_nfa = nfa_fragment_create (start, end);
1937   nfa_add_states (new_nfa, a->states_head, a->states_tail);
1938   automaton_fragment_clear (a);
1939
1940   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1941 }
1942
1943
1944 /**
1945  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1946  * alternates between a and b (a|b)
1947  *
1948  * @param ctx context
1949  */
1950 static void
1951 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1952 {
1953   struct GNUNET_REGEX_Automaton *a;
1954   struct GNUNET_REGEX_Automaton *b;
1955   struct GNUNET_REGEX_Automaton *new_nfa;
1956   struct GNUNET_REGEX_State *start;
1957   struct GNUNET_REGEX_State *end;
1958
1959   b = ctx->stack_tail;
1960   GNUNET_assert (NULL != b);
1961   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1962   a = ctx->stack_tail;
1963   GNUNET_assert (NULL != a);
1964   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1965
1966   start = nfa_state_create (ctx, 0);
1967   end = nfa_state_create (ctx, 1);
1968   state_add_transition (ctx, start, 0, a->start);
1969   state_add_transition (ctx, start, 0, b->start);
1970
1971   state_add_transition (ctx, a->end, 0, end);
1972   state_add_transition (ctx, b->end, 0, end);
1973
1974   a->end->accepting = 0;
1975   b->end->accepting = 0;
1976   end->accepting = 1;
1977
1978   new_nfa = nfa_fragment_create (start, end);
1979   nfa_add_states (new_nfa, a->states_head, a->states_tail);
1980   nfa_add_states (new_nfa, b->states_head, b->states_tail);
1981   automaton_fragment_clear (a);
1982   automaton_fragment_clear (b);
1983
1984   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1985 }
1986
1987
1988 /**
1989  * Adds a new nfa fragment to the stack
1990  *
1991  * @param ctx context
1992  * @param lit label for nfa transition
1993  */
1994 static void
1995 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1996 {
1997   struct GNUNET_REGEX_Automaton *n;
1998   struct GNUNET_REGEX_State *start;
1999   struct GNUNET_REGEX_State *end;
2000
2001   GNUNET_assert (NULL != ctx);
2002
2003   start = nfa_state_create (ctx, 0);
2004   end = nfa_state_create (ctx, 1);
2005   state_add_transition (ctx, start, lit, end);
2006   n = nfa_fragment_create (start, end);
2007   GNUNET_assert (NULL != n);
2008   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2009 }
2010
2011
2012 /**
2013  * Initialize a new context
2014  *
2015  * @param ctx context
2016  */
2017 static void
2018 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2019 {
2020   if (NULL == ctx)
2021   {
2022     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2023     return;
2024   }
2025   ctx->state_id = 0;
2026   ctx->transition_id = 0;
2027   ctx->stack_head = NULL;
2028   ctx->stack_tail = NULL;
2029 }
2030
2031
2032 /**
2033  * Construct an NFA by parsing the regex string of length 'len'.
2034  *
2035  * @param regex regular expression string
2036  * @param len length of the string
2037  *
2038  * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2039  */
2040 struct GNUNET_REGEX_Automaton *
2041 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2042 {
2043   struct GNUNET_REGEX_Context ctx;
2044   struct GNUNET_REGEX_Automaton *nfa;
2045   const char *regexp;
2046   char *error_msg;
2047   unsigned int count;
2048   unsigned int altcount;
2049   unsigned int atomcount;
2050   unsigned int pcount;
2051   struct
2052   {
2053     int altcount;
2054     int atomcount;
2055   }     *p;
2056
2057   GNUNET_REGEX_context_init (&ctx);
2058
2059   regexp = regex;
2060   p = NULL;
2061   error_msg = NULL;
2062   altcount = 0;
2063   atomcount = 0;
2064   pcount = 0;
2065
2066   for (count = 0; count < len && *regexp; count++, regexp++)
2067   {
2068     switch (*regexp)
2069     {
2070     case '(':
2071       if (atomcount > 1)
2072       {
2073         --atomcount;
2074         nfa_add_concatenation (&ctx);
2075       }
2076       GNUNET_array_grow (p, pcount, pcount + 1);
2077       p[pcount - 1].altcount = altcount;
2078       p[pcount - 1].atomcount = atomcount;
2079       altcount = 0;
2080       atomcount = 0;
2081       break;
2082     case '|':
2083       if (0 == atomcount)
2084       {
2085         error_msg = "Cannot append '|' to nothing";
2086         goto error;
2087       }
2088       while (--atomcount > 0)
2089         nfa_add_concatenation (&ctx);
2090       altcount++;
2091       break;
2092     case ')':
2093       if (0 == pcount)
2094       {
2095         error_msg = "Missing opening '('";
2096         goto error;
2097       }
2098       if (0 == atomcount)
2099       {
2100         // Ignore this: "()"
2101         pcount--;
2102         altcount = p[pcount].altcount;
2103         atomcount = p[pcount].atomcount;
2104         break;
2105       }
2106       while (--atomcount > 0)
2107         nfa_add_concatenation (&ctx);
2108       for (; altcount > 0; altcount--)
2109         nfa_add_alternation (&ctx);
2110       pcount--;
2111       altcount = p[pcount].altcount;
2112       atomcount = p[pcount].atomcount;
2113       atomcount++;
2114       break;
2115     case '*':
2116       if (atomcount == 0)
2117       {
2118         error_msg = "Cannot append '*' to nothing";
2119         goto error;
2120       }
2121       nfa_add_star_op (&ctx);
2122       break;
2123     case '+':
2124       if (atomcount == 0)
2125       {
2126         error_msg = "Cannot append '+' to nothing";
2127         goto error;
2128       }
2129       nfa_add_plus_op (&ctx);
2130       break;
2131     case '?':
2132       if (atomcount == 0)
2133       {
2134         error_msg = "Cannot append '?' to nothing";
2135         goto error;
2136       }
2137       nfa_add_question_op (&ctx);
2138       break;
2139     case 92:                   /* escape: \ */
2140       regexp++;
2141       count++;
2142       /* fall through! */
2143     default:
2144       if (atomcount > 1)
2145       {
2146         --atomcount;
2147         nfa_add_concatenation (&ctx);
2148       }
2149       nfa_add_label (&ctx, *regexp);
2150       atomcount++;
2151       break;
2152     }
2153   }
2154   if (0 != pcount)
2155   {
2156     error_msg = "Unbalanced parenthesis";
2157     goto error;
2158   }
2159   while (--atomcount > 0)
2160     nfa_add_concatenation (&ctx);
2161   for (; altcount > 0; altcount--)
2162     nfa_add_alternation (&ctx);
2163
2164   GNUNET_free_non_null (p);
2165
2166   nfa = ctx.stack_tail;
2167   GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2168
2169   if (NULL != ctx.stack_head)
2170   {
2171     error_msg = "Creating the NFA failed. NFA stack was not empty!";
2172     goto error;
2173   }
2174
2175   nfa->regex = GNUNET_strdup (regex);
2176
2177   return nfa;
2178
2179 error:
2180   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2181   if (NULL != error_msg)
2182     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2183
2184   GNUNET_free_non_null (p);
2185
2186   while (NULL != (nfa = ctx.stack_head))
2187   {
2188     GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2189     GNUNET_REGEX_automaton_destroy (nfa);
2190   }
2191
2192   return NULL;
2193 }
2194
2195
2196 /**
2197  * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2198  *
2199  * @param ctx context.
2200  * @param nfa NFA automaton.
2201  * @param dfa DFA automaton.
2202  * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2203  *                  for starting.
2204  */
2205 static void
2206 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2207                       struct GNUNET_REGEX_Automaton *nfa,
2208                       struct GNUNET_REGEX_Automaton *dfa,
2209                       struct GNUNET_REGEX_State *dfa_state)
2210 {
2211   struct GNUNET_REGEX_Transition *ctran;
2212   struct GNUNET_REGEX_State *state_iter;
2213   struct GNUNET_REGEX_State *new_dfa_state;
2214   struct GNUNET_REGEX_State *state_contains;
2215   struct GNUNET_REGEX_StateSet *tmp;
2216   struct GNUNET_REGEX_StateSet *nfa_set;
2217
2218   for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2219   {
2220     if (0 == ctran->label || NULL != ctran->to_state)
2221       continue;
2222
2223     tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2224     nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2225     state_set_clear (tmp);
2226     new_dfa_state = dfa_state_create (ctx, nfa_set);
2227     state_contains = NULL;
2228     for (state_iter = dfa->states_head; NULL != state_iter;
2229          state_iter = state_iter->next)
2230     {
2231       if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2232         state_contains = state_iter;
2233     }
2234
2235     if (NULL == state_contains)
2236     {
2237       automaton_add_state (dfa, new_dfa_state);
2238       ctran->to_state = new_dfa_state;
2239       construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2240     }
2241     else
2242     {
2243       ctran->to_state = state_contains;
2244       automaton_destroy_state (new_dfa_state);
2245     }
2246   }
2247 }
2248
2249
2250 /**
2251  * Construct DFA for the given 'regex' of length 'len'
2252  *
2253  * @param regex regular expression string
2254  * @param len length of the regular expression
2255  *
2256  * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2257  */
2258 struct GNUNET_REGEX_Automaton *
2259 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2260 {
2261   struct GNUNET_REGEX_Context ctx;
2262   struct GNUNET_REGEX_Automaton *dfa;
2263   struct GNUNET_REGEX_Automaton *nfa;
2264   struct GNUNET_REGEX_StateSet *nfa_set;
2265
2266   GNUNET_REGEX_context_init (&ctx);
2267
2268   // Create NFA
2269   nfa = GNUNET_REGEX_construct_nfa (regex, len);
2270
2271   if (NULL == nfa)
2272   {
2273     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2274                 "Could not create DFA, because NFA creation failed\n");
2275     return NULL;
2276   }
2277
2278   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2279   dfa->type = DFA;
2280   dfa->regex = GNUNET_strdup (regex);
2281
2282   // Create DFA start state from epsilon closure
2283   nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2284   dfa->start = dfa_state_create (&ctx, nfa_set);
2285   automaton_add_state (dfa, dfa->start);
2286
2287   construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2288
2289   GNUNET_REGEX_automaton_destroy (nfa);
2290
2291   // Minimize DFA
2292   dfa_minimize (&ctx, dfa);
2293
2294   // Create proofs for all states
2295   automaton_create_proofs (dfa);
2296
2297   return dfa;
2298 }
2299
2300
2301 /**
2302  * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2303  * structure.
2304  *
2305  * @param a automaton to be destroyed
2306  */
2307 void
2308 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2309 {
2310   struct GNUNET_REGEX_State *s;
2311   struct GNUNET_REGEX_State *next_state;
2312
2313   if (NULL == a)
2314     return;
2315
2316   GNUNET_free_non_null (a->regex);
2317   GNUNET_free_non_null (a->canonical_regex);
2318
2319   for (s = a->states_head; NULL != s;)
2320   {
2321     next_state = s->next;
2322     automaton_destroy_state (s);
2323     s = next_state;
2324   }
2325
2326   GNUNET_free (a);
2327 }
2328
2329
2330 /**
2331  * Evaluates the given string using the given DFA automaton
2332  *
2333  * @param a automaton, type must be DFA
2334  * @param string string that should be evaluated
2335  *
2336  * @return 0 if string matches, non 0 otherwise
2337  */
2338 static int
2339 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2340 {
2341   const char *strp;
2342   struct GNUNET_REGEX_State *s;
2343
2344   if (DFA != a->type)
2345   {
2346     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2347                 "Tried to evaluate DFA, but NFA automaton given");
2348     return -1;
2349   }
2350
2351   s = a->start;
2352
2353   // If the string is empty but the starting state is accepting, we accept.
2354   if ((NULL == string || 0 == strlen (string)) && s->accepting)
2355     return 0;
2356
2357   for (strp = string; NULL != strp && *strp; strp++)
2358   {
2359     s = dfa_move (s, *strp);
2360     if (NULL == s)
2361       break;
2362   }
2363
2364   if (NULL != s && s->accepting)
2365     return 0;
2366
2367   return 1;
2368 }
2369
2370
2371 /**
2372  * Evaluates the given string using the given NFA automaton
2373  *
2374  * @param a automaton, type must be NFA
2375  * @param string string that should be evaluated
2376  *
2377  * @return 0 if string matches, non 0 otherwise
2378  */
2379 static int
2380 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2381 {
2382   const char *strp;
2383   struct GNUNET_REGEX_State *s;
2384   struct GNUNET_REGEX_StateSet *sset;
2385   struct GNUNET_REGEX_StateSet *new_sset;
2386   unsigned int i;
2387   int result;
2388
2389   if (NFA != a->type)
2390   {
2391     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2392                 "Tried to evaluate NFA, but DFA automaton given");
2393     return -1;
2394   }
2395
2396   // If the string is empty but the starting state is accepting, we accept.
2397   if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2398     return 0;
2399
2400   result = 1;
2401   sset = nfa_closure_create (a, a->start, 0);
2402
2403   for (strp = string; NULL != strp && *strp; strp++)
2404   {
2405     new_sset = nfa_closure_set_create (a, sset, *strp);
2406     state_set_clear (sset);
2407     sset = nfa_closure_set_create (a, new_sset, 0);
2408     state_set_clear (new_sset);
2409   }
2410
2411   for (i = 0; i < sset->len; i++)
2412   {
2413     s = sset->states[i];
2414     if (NULL != s && s->accepting)
2415     {
2416       result = 0;
2417       break;
2418     }
2419   }
2420
2421   state_set_clear (sset);
2422   return result;
2423 }
2424
2425
2426 /**
2427  * Evaluates the given 'string' against the given compiled regex
2428  *
2429  * @param a automaton
2430  * @param string string to check
2431  *
2432  * @return 0 if string matches, non 0 otherwise
2433  */
2434 int
2435 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2436 {
2437   int result;
2438
2439   switch (a->type)
2440   {
2441   case DFA:
2442     result = evaluate_dfa (a, string);
2443     break;
2444   case NFA:
2445     result = evaluate_nfa (a, string);
2446     break;
2447   default:
2448     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2449                 "Evaluating regex failed, automaton has no type!\n");
2450     result = GNUNET_SYSERR;
2451     break;
2452   }
2453
2454   return result;
2455 }
2456
2457
2458 /**
2459  * Get the canonical regex of the given automaton.
2460  * When constructing the automaton a proof is computed for each state,
2461  * consisting of the regular expression leading to this state. A complete
2462  * regex for the automaton can be computed by combining these proofs.
2463  * As of now this function is only useful for testing.
2464  *
2465  * @param a automaton for which the canonical regex should be returned.
2466  *
2467  * @return
2468  */
2469 const char *
2470 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2471 {
2472   if (NULL == a)
2473     return NULL;
2474
2475   return a->canonical_regex;
2476 }
2477
2478
2479 /**
2480  * Get the first key for the given 'input_string'. This hashes the first x bits
2481  * of the 'input_string'.
2482  *
2483  * @param input_string string.
2484  * @param string_len length of the 'input_string'.
2485  * @param key pointer to where to write the hash code.
2486  *
2487  * @return number of bits of 'input_string' that have been consumed
2488  *         to construct the key
2489  */
2490 size_t
2491 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2492                             struct GNUNET_HashCode * key)
2493 {
2494   unsigned int size;
2495
2496   size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
2497
2498   if (NULL == input_string)
2499   {
2500     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2501     return 0;
2502   }
2503
2504   GNUNET_CRYPTO_hash (input_string, size, key);
2505
2506   return size;
2507 }
2508
2509
2510 /**
2511  * Check if the given 'proof' matches the given 'key'.
2512  *
2513  * @param proof partial regex of a state.
2514  * @param key hash of a state.
2515  *
2516  * @return GNUNET_OK if the proof is valid for the given key.
2517  */
2518 int
2519 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2520 {
2521   struct GNUNET_HashCode key_check;
2522
2523   GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2524   return (0 ==
2525           GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2526 }
2527
2528
2529 /**
2530  * Recursive helper function for iterate_initial_edges. Will call iterator
2531  * function for each initial state.
2532  *
2533  * @param min_len minimum length of the path in the graph.
2534  * @param max_len maximum length of the path in the graph.
2535  * @param cur_len current length of the path already traversed.
2536  * @param consumed_string string consumed by traversing the graph till this state.
2537  * @param state current state of the automaton.
2538  * @param iterator iterator function called for each edge.
2539  * @param iterator_cls closure for the iterator function.
2540  */
2541 static void
2542 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2543                       unsigned int cur_len, char *consumed_string,
2544                       struct GNUNET_REGEX_State *state,
2545                       GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2546 {
2547   unsigned int i;
2548   char label[state->transition_count][2];
2549   char *temp;
2550   struct GNUNET_REGEX_Transition *t;
2551   unsigned int num_edges = state->transition_count;
2552   struct GNUNET_REGEX_Edge edges[num_edges];
2553   struct GNUNET_HashCode hash;
2554
2555   if (cur_len > min_len && NULL != consumed_string && cur_len <= max_len)
2556   {
2557     for (i = 0, t = state->transitions_head; NULL != t; t = t->next, i++)
2558     {
2559       label[i][0] = t->label;
2560       label[i][1] = '\0';
2561       edges[i].label = label[i];
2562       edges[i].destination = t->to_state->hash;
2563     }
2564
2565     GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2566     iterator (iterator_cls, &hash, consumed_string, state->accepting, num_edges,
2567               edges);
2568   }
2569
2570   if (cur_len < max_len)
2571   {
2572     cur_len++;
2573     for (t = state->transitions_head; NULL != t; t = t->next)
2574     {
2575       if (NULL != consumed_string)
2576         GNUNET_asprintf (&temp, "%s%c", consumed_string, t->label);
2577       else
2578         GNUNET_asprintf (&temp, "%c", t->label);
2579
2580       iterate_initial_edge (min_len, max_len, cur_len, temp, t->to_state,
2581                             iterator, iterator_cls);
2582       GNUNET_free (temp);
2583     }
2584   }
2585 }
2586
2587
2588 /**
2589  * Iterate over all initial edges that aren't actually part of the automaton.
2590  * This is needed to find the initial states returned by
2591  * GNUNET_REGEX_get_first_key. Iteration will start at the first branch state (a
2592  * state that has more than one outgoing edge, can be the first state), because
2593  * all previous states will have the same proof and be iterated over in
2594  * iterate_all_edges.
2595  *
2596  * @param a the automaton for which the initial states should be computed.
2597  * @param initial_len length of the initial state string.
2598  * @param iterator iterator function called for each edge.
2599  * @param iterator_cls closure for the iterator function.
2600  */
2601 void
2602 iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
2603                        const unsigned int initial_len,
2604                        GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2605 {
2606   char *consumed_string;
2607   char *temp;
2608   struct GNUNET_REGEX_State *s;
2609   unsigned int cur_len;
2610
2611   if (1 > initial_len)
2612     return;
2613
2614   consumed_string = NULL;
2615   s = a->start;
2616   cur_len = 0;
2617
2618   if (1 == s->transition_count)
2619   {
2620     do
2621     {
2622       if (NULL != consumed_string)
2623       {
2624         temp = consumed_string;
2625         GNUNET_asprintf (&consumed_string, "%s%c", consumed_string,
2626                          s->transitions_head->label);
2627         GNUNET_free (temp);
2628       }
2629       else
2630         GNUNET_asprintf (&consumed_string, "%c", s->transitions_head->label);
2631
2632       s = s->transitions_head->to_state;
2633       cur_len++;
2634     }
2635     while (cur_len < initial_len && 1 == s->transition_count);
2636   }
2637
2638   iterate_initial_edge (cur_len, initial_len, cur_len, consumed_string, s,
2639                         iterator, iterator_cls);
2640
2641   GNUNET_free_non_null (consumed_string);
2642 }
2643
2644
2645 /**
2646  * Iterate over all edges helper function starting from state 's', calling
2647  * iterator on for each edge.
2648  *
2649  * @param s state.
2650  * @param iterator iterator function called for each edge.
2651  * @param iterator_cls closure.
2652  */
2653 static void
2654 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2655               void *iterator_cls)
2656 {
2657   struct GNUNET_REGEX_Transition *t;
2658   struct GNUNET_REGEX_Edge edges[s->transition_count];
2659   unsigned int num_edges;
2660
2661   if (GNUNET_YES != s->marked)
2662   {
2663     s->marked = GNUNET_YES;
2664
2665     num_edges = state_get_edges (s, edges);
2666
2667     if (0 < strlen (s->proof) || s->accepting)
2668       iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
2669                 edges);
2670
2671     for (t = s->transitions_head; NULL != t; t = t->next)
2672       iterate_edge (t->to_state, iterator, iterator_cls);
2673   }
2674 }
2675
2676
2677 /**
2678  * Iterate over all edges starting from start state of automaton 'a'. Calling
2679  * iterator for each edge.
2680  *
2681  * @param a automaton.
2682  * @param iterator iterator called for each edge.
2683  * @param iterator_cls closure.
2684  */
2685 void
2686 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2687                                 GNUNET_REGEX_KeyIterator iterator,
2688                                 void *iterator_cls)
2689 {
2690   struct GNUNET_REGEX_State *s;
2691
2692   for (s = a->states_head; NULL != s; s = s->next)
2693     s->marked = GNUNET_NO;
2694
2695   iterate_initial_edges (a, INITIAL_BITS, iterator, iterator_cls);
2696   iterate_edge (a->start, iterator, iterator_cls);
2697 }