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