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 Deterministic Finite Automatons (DFAs) from regular
23 * expressions (regexes). Used by mesh for announcing regexes in the network and
24 * matching strings against published regexes.
25 * @author Maximilian Szengel
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_regex_lib.h"
31 #include "regex_internal.h"
34 * Set this to GNUNET_YES to enable state naming. Used to debug NFA->DFA
35 * creation. Disabled by default for better performance.
37 #define REGEX_DEBUG_DFA GNUNET_NO
42 struct GNUNET_REGEX_StateSet
47 struct GNUNET_REGEX_State **states;
50 * Length of the 'states' array.
56 * Set of states using MDLL API.
58 struct GNUNET_REGEX_StateSet_MDLL
63 struct GNUNET_REGEX_State *head;
68 struct GNUNET_REGEX_State *tail;
78 * Compare two strings for equality. If either is NULL they are not equal.
80 * @param str1 first string for comparison.
81 * @param str2 second string for comparison.
83 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
86 nullstrcmp (const char *str1, const char *str2)
88 if ((NULL == str1) != (NULL == str2))
90 if ((NULL == str1) && (NULL == str2))
93 return strcmp (str1, str2);
98 * Adds a transition from one state to another on 'label'. Does not add
102 * @param from_state starting state for the transition
103 * @param label transition label
104 * @param to_state state to where the transition should point to
107 state_add_transition (struct GNUNET_REGEX_Context *ctx,
108 struct GNUNET_REGEX_State *from_state, const char *label,
109 struct GNUNET_REGEX_State *to_state)
111 struct GNUNET_REGEX_Transition *t;
112 struct GNUNET_REGEX_Transition *oth;
114 if (NULL == from_state)
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
120 /* Do not add duplicate state transitions */
121 for (t = from_state->transitions_head; NULL != t; t = t->next)
123 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
124 t->from_state == from_state)
128 /* sort transitions by label */
129 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
131 if (0 < nullstrcmp (oth->label, label))
135 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
137 t->id = ctx->transition_id++;
139 t->label = GNUNET_strdup (label);
142 t->to_state = to_state;
143 t->from_state = from_state;
145 /* Add outgoing transition to 'from_state' */
146 from_state->transition_count++;
147 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
148 from_state->transitions_tail, oth, t);
153 * Remove a 'transition' from 'state'.
155 * @param state state from which the to-be-removed transition originates.
156 * @param transition transition that should be removed from state 'state'.
159 state_remove_transition (struct GNUNET_REGEX_State *state,
160 struct GNUNET_REGEX_Transition *transition)
162 if (NULL == state || NULL == transition)
165 if (transition->from_state != state)
168 GNUNET_free_non_null (transition->label);
170 state->transition_count--;
171 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
174 GNUNET_free (transition);
179 * Compare two states. Used for sorting.
181 * @param a first state
182 * @param b second state
184 * @return an integer less than, equal to, or greater than zero
185 * if the first argument is considered to be respectively
186 * less than, equal to, or greater than the second.
189 state_compare (const void *a, const void *b)
191 struct GNUNET_REGEX_State **s1 = (struct GNUNET_REGEX_State **) a;
192 struct GNUNET_REGEX_State **s2 = (struct GNUNET_REGEX_State **) b;
194 return (*s1)->id - (*s2)->id;
199 * Get all edges leaving state 's'.
202 * @param edges all edges leaving 's', expected to be allocated and have enough
203 * space for s->transitions_count elements.
205 * @return number of edges.
208 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
210 struct GNUNET_REGEX_Transition *t;
218 for (t = s->transitions_head; NULL != t; t = t->next)
220 if (NULL != t->to_state)
222 edges[count].label = t->label;
223 edges[count].destination = t->to_state->hash;
232 * Compare to state sets by comparing the id's of the states that are contained
233 * in each set. Both sets are expected to be sorted by id!
235 * @param sset1 first state set
236 * @param sset2 second state set
237 * @return 0 if the sets are equal, otherwise non-zero
240 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
241 struct GNUNET_REGEX_StateSet *sset2)
246 if (NULL == sset1 || NULL == sset2)
249 result = sset1->len - sset2->len;
254 for (i = 0; i < sset1->len; i++)
255 if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i])))
262 * Clears the given StateSet 'set'
264 * @param set set to be cleared
267 state_set_clear (struct GNUNET_REGEX_StateSet *set)
273 GNUNET_array_grow (set->states, set->len, 0);
279 * Clears an automaton fragment. Does not destroy the states inside the
282 * @param a automaton to be cleared
285 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
292 a->states_head = NULL;
293 a->states_tail = NULL;
300 * Frees the memory used by State 's'
302 * @param s state that should be destroyed
305 automaton_destroy_state (struct GNUNET_REGEX_State *s)
307 struct GNUNET_REGEX_Transition *t;
308 struct GNUNET_REGEX_Transition *next_t;
313 GNUNET_free_non_null (s->name);
314 GNUNET_free_non_null (s->proof);
315 state_set_clear (s->nfa_set);
317 for (t = s->transitions_head; NULL != t; t = next_t)
320 state_remove_transition (s, t);
328 * Remove a state from the given automaton 'a'. Always use this function when
329 * altering the states of an automaton. Will also remove all transitions leading
330 * to this state, before destroying it.
333 * @param s state to remove
336 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
337 struct GNUNET_REGEX_State *s)
339 struct GNUNET_REGEX_State *s_check;
340 struct GNUNET_REGEX_Transition *t_check;
341 struct GNUNET_REGEX_Transition *t_check_next;
343 if (NULL == a || NULL == s)
346 /* remove all transitions leading to this state */
347 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
349 for (t_check = s_check->transitions_head; NULL != t_check;
350 t_check = t_check_next)
352 t_check_next = t_check->next;
353 if (t_check->to_state == s)
354 state_remove_transition (s_check, t_check);
359 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
362 automaton_destroy_state (s);
367 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
368 * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
372 * @param s1 first state
373 * @param s2 second state, will be destroyed
376 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
377 struct GNUNET_REGEX_Automaton *a,
378 struct GNUNET_REGEX_State *s1,
379 struct GNUNET_REGEX_State *s2)
381 struct GNUNET_REGEX_State *s_check;
382 struct GNUNET_REGEX_Transition *t_check;
383 struct GNUNET_REGEX_Transition *t;
384 struct GNUNET_REGEX_Transition *t_next;
387 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
392 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
393 * does not already exists, if it already exists remove transition. */
394 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
396 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
398 t_next = t_check->next;
400 if (s2 == t_check->to_state)
403 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
405 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
408 if (GNUNET_NO == is_dup)
409 t_check->to_state = s1;
411 state_remove_transition (t_check->from_state, t_check);
416 /* 2. Add all transitions from s2 to sX to s1 */
417 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
419 if (t_check->to_state != s1)
420 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
423 /* 3. Rename s1 to {s1,s2} */
428 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
429 GNUNET_free (new_name);
433 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
435 automaton_destroy_state (s2);
440 * Add a state to the automaton 'a', always use this function to alter the
441 * states DLL of the automaton.
443 * @param a automaton to add the state to
444 * @param s state that should be added
447 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
448 struct GNUNET_REGEX_State *s)
450 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
456 * Depth-first traversal (DFS) of all states that are reachable from state
457 * 's'. Performs 'action' on each visited state.
459 * @param s start state.
460 * @param marks an array of size a->state_count to remember which state was
462 * @param count current count of the state.
463 * @param check function that is checked before advancing on each transition
465 * @param check_cls closure for check.
466 * @param action action to be performed on each state.
467 * @param action_cls closure for action.
470 automaton_state_traverse (struct GNUNET_REGEX_State *s, int *marks,
472 GNUNET_REGEX_traverse_check check, void *check_cls,
473 GNUNET_REGEX_traverse_action action, void *action_cls)
475 struct GNUNET_REGEX_Transition *t;
477 if (GNUNET_YES == marks[s->traversal_id])
480 marks[s->traversal_id] = GNUNET_YES;
483 action (action_cls, *count, s);
487 for (t = s->transitions_head; NULL != t; t = t->next)
490 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
492 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
500 * Traverses the given automaton using depth-first-search (DFS) from it's start
501 * state, visiting all reachable states and calling 'action' on each one of
504 * @param a automaton to be traversed.
505 * @param start start state, pass a->start or NULL to traverse the whole automaton.
506 * @param check function that is checked before advancing on each transition
508 * @param check_cls closure for check.
509 * @param action action to be performed on each state.
510 * @param action_cls closure for action
513 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
514 struct GNUNET_REGEX_State *start,
515 GNUNET_REGEX_traverse_check check,
517 GNUNET_REGEX_traverse_action action,
521 struct GNUNET_REGEX_State *s;
523 if (NULL == a || 0 == a->state_count)
526 int marks[a->state_count];
528 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
529 s = s->next, count++)
531 s->traversal_id = count;
532 marks[s->traversal_id] = GNUNET_NO;
542 automaton_state_traverse (s, marks, &count, check, check_cls, action,
548 * Check if the given string 'str' needs parentheses around it when
549 * using it to generate a regex.
553 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
556 needs_parentheses (const char *str)
564 if ((NULL == str) || ((slen = strlen (str)) < 2))
573 cl = strchr (pos, ')');
579 op = strchr (pos, '(');
580 if ((NULL != op) && (op < cl))
590 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
595 * Remove parentheses surrounding string 'str'.
596 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
597 * You need to GNUNET_free the returned string.
599 * @param str string, free'd or re-used by this function, can be NULL
601 * @return string without surrounding parentheses, string 'str' if no preceding
602 * epsilon could be found, NULL if 'str' was NULL
605 remove_parentheses (char *str)
610 if ((NULL == str) || ('(' != str[0]) ||
611 (str[(slen = strlen (str)) - 1] != ')'))
614 pos = strchr (&str[1], ')');
615 if (pos == &str[slen - 1])
617 memmove (str, &str[1], slen - 2);
618 str[slen - 2] = '\0';
625 * Check if the string 'str' starts with an epsilon (empty string).
626 * Example: "(|a)" is starting with an epsilon.
628 * @param str string to test
630 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
633 has_epsilon (const char *str)
635 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
636 (')' == str[strlen (str) - 1]);
641 * Remove an epsilon from the string str. Where epsilon is an empty string
642 * Example: str = "(|a|b|c)", result: "a|b|c"
643 * The returned string needs to be freed.
647 * @return string without preceding epsilon, string 'str' if no preceding
648 * epsilon could be found, NULL if 'str' was NULL
651 remove_epsilon (char *str)
657 if (('(' == str[0]) && ('|' == str[1]))
660 if (')' == str[len - 1])
661 return GNUNET_strndup (&str[2], len - 3);
663 return GNUNET_strdup (str);
668 * Compare 'str1', starting from position 'k', with whole 'str2'
670 * @param str1 first string to compare, starting from position 'k'
671 * @param str2 second string for comparison
672 * @param k starting position in 'str1'
674 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
677 strkcmp (const char *str1, const char *str2, size_t k)
679 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
681 return strcmp (&str1[k], str2);
686 * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse'
687 * function to create the depth-first numbering of the states.
689 * @param cls states array.
690 * @param count current state counter.
691 * @param s current state.
694 number_states (void *cls, const unsigned int count,
695 struct GNUNET_REGEX_State *s)
697 struct GNUNET_REGEX_State **states = cls;
706 * Construct the regular expression given the inductive step,
707 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
708 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
710 * @param R_last_ij value of $R^{(k-1)_{ij}.
711 * @param R_last_ik value of $R^{(k-1)_{ik}.
712 * @param R_last_kk value of $R^{(k-1)_{kk}.
713 * @param R_last_kj value of $R^{(k-1)_{kj}.
714 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
715 * is expected to be NULL when called!
718 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
719 char *R_last_kk, char *R_last_kj,
745 GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
747 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
748 * R_last == R^{(k-1)}, R_cur == R^{(k)}
749 * R_cur_ij = R_cur_l | R_cur_r
750 * R_cur_l == R^{(k-1)}_{ij}
751 * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
754 if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
755 (NULL == R_last_kj)))
757 /* R^{(k)}_{ij} = N | N */
762 if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
765 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
766 *R_cur_ij = GNUNET_strdup (R_last_ij);
770 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
771 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
776 /* cache results from strcmp, we might need these many times */
777 ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
778 ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
779 ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
780 kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
782 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
783 * as parentheses, so we can better compare the contents */
784 R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
785 R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
786 R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
788 clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
789 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
791 /* construct R_cur_l (and, if necessary R_cur_r) */
792 if (NULL != R_last_ij)
794 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
795 * as parentheses, so we can better compare the contents */
796 R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
798 if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
799 && 0 == strcmp (R_temp_kk, R_temp_kj))
801 if (0 == strlen (R_temp_ij))
803 R_cur_r = GNUNET_strdup ("");
805 else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
806 (0 == strncmp (R_last_ik, "(|", 2) &&
807 0 == strncmp (R_last_kj, "(|", 2)))
810 * a|(e|a)a*(e|a) = a*
811 * a|(e|a)(e|a)*(e|a) = a*
813 * (e|a)|aa*(e|a) = a*
814 * (e|a)|(e|a)a*a = a*
815 * (e|a)|(e|a)a*(e|a) = a*
816 * (e|a)|(e|a)(e|a)*(e|a) = a*
818 if (GNUNET_YES == needs_parentheses (R_temp_ij))
819 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
821 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+
832 if (GNUNET_YES == needs_parentheses (R_temp_ij))
833 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
835 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
838 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
841 if (strlen (R_last_kk) < 1)
842 R_cur_r = GNUNET_strdup (R_last_ij);
843 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
844 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
846 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
850 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
853 if (strlen (R_last_kk) < 1)
854 R_cur_r = GNUNET_strdup (R_last_kj);
855 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
856 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
858 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
862 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
863 has_epsilon (R_last_kk))
865 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
866 if (needs_parentheses (R_temp_kk))
867 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
869 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
873 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
874 has_epsilon (R_last_kk))
876 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
877 if (needs_parentheses (R_temp_kk))
878 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
880 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
886 temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
887 temp_a = remove_parentheses (temp_a);
891 GNUNET_free_non_null (R_temp_ij);
895 /* we have no left side */
899 /* construct R_cur_r, if not already constructed */
902 length = strlen (R_temp_kk) - strlen (R_last_ik);
904 /* a(ba)*bx = (ab)+x */
905 if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
906 NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
907 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
908 0 == strncmp (R_temp_kk, R_last_kj, length))
910 temp_a = GNUNET_malloc (length + 1);
911 temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
916 for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
920 temp_a[length_l] = R_last_kj[cnt];
925 temp_b[length_r] = R_last_kj[cnt];
929 temp_a[length_l] = '\0';
930 temp_b[length_r] = '\0';
932 /* e|(ab)+ = (ab)* */
933 if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
935 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
936 GNUNET_free (R_cur_l);
941 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
943 GNUNET_free (temp_a);
944 GNUNET_free (temp_b);
946 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
947 0 == strcmp (R_temp_kk, R_temp_kj))
951 * (e|a)(e|a)*(e|a) = a*
953 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
955 if (needs_parentheses (R_temp_kk))
956 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
958 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
961 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
962 !has_epsilon (R_last_ik))
964 if (needs_parentheses (R_temp_kk))
965 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
967 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
978 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
979 has_epsilon (R_last_kj));
983 if (needs_parentheses (R_temp_kk))
984 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
986 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
994 else if (0 == strcmp (R_temp_ik, R_temp_kk))
996 if (has_epsilon (R_last_ik))
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 if (needs_parentheses (R_temp_kk))
1006 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
1008 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
1013 * b(e|a)*(e|a) = ba*
1015 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1017 if (has_epsilon (R_last_kj))
1019 if (needs_parentheses (R_temp_kk))
1020 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
1022 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
1026 if (needs_parentheses (R_temp_kk))
1027 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
1029 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
1034 if (strlen (R_temp_kk) > 0)
1036 if (needs_parentheses (R_temp_kk))
1038 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1043 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1049 GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1054 GNUNET_free_non_null (R_temp_ik);
1055 GNUNET_free_non_null (R_temp_kk);
1056 GNUNET_free_non_null (R_temp_kj);
1058 if (NULL == R_cur_l && NULL == R_cur_r)
1064 if (NULL != R_cur_l && NULL == R_cur_r)
1066 *R_cur_ij = R_cur_l;
1070 if (NULL == R_cur_l && NULL != R_cur_r)
1072 *R_cur_ij = R_cur_r;
1076 if (0 == nullstrcmp (R_cur_l, R_cur_r))
1078 *R_cur_ij = R_cur_l;
1079 GNUNET_free (R_cur_r);
1083 GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1085 GNUNET_free (R_cur_l);
1086 GNUNET_free (R_cur_r);
1091 * Create proofs for all states in the given automaton. Implementation of the
1092 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1093 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1095 * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1096 * proof) fields. The starting state will only have a valid proof/hash if it has
1097 * any incoming transitions.
1099 * @param a automaton for which to assign proofs and hashes.
1102 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1107 "Could not create proofs, automaton was NULL\n");
1111 unsigned int n = a->state_count;
1112 struct GNUNET_REGEX_State *states[n];
1116 struct GNUNET_REGEX_Transition *t;
1117 char *complete_regex;
1122 R_last = GNUNET_malloc_large (sizeof (char *) * n * n);
1123 R_cur = GNUNET_malloc_large (sizeof (char *) * n * n);
1125 /* create depth-first numbering of the states, initializes 'state' */
1126 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1129 for (i = 0; i < n; i++)
1130 GNUNET_assert (NULL != states[i]);
1132 /* Compute regular expressions of length "1" between each pair of states */
1133 for (i = 0; i < n; i++)
1135 for (j = 0; j < n; j++)
1137 R_cur[i * n + j] = NULL;
1138 R_last[i * n + j] = NULL;
1140 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1142 j = t->to_state->dfs_id;
1143 if (NULL == R_last[i * n + j])
1144 GNUNET_asprintf (&R_last[i * n + j], "%s", t->label);
1147 temp = R_last[i * n + j];
1148 GNUNET_asprintf (&R_last[i * n + j], "%s|%s", R_last[i * n + j],
1153 if (NULL == R_last[i * n + i])
1154 GNUNET_asprintf (&R_last[i * n + i], "");
1157 temp = R_last[i * n + i];
1158 GNUNET_asprintf (&R_last[i * n + i], "(|%s)", R_last[i * n + i]);
1162 for (i = 0; i < n; i++)
1163 for (j = 0; j < n; j++)
1164 if (needs_parentheses (R_last[i * n + j]))
1166 temp = R_last[i * n + j];
1167 GNUNET_asprintf (&R_last[i * n + j], "(%s)", R_last[i * n + j]);
1171 /* Compute regular expressions of length "k" between each pair of states per
1173 for (k = 0; k < n; k++)
1175 for (i = 0; i < n; i++)
1177 for (j = 0; j < n; j++)
1179 /* Basis for the recursion:
1180 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1181 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1184 /* Create R_cur[i][j] and simplify the expression */
1185 automaton_create_proofs_simplify (R_last[i * n + j], R_last[i * n + k],
1186 R_last[k * n + k], R_last[k * n + j],
1191 /* set R_last = R_cur */
1192 for (i = 0; i < n; i++)
1194 for (j = 0; j < n; j++)
1196 GNUNET_free_non_null (R_last[i * n + j]);
1197 R_last[i * n + j] = R_cur[i * n + j];
1198 R_cur[i * n + j] = NULL;
1203 /* assign proofs and hashes */
1204 for (i = 0; i < n; i++)
1206 if (NULL != R_last[a->start->dfs_id * n + i])
1208 states[i]->proof = GNUNET_strdup (R_last[a->start->dfs_id * n + i]);
1209 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1214 /* complete regex for whole DFA: union of all pairs (start state/accepting
1216 complete_regex = NULL;
1217 for (i = 0; i < n; i++)
1219 if (states[i]->accepting)
1221 if (NULL == complete_regex &&
1222 0 < strlen (R_last[a->start->dfs_id * n + i]))
1224 GNUNET_asprintf (&complete_regex, "%s",
1225 R_last[a->start->dfs_id * n + i]);
1227 else if (NULL != R_last[a->start->dfs_id * n + i] &&
1228 0 < strlen (R_last[a->start->dfs_id * n + i]))
1230 temp = complete_regex;
1231 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1232 R_last[a->start->dfs_id * n + i]);
1237 a->canonical_regex = complete_regex;
1240 for (i = 0; i < n; i++)
1242 for (j = 0; j < n; j++)
1243 GNUNET_free_non_null (R_last[i * n + j]);
1245 GNUNET_free (R_cur);
1246 GNUNET_free (R_last);
1251 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1252 * automaton_destroy_state.
1254 * @param ctx context
1255 * @param nfa_states set of NFA states on which the DFA should be based on
1257 * @return new DFA state
1259 static struct GNUNET_REGEX_State *
1260 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1261 struct GNUNET_REGEX_StateSet *nfa_states)
1263 struct GNUNET_REGEX_State *s;
1266 struct GNUNET_REGEX_State *cstate;
1267 struct GNUNET_REGEX_Transition *ctran;
1270 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1271 s->id = ctx->state_id++;
1275 if (NULL == nfa_states)
1277 GNUNET_asprintf (&s->name, "s%i", s->id);
1281 s->nfa_set = nfa_states;
1283 if (nfa_states->len < 1)
1286 /* Create a name based on 'nfa_states' */
1287 /* FIXME: insanely costly string operations here! */
1288 s->name = GNUNET_malloc (sizeof (char) * 2);
1289 strcat (s->name, "{");
1292 for (i = 0; i < nfa_states->len; i++)
1294 cstate = nfa_states->states[i];
1295 GNUNET_asprintf (&name, "%i,", cstate->id);
1297 len = strlen (s->name) + strlen (name) + 1;
1298 s->name = GNUNET_realloc (s->name, len);
1299 strcat (s->name, name);
1303 /* Add a transition for each distinct label to NULL state */
1304 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1305 if (NULL != ctran->label)
1306 state_add_transition (ctx, s, ctran->label, NULL);
1308 /* If the nfa_states contain an accepting state, the new dfa state is also
1310 if (cstate->accepting)
1314 s->name[strlen (s->name) - 1] = '}';
1321 * Move from the given state 's' to the next state on transition 'str'. Consumes
1322 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1323 * 's' will point to the next state, and the length of the substring used for
1324 * this transition will be returned. If no transition possible 0 is returned and
1325 * 's' points to NULL.
1327 * @param s starting state, will point to the next state or NULL (if no
1328 * transition possible)
1329 * @param str edge label to follow (will match longest common prefix)
1331 * @return length of the substring comsumed from 'str'
1334 dfa_move (struct GNUNET_REGEX_State **s, const char *str)
1336 struct GNUNET_REGEX_Transition *t;
1337 struct GNUNET_REGEX_State *new_s;
1339 unsigned int max_len;
1346 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1348 len = strlen (t->label);
1350 if (0 == strncmp (t->label, str, len))
1355 new_s = t->to_state;
1365 * Set the given state 'marked' to GNUNET_YES. Used by the
1366 * 'dfa_remove_unreachable_states' function to detect unreachable states in the
1369 * @param cls closure, not used.
1370 * @param count count, not used.
1371 * @param s state where the marked attribute will be set to GNUNET_YES.
1374 mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
1376 s->marked = GNUNET_YES;
1380 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1381 * states that are not reachable from the starting state.
1383 * @param a DFA automaton
1386 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1388 struct GNUNET_REGEX_State *s;
1389 struct GNUNET_REGEX_State *s_next;
1391 /* 1. unmark all states */
1392 for (s = a->states_head; NULL != s; s = s->next)
1393 s->marked = GNUNET_NO;
1395 /* 2. traverse dfa from start state and mark all visited states */
1396 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1398 /* 3. delete all states that were not visited */
1399 for (s = a->states_head; NULL != s; s = s_next)
1402 if (GNUNET_NO == s->marked)
1403 automaton_remove_state (a, s);
1409 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1410 * not transition to any other state but themselves.
1412 * @param a DFA automaton
1415 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1417 struct GNUNET_REGEX_State *s;
1418 struct GNUNET_REGEX_State *s_next;
1419 struct GNUNET_REGEX_Transition *t;
1422 GNUNET_assert (DFA == a->type);
1424 for (s = a->states_head; NULL != s; s = s_next)
1432 for (t = s->transitions_head; NULL != t; t = t->next)
1434 if (NULL != t->to_state && t->to_state != s)
1444 /* state s is dead, remove it */
1445 automaton_remove_state (a, s);
1451 * Merge all non distinguishable states in the DFA 'a'
1453 * @param ctx context
1454 * @param a DFA automaton
1457 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1458 struct GNUNET_REGEX_Automaton *a)
1461 struct GNUNET_REGEX_State *s1;
1462 struct GNUNET_REGEX_State *s2;
1463 struct GNUNET_REGEX_Transition *t1;
1464 struct GNUNET_REGEX_Transition *t2;
1465 struct GNUNET_REGEX_State *s1_next;
1466 struct GNUNET_REGEX_State *s2_next;
1468 unsigned int num_equal_edges;
1470 unsigned int state_cnt;
1472 if (NULL == a || 0 == a->state_count)
1474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1475 "Could not merge nondistinguishable states, automaton was NULL.\n");
1479 state_cnt = a->state_count;
1481 (int *) GNUNET_malloc_large (sizeof (int) * state_cnt * a->state_count);
1483 for (i = 0, s1 = a->states_head; i < state_cnt && NULL != s1;
1489 /* Mark all pairs of accepting/!accepting states */
1490 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1492 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1494 table[((s1->marked * state_cnt) + s2->marked)] = 0;
1496 if ((s1->accepting && !s2->accepting) ||
1497 (!s1->accepting && s2->accepting))
1499 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1504 /* Find all equal states */
1509 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1511 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1513 if (0 != table[((s1->marked * state_cnt) + s2->marked)])
1516 num_equal_edges = 0;
1517 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1519 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1521 if (0 == strcmp (t1->label, t2->label))
1525 table[((t1->to_state->marked * state_cnt) +
1526 t2->to_state->marked)] ||
1528 table[((t2->to_state->marked * state_cnt) +
1529 t1->to_state->marked)])
1531 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1537 if (num_equal_edges != s1->transition_count ||
1538 num_equal_edges != s2->transition_count)
1540 /* Make sure ALL edges of possible equal states are the same */
1541 table[((s1->marked * state_cnt) + s2->marked)] = -2;
1547 /* Merge states that are equal */
1548 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1551 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1554 if (0 == table[((s1->marked * state_cnt) + s2->marked)])
1555 automaton_merge_states (ctx, a, s1, s2);
1559 GNUNET_free (table);
1564 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1565 * dead states and merging all non distinguishable states
1567 * @param ctx context
1568 * @param a DFA automaton
1571 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1572 struct GNUNET_REGEX_Automaton *a)
1577 GNUNET_assert (DFA == a->type);
1579 /* 1. remove unreachable states */
1580 dfa_remove_unreachable_states (a);
1582 /* 2. remove dead states */
1583 dfa_remove_dead_states (a);
1585 /* 3. Merge nondistinguishable states */
1586 dfa_merge_nondistinguishable_states (ctx, a);
1591 * Context for adding strided transitions to a DFA.
1593 struct GNUNET_REGEX_Strided_Context
1596 * Length of the strides.
1598 const unsigned int stride;
1601 * Strided transitions DLL. New strided transitions will be stored in this DLL
1602 * and afterwards added to the DFA.
1604 struct GNUNET_REGEX_Transition *transitions_head;
1607 * Strided transitions DLL.
1609 struct GNUNET_REGEX_Transition *transitions_tail;
1614 * Recursive helper function to add strides to a DFA.
1616 * @param cls context, contains stride length and strided transitions DLL.
1617 * @param depth current depth of the depth-first traversal of the graph.
1618 * @param label current label, string that contains all labels on the path from
1620 * @param start start state for the depth-first traversal of the graph.
1621 * @param s current state in the depth-first traversal
1624 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1625 struct GNUNET_REGEX_State *start,
1626 struct GNUNET_REGEX_State *s)
1628 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1629 struct GNUNET_REGEX_Transition *t;
1632 if (depth == ctx->stride)
1634 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1635 t->label = GNUNET_strdup (label);
1637 t->from_state = start;
1638 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1643 for (t = s->transitions_head; NULL != t; t = t->next)
1645 /* Do not consider self-loops, because it end's up in too many
1647 if (t->to_state == t->from_state)
1652 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1655 new_label = GNUNET_strdup (t->label);
1657 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1661 GNUNET_free_non_null (label);
1666 * Function called for each state in the DFA. Starts a traversal of depth set in
1667 * context starting from state 's'.
1669 * @param cls context.
1670 * @param count not used.
1671 * @param s current state.
1674 dfa_add_multi_strides (void *cls, const unsigned int count,
1675 struct GNUNET_REGEX_State *s)
1677 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1682 * Adds multi-strided transitions to the given 'dfa'.
1684 * @param regex_ctx regex context needed to add transitions to the automaton.
1685 * @param dfa DFA to which the multi strided transitions should be added.
1686 * @param stride_len length of the strides.
1689 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1690 struct GNUNET_REGEX_Automaton *dfa,
1691 const unsigned int stride_len)
1693 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1694 struct GNUNET_REGEX_Transition *t;
1695 struct GNUNET_REGEX_Transition *t_next;
1697 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1700 /* Compute the new transitions of given stride_len */
1701 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1702 &dfa_add_multi_strides, &ctx);
1704 /* Add all the new transitions to the automaton. */
1705 for (t = ctx.transitions_head; NULL != t; t = t_next)
1708 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1709 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1710 GNUNET_free_non_null (t->label);
1714 /* Mark this automaton as multistrided */
1715 dfa->is_multistrided = GNUNET_YES;
1719 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1720 * and adds new transitions to the given transitions DLL and marks states that
1721 * should be removed by setting state->contained to GNUNET_YES.
1723 * @param dfa DFA for which the paths should be compressed.
1724 * @param start starting state for linear path search.
1725 * @param cur current state in the recursive DFS.
1726 * @param label current label (string of traversed labels).
1727 * @param max_len maximal path compression length.
1728 * @param transitions_head transitions DLL.
1729 * @param transitions_tail transitions DLL.
1732 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1733 struct GNUNET_REGEX_State *start,
1734 struct GNUNET_REGEX_State *cur, char *label,
1735 unsigned int max_len,
1736 struct GNUNET_REGEX_Transition **transitions_head,
1737 struct GNUNET_REGEX_Transition **transitions_tail)
1739 struct GNUNET_REGEX_Transition *t;
1743 if (NULL != label &&
1744 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
1745 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1746 max_len == strlen (label)) ||
1747 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
1749 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1750 t->label = GNUNET_strdup (label);
1752 t->from_state = start;
1753 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1755 if (GNUNET_NO == cur->marked)
1757 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1762 else if (cur != start)
1763 cur->contained = GNUNET_YES;
1765 if (GNUNET_YES == cur->marked && cur != start)
1768 cur->marked = GNUNET_YES;
1771 for (t = cur->transitions_head; NULL != t; t = t->next)
1774 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1776 new_label = GNUNET_strdup (t->label);
1778 if (t->to_state != cur)
1780 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1781 transitions_head, transitions_tail);
1783 GNUNET_free (new_label);
1788 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1789 * compressed to 0->3 by combining transitions.
1791 * @param regex_ctx context for adding new transitions.
1792 * @param dfa DFA representation, will directly modify the given DFA.
1793 * @param max_len maximal length of the compressed paths.
1796 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1797 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1799 struct GNUNET_REGEX_State *s;
1800 struct GNUNET_REGEX_State *s_next;
1801 struct GNUNET_REGEX_Transition *t;
1802 struct GNUNET_REGEX_Transition *t_next;
1803 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1804 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1809 /* Count the incoming transitions on each state. */
1810 for (s = dfa->states_head; NULL != s; s = s->next)
1812 for (t = s->transitions_head; NULL != t; t = t->next)
1814 if (NULL != t->to_state)
1815 t->to_state->incoming_transition_count++;
1819 /* Unmark all states. */
1820 for (s = dfa->states_head; NULL != s; s = s->next)
1822 s->marked = GNUNET_NO;
1823 s->contained = GNUNET_NO;
1826 /* Add strides and mark states that can be deleted. */
1827 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1828 &transitions_head, &transitions_tail);
1830 /* Add all the new transitions to the automaton. */
1831 for (t = transitions_head; NULL != t; t = t_next)
1834 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1835 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1836 GNUNET_free_non_null (t->label);
1840 /* Remove marked states (including their incoming and outgoing transitions). */
1841 for (s = dfa->states_head; NULL != s; s = s_next)
1844 if (GNUNET_YES == s->contained)
1845 automaton_remove_state (dfa, s);
1851 * Creates a new NFA fragment. Needs to be cleared using
1852 * automaton_fragment_clear.
1854 * @param start starting state
1855 * @param end end state
1857 * @return new NFA fragment
1859 static struct GNUNET_REGEX_Automaton *
1860 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1861 struct GNUNET_REGEX_State *end)
1863 struct GNUNET_REGEX_Automaton *n;
1865 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1872 if (NULL == start || NULL == end)
1875 automaton_add_state (n, end);
1876 automaton_add_state (n, start);
1888 * Adds a list of states to the given automaton 'n'.
1890 * @param n automaton to which the states should be added
1891 * @param states_head head of the DLL of states
1892 * @param states_tail tail of the DLL of states
1895 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1896 struct GNUNET_REGEX_State *states_head,
1897 struct GNUNET_REGEX_State *states_tail)
1899 struct GNUNET_REGEX_State *s;
1901 if (NULL == n || NULL == states_head)
1903 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1907 if (NULL == n->states_head)
1909 n->states_head = states_head;
1910 n->states_tail = states_tail;
1914 if (NULL != states_head)
1916 n->states_tail->next = states_head;
1917 n->states_tail = states_tail;
1920 for (s = states_head; NULL != s; s = s->next)
1926 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1928 * @param ctx context
1929 * @param accepting is it an accepting state or not
1931 * @return new NFA state
1933 static struct GNUNET_REGEX_State *
1934 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1936 struct GNUNET_REGEX_State *s;
1938 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1939 s->id = ctx->state_id++;
1940 s->accepting = accepting;
1941 s->marked = GNUNET_NO;
1947 GNUNET_asprintf (&s->name, "s%i", s->id);
1954 * Calculates the NFA closure set for the given state.
1956 * @param nfa the NFA containing 's'
1957 * @param s starting point state
1958 * @param label transitioning label on which to base the closure on,
1959 * pass NULL for epsilon transition
1961 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1963 static struct GNUNET_REGEX_StateSet *
1964 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1965 struct GNUNET_REGEX_State *s, const char *label)
1968 struct GNUNET_REGEX_StateSet *cls;
1969 struct GNUNET_REGEX_StateSet_MDLL cls_stack;
1970 struct GNUNET_REGEX_State *clsstate;
1971 struct GNUNET_REGEX_State *currentstate;
1972 struct GNUNET_REGEX_Transition *ctran;
1977 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1978 cls_stack.head = NULL;
1979 cls_stack.tail = NULL;
1981 /* Add start state to closure only for epsilon closure */
1983 GNUNET_array_append (cls->states, cls->len, s);
1985 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
1988 while (cls_stack.len > 0)
1990 currentstate = cls_stack.tail;
1991 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
1995 for (ctran = currentstate->transitions_head; NULL != ctran;
1996 ctran = ctran->next)
1998 if (NULL == (clsstate = ctran->to_state))
2000 if (0 != nullstrcmp (label, ctran->label))
2002 if (0 == clsstate->contained)
2004 GNUNET_array_append (cls->states, cls->len, clsstate);
2005 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2008 clsstate->contained = 1;
2013 for (i = 0; i < cls->len; i++)
2014 cls->states[i]->contained = 0;
2016 /* sort the states */
2018 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2026 * Calculates the closure set for the given set of states.
2028 * @param nfa the NFA containing 's'
2029 * @param states list of states on which to base the closure on
2030 * @param label transitioning label for which to base the closure on,
2031 * pass NULL for epsilon transition
2033 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2035 static struct GNUNET_REGEX_StateSet *
2036 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
2037 struct GNUNET_REGEX_StateSet *states, const char *label)
2039 struct GNUNET_REGEX_State *s;
2040 struct GNUNET_REGEX_StateSet *sset;
2041 struct GNUNET_REGEX_StateSet *cls;
2045 unsigned int contains;
2050 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2052 for (i = 0; i < states->len; i++)
2054 s = states->states[i];
2055 sset = nfa_closure_create (nfa, s, label);
2057 for (j = 0; j < sset->len; j++)
2060 for (k = 0; k < cls->len; k++)
2062 if (sset->states[j]->id == cls->states[k]->id)
2069 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2071 state_set_clear (sset);
2075 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2083 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2085 * @param ctx context
2088 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2090 struct GNUNET_REGEX_Automaton *a;
2091 struct GNUNET_REGEX_Automaton *b;
2092 struct GNUNET_REGEX_Automaton *new_nfa;
2094 b = ctx->stack_tail;
2095 GNUNET_assert (NULL != b);
2096 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2097 a = ctx->stack_tail;
2098 GNUNET_assert (NULL != a);
2099 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2101 state_add_transition (ctx, a->end, NULL, b->start);
2102 a->end->accepting = 0;
2103 b->end->accepting = 1;
2105 new_nfa = nfa_fragment_create (NULL, NULL);
2106 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2107 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2108 new_nfa->start = a->start;
2109 new_nfa->end = b->end;
2110 new_nfa->state_count += a->state_count + b->state_count;
2111 automaton_fragment_clear (a);
2112 automaton_fragment_clear (b);
2114 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2119 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2121 * @param ctx context
2124 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2126 struct GNUNET_REGEX_Automaton *a;
2127 struct GNUNET_REGEX_Automaton *new_nfa;
2128 struct GNUNET_REGEX_State *start;
2129 struct GNUNET_REGEX_State *end;
2131 a = ctx->stack_tail;
2135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2136 "nfa_add_star_op failed, because there was no element on the stack");
2140 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2142 start = nfa_state_create (ctx, 0);
2143 end = nfa_state_create (ctx, 1);
2145 state_add_transition (ctx, start, NULL, a->start);
2146 state_add_transition (ctx, start, NULL, end);
2147 state_add_transition (ctx, a->end, NULL, a->start);
2148 state_add_transition (ctx, a->end, NULL, end);
2150 a->end->accepting = 0;
2153 new_nfa = nfa_fragment_create (start, end);
2154 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2155 automaton_fragment_clear (a);
2157 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2162 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2164 * @param ctx context
2167 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2169 struct GNUNET_REGEX_Automaton *a;
2171 a = ctx->stack_tail;
2175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2176 "nfa_add_plus_op failed, because there was no element on the stack");
2180 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2182 state_add_transition (ctx, a->end, NULL, a->start);
2184 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2189 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2191 * @param ctx context
2194 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2196 struct GNUNET_REGEX_Automaton *a;
2197 struct GNUNET_REGEX_Automaton *new_nfa;
2198 struct GNUNET_REGEX_State *start;
2199 struct GNUNET_REGEX_State *end;
2201 a = ctx->stack_tail;
2205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2206 "nfa_add_question_op failed, because there was no element on the stack");
2210 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2212 start = nfa_state_create (ctx, 0);
2213 end = nfa_state_create (ctx, 1);
2215 state_add_transition (ctx, start, NULL, a->start);
2216 state_add_transition (ctx, start, NULL, end);
2217 state_add_transition (ctx, a->end, NULL, end);
2219 a->end->accepting = 0;
2221 new_nfa = nfa_fragment_create (start, end);
2222 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2223 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2224 automaton_fragment_clear (a);
2229 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2230 * alternates between a and b (a|b)
2232 * @param ctx context
2235 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2237 struct GNUNET_REGEX_Automaton *a;
2238 struct GNUNET_REGEX_Automaton *b;
2239 struct GNUNET_REGEX_Automaton *new_nfa;
2240 struct GNUNET_REGEX_State *start;
2241 struct GNUNET_REGEX_State *end;
2243 b = ctx->stack_tail;
2244 GNUNET_assert (NULL != b);
2245 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2246 a = ctx->stack_tail;
2247 GNUNET_assert (NULL != a);
2248 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2250 start = nfa_state_create (ctx, 0);
2251 end = nfa_state_create (ctx, 1);
2252 state_add_transition (ctx, start, NULL, a->start);
2253 state_add_transition (ctx, start, NULL, b->start);
2255 state_add_transition (ctx, a->end, NULL, end);
2256 state_add_transition (ctx, b->end, NULL, end);
2258 a->end->accepting = 0;
2259 b->end->accepting = 0;
2262 new_nfa = nfa_fragment_create (start, end);
2263 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2264 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2265 automaton_fragment_clear (a);
2266 automaton_fragment_clear (b);
2268 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2273 * Adds a new nfa fragment to the stack
2275 * @param ctx context
2276 * @param label label for nfa transition
2279 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2281 struct GNUNET_REGEX_Automaton *n;
2282 struct GNUNET_REGEX_State *start;
2283 struct GNUNET_REGEX_State *end;
2285 GNUNET_assert (NULL != ctx);
2287 start = nfa_state_create (ctx, 0);
2288 end = nfa_state_create (ctx, 1);
2289 state_add_transition (ctx, start, label, end);
2290 n = nfa_fragment_create (start, end);
2291 GNUNET_assert (NULL != n);
2292 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2297 * Initialize a new context
2299 * @param ctx context
2302 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2310 ctx->transition_id = 0;
2311 ctx->stack_head = NULL;
2312 ctx->stack_tail = NULL;
2317 * Construct an NFA by parsing the regex string of length 'len'.
2319 * @param regex regular expression string
2320 * @param len length of the string
2322 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2324 struct GNUNET_REGEX_Automaton *
2325 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2327 struct GNUNET_REGEX_Context ctx;
2328 struct GNUNET_REGEX_Automaton *nfa;
2333 unsigned int altcount;
2334 unsigned int atomcount;
2335 unsigned int pcount;
2342 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2345 "Could not parse regex. Empty regex string provided.\n");
2350 GNUNET_REGEX_context_init (&ctx);
2360 for (count = 0; count < len && *regexp; count++, regexp++)
2368 nfa_add_concatenation (&ctx);
2370 GNUNET_array_grow (p, pcount, pcount + 1);
2371 p[pcount - 1].altcount = altcount;
2372 p[pcount - 1].atomcount = atomcount;
2379 error_msg = "Cannot append '|' to nothing";
2382 while (--atomcount > 0)
2383 nfa_add_concatenation (&ctx);
2389 error_msg = "Missing opening '('";
2394 /* Ignore this: "()" */
2396 altcount = p[pcount].altcount;
2397 atomcount = p[pcount].atomcount;
2400 while (--atomcount > 0)
2401 nfa_add_concatenation (&ctx);
2402 for (; altcount > 0; altcount--)
2403 nfa_add_alternation (&ctx);
2405 altcount = p[pcount].altcount;
2406 atomcount = p[pcount].atomcount;
2412 error_msg = "Cannot append '*' to nothing";
2415 nfa_add_star_op (&ctx);
2420 error_msg = "Cannot append '+' to nothing";
2423 nfa_add_plus_op (&ctx);
2428 error_msg = "Cannot append '?' to nothing";
2431 nfa_add_question_op (&ctx);
2437 nfa_add_concatenation (&ctx);
2439 curlabel[0] = *regexp;
2440 nfa_add_label (&ctx, curlabel);
2447 error_msg = "Unbalanced parenthesis";
2450 while (--atomcount > 0)
2451 nfa_add_concatenation (&ctx);
2452 for (; altcount > 0; altcount--)
2453 nfa_add_alternation (&ctx);
2455 GNUNET_free_non_null (p);
2457 nfa = ctx.stack_tail;
2458 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2460 if (NULL != ctx.stack_head)
2462 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2466 /* Remember the regex that was used to generate this NFA */
2467 nfa->regex = GNUNET_strdup (regex);
2469 /* create depth-first numbering of the states for pretty printing */
2470 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2472 /* No multistriding added so far */
2473 nfa->is_multistrided = GNUNET_NO;
2478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2479 if (NULL != error_msg)
2480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2482 GNUNET_free_non_null (p);
2484 while (NULL != (nfa = ctx.stack_head))
2486 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2487 GNUNET_REGEX_automaton_destroy (nfa);
2495 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2497 * @param ctx context.
2498 * @param nfa NFA automaton.
2499 * @param dfa DFA automaton.
2500 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2504 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2505 struct GNUNET_REGEX_Automaton *nfa,
2506 struct GNUNET_REGEX_Automaton *dfa,
2507 struct GNUNET_REGEX_State *dfa_state)
2509 struct GNUNET_REGEX_Transition *ctran;
2510 struct GNUNET_REGEX_State *state_iter;
2511 struct GNUNET_REGEX_State *new_dfa_state;
2512 struct GNUNET_REGEX_State *state_contains;
2513 struct GNUNET_REGEX_StateSet *tmp;
2514 struct GNUNET_REGEX_StateSet *nfa_set;
2516 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2518 if (NULL == ctran->label || NULL != ctran->to_state)
2521 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2522 nfa_set = nfa_closure_set_create (nfa, tmp, NULL);
2523 state_set_clear (tmp);
2525 /* FIXME: this O(n) loop can be done in O(1) with a hash map */
2526 state_contains = NULL;
2527 for (state_iter = dfa->states_head; NULL != state_iter;
2528 state_iter = state_iter->next)
2530 if (0 == state_set_compare (state_iter->nfa_set, nfa_set))
2532 state_contains = state_iter;
2537 if (NULL == state_contains)
2539 new_dfa_state = dfa_state_create (ctx, nfa_set);
2540 automaton_add_state (dfa, new_dfa_state);
2541 ctran->to_state = new_dfa_state;
2542 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2546 ctran->to_state = state_contains;
2553 * Construct DFA for the given 'regex' of length 'len'.
2555 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
2556 * compressed to o -> abc -> o. Note that this parameter influences the
2557 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
2558 * edges with the same label). For example for an application that stores IPv4
2559 * addresses as bitstrings it could make sense to limit the path compression to
2562 * @param regex regular expression string.
2563 * @param len length of the regular expression.
2564 * @param max_path_len limit the path compression length to the
2565 * given value. If set to 1, no path compression is applied. Set to 0 for
2566 * maximal possible path compression (generally not desireable).
2567 * @return DFA, needs to be freed using GNUNET_REGEX_automaton_destroy.
2569 struct GNUNET_REGEX_Automaton *
2570 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len,
2571 unsigned int max_path_len)
2573 struct GNUNET_REGEX_Context ctx;
2574 struct GNUNET_REGEX_Automaton *dfa;
2575 struct GNUNET_REGEX_Automaton *nfa;
2576 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2578 GNUNET_REGEX_context_init (&ctx);
2581 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2586 "Could not create DFA, because NFA creation failed\n");
2590 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2592 dfa->regex = GNUNET_strdup (regex);
2594 /* Create DFA start state from epsilon closure */
2595 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, NULL);
2596 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2597 automaton_add_state (dfa, dfa->start);
2599 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2601 GNUNET_REGEX_automaton_destroy (nfa);
2604 dfa_minimize (&ctx, dfa);
2606 /* Create proofs and hashes for all states */
2607 automaton_create_proofs (dfa);
2609 /* Compress linear DFA paths */
2610 if (1 != max_path_len)
2611 dfa_compress_paths (&ctx, dfa, max_path_len);
2618 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2621 * @param a automaton to be destroyed
2624 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2626 struct GNUNET_REGEX_State *s;
2627 struct GNUNET_REGEX_State *next_state;
2632 GNUNET_free_non_null (a->regex);
2633 GNUNET_free_non_null (a->canonical_regex);
2635 for (s = a->states_head; NULL != s; s = next_state)
2637 next_state = s->next;
2638 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2639 automaton_destroy_state (s);
2647 * Evaluates the given string using the given DFA automaton
2649 * @param a automaton, type must be DFA
2650 * @param string string that should be evaluated
2652 * @return 0 if string matches, non 0 otherwise
2655 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2658 struct GNUNET_REGEX_State *s;
2659 unsigned int step_len;
2663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2664 "Tried to evaluate DFA, but NFA automaton given");
2670 /* If the string is empty but the starting state is accepting, we accept. */
2671 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2674 for (strp = string; NULL != strp && *strp; strp += step_len)
2676 step_len = dfa_move (&s, strp);
2682 if (NULL != s && s->accepting)
2690 * Evaluates the given string using the given NFA automaton
2692 * @param a automaton, type must be NFA
2693 * @param string string that should be evaluated
2695 * @return 0 if string matches, non 0 otherwise
2698 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2702 struct GNUNET_REGEX_State *s;
2703 struct GNUNET_REGEX_StateSet *sset;
2704 struct GNUNET_REGEX_StateSet *new_sset;
2710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2711 "Tried to evaluate NFA, but DFA automaton given");
2715 /* If the string is empty but the starting state is accepting, we accept. */
2716 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2720 sset = nfa_closure_create (a, a->start, 0);
2723 for (strp = string; NULL != strp && *strp; strp++)
2726 new_sset = nfa_closure_set_create (a, sset, str);
2727 state_set_clear (sset);
2728 sset = nfa_closure_set_create (a, new_sset, 0);
2729 state_set_clear (new_sset);
2732 for (i = 0; i < sset->len; i++)
2734 s = sset->states[i];
2735 if (NULL != s && s->accepting)
2742 state_set_clear (sset);
2748 * Evaluates the given 'string' against the given compiled regex
2750 * @param a automaton
2751 * @param string string to check
2753 * @return 0 if string matches, non 0 otherwise
2756 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2763 result = evaluate_dfa (a, string);
2766 result = evaluate_nfa (a, string);
2769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2770 "Evaluating regex failed, automaton has no type!\n");
2771 result = GNUNET_SYSERR;
2780 * Get the canonical regex of the given automaton.
2781 * When constructing the automaton a proof is computed for each state,
2782 * consisting of the regular expression leading to this state. A complete
2783 * regex for the automaton can be computed by combining these proofs.
2784 * As of now this function is only useful for testing.
2786 * @param a automaton for which the canonical regex should be returned.
2791 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2796 return a->canonical_regex;
2801 * Get the number of transitions that are contained in the given automaton.
2803 * @param a automaton for which the number of transitions should be returned.
2805 * @return number of transitions in the given automaton.
2808 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2810 unsigned int t_count;
2811 struct GNUNET_REGEX_State *s;
2817 for (s = a->states_head; NULL != s; s = s->next)
2818 t_count += s->transition_count;
2825 * Get the first key for the given 'input_string'. This hashes the first x bits
2826 * of the 'input_string'.
2828 * @param input_string string.
2829 * @param string_len length of the 'input_string'.
2830 * @param key pointer to where to write the hash code.
2832 * @return number of bits of 'input_string' that have been consumed
2833 * to construct the key
2836 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2837 struct GNUNET_HashCode * key)
2843 GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES;
2845 if (NULL == input_string)
2847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2851 GNUNET_CRYPTO_hash (input_string, size, key);
2858 * Check if the given 'proof' matches the given 'key'.
2860 * @param proof partial regex of a state.
2861 * @param key hash of a state.
2863 * @return GNUNET_OK if the proof is valid for the given key.
2866 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2868 struct GNUNET_HashCode key_check;
2870 if (NULL == proof || NULL == key)
2872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2876 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2878 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2883 * Recursive function that calls the iterator for each synthetic start state.
2885 * @param min_len minimum length of the path in the graph.
2886 * @param max_len maximum length of the path in the graph.
2887 * @param consumed_string string consumed by traversing the graph till this state.
2888 * @param state current state of the automaton.
2889 * @param iterator iterator function called for each edge.
2890 * @param iterator_cls closure for the iterator function.
2893 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2894 char *consumed_string, struct GNUNET_REGEX_State *state,
2895 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2899 struct GNUNET_REGEX_Transition *t;
2900 unsigned int num_edges = state->transition_count;
2901 struct GNUNET_REGEX_Edge edges[num_edges];
2902 struct GNUNET_REGEX_Edge edge[1];
2903 struct GNUNET_HashCode hash;
2904 struct GNUNET_HashCode hash_new;
2906 unsigned int cur_len;
2908 if (NULL != consumed_string)
2909 cur_len = strlen (consumed_string);
2913 if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
2914 NULL != consumed_string)
2916 if (cur_len <= max_len)
2918 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2920 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2923 edges[i].label = t->label;
2924 edges[i].destination = t->to_state->hash;
2926 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2927 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2931 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2932 state->transition_count < 1 && cur_len < max_len)
2934 /* Special case for regex consisting of just a string that is shorter than
2936 edge[0].label = &consumed_string[cur_len - 1];
2937 edge[0].destination = state->hash;
2938 temp = GNUNET_strdup (consumed_string);
2939 temp[cur_len - 1] = '\0';
2940 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2941 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2945 else if (max_len < cur_len)
2947 /* Case where the concatenated labels are longer than max_len, then split. */
2948 edge[0].label = &consumed_string[max_len];
2949 edge[0].destination = state->hash;
2950 temp = GNUNET_strdup (consumed_string);
2951 temp[max_len] = '\0';
2952 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2953 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2958 if (cur_len < max_len)
2960 for (t = state->transitions_head; NULL != t; t = t->next)
2962 if (NULL != consumed_string)
2963 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2965 GNUNET_asprintf (&temp, "%s", t->label);
2967 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2976 * Iterate over all edges starting from start state of automaton 'a'. Calling
2977 * iterator for each edge.
2979 * @param a automaton.
2980 * @param iterator iterator called for each edge.
2981 * @param iterator_cls closure.
2984 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2985 GNUNET_REGEX_KeyIterator iterator,
2988 struct GNUNET_REGEX_State *s;
2990 for (s = a->states_head; NULL != s; s = s->next)
2992 struct GNUNET_REGEX_Edge edges[s->transition_count];
2993 unsigned int num_edges;
2995 num_edges = state_get_edges (s, edges);
2997 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
2998 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
3001 s->marked = GNUNET_NO;
3004 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, GNUNET_REGEX_INITIAL_BYTES,
3005 NULL, a->start, iterator, iterator_cls);
3010 * Create a string with binary IP notation for the given 'addr' in 'str'.
3012 * @param af address family of the given 'addr'.
3013 * @param addr address that should be converted to a string.
3014 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
3015 * @param str string that will contain binary notation of 'addr'. Expected
3016 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
3019 iptobinstr (const int af, const void *addr, char *str)
3027 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
3031 for (i = 31; i >= 0; i--)
3033 *str = (b & 1) + '0';
3041 struct in6_addr b = *(const struct in6_addr *) addr;
3045 for (i = 127; i >= 0; i--)
3047 *str = (b.s6_addr[i / 8] & 1) + '0';
3049 b.s6_addr[i / 8] >>= 1;
3058 * Get the ipv4 network prefix from the given 'netmask'.
3060 * @param netmask netmask for which to get the prefix len.
3062 * @return length of ipv4 prefix for 'netmask'.
3065 ipv4netmasktoprefixlen (const char *netmask)
3071 if (1 != inet_pton (AF_INET, netmask, &a))
3074 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3081 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3083 * @param ip IPv4 representation.
3084 * @param netmask netmask for the ip.
3085 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3089 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3092 unsigned int pfxlen;
3094 pfxlen = ipv4netmasktoprefixlen (netmask);
3095 iptobinstr (AF_INET, ip, rxstr);
3096 rxstr[pfxlen] = '\0';
3098 strcat (rxstr, "(0|1)+");
3103 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3105 * @param ipv6 IPv6 representation.
3106 * @param prefixlen length of the ipv6 prefix.
3107 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3111 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3114 iptobinstr (AF_INET6, ipv6, rxstr);
3115 rxstr[prefixlen] = '\0';
3116 if (prefixlen < 128)
3117 strcat (rxstr, "(0|1)+");