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"
33 * Constant for how many bits the initial string regex should have.
35 #define INITIAL_BITS 8
39 * Context that contains an id counter for states and transitions as well as a
40 * DLL of automatons used as a stack for NFA construction.
42 struct GNUNET_REGEX_Context
47 unsigned int state_id;
50 * Unique transition id.
52 unsigned int transition_id;
55 * DLL of GNUNET_REGEX_Automaton's used as a stack.
57 struct GNUNET_REGEX_Automaton *stack_head;
60 * DLL of GNUNET_REGEX_Automaton's used as a stack.
62 struct GNUNET_REGEX_Automaton *stack_tail;
69 struct GNUNET_REGEX_StateSet
74 struct GNUNET_REGEX_State **states;
77 * Length of the 'states' array.
84 * Debug helper functions
88 * Print all the transitions of state 's'.
90 * @param s state for which to print it's transitions.
93 debug_print_transitions (struct GNUNET_REGEX_State *s);
97 * Print information of the given state 's'.
99 * @param s state for which debug information should be printed.
102 debug_print_state (struct GNUNET_REGEX_State *s)
106 if (NULL == s->proof)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
113 s->id, s->name, s->marked, s->accepting, s->scc_id,
114 s->transition_count, proof);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
117 debug_print_transitions (s);
122 * Print debug information for all states contained in the automaton 'a'.
124 * @param a automaton for which debug information of it's states should be printed.
127 debug_print_states (struct GNUNET_REGEX_Automaton *a)
129 struct GNUNET_REGEX_State *s;
131 for (s = a->states_head; NULL != s; s = s->next)
132 debug_print_state (s);
137 * Print debug information for given transition 't'.
139 * @param t transition for which to print debug info.
142 debug_print_transition (struct GNUNET_REGEX_Transition *t)
156 if (NULL == t->to_state)
159 to_state = t->to_state->name;
161 if (NULL == t->from_state)
164 from_state = t->from_state->name;
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n",
167 t->id, from_state, label, to_state);
172 debug_print_transitions (struct GNUNET_REGEX_State *s)
174 struct GNUNET_REGEX_Transition *t;
176 for (t = s->transitions_head; NULL != t; t = t->next)
177 debug_print_transition (t);
182 * Adds a transition from one state to another on 'label'. Does not add
186 * @param from_state starting state for the transition
187 * @param label transition label
188 * @param to_state state to where the transition should point to
191 state_add_transition (struct GNUNET_REGEX_Context *ctx,
192 struct GNUNET_REGEX_State *from_state, const char label,
193 struct GNUNET_REGEX_State *to_state)
196 struct GNUNET_REGEX_Transition *t;
197 struct GNUNET_REGEX_Transition *oth;
199 if (NULL == from_state)
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
205 // Do not add duplicate state transitions
207 for (t = from_state->transitions_head; NULL != t; t = t->next)
209 if (t->to_state == to_state && t->label == label &&
210 t->from_state == from_state)
217 if (GNUNET_YES == is_dup)
220 // sort transitions by label
221 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
223 if (oth->label > label)
227 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
228 t->id = ctx->transition_id++;
230 t->to_state = to_state;
231 t->from_state = from_state;
233 // Add outgoing transition to 'from_state'
234 from_state->transition_count++;
235 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
236 from_state->transitions_tail, oth, t);
241 * Remove a 'transition' from 'state'.
243 * @param state state from which the to-be-removed transition originates.
244 * @param transition transition that should be removed from state 'state'.
247 state_remove_transition (struct GNUNET_REGEX_State *state,
248 struct GNUNET_REGEX_Transition *transition)
250 if (NULL == state || NULL == transition)
253 if (transition->from_state != state)
256 state->transition_count--;
257 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
259 GNUNET_free (transition);
264 * Compare two states. Used for sorting.
266 * @param a first state
267 * @param b second state
269 * @return an integer less than, equal to, or greater than zero
270 * if the first argument is considered to be respectively
271 * less than, equal to, or greater than the second.
274 state_compare (const void *a, const void *b)
276 struct GNUNET_REGEX_State **s1;
277 struct GNUNET_REGEX_State **s2;
279 s1 = (struct GNUNET_REGEX_State **) a;
280 s2 = (struct GNUNET_REGEX_State **) b;
282 return (*s1)->id - (*s2)->id;
287 * Get all edges leaving state 's'.
290 * @param edges all edges leaving 's', expected to be allocated and have enough
291 * space for s->transitions_count elements.
293 * @return number of edges.
296 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
298 struct GNUNET_REGEX_Transition *t;
306 for (t = s->transitions_head; NULL != t; t = t->next)
308 if (NULL != t->to_state)
310 edges[count].label = &t->label;
311 edges[count].destination = t->to_state->hash;
320 * Compare to state sets by comparing the id's of the states that are contained
321 * in each set. Both sets are expected to be sorted by id!
323 * @param sset1 first state set
324 * @param sset2 second state set
326 * @return an integer less than, equal to, or greater than zero
327 * if the first argument is considered to be respectively
328 * less than, equal to, or greater than the second.
331 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
332 struct GNUNET_REGEX_StateSet *sset2)
337 if (NULL == sset1 || NULL == sset2)
340 result = sset1->len - sset2->len;
342 for (i = 0; i < sset1->len; i++)
347 result = state_compare (&sset1->states[i], &sset2->states[i]);
354 * Clears the given StateSet 'set'
356 * @param set set to be cleared
359 state_set_clear (struct GNUNET_REGEX_StateSet *set)
363 GNUNET_free_non_null (set->states);
370 * Clears an automaton fragment. Does not destroy the states inside the
373 * @param a automaton to be cleared
376 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
383 a->states_head = NULL;
384 a->states_tail = NULL;
391 * Frees the memory used by State 's'
393 * @param s state that should be destroyed
396 automaton_destroy_state (struct GNUNET_REGEX_State *s)
398 struct GNUNET_REGEX_Transition *t;
399 struct GNUNET_REGEX_Transition *next_t;
404 GNUNET_free_non_null (s->name);
405 GNUNET_free_non_null (s->proof);
407 for (t = s->transitions_head; NULL != t; t = next_t)
410 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
414 state_set_clear (s->nfa_set);
421 * Remove a state from the given automaton 'a'. Always use this function when
422 * altering the states of an automaton. Will also remove all transitions leading
423 * to this state, before destroying it.
426 * @param s state to remove
429 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
430 struct GNUNET_REGEX_State *s)
432 struct GNUNET_REGEX_State *ss;
433 struct GNUNET_REGEX_State *s_check;
434 struct GNUNET_REGEX_Transition *t_check;
436 if (NULL == a || NULL == s)
441 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
444 // remove all transitions leading to this state
445 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
447 for (t_check = s_check->transitions_head; NULL != t_check;
448 t_check = t_check->next)
450 if (t_check->to_state == ss)
452 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
453 s_check->transitions_tail, t_check);
454 s_check->transition_count--;
459 automaton_destroy_state (ss);
464 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
469 * @param s1 first state
470 * @param s2 second state, will be destroyed
473 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
474 struct GNUNET_REGEX_Automaton *a,
475 struct GNUNET_REGEX_State *s1,
476 struct GNUNET_REGEX_State *s2)
478 struct GNUNET_REGEX_State *s_check;
479 struct GNUNET_REGEX_Transition *t_check;
480 struct GNUNET_REGEX_Transition *t;
481 struct GNUNET_REGEX_Transition *t_next;
485 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
490 // 1. Make all transitions pointing to s2 point to s1, unless this transition
491 // does not already exists, if it already exists remove transition.
492 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
494 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
496 t_next = t_check->next;
498 if (s2 == t_check->to_state)
501 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
503 if (t->to_state == s1 && t_check->label == t->label)
506 if (GNUNET_NO == is_dup)
507 t_check->to_state = s1;
509 state_remove_transition (t_check->from_state, t_check);
514 // 2. Add all transitions from s2 to sX to s1
515 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
517 if (t_check->to_state != s1)
518 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
521 // 3. Rename s1 to {s1,s2}
523 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
524 GNUNET_free (new_name);
527 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
529 automaton_destroy_state (s2);
534 * Add a state to the automaton 'a', always use this function to alter the
535 * states DLL of the automaton.
537 * @param a automaton to add the state to
538 * @param s state that should be added
541 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
542 struct GNUNET_REGEX_State *s)
544 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
550 * Depth-first traversal of all states that are reachable from state 's'. Expects the states to
551 * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited
554 * @param s start state.
555 * @param count current count of the state.
556 * @param action action to be performed on each state.
557 * @param action_cls closure for action
560 automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
561 GNUNET_REGEX_traverse_action action, void *action_cls)
563 struct GNUNET_REGEX_Transition *t;
565 if (GNUNET_NO != s->marked)
567 s->marked = GNUNET_YES;
569 action (action_cls, *count, s);
571 for (t = s->transitions_head; NULL != t; t = t->next)
572 automaton_state_traverse (t->to_state, count, action, action_cls);
577 * Traverses the given automaton from it's start state, visiting all reachable
578 * states and calling 'action' on each one of them.
580 * @param a automaton.
581 * @param action action to be performed on each state.
582 * @param action_cls closure for action
585 GNUNET_REGEX_automaton_traverse (struct GNUNET_REGEX_Automaton *a,
586 GNUNET_REGEX_traverse_action action,
590 struct GNUNET_REGEX_State *s;
592 for (s = a->states_head; NULL != s; s = s->next)
593 s->marked = GNUNET_NO;
595 automaton_state_traverse (a->start, &count, action, action_cls);
600 * Check if the given string 'str' needs parentheses around it when
601 * using it to generate a regex.
605 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
608 needs_parentheses (const char *str)
616 if ((NULL == str) || ((slen = strlen (str)) < 2))
625 cl = strchr (pos, ')');
631 op = strchr (pos, '(');
632 if ((NULL != op) && (op < cl))
642 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
647 * Remove parentheses surrounding string 'str'.
648 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
649 * You need to GNUNET_free the returned string.
651 * @param str string, free'd or re-used by this function, can be NULL
653 * @return string without surrounding parentheses, string 'str' if no preceding
654 * epsilon could be found, NULL if 'str' was NULL
657 remove_parentheses (char *str)
662 if ((NULL == str) || ('(' != str[0]) ||
663 (str[(slen = strlen (str)) - 1] != ')'))
666 pos = strchr (&str[1], ')');
667 if (pos == &str[slen - 1])
669 memmove (str, &str[1], slen - 2);
670 str[slen - 2] = '\0';
677 * Check if the string 'str' starts with an epsilon (empty string).
678 * Example: "(|a)" is starting with an epsilon.
680 * @param str string to test
682 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
685 has_epsilon (const char *str)
687 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
688 (')' == str[strlen (str) - 1]);
693 * Remove an epsilon from the string str. Where epsilon is an empty string
694 * Example: str = "(|a|b|c)", result: "a|b|c"
695 * The returned string needs to be freed.
699 * @return string without preceding epsilon, string 'str' if no preceding epsilon
700 * could be found, NULL if 'str' was NULL
703 remove_epsilon (const char *str)
709 if (('(' == str[0]) && ('|' == str[1]))
712 if (')' == str[len - 1])
713 return GNUNET_strndup (&str[2], len - 3);
715 return GNUNET_strdup (str);
720 * Compare 'str1', starting from position 'k', with whole 'str2'
722 * @param str1 first string to compare, starting from position 'k'
723 * @param str2 second string for comparison
724 * @param k starting position in 'str1'
726 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
729 strkcmp (const char *str1, const char *str2, size_t k)
731 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
733 return strcmp (&str1[k], str2);
738 * Compare two strings for equality. If either is NULL (or if both are
739 * NULL), they are not equal.
741 * @param str1 first string for comparison.
742 * @param str2 second string for comparison.
744 * @return 0 if the strings are the same, 1 or -1 if not
747 nullstrcmp (const char *str1, const char *str2)
749 if ((NULL == str1) || (NULL == str2))
751 return strcmp (str1, str2);
756 * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse' function to create
757 * the depth-first numbering of the states.
759 * @param cls states array.
760 * @param count current state counter.
761 * @param s current state.
764 number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
766 struct GNUNET_REGEX_State **states = cls;
775 * Construct the regular expression given the inductive step,
776 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
777 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
779 * @param R_last_ij value of $R^{(k-1)_{ij}.
780 * @param R_last_ik value of $R^{(k-1)_{ik}.
781 * @param R_last_kk value of $R^{(k-1)_{kk}.
782 * @param R_last_kj value of $R^{(k-1)_{kj}.
783 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
784 * is expected to be NULL when called!
787 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
788 char *R_last_kk, char *R_last_kj,
814 GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
816 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
817 // R_last == R^{(k-1)}, R_cur == R^{(k)}
818 // R_cur_ij = R_cur_l | R_cur_r
819 // R_cur_l == R^{(k-1)}_{ij}
820 // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
822 if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
823 (NULL == R_last_kj)))
825 /* R^{(k)}_{ij} = N | N */
830 if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
833 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
834 *R_cur_ij = GNUNET_strdup (R_last_ij);
838 // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
839 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
844 // cache results from strcmp, we might need these many times
845 ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
846 ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
847 ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
848 kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
850 // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
851 // as parentheses, so we can better compare the contents
852 R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
853 R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
854 R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
856 clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
857 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
859 // construct R_cur_l (and, if necessary R_cur_r)
860 if (NULL != R_last_ij)
862 // Assign R_temp_ij to R_last_ij and remove epsilon as well
863 // as parentheses, so we can better compare the contents
864 R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
866 if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
867 && 0 == strcmp (R_temp_kk, R_temp_kj))
869 if (0 == strlen (R_temp_ij))
871 R_cur_r = GNUNET_strdup ("");
873 else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
874 (0 == strncmp (R_last_ik, "(|", 2) &&
875 0 == strncmp (R_last_kj, "(|", 2)))
877 // a|(e|a)a*(e|a) = a*
878 // a|(e|a)(e|a)*(e|a) = a*
880 // (e|a)|aa*(e|a) = a*
881 // (e|a)|(e|a)a*a = a*
882 // (e|a)|(e|a)a*(e|a) = a*
883 // (e|a)|(e|a)(e|a)*(e|a) = a*
884 if (GNUNET_YES == needs_parentheses (R_temp_ij))
885 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
887 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
894 // a|(e|a)(e|a)*a = a+
895 // a|a(e|a)*(e|a) = a+
896 if (GNUNET_YES == needs_parentheses (R_temp_ij))
897 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
899 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
902 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
905 if (strlen (R_last_kk) < 1)
906 R_cur_r = GNUNET_strdup (R_last_ij);
907 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
908 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
910 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
914 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
917 if (strlen (R_last_kk) < 1)
918 R_cur_r = GNUNET_strdup (R_last_kj);
919 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
920 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
922 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
926 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
927 has_epsilon (R_last_kk))
929 // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
930 if (needs_parentheses (R_temp_kk))
931 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
933 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
937 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
938 has_epsilon (R_last_kk))
940 // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
941 if (needs_parentheses (R_temp_kk))
942 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
944 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
950 temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
951 temp_a = remove_parentheses (temp_a);
955 GNUNET_free_non_null (R_temp_ij);
959 // we have no left side
963 // construct R_cur_r, if not already constructed
966 length = strlen (R_temp_kk) - strlen (R_last_ik);
969 if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
970 NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
971 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
972 0 == strncmp (R_temp_kk, R_last_kj, length))
974 temp_a = GNUNET_malloc (length + 1);
975 temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
980 for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
984 temp_a[length_l] = R_last_kj[cnt];
989 temp_b[length_r] = R_last_kj[cnt];
993 temp_a[length_l] = '\0';
994 temp_b[length_r] = '\0';
997 if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
999 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
1000 GNUNET_free (R_cur_l);
1005 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
1007 GNUNET_free (temp_a);
1008 GNUNET_free (temp_b);
1010 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
1011 0 == strcmp (R_temp_kk, R_temp_kj))
1013 // (e|a)a*(e|a) = a*
1014 // (e|a)(e|a)*(e|a) = a*
1015 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1017 if (needs_parentheses (R_temp_kk))
1018 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
1020 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
1023 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
1024 !has_epsilon (R_last_ik))
1026 if (needs_parentheses (R_temp_kk))
1027 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1029 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
1033 // a(e|a)*(e|a) = a+
1038 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1039 has_epsilon (R_last_kj));
1043 if (needs_parentheses (R_temp_kk))
1044 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
1046 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
1051 // (e|a)(e|a)*b = a*b
1052 else if (0 == strcmp (R_temp_ik, R_temp_kk))
1054 if (has_epsilon (R_last_ik))
1056 if (needs_parentheses (R_temp_kk))
1057 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
1059 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
1063 if (needs_parentheses (R_temp_kk))
1064 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
1066 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
1070 // b(e|a)*(e|a) = ba*
1071 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1073 if (has_epsilon (R_last_kj))
1075 if (needs_parentheses (R_temp_kk))
1076 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
1078 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
1082 if (needs_parentheses (R_temp_kk))
1083 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
1085 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
1090 if (strlen (R_temp_kk) > 0)
1092 if (needs_parentheses (R_temp_kk))
1094 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1099 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1105 GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1110 GNUNET_free_non_null (R_temp_ik);
1111 GNUNET_free_non_null (R_temp_kk);
1112 GNUNET_free_non_null (R_temp_kj);
1114 if (NULL == R_cur_l && NULL == R_cur_r)
1120 if (NULL != R_cur_l && NULL == R_cur_r)
1122 *R_cur_ij = R_cur_l;
1126 if (NULL == R_cur_l && NULL != R_cur_r)
1128 *R_cur_ij = R_cur_r;
1132 if (0 == nullstrcmp (R_cur_l, R_cur_r))
1134 *R_cur_ij = R_cur_l;
1135 GNUNET_free (R_cur_r);
1139 GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1141 GNUNET_free (R_cur_l);
1142 GNUNET_free (R_cur_r);
1147 * create proofs for all states in the given automaton. Implementation of the
1148 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1149 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1151 * @param a automaton.
1154 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1156 unsigned int n = a->state_count;
1157 struct GNUNET_REGEX_State *states[n];
1161 struct GNUNET_REGEX_Transition *t;
1162 char *complete_regex;
1168 /* create depth-first numbering of the states, initializes 'state' */
1169 GNUNET_REGEX_automaton_traverse (a, &number_states, states);
1171 /* Compute regular expressions of length "1" between each pair of states */
1172 for (i = 0; i < n; i++)
1174 for (j = 0; j < n; j++)
1177 R_last[i][j] = NULL;
1179 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1181 j = t->to_state->proof_id;
1182 if (NULL == R_last[i][j])
1183 GNUNET_asprintf (&R_last[i][j], "%c", t->label);
1186 temp = R_last[i][j];
1187 GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
1191 if (NULL == R_last[i][i])
1192 GNUNET_asprintf (&R_last[i][i], "");
1195 temp = R_last[i][i];
1196 GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
1200 for (i = 0; i < n; i++)
1201 for (j = 0; j < n; j++)
1202 if (needs_parentheses (R_last[i][j]))
1204 temp = R_last[i][j];
1205 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
1209 /* Compute regular expressions of length "k" between each pair of states per induction */
1210 for (k = 0; k < n; k++)
1212 for (i = 0; i < n; i++)
1214 for (j = 0; j < n; j++)
1216 // Basis for the recursion:
1217 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1218 // R_last == R^{(k-1)}, R_cur == R^{(k)}
1220 // Create R_cur[i][j] and simplify the expression
1221 automaton_create_proofs_simplify (R_last[i][j], R_last[i][k],
1222 R_last[k][k], R_last[k][j],
1227 // set R_last = R_cur
1228 for (i = 0; i < n; i++)
1230 for (j = 0; j < n; j++)
1232 GNUNET_free_non_null (R_last[i][j]);
1233 R_last[i][j] = R_cur[i][j];
1239 // assign proofs and hashes
1240 for (i = 0; i < n; i++)
1242 if (NULL != R_last[a->start->proof_id][i])
1244 states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
1245 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1250 // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
1251 complete_regex = NULL;
1252 for (i = 0; i < n; i++)
1254 if (states[i]->accepting)
1256 if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
1258 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
1260 else if (NULL != R_last[a->start->proof_id][i] &&
1261 0 < strlen (R_last[a->start->proof_id][i]))
1263 temp = complete_regex;
1264 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1265 R_last[a->start->proof_id][i]);
1270 a->canonical_regex = complete_regex;
1273 for (i = 0; i < n; i++)
1275 for (j = 0; j < n; j++)
1276 GNUNET_free_non_null (R_last[i][j]);
1282 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1283 * automaton_destroy_state.
1285 * @param ctx context
1286 * @param nfa_states set of NFA states on which the DFA should be based on
1288 * @return new DFA state
1290 static struct GNUNET_REGEX_State *
1291 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1292 struct GNUNET_REGEX_StateSet *nfa_states)
1294 struct GNUNET_REGEX_State *s;
1297 struct GNUNET_REGEX_State *cstate;
1298 struct GNUNET_REGEX_Transition *ctran;
1301 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1302 s->id = ctx->state_id++;
1312 if (NULL == nfa_states)
1314 GNUNET_asprintf (&s->name, "s%i", s->id);
1318 s->nfa_set = nfa_states;
1320 if (nfa_states->len < 1)
1323 // Create a name based on 'sset'
1324 s->name = GNUNET_malloc (sizeof (char) * 2);
1325 strcat (s->name, "{");
1328 for (i = 0; i < nfa_states->len; i++)
1330 cstate = nfa_states->states[i];
1331 GNUNET_asprintf (&name, "%i,", cstate->id);
1335 len = strlen (s->name) + strlen (name) + 1;
1336 s->name = GNUNET_realloc (s->name, len);
1337 strcat (s->name, name);
1342 // Add a transition for each distinct label to NULL state
1343 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1345 if (0 != ctran->label)
1346 state_add_transition (ctx, s, ctran->label, NULL);
1349 // If the nfa_states contain an accepting state, the new dfa state is also
1351 if (cstate->accepting)
1355 s->name[strlen (s->name) - 1] = '}';
1362 * Move from the given state 's' to the next state on transition 'label'
1364 * @param s starting state
1365 * @param label edge label to follow
1367 * @return new state or NULL, if transition on label not possible
1369 static struct GNUNET_REGEX_State *
1370 dfa_move (struct GNUNET_REGEX_State *s, const char label)
1372 struct GNUNET_REGEX_Transition *t;
1373 struct GNUNET_REGEX_State *new_s;
1380 for (t = s->transitions_head; NULL != t; t = t->next)
1382 if (label == t->label)
1384 new_s = t->to_state;
1394 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1395 * states that are not reachable from the starting state.
1397 * @param a DFA automaton
1400 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1402 struct GNUNET_REGEX_State *s;
1403 struct GNUNET_REGEX_State *s_next;
1405 // 1. unmark all states
1406 for (s = a->states_head; NULL != s; s = s->next)
1407 s->marked = GNUNET_NO;
1409 // 2. traverse dfa from start state and mark all visited states
1410 GNUNET_REGEX_automaton_traverse (a, NULL, NULL);
1412 // 3. delete all states that were not visited
1413 for (s = a->states_head; NULL != s; s = s_next)
1416 if (GNUNET_NO == s->marked)
1417 automaton_remove_state (a, s);
1423 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1424 * not transition to any other state but themselves.
1426 * @param a DFA automaton
1429 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1431 struct GNUNET_REGEX_State *s;
1432 struct GNUNET_REGEX_Transition *t;
1435 GNUNET_assert (DFA == a->type);
1437 for (s = a->states_head; NULL != s; s = s->next)
1443 for (t = s->transitions_head; NULL != t; t = t->next)
1445 if (NULL != t->to_state && t->to_state != s)
1455 // state s is dead, remove it
1456 automaton_remove_state (a, s);
1462 * Merge all non distinguishable states in the DFA 'a'
1464 * @param ctx context
1465 * @param a DFA automaton
1468 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1469 struct GNUNET_REGEX_Automaton *a)
1472 int table[a->state_count][a->state_count];
1473 struct GNUNET_REGEX_State *s1;
1474 struct GNUNET_REGEX_State *s2;
1475 struct GNUNET_REGEX_Transition *t1;
1476 struct GNUNET_REGEX_Transition *t2;
1477 struct GNUNET_REGEX_State *s1_next;
1478 struct GNUNET_REGEX_State *s2_next;
1480 unsigned int num_equal_edges;
1482 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1488 // Mark all pairs of accepting/!accepting states
1489 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1491 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1493 table[s1->marked][s2->marked] = 0;
1495 if ((s1->accepting && !s2->accepting) ||
1496 (!s1->accepting && s2->accepting))
1498 table[s1->marked][s2->marked] = 1;
1503 // Find all equal states
1508 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1510 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1512 if (0 != table[s1->marked][s2->marked])
1515 num_equal_edges = 0;
1516 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1518 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1520 if (t1->label == t2->label)
1523 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1524 0 != table[t2->to_state->marked][t1->to_state->marked])
1526 table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1;
1532 if (num_equal_edges != s1->transition_count ||
1533 num_equal_edges != s2->transition_count)
1535 // Make sure ALL edges of possible equal states are the same
1536 table[s1->marked][s2->marked] = -2;
1542 // Merge states that are equal
1543 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1546 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1549 if (table[s1->marked][s2->marked] == 0)
1550 automaton_merge_states (ctx, a, s1, s2);
1557 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1558 * dead states and merging all non distinguishable states
1560 * @param ctx context
1561 * @param a DFA automaton
1564 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1565 struct GNUNET_REGEX_Automaton *a)
1570 GNUNET_assert (DFA == a->type);
1572 // 1. remove unreachable states
1573 dfa_remove_unreachable_states (a);
1575 // 2. remove dead states
1576 dfa_remove_dead_states (a);
1578 // 3. Merge nondistinguishable states
1579 dfa_merge_nondistinguishable_states (ctx, a);
1584 * Creates a new NFA fragment. Needs to be cleared using
1585 * automaton_fragment_clear.
1587 * @param start starting state
1588 * @param end end state
1590 * @return new NFA fragment
1592 static struct GNUNET_REGEX_Automaton *
1593 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1594 struct GNUNET_REGEX_State *end)
1596 struct GNUNET_REGEX_Automaton *n;
1598 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1604 if (NULL == start || NULL == end)
1607 automaton_add_state (n, end);
1608 automaton_add_state (n, start);
1618 * Adds a list of states to the given automaton 'n'.
1620 * @param n automaton to which the states should be added
1621 * @param states_head head of the DLL of states
1622 * @param states_tail tail of the DLL of states
1625 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1626 struct GNUNET_REGEX_State *states_head,
1627 struct GNUNET_REGEX_State *states_tail)
1629 struct GNUNET_REGEX_State *s;
1631 if (NULL == n || NULL == states_head)
1633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1637 if (NULL == n->states_head)
1639 n->states_head = states_head;
1640 n->states_tail = states_tail;
1644 if (NULL != states_head)
1646 n->states_tail->next = states_head;
1647 n->states_tail = states_tail;
1650 for (s = states_head; NULL != s; s = s->next)
1656 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1658 * @param ctx context
1659 * @param accepting is it an accepting state or not
1661 * @return new NFA state
1663 static struct GNUNET_REGEX_State *
1664 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1666 struct GNUNET_REGEX_State *s;
1668 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1669 s->id = ctx->state_id++;
1670 s->accepting = accepting;
1677 GNUNET_asprintf (&s->name, "s%i", s->id);
1684 * Calculates the NFA closure set for the given state.
1686 * @param nfa the NFA containing 's'
1687 * @param s starting point state
1688 * @param label transitioning label on which to base the closure on,
1689 * pass 0 for epsilon transition
1691 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1693 static struct GNUNET_REGEX_StateSet *
1694 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1695 struct GNUNET_REGEX_State *s, const char label)
1697 struct GNUNET_REGEX_StateSet *cls;
1698 struct GNUNET_REGEX_StateSet *cls_check;
1699 struct GNUNET_REGEX_State *clsstate;
1700 struct GNUNET_REGEX_State *currentstate;
1701 struct GNUNET_REGEX_Transition *ctran;
1706 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1707 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1709 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1710 clsstate->contained = 0;
1712 // Add start state to closure only for epsilon closure
1714 GNUNET_array_append (cls->states, cls->len, s);
1716 GNUNET_array_append (cls_check->states, cls_check->len, s);
1717 while (cls_check->len > 0)
1719 currentstate = cls_check->states[cls_check->len - 1];
1720 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1722 for (ctran = currentstate->transitions_head; NULL != ctran;
1723 ctran = ctran->next)
1725 if (NULL != ctran->to_state && label == ctran->label)
1727 clsstate = ctran->to_state;
1729 if (NULL != clsstate && 0 == clsstate->contained)
1731 GNUNET_array_append (cls->states, cls->len, clsstate);
1732 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1733 clsstate->contained = 1;
1738 GNUNET_assert (0 == cls_check->len);
1739 GNUNET_free (cls_check);
1743 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1751 * Calculates the closure set for the given set of states.
1753 * @param nfa the NFA containing 's'
1754 * @param states list of states on which to base the closure on
1755 * @param label transitioning label for which to base the closure on,
1756 * pass 0 for epsilon transition
1758 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
1760 static struct GNUNET_REGEX_StateSet *
1761 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1762 struct GNUNET_REGEX_StateSet *states, const char label)
1764 struct GNUNET_REGEX_State *s;
1765 struct GNUNET_REGEX_StateSet *sset;
1766 struct GNUNET_REGEX_StateSet *cls;
1770 unsigned int contains;
1775 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1777 for (i = 0; i < states->len; i++)
1779 s = states->states[i];
1780 sset = nfa_closure_create (nfa, s, label);
1782 for (j = 0; j < sset->len; j++)
1785 for (k = 0; k < cls->len; k++)
1787 if (sset->states[j]->id == cls->states[k]->id)
1794 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1796 state_set_clear (sset);
1800 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1808 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1810 * @param ctx context
1813 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1815 struct GNUNET_REGEX_Automaton *a;
1816 struct GNUNET_REGEX_Automaton *b;
1817 struct GNUNET_REGEX_Automaton *new_nfa;
1819 b = ctx->stack_tail;
1820 GNUNET_assert (NULL != b);
1821 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1822 a = ctx->stack_tail;
1823 GNUNET_assert (NULL != a);
1824 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1826 state_add_transition (ctx, a->end, 0, b->start);
1827 a->end->accepting = 0;
1828 b->end->accepting = 1;
1830 new_nfa = nfa_fragment_create (NULL, NULL);
1831 nfa_add_states (new_nfa, a->states_head, a->states_tail);
1832 nfa_add_states (new_nfa, b->states_head, b->states_tail);
1833 new_nfa->start = a->start;
1834 new_nfa->end = b->end;
1835 automaton_fragment_clear (a);
1836 automaton_fragment_clear (b);
1838 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1843 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1845 * @param ctx context
1848 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1850 struct GNUNET_REGEX_Automaton *a;
1851 struct GNUNET_REGEX_Automaton *new_nfa;
1852 struct GNUNET_REGEX_State *start;
1853 struct GNUNET_REGEX_State *end;
1855 a = ctx->stack_tail;
1859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1860 "nfa_add_star_op failed, because there was no element on the stack");
1864 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1866 start = nfa_state_create (ctx, 0);
1867 end = nfa_state_create (ctx, 1);
1869 state_add_transition (ctx, start, 0, a->start);
1870 state_add_transition (ctx, start, 0, end);
1871 state_add_transition (ctx, a->end, 0, a->start);
1872 state_add_transition (ctx, a->end, 0, end);
1874 a->end->accepting = 0;
1877 new_nfa = nfa_fragment_create (start, end);
1878 nfa_add_states (new_nfa, a->states_head, a->states_tail);
1879 automaton_fragment_clear (a);
1881 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1886 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1888 * @param ctx context
1891 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1893 struct GNUNET_REGEX_Automaton *a;
1895 a = ctx->stack_tail;
1896 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1898 state_add_transition (ctx, a->end, 0, a->start);
1900 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1905 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1907 * @param ctx context
1910 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1912 struct GNUNET_REGEX_Automaton *a;
1913 struct GNUNET_REGEX_Automaton *new_nfa;
1914 struct GNUNET_REGEX_State *start;
1915 struct GNUNET_REGEX_State *end;
1917 a = ctx->stack_tail;
1921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1922 "nfa_add_question_op failed, because there was no element on the stack");
1926 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1928 start = nfa_state_create (ctx, 0);
1929 end = nfa_state_create (ctx, 1);
1931 state_add_transition (ctx, start, 0, a->start);
1932 state_add_transition (ctx, start, 0, end);
1933 state_add_transition (ctx, a->end, 0, end);
1935 a->end->accepting = 0;
1937 new_nfa = nfa_fragment_create (start, end);
1938 nfa_add_states (new_nfa, a->states_head, a->states_tail);
1939 automaton_fragment_clear (a);
1941 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1946 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
1947 * alternates between a and b (a|b)
1949 * @param ctx context
1952 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1954 struct GNUNET_REGEX_Automaton *a;
1955 struct GNUNET_REGEX_Automaton *b;
1956 struct GNUNET_REGEX_Automaton *new_nfa;
1957 struct GNUNET_REGEX_State *start;
1958 struct GNUNET_REGEX_State *end;
1960 b = ctx->stack_tail;
1961 GNUNET_assert (NULL != b);
1962 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1963 a = ctx->stack_tail;
1964 GNUNET_assert (NULL != a);
1965 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1967 start = nfa_state_create (ctx, 0);
1968 end = nfa_state_create (ctx, 1);
1969 state_add_transition (ctx, start, 0, a->start);
1970 state_add_transition (ctx, start, 0, b->start);
1972 state_add_transition (ctx, a->end, 0, end);
1973 state_add_transition (ctx, b->end, 0, end);
1975 a->end->accepting = 0;
1976 b->end->accepting = 0;
1979 new_nfa = nfa_fragment_create (start, end);
1980 nfa_add_states (new_nfa, a->states_head, a->states_tail);
1981 nfa_add_states (new_nfa, b->states_head, b->states_tail);
1982 automaton_fragment_clear (a);
1983 automaton_fragment_clear (b);
1985 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
1990 * Adds a new nfa fragment to the stack
1992 * @param ctx context
1993 * @param lit label for nfa transition
1996 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
1998 struct GNUNET_REGEX_Automaton *n;
1999 struct GNUNET_REGEX_State *start;
2000 struct GNUNET_REGEX_State *end;
2002 GNUNET_assert (NULL != ctx);
2004 start = nfa_state_create (ctx, 0);
2005 end = nfa_state_create (ctx, 1);
2006 state_add_transition (ctx, start, lit, end);
2007 n = nfa_fragment_create (start, end);
2008 GNUNET_assert (NULL != n);
2009 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2014 * Initialize a new context
2016 * @param ctx context
2019 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2027 ctx->transition_id = 0;
2028 ctx->stack_head = NULL;
2029 ctx->stack_tail = NULL;
2034 * Construct an NFA by parsing the regex string of length 'len'.
2036 * @param regex regular expression string
2037 * @param len length of the string
2039 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2041 struct GNUNET_REGEX_Automaton *
2042 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2044 struct GNUNET_REGEX_Context ctx;
2045 struct GNUNET_REGEX_Automaton *nfa;
2049 unsigned int altcount;
2050 unsigned int atomcount;
2051 unsigned int pcount;
2058 GNUNET_REGEX_context_init (&ctx);
2067 for (count = 0; count < len && *regexp; count++, regexp++)
2075 nfa_add_concatenation (&ctx);
2077 GNUNET_array_grow (p, pcount, pcount + 1);
2078 p[pcount - 1].altcount = altcount;
2079 p[pcount - 1].atomcount = atomcount;
2086 error_msg = "Cannot append '|' to nothing";
2089 while (--atomcount > 0)
2090 nfa_add_concatenation (&ctx);
2096 error_msg = "Missing opening '('";
2101 // Ignore this: "()"
2103 altcount = p[pcount].altcount;
2104 atomcount = p[pcount].atomcount;
2107 while (--atomcount > 0)
2108 nfa_add_concatenation (&ctx);
2109 for (; altcount > 0; altcount--)
2110 nfa_add_alternation (&ctx);
2112 altcount = p[pcount].altcount;
2113 atomcount = p[pcount].atomcount;
2119 error_msg = "Cannot append '*' to nothing";
2122 nfa_add_star_op (&ctx);
2127 error_msg = "Cannot append '+' to nothing";
2130 nfa_add_plus_op (&ctx);
2135 error_msg = "Cannot append '?' to nothing";
2138 nfa_add_question_op (&ctx);
2140 case 92: /* escape: \ */
2148 nfa_add_concatenation (&ctx);
2150 nfa_add_label (&ctx, *regexp);
2157 error_msg = "Unbalanced parenthesis";
2160 while (--atomcount > 0)
2161 nfa_add_concatenation (&ctx);
2162 for (; altcount > 0; altcount--)
2163 nfa_add_alternation (&ctx);
2165 GNUNET_free_non_null (p);
2167 nfa = ctx.stack_tail;
2168 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2170 if (NULL != ctx.stack_head)
2172 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2176 nfa->regex = GNUNET_strdup (regex);
2178 /* create depth-first numbering of the states for pretty printing */
2179 GNUNET_REGEX_automaton_traverse (nfa, &number_states, NULL);
2184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2185 if (NULL != error_msg)
2186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2188 GNUNET_free_non_null (p);
2190 while (NULL != (nfa = ctx.stack_head))
2192 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2193 GNUNET_REGEX_automaton_destroy (nfa);
2201 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2203 * @param ctx context.
2204 * @param nfa NFA automaton.
2205 * @param dfa DFA automaton.
2206 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2210 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2211 struct GNUNET_REGEX_Automaton *nfa,
2212 struct GNUNET_REGEX_Automaton *dfa,
2213 struct GNUNET_REGEX_State *dfa_state)
2215 struct GNUNET_REGEX_Transition *ctran;
2216 struct GNUNET_REGEX_State *state_iter;
2217 struct GNUNET_REGEX_State *new_dfa_state;
2218 struct GNUNET_REGEX_State *state_contains;
2219 struct GNUNET_REGEX_StateSet *tmp;
2220 struct GNUNET_REGEX_StateSet *nfa_set;
2222 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2224 if (0 == ctran->label || NULL != ctran->to_state)
2227 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2228 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2229 state_set_clear (tmp);
2230 new_dfa_state = dfa_state_create (ctx, nfa_set);
2231 state_contains = NULL;
2232 for (state_iter = dfa->states_head; NULL != state_iter;
2233 state_iter = state_iter->next)
2235 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2236 state_contains = state_iter;
2239 if (NULL == state_contains)
2241 automaton_add_state (dfa, new_dfa_state);
2242 ctran->to_state = new_dfa_state;
2243 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2247 ctran->to_state = state_contains;
2248 automaton_destroy_state (new_dfa_state);
2255 * Construct DFA for the given 'regex' of length 'len'
2257 * @param regex regular expression string
2258 * @param len length of the regular expression
2260 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2262 struct GNUNET_REGEX_Automaton *
2263 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2265 struct GNUNET_REGEX_Context ctx;
2266 struct GNUNET_REGEX_Automaton *dfa;
2267 struct GNUNET_REGEX_Automaton *nfa;
2268 struct GNUNET_REGEX_StateSet *nfa_set;
2270 GNUNET_REGEX_context_init (&ctx);
2273 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2278 "Could not create DFA, because NFA creation failed\n");
2282 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2284 dfa->regex = GNUNET_strdup (regex);
2286 // Create DFA start state from epsilon closure
2287 nfa_set = nfa_closure_create (nfa, nfa->start, 0);
2288 dfa->start = dfa_state_create (&ctx, nfa_set);
2289 automaton_add_state (dfa, dfa->start);
2291 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2293 GNUNET_REGEX_automaton_destroy (nfa);
2296 dfa_minimize (&ctx, dfa);
2298 // Create proofs for all states
2299 automaton_create_proofs (dfa);
2306 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2309 * @param a automaton to be destroyed
2312 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2314 struct GNUNET_REGEX_State *s;
2315 struct GNUNET_REGEX_State *next_state;
2320 GNUNET_free_non_null (a->regex);
2321 GNUNET_free_non_null (a->canonical_regex);
2323 for (s = a->states_head; NULL != s;)
2325 next_state = s->next;
2326 automaton_destroy_state (s);
2335 * Evaluates the given string using the given DFA automaton
2337 * @param a automaton, type must be DFA
2338 * @param string string that should be evaluated
2340 * @return 0 if string matches, non 0 otherwise
2343 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2346 struct GNUNET_REGEX_State *s;
2350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2351 "Tried to evaluate DFA, but NFA automaton given");
2357 // If the string is empty but the starting state is accepting, we accept.
2358 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2361 for (strp = string; NULL != strp && *strp; strp++)
2363 s = dfa_move (s, *strp);
2368 if (NULL != s && s->accepting)
2376 * Evaluates the given string using the given NFA automaton
2378 * @param a automaton, type must be NFA
2379 * @param string string that should be evaluated
2381 * @return 0 if string matches, non 0 otherwise
2384 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2387 struct GNUNET_REGEX_State *s;
2388 struct GNUNET_REGEX_StateSet *sset;
2389 struct GNUNET_REGEX_StateSet *new_sset;
2395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2396 "Tried to evaluate NFA, but DFA automaton given");
2400 // If the string is empty but the starting state is accepting, we accept.
2401 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2405 sset = nfa_closure_create (a, a->start, 0);
2407 for (strp = string; NULL != strp && *strp; strp++)
2409 new_sset = nfa_closure_set_create (a, sset, *strp);
2410 state_set_clear (sset);
2411 sset = nfa_closure_set_create (a, new_sset, 0);
2412 state_set_clear (new_sset);
2415 for (i = 0; i < sset->len; i++)
2417 s = sset->states[i];
2418 if (NULL != s && s->accepting)
2425 state_set_clear (sset);
2431 * Evaluates the given 'string' against the given compiled regex
2433 * @param a automaton
2434 * @param string string to check
2436 * @return 0 if string matches, non 0 otherwise
2439 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2446 result = evaluate_dfa (a, string);
2449 result = evaluate_nfa (a, string);
2452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2453 "Evaluating regex failed, automaton has no type!\n");
2454 result = GNUNET_SYSERR;
2463 * Get the canonical regex of the given automaton.
2464 * When constructing the automaton a proof is computed for each state,
2465 * consisting of the regular expression leading to this state. A complete
2466 * regex for the automaton can be computed by combining these proofs.
2467 * As of now this function is only useful for testing.
2469 * @param a automaton for which the canonical regex should be returned.
2474 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2479 return a->canonical_regex;
2484 * Get the first key for the given 'input_string'. This hashes the first x bits
2485 * of the 'input_string'.
2487 * @param input_string string.
2488 * @param string_len length of the 'input_string'.
2489 * @param key pointer to where to write the hash code.
2491 * @return number of bits of 'input_string' that have been consumed
2492 * to construct the key
2495 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2496 struct GNUNET_HashCode * key)
2500 size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
2502 if (NULL == input_string)
2504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2508 GNUNET_CRYPTO_hash (input_string, size, key);
2515 * Check if the given 'proof' matches the given 'key'.
2517 * @param proof partial regex of a state.
2518 * @param key hash of a state.
2520 * @return GNUNET_OK if the proof is valid for the given key.
2523 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2525 struct GNUNET_HashCode key_check;
2527 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2529 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2534 * Recursive helper function for iterate_initial_edges. Will call iterator
2535 * function for each initial state.
2537 * @param min_len minimum length of the path in the graph.
2538 * @param max_len maximum length of the path in the graph.
2539 * @param cur_len current length of the path already traversed.
2540 * @param consumed_string string consumed by traversing the graph till this state.
2541 * @param state current state of the automaton.
2542 * @param iterator iterator function called for each edge.
2543 * @param iterator_cls closure for the iterator function.
2546 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2547 unsigned int cur_len, char *consumed_string,
2548 struct GNUNET_REGEX_State *state,
2549 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2552 char label[state->transition_count][2];
2554 struct GNUNET_REGEX_Transition *t;
2555 unsigned int num_edges = state->transition_count;
2556 struct GNUNET_REGEX_Edge edges[num_edges];
2557 struct GNUNET_HashCode hash;
2559 if (cur_len > min_len && NULL != consumed_string && cur_len <= max_len)
2561 for (i = 0, t = state->transitions_head; NULL != t; t = t->next, i++)
2563 label[i][0] = t->label;
2565 edges[i].label = label[i];
2566 edges[i].destination = t->to_state->hash;
2569 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2570 iterator (iterator_cls, &hash, consumed_string, state->accepting, num_edges,
2574 if (cur_len < max_len)
2577 for (t = state->transitions_head; NULL != t; t = t->next)
2579 if (NULL != consumed_string)
2580 GNUNET_asprintf (&temp, "%s%c", consumed_string, t->label);
2582 GNUNET_asprintf (&temp, "%c", t->label);
2584 iterate_initial_edge (min_len, max_len, cur_len, temp, t->to_state,
2585 iterator, iterator_cls);
2593 * Iterate over all initial edges that aren't actually part of the automaton.
2594 * This is needed to find the initial states returned by
2595 * GNUNET_REGEX_get_first_key. Iteration will start at the first branch state (a
2596 * state that has more than one outgoing edge, can be the first state), because
2597 * all previous states will have the same proof and be iterated over in
2598 * iterate_all_edges.
2600 * @param a the automaton for which the initial states should be computed.
2601 * @param initial_len length of the initial state string.
2602 * @param iterator iterator function called for each edge.
2603 * @param iterator_cls closure for the iterator function.
2606 iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
2607 const unsigned int initial_len,
2608 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2610 char *consumed_string;
2612 struct GNUNET_REGEX_State *s;
2613 unsigned int cur_len;
2615 if (1 > initial_len)
2618 consumed_string = NULL;
2622 if (1 == s->transition_count)
2626 if (NULL != consumed_string)
2628 temp = consumed_string;
2629 GNUNET_asprintf (&consumed_string, "%s%c", consumed_string,
2630 s->transitions_head->label);
2634 GNUNET_asprintf (&consumed_string, "%c", s->transitions_head->label);
2636 s = s->transitions_head->to_state;
2639 while (cur_len < initial_len && 1 == s->transition_count);
2642 iterate_initial_edge (cur_len, initial_len, cur_len, consumed_string, s,
2643 iterator, iterator_cls);
2645 GNUNET_free_non_null (consumed_string);
2650 * Iterate over all edges helper function starting from state 's', calling
2651 * iterator on for each edge.
2654 * @param iterator iterator function called for each edge.
2655 * @param iterator_cls closure.
2658 iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
2661 struct GNUNET_REGEX_Transition *t;
2662 struct GNUNET_REGEX_Edge edges[s->transition_count];
2663 unsigned int num_edges;
2665 if (GNUNET_YES != s->marked)
2667 s->marked = GNUNET_YES;
2669 num_edges = state_get_edges (s, edges);
2671 if (0 < strlen (s->proof) || s->accepting)
2672 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
2675 for (t = s->transitions_head; NULL != t; t = t->next)
2676 iterate_edge (t->to_state, iterator, iterator_cls);
2682 * Iterate over all edges starting from start state of automaton 'a'. Calling
2683 * iterator for each edge.
2685 * @param a automaton.
2686 * @param iterator iterator called for each edge.
2687 * @param iterator_cls closure.
2690 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2691 GNUNET_REGEX_KeyIterator iterator,
2694 struct GNUNET_REGEX_State *s;
2696 for (s = a->states_head; NULL != s; s = s->next)
2697 s->marked = GNUNET_NO;
2699 iterate_initial_edges (a, INITIAL_BITS, iterator, iterator_cls);
2700 iterate_edge (a->start, iterator, iterator_cls);