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 transitions: %i\n", s->id,
266 s->name, s->marked, s->accepting, s->scc_id, s->transition_count);
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'. Does not add
472 * @param from_state starting state for the transition
473 * @param label transition label
474 * @param to_state state to where the transition should point to
477 state_add_transition (struct GNUNET_REGEX_Context *ctx,
478 struct GNUNET_REGEX_State *from_state, const char label,
479 struct GNUNET_REGEX_State *to_state)
482 struct Transition *t;
484 if (NULL == from_state)
486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
490 // Do not add duplicate states
492 for (t = from_state->transitions_head; NULL != t; t = t->next)
494 if (t->to_state == to_state
496 && t->from_state == from_state)
506 t = GNUNET_malloc (sizeof (struct Transition));
508 t->id = ctx->transition_id++;
510 t->to_state = to_state;
511 t->from_state = from_state;
513 from_state->transition_count++;
515 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
516 from_state->transitions_tail, t);
520 * Clears an automaton fragment. Does not destroy the states inside
523 * @param a automaton to be cleared
526 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
533 a->states_head = NULL;
534 a->states_tail = NULL;
540 * Frees the memory used by State 's'
542 * @param s state that should be destroyed
545 automaton_destroy_state (struct GNUNET_REGEX_State *s)
547 struct Transition *t;
548 struct Transition *next_t;
554 GNUNET_free (s->name);
556 for (t = s->transitions_head; NULL != t; t = next_t)
559 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
563 state_set_clear (s->nfa_set);
569 * Remove a state from the given automaton 'a'. Always use this function
570 * when altering the states of an automaton. Will also remove all transitions
571 * leading to this state, before destroying it.
574 * @param s state to remove
577 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
578 struct GNUNET_REGEX_State *s)
580 struct GNUNET_REGEX_State *ss;
581 struct GNUNET_REGEX_State *s_check;
582 struct Transition *t_check;
584 if (NULL == a || NULL == s)
589 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
592 // remove all transitions leading to this state
593 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
595 for (t_check = s_check->transitions_head; NULL != t_check;
596 t_check = t_check->next)
598 if (t_check->to_state == ss)
600 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
601 s_check->transitions_tail, t_check);
602 s_check->transition_count--;
607 automaton_destroy_state (ss);
611 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
615 * @param s1 first state
616 * @param s2 second state, will be destroyed
619 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
620 struct GNUNET_REGEX_Automaton *a,
621 struct GNUNET_REGEX_State *s1,
622 struct GNUNET_REGEX_State *s2)
624 struct GNUNET_REGEX_State *s_check;
625 struct Transition *t_check;
628 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
630 // 1. Make all transitions pointing to s2 point to s1
631 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
633 for (t_check = s_check->transitions_head; NULL != t_check;
634 t_check = t_check->next)
636 if (s2 == t_check->to_state)
637 t_check->to_state = s1;
641 // 2. Add all transitions from s2 to sX to s1
642 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
644 if (t_check->to_state != s1)
645 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
648 // 3. Rename s1 to {s1,s2}
649 new_name = GNUNET_strdup (s1->name);
650 if (NULL != s1->name)
652 GNUNET_free (s1->name);
655 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
656 GNUNET_free (new_name);
659 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
661 automaton_destroy_state (s2);
665 * Add a state to the automaton 'a', always use this function to
666 * alter the states DLL of the automaton.
668 * @param a automaton to add the state to
669 * @param s state that should be added
672 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
673 struct GNUNET_REGEX_State *s)
675 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
679 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
680 struct GNUNET_REGEX_State * s);
683 * Traverses all states that are reachable from state 's'. Expects
684 * the states to be unmarked (s->marked == GNUNET_NO). Performs
685 * 'action' on each visited state.
687 * @param cls closure.
688 * @param s start state.
689 * @param action action to be performed on each state.
692 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
693 GNUNET_REGEX_traverse_action action)
695 struct Transition *t;
697 if (GNUNET_NO == s->marked)
699 s->marked = GNUNET_YES;
704 for (t = s->transitions_head; NULL != t; t = t->next)
705 automaton_state_traverse (cls, t->to_state, action);
710 * Traverses the given automaton from it's start state, visiting all
711 * reachable states and calling 'action' on each one of them.
713 * @param cls closure.
714 * @param a automaton.
715 * @param action action to be performed on each state.
718 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
719 GNUNET_REGEX_traverse_action action)
721 struct GNUNET_REGEX_State *s;
723 for (s = a->start; NULL != s; s = s->next)
724 s->marked = GNUNET_NO;
726 automaton_state_traverse (cls, a->start, action);
730 * Creates a new DFA state based on a set of NFA states. Needs to be freed
731 * using automaton_destroy_state.
734 * @param nfa_states set of NFA states on which the DFA should be based on
736 * @return new DFA state
738 static struct GNUNET_REGEX_State *
739 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
740 struct GNUNET_REGEX_StateSet *nfa_states)
742 struct GNUNET_REGEX_State *s;
745 struct GNUNET_REGEX_State *cstate;
746 struct Transition *ctran;
748 struct Transition *t;
751 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
752 s->id = ctx->state_id++;
761 if (NULL == nfa_states)
763 GNUNET_asprintf (&s->name, "s%i", s->id);
767 s->nfa_set = nfa_states;
769 if (nfa_states->len < 1)
772 // Create a name based on 'sset'
773 s->name = GNUNET_malloc (sizeof (char) * 2);
774 strcat (s->name, "{");
777 for (i = 0; i < nfa_states->len; i++)
779 cstate = nfa_states->states[i];
780 GNUNET_asprintf (&name, "%i,", cstate->id);
784 len = strlen (s->name) + strlen (name) + 1;
785 s->name = GNUNET_realloc (s->name, len);
786 strcat (s->name, name);
791 // Add a transition for each distinct label to NULL state
792 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
794 if (0 != ctran->label)
798 for (t = s->transitions_head; NULL != t; t = t->next)
800 if (t->label == ctran->label)
808 state_add_transition (ctx, s, ctran->label, NULL);
812 // If the nfa_states contain an accepting state, the new dfa state is also accepting
813 if (cstate->accepting)
817 s->name[strlen (s->name) - 1] = '}';
823 * Move from the given state 's' to the next state on
826 * @param s starting state
827 * @param label edge label to follow
829 * @return new state or NULL, if transition on label not possible
831 static struct GNUNET_REGEX_State *
832 dfa_move (struct GNUNET_REGEX_State *s, const char label)
834 struct Transition *t;
835 struct GNUNET_REGEX_State *new_s;
842 for (t = s->transitions_head; NULL != t; t = t->next)
844 if (label == t->label)
856 * Remove all unreachable states from DFA 'a'. Unreachable states
857 * are those states that are not reachable from the starting state.
859 * @param a DFA automaton
862 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
864 struct GNUNET_REGEX_State *s;
865 struct GNUNET_REGEX_State *s_next;
867 // 1. unmark all states
868 for (s = a->states_head; NULL != s; s = s->next)
869 s->marked = GNUNET_NO;
871 // 2. traverse dfa from start state and mark all visited states
872 automaton_traverse (NULL, a, NULL);
874 // 3. delete all states that were not visited
875 for (s = a->states_head; NULL != s; s = s_next)
878 if (GNUNET_NO == s->marked)
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
881 automaton_remove_state (a, s);
887 * Remove all dead states from the DFA 'a'. Dead states are those
888 * states that do not transition to any other state but themselfes.
890 * @param a DFA automaton
893 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
895 struct GNUNET_REGEX_State *s;
896 struct Transition *t;
899 GNUNET_assert (DFA == a->type);
901 for (s = a->states_head; NULL != s; s = s->next)
907 for (t = s->transitions_head; NULL != t; t = t->next)
909 if (NULL != t->to_state && t->to_state != s)
919 // state s is dead, remove it
920 automaton_remove_state (a, s);
925 * Merge all non distinguishable states in the DFA 'a'
928 * @param a DFA automaton
931 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
932 struct GNUNET_REGEX_Automaton *a)
935 int table[a->state_count][a->state_count];
936 struct GNUNET_REGEX_State *s1;
937 struct GNUNET_REGEX_State *s2;
938 struct Transition *t1;
939 struct Transition *t2;
943 for (i = 0, s1 = a->states_head;
944 i < a->state_count && NULL != s1;
950 // Mark all pairs of accepting/!accepting states
951 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
953 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
955 if ((s1->accepting && !s2->accepting) ||
956 (!s1->accepting && s2->accepting))
958 table[s1->marked][s2->marked] = 1;
961 table[s1->marked][s2->marked] = 0;
969 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
971 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
973 if (0 != table[s1->marked][s2->marked])
976 common_labels = GNUNET_NO;
977 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
979 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
981 if (t1->label == t2->label)
983 common_labels = GNUNET_YES;
985 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
986 0 != table[t2->to_state->marked][t1->to_state->marked])
988 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
994 if (GNUNET_NO == common_labels)
995 table[s1->marked][s2->marked] = -2;
1000 struct GNUNET_REGEX_State *s2_next;
1002 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1004 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1007 if (table[s1->marked][s2->marked] == 0)
1008 automaton_merge_states (ctx, a, s1, s2);
1014 * Minimize the given DFA 'a' by removing all unreachable states,
1015 * removing all dead states and merging all non distinguishable states
1017 * @param ctx context
1018 * @param a DFA automaton
1021 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1022 struct GNUNET_REGEX_Automaton *a)
1027 GNUNET_assert (DFA == a->type);
1029 // 1. remove unreachable states
1030 dfa_remove_unreachable_states (a);
1032 // 2. remove dead states
1033 dfa_remove_dead_states (a);
1035 // 3. Merge nondistinguishable states
1036 dfa_merge_nondistinguishable_states (ctx, a);
1040 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
1042 * @param start starting state
1043 * @param end end state
1045 * @return new NFA fragment
1047 static struct GNUNET_REGEX_Automaton *
1048 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1049 struct GNUNET_REGEX_State *end)
1051 struct GNUNET_REGEX_Automaton *n;
1053 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1059 if (NULL == start && NULL == end)
1062 automaton_add_state (n, end);
1063 automaton_add_state (n, start);
1072 * Adds a list of states to the given automaton 'n'.
1074 * @param n automaton to which the states should be added
1075 * @param states_head head of the DLL of states
1076 * @param states_tail tail of the DLL of states
1079 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1080 struct GNUNET_REGEX_State *states_head,
1081 struct GNUNET_REGEX_State *states_tail)
1083 struct GNUNET_REGEX_State *s;
1085 if (NULL == n || NULL == states_head)
1087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1091 if (NULL == n->states_head)
1093 n->states_head = states_head;
1094 n->states_tail = states_tail;
1098 if (NULL != states_head)
1100 n->states_tail->next = states_head;
1101 n->states_tail = states_tail;
1104 for (s = states_head; NULL != s; s = s->next)
1109 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1111 * @param ctx context
1112 * @param accepting is it an accepting state or not
1114 * @return new NFA state
1116 static struct GNUNET_REGEX_State *
1117 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1119 struct GNUNET_REGEX_State *s;
1121 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1122 s->id = ctx->state_id++;
1123 s->accepting = accepting;
1130 GNUNET_asprintf (&s->name, "s%i", s->id);
1136 * Calculates the NFA closure set for the given state.
1138 * @param nfa the NFA containing 's'
1139 * @param s starting point state
1140 * @param label transitioning label on which to base the closure on,
1141 * pass 0 for epsilon transition
1143 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1145 static struct GNUNET_REGEX_StateSet *
1146 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1147 struct GNUNET_REGEX_State *s, const char label)
1149 struct GNUNET_REGEX_StateSet *cls;
1150 struct GNUNET_REGEX_StateSet *cls_check;
1151 struct GNUNET_REGEX_State *clsstate;
1152 struct GNUNET_REGEX_State *currentstate;
1153 struct Transition *ctran;
1158 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1159 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1161 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1162 clsstate->contained = 0;
1164 // Add start state to closure only for epsilon closure
1166 GNUNET_array_append (cls->states, cls->len, s);
1168 GNUNET_array_append (cls_check->states, cls_check->len, s);
1169 while (cls_check->len > 0)
1171 currentstate = cls_check->states[cls_check->len - 1];
1172 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1174 for (ctran = currentstate->transitions_head; NULL != ctran;
1175 ctran = ctran->next)
1177 if (NULL != ctran->to_state && label == ctran->label)
1179 clsstate = ctran->to_state;
1181 if (NULL != clsstate && 0 == clsstate->contained)
1183 GNUNET_array_append (cls->states, cls->len, clsstate);
1184 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1185 clsstate->contained = 1;
1190 GNUNET_assert (0 == cls_check->len);
1191 GNUNET_free (cls_check);
1194 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1201 * Calculates the closure set for the given set of states.
1203 * @param nfa the NFA containing 's'
1204 * @param states list of states on which to base the closure on
1205 * @param label transitioning label for which to base the closure on,
1206 * pass 0 for epsilon transition
1208 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1210 static struct GNUNET_REGEX_StateSet *
1211 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1212 struct GNUNET_REGEX_StateSet *states, const char label)
1214 struct GNUNET_REGEX_State *s;
1215 struct GNUNET_REGEX_StateSet *sset;
1216 struct GNUNET_REGEX_StateSet *cls;
1225 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1227 for (i = 0; i < states->len; i++)
1229 s = states->states[i];
1230 sset = nfa_closure_create (nfa, s, label);
1232 for (j = 0; j < sset->len; j++)
1235 for (k = 0; k < cls->len; k++)
1237 if (sset->states[j]->id == cls->states[k]->id)
1244 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1246 state_set_clear (sset);
1250 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1257 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1259 * @param ctx context
1262 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1264 struct GNUNET_REGEX_Automaton *a;
1265 struct GNUNET_REGEX_Automaton *b;
1266 struct GNUNET_REGEX_Automaton *new;
1268 b = ctx->stack_tail;
1269 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1270 a = ctx->stack_tail;
1271 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1273 state_add_transition (ctx, a->end, 0, b->start);
1274 a->end->accepting = 0;
1275 b->end->accepting = 1;
1277 new = nfa_fragment_create (NULL, NULL);
1278 nfa_add_states (new, a->states_head, a->states_tail);
1279 nfa_add_states (new, b->states_head, b->states_tail);
1280 new->start = a->start;
1282 automaton_fragment_clear (a);
1283 automaton_fragment_clear (b);
1285 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1289 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1291 * @param ctx context
1294 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1296 struct GNUNET_REGEX_Automaton *a;
1297 struct GNUNET_REGEX_Automaton *new;
1298 struct GNUNET_REGEX_State *start;
1299 struct GNUNET_REGEX_State *end;
1301 a = ctx->stack_tail;
1302 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 "nfa_add_star_op failed, because there was no element on the stack");
1311 start = nfa_state_create (ctx, 0);
1312 end = nfa_state_create (ctx, 1);
1314 state_add_transition (ctx, start, 0, a->start);
1315 state_add_transition (ctx, start, 0, end);
1316 state_add_transition (ctx, a->end, 0, a->start);
1317 state_add_transition (ctx, a->end, 0, end);
1319 a->end->accepting = 0;
1322 new = nfa_fragment_create (start, end);
1323 nfa_add_states (new, a->states_head, a->states_tail);
1324 automaton_fragment_clear (a);
1326 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1330 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1332 * @param ctx context
1335 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1337 struct GNUNET_REGEX_Automaton *a;
1339 a = ctx->stack_tail;
1340 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1342 state_add_transition (ctx, a->end, 0, a->start);
1344 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1348 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1350 * @param ctx context
1353 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1355 struct GNUNET_REGEX_Automaton *a;
1356 struct GNUNET_REGEX_Automaton *new;
1357 struct GNUNET_REGEX_State *start;
1358 struct GNUNET_REGEX_State *end;
1360 a = ctx->stack_tail;
1361 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1366 "nfa_add_question_op failed, because there was no element on the stack");
1370 start = nfa_state_create (ctx, 0);
1371 end = nfa_state_create (ctx, 1);
1373 state_add_transition (ctx, start, 0, a->start);
1374 state_add_transition (ctx, start, 0, end);
1375 state_add_transition (ctx, a->end, 0, end);
1377 a->end->accepting = 0;
1379 new = nfa_fragment_create (start, end);
1380 nfa_add_states (new, a->states_head, a->states_tail);
1381 automaton_fragment_clear (a);
1383 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1387 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1388 * that alternates between a and b (a|b)
1390 * @param ctx context
1393 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1395 struct GNUNET_REGEX_Automaton *a;
1396 struct GNUNET_REGEX_Automaton *b;
1397 struct GNUNET_REGEX_Automaton *new;
1398 struct GNUNET_REGEX_State *start;
1399 struct GNUNET_REGEX_State *end;
1401 b = ctx->stack_tail;
1402 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1403 a = ctx->stack_tail;
1404 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1406 start = nfa_state_create (ctx, 0);
1407 end = nfa_state_create (ctx, 1);
1408 state_add_transition (ctx, start, 0, a->start);
1409 state_add_transition (ctx, start, 0, b->start);
1411 state_add_transition (ctx, a->end, 0, end);
1412 state_add_transition (ctx, b->end, 0, end);
1414 a->end->accepting = 0;
1415 b->end->accepting = 0;
1418 new = nfa_fragment_create (start, end);
1419 nfa_add_states (new, a->states_head, a->states_tail);
1420 nfa_add_states (new, b->states_head, b->states_tail);
1421 automaton_fragment_clear (a);
1422 automaton_fragment_clear (b);
1424 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1428 * Adds a new nfa fragment to the stack
1430 * @param ctx context
1431 * @param lit label for nfa transition
1434 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1436 struct GNUNET_REGEX_Automaton *n;
1437 struct GNUNET_REGEX_State *start;
1438 struct GNUNET_REGEX_State *end;
1440 GNUNET_assert (NULL != ctx);
1442 start = nfa_state_create (ctx, 0);
1443 end = nfa_state_create (ctx, 1);
1444 state_add_transition (ctx, start, lit, end);
1445 n = nfa_fragment_create (start, end);
1446 GNUNET_assert (NULL != n);
1447 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1451 * Initialize a new context
1453 * @param ctx context
1456 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1464 ctx->transition_id = 0;
1466 ctx->stack_head = NULL;
1467 ctx->stack_tail = NULL;
1471 * Construct an NFA by parsing the regex string of length 'len'.
1473 * @param regex regular expression string
1474 * @param len length of the string
1476 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1478 struct GNUNET_REGEX_Automaton *
1479 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1481 struct GNUNET_REGEX_Context ctx;
1482 struct GNUNET_REGEX_Automaton *nfa;
1486 unsigned int altcount;
1487 unsigned int atomcount;
1488 unsigned int pcount;
1495 GNUNET_REGEX_context_init (&ctx);
1504 for (count = 0; count < len && *regexp; count++, regexp++)
1512 nfa_add_concatenation (&ctx);
1514 GNUNET_array_grow (p, pcount, pcount + 1);
1515 p[pcount - 1].altcount = altcount;
1516 p[pcount - 1].atomcount = atomcount;
1523 error_msg = "Cannot append '|' to nothing";
1526 while (--atomcount > 0)
1527 nfa_add_concatenation (&ctx);
1533 error_msg = "Missing opening '('";
1538 // Ignore this: "()"
1540 altcount = p[pcount].altcount;
1541 atomcount = p[pcount].atomcount;
1544 while (--atomcount > 0)
1545 nfa_add_concatenation (&ctx);
1546 for (; altcount > 0; altcount--)
1547 nfa_add_alternation (&ctx);
1549 altcount = p[pcount].altcount;
1550 atomcount = p[pcount].atomcount;
1556 error_msg = "Cannot append '+' to nothing";
1559 nfa_add_star_op (&ctx);
1564 error_msg = "Cannot append '+' to nothing";
1567 nfa_add_plus_op (&ctx);
1572 error_msg = "Cannot append '?' to nothing";
1575 nfa_add_question_op (&ctx);
1577 case 92: /* escape: \ */
1584 nfa_add_concatenation (&ctx);
1586 nfa_add_label (&ctx, *regexp);
1593 error_msg = "Unbalanced parenthesis";
1596 while (--atomcount > 0)
1597 nfa_add_concatenation (&ctx);
1598 for (; altcount > 0; altcount--)
1599 nfa_add_alternation (&ctx);
1604 nfa = ctx.stack_tail;
1605 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1608 if (NULL != ctx.stack_head)
1610 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1618 if (NULL != error_msg)
1619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1622 while (NULL != ctx.stack_tail)
1624 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1625 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1632 * Construct DFA for the given 'regex' of length 'len'
1634 * @param regex regular expression string
1635 * @param len length of the regular expression
1637 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1639 struct GNUNET_REGEX_Automaton *
1640 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1642 struct GNUNET_REGEX_Context ctx;
1643 struct GNUNET_REGEX_Automaton *dfa;
1644 struct GNUNET_REGEX_Automaton *nfa;
1645 struct GNUNET_REGEX_StateSet *tmp;
1646 struct GNUNET_REGEX_StateSet *nfa_set;
1647 struct GNUNET_REGEX_StateSet *dfa_stack;
1648 struct Transition *ctran;
1649 struct GNUNET_REGEX_State *dfa_state;
1650 struct GNUNET_REGEX_State *new_dfa_state;
1651 struct GNUNET_REGEX_State *state_contains;
1652 struct GNUNET_REGEX_State *state_iter;
1654 GNUNET_REGEX_context_init (&ctx);
1657 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1662 "Could not create DFA, because NFA creation failed\n");
1666 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1669 // Create DFA start state from epsilon closure
1670 dfa_stack = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1671 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1672 dfa->start = dfa_state_create (&ctx, nfa_set);
1673 automaton_add_state (dfa, dfa->start);
1674 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1676 // Create dfa states by combining nfa states
1677 while (dfa_stack->len > 0)
1679 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1680 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1682 for (ctran = dfa_state->transitions_head; NULL != ctran;
1683 ctran = ctran->next)
1685 if (0 != ctran->label && NULL == ctran->to_state)
1687 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1688 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1689 state_set_clear (tmp);
1690 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1691 state_contains = NULL;
1692 for (state_iter = dfa->states_head; NULL != state_iter;
1693 state_iter = state_iter->next)
1696 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1697 state_contains = state_iter;
1700 if (NULL == state_contains)
1702 automaton_add_state (dfa, new_dfa_state);
1703 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1705 ctran->to_state = new_dfa_state;
1709 ctran->to_state = state_contains;
1710 automaton_destroy_state (new_dfa_state);
1716 GNUNET_free (dfa_stack);
1717 GNUNET_REGEX_automaton_destroy (nfa);
1719 dfa_minimize (&ctx, dfa);
1720 scc_tarjan (&ctx, dfa);
1726 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1729 * @param a automaton to be destroyed
1732 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1734 struct GNUNET_REGEX_State *s;
1735 struct GNUNET_REGEX_State *next_state;
1740 for (s = a->states_head; NULL != s;)
1742 next_state = s->next;
1743 automaton_destroy_state (s);
1751 * Save the given automaton as a GraphViz dot file
1753 * @param a the automaton to be saved
1754 * @param filename where to save the file
1757 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1758 const char *filename)
1760 struct GNUNET_REGEX_State *s;
1761 struct Transition *ctran;
1763 char *s_tran = NULL;
1770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1774 if (NULL == filename || strlen (filename) < 1)
1776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1780 p = fopen (filename, "w");
1784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1789 start = "digraph G {\nrankdir=LR\n";
1790 fwrite (start, strlen (start), 1, p);
1792 for (s = a->states_head; NULL != s; s = s->next)
1796 GNUNET_asprintf (&s_acc,
1797 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1798 s->name, s->scc_id);
1802 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
1808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
1811 fwrite (s_acc, strlen (s_acc), 1, p);
1812 GNUNET_free (s_acc);
1815 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1817 if (NULL == ctran->to_state)
1819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1820 "Transition from State %i has has no state for transitioning\n",
1825 if (ctran->label == 0)
1827 GNUNET_asprintf (&s_tran,
1828 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
1829 s->name, ctran->to_state->name, s->scc_id);
1833 GNUNET_asprintf (&s_tran,
1834 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
1835 s->name, ctran->to_state->name, ctran->label,
1841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
1845 fwrite (s_tran, strlen (s_tran), 1, p);
1846 GNUNET_free (s_tran);
1852 fwrite (end, strlen (end), 1, p);
1857 * Evaluates the given string using the given DFA automaton
1859 * @param a automaton, type must be DFA
1860 * @param string string that should be evaluated
1862 * @return 0 if string matches, non 0 otherwise
1865 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1868 struct GNUNET_REGEX_State *s;
1872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1873 "Tried to evaluate DFA, but NFA automaton given");
1879 for (strp = string; NULL != strp && *strp; strp++)
1881 s = dfa_move (s, *strp);
1886 if (NULL != s && s->accepting)
1893 * Evaluates the given string using the given NFA automaton
1895 * @param a automaton, type must be NFA
1896 * @param string string that should be evaluated
1898 * @return 0 if string matches, non 0 otherwise
1901 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1904 struct GNUNET_REGEX_State *s;
1905 struct GNUNET_REGEX_StateSet *sset;
1906 struct GNUNET_REGEX_StateSet *new_sset;
1912 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1913 "Tried to evaluate NFA, but DFA automaton given");
1919 sset = nfa_closure_create (a, a->start, 0);
1921 for (strp = string; NULL != strp && *strp; strp++)
1923 new_sset = nfa_closure_set_create (a, sset, *strp);
1924 state_set_clear (sset);
1925 sset = nfa_closure_set_create (a, new_sset, 0);
1926 state_set_clear (new_sset);
1929 for (i = 0; i < sset->len; i++)
1931 s = sset->states[i];
1932 if (NULL != s && s->accepting)
1939 state_set_clear (sset);
1944 * Evaluates the given 'string' against the given compiled regex
1946 * @param a automaton
1947 * @param string string to check
1949 * @return 0 if string matches, non 0 otherwise
1952 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1959 result = evaluate_dfa (a, string);
1962 result = evaluate_nfa (a, string);
1965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1966 "Evaluating regex failed, automaton has no type!\n");
1967 result = GNUNET_SYSERR;
1975 * Get the first key for the given 'input_string'. This hashes
1976 * the first x bits of the 'input_strings'.
1978 * @param input_string string.
1979 * @param string_len length of the 'input_string'.
1980 * @param key pointer to where to write the hash code.
1982 * @return number of bits of 'input_string' that have been consumed
1983 * to construct the key
1986 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
1987 GNUNET_HashCode * key)
1991 size = string_len < initial_bits ? string_len : initial_bits;
1993 if (NULL == input_string)
1995 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
1999 GNUNET_CRYPTO_hash (input_string, size, key);
2005 * Check if the given 'proof' matches the given 'key'.
2007 * @param proof partial regex
2010 * @return GNUNET_OK if the proof is valid for the given key
2013 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2019 * Get all edges leaving state 's'.
2022 * @param edges all edges leaving 's'.
2024 * @return number of edges.
2027 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
2029 struct Transition *t;
2037 for (t = s->transitions_head; NULL != t; t = t->next)
2039 if (NULL != t->to_state)
2041 edges[count].label = &t->label;
2042 edges[count].destination = t->to_state->hash;
2050 * Iterate over all edges helper function starting from state 's', calling
2051 * iterator on for each edge.
2054 * @param iterator iterator function called for each edge.
2055 * @param iterator_cls closure.
2058 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2061 struct Transition *t;
2062 struct GNUNET_REGEX_Edge edges[s->transition_count];
2063 unsigned int num_edges;
2065 if (GNUNET_YES != s->marked)
2067 s->marked = GNUNET_YES;
2069 num_edges = state_get_edges (s, edges);
2071 iterator (iterator_cls, &s->hash, NULL, s->accepting, num_edges, edges);
2073 for (t = s->transitions_head; NULL != t; t = t->next)
2074 iterate_edge (t->to_state, iterator, iterator_cls);
2079 * Iterate over all edges starting from start state of automaton 'a'. Calling
2080 * iterator for each edge.
2082 * @param a automaton.
2083 * @param iterator iterator called for each edge.
2084 * @param iterator_cls closure.
2087 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2088 GNUNET_REGEX_KeyIterator iterator,
2091 struct GNUNET_REGEX_State *s;
2093 for (s = a->states_head; NULL != s; s = s->next)
2094 s->marked = GNUNET_NO;
2096 iterate_edge (a->start, iterator, iterator_cls);