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 * Set this to GNUNET_YES to enable state naming. Used to debug NFA->DFA
35 #define REGEX_DEBUG_DFA GNUNET_NO
40 struct GNUNET_REGEX_StateSet
45 struct GNUNET_REGEX_State **states;
48 * Length of the 'states' array.
54 * Set of states using MDLL API.
56 struct GNUNET_REGEX_StateSet_MDLL
61 struct GNUNET_REGEX_State *head;
66 struct GNUNET_REGEX_State *tail;
76 * Compare two strings for equality. If either is NULL they are not equal.
78 * @param str1 first string for comparison.
79 * @param str2 second string for comparison.
81 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
84 nullstrcmp (const char *str1, const char *str2)
86 if ((NULL == str1) != (NULL == str2))
88 if ((NULL == str1) && (NULL == str2))
91 return strcmp (str1, str2);
96 * Adds a transition from one state to another on 'label'. Does not add
100 * @param from_state starting state for the transition
101 * @param label transition label
102 * @param to_state state to where the transition should point to
105 state_add_transition (struct GNUNET_REGEX_Context *ctx,
106 struct GNUNET_REGEX_State *from_state, const char *label,
107 struct GNUNET_REGEX_State *to_state)
109 struct GNUNET_REGEX_Transition *t;
110 struct GNUNET_REGEX_Transition *oth;
112 if (NULL == from_state)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
118 // Do not add duplicate state transitions
119 for (t = from_state->transitions_head; NULL != t; t = t->next)
121 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
122 t->from_state == from_state)
126 // sort transitions by label
127 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
129 if (0 < nullstrcmp (oth->label, label))
133 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
135 t->id = ctx->transition_id++;
137 t->label = GNUNET_strdup (label);
140 t->to_state = to_state;
141 t->from_state = from_state;
143 // Add outgoing transition to 'from_state'
144 from_state->transition_count++;
145 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
146 from_state->transitions_tail, oth, t);
151 * Remove a 'transition' from 'state'.
153 * @param state state from which the to-be-removed transition originates.
154 * @param transition transition that should be removed from state 'state'.
157 state_remove_transition (struct GNUNET_REGEX_State *state,
158 struct GNUNET_REGEX_Transition *transition)
160 if (NULL == state || NULL == transition)
163 if (transition->from_state != state)
166 GNUNET_free_non_null (transition->label);
168 state->transition_count--;
169 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
172 GNUNET_free (transition);
177 * Compare two states. Used for sorting.
179 * @param a first state
180 * @param b second state
182 * @return an integer less than, equal to, or greater than zero
183 * if the first argument is considered to be respectively
184 * less than, equal to, or greater than the second.
187 state_compare (const void *a, const void *b)
189 struct GNUNET_REGEX_State **s1;
190 struct GNUNET_REGEX_State **s2;
192 s1 = (struct GNUNET_REGEX_State **) a;
193 s2 = (struct GNUNET_REGEX_State **) b;
195 return (*s1)->id - (*s2)->id;
200 * Get all edges leaving state 's'.
203 * @param edges all edges leaving 's', expected to be allocated and have enough
204 * space for s->transitions_count elements.
206 * @return number of edges.
209 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
211 struct GNUNET_REGEX_Transition *t;
219 for (t = s->transitions_head; NULL != t; t = t->next)
221 if (NULL != t->to_state)
223 edges[count].label = t->label;
224 edges[count].destination = t->to_state->hash;
233 * Compare to state sets by comparing the id's of the states that are contained
234 * in each set. Both sets are expected to be sorted by id!
236 * @param sset1 first state set
237 * @param sset2 second state set
239 * @return an integer less than, equal to, or greater than zero
240 * if the first argument is considered to be respectively
241 * less than, equal to, or greater than the second.
244 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
245 struct GNUNET_REGEX_StateSet *sset2)
250 if (NULL == sset1 || NULL == sset2)
253 result = sset1->len - sset2->len;
255 for (i = 0; i < sset1->len; i++)
260 result = state_compare (&sset1->states[i], &sset2->states[i]);
267 * Clears the given StateSet 'set'
269 * @param set set to be cleared
272 state_set_clear (struct GNUNET_REGEX_StateSet *set)
278 GNUNET_array_grow (set->states, set->len, 0);
284 * Clears an automaton fragment. Does not destroy the states inside the
287 * @param a automaton to be cleared
290 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
297 a->states_head = NULL;
298 a->states_tail = NULL;
305 * Frees the memory used by State 's'
307 * @param s state that should be destroyed
310 automaton_destroy_state (struct GNUNET_REGEX_State *s)
312 struct GNUNET_REGEX_Transition *t;
313 struct GNUNET_REGEX_Transition *next_t;
318 GNUNET_free_non_null (s->name);
319 GNUNET_free_non_null (s->proof);
320 state_set_clear (s->nfa_set);
322 for (t = s->transitions_head; NULL != t; t = next_t)
325 state_remove_transition (s, t);
333 * Remove a state from the given automaton 'a'. Always use this function when
334 * altering the states of an automaton. Will also remove all transitions leading
335 * to this state, before destroying it.
338 * @param s state to remove
341 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
342 struct GNUNET_REGEX_State *s)
344 struct GNUNET_REGEX_State *s_check;
345 struct GNUNET_REGEX_Transition *t_check;
346 struct GNUNET_REGEX_Transition *t_check_next;
348 if (NULL == a || NULL == s)
351 // remove all transitions leading to this state
352 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
354 for (t_check = s_check->transitions_head; NULL != t_check;
355 t_check = t_check_next)
357 t_check_next = t_check->next;
358 if (t_check->to_state == s)
359 state_remove_transition (s_check, t_check);
364 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
367 automaton_destroy_state (s);
372 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
377 * @param s1 first state
378 * @param s2 second state, will be destroyed
381 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
382 struct GNUNET_REGEX_Automaton *a,
383 struct GNUNET_REGEX_State *s1,
384 struct GNUNET_REGEX_State *s2)
386 struct GNUNET_REGEX_State *s_check;
387 struct GNUNET_REGEX_Transition *t_check;
388 struct GNUNET_REGEX_Transition *t;
389 struct GNUNET_REGEX_Transition *t_next;
392 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
397 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
398 does not already exists, if it already exists remove transition. */
399 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
401 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
403 t_next = t_check->next;
405 if (s2 == t_check->to_state)
408 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
410 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
413 if (GNUNET_NO == is_dup)
414 t_check->to_state = s1;
416 state_remove_transition (t_check->from_state, t_check);
421 /* 2. Add all transitions from s2 to sX to s1 */
422 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
424 if (t_check->to_state != s1)
425 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
428 /* 3. Rename s1 to {s1,s2} */
432 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
433 GNUNET_free (new_name);
437 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
439 automaton_destroy_state (s2);
444 * Add a state to the automaton 'a', always use this function to alter the
445 * states DLL of the automaton.
447 * @param a automaton to add the state to
448 * @param s state that should be added
451 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
452 struct GNUNET_REGEX_State *s)
454 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
460 * Depth-first traversal (DFS) of all states that are reachable from state
461 * 's'. Performs 'action' on each visited state.
463 * @param s start state.
464 * @param marks an array of size a->state_count to remember which state was
466 * @param count current count of the state.
467 * @param check function that is checked before advancing on each transition
469 * @param check_cls closure for check.
470 * @param action action to be performed on each state.
471 * @param action_cls closure for action.
474 automaton_state_traverse (struct GNUNET_REGEX_State *s, int *marks,
476 GNUNET_REGEX_traverse_check check, void *check_cls,
477 GNUNET_REGEX_traverse_action action, void *action_cls)
479 struct GNUNET_REGEX_Transition *t;
481 if (GNUNET_YES == marks[s->traversal_id])
484 marks[s->traversal_id] = GNUNET_YES;
487 action (action_cls, *count, s);
491 for (t = s->transitions_head; NULL != t; t = t->next)
494 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
496 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
504 * Traverses the given automaton using depth-first-search (DFS) from it's start
505 * state, visiting all reachable states and calling 'action' on each one of
508 * @param a automaton to be traversed.
509 * @param start start state, pass a->start or NULL to traverse the whole automaton.
510 * @param check function that is checked before advancing on each transition
512 * @param check_cls closure for check.
513 * @param action action to be performed on each state.
514 * @param action_cls closure for action
517 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
518 struct GNUNET_REGEX_State *start,
519 GNUNET_REGEX_traverse_check check,
521 GNUNET_REGEX_traverse_action action,
525 struct GNUNET_REGEX_State *s;
527 if (NULL == a || 0 == a->state_count)
530 int marks[a->state_count];
532 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
533 s = s->next, count++)
535 s->traversal_id = count;
536 marks[s->traversal_id] = GNUNET_NO;
546 automaton_state_traverse (s, marks, &count, check, check_cls, action,
552 * Check if the given string 'str' needs parentheses around it when
553 * using it to generate a regex.
557 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
560 needs_parentheses (const char *str)
568 if ((NULL == str) || ((slen = strlen (str)) < 2))
577 cl = strchr (pos, ')');
583 op = strchr (pos, '(');
584 if ((NULL != op) && (op < cl))
594 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
599 * Remove parentheses surrounding string 'str'.
600 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
601 * You need to GNUNET_free the returned string.
603 * @param str string, free'd or re-used by this function, can be NULL
605 * @return string without surrounding parentheses, string 'str' if no preceding
606 * epsilon could be found, NULL if 'str' was NULL
609 remove_parentheses (char *str)
614 if ((NULL == str) || ('(' != str[0]) ||
615 (str[(slen = strlen (str)) - 1] != ')'))
618 pos = strchr (&str[1], ')');
619 if (pos == &str[slen - 1])
621 memmove (str, &str[1], slen - 2);
622 str[slen - 2] = '\0';
629 * Check if the string 'str' starts with an epsilon (empty string).
630 * Example: "(|a)" is starting with an epsilon.
632 * @param str string to test
634 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
637 has_epsilon (const char *str)
639 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
640 (')' == str[strlen (str) - 1]);
645 * Remove an epsilon from the string str. Where epsilon is an empty string
646 * Example: str = "(|a|b|c)", result: "a|b|c"
647 * The returned string needs to be freed.
651 * @return string without preceding epsilon, string 'str' if no preceding
652 * epsilon could be found, NULL if 'str' was NULL
655 remove_epsilon (char *str)
661 if (('(' == str[0]) && ('|' == str[1]))
664 if (')' == str[len - 1])
665 return GNUNET_strndup (&str[2], len - 3);
667 return GNUNET_strdup (str);
672 * Compare 'str1', starting from position 'k', with whole 'str2'
674 * @param str1 first string to compare, starting from position 'k'
675 * @param str2 second string for comparison
676 * @param k starting position in 'str1'
678 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
681 strkcmp (const char *str1, const char *str2, size_t k)
683 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
685 return strcmp (&str1[k], str2);
690 * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse'
691 * function to create the depth-first numbering of the states.
693 * @param cls states array.
694 * @param count current state counter.
695 * @param s current state.
698 number_states (void *cls, const unsigned int count,
699 struct GNUNET_REGEX_State *s)
701 struct GNUNET_REGEX_State **states = cls;
710 * Construct the regular expression given the inductive step,
711 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
712 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
714 * @param R_last_ij value of $R^{(k-1)_{ij}.
715 * @param R_last_ik value of $R^{(k-1)_{ik}.
716 * @param R_last_kk value of $R^{(k-1)_{kk}.
717 * @param R_last_kj value of $R^{(k-1)_{kj}.
718 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
719 * is expected to be NULL when called!
722 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
723 char *R_last_kk, char *R_last_kj,
749 GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
751 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
752 // R_last == R^{(k-1)}, R_cur == R^{(k)}
753 // R_cur_ij = R_cur_l | R_cur_r
754 // R_cur_l == R^{(k-1)}_{ij}
755 // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
757 if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
758 (NULL == R_last_kj)))
760 /* R^{(k)}_{ij} = N | N */
765 if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
768 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
769 *R_cur_ij = GNUNET_strdup (R_last_ij);
773 // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
774 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
779 // cache results from strcmp, we might need these many times
780 ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
781 ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
782 ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
783 kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
785 // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
786 // as parentheses, so we can better compare the contents
787 R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
788 R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
789 R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
791 clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
792 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
794 // construct R_cur_l (and, if necessary R_cur_r)
795 if (NULL != R_last_ij)
797 // Assign R_temp_ij to R_last_ij and remove epsilon as well
798 // as parentheses, so we can better compare the contents
799 R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
801 if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
802 && 0 == strcmp (R_temp_kk, R_temp_kj))
804 if (0 == strlen (R_temp_ij))
806 R_cur_r = GNUNET_strdup ("");
808 else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
809 (0 == strncmp (R_last_ik, "(|", 2) &&
810 0 == strncmp (R_last_kj, "(|", 2)))
812 // a|(e|a)a*(e|a) = a*
813 // a|(e|a)(e|a)*(e|a) = a*
815 // (e|a)|aa*(e|a) = a*
816 // (e|a)|(e|a)a*a = a*
817 // (e|a)|(e|a)a*(e|a) = a*
818 // (e|a)|(e|a)(e|a)*(e|a) = a*
819 if (GNUNET_YES == needs_parentheses (R_temp_ij))
820 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
822 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
829 // a|(e|a)(e|a)*a = a+
830 // a|a(e|a)*(e|a) = a+
831 if (GNUNET_YES == needs_parentheses (R_temp_ij))
832 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
834 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
837 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
840 if (strlen (R_last_kk) < 1)
841 R_cur_r = GNUNET_strdup (R_last_ij);
842 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
843 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
845 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
849 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
852 if (strlen (R_last_kk) < 1)
853 R_cur_r = GNUNET_strdup (R_last_kj);
854 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
855 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
857 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
861 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
862 has_epsilon (R_last_kk))
864 // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
865 if (needs_parentheses (R_temp_kk))
866 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
868 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
872 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
873 has_epsilon (R_last_kk))
875 // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
876 if (needs_parentheses (R_temp_kk))
877 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
879 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
885 temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
886 temp_a = remove_parentheses (temp_a);
890 GNUNET_free_non_null (R_temp_ij);
894 // we have no left side
898 // construct R_cur_r, if not already constructed
901 length = strlen (R_temp_kk) - strlen (R_last_ik);
904 if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
905 NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
906 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
907 0 == strncmp (R_temp_kk, R_last_kj, length))
909 temp_a = GNUNET_malloc (length + 1);
910 temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
915 for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
919 temp_a[length_l] = R_last_kj[cnt];
924 temp_b[length_r] = R_last_kj[cnt];
928 temp_a[length_l] = '\0';
929 temp_b[length_r] = '\0';
932 if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
934 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
935 GNUNET_free (R_cur_l);
940 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
942 GNUNET_free (temp_a);
943 GNUNET_free (temp_b);
945 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
946 0 == strcmp (R_temp_kk, R_temp_kj))
949 // (e|a)(e|a)*(e|a) = a*
950 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
952 if (needs_parentheses (R_temp_kk))
953 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
955 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
958 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
959 !has_epsilon (R_last_ik))
961 if (needs_parentheses (R_temp_kk))
962 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
964 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
973 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
974 has_epsilon (R_last_kj));
978 if (needs_parentheses (R_temp_kk))
979 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
981 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
986 // (e|a)(e|a)*b = a*b
987 else if (0 == strcmp (R_temp_ik, R_temp_kk))
989 if (has_epsilon (R_last_ik))
991 if (needs_parentheses (R_temp_kk))
992 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
994 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
998 if (needs_parentheses (R_temp_kk))
999 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
1001 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
1005 // b(e|a)*(e|a) = ba*
1006 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1008 if (has_epsilon (R_last_kj))
1010 if (needs_parentheses (R_temp_kk))
1011 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
1013 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
1017 if (needs_parentheses (R_temp_kk))
1018 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
1020 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
1025 if (strlen (R_temp_kk) > 0)
1027 if (needs_parentheses (R_temp_kk))
1029 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1034 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1040 GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1045 GNUNET_free_non_null (R_temp_ik);
1046 GNUNET_free_non_null (R_temp_kk);
1047 GNUNET_free_non_null (R_temp_kj);
1049 if (NULL == R_cur_l && NULL == R_cur_r)
1055 if (NULL != R_cur_l && NULL == R_cur_r)
1057 *R_cur_ij = R_cur_l;
1061 if (NULL == R_cur_l && NULL != R_cur_r)
1063 *R_cur_ij = R_cur_r;
1067 if (0 == nullstrcmp (R_cur_l, R_cur_r))
1069 *R_cur_ij = R_cur_l;
1070 GNUNET_free (R_cur_r);
1074 GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1076 GNUNET_free (R_cur_l);
1077 GNUNET_free (R_cur_r);
1082 * create proofs for all states in the given automaton. Implementation of the
1083 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1084 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1086 * @param a automaton.
1089 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1094 "Could not create proofs, automaton was NULL\n");
1098 unsigned int n = a->state_count;
1099 struct GNUNET_REGEX_State *states[n];
1103 struct GNUNET_REGEX_Transition *t;
1104 char *complete_regex;
1109 R_last = GNUNET_malloc_large (sizeof (char *) * n * n);
1110 R_cur = GNUNET_malloc_large (sizeof (char *) * n * n);
1112 /* create depth-first numbering of the states, initializes 'state' */
1113 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1116 for (i = 0; i < n; i++)
1117 GNUNET_assert (NULL != states[i]);
1119 /* Compute regular expressions of length "1" between each pair of states */
1120 for (i = 0; i < n; i++)
1122 for (j = 0; j < n; j++)
1124 R_cur[i * n + j] = NULL;
1125 R_last[i * n + j] = NULL;
1127 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1129 j = t->to_state->dfs_id;
1130 if (NULL == R_last[i * n + j])
1131 GNUNET_asprintf (&R_last[i * n + j], "%s", t->label);
1134 temp = R_last[i * n + j];
1135 GNUNET_asprintf (&R_last[i * n + j], "%s|%s", R_last[i * n + j], t->label);
1139 if (NULL == R_last[i*n+i])
1140 GNUNET_asprintf (&R_last[i*n+i], "");
1143 temp = R_last[i*n+i];
1144 GNUNET_asprintf (&R_last[i*n+i], "(|%s)", R_last[i*n+i]);
1148 for (i = 0; i < n; i++)
1149 for (j = 0; j < n; j++)
1150 if (needs_parentheses (R_last[i * n + j]))
1152 temp = R_last[i * n + j];
1153 GNUNET_asprintf (&R_last[i * n + j], "(%s)", R_last[i * n + j]);
1157 /* Compute regular expressions of length "k" between each pair of states per
1159 for (k = 0; k < n; k++)
1161 for (i = 0; i < n; i++)
1163 for (j = 0; j < n; j++)
1165 // Basis for the recursion:
1166 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1167 // R_last == R^{(k-1)}, R_cur == R^{(k)}
1169 // Create R_cur[i][j] and simplify the expression
1170 automaton_create_proofs_simplify (R_last[i * n + j], R_last[i*n+k],
1171 R_last[k*n+k], R_last[k*n+j],
1176 // set R_last = R_cur
1177 for (i = 0; i < n; i++)
1179 for (j = 0; j < n; j++)
1181 GNUNET_free_non_null (R_last[i * n + j]);
1182 R_last[i * n + j] = R_cur[i * n + j];
1183 R_cur[i * n + j] = NULL;
1188 // assign proofs and hashes
1189 for (i = 0; i < n; i++)
1191 if (NULL != R_last[a->start->dfs_id*n+i])
1193 states[i]->proof = GNUNET_strdup (R_last[a->start->dfs_id*n+i]);
1194 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1199 // complete regex for whole DFA: union of all pairs (start state/accepting
1201 complete_regex = NULL;
1202 for (i = 0; i < n; i++)
1204 if (states[i]->accepting)
1206 if (NULL == complete_regex && 0 < strlen (R_last[a->start->dfs_id*n+i]))
1208 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->dfs_id*n+i]);
1210 else if (NULL != R_last[a->start->dfs_id*n+i] &&
1211 0 < strlen (R_last[a->start->dfs_id*n+i]))
1213 temp = complete_regex;
1214 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1215 R_last[a->start->dfs_id*n+i]);
1220 a->canonical_regex = complete_regex;
1223 for (i = 0; i < n; i++)
1225 for (j = 0; j < n; j++)
1226 GNUNET_free_non_null (R_last[i * n + j]);
1228 GNUNET_free (R_cur);
1229 GNUNET_free (R_last);
1234 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1235 * automaton_destroy_state.
1237 * @param ctx context
1238 * @param nfa_states set of NFA states on which the DFA should be based on
1240 * @return new DFA state
1242 static struct GNUNET_REGEX_State *
1243 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1244 struct GNUNET_REGEX_StateSet *nfa_states)
1246 struct GNUNET_REGEX_State *s;
1249 struct GNUNET_REGEX_State *cstate;
1250 struct GNUNET_REGEX_Transition *ctran;
1253 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1254 s->id = ctx->state_id++;
1256 s->marked = GNUNET_NO;
1264 if (NULL == nfa_states)
1266 GNUNET_asprintf (&s->name, "s%i", s->id);
1270 s->nfa_set = nfa_states;
1272 if (nfa_states->len < 1)
1275 // Create a name based on 'nfa_states'
1276 s->name = GNUNET_malloc (sizeof (char) * 2);
1277 strcat (s->name, "{");
1280 for (i = 0; i < nfa_states->len; i++)
1282 cstate = nfa_states->states[i];
1283 GNUNET_asprintf (&name, "%i,", cstate->id);
1287 len = strlen (s->name) + strlen (name) + 1;
1288 s->name = GNUNET_realloc (s->name, len);
1289 strcat (s->name, name);
1294 // Add a transition for each distinct label to NULL state
1295 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1297 if (NULL != ctran->label)
1298 state_add_transition (ctx, s, ctran->label, NULL);
1301 // If the nfa_states contain an accepting state, the new dfa state is also
1303 if (cstate->accepting)
1307 s->name[strlen (s->name) - 1] = '}';
1314 * Move from the given state 's' to the next state on transition 'str'. Consumes
1315 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1316 * 's' will point to the next state, and the length of the substring used for
1317 * this transition will be returned. If no transition possible 0 is returned and
1318 * 's' points to NULL.
1320 * @param s starting state, will point to the next state or NULL (if no
1321 * transition possible)
1322 * @param str edge label to follow (will match longest common prefix)
1324 * @return length of the substring comsumed from 'str'
1327 dfa_move (struct GNUNET_REGEX_State **s, const char *str)
1329 struct GNUNET_REGEX_Transition *t;
1330 struct GNUNET_REGEX_State *new_s;
1332 unsigned int max_len;
1339 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1341 len = strlen (t->label);
1343 if (0 == strncmp (t->label, str, len))
1348 new_s = t->to_state;
1358 * Set the given state 'marked' to GNUNET_YES. Used by the
1359 * 'dfa_remove_unreachable_states' function to detect unreachable states in the
1362 * @param cls closure, not used.
1363 * @param count count, not used.
1364 * @param s state where the marked attribute will be set to GNUNET_YES.
1367 mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
1369 s->marked = GNUNET_YES;
1373 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1374 * states that are not reachable from the starting state.
1376 * @param a DFA automaton
1379 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1381 struct GNUNET_REGEX_State *s;
1382 struct GNUNET_REGEX_State *s_next;
1384 // 1. unmark all states
1385 for (s = a->states_head; NULL != s; s = s->next)
1386 s->marked = GNUNET_NO;
1388 // 2. traverse dfa from start state and mark all visited states
1389 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1391 // 3. delete all states that were not visited
1392 for (s = a->states_head; NULL != s; s = s_next)
1395 if (GNUNET_NO == s->marked)
1396 automaton_remove_state (a, s);
1402 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1403 * not transition to any other state but themselves.
1405 * @param a DFA automaton
1408 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1410 struct GNUNET_REGEX_State *s;
1411 struct GNUNET_REGEX_State *s_next;
1412 struct GNUNET_REGEX_Transition *t;
1415 GNUNET_assert (DFA == a->type);
1417 for (s = a->states_head; NULL != s; s = s_next)
1425 for (t = s->transitions_head; NULL != t; t = t->next)
1427 if (NULL != t->to_state && t->to_state != s)
1437 // state s is dead, remove it
1438 automaton_remove_state (a, s);
1444 * Merge all non distinguishable states in the DFA 'a'
1446 * @param ctx context
1447 * @param a DFA automaton
1450 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1451 struct GNUNET_REGEX_Automaton *a)
1454 struct GNUNET_REGEX_State *s1;
1455 struct GNUNET_REGEX_State *s2;
1456 struct GNUNET_REGEX_Transition *t1;
1457 struct GNUNET_REGEX_Transition *t2;
1458 struct GNUNET_REGEX_State *s1_next;
1459 struct GNUNET_REGEX_State *s2_next;
1461 unsigned int num_equal_edges;
1463 unsigned int state_cnt;
1465 if (NULL == a || 0 == a->state_count)
1467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1468 "Could not merge nondistinguishable states, automaton was NULL.\n");
1472 state_cnt = a->state_count;
1473 table = (int *)GNUNET_malloc_large (sizeof (int) * state_cnt * a->state_count);
1475 for (i = 0, s1 = a->states_head; i < state_cnt && NULL != s1;
1481 /* Mark all pairs of accepting/!accepting states */
1482 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1484 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1486 table[((s1->marked * state_cnt) + s2->marked)] = 0;
1488 if ((s1->accepting && !s2->accepting) ||
1489 (!s1->accepting && s2->accepting))
1491 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1496 /* Find all equal states */
1501 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1503 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1505 if (0 != table[((s1->marked * state_cnt) + s2->marked)])
1508 num_equal_edges = 0;
1509 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1511 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1513 if (0 == strcmp (t1->label, t2->label))
1516 if (0 != table[((t1->to_state->marked * state_cnt) + t2->to_state->marked)] ||
1517 0 != table[((t2->to_state->marked * state_cnt) + t1->to_state->marked)])
1519 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1525 if (num_equal_edges != s1->transition_count ||
1526 num_equal_edges != s2->transition_count)
1528 /* Make sure ALL edges of possible equal states are the same */
1529 table[((s1->marked * state_cnt) + s2->marked)] = -2;
1535 /* Merge states that are equal */
1536 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1539 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1542 if (0 == table[((s1->marked * state_cnt) + s2->marked)])
1543 automaton_merge_states (ctx, a, s1, s2);
1547 GNUNET_free (table);
1552 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1553 * dead states and merging all non distinguishable states
1555 * @param ctx context
1556 * @param a DFA automaton
1559 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1560 struct GNUNET_REGEX_Automaton *a)
1565 GNUNET_assert (DFA == a->type);
1567 // 1. remove unreachable states
1568 dfa_remove_unreachable_states (a);
1570 // 2. remove dead states
1571 dfa_remove_dead_states (a);
1573 // 3. Merge nondistinguishable states
1574 dfa_merge_nondistinguishable_states (ctx, a);
1579 * Context for adding strided transitions to a DFA.
1581 struct GNUNET_REGEX_Strided_Context
1584 * Length of the strides.
1586 const unsigned int stride;
1589 * Strided transitions DLL. New strided transitions will be stored in this DLL
1590 * and afterwards added to the DFA.
1592 struct GNUNET_REGEX_Transition *transitions_head;
1595 * Strided transitions DLL.
1597 struct GNUNET_REGEX_Transition *transitions_tail;
1602 * Recursive helper function to add strides to a DFA.
1604 * @param cls context, contains stride length and strided transitions DLL.
1605 * @param depth current depth of the depth-first traversal of the graph.
1606 * @param label current label, string that contains all labels on the path from
1608 * @param start start state for the depth-first traversal of the graph.
1609 * @param s current state in the depth-first traversal
1612 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1613 struct GNUNET_REGEX_State *start,
1614 struct GNUNET_REGEX_State *s)
1616 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1617 struct GNUNET_REGEX_Transition *t;
1620 if (depth == ctx->stride)
1622 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1623 t->label = GNUNET_strdup (label);
1625 t->from_state = start;
1626 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1631 for (t = s->transitions_head; NULL != t; t = t->next)
1633 /* Do not consider self-loops, because it end's up in too many
1635 if (t->to_state == t->from_state)
1640 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1643 new_label = GNUNET_strdup (t->label);
1645 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1649 GNUNET_free_non_null (label);
1654 * Function called for each state in the DFA. Starts a traversal of depth set in
1655 * context starting from state 's'.
1657 * @param cls context.
1658 * @param count not used.
1659 * @param s current state.
1662 dfa_add_multi_strides (void *cls, const unsigned int count,
1663 struct GNUNET_REGEX_State *s)
1665 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1670 * Adds multi-strided transitions to the given 'dfa'.
1672 * @param regex_ctx regex context needed to add transitions to the automaton.
1673 * @param dfa DFA to which the multi strided transitions should be added.
1674 * @param stride_len length of the strides.
1677 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1678 struct GNUNET_REGEX_Automaton *dfa,
1679 const unsigned int stride_len)
1681 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1682 struct GNUNET_REGEX_Transition *t;
1683 struct GNUNET_REGEX_Transition *t_next;
1685 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1688 // Compute the new transitions of given stride_len
1689 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1690 &dfa_add_multi_strides, &ctx);
1692 // Add all the new transitions to the automaton.
1693 for (t = ctx.transitions_head; NULL != t; t = t_next)
1696 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1697 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1698 GNUNET_free_non_null (t->label);
1702 // Mark this automaton as multistrided
1703 dfa->is_multistrided = GNUNET_YES;
1707 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1708 * and adds new transitions to the given transitions DLL and marks states that
1709 * should be removed by setting state->contained to GNUNET_YES.
1711 * @param dfa DFA for which the paths should be compressed.
1712 * @param start starting state for linear path search.
1713 * @param cur current state in the recursive DFS.
1714 * @param label current label (string of traversed labels).
1715 * @param max_len maximal path compression length.
1716 * @param transitions_head transitions DLL.
1717 * @param transitions_tail transitions DLL.
1720 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1721 struct GNUNET_REGEX_State *start,
1722 struct GNUNET_REGEX_State *cur, char *label,
1723 unsigned int max_len,
1724 struct GNUNET_REGEX_Transition **transitions_head,
1725 struct GNUNET_REGEX_Transition **transitions_tail)
1727 struct GNUNET_REGEX_Transition *t;
1731 if (NULL != label &&
1732 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting
1733 // || cur->transition_count > 1
1734 || GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1735 max_len == strlen (label)) ||
1736 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
1738 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1739 t->label = GNUNET_strdup (label);
1741 t->from_state = start;
1742 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1744 if (GNUNET_NO == cur->marked)
1746 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1751 else if (cur != start)
1752 cur->contained = GNUNET_YES;
1754 if (GNUNET_YES == cur->marked && cur != start)
1757 cur->marked = GNUNET_YES;
1760 for (t = cur->transitions_head; NULL != t; t = t->next)
1763 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1765 new_label = GNUNET_strdup (t->label);
1767 if (t->to_state != cur)
1769 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1770 transitions_head, transitions_tail);
1772 GNUNET_free (new_label);
1777 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1778 * compressed to 0->3 by combining transitions.
1780 * @param regex_ctx context for adding new transitions.
1781 * @param dfa DFA representation, will directly modify the given DFA.
1782 * @param max_len maximal length of the compressed paths.
1785 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1786 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1788 struct GNUNET_REGEX_State *s;
1789 struct GNUNET_REGEX_State *s_next;
1790 struct GNUNET_REGEX_Transition *t;
1791 struct GNUNET_REGEX_Transition *t_next;
1792 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1793 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1798 // Count the incoming transitions on each state.
1799 for (s = dfa->states_head; NULL != s; s = s->next)
1801 for (t = s->transitions_head; NULL != t; t = t->next)
1803 if (NULL != t->to_state)
1804 t->to_state->incoming_transition_count++;
1808 // Unmark all states.
1809 for (s = dfa->states_head; NULL != s; s = s->next)
1811 s->marked = GNUNET_NO;
1812 s->contained = GNUNET_NO;
1815 // Add strides and mark states that can be deleted.
1816 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1817 &transitions_head, &transitions_tail);
1819 // Add all the new transitions to the automaton.
1820 for (t = transitions_head; NULL != t; t = t_next)
1823 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1824 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1825 GNUNET_free_non_null (t->label);
1829 // Remove marked states (including their incoming and outgoing transitions).
1830 for (s = dfa->states_head; NULL != s; s = s_next)
1833 if (GNUNET_YES == s->contained)
1834 automaton_remove_state (dfa, s);
1840 * Creates a new NFA fragment. Needs to be cleared using
1841 * automaton_fragment_clear.
1843 * @param start starting state
1844 * @param end end state
1846 * @return new NFA fragment
1848 static struct GNUNET_REGEX_Automaton *
1849 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1850 struct GNUNET_REGEX_State *end)
1852 struct GNUNET_REGEX_Automaton *n;
1854 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1861 if (NULL == start || NULL == end)
1864 automaton_add_state (n, end);
1865 automaton_add_state (n, start);
1877 * Adds a list of states to the given automaton 'n'.
1879 * @param n automaton to which the states should be added
1880 * @param states_head head of the DLL of states
1881 * @param states_tail tail of the DLL of states
1884 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1885 struct GNUNET_REGEX_State *states_head,
1886 struct GNUNET_REGEX_State *states_tail)
1888 struct GNUNET_REGEX_State *s;
1890 if (NULL == n || NULL == states_head)
1892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1896 if (NULL == n->states_head)
1898 n->states_head = states_head;
1899 n->states_tail = states_tail;
1903 if (NULL != states_head)
1905 n->states_tail->next = states_head;
1906 n->states_tail = states_tail;
1909 for (s = states_head; NULL != s; s = s->next)
1915 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1917 * @param ctx context
1918 * @param accepting is it an accepting state or not
1920 * @return new NFA state
1922 static struct GNUNET_REGEX_State *
1923 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1925 struct GNUNET_REGEX_State *s;
1927 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1928 s->id = ctx->state_id++;
1929 s->accepting = accepting;
1930 s->marked = GNUNET_NO;
1936 GNUNET_asprintf (&s->name, "s%i", s->id);
1943 * Calculates the NFA closure set for the given state.
1945 * @param nfa the NFA containing 's'
1946 * @param s starting point state
1947 * @param label transitioning label on which to base the closure on,
1948 * pass NULL for epsilon transition
1950 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1952 static struct GNUNET_REGEX_StateSet *
1953 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1954 struct GNUNET_REGEX_State *s, const char *label)
1957 struct GNUNET_REGEX_StateSet *cls;
1958 struct GNUNET_REGEX_StateSet_MDLL cls_check;
1959 struct GNUNET_REGEX_State *clsstate;
1960 struct GNUNET_REGEX_State *currentstate;
1961 struct GNUNET_REGEX_Transition *ctran;
1966 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1967 cls_check.head = NULL;
1968 cls_check.tail = NULL;
1970 // Add start state to closure only for epsilon closure
1972 GNUNET_array_append (cls->states, cls->len, s);
1974 GNUNET_CONTAINER_MDLL_insert (SS, cls_check.head, cls_check.tail, s);
1977 while (cls_check.len > 0)
1979 currentstate = cls_check.tail;
1980 GNUNET_CONTAINER_MDLL_remove (SS, cls_check.head, cls_check.tail, currentstate);
1983 for (ctran = currentstate->transitions_head; NULL != ctran;
1984 ctran = ctran->next)
1986 if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
1988 clsstate = ctran->to_state;
1990 if (NULL != clsstate && 0 == clsstate->contained)
1992 GNUNET_array_append (cls->states, cls->len, clsstate);
1993 GNUNET_CONTAINER_MDLL_insert_tail (SS, cls_check.head, cls_check.tail, clsstate);
1995 clsstate->contained = 1;
2001 for (i = 0; i < cls->len; i++)
2002 cls->states[i]->contained = 0;
2006 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2014 * Calculates the closure set for the given set of states.
2016 * @param nfa the NFA containing 's'
2017 * @param states list of states on which to base the closure on
2018 * @param label transitioning label for which to base the closure on,
2019 * pass NULL for epsilon transition
2021 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2023 static struct GNUNET_REGEX_StateSet *
2024 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
2025 struct GNUNET_REGEX_StateSet *states, const char *label)
2027 struct GNUNET_REGEX_State *s;
2028 struct GNUNET_REGEX_StateSet *sset;
2029 struct GNUNET_REGEX_StateSet *cls;
2033 unsigned int contains;
2038 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2040 for (i = 0; i < states->len; i++)
2042 s = states->states[i];
2043 sset = nfa_closure_create (nfa, s, label);
2045 for (j = 0; j < sset->len; j++)
2048 for (k = 0; k < cls->len; k++)
2050 if (sset->states[j]->id == cls->states[k]->id)
2057 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2059 state_set_clear (sset);
2063 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2071 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2073 * @param ctx context
2076 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2078 struct GNUNET_REGEX_Automaton *a;
2079 struct GNUNET_REGEX_Automaton *b;
2080 struct GNUNET_REGEX_Automaton *new_nfa;
2082 b = ctx->stack_tail;
2083 GNUNET_assert (NULL != b);
2084 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2085 a = ctx->stack_tail;
2086 GNUNET_assert (NULL != a);
2087 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2089 state_add_transition (ctx, a->end, NULL, b->start);
2090 a->end->accepting = 0;
2091 b->end->accepting = 1;
2093 new_nfa = nfa_fragment_create (NULL, NULL);
2094 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2095 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2096 new_nfa->start = a->start;
2097 new_nfa->end = b->end;
2098 new_nfa->state_count += a->state_count + b->state_count;
2099 automaton_fragment_clear (a);
2100 automaton_fragment_clear (b);
2102 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2107 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2109 * @param ctx context
2112 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2114 struct GNUNET_REGEX_Automaton *a;
2115 struct GNUNET_REGEX_Automaton *new_nfa;
2116 struct GNUNET_REGEX_State *start;
2117 struct GNUNET_REGEX_State *end;
2119 a = ctx->stack_tail;
2123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2124 "nfa_add_star_op failed, because there was no element on the stack");
2128 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2130 start = nfa_state_create (ctx, 0);
2131 end = nfa_state_create (ctx, 1);
2133 state_add_transition (ctx, start, NULL, a->start);
2134 state_add_transition (ctx, start, NULL, end);
2135 state_add_transition (ctx, a->end, NULL, a->start);
2136 state_add_transition (ctx, a->end, NULL, end);
2138 a->end->accepting = 0;
2141 new_nfa = nfa_fragment_create (start, end);
2142 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2143 automaton_fragment_clear (a);
2145 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2150 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2152 * @param ctx context
2155 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2157 struct GNUNET_REGEX_Automaton *a;
2159 a = ctx->stack_tail;
2163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2164 "nfa_add_plus_op failed, because there was no element on the stack");
2168 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2170 state_add_transition (ctx, a->end, NULL, a->start);
2172 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2177 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2179 * @param ctx context
2182 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2184 struct GNUNET_REGEX_Automaton *a;
2185 struct GNUNET_REGEX_Automaton *new_nfa;
2186 struct GNUNET_REGEX_State *start;
2187 struct GNUNET_REGEX_State *end;
2189 a = ctx->stack_tail;
2193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2194 "nfa_add_question_op failed, because there was no element on the stack");
2198 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2200 start = nfa_state_create (ctx, 0);
2201 end = nfa_state_create (ctx, 1);
2203 state_add_transition (ctx, start, NULL, a->start);
2204 state_add_transition (ctx, start, NULL, end);
2205 state_add_transition (ctx, a->end, NULL, end);
2207 a->end->accepting = 0;
2209 new_nfa = nfa_fragment_create (start, end);
2210 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2211 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2212 automaton_fragment_clear (a);
2217 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2218 * alternates between a and b (a|b)
2220 * @param ctx context
2223 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2225 struct GNUNET_REGEX_Automaton *a;
2226 struct GNUNET_REGEX_Automaton *b;
2227 struct GNUNET_REGEX_Automaton *new_nfa;
2228 struct GNUNET_REGEX_State *start;
2229 struct GNUNET_REGEX_State *end;
2231 b = ctx->stack_tail;
2232 GNUNET_assert (NULL != b);
2233 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2234 a = ctx->stack_tail;
2235 GNUNET_assert (NULL != a);
2236 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2238 start = nfa_state_create (ctx, 0);
2239 end = nfa_state_create (ctx, 1);
2240 state_add_transition (ctx, start, NULL, a->start);
2241 state_add_transition (ctx, start, NULL, b->start);
2243 state_add_transition (ctx, a->end, NULL, end);
2244 state_add_transition (ctx, b->end, NULL, end);
2246 a->end->accepting = 0;
2247 b->end->accepting = 0;
2250 new_nfa = nfa_fragment_create (start, end);
2251 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2252 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2253 automaton_fragment_clear (a);
2254 automaton_fragment_clear (b);
2256 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2261 * Adds a new nfa fragment to the stack
2263 * @param ctx context
2264 * @param label label for nfa transition
2267 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2269 struct GNUNET_REGEX_Automaton *n;
2270 struct GNUNET_REGEX_State *start;
2271 struct GNUNET_REGEX_State *end;
2273 GNUNET_assert (NULL != ctx);
2275 start = nfa_state_create (ctx, 0);
2276 end = nfa_state_create (ctx, 1);
2277 state_add_transition (ctx, start, label, end);
2278 n = nfa_fragment_create (start, end);
2279 GNUNET_assert (NULL != n);
2280 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2285 * Initialize a new context
2287 * @param ctx context
2290 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2298 ctx->transition_id = 0;
2299 ctx->stack_head = NULL;
2300 ctx->stack_tail = NULL;
2305 * Construct an NFA by parsing the regex string of length 'len'.
2307 * @param regex regular expression string
2308 * @param len length of the string
2310 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2312 struct GNUNET_REGEX_Automaton *
2313 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2315 struct GNUNET_REGEX_Context ctx;
2316 struct GNUNET_REGEX_Automaton *nfa;
2321 unsigned int altcount;
2322 unsigned int atomcount;
2323 unsigned int pcount;
2330 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2333 "Could not parse regex. Empty regex string provided.\n");
2338 GNUNET_REGEX_context_init (&ctx);
2348 for (count = 0; count < len && *regexp; count++, regexp++)
2356 nfa_add_concatenation (&ctx);
2358 GNUNET_array_grow (p, pcount, pcount + 1);
2359 p[pcount - 1].altcount = altcount;
2360 p[pcount - 1].atomcount = atomcount;
2367 error_msg = "Cannot append '|' to nothing";
2370 while (--atomcount > 0)
2371 nfa_add_concatenation (&ctx);
2377 error_msg = "Missing opening '('";
2382 // Ignore this: "()"
2384 altcount = p[pcount].altcount;
2385 atomcount = p[pcount].atomcount;
2388 while (--atomcount > 0)
2389 nfa_add_concatenation (&ctx);
2390 for (; altcount > 0; altcount--)
2391 nfa_add_alternation (&ctx);
2393 altcount = p[pcount].altcount;
2394 atomcount = p[pcount].atomcount;
2400 error_msg = "Cannot append '*' to nothing";
2403 nfa_add_star_op (&ctx);
2408 error_msg = "Cannot append '+' to nothing";
2411 nfa_add_plus_op (&ctx);
2416 error_msg = "Cannot append '?' to nothing";
2419 nfa_add_question_op (&ctx);
2425 nfa_add_concatenation (&ctx);
2427 curlabel[0] = *regexp;
2428 nfa_add_label (&ctx, curlabel);
2435 error_msg = "Unbalanced parenthesis";
2438 while (--atomcount > 0)
2439 nfa_add_concatenation (&ctx);
2440 for (; altcount > 0; altcount--)
2441 nfa_add_alternation (&ctx);
2443 GNUNET_free_non_null (p);
2445 nfa = ctx.stack_tail;
2446 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2448 if (NULL != ctx.stack_head)
2450 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2454 /* Remember the regex that was used to generate this NFA */
2455 nfa->regex = GNUNET_strdup (regex);
2457 /* create depth-first numbering of the states for pretty printing */
2458 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2460 /* No multistriding added so far */
2461 nfa->is_multistrided = GNUNET_NO;
2466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2467 if (NULL != error_msg)
2468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2470 GNUNET_free_non_null (p);
2472 while (NULL != (nfa = ctx.stack_head))
2474 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2475 GNUNET_REGEX_automaton_destroy (nfa);
2483 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2485 * @param ctx context.
2486 * @param nfa NFA automaton.
2487 * @param dfa DFA automaton.
2488 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2492 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2493 struct GNUNET_REGEX_Automaton *nfa,
2494 struct GNUNET_REGEX_Automaton *dfa,
2495 struct GNUNET_REGEX_State *dfa_state)
2497 struct GNUNET_REGEX_Transition *ctran;
2498 struct GNUNET_REGEX_State *state_iter;
2499 struct GNUNET_REGEX_State *new_dfa_state;
2500 struct GNUNET_REGEX_State *state_contains;
2501 struct GNUNET_REGEX_StateSet *tmp;
2502 struct GNUNET_REGEX_StateSet *nfa_set;
2504 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2506 if (NULL == ctran->label || NULL != ctran->to_state)
2509 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2510 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2511 state_set_clear (tmp);
2512 new_dfa_state = dfa_state_create (ctx, nfa_set);
2513 state_contains = NULL;
2514 for (state_iter = dfa->states_head; NULL != state_iter;
2515 state_iter = state_iter->next)
2517 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2518 state_contains = state_iter;
2521 if (NULL == state_contains)
2523 automaton_add_state (dfa, new_dfa_state);
2524 ctran->to_state = new_dfa_state;
2525 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2529 ctran->to_state = state_contains;
2530 automaton_destroy_state (new_dfa_state);
2536 * Construct DFA for the given 'regex' of length 'len'.
2538 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
2539 * compressed to o -> abc -> o. Note that this parameter influences the
2540 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
2541 * edges with the same label). For example for an application that stores IPv4
2542 * addresses as bitstrings it could make sense to limit the path compression to
2545 * @param regex regular expression string.
2546 * @param len length of the regular expression.
2547 * @param max_path_len limit the path compression length to the
2548 * given value. If set to 1, no path compression is applied. Set to 0 for
2549 * maximal possible path compression (generally not desireable).
2550 * @return DFA, needs to be freed using GNUNET_REGEX_automaton_destroy.
2552 struct GNUNET_REGEX_Automaton *
2553 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len,
2556 struct GNUNET_REGEX_Context ctx;
2557 struct GNUNET_REGEX_Automaton *dfa;
2558 struct GNUNET_REGEX_Automaton *nfa;
2559 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2561 GNUNET_REGEX_context_init (&ctx);
2564 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2569 "Could not create DFA, because NFA creation failed\n");
2573 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2575 dfa->state_count = 0;
2576 dfa->states_head = NULL;
2577 dfa->states_tail = NULL;
2578 dfa->regex = GNUNET_strdup (regex);
2579 dfa->is_multistrided = GNUNET_NO;
2581 // Create DFA start state from epsilon closure
2582 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, 0);
2583 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2584 automaton_add_state (dfa, dfa->start);
2586 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2588 GNUNET_REGEX_automaton_destroy (nfa);
2591 dfa_minimize (&ctx, dfa);
2593 // Create proofs for all states
2594 automaton_create_proofs (dfa);
2596 // Compress DFA paths
2597 if (1 != max_path_len)
2598 dfa_compress_paths (&ctx, dfa, max_path_len);
2600 // Add strides to DFA
2601 //GNUNET_REGEX_dfa_add_multi_strides (&ctx, dfa, 2);
2608 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2611 * @param a automaton to be destroyed
2614 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2616 struct GNUNET_REGEX_State *s;
2617 struct GNUNET_REGEX_State *next_state;
2622 GNUNET_free_non_null (a->regex);
2623 GNUNET_free_non_null (a->canonical_regex);
2625 for (s = a->states_head; NULL != s; s = next_state)
2627 next_state = s->next;
2628 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2629 automaton_destroy_state (s);
2637 * Evaluates the given string using the given DFA automaton
2639 * @param a automaton, type must be DFA
2640 * @param string string that should be evaluated
2642 * @return 0 if string matches, non 0 otherwise
2645 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2648 struct GNUNET_REGEX_State *s;
2649 unsigned int step_len;
2653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2654 "Tried to evaluate DFA, but NFA automaton given");
2660 // If the string is empty but the starting state is accepting, we accept.
2661 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2664 for (strp = string; NULL != strp && *strp; strp += step_len)
2666 step_len = dfa_move (&s, strp);
2672 if (NULL != s && s->accepting)
2680 * Evaluates the given string using the given NFA automaton
2682 * @param a automaton, type must be NFA
2683 * @param string string that should be evaluated
2685 * @return 0 if string matches, non 0 otherwise
2688 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2692 struct GNUNET_REGEX_State *s;
2693 struct GNUNET_REGEX_StateSet *sset;
2694 struct GNUNET_REGEX_StateSet *new_sset;
2700 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2701 "Tried to evaluate NFA, but DFA automaton given");
2705 // If the string is empty but the starting state is accepting, we accept.
2706 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2710 sset = nfa_closure_create (a, a->start, 0);
2713 for (strp = string; NULL != strp && *strp; strp++)
2716 new_sset = nfa_closure_set_create (a, sset, str);
2717 state_set_clear (sset);
2718 sset = nfa_closure_set_create (a, new_sset, 0);
2719 state_set_clear (new_sset);
2722 for (i = 0; i < sset->len; i++)
2724 s = sset->states[i];
2725 if (NULL != s && s->accepting)
2732 state_set_clear (sset);
2738 * Evaluates the given 'string' against the given compiled regex
2740 * @param a automaton
2741 * @param string string to check
2743 * @return 0 if string matches, non 0 otherwise
2746 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2753 result = evaluate_dfa (a, string);
2756 result = evaluate_nfa (a, string);
2759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2760 "Evaluating regex failed, automaton has no type!\n");
2761 result = GNUNET_SYSERR;
2770 * Get the canonical regex of the given automaton.
2771 * When constructing the automaton a proof is computed for each state,
2772 * consisting of the regular expression leading to this state. A complete
2773 * regex for the automaton can be computed by combining these proofs.
2774 * As of now this function is only useful for testing.
2776 * @param a automaton for which the canonical regex should be returned.
2781 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2786 return a->canonical_regex;
2791 * Get the number of transitions that are contained in the given automaton.
2793 * @param a automaton for which the number of transitions should be returned.
2795 * @return number of transitions in the given automaton.
2798 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2800 unsigned int t_count;
2801 struct GNUNET_REGEX_State *s;
2807 for (s = a->states_head; NULL != s; s = s->next)
2808 t_count += s->transition_count;
2815 * Get the first key for the given 'input_string'. This hashes the first x bits
2816 * of the 'input_string'.
2818 * @param input_string string.
2819 * @param string_len length of the 'input_string'.
2820 * @param key pointer to where to write the hash code.
2822 * @return number of bits of 'input_string' that have been consumed
2823 * to construct the key
2826 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2827 struct GNUNET_HashCode * key)
2833 GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES;
2835 if (NULL == input_string)
2837 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2841 GNUNET_CRYPTO_hash (input_string, size, key);
2848 * Check if the given 'proof' matches the given 'key'.
2850 * @param proof partial regex of a state.
2851 * @param key hash of a state.
2853 * @return GNUNET_OK if the proof is valid for the given key.
2856 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2858 struct GNUNET_HashCode key_check;
2860 if (NULL == proof || NULL == key)
2862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2866 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2868 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2873 * Recursive function that calls the iterator for each synthetic start state.
2875 * @param min_len minimum length of the path in the graph.
2876 * @param max_len maximum length of the path in the graph.
2877 * @param consumed_string string consumed by traversing the graph till this state.
2878 * @param state current state of the automaton.
2879 * @param iterator iterator function called for each edge.
2880 * @param iterator_cls closure for the iterator function.
2883 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2884 char *consumed_string, struct GNUNET_REGEX_State *state,
2885 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2889 struct GNUNET_REGEX_Transition *t;
2890 unsigned int num_edges = state->transition_count;
2891 struct GNUNET_REGEX_Edge edges[num_edges];
2892 struct GNUNET_REGEX_Edge edge[1];
2893 struct GNUNET_HashCode hash;
2894 struct GNUNET_HashCode hash_new;
2896 unsigned int cur_len;
2898 if (NULL != consumed_string)
2899 cur_len = strlen (consumed_string);
2903 if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
2904 NULL != consumed_string)
2906 if (cur_len <= max_len)
2908 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2910 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2913 edges[i].label = t->label;
2914 edges[i].destination = t->to_state->hash;
2916 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2917 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2921 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2922 state->transition_count < 1 && cur_len < max_len)
2924 // Special case for regex consisting of just a string that is shorter than
2926 edge[0].label = &consumed_string[cur_len - 1];
2927 edge[0].destination = state->hash;
2928 temp = GNUNET_strdup (consumed_string);
2929 temp[cur_len - 1] = '\0';
2930 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2931 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2935 else if (max_len < cur_len)
2937 // Case where the concatenated labels are longer than max_len, then split.
2938 edge[0].label = &consumed_string[max_len];
2939 edge[0].destination = state->hash;
2940 temp = GNUNET_strdup (consumed_string);
2941 temp[max_len] = '\0';
2942 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2943 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2948 if (cur_len < max_len)
2950 for (t = state->transitions_head; NULL != t; t = t->next)
2952 if (NULL != consumed_string)
2953 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2955 GNUNET_asprintf (&temp, "%s", t->label);
2957 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2966 * Iterate over all edges starting from start state of automaton 'a'. Calling
2967 * iterator for each edge.
2969 * @param a automaton.
2970 * @param iterator iterator called for each edge.
2971 * @param iterator_cls closure.
2974 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2975 GNUNET_REGEX_KeyIterator iterator,
2978 struct GNUNET_REGEX_State *s;
2980 for (s = a->states_head; NULL != s; s = s->next)
2982 struct GNUNET_REGEX_Edge edges[s->transition_count];
2983 unsigned int num_edges;
2985 num_edges = state_get_edges (s, edges);
2987 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
2988 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
2991 s->marked = GNUNET_NO;
2994 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, GNUNET_REGEX_INITIAL_BYTES,
2995 NULL, a->start, iterator, iterator_cls);
3000 * Create a string with binary IP notation for the given 'addr' in 'str'.
3002 * @param af address family of the given 'addr'.
3003 * @param addr address that should be converted to a string.
3004 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
3005 * @param str string that will contain binary notation of 'addr'. Expected
3006 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
3009 iptobinstr (const int af, const void *addr, char *str)
3017 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
3021 for (i = 31; i >= 0; i--)
3023 *str = (b & 1) + '0';
3031 struct in6_addr b = *(const struct in6_addr *) addr;
3035 for (i = 127; i >= 0; i--)
3037 *str = (b.s6_addr[i / 8] & 1) + '0';
3039 b.s6_addr[i / 8] >>= 1;
3048 * Get the ipv4 network prefix from the given 'netmask'.
3050 * @param netmask netmask for which to get the prefix len.
3052 * @return length of ipv4 prefix for 'netmask'.
3055 ipv4netmasktoprefixlen (const char *netmask)
3061 if (1 != inet_pton (AF_INET, netmask, &a))
3064 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3071 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3073 * @param ip IPv4 representation.
3074 * @param netmask netmask for the ip.
3075 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3079 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3082 unsigned int pfxlen;
3084 pfxlen = ipv4netmasktoprefixlen (netmask);
3085 iptobinstr (AF_INET, ip, rxstr);
3086 rxstr[pfxlen] = '\0';
3088 strcat (rxstr, "(0|1)+");
3093 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3095 * @param ipv6 IPv6 representation.
3096 * @param prefixlen length of the ipv6 prefix.
3097 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3101 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3104 iptobinstr (AF_INET6, ipv6, rxstr);
3105 rxstr[prefixlen] = '\0';
3106 if (prefixlen < 128)
3107 strcat (rxstr, "(0|1)+");