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"
35 struct GNUNET_REGEX_StateSet
40 struct GNUNET_REGEX_State **states;
43 * Length of the 'states' array.
50 * Compare two strings for equality. If either is NULL they are not equal.
52 * @param str1 first string for comparison.
53 * @param str2 second string for comparison.
55 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
58 nullstrcmp (const char *str1, const char *str2)
60 if ((NULL == str1) != (NULL == str2))
62 if ((NULL == str1) && (NULL == str2))
65 return strcmp (str1, str2);
70 * Adds a transition from one state to another on 'label'. Does not add
74 * @param from_state starting state for the transition
75 * @param label transition label
76 * @param to_state state to where the transition should point to
79 state_add_transition (struct GNUNET_REGEX_Context *ctx,
80 struct GNUNET_REGEX_State *from_state, const char *label,
81 struct GNUNET_REGEX_State *to_state)
83 struct GNUNET_REGEX_Transition *t;
84 struct GNUNET_REGEX_Transition *oth;
86 if (NULL == from_state)
88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
92 // Do not add duplicate state transitions
93 for (t = from_state->transitions_head; NULL != t; t = t->next)
95 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
96 t->from_state == from_state)
100 // sort transitions by label
101 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
103 if (0 < nullstrcmp (oth->label, label))
107 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
109 t->id = ctx->transition_id++;
111 t->label = GNUNET_strdup (label);
114 t->to_state = to_state;
115 t->from_state = from_state;
117 // Add outgoing transition to 'from_state'
118 from_state->transition_count++;
119 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
120 from_state->transitions_tail, oth, t);
125 * Remove a 'transition' from 'state'.
127 * @param state state from which the to-be-removed transition originates.
128 * @param transition transition that should be removed from state 'state'.
131 state_remove_transition (struct GNUNET_REGEX_State *state,
132 struct GNUNET_REGEX_Transition *transition)
134 if (NULL == state || NULL == transition)
137 if (transition->from_state != state)
140 GNUNET_free_non_null (transition->label);
142 state->transition_count--;
143 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
146 GNUNET_free (transition);
151 * Compare two states. Used for sorting.
153 * @param a first state
154 * @param b second state
156 * @return an integer less than, equal to, or greater than zero
157 * if the first argument is considered to be respectively
158 * less than, equal to, or greater than the second.
161 state_compare (const void *a, const void *b)
163 struct GNUNET_REGEX_State **s1;
164 struct GNUNET_REGEX_State **s2;
166 s1 = (struct GNUNET_REGEX_State **) a;
167 s2 = (struct GNUNET_REGEX_State **) b;
169 return (*s1)->id - (*s2)->id;
174 * Get all edges leaving state 's'.
177 * @param edges all edges leaving 's', expected to be allocated and have enough
178 * space for s->transitions_count elements.
180 * @return number of edges.
183 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
185 struct GNUNET_REGEX_Transition *t;
193 for (t = s->transitions_head; NULL != t; t = t->next)
195 if (NULL != t->to_state)
197 edges[count].label = t->label;
198 edges[count].destination = t->to_state->hash;
207 * Compare to state sets by comparing the id's of the states that are contained
208 * in each set. Both sets are expected to be sorted by id!
210 * @param sset1 first state set
211 * @param sset2 second state set
213 * @return an integer less than, equal to, or greater than zero
214 * if the first argument is considered to be respectively
215 * less than, equal to, or greater than the second.
218 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
219 struct GNUNET_REGEX_StateSet *sset2)
224 if (NULL == sset1 || NULL == sset2)
227 result = sset1->len - sset2->len;
229 for (i = 0; i < sset1->len; i++)
234 result = state_compare (&sset1->states[i], &sset2->states[i]);
241 * Clears the given StateSet 'set'
243 * @param set set to be cleared
246 state_set_clear (struct GNUNET_REGEX_StateSet *set)
252 GNUNET_array_grow (set->states, set->len, 0);
258 * Clears an automaton fragment. Does not destroy the states inside the
261 * @param a automaton to be cleared
264 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
271 a->states_head = NULL;
272 a->states_tail = NULL;
279 * Frees the memory used by State 's'
281 * @param s state that should be destroyed
284 automaton_destroy_state (struct GNUNET_REGEX_State *s)
286 struct GNUNET_REGEX_Transition *t;
287 struct GNUNET_REGEX_Transition *next_t;
292 GNUNET_free_non_null (s->name);
293 GNUNET_free_non_null (s->proof);
294 state_set_clear (s->nfa_set);
296 for (t = s->transitions_head; NULL != t; t = next_t)
299 state_remove_transition (s, t);
307 * Remove a state from the given automaton 'a'. Always use this function when
308 * altering the states of an automaton. Will also remove all transitions leading
309 * to this state, before destroying it.
312 * @param s state to remove
315 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
316 struct GNUNET_REGEX_State *s)
318 struct GNUNET_REGEX_State *s_check;
319 struct GNUNET_REGEX_Transition *t_check;
320 struct GNUNET_REGEX_Transition *t_check_next;
322 if (NULL == a || NULL == s)
325 // remove all transitions leading to this state
326 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
328 for (t_check = s_check->transitions_head; NULL != t_check;
329 t_check = t_check_next)
331 t_check_next = t_check->next;
332 if (t_check->to_state == s)
333 state_remove_transition (s_check, t_check);
338 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
341 automaton_destroy_state (s);
346 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
351 * @param s1 first state
352 * @param s2 second state, will be destroyed
355 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
356 struct GNUNET_REGEX_Automaton *a,
357 struct GNUNET_REGEX_State *s1,
358 struct GNUNET_REGEX_State *s2)
360 struct GNUNET_REGEX_State *s_check;
361 struct GNUNET_REGEX_Transition *t_check;
362 struct GNUNET_REGEX_Transition *t;
363 struct GNUNET_REGEX_Transition *t_next;
367 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
372 // 1. Make all transitions pointing to s2 point to s1, unless this transition
373 // does not already exists, if it already exists remove transition.
374 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
376 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
378 t_next = t_check->next;
380 if (s2 == t_check->to_state)
383 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
385 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
388 if (GNUNET_NO == is_dup)
389 t_check->to_state = s1;
391 state_remove_transition (t_check->from_state, t_check);
396 // 2. Add all transitions from s2 to sX to s1
397 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
399 if (t_check->to_state != s1)
400 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
403 // 3. Rename s1 to {s1,s2}
405 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
406 GNUNET_free (new_name);
409 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
411 automaton_destroy_state (s2);
416 * Add a state to the automaton 'a', always use this function to alter the
417 * states DLL of the automaton.
419 * @param a automaton to add the state to
420 * @param s state that should be added
423 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
424 struct GNUNET_REGEX_State *s)
426 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
432 * Depth-first traversal (DFS) of all states that are reachable from state
433 * 's'. Performs 'action' on each visited state.
435 * @param s start state.
436 * @param marks an array of size a->state_count to remember which state was
438 * @param count current count of the state.
439 * @param check function that is checked before advancing on each transition
441 * @param check_cls closure for check.
442 * @param action action to be performed on each state.
443 * @param action_cls closure for action.
446 automaton_state_traverse (struct GNUNET_REGEX_State *s, int *marks,
448 GNUNET_REGEX_traverse_check check, void *check_cls,
449 GNUNET_REGEX_traverse_action action, void *action_cls)
451 struct GNUNET_REGEX_Transition *t;
453 if (GNUNET_YES == marks[s->traversal_id])
456 marks[s->traversal_id] = GNUNET_YES;
459 action (action_cls, *count, s);
463 for (t = s->transitions_head; NULL != t; t = t->next)
466 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
468 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
476 * Traverses the given automaton using depth-first-search (DFS) from it's start
477 * state, visiting all reachable states and calling 'action' on each one of
480 * @param a automaton to be traversed.
481 * @param start start state, pass a->start or NULL to traverse the whole automaton.
482 * @param check function that is checked before advancing on each transition
484 * @param check_cls closure for check.
485 * @param action action to be performed on each state.
486 * @param action_cls closure for action
489 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
490 struct GNUNET_REGEX_State *start,
491 GNUNET_REGEX_traverse_check check,
493 GNUNET_REGEX_traverse_action action,
497 struct GNUNET_REGEX_State *s;
499 if (NULL == a || 0 == a->state_count)
502 int marks[a->state_count];
504 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
505 s = s->next, count++)
507 s->traversal_id = count;
508 marks[s->traversal_id] = GNUNET_NO;
518 automaton_state_traverse (s, marks, &count, check, check_cls, action,
524 * Check if the given string 'str' needs parentheses around it when
525 * using it to generate a regex.
529 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
532 needs_parentheses (const char *str)
540 if ((NULL == str) || ((slen = strlen (str)) < 2))
549 cl = strchr (pos, ')');
555 op = strchr (pos, '(');
556 if ((NULL != op) && (op < cl))
566 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
571 * Remove parentheses surrounding string 'str'.
572 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
573 * You need to GNUNET_free the returned string.
575 * @param str string, free'd or re-used by this function, can be NULL
577 * @return string without surrounding parentheses, string 'str' if no preceding
578 * epsilon could be found, NULL if 'str' was NULL
581 remove_parentheses (char *str)
586 if ((NULL == str) || ('(' != str[0]) ||
587 (str[(slen = strlen (str)) - 1] != ')'))
590 pos = strchr (&str[1], ')');
591 if (pos == &str[slen - 1])
593 memmove (str, &str[1], slen - 2);
594 str[slen - 2] = '\0';
601 * Check if the string 'str' starts with an epsilon (empty string).
602 * Example: "(|a)" is starting with an epsilon.
604 * @param str string to test
606 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
609 has_epsilon (const char *str)
611 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
612 (')' == str[strlen (str) - 1]);
617 * Remove an epsilon from the string str. Where epsilon is an empty string
618 * Example: str = "(|a|b|c)", result: "a|b|c"
619 * The returned string needs to be freed.
623 * @return string without preceding epsilon, string 'str' if no preceding
624 * epsilon could be found, NULL if 'str' was NULL
627 remove_epsilon (const char *str)
633 if (('(' == str[0]) && ('|' == str[1]))
636 if (')' == str[len - 1])
637 return GNUNET_strndup (&str[2], len - 3);
639 return GNUNET_strdup (str);
644 * Compare 'str1', starting from position 'k', with whole 'str2'
646 * @param str1 first string to compare, starting from position 'k'
647 * @param str2 second string for comparison
648 * @param k starting position in 'str1'
650 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
653 strkcmp (const char *str1, const char *str2, size_t k)
655 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
657 return strcmp (&str1[k], str2);
662 * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse'
663 * function to create the depth-first numbering of the states.
665 * @param cls states array.
666 * @param count current state counter.
667 * @param s current state.
670 number_states (void *cls, const unsigned int count,
671 struct GNUNET_REGEX_State *s)
673 struct GNUNET_REGEX_State **states = cls;
682 * Construct the regular expression given the inductive step,
683 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
684 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
686 * @param R_last_ij value of $R^{(k-1)_{ij}.
687 * @param R_last_ik value of $R^{(k-1)_{ik}.
688 * @param R_last_kk value of $R^{(k-1)_{kk}.
689 * @param R_last_kj value of $R^{(k-1)_{kj}.
690 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
691 * is expected to be NULL when called!
694 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
695 char *R_last_kk, char *R_last_kj,
721 GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
723 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
724 // R_last == R^{(k-1)}, R_cur == R^{(k)}
725 // R_cur_ij = R_cur_l | R_cur_r
726 // R_cur_l == R^{(k-1)}_{ij}
727 // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
729 if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
730 (NULL == R_last_kj)))
732 /* R^{(k)}_{ij} = N | N */
737 if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
740 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
741 *R_cur_ij = GNUNET_strdup (R_last_ij);
745 // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
746 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
751 // cache results from strcmp, we might need these many times
752 ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
753 ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
754 ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
755 kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
757 // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
758 // as parentheses, so we can better compare the contents
759 R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
760 R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
761 R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
763 clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
764 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
766 // construct R_cur_l (and, if necessary R_cur_r)
767 if (NULL != R_last_ij)
769 // Assign R_temp_ij to R_last_ij and remove epsilon as well
770 // as parentheses, so we can better compare the contents
771 R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
773 if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
774 && 0 == strcmp (R_temp_kk, R_temp_kj))
776 if (0 == strlen (R_temp_ij))
778 R_cur_r = GNUNET_strdup ("");
780 else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
781 (0 == strncmp (R_last_ik, "(|", 2) &&
782 0 == strncmp (R_last_kj, "(|", 2)))
784 // a|(e|a)a*(e|a) = a*
785 // a|(e|a)(e|a)*(e|a) = a*
787 // (e|a)|aa*(e|a) = a*
788 // (e|a)|(e|a)a*a = a*
789 // (e|a)|(e|a)a*(e|a) = a*
790 // (e|a)|(e|a)(e|a)*(e|a) = a*
791 if (GNUNET_YES == needs_parentheses (R_temp_ij))
792 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
794 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
801 // a|(e|a)(e|a)*a = a+
802 // a|a(e|a)*(e|a) = a+
803 if (GNUNET_YES == needs_parentheses (R_temp_ij))
804 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
806 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
809 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
812 if (strlen (R_last_kk) < 1)
813 R_cur_r = GNUNET_strdup (R_last_ij);
814 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
815 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
817 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
821 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
824 if (strlen (R_last_kk) < 1)
825 R_cur_r = GNUNET_strdup (R_last_kj);
826 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
827 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
829 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
833 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
834 has_epsilon (R_last_kk))
836 // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
837 if (needs_parentheses (R_temp_kk))
838 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
840 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
844 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
845 has_epsilon (R_last_kk))
847 // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
848 if (needs_parentheses (R_temp_kk))
849 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
851 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
857 temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
858 temp_a = remove_parentheses (temp_a);
862 GNUNET_free_non_null (R_temp_ij);
866 // we have no left side
870 // construct R_cur_r, if not already constructed
873 length = strlen (R_temp_kk) - strlen (R_last_ik);
876 if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
877 NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
878 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
879 0 == strncmp (R_temp_kk, R_last_kj, length))
881 temp_a = GNUNET_malloc (length + 1);
882 temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
887 for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
891 temp_a[length_l] = R_last_kj[cnt];
896 temp_b[length_r] = R_last_kj[cnt];
900 temp_a[length_l] = '\0';
901 temp_b[length_r] = '\0';
904 if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
906 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
907 GNUNET_free (R_cur_l);
912 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
914 GNUNET_free (temp_a);
915 GNUNET_free (temp_b);
917 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
918 0 == strcmp (R_temp_kk, R_temp_kj))
921 // (e|a)(e|a)*(e|a) = a*
922 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
924 if (needs_parentheses (R_temp_kk))
925 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
927 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
930 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
931 !has_epsilon (R_last_ik))
933 if (needs_parentheses (R_temp_kk))
934 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
936 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
945 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
946 has_epsilon (R_last_kj));
950 if (needs_parentheses (R_temp_kk))
951 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
953 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
958 // (e|a)(e|a)*b = a*b
959 else if (0 == strcmp (R_temp_ik, R_temp_kk))
961 if (has_epsilon (R_last_ik))
963 if (needs_parentheses (R_temp_kk))
964 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
966 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
970 if (needs_parentheses (R_temp_kk))
971 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
973 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
977 // b(e|a)*(e|a) = ba*
978 else if (0 == strcmp (R_temp_kk, R_temp_kj))
980 if (has_epsilon (R_last_kj))
982 if (needs_parentheses (R_temp_kk))
983 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
985 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
989 if (needs_parentheses (R_temp_kk))
990 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
992 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
997 if (strlen (R_temp_kk) > 0)
999 if (needs_parentheses (R_temp_kk))
1001 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1006 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1012 GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1017 GNUNET_free_non_null (R_temp_ik);
1018 GNUNET_free_non_null (R_temp_kk);
1019 GNUNET_free_non_null (R_temp_kj);
1021 if (NULL == R_cur_l && NULL == R_cur_r)
1027 if (NULL != R_cur_l && NULL == R_cur_r)
1029 *R_cur_ij = R_cur_l;
1033 if (NULL == R_cur_l && NULL != R_cur_r)
1035 *R_cur_ij = R_cur_r;
1039 if (0 == nullstrcmp (R_cur_l, R_cur_r))
1041 *R_cur_ij = R_cur_l;
1042 GNUNET_free (R_cur_r);
1046 GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1048 GNUNET_free (R_cur_l);
1049 GNUNET_free (R_cur_r);
1054 * create proofs for all states in the given automaton. Implementation of the
1055 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1056 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1058 * @param a automaton.
1061 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1066 "Could not create proofs, automaton was NULL\n");
1070 unsigned int n = a->state_count;
1071 struct GNUNET_REGEX_State *states[n];
1075 struct GNUNET_REGEX_Transition *t;
1076 char *complete_regex;
1081 /* create depth-first numbering of the states, initializes 'state' */
1082 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1085 for (i = 0; i < n; i++)
1086 GNUNET_assert (NULL != states[i]);
1088 /* Compute regular expressions of length "1" between each pair of states */
1089 for (i = 0; i < n; i++)
1091 for (j = 0; j < n; j++)
1094 R_last[i][j] = NULL;
1096 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1098 j = t->to_state->dfs_id;
1099 if (NULL == R_last[i][j])
1100 GNUNET_asprintf (&R_last[i][j], "%s", t->label);
1103 temp = R_last[i][j];
1104 GNUNET_asprintf (&R_last[i][j], "%s|%s", R_last[i][j], t->label);
1108 if (NULL == R_last[i][i])
1109 GNUNET_asprintf (&R_last[i][i], "");
1112 temp = R_last[i][i];
1113 GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
1117 for (i = 0; i < n; i++)
1118 for (j = 0; j < n; j++)
1119 if (needs_parentheses (R_last[i][j]))
1121 temp = R_last[i][j];
1122 GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
1126 /* Compute regular expressions of length "k" between each pair of states per
1128 for (k = 0; k < n; k++)
1130 for (i = 0; i < n; i++)
1132 for (j = 0; j < n; j++)
1134 // Basis for the recursion:
1135 // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1136 // R_last == R^{(k-1)}, R_cur == R^{(k)}
1138 // Create R_cur[i][j] and simplify the expression
1139 automaton_create_proofs_simplify (R_last[i][j], R_last[i][k],
1140 R_last[k][k], R_last[k][j],
1145 // set R_last = R_cur
1146 for (i = 0; i < n; i++)
1148 for (j = 0; j < n; j++)
1150 GNUNET_free_non_null (R_last[i][j]);
1151 R_last[i][j] = R_cur[i][j];
1157 // assign proofs and hashes
1158 for (i = 0; i < n; i++)
1160 if (NULL != R_last[a->start->dfs_id][i])
1162 states[i]->proof = GNUNET_strdup (R_last[a->start->dfs_id][i]);
1163 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1168 // complete regex for whole DFA: union of all pairs (start state/accepting
1170 complete_regex = NULL;
1171 for (i = 0; i < n; i++)
1173 if (states[i]->accepting)
1175 if (NULL == complete_regex && 0 < strlen (R_last[a->start->dfs_id][i]))
1177 GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->dfs_id][i]);
1179 else if (NULL != R_last[a->start->dfs_id][i] &&
1180 0 < strlen (R_last[a->start->dfs_id][i]))
1182 temp = complete_regex;
1183 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1184 R_last[a->start->dfs_id][i]);
1189 a->canonical_regex = complete_regex;
1192 for (i = 0; i < n; i++)
1194 for (j = 0; j < n; j++)
1195 GNUNET_free_non_null (R_last[i][j]);
1201 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1202 * automaton_destroy_state.
1204 * @param ctx context
1205 * @param nfa_states set of NFA states on which the DFA should be based on
1207 * @return new DFA state
1209 static struct GNUNET_REGEX_State *
1210 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1211 struct GNUNET_REGEX_StateSet *nfa_states)
1213 struct GNUNET_REGEX_State *s;
1216 struct GNUNET_REGEX_State *cstate;
1217 struct GNUNET_REGEX_Transition *ctran;
1220 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1221 s->id = ctx->state_id++;
1223 s->marked = GNUNET_NO;
1231 if (NULL == nfa_states)
1233 GNUNET_asprintf (&s->name, "s%i", s->id);
1237 s->nfa_set = nfa_states;
1239 if (nfa_states->len < 1)
1242 // Create a name based on 'nfa_states'
1243 s->name = GNUNET_malloc (sizeof (char) * 2);
1244 strcat (s->name, "{");
1247 for (i = 0; i < nfa_states->len; i++)
1249 cstate = nfa_states->states[i];
1250 GNUNET_asprintf (&name, "%i,", cstate->id);
1254 len = strlen (s->name) + strlen (name) + 1;
1255 s->name = GNUNET_realloc (s->name, len);
1256 strcat (s->name, name);
1261 // Add a transition for each distinct label to NULL state
1262 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1264 if (NULL != ctran->label)
1265 state_add_transition (ctx, s, ctran->label, NULL);
1268 // If the nfa_states contain an accepting state, the new dfa state is also
1270 if (cstate->accepting)
1274 s->name[strlen (s->name) - 1] = '}';
1281 * Move from the given state 's' to the next state on transition 'str'. Consumes
1282 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1283 * 's' will point to the next state, and the length of the substring used for
1284 * this transition will be returned. If no transition possible 0 is returned and
1285 * 's' points to NULL.
1287 * @param s starting state, will point to the next state or NULL (if no
1288 * transition possible)
1289 * @param str edge label to follow (will match longest common prefix)
1291 * @return length of the substring comsumed from 'str'
1294 dfa_move (struct GNUNET_REGEX_State **s, const char *str)
1296 struct GNUNET_REGEX_Transition *t;
1297 struct GNUNET_REGEX_State *new_s;
1299 unsigned int max_len;
1306 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1308 len = strlen (t->label);
1310 if (0 == strncmp (t->label, str, len))
1315 new_s = t->to_state;
1325 * Set the given state 'marked' to GNUNET_YES. Used by the
1326 * 'dfa_remove_unreachable_states' function to detect unreachable states in the
1329 * @param cls closure, not used.
1330 * @param count count, not used.
1331 * @param s state where the marked attribute will be set to GNUNET_YES.
1334 mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
1336 s->marked = GNUNET_YES;
1340 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1341 * states that are not reachable from the starting state.
1343 * @param a DFA automaton
1346 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1348 struct GNUNET_REGEX_State *s;
1349 struct GNUNET_REGEX_State *s_next;
1351 // 1. unmark all states
1352 for (s = a->states_head; NULL != s; s = s->next)
1353 s->marked = GNUNET_NO;
1355 // 2. traverse dfa from start state and mark all visited states
1356 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1358 // 3. delete all states that were not visited
1359 for (s = a->states_head; NULL != s; s = s_next)
1362 if (GNUNET_NO == s->marked)
1363 automaton_remove_state (a, s);
1369 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1370 * not transition to any other state but themselves.
1372 * @param a DFA automaton
1375 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1377 struct GNUNET_REGEX_State *s;
1378 struct GNUNET_REGEX_Transition *t;
1381 GNUNET_assert (DFA == a->type);
1383 for (s = a->states_head; NULL != s; s = s->next)
1389 for (t = s->transitions_head; NULL != t; t = t->next)
1391 if (NULL != t->to_state && t->to_state != s)
1401 // state s is dead, remove it
1402 automaton_remove_state (a, s);
1408 * Merge all non distinguishable states in the DFA 'a'
1410 * @param ctx context
1411 * @param a DFA automaton
1414 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1415 struct GNUNET_REGEX_Automaton *a)
1417 int table[a->state_count][a->state_count];
1418 struct GNUNET_REGEX_State *s1;
1419 struct GNUNET_REGEX_State *s2;
1420 struct GNUNET_REGEX_Transition *t1;
1421 struct GNUNET_REGEX_Transition *t2;
1422 struct GNUNET_REGEX_State *s1_next;
1423 struct GNUNET_REGEX_State *s2_next;
1425 unsigned int num_equal_edges;
1428 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1434 // Mark all pairs of accepting/!accepting states
1435 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1437 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1439 table[s1->marked][s2->marked] = 0;
1441 if ((s1->accepting && !s2->accepting) ||
1442 (!s1->accepting && s2->accepting))
1444 table[s1->marked][s2->marked] = 1;
1449 // Find all equal states
1454 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1456 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1458 if (0 != table[s1->marked][s2->marked])
1461 num_equal_edges = 0;
1462 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1464 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1466 if (0 == strcmp (t1->label, t2->label))
1469 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1470 0 != table[t2->to_state->marked][t1->to_state->marked])
1472 table[s1->marked][s2->marked] = 1;
1478 if (num_equal_edges != s1->transition_count ||
1479 num_equal_edges != s2->transition_count)
1481 // Make sure ALL edges of possible equal states are the same
1482 table[s1->marked][s2->marked] = -2;
1488 // Merge states that are equal
1489 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1492 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1495 if (table[s1->marked][s2->marked] == 0)
1496 automaton_merge_states (ctx, a, s1, s2);
1503 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1504 * dead states and merging all non distinguishable states
1506 * @param ctx context
1507 * @param a DFA automaton
1510 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1511 struct GNUNET_REGEX_Automaton *a)
1516 GNUNET_assert (DFA == a->type);
1518 // 1. remove unreachable states
1519 dfa_remove_unreachable_states (a);
1521 // 2. remove dead states
1522 dfa_remove_dead_states (a);
1524 // 3. Merge nondistinguishable states
1525 dfa_merge_nondistinguishable_states (ctx, a);
1530 * Context for adding strided transitions to a DFA.
1532 struct GNUNET_REGEX_Strided_Context
1535 * Length of the strides.
1537 const unsigned int stride;
1540 * Strided transitions DLL. New strided transitions will be stored in this DLL
1541 * and afterwards added to the DFA.
1543 struct GNUNET_REGEX_Transition *transitions_head;
1546 * Strided transitions DLL.
1548 struct GNUNET_REGEX_Transition *transitions_tail;
1553 * Recursive helper function to add strides to a DFA.
1555 * @param cls context, contains stride length and strided transitions DLL.
1556 * @param depth current depth of the depth-first traversal of the graph.
1557 * @param label current label, string that contains all labels on the path from
1559 * @param start start state for the depth-first traversal of the graph.
1560 * @param s current state in the depth-first traversal
1563 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1564 struct GNUNET_REGEX_State *start,
1565 struct GNUNET_REGEX_State *s)
1567 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1568 struct GNUNET_REGEX_Transition *t;
1571 if (depth == ctx->stride)
1573 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1574 t->label = GNUNET_strdup (label);
1576 t->from_state = start;
1577 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1582 for (t = s->transitions_head; NULL != t; t = t->next)
1584 /* Do not consider self-loops, because it end's up in too many
1586 if (t->to_state == t->from_state)
1591 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1594 new_label = GNUNET_strdup (t->label);
1596 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1600 GNUNET_free_non_null (label);
1605 * Function called for each state in the DFA. Starts a traversal of depth set in
1606 * context starting from state 's'.
1608 * @param cls context.
1609 * @param count not used.
1610 * @param s current state.
1613 dfa_add_multi_strides (void *cls, const unsigned int count,
1614 struct GNUNET_REGEX_State *s)
1616 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1621 * Adds multi-strided transitions to the given 'dfa'.
1623 * @param regex_ctx regex context needed to add transitions to the automaton.
1624 * @param dfa DFA to which the multi strided transitions should be added.
1625 * @param stride_len length of the strides.
1628 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1629 struct GNUNET_REGEX_Automaton *dfa,
1630 const unsigned int stride_len)
1632 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1633 struct GNUNET_REGEX_Transition *t;
1634 struct GNUNET_REGEX_Transition *t_next;
1636 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1639 // Compute the new transitions of given stride_len
1640 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1641 &dfa_add_multi_strides, &ctx);
1643 // Add all the new transitions to the automaton.
1644 for (t = ctx.transitions_head; NULL != t; t = t_next)
1647 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1648 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1649 GNUNET_free_non_null (t->label);
1653 // Mark this automaton as multistrided
1654 dfa->is_multistrided = GNUNET_YES;
1658 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1659 * and adds new transitions to the given transitions DLL and marks states that
1660 * should be removed by setting state->contained to GNUNET_YES.
1662 * @param dfa DFA for which the paths should be compressed.
1663 * @param start starting state for linear path search.
1664 * @param cur current state in the recursive DFS.
1665 * @param label current label (string of traversed labels).
1666 * @param max_len maximal path compression length.
1667 * @param transitions_head transitions DLL.
1668 * @param transitions_tail transitions DLL.
1671 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1672 struct GNUNET_REGEX_State *start,
1673 struct GNUNET_REGEX_State *cur, char *label,
1674 unsigned int max_len,
1675 struct GNUNET_REGEX_Transition **transitions_head,
1676 struct GNUNET_REGEX_Transition **transitions_tail)
1678 struct GNUNET_REGEX_Transition *t;
1682 if (NULL != label &&
1683 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting
1684 // || cur->transition_count > 1
1685 || GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1686 max_len == strlen (label)) ||
1687 (start == dfa->start && GNUNET_REGEX_INITIAL_BITS == strlen (label))))
1689 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1690 t->label = GNUNET_strdup (label);
1692 t->from_state = start;
1693 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1695 if (GNUNET_NO == cur->marked)
1697 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1702 else if (cur != start)
1703 cur->contained = GNUNET_YES;
1705 if (GNUNET_YES == cur->marked && cur != start)
1708 cur->marked = GNUNET_YES;
1711 for (t = cur->transitions_head; NULL != t; t = t->next)
1714 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1716 new_label = GNUNET_strdup (t->label);
1718 if (t->to_state != cur)
1720 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1721 transitions_head, transitions_tail);
1723 GNUNET_free (new_label);
1728 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1729 * compressed to 0->3 by combining transitions.
1731 * @param regex_ctx context for adding new transitions.
1732 * @param dfa DFA representation, will directly modify the given DFA.
1733 * @param max_len maximal length of the compressed paths.
1736 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1737 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1739 struct GNUNET_REGEX_State *s;
1740 struct GNUNET_REGEX_State *s_next;
1741 struct GNUNET_REGEX_Transition *t;
1742 struct GNUNET_REGEX_Transition *t_next;
1743 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1744 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1749 // Count the incoming transitions on each state.
1750 for (s = dfa->states_head; NULL != s; s = s->next)
1752 for (t = s->transitions_head; NULL != t; t = t->next)
1754 if (NULL != t->to_state)
1755 t->to_state->incoming_transition_count++;
1759 // Unmark all states.
1760 for (s = dfa->states_head; NULL != s; s = s->next)
1762 s->marked = GNUNET_NO;
1763 s->contained = GNUNET_NO;
1766 // Add strides and mark states that can be deleted.
1767 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1768 &transitions_head, &transitions_tail);
1770 // Add all the new transitions to the automaton.
1771 for (t = transitions_head; NULL != t; t = t_next)
1774 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1775 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1776 GNUNET_free_non_null (t->label);
1780 // Remove marked states (including their incoming and outgoing transitions).
1781 for (s = dfa->states_head; NULL != s; s = s_next)
1784 if (GNUNET_YES == s->contained)
1785 automaton_remove_state (dfa, s);
1791 * Creates a new NFA fragment. Needs to be cleared using
1792 * automaton_fragment_clear.
1794 * @param start starting state
1795 * @param end end state
1797 * @return new NFA fragment
1799 static struct GNUNET_REGEX_Automaton *
1800 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1801 struct GNUNET_REGEX_State *end)
1803 struct GNUNET_REGEX_Automaton *n;
1805 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1812 if (NULL == start || NULL == end)
1815 automaton_add_state (n, end);
1816 automaton_add_state (n, start);
1828 * Adds a list of states to the given automaton 'n'.
1830 * @param n automaton to which the states should be added
1831 * @param states_head head of the DLL of states
1832 * @param states_tail tail of the DLL of states
1835 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1836 struct GNUNET_REGEX_State *states_head,
1837 struct GNUNET_REGEX_State *states_tail)
1839 struct GNUNET_REGEX_State *s;
1841 if (NULL == n || NULL == states_head)
1843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1847 if (NULL == n->states_head)
1849 n->states_head = states_head;
1850 n->states_tail = states_tail;
1854 if (NULL != states_head)
1856 n->states_tail->next = states_head;
1857 n->states_tail = states_tail;
1860 for (s = states_head; NULL != s; s = s->next)
1866 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1868 * @param ctx context
1869 * @param accepting is it an accepting state or not
1871 * @return new NFA state
1873 static struct GNUNET_REGEX_State *
1874 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1876 struct GNUNET_REGEX_State *s;
1878 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1879 s->id = ctx->state_id++;
1880 s->accepting = accepting;
1881 s->marked = GNUNET_NO;
1887 GNUNET_asprintf (&s->name, "s%i", s->id);
1894 * Calculates the NFA closure set for the given state.
1896 * @param nfa the NFA containing 's'
1897 * @param s starting point state
1898 * @param label transitioning label on which to base the closure on,
1899 * pass NULL for epsilon transition
1901 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1903 static struct GNUNET_REGEX_StateSet *
1904 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1905 struct GNUNET_REGEX_State *s, const char *label)
1907 struct GNUNET_REGEX_StateSet *cls;
1908 struct GNUNET_REGEX_StateSet *cls_check;
1909 struct GNUNET_REGEX_State *clsstate;
1910 struct GNUNET_REGEX_State *currentstate;
1911 struct GNUNET_REGEX_Transition *ctran;
1916 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1917 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1919 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1920 clsstate->contained = 0;
1922 // Add start state to closure only for epsilon closure
1924 GNUNET_array_append (cls->states, cls->len, s);
1926 GNUNET_array_append (cls_check->states, cls_check->len, s);
1927 while (cls_check->len > 0)
1929 currentstate = cls_check->states[cls_check->len - 1];
1930 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1932 for (ctran = currentstate->transitions_head; NULL != ctran;
1933 ctran = ctran->next)
1935 if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
1937 clsstate = ctran->to_state;
1939 if (NULL != clsstate && 0 == clsstate->contained)
1941 GNUNET_array_append (cls->states, cls->len, clsstate);
1942 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1943 clsstate->contained = 1;
1948 GNUNET_assert (0 == cls_check->len);
1949 GNUNET_free (cls_check);
1953 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1961 * Calculates the closure set for the given set of states.
1963 * @param nfa the NFA containing 's'
1964 * @param states list of states on which to base the closure on
1965 * @param label transitioning label for which to base the closure on,
1966 * pass NULL for epsilon transition
1968 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1970 static struct GNUNET_REGEX_StateSet *
1971 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1972 struct GNUNET_REGEX_StateSet *states, const char *label)
1974 struct GNUNET_REGEX_State *s;
1975 struct GNUNET_REGEX_StateSet *sset;
1976 struct GNUNET_REGEX_StateSet *cls;
1980 unsigned int contains;
1985 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1987 for (i = 0; i < states->len; i++)
1989 s = states->states[i];
1990 sset = nfa_closure_create (nfa, s, label);
1992 for (j = 0; j < sset->len; j++)
1995 for (k = 0; k < cls->len; k++)
1997 if (sset->states[j]->id == cls->states[k]->id)
2004 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2006 state_set_clear (sset);
2010 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2018 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2020 * @param ctx context
2023 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2025 struct GNUNET_REGEX_Automaton *a;
2026 struct GNUNET_REGEX_Automaton *b;
2027 struct GNUNET_REGEX_Automaton *new_nfa;
2029 b = ctx->stack_tail;
2030 GNUNET_assert (NULL != b);
2031 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2032 a = ctx->stack_tail;
2033 GNUNET_assert (NULL != a);
2034 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2036 state_add_transition (ctx, a->end, NULL, b->start);
2037 a->end->accepting = 0;
2038 b->end->accepting = 1;
2040 new_nfa = nfa_fragment_create (NULL, NULL);
2041 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2042 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2043 new_nfa->start = a->start;
2044 new_nfa->end = b->end;
2045 new_nfa->state_count += a->state_count + b->state_count;
2046 automaton_fragment_clear (a);
2047 automaton_fragment_clear (b);
2049 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2054 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2056 * @param ctx context
2059 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2061 struct GNUNET_REGEX_Automaton *a;
2062 struct GNUNET_REGEX_Automaton *new_nfa;
2063 struct GNUNET_REGEX_State *start;
2064 struct GNUNET_REGEX_State *end;
2066 a = ctx->stack_tail;
2070 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2071 "nfa_add_star_op failed, because there was no element on the stack");
2075 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2077 start = nfa_state_create (ctx, 0);
2078 end = nfa_state_create (ctx, 1);
2080 state_add_transition (ctx, start, NULL, a->start);
2081 state_add_transition (ctx, start, NULL, end);
2082 state_add_transition (ctx, a->end, NULL, a->start);
2083 state_add_transition (ctx, a->end, NULL, end);
2085 a->end->accepting = 0;
2088 new_nfa = nfa_fragment_create (start, end);
2089 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2090 automaton_fragment_clear (a);
2092 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2097 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2099 * @param ctx context
2102 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2104 struct GNUNET_REGEX_Automaton *a;
2106 a = ctx->stack_tail;
2110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2111 "nfa_add_plus_op failed, because there was no element on the stack");
2115 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2117 state_add_transition (ctx, a->end, NULL, a->start);
2119 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2124 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2126 * @param ctx context
2129 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2131 struct GNUNET_REGEX_Automaton *a;
2132 struct GNUNET_REGEX_Automaton *new_nfa;
2133 struct GNUNET_REGEX_State *start;
2134 struct GNUNET_REGEX_State *end;
2136 a = ctx->stack_tail;
2140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2141 "nfa_add_question_op failed, because there was no element on the stack");
2145 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2147 start = nfa_state_create (ctx, 0);
2148 end = nfa_state_create (ctx, 1);
2150 state_add_transition (ctx, start, NULL, a->start);
2151 state_add_transition (ctx, start, NULL, end);
2152 state_add_transition (ctx, a->end, NULL, end);
2154 a->end->accepting = 0;
2156 new_nfa = nfa_fragment_create (start, end);
2157 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2158 automaton_fragment_clear (a);
2160 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2165 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2166 * alternates between a and b (a|b)
2168 * @param ctx context
2171 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2173 struct GNUNET_REGEX_Automaton *a;
2174 struct GNUNET_REGEX_Automaton *b;
2175 struct GNUNET_REGEX_Automaton *new_nfa;
2176 struct GNUNET_REGEX_State *start;
2177 struct GNUNET_REGEX_State *end;
2179 b = ctx->stack_tail;
2180 GNUNET_assert (NULL != b);
2181 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2182 a = ctx->stack_tail;
2183 GNUNET_assert (NULL != a);
2184 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2186 start = nfa_state_create (ctx, 0);
2187 end = nfa_state_create (ctx, 1);
2188 state_add_transition (ctx, start, NULL, a->start);
2189 state_add_transition (ctx, start, NULL, b->start);
2191 state_add_transition (ctx, a->end, NULL, end);
2192 state_add_transition (ctx, b->end, NULL, end);
2194 a->end->accepting = 0;
2195 b->end->accepting = 0;
2198 new_nfa = nfa_fragment_create (start, end);
2199 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2200 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2201 automaton_fragment_clear (a);
2202 automaton_fragment_clear (b);
2204 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2209 * Adds a new nfa fragment to the stack
2211 * @param ctx context
2212 * @param label label for nfa transition
2215 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2217 struct GNUNET_REGEX_Automaton *n;
2218 struct GNUNET_REGEX_State *start;
2219 struct GNUNET_REGEX_State *end;
2221 GNUNET_assert (NULL != ctx);
2223 start = nfa_state_create (ctx, 0);
2224 end = nfa_state_create (ctx, 1);
2225 state_add_transition (ctx, start, label, end);
2226 n = nfa_fragment_create (start, end);
2227 GNUNET_assert (NULL != n);
2228 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2233 * Initialize a new context
2235 * @param ctx context
2238 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2246 ctx->transition_id = 0;
2247 ctx->stack_head = NULL;
2248 ctx->stack_tail = NULL;
2253 * Construct an NFA by parsing the regex string of length 'len'.
2255 * @param regex regular expression string
2256 * @param len length of the string
2258 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2260 struct GNUNET_REGEX_Automaton *
2261 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2263 struct GNUNET_REGEX_Context ctx;
2264 struct GNUNET_REGEX_Automaton *nfa;
2269 unsigned int altcount;
2270 unsigned int atomcount;
2271 unsigned int pcount;
2278 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2281 "Could not parse regex. Empty regex string provided.\n");
2286 GNUNET_REGEX_context_init (&ctx);
2296 for (count = 0; count < len && *regexp; count++, regexp++)
2304 nfa_add_concatenation (&ctx);
2306 GNUNET_array_grow (p, pcount, pcount + 1);
2307 p[pcount - 1].altcount = altcount;
2308 p[pcount - 1].atomcount = atomcount;
2315 error_msg = "Cannot append '|' to nothing";
2318 while (--atomcount > 0)
2319 nfa_add_concatenation (&ctx);
2325 error_msg = "Missing opening '('";
2330 // Ignore this: "()"
2332 altcount = p[pcount].altcount;
2333 atomcount = p[pcount].atomcount;
2336 while (--atomcount > 0)
2337 nfa_add_concatenation (&ctx);
2338 for (; altcount > 0; altcount--)
2339 nfa_add_alternation (&ctx);
2341 altcount = p[pcount].altcount;
2342 atomcount = p[pcount].atomcount;
2348 error_msg = "Cannot append '*' to nothing";
2351 nfa_add_star_op (&ctx);
2356 error_msg = "Cannot append '+' to nothing";
2359 nfa_add_plus_op (&ctx);
2364 error_msg = "Cannot append '?' to nothing";
2367 nfa_add_question_op (&ctx);
2373 nfa_add_concatenation (&ctx);
2375 curlabel[0] = *regexp;
2376 nfa_add_label (&ctx, curlabel);
2383 error_msg = "Unbalanced parenthesis";
2386 while (--atomcount > 0)
2387 nfa_add_concatenation (&ctx);
2388 for (; altcount > 0; altcount--)
2389 nfa_add_alternation (&ctx);
2391 GNUNET_free_non_null (p);
2393 nfa = ctx.stack_tail;
2394 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2396 if (NULL != ctx.stack_head)
2398 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2402 /* Remember the regex that was used to generate this NFA */
2403 nfa->regex = GNUNET_strdup (regex);
2405 /* create depth-first numbering of the states for pretty printing */
2406 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2408 /* No multistriding added so far */
2409 nfa->is_multistrided = GNUNET_NO;
2414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2415 if (NULL != error_msg)
2416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2418 GNUNET_free_non_null (p);
2420 while (NULL != (nfa = ctx.stack_head))
2422 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2423 GNUNET_REGEX_automaton_destroy (nfa);
2431 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2433 * @param ctx context.
2434 * @param nfa NFA automaton.
2435 * @param dfa DFA automaton.
2436 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2440 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2441 struct GNUNET_REGEX_Automaton *nfa,
2442 struct GNUNET_REGEX_Automaton *dfa,
2443 struct GNUNET_REGEX_State *dfa_state)
2445 struct GNUNET_REGEX_Transition *ctran;
2446 struct GNUNET_REGEX_State *state_iter;
2447 struct GNUNET_REGEX_State *new_dfa_state;
2448 struct GNUNET_REGEX_State *state_contains;
2449 struct GNUNET_REGEX_StateSet *tmp;
2450 struct GNUNET_REGEX_StateSet *nfa_set;
2452 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2454 if (NULL == ctran->label || NULL != ctran->to_state)
2457 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2458 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2459 state_set_clear (tmp);
2460 new_dfa_state = dfa_state_create (ctx, nfa_set);
2461 state_contains = NULL;
2462 for (state_iter = dfa->states_head; NULL != state_iter;
2463 state_iter = state_iter->next)
2465 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2466 state_contains = state_iter;
2469 if (NULL == state_contains)
2471 automaton_add_state (dfa, new_dfa_state);
2472 ctran->to_state = new_dfa_state;
2473 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2477 ctran->to_state = state_contains;
2478 automaton_destroy_state (new_dfa_state);
2485 * Construct DFA for the given 'regex' of length 'len'
2487 * @param regex regular expression string
2488 * @param len length of the regular expression
2490 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2492 struct GNUNET_REGEX_Automaton *
2493 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
2495 struct GNUNET_REGEX_Context ctx;
2496 struct GNUNET_REGEX_Automaton *dfa;
2497 struct GNUNET_REGEX_Automaton *nfa;
2498 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2500 GNUNET_REGEX_context_init (&ctx);
2503 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2508 "Could not create DFA, because NFA creation failed\n");
2512 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2514 dfa->state_count = 0;
2515 dfa->states_head = NULL;
2516 dfa->states_tail = NULL;
2517 dfa->regex = GNUNET_strdup (regex);
2518 dfa->is_multistrided = GNUNET_NO;
2520 // Create DFA start state from epsilon closure
2521 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, 0);
2522 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2523 automaton_add_state (dfa, dfa->start);
2525 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2527 GNUNET_REGEX_automaton_destroy (nfa);
2530 dfa_minimize (&ctx, dfa);
2532 // Create proofs for all states
2533 automaton_create_proofs (dfa);
2535 // Compress DFA paths
2536 dfa_compress_paths (&ctx, dfa, 8);
2538 // Add strides to DFA
2539 //GNUNET_REGEX_dfa_add_multi_strides (&ctx, dfa, 2);
2546 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2549 * @param a automaton to be destroyed
2552 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2554 struct GNUNET_REGEX_State *s;
2555 struct GNUNET_REGEX_State *next_state;
2560 GNUNET_free_non_null (a->regex);
2561 GNUNET_free_non_null (a->canonical_regex);
2563 for (s = a->states_head; NULL != s; s = next_state)
2565 next_state = s->next;
2566 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2567 automaton_destroy_state (s);
2575 * Evaluates the given string using the given DFA automaton
2577 * @param a automaton, type must be DFA
2578 * @param string string that should be evaluated
2580 * @return 0 if string matches, non 0 otherwise
2583 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2586 struct GNUNET_REGEX_State *s;
2587 unsigned int step_len;
2591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2592 "Tried to evaluate DFA, but NFA automaton given");
2598 // If the string is empty but the starting state is accepting, we accept.
2599 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2602 for (strp = string; NULL != strp && *strp; strp += step_len)
2604 step_len = dfa_move (&s, strp);
2610 if (NULL != s && s->accepting)
2618 * Evaluates the given string using the given NFA automaton
2620 * @param a automaton, type must be NFA
2621 * @param string string that should be evaluated
2623 * @return 0 if string matches, non 0 otherwise
2626 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2630 struct GNUNET_REGEX_State *s;
2631 struct GNUNET_REGEX_StateSet *sset;
2632 struct GNUNET_REGEX_StateSet *new_sset;
2638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2639 "Tried to evaluate NFA, but DFA automaton given");
2643 // If the string is empty but the starting state is accepting, we accept.
2644 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2648 sset = nfa_closure_create (a, a->start, 0);
2651 for (strp = string; NULL != strp && *strp; strp++)
2654 new_sset = nfa_closure_set_create (a, sset, str);
2655 state_set_clear (sset);
2656 sset = nfa_closure_set_create (a, new_sset, 0);
2657 state_set_clear (new_sset);
2660 for (i = 0; i < sset->len; i++)
2662 s = sset->states[i];
2663 if (NULL != s && s->accepting)
2670 state_set_clear (sset);
2676 * Evaluates the given 'string' against the given compiled regex
2678 * @param a automaton
2679 * @param string string to check
2681 * @return 0 if string matches, non 0 otherwise
2684 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2691 result = evaluate_dfa (a, string);
2694 result = evaluate_nfa (a, string);
2697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2698 "Evaluating regex failed, automaton has no type!\n");
2699 result = GNUNET_SYSERR;
2708 * Get the canonical regex of the given automaton.
2709 * When constructing the automaton a proof is computed for each state,
2710 * consisting of the regular expression leading to this state. A complete
2711 * regex for the automaton can be computed by combining these proofs.
2712 * As of now this function is only useful for testing.
2714 * @param a automaton for which the canonical regex should be returned.
2719 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2724 return a->canonical_regex;
2729 * Get the number of transitions that are contained in the given automaton.
2731 * @param a automaton for which the number of transitions should be returned.
2733 * @return number of transitions in the given automaton.
2736 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2738 unsigned int t_count;
2739 struct GNUNET_REGEX_State *s;
2745 for (s = a->states_head; NULL != s; s = s->next)
2746 t_count += s->transition_count;
2753 * Get the first key for the given 'input_string'. This hashes the first x bits
2754 * of the 'input_string'.
2756 * @param input_string string.
2757 * @param string_len length of the 'input_string'.
2758 * @param key pointer to where to write the hash code.
2760 * @return number of bits of 'input_string' that have been consumed
2761 * to construct the key
2764 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2765 struct GNUNET_HashCode * key)
2771 GNUNET_REGEX_INITIAL_BITS ? string_len : GNUNET_REGEX_INITIAL_BITS;
2773 if (NULL == input_string)
2775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2779 GNUNET_CRYPTO_hash (input_string, size, key);
2786 * Check if the given 'proof' matches the given 'key'.
2788 * @param proof partial regex of a state.
2789 * @param key hash of a state.
2791 * @return GNUNET_OK if the proof is valid for the given key.
2794 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2796 struct GNUNET_HashCode key_check;
2798 if (NULL == proof || NULL == key)
2800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2804 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2806 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2811 * Recursive function that calls the iterator for each synthetic start state.
2813 * @param min_len minimum length of the path in the graph.
2814 * @param max_len maximum length of the path in the graph.
2815 * @param consumed_string string consumed by traversing the graph till this state.
2816 * @param state current state of the automaton.
2817 * @param iterator iterator function called for each edge.
2818 * @param iterator_cls closure for the iterator function.
2821 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2822 char *consumed_string, struct GNUNET_REGEX_State *state,
2823 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2827 struct GNUNET_REGEX_Transition *t;
2828 unsigned int num_edges = state->transition_count;
2829 struct GNUNET_REGEX_Edge edges[num_edges];
2830 struct GNUNET_REGEX_Edge edge[1];
2831 struct GNUNET_HashCode hash;
2832 struct GNUNET_HashCode hash_new;
2834 unsigned int cur_len;
2836 if (NULL != consumed_string)
2837 cur_len = strlen (consumed_string);
2841 if (cur_len >= min_len && cur_len > 0 && NULL != consumed_string)
2843 if (cur_len <= max_len)
2845 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2847 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2850 edges[i].label = t->label;
2851 edges[i].destination = t->to_state->hash;
2853 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2854 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2858 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2859 state->transition_count < 1 && cur_len < max_len)
2861 // Special case for regex consisting of just a string that is shorter than
2863 edge[0].label = &consumed_string[cur_len - 1];
2864 edge[0].destination = state->hash;
2865 temp = GNUNET_strdup (consumed_string);
2866 temp[cur_len - 1] = '\0';
2867 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2868 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2872 else if (max_len < cur_len)
2874 // Case where the concatenated labels are longer than max_len, then split.
2875 edge[0].label = &consumed_string[max_len];
2876 edge[0].destination = state->hash;
2877 temp = GNUNET_strdup (consumed_string);
2878 temp[max_len] = '\0';
2879 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2880 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2885 if (cur_len < max_len)
2887 for (t = state->transitions_head; NULL != t; t = t->next)
2889 if (NULL != consumed_string)
2890 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2892 GNUNET_asprintf (&temp, "%s", t->label);
2894 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2903 * Iterate over all edges starting from start state of automaton 'a'. Calling
2904 * iterator for each edge.
2906 * @param a automaton.
2907 * @param iterator iterator called for each edge.
2908 * @param iterator_cls closure.
2911 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2912 GNUNET_REGEX_KeyIterator iterator,
2915 struct GNUNET_REGEX_State *s;
2917 for (s = a->states_head; NULL != s; s = s->next)
2919 struct GNUNET_REGEX_Edge edges[s->transition_count];
2920 unsigned int num_edges;
2922 num_edges = state_get_edges (s, edges);
2924 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
2925 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
2928 s->marked = GNUNET_NO;
2931 iterate_initial_edge (GNUNET_REGEX_INITIAL_BITS, GNUNET_REGEX_INITIAL_BITS,
2932 NULL, a->start, iterator, iterator_cls);
2937 * Create a string with binary IP notation for the given 'addr' in 'str'.
2939 * @param af address family of the given 'addr'.
2940 * @param addr address that should be converted to a string.
2941 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
2942 * @param str string that will contain binary notation of 'addr'. Expected
2943 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
2946 iptobinstr (const int af, const void *addr, char *str)
2954 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
2958 for (i = 31; i >= 0; i--)
2960 *str = (b & 1) + '0';
2968 struct in6_addr b = *(const struct in6_addr *) addr;
2972 for (i = 127; i >= 0; i--)
2974 *str = (b.s6_addr[i / 8] & 1) + '0';
2976 b.s6_addr[i / 8] >>= 1;
2985 * Get the ipv4 network prefix from the given 'netmask'.
2987 * @param netmask netmask for which to get the prefix len.
2989 * @return length of ipv4 prefix for 'netmask'.
2992 ipv4netmasktoprefixlen (const char *netmask)
2998 if (1 != inet_pton (AF_INET, netmask, &a))
3001 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3008 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3010 * @param ip IPv4 representation.
3011 * @param netmask netmask for the ip.
3012 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3016 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3019 unsigned int pfxlen;
3021 pfxlen = ipv4netmasktoprefixlen (netmask);
3022 iptobinstr (AF_INET, ip, rxstr);
3023 rxstr[pfxlen] = '\0';
3025 strcat (rxstr, "(0|1)+");
3030 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3032 * @param ipv6 IPv6 representation.
3033 * @param prefixlen length of the ipv6 prefix.
3034 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3038 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3041 iptobinstr (AF_INET6, ipv6, rxstr);
3042 rxstr[prefixlen] = '\0';
3043 if (prefixlen < 128)
3044 strcat (rxstr, "(0|1)+");