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"
29 #include "regex_internal.h"
32 * Constant for how many bits the initial string regex should have.
34 #define INITIAL_BITS 10
37 * Context that contains an id counter for states and transitions as well as a
38 * DLL of automatons used as a stack for NFA construction.
40 struct GNUNET_REGEX_Context
45 unsigned int state_id;
48 * Unique transition id.
50 unsigned int transition_id;
53 * DLL of GNUNET_REGEX_Automaton's used as a stack.
55 struct GNUNET_REGEX_Automaton *stack_head;
58 * DLL of GNUNET_REGEX_Automaton's used as a stack.
60 struct GNUNET_REGEX_Automaton *stack_tail;
64 * Type of an automaton.
66 enum GNUNET_REGEX_AutomatonType
73 * Automaton representation.
75 struct GNUNET_REGEX_Automaton
78 * Linked list of NFAs used for partial NFA creation.
80 struct GNUNET_REGEX_Automaton *prev;
83 * Linked list of NFAs used for partial NFA creation.
85 struct GNUNET_REGEX_Automaton *next;
88 * First state of the automaton. This is mainly used for constructing an NFA,
89 * where each NFA itself consists of one or more NFAs linked together.
91 struct GNUNET_REGEX_State *start;
94 * End state of the partial NFA. This is undefined for DFAs
96 struct GNUNET_REGEX_State *end;
99 * Number of states in the automaton.
101 unsigned int state_count;
106 struct GNUNET_REGEX_State *states_head;
111 struct GNUNET_REGEX_State *states_tail;
114 * Type of the automaton.
116 enum GNUNET_REGEX_AutomatonType type;
124 * Canonical regex (result of RX->NFA->DFA->RX)
126 char *canonical_regex;
130 * A state. Can be used in DFA and NFA automatons.
132 struct GNUNET_REGEX_State
135 * This is a linked list.
137 struct GNUNET_REGEX_State *prev;
140 * This is a linked list.
142 struct GNUNET_REGEX_State *next;
150 * If this is an accepting state or not.
155 * Marking of the state. This is used for marking all visited states when
156 * traversing all states of an automaton and for cases where the state id
157 * cannot be used (dfa minimization).
162 * Marking the state as contained. This is used for checking, if the state is
163 * contained in a set in constant time
168 * Marking the state as part of an SCC (Strongly Connected Component). All
169 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
170 * is not a part of any SCC.
175 * Used for SCC detection.
180 * Used for SCC detection.
185 * Human readable name of the automaton. Used for debugging and graph
193 struct GNUNET_HashCode hash;
196 * State ID for proof creation.
198 unsigned int proof_id;
201 * Proof for this state.
206 * Number of transitions from this state to other states.
208 unsigned int transition_count;
211 * DLL of transitions.
213 struct Transition *transitions_head;
216 * DLL of transitions.
218 struct Transition *transitions_tail;
221 * Set of states on which this state is based on. Used when creating a DFA out
222 * of several NFA states.
224 struct GNUNET_REGEX_StateSet *nfa_set;
228 * Transition between two states. Each state can have 0-n transitions. If label
229 * is 0, this is considered to be an epsilon transition.
234 * This is a linked list.
236 struct Transition *prev;
239 * This is a linked list.
241 struct Transition *next;
244 * Unique id of this transition.
249 * Label for this transition. This is basically the edge label for the graph.
254 * State to which this transition leads.
256 struct GNUNET_REGEX_State *to_state;
259 * State from which this transition origins.
261 struct GNUNET_REGEX_State *from_state;
264 * Mark this transition. For example when reversing the automaton.
272 struct GNUNET_REGEX_StateSet
277 struct GNUNET_REGEX_State **states;
280 * Length of the 'states' array.
286 * Debug helper functions
290 * Print all the transitions of state 's'.
292 * @param s state for which to print it's transitions.
295 debug_print_transitions (struct GNUNET_REGEX_State *s);
298 * Print information of the given state 's'.
300 * @param s state for which debug information should be printed.
303 debug_print_state (struct GNUNET_REGEX_State *s)
307 if (NULL == s->proof)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
314 s->id, s->name, s->marked, s->accepting, s->scc_id,
315 s->transition_count, proof);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
318 debug_print_transitions (s);
322 * Print debug information for all states contained in the automaton 'a'.
324 * @param a automaton for which debug information of it's states should be printed.
327 debug_print_states (struct GNUNET_REGEX_Automaton *a)
329 struct GNUNET_REGEX_State *s;
331 for (s = a->states_head; NULL != s; s = s->next)
332 debug_print_state (s);
336 * Print debug information for given transition 't'.
338 * @param t transition for which to print debug info.
341 debug_print_transition (struct Transition *t)
355 if (NULL == t->to_state)
358 to_state = t->to_state->name;
360 if (NULL == t->from_state)
363 from_state = t->from_state->name;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
366 t->id, from_state, label, to_state);
370 debug_print_transitions (struct GNUNET_REGEX_State *s)
372 struct Transition *t;
374 for (t = s->transitions_head; NULL != t; t = t->next)
375 debug_print_transition (t);
380 * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside
381 * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all
382 * SCCs inside an automaton.
384 * @param scc_counter counter for numbering the sccs
385 * @param v start vertex
386 * @param index current index
387 * @param stack stack for saving all SCCs
388 * @param stack_size current size of the stack
391 scc_tarjan_strongconnect (unsigned int *scc_counter,
392 struct GNUNET_REGEX_State *v, unsigned int *index,
393 struct GNUNET_REGEX_State **stack,
394 unsigned int *stack_size)
396 struct GNUNET_REGEX_State *w;
397 struct Transition *t;
402 stack[(*stack_size)++] = v;
405 for (t = v->transitions_head; NULL != t; t = t->next)
408 if (NULL != w && w->index < 0)
410 scc_tarjan_strongconnect (scc_counter, w, index, stack, stack_size);
411 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
413 else if (0 != w->contained)
414 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
417 if (v->lowlink == v->index)
419 w = stack[--(*stack_size)];
427 w->scc_id = *scc_counter;
428 w = stack[--(*stack_size)];
431 w->scc_id = *scc_counter;
438 * Detect all SCCs (Strongly Connected Components) inside the given automaton.
439 * SCCs will be marked using the scc_id on each state.
441 * @param a the automaton for which SCCs should be computed and assigned.
444 scc_tarjan (struct GNUNET_REGEX_Automaton *a)
447 unsigned int scc_counter;
448 struct GNUNET_REGEX_State *v;
449 struct GNUNET_REGEX_State *stack[a->state_count];
450 unsigned int stack_size;
452 for (v = a->states_head; NULL != v; v = v->next)
463 for (v = a->states_head; NULL != v; v = v->next)
466 scc_tarjan_strongconnect (&scc_counter, v, &index, stack, &stack_size);
471 * Adds a transition from one state to another on 'label'. Does not add
475 * @param from_state starting state for the transition
476 * @param label transition label
477 * @param to_state state to where the transition should point to
480 state_add_transition (struct GNUNET_REGEX_Context *ctx,
481 struct GNUNET_REGEX_State *from_state, const char label,
482 struct GNUNET_REGEX_State *to_state)
485 struct Transition *t;
486 struct Transition *oth;
488 if (NULL == from_state)
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
494 // Do not add duplicate state transitions
496 for (t = from_state->transitions_head; NULL != t; t = t->next)
498 if (t->to_state == to_state && t->label == label &&
499 t->from_state == from_state)
509 // sort transitions by label
510 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
512 if (oth->label > label)
516 t = GNUNET_malloc (sizeof (struct Transition));
517 t->id = ctx->transition_id++;
519 t->to_state = to_state;
520 t->from_state = from_state;
522 // Add outgoing transition to 'from_state'
523 from_state->transition_count++;
524 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
525 from_state->transitions_tail, oth, t);
529 * Compare two states. Used for sorting.
531 * @param a first state
532 * @param b second state
534 * @return an integer less than, equal to, or greater than zero
535 * if the first argument is considered to be respectively
536 * less than, equal to, or greater than the second.
539 state_compare (const void *a, const void *b)
541 struct GNUNET_REGEX_State **s1;
542 struct GNUNET_REGEX_State **s2;
544 s1 = (struct GNUNET_REGEX_State **) a;
545 s2 = (struct GNUNET_REGEX_State **) b;
547 return (*s1)->id - (*s2)->id;
551 * Get all edges leaving state 's'.
554 * @param edges all edges leaving 's'.
556 * @return number of edges.
559 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
561 struct Transition *t;
569 for (t = s->transitions_head; NULL != t; t = t->next)
571 if (NULL != t->to_state)
573 edges[count].label = &t->label;
574 edges[count].destination = t->to_state->hash;
582 * Compare to state sets by comparing the id's of the states that are contained
583 * in each set. Both sets are expected to be sorted by id!
585 * @param sset1 first state set
586 * @param sset2 second state set
588 * @return an integer less than, equal to, or greater than zero
589 * if the first argument is considered to be respectively
590 * less than, equal to, or greater than the second.
593 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
594 struct GNUNET_REGEX_StateSet *sset2)
599 if (NULL == sset1 || NULL == sset2)
602 result = sset1->len - sset2->len;
604 for (i = 0; i < sset1->len; i++)
609 result = state_compare (&sset1->states[i], &sset2->states[i]);
615 * Clears the given StateSet 'set'
617 * @param set set to be cleared
620 state_set_clear (struct GNUNET_REGEX_StateSet *set)
624 GNUNET_free_non_null (set->states);
630 * Clears an automaton fragment. Does not destroy the states inside the
633 * @param a automaton to be cleared
636 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
643 a->states_head = NULL;
644 a->states_tail = NULL;
650 * Frees the memory used by State 's'
652 * @param s state that should be destroyed
655 automaton_destroy_state (struct GNUNET_REGEX_State *s)
657 struct Transition *t;
658 struct Transition *next_t;
663 GNUNET_free_non_null (s->name);
664 GNUNET_free_non_null (s->proof);
666 for (t = s->transitions_head; NULL != t; t = next_t)
669 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
673 state_set_clear (s->nfa_set);
679 * Remove a state from the given automaton 'a'. Always use this function when
680 * altering the states of an automaton. Will also remove all transitions leading
681 * to this state, before destroying it.
684 * @param s state to remove
687 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
688 struct GNUNET_REGEX_State *s)
690 struct GNUNET_REGEX_State *ss;
691 struct GNUNET_REGEX_State *s_check;
692 struct Transition *t_check;
694 if (NULL == a || NULL == s)
699 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
702 // remove all transitions leading to this state
703 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
705 for (t_check = s_check->transitions_head; NULL != t_check;
706 t_check = t_check->next)
708 if (t_check->to_state == ss)
710 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
711 s_check->transitions_tail, t_check);
712 s_check->transition_count--;
717 automaton_destroy_state (ss);
721 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
726 * @param s1 first state
727 * @param s2 second state, will be destroyed
730 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
731 struct GNUNET_REGEX_Automaton *a,
732 struct GNUNET_REGEX_State *s1,
733 struct GNUNET_REGEX_State *s2)
735 struct GNUNET_REGEX_State *s_check;
736 struct Transition *t_check;
739 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
744 // 1. Make all transitions pointing to s2 point to s1
745 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
747 for (t_check = s_check->transitions_head; NULL != t_check;
748 t_check = t_check->next)
750 if (s2 == t_check->to_state)
751 t_check->to_state = s1;
755 // 2. Add all transitions from s2 to sX to s1
756 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
758 if (t_check->to_state != s1)
759 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
762 // 3. Rename s1 to {s1,s2}
764 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
765 GNUNET_free (new_name);
768 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
770 automaton_destroy_state (s2);
774 * Add a state to the automaton 'a', always use this function to alter the
775 * states DLL of the automaton.
777 * @param a automaton to add the state to
778 * @param s state that should be added
781 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
782 struct GNUNET_REGEX_State *s)
784 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
789 * Function that is called with each state, when traversing an automaton.
791 * @param cls closure.
792 * @param count current count of the state, from 0 to a->state_count -1.
795 typedef void (*GNUNET_REGEX_traverse_action) (void *cls, unsigned int count,
796 struct GNUNET_REGEX_State * s);
799 * Depth-first traversal of all states that are reachable from state 's'. Expects the states to
800 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
803 * @param s start state.
804 * @param count current count of the state.
805 * @param action action to be performed on each state.
806 * @param action_cls closure for action
809 automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
810 GNUNET_REGEX_traverse_action action, void *action_cls)
812 struct Transition *t;
814 if (GNUNET_NO != s->marked)
816 s->marked = GNUNET_YES;
818 action (action_cls, *count, s);
820 for (t = s->transitions_head; NULL != t; t = t->next)
821 automaton_state_traverse (t->to_state, count, action, action_cls);
826 * Traverses the given automaton from it's start state, visiting all reachable
827 * states and calling 'action' on each one of them.
829 * @param a automaton.
830 * @param action action to be performed on each state.
831 * @param action_cls closure for action
834 automaton_traverse (struct GNUNET_REGEX_Automaton *a,
835 GNUNET_REGEX_traverse_action action, void *action_cls)
838 struct GNUNET_REGEX_State *s;
840 for (s = a->states_head; NULL != s; s = s->next)
841 s->marked = GNUNET_NO;
843 automaton_state_traverse (a->start, &count, action, action_cls);
848 * Check if the given string 'str' needs parentheses around it when
849 * using it to generate a regex.
851 * Currently only tests for first and last characters being '()' respectively.
852 * FIXME: What about "(ab)|(cd)"?
856 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
859 needs_parentheses (const char *str)
867 if ((NULL == str) || ((slen = strlen (str)) < 2))
876 cl = strchr (pos, ')');
882 op = strchr (pos, '(');
883 if ((NULL != op) && (op < cl))
893 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
898 * Remove parentheses surrounding string 'str'.
899 * Example: "(a)" becomes "a".
900 * You need to GNUNET_free the returned string.
902 * Currently only tests for first and last characters being '()' respectively.
903 * FIXME: What about "(ab)|(cd)"?
905 * @param str string, free'd or re-used by this function, can be NULL
907 * @return string without surrounding parentheses, string 'str' if no preceding
908 * epsilon could be found, NULL if 'str' was NULL
911 remove_parentheses (char *str)
915 if ((NULL == str) || ('(' != str[0]) ||
916 (str[(slen = strlen (str)) - 1] != ')'))
918 memmove (str, &str[1], slen - 2);
919 str[slen - 2] = '\0';
925 * Check if the string 'str' starts with an epsilon (empty string).
926 * Example: "(|a)" is starting with an epsilon.
928 * @param str string to test
930 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
933 has_epsilon (const char *str)
935 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
936 (')' == str[strlen (str) - 1]);
941 * Remove an epsilon from the string str. Where epsilon is an empty string
942 * Example: str = "(|a|b|c)", result: "a|b|c"
943 * The returned string needs to be freed.
947 * @return string without preceding epsilon, string 'str' if no preceding epsilon
948 * could be found, NULL if 'str' was NULL
951 remove_epsilon (const char *str)
957 if (('(' == str[0]) && ('|' == str[1]))
960 if (')' == str[len - 1])
961 return GNUNET_strndup (&str[2], len - 3);
963 return GNUNET_strdup (str);
967 * Compare 'str1', starting from position 'k', with whole 'str2'
969 * @param str1 first string to compare, starting from position 'k'
970 * @param str2 second string for comparison
971 * @param k starting position in 'str1'
973 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
976 strkcmp (const char *str1, const char *str2, size_t k)
978 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
980 return strcmp (&str1[k], str2);
985 * Compare two strings for equality. If either is NULL (or if both are
986 * NULL), they are not equal.
988 * @param str1 first string for comparison.
989 * @param str2 second string for comparison.
991 * @return 0 if the strings are the same, 1 or -1 if not
994 nullstrcmp (const char *str1, const char *str2)
996 if ((NULL == str1) || (NULL == str2))
998 return strcmp (str1, str2);
1002 * Helper function used as 'action' in 'automaton_traverse' function to create
1003 * the depth-first numbering of the states.
1005 * @param cls states array.
1006 * @param count current state counter.
1007 * @param s current state.
1010 number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
1012 struct GNUNET_REGEX_State **states = cls;
1014 s->proof_id = count;
1020 * create proofs for all states in the given automaton. Implementation of the
1021 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1022 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1024 * @param a automaton.
1027 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1029 unsigned int n = a->state_count;
1030 struct GNUNET_REGEX_State *states[n];
1033 struct Transition *t;
1042 char *complete_regex;
1053 int clean_ik_kk_cmp;
1054 int clean_kk_kj_cmp;
1059 /* create depth-first numbering of the states, initializes 'state' */
1060 automaton_traverse (a, &number_states, states);
1062 /* Compute regular expressions of length "1" between each pair of states */
1063 for (i = 0; i < n; i++)
1065 for (j = 0; j < n; j++)
1068 R_last[i][j] = NULL;
1070 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1072 j = t->to_state->proof_id;
1073 if (NULL == R_last[i][j])
1074 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
1077 temp_a = R_last[i][j];
1078 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
1079 GNUNET_free (temp_a);
1082 if (NULL == R_last[i][i])
1083 GNUNET_asprintf (&R_last[i][i], "");
1086 temp_a = R_last[i][i];
1087 GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
1088 GNUNET_free (temp_a);
1091 for (i = 0; i < n; i++)
1092 for (j = 0; j < n; j++)
1093 if (needs_parentheses (R_last[i][j]))
1095 temp_a = R_last[i][j];
1096 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
1097 GNUNET_free (temp_a);
1100 // TODO: clean up and fix the induction part
1103 for (k = 0; k < n; k++)
1105 for (i = 0; i < n; i++)
1107 for (j = 0; j < n; j++)
1109 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, */
1110 /* ">>> R_last[i][j] = %s R_last[i][k] = %s " */
1111 /* "R_last[k][k] = %s R_last[k][j] = %s\n", R_last[i][j], */
1112 /* R_last[i][k], R_last[k][k], R_last[k][j]); */
1118 // cache results from strcmp, we might need these many times
1119 ij_kj_cmp = nullstrcmp (R_last[i][j], R_last[k][j]);
1120 ij_ik_cmp = nullstrcmp (R_last[i][j], R_last[i][k]);
1121 ik_kk_cmp = nullstrcmp (R_last[i][k], R_last[k][k]);
1122 ik_kj_cmp = nullstrcmp (R_last[i][k], R_last[k][j]);
1123 kk_kj_cmp = nullstrcmp (R_last[k][k], R_last[k][j]);
1125 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk})^* R^{(k-1)}_{kj}
1126 // With: R_cur[i][j] = R_cur_l | R_cur_r
1127 // Rij(k) = Rij(k-1), because right side (R_cur_r) is empty set (NULL)
1128 if ((NULL == R_last[i][k] || NULL == R_last[k][j] ||
1129 NULL == R_last[k][k]) && NULL != R_last[i][j])
1131 R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
1133 // Everything is NULL, so Rij(k) = NULL
1134 else if ((NULL == R_last[i][k] || NULL == R_last[k][j] ||
1135 NULL == R_last[k][k]) && NULL == R_last[i][j])
1139 // Right side (R_cur_r) not NULL
1142 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, */
1143 /* "R_temp_ij = %s R_temp_ik = %s R_temp_kk = %s R_temp_kj = %s\n", */
1144 /* R_temp_ij, R_temp_ik, R_temp_kk, R_temp_kj); */
1146 // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1147 // as parentheses, so we can better compare the contents
1148 R_temp_ik = remove_parentheses (remove_epsilon (R_last[i][k]));
1149 R_temp_kk = remove_parentheses (remove_epsilon (R_last[k][k]));
1150 R_temp_kj = remove_parentheses (remove_epsilon (R_last[k][j]));
1152 clean_ik_kk_cmp = nullstrcmp (R_last[i][k], R_temp_kk);
1153 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last[k][j]);
1155 // construct R_cur_l (and, if necessary R_cur_r)
1156 if (NULL != R_last[i][j])
1158 // Assign R_temp_ij to R_last[i][j] and remove epsilon as well
1159 // as parentheses, so we can better compare the contents
1160 R_temp_ij = remove_parentheses (remove_epsilon (R_last[i][j]));
1162 if (0 == strcmp (R_temp_ij, R_temp_ik) &&
1163 0 == strcmp (R_temp_ik, R_temp_kk) &&
1164 0 == strcmp (R_temp_kk, R_temp_kj))
1166 if (0 == strlen (R_temp_ij))
1168 R_cur_r = GNUNET_strdup ("");
1170 // a|(e|a)a*(e|a) = a*
1171 // a|(e|a)(e|a)*(e|a) = a*
1173 // (e|a)|aa*(e|a) = a*
1174 // (e|a)|(e|a)a*a = a*
1175 // (e|a)|(e|a)a*(e|a) = a*
1176 // (e|a)|(e|a)(e|a)*(e|a) = a*
1177 else if ((0 == strncmp (R_last[i][j], "(|", 2)) ||
1178 (0 == strncmp (R_last[i][k], "(|", 2) &&
1179 0 == strncmp (R_last[k][j], "(|", 2)))
1181 if (GNUNET_YES == needs_parentheses (R_temp_ij))
1182 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
1184 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
1189 // a|(e|a)(e|a)*a = a+
1190 // a|a(e|a)*(e|a) = a+
1193 if (GNUNET_YES == needs_parentheses (R_temp_ij))
1194 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
1196 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
1200 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp &&
1201 0 != clean_ik_kk_cmp)
1203 if (strlen (R_last[k][k]) < 1)
1204 R_cur_r = GNUNET_strdup (R_last[i][j]);
1205 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
1206 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][j], R_temp_kk);
1208 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][j], R_last[k][k]);
1213 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp &&
1214 0 != clean_kk_kj_cmp)
1216 if (strlen (R_last[k][k]) < 1)
1217 R_cur_r = GNUNET_strdup (R_last[k][j]);
1218 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
1219 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last[k][j]);
1221 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[k][j]);
1225 // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
1226 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp &&
1227 !has_epsilon (R_last[i][j]) && has_epsilon (R_last[k][k]))
1229 if (needs_parentheses (R_temp_kk))
1230 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][j], R_temp_kk);
1232 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][j], R_temp_kk);
1236 // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
1237 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp &&
1238 !has_epsilon (R_last[i][j]) && has_epsilon (R_last[k][k]))
1240 if (needs_parentheses (R_temp_kk))
1241 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last[i][j]);
1243 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[i][j]);
1249 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "NO SIMPLIFICATION\n"); */
1251 (NULL == R_last[i][j]) ? NULL : GNUNET_strdup (R_last[i][j]);
1252 temp_a = remove_parentheses (temp_a);
1256 GNUNET_free_non_null (R_temp_ij);
1258 // we have no left side
1264 // construct R_cur_r, if not already constructed
1265 if (NULL == R_cur_r)
1267 length = strlen (R_temp_kk) - strlen (R_last[i][k]);
1269 // a(ba)*bx = (ab)+x
1270 if (length > 0 && NULL != R_last[k][k] && 0 < strlen (R_last[k][k])
1271 && NULL != R_last[k][j] && 0 < strlen (R_last[k][j]) &&
1272 NULL != R_last[i][k] && 0 < strlen (R_last[i][k]) &&
1273 0 == strkcmp (R_temp_kk, R_last[i][k], length) &&
1274 0 == strncmp (R_temp_kk, R_last[k][j], length))
1276 temp_a = GNUNET_malloc (length + 1);
1277 temp_b = GNUNET_malloc ((strlen (R_last[k][j]) - length) + 1);
1282 for (cnt = 0; cnt < strlen (R_last[k][j]); cnt++)
1286 temp_a[length_l] = R_last[k][j][cnt];
1291 temp_b[length_r] = R_last[k][j][cnt];
1295 temp_a[length_l] = '\0';
1296 temp_b[length_r] = '\0';
1299 if (NULL != R_cur_l && 0 == strlen (R_cur_l) &&
1300 0 == strlen (temp_b))
1302 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last[i][k], temp_a);
1303 GNUNET_free (R_cur_l);
1308 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last[i][k], temp_a,
1311 GNUNET_free (temp_a);
1312 GNUNET_free (temp_b);
1314 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
1315 0 == strcmp (R_temp_kk, R_temp_kj))
1317 // (e|a)a*(e|a) = a*
1318 // (e|a)(e|a)*(e|a) = a*
1319 if (has_epsilon (R_last[i][k]) && has_epsilon (R_last[k][j]))
1321 if (needs_parentheses (R_temp_kk))
1322 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
1324 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
1327 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
1328 !has_epsilon (R_last[i][k]))
1330 if (needs_parentheses (R_temp_kk))
1331 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1333 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1337 // a(e|a)*(e|a) = a+
1342 (has_epsilon (R_last[i][k]) + has_epsilon (R_last[k][k]) +
1343 has_epsilon (R_last[k][j]));
1347 if (needs_parentheses (R_temp_kk))
1348 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
1350 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
1355 // (e|a)(e|a)*b = a*b
1356 else if (0 == strcmp (R_temp_ik, R_temp_kk))
1358 if (has_epsilon (R_last[i][k]))
1360 if (needs_parentheses (R_temp_kk))
1361 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk,
1364 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[k][j]);
1368 if (needs_parentheses (R_temp_kk))
1369 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk,
1372 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last[k][j]);
1376 // b(e|a)*(e|a) = ba*
1377 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1379 if (has_epsilon (R_last[k][j]))
1381 if (needs_parentheses (R_temp_kk))
1382 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][k],
1385 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][k], R_temp_kk);
1389 if (needs_parentheses (R_temp_kk))
1390 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last[i][k],
1393 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last[i][k], R_temp_kk);
1398 if (strlen (R_temp_kk) > 0)
1400 if (needs_parentheses (R_temp_kk))
1402 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last[i][k],
1403 R_temp_kk, R_last[k][j]);
1407 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last[i][k], R_temp_kk,
1413 GNUNET_asprintf (&R_cur_r, "%s%s", R_last[i][k], R_last[k][j]);
1418 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "R_cur_l: %s\n", R_cur_l); */
1419 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "R_cur_r: %s\n", R_cur_r); */
1421 // putting it all together
1422 if (NULL != R_cur_l && NULL != R_cur_r)
1425 if (0 == strcmp (R_cur_l, R_cur_r))
1427 R_cur[i][j] = GNUNET_strdup (R_cur_l);
1429 // R_cur_l | R_cur_r
1432 GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
1435 else if (NULL != R_cur_l)
1437 R_cur[i][j] = GNUNET_strdup (R_cur_l);
1439 else if (NULL != R_cur_r)
1441 R_cur[i][j] = GNUNET_strdup (R_cur_r);
1448 GNUNET_free_non_null (R_cur_l);
1449 GNUNET_free_non_null (R_cur_r);
1451 GNUNET_free_non_null (R_temp_ik);
1452 GNUNET_free_non_null (R_temp_kk);
1453 GNUNET_free_non_null (R_temp_kj);
1458 // set R_last = R_cur
1459 for (i = 0; i < n; i++)
1461 for (j = 0; j < n; j++)
1463 GNUNET_free_non_null (R_last[i][j]);
1464 R_last[i][j] = R_cur[i][j];
1470 // assign proofs and hashes
1471 for (i = 0; i < n; i++)
1473 if (NULL != R_last[a->start->proof_id][i])
1475 states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
1476 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1481 // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
1482 complete_regex = NULL;
1483 for (i = 0; i < n; i++)
1485 if (states[i]->accepting)
1487 if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
1488 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
1489 else if (NULL != R_last[a->start->proof_id][i] &&
1490 0 < strlen (R_last[a->start->proof_id][i]))
1492 temp_a = complete_regex;
1493 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1494 R_last[a->start->proof_id][i]);
1495 GNUNET_free (temp_a);
1499 a->canonical_regex = complete_regex;
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 "---------------------------------------------\n");
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex: %s\n", a->regex);
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete Regex: %s\n", complete_regex);
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "---------------------------------------------\n");
1509 for (i = 0; i < n; i++)
1511 for (j = 0; j < n; j++)
1512 GNUNET_free_non_null (R_last[i][j]);
1517 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1518 * automaton_destroy_state.
1520 * @param ctx context
1521 * @param nfa_states set of NFA states on which the DFA should be based on
1523 * @return new DFA state
1525 static struct GNUNET_REGEX_State *
1526 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1527 struct GNUNET_REGEX_StateSet *nfa_states)
1529 struct GNUNET_REGEX_State *s;
1532 struct GNUNET_REGEX_State *cstate;
1533 struct Transition *ctran;
1535 struct Transition *t;
1538 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1539 s->id = ctx->state_id++;
1549 if (NULL == nfa_states)
1551 GNUNET_asprintf (&s->name, "s%i", s->id);
1555 s->nfa_set = nfa_states;
1557 if (nfa_states->len < 1)
1560 // Create a name based on 'sset'
1561 s->name = GNUNET_malloc (sizeof (char) * 2);
1562 strcat (s->name, "{");
1565 for (i = 0; i < nfa_states->len; i++)
1567 cstate = nfa_states->states[i];
1568 GNUNET_asprintf (&name, "%i,", cstate->id);
1572 len = strlen (s->name) + strlen (name) + 1;
1573 s->name = GNUNET_realloc (s->name, len);
1574 strcat (s->name, name);
1579 // Add a transition for each distinct label to NULL state
1580 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1582 if (0 != ctran->label)
1586 for (t = s->transitions_head; NULL != t; t = t->next)
1588 if (t->label == ctran->label)
1596 state_add_transition (ctx, s, ctran->label, NULL);
1600 // If the nfa_states contain an accepting state, the new dfa state is also
1602 if (cstate->accepting)
1606 s->name[strlen (s->name) - 1] = '}';
1612 * Move from the given state 's' to the next state on transition 'label'
1614 * @param s starting state
1615 * @param label edge label to follow
1617 * @return new state or NULL, if transition on label not possible
1619 static struct GNUNET_REGEX_State *
1620 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1622 struct Transition *t;
1623 struct GNUNET_REGEX_State *new_s;
1630 for (t = s->transitions_head; NULL != t; t = t->next)
1632 if (label == t->label)
1634 new_s = t->to_state;
1643 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1644 * states that are not reachable from the starting state.
1646 * @param a DFA automaton
1649 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1651 struct GNUNET_REGEX_State *s;
1652 struct GNUNET_REGEX_State *s_next;
1654 // 1. unmark all states
1655 for (s = a->states_head; NULL != s; s = s->next)
1656 s->marked = GNUNET_NO;
1658 // 2. traverse dfa from start state and mark all visited states
1659 automaton_traverse (a, NULL, NULL);
1661 // 3. delete all states that were not visited
1662 for (s = a->states_head; NULL != s; s = s_next)
1665 if (GNUNET_NO == s->marked)
1666 automaton_remove_state (a, s);
1671 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1672 * not transition to any other state but themselfes.
1674 * @param a DFA automaton
1677 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1679 struct GNUNET_REGEX_State *s;
1680 struct Transition *t;
1683 GNUNET_assert (DFA == a->type);
1685 for (s = a->states_head; NULL != s; s = s->next)
1691 for (t = s->transitions_head; NULL != t; t = t->next)
1693 if (NULL != t->to_state && t->to_state != s)
1703 // state s is dead, remove it
1704 automaton_remove_state (a, s);
1709 * Merge all non distinguishable states in the DFA 'a'
1711 * @param ctx context
1712 * @param a DFA automaton
1715 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1716 struct GNUNET_REGEX_Automaton *a)
1719 int table[a->state_count][a->state_count];
1720 struct GNUNET_REGEX_State *s1;
1721 struct GNUNET_REGEX_State *s2;
1722 struct Transition *t1;
1723 struct Transition *t2;
1724 struct GNUNET_REGEX_State *s1_next;
1725 struct GNUNET_REGEX_State *s2_next;
1727 int num_equal_edges;
1729 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1735 // Mark all pairs of accepting/!accepting states
1736 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1738 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1740 table[s1->marked][s2->marked] = 0;
1742 if ((s1->accepting && !s2->accepting) ||
1743 (!s1->accepting && s2->accepting))
1745 table[s1->marked][s2->marked] = 1;
1750 // Find all equal states
1755 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1757 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1759 if (0 != table[s1->marked][s2->marked])
1762 num_equal_edges = 0;
1763 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1765 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1767 if (t1->label == t2->label)
1770 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1771 0 != table[t2->to_state->marked][t1->to_state->marked])
1773 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1779 if (num_equal_edges != s1->transition_count ||
1780 num_equal_edges != s2->transition_count)
1782 // Make sure ALL edges of possible equal states are the same
1783 table[s1->marked][s2->marked] = -2;
1789 // Merge states that are equal
1790 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1793 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1796 if (table[s1->marked][s2->marked] == 0)
1797 automaton_merge_states (ctx, a, s1, s2);
1803 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1804 * dead states and merging all non distinguishable states
1806 * @param ctx context
1807 * @param a DFA automaton
1810 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1811 struct GNUNET_REGEX_Automaton *a)
1816 GNUNET_assert (DFA == a->type);
1818 // 1. remove unreachable states
1819 dfa_remove_unreachable_states (a);
1821 // 2. remove dead states
1822 dfa_remove_dead_states (a);
1824 // 3. Merge nondistinguishable states
1825 dfa_merge_nondistinguishable_states (ctx, a);
1829 * Creates a new NFA fragment. Needs to be cleared using
1830 * automaton_fragment_clear.
1832 * @param start starting state
1833 * @param end end state
1835 * @return new NFA fragment
1837 static struct GNUNET_REGEX_Automaton *
1838 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1839 struct GNUNET_REGEX_State *end)
1841 struct GNUNET_REGEX_Automaton *n;
1843 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1849 if (NULL == start && NULL == end)
1852 automaton_add_state (n, end);
1853 automaton_add_state (n, start);
1862 * Adds a list of states to the given automaton 'n'.
1864 * @param n automaton to which the states should be added
1865 * @param states_head head of the DLL of states
1866 * @param states_tail tail of the DLL of states
1869 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1870 struct GNUNET_REGEX_State *states_head,
1871 struct GNUNET_REGEX_State *states_tail)
1873 struct GNUNET_REGEX_State *s;
1875 if (NULL == n || NULL == states_head)
1877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1881 if (NULL == n->states_head)
1883 n->states_head = states_head;
1884 n->states_tail = states_tail;
1888 if (NULL != states_head)
1890 n->states_tail->next = states_head;
1891 n->states_tail = states_tail;
1894 for (s = states_head; NULL != s; s = s->next)
1899 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1901 * @param ctx context
1902 * @param accepting is it an accepting state or not
1904 * @return new NFA state
1906 static struct GNUNET_REGEX_State *
1907 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1909 struct GNUNET_REGEX_State *s;
1911 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1912 s->id = ctx->state_id++;
1913 s->accepting = accepting;
1920 GNUNET_asprintf (&s->name, "s%i", s->id);
1926 * Calculates the NFA closure set for the given state.
1928 * @param nfa the NFA containing 's'
1929 * @param s starting point state
1930 * @param label transitioning label on which to base the closure on,
1931 * pass 0 for epsilon transition
1933 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1935 static struct GNUNET_REGEX_StateSet *
1936 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1937 struct GNUNET_REGEX_State *s, const char label)
1939 struct GNUNET_REGEX_StateSet *cls;
1940 struct GNUNET_REGEX_StateSet *cls_check;
1941 struct GNUNET_REGEX_State *clsstate;
1942 struct GNUNET_REGEX_State *currentstate;
1943 struct Transition *ctran;
1948 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1949 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1951 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1952 clsstate->contained = 0;
1954 // Add start state to closure only for epsilon closure
1956 GNUNET_array_append (cls->states, cls->len, s);
1958 GNUNET_array_append (cls_check->states, cls_check->len, s);
1959 while (cls_check->len > 0)
1961 currentstate = cls_check->states[cls_check->len - 1];
1962 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1964 for (ctran = currentstate->transitions_head; NULL != ctran;
1965 ctran = ctran->next)
1967 if (NULL != ctran->to_state && label == ctran->label)
1969 clsstate = ctran->to_state;
1971 if (NULL != clsstate && 0 == clsstate->contained)
1973 GNUNET_array_append (cls->states, cls->len, clsstate);
1974 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1975 clsstate->contained = 1;
1980 GNUNET_assert (0 == cls_check->len);
1981 GNUNET_free (cls_check);
1985 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1992 * Calculates the closure set for the given set of states.
1994 * @param nfa the NFA containing 's'
1995 * @param states list of states on which to base the closure on
1996 * @param label transitioning label for which to base the closure on,
1997 * pass 0 for epsilon transition
1999 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
2001 static struct GNUNET_REGEX_StateSet *
2002 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
2003 struct GNUNET_REGEX_StateSet *states, const char label)
2005 struct GNUNET_REGEX_State *s;
2006 struct GNUNET_REGEX_StateSet *sset;
2007 struct GNUNET_REGEX_StateSet *cls;
2016 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2018 for (i = 0; i < states->len; i++)
2020 s = states->states[i];
2021 sset = nfa_closure_create (nfa, s, label);
2023 for (j = 0; j < sset->len; j++)
2026 for (k = 0; k < cls->len; k++)
2028 if (sset->states[j]->id == cls->states[k]->id)
2035 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2037 state_set_clear (sset);
2041 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2048 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2050 * @param ctx context
2053 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2055 struct GNUNET_REGEX_Automaton *a;
2056 struct GNUNET_REGEX_Automaton *b;
2057 struct GNUNET_REGEX_Automaton *new;
2059 b = ctx->stack_tail;
2060 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2061 a = ctx->stack_tail;
2062 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2064 state_add_transition (ctx, a->end, 0, b->start);
2065 a->end->accepting = 0;
2066 b->end->accepting = 1;
2068 new = nfa_fragment_create (NULL, NULL);
2069 nfa_add_states (new, a->states_head, a->states_tail);
2070 nfa_add_states (new, b->states_head, b->states_tail);
2071 new->start = a->start;
2073 automaton_fragment_clear (a);
2074 automaton_fragment_clear (b);
2076 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2080 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2082 * @param ctx context
2085 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2087 struct GNUNET_REGEX_Automaton *a;
2088 struct GNUNET_REGEX_Automaton *new;
2089 struct GNUNET_REGEX_State *start;
2090 struct GNUNET_REGEX_State *end;
2092 a = ctx->stack_tail;
2093 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2097 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2098 "nfa_add_star_op failed, because there was no element on the stack");
2102 start = nfa_state_create (ctx, 0);
2103 end = nfa_state_create (ctx, 1);
2105 state_add_transition (ctx, start, 0, a->start);
2106 state_add_transition (ctx, start, 0, end);
2107 state_add_transition (ctx, a->end, 0, a->start);
2108 state_add_transition (ctx, a->end, 0, end);
2110 a->end->accepting = 0;
2113 new = nfa_fragment_create (start, end);
2114 nfa_add_states (new, a->states_head, a->states_tail);
2115 automaton_fragment_clear (a);
2117 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2121 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2123 * @param ctx context
2126 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2128 struct GNUNET_REGEX_Automaton *a;
2130 a = ctx->stack_tail;
2131 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2133 state_add_transition (ctx, a->end, 0, a->start);
2135 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2139 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2141 * @param ctx context
2144 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2146 struct GNUNET_REGEX_Automaton *a;
2147 struct GNUNET_REGEX_Automaton *new;
2148 struct GNUNET_REGEX_State *start;
2149 struct GNUNET_REGEX_State *end;
2151 a = ctx->stack_tail;
2152 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2157 "nfa_add_question_op failed, because there was no element on the stack");
2161 start = nfa_state_create (ctx, 0);
2162 end = nfa_state_create (ctx, 1);
2164 state_add_transition (ctx, start, 0, a->start);
2165 state_add_transition (ctx, start, 0, end);
2166 state_add_transition (ctx, a->end, 0, end);
2168 a->end->accepting = 0;
2170 new = nfa_fragment_create (start, end);
2171 nfa_add_states (new, a->states_head, a->states_tail);
2172 automaton_fragment_clear (a);
2174 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2178 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2179 * alternates between a and b (a|b)
2181 * @param ctx context
2184 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2186 struct GNUNET_REGEX_Automaton *a;
2187 struct GNUNET_REGEX_Automaton *b;
2188 struct GNUNET_REGEX_Automaton *new;
2189 struct GNUNET_REGEX_State *start;
2190 struct GNUNET_REGEX_State *end;
2192 b = ctx->stack_tail;
2193 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2194 a = ctx->stack_tail;
2195 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2197 start = nfa_state_create (ctx, 0);
2198 end = nfa_state_create (ctx, 1);
2199 state_add_transition (ctx, start, 0, a->start);
2200 state_add_transition (ctx, start, 0, b->start);
2202 state_add_transition (ctx, a->end, 0, end);
2203 state_add_transition (ctx, b->end, 0, end);
2205 a->end->accepting = 0;
2206 b->end->accepting = 0;
2209 new = nfa_fragment_create (start, end);
2210 nfa_add_states (new, a->states_head, a->states_tail);
2211 nfa_add_states (new, b->states_head, b->states_tail);
2212 automaton_fragment_clear (a);
2213 automaton_fragment_clear (b);
2215 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2219 * Adds a new nfa fragment to the stack
2221 * @param ctx context
2222 * @param lit label for nfa transition
2225 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
2227 struct GNUNET_REGEX_Automaton *n;
2228 struct GNUNET_REGEX_State *start;
2229 struct GNUNET_REGEX_State *end;
2231 GNUNET_assert (NULL != ctx);
2233 start = nfa_state_create (ctx, 0);
2234 end = nfa_state_create (ctx, 1);
2235 state_add_transition (ctx, start, lit, end);
2236 n = nfa_fragment_create (start, end);
2237 GNUNET_assert (NULL != n);
2238 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2242 * Initialize a new context
2244 * @param ctx context
2247 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2255 ctx->transition_id = 0;
2256 ctx->stack_head = NULL;
2257 ctx->stack_tail = NULL;
2261 * Construct an NFA by parsing the regex string of length 'len'.
2263 * @param regex regular expression string
2264 * @param len length of the string
2266 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2268 struct GNUNET_REGEX_Automaton *
2269 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2271 struct GNUNET_REGEX_Context ctx;
2272 struct GNUNET_REGEX_Automaton *nfa;
2276 unsigned int altcount;
2277 unsigned int atomcount;
2278 unsigned int pcount;
2285 GNUNET_REGEX_context_init (&ctx);
2294 for (count = 0; count < len && *regexp; count++, regexp++)
2302 nfa_add_concatenation (&ctx);
2304 GNUNET_array_grow (p, pcount, pcount + 1);
2305 p[pcount - 1].altcount = altcount;
2306 p[pcount - 1].atomcount = atomcount;
2313 error_msg = "Cannot append '|' to nothing";
2316 while (--atomcount > 0)
2317 nfa_add_concatenation (&ctx);
2323 error_msg = "Missing opening '('";
2328 // Ignore this: "()"
2330 altcount = p[pcount].altcount;
2331 atomcount = p[pcount].atomcount;
2334 while (--atomcount > 0)
2335 nfa_add_concatenation (&ctx);
2336 for (; altcount > 0; altcount--)
2337 nfa_add_alternation (&ctx);
2339 altcount = p[pcount].altcount;
2340 atomcount = p[pcount].atomcount;
2346 error_msg = "Cannot append '*' to nothing";
2349 nfa_add_star_op (&ctx);
2354 error_msg = "Cannot append '+' to nothing";
2357 nfa_add_plus_op (&ctx);
2362 error_msg = "Cannot append '?' to nothing";
2365 nfa_add_question_op (&ctx);
2367 case 92: /* escape: \ */
2374 nfa_add_concatenation (&ctx);
2376 nfa_add_label (&ctx, *regexp);
2383 error_msg = "Unbalanced parenthesis";
2386 while (--atomcount > 0)
2387 nfa_add_concatenation (&ctx);
2388 for (; altcount > 0; altcount--)
2389 nfa_add_alternation (&ctx);
2391 GNUNET_free_non_null (p);
2393 nfa = ctx.stack_tail;
2394 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2396 if (NULL != ctx.stack_head)
2398 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2402 nfa->regex = GNUNET_strdup (regex);
2407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2408 if (NULL != error_msg)
2409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2411 GNUNET_free_non_null (p);
2413 while (NULL != (nfa = ctx.stack_head))
2415 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2416 GNUNET_REGEX_automaton_destroy (nfa);
2423 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2425 * @param ctx context.
2426 * @param nfa NFA automaton.
2427 * @param dfa DFA automaton.
2428 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2432 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2433 struct GNUNET_REGEX_Automaton *nfa,
2434 struct GNUNET_REGEX_Automaton *dfa,
2435 struct GNUNET_REGEX_State *dfa_state)
2437 struct Transition *ctran;
2438 struct GNUNET_REGEX_State *state_iter;
2439 struct GNUNET_REGEX_State *new_dfa_state;
2440 struct GNUNET_REGEX_State *state_contains;
2441 struct GNUNET_REGEX_StateSet *tmp;
2442 struct GNUNET_REGEX_StateSet *nfa_set;
2444 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2446 if (0 == ctran->label || NULL != ctran->to_state)
2449 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2450 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2451 state_set_clear (tmp);
2452 new_dfa_state = dfa_state_create (ctx, nfa_set);
2453 state_contains = NULL;
2454 for (state_iter = dfa->states_head; NULL != state_iter;
2455 state_iter = state_iter->next)
2457 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2458 state_contains = state_iter;
2461 if (NULL == state_contains)
2463 automaton_add_state (dfa, new_dfa_state);
2464 ctran->to_state = new_dfa_state;
2465 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2469 ctran->to_state = state_contains;
2470 automaton_destroy_state (new_dfa_state);
2476 * Construct DFA for the given 'regex' of length 'len'
2478 * @param regex regular expression string
2479 * @param len length of the regular expression
2481 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2483 struct GNUNET_REGEX_Automaton *
2484 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2486 struct GNUNET_REGEX_Context ctx;
2487 struct GNUNET_REGEX_Automaton *dfa;
2488 struct GNUNET_REGEX_Automaton *nfa;
2489 struct GNUNET_REGEX_StateSet *nfa_set;
2491 GNUNET_REGEX_context_init (&ctx);
2494 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2499 "Could not create DFA, because NFA creation failed\n");
2503 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2505 dfa->regex = GNUNET_strdup (regex);
2507 // Create DFA start state from epsilon closure
2508 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2509 dfa->start = dfa_state_create (&ctx, nfa_set);
2510 automaton_add_state (dfa, dfa->start);
2512 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2514 GNUNET_REGEX_automaton_destroy (nfa);
2517 dfa_minimize (&ctx, dfa);
2519 // Create proofs for all states
2520 automaton_create_proofs (dfa);
2526 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2529 * @param a automaton to be destroyed
2532 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2534 struct GNUNET_REGEX_State *s;
2535 struct GNUNET_REGEX_State *next_state;
2540 GNUNET_free_non_null (a->regex);
2541 GNUNET_free_non_null (a->canonical_regex);
2543 for (s = a->states_head; NULL != s;)
2545 next_state = s->next;
2546 automaton_destroy_state (s);
2554 * Save a state to an open file pointer. cls is expected to be a file pointer to
2555 * an open file. Used only in conjunction with
2556 * GNUNET_REGEX_automaton_save_graph.
2558 * @param cls file pointer.
2559 * @param count current count of the state, not used.
2563 GNUNET_REGEX_automaton_save_graph_step (void *cls, unsigned int count,
2564 struct GNUNET_REGEX_State *s)
2567 struct Transition *ctran;
2569 char *s_tran = NULL;
2575 GNUNET_asprintf (&s_acc,
2576 "\"%s(%i)\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2577 s->name, s->proof_id, s->scc_id);
2581 GNUNET_asprintf (&s_acc, "\"%s(%i)\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2582 s->proof_id, s->scc_id);
2587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
2590 fwrite (s_acc, strlen (s_acc), 1, p);
2591 GNUNET_free (s_acc);
2594 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2596 if (NULL == ctran->to_state)
2598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2599 "Transition from State %i has no state for transitioning\n",
2604 if (ctran->label == 0)
2606 GNUNET_asprintf (&s_tran,
2607 "\"%s(%i)\" -> \"%s(%i)\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2608 s->name, s->proof_id, ctran->to_state->name,
2609 ctran->to_state->proof_id, s->scc_id);
2613 GNUNET_asprintf (&s_tran,
2614 "\"%s(%i)\" -> \"%s(%i)\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2615 s->name, s->proof_id, ctran->to_state->name,
2616 ctran->to_state->proof_id, ctran->label, s->scc_id);
2621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2626 fwrite (s_tran, strlen (s_tran), 1, p);
2627 GNUNET_free (s_tran);
2633 * Save the given automaton as a GraphViz dot file
2635 * @param a the automaton to be saved
2636 * @param filename where to save the file
2639 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2640 const char *filename)
2648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2652 if (NULL == filename || strlen (filename) < 1)
2654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2658 p = fopen (filename, "w");
2662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2667 /* First add the SCCs to the automaton, so we can color them nicely */
2670 start = "digraph G {\nrankdir=LR\n";
2671 fwrite (start, strlen (start), 1, p);
2673 automaton_traverse (a, &GNUNET_REGEX_automaton_save_graph_step, p);
2676 fwrite (end, strlen (end), 1, p);
2681 * Evaluates the given string using the given DFA automaton
2683 * @param a automaton, type must be DFA
2684 * @param string string that should be evaluated
2686 * @return 0 if string matches, non 0 otherwise
2689 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2692 struct GNUNET_REGEX_State *s;
2696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2697 "Tried to evaluate DFA, but NFA automaton given");
2703 // If the string is empty but the starting state is accepting, we accept.
2704 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2707 for (strp = string; NULL != strp && *strp; strp++)
2709 s = dfa_move (s, *strp);
2714 if (NULL != s && s->accepting)
2721 * Evaluates the given string using the given NFA automaton
2723 * @param a automaton, type must be NFA
2724 * @param string string that should be evaluated
2726 * @return 0 if string matches, non 0 otherwise
2729 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2732 struct GNUNET_REGEX_State *s;
2733 struct GNUNET_REGEX_StateSet *sset;
2734 struct GNUNET_REGEX_StateSet *new_sset;
2740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2741 "Tried to evaluate NFA, but DFA automaton given");
2745 // If the string is empty but the starting state is accepting, we accept.
2746 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2751 sset = nfa_closure_create (a, a->start, 0);
2753 for (strp = string; NULL != strp && *strp; strp++)
2755 new_sset = nfa_closure_set_create (a, sset, *strp);
2756 state_set_clear (sset);
2757 sset = nfa_closure_set_create (a, new_sset, 0);
2758 state_set_clear (new_sset);
2761 for (i = 0; i < sset->len; i++)
2763 s = sset->states[i];
2764 if (NULL != s && s->accepting)
2771 state_set_clear (sset);
2776 * Evaluates the given 'string' against the given compiled regex
2778 * @param a automaton
2779 * @param string string to check
2781 * @return 0 if string matches, non 0 otherwise
2784 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2791 result = evaluate_dfa (a, string);
2794 result = evaluate_nfa (a, string);
2797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2798 "Evaluating regex failed, automaton has no type!\n");
2799 result = GNUNET_SYSERR;
2808 * Get the canonical regex of the given automaton.
2809 * When constructing the automaton a proof is computed for each state,
2810 * consisting of the regular expression leading to this state. A complete
2811 * regex for the automaton can be computed by combining these proofs.
2812 * As of now this function is only useful for testing.
2814 * @param a automaton for which the canonical regex should be returned.
2819 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2824 return a->canonical_regex;
2828 * Get the first key for the given 'input_string'. This hashes the first x bits
2829 * of the 'input_strings'.
2831 * @param input_string string.
2832 * @param string_len length of the 'input_string'.
2833 * @param key pointer to where to write the hash code.
2835 * @return number of bits of 'input_string' that have been consumed
2836 * to construct the key
2839 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2840 struct GNUNET_HashCode *key)
2844 size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
2846 if (NULL == input_string)
2848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2852 GNUNET_CRYPTO_hash (input_string, size, key);
2858 * Check if the given 'proof' matches the given 'key'.
2860 * @param proof partial regex
2863 * @return GNUNET_OK if the proof is valid for the given key
2866 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2872 * Iterate over all edges helper function starting from state 's', calling
2873 * iterator on for each edge.
2876 * @param iterator iterator function called for each edge.
2877 * @param iterator_cls closure.
2880 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2883 struct Transition *t;
2884 struct GNUNET_REGEX_Edge edges[s->transition_count];
2885 unsigned int num_edges;
2887 if (GNUNET_YES != s->marked)
2889 s->marked = GNUNET_YES;
2891 num_edges = state_get_edges (s, edges);
2893 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2895 for (t = s->transitions_head; NULL != t; t = t->next)
2896 iterate_edge (t->to_state, iterator, iterator_cls);
2901 * Iterate over all edges starting from start state of automaton 'a'. Calling
2902 * iterator for each edge.
2904 * @param a automaton.
2905 * @param iterator iterator called for each edge.
2906 * @param iterator_cls closure.
2909 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2910 GNUNET_REGEX_KeyIterator iterator,
2913 struct GNUNET_REGEX_State *s;
2915 for (s = a->states_head; NULL != s; s = s->next)
2916 s->marked = GNUNET_NO;
2918 iterate_edge (a->start, iterator, iterator_cls);