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;
1054 int clean_ik_kk_cmp;
1055 int clean_kk_kj_cmp;
1060 /* create depth-first numbering of the states, initializes 'state' */
1061 automaton_traverse (a, &number_states, states);
1063 /* Compute regular expressions of length "1" between each pair of states */
1064 for (i = 0; i < n; i++)
1066 for (j = 0; j < n; j++)
1069 R_last[i][j] = NULL;
1071 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1073 j = t->to_state->proof_id;
1074 if (NULL == R_last[i][j])
1075 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
1078 temp_a = R_last[i][j];
1079 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
1080 GNUNET_free (temp_a);
1083 if (NULL == R_last[i][i])
1084 GNUNET_asprintf (&R_last[i][i], "");
1087 temp_a = R_last[i][i];
1088 GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
1089 GNUNET_free (temp_a);
1092 for (i = 0; i < n; i++)
1093 for (j = 0; j < n; j++)
1094 if (needs_parentheses (R_last[i][j]))
1096 temp_a = R_last[i][j];
1097 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
1098 GNUNET_free (temp_a);
1101 // TODO: clean up and fix the induction part
1103 /* Compute regular expressions of length "k" between each pair of states per induction */
1104 for (k = 0; k < n; k++)
1106 for (i = 0; i < n; i++)
1108 for (j = 0; j < n; j++)
1110 // Basis for the recursion:
1111 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1112 // R_last == R^{(k-1)}, R_cur == R^{(k)}
1113 // With: R_cur[i][j] = R_cur_l | R_cur_r
1114 // R_cur_l == R^{(k-1)}_{ij}
1115 // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1117 if ((NULL == R_last[i][j]) && ((NULL == R_last[i][k]) || (NULL == R_last[k][k]) || /* technically cannot happen, but looks saner */
1118 (NULL == R_last[k][j])))
1120 /* R^{(k)}_{ij} = N | N */
1121 /* R_cur[i][j] is already NULL */
1124 if ((NULL == R_last[i][k]) || (NULL == R_last[k][k]) || /* technically cannot happen, but looks saner */
1125 (NULL == R_last[k][j]))
1127 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1128 R_cur[i][j] = GNUNET_strdup (R_last[i][j]);
1131 // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
1132 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1138 // cache results from strcmp, we might need these many times
1139 ij_kj_cmp = nullstrcmp (R_last[i][j], R_last[k][j]);
1140 ij_ik_cmp = nullstrcmp (R_last[i][j], R_last[i][k]);
1141 ik_kk_cmp = nullstrcmp (R_last[i][k], R_last[k][k]);
1142 //ik_kj_cmp = nullstrcmp (R_last[i][k], R_last[k][j]);
1143 kk_kj_cmp = nullstrcmp (R_last[k][k], R_last[k][j]);
1145 // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1146 // as parentheses, so we can better compare the contents
1147 R_temp_ik = remove_parentheses (remove_epsilon (R_last[i][k]));
1148 R_temp_kk = remove_parentheses (remove_epsilon (R_last[k][k]));
1149 R_temp_kj = remove_parentheses (remove_epsilon (R_last[k][j]));
1151 clean_ik_kk_cmp = nullstrcmp (R_last[i][k], R_temp_kk);
1152 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last[k][j]);
1154 // construct R_cur_l (and, if necessary R_cur_r)
1155 if (NULL != R_last[i][j])
1157 // Assign R_temp_ij to R_last[i][j] and remove epsilon as well
1158 // as parentheses, so we can better compare the contents
1159 R_temp_ij = remove_parentheses (remove_epsilon (R_last[i][j]));
1161 if (0 == strcmp (R_temp_ij, R_temp_ik) &&
1162 0 == strcmp (R_temp_ik, R_temp_kk) &&
1163 0 == strcmp (R_temp_kk, R_temp_kj))
1165 if (0 == strlen (R_temp_ij))
1167 R_cur_r = GNUNET_strdup ("");
1169 else if ((0 == strncmp (R_last[i][j], "(|", 2)) ||
1170 (0 == strncmp (R_last[i][k], "(|", 2) &&
1171 0 == strncmp (R_last[k][j], "(|", 2)))
1173 // a|(e|a)a*(e|a) = a*
1174 // a|(e|a)(e|a)*(e|a) = a*
1176 // (e|a)|aa*(e|a) = a*
1177 // (e|a)|(e|a)a*a = a*
1178 // (e|a)|(e|a)a*(e|a) = a*
1179 // (e|a)|(e|a)(e|a)*(e|a) = a*
1180 if (GNUNET_YES == needs_parentheses (R_temp_ij))
1181 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
1183 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
1190 // a|(e|a)(e|a)*a = a+
1191 // a|a(e|a)*(e|a) = a+
1192 if (GNUNET_YES == needs_parentheses (R_temp_ij))
1193 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
1195 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
1198 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp &&
1199 0 != clean_ik_kk_cmp)
1202 if (strlen (R_last[k][k]) < 1)
1203 R_cur_r = GNUNET_strdup (R_last[i][j]);
1204 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
1205 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][j], R_temp_kk);
1207 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][j], R_last[k][k]);
1211 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp &&
1212 0 != clean_kk_kj_cmp)
1215 if (strlen (R_last[k][k]) < 1)
1216 R_cur_r = GNUNET_strdup (R_last[k][j]);
1217 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
1218 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last[k][j]);
1220 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[k][j]);
1224 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp &&
1225 !has_epsilon (R_last[i][j]) && has_epsilon (R_last[k][k]))
1227 // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
1228 if (needs_parentheses (R_temp_kk))
1229 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][j], R_temp_kk);
1231 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][j], R_temp_kk);
1235 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp &&
1236 !has_epsilon (R_last[i][j]) && has_epsilon (R_last[k][k]))
1238 // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
1239 if (needs_parentheses (R_temp_kk))
1240 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last[i][j]);
1242 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[i][j]);
1249 (NULL == R_last[i][j]) ? NULL : GNUNET_strdup (R_last[i][j]);
1250 temp_a = remove_parentheses (temp_a);
1254 GNUNET_free_non_null (R_temp_ij);
1258 // we have no left side
1262 // construct R_cur_r, if not already constructed
1263 if (NULL == R_cur_r)
1265 length = strlen (R_temp_kk) - strlen (R_last[i][k]);
1267 // a(ba)*bx = (ab)+x
1268 if (length > 0 && NULL != R_last[k][k] && 0 < strlen (R_last[k][k]) &&
1269 NULL != R_last[k][j] && 0 < strlen (R_last[k][j]) &&
1270 NULL != R_last[i][k] && 0 < strlen (R_last[i][k]) &&
1271 0 == strkcmp (R_temp_kk, R_last[i][k], length) &&
1272 0 == strncmp (R_temp_kk, R_last[k][j], length))
1274 temp_a = GNUNET_malloc (length + 1);
1275 temp_b = GNUNET_malloc ((strlen (R_last[k][j]) - length) + 1);
1280 for (cnt = 0; cnt < strlen (R_last[k][j]); cnt++)
1284 temp_a[length_l] = R_last[k][j][cnt];
1289 temp_b[length_r] = R_last[k][j][cnt];
1293 temp_a[length_l] = '\0';
1294 temp_b[length_r] = '\0';
1297 if (NULL != R_cur_l && 0 == strlen (R_cur_l) &&
1298 0 == strlen (temp_b))
1300 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last[i][k], temp_a);
1301 GNUNET_free (R_cur_l);
1306 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last[i][k], temp_a,
1309 GNUNET_free (temp_a);
1310 GNUNET_free (temp_b);
1312 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
1313 0 == strcmp (R_temp_kk, R_temp_kj))
1315 // (e|a)a*(e|a) = a*
1316 // (e|a)(e|a)*(e|a) = a*
1317 if (has_epsilon (R_last[i][k]) && has_epsilon (R_last[k][j]))
1319 if (needs_parentheses (R_temp_kk))
1320 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
1322 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
1325 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
1326 !has_epsilon (R_last[i][k]))
1328 if (needs_parentheses (R_temp_kk))
1329 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1331 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1335 // a(e|a)*(e|a) = a+
1340 (has_epsilon (R_last[i][k]) + has_epsilon (R_last[k][k]) +
1341 has_epsilon (R_last[k][j]));
1345 if (needs_parentheses (R_temp_kk))
1346 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
1348 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
1353 // (e|a)(e|a)*b = a*b
1354 else if (0 == strcmp (R_temp_ik, R_temp_kk))
1356 if (has_epsilon (R_last[i][k]))
1358 if (needs_parentheses (R_temp_kk))
1359 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last[k][j]);
1361 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last[k][j]);
1365 if (needs_parentheses (R_temp_kk))
1366 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last[k][j]);
1368 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last[k][j]);
1372 // b(e|a)*(e|a) = ba*
1373 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1375 if (has_epsilon (R_last[k][j]))
1377 if (needs_parentheses (R_temp_kk))
1378 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last[i][k], R_temp_kk);
1380 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last[i][k], R_temp_kk);
1384 if (needs_parentheses (R_temp_kk))
1385 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last[i][k], R_temp_kk);
1387 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last[i][k], R_temp_kk);
1392 if (strlen (R_temp_kk) > 0)
1394 if (needs_parentheses (R_temp_kk))
1396 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last[i][k], R_temp_kk,
1401 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last[i][k], R_temp_kk,
1407 GNUNET_asprintf (&R_cur_r, "%s%s", R_last[i][k], R_last[k][j]);
1412 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "R_cur_l: %s\n", R_cur_l); */
1413 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "R_cur_r: %s\n", R_cur_r); */
1415 // putting it all together
1416 if (NULL != R_cur_l && NULL != R_cur_r)
1419 if (0 == strcmp (R_cur_l, R_cur_r))
1421 R_cur[i][j] = GNUNET_strdup (R_cur_l);
1423 // R_cur_l | R_cur_r
1426 GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", R_cur_l, R_cur_r);
1429 else if (NULL != R_cur_l)
1431 R_cur[i][j] = GNUNET_strdup (R_cur_l);
1433 else if (NULL != R_cur_r)
1435 R_cur[i][j] = GNUNET_strdup (R_cur_r);
1442 GNUNET_free_non_null (R_cur_l);
1443 GNUNET_free_non_null (R_cur_r);
1445 GNUNET_free_non_null (R_temp_ik);
1446 GNUNET_free_non_null (R_temp_kk);
1447 GNUNET_free_non_null (R_temp_kj);
1451 // set R_last = R_cur
1452 for (i = 0; i < n; i++)
1454 for (j = 0; j < n; j++)
1456 GNUNET_free_non_null (R_last[i][j]);
1457 R_last[i][j] = R_cur[i][j];
1463 // assign proofs and hashes
1464 for (i = 0; i < n; i++)
1466 if (NULL != R_last[a->start->proof_id][i])
1468 states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
1469 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1474 // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
1475 complete_regex = NULL;
1476 for (i = 0; i < n; i++)
1478 if (states[i]->accepting)
1480 if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
1481 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
1482 else if (NULL != R_last[a->start->proof_id][i] &&
1483 0 < strlen (R_last[a->start->proof_id][i]))
1485 temp_a = complete_regex;
1486 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1487 R_last[a->start->proof_id][i]);
1488 GNUNET_free (temp_a);
1492 a->canonical_regex = complete_regex;
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "---------------------------------------------\n");
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex: %s\n", a->regex);
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Complete Regex: %s\n", complete_regex);
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "---------------------------------------------\n");
1502 for (i = 0; i < n; i++)
1504 for (j = 0; j < n; j++)
1505 GNUNET_free_non_null (R_last[i][j]);
1510 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1511 * automaton_destroy_state.
1513 * @param ctx context
1514 * @param nfa_states set of NFA states on which the DFA should be based on
1516 * @return new DFA state
1518 static struct GNUNET_REGEX_State *
1519 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1520 struct GNUNET_REGEX_StateSet *nfa_states)
1522 struct GNUNET_REGEX_State *s;
1525 struct GNUNET_REGEX_State *cstate;
1526 struct Transition *ctran;
1528 struct Transition *t;
1531 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1532 s->id = ctx->state_id++;
1542 if (NULL == nfa_states)
1544 GNUNET_asprintf (&s->name, "s%i", s->id);
1548 s->nfa_set = nfa_states;
1550 if (nfa_states->len < 1)
1553 // Create a name based on 'sset'
1554 s->name = GNUNET_malloc (sizeof (char) * 2);
1555 strcat (s->name, "{");
1558 for (i = 0; i < nfa_states->len; i++)
1560 cstate = nfa_states->states[i];
1561 GNUNET_asprintf (&name, "%i,", cstate->id);
1565 len = strlen (s->name) + strlen (name) + 1;
1566 s->name = GNUNET_realloc (s->name, len);
1567 strcat (s->name, name);
1572 // Add a transition for each distinct label to NULL state
1573 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1575 if (0 != ctran->label)
1579 for (t = s->transitions_head; NULL != t; t = t->next)
1581 if (t->label == ctran->label)
1589 state_add_transition (ctx, s, ctran->label, NULL);
1593 // If the nfa_states contain an accepting state, the new dfa state is also
1595 if (cstate->accepting)
1599 s->name[strlen (s->name) - 1] = '}';
1605 * Move from the given state 's' to the next state on transition 'label'
1607 * @param s starting state
1608 * @param label edge label to follow
1610 * @return new state or NULL, if transition on label not possible
1612 static struct GNUNET_REGEX_State *
1613 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1615 struct Transition *t;
1616 struct GNUNET_REGEX_State *new_s;
1623 for (t = s->transitions_head; NULL != t; t = t->next)
1625 if (label == t->label)
1627 new_s = t->to_state;
1636 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1637 * states that are not reachable from the starting state.
1639 * @param a DFA automaton
1642 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1644 struct GNUNET_REGEX_State *s;
1645 struct GNUNET_REGEX_State *s_next;
1647 // 1. unmark all states
1648 for (s = a->states_head; NULL != s; s = s->next)
1649 s->marked = GNUNET_NO;
1651 // 2. traverse dfa from start state and mark all visited states
1652 automaton_traverse (a, NULL, NULL);
1654 // 3. delete all states that were not visited
1655 for (s = a->states_head; NULL != s; s = s_next)
1658 if (GNUNET_NO == s->marked)
1659 automaton_remove_state (a, s);
1664 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1665 * not transition to any other state but themselfes.
1667 * @param a DFA automaton
1670 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1672 struct GNUNET_REGEX_State *s;
1673 struct Transition *t;
1676 GNUNET_assert (DFA == a->type);
1678 for (s = a->states_head; NULL != s; s = s->next)
1684 for (t = s->transitions_head; NULL != t; t = t->next)
1686 if (NULL != t->to_state && t->to_state != s)
1696 // state s is dead, remove it
1697 automaton_remove_state (a, s);
1702 * Merge all non distinguishable states in the DFA 'a'
1704 * @param ctx context
1705 * @param a DFA automaton
1708 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1709 struct GNUNET_REGEX_Automaton *a)
1712 int table[a->state_count][a->state_count];
1713 struct GNUNET_REGEX_State *s1;
1714 struct GNUNET_REGEX_State *s2;
1715 struct Transition *t1;
1716 struct Transition *t2;
1717 struct GNUNET_REGEX_State *s1_next;
1718 struct GNUNET_REGEX_State *s2_next;
1720 unsigned int num_equal_edges;
1722 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1728 // Mark all pairs of accepting/!accepting states
1729 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1731 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1733 table[s1->marked][s2->marked] = 0;
1735 if ((s1->accepting && !s2->accepting) ||
1736 (!s1->accepting && s2->accepting))
1738 table[s1->marked][s2->marked] = 1;
1743 // Find all equal states
1748 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1750 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1752 if (0 != table[s1->marked][s2->marked])
1755 num_equal_edges = 0;
1756 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1758 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1760 if (t1->label == t2->label)
1763 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1764 0 != table[t2->to_state->marked][t1->to_state->marked])
1766 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1772 if (num_equal_edges != s1->transition_count ||
1773 num_equal_edges != s2->transition_count)
1775 // Make sure ALL edges of possible equal states are the same
1776 table[s1->marked][s2->marked] = -2;
1782 // Merge states that are equal
1783 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1786 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1789 if (table[s1->marked][s2->marked] == 0)
1790 automaton_merge_states (ctx, a, s1, s2);
1796 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1797 * dead states and merging all non distinguishable states
1799 * @param ctx context
1800 * @param a DFA automaton
1803 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1804 struct GNUNET_REGEX_Automaton *a)
1809 GNUNET_assert (DFA == a->type);
1811 // 1. remove unreachable states
1812 dfa_remove_unreachable_states (a);
1814 // 2. remove dead states
1815 dfa_remove_dead_states (a);
1817 // 3. Merge nondistinguishable states
1818 dfa_merge_nondistinguishable_states (ctx, a);
1822 * Creates a new NFA fragment. Needs to be cleared using
1823 * automaton_fragment_clear.
1825 * @param start starting state
1826 * @param end end state
1828 * @return new NFA fragment
1830 static struct GNUNET_REGEX_Automaton *
1831 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1832 struct GNUNET_REGEX_State *end)
1834 struct GNUNET_REGEX_Automaton *n;
1836 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1842 if (NULL == start && NULL == end)
1845 automaton_add_state (n, end);
1846 automaton_add_state (n, start);
1855 * Adds a list of states to the given automaton 'n'.
1857 * @param n automaton to which the states should be added
1858 * @param states_head head of the DLL of states
1859 * @param states_tail tail of the DLL of states
1862 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1863 struct GNUNET_REGEX_State *states_head,
1864 struct GNUNET_REGEX_State *states_tail)
1866 struct GNUNET_REGEX_State *s;
1868 if (NULL == n || NULL == states_head)
1870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1874 if (NULL == n->states_head)
1876 n->states_head = states_head;
1877 n->states_tail = states_tail;
1881 if (NULL != states_head)
1883 n->states_tail->next = states_head;
1884 n->states_tail = states_tail;
1887 for (s = states_head; NULL != s; s = s->next)
1892 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1894 * @param ctx context
1895 * @param accepting is it an accepting state or not
1897 * @return new NFA state
1899 static struct GNUNET_REGEX_State *
1900 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1902 struct GNUNET_REGEX_State *s;
1904 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1905 s->id = ctx->state_id++;
1906 s->accepting = accepting;
1913 GNUNET_asprintf (&s->name, "s%i", s->id);
1919 * Calculates the NFA closure set for the given state.
1921 * @param nfa the NFA containing 's'
1922 * @param s starting point state
1923 * @param label transitioning label on which to base the closure on,
1924 * pass 0 for epsilon transition
1926 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1928 static struct GNUNET_REGEX_StateSet *
1929 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1930 struct GNUNET_REGEX_State *s, const char label)
1932 struct GNUNET_REGEX_StateSet *cls;
1933 struct GNUNET_REGEX_StateSet *cls_check;
1934 struct GNUNET_REGEX_State *clsstate;
1935 struct GNUNET_REGEX_State *currentstate;
1936 struct Transition *ctran;
1941 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1942 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1944 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1945 clsstate->contained = 0;
1947 // Add start state to closure only for epsilon closure
1949 GNUNET_array_append (cls->states, cls->len, s);
1951 GNUNET_array_append (cls_check->states, cls_check->len, s);
1952 while (cls_check->len > 0)
1954 currentstate = cls_check->states[cls_check->len - 1];
1955 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1957 for (ctran = currentstate->transitions_head; NULL != ctran;
1958 ctran = ctran->next)
1960 if (NULL != ctran->to_state && label == ctran->label)
1962 clsstate = ctran->to_state;
1964 if (NULL != clsstate && 0 == clsstate->contained)
1966 GNUNET_array_append (cls->states, cls->len, clsstate);
1967 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1968 clsstate->contained = 1;
1973 GNUNET_assert (0 == cls_check->len);
1974 GNUNET_free (cls_check);
1978 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1985 * Calculates the closure set for the given set of states.
1987 * @param nfa the NFA containing 's'
1988 * @param states list of states on which to base the closure on
1989 * @param label transitioning label for which to base the closure on,
1990 * pass 0 for epsilon transition
1992 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1994 static struct GNUNET_REGEX_StateSet *
1995 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1996 struct GNUNET_REGEX_StateSet *states, const char label)
1998 struct GNUNET_REGEX_State *s;
1999 struct GNUNET_REGEX_StateSet *sset;
2000 struct GNUNET_REGEX_StateSet *cls;
2004 unsigned int contains;
2009 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2011 for (i = 0; i < states->len; i++)
2013 s = states->states[i];
2014 sset = nfa_closure_create (nfa, s, label);
2016 for (j = 0; j < sset->len; j++)
2019 for (k = 0; k < cls->len; k++)
2021 if (sset->states[j]->id == cls->states[k]->id)
2028 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2030 state_set_clear (sset);
2034 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2041 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2043 * @param ctx context
2046 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2048 struct GNUNET_REGEX_Automaton *a;
2049 struct GNUNET_REGEX_Automaton *b;
2050 struct GNUNET_REGEX_Automaton *new;
2052 b = ctx->stack_tail;
2053 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2054 a = ctx->stack_tail;
2055 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2057 state_add_transition (ctx, a->end, 0, b->start);
2058 a->end->accepting = 0;
2059 b->end->accepting = 1;
2061 new = nfa_fragment_create (NULL, NULL);
2062 nfa_add_states (new, a->states_head, a->states_tail);
2063 nfa_add_states (new, b->states_head, b->states_tail);
2064 new->start = a->start;
2066 automaton_fragment_clear (a);
2067 automaton_fragment_clear (b);
2069 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2073 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2075 * @param ctx context
2078 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2080 struct GNUNET_REGEX_Automaton *a;
2081 struct GNUNET_REGEX_Automaton *new;
2082 struct GNUNET_REGEX_State *start;
2083 struct GNUNET_REGEX_State *end;
2085 a = ctx->stack_tail;
2086 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2091 "nfa_add_star_op failed, because there was no element on the stack");
2095 start = nfa_state_create (ctx, 0);
2096 end = nfa_state_create (ctx, 1);
2098 state_add_transition (ctx, start, 0, a->start);
2099 state_add_transition (ctx, start, 0, end);
2100 state_add_transition (ctx, a->end, 0, a->start);
2101 state_add_transition (ctx, a->end, 0, end);
2103 a->end->accepting = 0;
2106 new = nfa_fragment_create (start, end);
2107 nfa_add_states (new, a->states_head, a->states_tail);
2108 automaton_fragment_clear (a);
2110 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2114 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2116 * @param ctx context
2119 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2121 struct GNUNET_REGEX_Automaton *a;
2123 a = ctx->stack_tail;
2124 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2126 state_add_transition (ctx, a->end, 0, a->start);
2128 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2132 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2134 * @param ctx context
2137 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2139 struct GNUNET_REGEX_Automaton *a;
2140 struct GNUNET_REGEX_Automaton *new;
2141 struct GNUNET_REGEX_State *start;
2142 struct GNUNET_REGEX_State *end;
2144 a = ctx->stack_tail;
2145 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2150 "nfa_add_question_op failed, because there was no element on the stack");
2154 start = nfa_state_create (ctx, 0);
2155 end = nfa_state_create (ctx, 1);
2157 state_add_transition (ctx, start, 0, a->start);
2158 state_add_transition (ctx, start, 0, end);
2159 state_add_transition (ctx, a->end, 0, end);
2161 a->end->accepting = 0;
2163 new = nfa_fragment_create (start, end);
2164 nfa_add_states (new, a->states_head, a->states_tail);
2165 automaton_fragment_clear (a);
2167 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2171 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2172 * alternates between a and b (a|b)
2174 * @param ctx context
2177 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2179 struct GNUNET_REGEX_Automaton *a;
2180 struct GNUNET_REGEX_Automaton *b;
2181 struct GNUNET_REGEX_Automaton *new;
2182 struct GNUNET_REGEX_State *start;
2183 struct GNUNET_REGEX_State *end;
2185 b = ctx->stack_tail;
2186 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2187 a = ctx->stack_tail;
2188 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2190 start = nfa_state_create (ctx, 0);
2191 end = nfa_state_create (ctx, 1);
2192 state_add_transition (ctx, start, 0, a->start);
2193 state_add_transition (ctx, start, 0, b->start);
2195 state_add_transition (ctx, a->end, 0, end);
2196 state_add_transition (ctx, b->end, 0, end);
2198 a->end->accepting = 0;
2199 b->end->accepting = 0;
2202 new = nfa_fragment_create (start, end);
2203 nfa_add_states (new, a->states_head, a->states_tail);
2204 nfa_add_states (new, b->states_head, b->states_tail);
2205 automaton_fragment_clear (a);
2206 automaton_fragment_clear (b);
2208 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
2212 * Adds a new nfa fragment to the stack
2214 * @param ctx context
2215 * @param lit label for nfa transition
2218 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
2220 struct GNUNET_REGEX_Automaton *n;
2221 struct GNUNET_REGEX_State *start;
2222 struct GNUNET_REGEX_State *end;
2224 GNUNET_assert (NULL != ctx);
2226 start = nfa_state_create (ctx, 0);
2227 end = nfa_state_create (ctx, 1);
2228 state_add_transition (ctx, start, lit, end);
2229 n = nfa_fragment_create (start, end);
2230 GNUNET_assert (NULL != n);
2231 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2235 * Initialize a new context
2237 * @param ctx context
2240 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2248 ctx->transition_id = 0;
2249 ctx->stack_head = NULL;
2250 ctx->stack_tail = NULL;
2254 * Construct an NFA by parsing the regex string of length 'len'.
2256 * @param regex regular expression string
2257 * @param len length of the string
2259 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2261 struct GNUNET_REGEX_Automaton *
2262 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2264 struct GNUNET_REGEX_Context ctx;
2265 struct GNUNET_REGEX_Automaton *nfa;
2269 unsigned int altcount;
2270 unsigned int atomcount;
2271 unsigned int pcount;
2278 GNUNET_REGEX_context_init (&ctx);
2287 for (count = 0; count < len && *regexp; count++, regexp++)
2295 nfa_add_concatenation (&ctx);
2297 GNUNET_array_grow (p, pcount, pcount + 1);
2298 p[pcount - 1].altcount = altcount;
2299 p[pcount - 1].atomcount = atomcount;
2306 error_msg = "Cannot append '|' to nothing";
2309 while (--atomcount > 0)
2310 nfa_add_concatenation (&ctx);
2316 error_msg = "Missing opening '('";
2321 // Ignore this: "()"
2323 altcount = p[pcount].altcount;
2324 atomcount = p[pcount].atomcount;
2327 while (--atomcount > 0)
2328 nfa_add_concatenation (&ctx);
2329 for (; altcount > 0; altcount--)
2330 nfa_add_alternation (&ctx);
2332 altcount = p[pcount].altcount;
2333 atomcount = p[pcount].atomcount;
2339 error_msg = "Cannot append '*' to nothing";
2342 nfa_add_star_op (&ctx);
2347 error_msg = "Cannot append '+' to nothing";
2350 nfa_add_plus_op (&ctx);
2355 error_msg = "Cannot append '?' to nothing";
2358 nfa_add_question_op (&ctx);
2360 case 92: /* escape: \ */
2367 nfa_add_concatenation (&ctx);
2369 nfa_add_label (&ctx, *regexp);
2376 error_msg = "Unbalanced parenthesis";
2379 while (--atomcount > 0)
2380 nfa_add_concatenation (&ctx);
2381 for (; altcount > 0; altcount--)
2382 nfa_add_alternation (&ctx);
2384 GNUNET_free_non_null (p);
2386 nfa = ctx.stack_tail;
2387 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2389 if (NULL != ctx.stack_head)
2391 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2395 nfa->regex = GNUNET_strdup (regex);
2400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2401 if (NULL != error_msg)
2402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2404 GNUNET_free_non_null (p);
2406 while (NULL != (nfa = ctx.stack_head))
2408 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2409 GNUNET_REGEX_automaton_destroy (nfa);
2416 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2418 * @param ctx context.
2419 * @param nfa NFA automaton.
2420 * @param dfa DFA automaton.
2421 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2425 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2426 struct GNUNET_REGEX_Automaton *nfa,
2427 struct GNUNET_REGEX_Automaton *dfa,
2428 struct GNUNET_REGEX_State *dfa_state)
2430 struct Transition *ctran;
2431 struct GNUNET_REGEX_State *state_iter;
2432 struct GNUNET_REGEX_State *new_dfa_state;
2433 struct GNUNET_REGEX_State *state_contains;
2434 struct GNUNET_REGEX_StateSet *tmp;
2435 struct GNUNET_REGEX_StateSet *nfa_set;
2437 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2439 if (0 == ctran->label || NULL != ctran->to_state)
2442 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2443 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2444 state_set_clear (tmp);
2445 new_dfa_state = dfa_state_create (ctx, nfa_set);
2446 state_contains = NULL;
2447 for (state_iter = dfa->states_head; NULL != state_iter;
2448 state_iter = state_iter->next)
2450 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2451 state_contains = state_iter;
2454 if (NULL == state_contains)
2456 automaton_add_state (dfa, new_dfa_state);
2457 ctran->to_state = new_dfa_state;
2458 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2462 ctran->to_state = state_contains;
2463 automaton_destroy_state (new_dfa_state);
2469 * Construct DFA for the given 'regex' of length 'len'
2471 * @param regex regular expression string
2472 * @param len length of the regular expression
2474 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2476 struct GNUNET_REGEX_Automaton *
2477 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2479 struct GNUNET_REGEX_Context ctx;
2480 struct GNUNET_REGEX_Automaton *dfa;
2481 struct GNUNET_REGEX_Automaton *nfa;
2482 struct GNUNET_REGEX_StateSet *nfa_set;
2484 GNUNET_REGEX_context_init (&ctx);
2487 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2492 "Could not create DFA, because NFA creation failed\n");
2496 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2498 dfa->regex = GNUNET_strdup (regex);
2500 // Create DFA start state from epsilon closure
2501 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2502 dfa->start = dfa_state_create (&ctx, nfa_set);
2503 automaton_add_state (dfa, dfa->start);
2505 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2507 GNUNET_REGEX_automaton_destroy (nfa);
2510 dfa_minimize (&ctx, dfa);
2512 // Create proofs for all states
2513 automaton_create_proofs (dfa);
2519 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2522 * @param a automaton to be destroyed
2525 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2527 struct GNUNET_REGEX_State *s;
2528 struct GNUNET_REGEX_State *next_state;
2533 GNUNET_free_non_null (a->regex);
2534 GNUNET_free_non_null (a->canonical_regex);
2536 for (s = a->states_head; NULL != s;)
2538 next_state = s->next;
2539 automaton_destroy_state (s);
2547 * Save a state to an open file pointer. cls is expected to be a file pointer to
2548 * an open file. Used only in conjunction with
2549 * GNUNET_REGEX_automaton_save_graph.
2551 * @param cls file pointer.
2552 * @param count current count of the state, not used.
2556 GNUNET_REGEX_automaton_save_graph_step (void *cls, unsigned int count,
2557 struct GNUNET_REGEX_State *s)
2560 struct Transition *ctran;
2562 char *s_tran = NULL;
2568 GNUNET_asprintf (&s_acc,
2569 "\"%s(%i)\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
2570 s->name, s->proof_id, s->scc_id);
2574 GNUNET_asprintf (&s_acc, "\"%s(%i)\" [color=\"0.%i 0.8 0.95\"];\n", s->name,
2575 s->proof_id, s->scc_id);
2580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
2583 fwrite (s_acc, strlen (s_acc), 1, p);
2584 GNUNET_free (s_acc);
2587 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
2589 if (NULL == ctran->to_state)
2591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2592 "Transition from State %i has no state for transitioning\n",
2597 if (ctran->label == 0)
2599 GNUNET_asprintf (&s_tran,
2600 "\"%s(%i)\" -> \"%s(%i)\" [label = \"epsilon\", color=\"0.%i 0.8 0.95\"];\n",
2601 s->name, s->proof_id, ctran->to_state->name,
2602 ctran->to_state->proof_id, s->scc_id);
2606 GNUNET_asprintf (&s_tran,
2607 "\"%s(%i)\" -> \"%s(%i)\" [label = \"%c\", color=\"0.%i 0.8 0.95\"];\n",
2608 s->name, s->proof_id, ctran->to_state->name,
2609 ctran->to_state->proof_id, ctran->label, s->scc_id);
2614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
2619 fwrite (s_tran, strlen (s_tran), 1, p);
2620 GNUNET_free (s_tran);
2626 * Save the given automaton as a GraphViz dot file
2628 * @param a the automaton to be saved
2629 * @param filename where to save the file
2632 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
2633 const char *filename)
2641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
2645 if (NULL == filename || strlen (filename) < 1)
2647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
2651 p = fopen (filename, "w");
2655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
2660 /* First add the SCCs to the automaton, so we can color them nicely */
2663 start = "digraph G {\nrankdir=LR\n";
2664 fwrite (start, strlen (start), 1, p);
2666 automaton_traverse (a, &GNUNET_REGEX_automaton_save_graph_step, p);
2669 fwrite (end, strlen (end), 1, p);
2674 * Evaluates the given string using the given DFA automaton
2676 * @param a automaton, type must be DFA
2677 * @param string string that should be evaluated
2679 * @return 0 if string matches, non 0 otherwise
2682 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2685 struct GNUNET_REGEX_State *s;
2689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2690 "Tried to evaluate DFA, but NFA automaton given");
2696 // If the string is empty but the starting state is accepting, we accept.
2697 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2700 for (strp = string; NULL != strp && *strp; strp++)
2702 s = dfa_move (s, *strp);
2707 if (NULL != s && s->accepting)
2714 * Evaluates the given string using the given NFA automaton
2716 * @param a automaton, type must be NFA
2717 * @param string string that should be evaluated
2719 * @return 0 if string matches, non 0 otherwise
2722 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2725 struct GNUNET_REGEX_State *s;
2726 struct GNUNET_REGEX_StateSet *sset;
2727 struct GNUNET_REGEX_StateSet *new_sset;
2733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2734 "Tried to evaluate NFA, but DFA automaton given");
2738 // If the string is empty but the starting state is accepting, we accept.
2739 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2744 sset = nfa_closure_create (a, a->start, 0);
2746 for (strp = string; NULL != strp && *strp; strp++)
2748 new_sset = nfa_closure_set_create (a, sset, *strp);
2749 state_set_clear (sset);
2750 sset = nfa_closure_set_create (a, new_sset, 0);
2751 state_set_clear (new_sset);
2754 for (i = 0; i < sset->len; i++)
2756 s = sset->states[i];
2757 if (NULL != s && s->accepting)
2764 state_set_clear (sset);
2769 * Evaluates the given 'string' against the given compiled regex
2771 * @param a automaton
2772 * @param string string to check
2774 * @return 0 if string matches, non 0 otherwise
2777 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2784 result = evaluate_dfa (a, string);
2787 result = evaluate_nfa (a, string);
2790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2791 "Evaluating regex failed, automaton has no type!\n");
2792 result = GNUNET_SYSERR;
2801 * Get the canonical regex of the given automaton.
2802 * When constructing the automaton a proof is computed for each state,
2803 * consisting of the regular expression leading to this state. A complete
2804 * regex for the automaton can be computed by combining these proofs.
2805 * As of now this function is only useful for testing.
2807 * @param a automaton for which the canonical regex should be returned.
2812 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2817 return a->canonical_regex;
2821 * Get the first key for the given 'input_string'. This hashes the first x bits
2822 * of the 'input_strings'.
2824 * @param input_string string.
2825 * @param string_len length of the 'input_string'.
2826 * @param key pointer to where to write the hash code.
2828 * @return number of bits of 'input_string' that have been consumed
2829 * to construct the key
2832 GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len,
2833 struct GNUNET_HashCode *key)
2837 size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
2839 if (NULL == input_string)
2841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2845 GNUNET_CRYPTO_hash (input_string, size, key);
2851 * Check if the given 'proof' matches the given 'key'.
2853 * @param proof partial regex
2856 * @return GNUNET_OK if the proof is valid for the given key
2859 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2865 * Iterate over all edges helper function starting from state 's', calling
2866 * iterator on for each edge.
2869 * @param iterator iterator function called for each edge.
2870 * @param iterator_cls closure.
2873 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2876 struct Transition *t;
2877 struct GNUNET_REGEX_Edge edges[s->transition_count];
2878 unsigned int num_edges;
2880 if (GNUNET_YES != s->marked)
2882 s->marked = GNUNET_YES;
2884 num_edges = state_get_edges (s, edges);
2886 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
2888 for (t = s->transitions_head; NULL != t; t = t->next)
2889 iterate_edge (t->to_state, iterator, iterator_cls);
2894 * Iterate over all edges starting from start state of automaton 'a'. Calling
2895 * iterator for each edge.
2897 * @param a automaton.
2898 * @param iterator iterator called for each edge.
2899 * @param iterator_cls closure.
2902 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2903 GNUNET_REGEX_KeyIterator iterator,
2906 struct GNUNET_REGEX_State *s;
2908 for (s = a->states_head; NULL != s; s = s->next)
2909 s->marked = GNUNET_NO;
2911 iterate_edge (a->start, iterator, iterator_cls);