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 as well as a
35 * 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 used for constructing an NFA,
91 * where each NFA itself consists of one or more NFAs linked together.
93 struct GNUNET_REGEX_State *start;
96 * End state of the automaton.
98 struct GNUNET_REGEX_State *end;
101 * Number of states in the automaton.
103 unsigned int state_count;
108 struct GNUNET_REGEX_State *states_head;
113 struct GNUNET_REGEX_State *states_tail;
116 * Type of the automaton.
118 enum GNUNET_REGEX_automaton_type type;
122 * A state. Can be used in DFA and NFA automatons.
124 struct GNUNET_REGEX_State
127 * This is a linked list.
129 struct GNUNET_REGEX_State *prev;
132 * This is a linked list.
134 struct GNUNET_REGEX_State *next;
142 * If this is an accepting state or not.
147 * Marking of the state. This is used for marking all visited states when
148 * traversing all states of an automaton and for cases where the state id
149 * cannot be used (dfa minimization).
154 * Marking the state as contained. This is used for checking, if the state is
155 * contained in a set in constant time
160 * Marking the state as part of an SCC (Strongly Connected Component). All
161 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
162 * is not a part of any SCC.
167 * Used for SCC detection.
172 * Used for SCC detection.
177 * Human readable name of the automaton. Used for debugging and graph
185 GNUNET_HashCode hash;
188 * Proof for this state.
193 * Number of transitions from this state to other states.
195 unsigned int transition_count;
198 * DLL of transitions.
200 struct Transition *transitions_head;
203 * DLL of transitions.
205 struct Transition *transitions_tail;
208 * Set of states on which this state is based on. Used when creating a DFA out
209 * of several NFA states.
211 struct GNUNET_REGEX_StateSet *nfa_set;
215 * Transition between two states. Each state can have 0-n transitions. If label
216 * is 0, this is considered to be an epsilon transition.
221 * This is a linked list.
223 struct Transition *prev;
226 * This is a linked list.
228 struct Transition *next;
231 * Unique id of this transition.
236 * Label for this transition. This is basically the edge label for the graph.
241 * State to which this transition leads.
243 struct GNUNET_REGEX_State *to_state;
246 * State from which this transition origins.
248 struct GNUNET_REGEX_State *from_state;
254 struct GNUNET_REGEX_StateSet
259 struct GNUNET_REGEX_State **states;
262 * Length of the 'states' array.
268 debug_print_state (struct GNUNET_REGEX_State *s)
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i\n",
272 s->id, s->name, s->marked, s->accepting, s->scc_id,
273 s->transition_count);
277 debug_print_states (struct GNUNET_REGEX_StateSet *sset)
279 struct GNUNET_REGEX_State *s;
282 for (i = 0; i < sset->len; i++)
285 debug_print_state (s);
290 debug_print_transitions (struct GNUNET_REGEX_State *s)
292 struct Transition *t;
296 for (t = s->transitions_head; NULL != t; t = t->next)
303 if (NULL == t->to_state)
306 state = t->to_state->name;
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
314 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
315 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
316 * SCCs inside an automaton.
319 * @param v start vertex
320 * @param index current index
321 * @param stack stack for saving all SCCs
322 * @param stack_size current size of the stack
325 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
326 struct GNUNET_REGEX_State *v, int *index,
327 struct GNUNET_REGEX_State **stack,
328 unsigned int *stack_size)
330 struct GNUNET_REGEX_State *w;
331 struct Transition *t;
336 stack[(*stack_size)++] = v;
339 for (t = v->transitions_head; NULL != t; t = t->next)
342 if (NULL != w && w->index < 0)
344 scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
345 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
347 else if (0 != w->contained)
348 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
351 if (v->lowlink == v->index)
353 w = stack[--(*stack_size)];
361 w->scc_id = ctx->scc_id;
362 w = stack[--(*stack_size)];
365 w->scc_id = ctx->scc_id;
371 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
372 * SCCs will be marked using the scc_id on each state.
378 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
382 struct GNUNET_REGEX_State *v;
383 struct GNUNET_REGEX_State *stack[a->state_count];
384 unsigned int stack_size;
386 for (v = a->states_head; NULL != v; v = v->next)
396 for (i = 0, v = a->states_head; NULL != v; v = v->next)
399 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
404 * Adds a transition from one state to another on 'label'. Does not add
408 * @param from_state starting state for the transition
409 * @param label transition label
410 * @param to_state state to where the transition should point to
413 state_add_transition (struct GNUNET_REGEX_Context *ctx,
414 struct GNUNET_REGEX_State *from_state, const char label,
415 struct GNUNET_REGEX_State *to_state)
418 struct Transition *t;
420 if (NULL == from_state)
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
426 // Do not add duplicate state transitions
428 for (t = from_state->transitions_head; NULL != t; t = t->next)
430 if (t->to_state == to_state && t->label == label &&
431 t->from_state == from_state)
441 t = GNUNET_malloc (sizeof (struct Transition));
442 t->id = ctx->transition_id++;
444 t->to_state = to_state;
445 t->from_state = from_state;
447 // Add outgoing transition to 'from_state'
448 from_state->transition_count++;
449 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
450 from_state->transitions_tail, t);
454 * Compare two states. Used for sorting.
456 * @param a first state
457 * @param b second state
459 * @return an integer less than, equal to, or greater than zero
460 * if the first argument is considered to be respectively
461 * less than, equal to, or greater than the second.
464 state_compare (const void *a, const void *b)
466 struct GNUNET_REGEX_State **s1;
467 struct GNUNET_REGEX_State **s2;
469 s1 = (struct GNUNET_REGEX_State **) a;
470 s2 = (struct GNUNET_REGEX_State **) b;
472 return (*s1)->id - (*s2)->id;
476 * Get all edges leaving state 's'.
479 * @param edges all edges leaving 's'.
481 * @return number of edges.
484 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
486 struct Transition *t;
494 for (t = s->transitions_head; NULL != t; t = t->next)
496 if (NULL != t->to_state)
498 edges[count].label = &t->label;
499 edges[count].destination = t->to_state->hash;
507 * Compare to state sets by comparing the id's of the states that are contained
508 * in each set. Both sets are expected to be sorted by id!
510 * @param sset1 first state set
511 * @param sset2 second state set
513 * @return an integer less than, equal to, or greater than zero
514 * if the first argument is considered to be respectively
515 * less than, equal to, or greater than the second.
518 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
519 struct GNUNET_REGEX_StateSet *sset2)
524 if (NULL == sset1 || NULL == sset2)
527 result = sset1->len - sset2->len;
529 for (i = 0; i < sset1->len; i++)
534 result = state_compare (&sset1->states[i], &sset2->states[i]);
540 * Clears the given StateSet 'set'
542 * @param set set to be cleared
545 state_set_clear (struct GNUNET_REGEX_StateSet *set)
549 if (NULL != set->states)
550 GNUNET_free (set->states);
556 * Clears an automaton fragment. Does not destroy the states inside the
559 * @param a automaton to be cleared
562 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
569 a->states_head = NULL;
570 a->states_tail = NULL;
576 * Frees the memory used by State 's'
578 * @param s state that should be destroyed
581 automaton_destroy_state (struct GNUNET_REGEX_State *s)
583 struct Transition *t;
584 struct Transition *next_t;
590 GNUNET_free (s->name);
592 for (t = s->transitions_head; NULL != t; t = next_t)
595 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
599 state_set_clear (s->nfa_set);
605 * Remove a state from the given automaton 'a'. Always use this function when
606 * altering the states of an automaton. Will also remove all transitions leading
607 * to this state, before destroying it.
610 * @param s state to remove
613 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
614 struct GNUNET_REGEX_State *s)
616 struct GNUNET_REGEX_State *ss;
617 struct GNUNET_REGEX_State *s_check;
618 struct Transition *t_check;
620 if (NULL == a || NULL == s)
625 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
628 // remove all transitions leading to this state
629 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
631 for (t_check = s_check->transitions_head; NULL != t_check;
632 t_check = t_check->next)
634 if (t_check->to_state == ss)
636 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
637 s_check->transitions_tail, t_check);
638 s_check->transition_count--;
643 automaton_destroy_state (ss);
647 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
652 * @param s1 first state
653 * @param s2 second state, will be destroyed
656 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
657 struct GNUNET_REGEX_Automaton *a,
658 struct GNUNET_REGEX_State *s1,
659 struct GNUNET_REGEX_State *s2)
661 struct GNUNET_REGEX_State *s_check;
662 struct Transition *t_check;
665 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
667 // 1. Make all transitions pointing to s2 point to s1
668 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
670 for (t_check = s_check->transitions_head; NULL != t_check;
671 t_check = t_check->next)
673 if (s2 == t_check->to_state)
674 t_check->to_state = s1;
678 // 2. Add all transitions from s2 to sX to s1
679 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
681 if (t_check->to_state != s1)
682 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
685 // 3. Rename s1 to {s1,s2}
686 new_name = GNUNET_strdup (s1->name);
687 if (NULL != s1->name)
689 GNUNET_free (s1->name);
692 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
693 GNUNET_free (new_name);
696 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
698 automaton_destroy_state (s2);
702 * Add a state to the automaton 'a', always use this function to alter the
703 * states DLL of the automaton.
705 * @param a automaton to add the state to
706 * @param s state that should be added
709 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
710 struct GNUNET_REGEX_State *s)
712 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
716 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
717 struct GNUNET_REGEX_State * s);
720 automaton_state_traverse_backward (void *cls, struct GNUNET_REGEX_State *s,
721 GNUNET_REGEX_traverse_action action)
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Traversing backwards...\n");
729 * Traverses all states that are reachable from state 's'. Expects the states to
730 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
733 * @param cls closure.
734 * @param s start state.
735 * @param action action to be performed on each state.
738 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
739 GNUNET_REGEX_traverse_action action)
741 struct Transition *t;
743 if (GNUNET_NO == s->marked)
745 s->marked = GNUNET_YES;
750 for (t = s->transitions_head; NULL != t; t = t->next)
751 automaton_state_traverse (cls, t->to_state, action);
756 * Traverses the given automaton from it's start state, visiting all reachable
757 * states and calling 'action' on each one of them.
759 * @param cls closure.
760 * @param a automaton.
761 * @param action action to be performed on each state.
764 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
765 GNUNET_REGEX_traverse_action action)
767 struct GNUNET_REGEX_State *s;
769 for (s = a->start; NULL != s; s = s->next)
770 s->marked = GNUNET_NO;
772 automaton_state_traverse (cls, a->start, action);
776 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
777 * automaton_destroy_state.
780 * @param nfa_states set of NFA states on which the DFA should be based on
782 * @return new DFA state
784 static struct GNUNET_REGEX_State *
785 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
786 struct GNUNET_REGEX_StateSet *nfa_states)
788 struct GNUNET_REGEX_State *s;
791 struct GNUNET_REGEX_State *cstate;
792 struct Transition *ctran;
794 struct Transition *t;
797 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
798 s->id = ctx->state_id++;
807 if (NULL == nfa_states)
809 GNUNET_asprintf (&s->name, "s%i", s->id);
813 s->nfa_set = nfa_states;
815 if (nfa_states->len < 1)
818 // Create a name based on 'sset'
819 s->name = GNUNET_malloc (sizeof (char) * 2);
820 strcat (s->name, "{");
823 for (i = 0; i < nfa_states->len; i++)
825 cstate = nfa_states->states[i];
826 GNUNET_asprintf (&name, "%i,", cstate->id);
830 len = strlen (s->name) + strlen (name) + 1;
831 s->name = GNUNET_realloc (s->name, len);
832 strcat (s->name, name);
837 // Add a transition for each distinct label to NULL state
838 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
840 if (0 != ctran->label)
844 for (t = s->transitions_head; NULL != t; t = t->next)
846 if (t->label == ctran->label)
854 state_add_transition (ctx, s, ctran->label, NULL);
858 // If the nfa_states contain an accepting state, the new dfa state is also
860 if (cstate->accepting)
864 s->name[strlen (s->name) - 1] = '}';
870 * Move from the given state 's' to the next state on transition 'label'
872 * @param s starting state
873 * @param label edge label to follow
875 * @return new state or NULL, if transition on label not possible
877 static struct GNUNET_REGEX_State *
878 dfa_move (struct GNUNET_REGEX_State *s, const char label)
880 struct Transition *t;
881 struct GNUNET_REGEX_State *new_s;
888 for (t = s->transitions_head; NULL != t; t = t->next)
890 if (label == t->label)
902 * Remove all unreachable states from DFA 'a'. Unreachable states are those
903 * states that are not reachable from the starting state.
905 * @param a DFA automaton
908 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
910 struct GNUNET_REGEX_State *s;
911 struct GNUNET_REGEX_State *s_next;
913 // 1. unmark all states
914 for (s = a->states_head; NULL != s; s = s->next)
915 s->marked = GNUNET_NO;
917 // 2. traverse dfa from start state and mark all visited states
918 automaton_traverse (NULL, a, NULL);
920 // 3. delete all states that were not visited
921 for (s = a->states_head; NULL != s; s = s_next)
924 if (GNUNET_NO == s->marked)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
927 automaton_remove_state (a, s);
933 * Remove all dead states from the DFA 'a'. Dead states are those states that do
934 * not transition to any other state but themselfes.
936 * @param a DFA automaton
939 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
941 struct GNUNET_REGEX_State *s;
942 struct Transition *t;
945 GNUNET_assert (DFA == a->type);
947 for (s = a->states_head; NULL != s; s = s->next)
953 for (t = s->transitions_head; NULL != t; t = t->next)
955 if (NULL != t->to_state && t->to_state != s)
965 // state s is dead, remove it
966 automaton_remove_state (a, s);
971 * Merge all non distinguishable states in the DFA 'a'
974 * @param a DFA automaton
977 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
978 struct GNUNET_REGEX_Automaton *a)
981 int table[a->state_count][a->state_count];
982 struct GNUNET_REGEX_State *s1;
983 struct GNUNET_REGEX_State *s2;
984 struct Transition *t1;
985 struct Transition *t2;
989 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
995 // Mark all pairs of accepting/!accepting states
996 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
998 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1000 if ((s1->accepting && !s2->accepting) ||
1001 (!s1->accepting && s2->accepting))
1003 table[s1->marked][s2->marked] = 1;
1006 table[s1->marked][s2->marked] = 0;
1014 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1016 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1018 if (0 != table[s1->marked][s2->marked])
1021 common_labels = GNUNET_NO;
1022 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1024 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1026 if (t1->label == t2->label)
1028 common_labels = GNUNET_YES;
1030 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1031 0 != table[t2->to_state->marked][t1->to_state->marked])
1033 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1039 if (GNUNET_NO == common_labels)
1040 table[s1->marked][s2->marked] = -2;
1045 struct GNUNET_REGEX_State *s2_next;
1047 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1049 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1052 if (table[s1->marked][s2->marked] == 0)
1053 automaton_merge_states (ctx, a, s1, s2);
1059 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1060 * dead states and merging all non distinguishable states
1062 * @param ctx context
1063 * @param a DFA automaton
1066 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1067 struct GNUNET_REGEX_Automaton *a)
1072 GNUNET_assert (DFA == a->type);
1074 // 1. remove unreachable states
1075 dfa_remove_unreachable_states (a);
1077 // 2. remove dead states
1078 dfa_remove_dead_states (a);
1080 // 3. Merge nondistinguishable states
1081 dfa_merge_nondistinguishable_states (ctx, a);
1085 * Creates a new NFA fragment. Needs to be cleared using
1086 * automaton_fragment_clear.
1088 * @param start starting state
1089 * @param end end state
1091 * @return new NFA fragment
1093 static struct GNUNET_REGEX_Automaton *
1094 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1095 struct GNUNET_REGEX_State *end)
1097 struct GNUNET_REGEX_Automaton *n;
1099 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1105 if (NULL == start && NULL == end)
1108 automaton_add_state (n, end);
1109 automaton_add_state (n, start);
1118 * Adds a list of states to the given automaton 'n'.
1120 * @param n automaton to which the states should be added
1121 * @param states_head head of the DLL of states
1122 * @param states_tail tail of the DLL of states
1125 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1126 struct GNUNET_REGEX_State *states_head,
1127 struct GNUNET_REGEX_State *states_tail)
1129 struct GNUNET_REGEX_State *s;
1131 if (NULL == n || NULL == states_head)
1133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1137 if (NULL == n->states_head)
1139 n->states_head = states_head;
1140 n->states_tail = states_tail;
1144 if (NULL != states_head)
1146 n->states_tail->next = states_head;
1147 n->states_tail = states_tail;
1150 for (s = states_head; NULL != s; s = s->next)
1155 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1157 * @param ctx context
1158 * @param accepting is it an accepting state or not
1160 * @return new NFA state
1162 static struct GNUNET_REGEX_State *
1163 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1165 struct GNUNET_REGEX_State *s;
1167 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1168 s->id = ctx->state_id++;
1169 s->accepting = accepting;
1176 GNUNET_asprintf (&s->name, "s%i", s->id);
1182 * Calculates the NFA closure set for the given state.
1184 * @param nfa the NFA containing 's'
1185 * @param s starting point state
1186 * @param label transitioning label on 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_create (struct GNUNET_REGEX_Automaton *nfa,
1193 struct GNUNET_REGEX_State *s, const char label)
1195 struct GNUNET_REGEX_StateSet *cls;
1196 struct GNUNET_REGEX_StateSet *cls_check;
1197 struct GNUNET_REGEX_State *clsstate;
1198 struct GNUNET_REGEX_State *currentstate;
1199 struct Transition *ctran;
1204 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1205 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1207 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1208 clsstate->contained = 0;
1210 // Add start state to closure only for epsilon closure
1212 GNUNET_array_append (cls->states, cls->len, s);
1214 GNUNET_array_append (cls_check->states, cls_check->len, s);
1215 while (cls_check->len > 0)
1217 currentstate = cls_check->states[cls_check->len - 1];
1218 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1220 for (ctran = currentstate->transitions_head; NULL != ctran;
1221 ctran = ctran->next)
1223 if (NULL != ctran->to_state && label == ctran->label)
1225 clsstate = ctran->to_state;
1227 if (NULL != clsstate && 0 == clsstate->contained)
1229 GNUNET_array_append (cls->states, cls->len, clsstate);
1230 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1231 clsstate->contained = 1;
1236 GNUNET_assert (0 == cls_check->len);
1237 GNUNET_free (cls_check);
1240 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1247 * Calculates the closure set for the given set of states.
1249 * @param nfa the NFA containing 's'
1250 * @param states list of states on which to base the closure on
1251 * @param label transitioning label for which to base the closure on,
1252 * pass 0 for epsilon transition
1254 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1256 static struct GNUNET_REGEX_StateSet *
1257 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1258 struct GNUNET_REGEX_StateSet *states, const char label)
1260 struct GNUNET_REGEX_State *s;
1261 struct GNUNET_REGEX_StateSet *sset;
1262 struct GNUNET_REGEX_StateSet *cls;
1271 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1273 for (i = 0; i < states->len; i++)
1275 s = states->states[i];
1276 sset = nfa_closure_create (nfa, s, label);
1278 for (j = 0; j < sset->len; j++)
1281 for (k = 0; k < cls->len; k++)
1283 if (sset->states[j]->id == cls->states[k]->id)
1290 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1292 state_set_clear (sset);
1296 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1303 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1305 * @param ctx context
1308 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1310 struct GNUNET_REGEX_Automaton *a;
1311 struct GNUNET_REGEX_Automaton *b;
1312 struct GNUNET_REGEX_Automaton *new;
1314 b = ctx->stack_tail;
1315 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1316 a = ctx->stack_tail;
1317 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1319 state_add_transition (ctx, a->end, 0, b->start);
1320 a->end->accepting = 0;
1321 b->end->accepting = 1;
1323 new = nfa_fragment_create (NULL, NULL);
1324 nfa_add_states (new, a->states_head, a->states_tail);
1325 nfa_add_states (new, b->states_head, b->states_tail);
1326 new->start = a->start;
1328 automaton_fragment_clear (a);
1329 automaton_fragment_clear (b);
1331 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1335 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1337 * @param ctx context
1340 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1342 struct GNUNET_REGEX_Automaton *a;
1343 struct GNUNET_REGEX_Automaton *new;
1344 struct GNUNET_REGEX_State *start;
1345 struct GNUNET_REGEX_State *end;
1347 a = ctx->stack_tail;
1348 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1353 "nfa_add_star_op failed, because there was no element on the stack");
1357 start = nfa_state_create (ctx, 0);
1358 end = nfa_state_create (ctx, 1);
1360 state_add_transition (ctx, start, 0, a->start);
1361 state_add_transition (ctx, start, 0, end);
1362 state_add_transition (ctx, a->end, 0, a->start);
1363 state_add_transition (ctx, a->end, 0, end);
1365 a->end->accepting = 0;
1368 new = nfa_fragment_create (start, end);
1369 nfa_add_states (new, a->states_head, a->states_tail);
1370 automaton_fragment_clear (a);
1372 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1376 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1378 * @param ctx context
1381 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1383 struct GNUNET_REGEX_Automaton *a;
1385 a = ctx->stack_tail;
1386 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1388 state_add_transition (ctx, a->end, 0, a->start);
1390 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1394 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1396 * @param ctx context
1399 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1401 struct GNUNET_REGEX_Automaton *a;
1402 struct GNUNET_REGEX_Automaton *new;
1403 struct GNUNET_REGEX_State *start;
1404 struct GNUNET_REGEX_State *end;
1406 a = ctx->stack_tail;
1407 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1412 "nfa_add_question_op failed, because there was no element on the stack");
1416 start = nfa_state_create (ctx, 0);
1417 end = nfa_state_create (ctx, 1);
1419 state_add_transition (ctx, start, 0, a->start);
1420 state_add_transition (ctx, start, 0, end);
1421 state_add_transition (ctx, a->end, 0, end);
1423 a->end->accepting = 0;
1425 new = nfa_fragment_create (start, end);
1426 nfa_add_states (new, a->states_head, a->states_tail);
1427 automaton_fragment_clear (a);
1429 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1433 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1434 * alternates between a and b (a|b)
1436 * @param ctx context
1439 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1441 struct GNUNET_REGEX_Automaton *a;
1442 struct GNUNET_REGEX_Automaton *b;
1443 struct GNUNET_REGEX_Automaton *new;
1444 struct GNUNET_REGEX_State *start;
1445 struct GNUNET_REGEX_State *end;
1447 b = ctx->stack_tail;
1448 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1449 a = ctx->stack_tail;
1450 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1452 start = nfa_state_create (ctx, 0);
1453 end = nfa_state_create (ctx, 1);
1454 state_add_transition (ctx, start, 0, a->start);
1455 state_add_transition (ctx, start, 0, b->start);
1457 state_add_transition (ctx, a->end, 0, end);
1458 state_add_transition (ctx, b->end, 0, end);
1460 a->end->accepting = 0;
1461 b->end->accepting = 0;
1464 new = nfa_fragment_create (start, end);
1465 nfa_add_states (new, a->states_head, a->states_tail);
1466 nfa_add_states (new, b->states_head, b->states_tail);
1467 automaton_fragment_clear (a);
1468 automaton_fragment_clear (b);
1470 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1474 * Adds a new nfa fragment to the stack
1476 * @param ctx context
1477 * @param lit label for nfa transition
1480 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1482 struct GNUNET_REGEX_Automaton *n;
1483 struct GNUNET_REGEX_State *start;
1484 struct GNUNET_REGEX_State *end;
1486 GNUNET_assert (NULL != ctx);
1488 start = nfa_state_create (ctx, 0);
1489 end = nfa_state_create (ctx, 1);
1490 state_add_transition (ctx, start, lit, end);
1491 n = nfa_fragment_create (start, end);
1492 GNUNET_assert (NULL != n);
1493 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1497 * Initialize a new context
1499 * @param ctx context
1502 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1510 ctx->transition_id = 0;
1512 ctx->stack_head = NULL;
1513 ctx->stack_tail = NULL;
1517 * Construct an NFA by parsing the regex string of length 'len'.
1519 * @param regex regular expression string
1520 * @param len length of the string
1522 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1524 struct GNUNET_REGEX_Automaton *
1525 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1527 struct GNUNET_REGEX_Context ctx;
1528 struct GNUNET_REGEX_Automaton *nfa;
1532 unsigned int altcount;
1533 unsigned int atomcount;
1534 unsigned int pcount;
1541 GNUNET_REGEX_context_init (&ctx);
1550 for (count = 0; count < len && *regexp; count++, regexp++)
1558 nfa_add_concatenation (&ctx);
1560 GNUNET_array_grow (p, pcount, pcount + 1);
1561 p[pcount - 1].altcount = altcount;
1562 p[pcount - 1].atomcount = atomcount;
1569 error_msg = "Cannot append '|' to nothing";
1572 while (--atomcount > 0)
1573 nfa_add_concatenation (&ctx);
1579 error_msg = "Missing opening '('";
1584 // Ignore this: "()"
1586 altcount = p[pcount].altcount;
1587 atomcount = p[pcount].atomcount;
1590 while (--atomcount > 0)
1591 nfa_add_concatenation (&ctx);
1592 for (; altcount > 0; altcount--)
1593 nfa_add_alternation (&ctx);
1595 altcount = p[pcount].altcount;
1596 atomcount = p[pcount].atomcount;
1602 error_msg = "Cannot append '+' to nothing";
1605 nfa_add_star_op (&ctx);
1610 error_msg = "Cannot append '+' to nothing";
1613 nfa_add_plus_op (&ctx);
1618 error_msg = "Cannot append '?' to nothing";
1621 nfa_add_question_op (&ctx);
1623 case 92: /* escape: \ */
1630 nfa_add_concatenation (&ctx);
1632 nfa_add_label (&ctx, *regexp);
1639 error_msg = "Unbalanced parenthesis";
1642 while (--atomcount > 0)
1643 nfa_add_concatenation (&ctx);
1644 for (; altcount > 0; altcount--)
1645 nfa_add_alternation (&ctx);
1650 nfa = ctx.stack_tail;
1651 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1654 if (NULL != ctx.stack_head)
1656 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1664 if (NULL != error_msg)
1665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1668 while (NULL != ctx.stack_tail)
1670 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1671 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1678 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1680 * @param ctx context.
1681 * @param nfa NFA automaton.
1682 * @param dfa DFA automaton.
1683 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1687 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1688 struct GNUNET_REGEX_Automaton *nfa,
1689 struct GNUNET_REGEX_Automaton *dfa,
1690 struct GNUNET_REGEX_State *dfa_state)
1692 struct Transition *ctran;
1693 struct GNUNET_REGEX_State *state_iter;
1694 struct GNUNET_REGEX_State *new_dfa_state;
1695 struct GNUNET_REGEX_State *state_contains;
1696 struct GNUNET_REGEX_StateSet *tmp;
1697 struct GNUNET_REGEX_StateSet *nfa_set;
1699 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1701 if (0 == ctran->label || NULL != ctran->to_state)
1704 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1705 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1706 state_set_clear (tmp);
1707 new_dfa_state = dfa_state_create (ctx, nfa_set);
1708 state_contains = NULL;
1709 for (state_iter = dfa->states_head; NULL != state_iter;
1710 state_iter = state_iter->next)
1712 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1713 state_contains = state_iter;
1716 if (NULL == state_contains)
1718 automaton_add_state (dfa, new_dfa_state);
1719 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1720 ctran->to_state = new_dfa_state;
1724 ctran->to_state = state_contains;
1725 automaton_destroy_state (new_dfa_state);
1731 * Construct DFA for the given 'regex' of length 'len'
1733 * @param regex regular expression string
1734 * @param len length of the regular expression
1736 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1738 struct GNUNET_REGEX_Automaton *
1739 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1741 struct GNUNET_REGEX_Context ctx;
1742 struct GNUNET_REGEX_Automaton *dfa;
1743 struct GNUNET_REGEX_Automaton *nfa;
1744 struct GNUNET_REGEX_StateSet *nfa_set;
1746 GNUNET_REGEX_context_init (&ctx);
1749 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1754 "Could not create DFA, because NFA creation failed\n");
1758 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1761 // Create DFA start state from epsilon closure
1762 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1763 dfa->start = dfa_state_create (&ctx, nfa_set);
1764 automaton_add_state (dfa, dfa->start);
1766 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
1768 GNUNET_REGEX_automaton_destroy (nfa);
1770 dfa_minimize (&ctx, dfa);
1771 scc_tarjan (&ctx, dfa);
1777 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
1780 * @param a automaton to be destroyed
1783 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1785 struct GNUNET_REGEX_State *s;
1786 struct GNUNET_REGEX_State *next_state;
1791 for (s = a->states_head; NULL != s;)
1793 next_state = s->next;
1794 automaton_destroy_state (s);
1802 * Save the given automaton as a GraphViz dot file
1804 * @param a the automaton to be saved
1805 * @param filename where to save the file
1808 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1809 const char *filename)
1811 struct GNUNET_REGEX_State *s;
1812 struct Transition *ctran;
1814 char *s_tran = NULL;
1821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1825 if (NULL == filename || strlen (filename) < 1)
1827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1831 p = fopen (filename, "w");
1835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1840 start = "digraph G {\nrankdir=LR\n";
1841 fwrite (start, strlen (start), 1, p);
1843 for (s = a->states_head; NULL != s; s = s->next)
1847 GNUNET_asprintf (&s_acc,
1848 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1849 s->name, s->scc_id);
1853 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
1859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
1863 fwrite (s_acc, strlen (s_acc), 1, p);
1864 GNUNET_free (s_acc);
1867 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1869 if (NULL == ctran->to_state)
1871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1872 "Transition from State %i has has no state for transitioning\n",
1877 if (ctran->label == 0)
1879 GNUNET_asprintf (&s_tran,
1880 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
1881 s->name, ctran->to_state->name, s->scc_id);
1885 GNUNET_asprintf (&s_tran,
1886 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
1887 s->name, ctran->to_state->name, ctran->label,
1893 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
1898 fwrite (s_tran, strlen (s_tran), 1, p);
1899 GNUNET_free (s_tran);
1905 fwrite (end, strlen (end), 1, p);
1910 * Evaluates the given string using the given DFA automaton
1912 * @param a automaton, type must be DFA
1913 * @param string string that should be evaluated
1915 * @return 0 if string matches, non 0 otherwise
1918 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1921 struct GNUNET_REGEX_State *s;
1925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1926 "Tried to evaluate DFA, but NFA automaton given");
1932 for (strp = string; NULL != strp && *strp; strp++)
1934 s = dfa_move (s, *strp);
1939 if (NULL != s && s->accepting)
1946 * Evaluates the given string using the given NFA automaton
1948 * @param a automaton, type must be NFA
1949 * @param string string that should be evaluated
1951 * @return 0 if string matches, non 0 otherwise
1954 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1957 struct GNUNET_REGEX_State *s;
1958 struct GNUNET_REGEX_StateSet *sset;
1959 struct GNUNET_REGEX_StateSet *new_sset;
1965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1966 "Tried to evaluate NFA, but DFA automaton given");
1972 sset = nfa_closure_create (a, a->start, 0);
1974 for (strp = string; NULL != strp && *strp; strp++)
1976 new_sset = nfa_closure_set_create (a, sset, *strp);
1977 state_set_clear (sset);
1978 sset = nfa_closure_set_create (a, new_sset, 0);
1979 state_set_clear (new_sset);
1982 for (i = 0; i < sset->len; i++)
1984 s = sset->states[i];
1985 if (NULL != s && s->accepting)
1992 state_set_clear (sset);
1997 * Evaluates the given 'string' against the given compiled regex
1999 * @param a automaton
2000 * @param string string to check
2002 * @return 0 if string matches, non 0 otherwise
2005 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2012 result = evaluate_dfa (a, string);
2015 result = evaluate_nfa (a, string);
2018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2019 "Evaluating regex failed, automaton has no type!\n");
2020 result = GNUNET_SYSERR;
2028 * Get the first key for the given 'input_string'. This hashes the first x bits
2029 * of the 'input_strings'.
2031 * @param input_string string.
2032 * @param string_len length of the 'input_string'.
2033 * @param key pointer to where to write the hash code.
2035 * @return number of bits of 'input_string' that have been consumed
2036 * to construct the key
2039 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2040 GNUNET_HashCode * key)
2044 size = string_len < initial_bits ? string_len : initial_bits;
2046 if (NULL == input_string)
2048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2052 GNUNET_CRYPTO_hash (input_string, size, key);
2058 * Check if the given 'proof' matches the given 'key'.
2060 * @param proof partial regex
2063 * @return GNUNET_OK if the proof is valid for the given key
2066 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2073 * Iterate over all edges helper function starting from state 's', calling
2074 * iterator on for each edge.
2077 * @param iterator iterator function called for each edge.
2078 * @param iterator_cls closure.
2081 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2084 struct Transition *t;
2085 struct GNUNET_REGEX_Edge edges[s->transition_count];
2086 unsigned int num_edges;
2088 if (GNUNET_YES != s->marked)
2090 s->marked = GNUNET_YES;
2092 num_edges = state_get_edges (s, edges);
2094 iterator (iterator_cls, &s->hash, NULL, s->accepting, num_edges, edges);
2096 for (t = s->transitions_head; NULL != t; t = t->next)
2097 iterate_edge (t->to_state, iterator, iterator_cls);
2102 * Iterate over all edges starting from start state of automaton 'a'. Calling
2103 * iterator for each edge.
2105 * @param a automaton.
2106 * @param iterator iterator called for each edge.
2107 * @param iterator_cls closure.
2110 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2111 GNUNET_REGEX_KeyIterator iterator,
2114 struct GNUNET_REGEX_State *s;
2116 for (s = a->states_head; NULL != s; s = s->next)
2117 s->marked = GNUNET_NO;
2119 iterate_edge (a->start, iterator, iterator_cls);