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