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_State *s_next;
1379 struct GNUNET_REGEX_Transition *t;
1382 GNUNET_assert (DFA == a->type);
1384 for (s = a->states_head; NULL != s; s = s_next)
1392 for (t = s->transitions_head; NULL != t; t = t->next)
1394 if (NULL != t->to_state && t->to_state != s)
1404 // state s is dead, remove it
1405 automaton_remove_state (a, s);
1411 * Merge all non distinguishable states in the DFA 'a'
1413 * @param ctx context
1414 * @param a DFA automaton
1417 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1418 struct GNUNET_REGEX_Automaton *a)
1420 int table[a->state_count][a->state_count];
1421 struct GNUNET_REGEX_State *s1;
1422 struct GNUNET_REGEX_State *s2;
1423 struct GNUNET_REGEX_Transition *t1;
1424 struct GNUNET_REGEX_Transition *t2;
1425 struct GNUNET_REGEX_State *s1_next;
1426 struct GNUNET_REGEX_State *s2_next;
1428 unsigned int num_equal_edges;
1431 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
1437 // Mark all pairs of accepting/!accepting states
1438 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1440 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1442 table[s1->marked][s2->marked] = 0;
1444 if ((s1->accepting && !s2->accepting) ||
1445 (!s1->accepting && s2->accepting))
1447 table[s1->marked][s2->marked] = 1;
1452 // Find all equal states
1457 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1459 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1461 if (0 != table[s1->marked][s2->marked])
1464 num_equal_edges = 0;
1465 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1467 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1469 if (0 == strcmp (t1->label, t2->label))
1472 if (0 != table[t1->to_state->marked][t2->to_state->marked] ||
1473 0 != table[t2->to_state->marked][t1->to_state->marked])
1475 table[s1->marked][s2->marked] = 1;
1481 if (num_equal_edges != s1->transition_count ||
1482 num_equal_edges != s2->transition_count)
1484 // Make sure ALL edges of possible equal states are the same
1485 table[s1->marked][s2->marked] = -2;
1491 // Merge states that are equal
1492 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1495 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1498 if (table[s1->marked][s2->marked] == 0)
1499 automaton_merge_states (ctx, a, s1, s2);
1506 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1507 * dead states and merging all non distinguishable states
1509 * @param ctx context
1510 * @param a DFA automaton
1513 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1514 struct GNUNET_REGEX_Automaton *a)
1519 GNUNET_assert (DFA == a->type);
1521 // 1. remove unreachable states
1522 dfa_remove_unreachable_states (a);
1524 // 2. remove dead states
1525 dfa_remove_dead_states (a);
1527 // 3. Merge nondistinguishable states
1528 dfa_merge_nondistinguishable_states (ctx, a);
1533 * Context for adding strided transitions to a DFA.
1535 struct GNUNET_REGEX_Strided_Context
1538 * Length of the strides.
1540 const unsigned int stride;
1543 * Strided transitions DLL. New strided transitions will be stored in this DLL
1544 * and afterwards added to the DFA.
1546 struct GNUNET_REGEX_Transition *transitions_head;
1549 * Strided transitions DLL.
1551 struct GNUNET_REGEX_Transition *transitions_tail;
1556 * Recursive helper function to add strides to a DFA.
1558 * @param cls context, contains stride length and strided transitions DLL.
1559 * @param depth current depth of the depth-first traversal of the graph.
1560 * @param label current label, string that contains all labels on the path from
1562 * @param start start state for the depth-first traversal of the graph.
1563 * @param s current state in the depth-first traversal
1566 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1567 struct GNUNET_REGEX_State *start,
1568 struct GNUNET_REGEX_State *s)
1570 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1571 struct GNUNET_REGEX_Transition *t;
1574 if (depth == ctx->stride)
1576 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1577 t->label = GNUNET_strdup (label);
1579 t->from_state = start;
1580 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1585 for (t = s->transitions_head; NULL != t; t = t->next)
1587 /* Do not consider self-loops, because it end's up in too many
1589 if (t->to_state == t->from_state)
1594 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1597 new_label = GNUNET_strdup (t->label);
1599 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1603 GNUNET_free_non_null (label);
1608 * Function called for each state in the DFA. Starts a traversal of depth set in
1609 * context starting from state 's'.
1611 * @param cls context.
1612 * @param count not used.
1613 * @param s current state.
1616 dfa_add_multi_strides (void *cls, const unsigned int count,
1617 struct GNUNET_REGEX_State *s)
1619 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1624 * Adds multi-strided transitions to the given 'dfa'.
1626 * @param regex_ctx regex context needed to add transitions to the automaton.
1627 * @param dfa DFA to which the multi strided transitions should be added.
1628 * @param stride_len length of the strides.
1631 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1632 struct GNUNET_REGEX_Automaton *dfa,
1633 const unsigned int stride_len)
1635 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1636 struct GNUNET_REGEX_Transition *t;
1637 struct GNUNET_REGEX_Transition *t_next;
1639 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1642 // Compute the new transitions of given stride_len
1643 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1644 &dfa_add_multi_strides, &ctx);
1646 // Add all the new transitions to the automaton.
1647 for (t = ctx.transitions_head; NULL != t; t = t_next)
1650 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1651 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1652 GNUNET_free_non_null (t->label);
1656 // Mark this automaton as multistrided
1657 dfa->is_multistrided = GNUNET_YES;
1661 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1662 * and adds new transitions to the given transitions DLL and marks states that
1663 * should be removed by setting state->contained to GNUNET_YES.
1665 * @param dfa DFA for which the paths should be compressed.
1666 * @param start starting state for linear path search.
1667 * @param cur current state in the recursive DFS.
1668 * @param label current label (string of traversed labels).
1669 * @param max_len maximal path compression length.
1670 * @param transitions_head transitions DLL.
1671 * @param transitions_tail transitions DLL.
1674 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1675 struct GNUNET_REGEX_State *start,
1676 struct GNUNET_REGEX_State *cur, char *label,
1677 unsigned int max_len,
1678 struct GNUNET_REGEX_Transition **transitions_head,
1679 struct GNUNET_REGEX_Transition **transitions_tail)
1681 struct GNUNET_REGEX_Transition *t;
1685 if (NULL != label &&
1686 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting
1687 // || cur->transition_count > 1
1688 || GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1689 max_len == strlen (label)) ||
1690 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
1692 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1693 t->label = GNUNET_strdup (label);
1695 t->from_state = start;
1696 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1698 if (GNUNET_NO == cur->marked)
1700 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1705 else if (cur != start)
1706 cur->contained = GNUNET_YES;
1708 if (GNUNET_YES == cur->marked && cur != start)
1711 cur->marked = GNUNET_YES;
1714 for (t = cur->transitions_head; NULL != t; t = t->next)
1717 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1719 new_label = GNUNET_strdup (t->label);
1721 if (t->to_state != cur)
1723 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1724 transitions_head, transitions_tail);
1726 GNUNET_free (new_label);
1731 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1732 * compressed to 0->3 by combining transitions.
1734 * @param regex_ctx context for adding new transitions.
1735 * @param dfa DFA representation, will directly modify the given DFA.
1736 * @param max_len maximal length of the compressed paths.
1739 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1740 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1742 struct GNUNET_REGEX_State *s;
1743 struct GNUNET_REGEX_State *s_next;
1744 struct GNUNET_REGEX_Transition *t;
1745 struct GNUNET_REGEX_Transition *t_next;
1746 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1747 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1752 // Count the incoming transitions on each state.
1753 for (s = dfa->states_head; NULL != s; s = s->next)
1755 for (t = s->transitions_head; NULL != t; t = t->next)
1757 if (NULL != t->to_state)
1758 t->to_state->incoming_transition_count++;
1762 // Unmark all states.
1763 for (s = dfa->states_head; NULL != s; s = s->next)
1765 s->marked = GNUNET_NO;
1766 s->contained = GNUNET_NO;
1769 // Add strides and mark states that can be deleted.
1770 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1771 &transitions_head, &transitions_tail);
1773 // Add all the new transitions to the automaton.
1774 for (t = transitions_head; NULL != t; t = t_next)
1777 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1778 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1779 GNUNET_free_non_null (t->label);
1783 // Remove marked states (including their incoming and outgoing transitions).
1784 for (s = dfa->states_head; NULL != s; s = s_next)
1787 if (GNUNET_YES == s->contained)
1788 automaton_remove_state (dfa, s);
1794 * Creates a new NFA fragment. Needs to be cleared using
1795 * automaton_fragment_clear.
1797 * @param start starting state
1798 * @param end end state
1800 * @return new NFA fragment
1802 static struct GNUNET_REGEX_Automaton *
1803 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1804 struct GNUNET_REGEX_State *end)
1806 struct GNUNET_REGEX_Automaton *n;
1808 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1815 if (NULL == start || NULL == end)
1818 automaton_add_state (n, end);
1819 automaton_add_state (n, start);
1831 * Adds a list of states to the given automaton 'n'.
1833 * @param n automaton to which the states should be added
1834 * @param states_head head of the DLL of states
1835 * @param states_tail tail of the DLL of states
1838 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1839 struct GNUNET_REGEX_State *states_head,
1840 struct GNUNET_REGEX_State *states_tail)
1842 struct GNUNET_REGEX_State *s;
1844 if (NULL == n || NULL == states_head)
1846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1850 if (NULL == n->states_head)
1852 n->states_head = states_head;
1853 n->states_tail = states_tail;
1857 if (NULL != states_head)
1859 n->states_tail->next = states_head;
1860 n->states_tail = states_tail;
1863 for (s = states_head; NULL != s; s = s->next)
1869 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1871 * @param ctx context
1872 * @param accepting is it an accepting state or not
1874 * @return new NFA state
1876 static struct GNUNET_REGEX_State *
1877 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1879 struct GNUNET_REGEX_State *s;
1881 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1882 s->id = ctx->state_id++;
1883 s->accepting = accepting;
1884 s->marked = GNUNET_NO;
1890 GNUNET_asprintf (&s->name, "s%i", s->id);
1897 * Calculates the NFA closure set for the given state.
1899 * @param nfa the NFA containing 's'
1900 * @param s starting point state
1901 * @param label transitioning label on which to base the closure on,
1902 * pass NULL for epsilon transition
1904 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1906 static struct GNUNET_REGEX_StateSet *
1907 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1908 struct GNUNET_REGEX_State *s, const char *label)
1910 struct GNUNET_REGEX_StateSet *cls;
1911 struct GNUNET_REGEX_StateSet *cls_check;
1912 struct GNUNET_REGEX_State *clsstate;
1913 struct GNUNET_REGEX_State *currentstate;
1914 struct GNUNET_REGEX_Transition *ctran;
1919 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1920 cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1922 for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
1923 clsstate->contained = 0;
1925 // Add start state to closure only for epsilon closure
1927 GNUNET_array_append (cls->states, cls->len, s);
1929 GNUNET_array_append (cls_check->states, cls_check->len, s);
1930 while (cls_check->len > 0)
1932 currentstate = cls_check->states[cls_check->len - 1];
1933 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1935 for (ctran = currentstate->transitions_head; NULL != ctran;
1936 ctran = ctran->next)
1938 if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
1940 clsstate = ctran->to_state;
1942 if (NULL != clsstate && 0 == clsstate->contained)
1944 GNUNET_array_append (cls->states, cls->len, clsstate);
1945 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1946 clsstate->contained = 1;
1951 GNUNET_assert (0 == cls_check->len);
1952 GNUNET_free (cls_check);
1956 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
1964 * Calculates the closure set for the given set of states.
1966 * @param nfa the NFA containing 's'
1967 * @param states list of states on which to base the closure on
1968 * @param label transitioning label for which to base the closure on,
1969 * pass NULL for epsilon transition
1971 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1973 static struct GNUNET_REGEX_StateSet *
1974 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
1975 struct GNUNET_REGEX_StateSet *states, const char *label)
1977 struct GNUNET_REGEX_State *s;
1978 struct GNUNET_REGEX_StateSet *sset;
1979 struct GNUNET_REGEX_StateSet *cls;
1983 unsigned int contains;
1988 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1990 for (i = 0; i < states->len; i++)
1992 s = states->states[i];
1993 sset = nfa_closure_create (nfa, s, label);
1995 for (j = 0; j < sset->len; j++)
1998 for (k = 0; k < cls->len; k++)
2000 if (sset->states[j]->id == cls->states[k]->id)
2007 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2009 state_set_clear (sset);
2013 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2021 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2023 * @param ctx context
2026 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2028 struct GNUNET_REGEX_Automaton *a;
2029 struct GNUNET_REGEX_Automaton *b;
2030 struct GNUNET_REGEX_Automaton *new_nfa;
2032 b = ctx->stack_tail;
2033 GNUNET_assert (NULL != b);
2034 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2035 a = ctx->stack_tail;
2036 GNUNET_assert (NULL != a);
2037 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2039 state_add_transition (ctx, a->end, NULL, b->start);
2040 a->end->accepting = 0;
2041 b->end->accepting = 1;
2043 new_nfa = nfa_fragment_create (NULL, NULL);
2044 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2045 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2046 new_nfa->start = a->start;
2047 new_nfa->end = b->end;
2048 new_nfa->state_count += a->state_count + b->state_count;
2049 automaton_fragment_clear (a);
2050 automaton_fragment_clear (b);
2052 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2057 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2059 * @param ctx context
2062 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2064 struct GNUNET_REGEX_Automaton *a;
2065 struct GNUNET_REGEX_Automaton *new_nfa;
2066 struct GNUNET_REGEX_State *start;
2067 struct GNUNET_REGEX_State *end;
2069 a = ctx->stack_tail;
2073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2074 "nfa_add_star_op failed, because there was no element on the stack");
2078 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2080 start = nfa_state_create (ctx, 0);
2081 end = nfa_state_create (ctx, 1);
2083 state_add_transition (ctx, start, NULL, a->start);
2084 state_add_transition (ctx, start, NULL, end);
2085 state_add_transition (ctx, a->end, NULL, a->start);
2086 state_add_transition (ctx, a->end, NULL, end);
2088 a->end->accepting = 0;
2091 new_nfa = nfa_fragment_create (start, end);
2092 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2093 automaton_fragment_clear (a);
2095 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2100 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2102 * @param ctx context
2105 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2107 struct GNUNET_REGEX_Automaton *a;
2109 a = ctx->stack_tail;
2113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2114 "nfa_add_plus_op failed, because there was no element on the stack");
2118 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2120 state_add_transition (ctx, a->end, NULL, a->start);
2122 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2127 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2129 * @param ctx context
2132 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2134 struct GNUNET_REGEX_Automaton *a;
2135 struct GNUNET_REGEX_Automaton *new_nfa;
2136 struct GNUNET_REGEX_State *start;
2137 struct GNUNET_REGEX_State *end;
2139 a = ctx->stack_tail;
2143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2144 "nfa_add_question_op failed, because there was no element on the stack");
2148 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2150 start = nfa_state_create (ctx, 0);
2151 end = nfa_state_create (ctx, 1);
2153 state_add_transition (ctx, start, NULL, a->start);
2154 state_add_transition (ctx, start, NULL, end);
2155 state_add_transition (ctx, a->end, NULL, end);
2157 a->end->accepting = 0;
2159 new_nfa = nfa_fragment_create (start, end);
2160 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2161 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2162 automaton_fragment_clear (a);
2167 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2168 * alternates between a and b (a|b)
2170 * @param ctx context
2173 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2175 struct GNUNET_REGEX_Automaton *a;
2176 struct GNUNET_REGEX_Automaton *b;
2177 struct GNUNET_REGEX_Automaton *new_nfa;
2178 struct GNUNET_REGEX_State *start;
2179 struct GNUNET_REGEX_State *end;
2181 b = ctx->stack_tail;
2182 GNUNET_assert (NULL != b);
2183 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2184 a = ctx->stack_tail;
2185 GNUNET_assert (NULL != a);
2186 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2188 start = nfa_state_create (ctx, 0);
2189 end = nfa_state_create (ctx, 1);
2190 state_add_transition (ctx, start, NULL, a->start);
2191 state_add_transition (ctx, start, NULL, b->start);
2193 state_add_transition (ctx, a->end, NULL, end);
2194 state_add_transition (ctx, b->end, NULL, end);
2196 a->end->accepting = 0;
2197 b->end->accepting = 0;
2200 new_nfa = nfa_fragment_create (start, end);
2201 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2202 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2203 automaton_fragment_clear (a);
2204 automaton_fragment_clear (b);
2206 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2211 * Adds a new nfa fragment to the stack
2213 * @param ctx context
2214 * @param label label for nfa transition
2217 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2219 struct GNUNET_REGEX_Automaton *n;
2220 struct GNUNET_REGEX_State *start;
2221 struct GNUNET_REGEX_State *end;
2223 GNUNET_assert (NULL != ctx);
2225 start = nfa_state_create (ctx, 0);
2226 end = nfa_state_create (ctx, 1);
2227 state_add_transition (ctx, start, label, end);
2228 n = nfa_fragment_create (start, end);
2229 GNUNET_assert (NULL != n);
2230 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2235 * Initialize a new context
2237 * @param ctx context
2240 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2248 ctx->transition_id = 0;
2249 ctx->stack_head = NULL;
2250 ctx->stack_tail = NULL;
2255 * Construct an NFA by parsing the regex string of length 'len'.
2257 * @param regex regular expression string
2258 * @param len length of the string
2260 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2262 struct GNUNET_REGEX_Automaton *
2263 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2265 struct GNUNET_REGEX_Context ctx;
2266 struct GNUNET_REGEX_Automaton *nfa;
2271 unsigned int altcount;
2272 unsigned int atomcount;
2273 unsigned int pcount;
2280 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2283 "Could not parse regex. Empty regex string provided.\n");
2288 GNUNET_REGEX_context_init (&ctx);
2298 for (count = 0; count < len && *regexp; count++, regexp++)
2306 nfa_add_concatenation (&ctx);
2308 GNUNET_array_grow (p, pcount, pcount + 1);
2309 p[pcount - 1].altcount = altcount;
2310 p[pcount - 1].atomcount = atomcount;
2317 error_msg = "Cannot append '|' to nothing";
2320 while (--atomcount > 0)
2321 nfa_add_concatenation (&ctx);
2327 error_msg = "Missing opening '('";
2332 // Ignore this: "()"
2334 altcount = p[pcount].altcount;
2335 atomcount = p[pcount].atomcount;
2338 while (--atomcount > 0)
2339 nfa_add_concatenation (&ctx);
2340 for (; altcount > 0; altcount--)
2341 nfa_add_alternation (&ctx);
2343 altcount = p[pcount].altcount;
2344 atomcount = p[pcount].atomcount;
2350 error_msg = "Cannot append '*' to nothing";
2353 nfa_add_star_op (&ctx);
2358 error_msg = "Cannot append '+' to nothing";
2361 nfa_add_plus_op (&ctx);
2366 error_msg = "Cannot append '?' to nothing";
2369 nfa_add_question_op (&ctx);
2375 nfa_add_concatenation (&ctx);
2377 curlabel[0] = *regexp;
2378 nfa_add_label (&ctx, curlabel);
2385 error_msg = "Unbalanced parenthesis";
2388 while (--atomcount > 0)
2389 nfa_add_concatenation (&ctx);
2390 for (; altcount > 0; altcount--)
2391 nfa_add_alternation (&ctx);
2393 GNUNET_free_non_null (p);
2395 nfa = ctx.stack_tail;
2396 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2398 if (NULL != ctx.stack_head)
2400 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2404 /* Remember the regex that was used to generate this NFA */
2405 nfa->regex = GNUNET_strdup (regex);
2407 /* create depth-first numbering of the states for pretty printing */
2408 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2410 /* No multistriding added so far */
2411 nfa->is_multistrided = GNUNET_NO;
2416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2417 if (NULL != error_msg)
2418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2420 GNUNET_free_non_null (p);
2422 while (NULL != (nfa = ctx.stack_head))
2424 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2425 GNUNET_REGEX_automaton_destroy (nfa);
2433 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2435 * @param ctx context.
2436 * @param nfa NFA automaton.
2437 * @param dfa DFA automaton.
2438 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2442 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2443 struct GNUNET_REGEX_Automaton *nfa,
2444 struct GNUNET_REGEX_Automaton *dfa,
2445 struct GNUNET_REGEX_State *dfa_state)
2447 struct GNUNET_REGEX_Transition *ctran;
2448 struct GNUNET_REGEX_State *state_iter;
2449 struct GNUNET_REGEX_State *new_dfa_state;
2450 struct GNUNET_REGEX_State *state_contains;
2451 struct GNUNET_REGEX_StateSet *tmp;
2452 struct GNUNET_REGEX_StateSet *nfa_set;
2454 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2456 if (NULL == ctran->label || NULL != ctran->to_state)
2459 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2460 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2461 state_set_clear (tmp);
2462 new_dfa_state = dfa_state_create (ctx, nfa_set);
2463 state_contains = NULL;
2464 for (state_iter = dfa->states_head; NULL != state_iter;
2465 state_iter = state_iter->next)
2467 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2468 state_contains = state_iter;
2471 if (NULL == state_contains)
2473 automaton_add_state (dfa, new_dfa_state);
2474 ctran->to_state = new_dfa_state;
2475 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2479 ctran->to_state = state_contains;
2480 automaton_destroy_state (new_dfa_state);
2486 * Construct DFA for the given 'regex' of length 'len'.
2488 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
2489 * compressed to o -> abc -> o. Note that this parameter influences the
2490 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
2491 * edges with the same label). For example for an application that stores IPv4
2492 * addresses as bitstrings it could make sense to limit the path compression to
2495 * @param regex regular expression string.
2496 * @param len length of the regular expression.
2497 * @param max_path_len limit the path compression length to the
2498 * given value. If set to 1, no path compression is applied. Set to 0 for
2499 * maximal possible path compression (generally not desireable).
2500 * @return DFA, needs to be freed using GNUNET_REGEX_automaton_destroy.
2502 struct GNUNET_REGEX_Automaton *
2503 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len,
2506 struct GNUNET_REGEX_Context ctx;
2507 struct GNUNET_REGEX_Automaton *dfa;
2508 struct GNUNET_REGEX_Automaton *nfa;
2509 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2511 GNUNET_REGEX_context_init (&ctx);
2514 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2519 "Could not create DFA, because NFA creation failed\n");
2523 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2525 dfa->state_count = 0;
2526 dfa->states_head = NULL;
2527 dfa->states_tail = NULL;
2528 dfa->regex = GNUNET_strdup (regex);
2529 dfa->is_multistrided = GNUNET_NO;
2531 // Create DFA start state from epsilon closure
2532 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, 0);
2533 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2534 automaton_add_state (dfa, dfa->start);
2536 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2538 GNUNET_REGEX_automaton_destroy (nfa);
2541 dfa_minimize (&ctx, dfa);
2543 // Create proofs for all states
2544 automaton_create_proofs (dfa);
2546 // Compress DFA paths
2547 if (1 != max_path_len)
2548 dfa_compress_paths (&ctx, dfa, max_path_len);
2550 // Add strides to DFA
2551 //GNUNET_REGEX_dfa_add_multi_strides (&ctx, dfa, 2);
2558 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2561 * @param a automaton to be destroyed
2564 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2566 struct GNUNET_REGEX_State *s;
2567 struct GNUNET_REGEX_State *next_state;
2572 GNUNET_free_non_null (a->regex);
2573 GNUNET_free_non_null (a->canonical_regex);
2575 for (s = a->states_head; NULL != s; s = next_state)
2577 next_state = s->next;
2578 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2579 automaton_destroy_state (s);
2587 * Evaluates the given string using the given DFA automaton
2589 * @param a automaton, type must be DFA
2590 * @param string string that should be evaluated
2592 * @return 0 if string matches, non 0 otherwise
2595 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2598 struct GNUNET_REGEX_State *s;
2599 unsigned int step_len;
2603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2604 "Tried to evaluate DFA, but NFA automaton given");
2610 // If the string is empty but the starting state is accepting, we accept.
2611 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2614 for (strp = string; NULL != strp && *strp; strp += step_len)
2616 step_len = dfa_move (&s, strp);
2622 if (NULL != s && s->accepting)
2630 * Evaluates the given string using the given NFA automaton
2632 * @param a automaton, type must be NFA
2633 * @param string string that should be evaluated
2635 * @return 0 if string matches, non 0 otherwise
2638 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2642 struct GNUNET_REGEX_State *s;
2643 struct GNUNET_REGEX_StateSet *sset;
2644 struct GNUNET_REGEX_StateSet *new_sset;
2650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2651 "Tried to evaluate NFA, but DFA automaton given");
2655 // If the string is empty but the starting state is accepting, we accept.
2656 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2660 sset = nfa_closure_create (a, a->start, 0);
2663 for (strp = string; NULL != strp && *strp; strp++)
2666 new_sset = nfa_closure_set_create (a, sset, str);
2667 state_set_clear (sset);
2668 sset = nfa_closure_set_create (a, new_sset, 0);
2669 state_set_clear (new_sset);
2672 for (i = 0; i < sset->len; i++)
2674 s = sset->states[i];
2675 if (NULL != s && s->accepting)
2682 state_set_clear (sset);
2688 * Evaluates the given 'string' against the given compiled regex
2690 * @param a automaton
2691 * @param string string to check
2693 * @return 0 if string matches, non 0 otherwise
2696 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2703 result = evaluate_dfa (a, string);
2706 result = evaluate_nfa (a, string);
2709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2710 "Evaluating regex failed, automaton has no type!\n");
2711 result = GNUNET_SYSERR;
2720 * Get the canonical regex of the given automaton.
2721 * When constructing the automaton a proof is computed for each state,
2722 * consisting of the regular expression leading to this state. A complete
2723 * regex for the automaton can be computed by combining these proofs.
2724 * As of now this function is only useful for testing.
2726 * @param a automaton for which the canonical regex should be returned.
2731 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2736 return a->canonical_regex;
2741 * Get the number of transitions that are contained in the given automaton.
2743 * @param a automaton for which the number of transitions should be returned.
2745 * @return number of transitions in the given automaton.
2748 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2750 unsigned int t_count;
2751 struct GNUNET_REGEX_State *s;
2757 for (s = a->states_head; NULL != s; s = s->next)
2758 t_count += s->transition_count;
2765 * Get the first key for the given 'input_string'. This hashes the first x bits
2766 * of the 'input_string'.
2768 * @param input_string string.
2769 * @param string_len length of the 'input_string'.
2770 * @param key pointer to where to write the hash code.
2772 * @return number of bits of 'input_string' that have been consumed
2773 * to construct the key
2776 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2777 struct GNUNET_HashCode * key)
2783 GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES;
2785 if (NULL == input_string)
2787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2791 GNUNET_CRYPTO_hash (input_string, size, key);
2798 * Check if the given 'proof' matches the given 'key'.
2800 * @param proof partial regex of a state.
2801 * @param key hash of a state.
2803 * @return GNUNET_OK if the proof is valid for the given key.
2806 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2808 struct GNUNET_HashCode key_check;
2810 if (NULL == proof || NULL == key)
2812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2816 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2818 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2823 * Recursive function that calls the iterator for each synthetic start state.
2825 * @param min_len minimum length of the path in the graph.
2826 * @param max_len maximum length of the path in the graph.
2827 * @param consumed_string string consumed by traversing the graph till this state.
2828 * @param state current state of the automaton.
2829 * @param iterator iterator function called for each edge.
2830 * @param iterator_cls closure for the iterator function.
2833 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2834 char *consumed_string, struct GNUNET_REGEX_State *state,
2835 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2839 struct GNUNET_REGEX_Transition *t;
2840 unsigned int num_edges = state->transition_count;
2841 struct GNUNET_REGEX_Edge edges[num_edges];
2842 struct GNUNET_REGEX_Edge edge[1];
2843 struct GNUNET_HashCode hash;
2844 struct GNUNET_HashCode hash_new;
2846 unsigned int cur_len;
2848 if (NULL != consumed_string)
2849 cur_len = strlen (consumed_string);
2853 if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
2854 NULL != consumed_string)
2856 if (cur_len <= max_len)
2858 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2860 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2863 edges[i].label = t->label;
2864 edges[i].destination = t->to_state->hash;
2866 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2867 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2871 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2872 state->transition_count < 1 && cur_len < max_len)
2874 // Special case for regex consisting of just a string that is shorter than
2876 edge[0].label = &consumed_string[cur_len - 1];
2877 edge[0].destination = state->hash;
2878 temp = GNUNET_strdup (consumed_string);
2879 temp[cur_len - 1] = '\0';
2880 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2881 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2885 else if (max_len < cur_len)
2887 // Case where the concatenated labels are longer than max_len, then split.
2888 edge[0].label = &consumed_string[max_len];
2889 edge[0].destination = state->hash;
2890 temp = GNUNET_strdup (consumed_string);
2891 temp[max_len] = '\0';
2892 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2893 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2898 if (cur_len < max_len)
2900 for (t = state->transitions_head; NULL != t; t = t->next)
2902 if (NULL != consumed_string)
2903 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2905 GNUNET_asprintf (&temp, "%s", t->label);
2907 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2916 * Iterate over all edges starting from start state of automaton 'a'. Calling
2917 * iterator for each edge.
2919 * @param a automaton.
2920 * @param iterator iterator called for each edge.
2921 * @param iterator_cls closure.
2924 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2925 GNUNET_REGEX_KeyIterator iterator,
2928 struct GNUNET_REGEX_State *s;
2930 for (s = a->states_head; NULL != s; s = s->next)
2932 struct GNUNET_REGEX_Edge edges[s->transition_count];
2933 unsigned int num_edges;
2935 num_edges = state_get_edges (s, edges);
2937 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
2938 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
2941 s->marked = GNUNET_NO;
2944 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, GNUNET_REGEX_INITIAL_BYTES,
2945 NULL, a->start, iterator, iterator_cls);
2950 * Create a string with binary IP notation for the given 'addr' in 'str'.
2952 * @param af address family of the given 'addr'.
2953 * @param addr address that should be converted to a string.
2954 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
2955 * @param str string that will contain binary notation of 'addr'. Expected
2956 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
2959 iptobinstr (const int af, const void *addr, char *str)
2967 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
2971 for (i = 31; i >= 0; i--)
2973 *str = (b & 1) + '0';
2981 struct in6_addr b = *(const struct in6_addr *) addr;
2985 for (i = 127; i >= 0; i--)
2987 *str = (b.s6_addr[i / 8] & 1) + '0';
2989 b.s6_addr[i / 8] >>= 1;
2998 * Get the ipv4 network prefix from the given 'netmask'.
3000 * @param netmask netmask for which to get the prefix len.
3002 * @return length of ipv4 prefix for 'netmask'.
3005 ipv4netmasktoprefixlen (const char *netmask)
3011 if (1 != inet_pton (AF_INET, netmask, &a))
3014 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3021 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3023 * @param ip IPv4 representation.
3024 * @param netmask netmask for the ip.
3025 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3029 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3032 unsigned int pfxlen;
3034 pfxlen = ipv4netmasktoprefixlen (netmask);
3035 iptobinstr (AF_INET, ip, rxstr);
3036 rxstr[pfxlen] = '\0';
3038 strcat (rxstr, "(0|1)+");
3043 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3045 * @param ipv6 IPv6 representation.
3046 * @param prefixlen length of the ipv6 prefix.
3047 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3051 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3054 iptobinstr (AF_INET6, ipv6, rxstr);
3055 rxstr[prefixlen] = '\0';
3056 if (prefixlen < 128)
3057 strcat (rxstr, "(0|1)+");