e9b262f9594c605960d795b9aa1c8720a31afb4e
[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.h"
30
31 #define initial_bits 10
32
33 /**
34  * Context that contains an id counter for states and transitions
35  * as well as a DLL of automatons used as a stack for NFA construction.
36  */
37 struct GNUNET_REGEX_Context
38 {
39   /**
40    * Unique state id.
41    */
42   unsigned int state_id;
43
44   /**
45    * Unique transition id.
46    */
47   unsigned int transition_id;
48
49   /**
50    * Unique SCC (Strongly Connected Component) id.
51    */
52   unsigned int scc_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  * Type of an automaton.
67  */
68 enum GNUNET_REGEX_automaton_type
69 {
70   NFA,
71   DFA
72 };
73
74 /**
75  * Automaton representation.
76  */
77 struct GNUNET_REGEX_Automaton
78 {
79   /**
80    * This is a linked list.
81    */
82   struct GNUNET_REGEX_Automaton *prev;
83
84   /**
85    * This is a linked list.
86    */
87   struct GNUNET_REGEX_Automaton *next;
88
89   /**
90    * First state of the automaton. This is mainly
91    * used for constructing an NFA, where each NFA
92    * itself consists of one or more NFAs linked
93    * together.
94    */
95   struct GNUNET_REGEX_State *start;
96
97   /**
98    * End state of the automaton.
99    */
100   struct GNUNET_REGEX_State *end;
101
102   /**
103    * Number of states in the automaton.
104    */
105   unsigned int state_count;
106
107   /**
108    * DLL of states.
109    */
110   struct GNUNET_REGEX_State *states_head;
111
112   /**
113    * DLL of states
114    */
115   struct GNUNET_REGEX_State *states_tail;
116
117   /**
118    * Type of the automaton.
119    */
120   enum GNUNET_REGEX_automaton_type type;
121 };
122
123 /**
124  * A state. Can be used in DFA and NFA automatons.
125  */
126 struct GNUNET_REGEX_State
127 {
128   /**
129    * This is a linked list.
130    */
131   struct GNUNET_REGEX_State *prev;
132
133   /**
134    * This is a linked list.
135    */
136   struct GNUNET_REGEX_State *next;
137
138   /**
139    * Unique state id.
140    */
141   unsigned int id;
142
143   /**
144    * If this is an accepting state or not.
145    */
146   int accepting;
147
148   /**
149    * Marking of the state. This is used for marking all visited
150    * states when traversing all states of an automaton and for
151    * cases where the state id cannot be used (dfa minimization).
152    */
153   int marked;
154
155   /**
156    * Marking the state as contained. This is used for checking,
157    * if the state is contained in a set in constant time
158    */
159   int contained;
160
161   /**
162    * Marking the state as part of an SCC (Strongly Connected Component).
163    * All states with the same scc_id are part of the same SCC.
164    */
165   unsigned int scc_id;
166
167   /**
168    * Used for SCC detection.
169    */
170   int index;
171
172   /**
173    * Used for SCC detection.
174    */
175   int lowlink;
176
177   /**
178    * Human readable name of the automaton. Used for debugging
179    * and graph creation.
180    */
181   char *name;
182
183   GNUNET_HashCode hash;
184
185   /**
186    * Number of transitions from this state to other states.
187    */
188   unsigned int transition_count;
189
190   /**
191    * DLL of transitions.
192    */
193   struct Transition *transitions_head;
194
195   /**
196    * DLL of transitions.
197    */
198   struct Transition *transitions_tail;
199
200   /**
201    * Set of states on which this state is based on. Used when
202    * creating a DFA out of several NFA states.
203    */
204   struct GNUNET_REGEX_StateSet *nfa_set;
205 };
206
207 /**
208  * Transition between two states. Each state can have 0-n transitions.
209  * If label is 0, this is considered to be an epsilon transition.
210  */
211 struct Transition
212 {
213   /**
214    * This is a linked list.
215    */
216   struct Transition *prev;
217
218   /**
219    * This is a linked list.
220    */
221   struct Transition *next;
222
223   /**
224    * Unique id of this transition.
225    */
226   unsigned int id;
227
228   /**
229    * label for this transition. This is basically the edge label for
230    * the graph.
231    */
232   char label;
233
234   /**
235    * State to which this transition leads.
236    */
237   struct GNUNET_REGEX_State *to_state;
238
239   /**
240    * State from which this transition origins.
241    */
242   struct GNUNET_REGEX_State *from_state;
243 };
244
245 /**
246  * Set of states.
247  */
248 struct GNUNET_REGEX_StateSet
249 {
250   /**
251    * Array of states.
252    */
253   struct GNUNET_REGEX_State **states;
254
255   /**
256    * Length of the 'states' array.
257    */
258   unsigned int len;
259 };
260
261 static void
262 debug_print_state (struct GNUNET_REGEX_State *s)
263 {
264   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265               "State %i: %s marked: %i accepting: %i scc_id: %i\n", s->id,
266               s->name, s->marked, s->accepting, s->scc_id);
267 }
268
269 static void
270 debug_print_states (struct GNUNET_REGEX_StateSet *sset)
271 {
272   struct GNUNET_REGEX_State *s;
273   int i;
274
275   for (i = 0; i < sset->len; i++)
276   {
277     s = sset->states[i];
278     debug_print_state (s);
279   }
280 }
281
282 static void
283 debug_print_transitions (struct GNUNET_REGEX_State *s)
284 {
285   struct Transition *t;
286   char *state;
287   char label;
288
289   for (t = s->transitions_head; NULL != t; t = t->next)
290   {
291     if (0 == t->label)
292       label = '0';
293     else
294       label = t->label;
295
296     if (NULL == t->to_state)
297       state = "NULL";
298     else
299       state = t->to_state->name;
300
301     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
302                 label, state);
303   }
304 }
305
306 /**
307  * Recursive function doing DFS with 'v' as a start, detecting all SCCs
308  * inside the subgraph reachable from 'v'. Used with scc_tarjan function
309  * to detect all SCCs inside an automaton.
310  *
311  * @param ctx context
312  * @param v start vertex
313  * @param index current index
314  * @param stack stack for saving all SCCs
315  * @param stack_size current size of the stack
316  */
317 static void
318 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
319                           struct GNUNET_REGEX_State *v, int *index,
320                           struct GNUNET_REGEX_State **stack,
321                           unsigned int *stack_size)
322 {
323   struct GNUNET_REGEX_State *w;
324   struct Transition *t;
325
326   v->index = *index;
327   v->lowlink = *index;
328   (*index)++;
329   stack[(*stack_size)++] = v;
330   v->contained = 1;
331
332   for (t = v->transitions_head; NULL != t; t = t->next)
333   {
334     w = t->to_state;
335     if (NULL != w && w->index < 0)
336     {
337       scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
338       v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
339     }
340     else if (0 != w->contained)
341       v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
342   }
343
344   if (v->lowlink == v->index)
345   {
346     w = stack[--(*stack_size)];
347     w->contained = 0;
348
349     if (v != w)
350     {
351       ctx->scc_id++;
352       while (v != w)
353       {
354         w->scc_id = ctx->scc_id;
355         w = stack[--(*stack_size)];
356         w->contained = 0;
357       }
358       w->scc_id = ctx->scc_id;
359     }
360   }
361 }
362
363 /**
364  * Detect all SCCs (Strongly Connected Components) inside the given automaton.
365  * SCCs will be marked using the scc_id on each state.
366  *
367  * @param ctx context
368  * @param a automaton
369  */
370 static void
371 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
372 {
373   unsigned int i;
374   int index;
375   struct GNUNET_REGEX_State *v;
376   struct GNUNET_REGEX_State *stack[a->state_count];
377   unsigned int stack_size;
378
379   for (v = a->states_head; NULL != v; v = v->next)
380   {
381     v->contained = 0;
382     v->index = -1;
383     v->lowlink = -1;
384   }
385
386   stack_size = 0;
387   index = 0;
388
389   for (i = 0, v = a->states_head; NULL != v; v = v->next)
390   {
391     if (v->index < 0)
392       scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
393   }
394 }
395
396 /**
397  * Compare two states. Used for sorting.
398  *
399  * @param a first state
400  * @param b second state
401  *
402  * @return an integer less than, equal to, or greater than zero
403  *         if the first argument is considered to be respectively
404  *         less than, equal to, or greater than the second.
405  */
406 static int
407 state_compare (const void *a, const void *b)
408 {
409   struct GNUNET_REGEX_State **s1;
410   struct GNUNET_REGEX_State **s2;
411
412   s1 = (struct GNUNET_REGEX_State **) a;
413   s2 = (struct GNUNET_REGEX_State **) b;
414
415   return (*s1)->id - (*s2)->id;
416 }
417
418 /**
419  * Compare to state sets by comparing the id's of the states that are
420  * contained in each set. Both sets are expected to be sorted by id!
421  *
422  * @param sset1 first state set
423  * @param sset2 second state set
424  *
425  * @return an integer less than, equal to, or greater than zero
426  *         if the first argument is considered to be respectively
427  *         less than, equal to, or greater than the second.
428  */
429 static int
430 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
431                    struct GNUNET_REGEX_StateSet *sset2)
432 {
433   int result;
434   int i;
435
436   if (NULL == sset1 || NULL == sset2)
437     return 1;
438
439   result = sset1->len - sset2->len;
440
441   for (i = 0; i < sset1->len; i++)
442   {
443     if (0 != result)
444       break;
445
446     result = state_compare (&sset1->states[i], &sset2->states[i]);
447   }
448   return result;
449 }
450
451 /**
452  * Clears the given StateSet 'set'
453  *
454  * @param set set to be cleared
455  */
456 static void
457 state_set_clear (struct GNUNET_REGEX_StateSet *set)
458 {
459   if (NULL != set)
460   {
461     if (NULL != set->states)
462       GNUNET_free (set->states);
463     GNUNET_free (set);
464   }
465 }
466
467 /**
468  * Adds a transition from one state to another on 'label'
469  *
470  * @param ctx context
471  * @param from_state starting state for the transition
472  * @param label transition label
473  * @param to_state state to where the transition should point to
474  */
475 static void
476 state_add_transition (struct GNUNET_REGEX_Context *ctx,
477                       struct GNUNET_REGEX_State *from_state, const char label,
478                       struct GNUNET_REGEX_State *to_state)
479 {
480   struct Transition *t;
481
482   if (NULL == from_state)
483   {
484     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
485     return;
486   }
487
488   t = GNUNET_malloc (sizeof (struct Transition));
489
490   t->id = ctx->transition_id++;
491   t->label = label;
492   t->to_state = to_state;
493   t->from_state = from_state;
494
495   GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
496                                from_state->transitions_tail, t);
497 }
498
499 /**
500  * Clears an automaton fragment. Does not destroy the states inside
501  * the automaton.
502  *
503  * @param a automaton to be cleared
504  */
505 static void
506 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
507 {
508   if (NULL == a)
509     return;
510
511   a->start = NULL;
512   a->end = NULL;
513   a->states_head = NULL;
514   a->states_tail = NULL;
515   a->state_count = 0;
516   GNUNET_free (a);
517 }
518
519 /**
520  * Frees the memory used by State 's'
521  *
522  * @param s state that should be destroyed
523  */
524 static void
525 automaton_destroy_state (struct GNUNET_REGEX_State *s)
526 {
527   struct Transition *t;
528   struct Transition *next_t;
529
530   if (NULL == s)
531     return;
532
533   if (NULL != s->name)
534     GNUNET_free (s->name);
535
536   for (t = s->transitions_head; NULL != t;)
537   {
538     next_t = t->next;
539     GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
540     GNUNET_free (t);
541     t = next_t;
542   }
543
544   state_set_clear (s->nfa_set);
545
546   GNUNET_free (s);
547 }
548
549 /**
550  * Remove a state from the given automaton 'a'. Always use this function
551  * when altering the states of an automaton. Will also remove all transitions
552  * leading to this state, before destroying it.
553  *
554  * @param a automaton
555  * @param s state to remove
556  */
557 static void
558 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
559                         struct GNUNET_REGEX_State *s)
560 {
561   struct GNUNET_REGEX_State *ss;
562   struct GNUNET_REGEX_State *s_check;
563   struct Transition *t_check;
564
565   if (NULL == a || NULL == s)
566     return;
567
568   // remove state
569   ss = s;
570   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
571   a->state_count--;
572
573   // remove all transitions leading to this state
574   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
575   {
576     for (t_check = s_check->transitions_head; NULL != t_check;
577          t_check = t_check->next)
578     {
579       if (t_check->to_state == ss)
580       {
581         GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
582                                      s_check->transitions_tail, t_check);
583         s_check->transition_count--;
584       }
585     }
586   }
587
588   automaton_destroy_state (ss);
589 }
590
591 /**
592  * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
593  *
594  * @param ctx context
595  * @param a automaton
596  * @param s1 first state
597  * @param s2 second state, will be destroyed
598  */
599 static void
600 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
601                         struct GNUNET_REGEX_Automaton *a,
602                         struct GNUNET_REGEX_State *s1,
603                         struct GNUNET_REGEX_State *s2)
604 {
605   struct GNUNET_REGEX_State *s_check;
606   struct Transition *t_check;
607   struct Transition *t;
608   char *new_name;
609
610   GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
611
612   // 1. Make all transitions pointing to s2 point to s1
613   for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
614   {
615     for (t_check = s_check->transitions_head; NULL != t_check;
616          t_check = t_check->next)
617     {
618       if (s_check != s1 && s2 == t_check->to_state)
619         t_check->to_state = s1;
620     }
621   }
622
623   // 2. Add all transitions from s2 to sX to s1
624   for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
625   {
626     for (t = s1->transitions_head; NULL != t; t = t->next)
627     {
628       if (t_check->label != t->label && NULL != t_check->to_state &&
629           t_check->to_state != t->to_state && t_check->to_state != s2)
630       {
631         state_add_transition (ctx, s1, t_check->label, t_check->to_state);
632       }
633     }
634   }
635
636   // 3. Rename s1 to {s1,s2}
637   new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
638   strncat (new_name, s1->name, strlen (s1->name));
639   strncat (new_name, s2->name, strlen (s2->name));
640   if (NULL != s1->name)
641     GNUNET_free (s1->name);
642   s1->name = new_name;
643
644   // remove state
645   s_check = s2;
646   GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
647   a->state_count--;
648   automaton_destroy_state (s_check);
649 }
650
651 /**
652  * Add a state to the automaton 'a', always use this function to
653  * alter the states DLL of the automaton.
654  *
655  * @param a automaton to add the state to
656  * @param s state that should be added
657  */
658 static void
659 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
660                      struct GNUNET_REGEX_State *s)
661 {
662   GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
663   a->state_count++;
664 }
665
666 typedef void (*GNUNET_REGEX_traverse_action)(void *cls, struct
667                                              GNUNET_REGEX_State *s);
668
669 /**
670  * Traverses all states that are reachable from state 's'. Expects
671  * the states to be unmarked (s->marked == GNUNET_NO). Performs
672  * 'action' on each visited state.
673  *
674  * @param s start state.
675  * @param action action to be performed on each state.
676  */
677 static void
678 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
679                           GNUNET_REGEX_traverse_action action)
680 {
681   struct Transition *t;
682
683   if (GNUNET_NO == s->marked)
684   {
685     s->marked = GNUNET_YES;
686
687     if (NULL != action)
688       action (cls, s);
689
690     for (t = s->transitions_head; NULL != t; t = t->next)
691       automaton_state_traverse (cls, t->to_state, action);
692   }
693 }
694
695 /**
696  * Traverses the given automaton from it's start state, visiting all
697  * reachable states and calling 'action' on each one of them.
698  *
699  * @param a automaton.
700  * @param action action to be performed on each state.
701  */
702 static void
703 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
704                     GNUNET_REGEX_traverse_action action)
705 {
706   struct GNUNET_REGEX_State *s;
707
708   for (s = a->start; NULL != s; s = s->next)
709     s->marked = GNUNET_NO;
710
711   automaton_state_traverse (cls, a->start, action);
712 }
713
714 /**
715  * Creates a new DFA state based on a set of NFA states. Needs to be freed
716  * using automaton_destroy_state.
717  *
718  * @param ctx context
719  * @param nfa_states set of NFA states on which the DFA should be based on
720  *
721  * @return new DFA state
722  */
723 static struct GNUNET_REGEX_State *
724 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
725                   struct GNUNET_REGEX_StateSet *nfa_states)
726 {
727   struct GNUNET_REGEX_State *s;
728   char *name;
729   int len = 0;
730   struct GNUNET_REGEX_State *cstate;
731   struct Transition *ctran;
732   int insert = 1;
733   struct Transition *t;
734   int i;
735
736   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
737   s->id = ctx->state_id++;
738   s->accepting = 0;
739   s->marked = 0;
740   s->name = NULL;
741   s->scc_id = 0;
742   s->index = -1;
743   s->lowlink = -1;
744   s->contained = 0;
745
746   if (NULL == nfa_states)
747   {
748     GNUNET_asprintf (&s->name, "s%i", s->id);
749     return s;
750   }
751
752   s->nfa_set = nfa_states;
753
754   if (nfa_states->len < 1)
755     return s;
756
757   // Create a name based on 'sset'
758   s->name = GNUNET_malloc (sizeof (char) * 2);
759   strcat (s->name, "{");
760   name = NULL;
761
762   for (i = 0; i < nfa_states->len; i++)
763   {
764     cstate = nfa_states->states[i];
765     GNUNET_asprintf (&name, "%i,", cstate->id);
766
767     if (NULL != name)
768     {
769       len = strlen (s->name) + strlen (name) + 1;
770       s->name = GNUNET_realloc (s->name, len);
771       strcat (s->name, name);
772       GNUNET_free (name);
773       name = NULL;
774     }
775
776     // Add a transition for each distinct label to NULL state
777     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
778     {
779       if (0 != ctran->label)
780       {
781         insert = 1;
782
783         for (t = s->transitions_head; NULL != t; t = t->next)
784         {
785           if (t->label == ctran->label)
786           {
787             insert = 0;
788             break;
789           }
790         }
791
792         if (insert)
793           state_add_transition (ctx, s, ctran->label, NULL);
794       }
795     }
796
797     // If the nfa_states contain an accepting state, the new dfa state is also accepting
798     if (cstate->accepting)
799       s->accepting = 1;
800   }
801
802   s->name[strlen (s->name) - 1] = '}';
803
804   return s;
805 }
806
807 /**
808  * Move from the given state 's' to the next state on
809  * transition 'label'
810  *
811  * @param s starting state
812  * @param label edge label to follow
813  *
814  * @return new state or NULL, if transition on label not possible
815  */
816 static struct GNUNET_REGEX_State *
817 dfa_move (struct GNUNET_REGEX_State *s, const char label)
818 {
819   struct Transition *t;
820   struct GNUNET_REGEX_State *new_s;
821
822   if (NULL == s)
823     return NULL;
824
825   new_s = NULL;
826
827   for (t = s->transitions_head; NULL != t; t = t->next)
828   {
829     if (label == t->label)
830     {
831       new_s = t->to_state;
832       break;
833     }
834   }
835
836   return new_s;
837 }
838
839
840 /**
841  * Remove all unreachable states from DFA 'a'. Unreachable states
842  * are those states that are not reachable from the starting state.
843  *
844  * @param a DFA automaton
845  */
846 static void
847 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
848 {
849   struct GNUNET_REGEX_State *s;
850   struct GNUNET_REGEX_State *s_next;
851
852   // 1. unmark all states
853   for (s = a->states_head; NULL != s; s = s->next)
854     s->marked = GNUNET_NO;
855
856   // 2. traverse dfa from start state and mark all visited states
857   automaton_traverse (NULL, a, NULL);
858
859   // 3. delete all states that were not visited
860   for (s = a->states_head; NULL != s; s = s_next)
861   {
862     s_next = s->next;
863     if (GNUNET_NO == s->marked)
864     {
865       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
866       automaton_remove_state (a, s);
867     }
868   }
869 }
870
871 /**
872  * Remove all dead states from the DFA 'a'. Dead states are those
873  * states that do not transition to any other state but themselfes.
874  *
875  * @param a DFA automaton
876  */
877 static void
878 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
879 {
880   struct GNUNET_REGEX_State *s;
881   struct Transition *t;
882   int dead;
883
884   GNUNET_assert (DFA == a->type);
885
886   for (s = a->states_head; NULL != s; s = s->next)
887   {
888     if (s->accepting)
889       continue;
890
891     dead = 1;
892     for (t = s->transitions_head; NULL != t; t = t->next)
893     {
894       if (NULL != t->to_state && t->to_state != s)
895       {
896         dead = 0;
897         break;
898       }
899     }
900
901     if (0 == dead)
902       continue;
903
904     // state s is dead, remove it
905     automaton_remove_state (a, s);
906   }
907 }
908
909 /**
910  * Merge all non distinguishable states in the DFA 'a'
911  *
912  * @param ctx context
913  * @param a DFA automaton
914  */
915 static void
916 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
917                                      struct GNUNET_REGEX_Automaton *a)
918 {
919   int i;
920   int table[a->state_count][a->state_count];
921   struct GNUNET_REGEX_State *s1;
922   struct GNUNET_REGEX_State *s2;
923   struct Transition *t1;
924   struct Transition *t2;
925   int change;
926
927   change = 1;
928   for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
929        i++, s1 = s1->next)
930     s1->marked = i;
931
932   // Mark all pairs of accepting/!accepting states
933   for (s1 = a->states_head; NULL != s1; s1 = s1->next)
934   {
935     for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
936     {
937       if ((s1->accepting && !s2->accepting) ||
938           (!s1->accepting && s2->accepting))
939       {
940         table[s1->marked][s2->marked] = 1;
941       }
942       else
943         table[s1->marked][s2->marked] = 0;
944     }
945   }
946
947   while (0 != change)
948   {
949     change = 0;
950     for (s1 = a->states_head; NULL != s1; s1 = s1->next)
951     {
952       for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
953       {
954         if (0 != table[s1->marked][s2->marked])
955           continue;
956
957         for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
958         {
959           for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
960           {
961             if (t1->label == t2->label && t1->to_state == t2->to_state &&
962                 (0 != table[t1->to_state->marked][t2->to_state->marked] ||
963                  0 != table[t2->to_state->marked][t1->to_state->marked]))
964             {
965               table[s1->marked][s2->marked] = t1->label;
966               change = 1;
967             }
968             else if (t1->label != t2->label && t1->to_state != t2->to_state)
969             {
970               table[s1->marked][s2->marked] = -1;
971               change = 1;
972             }
973           }
974         }
975       }
976     }
977   }
978
979   struct GNUNET_REGEX_State *s2_next;
980
981   for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
982   {
983     for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
984     {
985       s2_next = s2->next;
986       if (s1 != s2 && table[s1->marked][s2->marked] == 0)
987         automaton_merge_states (ctx, a, s1, s2);
988     }
989   }
990 }
991
992 /**
993  * Minimize the given DFA 'a' by removing all unreachable states,
994  * removing all dead states and merging all non distinguishable states
995  *
996  * @param ctx context
997  * @param a DFA automaton
998  */
999 static void
1000 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1001               struct GNUNET_REGEX_Automaton *a)
1002 {
1003   if (NULL == a)
1004     return;
1005
1006   GNUNET_assert (DFA == a->type);
1007
1008   // 1. remove unreachable states
1009   dfa_remove_unreachable_states (a);
1010
1011   // 2. remove dead states
1012   dfa_remove_dead_states (a);
1013
1014   // 3. Merge nondistinguishable states
1015   dfa_merge_nondistinguishable_states (ctx, a);
1016 }
1017
1018 /**
1019  * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
1020  *
1021  * @param start starting state
1022  * @param end end state
1023  *
1024  * @return new NFA fragment
1025  */
1026 static struct GNUNET_REGEX_Automaton *
1027 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1028                      struct GNUNET_REGEX_State *end)
1029 {
1030   struct GNUNET_REGEX_Automaton *n;
1031
1032   n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1033
1034   n->type = NFA;
1035   n->start = NULL;
1036   n->end = NULL;
1037
1038   if (NULL == start && NULL == end)
1039     return n;
1040
1041   automaton_add_state (n, end);
1042   automaton_add_state (n, start);
1043
1044   n->start = start;
1045   n->end = end;
1046
1047   return n;
1048 }
1049
1050 /**
1051  * Adds a list of states to the given automaton 'n'.
1052  *
1053  * @param n automaton to which the states should be added
1054  * @param states_head head of the DLL of states
1055  * @param states_tail tail of the DLL of states
1056  */
1057 static void
1058 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1059                 struct GNUNET_REGEX_State *states_head,
1060                 struct GNUNET_REGEX_State *states_tail)
1061 {
1062   struct GNUNET_REGEX_State *s;
1063
1064   if (NULL == n || NULL == states_head)
1065   {
1066     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1067     return;
1068   }
1069
1070   if (NULL == n->states_head)
1071   {
1072     n->states_head = states_head;
1073     n->states_tail = states_tail;
1074     return;
1075   }
1076
1077   if (NULL != states_head)
1078   {
1079     n->states_tail->next = states_head;
1080     n->states_tail = states_tail;
1081   }
1082
1083   for (s = states_head; NULL != s; s = s->next)
1084     n->state_count++;
1085 }
1086
1087 /**
1088  * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1089  *
1090  * @param ctx context
1091  * @param accepting is it an accepting state or not
1092  *
1093  * @return new NFA state
1094  */
1095 static struct GNUNET_REGEX_State *
1096 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1097 {
1098   struct GNUNET_REGEX_State *s;
1099
1100   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1101   s->id = ctx->state_id++;
1102   s->accepting = accepting;
1103   s->marked = 0;
1104   s->contained = 0;
1105   s->index = -1;
1106   s->lowlink = -1;
1107   s->scc_id = 0;
1108   s->name = NULL;
1109   GNUNET_asprintf (&s->name, "s%i", s->id);
1110
1111   return s;
1112 }
1113
1114 /**
1115  * Calculates the NFA closure set for the given state.
1116  *
1117  * @param nfa the NFA containing 's'
1118  * @param s starting point state
1119  * @param label transitioning label on which to base the closure on,
1120  *                pass 0 for epsilon transition
1121  *
1122  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1123  */
1124 static struct GNUNET_REGEX_StateSet *
1125 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1126                     struct GNUNET_REGEX_State *s, const char label)
1127 {
1128   struct GNUNET_REGEX_StateSet *cls;
1129   struct GNUNET_REGEX_StateSet *cls_check;
1130   struct GNUNET_REGEX_State *clsstate;
1131   struct GNUNET_REGEX_State *currentstate;
1132   struct Transition *ctran;
1133
1134   if (NULL == s)
1135     return NULL;
1136
1137   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1138   cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1139
1140   for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1141     clsstate->contained = 0;
1142
1143   // Add start state to closure only for epsilon closure
1144   if (0 == label)
1145     GNUNET_array_append (cls->states, cls->len, s);
1146
1147   GNUNET_array_append (cls_check->states, cls_check->len, s);
1148   while (cls_check->len > 0)
1149   {
1150     currentstate = cls_check->states[cls_check->len - 1];
1151     GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1152
1153     for (ctran = currentstate->transitions_head; NULL != ctran;
1154          ctran = ctran->next)
1155     {
1156       if (NULL != ctran->to_state && label == ctran->label)
1157       {
1158         clsstate = ctran->to_state;
1159
1160         if (NULL != clsstate && 0 == clsstate->contained)
1161         {
1162           GNUNET_array_append (cls->states, cls->len, clsstate);
1163           GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1164           clsstate->contained = 1;
1165         }
1166       }
1167     }
1168   }
1169   GNUNET_assert (0 == cls_check->len);
1170   GNUNET_free (cls_check);
1171
1172   if (cls->len > 1)
1173     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1174            state_compare);
1175
1176   return cls;
1177 }
1178
1179 /**
1180  * Calculates the closure set for the given set of states.
1181  *
1182  * @param nfa the NFA containing 's'
1183  * @param states list of states on which to base the closure on
1184  * @param label transitioning label for which to base the closure on,
1185  *                pass 0 for epsilon transition
1186  *
1187  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1188  */
1189 static struct GNUNET_REGEX_StateSet *
1190 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1191                         struct GNUNET_REGEX_StateSet *states,
1192                         const char label)
1193 {
1194   struct GNUNET_REGEX_State *s;
1195   struct GNUNET_REGEX_StateSet *sset;
1196   struct GNUNET_REGEX_StateSet *cls;
1197   int i;
1198   int j;
1199   int k;
1200   int contains;
1201
1202   if (NULL == states)
1203     return NULL;
1204
1205   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1206
1207   for (i = 0; i < states->len; i++)
1208   {
1209     s = states->states[i];
1210     sset = nfa_closure_create (nfa, s, label);
1211
1212     for (j = 0; j < sset->len; j++)
1213     {
1214       contains = 0;
1215       for (k = 0; k < cls->len; k++)
1216       {
1217         if (sset->states[j]->id == cls->states[k]->id)
1218         {
1219           contains = 1;
1220           break;
1221         }
1222       }
1223       if (!contains)
1224         GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1225     }
1226     state_set_clear (sset);
1227   }
1228
1229   if (cls->len > 1)
1230     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1231            state_compare);
1232
1233   return cls;
1234 }
1235
1236 /**
1237  * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1238  *
1239  * @param ctx context
1240  */
1241 static void
1242 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1243 {
1244   struct GNUNET_REGEX_Automaton *a;
1245   struct GNUNET_REGEX_Automaton *b;
1246   struct GNUNET_REGEX_Automaton *new;
1247
1248   b = ctx->stack_tail;
1249   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1250   a = ctx->stack_tail;
1251   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1252
1253   state_add_transition (ctx, a->end, 0, b->start);
1254   a->end->accepting = 0;
1255   b->end->accepting = 1;
1256
1257   new = nfa_fragment_create (NULL, NULL);
1258   nfa_add_states (new, a->states_head, a->states_tail);
1259   nfa_add_states (new, b->states_head, b->states_tail);
1260   new->start = a->start;
1261   new->end = b->end;
1262   automaton_fragment_clear (a);
1263   automaton_fragment_clear (b);
1264
1265   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1266 }
1267
1268 /**
1269  * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1270  *
1271  * @param ctx context
1272  */
1273 static void
1274 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1275 {
1276   struct GNUNET_REGEX_Automaton *a;
1277   struct GNUNET_REGEX_Automaton *new;
1278   struct GNUNET_REGEX_State *start;
1279   struct GNUNET_REGEX_State *end;
1280
1281   a = ctx->stack_tail;
1282   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1283
1284   if (NULL == a)
1285   {
1286     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287                 "nfa_add_star_op failed, because there was no element on the stack");
1288     return;
1289   }
1290
1291   start = nfa_state_create (ctx, 0);
1292   end = nfa_state_create (ctx, 1);
1293
1294   state_add_transition (ctx, start, 0, a->start);
1295   state_add_transition (ctx, start, 0, end);
1296   state_add_transition (ctx, a->end, 0, a->start);
1297   state_add_transition (ctx, a->end, 0, end);
1298
1299   a->end->accepting = 0;
1300   end->accepting = 1;
1301
1302   new = nfa_fragment_create (start, end);
1303   nfa_add_states (new, a->states_head, a->states_tail);
1304   automaton_fragment_clear (a);
1305
1306   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1307 }
1308
1309 /**
1310  * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1311  *
1312  * @param ctx context
1313  */
1314 static void
1315 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1316 {
1317   struct GNUNET_REGEX_Automaton *a;
1318
1319   a = ctx->stack_tail;
1320   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1321
1322   state_add_transition (ctx, a->end, 0, a->start);
1323
1324   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1325 }
1326
1327 /**
1328  * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1329  *
1330  * @param ctx context
1331  */
1332 static void
1333 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1334 {
1335   struct GNUNET_REGEX_Automaton *a;
1336   struct GNUNET_REGEX_Automaton *new;
1337   struct GNUNET_REGEX_State *start;
1338   struct GNUNET_REGEX_State *end;
1339
1340   a = ctx->stack_tail;
1341   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1342
1343   if (NULL == a)
1344   {
1345     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1346                 "nfa_add_question_op failed, because there was no element on the stack");
1347     return;
1348   }
1349
1350   start = nfa_state_create (ctx, 0);
1351   end = nfa_state_create (ctx, 1);
1352
1353   state_add_transition (ctx, start, 0, a->start);
1354   state_add_transition (ctx, start, 0, end);
1355   state_add_transition (ctx, a->end, 0, end);
1356
1357   a->end->accepting = 0;
1358
1359   new = nfa_fragment_create (start, end);
1360   nfa_add_states (new, a->states_head, a->states_tail);
1361   automaton_fragment_clear (a);
1362
1363   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1364 }
1365
1366 /**
1367  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1368  * that alternates between a and b (a|b)
1369  *
1370  * @param ctx context
1371  */
1372 static void
1373 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1374 {
1375   struct GNUNET_REGEX_Automaton *a;
1376   struct GNUNET_REGEX_Automaton *b;
1377   struct GNUNET_REGEX_Automaton *new;
1378   struct GNUNET_REGEX_State *start;
1379   struct GNUNET_REGEX_State *end;
1380
1381   b = ctx->stack_tail;
1382   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1383   a = ctx->stack_tail;
1384   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1385
1386   start = nfa_state_create (ctx, 0);
1387   end = nfa_state_create (ctx, 1);
1388   state_add_transition (ctx, start, 0, a->start);
1389   state_add_transition (ctx, start, 0, b->start);
1390
1391   state_add_transition (ctx, a->end, 0, end);
1392   state_add_transition (ctx, b->end, 0, end);
1393
1394   a->end->accepting = 0;
1395   b->end->accepting = 0;
1396   end->accepting = 1;
1397
1398   new = nfa_fragment_create (start, end);
1399   nfa_add_states (new, a->states_head, a->states_tail);
1400   nfa_add_states (new, b->states_head, b->states_tail);
1401   automaton_fragment_clear (a);
1402   automaton_fragment_clear (b);
1403
1404   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1405 }
1406
1407 /**
1408  * Adds a new nfa fragment to the stack
1409  *
1410  * @param ctx context
1411  * @param lit label for nfa transition
1412  */
1413 static void
1414 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1415 {
1416   struct GNUNET_REGEX_Automaton *n;
1417   struct GNUNET_REGEX_State *start;
1418   struct GNUNET_REGEX_State *end;
1419
1420   GNUNET_assert (NULL != ctx);
1421
1422   start = nfa_state_create (ctx, 0);
1423   end = nfa_state_create (ctx, 1);
1424   state_add_transition (ctx, start, lit, end);
1425   n = nfa_fragment_create (start, end);
1426   GNUNET_assert (NULL != n);
1427   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1428 }
1429
1430 /**
1431  * Initialize a new context
1432  *
1433  * @param ctx context
1434  */
1435 static void
1436 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1437 {
1438   if (NULL == ctx)
1439   {
1440     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1441     return;
1442   }
1443   ctx->state_id = 0;
1444   ctx->transition_id = 0;
1445   ctx->scc_id = 0;
1446   ctx->stack_head = NULL;
1447   ctx->stack_tail = NULL;
1448 }
1449
1450 /**
1451  * Construct an NFA by parsing the regex string of length 'len'.
1452  *
1453  * @param regex regular expression string
1454  * @param len length of the string
1455  *
1456  * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1457  */
1458 struct GNUNET_REGEX_Automaton *
1459 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1460 {
1461   struct GNUNET_REGEX_Context ctx;
1462   struct GNUNET_REGEX_Automaton *nfa;
1463   const char *regexp;
1464   char *error_msg;
1465   unsigned int count;
1466   unsigned int altcount;
1467   unsigned int atomcount;
1468   unsigned int pcount;
1469   struct
1470   {
1471     int altcount;
1472     int atomcount;
1473   }     *p;
1474
1475   GNUNET_REGEX_context_init (&ctx);
1476
1477   regexp = regex;
1478   p = NULL;
1479   error_msg = NULL;
1480   altcount = 0;
1481   atomcount = 0;
1482   pcount = 0;
1483
1484   for (count = 0; count < len && *regexp; count++, regexp++)
1485   {
1486     switch (*regexp)
1487     {
1488     case '(':
1489       if (atomcount > 1)
1490       {
1491         --atomcount;
1492         nfa_add_concatenation (&ctx);
1493       }
1494       GNUNET_array_grow (p, pcount, pcount + 1);
1495       p[pcount - 1].altcount = altcount;
1496       p[pcount - 1].atomcount = atomcount;
1497       altcount = 0;
1498       atomcount = 0;
1499       break;
1500     case '|':
1501       if (0 == atomcount)
1502       {
1503         error_msg = "Cannot append '|' to nothing";
1504         goto error;
1505       }
1506       while (--atomcount > 0)
1507         nfa_add_concatenation (&ctx);
1508       altcount++;
1509       break;
1510     case ')':
1511       if (0 == pcount)
1512       {
1513         error_msg = "Missing opening '('";
1514         goto error;
1515       }
1516       if (0 == atomcount)
1517       {
1518         // Ignore this: "()"
1519         pcount--;
1520         altcount = p[pcount].altcount;
1521         atomcount = p[pcount].atomcount;
1522         break;
1523       }
1524       while (--atomcount > 0)
1525         nfa_add_concatenation (&ctx);
1526       for (; altcount > 0; altcount--)
1527         nfa_add_alternation (&ctx);
1528       pcount--;
1529       altcount = p[pcount].altcount;
1530       atomcount = p[pcount].atomcount;
1531       atomcount++;
1532       break;
1533     case '*':
1534       if (atomcount == 0)
1535       {
1536         error_msg = "Cannot append '+' to nothing";
1537         goto error;
1538       }
1539       nfa_add_star_op (&ctx);
1540       break;
1541     case '+':
1542       if (atomcount == 0)
1543       {
1544         error_msg = "Cannot append '+' to nothing";
1545         goto error;
1546       }
1547       nfa_add_plus_op (&ctx);
1548       break;
1549     case '?':
1550       if (atomcount == 0)
1551       {
1552         error_msg = "Cannot append '?' to nothing";
1553         goto error;
1554       }
1555       nfa_add_question_op (&ctx);
1556       break;
1557     case 92:                   /* escape: \ */
1558       regexp++;
1559       count++;
1560     default:
1561       if (atomcount > 1)
1562       {
1563         --atomcount;
1564         nfa_add_concatenation (&ctx);
1565       }
1566       nfa_add_label (&ctx, *regexp);
1567       atomcount++;
1568       break;
1569     }
1570   }
1571   if (0 != pcount)
1572   {
1573     error_msg = "Unbalanced parenthesis";
1574     goto error;
1575   }
1576   while (--atomcount > 0)
1577     nfa_add_concatenation (&ctx);
1578   for (; altcount > 0; altcount--)
1579     nfa_add_alternation (&ctx);
1580
1581   if (NULL != p)
1582     GNUNET_free (p);
1583
1584   nfa = ctx.stack_tail;
1585   GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1586
1587
1588   if (NULL != ctx.stack_head)
1589   {
1590     error_msg = "Creating the NFA failed. NFA stack was not empty!";
1591     goto error;
1592   }
1593
1594   return nfa;
1595
1596 error:
1597   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1598   if (NULL != error_msg)
1599     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1600   if (NULL != p)
1601     GNUNET_free (p);
1602   while (NULL != ctx.stack_tail)
1603   {
1604     GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1605     GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1606                                  ctx.stack_tail);
1607   }
1608   return NULL;
1609 }
1610
1611 /**
1612  * Construct DFA for the given 'regex' of length 'len'
1613  *
1614  * @param regex regular expression string
1615  * @param len length of the regular expression
1616  *
1617  * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1618  */
1619 struct GNUNET_REGEX_Automaton *
1620 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1621 {
1622   struct GNUNET_REGEX_Context ctx;
1623   struct GNUNET_REGEX_Automaton *dfa;
1624   struct GNUNET_REGEX_Automaton *nfa;
1625   struct GNUNET_REGEX_StateSet *tmp;
1626   struct GNUNET_REGEX_StateSet *nfa_set;
1627   struct GNUNET_REGEX_StateSet *dfa_stack;
1628   struct Transition *ctran;
1629   struct GNUNET_REGEX_State *dfa_state;
1630   struct GNUNET_REGEX_State *new_dfa_state;
1631   struct GNUNET_REGEX_State *state_contains;
1632   struct GNUNET_REGEX_State *state_iter;
1633
1634   GNUNET_REGEX_context_init (&ctx);
1635
1636   // Create NFA
1637   nfa = GNUNET_REGEX_construct_nfa (regex, len);
1638
1639   if (NULL == nfa)
1640   {
1641     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1642                 "Could not create DFA, because NFA creation failed\n");
1643     return NULL;
1644   }
1645
1646   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1647   dfa->type = DFA;
1648
1649   // Create DFA start state from epsilon closure
1650   dfa_stack = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1651   nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1652   dfa->start = dfa_state_create (&ctx, nfa_set);
1653   automaton_add_state (dfa, dfa->start);
1654   GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1655
1656   // Create dfa states by combining nfa states
1657   while (dfa_stack->len > 0)
1658   {
1659     dfa_state = dfa_stack->states[dfa_stack->len - 1];
1660     GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1661
1662     for (ctran = dfa_state->transitions_head; NULL != ctran;
1663          ctran = ctran->next)
1664     {
1665       if (0 != ctran->label && NULL == ctran->to_state)
1666       {
1667         tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1668         nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1669         state_set_clear (tmp);
1670         new_dfa_state = dfa_state_create (&ctx, nfa_set);
1671         state_contains = NULL;
1672         for (state_iter = dfa->states_head; NULL != state_iter;
1673              state_iter = state_iter->next)
1674         {
1675           if (0 ==
1676               state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1677             state_contains = state_iter;
1678         }
1679
1680         if (NULL == state_contains)
1681         {
1682           automaton_add_state (dfa, new_dfa_state);
1683           GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1684                                new_dfa_state);
1685           ctran->to_state = new_dfa_state;
1686         }
1687         else
1688         {
1689           ctran->to_state = state_contains;
1690           automaton_destroy_state (new_dfa_state);
1691         }
1692       }
1693     }
1694   }
1695
1696   GNUNET_free (dfa_stack);
1697   GNUNET_REGEX_automaton_destroy (nfa);
1698
1699   dfa_minimize (&ctx, dfa);
1700   scc_tarjan (&ctx, dfa);
1701
1702   return dfa;
1703 }
1704
1705 /**
1706  * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1707  * data structure.
1708  *
1709  * @param a automaton to be destroyed
1710  */
1711 void
1712 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1713 {
1714   struct GNUNET_REGEX_State *s;
1715   struct GNUNET_REGEX_State *next_state;
1716
1717   if (NULL == a)
1718     return;
1719
1720   for (s = a->states_head; NULL != s;)
1721   {
1722     next_state = s->next;
1723     automaton_destroy_state (s);
1724     s = next_state;
1725   }
1726
1727   GNUNET_free (a);
1728 }
1729
1730 /**
1731  * Save the given automaton as a GraphViz dot file
1732  *
1733  * @param a the automaton to be saved
1734  * @param filename where to save the file
1735  */
1736 void
1737 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1738                                    const char *filename)
1739 {
1740   struct GNUNET_REGEX_State *s;
1741   struct Transition *ctran;
1742   char *s_acc = NULL;
1743   char *s_tran = NULL;
1744   char *start;
1745   char *end;
1746   FILE *p;
1747
1748   if (NULL == a)
1749   {
1750     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1751     return;
1752   }
1753
1754   if (NULL == filename || strlen (filename) < 1)
1755   {
1756     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1757     return;
1758   }
1759
1760   p = fopen (filename, "w");
1761
1762   if (p == NULL)
1763   {
1764     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1765                 filename);
1766     return;
1767   }
1768
1769   start = "digraph G {\nrankdir=LR\n";
1770   fwrite (start, strlen (start), 1, p);
1771
1772   for (s = a->states_head; NULL != s; s = s->next)
1773   {
1774     if (s->accepting)
1775     {
1776       GNUNET_asprintf (&s_acc,
1777                        "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1778                        s->name, s->scc_id);
1779       fwrite (s_acc, strlen (s_acc), 1, p);
1780       GNUNET_free (s_acc);
1781     }
1782     else
1783     {
1784       GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
1785                        s->scc_id);
1786       fwrite (s_acc, strlen (s_acc), 1, p);
1787       GNUNET_free (s_acc);
1788     }
1789
1790     s->marked = 1;
1791
1792     for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1793     {
1794       if (NULL == ctran->to_state)
1795       {
1796         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1797                     "Transition from State %i has has no state for transitioning\n",
1798                     s->id);
1799         continue;
1800       }
1801
1802       if (ctran->label == 0)
1803       {
1804         GNUNET_asprintf (&s_tran,
1805                          "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
1806                          s->name, ctran->to_state->name, s->scc_id);
1807       }
1808       else
1809       {
1810         GNUNET_asprintf (&s_tran,
1811                          "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
1812                          s->name, ctran->to_state->name, ctran->label,
1813                          s->scc_id);
1814       }
1815
1816       fwrite (s_tran, strlen (s_tran), 1, p);
1817       GNUNET_free (s_tran);
1818     }
1819   }
1820
1821   end = "\n}\n";
1822   fwrite (end, strlen (end), 1, p);
1823   fclose (p);
1824 }
1825
1826 /**
1827  * Evaluates the given string using the given DFA automaton
1828  *
1829  * @param a automaton, type must be DFA
1830  * @param string string that should be evaluated
1831  *
1832  * @return 0 if string matches, non 0 otherwise
1833  */
1834 static int
1835 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1836 {
1837   const char *strp;
1838   struct GNUNET_REGEX_State *s;
1839
1840   if (DFA != a->type)
1841   {
1842     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1843                 "Tried to evaluate DFA, but NFA automaton given");
1844     return -1;
1845   }
1846
1847   s = a->start;
1848
1849   for (strp = string; NULL != strp && *strp; strp++)
1850   {
1851     s = dfa_move (s, *strp);
1852     if (NULL == s)
1853       break;
1854   }
1855
1856   if (NULL != s && s->accepting)
1857     return 0;
1858
1859   return 1;
1860 }
1861
1862 /**
1863  * Evaluates the given string using the given NFA automaton
1864  *
1865  * @param a automaton, type must be NFA
1866  * @param string string that should be evaluated
1867  *
1868  * @return 0 if string matches, non 0 otherwise
1869  */
1870 static int
1871 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1872 {
1873   const char *strp;
1874   struct GNUNET_REGEX_State *s;
1875   struct GNUNET_REGEX_StateSet *sset;
1876   struct GNUNET_REGEX_StateSet *new_sset;
1877   int i;
1878   int result;
1879
1880   if (NFA != a->type)
1881   {
1882     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1883                 "Tried to evaluate NFA, but DFA automaton given");
1884     return -1;
1885   }
1886
1887   result = 1;
1888   strp = string;
1889   sset = nfa_closure_create (a, a->start, 0);
1890
1891   for (strp = string; NULL != strp && *strp; strp++)
1892   {
1893     new_sset = nfa_closure_set_create (a, sset, *strp);
1894     state_set_clear (sset);
1895     sset = nfa_closure_set_create (a, new_sset, 0);
1896     state_set_clear (new_sset);
1897   }
1898
1899   for (i = 0; i < sset->len; i++)
1900   {
1901     s = sset->states[i];
1902     if (NULL != s && s->accepting)
1903     {
1904       result = 0;
1905       break;
1906     }
1907   }
1908
1909   state_set_clear (sset);
1910   return result;
1911 }
1912
1913 /**
1914  * Evaluates the given 'string' against the given compiled regex
1915  *
1916  * @param a automaton
1917  * @param string string to check
1918  *
1919  * @return 0 if string matches, non 0 otherwise
1920  */
1921 int
1922 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1923 {
1924   int result;
1925
1926   switch (a->type)
1927   {
1928   case DFA:
1929     result = evaluate_dfa (a, string);
1930     break;
1931   case NFA:
1932     result = evaluate_nfa (a, string);
1933     break;
1934   default:
1935     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1936                 "Evaluating regex failed, automaton has no type!\n");
1937     result = GNUNET_SYSERR;
1938     break;
1939   }
1940
1941   return result;
1942 }
1943
1944 /**
1945  * Get the first key for the given 'input_string'. This hashes
1946  * the first x bits of the 'input_strings'.
1947  *
1948  * @param input_string string.
1949  * @param string_len length of the 'input_string'.
1950  * @param key pointer to where to write the hash code.
1951  *
1952  * @return number of bits of 'input_string' that have been consumed
1953  *         to construct the key
1954  */
1955 unsigned int
1956 GNUNET_REGEX_get_first_key (const char *input_string,
1957                             unsigned int string_len,
1958                             GNUNET_HashCode *key)
1959 {
1960   unsigned int size;
1961
1962   size = string_len < initial_bits ? string_len : initial_bits;
1963
1964   if (NULL == input_string)
1965   {
1966     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
1967     return 0;
1968   }
1969
1970   GNUNET_CRYPTO_hash (input_string, size, key);
1971
1972   return size;
1973 }
1974
1975 /**
1976  * Check if the given 'proof' matches the given 'key'.
1977  *
1978  * @param proof partial regex
1979  * @param key hash
1980  *
1981  * @return GNUNET_OK if the proof is valid for the given key
1982  */
1983 int
1984 GNUNET_REGEX_check_proof (const char *proof,
1985                           const GNUNET_HashCode *key)
1986 {
1987   return GNUNET_OK;
1988 }
1989
1990 /**
1991  * Get all edges leaving state 's'.
1992  *
1993  * @param s state.
1994  * @param edges all edges leaving 's'.
1995  *
1996  * @return number of edges.
1997  */
1998 static unsigned int
1999 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
2000 {
2001   struct Transition *t;
2002   unsigned int count;
2003
2004   if (NULL == s)
2005     return 0;
2006
2007   count = 0;
2008
2009   for (t = s->transitions_head; NULL != t; t = t->next)
2010   {
2011     if (NULL != t->to_state)
2012     {
2013       edges[count].label = &t->label;
2014       edges[count].destination = t->to_state->hash;
2015       count++;
2016     }
2017   }
2018   return count;
2019 }
2020
2021 /**
2022  * Iterate over all edges helper function starting from state 's', calling
2023  * iterator on for each edge.
2024  *
2025  * @param s state.
2026  * @param iterator iterator function called for each edge.
2027  * @param iterator_cls closure.
2028  */
2029 static void
2030 iterate_edge (struct GNUNET_REGEX_State *s,
2031               GNUNET_REGEX_KeyIterator iterator,
2032               void *iterator_cls)
2033 {
2034   struct Transition *t;
2035   struct GNUNET_REGEX_Edge edges[s->transition_count];
2036   unsigned int num_edges;
2037
2038   if (GNUNET_NO == s->marked)
2039   {
2040     s->marked = GNUNET_YES;
2041
2042     num_edges = state_get_edges (s, edges);
2043
2044     iterator (iterator_cls,
2045               &s->hash,
2046               NULL,
2047
2048               num_edges,
2049               edges);
2050
2051
2052     for (t = s->transitions_head; NULL != t; t = t->next)
2053       iterate_edge (t->to_state, iterator, iterator_cls);
2054   }
2055 }
2056
2057 /**
2058  * Iterate over all edges starting from start state of automaton 'a'. Calling
2059  * iterator for each edge.
2060  *
2061  * @param a automaton.
2062  * @param iterator iterator called for each edge.
2063  * @param iterator_cls closure.
2064  */
2065 void
2066 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2067                                 GNUNET_REGEX_KeyIterator iterator,
2068                                 void *iterator_cls)
2069 {
2070   iterate_edge (a->start, iterator, iterator_cls);
2071 }