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 helper functions
281 debug_print_transitions (struct GNUNET_REGEX_State *);
284 debug_print_state (struct GNUNET_REGEX_State *s)
288 if (NULL == s->proof)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
295 s->id, s->name, s->marked, s->accepting, s->scc_id,
296 s->transition_count, proof);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
299 debug_print_transitions (s);
303 debug_print_states (struct GNUNET_REGEX_Automaton *a)
305 struct GNUNET_REGEX_State *s;
307 for (s = a->states_head; NULL != s; s = s->next)
308 debug_print_state (s);
312 debug_print_transition (struct Transition *t)
326 if (NULL == t->to_state)
329 to_state = t->to_state->name;
331 if (NULL == t->from_state)
334 from_state = t->from_state->name;
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
337 t->id, from_state, label, to_state);
341 debug_print_transitions (struct GNUNET_REGEX_State *s)
343 struct Transition *t;
345 for (t = s->transitions_head; NULL != t; t = t->next)
346 debug_print_transition (t);
350 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
351 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
352 * SCCs inside an automaton.
355 * @param v start vertex
356 * @param index current index
357 * @param stack stack for saving all SCCs
358 * @param stack_size current size of the stack
361 scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx,
362 struct GNUNET_REGEX_State *v, int *index,
363 struct GNUNET_REGEX_State **stack,
364 unsigned int *stack_size)
366 struct GNUNET_REGEX_State *w;
367 struct Transition *t;
372 stack[(*stack_size)++] = v;
375 for (t = v->transitions_head; NULL != t; t = t->next)
378 if (NULL != w && w->index < 0)
380 scc_tarjan_strongconnect (ctx, w, index, stack, stack_size);
381 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
383 else if (0 != w->contained)
384 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
387 if (v->lowlink == v->index)
389 w = stack[--(*stack_size)];
397 w->scc_id = ctx->scc_id;
398 w = stack[--(*stack_size)];
401 w->scc_id = ctx->scc_id;
407 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
408 * SCCs will be marked using the scc_id on each state.
414 scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a)
417 struct GNUNET_REGEX_State *v;
418 struct GNUNET_REGEX_State *stack[a->state_count];
419 unsigned int stack_size;
421 for (v = a->states_head; NULL != v; v = v->next)
431 for (v = a->states_head; NULL != v; v = v->next)
434 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
439 * Adds a transition from one state to another on 'label'. Does not add
443 * @param from_state starting state for the transition
444 * @param label transition label
445 * @param to_state state to where the transition should point to
448 state_add_transition (struct GNUNET_REGEX_Context *ctx,
449 struct GNUNET_REGEX_State *from_state, const char label,
450 struct GNUNET_REGEX_State *to_state)
453 struct Transition *t;
455 if (NULL == from_state)
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
461 // Do not add duplicate state transitions
463 for (t = from_state->transitions_head; NULL != t; t = t->next)
465 if (t->to_state == to_state && t->label == label &&
466 t->from_state == from_state)
476 t = GNUNET_malloc (sizeof (struct Transition));
477 t->id = ctx->transition_id++;
479 t->to_state = to_state;
480 t->from_state = from_state;
482 // Add outgoing transition to 'from_state'
483 from_state->transition_count++;
484 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
485 from_state->transitions_tail, t);
489 * Compare two states. Used for sorting.
491 * @param a first state
492 * @param b second state
494 * @return an integer less than, equal to, or greater than zero
495 * if the first argument is considered to be respectively
496 * less than, equal to, or greater than the second.
499 state_compare (const void *a, const void *b)
501 struct GNUNET_REGEX_State **s1;
502 struct GNUNET_REGEX_State **s2;
504 s1 = (struct GNUNET_REGEX_State **) a;
505 s2 = (struct GNUNET_REGEX_State **) b;
507 return (*s1)->id - (*s2)->id;
511 * Create a proof for the given state. Intended to be used as a parameter for
512 * automaton_traverse() function.
518 state_create_proof (void *cls, struct GNUNET_REGEX_State *s)
520 struct Transition *inc_t;
526 for (i = 0; i < s->incoming_transition_count; i++)
528 inc_t = s->incoming_transitions[i];
533 GNUNET_assert (inc_t->label != 0 && inc_t->from_state != NULL);
535 if (inc_t->from_state == inc_t->to_state)
536 GNUNET_asprintf (&stars, "%c*", inc_t->label);
539 if (NULL != inc_t->from_state->proof)
540 GNUNET_asprintf (&tmp, "%s%c", inc_t->from_state->proof, inc_t->label);
542 GNUNET_asprintf (&tmp, "%c", inc_t->label);
545 if (i > 0 && NULL != tmp && NULL != proof)
546 GNUNET_asprintf (&proof, "%s|%s", proof, tmp);
547 else if (NULL != tmp)
548 proof = GNUNET_strdup (tmp);
557 if (NULL != s->proof)
558 GNUNET_free (s->proof);
560 if (s->incoming_transition_count > 1)
564 GNUNET_asprintf (&s->proof, "(%s)%s", proof, stars);
567 else if (NULL != proof)
568 GNUNET_asprintf (&s->proof, "(%s)", proof);
578 * Get all edges leaving state 's'.
581 * @param edges all edges leaving 's'.
583 * @return number of edges.
586 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
588 struct Transition *t;
596 for (t = s->transitions_head; NULL != t; t = t->next)
598 if (NULL != t->to_state)
600 edges[count].label = &t->label;
601 edges[count].destination = t->to_state->hash;
609 * Compare to state sets by comparing the id's of the states that are contained
610 * in each set. Both sets are expected to be sorted by id!
612 * @param sset1 first state set
613 * @param sset2 second state set
615 * @return an integer less than, equal to, or greater than zero
616 * if the first argument is considered to be respectively
617 * less than, equal to, or greater than the second.
620 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
621 struct GNUNET_REGEX_StateSet *sset2)
626 if (NULL == sset1 || NULL == sset2)
629 result = sset1->len - sset2->len;
631 for (i = 0; i < sset1->len; i++)
636 result = state_compare (&sset1->states[i], &sset2->states[i]);
642 * Clears the given StateSet 'set'
644 * @param set set to be cleared
647 state_set_clear (struct GNUNET_REGEX_StateSet *set)
651 if (NULL != set->states)
652 GNUNET_free (set->states);
658 * Clears an automaton fragment. Does not destroy the states inside the
661 * @param a automaton to be cleared
664 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
671 a->states_head = NULL;
672 a->states_tail = NULL;
678 * Frees the memory used by State 's'
680 * @param s state that should be destroyed
683 automaton_destroy_state (struct GNUNET_REGEX_State *s)
685 struct Transition *t;
686 struct Transition *next_t;
692 GNUNET_free (s->name);
694 for (t = s->transitions_head; NULL != t; t = next_t)
697 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
701 if (s->incoming_transition_count > 0 && NULL != s->incoming_transitions)
703 GNUNET_free (s->incoming_transitions);
706 state_set_clear (s->nfa_set);
712 * Remove a state from the given automaton 'a'. Always use this function when
713 * altering the states of an automaton. Will also remove all transitions leading
714 * to this state, before destroying it.
717 * @param s state to remove
720 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
721 struct GNUNET_REGEX_State *s)
723 struct GNUNET_REGEX_State *ss;
724 struct GNUNET_REGEX_State *s_check;
725 struct Transition *t_check;
727 if (NULL == a || NULL == s)
732 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
735 // remove all transitions leading to this state
736 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
738 for (t_check = s_check->transitions_head; NULL != t_check;
739 t_check = t_check->next)
741 if (t_check->to_state == ss)
743 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
744 s_check->transitions_tail, t_check);
745 s_check->transition_count--;
750 automaton_destroy_state (ss);
754 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
759 * @param s1 first state
760 * @param s2 second state, will be destroyed
763 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
764 struct GNUNET_REGEX_Automaton *a,
765 struct GNUNET_REGEX_State *s1,
766 struct GNUNET_REGEX_State *s2)
768 struct GNUNET_REGEX_State *s_check;
769 struct Transition *t_check;
772 struct Transition *inc_t;
774 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
779 // 1. Make all transitions pointing to s2 point to s1
780 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
782 for (t_check = s_check->transitions_head; NULL != t_check;
783 t_check = t_check->next)
785 if (s2 == t_check->to_state)
786 t_check->to_state = s1;
790 // 2. Remove all transitions coming from s2
791 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
793 for (i = 0; i < s_check->incoming_transition_count; i++)
795 inc_t = s_check->incoming_transitions[i];
797 if (inc_t != 0 && inc_t->from_state == s2)
799 s_check->incoming_transitions[i] = NULL;
804 // 3. Add all transitions from s2 to sX to s1
805 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
807 if (t_check->to_state != s1)
808 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
811 // 4. Rename s1 to {s1,s2}
812 new_name = GNUNET_strdup (s1->name);
813 if (NULL != s1->name)
815 GNUNET_free (s1->name);
818 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
819 GNUNET_free (new_name);
822 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
824 automaton_destroy_state (s2);
828 * Add a state to the automaton 'a', always use this function to alter the
829 * states DLL of the automaton.
831 * @param a automaton to add the state to
832 * @param s state that should be added
835 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
836 struct GNUNET_REGEX_State *s)
838 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
843 * Function that is called with each state, when traversing an automaton.
848 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
849 struct GNUNET_REGEX_State * s);
852 * Traverses all states that are reachable from state 's'. Expects the states to
853 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
856 * @param cls closure.
857 * @param s start state.
858 * @param action action to be performed on each state.
861 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
862 GNUNET_REGEX_traverse_action action)
864 struct Transition *t;
867 if (GNUNET_NO == s->marked)
869 s->marked = GNUNET_YES;
871 // First make sure all incoming states have been traversed
872 for (i = 0; i < s->incoming_transition_count; i++)
874 if (NULL != s->incoming_transitions[i])
875 automaton_state_traverse (cls, s->incoming_transitions[i]->from_state,
882 for (t = s->transitions_head; NULL != t; t = t->next)
883 automaton_state_traverse (cls, t->to_state, action);
888 * Traverses the given automaton from it's start state, visiting all reachable
889 * states and calling 'action' on each one of them.
891 * @param cls closure.
892 * @param a automaton.
893 * @param action action to be performed on each state.
896 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
897 GNUNET_REGEX_traverse_action action)
899 struct GNUNET_REGEX_State *s;
901 for (s = a->states_head; NULL != s; s = s->next)
902 s->marked = GNUNET_NO;
904 automaton_state_traverse (cls, a->start, action);
908 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
909 * automaton_destroy_state.
912 * @param nfa_states set of NFA states on which the DFA should be based on
914 * @return new DFA state
916 static struct GNUNET_REGEX_State *
917 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
918 struct GNUNET_REGEX_StateSet *nfa_states)
920 struct GNUNET_REGEX_State *s;
923 struct GNUNET_REGEX_State *cstate;
924 struct Transition *ctran;
926 struct Transition *t;
929 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
930 s->id = ctx->state_id++;
940 if (NULL == nfa_states)
942 GNUNET_asprintf (&s->name, "s%i", s->id);
946 s->nfa_set = nfa_states;
948 if (nfa_states->len < 1)
951 // Create a name based on 'sset'
952 s->name = GNUNET_malloc (sizeof (char) * 2);
953 strcat (s->name, "{");
956 for (i = 0; i < nfa_states->len; i++)
958 cstate = nfa_states->states[i];
959 GNUNET_asprintf (&name, "%i,", cstate->id);
963 len = strlen (s->name) + strlen (name) + 1;
964 s->name = GNUNET_realloc (s->name, len);
965 strcat (s->name, name);
970 // Add a transition for each distinct label to NULL state
971 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
973 if (0 != ctran->label)
977 for (t = s->transitions_head; NULL != t; t = t->next)
979 if (t->label == ctran->label)
987 state_add_transition (ctx, s, ctran->label, NULL);
991 // If the nfa_states contain an accepting state, the new dfa state is also
993 if (cstate->accepting)
997 s->name[strlen (s->name) - 1] = '}';
1003 * Move from the given state 's' to the next state on transition 'label'
1005 * @param s starting state
1006 * @param label edge label to follow
1008 * @return new state or NULL, if transition on label not possible
1010 static struct GNUNET_REGEX_State *
1011 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1013 struct Transition *t;
1014 struct GNUNET_REGEX_State *new_s;
1021 for (t = s->transitions_head; NULL != t; t = t->next)
1023 if (label == t->label)
1025 new_s = t->to_state;
1034 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1035 * states that are not reachable from the starting state.
1037 * @param a DFA automaton
1040 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1042 struct GNUNET_REGEX_State *s;
1043 struct GNUNET_REGEX_State *s_next;
1045 // 1. unmark all states
1046 for (s = a->states_head; NULL != s; s = s->next)
1047 s->marked = GNUNET_NO;
1049 // 2. traverse dfa from start state and mark all visited states
1050 automaton_traverse (NULL, a, NULL);
1052 // 3. delete all states that were not visited
1053 for (s = a->states_head; NULL != s; s = s_next)
1056 if (GNUNET_NO == s->marked)
1057 automaton_remove_state (a, s);
1062 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1063 * not transition to any other state but themselfes.
1065 * @param a DFA automaton
1068 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1070 struct GNUNET_REGEX_State *s;
1071 struct Transition *t;
1074 GNUNET_assert (DFA == a->type);
1076 for (s = a->states_head; NULL != s; s = s->next)
1082 for (t = s->transitions_head; NULL != t; t = t->next)
1084 if (NULL != t->to_state && t->to_state != s)
1094 // state s is dead, remove it
1095 automaton_remove_state (a, s);
1100 * Merge all non distinguishable states in the DFA 'a'
1102 * @param ctx context
1103 * @param a DFA automaton
1106 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1107 struct GNUNET_REGEX_Automaton *a)
1110 int table[a->state_count][a->state_count];
1111 struct GNUNET_REGEX_State *s1;
1112 struct GNUNET_REGEX_State *s2;
1113 struct Transition *t1;
1114 struct Transition *t2;
1115 struct GNUNET_REGEX_State *s1_next;
1116 struct GNUNET_REGEX_State *s2_next;
1118 int num_equal_edges;
1120 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1126 // Mark all pairs of accepting/!accepting states
1127 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1129 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1131 table[s1->marked][s2->marked] = 0;
1133 if ((s1->accepting && !s2->accepting) ||
1134 (!s1->accepting && s2->accepting))
1136 table[s1->marked][s2->marked] = 1;
1141 // Find all equal states
1146 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1148 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1150 if (0 != table[s1->marked][s2->marked])
1153 num_equal_edges = 0;
1154 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1156 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1158 if (t1->label == t2->label)
1161 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1162 0 != table[t2->to_state->marked][t1->to_state->marked])
1164 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1170 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);
1920 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
1923 * @param a automaton to be destroyed
1926 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1928 struct GNUNET_REGEX_State *s;
1929 struct GNUNET_REGEX_State *next_state;
1934 for (s = a->states_head; NULL != s;)
1936 next_state = s->next;
1937 automaton_destroy_state (s);
1945 * Save the given automaton as a GraphViz dot file
1947 * @param a the automaton to be saved
1948 * @param filename where to save the file
1951 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1952 const char *filename)
1954 struct GNUNET_REGEX_State *s;
1955 struct Transition *ctran;
1957 char *s_tran = NULL;
1964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1968 if (NULL == filename || strlen (filename) < 1)
1970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1974 p = fopen (filename, "w");
1978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1983 start = "digraph G {\nrankdir=LR\n";
1984 fwrite (start, strlen (start), 1, p);
1986 for (s = a->states_head; NULL != s; s = s->next)
1990 GNUNET_asprintf (&s_acc,
1991 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
1992 s->name, s->scc_id);
1996 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2006 fwrite (s_acc, strlen (s_acc), 1, p);
2007 GNUNET_free (s_acc);
2010 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2012 if (NULL == ctran->to_state)
2014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2015 "Transition from State %i has has no state for transitioning\n",
2020 if (ctran->label == 0)
2022 GNUNET_asprintf (&s_tran,
2023 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2024 s->name, ctran->to_state->name, s->scc_id);
2028 GNUNET_asprintf (&s_tran,
2029 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2030 s->name, ctran->to_state->name, ctran->label,
2036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2041 fwrite (s_tran, strlen (s_tran), 1, p);
2042 GNUNET_free (s_tran);
2048 fwrite (end, strlen (end), 1, p);
2053 * Evaluates the given string using the given DFA automaton
2055 * @param a automaton, type must be DFA
2056 * @param string string that should be evaluated
2058 * @return 0 if string matches, non 0 otherwise
2061 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2064 struct GNUNET_REGEX_State *s;
2068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2069 "Tried to evaluate DFA, but NFA automaton given");
2075 for (strp = string; NULL != strp && *strp; strp++)
2077 s = dfa_move (s, *strp);
2082 if (NULL != s && s->accepting)
2089 * Evaluates the given string using the given NFA automaton
2091 * @param a automaton, type must be NFA
2092 * @param string string that should be evaluated
2094 * @return 0 if string matches, non 0 otherwise
2097 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2100 struct GNUNET_REGEX_State *s;
2101 struct GNUNET_REGEX_StateSet *sset;
2102 struct GNUNET_REGEX_StateSet *new_sset;
2108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2109 "Tried to evaluate NFA, but DFA automaton given");
2115 sset = nfa_closure_create (a, a->start, 0);
2117 for (strp = string; NULL != strp && *strp; strp++)
2119 new_sset = nfa_closure_set_create (a, sset, *strp);
2120 state_set_clear (sset);
2121 sset = nfa_closure_set_create (a, new_sset, 0);
2122 state_set_clear (new_sset);
2125 for (i = 0; i < sset->len; i++)
2127 s = sset->states[i];
2128 if (NULL != s && s->accepting)
2135 state_set_clear (sset);
2140 * Evaluates the given 'string' against the given compiled regex
2142 * @param a automaton
2143 * @param string string to check
2145 * @return 0 if string matches, non 0 otherwise
2148 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2155 result = evaluate_dfa (a, string);
2158 result = evaluate_nfa (a, string);
2161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2162 "Evaluating regex failed, automaton has no type!\n");
2163 result = GNUNET_SYSERR;
2171 * Get the first key for the given 'input_string'. This hashes the first x bits
2172 * of the 'input_strings'.
2174 * @param input_string string.
2175 * @param string_len length of the 'input_string'.
2176 * @param key pointer to where to write the hash code.
2178 * @return number of bits of 'input_string' that have been consumed
2179 * to construct the key
2182 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2183 GNUNET_HashCode * key)
2187 size = string_len < initial_bits ? string_len : initial_bits;
2189 if (NULL == input_string)
2191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2195 GNUNET_CRYPTO_hash (input_string, size, key);
2201 * Check if the given 'proof' matches the given 'key'.
2203 * @param proof partial regex
2206 * @return GNUNET_OK if the proof is valid for the given key
2209 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2215 * Iterate over all edges helper function starting from state 's', calling
2216 * iterator on for each edge.
2219 * @param iterator iterator function called for each edge.
2220 * @param iterator_cls closure.
2223 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2226 struct Transition *t;
2227 struct GNUNET_REGEX_Edge edges[s->transition_count];
2228 unsigned int num_edges;
2230 if (GNUNET_YES != s->marked)
2232 s->marked = GNUNET_YES;
2234 num_edges = state_get_edges (s, edges);
2236 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2238 for (t = s->transitions_head; NULL != t; t = t->next)
2239 iterate_edge (t->to_state, iterator, iterator_cls);
2244 * Iterate over all edges starting from start state of automaton 'a'. Calling
2245 * iterator for each edge.
2247 * @param a automaton.
2248 * @param iterator iterator called for each edge.
2249 * @param iterator_cls closure.
2252 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2253 GNUNET_REGEX_KeyIterator iterator,
2256 struct GNUNET_REGEX_State *s;
2258 for (s = a->states_head; NULL != s; s = s->next)
2259 s->marked = GNUNET_NO;
2261 iterate_edge (a->start, iterator, iterator_cls);