2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file src/regex/regex.c
22 * @brief library to create automatons from regular expressions
23 * @author Maximilian Szengel
26 #include "gnunet_container_lib.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_regex_lib.h"
31 #define initial_bits 10
34 * Context that contains an id counter for states and transitions as well as a
35 * DLL of automatons used as a stack for NFA construction.
37 struct GNUNET_REGEX_Context
42 unsigned int state_id;
45 * Unique transition id.
47 unsigned int transition_id;
50 * Unique SCC (Strongly Connected Component) id.
55 * DLL of GNUNET_REGEX_Automaton's used as a stack.
57 struct GNUNET_REGEX_Automaton *stack_head;
60 * DLL of GNUNET_REGEX_Automaton's used as a stack.
62 struct GNUNET_REGEX_Automaton *stack_tail;
66 * Type of an automaton.
68 enum GNUNET_REGEX_automaton_type
75 * Automaton representation.
77 struct GNUNET_REGEX_Automaton
80 * This is a linked list.
82 struct GNUNET_REGEX_Automaton *prev;
85 * This is a linked list.
87 struct GNUNET_REGEX_Automaton *next;
90 * First state of the automaton. This is mainly used for constructing an NFA,
91 * where each NFA itself consists of one or more NFAs linked together.
93 struct GNUNET_REGEX_State *start;
96 * End state of the automaton.
98 struct GNUNET_REGEX_State *end;
101 * Number of states in the automaton.
103 unsigned int state_count;
108 struct GNUNET_REGEX_State *states_head;
113 struct GNUNET_REGEX_State *states_tail;
116 * Type of the automaton.
118 enum GNUNET_REGEX_automaton_type type;
122 * A state. Can be used in DFA and NFA automatons.
124 struct GNUNET_REGEX_State
127 * This is a linked list.
129 struct GNUNET_REGEX_State *prev;
132 * This is a linked list.
134 struct GNUNET_REGEX_State *next;
142 * If this is an accepting state or not.
147 * Marking of the state. This is used for marking all visited states when
148 * traversing all states of an automaton and for cases where the state id
149 * cannot be used (dfa minimization).
154 * Marking the state as contained. This is used for checking, if the state is
155 * contained in a set in constant time
160 * Marking the state as part of an SCC (Strongly Connected Component). All
161 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
162 * is not a part of any SCC.
167 * Used for SCC detection.
172 * Used for SCC detection.
177 * Human readable name of the automaton. Used for debugging and graph
185 GNUNET_HashCode hash;
188 * Proof for this state.
193 * Number of transitions from this state to other states.
195 unsigned int transition_count;
198 * DLL of transitions.
200 struct Transition *transitions_head;
203 * DLL of transitions.
205 struct Transition *transitions_tail;
208 * Number of incoming transitions.
210 unsigned int incoming_transition_count;
213 * Set of incoming transitions.
215 struct Transition **incoming_transitions;
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;
264 struct GNUNET_REGEX_StateSet
269 struct GNUNET_REGEX_State **states;
272 * Length of the 'states' array.
278 debug_print_state (struct GNUNET_REGEX_State *s)
282 if (NULL == s->proof)
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
289 s->id, s->name, s->marked, s->accepting, s->scc_id,
290 s->transition_count, proof);
294 debug_print_states (struct GNUNET_REGEX_StateSet *sset)
296 struct GNUNET_REGEX_State *s;
299 for (i = 0; i < sset->len; i++)
302 debug_print_state (s);
307 debug_print_transition (struct Transition *t)
321 if (NULL == t->to_state)
324 to_state = t->to_state->name;
326 if (NULL == t->from_state)
329 from_state = t->from_state->name;
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
332 t->id, from_state, label, to_state);
336 debug_print_transitions (struct GNUNET_REGEX_State *s)
338 struct Transition *t;
340 for (t = s->transitions_head; NULL != t; t = t->next)
341 debug_print_transition (t);
345 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
346 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
347 * SCCs inside an automaton.
350 * @param v start vertex
351 * @param index current index
352 * @param stack stack for saving all SCCs
353 * @param stack_size current size of the stack
356 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
357 struct GNUNET_REGEX_State *v, int *index,
358 struct GNUNET_REGEX_State **stack,
359 unsigned int *stack_size)
361 struct GNUNET_REGEX_State *w;
362 struct Transition *t;
367 stack[(*stack_size)++] = v;
370 for (t = v->transitions_head; NULL != t; t = t->next)
373 if (NULL != w && w->index < 0)
375 scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
376 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
378 else if (0 != w->contained)
379 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
382 if (v->lowlink == v->index)
384 w = stack[--(*stack_size)];
392 w->scc_id = ctx->scc_id;
393 w = stack[--(*stack_size)];
396 w->scc_id = ctx->scc_id;
402 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
403 * SCCs will be marked using the scc_id on each state.
409 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
413 struct GNUNET_REGEX_State *v;
414 struct GNUNET_REGEX_State *stack[a->state_count];
415 unsigned int stack_size;
417 for (v = a->states_head; NULL != v; v = v->next)
427 for (i = 0, v = a->states_head; NULL != v; v = v->next)
430 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
435 * Adds a transition from one state to another on 'label'. Does not add
439 * @param from_state starting state for the transition
440 * @param label transition label
441 * @param to_state state to where the transition should point to
444 state_add_transition (struct GNUNET_REGEX_Context *ctx,
445 struct GNUNET_REGEX_State *from_state, const char label,
446 struct GNUNET_REGEX_State *to_state)
449 struct Transition *t;
451 if (NULL == from_state)
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
457 // Do not add duplicate state transitions
459 for (t = from_state->transitions_head; NULL != t; t = t->next)
461 if (t->to_state == to_state && t->label == label &&
462 t->from_state == from_state)
472 t = GNUNET_malloc (sizeof (struct Transition));
473 t->id = ctx->transition_id++;
475 t->to_state = to_state;
476 t->from_state = from_state;
478 // Add outgoing transition to 'from_state'
479 from_state->transition_count++;
480 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
481 from_state->transitions_tail, t);
485 * Compare two states. Used for sorting.
487 * @param a first state
488 * @param b second state
490 * @return an integer less than, equal to, or greater than zero
491 * if the first argument is considered to be respectively
492 * less than, equal to, or greater than the second.
495 state_compare (const void *a, const void *b)
497 struct GNUNET_REGEX_State **s1;
498 struct GNUNET_REGEX_State **s2;
500 s1 = (struct GNUNET_REGEX_State **) a;
501 s2 = (struct GNUNET_REGEX_State **) b;
503 return (*s1)->id - (*s2)->id;
507 * Create a proof for the given state. Intended to be used as a parameter for
508 * automaton_traverse() function.
514 state_create_proof (void *cls, struct GNUNET_REGEX_State *s)
516 struct Transition *inc_t;
522 for (i = 0; i < s->incoming_transition_count; i++)
524 inc_t = s->incoming_transitions[i];
529 GNUNET_assert (inc_t->label != 0 && inc_t->from_state != NULL);
531 if (inc_t->from_state == inc_t->to_state)
532 GNUNET_asprintf (&stars, "%c*", inc_t->label);
535 if (NULL != inc_t->from_state->proof)
536 GNUNET_asprintf (&tmp, "%s%c", inc_t->from_state->proof, inc_t->label);
538 GNUNET_asprintf (&tmp, "%c", inc_t->label);
541 if (i > 0 && NULL != tmp && NULL != proof)
542 GNUNET_asprintf (&proof, "%s|%s", proof, tmp);
543 else if (NULL != tmp)
544 proof = GNUNET_strdup (tmp);
553 if (NULL != s->proof)
554 GNUNET_free (s->proof);
556 if (s->incoming_transition_count > 1)
560 GNUNET_asprintf (&s->proof, "(%s)%s", proof, stars);
563 else if (NULL != proof)
564 GNUNET_asprintf (&s->proof, "(%s)", proof);
574 * Get all edges leaving state 's'.
577 * @param edges all edges leaving 's'.
579 * @return number of edges.
582 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
584 struct Transition *t;
592 for (t = s->transitions_head; NULL != t; t = t->next)
594 if (NULL != t->to_state)
596 edges[count].label = &t->label;
597 edges[count].destination = t->to_state->hash;
605 * Compare to state sets by comparing the id's of the states that are contained
606 * in each set. Both sets are expected to be sorted by id!
608 * @param sset1 first state set
609 * @param sset2 second state set
611 * @return an integer less than, equal to, or greater than zero
612 * if the first argument is considered to be respectively
613 * less than, equal to, or greater than the second.
616 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
617 struct GNUNET_REGEX_StateSet *sset2)
622 if (NULL == sset1 || NULL == sset2)
625 result = sset1->len - sset2->len;
627 for (i = 0; i < sset1->len; i++)
632 result = state_compare (&sset1->states[i], &sset2->states[i]);
638 * Clears the given StateSet 'set'
640 * @param set set to be cleared
643 state_set_clear (struct GNUNET_REGEX_StateSet *set)
647 if (NULL != set->states)
648 GNUNET_free (set->states);
654 * Clears an automaton fragment. Does not destroy the states inside the
657 * @param a automaton to be cleared
660 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
667 a->states_head = NULL;
668 a->states_tail = NULL;
674 * Frees the memory used by State 's'
676 * @param s state that should be destroyed
679 automaton_destroy_state (struct GNUNET_REGEX_State *s)
681 struct Transition *t;
682 struct Transition *next_t;
688 GNUNET_free (s->name);
690 for (t = s->transitions_head; NULL != t; t = next_t)
693 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
697 if (s->incoming_transition_count > 0 && NULL != s->incoming_transitions)
699 GNUNET_free (s->incoming_transitions);
702 state_set_clear (s->nfa_set);
708 * Remove a state from the given automaton 'a'. Always use this function when
709 * altering the states of an automaton. Will also remove all transitions leading
710 * to this state, before destroying it.
713 * @param s state to remove
716 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
717 struct GNUNET_REGEX_State *s)
719 struct GNUNET_REGEX_State *ss;
720 struct GNUNET_REGEX_State *s_check;
721 struct Transition *t_check;
723 if (NULL == a || NULL == s)
728 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
731 // remove all transitions leading to this state
732 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
734 for (t_check = s_check->transitions_head; NULL != t_check;
735 t_check = t_check->next)
737 if (t_check->to_state == ss)
739 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
740 s_check->transitions_tail, t_check);
741 s_check->transition_count--;
746 automaton_destroy_state (ss);
750 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
755 * @param s1 first state
756 * @param s2 second state, will be destroyed
759 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
760 struct GNUNET_REGEX_Automaton *a,
761 struct GNUNET_REGEX_State *s1,
762 struct GNUNET_REGEX_State *s2)
764 struct GNUNET_REGEX_State *s_check;
765 struct Transition *t_check;
768 struct Transition *inc_t;
770 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
775 // 1. Make all transitions pointing to s2 point to s1
776 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
778 for (t_check = s_check->transitions_head; NULL != t_check;
779 t_check = t_check->next)
781 if (s2 == t_check->to_state)
782 t_check->to_state = s1;
786 // 2. Remove all transitions coming from s2
787 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
789 for (i = 0; i < s_check->incoming_transition_count; i++)
791 inc_t = s_check->incoming_transitions[i];
793 if (inc_t != 0 && inc_t->from_state == s2)
795 s_check->incoming_transitions[i] = NULL;
800 // 3. Add all transitions from s2 to sX to s1
801 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
803 if (t_check->to_state != s1)
804 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
807 // 4. Rename s1 to {s1,s2}
808 new_name = GNUNET_strdup (s1->name);
809 if (NULL != s1->name)
811 GNUNET_free (s1->name);
814 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
815 GNUNET_free (new_name);
818 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
820 automaton_destroy_state (s2);
824 * Add a state to the automaton 'a', always use this function to alter the
825 * states DLL of the automaton.
827 * @param a automaton to add the state to
828 * @param s state that should be added
831 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
832 struct GNUNET_REGEX_State *s)
834 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
839 * Function that is called with each state, when traversing an automaton.
844 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
845 struct GNUNET_REGEX_State * s);
848 * Traverses all states that are reachable from state 's'. Expects the states to
849 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
852 * @param cls closure.
853 * @param s start state.
854 * @param action action to be performed on each state.
857 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
858 GNUNET_REGEX_traverse_action action)
860 struct Transition *t;
863 if (GNUNET_NO == s->marked)
865 s->marked = GNUNET_YES;
867 // First make sure all incoming states have been traversed
868 for (i = 0; i < s->incoming_transition_count; i++)
870 if (NULL != s->incoming_transitions[i])
871 automaton_state_traverse (cls, s->incoming_transitions[i]->from_state,
878 for (t = s->transitions_head; NULL != t; t = t->next)
879 automaton_state_traverse (cls, t->to_state, action);
884 * Traverses the given automaton from it's start state, visiting all reachable
885 * states and calling 'action' on each one of them.
887 * @param cls closure.
888 * @param a automaton.
889 * @param action action to be performed on each state.
892 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
893 GNUNET_REGEX_traverse_action action)
895 struct GNUNET_REGEX_State *s;
897 for (s = a->states_head; NULL != s; s = s->next)
898 s->marked = GNUNET_NO;
900 automaton_state_traverse (cls, a->start, action);
904 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
905 * automaton_destroy_state.
908 * @param nfa_states set of NFA states on which the DFA should be based on
910 * @return new DFA state
912 static struct GNUNET_REGEX_State *
913 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
914 struct GNUNET_REGEX_StateSet *nfa_states)
916 struct GNUNET_REGEX_State *s;
919 struct GNUNET_REGEX_State *cstate;
920 struct Transition *ctran;
922 struct Transition *t;
925 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
926 s->id = ctx->state_id++;
936 if (NULL == nfa_states)
938 GNUNET_asprintf (&s->name, "s%i", s->id);
942 s->nfa_set = nfa_states;
944 if (nfa_states->len < 1)
947 // Create a name based on 'sset'
948 s->name = GNUNET_malloc (sizeof (char) * 2);
949 strcat (s->name, "{");
952 for (i = 0; i < nfa_states->len; i++)
954 cstate = nfa_states->states[i];
955 GNUNET_asprintf (&name, "%i,", cstate->id);
959 len = strlen (s->name) + strlen (name) + 1;
960 s->name = GNUNET_realloc (s->name, len);
961 strcat (s->name, name);
966 // Add a transition for each distinct label to NULL state
967 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
969 if (0 != ctran->label)
973 for (t = s->transitions_head; NULL != t; t = t->next)
975 if (t->label == ctran->label)
983 state_add_transition (ctx, s, ctran->label, NULL);
987 // If the nfa_states contain an accepting state, the new dfa state is also
989 if (cstate->accepting)
993 s->name[strlen (s->name) - 1] = '}';
999 * Move from the given state 's' to the next state on transition 'label'
1001 * @param s starting state
1002 * @param label edge label to follow
1004 * @return new state or NULL, if transition on label not possible
1006 static struct GNUNET_REGEX_State *
1007 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1009 struct Transition *t;
1010 struct GNUNET_REGEX_State *new_s;
1017 for (t = s->transitions_head; NULL != t; t = t->next)
1019 if (label == t->label)
1021 new_s = t->to_state;
1030 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1031 * states that are not reachable from the starting state.
1033 * @param a DFA automaton
1036 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1038 struct GNUNET_REGEX_State *s;
1039 struct GNUNET_REGEX_State *s_next;
1041 // 1. unmark all states
1042 for (s = a->states_head; NULL != s; s = s->next)
1043 s->marked = GNUNET_NO;
1045 // 2. traverse dfa from start state and mark all visited states
1046 automaton_traverse (NULL, a, NULL);
1048 // 3. delete all states that were not visited
1049 for (s = a->states_head; NULL != s; s = s_next)
1052 if (GNUNET_NO == s->marked)
1053 automaton_remove_state (a, s);
1058 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1059 * not transition to any other state but themselfes.
1061 * @param a DFA automaton
1064 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1066 struct GNUNET_REGEX_State *s;
1067 struct Transition *t;
1070 GNUNET_assert (DFA == a->type);
1072 for (s = a->states_head; NULL != s; s = s->next)
1078 for (t = s->transitions_head; NULL != t; t = t->next)
1080 if (NULL != t->to_state && t->to_state != s)
1090 // state s is dead, remove it
1091 automaton_remove_state (a, s);
1096 * Merge all non distinguishable states in the DFA 'a'
1098 * @param ctx context
1099 * @param a DFA automaton
1102 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1103 struct GNUNET_REGEX_Automaton *a)
1106 int table[a->state_count][a->state_count];
1107 struct GNUNET_REGEX_State *s1;
1108 struct GNUNET_REGEX_State *s2;
1109 struct Transition *t1;
1110 struct Transition *t2;
1111 struct GNUNET_REGEX_State *s1_next;
1112 struct GNUNET_REGEX_State *s2_next;
1114 int num_equal_edges;
1116 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1122 // Mark all pairs of accepting/!accepting states
1123 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1125 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1127 table[s1->marked][s2->marked] = 0;
1129 if ((s1->accepting && !s2->accepting) ||
1130 (!s1->accepting && s2->accepting))
1132 table[s1->marked][s2->marked] = 1;
1137 // Find all equal states
1142 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1144 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1146 if (0 != table[s1->marked][s2->marked])
1149 num_equal_edges = 0;
1150 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1152 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1154 if (t1->label == t2->label)
1157 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1158 0 != table[t2->to_state->marked][t1->to_state->marked])
1160 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1166 if (num_equal_edges == 0)
1168 table[s1->marked][s2->marked] = -1;
1170 else if (num_equal_edges != s1->transition_count ||
1171 num_equal_edges != s2->transition_count)
1173 // Make sure ALL edges of possible equal states are the same
1174 table[s1->marked][s2->marked] = -2;
1180 // Merge states that are equal
1181 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1184 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1187 if (table[s1->marked][s2->marked] == 0)
1188 automaton_merge_states (ctx, a, s1, s2);
1194 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1195 * dead states and merging all non distinguishable states
1197 * @param ctx context
1198 * @param a DFA automaton
1201 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1202 struct GNUNET_REGEX_Automaton *a)
1207 GNUNET_assert (DFA == a->type);
1209 // 1. remove unreachable states
1210 dfa_remove_unreachable_states (a);
1212 // 2. remove dead states
1213 dfa_remove_dead_states (a);
1215 // 3. Merge nondistinguishable states
1216 dfa_merge_nondistinguishable_states (ctx, a);
1220 * Creates a new NFA fragment. Needs to be cleared using
1221 * automaton_fragment_clear.
1223 * @param start starting state
1224 * @param end end state
1226 * @return new NFA fragment
1228 static struct GNUNET_REGEX_Automaton *
1229 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1230 struct GNUNET_REGEX_State *end)
1232 struct GNUNET_REGEX_Automaton *n;
1234 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1240 if (NULL == start && NULL == end)
1243 automaton_add_state (n, end);
1244 automaton_add_state (n, start);
1253 * Adds a list of states to the given automaton 'n'.
1255 * @param n automaton to which the states should be added
1256 * @param states_head head of the DLL of states
1257 * @param states_tail tail of the DLL of states
1260 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1261 struct GNUNET_REGEX_State *states_head,
1262 struct GNUNET_REGEX_State *states_tail)
1264 struct GNUNET_REGEX_State *s;
1266 if (NULL == n || NULL == states_head)
1268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1272 if (NULL == n->states_head)
1274 n->states_head = states_head;
1275 n->states_tail = states_tail;
1279 if (NULL != states_head)
1281 n->states_tail->next = states_head;
1282 n->states_tail = states_tail;
1285 for (s = states_head; NULL != s; s = s->next)
1290 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1292 * @param ctx context
1293 * @param accepting is it an accepting state or not
1295 * @return new NFA state
1297 static struct GNUNET_REGEX_State *
1298 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1300 struct GNUNET_REGEX_State *s;
1302 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1303 s->id = ctx->state_id++;
1304 s->accepting = accepting;
1311 GNUNET_asprintf (&s->name, "s%i", s->id);
1317 * Calculates the NFA closure set for the given state.
1319 * @param nfa the NFA containing 's'
1320 * @param s starting point state
1321 * @param label transitioning label on which to base the closure on,
1322 * pass 0 for epsilon transition
1324 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1326 static struct GNUNET_REGEX_StateSet *
1327 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1328 struct GNUNET_REGEX_State *s, const char label)
1330 struct GNUNET_REGEX_StateSet *cls;
1331 struct GNUNET_REGEX_StateSet *cls_check;
1332 struct GNUNET_REGEX_State *clsstate;
1333 struct GNUNET_REGEX_State *currentstate;
1334 struct Transition *ctran;
1339 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1340 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1342 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1343 clsstate->contained = 0;
1345 // Add start state to closure only for epsilon closure
1347 GNUNET_array_append (cls->states, cls->len, s);
1349 GNUNET_array_append (cls_check->states, cls_check->len, s);
1350 while (cls_check->len > 0)
1352 currentstate = cls_check->states[cls_check->len - 1];
1353 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1355 for (ctran = currentstate->transitions_head; NULL != ctran;
1356 ctran = ctran->next)
1358 if (NULL != ctran->to_state && label == ctran->label)
1360 clsstate = ctran->to_state;
1362 if (NULL != clsstate && 0 == clsstate->contained)
1364 GNUNET_array_append (cls->states, cls->len, clsstate);
1365 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1366 clsstate->contained = 1;
1371 GNUNET_assert (0 == cls_check->len);
1372 GNUNET_free (cls_check);
1375 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1382 * Calculates the closure set for the given set of states.
1384 * @param nfa the NFA containing 's'
1385 * @param states list of states on which to base the closure on
1386 * @param label transitioning label for which to base the closure on,
1387 * pass 0 for epsilon transition
1389 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1391 static struct GNUNET_REGEX_StateSet *
1392 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1393 struct GNUNET_REGEX_StateSet *states, const char label)
1395 struct GNUNET_REGEX_State *s;
1396 struct GNUNET_REGEX_StateSet *sset;
1397 struct GNUNET_REGEX_StateSet *cls;
1406 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1408 for (i = 0; i < states->len; i++)
1410 s = states->states[i];
1411 sset = nfa_closure_create (nfa, s, label);
1413 for (j = 0; j < sset->len; j++)
1416 for (k = 0; k < cls->len; k++)
1418 if (sset->states[j]->id == cls->states[k]->id)
1425 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1427 state_set_clear (sset);
1431 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1438 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1440 * @param ctx context
1443 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1445 struct GNUNET_REGEX_Automaton *a;
1446 struct GNUNET_REGEX_Automaton *b;
1447 struct GNUNET_REGEX_Automaton *new;
1449 b = ctx->stack_tail;
1450 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1451 a = ctx->stack_tail;
1452 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1454 state_add_transition (ctx, a->end, 0, b->start);
1455 a->end->accepting = 0;
1456 b->end->accepting = 1;
1458 new = nfa_fragment_create (NULL, NULL);
1459 nfa_add_states (new, a->states_head, a->states_tail);
1460 nfa_add_states (new, b->states_head, b->states_tail);
1461 new->start = a->start;
1463 automaton_fragment_clear (a);
1464 automaton_fragment_clear (b);
1466 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1470 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1472 * @param ctx context
1475 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1477 struct GNUNET_REGEX_Automaton *a;
1478 struct GNUNET_REGEX_Automaton *new;
1479 struct GNUNET_REGEX_State *start;
1480 struct GNUNET_REGEX_State *end;
1482 a = ctx->stack_tail;
1483 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1488 "nfa_add_star_op failed, because there was no element on the stack");
1492 start = nfa_state_create (ctx, 0);
1493 end = nfa_state_create (ctx, 1);
1495 state_add_transition (ctx, start, 0, a->start);
1496 state_add_transition (ctx, start, 0, end);
1497 state_add_transition (ctx, a->end, 0, a->start);
1498 state_add_transition (ctx, a->end, 0, end);
1500 a->end->accepting = 0;
1503 new = nfa_fragment_create (start, end);
1504 nfa_add_states (new, a->states_head, a->states_tail);
1505 automaton_fragment_clear (a);
1507 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1511 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1513 * @param ctx context
1516 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1518 struct GNUNET_REGEX_Automaton *a;
1520 a = ctx->stack_tail;
1521 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1523 state_add_transition (ctx, a->end, 0, a->start);
1525 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1529 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1531 * @param ctx context
1534 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1536 struct GNUNET_REGEX_Automaton *a;
1537 struct GNUNET_REGEX_Automaton *new;
1538 struct GNUNET_REGEX_State *start;
1539 struct GNUNET_REGEX_State *end;
1541 a = ctx->stack_tail;
1542 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1547 "nfa_add_question_op failed, because there was no element on the stack");
1551 start = nfa_state_create (ctx, 0);
1552 end = nfa_state_create (ctx, 1);
1554 state_add_transition (ctx, start, 0, a->start);
1555 state_add_transition (ctx, start, 0, end);
1556 state_add_transition (ctx, a->end, 0, end);
1558 a->end->accepting = 0;
1560 new = nfa_fragment_create (start, end);
1561 nfa_add_states (new, a->states_head, a->states_tail);
1562 automaton_fragment_clear (a);
1564 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1568 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1569 * alternates between a and b (a|b)
1571 * @param ctx context
1574 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1576 struct GNUNET_REGEX_Automaton *a;
1577 struct GNUNET_REGEX_Automaton *b;
1578 struct GNUNET_REGEX_Automaton *new;
1579 struct GNUNET_REGEX_State *start;
1580 struct GNUNET_REGEX_State *end;
1582 b = ctx->stack_tail;
1583 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1584 a = ctx->stack_tail;
1585 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1587 start = nfa_state_create (ctx, 0);
1588 end = nfa_state_create (ctx, 1);
1589 state_add_transition (ctx, start, 0, a->start);
1590 state_add_transition (ctx, start, 0, b->start);
1592 state_add_transition (ctx, a->end, 0, end);
1593 state_add_transition (ctx, b->end, 0, end);
1595 a->end->accepting = 0;
1596 b->end->accepting = 0;
1599 new = nfa_fragment_create (start, end);
1600 nfa_add_states (new, a->states_head, a->states_tail);
1601 nfa_add_states (new, b->states_head, b->states_tail);
1602 automaton_fragment_clear (a);
1603 automaton_fragment_clear (b);
1605 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1609 * Adds a new nfa fragment to the stack
1611 * @param ctx context
1612 * @param lit label for nfa transition
1615 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1617 struct GNUNET_REGEX_Automaton *n;
1618 struct GNUNET_REGEX_State *start;
1619 struct GNUNET_REGEX_State *end;
1621 GNUNET_assert (NULL != ctx);
1623 start = nfa_state_create (ctx, 0);
1624 end = nfa_state_create (ctx, 1);
1625 state_add_transition (ctx, start, lit, end);
1626 n = nfa_fragment_create (start, end);
1627 GNUNET_assert (NULL != n);
1628 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1632 * Initialize a new context
1634 * @param ctx context
1637 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1645 ctx->transition_id = 0;
1647 ctx->stack_head = NULL;
1648 ctx->stack_tail = NULL;
1652 * Construct an NFA by parsing the regex string of length 'len'.
1654 * @param regex regular expression string
1655 * @param len length of the string
1657 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1659 struct GNUNET_REGEX_Automaton *
1660 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1662 struct GNUNET_REGEX_Context ctx;
1663 struct GNUNET_REGEX_Automaton *nfa;
1667 unsigned int altcount;
1668 unsigned int atomcount;
1669 unsigned int pcount;
1676 GNUNET_REGEX_context_init (&ctx);
1685 for (count = 0; count < len && *regexp; count++, regexp++)
1693 nfa_add_concatenation (&ctx);
1695 GNUNET_array_grow (p, pcount, pcount + 1);
1696 p[pcount - 1].altcount = altcount;
1697 p[pcount - 1].atomcount = atomcount;
1704 error_msg = "Cannot append '|' to nothing";
1707 while (--atomcount > 0)
1708 nfa_add_concatenation (&ctx);
1714 error_msg = "Missing opening '('";
1719 // Ignore this: "()"
1721 altcount = p[pcount].altcount;
1722 atomcount = p[pcount].atomcount;
1725 while (--atomcount > 0)
1726 nfa_add_concatenation (&ctx);
1727 for (; altcount > 0; altcount--)
1728 nfa_add_alternation (&ctx);
1730 altcount = p[pcount].altcount;
1731 atomcount = p[pcount].atomcount;
1737 error_msg = "Cannot append '*' to nothing";
1740 nfa_add_star_op (&ctx);
1745 error_msg = "Cannot append '+' to nothing";
1748 nfa_add_plus_op (&ctx);
1753 error_msg = "Cannot append '?' to nothing";
1756 nfa_add_question_op (&ctx);
1758 case 92: /* escape: \ */
1765 nfa_add_concatenation (&ctx);
1767 nfa_add_label (&ctx, *regexp);
1774 error_msg = "Unbalanced parenthesis";
1777 while (--atomcount > 0)
1778 nfa_add_concatenation (&ctx);
1779 for (; altcount > 0; altcount--)
1780 nfa_add_alternation (&ctx);
1785 nfa = ctx.stack_tail;
1786 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1788 if (NULL != ctx.stack_head)
1790 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1798 if (NULL != error_msg)
1799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1802 while (NULL != ctx.stack_tail)
1804 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1805 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1812 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1814 * @param ctx context.
1815 * @param nfa NFA automaton.
1816 * @param dfa DFA automaton.
1817 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1821 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1822 struct GNUNET_REGEX_Automaton *nfa,
1823 struct GNUNET_REGEX_Automaton *dfa,
1824 struct GNUNET_REGEX_State *dfa_state)
1826 struct Transition *ctran;
1827 struct GNUNET_REGEX_State *state_iter;
1828 struct GNUNET_REGEX_State *new_dfa_state;
1829 struct GNUNET_REGEX_State *state_contains;
1830 struct GNUNET_REGEX_StateSet *tmp;
1831 struct GNUNET_REGEX_StateSet *nfa_set;
1833 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1835 if (0 == ctran->label || NULL != ctran->to_state)
1838 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1839 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1840 state_set_clear (tmp);
1841 new_dfa_state = dfa_state_create (ctx, nfa_set);
1842 state_contains = NULL;
1843 for (state_iter = dfa->states_head; NULL != state_iter;
1844 state_iter = state_iter->next)
1846 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1847 state_contains = state_iter;
1850 if (NULL == state_contains)
1852 automaton_add_state (dfa, new_dfa_state);
1853 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1854 ctran->to_state = new_dfa_state;
1858 ctran->to_state = state_contains;
1859 automaton_destroy_state (new_dfa_state);
1862 GNUNET_array_append (ctran->to_state->incoming_transitions,
1863 ctran->to_state->incoming_transition_count, ctran);
1868 * Construct DFA for the given 'regex' of length 'len'
1870 * @param regex regular expression string
1871 * @param len length of the regular expression
1873 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1875 struct GNUNET_REGEX_Automaton *
1876 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1878 struct GNUNET_REGEX_Context ctx;
1879 struct GNUNET_REGEX_Automaton *dfa;
1880 struct GNUNET_REGEX_Automaton *nfa;
1881 struct GNUNET_REGEX_StateSet *nfa_set;
1883 GNUNET_REGEX_context_init (&ctx);
1886 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1891 "Could not create DFA, because NFA creation failed\n");
1895 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1898 // Create DFA start state from epsilon closure
1899 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1900 dfa->start = dfa_state_create (&ctx, nfa_set);
1901 automaton_add_state (dfa, dfa->start);
1903 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
1905 GNUNET_REGEX_automaton_destroy (nfa);
1908 dfa_minimize (&ctx, dfa);
1911 scc_tarjan (&ctx, dfa);
1913 // Create proofs for all states
1914 automaton_traverse (NULL, dfa, &state_create_proof);
1921 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
1924 * @param a automaton to be destroyed
1927 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1929 struct GNUNET_REGEX_State *s;
1930 struct GNUNET_REGEX_State *next_state;
1935 for (s = a->states_head; NULL != s;)
1937 next_state = s->next;
1938 automaton_destroy_state (s);
1946 * Save the given automaton as a GraphViz dot file
1948 * @param a the automaton to be saved
1949 * @param filename where to save the file
1952 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1953 const char *filename)
1955 struct GNUNET_REGEX_State *s;
1956 struct Transition *ctran;
1958 char *s_tran = NULL;
1965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1969 if (NULL == filename || strlen (filename) < 1)
1971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1975 p = fopen (filename, "w");
1979 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1984 start = "digraph G {\nrankdir=LR\n";
1985 fwrite (start, strlen (start), 1, p);
1987 for (s = a->states_head; NULL != s; s = s->next)
1991 GNUNET_asprintf (&s_acc,
1992 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1993 s->name, s->scc_id);
1997 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2007 fwrite (s_acc, strlen (s_acc), 1, p);
2008 GNUNET_free (s_acc);
2011 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2013 if (NULL == ctran->to_state)
2015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2016 "Transition from State %i has has no state for transitioning\n",
2021 if (ctran->label == 0)
2023 GNUNET_asprintf (&s_tran,
2024 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2025 s->name, ctran->to_state->name, s->scc_id);
2029 GNUNET_asprintf (&s_tran,
2030 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2031 s->name, ctran->to_state->name, ctran->label,
2037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2042 fwrite (s_tran, strlen (s_tran), 1, p);
2043 GNUNET_free (s_tran);
2049 fwrite (end, strlen (end), 1, p);
2054 * Evaluates the given string using the given DFA automaton
2056 * @param a automaton, type must be DFA
2057 * @param string string that should be evaluated
2059 * @return 0 if string matches, non 0 otherwise
2062 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2065 struct GNUNET_REGEX_State *s;
2069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2070 "Tried to evaluate DFA, but NFA automaton given");
2076 for (strp = string; NULL != strp && *strp; strp++)
2078 s = dfa_move (s, *strp);
2083 if (NULL != s && s->accepting)
2090 * Evaluates the given string using the given NFA automaton
2092 * @param a automaton, type must be NFA
2093 * @param string string that should be evaluated
2095 * @return 0 if string matches, non 0 otherwise
2098 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2101 struct GNUNET_REGEX_State *s;
2102 struct GNUNET_REGEX_StateSet *sset;
2103 struct GNUNET_REGEX_StateSet *new_sset;
2109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2110 "Tried to evaluate NFA, but DFA automaton given");
2116 sset = nfa_closure_create (a, a->start, 0);
2118 for (strp = string; NULL != strp && *strp; strp++)
2120 new_sset = nfa_closure_set_create (a, sset, *strp);
2121 state_set_clear (sset);
2122 sset = nfa_closure_set_create (a, new_sset, 0);
2123 state_set_clear (new_sset);
2126 for (i = 0; i < sset->len; i++)
2128 s = sset->states[i];
2129 if (NULL != s && s->accepting)
2136 state_set_clear (sset);
2141 * Evaluates the given 'string' against the given compiled regex
2143 * @param a automaton
2144 * @param string string to check
2146 * @return 0 if string matches, non 0 otherwise
2149 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2156 result = evaluate_dfa (a, string);
2159 result = evaluate_nfa (a, string);
2162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2163 "Evaluating regex failed, automaton has no type!\n");
2164 result = GNUNET_SYSERR;
2172 * Get the first key for the given 'input_string'. This hashes the first x bits
2173 * of the 'input_strings'.
2175 * @param input_string string.
2176 * @param string_len length of the 'input_string'.
2177 * @param key pointer to where to write the hash code.
2179 * @return number of bits of 'input_string' that have been consumed
2180 * to construct the key
2183 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2184 GNUNET_HashCode * key)
2188 size = string_len < initial_bits ? string_len : initial_bits;
2190 if (NULL == input_string)
2192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2196 GNUNET_CRYPTO_hash (input_string, size, key);
2202 * Check if the given 'proof' matches the given 'key'.
2204 * @param proof partial regex
2207 * @return GNUNET_OK if the proof is valid for the given key
2210 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2216 * Iterate over all edges helper function starting from state 's', calling
2217 * iterator on for each edge.
2220 * @param iterator iterator function called for each edge.
2221 * @param iterator_cls closure.
2224 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2227 struct Transition *t;
2228 struct GNUNET_REGEX_Edge edges[s->transition_count];
2229 unsigned int num_edges;
2231 if (GNUNET_YES != s->marked)
2233 s->marked = GNUNET_YES;
2235 num_edges = state_get_edges (s, edges);
2237 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2239 for (t = s->transitions_head; NULL != t; t = t->next)
2240 iterate_edge (t->to_state, iterator, iterator_cls);
2245 * Iterate over all edges starting from start state of automaton 'a'. Calling
2246 * iterator for each edge.
2248 * @param a automaton.
2249 * @param iterator iterator called for each edge.
2250 * @param iterator_cls closure.
2253 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2254 GNUNET_REGEX_KeyIterator iterator,
2257 struct GNUNET_REGEX_State *s;
2259 for (s = a->states_head; NULL != s; s = s->next)
2260 s->marked = GNUNET_NO;
2262 iterate_edge (a->start, iterator, iterator_cls);