2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file src/regex/regex.c
22 * @brief library to create Deterministic Finite Automatons (DFAs) from regular
23 * expressions (regexes). Used by mesh for announcing regexes in the network and
24 * matching strings against published regexes.
25 * @author Maximilian Szengel
28 #include "gnunet_container_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_regex_lib.h"
31 #include "regex_internal.h"
34 * Set this to GNUNET_YES to enable state naming. Used to debug NFA->DFA
35 * creation. Disabled by default for better performance.
37 #define REGEX_DEBUG_DFA GNUNET_NO
42 struct GNUNET_REGEX_StateSet
47 struct GNUNET_REGEX_State **states;
50 * Length of the 'states' array.
56 * Set of states using MDLL API.
58 struct GNUNET_REGEX_StateSet_MDLL
63 struct GNUNET_REGEX_State *head;
68 struct GNUNET_REGEX_State *tail;
78 * Compare two strings for equality. If either is NULL they are not equal.
80 * @param str1 first string for comparison.
81 * @param str2 second string for comparison.
83 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
86 nullstrcmp (const char *str1, const char *str2)
88 if ((NULL == str1) != (NULL == str2))
90 if ((NULL == str1) && (NULL == str2))
93 return strcmp (str1, str2);
98 * Adds a transition from one state to another on 'label'. Does not add
102 * @param from_state starting state for the transition
103 * @param label transition label
104 * @param to_state state to where the transition should point to
107 state_add_transition (struct GNUNET_REGEX_Context *ctx,
108 struct GNUNET_REGEX_State *from_state, const char *label,
109 struct GNUNET_REGEX_State *to_state)
111 struct GNUNET_REGEX_Transition *t;
112 struct GNUNET_REGEX_Transition *oth;
114 if (NULL == from_state)
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
120 /* Do not add duplicate state transitions */
121 for (t = from_state->transitions_head; NULL != t; t = t->next)
123 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
124 t->from_state == from_state)
128 /* sort transitions by label */
129 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
131 if (0 < nullstrcmp (oth->label, label))
135 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
137 t->id = ctx->transition_id++;
139 t->label = GNUNET_strdup (label);
142 t->to_state = to_state;
143 t->from_state = from_state;
145 /* Add outgoing transition to 'from_state' */
146 from_state->transition_count++;
147 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
148 from_state->transitions_tail, oth, t);
153 * Remove a 'transition' from 'state'.
155 * @param state state from which the to-be-removed transition originates.
156 * @param transition transition that should be removed from state 'state'.
159 state_remove_transition (struct GNUNET_REGEX_State *state,
160 struct GNUNET_REGEX_Transition *transition)
162 if (NULL == state || NULL == transition)
165 if (transition->from_state != state)
168 GNUNET_free_non_null (transition->label);
170 state->transition_count--;
171 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
174 GNUNET_free (transition);
179 * Compare two states. Used for sorting.
181 * @param a first state
182 * @param b second state
184 * @return an integer less than, equal to, or greater than zero
185 * if the first argument is considered to be respectively
186 * less than, equal to, or greater than the second.
189 state_compare (const void *a, const void *b)
191 struct GNUNET_REGEX_State **s1;
192 struct GNUNET_REGEX_State **s2;
194 s1 = (struct GNUNET_REGEX_State **) a;
195 s2 = (struct GNUNET_REGEX_State **) b;
197 return (*s1)->id - (*s2)->id;
202 * Get all edges leaving state 's'.
205 * @param edges all edges leaving 's', expected to be allocated and have enough
206 * space for s->transitions_count elements.
208 * @return number of edges.
211 state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
213 struct GNUNET_REGEX_Transition *t;
221 for (t = s->transitions_head; NULL != t; t = t->next)
223 if (NULL != t->to_state)
225 edges[count].label = t->label;
226 edges[count].destination = t->to_state->hash;
235 * Compare to state sets by comparing the id's of the states that are contained
236 * in each set. Both sets are expected to be sorted by id!
238 * @param sset1 first state set
239 * @param sset2 second state set
241 * @return an integer less than, equal to, or greater than zero
242 * if the first argument is considered to be respectively
243 * less than, equal to, or greater than the second.
246 state_set_compare (struct GNUNET_REGEX_StateSet *sset1,
247 struct GNUNET_REGEX_StateSet *sset2)
252 if (NULL == sset1 || NULL == sset2)
255 result = sset1->len - sset2->len;
257 for (i = 0; i < sset1->len; i++)
262 result = state_compare (&sset1->states[i], &sset2->states[i]);
269 * Clears the given StateSet 'set'
271 * @param set set to be cleared
274 state_set_clear (struct GNUNET_REGEX_StateSet *set)
280 GNUNET_array_grow (set->states, set->len, 0);
286 * Clears an automaton fragment. Does not destroy the states inside the
289 * @param a automaton to be cleared
292 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
299 a->states_head = NULL;
300 a->states_tail = NULL;
307 * Frees the memory used by State 's'
309 * @param s state that should be destroyed
312 automaton_destroy_state (struct GNUNET_REGEX_State *s)
314 struct GNUNET_REGEX_Transition *t;
315 struct GNUNET_REGEX_Transition *next_t;
320 GNUNET_free_non_null (s->name);
321 GNUNET_free_non_null (s->proof);
322 state_set_clear (s->nfa_set);
324 for (t = s->transitions_head; NULL != t; t = next_t)
327 state_remove_transition (s, t);
335 * Remove a state from the given automaton 'a'. Always use this function when
336 * altering the states of an automaton. Will also remove all transitions leading
337 * to this state, before destroying it.
340 * @param s state to remove
343 automaton_remove_state (struct GNUNET_REGEX_Automaton *a,
344 struct GNUNET_REGEX_State *s)
346 struct GNUNET_REGEX_State *s_check;
347 struct GNUNET_REGEX_Transition *t_check;
348 struct GNUNET_REGEX_Transition *t_check_next;
350 if (NULL == a || NULL == s)
353 /* remove all transitions leading to this state */
354 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
356 for (t_check = s_check->transitions_head; NULL != t_check;
357 t_check = t_check_next)
359 t_check_next = t_check->next;
360 if (t_check->to_state == s)
361 state_remove_transition (s_check, t_check);
366 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
369 automaton_destroy_state (s);
374 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
375 * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
379 * @param s1 first state
380 * @param s2 second state, will be destroyed
383 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
384 struct GNUNET_REGEX_Automaton *a,
385 struct GNUNET_REGEX_State *s1,
386 struct GNUNET_REGEX_State *s2)
388 struct GNUNET_REGEX_State *s_check;
389 struct GNUNET_REGEX_Transition *t_check;
390 struct GNUNET_REGEX_Transition *t;
391 struct GNUNET_REGEX_Transition *t_next;
394 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
399 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
400 * does not already exists, if it already exists remove transition. */
401 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
403 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
405 t_next = t_check->next;
407 if (s2 == t_check->to_state)
410 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
412 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
415 if (GNUNET_NO == is_dup)
416 t_check->to_state = s1;
418 state_remove_transition (t_check->from_state, t_check);
423 /* 2. Add all transitions from s2 to sX to s1 */
424 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
426 if (t_check->to_state != s1)
427 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
430 /* 3. Rename s1 to {s1,s2} */
435 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
436 GNUNET_free (new_name);
440 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
442 automaton_destroy_state (s2);
447 * Add a state to the automaton 'a', always use this function to alter the
448 * states DLL of the automaton.
450 * @param a automaton to add the state to
451 * @param s state that should be added
454 automaton_add_state (struct GNUNET_REGEX_Automaton *a,
455 struct GNUNET_REGEX_State *s)
457 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
463 * Depth-first traversal (DFS) of all states that are reachable from state
464 * 's'. Performs 'action' on each visited state.
466 * @param s start state.
467 * @param marks an array of size a->state_count to remember which state was
469 * @param count current count of the state.
470 * @param check function that is checked before advancing on each transition
472 * @param check_cls closure for check.
473 * @param action action to be performed on each state.
474 * @param action_cls closure for action.
477 automaton_state_traverse (struct GNUNET_REGEX_State *s, int *marks,
479 GNUNET_REGEX_traverse_check check, void *check_cls,
480 GNUNET_REGEX_traverse_action action, void *action_cls)
482 struct GNUNET_REGEX_Transition *t;
484 if (GNUNET_YES == marks[s->traversal_id])
487 marks[s->traversal_id] = GNUNET_YES;
490 action (action_cls, *count, s);
494 for (t = s->transitions_head; NULL != t; t = t->next)
497 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
499 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
507 * Traverses the given automaton using depth-first-search (DFS) from it's start
508 * state, visiting all reachable states and calling 'action' on each one of
511 * @param a automaton to be traversed.
512 * @param start start state, pass a->start or NULL to traverse the whole automaton.
513 * @param check function that is checked before advancing on each transition
515 * @param check_cls closure for check.
516 * @param action action to be performed on each state.
517 * @param action_cls closure for action
520 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
521 struct GNUNET_REGEX_State *start,
522 GNUNET_REGEX_traverse_check check,
524 GNUNET_REGEX_traverse_action action,
528 struct GNUNET_REGEX_State *s;
530 if (NULL == a || 0 == a->state_count)
533 int marks[a->state_count];
535 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
536 s = s->next, count++)
538 s->traversal_id = count;
539 marks[s->traversal_id] = GNUNET_NO;
549 automaton_state_traverse (s, marks, &count, check, check_cls, action,
555 * Check if the given string 'str' needs parentheses around it when
556 * using it to generate a regex.
560 * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
563 needs_parentheses (const char *str)
571 if ((NULL == str) || ((slen = strlen (str)) < 2))
580 cl = strchr (pos, ')');
586 op = strchr (pos, '(');
587 if ((NULL != op) && (op < cl))
597 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
602 * Remove parentheses surrounding string 'str'.
603 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
604 * You need to GNUNET_free the returned string.
606 * @param str string, free'd or re-used by this function, can be NULL
608 * @return string without surrounding parentheses, string 'str' if no preceding
609 * epsilon could be found, NULL if 'str' was NULL
612 remove_parentheses (char *str)
617 if ((NULL == str) || ('(' != str[0]) ||
618 (str[(slen = strlen (str)) - 1] != ')'))
621 pos = strchr (&str[1], ')');
622 if (pos == &str[slen - 1])
624 memmove (str, &str[1], slen - 2);
625 str[slen - 2] = '\0';
632 * Check if the string 'str' starts with an epsilon (empty string).
633 * Example: "(|a)" is starting with an epsilon.
635 * @param str string to test
637 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
640 has_epsilon (const char *str)
642 return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
643 (')' == str[strlen (str) - 1]);
648 * Remove an epsilon from the string str. Where epsilon is an empty string
649 * Example: str = "(|a|b|c)", result: "a|b|c"
650 * The returned string needs to be freed.
654 * @return string without preceding epsilon, string 'str' if no preceding
655 * epsilon could be found, NULL if 'str' was NULL
658 remove_epsilon (char *str)
664 if (('(' == str[0]) && ('|' == str[1]))
667 if (')' == str[len - 1])
668 return GNUNET_strndup (&str[2], len - 3);
670 return GNUNET_strdup (str);
675 * Compare 'str1', starting from position 'k', with whole 'str2'
677 * @param str1 first string to compare, starting from position 'k'
678 * @param str2 second string for comparison
679 * @param k starting position in 'str1'
681 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
684 strkcmp (const char *str1, const char *str2, size_t k)
686 if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
688 return strcmp (&str1[k], str2);
693 * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse'
694 * function to create the depth-first numbering of the states.
696 * @param cls states array.
697 * @param count current state counter.
698 * @param s current state.
701 number_states (void *cls, const unsigned int count,
702 struct GNUNET_REGEX_State *s)
704 struct GNUNET_REGEX_State **states = cls;
713 * Construct the regular expression given the inductive step,
714 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
715 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
717 * @param R_last_ij value of $R^{(k-1)_{ij}.
718 * @param R_last_ik value of $R^{(k-1)_{ik}.
719 * @param R_last_kk value of $R^{(k-1)_{kk}.
720 * @param R_last_kj value of $R^{(k-1)_{kj}.
721 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
722 * is expected to be NULL when called!
725 automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
726 char *R_last_kk, char *R_last_kj,
752 GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
754 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
755 * R_last == R^{(k-1)}, R_cur == R^{(k)}
756 * R_cur_ij = R_cur_l | R_cur_r
757 * R_cur_l == R^{(k-1)}_{ij}
758 * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
761 if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
762 (NULL == R_last_kj)))
764 /* R^{(k)}_{ij} = N | N */
769 if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
772 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
773 *R_cur_ij = GNUNET_strdup (R_last_ij);
777 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
778 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
783 /* cache results from strcmp, we might need these many times */
784 ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
785 ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
786 ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
787 kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
789 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
790 * as parentheses, so we can better compare the contents */
791 R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
792 R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
793 R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
795 clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
796 clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
798 /* construct R_cur_l (and, if necessary R_cur_r) */
799 if (NULL != R_last_ij)
801 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
802 * as parentheses, so we can better compare the contents */
803 R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
805 if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
806 && 0 == strcmp (R_temp_kk, R_temp_kj))
808 if (0 == strlen (R_temp_ij))
810 R_cur_r = GNUNET_strdup ("");
812 else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
813 (0 == strncmp (R_last_ik, "(|", 2) &&
814 0 == strncmp (R_last_kj, "(|", 2)))
817 * a|(e|a)a*(e|a) = a*
818 * a|(e|a)(e|a)*(e|a) = a*
820 * (e|a)|aa*(e|a) = a*
821 * (e|a)|(e|a)a*a = a*
822 * (e|a)|(e|a)a*(e|a) = a*
823 * (e|a)|(e|a)(e|a)*(e|a) = a*
825 if (GNUNET_YES == needs_parentheses (R_temp_ij))
826 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
828 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
836 * a|(e|a)(e|a)*a = a+
837 * a|a(e|a)*(e|a) = a+
839 if (GNUNET_YES == needs_parentheses (R_temp_ij))
840 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
842 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
845 else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
848 if (strlen (R_last_kk) < 1)
849 R_cur_r = GNUNET_strdup (R_last_ij);
850 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
851 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
853 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
857 else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
860 if (strlen (R_last_kk) < 1)
861 R_cur_r = GNUNET_strdup (R_last_kj);
862 else if (GNUNET_YES == needs_parentheses (R_temp_kk))
863 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
865 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
869 else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
870 has_epsilon (R_last_kk))
872 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
873 if (needs_parentheses (R_temp_kk))
874 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
876 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
880 else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
881 has_epsilon (R_last_kk))
883 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
884 if (needs_parentheses (R_temp_kk))
885 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
887 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
893 temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
894 temp_a = remove_parentheses (temp_a);
898 GNUNET_free_non_null (R_temp_ij);
902 /* we have no left side */
906 /* construct R_cur_r, if not already constructed */
909 length = strlen (R_temp_kk) - strlen (R_last_ik);
911 /* a(ba)*bx = (ab)+x */
912 if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
913 NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
914 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
915 0 == strncmp (R_temp_kk, R_last_kj, length))
917 temp_a = GNUNET_malloc (length + 1);
918 temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
923 for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
927 temp_a[length_l] = R_last_kj[cnt];
932 temp_b[length_r] = R_last_kj[cnt];
936 temp_a[length_l] = '\0';
937 temp_b[length_r] = '\0';
939 /* e|(ab)+ = (ab)* */
940 if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
942 GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
943 GNUNET_free (R_cur_l);
948 GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
950 GNUNET_free (temp_a);
951 GNUNET_free (temp_b);
953 else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
954 0 == strcmp (R_temp_kk, R_temp_kj))
958 * (e|a)(e|a)*(e|a) = a*
960 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
962 if (needs_parentheses (R_temp_kk))
963 GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
965 GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
968 else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
969 !has_epsilon (R_last_ik))
971 if (needs_parentheses (R_temp_kk))
972 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
974 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
985 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
986 has_epsilon (R_last_kj));
990 if (needs_parentheses (R_temp_kk))
991 GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
993 GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
1001 else if (0 == strcmp (R_temp_ik, R_temp_kk))
1003 if (has_epsilon (R_last_ik))
1005 if (needs_parentheses (R_temp_kk))
1006 GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
1008 GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
1012 if (needs_parentheses (R_temp_kk))
1013 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
1015 GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
1020 * b(e|a)*(e|a) = ba*
1022 else if (0 == strcmp (R_temp_kk, R_temp_kj))
1024 if (has_epsilon (R_last_kj))
1026 if (needs_parentheses (R_temp_kk))
1027 GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
1029 GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
1033 if (needs_parentheses (R_temp_kk))
1034 GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
1036 GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
1041 if (strlen (R_temp_kk) > 0)
1043 if (needs_parentheses (R_temp_kk))
1045 GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
1050 GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
1056 GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
1061 GNUNET_free_non_null (R_temp_ik);
1062 GNUNET_free_non_null (R_temp_kk);
1063 GNUNET_free_non_null (R_temp_kj);
1065 if (NULL == R_cur_l && NULL == R_cur_r)
1071 if (NULL != R_cur_l && NULL == R_cur_r)
1073 *R_cur_ij = R_cur_l;
1077 if (NULL == R_cur_l && NULL != R_cur_r)
1079 *R_cur_ij = R_cur_r;
1083 if (0 == nullstrcmp (R_cur_l, R_cur_r))
1085 *R_cur_ij = R_cur_l;
1086 GNUNET_free (R_cur_r);
1090 GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
1092 GNUNET_free (R_cur_l);
1093 GNUNET_free (R_cur_r);
1098 * Create proofs for all states in the given automaton. Implementation of the
1099 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1100 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1102 * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1103 * proof) fields. The starting state will only have a valid proof/hash if it has
1104 * any incoming transitions.
1106 * @param a automaton for which to assign proofs and hashes.
1109 automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
1113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1114 "Could not create proofs, automaton was NULL\n");
1118 unsigned int n = a->state_count;
1119 struct GNUNET_REGEX_State *states[n];
1123 struct GNUNET_REGEX_Transition *t;
1124 char *complete_regex;
1129 R_last = GNUNET_malloc_large (sizeof (char *) * n * n);
1130 R_cur = GNUNET_malloc_large (sizeof (char *) * n * n);
1132 /* create depth-first numbering of the states, initializes 'state' */
1133 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1136 for (i = 0; i < n; i++)
1137 GNUNET_assert (NULL != states[i]);
1139 /* Compute regular expressions of length "1" between each pair of states */
1140 for (i = 0; i < n; i++)
1142 for (j = 0; j < n; j++)
1144 R_cur[i * n + j] = NULL;
1145 R_last[i * n + j] = NULL;
1147 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1149 j = t->to_state->dfs_id;
1150 if (NULL == R_last[i * n + j])
1151 GNUNET_asprintf (&R_last[i * n + j], "%s", t->label);
1154 temp = R_last[i * n + j];
1155 GNUNET_asprintf (&R_last[i * n + j], "%s|%s", R_last[i * n + j],
1160 if (NULL == R_last[i * n + i])
1161 GNUNET_asprintf (&R_last[i * n + i], "");
1164 temp = R_last[i * n + i];
1165 GNUNET_asprintf (&R_last[i * n + i], "(|%s)", R_last[i * n + i]);
1169 for (i = 0; i < n; i++)
1170 for (j = 0; j < n; j++)
1171 if (needs_parentheses (R_last[i * n + j]))
1173 temp = R_last[i * n + j];
1174 GNUNET_asprintf (&R_last[i * n + j], "(%s)", R_last[i * n + j]);
1178 /* Compute regular expressions of length "k" between each pair of states per
1180 for (k = 0; k < n; k++)
1182 for (i = 0; i < n; i++)
1184 for (j = 0; j < n; j++)
1186 /* Basis for the recursion:
1187 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1188 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1191 /* Create R_cur[i][j] and simplify the expression */
1192 automaton_create_proofs_simplify (R_last[i * n + j], R_last[i * n + k],
1193 R_last[k * n + k], R_last[k * n + j],
1198 /* set R_last = R_cur */
1199 for (i = 0; i < n; i++)
1201 for (j = 0; j < n; j++)
1203 GNUNET_free_non_null (R_last[i * n + j]);
1204 R_last[i * n + j] = R_cur[i * n + j];
1205 R_cur[i * n + j] = NULL;
1210 /* assign proofs and hashes */
1211 for (i = 0; i < n; i++)
1213 if (NULL != R_last[a->start->dfs_id * n + i])
1215 states[i]->proof = GNUNET_strdup (R_last[a->start->dfs_id * n + i]);
1216 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1221 /* complete regex for whole DFA: union of all pairs (start state/accepting
1223 complete_regex = NULL;
1224 for (i = 0; i < n; i++)
1226 if (states[i]->accepting)
1228 if (NULL == complete_regex &&
1229 0 < strlen (R_last[a->start->dfs_id * n + i]))
1231 GNUNET_asprintf (&complete_regex, "%s",
1232 R_last[a->start->dfs_id * n + i]);
1234 else if (NULL != R_last[a->start->dfs_id * n + i] &&
1235 0 < strlen (R_last[a->start->dfs_id * n + i]))
1237 temp = complete_regex;
1238 GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
1239 R_last[a->start->dfs_id * n + i]);
1244 a->canonical_regex = complete_regex;
1247 for (i = 0; i < n; i++)
1249 for (j = 0; j < n; j++)
1250 GNUNET_free_non_null (R_last[i * n + j]);
1252 GNUNET_free (R_cur);
1253 GNUNET_free (R_last);
1258 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1259 * automaton_destroy_state.
1261 * @param ctx context
1262 * @param nfa_states set of NFA states on which the DFA should be based on
1264 * @return new DFA state
1266 static struct GNUNET_REGEX_State *
1267 dfa_state_create (struct GNUNET_REGEX_Context *ctx,
1268 struct GNUNET_REGEX_StateSet *nfa_states)
1270 struct GNUNET_REGEX_State *s;
1273 struct GNUNET_REGEX_State *cstate;
1274 struct GNUNET_REGEX_Transition *ctran;
1277 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1278 s->id = ctx->state_id++;
1280 s->marked = GNUNET_NO;
1288 if (NULL == nfa_states)
1290 GNUNET_asprintf (&s->name, "s%i", s->id);
1294 s->nfa_set = nfa_states;
1296 if (nfa_states->len < 1)
1299 /* Create a name based on 'nfa_states' */
1300 s->name = GNUNET_malloc (sizeof (char) * 2);
1301 strcat (s->name, "{");
1304 for (i = 0; i < nfa_states->len; i++)
1306 cstate = nfa_states->states[i];
1307 GNUNET_asprintf (&name, "%i,", cstate->id);
1311 len = strlen (s->name) + strlen (name) + 1;
1312 s->name = GNUNET_realloc (s->name, len);
1313 strcat (s->name, name);
1318 /* Add a transition for each distinct label to NULL state */
1319 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1321 if (NULL != ctran->label)
1322 state_add_transition (ctx, s, ctran->label, NULL);
1325 /* If the nfa_states contain an accepting state, the new dfa state is also
1327 if (cstate->accepting)
1331 s->name[strlen (s->name) - 1] = '}';
1338 * Move from the given state 's' to the next state on transition 'str'. Consumes
1339 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1340 * 's' will point to the next state, and the length of the substring used for
1341 * this transition will be returned. If no transition possible 0 is returned and
1342 * 's' points to NULL.
1344 * @param s starting state, will point to the next state or NULL (if no
1345 * transition possible)
1346 * @param str edge label to follow (will match longest common prefix)
1348 * @return length of the substring comsumed from 'str'
1351 dfa_move (struct GNUNET_REGEX_State **s, const char *str)
1353 struct GNUNET_REGEX_Transition *t;
1354 struct GNUNET_REGEX_State *new_s;
1356 unsigned int max_len;
1363 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1365 len = strlen (t->label);
1367 if (0 == strncmp (t->label, str, len))
1372 new_s = t->to_state;
1382 * Set the given state 'marked' to GNUNET_YES. Used by the
1383 * 'dfa_remove_unreachable_states' function to detect unreachable states in the
1386 * @param cls closure, not used.
1387 * @param count count, not used.
1388 * @param s state where the marked attribute will be set to GNUNET_YES.
1391 mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
1393 s->marked = GNUNET_YES;
1397 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1398 * states that are not reachable from the starting state.
1400 * @param a DFA automaton
1403 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1405 struct GNUNET_REGEX_State *s;
1406 struct GNUNET_REGEX_State *s_next;
1408 /* 1. unmark all states */
1409 for (s = a->states_head; NULL != s; s = s->next)
1410 s->marked = GNUNET_NO;
1412 /* 2. traverse dfa from start state and mark all visited states */
1413 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1415 /* 3. delete all states that were not visited */
1416 for (s = a->states_head; NULL != s; s = s_next)
1419 if (GNUNET_NO == s->marked)
1420 automaton_remove_state (a, s);
1426 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1427 * not transition to any other state but themselves.
1429 * @param a DFA automaton
1432 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1434 struct GNUNET_REGEX_State *s;
1435 struct GNUNET_REGEX_State *s_next;
1436 struct GNUNET_REGEX_Transition *t;
1439 GNUNET_assert (DFA == a->type);
1441 for (s = a->states_head; NULL != s; s = s_next)
1449 for (t = s->transitions_head; NULL != t; t = t->next)
1451 if (NULL != t->to_state && t->to_state != s)
1461 /* state s is dead, remove it */
1462 automaton_remove_state (a, s);
1468 * Merge all non distinguishable states in the DFA 'a'
1470 * @param ctx context
1471 * @param a DFA automaton
1474 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1475 struct GNUNET_REGEX_Automaton *a)
1478 struct GNUNET_REGEX_State *s1;
1479 struct GNUNET_REGEX_State *s2;
1480 struct GNUNET_REGEX_Transition *t1;
1481 struct GNUNET_REGEX_Transition *t2;
1482 struct GNUNET_REGEX_State *s1_next;
1483 struct GNUNET_REGEX_State *s2_next;
1485 unsigned int num_equal_edges;
1487 unsigned int state_cnt;
1489 if (NULL == a || 0 == a->state_count)
1491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1492 "Could not merge nondistinguishable states, automaton was NULL.\n");
1496 state_cnt = a->state_count;
1498 (int *) GNUNET_malloc_large (sizeof (int) * state_cnt * a->state_count);
1500 for (i = 0, s1 = a->states_head; i < state_cnt && NULL != s1;
1506 /* Mark all pairs of accepting/!accepting states */
1507 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1509 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1511 table[((s1->marked * state_cnt) + s2->marked)] = 0;
1513 if ((s1->accepting && !s2->accepting) ||
1514 (!s1->accepting && s2->accepting))
1516 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1521 /* Find all equal states */
1526 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1528 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1530 if (0 != table[((s1->marked * state_cnt) + s2->marked)])
1533 num_equal_edges = 0;
1534 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1536 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1538 if (0 == strcmp (t1->label, t2->label))
1542 table[((t1->to_state->marked * state_cnt) +
1543 t2->to_state->marked)] ||
1545 table[((t2->to_state->marked * state_cnt) +
1546 t1->to_state->marked)])
1548 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1554 if (num_equal_edges != s1->transition_count ||
1555 num_equal_edges != s2->transition_count)
1557 /* Make sure ALL edges of possible equal states are the same */
1558 table[((s1->marked * state_cnt) + s2->marked)] = -2;
1564 /* Merge states that are equal */
1565 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1568 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1571 if (0 == table[((s1->marked * state_cnt) + s2->marked)])
1572 automaton_merge_states (ctx, a, s1, s2);
1576 GNUNET_free (table);
1581 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1582 * dead states and merging all non distinguishable states
1584 * @param ctx context
1585 * @param a DFA automaton
1588 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1589 struct GNUNET_REGEX_Automaton *a)
1594 GNUNET_assert (DFA == a->type);
1596 /* 1. remove unreachable states */
1597 dfa_remove_unreachable_states (a);
1599 /* 2. remove dead states */
1600 dfa_remove_dead_states (a);
1602 /* 3. Merge nondistinguishable states */
1603 dfa_merge_nondistinguishable_states (ctx, a);
1608 * Context for adding strided transitions to a DFA.
1610 struct GNUNET_REGEX_Strided_Context
1613 * Length of the strides.
1615 const unsigned int stride;
1618 * Strided transitions DLL. New strided transitions will be stored in this DLL
1619 * and afterwards added to the DFA.
1621 struct GNUNET_REGEX_Transition *transitions_head;
1624 * Strided transitions DLL.
1626 struct GNUNET_REGEX_Transition *transitions_tail;
1631 * Recursive helper function to add strides to a DFA.
1633 * @param cls context, contains stride length and strided transitions DLL.
1634 * @param depth current depth of the depth-first traversal of the graph.
1635 * @param label current label, string that contains all labels on the path from
1637 * @param start start state for the depth-first traversal of the graph.
1638 * @param s current state in the depth-first traversal
1641 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1642 struct GNUNET_REGEX_State *start,
1643 struct GNUNET_REGEX_State *s)
1645 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1646 struct GNUNET_REGEX_Transition *t;
1649 if (depth == ctx->stride)
1651 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1652 t->label = GNUNET_strdup (label);
1654 t->from_state = start;
1655 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1660 for (t = s->transitions_head; NULL != t; t = t->next)
1662 /* Do not consider self-loops, because it end's up in too many
1664 if (t->to_state == t->from_state)
1669 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1672 new_label = GNUNET_strdup (t->label);
1674 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1678 GNUNET_free_non_null (label);
1683 * Function called for each state in the DFA. Starts a traversal of depth set in
1684 * context starting from state 's'.
1686 * @param cls context.
1687 * @param count not used.
1688 * @param s current state.
1691 dfa_add_multi_strides (void *cls, const unsigned int count,
1692 struct GNUNET_REGEX_State *s)
1694 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1699 * Adds multi-strided transitions to the given 'dfa'.
1701 * @param regex_ctx regex context needed to add transitions to the automaton.
1702 * @param dfa DFA to which the multi strided transitions should be added.
1703 * @param stride_len length of the strides.
1706 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1707 struct GNUNET_REGEX_Automaton *dfa,
1708 const unsigned int stride_len)
1710 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1711 struct GNUNET_REGEX_Transition *t;
1712 struct GNUNET_REGEX_Transition *t_next;
1714 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1717 /* Compute the new transitions of given stride_len */
1718 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1719 &dfa_add_multi_strides, &ctx);
1721 /* Add all the new transitions to the automaton. */
1722 for (t = ctx.transitions_head; NULL != t; t = t_next)
1725 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1726 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1727 GNUNET_free_non_null (t->label);
1731 /* Mark this automaton as multistrided */
1732 dfa->is_multistrided = GNUNET_YES;
1736 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1737 * and adds new transitions to the given transitions DLL and marks states that
1738 * should be removed by setting state->contained to GNUNET_YES.
1740 * @param dfa DFA for which the paths should be compressed.
1741 * @param start starting state for linear path search.
1742 * @param cur current state in the recursive DFS.
1743 * @param label current label (string of traversed labels).
1744 * @param max_len maximal path compression length.
1745 * @param transitions_head transitions DLL.
1746 * @param transitions_tail transitions DLL.
1749 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1750 struct GNUNET_REGEX_State *start,
1751 struct GNUNET_REGEX_State *cur, char *label,
1752 unsigned int max_len,
1753 struct GNUNET_REGEX_Transition **transitions_head,
1754 struct GNUNET_REGEX_Transition **transitions_tail)
1756 struct GNUNET_REGEX_Transition *t;
1760 if (NULL != label &&
1761 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
1762 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1763 max_len == strlen (label)) ||
1764 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
1766 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1767 t->label = GNUNET_strdup (label);
1769 t->from_state = start;
1770 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1772 if (GNUNET_NO == cur->marked)
1774 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1779 else if (cur != start)
1780 cur->contained = GNUNET_YES;
1782 if (GNUNET_YES == cur->marked && cur != start)
1785 cur->marked = GNUNET_YES;
1788 for (t = cur->transitions_head; NULL != t; t = t->next)
1791 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1793 new_label = GNUNET_strdup (t->label);
1795 if (t->to_state != cur)
1797 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1798 transitions_head, transitions_tail);
1800 GNUNET_free (new_label);
1805 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1806 * compressed to 0->3 by combining transitions.
1808 * @param regex_ctx context for adding new transitions.
1809 * @param dfa DFA representation, will directly modify the given DFA.
1810 * @param max_len maximal length of the compressed paths.
1813 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1814 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1816 struct GNUNET_REGEX_State *s;
1817 struct GNUNET_REGEX_State *s_next;
1818 struct GNUNET_REGEX_Transition *t;
1819 struct GNUNET_REGEX_Transition *t_next;
1820 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1821 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1826 /* Count the incoming transitions on each state. */
1827 for (s = dfa->states_head; NULL != s; s = s->next)
1829 for (t = s->transitions_head; NULL != t; t = t->next)
1831 if (NULL != t->to_state)
1832 t->to_state->incoming_transition_count++;
1836 /* Unmark all states. */
1837 for (s = dfa->states_head; NULL != s; s = s->next)
1839 s->marked = GNUNET_NO;
1840 s->contained = GNUNET_NO;
1843 /* Add strides and mark states that can be deleted. */
1844 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1845 &transitions_head, &transitions_tail);
1847 /* Add all the new transitions to the automaton. */
1848 for (t = transitions_head; NULL != t; t = t_next)
1851 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1852 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1853 GNUNET_free_non_null (t->label);
1857 /* Remove marked states (including their incoming and outgoing transitions). */
1858 for (s = dfa->states_head; NULL != s; s = s_next)
1861 if (GNUNET_YES == s->contained)
1862 automaton_remove_state (dfa, s);
1868 * Creates a new NFA fragment. Needs to be cleared using
1869 * automaton_fragment_clear.
1871 * @param start starting state
1872 * @param end end state
1874 * @return new NFA fragment
1876 static struct GNUNET_REGEX_Automaton *
1877 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1878 struct GNUNET_REGEX_State *end)
1880 struct GNUNET_REGEX_Automaton *n;
1882 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1889 if (NULL == start || NULL == end)
1892 automaton_add_state (n, end);
1893 automaton_add_state (n, start);
1905 * Adds a list of states to the given automaton 'n'.
1907 * @param n automaton to which the states should be added
1908 * @param states_head head of the DLL of states
1909 * @param states_tail tail of the DLL of states
1912 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1913 struct GNUNET_REGEX_State *states_head,
1914 struct GNUNET_REGEX_State *states_tail)
1916 struct GNUNET_REGEX_State *s;
1918 if (NULL == n || NULL == states_head)
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1924 if (NULL == n->states_head)
1926 n->states_head = states_head;
1927 n->states_tail = states_tail;
1931 if (NULL != states_head)
1933 n->states_tail->next = states_head;
1934 n->states_tail = states_tail;
1937 for (s = states_head; NULL != s; s = s->next)
1943 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1945 * @param ctx context
1946 * @param accepting is it an accepting state or not
1948 * @return new NFA state
1950 static struct GNUNET_REGEX_State *
1951 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1953 struct GNUNET_REGEX_State *s;
1955 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1956 s->id = ctx->state_id++;
1957 s->accepting = accepting;
1958 s->marked = GNUNET_NO;
1964 GNUNET_asprintf (&s->name, "s%i", s->id);
1971 * Calculates the NFA closure set for the given state.
1973 * @param nfa the NFA containing 's'
1974 * @param s starting point state
1975 * @param label transitioning label on which to base the closure on,
1976 * pass NULL for epsilon transition
1978 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1980 static struct GNUNET_REGEX_StateSet *
1981 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1982 struct GNUNET_REGEX_State *s, const char *label)
1985 struct GNUNET_REGEX_StateSet *cls;
1986 struct GNUNET_REGEX_StateSet_MDLL cls_stack;
1987 struct GNUNET_REGEX_State *clsstate;
1988 struct GNUNET_REGEX_State *currentstate;
1989 struct GNUNET_REGEX_Transition *ctran;
1994 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1995 cls_stack.head = NULL;
1996 cls_stack.tail = NULL;
1998 /* Add start state to closure only for epsilon closure */
2000 GNUNET_array_append (cls->states, cls->len, s);
2002 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
2005 while (cls_stack.len > 0)
2007 currentstate = cls_stack.tail;
2008 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2012 for (ctran = currentstate->transitions_head; NULL != ctran;
2013 ctran = ctran->next)
2015 if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
2017 clsstate = ctran->to_state;
2019 if (NULL != clsstate && 0 == clsstate->contained)
2021 GNUNET_array_append (cls->states, cls->len, clsstate);
2022 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2025 clsstate->contained = 1;
2031 for (i = 0; i < cls->len; i++)
2032 cls->states[i]->contained = 0;
2034 /* sort the states */
2036 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2044 * Calculates the closure set for the given set of states.
2046 * @param nfa the NFA containing 's'
2047 * @param states list of states on which to base the closure on
2048 * @param label transitioning label for which to base the closure on,
2049 * pass NULL for epsilon transition
2051 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2053 static struct GNUNET_REGEX_StateSet *
2054 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
2055 struct GNUNET_REGEX_StateSet *states, const char *label)
2057 struct GNUNET_REGEX_State *s;
2058 struct GNUNET_REGEX_StateSet *sset;
2059 struct GNUNET_REGEX_StateSet *cls;
2063 unsigned int contains;
2068 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2070 for (i = 0; i < states->len; i++)
2072 s = states->states[i];
2073 sset = nfa_closure_create (nfa, s, label);
2075 for (j = 0; j < sset->len; j++)
2078 for (k = 0; k < cls->len; k++)
2080 if (sset->states[j]->id == cls->states[k]->id)
2087 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2089 state_set_clear (sset);
2093 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2101 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2103 * @param ctx context
2106 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2108 struct GNUNET_REGEX_Automaton *a;
2109 struct GNUNET_REGEX_Automaton *b;
2110 struct GNUNET_REGEX_Automaton *new_nfa;
2112 b = ctx->stack_tail;
2113 GNUNET_assert (NULL != b);
2114 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2115 a = ctx->stack_tail;
2116 GNUNET_assert (NULL != a);
2117 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2119 state_add_transition (ctx, a->end, NULL, b->start);
2120 a->end->accepting = 0;
2121 b->end->accepting = 1;
2123 new_nfa = nfa_fragment_create (NULL, NULL);
2124 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2125 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2126 new_nfa->start = a->start;
2127 new_nfa->end = b->end;
2128 new_nfa->state_count += a->state_count + b->state_count;
2129 automaton_fragment_clear (a);
2130 automaton_fragment_clear (b);
2132 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2137 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2139 * @param ctx context
2142 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2144 struct GNUNET_REGEX_Automaton *a;
2145 struct GNUNET_REGEX_Automaton *new_nfa;
2146 struct GNUNET_REGEX_State *start;
2147 struct GNUNET_REGEX_State *end;
2149 a = ctx->stack_tail;
2153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2154 "nfa_add_star_op failed, because there was no element on the stack");
2158 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2160 start = nfa_state_create (ctx, 0);
2161 end = nfa_state_create (ctx, 1);
2163 state_add_transition (ctx, start, NULL, a->start);
2164 state_add_transition (ctx, start, NULL, end);
2165 state_add_transition (ctx, a->end, NULL, a->start);
2166 state_add_transition (ctx, a->end, NULL, end);
2168 a->end->accepting = 0;
2171 new_nfa = nfa_fragment_create (start, end);
2172 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2173 automaton_fragment_clear (a);
2175 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2180 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2182 * @param ctx context
2185 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2187 struct GNUNET_REGEX_Automaton *a;
2189 a = ctx->stack_tail;
2193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2194 "nfa_add_plus_op failed, because there was no element on the stack");
2198 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2200 state_add_transition (ctx, a->end, NULL, a->start);
2202 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2207 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2209 * @param ctx context
2212 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2214 struct GNUNET_REGEX_Automaton *a;
2215 struct GNUNET_REGEX_Automaton *new_nfa;
2216 struct GNUNET_REGEX_State *start;
2217 struct GNUNET_REGEX_State *end;
2219 a = ctx->stack_tail;
2223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2224 "nfa_add_question_op failed, because there was no element on the stack");
2228 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2230 start = nfa_state_create (ctx, 0);
2231 end = nfa_state_create (ctx, 1);
2233 state_add_transition (ctx, start, NULL, a->start);
2234 state_add_transition (ctx, start, NULL, end);
2235 state_add_transition (ctx, a->end, NULL, end);
2237 a->end->accepting = 0;
2239 new_nfa = nfa_fragment_create (start, end);
2240 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2241 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2242 automaton_fragment_clear (a);
2247 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2248 * alternates between a and b (a|b)
2250 * @param ctx context
2253 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2255 struct GNUNET_REGEX_Automaton *a;
2256 struct GNUNET_REGEX_Automaton *b;
2257 struct GNUNET_REGEX_Automaton *new_nfa;
2258 struct GNUNET_REGEX_State *start;
2259 struct GNUNET_REGEX_State *end;
2261 b = ctx->stack_tail;
2262 GNUNET_assert (NULL != b);
2263 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2264 a = ctx->stack_tail;
2265 GNUNET_assert (NULL != a);
2266 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2268 start = nfa_state_create (ctx, 0);
2269 end = nfa_state_create (ctx, 1);
2270 state_add_transition (ctx, start, NULL, a->start);
2271 state_add_transition (ctx, start, NULL, b->start);
2273 state_add_transition (ctx, a->end, NULL, end);
2274 state_add_transition (ctx, b->end, NULL, end);
2276 a->end->accepting = 0;
2277 b->end->accepting = 0;
2280 new_nfa = nfa_fragment_create (start, end);
2281 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2282 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2283 automaton_fragment_clear (a);
2284 automaton_fragment_clear (b);
2286 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2291 * Adds a new nfa fragment to the stack
2293 * @param ctx context
2294 * @param label label for nfa transition
2297 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2299 struct GNUNET_REGEX_Automaton *n;
2300 struct GNUNET_REGEX_State *start;
2301 struct GNUNET_REGEX_State *end;
2303 GNUNET_assert (NULL != ctx);
2305 start = nfa_state_create (ctx, 0);
2306 end = nfa_state_create (ctx, 1);
2307 state_add_transition (ctx, start, label, end);
2308 n = nfa_fragment_create (start, end);
2309 GNUNET_assert (NULL != n);
2310 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2315 * Initialize a new context
2317 * @param ctx context
2320 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2328 ctx->transition_id = 0;
2329 ctx->stack_head = NULL;
2330 ctx->stack_tail = NULL;
2335 * Construct an NFA by parsing the regex string of length 'len'.
2337 * @param regex regular expression string
2338 * @param len length of the string
2340 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2342 struct GNUNET_REGEX_Automaton *
2343 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2345 struct GNUNET_REGEX_Context ctx;
2346 struct GNUNET_REGEX_Automaton *nfa;
2351 unsigned int altcount;
2352 unsigned int atomcount;
2353 unsigned int pcount;
2360 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2363 "Could not parse regex. Empty regex string provided.\n");
2368 GNUNET_REGEX_context_init (&ctx);
2378 for (count = 0; count < len && *regexp; count++, regexp++)
2386 nfa_add_concatenation (&ctx);
2388 GNUNET_array_grow (p, pcount, pcount + 1);
2389 p[pcount - 1].altcount = altcount;
2390 p[pcount - 1].atomcount = atomcount;
2397 error_msg = "Cannot append '|' to nothing";
2400 while (--atomcount > 0)
2401 nfa_add_concatenation (&ctx);
2407 error_msg = "Missing opening '('";
2412 /* Ignore this: "()" */
2414 altcount = p[pcount].altcount;
2415 atomcount = p[pcount].atomcount;
2418 while (--atomcount > 0)
2419 nfa_add_concatenation (&ctx);
2420 for (; altcount > 0; altcount--)
2421 nfa_add_alternation (&ctx);
2423 altcount = p[pcount].altcount;
2424 atomcount = p[pcount].atomcount;
2430 error_msg = "Cannot append '*' to nothing";
2433 nfa_add_star_op (&ctx);
2438 error_msg = "Cannot append '+' to nothing";
2441 nfa_add_plus_op (&ctx);
2446 error_msg = "Cannot append '?' to nothing";
2449 nfa_add_question_op (&ctx);
2455 nfa_add_concatenation (&ctx);
2457 curlabel[0] = *regexp;
2458 nfa_add_label (&ctx, curlabel);
2465 error_msg = "Unbalanced parenthesis";
2468 while (--atomcount > 0)
2469 nfa_add_concatenation (&ctx);
2470 for (; altcount > 0; altcount--)
2471 nfa_add_alternation (&ctx);
2473 GNUNET_free_non_null (p);
2475 nfa = ctx.stack_tail;
2476 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2478 if (NULL != ctx.stack_head)
2480 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2484 /* Remember the regex that was used to generate this NFA */
2485 nfa->regex = GNUNET_strdup (regex);
2487 /* create depth-first numbering of the states for pretty printing */
2488 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2490 /* No multistriding added so far */
2491 nfa->is_multistrided = GNUNET_NO;
2496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2497 if (NULL != error_msg)
2498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2500 GNUNET_free_non_null (p);
2502 while (NULL != (nfa = ctx.stack_head))
2504 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2505 GNUNET_REGEX_automaton_destroy (nfa);
2513 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2515 * @param ctx context.
2516 * @param nfa NFA automaton.
2517 * @param dfa DFA automaton.
2518 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2522 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2523 struct GNUNET_REGEX_Automaton *nfa,
2524 struct GNUNET_REGEX_Automaton *dfa,
2525 struct GNUNET_REGEX_State *dfa_state)
2527 struct GNUNET_REGEX_Transition *ctran;
2528 struct GNUNET_REGEX_State *state_iter;
2529 struct GNUNET_REGEX_State *new_dfa_state;
2530 struct GNUNET_REGEX_State *state_contains;
2531 struct GNUNET_REGEX_StateSet *tmp;
2532 struct GNUNET_REGEX_StateSet *nfa_set;
2534 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2536 if (NULL == ctran->label || NULL != ctran->to_state)
2539 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2540 nfa_set = nfa_closure_set_create (nfa, tmp, 0);
2541 state_set_clear (tmp);
2542 new_dfa_state = dfa_state_create (ctx, nfa_set);
2543 state_contains = NULL;
2544 for (state_iter = dfa->states_head; NULL != state_iter;
2545 state_iter = state_iter->next)
2547 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2548 state_contains = state_iter;
2551 if (NULL == state_contains)
2553 automaton_add_state (dfa, new_dfa_state);
2554 ctran->to_state = new_dfa_state;
2555 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2559 ctran->to_state = state_contains;
2560 automaton_destroy_state (new_dfa_state);
2566 * Construct DFA for the given 'regex' of length 'len'.
2568 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
2569 * compressed to o -> abc -> o. Note that this parameter influences the
2570 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
2571 * edges with the same label). For example for an application that stores IPv4
2572 * addresses as bitstrings it could make sense to limit the path compression to
2575 * @param regex regular expression string.
2576 * @param len length of the regular expression.
2577 * @param max_path_len limit the path compression length to the
2578 * given value. If set to 1, no path compression is applied. Set to 0 for
2579 * maximal possible path compression (generally not desireable).
2580 * @return DFA, needs to be freed using GNUNET_REGEX_automaton_destroy.
2582 struct GNUNET_REGEX_Automaton *
2583 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len,
2586 struct GNUNET_REGEX_Context ctx;
2587 struct GNUNET_REGEX_Automaton *dfa;
2588 struct GNUNET_REGEX_Automaton *nfa;
2589 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2591 GNUNET_REGEX_context_init (&ctx);
2594 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2599 "Could not create DFA, because NFA creation failed\n");
2603 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2605 dfa->state_count = 0;
2606 dfa->states_head = NULL;
2607 dfa->states_tail = NULL;
2608 dfa->regex = GNUNET_strdup (regex);
2609 dfa->is_multistrided = GNUNET_NO;
2611 /* Create DFA start state from epsilon closure */
2612 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, 0);
2613 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2614 automaton_add_state (dfa, dfa->start);
2616 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2618 GNUNET_REGEX_automaton_destroy (nfa);
2621 dfa_minimize (&ctx, dfa);
2623 /* Create proofs and hashes for all states */
2624 automaton_create_proofs (dfa);
2626 /* Compress linear DFA paths */
2627 if (1 != max_path_len)
2628 dfa_compress_paths (&ctx, dfa, max_path_len);
2635 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2638 * @param a automaton to be destroyed
2641 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2643 struct GNUNET_REGEX_State *s;
2644 struct GNUNET_REGEX_State *next_state;
2649 GNUNET_free_non_null (a->regex);
2650 GNUNET_free_non_null (a->canonical_regex);
2652 for (s = a->states_head; NULL != s; s = next_state)
2654 next_state = s->next;
2655 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2656 automaton_destroy_state (s);
2664 * Evaluates the given string using the given DFA automaton
2666 * @param a automaton, type must be DFA
2667 * @param string string that should be evaluated
2669 * @return 0 if string matches, non 0 otherwise
2672 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2675 struct GNUNET_REGEX_State *s;
2676 unsigned int step_len;
2680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2681 "Tried to evaluate DFA, but NFA automaton given");
2687 /* If the string is empty but the starting state is accepting, we accept. */
2688 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2691 for (strp = string; NULL != strp && *strp; strp += step_len)
2693 step_len = dfa_move (&s, strp);
2699 if (NULL != s && s->accepting)
2707 * Evaluates the given string using the given NFA automaton
2709 * @param a automaton, type must be NFA
2710 * @param string string that should be evaluated
2712 * @return 0 if string matches, non 0 otherwise
2715 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2719 struct GNUNET_REGEX_State *s;
2720 struct GNUNET_REGEX_StateSet *sset;
2721 struct GNUNET_REGEX_StateSet *new_sset;
2727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2728 "Tried to evaluate NFA, but DFA automaton given");
2732 /* If the string is empty but the starting state is accepting, we accept. */
2733 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2737 sset = nfa_closure_create (a, a->start, 0);
2740 for (strp = string; NULL != strp && *strp; strp++)
2743 new_sset = nfa_closure_set_create (a, sset, str);
2744 state_set_clear (sset);
2745 sset = nfa_closure_set_create (a, new_sset, 0);
2746 state_set_clear (new_sset);
2749 for (i = 0; i < sset->len; i++)
2751 s = sset->states[i];
2752 if (NULL != s && s->accepting)
2759 state_set_clear (sset);
2765 * Evaluates the given 'string' against the given compiled regex
2767 * @param a automaton
2768 * @param string string to check
2770 * @return 0 if string matches, non 0 otherwise
2773 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2780 result = evaluate_dfa (a, string);
2783 result = evaluate_nfa (a, string);
2786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2787 "Evaluating regex failed, automaton has no type!\n");
2788 result = GNUNET_SYSERR;
2797 * Get the canonical regex of the given automaton.
2798 * When constructing the automaton a proof is computed for each state,
2799 * consisting of the regular expression leading to this state. A complete
2800 * regex for the automaton can be computed by combining these proofs.
2801 * As of now this function is only useful for testing.
2803 * @param a automaton for which the canonical regex should be returned.
2808 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2813 return a->canonical_regex;
2818 * Get the number of transitions that are contained in the given automaton.
2820 * @param a automaton for which the number of transitions should be returned.
2822 * @return number of transitions in the given automaton.
2825 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2827 unsigned int t_count;
2828 struct GNUNET_REGEX_State *s;
2834 for (s = a->states_head; NULL != s; s = s->next)
2835 t_count += s->transition_count;
2842 * Get the first key for the given 'input_string'. This hashes the first x bits
2843 * of the 'input_string'.
2845 * @param input_string string.
2846 * @param string_len length of the 'input_string'.
2847 * @param key pointer to where to write the hash code.
2849 * @return number of bits of 'input_string' that have been consumed
2850 * to construct the key
2853 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2854 struct GNUNET_HashCode * key)
2860 GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES;
2862 if (NULL == input_string)
2864 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2868 GNUNET_CRYPTO_hash (input_string, size, key);
2875 * Check if the given 'proof' matches the given 'key'.
2877 * @param proof partial regex of a state.
2878 * @param key hash of a state.
2880 * @return GNUNET_OK if the proof is valid for the given key.
2883 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2885 struct GNUNET_HashCode key_check;
2887 if (NULL == proof || NULL == key)
2889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2893 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2895 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2900 * Recursive function that calls the iterator for each synthetic start state.
2902 * @param min_len minimum length of the path in the graph.
2903 * @param max_len maximum length of the path in the graph.
2904 * @param consumed_string string consumed by traversing the graph till this state.
2905 * @param state current state of the automaton.
2906 * @param iterator iterator function called for each edge.
2907 * @param iterator_cls closure for the iterator function.
2910 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2911 char *consumed_string, struct GNUNET_REGEX_State *state,
2912 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2916 struct GNUNET_REGEX_Transition *t;
2917 unsigned int num_edges = state->transition_count;
2918 struct GNUNET_REGEX_Edge edges[num_edges];
2919 struct GNUNET_REGEX_Edge edge[1];
2920 struct GNUNET_HashCode hash;
2921 struct GNUNET_HashCode hash_new;
2923 unsigned int cur_len;
2925 if (NULL != consumed_string)
2926 cur_len = strlen (consumed_string);
2930 if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
2931 NULL != consumed_string)
2933 if (cur_len <= max_len)
2935 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2937 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2940 edges[i].label = t->label;
2941 edges[i].destination = t->to_state->hash;
2943 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2944 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2948 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2949 state->transition_count < 1 && cur_len < max_len)
2951 /* Special case for regex consisting of just a string that is shorter than
2953 edge[0].label = &consumed_string[cur_len - 1];
2954 edge[0].destination = state->hash;
2955 temp = GNUNET_strdup (consumed_string);
2956 temp[cur_len - 1] = '\0';
2957 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2958 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2962 else if (max_len < cur_len)
2964 /* Case where the concatenated labels are longer than max_len, then split. */
2965 edge[0].label = &consumed_string[max_len];
2966 edge[0].destination = state->hash;
2967 temp = GNUNET_strdup (consumed_string);
2968 temp[max_len] = '\0';
2969 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2970 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2975 if (cur_len < max_len)
2977 for (t = state->transitions_head; NULL != t; t = t->next)
2979 if (NULL != consumed_string)
2980 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2982 GNUNET_asprintf (&temp, "%s", t->label);
2984 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2993 * Iterate over all edges starting from start state of automaton 'a'. Calling
2994 * iterator for each edge.
2996 * @param a automaton.
2997 * @param iterator iterator called for each edge.
2998 * @param iterator_cls closure.
3001 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
3002 GNUNET_REGEX_KeyIterator iterator,
3005 struct GNUNET_REGEX_State *s;
3007 for (s = a->states_head; NULL != s; s = s->next)
3009 struct GNUNET_REGEX_Edge edges[s->transition_count];
3010 unsigned int num_edges;
3012 num_edges = state_get_edges (s, edges);
3014 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
3015 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
3018 s->marked = GNUNET_NO;
3021 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, GNUNET_REGEX_INITIAL_BYTES,
3022 NULL, a->start, iterator, iterator_cls);
3027 * Create a string with binary IP notation for the given 'addr' in 'str'.
3029 * @param af address family of the given 'addr'.
3030 * @param addr address that should be converted to a string.
3031 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
3032 * @param str string that will contain binary notation of 'addr'. Expected
3033 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
3036 iptobinstr (const int af, const void *addr, char *str)
3044 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
3048 for (i = 31; i >= 0; i--)
3050 *str = (b & 1) + '0';
3058 struct in6_addr b = *(const struct in6_addr *) addr;
3062 for (i = 127; i >= 0; i--)
3064 *str = (b.s6_addr[i / 8] & 1) + '0';
3066 b.s6_addr[i / 8] >>= 1;
3075 * Get the ipv4 network prefix from the given 'netmask'.
3077 * @param netmask netmask for which to get the prefix len.
3079 * @return length of ipv4 prefix for 'netmask'.
3082 ipv4netmasktoprefixlen (const char *netmask)
3088 if (1 != inet_pton (AF_INET, netmask, &a))
3091 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3098 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3100 * @param ip IPv4 representation.
3101 * @param netmask netmask for the ip.
3102 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3106 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3109 unsigned int pfxlen;
3111 pfxlen = ipv4netmasktoprefixlen (netmask);
3112 iptobinstr (AF_INET, ip, rxstr);
3113 rxstr[pfxlen] = '\0';
3115 strcat (rxstr, "(0|1)+");
3120 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3122 * @param ipv6 IPv6 representation.
3123 * @param prefixlen length of the ipv6 prefix.
3124 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3128 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3131 iptobinstr (AF_INET6, ipv6, rxstr);
3132 rxstr[prefixlen] = '\0';
3133 if (prefixlen < 128)
3134 strcat (rxstr, "(0|1)+");