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;
126 * Computed regex (result of RX->NFA->DFA->RX)
128 char *computed_regex;
132 * A state. Can be used in DFA and NFA automatons.
134 struct GNUNET_REGEX_State
137 * This is a linked list.
139 struct GNUNET_REGEX_State *prev;
142 * This is a linked list.
144 struct GNUNET_REGEX_State *next;
152 * If this is an accepting state or not.
157 * Marking of the state. This is used for marking all visited states when
158 * traversing all states of an automaton and for cases where the state id
159 * cannot be used (dfa minimization).
164 * Marking the state as contained. This is used for checking, if the state is
165 * contained in a set in constant time
170 * Marking the state as part of an SCC (Strongly Connected Component). All
171 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
172 * is not a part of any SCC.
177 * Used for SCC detection.
182 * Used for SCC detection.
187 * Human readable name of the automaton. Used for debugging and graph
195 GNUNET_HashCode hash;
198 * Proof for this state.
203 * Number of transitions from this state to other states.
205 unsigned int transition_count;
208 * DLL of transitions.
210 struct Transition *transitions_head;
213 * DLL of transitions.
215 struct Transition *transitions_tail;
218 * Set of states on which this state is based on. Used when creating a DFA out
219 * of several NFA states.
221 struct GNUNET_REGEX_StateSet *nfa_set;
225 * Transition between two states. Each state can have 0-n transitions. If label
226 * is 0, this is considered to be an epsilon transition.
231 * This is a linked list.
233 struct Transition *prev;
236 * This is a linked list.
238 struct Transition *next;
241 * Unique id of this transition.
246 * Label for this transition. This is basically the edge label for the graph.
251 * State to which this transition leads.
253 struct GNUNET_REGEX_State *to_state;
256 * State from which this transition origins.
258 struct GNUNET_REGEX_State *from_state;
261 * Mark this transition. For example when reversing the automaton.
269 struct GNUNET_REGEX_StateSet
274 struct GNUNET_REGEX_State **states;
277 * Length of the 'states' array.
283 * Debug helper functions
286 debug_print_transitions (struct GNUNET_REGEX_State *);
289 debug_print_state (struct GNUNET_REGEX_State *s)
293 if (NULL == s->proof)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
300 s->id, s->name, s->marked, s->accepting, s->scc_id,
301 s->transition_count, proof);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
304 debug_print_transitions (s);
308 debug_print_states (struct GNUNET_REGEX_Automaton *a)
310 struct GNUNET_REGEX_State *s;
312 for (s = a->states_head; NULL != s; s = s->next)
313 debug_print_state (s);
317 debug_print_transition (struct Transition *t)
331 if (NULL == t->to_state)
334 to_state = t->to_state->name;
336 if (NULL == t->from_state)
339 from_state = t->from_state->name;
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
342 t->id, from_state, label, to_state);
346 debug_print_transitions (struct GNUNET_REGEX_State *s)
348 struct Transition *t;
350 for (t = s->transitions_head; NULL != t; t = t->next)
351 debug_print_transition (t);
355 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
356 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
357 * SCCs inside an automaton.
360 * @param v start vertex
361 * @param index current index
362 * @param stack stack for saving all SCCs
363 * @param stack_size current size of the stack
366 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
367 struct GNUNET_REGEX_State *v, int *index,
368 struct GNUNET_REGEX_State **stack,
369 unsigned int *stack_size)
371 struct GNUNET_REGEX_State *w;
372 struct Transition *t;
377 stack[(*stack_size)++] = v;
380 for (t = v->transitions_head; NULL != t; t = t->next)
383 if (NULL != w && w->index < 0)
385 scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
386 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
388 else if (0 != w->contained)
389 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
392 if (v->lowlink == v->index)
394 w = stack[--(*stack_size)];
402 w->scc_id = ctx->scc_id;
403 w = stack[--(*stack_size)];
406 w->scc_id = ctx->scc_id;
412 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
413 * SCCs will be marked using the scc_id on each state.
419 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
422 struct GNUNET_REGEX_State *v;
423 struct GNUNET_REGEX_State *stack[a->state_count];
424 unsigned int stack_size;
426 for (v = a->states_head; NULL != v; v = v->next)
436 for (v = a->states_head; NULL != v; v = v->next)
439 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
444 * Adds a transition from one state to another on 'label'. Does not add
448 * @param from_state starting state for the transition
449 * @param label transition label
450 * @param to_state state to where the transition should point to
453 state_add_transition (struct GNUNET_REGEX_Context *ctx,
454 struct GNUNET_REGEX_State *from_state, const char label,
455 struct GNUNET_REGEX_State *to_state)
458 struct Transition *t;
459 struct Transition *oth;
461 if (NULL == from_state)
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
467 // Do not add duplicate state transitions
469 for (t = from_state->transitions_head; NULL != t; t = t->next)
471 if (t->to_state == to_state && t->label == label &&
472 t->from_state == from_state)
482 // sort transitions by label
483 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
485 if (oth->label > label)
489 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 // Add outgoing transition to 'from_state'
496 from_state->transition_count++;
497 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
498 from_state->transitions_tail, oth, t);
502 * Compare two states. Used for sorting.
504 * @param a first state
505 * @param b second state
507 * @return an integer less than, equal to, or greater than zero
508 * if the first argument is considered to be respectively
509 * less than, equal to, or greater than the second.
512 state_compare (const void *a, const void *b)
514 struct GNUNET_REGEX_State **s1;
515 struct GNUNET_REGEX_State **s2;
517 s1 = (struct GNUNET_REGEX_State **) a;
518 s2 = (struct GNUNET_REGEX_State **) b;
520 return (*s1)->id - (*s2)->id;
524 * Get all edges leaving state 's'.
527 * @param edges all edges leaving 's'.
529 * @return number of edges.
532 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
534 struct Transition *t;
542 for (t = s->transitions_head; NULL != t; t = t->next)
544 if (NULL != t->to_state)
546 edges[count].label = &t->label;
547 edges[count].destination = t->to_state->hash;
555 * Compare to state sets by comparing the id's of the states that are contained
556 * in each set. Both sets are expected to be sorted by id!
558 * @param sset1 first state set
559 * @param sset2 second state set
561 * @return an integer less than, equal to, or greater than zero
562 * if the first argument is considered to be respectively
563 * less than, equal to, or greater than the second.
566 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
567 struct GNUNET_REGEX_StateSet *sset2)
572 if (NULL == sset1 || NULL == sset2)
575 result = sset1->len - sset2->len;
577 for (i = 0; i < sset1->len; i++)
582 result = state_compare (&sset1->states[i], &sset2->states[i]);
588 * Clears the given StateSet 'set'
590 * @param set set to be cleared
593 state_set_clear (struct GNUNET_REGEX_StateSet *set)
597 GNUNET_free_non_null (set->states);
603 * Clears an automaton fragment. Does not destroy the states inside the
606 * @param a automaton to be cleared
609 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
616 a->states_head = NULL;
617 a->states_tail = NULL;
623 * Frees the memory used by State 's'
625 * @param s state that should be destroyed
628 automaton_destroy_state (struct GNUNET_REGEX_State *s)
630 struct Transition *t;
631 struct Transition *next_t;
636 GNUNET_free_non_null (s->name);
637 GNUNET_free_non_null (s->proof);
639 for (t = s->transitions_head; NULL != t; t = next_t)
642 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
646 state_set_clear (s->nfa_set);
652 * Remove a state from the given automaton 'a'. Always use this function when
653 * altering the states of an automaton. Will also remove all transitions leading
654 * to this state, before destroying it.
657 * @param s state to remove
660 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
661 struct GNUNET_REGEX_State *s)
663 struct GNUNET_REGEX_State *ss;
664 struct GNUNET_REGEX_State *s_check;
665 struct Transition *t_check;
667 if (NULL == a || NULL == s)
672 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
675 // remove all transitions leading to this state
676 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
678 for (t_check = s_check->transitions_head; NULL != t_check;
679 t_check = t_check->next)
681 if (t_check->to_state == ss)
683 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
684 s_check->transitions_tail, t_check);
685 s_check->transition_count--;
690 automaton_destroy_state (ss);
694 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
699 * @param s1 first state
700 * @param s2 second state, will be destroyed
703 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
704 struct GNUNET_REGEX_Automaton *a,
705 struct GNUNET_REGEX_State *s1,
706 struct GNUNET_REGEX_State *s2)
708 struct GNUNET_REGEX_State *s_check;
709 struct Transition *t_check;
712 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
717 // 1. Make all transitions pointing to s2 point to s1
718 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
720 for (t_check = s_check->transitions_head; NULL != t_check;
721 t_check = t_check->next)
723 if (s2 == t_check->to_state)
724 t_check->to_state = s1;
728 // 2. Add all transitions from s2 to sX to s1
729 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
731 if (t_check->to_state != s1)
732 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
735 // 3. Rename s1 to {s1,s2}
736 new_name = GNUNET_strdup (s1->name);
737 GNUNET_free_non_null (s1->name);
738 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
739 GNUNET_free (new_name);
742 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
744 automaton_destroy_state (s2);
748 * Add a state to the automaton 'a', always use this function to alter the
749 * states DLL of the automaton.
751 * @param a automaton to add the state to
752 * @param s state that should be added
755 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
756 struct GNUNET_REGEX_State *s)
758 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
763 * Function that is called with each state, when traversing an automaton.
768 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
769 struct GNUNET_REGEX_State * s);
772 * Traverses all states that are reachable from state 's'. Expects the states to
773 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
776 * @param cls closure.
777 * @param s start state.
778 * @param action action to be performed on each state.
781 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
782 GNUNET_REGEX_traverse_action action)
784 struct Transition *t;
786 if (GNUNET_NO == s->marked)
788 s->marked = GNUNET_YES;
793 for (t = s->transitions_head; NULL != t; t = t->next)
794 automaton_state_traverse (cls, t->to_state, action);
799 * Traverses the given automaton from it's start state, visiting all reachable
800 * states and calling 'action' on each one of them.
802 * @param cls closure.
803 * @param a automaton.
804 * @param action action to be performed on each state.
807 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
808 GNUNET_REGEX_traverse_action action)
810 struct GNUNET_REGEX_State *s;
812 for (s = a->states_head; NULL != s; s = s->next)
813 s->marked = GNUNET_NO;
815 automaton_state_traverse (cls, a->start, action);
819 * Create proofs for all states in the given automaton. Implementation of the
820 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
821 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
823 * @param a automaton.
826 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
828 struct GNUNET_REGEX_State *s;
829 struct Transition *t;
834 struct GNUNET_REGEX_State *states[a->state_count];
835 char *R_last[a->state_count][a->state_count];
836 char *R_cur[a->state_count][a->state_count];
844 char *complete_regex;
850 for (i = 0, s = a->states_head; NULL != s; s = s->next, i++)
857 for (i = 0; i < n; i++)
859 for (j = 0; j < n; j++)
863 for (t = states[i]->transitions_head; NULL != t; t = t->next)
865 if (t->to_state == states[j])
867 if (NULL == R_last[i][j])
868 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
872 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
880 if (NULL == R_last[i][j])
881 GNUNET_asprintf (&R_last[i][j], "");
882 else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
885 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
889 else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
892 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
899 for (k = 0; k < n; k++)
901 for (i = 0; i < n; i++)
903 for (j = 0; j < n; j++)
909 if (NULL == R_last[i][k] || NULL == R_last[k][j])
911 if (NULL != R_last[i][j])
912 R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
915 /*else if ((NULL == R_last[i][j] || 0 == strlen (R_last[i][j])) && */
916 /*NULL != R_last[k][k] && 0 < strlen (R_last[k][k]) && */
917 /*NULL != R_last[k][j] && 0 < strlen (R_last[k][j]) && */
918 /*0 == strncmp (R_last[k][k], R_last[k][j], (strlen (R_last[k][k]) - 1)) && */
919 /*R_last[i][k][0] == R_last[k][k][strlen (R_last[k][k]) - 1]) */
921 /*GNUNET_asprintf (&R_cur[i][j], "(%s%s)+", R_last[i][k], R_last[k][j]); */
925 // R(k)ij = R(k-1)ij + R(k-1)ik (R(k-1)kk)* R(k-1)kj
926 length_l = (NULL == R_last[i][j]) ? 1 : strlen (R_last[i][j]) + 1;
928 snprintf (NULL, 0, "%s(%s)*%s", R_last[i][k], R_last[k][k],
930 R_cur_l = GNUNET_malloc (length_l);
931 R_cur_r = GNUNET_malloc (length_r);
933 if (NULL != R_last[i][j])
934 strcat (R_cur_l, R_last[i][j]);
936 if (NULL != R_last[i][k] && 0 != strcmp (R_last[i][k], R_last[k][k]))
937 strcat (R_cur_r, R_last[i][k]);
939 if (NULL != R_last[k][k] && 0 != strcmp (R_last[k][k], ""))
941 if (R_last[k][k][0] == '(' &&
942 R_last[k][k][strlen (R_last[k][k]) - 1] == ')')
944 strcat (R_cur_r, R_last[k][k]);
948 strcat (R_cur_r, "(");
949 strcat (R_cur_r, R_last[k][k]);
950 strcat (R_cur_r, ")");
953 if (0 == strcmp (R_last[i][k], R_last[k][k]) ||
954 0 == strcmp (R_last[k][k], R_last[k][j]))
955 strcat (R_cur_r, "+");
957 strcat (R_cur_r, "*");
960 if (NULL != R_last[k][j] && 0 != strcmp (R_last[k][k], R_last[k][j]))
961 strcat (R_cur_r, R_last[k][j]);
963 // simplifications...
965 // | is idempotent: a | a = a for all a in A
966 if (0 == strcmp (R_cur_l, R_cur_r) || 0 == strcmp (R_cur_l, "") ||
967 0 == strcmp (R_cur_r, ""))
969 if (0 == strcmp (R_cur_l, ""))
970 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_r);
972 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_l);
974 // TODO: in theory only applicable if (e + a) | (e + a)(e + a)*(e+a)
975 // where e means epsilon... check if practical!
977 else if (R_last[i][j] == R_last[i][k] && R_last[i][k] == R_last[k][k]
978 && R_last[k][k] == R_last[k][j])
980 if (1 >= strlen (R_last[k][k]) ||
981 (R_last[k][k][0] == '(' &&
982 R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
983 GNUNET_asprintf (&R_cur[i][j], "%s*", R_last[k][k]);
985 GNUNET_asprintf (&R_cur[i][j], "(%s)*", R_last[k][k]);
987 // a | a b* b => a | a b | a b b | ... => a b*
988 else if (R_last[i][j] == R_last[i][k] && R_last[k][k] == R_last[k][j])
990 // if a is xb then a b* becomes x b b* = x b+
992 s_cnt = strlen (R_last[k][k]);
993 l_cnt = strlen (R_last[i][k]);
994 R_cur[i][j] = GNUNET_malloc (s_cnt + l_cnt + 4);
996 if (l_cnt > 0 && s_cnt >= l_cnt)
997 for (; s_cnt > 0; s_cnt--, l_cnt--)
998 if (R_last[i][k][l_cnt] != R_last[k][k][s_cnt])
1001 if (strlen (R_last[i][k]) > 0 && 0 == s_cnt && 0 <= l_cnt)
1002 strncat (R_cur[i][j], R_last[i][k], l_cnt);
1004 strcat (R_cur[i][j], R_last[i][k]);
1006 if (1 >= strlen (R_last[k][k]) &&
1007 !(R_last[k][k][0] == '(' &&
1008 R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
1010 strcat (R_cur[i][j], "(");
1011 strcat (R_cur[i][j], R_last[k][k]);
1012 strcat (R_cur[i][j], ")");
1015 strcat (R_cur[i][j], R_last[k][k]);
1017 if (0 == s_cnt && 0 <= l_cnt)
1018 strcat (R_cur[i][j], "+");
1020 strcat (R_cur[i][j], "*");
1022 // a | b b* a => a | b a | b b a | ... => b* a
1023 else if (R_last[i][j] == R_last[k][j] && R_last[i][k] == R_last[k][k])
1025 // if a is bx then b* a becomes b+ x
1028 s_cnt = strlen (R_last[k][k]);
1029 l_cnt = strlen (R_last[k][j]);
1030 R_cur[i][j] = GNUNET_malloc (s_cnt + l_cnt + 4);
1034 if (l_cnt > 0 && s_cnt >= l_cnt)
1035 for (bla = 0; bla < s_cnt; bla++)
1036 if (R_last[k][k][bla] != R_last[k][j][bla])
1039 if (1 >= strlen (R_last[k][k]) &&
1040 !(R_last[k][k][0] == '(' &&
1041 R_last[k][k][strlen (R_last[k][k]) - 1] == ')'))
1043 strcat (R_cur[i][j], "(");
1044 strcat (R_cur[i][j], R_last[k][k]);
1045 strcat (R_cur[i][j], ")");
1048 strcat (R_cur[i][j], R_last[k][k]);
1051 strcat (R_cur[i][j], "+");
1053 strcat (R_cur[i][j], "*");
1055 if (strlen (R_last[k][j]) > 0 && bla == s_cnt)
1056 strcat (R_cur[i][j], &R_last[k][j][bla]);
1058 strcat (R_cur[i][j], R_last[k][j]);
1061 GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
1063 GNUNET_free_non_null (R_cur_l);
1064 GNUNET_free_non_null (R_cur_r);
1069 // set R_last = R_cur
1070 for (i = 0; i < n; i++)
1072 for (j = 0; j < n; j++)
1074 GNUNET_free_non_null (R_last[i][j]);
1076 if (NULL != R_cur[i][j])
1078 R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
1079 GNUNET_free (R_cur[i][j]);
1086 // assign proofs and hashes
1087 for (i = 0; i < n; i++)
1089 states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
1090 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1094 // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
1095 complete_regex = NULL;
1096 for (i = 0; i < n; i++)
1098 if (states[i]->accepting)
1100 if (NULL == complete_regex)
1101 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->marked][i]);
1102 else if (NULL != R_last[a->start->marked][i] &&
1103 0 != strcmp (R_last[a->start->marked][i], ""))
1105 temp = complete_regex;
1106 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1107 R_last[a->start->marked][i]);
1112 a->computed_regex = complete_regex;
1115 for (i = 0; i < n; i++)
1117 for (j = 0; j < n; j++)
1118 GNUNET_free_non_null (R_last[i][j]);
1123 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1124 * automaton_destroy_state.
1126 * @param ctx context
1127 * @param nfa_states set of NFA states on which the DFA should be based on
1129 * @return new DFA state
1131 static struct GNUNET_REGEX_State *
1132 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1133 struct GNUNET_REGEX_StateSet *nfa_states)
1135 struct GNUNET_REGEX_State *s;
1138 struct GNUNET_REGEX_State *cstate;
1139 struct Transition *ctran;
1141 struct Transition *t;
1144 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1145 s->id = ctx->state_id++;
1155 if (NULL == nfa_states)
1157 GNUNET_asprintf (&s->name, "s%i", s->id);
1161 s->nfa_set = nfa_states;
1163 if (nfa_states->len < 1)
1166 // Create a name based on 'sset'
1167 s->name = GNUNET_malloc (sizeof (char) * 2);
1168 strcat (s->name, "{");
1171 for (i = 0; i < nfa_states->len; i++)
1173 cstate = nfa_states->states[i];
1174 GNUNET_asprintf (&name, "%i,", cstate->id);
1178 len = strlen (s->name) + strlen (name) + 1;
1179 s->name = GNUNET_realloc (s->name, len);
1180 strcat (s->name, name);
1185 // Add a transition for each distinct label to NULL state
1186 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1188 if (0 != ctran->label)
1192 for (t = s->transitions_head; NULL != t; t = t->next)
1194 if (t->label == ctran->label)
1202 state_add_transition (ctx, s, ctran->label, NULL);
1206 // If the nfa_states contain an accepting state, the new dfa state is also
1208 if (cstate->accepting)
1212 s->name[strlen (s->name) - 1] = '}';
1218 * Move from the given state 's' to the next state on transition 'label'
1220 * @param s starting state
1221 * @param label edge label to follow
1223 * @return new state or NULL, if transition on label not possible
1225 static struct GNUNET_REGEX_State *
1226 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1228 struct Transition *t;
1229 struct GNUNET_REGEX_State *new_s;
1236 for (t = s->transitions_head; NULL != t; t = t->next)
1238 if (label == t->label)
1240 new_s = t->to_state;
1249 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1250 * states that are not reachable from the starting state.
1252 * @param a DFA automaton
1255 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1257 struct GNUNET_REGEX_State *s;
1258 struct GNUNET_REGEX_State *s_next;
1260 // 1. unmark all states
1261 for (s = a->states_head; NULL != s; s = s->next)
1262 s->marked = GNUNET_NO;
1264 // 2. traverse dfa from start state and mark all visited states
1265 automaton_traverse (NULL, a, NULL);
1267 // 3. delete all states that were not visited
1268 for (s = a->states_head; NULL != s; s = s_next)
1271 if (GNUNET_NO == s->marked)
1272 automaton_remove_state (a, s);
1277 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1278 * not transition to any other state but themselfes.
1280 * @param a DFA automaton
1283 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1285 struct GNUNET_REGEX_State *s;
1286 struct Transition *t;
1289 GNUNET_assert (DFA == a->type);
1291 for (s = a->states_head; NULL != s; s = s->next)
1297 for (t = s->transitions_head; NULL != t; t = t->next)
1299 if (NULL != t->to_state && t->to_state != s)
1309 // state s is dead, remove it
1310 automaton_remove_state (a, s);
1315 * Merge all non distinguishable states in the DFA 'a'
1317 * @param ctx context
1318 * @param a DFA automaton
1321 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1322 struct GNUNET_REGEX_Automaton *a)
1325 int table[a->state_count][a->state_count];
1326 struct GNUNET_REGEX_State *s1;
1327 struct GNUNET_REGEX_State *s2;
1328 struct Transition *t1;
1329 struct Transition *t2;
1330 struct GNUNET_REGEX_State *s1_next;
1331 struct GNUNET_REGEX_State *s2_next;
1333 int num_equal_edges;
1335 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1341 // Mark all pairs of accepting/!accepting states
1342 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1344 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1346 table[s1->marked][s2->marked] = 0;
1348 if ((s1->accepting && !s2->accepting) ||
1349 (!s1->accepting && s2->accepting))
1351 table[s1->marked][s2->marked] = 1;
1356 // Find all equal states
1361 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1363 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1365 if (0 != table[s1->marked][s2->marked])
1368 num_equal_edges = 0;
1369 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1371 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1373 if (t1->label == t2->label)
1376 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1377 0 != table[t2->to_state->marked][t1->to_state->marked])
1379 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1385 if (num_equal_edges != s1->transition_count ||
1386 num_equal_edges != s2->transition_count)
1388 // Make sure ALL edges of possible equal states are the same
1389 table[s1->marked][s2->marked] = -2;
1395 // Merge states that are equal
1396 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1399 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1402 if (table[s1->marked][s2->marked] == 0)
1403 automaton_merge_states (ctx, a, s1, s2);
1409 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1410 * dead states and merging all non distinguishable states
1412 * @param ctx context
1413 * @param a DFA automaton
1416 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1417 struct GNUNET_REGEX_Automaton *a)
1422 GNUNET_assert (DFA == a->type);
1424 // 1. remove unreachable states
1425 dfa_remove_unreachable_states (a);
1427 // 2. remove dead states
1428 dfa_remove_dead_states (a);
1430 // 3. Merge nondistinguishable states
1431 dfa_merge_nondistinguishable_states (ctx, a);
1435 * Creates a new NFA fragment. Needs to be cleared using
1436 * automaton_fragment_clear.
1438 * @param start starting state
1439 * @param end end state
1441 * @return new NFA fragment
1443 static struct GNUNET_REGEX_Automaton *
1444 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1445 struct GNUNET_REGEX_State *end)
1447 struct GNUNET_REGEX_Automaton *n;
1449 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1455 if (NULL == start && NULL == end)
1458 automaton_add_state (n, end);
1459 automaton_add_state (n, start);
1468 * Adds a list of states to the given automaton 'n'.
1470 * @param n automaton to which the states should be added
1471 * @param states_head head of the DLL of states
1472 * @param states_tail tail of the DLL of states
1475 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1476 struct GNUNET_REGEX_State *states_head,
1477 struct GNUNET_REGEX_State *states_tail)
1479 struct GNUNET_REGEX_State *s;
1481 if (NULL == n || NULL == states_head)
1483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1487 if (NULL == n->states_head)
1489 n->states_head = states_head;
1490 n->states_tail = states_tail;
1494 if (NULL != states_head)
1496 n->states_tail->next = states_head;
1497 n->states_tail = states_tail;
1500 for (s = states_head; NULL != s; s = s->next)
1505 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1507 * @param ctx context
1508 * @param accepting is it an accepting state or not
1510 * @return new NFA state
1512 static struct GNUNET_REGEX_State *
1513 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1515 struct GNUNET_REGEX_State *s;
1517 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1518 s->id = ctx->state_id++;
1519 s->accepting = accepting;
1526 GNUNET_asprintf (&s->name, "s%i", s->id);
1532 * Calculates the NFA closure set for the given state.
1534 * @param nfa the NFA containing 's'
1535 * @param s starting point state
1536 * @param label transitioning label on which to base the closure on,
1537 * pass 0 for epsilon transition
1539 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1541 static struct GNUNET_REGEX_StateSet *
1542 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1543 struct GNUNET_REGEX_State *s, const char label)
1545 struct GNUNET_REGEX_StateSet *cls;
1546 struct GNUNET_REGEX_StateSet *cls_check;
1547 struct GNUNET_REGEX_State *clsstate;
1548 struct GNUNET_REGEX_State *currentstate;
1549 struct Transition *ctran;
1554 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1555 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1557 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1558 clsstate->contained = 0;
1560 // Add start state to closure only for epsilon closure
1562 GNUNET_array_append (cls->states, cls->len, s);
1564 GNUNET_array_append (cls_check->states, cls_check->len, s);
1565 while (cls_check->len > 0)
1567 currentstate = cls_check->states[cls_check->len - 1];
1568 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1570 for (ctran = currentstate->transitions_head; NULL != ctran;
1571 ctran = ctran->next)
1573 if (NULL != ctran->to_state && label == ctran->label)
1575 clsstate = ctran->to_state;
1577 if (NULL != clsstate && 0 == clsstate->contained)
1579 GNUNET_array_append (cls->states, cls->len, clsstate);
1580 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1581 clsstate->contained = 1;
1586 GNUNET_assert (0 == cls_check->len);
1587 GNUNET_free (cls_check);
1591 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1598 * Calculates the closure set for the given set of states.
1600 * @param nfa the NFA containing 's'
1601 * @param states list of states on which to base the closure on
1602 * @param label transitioning label for which to base the closure on,
1603 * pass 0 for epsilon transition
1605 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1607 static struct GNUNET_REGEX_StateSet *
1608 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1609 struct GNUNET_REGEX_StateSet *states, const char label)
1611 struct GNUNET_REGEX_State *s;
1612 struct GNUNET_REGEX_StateSet *sset;
1613 struct GNUNET_REGEX_StateSet *cls;
1622 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1624 for (i = 0; i < states->len; i++)
1626 s = states->states[i];
1627 sset = nfa_closure_create (nfa, s, label);
1629 for (j = 0; j < sset->len; j++)
1632 for (k = 0; k < cls->len; k++)
1634 if (sset->states[j]->id == cls->states[k]->id)
1641 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1643 state_set_clear (sset);
1647 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1654 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1656 * @param ctx context
1659 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1661 struct GNUNET_REGEX_Automaton *a;
1662 struct GNUNET_REGEX_Automaton *b;
1663 struct GNUNET_REGEX_Automaton *new;
1665 b = ctx->stack_tail;
1666 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1667 a = ctx->stack_tail;
1668 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1670 state_add_transition (ctx, a->end, 0, b->start);
1671 a->end->accepting = 0;
1672 b->end->accepting = 1;
1674 new = nfa_fragment_create (NULL, NULL);
1675 nfa_add_states (new, a->states_head, a->states_tail);
1676 nfa_add_states (new, b->states_head, b->states_tail);
1677 new->start = a->start;
1679 automaton_fragment_clear (a);
1680 automaton_fragment_clear (b);
1682 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1686 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1688 * @param ctx context
1691 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1693 struct GNUNET_REGEX_Automaton *a;
1694 struct GNUNET_REGEX_Automaton *new;
1695 struct GNUNET_REGEX_State *start;
1696 struct GNUNET_REGEX_State *end;
1698 a = ctx->stack_tail;
1699 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1704 "nfa_add_star_op failed, because there was no element on the stack");
1708 start = nfa_state_create (ctx, 0);
1709 end = nfa_state_create (ctx, 1);
1711 state_add_transition (ctx, start, 0, a->start);
1712 state_add_transition (ctx, start, 0, end);
1713 state_add_transition (ctx, a->end, 0, a->start);
1714 state_add_transition (ctx, a->end, 0, end);
1716 a->end->accepting = 0;
1719 new = nfa_fragment_create (start, end);
1720 nfa_add_states (new, a->states_head, a->states_tail);
1721 automaton_fragment_clear (a);
1723 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1727 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1729 * @param ctx context
1732 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1734 struct GNUNET_REGEX_Automaton *a;
1736 a = ctx->stack_tail;
1737 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1739 state_add_transition (ctx, a->end, 0, a->start);
1741 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1745 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1747 * @param ctx context
1750 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1752 struct GNUNET_REGEX_Automaton *a;
1753 struct GNUNET_REGEX_Automaton *new;
1754 struct GNUNET_REGEX_State *start;
1755 struct GNUNET_REGEX_State *end;
1757 a = ctx->stack_tail;
1758 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1763 "nfa_add_question_op failed, because there was no element on the stack");
1767 start = nfa_state_create (ctx, 0);
1768 end = nfa_state_create (ctx, 1);
1770 state_add_transition (ctx, start, 0, a->start);
1771 state_add_transition (ctx, start, 0, end);
1772 state_add_transition (ctx, a->end, 0, end);
1774 a->end->accepting = 0;
1776 new = nfa_fragment_create (start, end);
1777 nfa_add_states (new, a->states_head, a->states_tail);
1778 automaton_fragment_clear (a);
1780 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1784 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1785 * alternates between a and b (a|b)
1787 * @param ctx context
1790 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1792 struct GNUNET_REGEX_Automaton *a;
1793 struct GNUNET_REGEX_Automaton *b;
1794 struct GNUNET_REGEX_Automaton *new;
1795 struct GNUNET_REGEX_State *start;
1796 struct GNUNET_REGEX_State *end;
1798 b = ctx->stack_tail;
1799 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1800 a = ctx->stack_tail;
1801 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1803 start = nfa_state_create (ctx, 0);
1804 end = nfa_state_create (ctx, 1);
1805 state_add_transition (ctx, start, 0, a->start);
1806 state_add_transition (ctx, start, 0, b->start);
1808 state_add_transition (ctx, a->end, 0, end);
1809 state_add_transition (ctx, b->end, 0, end);
1811 a->end->accepting = 0;
1812 b->end->accepting = 0;
1815 new = nfa_fragment_create (start, end);
1816 nfa_add_states (new, a->states_head, a->states_tail);
1817 nfa_add_states (new, b->states_head, b->states_tail);
1818 automaton_fragment_clear (a);
1819 automaton_fragment_clear (b);
1821 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1825 * Adds a new nfa fragment to the stack
1827 * @param ctx context
1828 * @param lit label for nfa transition
1831 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1833 struct GNUNET_REGEX_Automaton *n;
1834 struct GNUNET_REGEX_State *start;
1835 struct GNUNET_REGEX_State *end;
1837 GNUNET_assert (NULL != ctx);
1839 start = nfa_state_create (ctx, 0);
1840 end = nfa_state_create (ctx, 1);
1841 state_add_transition (ctx, start, lit, end);
1842 n = nfa_fragment_create (start, end);
1843 GNUNET_assert (NULL != n);
1844 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1848 * Initialize a new context
1850 * @param ctx context
1853 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1861 ctx->transition_id = 0;
1863 ctx->stack_head = NULL;
1864 ctx->stack_tail = NULL;
1868 * Construct an NFA by parsing the regex string of length 'len'.
1870 * @param regex regular expression string
1871 * @param len length of the string
1873 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1875 struct GNUNET_REGEX_Automaton *
1876 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1878 struct GNUNET_REGEX_Context ctx;
1879 struct GNUNET_REGEX_Automaton *nfa;
1883 unsigned int altcount;
1884 unsigned int atomcount;
1885 unsigned int pcount;
1892 GNUNET_REGEX_context_init (&ctx);
1901 for (count = 0; count < len && *regexp; count++, regexp++)
1909 nfa_add_concatenation (&ctx);
1911 GNUNET_array_grow (p, pcount, pcount + 1);
1912 p[pcount - 1].altcount = altcount;
1913 p[pcount - 1].atomcount = atomcount;
1920 error_msg = "Cannot append '|' to nothing";
1923 while (--atomcount > 0)
1924 nfa_add_concatenation (&ctx);
1930 error_msg = "Missing opening '('";
1935 // Ignore this: "()"
1937 altcount = p[pcount].altcount;
1938 atomcount = p[pcount].atomcount;
1941 while (--atomcount > 0)
1942 nfa_add_concatenation (&ctx);
1943 for (; altcount > 0; altcount--)
1944 nfa_add_alternation (&ctx);
1946 altcount = p[pcount].altcount;
1947 atomcount = p[pcount].atomcount;
1953 error_msg = "Cannot append '*' to nothing";
1956 nfa_add_star_op (&ctx);
1961 error_msg = "Cannot append '+' to nothing";
1964 nfa_add_plus_op (&ctx);
1969 error_msg = "Cannot append '?' to nothing";
1972 nfa_add_question_op (&ctx);
1974 case 92: /* escape: \ */
1981 nfa_add_concatenation (&ctx);
1983 nfa_add_label (&ctx, *regexp);
1990 error_msg = "Unbalanced parenthesis";
1993 while (--atomcount > 0)
1994 nfa_add_concatenation (&ctx);
1995 for (; altcount > 0; altcount--)
1996 nfa_add_alternation (&ctx);
1998 GNUNET_free_non_null (p);
2000 nfa = ctx.stack_tail;
2001 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2003 if (NULL != ctx.stack_head)
2005 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2009 nfa->regex = GNUNET_strdup (regex);
2014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
2015 if (NULL != error_msg)
2016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2018 GNUNET_free_non_null (p);
2020 while (NULL != ctx.stack_tail)
2022 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
2023 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
2030 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2032 * @param ctx context.
2033 * @param nfa NFA automaton.
2034 * @param dfa DFA automaton.
2035 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2039 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2040 struct GNUNET_REGEX_Automaton *nfa,
2041 struct GNUNET_REGEX_Automaton *dfa,
2042 struct GNUNET_REGEX_State *dfa_state)
2044 struct Transition *ctran;
2045 struct GNUNET_REGEX_State *state_iter;
2046 struct GNUNET_REGEX_State *new_dfa_state;
2047 struct GNUNET_REGEX_State *state_contains;
2048 struct GNUNET_REGEX_StateSet *tmp;
2049 struct GNUNET_REGEX_StateSet *nfa_set;
2051 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2053 if (0 == ctran->label || NULL != ctran->to_state)
2056 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2057 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2058 state_set_clear (tmp);
2059 new_dfa_state = dfa_state_create (ctx, nfa_set);
2060 state_contains = NULL;
2061 for (state_iter = dfa->states_head; NULL != state_iter;
2062 state_iter = state_iter->next)
2064 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2065 state_contains = state_iter;
2068 if (NULL == state_contains)
2070 automaton_add_state (dfa, new_dfa_state);
2071 ctran->to_state = new_dfa_state;
2072 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2076 ctran->to_state = state_contains;
2077 automaton_destroy_state (new_dfa_state);
2083 * Construct DFA for the given 'regex' of length 'len'
2085 * @param regex regular expression string
2086 * @param len length of the regular expression
2088 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2090 struct GNUNET_REGEX_Automaton *
2091 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2093 struct GNUNET_REGEX_Context ctx;
2094 struct GNUNET_REGEX_Automaton *dfa;
2095 struct GNUNET_REGEX_Automaton *nfa;
2096 struct GNUNET_REGEX_StateSet *nfa_set;
2098 GNUNET_REGEX_context_init (&ctx);
2101 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2106 "Could not create DFA, because NFA creation failed\n");
2110 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2112 dfa->regex = GNUNET_strdup (regex);
2114 // Create DFA start state from epsilon closure
2115 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2116 dfa->start = dfa_state_create (&ctx, nfa_set);
2117 automaton_add_state (dfa, dfa->start);
2119 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2121 GNUNET_REGEX_automaton_destroy (nfa);
2124 dfa_minimize (&ctx, dfa);
2127 scc_tarjan (&ctx, dfa);
2129 // Create proofs for all states
2130 automaton_create_proofs (dfa);
2136 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2139 * @param a automaton to be destroyed
2142 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2144 struct GNUNET_REGEX_State *s;
2145 struct GNUNET_REGEX_State *next_state;
2150 GNUNET_free (a->regex);
2151 GNUNET_free_non_null (a->computed_regex);
2153 for (s = a->states_head; NULL != s;)
2155 next_state = s->next;
2156 automaton_destroy_state (s);
2164 * Save a state to an open file pointer. cls is expected to be a file pointer to
2165 * an open file. Used only in conjunction with
2166 * GNUNET_REGEX_automaton_save_graph.
2168 * @param cls file pointer
2172 GNUNET_REGEX_automaton_save_graph_step (void *cls, struct GNUNET_REGEX_State *s)
2175 struct Transition *ctran;
2177 char *s_tran = NULL;
2183 GNUNET_asprintf (&s_acc,
2184 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2185 s->name, s->scc_id);
2189 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
2198 fwrite (s_acc, strlen (s_acc), 1, p);
2199 GNUNET_free (s_acc);
2202 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2204 if (NULL == ctran->to_state)
2206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2207 "Transition from State %i has has no state for transitioning\n",
2212 if (ctran->label == 0)
2214 GNUNET_asprintf (&s_tran,
2215 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2216 s->name, ctran->to_state->name, s->scc_id);
2220 GNUNET_asprintf (&s_tran,
2221 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2222 s->name, ctran->to_state->name, ctran->label, s->scc_id);
2227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2232 fwrite (s_tran, strlen (s_tran), 1, p);
2233 GNUNET_free (s_tran);
2239 * Save the given automaton as a GraphViz dot file
2241 * @param a the automaton to be saved
2242 * @param filename where to save the file
2245 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2246 const char *filename)
2254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2258 if (NULL == filename || strlen (filename) < 1)
2260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2264 p = fopen (filename, "w");
2268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2273 start = "digraph G {\nrankdir=LR\n";
2274 fwrite (start, strlen (start), 1, p);
2276 automaton_traverse (p, a, GNUNET_REGEX_automaton_save_graph_step);
2279 fwrite (end, strlen (end), 1, p);
2284 * Evaluates the given string using the given DFA automaton
2286 * @param a automaton, type must be DFA
2287 * @param string string that should be evaluated
2289 * @return 0 if string matches, non 0 otherwise
2292 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2295 struct GNUNET_REGEX_State *s;
2299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2300 "Tried to evaluate DFA, but NFA automaton given");
2306 // If the string is empty but the starting state is accepting, we accept.
2307 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2310 for (strp = string; NULL != strp && *strp; strp++)
2312 s = dfa_move (s, *strp);
2317 if (NULL != s && s->accepting)
2324 * Evaluates the given string using the given NFA automaton
2326 * @param a automaton, type must be NFA
2327 * @param string string that should be evaluated
2329 * @return 0 if string matches, non 0 otherwise
2332 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2335 struct GNUNET_REGEX_State *s;
2336 struct GNUNET_REGEX_StateSet *sset;
2337 struct GNUNET_REGEX_StateSet *new_sset;
2343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2344 "Tried to evaluate NFA, but DFA automaton given");
2348 // If the string is empty but the starting state is accepting, we accept.
2349 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2354 sset = nfa_closure_create (a, a->start, 0);
2356 for (strp = string; NULL != strp && *strp; strp++)
2358 new_sset = nfa_closure_set_create (a, sset, *strp);
2359 state_set_clear (sset);
2360 sset = nfa_closure_set_create (a, new_sset, 0);
2361 state_set_clear (new_sset);
2364 for (i = 0; i < sset->len; i++)
2366 s = sset->states[i];
2367 if (NULL != s && s->accepting)
2374 state_set_clear (sset);
2379 * Evaluates the given 'string' against the given compiled regex
2381 * @param a automaton
2382 * @param string string to check
2384 * @return 0 if string matches, non 0 otherwise
2387 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2394 result = evaluate_dfa (a, string);
2397 result = evaluate_nfa (a, string);
2400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2401 "Evaluating regex failed, automaton has no type!\n");
2402 result = GNUNET_SYSERR;
2410 * Get the computed regex of the given automaton.
2411 * When constructing the automaton a proof is computed for each state,
2412 * consisting of the regular expression leading to this state. A complete
2413 * regex for the automaton can be computed by combining these proofs.
2414 * As of now this computed regex is only useful for testing.
2417 GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
2422 return a->computed_regex;
2426 * Get the first key for the given 'input_string'. This hashes the first x bits
2427 * of the 'input_strings'.
2429 * @param input_string string.
2430 * @param string_len length of the 'input_string'.
2431 * @param key pointer to where to write the hash code.
2433 * @return number of bits of 'input_string' that have been consumed
2434 * to construct the key
2437 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2438 GNUNET_HashCode * key)
2442 size = string_len < initial_bits ? string_len : initial_bits;
2444 if (NULL == input_string)
2446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2450 GNUNET_CRYPTO_hash (input_string, size, key);
2456 * Check if the given 'proof' matches the given 'key'.
2458 * @param proof partial regex
2461 * @return GNUNET_OK if the proof is valid for the given key
2464 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2470 * Iterate over all edges helper function starting from state 's', calling
2471 * iterator on for each edge.
2474 * @param iterator iterator function called for each edge.
2475 * @param iterator_cls closure.
2478 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2481 struct Transition *t;
2482 struct GNUNET_REGEX_Edge edges[s->transition_count];
2483 unsigned int num_edges;
2485 if (GNUNET_YES != s->marked)
2487 s->marked = GNUNET_YES;
2489 num_edges = state_get_edges (s, edges);
2491 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2493 for (t = s->transitions_head; NULL != t; t = t->next)
2494 iterate_edge (t->to_state, iterator, iterator_cls);
2499 * Iterate over all edges starting from start state of automaton 'a'. Calling
2500 * iterator for each edge.
2502 * @param a automaton.
2503 * @param iterator iterator called for each edge.
2504 * @param iterator_cls closure.
2507 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2508 GNUNET_REGEX_KeyIterator iterator,
2511 struct GNUNET_REGEX_State *s;
2513 for (s = a->states_head; NULL != s; s = s->next)
2514 s->marked = GNUNET_NO;
2516 iterate_edge (a->start, iterator, iterator_cls);