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, struct
667 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 s start state.
675 * @param action action to be performed on each state.
678 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
679 GNUNET_REGEX_traverse_action action)
681 struct Transition *t;
683 if (GNUNET_NO == s->marked)
685 s->marked = GNUNET_YES;
690 for (t = s->transitions_head; NULL != t; t = t->next)
691 automaton_state_traverse (cls, t->to_state, action);
696 * Traverses the given automaton from it's start state, visiting all
697 * reachable states and calling 'action' on each one of them.
699 * @param a automaton.
700 * @param action action to be performed on each state.
703 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
704 GNUNET_REGEX_traverse_action action)
706 struct GNUNET_REGEX_State *s;
708 for (s = a->start; NULL != s; s = s->next)
709 s->marked = GNUNET_NO;
711 automaton_state_traverse (cls, a->start, action);
715 * Creates a new DFA state based on a set of NFA states. Needs to be freed
716 * using automaton_destroy_state.
719 * @param nfa_states set of NFA states on which the DFA should be based on
721 * @return new DFA state
723 static struct GNUNET_REGEX_State *
724 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
725 struct GNUNET_REGEX_StateSet *nfa_states)
727 struct GNUNET_REGEX_State *s;
730 struct GNUNET_REGEX_State *cstate;
731 struct Transition *ctran;
733 struct Transition *t;
736 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
737 s->id = ctx->state_id++;
746 if (NULL == nfa_states)
748 GNUNET_asprintf (&s->name, "s%i", s->id);
752 s->nfa_set = nfa_states;
754 if (nfa_states->len < 1)
757 // Create a name based on 'sset'
758 s->name = GNUNET_malloc (sizeof (char) * 2);
759 strcat (s->name, "{");
762 for (i = 0; i < nfa_states->len; i++)
764 cstate = nfa_states->states[i];
765 GNUNET_asprintf (&name, "%i,", cstate->id);
769 len = strlen (s->name) + strlen (name) + 1;
770 s->name = GNUNET_realloc (s->name, len);
771 strcat (s->name, name);
776 // Add a transition for each distinct label to NULL state
777 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
779 if (0 != ctran->label)
783 for (t = s->transitions_head; NULL != t; t = t->next)
785 if (t->label == ctran->label)
793 state_add_transition (ctx, s, ctran->label, NULL);
797 // If the nfa_states contain an accepting state, the new dfa state is also accepting
798 if (cstate->accepting)
802 s->name[strlen (s->name) - 1] = '}';
808 * Move from the given state 's' to the next state on
811 * @param s starting state
812 * @param label edge label to follow
814 * @return new state or NULL, if transition on label not possible
816 static struct GNUNET_REGEX_State *
817 dfa_move (struct GNUNET_REGEX_State *s, const char label)
819 struct Transition *t;
820 struct GNUNET_REGEX_State *new_s;
827 for (t = s->transitions_head; NULL != t; t = t->next)
829 if (label == t->label)
841 * Remove all unreachable states from DFA 'a'. Unreachable states
842 * are those states that are not reachable from the starting state.
844 * @param a DFA automaton
847 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
849 struct GNUNET_REGEX_State *s;
850 struct GNUNET_REGEX_State *s_next;
852 // 1. unmark all states
853 for (s = a->states_head; NULL != s; s = s->next)
854 s->marked = GNUNET_NO;
856 // 2. traverse dfa from start state and mark all visited states
857 automaton_traverse (NULL, a, NULL);
859 // 3. delete all states that were not visited
860 for (s = a->states_head; NULL != s; s = s_next)
863 if (GNUNET_NO == s->marked)
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
866 automaton_remove_state (a, s);
872 * Remove all dead states from the DFA 'a'. Dead states are those
873 * states that do not transition to any other state but themselfes.
875 * @param a DFA automaton
878 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
880 struct GNUNET_REGEX_State *s;
881 struct Transition *t;
884 GNUNET_assert (DFA == a->type);
886 for (s = a->states_head; NULL != s; s = s->next)
892 for (t = s->transitions_head; NULL != t; t = t->next)
894 if (NULL != t->to_state && t->to_state != s)
904 // state s is dead, remove it
905 automaton_remove_state (a, s);
910 * Merge all non distinguishable states in the DFA 'a'
913 * @param a DFA automaton
916 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
917 struct GNUNET_REGEX_Automaton *a)
920 int table[a->state_count][a->state_count];
921 struct GNUNET_REGEX_State *s1;
922 struct GNUNET_REGEX_State *s2;
923 struct Transition *t1;
924 struct Transition *t2;
928 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
932 // Mark all pairs of accepting/!accepting states
933 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
935 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
937 if ((s1->accepting && !s2->accepting) ||
938 (!s1->accepting && s2->accepting))
940 table[s1->marked][s2->marked] = 1;
943 table[s1->marked][s2->marked] = 0;
950 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
952 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
954 if (0 != table[s1->marked][s2->marked])
957 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
959 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
961 if (t1->label == t2->label && t1->to_state == t2->to_state &&
962 (0 != table[t1->to_state->marked][t2->to_state->marked] ||
963 0 != table[t2->to_state->marked][t1->to_state->marked]))
965 table[s1->marked][s2->marked] = t1->label;
968 else if (t1->label != t2->label && t1->to_state != t2->to_state)
970 table[s1->marked][s2->marked] = -1;
979 struct GNUNET_REGEX_State *s2_next;
981 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
983 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
986 if (s1 != s2 && table[s1->marked][s2->marked] == 0)
987 automaton_merge_states (ctx, a, s1, s2);
993 * Minimize the given DFA 'a' by removing all unreachable states,
994 * removing all dead states and merging all non distinguishable states
997 * @param a DFA automaton
1000 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1001 struct GNUNET_REGEX_Automaton *a)
1006 GNUNET_assert (DFA == a->type);
1008 // 1. remove unreachable states
1009 dfa_remove_unreachable_states (a);
1011 // 2. remove dead states
1012 dfa_remove_dead_states (a);
1014 // 3. Merge nondistinguishable states
1015 dfa_merge_nondistinguishable_states (ctx, a);
1019 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
1021 * @param start starting state
1022 * @param end end state
1024 * @return new NFA fragment
1026 static struct GNUNET_REGEX_Automaton *
1027 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1028 struct GNUNET_REGEX_State *end)
1030 struct GNUNET_REGEX_Automaton *n;
1032 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1038 if (NULL == start && NULL == end)
1041 automaton_add_state (n, end);
1042 automaton_add_state (n, start);
1051 * Adds a list of states to the given automaton 'n'.
1053 * @param n automaton to which the states should be added
1054 * @param states_head head of the DLL of states
1055 * @param states_tail tail of the DLL of states
1058 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1059 struct GNUNET_REGEX_State *states_head,
1060 struct GNUNET_REGEX_State *states_tail)
1062 struct GNUNET_REGEX_State *s;
1064 if (NULL == n || NULL == states_head)
1066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1070 if (NULL == n->states_head)
1072 n->states_head = states_head;
1073 n->states_tail = states_tail;
1077 if (NULL != states_head)
1079 n->states_tail->next = states_head;
1080 n->states_tail = states_tail;
1083 for (s = states_head; NULL != s; s = s->next)
1088 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1090 * @param ctx context
1091 * @param accepting is it an accepting state or not
1093 * @return new NFA state
1095 static struct GNUNET_REGEX_State *
1096 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1098 struct GNUNET_REGEX_State *s;
1100 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1101 s->id = ctx->state_id++;
1102 s->accepting = accepting;
1109 GNUNET_asprintf (&s->name, "s%i", s->id);
1115 * Calculates the NFA closure set for the given state.
1117 * @param nfa the NFA containing 's'
1118 * @param s starting point state
1119 * @param label transitioning label on which to base the closure on,
1120 * pass 0 for epsilon transition
1122 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1124 static struct GNUNET_REGEX_StateSet *
1125 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1126 struct GNUNET_REGEX_State *s, const char label)
1128 struct GNUNET_REGEX_StateSet *cls;
1129 struct GNUNET_REGEX_StateSet *cls_check;
1130 struct GNUNET_REGEX_State *clsstate;
1131 struct GNUNET_REGEX_State *currentstate;
1132 struct Transition *ctran;
1137 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1138 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1140 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1141 clsstate->contained = 0;
1143 // Add start state to closure only for epsilon closure
1145 GNUNET_array_append (cls->states, cls->len, s);
1147 GNUNET_array_append (cls_check->states, cls_check->len, s);
1148 while (cls_check->len > 0)
1150 currentstate = cls_check->states[cls_check->len - 1];
1151 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1153 for (ctran = currentstate->transitions_head; NULL != ctran;
1154 ctran = ctran->next)
1156 if (NULL != ctran->to_state && label == ctran->label)
1158 clsstate = ctran->to_state;
1160 if (NULL != clsstate && 0 == clsstate->contained)
1162 GNUNET_array_append (cls->states, cls->len, clsstate);
1163 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1164 clsstate->contained = 1;
1169 GNUNET_assert (0 == cls_check->len);
1170 GNUNET_free (cls_check);
1173 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1180 * Calculates the closure set for the given set of states.
1182 * @param nfa the NFA containing 's'
1183 * @param states list of states on which to base the closure on
1184 * @param label transitioning label for which to base the closure on,
1185 * pass 0 for epsilon transition
1187 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1189 static struct GNUNET_REGEX_StateSet *
1190 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1191 struct GNUNET_REGEX_StateSet *states,
1194 struct GNUNET_REGEX_State *s;
1195 struct GNUNET_REGEX_StateSet *sset;
1196 struct GNUNET_REGEX_StateSet *cls;
1205 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1207 for (i = 0; i < states->len; i++)
1209 s = states->states[i];
1210 sset = nfa_closure_create (nfa, s, label);
1212 for (j = 0; j < sset->len; j++)
1215 for (k = 0; k < cls->len; k++)
1217 if (sset->states[j]->id == cls->states[k]->id)
1224 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1226 state_set_clear (sset);
1230 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1237 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1239 * @param ctx context
1242 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1244 struct GNUNET_REGEX_Automaton *a;
1245 struct GNUNET_REGEX_Automaton *b;
1246 struct GNUNET_REGEX_Automaton *new;
1248 b = ctx->stack_tail;
1249 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1250 a = ctx->stack_tail;
1251 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1253 state_add_transition (ctx, a->end, 0, b->start);
1254 a->end->accepting = 0;
1255 b->end->accepting = 1;
1257 new = nfa_fragment_create (NULL, NULL);
1258 nfa_add_states (new, a->states_head, a->states_tail);
1259 nfa_add_states (new, b->states_head, b->states_tail);
1260 new->start = a->start;
1262 automaton_fragment_clear (a);
1263 automaton_fragment_clear (b);
1265 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1269 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1271 * @param ctx context
1274 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1276 struct GNUNET_REGEX_Automaton *a;
1277 struct GNUNET_REGEX_Automaton *new;
1278 struct GNUNET_REGEX_State *start;
1279 struct GNUNET_REGEX_State *end;
1281 a = ctx->stack_tail;
1282 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287 "nfa_add_star_op failed, because there was no element on the stack");
1291 start = nfa_state_create (ctx, 0);
1292 end = nfa_state_create (ctx, 1);
1294 state_add_transition (ctx, start, 0, a->start);
1295 state_add_transition (ctx, start, 0, end);
1296 state_add_transition (ctx, a->end, 0, a->start);
1297 state_add_transition (ctx, a->end, 0, end);
1299 a->end->accepting = 0;
1302 new = nfa_fragment_create (start, end);
1303 nfa_add_states (new, a->states_head, a->states_tail);
1304 automaton_fragment_clear (a);
1306 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1310 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1312 * @param ctx context
1315 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1317 struct GNUNET_REGEX_Automaton *a;
1319 a = ctx->stack_tail;
1320 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1322 state_add_transition (ctx, a->end, 0, a->start);
1324 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1328 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1330 * @param ctx context
1333 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1335 struct GNUNET_REGEX_Automaton *a;
1336 struct GNUNET_REGEX_Automaton *new;
1337 struct GNUNET_REGEX_State *start;
1338 struct GNUNET_REGEX_State *end;
1340 a = ctx->stack_tail;
1341 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1346 "nfa_add_question_op failed, because there was no element on the stack");
1350 start = nfa_state_create (ctx, 0);
1351 end = nfa_state_create (ctx, 1);
1353 state_add_transition (ctx, start, 0, a->start);
1354 state_add_transition (ctx, start, 0, end);
1355 state_add_transition (ctx, a->end, 0, end);
1357 a->end->accepting = 0;
1359 new = nfa_fragment_create (start, end);
1360 nfa_add_states (new, a->states_head, a->states_tail);
1361 automaton_fragment_clear (a);
1363 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1367 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1368 * that alternates between a and b (a|b)
1370 * @param ctx context
1373 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1375 struct GNUNET_REGEX_Automaton *a;
1376 struct GNUNET_REGEX_Automaton *b;
1377 struct GNUNET_REGEX_Automaton *new;
1378 struct GNUNET_REGEX_State *start;
1379 struct GNUNET_REGEX_State *end;
1381 b = ctx->stack_tail;
1382 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1383 a = ctx->stack_tail;
1384 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1386 start = nfa_state_create (ctx, 0);
1387 end = nfa_state_create (ctx, 1);
1388 state_add_transition (ctx, start, 0, a->start);
1389 state_add_transition (ctx, start, 0, b->start);
1391 state_add_transition (ctx, a->end, 0, end);
1392 state_add_transition (ctx, b->end, 0, end);
1394 a->end->accepting = 0;
1395 b->end->accepting = 0;
1398 new = nfa_fragment_create (start, end);
1399 nfa_add_states (new, a->states_head, a->states_tail);
1400 nfa_add_states (new, b->states_head, b->states_tail);
1401 automaton_fragment_clear (a);
1402 automaton_fragment_clear (b);
1404 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1408 * Adds a new nfa fragment to the stack
1410 * @param ctx context
1411 * @param lit label for nfa transition
1414 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1416 struct GNUNET_REGEX_Automaton *n;
1417 struct GNUNET_REGEX_State *start;
1418 struct GNUNET_REGEX_State *end;
1420 GNUNET_assert (NULL != ctx);
1422 start = nfa_state_create (ctx, 0);
1423 end = nfa_state_create (ctx, 1);
1424 state_add_transition (ctx, start, lit, end);
1425 n = nfa_fragment_create (start, end);
1426 GNUNET_assert (NULL != n);
1427 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1431 * Initialize a new context
1433 * @param ctx context
1436 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1444 ctx->transition_id = 0;
1446 ctx->stack_head = NULL;
1447 ctx->stack_tail = NULL;
1451 * Construct an NFA by parsing the regex string of length 'len'.
1453 * @param regex regular expression string
1454 * @param len length of the string
1456 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1458 struct GNUNET_REGEX_Automaton *
1459 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1461 struct GNUNET_REGEX_Context ctx;
1462 struct GNUNET_REGEX_Automaton *nfa;
1466 unsigned int altcount;
1467 unsigned int atomcount;
1468 unsigned int pcount;
1475 GNUNET_REGEX_context_init (&ctx);
1484 for (count = 0; count < len && *regexp; count++, regexp++)
1492 nfa_add_concatenation (&ctx);
1494 GNUNET_array_grow (p, pcount, pcount + 1);
1495 p[pcount - 1].altcount = altcount;
1496 p[pcount - 1].atomcount = atomcount;
1503 error_msg = "Cannot append '|' to nothing";
1506 while (--atomcount > 0)
1507 nfa_add_concatenation (&ctx);
1513 error_msg = "Missing opening '('";
1518 // Ignore this: "()"
1520 altcount = p[pcount].altcount;
1521 atomcount = p[pcount].atomcount;
1524 while (--atomcount > 0)
1525 nfa_add_concatenation (&ctx);
1526 for (; altcount > 0; altcount--)
1527 nfa_add_alternation (&ctx);
1529 altcount = p[pcount].altcount;
1530 atomcount = p[pcount].atomcount;
1536 error_msg = "Cannot append '+' to nothing";
1539 nfa_add_star_op (&ctx);
1544 error_msg = "Cannot append '+' to nothing";
1547 nfa_add_plus_op (&ctx);
1552 error_msg = "Cannot append '?' to nothing";
1555 nfa_add_question_op (&ctx);
1557 case 92: /* escape: \ */
1564 nfa_add_concatenation (&ctx);
1566 nfa_add_label (&ctx, *regexp);
1573 error_msg = "Unbalanced parenthesis";
1576 while (--atomcount > 0)
1577 nfa_add_concatenation (&ctx);
1578 for (; altcount > 0; altcount--)
1579 nfa_add_alternation (&ctx);
1584 nfa = ctx.stack_tail;
1585 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1588 if (NULL != ctx.stack_head)
1590 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1598 if (NULL != error_msg)
1599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1602 while (NULL != ctx.stack_tail)
1604 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1605 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1612 * Construct DFA for the given 'regex' of length 'len'
1614 * @param regex regular expression string
1615 * @param len length of the regular expression
1617 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1619 struct GNUNET_REGEX_Automaton *
1620 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1622 struct GNUNET_REGEX_Context ctx;
1623 struct GNUNET_REGEX_Automaton *dfa;
1624 struct GNUNET_REGEX_Automaton *nfa;
1625 struct GNUNET_REGEX_StateSet *tmp;
1626 struct GNUNET_REGEX_StateSet *nfa_set;
1627 struct GNUNET_REGEX_StateSet *dfa_stack;
1628 struct Transition *ctran;
1629 struct GNUNET_REGEX_State *dfa_state;
1630 struct GNUNET_REGEX_State *new_dfa_state;
1631 struct GNUNET_REGEX_State *state_contains;
1632 struct GNUNET_REGEX_State *state_iter;
1634 GNUNET_REGEX_context_init (&ctx);
1637 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1642 "Could not create DFA, because NFA creation failed\n");
1646 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1649 // Create DFA start state from epsilon closure
1650 dfa_stack = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1651 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1652 dfa->start = dfa_state_create (&ctx, nfa_set);
1653 automaton_add_state (dfa, dfa->start);
1654 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1656 // Create dfa states by combining nfa states
1657 while (dfa_stack->len > 0)
1659 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1660 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1662 for (ctran = dfa_state->transitions_head; NULL != ctran;
1663 ctran = ctran->next)
1665 if (0 != ctran->label && NULL == ctran->to_state)
1667 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1668 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1669 state_set_clear (tmp);
1670 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1671 state_contains = NULL;
1672 for (state_iter = dfa->states_head; NULL != state_iter;
1673 state_iter = state_iter->next)
1676 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1677 state_contains = state_iter;
1680 if (NULL == state_contains)
1682 automaton_add_state (dfa, new_dfa_state);
1683 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1685 ctran->to_state = new_dfa_state;
1689 ctran->to_state = state_contains;
1690 automaton_destroy_state (new_dfa_state);
1696 GNUNET_free (dfa_stack);
1697 GNUNET_REGEX_automaton_destroy (nfa);
1699 dfa_minimize (&ctx, dfa);
1700 scc_tarjan (&ctx, dfa);
1706 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1709 * @param a automaton to be destroyed
1712 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1714 struct GNUNET_REGEX_State *s;
1715 struct GNUNET_REGEX_State *next_state;
1720 for (s = a->states_head; NULL != s;)
1722 next_state = s->next;
1723 automaton_destroy_state (s);
1731 * Save the given automaton as a GraphViz dot file
1733 * @param a the automaton to be saved
1734 * @param filename where to save the file
1737 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1738 const char *filename)
1740 struct GNUNET_REGEX_State *s;
1741 struct Transition *ctran;
1743 char *s_tran = NULL;
1750 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1754 if (NULL == filename || strlen (filename) < 1)
1756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1760 p = fopen (filename, "w");
1764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1769 start = "digraph G {\nrankdir=LR\n";
1770 fwrite (start, strlen (start), 1, p);
1772 for (s = a->states_head; NULL != s; s = s->next)
1776 GNUNET_asprintf (&s_acc,
1777 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1778 s->name, s->scc_id);
1779 fwrite (s_acc, strlen (s_acc), 1, p);
1780 GNUNET_free (s_acc);
1784 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
1786 fwrite (s_acc, strlen (s_acc), 1, p);
1787 GNUNET_free (s_acc);
1792 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1794 if (NULL == ctran->to_state)
1796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1797 "Transition from State %i has has no state for transitioning\n",
1802 if (ctran->label == 0)
1804 GNUNET_asprintf (&s_tran,
1805 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
1806 s->name, ctran->to_state->name, s->scc_id);
1810 GNUNET_asprintf (&s_tran,
1811 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
1812 s->name, ctran->to_state->name, ctran->label,
1816 fwrite (s_tran, strlen (s_tran), 1, p);
1817 GNUNET_free (s_tran);
1822 fwrite (end, strlen (end), 1, p);
1827 * Evaluates the given string using the given DFA automaton
1829 * @param a automaton, type must be DFA
1830 * @param string string that should be evaluated
1832 * @return 0 if string matches, non 0 otherwise
1835 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1838 struct GNUNET_REGEX_State *s;
1842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1843 "Tried to evaluate DFA, but NFA automaton given");
1849 for (strp = string; NULL != strp && *strp; strp++)
1851 s = dfa_move (s, *strp);
1856 if (NULL != s && s->accepting)
1863 * Evaluates the given string using the given NFA automaton
1865 * @param a automaton, type must be NFA
1866 * @param string string that should be evaluated
1868 * @return 0 if string matches, non 0 otherwise
1871 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1874 struct GNUNET_REGEX_State *s;
1875 struct GNUNET_REGEX_StateSet *sset;
1876 struct GNUNET_REGEX_StateSet *new_sset;
1882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1883 "Tried to evaluate NFA, but DFA automaton given");
1889 sset = nfa_closure_create (a, a->start, 0);
1891 for (strp = string; NULL != strp && *strp; strp++)
1893 new_sset = nfa_closure_set_create (a, sset, *strp);
1894 state_set_clear (sset);
1895 sset = nfa_closure_set_create (a, new_sset, 0);
1896 state_set_clear (new_sset);
1899 for (i = 0; i < sset->len; i++)
1901 s = sset->states[i];
1902 if (NULL != s && s->accepting)
1909 state_set_clear (sset);
1914 * Evaluates the given 'string' against the given compiled regex
1916 * @param a automaton
1917 * @param string string to check
1919 * @return 0 if string matches, non 0 otherwise
1922 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1929 result = evaluate_dfa (a, string);
1932 result = evaluate_nfa (a, string);
1935 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1936 "Evaluating regex failed, automaton has no type!\n");
1937 result = GNUNET_SYSERR;
1945 * Get the first key for the given 'input_string'. This hashes
1946 * the first x bits of the 'input_strings'.
1948 * @param input_string string.
1949 * @param string_len length of the 'input_string'.
1950 * @param key pointer to where to write the hash code.
1952 * @return number of bits of 'input_string' that have been consumed
1953 * to construct the key
1956 GNUNET_REGEX_get_first_key (const char *input_string,
1957 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,
1985 const GNUNET_HashCode *key)
1991 * Get all edges leaving state 's'.
1994 * @param edges all edges leaving 's'.
1996 * @return number of edges.
1999 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
2001 struct Transition *t;
2009 for (t = s->transitions_head; NULL != t; t = t->next)
2011 if (NULL != t->to_state)
2013 edges[count].label = &t->label;
2014 edges[count].destination = t->to_state->hash;
2022 * Iterate over all edges helper function starting from state 's', calling
2023 * iterator on for each edge.
2026 * @param iterator iterator function called for each edge.
2027 * @param iterator_cls closure.
2030 iterate_edge (struct GNUNET_REGEX_State *s,
2031 GNUNET_REGEX_KeyIterator iterator,
2034 struct Transition *t;
2035 struct GNUNET_REGEX_Edge edges[s->transition_count];
2036 unsigned int num_edges;
2038 if (GNUNET_NO == s->marked)
2040 s->marked = GNUNET_YES;
2042 num_edges = state_get_edges (s, edges);
2044 iterator (iterator_cls,
2052 for (t = s->transitions_head; NULL != t; t = t->next)
2053 iterate_edge (t->to_state, iterator, iterator_cls);
2058 * Iterate over all edges starting from start state of automaton 'a'. Calling
2059 * iterator for each edge.
2061 * @param a automaton.
2062 * @param iterator iterator called for each edge.
2063 * @param iterator_cls closure.
2066 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2067 GNUNET_REGEX_KeyIterator iterator,
2070 iterate_edge (a->start, iterator, iterator_cls);