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 if (R_last[k][k][0] == '(' && R_last[k][k][strlen (R_last[k][k])-1] == ')')
921 strcat (R_cur_r, R_last[k][k]);
922 strcat (R_cur_r, "*");
926 strcat (R_cur_r, "(");
927 strcat (R_cur_r, R_last[k][k]);
928 strcat (R_cur_r, ")*");
932 if (NULL != R_last[k][j])
933 strcat (R_cur_r, R_last[k][j]);
935 // | is idempotent: a | a = a for all a in A
936 if (0 == strcmp (R_cur_l, R_cur_r) || 0 == strcmp (R_cur_l, "") ||
937 0 == strcmp (R_cur_r, ""))
939 if (0 == strcmp (R_cur_l, ""))
940 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_r);
942 GNUNET_asprintf (&R_cur[i][j], "%s", R_cur_l);
945 else if (R_last[i][j] == R_last[i][k] && R_last[i][k] == R_last[k][k]
946 && R_last[k][k] == R_last[k][j])
948 GNUNET_asprintf (&R_cur[i][j], "%s+", R_last[i][j]);
950 // a | a b* b => a | a b | a b b | ... => a b*
951 else if (R_last[i][j] == R_last[i][k] && R_last[k][k] == R_last[k][j])
953 GNUNET_asprintf (&R_cur[i][j], "%s%s*", R_last[i][k], R_last[k][k]);
955 // a | b b* a => a | b a | b b a | ... => b* a
956 else if (R_last[i][j] == R_last[k][j] && R_last[i][k] == R_last[k][k])
958 GNUNET_asprintf (&R_cur[i][j], "%s*%s", R_last[k][k], R_last[k][j]);
961 GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
963 GNUNET_free_non_null (R_cur_l);
964 GNUNET_free_non_null (R_cur_r);
970 for (i = 0; i < n; i++)
972 for (j = 0; j < n; j++)
974 GNUNET_free_non_null (R_last[i][j]);
976 if (NULL != R_cur[i][j])
978 R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
979 GNUNET_free (R_cur[i][j]);
986 // assign proofs and hashes
987 for (i = 0; i < n; i++)
989 states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
990 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
994 // complete regex for whole DFA
995 complete_regex = NULL;
996 for (i = 0; i < n; i++)
998 if (states[i]->accepting)
1000 if (NULL == complete_regex)
1001 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->marked][i]);
1002 else if (NULL != R_last[a->start->marked][i] &&
1003 0 != strcmp (R_last[a->start->marked][i], ""))
1005 temp = complete_regex;
1006 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1007 R_last[a->start->marked][i]);
1012 a->computed_regex = complete_regex;
1015 for (i = 0; i < n; i++)
1017 for (j = 0; j < n; j++)
1018 GNUNET_free_non_null (R_last[i][j]);
1023 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1024 * automaton_destroy_state.
1026 * @param ctx context
1027 * @param nfa_states set of NFA states on which the DFA should be based on
1029 * @return new DFA state
1031 static struct GNUNET_REGEX_State *
1032 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1033 struct GNUNET_REGEX_StateSet *nfa_states)
1035 struct GNUNET_REGEX_State *s;
1038 struct GNUNET_REGEX_State *cstate;
1039 struct Transition *ctran;
1041 struct Transition *t;
1044 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1045 s->id = ctx->state_id++;
1055 if (NULL == nfa_states)
1057 GNUNET_asprintf (&s->name, "s%i", s->id);
1061 s->nfa_set = nfa_states;
1063 if (nfa_states->len < 1)
1066 // Create a name based on 'sset'
1067 s->name = GNUNET_malloc (sizeof (char) * 2);
1068 strcat (s->name, "{");
1071 for (i = 0; i < nfa_states->len; i++)
1073 cstate = nfa_states->states[i];
1074 GNUNET_asprintf (&name, "%i,", cstate->id);
1078 len = strlen (s->name) + strlen (name) + 1;
1079 s->name = GNUNET_realloc (s->name, len);
1080 strcat (s->name, name);
1085 // Add a transition for each distinct label to NULL state
1086 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1088 if (0 != ctran->label)
1092 for (t = s->transitions_head; NULL != t; t = t->next)
1094 if (t->label == ctran->label)
1102 state_add_transition (ctx, s, ctran->label, NULL);
1106 // If the nfa_states contain an accepting state, the new dfa state is also
1108 if (cstate->accepting)
1112 s->name[strlen (s->name) - 1] = '}';
1118 * Move from the given state 's' to the next state on transition 'label'
1120 * @param s starting state
1121 * @param label edge label to follow
1123 * @return new state or NULL, if transition on label not possible
1125 static struct GNUNET_REGEX_State *
1126 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1128 struct Transition *t;
1129 struct GNUNET_REGEX_State *new_s;
1136 for (t = s->transitions_head; NULL != t; t = t->next)
1138 if (label == t->label)
1140 new_s = t->to_state;
1149 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1150 * states that are not reachable from the starting state.
1152 * @param a DFA automaton
1155 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1157 struct GNUNET_REGEX_State *s;
1158 struct GNUNET_REGEX_State *s_next;
1160 // 1. unmark all states
1161 for (s = a->states_head; NULL != s; s = s->next)
1162 s->marked = GNUNET_NO;
1164 // 2. traverse dfa from start state and mark all visited states
1165 automaton_traverse (NULL, a, NULL);
1167 // 3. delete all states that were not visited
1168 for (s = a->states_head; NULL != s; s = s_next)
1171 if (GNUNET_NO == s->marked)
1172 automaton_remove_state (a, s);
1177 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1178 * not transition to any other state but themselfes.
1180 * @param a DFA automaton
1183 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1185 struct GNUNET_REGEX_State *s;
1186 struct Transition *t;
1189 GNUNET_assert (DFA == a->type);
1191 for (s = a->states_head; NULL != s; s = s->next)
1197 for (t = s->transitions_head; NULL != t; t = t->next)
1199 if (NULL != t->to_state && t->to_state != s)
1209 // state s is dead, remove it
1210 automaton_remove_state (a, s);
1215 * Merge all non distinguishable states in the DFA 'a'
1217 * @param ctx context
1218 * @param a DFA automaton
1221 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1222 struct GNUNET_REGEX_Automaton *a)
1225 int table[a->state_count][a->state_count];
1226 struct GNUNET_REGEX_State *s1;
1227 struct GNUNET_REGEX_State *s2;
1228 struct Transition *t1;
1229 struct Transition *t2;
1230 struct GNUNET_REGEX_State *s1_next;
1231 struct GNUNET_REGEX_State *s2_next;
1233 int num_equal_edges;
1235 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1241 // Mark all pairs of accepting/!accepting states
1242 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1244 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1246 table[s1->marked][s2->marked] = 0;
1248 if ((s1->accepting && !s2->accepting) ||
1249 (!s1->accepting && s2->accepting))
1251 table[s1->marked][s2->marked] = 1;
1256 // Find all equal states
1261 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1263 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1265 if (0 != table[s1->marked][s2->marked])
1268 num_equal_edges = 0;
1269 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1271 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1273 if (t1->label == t2->label)
1276 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1277 0 != table[t2->to_state->marked][t1->to_state->marked])
1279 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1285 if (num_equal_edges != s1->transition_count ||
1286 num_equal_edges != s2->transition_count)
1288 // Make sure ALL edges of possible equal states are the same
1289 table[s1->marked][s2->marked] = -2;
1295 // Merge states that are equal
1296 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1299 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1302 if (table[s1->marked][s2->marked] == 0)
1303 automaton_merge_states (ctx, a, s1, s2);
1309 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1310 * dead states and merging all non distinguishable states
1312 * @param ctx context
1313 * @param a DFA automaton
1316 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1317 struct GNUNET_REGEX_Automaton *a)
1322 GNUNET_assert (DFA == a->type);
1324 // 1. remove unreachable states
1325 dfa_remove_unreachable_states (a);
1327 // 2. remove dead states
1328 dfa_remove_dead_states (a);
1330 // 3. Merge nondistinguishable states
1331 dfa_merge_nondistinguishable_states (ctx, a);
1335 * Creates a new NFA fragment. Needs to be cleared using
1336 * automaton_fragment_clear.
1338 * @param start starting state
1339 * @param end end state
1341 * @return new NFA fragment
1343 static struct GNUNET_REGEX_Automaton *
1344 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1345 struct GNUNET_REGEX_State *end)
1347 struct GNUNET_REGEX_Automaton *n;
1349 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1355 if (NULL == start && NULL == end)
1358 automaton_add_state (n, end);
1359 automaton_add_state (n, start);
1368 * Adds a list of states to the given automaton 'n'.
1370 * @param n automaton to which the states should be added
1371 * @param states_head head of the DLL of states
1372 * @param states_tail tail of the DLL of states
1375 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1376 struct GNUNET_REGEX_State *states_head,
1377 struct GNUNET_REGEX_State *states_tail)
1379 struct GNUNET_REGEX_State *s;
1381 if (NULL == n || NULL == states_head)
1383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1387 if (NULL == n->states_head)
1389 n->states_head = states_head;
1390 n->states_tail = states_tail;
1394 if (NULL != states_head)
1396 n->states_tail->next = states_head;
1397 n->states_tail = states_tail;
1400 for (s = states_head; NULL != s; s = s->next)
1405 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1407 * @param ctx context
1408 * @param accepting is it an accepting state or not
1410 * @return new NFA state
1412 static struct GNUNET_REGEX_State *
1413 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1415 struct GNUNET_REGEX_State *s;
1417 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1418 s->id = ctx->state_id++;
1419 s->accepting = accepting;
1426 GNUNET_asprintf (&s->name, "s%i", s->id);
1432 * Calculates the NFA closure set for the given state.
1434 * @param nfa the NFA containing 's'
1435 * @param s starting point state
1436 * @param label transitioning label on which to base the closure on,
1437 * pass 0 for epsilon transition
1439 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1441 static struct GNUNET_REGEX_StateSet *
1442 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1443 struct GNUNET_REGEX_State *s, const char label)
1445 struct GNUNET_REGEX_StateSet *cls;
1446 struct GNUNET_REGEX_StateSet *cls_check;
1447 struct GNUNET_REGEX_State *clsstate;
1448 struct GNUNET_REGEX_State *currentstate;
1449 struct Transition *ctran;
1454 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1455 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1457 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1458 clsstate->contained = 0;
1460 // Add start state to closure only for epsilon closure
1462 GNUNET_array_append (cls->states, cls->len, s);
1464 GNUNET_array_append (cls_check->states, cls_check->len, s);
1465 while (cls_check->len > 0)
1467 currentstate = cls_check->states[cls_check->len - 1];
1468 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1470 for (ctran = currentstate->transitions_head; NULL != ctran;
1471 ctran = ctran->next)
1473 if (NULL != ctran->to_state && label == ctran->label)
1475 clsstate = ctran->to_state;
1477 if (NULL != clsstate && 0 == clsstate->contained)
1479 GNUNET_array_append (cls->states, cls->len, clsstate);
1480 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1481 clsstate->contained = 1;
1486 GNUNET_assert (0 == cls_check->len);
1487 GNUNET_free (cls_check);
1490 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1497 * Calculates the closure set for the given set of states.
1499 * @param nfa the NFA containing 's'
1500 * @param states list of states on which to base the closure on
1501 * @param label transitioning label for which to base the closure on,
1502 * pass 0 for epsilon transition
1504 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1506 static struct GNUNET_REGEX_StateSet *
1507 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1508 struct GNUNET_REGEX_StateSet *states, const char label)
1510 struct GNUNET_REGEX_State *s;
1511 struct GNUNET_REGEX_StateSet *sset;
1512 struct GNUNET_REGEX_StateSet *cls;
1521 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1523 for (i = 0; i < states->len; i++)
1525 s = states->states[i];
1526 sset = nfa_closure_create (nfa, s, label);
1528 for (j = 0; j < sset->len; j++)
1531 for (k = 0; k < cls->len; k++)
1533 if (sset->states[j]->id == cls->states[k]->id)
1540 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1542 state_set_clear (sset);
1546 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1553 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1555 * @param ctx context
1558 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1560 struct GNUNET_REGEX_Automaton *a;
1561 struct GNUNET_REGEX_Automaton *b;
1562 struct GNUNET_REGEX_Automaton *new;
1564 b = ctx->stack_tail;
1565 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1566 a = ctx->stack_tail;
1567 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1569 state_add_transition (ctx, a->end, 0, b->start);
1570 a->end->accepting = 0;
1571 b->end->accepting = 1;
1573 new = nfa_fragment_create (NULL, NULL);
1574 nfa_add_states (new, a->states_head, a->states_tail);
1575 nfa_add_states (new, b->states_head, b->states_tail);
1576 new->start = a->start;
1578 automaton_fragment_clear (a);
1579 automaton_fragment_clear (b);
1581 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1585 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1587 * @param ctx context
1590 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1592 struct GNUNET_REGEX_Automaton *a;
1593 struct GNUNET_REGEX_Automaton *new;
1594 struct GNUNET_REGEX_State *start;
1595 struct GNUNET_REGEX_State *end;
1597 a = ctx->stack_tail;
1598 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1602 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1603 "nfa_add_star_op failed, because there was no element on the stack");
1607 start = nfa_state_create (ctx, 0);
1608 end = nfa_state_create (ctx, 1);
1610 state_add_transition (ctx, start, 0, a->start);
1611 state_add_transition (ctx, start, 0, end);
1612 state_add_transition (ctx, a->end, 0, a->start);
1613 state_add_transition (ctx, a->end, 0, end);
1615 a->end->accepting = 0;
1618 new = nfa_fragment_create (start, end);
1619 nfa_add_states (new, a->states_head, a->states_tail);
1620 automaton_fragment_clear (a);
1622 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1626 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1628 * @param ctx context
1631 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1633 struct GNUNET_REGEX_Automaton *a;
1635 a = ctx->stack_tail;
1636 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1638 state_add_transition (ctx, a->end, 0, a->start);
1640 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1644 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1646 * @param ctx context
1649 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1651 struct GNUNET_REGEX_Automaton *a;
1652 struct GNUNET_REGEX_Automaton *new;
1653 struct GNUNET_REGEX_State *start;
1654 struct GNUNET_REGEX_State *end;
1656 a = ctx->stack_tail;
1657 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1662 "nfa_add_question_op failed, because there was no element on the stack");
1666 start = nfa_state_create (ctx, 0);
1667 end = nfa_state_create (ctx, 1);
1669 state_add_transition (ctx, start, 0, a->start);
1670 state_add_transition (ctx, start, 0, end);
1671 state_add_transition (ctx, a->end, 0, end);
1673 a->end->accepting = 0;
1675 new = nfa_fragment_create (start, end);
1676 nfa_add_states (new, a->states_head, a->states_tail);
1677 automaton_fragment_clear (a);
1679 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1683 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1684 * alternates between a and b (a|b)
1686 * @param ctx context
1689 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1691 struct GNUNET_REGEX_Automaton *a;
1692 struct GNUNET_REGEX_Automaton *b;
1693 struct GNUNET_REGEX_Automaton *new;
1694 struct GNUNET_REGEX_State *start;
1695 struct GNUNET_REGEX_State *end;
1697 b = ctx->stack_tail;
1698 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1699 a = ctx->stack_tail;
1700 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1702 start = nfa_state_create (ctx, 0);
1703 end = nfa_state_create (ctx, 1);
1704 state_add_transition (ctx, start, 0, a->start);
1705 state_add_transition (ctx, start, 0, b->start);
1707 state_add_transition (ctx, a->end, 0, end);
1708 state_add_transition (ctx, b->end, 0, end);
1710 a->end->accepting = 0;
1711 b->end->accepting = 0;
1714 new = nfa_fragment_create (start, end);
1715 nfa_add_states (new, a->states_head, a->states_tail);
1716 nfa_add_states (new, b->states_head, b->states_tail);
1717 automaton_fragment_clear (a);
1718 automaton_fragment_clear (b);
1720 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1724 * Adds a new nfa fragment to the stack
1726 * @param ctx context
1727 * @param lit label for nfa transition
1730 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1732 struct GNUNET_REGEX_Automaton *n;
1733 struct GNUNET_REGEX_State *start;
1734 struct GNUNET_REGEX_State *end;
1736 GNUNET_assert (NULL != ctx);
1738 start = nfa_state_create (ctx, 0);
1739 end = nfa_state_create (ctx, 1);
1740 state_add_transition (ctx, start, lit, end);
1741 n = nfa_fragment_create (start, end);
1742 GNUNET_assert (NULL != n);
1743 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1747 * Initialize a new context
1749 * @param ctx context
1752 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1760 ctx->transition_id = 0;
1762 ctx->stack_head = NULL;
1763 ctx->stack_tail = NULL;
1767 * Construct an NFA by parsing the regex string of length 'len'.
1769 * @param regex regular expression string
1770 * @param len length of the string
1772 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1774 struct GNUNET_REGEX_Automaton *
1775 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1777 struct GNUNET_REGEX_Context ctx;
1778 struct GNUNET_REGEX_Automaton *nfa;
1782 unsigned int altcount;
1783 unsigned int atomcount;
1784 unsigned int pcount;
1791 GNUNET_REGEX_context_init (&ctx);
1800 for (count = 0; count < len && *regexp; count++, regexp++)
1808 nfa_add_concatenation (&ctx);
1810 GNUNET_array_grow (p, pcount, pcount + 1);
1811 p[pcount - 1].altcount = altcount;
1812 p[pcount - 1].atomcount = atomcount;
1819 error_msg = "Cannot append '|' to nothing";
1822 while (--atomcount > 0)
1823 nfa_add_concatenation (&ctx);
1829 error_msg = "Missing opening '('";
1834 // Ignore this: "()"
1836 altcount = p[pcount].altcount;
1837 atomcount = p[pcount].atomcount;
1840 while (--atomcount > 0)
1841 nfa_add_concatenation (&ctx);
1842 for (; altcount > 0; altcount--)
1843 nfa_add_alternation (&ctx);
1845 altcount = p[pcount].altcount;
1846 atomcount = p[pcount].atomcount;
1852 error_msg = "Cannot append '*' to nothing";
1855 nfa_add_star_op (&ctx);
1860 error_msg = "Cannot append '+' to nothing";
1863 nfa_add_plus_op (&ctx);
1868 error_msg = "Cannot append '?' to nothing";
1871 nfa_add_question_op (&ctx);
1873 case 92: /* escape: \ */
1880 nfa_add_concatenation (&ctx);
1882 nfa_add_label (&ctx, *regexp);
1889 error_msg = "Unbalanced parenthesis";
1892 while (--atomcount > 0)
1893 nfa_add_concatenation (&ctx);
1894 for (; altcount > 0; altcount--)
1895 nfa_add_alternation (&ctx);
1897 GNUNET_free_non_null (p);
1899 nfa = ctx.stack_tail;
1900 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1902 if (NULL != ctx.stack_head)
1904 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1908 nfa->regex = GNUNET_strdup (regex);
1913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1914 if (NULL != error_msg)
1915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1917 GNUNET_free_non_null (p);
1919 while (NULL != ctx.stack_tail)
1921 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1922 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1929 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1931 * @param ctx context.
1932 * @param nfa NFA automaton.
1933 * @param dfa DFA automaton.
1934 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1938 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1939 struct GNUNET_REGEX_Automaton *nfa,
1940 struct GNUNET_REGEX_Automaton *dfa,
1941 struct GNUNET_REGEX_State *dfa_state)
1943 struct Transition *ctran;
1944 struct GNUNET_REGEX_State *state_iter;
1945 struct GNUNET_REGEX_State *new_dfa_state;
1946 struct GNUNET_REGEX_State *state_contains;
1947 struct GNUNET_REGEX_StateSet *tmp;
1948 struct GNUNET_REGEX_StateSet *nfa_set;
1950 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1952 if (0 == ctran->label || NULL != ctran->to_state)
1955 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1956 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1957 state_set_clear (tmp);
1958 new_dfa_state = dfa_state_create (ctx, nfa_set);
1959 state_contains = NULL;
1960 for (state_iter = dfa->states_head; NULL != state_iter;
1961 state_iter = state_iter->next)
1963 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1964 state_contains = state_iter;
1967 if (NULL == state_contains)
1969 automaton_add_state (dfa, new_dfa_state);
1970 ctran->to_state = new_dfa_state;
1971 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1975 ctran->to_state = state_contains;
1976 automaton_destroy_state (new_dfa_state);
1982 * Construct DFA for the given 'regex' of length 'len'
1984 * @param regex regular expression string
1985 * @param len length of the regular expression
1987 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1989 struct GNUNET_REGEX_Automaton *
1990 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1992 struct GNUNET_REGEX_Context ctx;
1993 struct GNUNET_REGEX_Automaton *dfa;
1994 struct GNUNET_REGEX_Automaton *nfa;
1995 struct GNUNET_REGEX_StateSet *nfa_set;
1997 GNUNET_REGEX_context_init (&ctx);
2000 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2005 "Could not create DFA, because NFA creation failed\n");
2009 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2011 dfa->regex = GNUNET_strdup (regex);
2013 // Create DFA start state from epsilon closure
2014 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2015 dfa->start = dfa_state_create (&ctx, nfa_set);
2016 automaton_add_state (dfa, dfa->start);
2018 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2020 GNUNET_REGEX_automaton_destroy (nfa);
2023 dfa_minimize (&ctx, dfa);
2026 scc_tarjan (&ctx, dfa);
2028 // Create proofs for all states
2029 automaton_create_proofs (dfa);
2035 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2038 * @param a automaton to be destroyed
2041 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2043 struct GNUNET_REGEX_State *s;
2044 struct GNUNET_REGEX_State *next_state;
2049 GNUNET_free (a->regex);
2051 for (s = a->states_head; NULL != s;)
2053 next_state = s->next;
2054 automaton_destroy_state (s);
2062 * Save the given automaton as a GraphViz dot file
2064 * @param a the automaton to be saved
2065 * @param filename where to save the file
2068 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2069 const char *filename)
2071 struct GNUNET_REGEX_State *s;
2072 struct Transition *ctran;
2074 char *s_tran = NULL;
2081 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2085 if (NULL == filename || strlen (filename) < 1)
2087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2091 p = fopen (filename, "w");
2095 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2100 start = "digraph G {\nrankdir=LR\n";
2101 fwrite (start, strlen (start), 1, p);
2103 for (s = a->states_head; NULL != s; s = s->next)
2107 GNUNET_asprintf (&s_acc,
2108 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2109 s->name, s->scc_id);
2113 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2123 fwrite (s_acc, strlen (s_acc), 1, p);
2124 GNUNET_free (s_acc);
2127 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2129 if (NULL == ctran->to_state)
2131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2132 "Transition from State %i has has no state for transitioning\n",
2137 if (ctran->label == 0)
2139 GNUNET_asprintf (&s_tran,
2140 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2141 s->name, ctran->to_state->name, s->scc_id);
2145 GNUNET_asprintf (&s_tran,
2146 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2147 s->name, ctran->to_state->name, ctran->label,
2153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2158 fwrite (s_tran, strlen (s_tran), 1, p);
2159 GNUNET_free (s_tran);
2165 fwrite (end, strlen (end), 1, p);
2170 * Evaluates the given string using the given DFA automaton
2172 * @param a automaton, type must be DFA
2173 * @param string string that should be evaluated
2175 * @return 0 if string matches, non 0 otherwise
2178 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2181 struct GNUNET_REGEX_State *s;
2185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2186 "Tried to evaluate DFA, but NFA automaton given");
2192 for (strp = string; NULL != strp && *strp; strp++)
2194 s = dfa_move (s, *strp);
2199 if (NULL != s && s->accepting)
2206 * Evaluates the given string using the given NFA automaton
2208 * @param a automaton, type must be NFA
2209 * @param string string that should be evaluated
2211 * @return 0 if string matches, non 0 otherwise
2214 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2217 struct GNUNET_REGEX_State *s;
2218 struct GNUNET_REGEX_StateSet *sset;
2219 struct GNUNET_REGEX_StateSet *new_sset;
2225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2226 "Tried to evaluate NFA, but DFA automaton given");
2232 sset = nfa_closure_create (a, a->start, 0);
2234 for (strp = string; NULL != strp && *strp; strp++)
2236 new_sset = nfa_closure_set_create (a, sset, *strp);
2237 state_set_clear (sset);
2238 sset = nfa_closure_set_create (a, new_sset, 0);
2239 state_set_clear (new_sset);
2242 for (i = 0; i < sset->len; i++)
2244 s = sset->states[i];
2245 if (NULL != s && s->accepting)
2252 state_set_clear (sset);
2257 * Evaluates the given 'string' against the given compiled regex
2259 * @param a automaton
2260 * @param string string to check
2262 * @return 0 if string matches, non 0 otherwise
2265 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2272 result = evaluate_dfa (a, string);
2275 result = evaluate_nfa (a, string);
2278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2279 "Evaluating regex failed, automaton has no type!\n");
2280 result = GNUNET_SYSERR;
2288 * Get the computed regex of the given automaton.
2289 * When constructing the automaton a proof is computed for each state,
2290 * consisting of the regular expression leading to this state. A complete
2291 * regex for the automaton can be computed by combining these proofs.
2292 * As of now this computed regex is only useful for testing.
2295 GNUNET_REGEX_get_computed_regex (struct GNUNET_REGEX_Automaton *a)
2300 return a->computed_regex;
2304 * Get the first key for the given 'input_string'. This hashes the first x bits
2305 * of the 'input_strings'.
2307 * @param input_string string.
2308 * @param string_len length of the 'input_string'.
2309 * @param key pointer to where to write the hash code.
2311 * @return number of bits of 'input_string' that have been consumed
2312 * to construct the key
2315 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2316 GNUNET_HashCode * key)
2320 size = string_len < initial_bits ? string_len : initial_bits;
2322 if (NULL == input_string)
2324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2328 GNUNET_CRYPTO_hash (input_string, size, key);
2334 * Check if the given 'proof' matches the given 'key'.
2336 * @param proof partial regex
2339 * @return GNUNET_OK if the proof is valid for the given key
2342 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2348 * Iterate over all edges helper function starting from state 's', calling
2349 * iterator on for each edge.
2352 * @param iterator iterator function called for each edge.
2353 * @param iterator_cls closure.
2356 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2359 struct Transition *t;
2360 struct GNUNET_REGEX_Edge edges[s->transition_count];
2361 unsigned int num_edges;
2363 if (GNUNET_YES != s->marked)
2365 s->marked = GNUNET_YES;
2367 num_edges = state_get_edges (s, edges);
2369 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2371 for (t = s->transitions_head; NULL != t; t = t->next)
2372 iterate_edge (t->to_state, iterator, iterator_cls);
2377 * Iterate over all edges starting from start state of automaton 'a'. Calling
2378 * iterator for each edge.
2380 * @param a automaton.
2381 * @param iterator iterator called for each edge.
2382 * @param iterator_cls closure.
2385 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2386 GNUNET_REGEX_KeyIterator iterator,
2389 struct GNUNET_REGEX_State *s;
2391 for (s = a->states_head; NULL != s; s = s->next)
2392 s->marked = GNUNET_NO;
2394 iterate_edge (a->start, iterator, iterator_cls);