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 * Set of states on which this state is based on. Used when creating a DFA out
209 * of several NFA states.
211 struct GNUNET_REGEX_StateSet *nfa_set;
215 * Transition between two states. Each state can have 0-n transitions. If label
216 * is 0, this is considered to be an epsilon transition.
221 * This is a linked list.
223 struct Transition *prev;
226 * This is a linked list.
228 struct Transition *next;
231 * Unique id of this transition.
236 * Label for this transition. This is basically the edge label for the graph.
241 * State to which this transition leads.
243 struct GNUNET_REGEX_State *to_state;
246 * State from which this transition origins.
248 struct GNUNET_REGEX_State *from_state;
251 * Mark this transition. For example when reversing the automaton.
259 struct GNUNET_REGEX_StateSet
264 struct GNUNET_REGEX_State **states;
267 * Length of the 'states' array.
273 * Debug helper functions
276 debug_print_transitions (struct GNUNET_REGEX_State *);
279 debug_print_state (struct GNUNET_REGEX_State *s)
283 if (NULL == s->proof)
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
290 s->id, s->name, s->marked, s->accepting, s->scc_id,
291 s->transition_count, proof);
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
294 debug_print_transitions (s);
298 debug_print_states (struct GNUNET_REGEX_Automaton *a)
300 struct GNUNET_REGEX_State *s;
302 for (s = a->states_head; NULL != s; s = s->next)
303 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)
412 struct GNUNET_REGEX_State *v;
413 struct GNUNET_REGEX_State *stack[a->state_count];
414 unsigned int stack_size;
416 for (v = a->states_head; NULL != v; v = v->next)
426 for (v = a->states_head; NULL != v; v = v->next)
429 scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size);
434 * Adds a transition from one state to another on 'label'. Does not add
438 * @param from_state starting state for the transition
439 * @param label transition label
440 * @param to_state state to where the transition should point to
443 state_add_transition (struct GNUNET_REGEX_Context *ctx,
444 struct GNUNET_REGEX_State *from_state, const char label,
445 struct GNUNET_REGEX_State *to_state)
448 struct Transition *t;
450 if (NULL == from_state)
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
456 // Do not add duplicate state transitions
458 for (t = from_state->transitions_head; NULL != t; t = t->next)
460 if (t->to_state == to_state && t->label == label &&
461 t->from_state == from_state)
471 t = GNUNET_malloc (sizeof (struct Transition));
472 t->id = ctx->transition_id++;
474 t->to_state = to_state;
475 t->from_state = from_state;
477 // Add outgoing transition to 'from_state'
478 from_state->transition_count++;
479 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
480 from_state->transitions_tail, t);
484 * Compare two states. Used for sorting.
486 * @param a first state
487 * @param b second state
489 * @return an integer less than, equal to, or greater than zero
490 * if the first argument is considered to be respectively
491 * less than, equal to, or greater than the second.
494 state_compare (const void *a, const void *b)
496 struct GNUNET_REGEX_State **s1;
497 struct GNUNET_REGEX_State **s2;
499 s1 = (struct GNUNET_REGEX_State **) a;
500 s2 = (struct GNUNET_REGEX_State **) b;
502 return (*s1)->id - (*s2)->id;
506 * Get all edges leaving state 's'.
509 * @param edges all edges leaving 's'.
511 * @return number of edges.
514 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
516 struct Transition *t;
524 for (t = s->transitions_head; NULL != t; t = t->next)
526 if (NULL != t->to_state)
528 edges[count].label = &t->label;
529 edges[count].destination = t->to_state->hash;
537 * Compare to state sets by comparing the id's of the states that are contained
538 * in each set. Both sets are expected to be sorted by id!
540 * @param sset1 first state set
541 * @param sset2 second state set
543 * @return an integer less than, equal to, or greater than zero
544 * if the first argument is considered to be respectively
545 * less than, equal to, or greater than the second.
548 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
549 struct GNUNET_REGEX_StateSet *sset2)
554 if (NULL == sset1 || NULL == sset2)
557 result = sset1->len - sset2->len;
559 for (i = 0; i < sset1->len; i++)
564 result = state_compare (&sset1->states[i], &sset2->states[i]);
570 * Clears the given StateSet 'set'
572 * @param set set to be cleared
575 state_set_clear (struct GNUNET_REGEX_StateSet *set)
579 GNUNET_free_non_null (set->states);
585 * Clears an automaton fragment. Does not destroy the states inside the
588 * @param a automaton to be cleared
591 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
598 a->states_head = NULL;
599 a->states_tail = NULL;
605 * Frees the memory used by State 's'
607 * @param s state that should be destroyed
610 automaton_destroy_state (struct GNUNET_REGEX_State *s)
612 struct Transition *t;
613 struct Transition *next_t;
618 GNUNET_free_non_null (s->name);
619 GNUNET_free_non_null (s->proof);
621 for (t = s->transitions_head; NULL != t; t = next_t)
624 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
628 state_set_clear (s->nfa_set);
634 * Remove a state from the given automaton 'a'. Always use this function when
635 * altering the states of an automaton. Will also remove all transitions leading
636 * to this state, before destroying it.
639 * @param s state to remove
642 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
643 struct GNUNET_REGEX_State *s)
645 struct GNUNET_REGEX_State *ss;
646 struct GNUNET_REGEX_State *s_check;
647 struct Transition *t_check;
649 if (NULL == a || NULL == s)
654 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
657 // remove all transitions leading to this state
658 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
660 for (t_check = s_check->transitions_head; NULL != t_check;
661 t_check = t_check->next)
663 if (t_check->to_state == ss)
665 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
666 s_check->transitions_tail, t_check);
667 s_check->transition_count--;
672 automaton_destroy_state (ss);
676 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
681 * @param s1 first state
682 * @param s2 second state, will be destroyed
685 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
686 struct GNUNET_REGEX_Automaton *a,
687 struct GNUNET_REGEX_State *s1,
688 struct GNUNET_REGEX_State *s2)
690 struct GNUNET_REGEX_State *s_check;
691 struct Transition *t_check;
694 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
699 // 1. Make all transitions pointing to s2 point to s1
700 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
702 for (t_check = s_check->transitions_head; NULL != t_check;
703 t_check = t_check->next)
705 if (s2 == t_check->to_state)
706 t_check->to_state = s1;
710 // 2. Add all transitions from s2 to sX to s1
711 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
713 if (t_check->to_state != s1)
714 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
717 // 3. Rename s1 to {s1,s2}
718 new_name = GNUNET_strdup (s1->name);
719 GNUNET_free_non_null (s1->name);
720 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
721 GNUNET_free (new_name);
724 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
726 automaton_destroy_state (s2);
730 * Add a state to the automaton 'a', always use this function to alter the
731 * states DLL of the automaton.
733 * @param a automaton to add the state to
734 * @param s state that should be added
737 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
738 struct GNUNET_REGEX_State *s)
740 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
745 * Function that is called with each state, when traversing an automaton.
750 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
751 struct GNUNET_REGEX_State * s);
754 * Traverses all states that are reachable from state 's'. Expects the states to
755 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
758 * @param cls closure.
759 * @param s start state.
760 * @param action action to be performed on each state.
763 automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s,
764 GNUNET_REGEX_traverse_action action)
766 struct Transition *t;
768 if (GNUNET_NO == s->marked)
770 s->marked = GNUNET_YES;
775 for (t = s->transitions_head; NULL != t; t = t->next)
776 automaton_state_traverse (cls, t->to_state, action);
781 * Traverses the given automaton from it's start state, visiting all reachable
782 * states and calling 'action' on each one of them.
784 * @param cls closure.
785 * @param a automaton.
786 * @param action action to be performed on each state.
789 automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a,
790 GNUNET_REGEX_traverse_action action)
792 struct GNUNET_REGEX_State *s;
794 for (s = a->states_head; NULL != s; s = s->next)
795 s->marked = GNUNET_NO;
797 automaton_state_traverse (cls, a->start, action);
801 * Create proofs for all states in the given automaton. Implementation of the
802 * algorithms descriped in chapter 3.2.1 of "Automata Theory, Languages, and
803 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
805 * @param a automaton.
808 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
810 struct GNUNET_REGEX_State *s;
811 struct Transition *t;
816 struct GNUNET_REGEX_State *states[a->state_count];
817 char *R_last[a->state_count][a->state_count];
818 char *R_cur[a->state_count][a->state_count];
824 for (i = 0, s = a->states_head; NULL != s; s = s->next, i++)
831 for (i = 0; i < n; i++)
833 for (j = 0; j < n; j++)
837 for (t = states[i]->transitions_head; NULL != t; t = t->next)
839 if (t->to_state == states[j])
841 if (NULL == R_last[i][j])
842 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
846 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
854 if (NULL == R_last[i][j])
855 GNUNET_asprintf (&R_last[i][j], "");
856 else if (1 < strlen (R_last[i][j]))
859 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
867 for (k = 0; k < n; k++)
869 for (i = 0; i < n; i++)
871 for (j = 0; j < n; j++)
873 if (NULL == R_last[i][k] || NULL == R_last[k][j])
875 if (NULL != R_last[i][j])
876 R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
878 else if (NULL == R_last[i][j] ||
879 0 == strcmp (R_last[i][j], R_last[i][k]))
881 if ((R_last[k][k][0] == '(' &&
882 R_last[k][k][strlen (R_last[k][k]) - 1] == ')') ||
883 (1 == strlen (R_last[k][k])))
884 GNUNET_asprintf (&R_cur[i][j], "(%s%s*%s)", R_last[i][k],
885 R_last[k][k], R_last[k][j]);
887 GNUNET_asprintf (&R_cur[i][j], "(%s(%s)*%s)", R_last[i][k],
888 R_last[k][k], R_last[k][j]);
892 if ((R_last[k][k][0] == '(' &&
893 R_last[k][k][strlen (R_last[k][k]) - 1] == ')') ||
894 (1 == strlen (R_last[k][k])))
895 GNUNET_asprintf (&R_cur[i][j], "(%s|%s%s*%s)", R_last[i][j],
896 R_last[i][k], R_last[k][k], R_last[k][j]);
898 GNUNET_asprintf (&R_cur[i][j], "(%s|%s(%s)*%s)", R_last[i][j],
899 R_last[i][k], R_last[k][k], R_last[k][j]);
904 for (i = 0; i < n; i++)
906 for (j = 0; j < n; j++)
908 GNUNET_free_non_null (R_last[i][j]);
910 if (NULL != R_cur[i][j])
912 R_last[i][j] = GNUNET_strdup (R_cur[i][j]);
913 GNUNET_free (R_cur[i][j]);
921 for (i = 0; i < n; i++)
923 states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]);
927 for (i = 0; i < n; i++)
929 for (j = 0; j < n; j++)
930 GNUNET_free_non_null (R_last[i][j]);
935 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
936 * automaton_destroy_state.
939 * @param nfa_states set of NFA states on which the DFA should be based on
941 * @return new DFA state
943 static struct GNUNET_REGEX_State *
944 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
945 struct GNUNET_REGEX_StateSet *nfa_states)
947 struct GNUNET_REGEX_State *s;
950 struct GNUNET_REGEX_State *cstate;
951 struct Transition *ctran;
953 struct Transition *t;
956 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
957 s->id = ctx->state_id++;
967 if (NULL == nfa_states)
969 GNUNET_asprintf (&s->name, "s%i", s->id);
973 s->nfa_set = nfa_states;
975 if (nfa_states->len < 1)
978 // Create a name based on 'sset'
979 s->name = GNUNET_malloc (sizeof (char) * 2);
980 strcat (s->name, "{");
983 for (i = 0; i < nfa_states->len; i++)
985 cstate = nfa_states->states[i];
986 GNUNET_asprintf (&name, "%i,", cstate->id);
990 len = strlen (s->name) + strlen (name) + 1;
991 s->name = GNUNET_realloc (s->name, len);
992 strcat (s->name, name);
997 // Add a transition for each distinct label to NULL state
998 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1000 if (0 != ctran->label)
1004 for (t = s->transitions_head; NULL != t; t = t->next)
1006 if (t->label == ctran->label)
1014 state_add_transition (ctx, s, ctran->label, NULL);
1018 // If the nfa_states contain an accepting state, the new dfa state is also
1020 if (cstate->accepting)
1024 s->name[strlen (s->name) - 1] = '}';
1030 * Move from the given state 's' to the next state on transition 'label'
1032 * @param s starting state
1033 * @param label edge label to follow
1035 * @return new state or NULL, if transition on label not possible
1037 static struct GNUNET_REGEX_State *
1038 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1040 struct Transition *t;
1041 struct GNUNET_REGEX_State *new_s;
1048 for (t = s->transitions_head; NULL != t; t = t->next)
1050 if (label == t->label)
1052 new_s = t->to_state;
1061 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1062 * states that are not reachable from the starting state.
1064 * @param a DFA automaton
1067 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1069 struct GNUNET_REGEX_State *s;
1070 struct GNUNET_REGEX_State *s_next;
1072 // 1. unmark all states
1073 for (s = a->states_head; NULL != s; s = s->next)
1074 s->marked = GNUNET_NO;
1076 // 2. traverse dfa from start state and mark all visited states
1077 automaton_traverse (NULL, a, NULL);
1079 // 3. delete all states that were not visited
1080 for (s = a->states_head; NULL != s; s = s_next)
1083 if (GNUNET_NO == s->marked)
1084 automaton_remove_state (a, s);
1089 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1090 * not transition to any other state but themselfes.
1092 * @param a DFA automaton
1095 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1097 struct GNUNET_REGEX_State *s;
1098 struct Transition *t;
1101 GNUNET_assert (DFA == a->type);
1103 for (s = a->states_head; NULL != s; s = s->next)
1109 for (t = s->transitions_head; NULL != t; t = t->next)
1111 if (NULL != t->to_state && t->to_state != s)
1121 // state s is dead, remove it
1122 automaton_remove_state (a, s);
1127 * Merge all non distinguishable states in the DFA 'a'
1129 * @param ctx context
1130 * @param a DFA automaton
1133 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1134 struct GNUNET_REGEX_Automaton *a)
1137 int table[a->state_count][a->state_count];
1138 struct GNUNET_REGEX_State *s1;
1139 struct GNUNET_REGEX_State *s2;
1140 struct Transition *t1;
1141 struct Transition *t2;
1142 struct GNUNET_REGEX_State *s1_next;
1143 struct GNUNET_REGEX_State *s2_next;
1145 int num_equal_edges;
1147 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1153 // Mark all pairs of accepting/!accepting states
1154 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1156 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1158 table[s1->marked][s2->marked] = 0;
1160 if ((s1->accepting && !s2->accepting) ||
1161 (!s1->accepting && s2->accepting))
1163 table[s1->marked][s2->marked] = 1;
1168 // Find all equal states
1173 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1175 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1177 if (0 != table[s1->marked][s2->marked])
1180 num_equal_edges = 0;
1181 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1183 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1185 if (t1->label == t2->label)
1188 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1189 0 != table[t2->to_state->marked][t1->to_state->marked])
1191 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1197 if (num_equal_edges != s1->transition_count ||
1198 num_equal_edges != s2->transition_count)
1200 // Make sure ALL edges of possible equal states are the same
1201 table[s1->marked][s2->marked] = -2;
1207 // Merge states that are equal
1208 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1211 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1214 if (table[s1->marked][s2->marked] == 0)
1215 automaton_merge_states (ctx, a, s1, s2);
1221 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1222 * dead states and merging all non distinguishable states
1224 * @param ctx context
1225 * @param a DFA automaton
1228 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1229 struct GNUNET_REGEX_Automaton *a)
1234 GNUNET_assert (DFA == a->type);
1236 // 1. remove unreachable states
1237 dfa_remove_unreachable_states (a);
1239 // 2. remove dead states
1240 dfa_remove_dead_states (a);
1242 // 3. Merge nondistinguishable states
1243 dfa_merge_nondistinguishable_states (ctx, a);
1247 * Creates a new NFA fragment. Needs to be cleared using
1248 * automaton_fragment_clear.
1250 * @param start starting state
1251 * @param end end state
1253 * @return new NFA fragment
1255 static struct GNUNET_REGEX_Automaton *
1256 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1257 struct GNUNET_REGEX_State *end)
1259 struct GNUNET_REGEX_Automaton *n;
1261 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1267 if (NULL == start && NULL == end)
1270 automaton_add_state (n, end);
1271 automaton_add_state (n, start);
1280 * Adds a list of states to the given automaton 'n'.
1282 * @param n automaton to which the states should be added
1283 * @param states_head head of the DLL of states
1284 * @param states_tail tail of the DLL of states
1287 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1288 struct GNUNET_REGEX_State *states_head,
1289 struct GNUNET_REGEX_State *states_tail)
1291 struct GNUNET_REGEX_State *s;
1293 if (NULL == n || NULL == states_head)
1295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1299 if (NULL == n->states_head)
1301 n->states_head = states_head;
1302 n->states_tail = states_tail;
1306 if (NULL != states_head)
1308 n->states_tail->next = states_head;
1309 n->states_tail = states_tail;
1312 for (s = states_head; NULL != s; s = s->next)
1317 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1319 * @param ctx context
1320 * @param accepting is it an accepting state or not
1322 * @return new NFA state
1324 static struct GNUNET_REGEX_State *
1325 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1327 struct GNUNET_REGEX_State *s;
1329 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1330 s->id = ctx->state_id++;
1331 s->accepting = accepting;
1338 GNUNET_asprintf (&s->name, "s%i", s->id);
1344 * Calculates the NFA closure set for the given state.
1346 * @param nfa the NFA containing 's'
1347 * @param s starting point state
1348 * @param label transitioning label on which to base the closure on,
1349 * pass 0 for epsilon transition
1351 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1353 static struct GNUNET_REGEX_StateSet *
1354 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1355 struct GNUNET_REGEX_State *s, const char label)
1357 struct GNUNET_REGEX_StateSet *cls;
1358 struct GNUNET_REGEX_StateSet *cls_check;
1359 struct GNUNET_REGEX_State *clsstate;
1360 struct GNUNET_REGEX_State *currentstate;
1361 struct Transition *ctran;
1366 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1367 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1369 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1370 clsstate->contained = 0;
1372 // Add start state to closure only for epsilon closure
1374 GNUNET_array_append (cls->states, cls->len, s);
1376 GNUNET_array_append (cls_check->states, cls_check->len, s);
1377 while (cls_check->len > 0)
1379 currentstate = cls_check->states[cls_check->len - 1];
1380 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1382 for (ctran = currentstate->transitions_head; NULL != ctran;
1383 ctran = ctran->next)
1385 if (NULL != ctran->to_state && label == ctran->label)
1387 clsstate = ctran->to_state;
1389 if (NULL != clsstate && 0 == clsstate->contained)
1391 GNUNET_array_append (cls->states, cls->len, clsstate);
1392 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1393 clsstate->contained = 1;
1398 GNUNET_assert (0 == cls_check->len);
1399 GNUNET_free (cls_check);
1402 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1409 * Calculates the closure set for the given set of states.
1411 * @param nfa the NFA containing 's'
1412 * @param states list of states on which to base the closure on
1413 * @param label transitioning label for which to base the closure on,
1414 * pass 0 for epsilon transition
1416 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1418 static struct GNUNET_REGEX_StateSet *
1419 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1420 struct GNUNET_REGEX_StateSet *states, const char label)
1422 struct GNUNET_REGEX_State *s;
1423 struct GNUNET_REGEX_StateSet *sset;
1424 struct GNUNET_REGEX_StateSet *cls;
1433 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1435 for (i = 0; i < states->len; i++)
1437 s = states->states[i];
1438 sset = nfa_closure_create (nfa, s, label);
1440 for (j = 0; j < sset->len; j++)
1443 for (k = 0; k < cls->len; k++)
1445 if (sset->states[j]->id == cls->states[k]->id)
1452 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1454 state_set_clear (sset);
1458 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1465 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1467 * @param ctx context
1470 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1472 struct GNUNET_REGEX_Automaton *a;
1473 struct GNUNET_REGEX_Automaton *b;
1474 struct GNUNET_REGEX_Automaton *new;
1476 b = ctx->stack_tail;
1477 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1478 a = ctx->stack_tail;
1479 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1481 state_add_transition (ctx, a->end, 0, b->start);
1482 a->end->accepting = 0;
1483 b->end->accepting = 1;
1485 new = nfa_fragment_create (NULL, NULL);
1486 nfa_add_states (new, a->states_head, a->states_tail);
1487 nfa_add_states (new, b->states_head, b->states_tail);
1488 new->start = a->start;
1490 automaton_fragment_clear (a);
1491 automaton_fragment_clear (b);
1493 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1497 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1499 * @param ctx context
1502 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1504 struct GNUNET_REGEX_Automaton *a;
1505 struct GNUNET_REGEX_Automaton *new;
1506 struct GNUNET_REGEX_State *start;
1507 struct GNUNET_REGEX_State *end;
1509 a = ctx->stack_tail;
1510 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1515 "nfa_add_star_op failed, because there was no element on the stack");
1519 start = nfa_state_create (ctx, 0);
1520 end = nfa_state_create (ctx, 1);
1522 state_add_transition (ctx, start, 0, a->start);
1523 state_add_transition (ctx, start, 0, end);
1524 state_add_transition (ctx, a->end, 0, a->start);
1525 state_add_transition (ctx, a->end, 0, end);
1527 a->end->accepting = 0;
1530 new = nfa_fragment_create (start, end);
1531 nfa_add_states (new, a->states_head, a->states_tail);
1532 automaton_fragment_clear (a);
1534 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1538 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1540 * @param ctx context
1543 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1545 struct GNUNET_REGEX_Automaton *a;
1547 a = ctx->stack_tail;
1548 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1550 state_add_transition (ctx, a->end, 0, a->start);
1552 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1556 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1558 * @param ctx context
1561 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1563 struct GNUNET_REGEX_Automaton *a;
1564 struct GNUNET_REGEX_Automaton *new;
1565 struct GNUNET_REGEX_State *start;
1566 struct GNUNET_REGEX_State *end;
1568 a = ctx->stack_tail;
1569 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1573 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1574 "nfa_add_question_op failed, because there was no element on the stack");
1578 start = nfa_state_create (ctx, 0);
1579 end = nfa_state_create (ctx, 1);
1581 state_add_transition (ctx, start, 0, a->start);
1582 state_add_transition (ctx, start, 0, end);
1583 state_add_transition (ctx, a->end, 0, end);
1585 a->end->accepting = 0;
1587 new = nfa_fragment_create (start, end);
1588 nfa_add_states (new, a->states_head, a->states_tail);
1589 automaton_fragment_clear (a);
1591 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1595 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1596 * alternates between a and b (a|b)
1598 * @param ctx context
1601 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1603 struct GNUNET_REGEX_Automaton *a;
1604 struct GNUNET_REGEX_Automaton *b;
1605 struct GNUNET_REGEX_Automaton *new;
1606 struct GNUNET_REGEX_State *start;
1607 struct GNUNET_REGEX_State *end;
1609 b = ctx->stack_tail;
1610 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1611 a = ctx->stack_tail;
1612 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1614 start = nfa_state_create (ctx, 0);
1615 end = nfa_state_create (ctx, 1);
1616 state_add_transition (ctx, start, 0, a->start);
1617 state_add_transition (ctx, start, 0, b->start);
1619 state_add_transition (ctx, a->end, 0, end);
1620 state_add_transition (ctx, b->end, 0, end);
1622 a->end->accepting = 0;
1623 b->end->accepting = 0;
1626 new = nfa_fragment_create (start, end);
1627 nfa_add_states (new, a->states_head, a->states_tail);
1628 nfa_add_states (new, b->states_head, b->states_tail);
1629 automaton_fragment_clear (a);
1630 automaton_fragment_clear (b);
1632 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1636 * Adds a new nfa fragment to the stack
1638 * @param ctx context
1639 * @param lit label for nfa transition
1642 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1644 struct GNUNET_REGEX_Automaton *n;
1645 struct GNUNET_REGEX_State *start;
1646 struct GNUNET_REGEX_State *end;
1648 GNUNET_assert (NULL != ctx);
1650 start = nfa_state_create (ctx, 0);
1651 end = nfa_state_create (ctx, 1);
1652 state_add_transition (ctx, start, lit, end);
1653 n = nfa_fragment_create (start, end);
1654 GNUNET_assert (NULL != n);
1655 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1659 * Initialize a new context
1661 * @param ctx context
1664 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
1668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
1672 ctx->transition_id = 0;
1674 ctx->stack_head = NULL;
1675 ctx->stack_tail = NULL;
1679 * Construct an NFA by parsing the regex string of length 'len'.
1681 * @param regex regular expression string
1682 * @param len length of the string
1684 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1686 struct GNUNET_REGEX_Automaton *
1687 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1689 struct GNUNET_REGEX_Context ctx;
1690 struct GNUNET_REGEX_Automaton *nfa;
1694 unsigned int altcount;
1695 unsigned int atomcount;
1696 unsigned int pcount;
1703 GNUNET_REGEX_context_init (&ctx);
1712 for (count = 0; count < len && *regexp; count++, regexp++)
1720 nfa_add_concatenation (&ctx);
1722 GNUNET_array_grow (p, pcount, pcount + 1);
1723 p[pcount - 1].altcount = altcount;
1724 p[pcount - 1].atomcount = atomcount;
1731 error_msg = "Cannot append '|' to nothing";
1734 while (--atomcount > 0)
1735 nfa_add_concatenation (&ctx);
1741 error_msg = "Missing opening '('";
1746 // Ignore this: "()"
1748 altcount = p[pcount].altcount;
1749 atomcount = p[pcount].atomcount;
1752 while (--atomcount > 0)
1753 nfa_add_concatenation (&ctx);
1754 for (; altcount > 0; altcount--)
1755 nfa_add_alternation (&ctx);
1757 altcount = p[pcount].altcount;
1758 atomcount = p[pcount].atomcount;
1764 error_msg = "Cannot append '*' to nothing";
1767 nfa_add_star_op (&ctx);
1772 error_msg = "Cannot append '+' to nothing";
1775 nfa_add_plus_op (&ctx);
1780 error_msg = "Cannot append '?' to nothing";
1783 nfa_add_question_op (&ctx);
1785 case 92: /* escape: \ */
1792 nfa_add_concatenation (&ctx);
1794 nfa_add_label (&ctx, *regexp);
1801 error_msg = "Unbalanced parenthesis";
1804 while (--atomcount > 0)
1805 nfa_add_concatenation (&ctx);
1806 for (; altcount > 0; altcount--)
1807 nfa_add_alternation (&ctx);
1809 GNUNET_free_non_null (p);
1811 nfa = ctx.stack_tail;
1812 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1814 if (NULL != ctx.stack_head)
1816 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1824 if (NULL != error_msg)
1825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1827 GNUNET_free_non_null (p);
1829 while (NULL != ctx.stack_tail)
1831 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1832 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1839 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
1841 * @param ctx context.
1842 * @param nfa NFA automaton.
1843 * @param dfa DFA automaton.
1844 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
1848 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
1849 struct GNUNET_REGEX_Automaton *nfa,
1850 struct GNUNET_REGEX_Automaton *dfa,
1851 struct GNUNET_REGEX_State *dfa_state)
1853 struct Transition *ctran;
1854 struct GNUNET_REGEX_State *state_iter;
1855 struct GNUNET_REGEX_State *new_dfa_state;
1856 struct GNUNET_REGEX_State *state_contains;
1857 struct GNUNET_REGEX_StateSet *tmp;
1858 struct GNUNET_REGEX_StateSet *nfa_set;
1860 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
1862 if (0 == ctran->label || NULL != ctran->to_state)
1865 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
1866 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
1867 state_set_clear (tmp);
1868 new_dfa_state = dfa_state_create (ctx, nfa_set);
1869 state_contains = NULL;
1870 for (state_iter = dfa->states_head; NULL != state_iter;
1871 state_iter = state_iter->next)
1873 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1874 state_contains = state_iter;
1877 if (NULL == state_contains)
1879 automaton_add_state (dfa, new_dfa_state);
1880 ctran->to_state = new_dfa_state;
1881 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
1885 ctran->to_state = state_contains;
1886 automaton_destroy_state (new_dfa_state);
1892 * Construct DFA for the given 'regex' of length 'len'
1894 * @param regex regular expression string
1895 * @param len length of the regular expression
1897 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1899 struct GNUNET_REGEX_Automaton *
1900 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1902 struct GNUNET_REGEX_Context ctx;
1903 struct GNUNET_REGEX_Automaton *dfa;
1904 struct GNUNET_REGEX_Automaton *nfa;
1905 struct GNUNET_REGEX_StateSet *nfa_set;
1907 GNUNET_REGEX_context_init (&ctx);
1910 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1915 "Could not create DFA, because NFA creation failed\n");
1919 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1922 // Create DFA start state from epsilon closure
1923 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
1924 dfa->start = dfa_state_create (&ctx, nfa_set);
1925 automaton_add_state (dfa, dfa->start);
1927 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
1929 GNUNET_REGEX_automaton_destroy (nfa);
1932 dfa_minimize (&ctx, dfa);
1935 scc_tarjan (&ctx, dfa);
1937 // Create proofs for all states
1938 automaton_create_proofs (dfa);
1944 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
1947 * @param a automaton to be destroyed
1950 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1952 struct GNUNET_REGEX_State *s;
1953 struct GNUNET_REGEX_State *next_state;
1958 for (s = a->states_head; NULL != s;)
1960 next_state = s->next;
1961 automaton_destroy_state (s);
1969 * Save the given automaton as a GraphViz dot file
1971 * @param a the automaton to be saved
1972 * @param filename where to save the file
1975 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1976 const char *filename)
1978 struct GNUNET_REGEX_State *s;
1979 struct Transition *ctran;
1981 char *s_tran = NULL;
1988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1992 if (NULL == filename || strlen (filename) < 1)
1994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1998 p = fopen (filename, "w");
2002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2007 start = "digraph G {\nrankdir=LR\n";
2008 fwrite (start, strlen (start), 1, p);
2010 for (s = a->states_head; NULL != s; s = s->next)
2014 GNUNET_asprintf (&s_acc,
2015 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2016 s->name, s->scc_id);
2020 GNUNET_asprintf (&s_acc, "\"%s\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2030 fwrite (s_acc, strlen (s_acc), 1, p);
2031 GNUNET_free (s_acc);
2034 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2036 if (NULL == ctran->to_state)
2038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2039 "Transition from State %i has has no state for transitioning\n",
2044 if (ctran->label == 0)
2046 GNUNET_asprintf (&s_tran,
2047 "\"%s\" -> \"%s\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2048 s->name, ctran->to_state->name, s->scc_id);
2052 GNUNET_asprintf (&s_tran,
2053 "\"%s\" -> \"%s\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2054 s->name, ctran->to_state->name, ctran->label,
2060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2065 fwrite (s_tran, strlen (s_tran), 1, p);
2066 GNUNET_free (s_tran);
2072 fwrite (end, strlen (end), 1, p);
2077 * Evaluates the given string using the given DFA automaton
2079 * @param a automaton, type must be DFA
2080 * @param string string that should be evaluated
2082 * @return 0 if string matches, non 0 otherwise
2085 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2088 struct GNUNET_REGEX_State *s;
2092 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2093 "Tried to evaluate DFA, but NFA automaton given");
2099 for (strp = string; NULL != strp && *strp; strp++)
2101 s = dfa_move (s, *strp);
2106 if (NULL != s && s->accepting)
2113 * Evaluates the given string using the given NFA automaton
2115 * @param a automaton, type must be NFA
2116 * @param string string that should be evaluated
2118 * @return 0 if string matches, non 0 otherwise
2121 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2124 struct GNUNET_REGEX_State *s;
2125 struct GNUNET_REGEX_StateSet *sset;
2126 struct GNUNET_REGEX_StateSet *new_sset;
2132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2133 "Tried to evaluate NFA, but DFA automaton given");
2139 sset = nfa_closure_create (a, a->start, 0);
2141 for (strp = string; NULL != strp && *strp; strp++)
2143 new_sset = nfa_closure_set_create (a, sset, *strp);
2144 state_set_clear (sset);
2145 sset = nfa_closure_set_create (a, new_sset, 0);
2146 state_set_clear (new_sset);
2149 for (i = 0; i < sset->len; i++)
2151 s = sset->states[i];
2152 if (NULL != s && s->accepting)
2159 state_set_clear (sset);
2164 * Evaluates the given 'string' against the given compiled regex
2166 * @param a automaton
2167 * @param string string to check
2169 * @return 0 if string matches, non 0 otherwise
2172 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2179 result = evaluate_dfa (a, string);
2182 result = evaluate_nfa (a, string);
2185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2186 "Evaluating regex failed, automaton has no type!\n");
2187 result = GNUNET_SYSERR;
2195 * Get the first key for the given 'input_string'. This hashes the first x bits
2196 * of the 'input_strings'.
2198 * @param input_string string.
2199 * @param string_len length of the 'input_string'.
2200 * @param key pointer to where to write the hash code.
2202 * @return number of bits of 'input_string' that have been consumed
2203 * to construct the key
2206 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2207 GNUNET_HashCode * key)
2211 size = string_len < initial_bits ? string_len : initial_bits;
2213 if (NULL == input_string)
2215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2219 GNUNET_CRYPTO_hash (input_string, size, key);
2225 * Check if the given 'proof' matches the given 'key'.
2227 * @param proof partial regex
2230 * @return GNUNET_OK if the proof is valid for the given key
2233 GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode * key)
2239 * Iterate over all edges helper function starting from state 's', calling
2240 * iterator on for each edge.
2243 * @param iterator iterator function called for each edge.
2244 * @param iterator_cls closure.
2247 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2250 struct Transition *t;
2251 struct GNUNET_REGEX_Edge edges[s->transition_count];
2252 unsigned int num_edges;
2254 if (GNUNET_YES != s->marked)
2256 s->marked = GNUNET_YES;
2258 num_edges = state_get_edges (s, edges);
2260 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2262 for (t = s->transitions_head; NULL != t; t = t->next)
2263 iterate_edge (t->to_state, iterator, iterator_cls);
2268 * Iterate over all edges starting from start state of automaton 'a'. Calling
2269 * iterator for each edge.
2271 * @param a automaton.
2272 * @param iterator iterator called for each edge.
2273 * @param iterator_cls closure.
2276 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2277 GNUNET_REGEX_KeyIterator iterator,
2280 struct GNUNET_REGEX_State *s;
2282 for (s = a->states_head; NULL != s; s = s->next)
2283 s->marked = GNUNET_NO;
2285 iterate_edge (a->start, iterator, iterator_cls);