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;
460 if (NULL == from_state)
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
466 // Do not add duplicate state transitions
468 for (t = from_state->transitions_head; NULL != t; t = t->next)
470 if (t->to_state == to_state && t->label == label &&
471 t->from_state == from_state)
481 t = GNUNET_malloc (sizeof (struct Transition));
482 t->id = ctx->transition_id++;
484 t->to_state = to_state;
485 t->from_state = from_state;
487 // Add outgoing transition to 'from_state'
488 from_state->transition_count++;
489 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
490 from_state->transitions_tail, t);
494 * Compare two states. Used for sorting.
496 * @param a first state
497 * @param b second state
499 * @return an integer less than, equal to, or greater than zero
500 * if the first argument is considered to be respectively
501 * less than, equal to, or greater than the second.
504 state_compare (const void *a, const void *b)
506 struct GNUNET_REGEX_State **s1;
507 struct GNUNET_REGEX_State **s2;
509 s1 = (struct GNUNET_REGEX_State **) a;
510 s2 = (struct GNUNET_REGEX_State **) b;
512 return (*s1)->id - (*s2)->id;
516 * Get all edges leaving state 's'.
519 * @param edges all edges leaving 's'.
521 * @return number of edges.
524 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
526 struct Transition *t;
534 for (t = s->transitions_head; NULL != t; t = t->next)
536 if (NULL != t->to_state)
538 edges[count].label = &t->label;
539 edges[count].destination = t->to_state->hash;
547 * Compare to state sets by comparing the id's of the states that are contained
548 * in each set. Both sets are expected to be sorted by id!
550 * @param sset1 first state set
551 * @param sset2 second state set
553 * @return an integer less than, equal to, or greater than zero
554 * if the first argument is considered to be respectively
555 * less than, equal to, or greater than the second.
558 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
559 struct GNUNET_REGEX_StateSet *sset2)
564 if (NULL == sset1 || NULL == sset2)
567 result = sset1->len - sset2->len;
569 for (i = 0; i < sset1->len; i++)
574 result = state_compare (&sset1->states[i], &sset2->states[i]);
580 * Clears the given StateSet 'set'
582 * @param set set to be cleared
585 state_set_clear (struct GNUNET_REGEX_StateSet *set)
589 GNUNET_free_non_null (set->states);
595 * Clears an automaton fragment. Does not destroy the states inside the
598 * @param a automaton to be cleared
601 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
608 a->states_head = NULL;
609 a->states_tail = NULL;
615 * Frees the memory used by State 's'
617 * @param s state that should be destroyed
620 automaton_destroy_state (struct GNUNET_REGEX_State *s)
622 struct Transition *t;
623 struct Transition *next_t;
628 GNUNET_free_non_null (s->name);
629 GNUNET_free_non_null (s->proof);
631 for (t = s->transitions_head; NULL != t; t = next_t)
634 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
638 state_set_clear (s->nfa_set);
644 * Remove a state from the given automaton 'a'. Always use this function when
645 * altering the states of an automaton. Will also remove all transitions leading
646 * to this state, before destroying it.
649 * @param s state to remove
652 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
653 struct GNUNET_REGEX_State *s)
655 struct GNUNET_REGEX_State *ss;
656 struct GNUNET_REGEX_State *s_check;
657 struct Transition *t_check;
659 if (NULL == a || NULL == s)
664 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
667 // remove all transitions leading to this state
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 (t_check->to_state == ss)
675 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
676 s_check->transitions_tail, t_check);
677 s_check->transition_count--;
682 automaton_destroy_state (ss);
686 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
691 * @param s1 first state
692 * @param s2 second state, will be destroyed
695 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
696 struct GNUNET_REGEX_Automaton *a,
697 struct GNUNET_REGEX_State *s1,
698 struct GNUNET_REGEX_State *s2)
700 struct GNUNET_REGEX_State *s_check;
701 struct Transition *t_check;
704 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
709 // 1. Make all transitions pointing to s2 point to s1
710 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
712 for (t_check = s_check->transitions_head; NULL != t_check;
713 t_check = t_check->next)
715 if (s2 == t_check->to_state)
716 t_check->to_state = s1;
720 // 2. Add all transitions from s2 to sX to s1
721 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
723 if (t_check->to_state != s1)
724 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
727 // 3. Rename s1 to {s1,s2}
728 new_name = GNUNET_strdup (s1->name);
729 GNUNET_free_non_null (s1->name);
730 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
731 GNUNET_free (new_name);
734 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
736 automaton_destroy_state (s2);
740 * Add a state to the automaton 'a', always use this function to alter the
741 * states DLL of the automaton.
743 * @param a automaton to add the state to
744 * @param s state that should be added
747 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
748 struct GNUNET_REGEX_State *s)
750 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
755 * Function that is called with each state, when traversing an automaton.
760 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
761 struct GNUNET_REGEX_State * s);
764 * Traverses all states that are reachable from state 's'. Expects the states to
765 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
768 * @param cls closure.
769 * @param s start state.
770 * @param action action to be performed on each state.
773 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
774 GNUNET_REGEX_traverse_action action)
776 struct Transition *t;
778 if (GNUNET_NO == s->marked)
780 s->marked = GNUNET_YES;
785 for (t = s->transitions_head; NULL != t; t = t->next)
786 automaton_state_traverse (cls, t->to_state, action);
791 * Traverses the given automaton from it's start state, visiting all reachable
792 * states and calling 'action' on each one of them.
794 * @param cls closure.
795 * @param a automaton.
796 * @param action action to be performed on each state.
799 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
800 GNUNET_REGEX_traverse_action action)
802 struct GNUNET_REGEX_State *s;
804 for (s = a->states_head; NULL != s; s = s->next)
805 s->marked = GNUNET_NO;
807 automaton_state_traverse (cls, a->start, action);
811 * Create proofs for all states in the given automaton. Implementation of the
812 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
813 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
815 * @param a automaton.
818 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
820 struct GNUNET_REGEX_State *s;
821 struct Transition *t;
826 struct GNUNET_REGEX_State *states[a->state_count];
827 char *R_last[a->state_count][a->state_count];
828 char *R_cur[a->state_count][a->state_count];
834 char *complete_regex;
839 for (i = 0, s = a->states_head; NULL != s; s = s->next, i++)
846 for (i = 0; i < n; i++)
848 for (j = 0; j < n; j++)
852 for (t = states[i]->transitions_head; NULL != t; t = t->next)
854 if (t->to_state == states[j])
856 if (NULL == R_last[i][j])
857 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
861 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
869 if (NULL == R_last[i][j])
870 GNUNET_asprintf (&R_last[i][j], "");
871 else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
874 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
878 else if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j]))
881 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
888 for (k = 0; k < n; k++)
890 for (i = 0; i < n; i++)
892 for (j = 0; j < n; j++)
896 if (NULL == R_last[i][k] || NULL == R_last[k][j])
898 if (NULL != R_last[i][j])
899 R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
903 // R(k)ij = R(k-1)ij + R(k-1)ik (R(k-1)kk)* R(k-1)kj
904 length_l = (NULL == R_last[i][j]) ? 1 : strlen (R_last[i][j]) + 1;
906 snprintf (NULL, 0, "%s(%s)*%s", R_last[i][k], R_last[k][k],
908 R_cur_l = GNUNET_malloc (length_l);
909 R_cur_r = GNUNET_malloc (length_r);
911 if (NULL != R_last[i][j])
912 strcat (R_cur_l, R_last[i][j]);
914 if (NULL != R_last[i][k])
915 strcat (R_cur_r, R_last[i][k]);
917 if (NULL != R_last[k][k] && 0 != strcmp (R_last[k][k], ""))
919 strcat (R_cur_r, "(");
920 strcat (R_cur_r, R_last[k][k]);
921 strcat (R_cur_r, ")*");
924 if (NULL != R_last[k][j])
925 strcat (R_cur_r, R_last[k][j]);
927 // | is idempotent: a | a = a for all a in A
928 if (0 == strcmp (R_cur_l, R_cur_r) || 0 == strcmp (R_cur_l, "") ||
929 0 == strcmp (R_cur_r, ""))
931 if (0 == strcmp (R_cur_l, ""))
932 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_r);
934 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_l);
937 else if (R_last[i][j] == R_last[i][k] && R_last[i][k] == R_last[k][k]
938 && R_last[k][k] == R_last[k][j])
940 GNUNET_asprintf (&R_cur[i][j], "%s+", R_last[i][j]);
942 // a | a b* b => a | a b | a b b | ... => a b*
943 else if (R_last[i][j] == R_last[i][k] && R_last[k][k] == R_last[k][j])
945 GNUNET_asprintf (&R_cur[i][j], "%s%s*", R_last[i][k], R_last[k][k]);
947 // a | b b* a => a | b a | b b a | ... => b* a
948 else if (R_last[i][j] == R_last[k][j] && R_last[i][k] == R_last[k][k])
950 GNUNET_asprintf (&R_cur[i][j], "%s*%s", R_last[k][k], R_last[k][j]);
953 GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
955 GNUNET_free_non_null (R_cur_l);
956 GNUNET_free_non_null (R_cur_r);
962 for (i = 0; i < n; i++)
964 for (j = 0; j < n; j++)
966 GNUNET_free_non_null (R_last[i][j]);
968 if (NULL != R_cur[i][j])
970 R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
971 GNUNET_free (R_cur[i][j]);
978 // assign proofs and hashes
979 for (i = 0; i < n; i++)
981 states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
982 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
986 // complete regex for whole DFA
987 complete_regex = NULL;
988 for (i = 0; i < n; i++)
990 if (states[i]->accepting)
992 if (NULL == complete_regex)
993 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->marked][i]);
994 else if (NULL != R_last[a->start->marked][i] &&
995 0 != strcmp (R_last[a->start->marked][i], ""))
997 temp = complete_regex;
998 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
999 R_last[a->start->marked][i]);
1004 a->computed_regex = complete_regex;
1007 for (i = 0; i < n; i++)
1009 for (j = 0; j < n; j++)
1010 GNUNET_free_non_null (R_last[i][j]);
1015 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1016 * automaton_destroy_state.
1018 * @param ctx context
1019 * @param nfa_states set of NFA states on which the DFA should be based on
1021 * @return new DFA state
1023 static struct GNUNET_REGEX_State *
1024 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1025 struct GNUNET_REGEX_StateSet *nfa_states)
1027 struct GNUNET_REGEX_State *s;
1030 struct GNUNET_REGEX_State *cstate;
1031 struct Transition *ctran;
1033 struct Transition *t;
1036 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1037 s->id = ctx->state_id++;
1047 if (NULL == nfa_states)
1049 GNUNET_asprintf (&s->name, "s%i", s->id);
1053 s->nfa_set = nfa_states;
1055 if (nfa_states->len < 1)
1058 // Create a name based on 'sset'
1059 s->name = GNUNET_malloc (sizeof (char) * 2);
1060 strcat (s->name, "{");
1063 for (i = 0; i < nfa_states->len; i++)
1065 cstate = nfa_states->states[i];
1066 GNUNET_asprintf (&name, "%i,", cstate->id);
1070 len = strlen (s->name) + strlen (name) + 1;
1071 s->name = GNUNET_realloc (s->name, len);
1072 strcat (s->name, name);
1077 // Add a transition for each distinct label to NULL state
1078 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1080 if (0 != ctran->label)
1084 for (t = s->transitions_head; NULL != t; t = t->next)
1086 if (t->label == ctran->label)
1094 state_add_transition (ctx, s, ctran->label, NULL);
1098 // If the nfa_states contain an accepting state, the new dfa state is also
1100 if (cstate->accepting)
1104 s->name[strlen (s->name) - 1] = '}';
1110 * Move from the given state 's' to the next state on transition 'label'
1112 * @param s starting state
1113 * @param label edge label to follow
1115 * @return new state or NULL, if transition on label not possible
1117 static struct GNUNET_REGEX_State *
1118 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1120 struct Transition *t;
1121 struct GNUNET_REGEX_State *new_s;
1128 for (t = s->transitions_head; NULL != t; t = t->next)
1130 if (label == t->label)
1132 new_s = t->to_state;
1141 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1142 * states that are not reachable from the starting state.
1144 * @param a DFA automaton
1147 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1149 struct GNUNET_REGEX_State *s;
1150 struct GNUNET_REGEX_State *s_next;
1152 // 1. unmark all states
1153 for (s = a->states_head; NULL != s; s = s->next)
1154 s->marked = GNUNET_NO;
1156 // 2. traverse dfa from start state and mark all visited states
1157 automaton_traverse (NULL, a, NULL);
1159 // 3. delete all states that were not visited
1160 for (s = a->states_head; NULL != s; s = s_next)
1163 if (GNUNET_NO == s->marked)
1164 automaton_remove_state (a, s);
1169 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1170 * not transition to any other state but themselfes.
1172 * @param a DFA automaton
1175 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1177 struct GNUNET_REGEX_State *s;
1178 struct Transition *t;
1181 GNUNET_assert (DFA == a->type);
1183 for (s = a->states_head; NULL != s; s = s->next)
1189 for (t = s->transitions_head; NULL != t; t = t->next)
1191 if (NULL != t->to_state && t->to_state != s)
1201 // state s is dead, remove it
1202 automaton_remove_state (a, s);
1207 * Merge all non distinguishable states in the DFA 'a'
1209 * @param ctx context
1210 * @param a DFA automaton
1213 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1214 struct GNUNET_REGEX_Automaton *a)
1217 int table[a->state_count][a->state_count];
1218 struct GNUNET_REGEX_State *s1;
1219 struct GNUNET_REGEX_State *s2;
1220 struct Transition *t1;
1221 struct Transition *t2;
1222 struct GNUNET_REGEX_State *s1_next;
1223 struct GNUNET_REGEX_State *s2_next;
1225 int num_equal_edges;
1227 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1233 // Mark all pairs of accepting/!accepting states
1234 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1236 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1238 table[s1->marked][s2->marked] = 0;
1240 if ((s1->accepting && !s2->accepting) ||
1241 (!s1->accepting && s2->accepting))
1243 table[s1->marked][s2->marked] = 1;
1248 // Find all equal states
1253 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1255 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1257 if (0 != table[s1->marked][s2->marked])
1260 num_equal_edges = 0;
1261 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1263 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1265 if (t1->label == t2->label)
1268 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1269 0 != table[t2->to_state->marked][t1->to_state->marked])
1271 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1277 if (num_equal_edges != s1->transition_count ||
1278 num_equal_edges != s2->transition_count)
1280 // Make sure ALL edges of possible equal states are the same
1281 table[s1->marked][s2->marked] = -2;
1287 // Merge states that are equal
1288 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1291 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1294 if (table[s1->marked][s2->marked] == 0)
1295 automaton_merge_states (ctx, a, s1, s2);
1301 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1302 * dead states and merging all non distinguishable states
1304 * @param ctx context
1305 * @param a DFA automaton
1308 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1309 struct GNUNET_REGEX_Automaton *a)
1314 GNUNET_assert (DFA == a->type);
1316 // 1. remove unreachable states
1317 dfa_remove_unreachable_states (a);
1319 // 2. remove dead states
1320 dfa_remove_dead_states (a);
1322 // 3. Merge nondistinguishable states
1323 dfa_merge_nondistinguishable_states (ctx, a);
1327 * Creates a new NFA fragment. Needs to be cleared using
1328 * automaton_fragment_clear.
1330 * @param start starting state
1331 * @param end end state
1333 * @return new NFA fragment
1335 static struct GNUNET_REGEX_Automaton *
1336 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1337 struct GNUNET_REGEX_State *end)
1339 struct GNUNET_REGEX_Automaton *n;
1341 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1347 if (NULL == start && NULL == end)
1350 automaton_add_state (n, end);
1351 automaton_add_state (n, start);
1360 * Adds a list of states to the given automaton 'n'.
1362 * @param n automaton to which the states should be added
1363 * @param states_head head of the DLL of states
1364 * @param states_tail tail of the DLL of states
1367 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1368 struct GNUNET_REGEX_State *states_head,
1369 struct GNUNET_REGEX_State *states_tail)
1371 struct GNUNET_REGEX_State *s;
1373 if (NULL == n || NULL == states_head)
1375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1379 if (NULL == n->states_head)
1381 n->states_head = states_head;
1382 n->states_tail = states_tail;
1386 if (NULL != states_head)
1388 n->states_tail->next = states_head;
1389 n->states_tail = states_tail;
1392 for (s = states_head; NULL != s; s = s->next)
1397 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1399 * @param ctx context
1400 * @param accepting is it an accepting state or not
1402 * @return new NFA state
1404 static struct GNUNET_REGEX_State *
1405 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1407 struct GNUNET_REGEX_State *s;
1409 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1410 s->id = ctx->state_id++;
1411 s->accepting = accepting;
1418 GNUNET_asprintf (&s->name, "s%i", s->id);
1424 * Calculates the NFA closure set for the given state.
1426 * @param nfa the NFA containing 's'
1427 * @param s starting point state
1428 * @param label transitioning label on which to base the closure on,
1429 * pass 0 for epsilon transition
1431 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1433 static struct GNUNET_REGEX_StateSet *
1434 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1435 struct GNUNET_REGEX_State *s, const char label)
1437 struct GNUNET_REGEX_StateSet *cls;
1438 struct GNUNET_REGEX_StateSet *cls_check;
1439 struct GNUNET_REGEX_State *clsstate;
1440 struct GNUNET_REGEX_State *currentstate;
1441 struct Transition *ctran;
1446 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1447 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1449 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1450 clsstate->contained = 0;
1452 // Add start state to closure only for epsilon closure
1454 GNUNET_array_append (cls->states, cls->len, s);
1456 GNUNET_array_append (cls_check->states, cls_check->len, s);
1457 while (cls_check->len > 0)
1459 currentstate = cls_check->states[cls_check->len - 1];
1460 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1462 for (ctran = currentstate->transitions_head; NULL != ctran;
1463 ctran = ctran->next)
1465 if (NULL != ctran->to_state && label == ctran->label)
1467 clsstate = ctran->to_state;
1469 if (NULL != clsstate && 0 == clsstate->contained)
1471 GNUNET_array_append (cls->states, cls->len, clsstate);
1472 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1473 clsstate->contained = 1;
1478 GNUNET_assert (0 == cls_check->len);
1479 GNUNET_free (cls_check);
1482 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1489 * Calculates the closure set for the given set of states.
1491 * @param nfa the NFA containing 's'
1492 * @param states list of states on which to base the closure on
1493 * @param label transitioning label for which to base the closure on,
1494 * pass 0 for epsilon transition
1496 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1498 static struct GNUNET_REGEX_StateSet *
1499 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1500 struct GNUNET_REGEX_StateSet *states, const char label)
1502 struct GNUNET_REGEX_State *s;
1503 struct GNUNET_REGEX_StateSet *sset;
1504 struct GNUNET_REGEX_StateSet *cls;
1513 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1515 for (i = 0; i < states->len; i++)
1517 s = states->states[i];
1518 sset = nfa_closure_create (nfa, s, label);
1520 for (j = 0; j < sset->len; j++)
1523 for (k = 0; k < cls->len; k++)
1525 if (sset->states[j]->id == cls->states[k]->id)
1532 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1534 state_set_clear (sset);
1538 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1545 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1547 * @param ctx context
1550 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1552 struct GNUNET_REGEX_Automaton *a;
1553 struct GNUNET_REGEX_Automaton *b;
1554 struct GNUNET_REGEX_Automaton *new;
1556 b = ctx->stack_tail;
1557 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1558 a = ctx->stack_tail;
1559 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1561 state_add_transition (ctx, a->end, 0, b->start);
1562 a->end->accepting = 0;
1563 b->end->accepting = 1;
1565 new = nfa_fragment_create (NULL, NULL);
1566 nfa_add_states (new, a->states_head, a->states_tail);
1567 nfa_add_states (new, b->states_head, b->states_tail);
1568 new->start = a->start;
1570 automaton_fragment_clear (a);
1571 automaton_fragment_clear (b);
1573 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1577 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1579 * @param ctx context
1582 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1584 struct GNUNET_REGEX_Automaton *a;
1585 struct GNUNET_REGEX_Automaton *new;
1586 struct GNUNET_REGEX_State *start;
1587 struct GNUNET_REGEX_State *end;
1589 a = ctx->stack_tail;
1590 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1595 "nfa_add_star_op failed, because there was no element on the stack");
1599 start = nfa_state_create (ctx, 0);
1600 end = nfa_state_create (ctx, 1);
1602 state_add_transition (ctx, start, 0, a->start);
1603 state_add_transition (ctx, start, 0, end);
1604 state_add_transition (ctx, a->end, 0, a->start);
1605 state_add_transition (ctx, a->end, 0, end);
1607 a->end->accepting = 0;
1610 new = nfa_fragment_create (start, end);
1611 nfa_add_states (new, a->states_head, a->states_tail);
1612 automaton_fragment_clear (a);
1614 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1618 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1620 * @param ctx context
1623 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1625 struct GNUNET_REGEX_Automaton *a;
1627 a = ctx->stack_tail;
1628 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1630 state_add_transition (ctx, a->end, 0, a->start);
1632 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1636 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1638 * @param ctx context
1641 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1643 struct GNUNET_REGEX_Automaton *a;
1644 struct GNUNET_REGEX_Automaton *new;
1645 struct GNUNET_REGEX_State *start;
1646 struct GNUNET_REGEX_State *end;
1648 a = ctx->stack_tail;
1649 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1654 "nfa_add_question_op failed, because there was no element on the stack");
1658 start = nfa_state_create (ctx, 0);
1659 end = nfa_state_create (ctx, 1);
1661 state_add_transition (ctx, start, 0, a->start);
1662 state_add_transition (ctx, start, 0, end);
1663 state_add_transition (ctx, a->end, 0, end);
1665 a->end->accepting = 0;
1667 new = nfa_fragment_create (start, end);
1668 nfa_add_states (new, a->states_head, a->states_tail);
1669 automaton_fragment_clear (a);
1671 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1675 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1676 * alternates between a and b (a|b)
1678 * @param ctx context
1681 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1683 struct GNUNET_REGEX_Automaton *a;
1684 struct GNUNET_REGEX_Automaton *b;
1685 struct GNUNET_REGEX_Automaton *new;
1686 struct GNUNET_REGEX_State *start;
1687 struct GNUNET_REGEX_State *end;
1689 b = ctx->stack_tail;
1690 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1691 a = ctx->stack_tail;
1692 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1694 start = nfa_state_create (ctx, 0);
1695 end = nfa_state_create (ctx, 1);
1696 state_add_transition (ctx, start, 0, a->start);
1697 state_add_transition (ctx, start, 0, b->start);
1699 state_add_transition (ctx, a->end, 0, end);
1700 state_add_transition (ctx, b->end, 0, end);
1702 a->end->accepting = 0;
1703 b->end->accepting = 0;
1706 new = nfa_fragment_create (start, end);
1707 nfa_add_states (new, a->states_head, a->states_tail);
1708 nfa_add_states (new, b->states_head, b->states_tail);
1709 automaton_fragment_clear (a);
1710 automaton_fragment_clear (b);
1712 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1716 * Adds a new nfa fragment to the stack
1718 * @param ctx context
1719 * @param lit label for nfa transition
1722 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1724 struct GNUNET_REGEX_Automaton *n;
1725 struct GNUNET_REGEX_State *start;
1726 struct GNUNET_REGEX_State *end;
1728 GNUNET_assert (NULL != ctx);
1730 start = nfa_state_create (ctx, 0);
1731 end = nfa_state_create (ctx, 1);
1732 state_add_transition (ctx, start, lit, end);
1733 n = nfa_fragment_create (start, end);
1734 GNUNET_assert (NULL != n);
1735 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1739 * Initialize a new context
1741 * @param ctx context
1744 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1752 ctx->transition_id = 0;
1754 ctx->stack_head = NULL;
1755 ctx->stack_tail = NULL;
1759 * Construct an NFA by parsing the regex string of length 'len'.
1761 * @param regex regular expression string
1762 * @param len length of the string
1764 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1766 struct GNUNET_REGEX_Automaton *
1767 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1769 struct GNUNET_REGEX_Context ctx;
1770 struct GNUNET_REGEX_Automaton *nfa;
1774 unsigned int altcount;
1775 unsigned int atomcount;
1776 unsigned int pcount;
1783 GNUNET_REGEX_context_init (&ctx);
1792 for (count = 0; count < len && *regexp; count++, regexp++)
1800 nfa_add_concatenation (&ctx);
1802 GNUNET_array_grow (p, pcount, pcount + 1);
1803 p[pcount - 1].altcount = altcount;
1804 p[pcount - 1].atomcount = atomcount;
1811 error_msg = "Cannot append '|' to nothing";
1814 while (--atomcount > 0)
1815 nfa_add_concatenation (&ctx);
1821 error_msg = "Missing opening '('";
1826 // Ignore this: "()"
1828 altcount = p[pcount].altcount;
1829 atomcount = p[pcount].atomcount;
1832 while (--atomcount > 0)
1833 nfa_add_concatenation (&ctx);
1834 for (; altcount > 0; altcount--)
1835 nfa_add_alternation (&ctx);
1837 altcount = p[pcount].altcount;
1838 atomcount = p[pcount].atomcount;
1844 error_msg = "Cannot append '*' to nothing";
1847 nfa_add_star_op (&ctx);
1852 error_msg = "Cannot append '+' to nothing";
1855 nfa_add_plus_op (&ctx);
1860 error_msg = "Cannot append '?' to nothing";
1863 nfa_add_question_op (&ctx);
1865 case 92: /* escape: \ */
1872 nfa_add_concatenation (&ctx);
1874 nfa_add_label (&ctx, *regexp);
1881 error_msg = "Unbalanced parenthesis";
1884 while (--atomcount > 0)
1885 nfa_add_concatenation (&ctx);
1886 for (; altcount > 0; altcount--)
1887 nfa_add_alternation (&ctx);
1889 GNUNET_free_non_null (p);
1891 nfa = ctx.stack_tail;
1892 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1894 if (NULL != ctx.stack_head)
1896 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1900 nfa->regex = GNUNET_strdup (regex);
1905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1906 if (NULL != error_msg)
1907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1909 GNUNET_free_non_null (p);
1911 while (NULL != ctx.stack_tail)
1913 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1914 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1921 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1923 * @param ctx context.
1924 * @param nfa NFA automaton.
1925 * @param dfa DFA automaton.
1926 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1930 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1931 struct GNUNET_REGEX_Automaton *nfa,
1932 struct GNUNET_REGEX_Automaton *dfa,
1933 struct GNUNET_REGEX_State *dfa_state)
1935 struct Transition *ctran;
1936 struct GNUNET_REGEX_State *state_iter;
1937 struct GNUNET_REGEX_State *new_dfa_state;
1938 struct GNUNET_REGEX_State *state_contains;
1939 struct GNUNET_REGEX_StateSet *tmp;
1940 struct GNUNET_REGEX_StateSet *nfa_set;
1942 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1944 if (0 == ctran->label || NULL != ctran->to_state)
1947 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1948 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1949 state_set_clear (tmp);
1950 new_dfa_state = dfa_state_create (ctx, nfa_set);
1951 state_contains = NULL;
1952 for (state_iter = dfa->states_head; NULL != state_iter;
1953 state_iter = state_iter->next)
1955 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1956 state_contains = state_iter;
1959 if (NULL == state_contains)
1961 automaton_add_state (dfa, new_dfa_state);
1962 ctran->to_state = new_dfa_state;
1963 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1967 ctran->to_state = state_contains;
1968 automaton_destroy_state (new_dfa_state);
1974 * Construct DFA for the given 'regex' of length 'len'
1976 * @param regex regular expression string
1977 * @param len length of the regular expression
1979 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1981 struct GNUNET_REGEX_Automaton *
1982 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1984 struct GNUNET_REGEX_Context ctx;
1985 struct GNUNET_REGEX_Automaton *dfa;
1986 struct GNUNET_REGEX_Automaton *nfa;
1987 struct GNUNET_REGEX_StateSet *nfa_set;
1989 GNUNET_REGEX_context_init (&ctx);
1992 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1997 "Could not create DFA, because NFA creation failed\n");
2001 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2003 dfa->regex = GNUNET_strdup (regex);
2005 // Create DFA start state from epsilon closure
2006 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2007 dfa->start = dfa_state_create (&ctx, nfa_set);
2008 automaton_add_state (dfa, dfa->start);
2010 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2012 GNUNET_REGEX_automaton_destroy (nfa);
2015 dfa_minimize (&ctx, dfa);
2018 scc_tarjan (&ctx, dfa);
2020 // Create proofs for all states
2021 automaton_create_proofs (dfa);
2027 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2030 * @param a automaton to be destroyed
2033 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2035 struct GNUNET_REGEX_State *s;
2036 struct GNUNET_REGEX_State *next_state;
2041 GNUNET_free (a->regex);
2043 for (s = a->states_head; NULL != s;)
2045 next_state = s->next;
2046 automaton_destroy_state (s);
2054 * Save the given automaton as a GraphViz dot file
2056 * @param a the automaton to be saved
2057 * @param filename where to save the file
2060 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2061 const char *filename)
2063 struct GNUNET_REGEX_State *s;
2064 struct Transition *ctran;
2066 char *s_tran = NULL;
2073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2077 if (NULL == filename || strlen (filename) < 1)
2079 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2083 p = fopen (filename, "w");
2087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2092 start = "digraph G {\nrankdir=LR\n";
2093 fwrite (start, strlen (start), 1, p);
2095 for (s = a->states_head; NULL != s; s = s->next)
2099 GNUNET_asprintf (&s_acc,
2100 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2101 s->name, s->scc_id);
2105 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2115 fwrite (s_acc, strlen (s_acc), 1, p);
2116 GNUNET_free (s_acc);
2119 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2121 if (NULL == ctran->to_state)
2123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2124 "Transition from State %i has has no state for transitioning\n",
2129 if (ctran->label == 0)
2131 GNUNET_asprintf (&s_tran,
2132 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2133 s->name, ctran->to_state->name, s->scc_id);
2137 GNUNET_asprintf (&s_tran,
2138 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2139 s->name, ctran->to_state->name, ctran->label,
2145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2150 fwrite (s_tran, strlen (s_tran), 1, p);
2151 GNUNET_free (s_tran);
2157 fwrite (end, strlen (end), 1, p);
2162 * Evaluates the given string using the given DFA automaton
2164 * @param a automaton, type must be DFA
2165 * @param string string that should be evaluated
2167 * @return 0 if string matches, non 0 otherwise
2170 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2173 struct GNUNET_REGEX_State *s;
2177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2178 "Tried to evaluate DFA, but NFA automaton given");
2184 for (strp = string; NULL != strp && *strp; strp++)
2186 s = dfa_move (s, *strp);
2191 if (NULL != s && s->accepting)
2198 * Evaluates the given string using the given NFA automaton
2200 * @param a automaton, type must be NFA
2201 * @param string string that should be evaluated
2203 * @return 0 if string matches, non 0 otherwise
2206 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2209 struct GNUNET_REGEX_State *s;
2210 struct GNUNET_REGEX_StateSet *sset;
2211 struct GNUNET_REGEX_StateSet *new_sset;
2217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2218 "Tried to evaluate NFA, but DFA automaton given");
2224 sset = nfa_closure_create (a, a->start, 0);
2226 for (strp = string; NULL != strp && *strp; strp++)
2228 new_sset = nfa_closure_set_create (a, sset, *strp);
2229 state_set_clear (sset);
2230 sset = nfa_closure_set_create (a, new_sset, 0);
2231 state_set_clear (new_sset);
2234 for (i = 0; i < sset->len; i++)
2236 s = sset->states[i];
2237 if (NULL != s && s->accepting)
2244 state_set_clear (sset);
2249 * Evaluates the given 'string' against the given compiled regex
2251 * @param a automaton
2252 * @param string string to check
2254 * @return 0 if string matches, non 0 otherwise
2257 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2264 result = evaluate_dfa (a, string);
2267 result = evaluate_nfa (a, string);
2270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2271 "Evaluating regex failed, automaton has no type!\n");
2272 result = GNUNET_SYSERR;
2280 * Get the computed regex of the given automaton.
2281 * When constructing the automaton a proof is computed for each state,
2282 * consisting of the regular expression leading to this state. A complete
2283 * regex for the automaton can be computed by combining these proofs.
2284 * As of now this computed regex is only useful for testing.
2287 GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
2292 return a->computed_regex;
2296 * Get the first key for the given 'input_string'. This hashes the first x bits
2297 * of the 'input_strings'.
2299 * @param input_string string.
2300 * @param string_len length of the 'input_string'.
2301 * @param key pointer to where to write the hash code.
2303 * @return number of bits of 'input_string' that have been consumed
2304 * to construct the key
2307 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2308 GNUNET_HashCode * key)
2312 size = string_len < initial_bits ? string_len : initial_bits;
2314 if (NULL == input_string)
2316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2320 GNUNET_CRYPTO_hash (input_string, size, key);
2326 * Check if the given 'proof' matches the given 'key'.
2328 * @param proof partial regex
2331 * @return GNUNET_OK if the proof is valid for the given key
2334 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2340 * Iterate over all edges helper function starting from state 's', calling
2341 * iterator on for each edge.
2344 * @param iterator iterator function called for each edge.
2345 * @param iterator_cls closure.
2348 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2351 struct Transition *t;
2352 struct GNUNET_REGEX_Edge edges[s->transition_count];
2353 unsigned int num_edges;
2355 if (GNUNET_YES != s->marked)
2357 s->marked = GNUNET_YES;
2359 num_edges = state_get_edges (s, edges);
2361 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2363 for (t = s->transitions_head; NULL != t; t = t->next)
2364 iterate_edge (t->to_state, iterator, iterator_cls);
2369 * Iterate over all edges starting from start state of automaton 'a'. Calling
2370 * iterator for each edge.
2372 * @param a automaton.
2373 * @param iterator iterator called for each edge.
2374 * @param iterator_cls closure.
2377 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2378 GNUNET_REGEX_KeyIterator iterator,
2381 struct GNUNET_REGEX_State *s;
2383 for (s = a->states_head; NULL != s; s = s->next)
2384 s->marked = GNUNET_NO;
2386 iterate_edge (a->start, iterator, iterator_cls);