2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file src/regex/regex.c
22 * @brief library to create automatons from regular expressions
23 * @author Maximilian Szengel
26 #include "gnunet_container_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_regex_lib.h"
31 #define initial_bits 10
34 * Context that contains an id counter for states and transitions
35 * as well as a DLL of automatons used as a stack for NFA construction.
37 struct GNUNET_REGEX_Context
42 unsigned int state_id;
45 * Unique transition id.
47 unsigned int transition_id;
50 * Unique SCC (Strongly Connected Component) id.
55 * DLL of GNUNET_REGEX_Automaton's used as a stack.
57 struct GNUNET_REGEX_Automaton *stack_head;
60 * DLL of GNUNET_REGEX_Automaton's used as a stack.
62 struct GNUNET_REGEX_Automaton *stack_tail;
66 * Type of an automaton.
68 enum GNUNET_REGEX_automaton_type
75 * Automaton representation.
77 struct GNUNET_REGEX_Automaton
80 * This is a linked list.
82 struct GNUNET_REGEX_Automaton *prev;
85 * This is a linked list.
87 struct GNUNET_REGEX_Automaton *next;
90 * First state of the automaton. This is mainly
91 * used for constructing an NFA, where each NFA
92 * itself consists of one or more NFAs linked
95 struct GNUNET_REGEX_State *start;
98 * End state of the automaton.
100 struct GNUNET_REGEX_State *end;
103 * Number of states in the automaton.
105 unsigned int state_count;
110 struct GNUNET_REGEX_State *states_head;
115 struct GNUNET_REGEX_State *states_tail;
118 * Type of the automaton.
120 enum GNUNET_REGEX_automaton_type type;
124 * A state. Can be used in DFA and NFA automatons.
126 struct GNUNET_REGEX_State
129 * This is a linked list.
131 struct GNUNET_REGEX_State *prev;
134 * This is a linked list.
136 struct GNUNET_REGEX_State *next;
144 * If this is an accepting state or not.
149 * Marking of the state. This is used for marking all visited
150 * states when traversing all states of an automaton and for
151 * cases where the state id cannot be used (dfa minimization).
156 * Marking the state as contained. This is used for checking,
157 * if the state is contained in a set in constant time
162 * Marking the state as part of an SCC (Strongly Connected Component).
163 * All states with the same scc_id are part of the same SCC.
168 * Used for SCC detection.
173 * Used for SCC detection.
178 * Human readable name of the automaton. Used for debugging
179 * and graph creation.
183 GNUNET_HashCode hash;
186 * Number of transitions from this state to other states.
188 unsigned int transition_count;
191 * DLL of transitions.
193 struct Transition *transitions_head;
196 * DLL of transitions.
198 struct Transition *transitions_tail;
201 * Set of states on which this state is based on. Used when
202 * creating a DFA out of several NFA states.
204 struct GNUNET_REGEX_StateSet *nfa_set;
208 * Transition between two states. Each state can have 0-n transitions.
209 * If label is 0, this is considered to be an epsilon transition.
214 * This is a linked list.
216 struct Transition *prev;
219 * This is a linked list.
221 struct Transition *next;
224 * Unique id of this transition.
229 * label for this transition. This is basically the edge label for
235 * State to which this transition leads.
237 struct GNUNET_REGEX_State *to_state;
240 * State from which this transition origins.
242 struct GNUNET_REGEX_State *from_state;
248 struct GNUNET_REGEX_StateSet
253 struct GNUNET_REGEX_State **states;
256 * Length of the 'states' array.
262 debug_print_state (struct GNUNET_REGEX_State *s)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "State %i: %s marked: %i accepting: %i scc_id: %i\n", s->id,
266 s->name, s->marked, s->accepting, s->scc_id);
270 debug_print_states (struct GNUNET_REGEX_StateSet *sset)
272 struct GNUNET_REGEX_State *s;
275 for (i = 0; i < sset->len; i++)
278 debug_print_state (s);
283 debug_print_transitions (struct GNUNET_REGEX_State *s)
285 struct Transition *t;
289 for (t = s->transitions_head; NULL != t; t = t->next)
296 if (NULL == t->to_state)
299 state = t->to_state->name;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
307 * Recursive function doing DFS with 'v' as a start, detecting all SCCs
308 * inside the subgraph reachable from 'v'. Used with scc_tarjan function
309 * to detect all SCCs inside an automaton.
312 * @param v start vertex
313 * @param index current index
314 * @param stack stack for saving all SCCs
315 * @param stack_size current size of the stack
318 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
319 struct GNUNET_REGEX_State *v, int *index,
320 struct GNUNET_REGEX_State **stack,
321 unsigned int *stack_size)
323 struct GNUNET_REGEX_State *w;
324 struct Transition *t;
329 stack[(*stack_size)++] = v;
332 for (t = v->transitions_head; NULL != t; t = t->next)
335 if (NULL != w && w->index < 0)
337 scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
338 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
340 else if (0 != w->contained)
341 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
344 if (v->lowlink == v->index)
346 w = stack[--(*stack_size)];
354 w->scc_id = ctx->scc_id;
355 w = stack[--(*stack_size)];
358 w->scc_id = ctx->scc_id;
364 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
365 * SCCs will be marked using the scc_id on each state.
371 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
375 struct GNUNET_REGEX_State *v;
376 struct GNUNET_REGEX_State *stack[a->state_count];
377 unsigned int stack_size;
379 for (v = a->states_head; NULL != v; v = v->next)
389 for (i = 0, v = a->states_head; NULL != v; v = v->next)
392 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
397 * Compare two states. Used for sorting.
399 * @param a first state
400 * @param b second state
402 * @return an integer less than, equal to, or greater than zero
403 * if the first argument is considered to be respectively
404 * less than, equal to, or greater than the second.
407 state_compare (const void *a, const void *b)
409 struct GNUNET_REGEX_State **s1;
410 struct GNUNET_REGEX_State **s2;
412 s1 = (struct GNUNET_REGEX_State **) a;
413 s2 = (struct GNUNET_REGEX_State **) b;
415 return (*s1)->id - (*s2)->id;
419 * Compare to state sets by comparing the id's of the states that are
420 * contained in each set. Both sets are expected to be sorted by id!
422 * @param sset1 first state set
423 * @param sset2 second state set
425 * @return an integer less than, equal to, or greater than zero
426 * if the first argument is considered to be respectively
427 * less than, equal to, or greater than the second.
430 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
431 struct GNUNET_REGEX_StateSet *sset2)
436 if (NULL == sset1 || NULL == sset2)
439 result = sset1->len - sset2->len;
441 for (i = 0; i < sset1->len; i++)
446 result = state_compare (&sset1->states[i], &sset2->states[i]);
452 * Clears the given StateSet 'set'
454 * @param set set to be cleared
457 state_set_clear (struct GNUNET_REGEX_StateSet *set)
461 if (NULL != set->states)
462 GNUNET_free (set->states);
468 * Adds a transition from one state to another on 'label'
471 * @param from_state starting state for the transition
472 * @param label transition label
473 * @param to_state state to where the transition should point to
476 state_add_transition (struct GNUNET_REGEX_Context *ctx,
477 struct GNUNET_REGEX_State *from_state, const char label,
478 struct GNUNET_REGEX_State *to_state)
480 struct Transition *t;
482 if (NULL == from_state)
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
488 t = GNUNET_malloc (sizeof (struct Transition));
490 t->id = ctx->transition_id++;
492 t->to_state = to_state;
493 t->from_state = from_state;
495 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
496 from_state->transitions_tail, t);
500 * Clears an automaton fragment. Does not destroy the states inside
503 * @param a automaton to be cleared
506 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
513 a->states_head = NULL;
514 a->states_tail = NULL;
520 * Frees the memory used by State 's'
522 * @param s state that should be destroyed
525 automaton_destroy_state (struct GNUNET_REGEX_State *s)
527 struct Transition *t;
528 struct Transition *next_t;
534 GNUNET_free (s->name);
536 for (t = s->transitions_head; NULL != t;)
539 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
544 state_set_clear (s->nfa_set);
550 * Remove a state from the given automaton 'a'. Always use this function
551 * when altering the states of an automaton. Will also remove all transitions
552 * leading to this state, before destroying it.
555 * @param s state to remove
558 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
559 struct GNUNET_REGEX_State *s)
561 struct GNUNET_REGEX_State *ss;
562 struct GNUNET_REGEX_State *s_check;
563 struct Transition *t_check;
565 if (NULL == a || NULL == s)
570 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
573 // remove all transitions leading to this state
574 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
576 for (t_check = s_check->transitions_head; NULL != t_check;
577 t_check = t_check->next)
579 if (t_check->to_state == ss)
581 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
582 s_check->transitions_tail, t_check);
583 s_check->transition_count--;
588 automaton_destroy_state (ss);
592 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
596 * @param s1 first state
597 * @param s2 second state, will be destroyed
600 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
601 struct GNUNET_REGEX_Automaton *a,
602 struct GNUNET_REGEX_State *s1,
603 struct GNUNET_REGEX_State *s2)
605 struct GNUNET_REGEX_State *s_check;
606 struct Transition *t_check;
607 struct Transition *t;
610 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
612 // 1. Make all transitions pointing to s2 point to s1
613 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
615 for (t_check = s_check->transitions_head; NULL != t_check;
616 t_check = t_check->next)
618 if (s_check != s1 && s2 == t_check->to_state)
619 t_check->to_state = s1;
623 // 2. Add all transitions from s2 to sX to s1
624 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
626 for (t = s1->transitions_head; NULL != t; t = t->next)
628 if (t_check->label != t->label && NULL != t_check->to_state &&
629 t_check->to_state != t->to_state && t_check->to_state != s2)
631 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
636 // 3. Rename s1 to {s1,s2}
637 new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
638 strncat (new_name, s1->name, strlen (s1->name));
639 strncat (new_name, s2->name, strlen (s2->name));
640 if (NULL != s1->name)
641 GNUNET_free (s1->name);
646 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
648 automaton_destroy_state (s_check);
652 * Add a state to the automaton 'a', always use this function to
653 * alter the states DLL of the automaton.
655 * @param a automaton to add the state to
656 * @param s state that should be added
659 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
660 struct GNUNET_REGEX_State *s)
662 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
666 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
667 struct GNUNET_REGEX_State * s);
670 * Traverses all states that are reachable from state 's'. Expects
671 * the states to be unmarked (s->marked == GNUNET_NO). Performs
672 * 'action' on each visited state.
674 * @param cls closure.
675 * @param s start state.
676 * @param action action to be performed on each state.
679 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
680 GNUNET_REGEX_traverse_action action)
682 struct Transition *t;
684 if (GNUNET_NO == s->marked)
686 s->marked = GNUNET_YES;
691 for (t = s->transitions_head; NULL != t; t = t->next)
692 automaton_state_traverse (cls, t->to_state, action);
697 * Traverses the given automaton from it's start state, visiting all
698 * reachable states and calling 'action' on each one of them.
700 * @param cls closure.
701 * @param a automaton.
702 * @param action action to be performed on each state.
705 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
706 GNUNET_REGEX_traverse_action action)
708 struct GNUNET_REGEX_State *s;
710 for (s = a->start; NULL != s; s = s->next)
711 s->marked = GNUNET_NO;
713 automaton_state_traverse (cls, a->start, action);
717 * Creates a new DFA state based on a set of NFA states. Needs to be freed
718 * using automaton_destroy_state.
721 * @param nfa_states set of NFA states on which the DFA should be based on
723 * @return new DFA state
725 static struct GNUNET_REGEX_State *
726 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
727 struct GNUNET_REGEX_StateSet *nfa_states)
729 struct GNUNET_REGEX_State *s;
732 struct GNUNET_REGEX_State *cstate;
733 struct Transition *ctran;
735 struct Transition *t;
738 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
739 s->id = ctx->state_id++;
748 if (NULL == nfa_states)
750 GNUNET_asprintf (&s->name, "s%i", s->id);
754 s->nfa_set = nfa_states;
756 if (nfa_states->len < 1)
759 // Create a name based on 'sset'
760 s->name = GNUNET_malloc (sizeof (char) * 2);
761 strcat (s->name, "{");
764 for (i = 0; i < nfa_states->len; i++)
766 cstate = nfa_states->states[i];
767 GNUNET_asprintf (&name, "%i,", cstate->id);
771 len = strlen (s->name) + strlen (name) + 1;
772 s->name = GNUNET_realloc (s->name, len);
773 strcat (s->name, name);
778 // Add a transition for each distinct label to NULL state
779 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
781 if (0 != ctran->label)
785 for (t = s->transitions_head; NULL != t; t = t->next)
787 if (t->label == ctran->label)
795 state_add_transition (ctx, s, ctran->label, NULL);
799 // If the nfa_states contain an accepting state, the new dfa state is also accepting
800 if (cstate->accepting)
804 s->name[strlen (s->name) - 1] = '}';
810 * Move from the given state 's' to the next state on
813 * @param s starting state
814 * @param label edge label to follow
816 * @return new state or NULL, if transition on label not possible
818 static struct GNUNET_REGEX_State *
819 dfa_move (struct GNUNET_REGEX_State *s, const char label)
821 struct Transition *t;
822 struct GNUNET_REGEX_State *new_s;
829 for (t = s->transitions_head; NULL != t; t = t->next)
831 if (label == t->label)
843 * Remove all unreachable states from DFA 'a'. Unreachable states
844 * are those states that are not reachable from the starting state.
846 * @param a DFA automaton
849 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
851 struct GNUNET_REGEX_State *s;
852 struct GNUNET_REGEX_State *s_next;
854 // 1. unmark all states
855 for (s = a->states_head; NULL != s; s = s->next)
856 s->marked = GNUNET_NO;
858 // 2. traverse dfa from start state and mark all visited states
859 automaton_traverse (NULL, a, NULL);
861 // 3. delete all states that were not visited
862 for (s = a->states_head; NULL != s; s = s_next)
865 if (GNUNET_NO == s->marked)
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
868 automaton_remove_state (a, s);
874 * Remove all dead states from the DFA 'a'. Dead states are those
875 * states that do not transition to any other state but themselfes.
877 * @param a DFA automaton
880 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
882 struct GNUNET_REGEX_State *s;
883 struct Transition *t;
886 GNUNET_assert (DFA == a->type);
888 for (s = a->states_head; NULL != s; s = s->next)
894 for (t = s->transitions_head; NULL != t; t = t->next)
896 if (NULL != t->to_state && t->to_state != s)
906 // state s is dead, remove it
907 automaton_remove_state (a, s);
912 * Merge all non distinguishable states in the DFA 'a'
915 * @param a DFA automaton
918 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
919 struct GNUNET_REGEX_Automaton *a)
922 int table[a->state_count][a->state_count];
923 struct GNUNET_REGEX_State *s1;
924 struct GNUNET_REGEX_State *s2;
925 struct Transition *t1;
926 struct Transition *t2;
930 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
934 // Mark all pairs of accepting/!accepting states
935 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
937 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
939 if ((s1->accepting && !s2->accepting) ||
940 (!s1->accepting && s2->accepting))
942 table[s1->marked][s2->marked] = 1;
945 table[s1->marked][s2->marked] = 0;
952 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
954 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
956 if (0 != table[s1->marked][s2->marked])
959 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
961 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
963 if (t1->label == t2->label && t1->to_state == t2->to_state &&
964 (0 != table[t1->to_state->marked][t2->to_state->marked] ||
965 0 != table[t2->to_state->marked][t1->to_state->marked]))
967 table[s1->marked][s2->marked] = t1->label;
970 else if (t1->label != t2->label && t1->to_state != t2->to_state)
972 table[s1->marked][s2->marked] = -1;
981 struct GNUNET_REGEX_State *s2_next;
983 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
985 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
988 if (s1 != s2 && table[s1->marked][s2->marked] == 0)
989 automaton_merge_states (ctx, a, s1, s2);
995 * Minimize the given DFA 'a' by removing all unreachable states,
996 * removing all dead states and merging all non distinguishable states
999 * @param a DFA automaton
1002 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1003 struct GNUNET_REGEX_Automaton *a)
1008 GNUNET_assert (DFA == a->type);
1010 // 1. remove unreachable states
1011 dfa_remove_unreachable_states (a);
1013 // 2. remove dead states
1014 dfa_remove_dead_states (a);
1016 // 3. Merge nondistinguishable states
1017 dfa_merge_nondistinguishable_states (ctx, a);
1021 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
1023 * @param start starting state
1024 * @param end end state
1026 * @return new NFA fragment
1028 static struct GNUNET_REGEX_Automaton *
1029 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1030 struct GNUNET_REGEX_State *end)
1032 struct GNUNET_REGEX_Automaton *n;
1034 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1040 if (NULL == start && NULL == end)
1043 automaton_add_state (n, end);
1044 automaton_add_state (n, start);
1053 * Adds a list of states to the given automaton 'n'.
1055 * @param n automaton to which the states should be added
1056 * @param states_head head of the DLL of states
1057 * @param states_tail tail of the DLL of states
1060 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1061 struct GNUNET_REGEX_State *states_head,
1062 struct GNUNET_REGEX_State *states_tail)
1064 struct GNUNET_REGEX_State *s;
1066 if (NULL == n || NULL == states_head)
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1072 if (NULL == n->states_head)
1074 n->states_head = states_head;
1075 n->states_tail = states_tail;
1079 if (NULL != states_head)
1081 n->states_tail->next = states_head;
1082 n->states_tail = states_tail;
1085 for (s = states_head; NULL != s; s = s->next)
1090 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1092 * @param ctx context
1093 * @param accepting is it an accepting state or not
1095 * @return new NFA state
1097 static struct GNUNET_REGEX_State *
1098 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1100 struct GNUNET_REGEX_State *s;
1102 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1103 s->id = ctx->state_id++;
1104 s->accepting = accepting;
1111 GNUNET_asprintf (&s->name, "s%i", s->id);
1117 * Calculates the NFA closure set for the given state.
1119 * @param nfa the NFA containing 's'
1120 * @param s starting point state
1121 * @param label transitioning label on which to base the closure on,
1122 * pass 0 for epsilon transition
1124 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1126 static struct GNUNET_REGEX_StateSet *
1127 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1128 struct GNUNET_REGEX_State *s, const char label)
1130 struct GNUNET_REGEX_StateSet *cls;
1131 struct GNUNET_REGEX_StateSet *cls_check;
1132 struct GNUNET_REGEX_State *clsstate;
1133 struct GNUNET_REGEX_State *currentstate;
1134 struct Transition *ctran;
1139 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1140 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1142 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1143 clsstate->contained = 0;
1145 // Add start state to closure only for epsilon closure
1147 GNUNET_array_append (cls->states, cls->len, s);
1149 GNUNET_array_append (cls_check->states, cls_check->len, s);
1150 while (cls_check->len > 0)
1152 currentstate = cls_check->states[cls_check->len - 1];
1153 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1155 for (ctran = currentstate->transitions_head; NULL != ctran;
1156 ctran = ctran->next)
1158 if (NULL != ctran->to_state && label == ctran->label)
1160 clsstate = ctran->to_state;
1162 if (NULL != clsstate && 0 == clsstate->contained)
1164 GNUNET_array_append (cls->states, cls->len, clsstate);
1165 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1166 clsstate->contained = 1;
1171 GNUNET_assert (0 == cls_check->len);
1172 GNUNET_free (cls_check);
1175 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1182 * Calculates the closure set for the given set of states.
1184 * @param nfa the NFA containing 's'
1185 * @param states list of states on which to base the closure on
1186 * @param label transitioning label for which to base the closure on,
1187 * pass 0 for epsilon transition
1189 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1191 static struct GNUNET_REGEX_StateSet *
1192 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1193 struct GNUNET_REGEX_StateSet *states, const char label)
1195 struct GNUNET_REGEX_State *s;
1196 struct GNUNET_REGEX_StateSet *sset;
1197 struct GNUNET_REGEX_StateSet *cls;
1206 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1208 for (i = 0; i < states->len; i++)
1210 s = states->states[i];
1211 sset = nfa_closure_create (nfa, s, label);
1213 for (j = 0; j < sset->len; j++)
1216 for (k = 0; k < cls->len; k++)
1218 if (sset->states[j]->id == cls->states[k]->id)
1225 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1227 state_set_clear (sset);
1231 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1238 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1240 * @param ctx context
1243 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1245 struct GNUNET_REGEX_Automaton *a;
1246 struct GNUNET_REGEX_Automaton *b;
1247 struct GNUNET_REGEX_Automaton *new;
1249 b = ctx->stack_tail;
1250 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1251 a = ctx->stack_tail;
1252 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1254 state_add_transition (ctx, a->end, 0, b->start);
1255 a->end->accepting = 0;
1256 b->end->accepting = 1;
1258 new = nfa_fragment_create (NULL, NULL);
1259 nfa_add_states (new, a->states_head, a->states_tail);
1260 nfa_add_states (new, b->states_head, b->states_tail);
1261 new->start = a->start;
1263 automaton_fragment_clear (a);
1264 automaton_fragment_clear (b);
1266 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1270 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1272 * @param ctx context
1275 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1277 struct GNUNET_REGEX_Automaton *a;
1278 struct GNUNET_REGEX_Automaton *new;
1279 struct GNUNET_REGEX_State *start;
1280 struct GNUNET_REGEX_State *end;
1282 a = ctx->stack_tail;
1283 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1288 "nfa_add_star_op failed, because there was no element on the stack");
1292 start = nfa_state_create (ctx, 0);
1293 end = nfa_state_create (ctx, 1);
1295 state_add_transition (ctx, start, 0, a->start);
1296 state_add_transition (ctx, start, 0, end);
1297 state_add_transition (ctx, a->end, 0, a->start);
1298 state_add_transition (ctx, a->end, 0, end);
1300 a->end->accepting = 0;
1303 new = nfa_fragment_create (start, end);
1304 nfa_add_states (new, a->states_head, a->states_tail);
1305 automaton_fragment_clear (a);
1307 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1311 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1313 * @param ctx context
1316 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1318 struct GNUNET_REGEX_Automaton *a;
1320 a = ctx->stack_tail;
1321 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1323 state_add_transition (ctx, a->end, 0, a->start);
1325 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1329 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1331 * @param ctx context
1334 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1336 struct GNUNET_REGEX_Automaton *a;
1337 struct GNUNET_REGEX_Automaton *new;
1338 struct GNUNET_REGEX_State *start;
1339 struct GNUNET_REGEX_State *end;
1341 a = ctx->stack_tail;
1342 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1347 "nfa_add_question_op failed, because there was no element on the stack");
1351 start = nfa_state_create (ctx, 0);
1352 end = nfa_state_create (ctx, 1);
1354 state_add_transition (ctx, start, 0, a->start);
1355 state_add_transition (ctx, start, 0, end);
1356 state_add_transition (ctx, a->end, 0, end);
1358 a->end->accepting = 0;
1360 new = nfa_fragment_create (start, end);
1361 nfa_add_states (new, a->states_head, a->states_tail);
1362 automaton_fragment_clear (a);
1364 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1368 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1369 * that alternates between a and b (a|b)
1371 * @param ctx context
1374 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1376 struct GNUNET_REGEX_Automaton *a;
1377 struct GNUNET_REGEX_Automaton *b;
1378 struct GNUNET_REGEX_Automaton *new;
1379 struct GNUNET_REGEX_State *start;
1380 struct GNUNET_REGEX_State *end;
1382 b = ctx->stack_tail;
1383 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1384 a = ctx->stack_tail;
1385 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1387 start = nfa_state_create (ctx, 0);
1388 end = nfa_state_create (ctx, 1);
1389 state_add_transition (ctx, start, 0, a->start);
1390 state_add_transition (ctx, start, 0, b->start);
1392 state_add_transition (ctx, a->end, 0, end);
1393 state_add_transition (ctx, b->end, 0, end);
1395 a->end->accepting = 0;
1396 b->end->accepting = 0;
1399 new = nfa_fragment_create (start, end);
1400 nfa_add_states (new, a->states_head, a->states_tail);
1401 nfa_add_states (new, b->states_head, b->states_tail);
1402 automaton_fragment_clear (a);
1403 automaton_fragment_clear (b);
1405 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1409 * Adds a new nfa fragment to the stack
1411 * @param ctx context
1412 * @param lit label for nfa transition
1415 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1417 struct GNUNET_REGEX_Automaton *n;
1418 struct GNUNET_REGEX_State *start;
1419 struct GNUNET_REGEX_State *end;
1421 GNUNET_assert (NULL != ctx);
1423 start = nfa_state_create (ctx, 0);
1424 end = nfa_state_create (ctx, 1);
1425 state_add_transition (ctx, start, lit, end);
1426 n = nfa_fragment_create (start, end);
1427 GNUNET_assert (NULL != n);
1428 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1432 * Initialize a new context
1434 * @param ctx context
1437 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1445 ctx->transition_id = 0;
1447 ctx->stack_head = NULL;
1448 ctx->stack_tail = NULL;
1452 * Construct an NFA by parsing the regex string of length 'len'.
1454 * @param regex regular expression string
1455 * @param len length of the string
1457 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1459 struct GNUNET_REGEX_Automaton *
1460 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1462 struct GNUNET_REGEX_Context ctx;
1463 struct GNUNET_REGEX_Automaton *nfa;
1467 unsigned int altcount;
1468 unsigned int atomcount;
1469 unsigned int pcount;
1476 GNUNET_REGEX_context_init (&ctx);
1485 for (count = 0; count < len && *regexp; count++, regexp++)
1493 nfa_add_concatenation (&ctx);
1495 GNUNET_array_grow (p, pcount, pcount + 1);
1496 p[pcount - 1].altcount = altcount;
1497 p[pcount - 1].atomcount = atomcount;
1504 error_msg = "Cannot append '|' to nothing";
1507 while (--atomcount > 0)
1508 nfa_add_concatenation (&ctx);
1514 error_msg = "Missing opening '('";
1519 // Ignore this: "()"
1521 altcount = p[pcount].altcount;
1522 atomcount = p[pcount].atomcount;
1525 while (--atomcount > 0)
1526 nfa_add_concatenation (&ctx);
1527 for (; altcount > 0; altcount--)
1528 nfa_add_alternation (&ctx);
1530 altcount = p[pcount].altcount;
1531 atomcount = p[pcount].atomcount;
1537 error_msg = "Cannot append '+' to nothing";
1540 nfa_add_star_op (&ctx);
1545 error_msg = "Cannot append '+' to nothing";
1548 nfa_add_plus_op (&ctx);
1553 error_msg = "Cannot append '?' to nothing";
1556 nfa_add_question_op (&ctx);
1558 case 92: /* escape: \ */
1565 nfa_add_concatenation (&ctx);
1567 nfa_add_label (&ctx, *regexp);
1574 error_msg = "Unbalanced parenthesis";
1577 while (--atomcount > 0)
1578 nfa_add_concatenation (&ctx);
1579 for (; altcount > 0; altcount--)
1580 nfa_add_alternation (&ctx);
1585 nfa = ctx.stack_tail;
1586 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1589 if (NULL != ctx.stack_head)
1591 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1599 if (NULL != error_msg)
1600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1603 while (NULL != ctx.stack_tail)
1605 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1606 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1613 * Construct DFA for the given 'regex' of length 'len'
1615 * @param regex regular expression string
1616 * @param len length of the regular expression
1618 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1620 struct GNUNET_REGEX_Automaton *
1621 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1623 struct GNUNET_REGEX_Context ctx;
1624 struct GNUNET_REGEX_Automaton *dfa;
1625 struct GNUNET_REGEX_Automaton *nfa;
1626 struct GNUNET_REGEX_StateSet *tmp;
1627 struct GNUNET_REGEX_StateSet *nfa_set;
1628 struct GNUNET_REGEX_StateSet *dfa_stack;
1629 struct Transition *ctran;
1630 struct GNUNET_REGEX_State *dfa_state;
1631 struct GNUNET_REGEX_State *new_dfa_state;
1632 struct GNUNET_REGEX_State *state_contains;
1633 struct GNUNET_REGEX_State *state_iter;
1635 GNUNET_REGEX_context_init (&ctx);
1638 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1642 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1643 "Could not create DFA, because NFA creation failed\n");
1647 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1650 // Create DFA start state from epsilon closure
1651 dfa_stack = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1652 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1653 dfa->start = dfa_state_create (&ctx, nfa_set);
1654 automaton_add_state (dfa, dfa->start);
1655 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1657 // Create dfa states by combining nfa states
1658 while (dfa_stack->len > 0)
1660 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1661 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1663 for (ctran = dfa_state->transitions_head; NULL != ctran;
1664 ctran = ctran->next)
1666 if (0 != ctran->label && NULL == ctran->to_state)
1668 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1669 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1670 state_set_clear (tmp);
1671 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1672 state_contains = NULL;
1673 for (state_iter = dfa->states_head; NULL != state_iter;
1674 state_iter = state_iter->next)
1677 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1678 state_contains = state_iter;
1681 if (NULL == state_contains)
1683 automaton_add_state (dfa, new_dfa_state);
1684 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1686 ctran->to_state = new_dfa_state;
1690 ctran->to_state = state_contains;
1691 automaton_destroy_state (new_dfa_state);
1697 GNUNET_free (dfa_stack);
1698 GNUNET_REGEX_automaton_destroy (nfa);
1700 dfa_minimize (&ctx, dfa);
1701 scc_tarjan (&ctx, dfa);
1707 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1710 * @param a automaton to be destroyed
1713 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1715 struct GNUNET_REGEX_State *s;
1716 struct GNUNET_REGEX_State *next_state;
1721 for (s = a->states_head; NULL != s;)
1723 next_state = s->next;
1724 automaton_destroy_state (s);
1732 * Save the given automaton as a GraphViz dot file
1734 * @param a the automaton to be saved
1735 * @param filename where to save the file
1738 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1739 const char *filename)
1741 struct GNUNET_REGEX_State *s;
1742 struct Transition *ctran;
1744 char *s_tran = NULL;
1751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1755 if (NULL == filename || strlen (filename) < 1)
1757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1761 p = fopen (filename, "w");
1765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1770 start = "digraph G {\nrankdir=LR\n";
1771 fwrite (start, strlen (start), 1, p);
1773 for (s = a->states_head; NULL != s; s = s->next)
1777 GNUNET_asprintf (&s_acc,
1778 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1779 s->name, s->scc_id);
1780 fwrite (s_acc, strlen (s_acc), 1, p);
1781 GNUNET_free (s_acc);
1785 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
1787 fwrite (s_acc, strlen (s_acc), 1, p);
1788 GNUNET_free (s_acc);
1793 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1795 if (NULL == ctran->to_state)
1797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1798 "Transition from State %i has has no state for transitioning\n",
1803 if (ctran->label == 0)
1805 GNUNET_asprintf (&s_tran,
1806 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
1807 s->name, ctran->to_state->name, s->scc_id);
1811 GNUNET_asprintf (&s_tran,
1812 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
1813 s->name, ctran->to_state->name, ctran->label,
1817 fwrite (s_tran, strlen (s_tran), 1, p);
1818 GNUNET_free (s_tran);
1823 fwrite (end, strlen (end), 1, p);
1828 * Evaluates the given string using the given DFA automaton
1830 * @param a automaton, type must be DFA
1831 * @param string string that should be evaluated
1833 * @return 0 if string matches, non 0 otherwise
1836 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1839 struct GNUNET_REGEX_State *s;
1843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1844 "Tried to evaluate DFA, but NFA automaton given");
1850 for (strp = string; NULL != strp && *strp; strp++)
1852 s = dfa_move (s, *strp);
1857 if (NULL != s && s->accepting)
1864 * Evaluates the given string using the given NFA automaton
1866 * @param a automaton, type must be NFA
1867 * @param string string that should be evaluated
1869 * @return 0 if string matches, non 0 otherwise
1872 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1875 struct GNUNET_REGEX_State *s;
1876 struct GNUNET_REGEX_StateSet *sset;
1877 struct GNUNET_REGEX_StateSet *new_sset;
1883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1884 "Tried to evaluate NFA, but DFA automaton given");
1890 sset = nfa_closure_create (a, a->start, 0);
1892 for (strp = string; NULL != strp && *strp; strp++)
1894 new_sset = nfa_closure_set_create (a, sset, *strp);
1895 state_set_clear (sset);
1896 sset = nfa_closure_set_create (a, new_sset, 0);
1897 state_set_clear (new_sset);
1900 for (i = 0; i < sset->len; i++)
1902 s = sset->states[i];
1903 if (NULL != s && s->accepting)
1910 state_set_clear (sset);
1915 * Evaluates the given 'string' against the given compiled regex
1917 * @param a automaton
1918 * @param string string to check
1920 * @return 0 if string matches, non 0 otherwise
1923 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1930 result = evaluate_dfa (a, string);
1933 result = evaluate_nfa (a, string);
1936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1937 "Evaluating regex failed, automaton has no type!\n");
1938 result = GNUNET_SYSERR;
1946 * Get the first key for the given 'input_string'. This hashes
1947 * the first x bits of the 'input_strings'.
1949 * @param input_string string.
1950 * @param string_len length of the 'input_string'.
1951 * @param key pointer to where to write the hash code.
1953 * @return number of bits of 'input_string' that have been consumed
1954 * to construct the key
1957 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
1958 GNUNET_HashCode * key)
1962 size = string_len < initial_bits ? string_len : initial_bits;
1964 if (NULL == input_string)
1966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
1970 GNUNET_CRYPTO_hash (input_string, size, key);
1976 * Check if the given 'proof' matches the given 'key'.
1978 * @param proof partial regex
1981 * @return GNUNET_OK if the proof is valid for the given key
1984 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
1990 * Get all edges leaving state 's'.
1993 * @param edges all edges leaving 's'.
1995 * @return number of edges.
1998 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
2000 struct Transition *t;
2008 for (t = s->transitions_head; NULL != t; t = t->next)
2010 if (NULL != t->to_state)
2012 edges[count].label = &t->label;
2013 edges[count].destination = t->to_state->hash;
2021 * Iterate over all edges helper function starting from state 's', calling
2022 * iterator on for each edge.
2025 * @param iterator iterator function called for each edge.
2026 * @param iterator_cls closure.
2029 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2032 struct Transition *t;
2033 struct GNUNET_REGEX_Edge edges[s->transition_count];
2034 unsigned int num_edges;
2036 if (GNUNET_NO == s->marked)
2038 s->marked = GNUNET_YES;
2040 num_edges = state_get_edges (s, edges);
2042 iterator (iterator_cls, &s->hash, NULL, s->accepting, num_edges, edges);
2045 for (t = s->transitions_head; NULL != t; t = t->next)
2046 iterate_edge (t->to_state, iterator, iterator_cls);
2051 * Iterate over all edges starting from start state of automaton 'a'. Calling
2052 * iterator for each edge.
2054 * @param a automaton.
2055 * @param iterator iterator called for each edge.
2056 * @param iterator_cls closure.
2059 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2060 GNUNET_REGEX_KeyIterator iterator,
2063 iterate_edge (a->start, iterator, iterator_cls);