cd08b8dcd2e17a4c4514d9b619faba8f42d75cf1
[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             strcat (R_cur_r, "(");
920             strcat (R_cur_r, R_last[k][k]);
921             strcat (R_cur_r, ")*");
922           }
923
924           if (NULL != R_last[k][j])
925             strcat (R_cur_r, R_last[k][j]);
926
927           // | is idempotent: a | a = a for all a in A
928           if (0 == strcmp (R_cur_l, R_cur_r) || 0 == strcmp (R_cur_l, "") ||
929               0 == strcmp (R_cur_r, ""))
930           {
931             if (0 == strcmp (R_cur_l, ""))
932               GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_r);
933             else
934               GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_l);
935           }
936           // a | a a* a = a+
937           else if (R_last[i][j] == R_last[i][k] && R_last[i][k] == R_last[k][k]
938                    && R_last[k][k] == R_last[k][j])
939           {
940             GNUNET_asprintf (&R_cur[i][j], "%s+", R_last[i][j]);
941           }
942           // a | a b* b => a | a b | a b b | ... => a b*
943           else if (R_last[i][j] == R_last[i][k] && R_last[k][k] == R_last[k][j])
944           {
945             GNUNET_asprintf (&R_cur[i][j], "%s%s*", R_last[i][k], R_last[k][k]);
946           }
947           // a | b b* a => a | b a | b b a | ... => b* a
948           else if (R_last[i][j] == R_last[k][j] && R_last[i][k] == R_last[k][k])
949           {
950             GNUNET_asprintf (&R_cur[i][j], "%s*%s", R_last[k][k], R_last[k][j]);
951           }
952           else
953             GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
954
955           GNUNET_free_non_null (R_cur_l);
956           GNUNET_free_non_null (R_cur_r);
957
958         }
959       }
960     }
961
962     for (i = 0; i < n; i++)
963     {
964       for (j = 0; j < n; j++)
965       {
966         GNUNET_free_non_null (R_last[i][j]);
967
968         if (NULL != R_cur[i][j])
969         {
970           R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
971           GNUNET_free (R_cur[i][j]);
972           R_cur[i][j] = NULL;
973         }
974       }
975     }
976   }
977
978   // assign proofs and hashes
979   for (i = 0; i < n; i++)
980   {
981     states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
982     GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
983                         &states[i]->hash);
984   }
985
986   // complete regex for whole DFA
987   complete_regex = NULL;
988   for (i = 0; i < n; i++)
989   {
990     if (states[i]->accepting)
991     {
992       if (NULL == complete_regex)
993         GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->marked][i]);
994       else if (NULL != R_last[a->start->marked][i] &&
995                0 != strcmp (R_last[a->start->marked][i], ""))
996       {
997         temp = complete_regex;
998         GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
999                          R_last[a->start->marked][i]);
1000         GNUNET_free (temp);
1001       }
1002     }
1003   }
1004   a->computed_regex = complete_regex;
1005
1006   // cleanup
1007   for (i = 0; i < n; i++)
1008   {
1009     for (j = 0; j < n; j++)
1010       GNUNET_free_non_null (R_last[i][j]);
1011   }
1012 }
1013
1014 /**
1015  * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1016  * automaton_destroy_state.
1017  *
1018  * @param ctx context
1019  * @param nfa_states set of NFA states on which the DFA should be based on
1020  *
1021  * @return new DFA state
1022  */
1023 static struct GNUNET_REGEX_State *
1024 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1025                   struct GNUNET_REGEX_StateSet *nfa_states)
1026 {
1027   struct GNUNET_REGEX_State *s;
1028   char *name;
1029   int len = 0;
1030   struct GNUNET_REGEX_State *cstate;
1031   struct Transition *ctran;
1032   int insert = 1;
1033   struct Transition *t;
1034   int i;
1035
1036   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1037   s->id = ctx->state_id++;
1038   s->accepting = 0;
1039   s->marked = 0;
1040   s->name = NULL;
1041   s->scc_id = 0;
1042   s->index = -1;
1043   s->lowlink = -1;
1044   s->contained = 0;
1045   s->proof = NULL;
1046
1047   if (NULL == nfa_states)
1048   {
1049     GNUNET_asprintf (&s->name, "s%i", s->id);
1050     return s;
1051   }
1052
1053   s->nfa_set = nfa_states;
1054
1055   if (nfa_states->len < 1)
1056     return s;
1057
1058   // Create a name based on 'sset'
1059   s->name = GNUNET_malloc (sizeof (char) * 2);
1060   strcat (s->name, "{");
1061   name = NULL;
1062
1063   for (i = 0; i < nfa_states->len; i++)
1064   {
1065     cstate = nfa_states->states[i];
1066     GNUNET_asprintf (&name, "%i,", cstate->id);
1067
1068     if (NULL != name)
1069     {
1070       len = strlen (s->name) + strlen (name) + 1;
1071       s->name = GNUNET_realloc (s->name, len);
1072       strcat (s->name, name);
1073       GNUNET_free (name);
1074       name = NULL;
1075     }
1076
1077     // Add a transition for each distinct label to NULL state
1078     for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1079     {
1080       if (0 != ctran->label)
1081       {
1082         insert = 1;
1083
1084         for (t = s->transitions_head; NULL != t; t = t->next)
1085         {
1086           if (t->label == ctran->label)
1087           {
1088             insert = 0;
1089             break;
1090           }
1091         }
1092
1093         if (insert)
1094           state_add_transition (ctx, s, ctran->label, NULL);
1095       }
1096     }
1097
1098     // If the nfa_states contain an accepting state, the new dfa state is also
1099     // accepting
1100     if (cstate->accepting)
1101       s->accepting = 1;
1102   }
1103
1104   s->name[strlen (s->name) - 1] = '}';
1105
1106   return s;
1107 }
1108
1109 /**
1110  * Move from the given state 's' to the next state on transition 'label'
1111  *
1112  * @param s starting state
1113  * @param label edge label to follow
1114  *
1115  * @return new state or NULL, if transition on label not possible
1116  */
1117 static struct GNUNET_REGEX_State *
1118 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1119 {
1120   struct Transition *t;
1121   struct GNUNET_REGEX_State *new_s;
1122
1123   if (NULL == s)
1124     return NULL;
1125
1126   new_s = NULL;
1127
1128   for (t = s->transitions_head; NULL != t; t = t->next)
1129   {
1130     if (label == t->label)
1131     {
1132       new_s = t->to_state;
1133       break;
1134     }
1135   }
1136
1137   return new_s;
1138 }
1139
1140 /**
1141  * Remove all unreachable states from DFA 'a'. Unreachable states are those
1142  * states that are not reachable from the starting state.
1143  *
1144  * @param a DFA automaton
1145  */
1146 static void
1147 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1148 {
1149   struct GNUNET_REGEX_State *s;
1150   struct GNUNET_REGEX_State *s_next;
1151
1152   // 1. unmark all states
1153   for (s = a->states_head; NULL != s; s = s->next)
1154     s->marked = GNUNET_NO;
1155
1156   // 2. traverse dfa from start state and mark all visited states
1157   automaton_traverse (NULL, a, NULL);
1158
1159   // 3. delete all states that were not visited
1160   for (s = a->states_head; NULL != s; s = s_next)
1161   {
1162     s_next = s->next;
1163     if (GNUNET_NO == s->marked)
1164       automaton_remove_state (a, s);
1165   }
1166 }
1167
1168 /**
1169  * Remove all dead states from the DFA 'a'. Dead states are those states that do
1170  * not transition to any other state but themselfes.
1171  *
1172  * @param a DFA automaton
1173  */
1174 static void
1175 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1176 {
1177   struct GNUNET_REGEX_State *s;
1178   struct Transition *t;
1179   int dead;
1180
1181   GNUNET_assert (DFA == a->type);
1182
1183   for (s = a->states_head; NULL != s; s = s->next)
1184   {
1185     if (s->accepting)
1186       continue;
1187
1188     dead = 1;
1189     for (t = s->transitions_head; NULL != t; t = t->next)
1190     {
1191       if (NULL != t->to_state && t->to_state != s)
1192       {
1193         dead = 0;
1194         break;
1195       }
1196     }
1197
1198     if (0 == dead)
1199       continue;
1200
1201     // state s is dead, remove it
1202     automaton_remove_state (a, s);
1203   }
1204 }
1205
1206 /**
1207  * Merge all non distinguishable states in the DFA 'a'
1208  *
1209  * @param ctx context
1210  * @param a DFA automaton
1211  */
1212 static void
1213 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1214                                      struct GNUNET_REGEX_Automaton *a)
1215 {
1216   int i;
1217   int table[a->state_count][a->state_count];
1218   struct GNUNET_REGEX_State *s1;
1219   struct GNUNET_REGEX_State *s2;
1220   struct Transition *t1;
1221   struct Transition *t2;
1222   struct GNUNET_REGEX_State *s1_next;
1223   struct GNUNET_REGEX_State *s2_next;
1224   int change;
1225   int num_equal_edges;
1226
1227   for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1228        i++, s1 = s1->next)
1229   {
1230     s1->marked = i;
1231   }
1232
1233   // Mark all pairs of accepting/!accepting states
1234   for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1235   {
1236     for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1237     {
1238       table[s1->marked][s2->marked] = 0;
1239
1240       if ((s1->accepting && !s2->accepting) ||
1241           (!s1->accepting && s2->accepting))
1242       {
1243         table[s1->marked][s2->marked] = 1;
1244       }
1245     }
1246   }
1247
1248   // Find all equal states
1249   change = 1;
1250   while (0 != change)
1251   {
1252     change = 0;
1253     for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1254     {
1255       for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1256       {
1257         if (0 != table[s1->marked][s2->marked])
1258           continue;
1259
1260         num_equal_edges = 0;
1261         for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1262         {
1263           for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1264           {
1265             if (t1->label == t2->label)
1266             {
1267               num_equal_edges++;
1268               if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1269                   0 != table[t2->to_state->marked][t1->to_state->marked])
1270               {
1271                 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1272                 change = 1;
1273               }
1274             }
1275           }
1276         }
1277         if (num_equal_edges != s1->transition_count ||
1278             num_equal_edges != s2->transition_count)
1279         {
1280           // Make sure ALL edges of possible equal states are the same
1281           table[s1->marked][s2->marked] = -2;
1282         }
1283       }
1284     }
1285   }
1286
1287   // Merge states that are equal
1288   for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1289   {
1290     s1_next = s1->next;
1291     for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1292     {
1293       s2_next = s2->next;
1294       if (table[s1->marked][s2->marked] == 0)
1295         automaton_merge_states (ctx, a, s1, s2);
1296     }
1297   }
1298 }
1299
1300 /**
1301  * Minimize the given DFA 'a' by removing all unreachable states, removing all
1302  * dead states and merging all non distinguishable states
1303  *
1304  * @param ctx context
1305  * @param a DFA automaton
1306  */
1307 static void
1308 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1309               struct GNUNET_REGEX_Automaton *a)
1310 {
1311   if (NULL == a)
1312     return;
1313
1314   GNUNET_assert (DFA == a->type);
1315
1316   // 1. remove unreachable states
1317   dfa_remove_unreachable_states (a);
1318
1319   // 2. remove dead states
1320   dfa_remove_dead_states (a);
1321
1322   // 3. Merge nondistinguishable states
1323   dfa_merge_nondistinguishable_states (ctx, a);
1324 }
1325
1326 /**
1327  * Creates a new NFA fragment. Needs to be cleared using
1328  * automaton_fragment_clear.
1329  *
1330  * @param start starting state
1331  * @param end end state
1332  *
1333  * @return new NFA fragment
1334  */
1335 static struct GNUNET_REGEX_Automaton *
1336 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1337                      struct GNUNET_REGEX_State *end)
1338 {
1339   struct GNUNET_REGEX_Automaton *n;
1340
1341   n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1342
1343   n->type = NFA;
1344   n->start = NULL;
1345   n->end = NULL;
1346
1347   if (NULL == start && NULL == end)
1348     return n;
1349
1350   automaton_add_state (n, end);
1351   automaton_add_state (n, start);
1352
1353   n->start = start;
1354   n->end = end;
1355
1356   return n;
1357 }
1358
1359 /**
1360  * Adds a list of states to the given automaton 'n'.
1361  *
1362  * @param n automaton to which the states should be added
1363  * @param states_head head of the DLL of states
1364  * @param states_tail tail of the DLL of states
1365  */
1366 static void
1367 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1368                 struct GNUNET_REGEX_State *states_head,
1369                 struct GNUNET_REGEX_State *states_tail)
1370 {
1371   struct GNUNET_REGEX_State *s;
1372
1373   if (NULL == n || NULL == states_head)
1374   {
1375     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1376     return;
1377   }
1378
1379   if (NULL == n->states_head)
1380   {
1381     n->states_head = states_head;
1382     n->states_tail = states_tail;
1383     return;
1384   }
1385
1386   if (NULL != states_head)
1387   {
1388     n->states_tail->next = states_head;
1389     n->states_tail = states_tail;
1390   }
1391
1392   for (s = states_head; NULL != s; s = s->next)
1393     n->state_count++;
1394 }
1395
1396 /**
1397  * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1398  *
1399  * @param ctx context
1400  * @param accepting is it an accepting state or not
1401  *
1402  * @return new NFA state
1403  */
1404 static struct GNUNET_REGEX_State *
1405 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1406 {
1407   struct GNUNET_REGEX_State *s;
1408
1409   s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1410   s->id = ctx->state_id++;
1411   s->accepting = accepting;
1412   s->marked = 0;
1413   s->contained = 0;
1414   s->index = -1;
1415   s->lowlink = -1;
1416   s->scc_id = 0;
1417   s->name = NULL;
1418   GNUNET_asprintf (&s->name, "s%i", s->id);
1419
1420   return s;
1421 }
1422
1423 /**
1424  * Calculates the NFA closure set for the given state.
1425  *
1426  * @param nfa the NFA containing 's'
1427  * @param s starting point state
1428  * @param label transitioning label on which to base the closure on,
1429  *                pass 0 for epsilon transition
1430  *
1431  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1432  */
1433 static struct GNUNET_REGEX_StateSet *
1434 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1435                     struct GNUNET_REGEX_State *s, const char label)
1436 {
1437   struct GNUNET_REGEX_StateSet *cls;
1438   struct GNUNET_REGEX_StateSet *cls_check;
1439   struct GNUNET_REGEX_State *clsstate;
1440   struct GNUNET_REGEX_State *currentstate;
1441   struct Transition *ctran;
1442
1443   if (NULL == s)
1444     return NULL;
1445
1446   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1447   cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1448
1449   for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1450     clsstate->contained = 0;
1451
1452   // Add start state to closure only for epsilon closure
1453   if (0 == label)
1454     GNUNET_array_append (cls->states, cls->len, s);
1455
1456   GNUNET_array_append (cls_check->states, cls_check->len, s);
1457   while (cls_check->len > 0)
1458   {
1459     currentstate = cls_check->states[cls_check->len - 1];
1460     GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1461
1462     for (ctran = currentstate->transitions_head; NULL != ctran;
1463          ctran = ctran->next)
1464     {
1465       if (NULL != ctran->to_state && label == ctran->label)
1466       {
1467         clsstate = ctran->to_state;
1468
1469         if (NULL != clsstate && 0 == clsstate->contained)
1470         {
1471           GNUNET_array_append (cls->states, cls->len, clsstate);
1472           GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1473           clsstate->contained = 1;
1474         }
1475       }
1476     }
1477   }
1478   GNUNET_assert (0 == cls_check->len);
1479   GNUNET_free (cls_check);
1480
1481   if (cls->len > 1)
1482     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1483            state_compare);
1484
1485   return cls;
1486 }
1487
1488 /**
1489  * Calculates the closure set for the given set of states.
1490  *
1491  * @param nfa the NFA containing 's'
1492  * @param states list of states on which to base the closure on
1493  * @param label transitioning label for which to base the closure on,
1494  *                pass 0 for epsilon transition
1495  *
1496  * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1497  */
1498 static struct GNUNET_REGEX_StateSet *
1499 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1500                         struct GNUNET_REGEX_StateSet *states, const char label)
1501 {
1502   struct GNUNET_REGEX_State *s;
1503   struct GNUNET_REGEX_StateSet *sset;
1504   struct GNUNET_REGEX_StateSet *cls;
1505   int i;
1506   int j;
1507   int k;
1508   int contains;
1509
1510   if (NULL == states)
1511     return NULL;
1512
1513   cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1514
1515   for (i = 0; i < states->len; i++)
1516   {
1517     s = states->states[i];
1518     sset = nfa_closure_create (nfa, s, label);
1519
1520     for (j = 0; j < sset->len; j++)
1521     {
1522       contains = 0;
1523       for (k = 0; k < cls->len; k++)
1524       {
1525         if (sset->states[j]->id == cls->states[k]->id)
1526         {
1527           contains = 1;
1528           break;
1529         }
1530       }
1531       if (!contains)
1532         GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1533     }
1534     state_set_clear (sset);
1535   }
1536
1537   if (cls->len > 1)
1538     qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1539            state_compare);
1540
1541   return cls;
1542 }
1543
1544 /**
1545  * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1546  *
1547  * @param ctx context
1548  */
1549 static void
1550 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1551 {
1552   struct GNUNET_REGEX_Automaton *a;
1553   struct GNUNET_REGEX_Automaton *b;
1554   struct GNUNET_REGEX_Automaton *new;
1555
1556   b = ctx->stack_tail;
1557   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1558   a = ctx->stack_tail;
1559   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1560
1561   state_add_transition (ctx, a->end, 0, b->start);
1562   a->end->accepting = 0;
1563   b->end->accepting = 1;
1564
1565   new = nfa_fragment_create (NULL, NULL);
1566   nfa_add_states (new, a->states_head, a->states_tail);
1567   nfa_add_states (new, b->states_head, b->states_tail);
1568   new->start = a->start;
1569   new->end = b->end;
1570   automaton_fragment_clear (a);
1571   automaton_fragment_clear (b);
1572
1573   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1574 }
1575
1576 /**
1577  * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1578  *
1579  * @param ctx context
1580  */
1581 static void
1582 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1583 {
1584   struct GNUNET_REGEX_Automaton *a;
1585   struct GNUNET_REGEX_Automaton *new;
1586   struct GNUNET_REGEX_State *start;
1587   struct GNUNET_REGEX_State *end;
1588
1589   a = ctx->stack_tail;
1590   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1591
1592   if (NULL == a)
1593   {
1594     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1595                 "nfa_add_star_op failed, because there was no element on the stack");
1596     return;
1597   }
1598
1599   start = nfa_state_create (ctx, 0);
1600   end = nfa_state_create (ctx, 1);
1601
1602   state_add_transition (ctx, start, 0, a->start);
1603   state_add_transition (ctx, start, 0, end);
1604   state_add_transition (ctx, a->end, 0, a->start);
1605   state_add_transition (ctx, a->end, 0, end);
1606
1607   a->end->accepting = 0;
1608   end->accepting = 1;
1609
1610   new = nfa_fragment_create (start, end);
1611   nfa_add_states (new, a->states_head, a->states_tail);
1612   automaton_fragment_clear (a);
1613
1614   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1615 }
1616
1617 /**
1618  * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1619  *
1620  * @param ctx context
1621  */
1622 static void
1623 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1624 {
1625   struct GNUNET_REGEX_Automaton *a;
1626
1627   a = ctx->stack_tail;
1628   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1629
1630   state_add_transition (ctx, a->end, 0, a->start);
1631
1632   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1633 }
1634
1635 /**
1636  * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1637  *
1638  * @param ctx context
1639  */
1640 static void
1641 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1642 {
1643   struct GNUNET_REGEX_Automaton *a;
1644   struct GNUNET_REGEX_Automaton *new;
1645   struct GNUNET_REGEX_State *start;
1646   struct GNUNET_REGEX_State *end;
1647
1648   a = ctx->stack_tail;
1649   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1650
1651   if (NULL == a)
1652   {
1653     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1654                 "nfa_add_question_op failed, because there was no element on the stack");
1655     return;
1656   }
1657
1658   start = nfa_state_create (ctx, 0);
1659   end = nfa_state_create (ctx, 1);
1660
1661   state_add_transition (ctx, start, 0, a->start);
1662   state_add_transition (ctx, start, 0, end);
1663   state_add_transition (ctx, a->end, 0, end);
1664
1665   a->end->accepting = 0;
1666
1667   new = nfa_fragment_create (start, end);
1668   nfa_add_states (new, a->states_head, a->states_tail);
1669   automaton_fragment_clear (a);
1670
1671   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1672 }
1673
1674 /**
1675  * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1676  * alternates between a and b (a|b)
1677  *
1678  * @param ctx context
1679  */
1680 static void
1681 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1682 {
1683   struct GNUNET_REGEX_Automaton *a;
1684   struct GNUNET_REGEX_Automaton *b;
1685   struct GNUNET_REGEX_Automaton *new;
1686   struct GNUNET_REGEX_State *start;
1687   struct GNUNET_REGEX_State *end;
1688
1689   b = ctx->stack_tail;
1690   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1691   a = ctx->stack_tail;
1692   GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1693
1694   start = nfa_state_create (ctx, 0);
1695   end = nfa_state_create (ctx, 1);
1696   state_add_transition (ctx, start, 0, a->start);
1697   state_add_transition (ctx, start, 0, b->start);
1698
1699   state_add_transition (ctx, a->end, 0, end);
1700   state_add_transition (ctx, b->end, 0, end);
1701
1702   a->end->accepting = 0;
1703   b->end->accepting = 0;
1704   end->accepting = 1;
1705
1706   new = nfa_fragment_create (start, end);
1707   nfa_add_states (new, a->states_head, a->states_tail);
1708   nfa_add_states (new, b->states_head, b->states_tail);
1709   automaton_fragment_clear (a);
1710   automaton_fragment_clear (b);
1711
1712   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1713 }
1714
1715 /**
1716  * Adds a new nfa fragment to the stack
1717  *
1718  * @param ctx context
1719  * @param lit label for nfa transition
1720  */
1721 static void
1722 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1723 {
1724   struct GNUNET_REGEX_Automaton *n;
1725   struct GNUNET_REGEX_State *start;
1726   struct GNUNET_REGEX_State *end;
1727
1728   GNUNET_assert (NULL != ctx);
1729
1730   start = nfa_state_create (ctx, 0);
1731   end = nfa_state_create (ctx, 1);
1732   state_add_transition (ctx, start, lit, end);
1733   n = nfa_fragment_create (start, end);
1734   GNUNET_assert (NULL != n);
1735   GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1736 }
1737
1738 /**
1739  * Initialize a new context
1740  *
1741  * @param ctx context
1742  */
1743 static void
1744 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1745 {
1746   if (NULL == ctx)
1747   {
1748     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1749     return;
1750   }
1751   ctx->state_id = 0;
1752   ctx->transition_id = 0;
1753   ctx->scc_id = 0;
1754   ctx->stack_head = NULL;
1755   ctx->stack_tail = NULL;
1756 }
1757
1758 /**
1759  * Construct an NFA by parsing the regex string of length 'len'.
1760  *
1761  * @param regex regular expression string
1762  * @param len length of the string
1763  *
1764  * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1765  */
1766 struct GNUNET_REGEX_Automaton *
1767 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1768 {
1769   struct GNUNET_REGEX_Context ctx;
1770   struct GNUNET_REGEX_Automaton *nfa;
1771   const char *regexp;
1772   char *error_msg;
1773   unsigned int count;
1774   unsigned int altcount;
1775   unsigned int atomcount;
1776   unsigned int pcount;
1777   struct
1778   {
1779     int altcount;
1780     int atomcount;
1781   }     *p;
1782
1783   GNUNET_REGEX_context_init (&ctx);
1784
1785   regexp = regex;
1786   p = NULL;
1787   error_msg = NULL;
1788   altcount = 0;
1789   atomcount = 0;
1790   pcount = 0;
1791
1792   for (count = 0; count < len && *regexp; count++, regexp++)
1793   {
1794     switch (*regexp)
1795     {
1796     case '(':
1797       if (atomcount > 1)
1798       {
1799         --atomcount;
1800         nfa_add_concatenation (&ctx);
1801       }
1802       GNUNET_array_grow (p, pcount, pcount + 1);
1803       p[pcount - 1].altcount = altcount;
1804       p[pcount - 1].atomcount = atomcount;
1805       altcount = 0;
1806       atomcount = 0;
1807       break;
1808     case '|':
1809       if (0 == atomcount)
1810       {
1811         error_msg = "Cannot append '|' to nothing";
1812         goto error;
1813       }
1814       while (--atomcount > 0)
1815         nfa_add_concatenation (&ctx);
1816       altcount++;
1817       break;
1818     case ')':
1819       if (0 == pcount)
1820       {
1821         error_msg = "Missing opening '('";
1822         goto error;
1823       }
1824       if (0 == atomcount)
1825       {
1826         // Ignore this: "()"
1827         pcount--;
1828         altcount = p[pcount].altcount;
1829         atomcount = p[pcount].atomcount;
1830         break;
1831       }
1832       while (--atomcount > 0)
1833         nfa_add_concatenation (&ctx);
1834       for (; altcount > 0; altcount--)
1835         nfa_add_alternation (&ctx);
1836       pcount--;
1837       altcount = p[pcount].altcount;
1838       atomcount = p[pcount].atomcount;
1839       atomcount++;
1840       break;
1841     case '*':
1842       if (atomcount == 0)
1843       {
1844         error_msg = "Cannot append '*' to nothing";
1845         goto error;
1846       }
1847       nfa_add_star_op (&ctx);
1848       break;
1849     case '+':
1850       if (atomcount == 0)
1851       {
1852         error_msg = "Cannot append '+' to nothing";
1853         goto error;
1854       }
1855       nfa_add_plus_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_question_op (&ctx);
1864       break;
1865     case 92:                   /* escape: \ */
1866       regexp++;
1867       count++;
1868     default:
1869       if (atomcount > 1)
1870       {
1871         --atomcount;
1872         nfa_add_concatenation (&ctx);
1873       }
1874       nfa_add_label (&ctx, *regexp);
1875       atomcount++;
1876       break;
1877     }
1878   }
1879   if (0 != pcount)
1880   {
1881     error_msg = "Unbalanced parenthesis";
1882     goto error;
1883   }
1884   while (--atomcount > 0)
1885     nfa_add_concatenation (&ctx);
1886   for (; altcount > 0; altcount--)
1887     nfa_add_alternation (&ctx);
1888
1889   GNUNET_free_non_null (p);
1890
1891   nfa = ctx.stack_tail;
1892   GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1893
1894   if (NULL != ctx.stack_head)
1895   {
1896     error_msg = "Creating the NFA failed. NFA stack was not empty!";
1897     goto error;
1898   }
1899
1900   nfa->regex = GNUNET_strdup (regex);
1901
1902   return nfa;
1903
1904 error:
1905   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1906   if (NULL != error_msg)
1907     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1908
1909   GNUNET_free_non_null (p);
1910
1911   while (NULL != ctx.stack_tail)
1912   {
1913     GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1914     GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1915                                  ctx.stack_tail);
1916   }
1917   return NULL;
1918 }
1919
1920 /**
1921  * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1922  *
1923  * @param ctx context.
1924  * @param nfa NFA automaton.
1925  * @param dfa DFA automaton.
1926  * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1927  *                  for starting.
1928  */
1929 static void
1930 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1931                       struct GNUNET_REGEX_Automaton *nfa,
1932                       struct GNUNET_REGEX_Automaton *dfa,
1933                       struct GNUNET_REGEX_State *dfa_state)
1934 {
1935   struct Transition *ctran;
1936   struct GNUNET_REGEX_State *state_iter;
1937   struct GNUNET_REGEX_State *new_dfa_state;
1938   struct GNUNET_REGEX_State *state_contains;
1939   struct GNUNET_REGEX_StateSet *tmp;
1940   struct GNUNET_REGEX_StateSet *nfa_set;
1941
1942   for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1943   {
1944     if (0 == ctran->label || NULL != ctran->to_state)
1945       continue;
1946
1947     tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1948     nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1949     state_set_clear (tmp);
1950     new_dfa_state = dfa_state_create (ctx, nfa_set);
1951     state_contains = NULL;
1952     for (state_iter = dfa->states_head; NULL != state_iter;
1953          state_iter = state_iter->next)
1954     {
1955       if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1956         state_contains = state_iter;
1957     }
1958
1959     if (NULL == state_contains)
1960     {
1961       automaton_add_state (dfa, new_dfa_state);
1962       ctran->to_state = new_dfa_state;
1963       construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1964     }
1965     else
1966     {
1967       ctran->to_state = state_contains;
1968       automaton_destroy_state (new_dfa_state);
1969     }
1970   }
1971 }
1972
1973 /**
1974  * Construct DFA for the given 'regex' of length 'len'
1975  *
1976  * @param regex regular expression string
1977  * @param len length of the regular expression
1978  *
1979  * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1980  */
1981 struct GNUNET_REGEX_Automaton *
1982 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1983 {
1984   struct GNUNET_REGEX_Context ctx;
1985   struct GNUNET_REGEX_Automaton *dfa;
1986   struct GNUNET_REGEX_Automaton *nfa;
1987   struct GNUNET_REGEX_StateSet *nfa_set;
1988
1989   GNUNET_REGEX_context_init (&ctx);
1990
1991   // Create NFA
1992   nfa = GNUNET_REGEX_construct_nfa (regex, len);
1993
1994   if (NULL == nfa)
1995   {
1996     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1997                 "Could not create DFA, because NFA creation failed\n");
1998     return NULL;
1999   }
2000
2001   dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2002   dfa->type = DFA;
2003   dfa->regex = GNUNET_strdup (regex);
2004
2005   // Create DFA start state from epsilon closure
2006   nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2007   dfa->start = dfa_state_create (&ctx, nfa_set);
2008   automaton_add_state (dfa, dfa->start);
2009
2010   construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2011
2012   GNUNET_REGEX_automaton_destroy (nfa);
2013
2014   // Minimize DFA
2015   dfa_minimize (&ctx, dfa);
2016
2017   // Calculate SCCs
2018   scc_tarjan (&ctx, dfa);
2019
2020   // Create proofs for all states
2021   automaton_create_proofs (dfa);
2022
2023   return dfa;
2024 }
2025
2026 /**
2027  * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2028  * structure.
2029  *
2030  * @param a automaton to be destroyed
2031  */
2032 void
2033 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2034 {
2035   struct GNUNET_REGEX_State *s;
2036   struct GNUNET_REGEX_State *next_state;
2037
2038   if (NULL == a)
2039     return;
2040
2041   GNUNET_free (a->regex);
2042
2043   for (s = a->states_head; NULL != s;)
2044   {
2045     next_state = s->next;
2046     automaton_destroy_state (s);
2047     s = next_state;
2048   }
2049
2050   GNUNET_free (a);
2051 }
2052
2053 /**
2054  * Save the given automaton as a GraphViz dot file
2055  *
2056  * @param a the automaton to be saved
2057  * @param filename where to save the file
2058  */
2059 void
2060 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2061                                    const char *filename)
2062 {
2063   struct GNUNET_REGEX_State *s;
2064   struct Transition *ctran;
2065   char *s_acc = NULL;
2066   char *s_tran = NULL;
2067   char *start;
2068   char *end;
2069   FILE *p;
2070
2071   if (NULL == a)
2072   {
2073     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2074     return;
2075   }
2076
2077   if (NULL == filename || strlen (filename) < 1)
2078   {
2079     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2080     return;
2081   }
2082
2083   p = fopen (filename, "w");
2084
2085   if (NULL == p)
2086   {
2087     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2088                 filename);
2089     return;
2090   }
2091
2092   start = "digraph G {\nrankdir=LR\n";
2093   fwrite (start, strlen (start), 1, p);
2094
2095   for (s = a->states_head; NULL != s; s = s->next)
2096   {
2097     if (s->accepting)
2098     {
2099       GNUNET_asprintf (&s_acc,
2100                        "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2101                        s->name, s->scc_id);
2102     }
2103     else
2104     {
2105       GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2106                        s->scc_id);
2107     }
2108
2109     if (NULL == s_acc)
2110     {
2111       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2112                   s->name);
2113       return;
2114     }
2115     fwrite (s_acc, strlen (s_acc), 1, p);
2116     GNUNET_free (s_acc);
2117     s_acc = NULL;
2118
2119     for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2120     {
2121       if (NULL == ctran->to_state)
2122       {
2123         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2124                     "Transition from State %i has has no state for transitioning\n",
2125                     s->id);
2126         continue;
2127       }
2128
2129       if (ctran->label == 0)
2130       {
2131         GNUNET_asprintf (&s_tran,
2132                          "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2133                          s->name, ctran->to_state->name, s->scc_id);
2134       }
2135       else
2136       {
2137         GNUNET_asprintf (&s_tran,
2138                          "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2139                          s->name, ctran->to_state->name, ctran->label,
2140                          s->scc_id);
2141       }
2142
2143       if (NULL == s_tran)
2144       {
2145         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2146                     s->name);
2147         return;
2148       }
2149
2150       fwrite (s_tran, strlen (s_tran), 1, p);
2151       GNUNET_free (s_tran);
2152       s_tran = NULL;
2153     }
2154   }
2155
2156   end = "\n}\n";
2157   fwrite (end, strlen (end), 1, p);
2158   fclose (p);
2159 }
2160
2161 /**
2162  * Evaluates the given string using the given DFA automaton
2163  *
2164  * @param a automaton, type must be DFA
2165  * @param string string that should be evaluated
2166  *
2167  * @return 0 if string matches, non 0 otherwise
2168  */
2169 static int
2170 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2171 {
2172   const char *strp;
2173   struct GNUNET_REGEX_State *s;
2174
2175   if (DFA != a->type)
2176   {
2177     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2178                 "Tried to evaluate DFA, but NFA automaton given");
2179     return -1;
2180   }
2181
2182   s = a->start;
2183
2184   for (strp = string; NULL != strp && *strp; strp++)
2185   {
2186     s = dfa_move (s, *strp);
2187     if (NULL == s)
2188       break;
2189   }
2190
2191   if (NULL != s && s->accepting)
2192     return 0;
2193
2194   return 1;
2195 }
2196
2197 /**
2198  * Evaluates the given string using the given NFA automaton
2199  *
2200  * @param a automaton, type must be NFA
2201  * @param string string that should be evaluated
2202  *
2203  * @return 0 if string matches, non 0 otherwise
2204  */
2205 static int
2206 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2207 {
2208   const char *strp;
2209   struct GNUNET_REGEX_State *s;
2210   struct GNUNET_REGEX_StateSet *sset;
2211   struct GNUNET_REGEX_StateSet *new_sset;
2212   int i;
2213   int result;
2214
2215   if (NFA != a->type)
2216   {
2217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2218                 "Tried to evaluate NFA, but DFA automaton given");
2219     return -1;
2220   }
2221
2222   result = 1;
2223   strp = string;
2224   sset = nfa_closure_create (a, a->start, 0);
2225
2226   for (strp = string; NULL != strp && *strp; strp++)
2227   {
2228     new_sset = nfa_closure_set_create (a, sset, *strp);
2229     state_set_clear (sset);
2230     sset = nfa_closure_set_create (a, new_sset, 0);
2231     state_set_clear (new_sset);
2232   }
2233
2234   for (i = 0; i < sset->len; i++)
2235   {
2236     s = sset->states[i];
2237     if (NULL != s && s->accepting)
2238     {
2239       result = 0;
2240       break;
2241     }
2242   }
2243
2244   state_set_clear (sset);
2245   return result;
2246 }
2247
2248 /**
2249  * Evaluates the given 'string' against the given compiled regex
2250  *
2251  * @param a automaton
2252  * @param string string to check
2253  *
2254  * @return 0 if string matches, non 0 otherwise
2255  */
2256 int
2257 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2258 {
2259   int result;
2260
2261   switch (a->type)
2262   {
2263   case DFA:
2264     result = evaluate_dfa (a, string);
2265     break;
2266   case NFA:
2267     result = evaluate_nfa (a, string);
2268     break;
2269   default:
2270     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2271                 "Evaluating regex failed, automaton has no type!\n");
2272     result = GNUNET_SYSERR;
2273     break;
2274   }
2275
2276   return result;
2277 }
2278
2279 /**
2280  * Get the computed regex of the given automaton.
2281  * When constructing the automaton a proof is computed for each state,
2282  * consisting of the regular expression leading to this state. A complete
2283  * regex for the automaton can be computed by combining these proofs.
2284  * As of now this computed regex is only useful for testing.
2285  */
2286 const char *
2287 GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
2288 {
2289   if (NULL == a)
2290     return NULL;
2291
2292   return a->computed_regex;
2293 }
2294
2295 /**
2296  * Get the first key for the given 'input_string'. This hashes the first x bits
2297  * of the 'input_strings'.
2298  *
2299  * @param input_string string.
2300  * @param string_len length of the 'input_string'.
2301  * @param key pointer to where to write the hash code.
2302  *
2303  * @return number of bits of 'input_string' that have been consumed
2304  *         to construct the key
2305  */
2306 unsigned int
2307 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2308                             GNUNET_HashCode * key)
2309 {
2310   unsigned int size;
2311
2312   size = string_len < initial_bits ? string_len : initial_bits;
2313
2314   if (NULL == input_string)
2315   {
2316     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2317     return 0;
2318   }
2319
2320   GNUNET_CRYPTO_hash (input_string, size, key);
2321
2322   return size;
2323 }
2324
2325 /**
2326  * Check if the given 'proof' matches the given 'key'.
2327  *
2328  * @param proof partial regex
2329  * @param key hash
2330  *
2331  * @return GNUNET_OK if the proof is valid for the given key
2332  */
2333 int
2334 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2335 {
2336   return GNUNET_OK;
2337 }
2338
2339 /**
2340  * Iterate over all edges helper function starting from state 's', calling
2341  * iterator on for each edge.
2342  *
2343  * @param s state.
2344  * @param iterator iterator function called for each edge.
2345  * @param iterator_cls closure.
2346  */
2347 static void
2348 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2349               void *iterator_cls)
2350 {
2351   struct Transition *t;
2352   struct GNUNET_REGEX_Edge edges[s->transition_count];
2353   unsigned int num_edges;
2354
2355   if (GNUNET_YES != s->marked)
2356   {
2357     s->marked = GNUNET_YES;
2358
2359     num_edges = state_get_edges (s, edges);
2360
2361     iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2362
2363     for (t = s->transitions_head; NULL != t; t = t->next)
2364       iterate_edge (t->to_state, iterator, iterator_cls);
2365   }
2366 }
2367
2368 /**
2369  * Iterate over all edges starting from start state of automaton 'a'. Calling
2370  * iterator for each edge.
2371  *
2372  * @param a automaton.
2373  * @param iterator iterator called for each edge.
2374  * @param iterator_cls closure.
2375  */
2376 void
2377 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2378                                 GNUNET_REGEX_KeyIterator iterator,
2379                                 void *iterator_cls)
2380 {
2381   struct GNUNET_REGEX_State *s;
2382
2383   for (s = a->states_head; NULL != s; s = s->next)
2384     s->marked = GNUNET_NO;
2385
2386   iterate_edge (a->start, iterator, iterator_cls);
2387 }