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++;
1282 if (NULL == nfa_states)
1284 GNUNET_asprintf (&s->name, "s%i", s->id);
1288 s->nfa_set = nfa_states;
1290 if (nfa_states->len < 1)
1293 /* Create a name based on 'nfa_states' */
1294 s->name = GNUNET_malloc (sizeof (char) * 2);
1295 strcat (s->name, "{");
1298 for (i = 0; i < nfa_states->len; i++)
1300 cstate = nfa_states->states[i];
1301 GNUNET_asprintf (&name, "%i,", cstate->id);
1303 len = strlen (s->name) + strlen (name) + 1;
1304 s->name = GNUNET_realloc (s->name, len);
1305 strcat (s->name, name);
1309 /* Add a transition for each distinct label to NULL state */
1310 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1311 if (NULL != ctran->label)
1312 state_add_transition (ctx, s, ctran->label, NULL);
1314 /* If the nfa_states contain an accepting state, the new dfa state is also
1316 if (cstate->accepting)
1320 s->name[strlen (s->name) - 1] = '}';
1327 * Move from the given state 's' to the next state on transition 'str'. Consumes
1328 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1329 * 's' will point to the next state, and the length of the substring used for
1330 * this transition will be returned. If no transition possible 0 is returned and
1331 * 's' points to NULL.
1333 * @param s starting state, will point to the next state or NULL (if no
1334 * transition possible)
1335 * @param str edge label to follow (will match longest common prefix)
1337 * @return length of the substring comsumed from 'str'
1340 dfa_move (struct GNUNET_REGEX_State **s, const char *str)
1342 struct GNUNET_REGEX_Transition *t;
1343 struct GNUNET_REGEX_State *new_s;
1345 unsigned int max_len;
1352 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1354 len = strlen (t->label);
1356 if (0 == strncmp (t->label, str, len))
1361 new_s = t->to_state;
1371 * Set the given state 'marked' to GNUNET_YES. Used by the
1372 * 'dfa_remove_unreachable_states' function to detect unreachable states in the
1375 * @param cls closure, not used.
1376 * @param count count, not used.
1377 * @param s state where the marked attribute will be set to GNUNET_YES.
1380 mark_states (void *cls, const unsigned int count, struct GNUNET_REGEX_State *s)
1382 s->marked = GNUNET_YES;
1386 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1387 * states that are not reachable from the starting state.
1389 * @param a DFA automaton
1392 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
1394 struct GNUNET_REGEX_State *s;
1395 struct GNUNET_REGEX_State *s_next;
1397 /* 1. unmark all states */
1398 for (s = a->states_head; NULL != s; s = s->next)
1399 s->marked = GNUNET_NO;
1401 /* 2. traverse dfa from start state and mark all visited states */
1402 GNUNET_REGEX_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1404 /* 3. delete all states that were not visited */
1405 for (s = a->states_head; NULL != s; s = s_next)
1408 if (GNUNET_NO == s->marked)
1409 automaton_remove_state (a, s);
1415 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1416 * not transition to any other state but themselves.
1418 * @param a DFA automaton
1421 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
1423 struct GNUNET_REGEX_State *s;
1424 struct GNUNET_REGEX_State *s_next;
1425 struct GNUNET_REGEX_Transition *t;
1428 GNUNET_assert (DFA == a->type);
1430 for (s = a->states_head; NULL != s; s = s_next)
1438 for (t = s->transitions_head; NULL != t; t = t->next)
1440 if (NULL != t->to_state && t->to_state != s)
1450 /* state s is dead, remove it */
1451 automaton_remove_state (a, s);
1457 * Merge all non distinguishable states in the DFA 'a'
1459 * @param ctx context
1460 * @param a DFA automaton
1463 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
1464 struct GNUNET_REGEX_Automaton *a)
1467 struct GNUNET_REGEX_State *s1;
1468 struct GNUNET_REGEX_State *s2;
1469 struct GNUNET_REGEX_Transition *t1;
1470 struct GNUNET_REGEX_Transition *t2;
1471 struct GNUNET_REGEX_State *s1_next;
1472 struct GNUNET_REGEX_State *s2_next;
1474 unsigned int num_equal_edges;
1476 unsigned int state_cnt;
1478 if (NULL == a || 0 == a->state_count)
1480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1481 "Could not merge nondistinguishable states, automaton was NULL.\n");
1485 state_cnt = a->state_count;
1487 (int *) GNUNET_malloc_large (sizeof (int) * state_cnt * a->state_count);
1489 for (i = 0, s1 = a->states_head; i < state_cnt && NULL != s1;
1495 /* Mark all pairs of accepting/!accepting states */
1496 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1498 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1500 table[((s1->marked * state_cnt) + s2->marked)] = 0;
1502 if ((s1->accepting && !s2->accepting) ||
1503 (!s1->accepting && s2->accepting))
1505 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1510 /* Find all equal states */
1515 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1517 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
1519 if (0 != table[((s1->marked * state_cnt) + s2->marked)])
1522 num_equal_edges = 0;
1523 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
1525 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
1527 if (0 == strcmp (t1->label, t2->label))
1531 table[((t1->to_state->marked * state_cnt) +
1532 t2->to_state->marked)] ||
1534 table[((t2->to_state->marked * state_cnt) +
1535 t1->to_state->marked)])
1537 table[((s1->marked * state_cnt) + s2->marked)] = 1;
1543 if (num_equal_edges != s1->transition_count ||
1544 num_equal_edges != s2->transition_count)
1546 /* Make sure ALL edges of possible equal states are the same */
1547 table[((s1->marked * state_cnt) + s2->marked)] = -2;
1553 /* Merge states that are equal */
1554 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
1557 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
1560 if (0 == table[((s1->marked * state_cnt) + s2->marked)])
1561 automaton_merge_states (ctx, a, s1, s2);
1565 GNUNET_free (table);
1570 * Minimize the given DFA 'a' by removing all unreachable states, removing all
1571 * dead states and merging all non distinguishable states
1573 * @param ctx context
1574 * @param a DFA automaton
1577 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
1578 struct GNUNET_REGEX_Automaton *a)
1583 GNUNET_assert (DFA == a->type);
1585 /* 1. remove unreachable states */
1586 dfa_remove_unreachable_states (a);
1588 /* 2. remove dead states */
1589 dfa_remove_dead_states (a);
1591 /* 3. Merge nondistinguishable states */
1592 dfa_merge_nondistinguishable_states (ctx, a);
1597 * Context for adding strided transitions to a DFA.
1599 struct GNUNET_REGEX_Strided_Context
1602 * Length of the strides.
1604 const unsigned int stride;
1607 * Strided transitions DLL. New strided transitions will be stored in this DLL
1608 * and afterwards added to the DFA.
1610 struct GNUNET_REGEX_Transition *transitions_head;
1613 * Strided transitions DLL.
1615 struct GNUNET_REGEX_Transition *transitions_tail;
1620 * Recursive helper function to add strides to a DFA.
1622 * @param cls context, contains stride length and strided transitions DLL.
1623 * @param depth current depth of the depth-first traversal of the graph.
1624 * @param label current label, string that contains all labels on the path from
1626 * @param start start state for the depth-first traversal of the graph.
1627 * @param s current state in the depth-first traversal
1630 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
1631 struct GNUNET_REGEX_State *start,
1632 struct GNUNET_REGEX_State *s)
1634 struct GNUNET_REGEX_Strided_Context *ctx = cls;
1635 struct GNUNET_REGEX_Transition *t;
1638 if (depth == ctx->stride)
1640 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1641 t->label = GNUNET_strdup (label);
1643 t->from_state = start;
1644 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
1649 for (t = s->transitions_head; NULL != t; t = t->next)
1651 /* Do not consider self-loops, because it end's up in too many
1653 if (t->to_state == t->from_state)
1658 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1661 new_label = GNUNET_strdup (t->label);
1663 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
1667 GNUNET_free_non_null (label);
1672 * Function called for each state in the DFA. Starts a traversal of depth set in
1673 * context starting from state 's'.
1675 * @param cls context.
1676 * @param count not used.
1677 * @param s current state.
1680 dfa_add_multi_strides (void *cls, const unsigned int count,
1681 struct GNUNET_REGEX_State *s)
1683 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
1688 * Adds multi-strided transitions to the given 'dfa'.
1690 * @param regex_ctx regex context needed to add transitions to the automaton.
1691 * @param dfa DFA to which the multi strided transitions should be added.
1692 * @param stride_len length of the strides.
1695 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
1696 struct GNUNET_REGEX_Automaton *dfa,
1697 const unsigned int stride_len)
1699 struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
1700 struct GNUNET_REGEX_Transition *t;
1701 struct GNUNET_REGEX_Transition *t_next;
1703 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
1706 /* Compute the new transitions of given stride_len */
1707 GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
1708 &dfa_add_multi_strides, &ctx);
1710 /* Add all the new transitions to the automaton. */
1711 for (t = ctx.transitions_head; NULL != t; t = t_next)
1714 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1715 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
1716 GNUNET_free_non_null (t->label);
1720 /* Mark this automaton as multistrided */
1721 dfa->is_multistrided = GNUNET_YES;
1725 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
1726 * and adds new transitions to the given transitions DLL and marks states that
1727 * should be removed by setting state->contained to GNUNET_YES.
1729 * @param dfa DFA for which the paths should be compressed.
1730 * @param start starting state for linear path search.
1731 * @param cur current state in the recursive DFS.
1732 * @param label current label (string of traversed labels).
1733 * @param max_len maximal path compression length.
1734 * @param transitions_head transitions DLL.
1735 * @param transitions_tail transitions DLL.
1738 dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
1739 struct GNUNET_REGEX_State *start,
1740 struct GNUNET_REGEX_State *cur, char *label,
1741 unsigned int max_len,
1742 struct GNUNET_REGEX_Transition **transitions_head,
1743 struct GNUNET_REGEX_Transition **transitions_tail)
1745 struct GNUNET_REGEX_Transition *t;
1749 if (NULL != label &&
1750 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
1751 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
1752 max_len == strlen (label)) ||
1753 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
1755 t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
1756 t->label = GNUNET_strdup (label);
1758 t->from_state = start;
1759 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
1761 if (GNUNET_NO == cur->marked)
1763 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
1768 else if (cur != start)
1769 cur->contained = GNUNET_YES;
1771 if (GNUNET_YES == cur->marked && cur != start)
1774 cur->marked = GNUNET_YES;
1777 for (t = cur->transitions_head; NULL != t; t = t->next)
1780 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
1782 new_label = GNUNET_strdup (t->label);
1784 if (t->to_state != cur)
1786 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
1787 transitions_head, transitions_tail);
1789 GNUNET_free (new_label);
1794 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
1795 * compressed to 0->3 by combining transitions.
1797 * @param regex_ctx context for adding new transitions.
1798 * @param dfa DFA representation, will directly modify the given DFA.
1799 * @param max_len maximal length of the compressed paths.
1802 dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
1803 struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
1805 struct GNUNET_REGEX_State *s;
1806 struct GNUNET_REGEX_State *s_next;
1807 struct GNUNET_REGEX_Transition *t;
1808 struct GNUNET_REGEX_Transition *t_next;
1809 struct GNUNET_REGEX_Transition *transitions_head = NULL;
1810 struct GNUNET_REGEX_Transition *transitions_tail = NULL;
1815 /* Count the incoming transitions on each state. */
1816 for (s = dfa->states_head; NULL != s; s = s->next)
1818 for (t = s->transitions_head; NULL != t; t = t->next)
1820 if (NULL != t->to_state)
1821 t->to_state->incoming_transition_count++;
1825 /* Unmark all states. */
1826 for (s = dfa->states_head; NULL != s; s = s->next)
1828 s->marked = GNUNET_NO;
1829 s->contained = GNUNET_NO;
1832 /* Add strides and mark states that can be deleted. */
1833 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
1834 &transitions_head, &transitions_tail);
1836 /* Add all the new transitions to the automaton. */
1837 for (t = transitions_head; NULL != t; t = t_next)
1840 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
1841 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
1842 GNUNET_free_non_null (t->label);
1846 /* Remove marked states (including their incoming and outgoing transitions). */
1847 for (s = dfa->states_head; NULL != s; s = s_next)
1850 if (GNUNET_YES == s->contained)
1851 automaton_remove_state (dfa, s);
1857 * Creates a new NFA fragment. Needs to be cleared using
1858 * automaton_fragment_clear.
1860 * @param start starting state
1861 * @param end end state
1863 * @return new NFA fragment
1865 static struct GNUNET_REGEX_Automaton *
1866 nfa_fragment_create (struct GNUNET_REGEX_State *start,
1867 struct GNUNET_REGEX_State *end)
1869 struct GNUNET_REGEX_Automaton *n;
1871 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1878 if (NULL == start || NULL == end)
1881 automaton_add_state (n, end);
1882 automaton_add_state (n, start);
1894 * Adds a list of states to the given automaton 'n'.
1896 * @param n automaton to which the states should be added
1897 * @param states_head head of the DLL of states
1898 * @param states_tail tail of the DLL of states
1901 nfa_add_states (struct GNUNET_REGEX_Automaton *n,
1902 struct GNUNET_REGEX_State *states_head,
1903 struct GNUNET_REGEX_State *states_tail)
1905 struct GNUNET_REGEX_State *s;
1907 if (NULL == n || NULL == states_head)
1909 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
1913 if (NULL == n->states_head)
1915 n->states_head = states_head;
1916 n->states_tail = states_tail;
1920 if (NULL != states_head)
1922 n->states_tail->next = states_head;
1923 n->states_tail = states_tail;
1926 for (s = states_head; NULL != s; s = s->next)
1932 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
1934 * @param ctx context
1935 * @param accepting is it an accepting state or not
1937 * @return new NFA state
1939 static struct GNUNET_REGEX_State *
1940 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
1942 struct GNUNET_REGEX_State *s;
1944 s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
1945 s->id = ctx->state_id++;
1946 s->accepting = accepting;
1947 s->marked = GNUNET_NO;
1953 GNUNET_asprintf (&s->name, "s%i", s->id);
1960 * Calculates the NFA closure set for the given state.
1962 * @param nfa the NFA containing 's'
1963 * @param s starting point state
1964 * @param label transitioning label on which to base the closure on,
1965 * pass NULL for epsilon transition
1967 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
1969 static struct GNUNET_REGEX_StateSet *
1970 nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
1971 struct GNUNET_REGEX_State *s, const char *label)
1974 struct GNUNET_REGEX_StateSet *cls;
1975 struct GNUNET_REGEX_StateSet_MDLL cls_stack;
1976 struct GNUNET_REGEX_State *clsstate;
1977 struct GNUNET_REGEX_State *currentstate;
1978 struct GNUNET_REGEX_Transition *ctran;
1983 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
1984 cls_stack.head = NULL;
1985 cls_stack.tail = NULL;
1987 /* Add start state to closure only for epsilon closure */
1989 GNUNET_array_append (cls->states, cls->len, s);
1991 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
1994 while (cls_stack.len > 0)
1996 currentstate = cls_stack.tail;
1997 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2001 for (ctran = currentstate->transitions_head; NULL != ctran;
2002 ctran = ctran->next)
2004 if (NULL != ctran->to_state && 0 == nullstrcmp (label, ctran->label))
2006 clsstate = ctran->to_state;
2008 if (NULL != clsstate && 0 == clsstate->contained)
2010 GNUNET_array_append (cls->states, cls->len, clsstate);
2011 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2014 clsstate->contained = 1;
2020 for (i = 0; i < cls->len; i++)
2021 cls->states[i]->contained = 0;
2023 /* sort the states */
2025 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2033 * Calculates the closure set for the given set of states.
2035 * @param nfa the NFA containing 's'
2036 * @param states list of states on which to base the closure on
2037 * @param label transitioning label for which to base the closure on,
2038 * pass NULL for epsilon transition
2040 * @return sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2042 static struct GNUNET_REGEX_StateSet *
2043 nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
2044 struct GNUNET_REGEX_StateSet *states, const char *label)
2046 struct GNUNET_REGEX_State *s;
2047 struct GNUNET_REGEX_StateSet *sset;
2048 struct GNUNET_REGEX_StateSet *cls;
2052 unsigned int contains;
2057 cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
2059 for (i = 0; i < states->len; i++)
2061 s = states->states[i];
2062 sset = nfa_closure_create (nfa, s, label);
2064 for (j = 0; j < sset->len; j++)
2067 for (k = 0; k < cls->len; k++)
2069 if (sset->states[j]->id == cls->states[k]->id)
2076 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
2078 state_set_clear (sset);
2082 qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
2090 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2092 * @param ctx context
2095 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
2097 struct GNUNET_REGEX_Automaton *a;
2098 struct GNUNET_REGEX_Automaton *b;
2099 struct GNUNET_REGEX_Automaton *new_nfa;
2101 b = ctx->stack_tail;
2102 GNUNET_assert (NULL != b);
2103 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2104 a = ctx->stack_tail;
2105 GNUNET_assert (NULL != a);
2106 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2108 state_add_transition (ctx, a->end, NULL, b->start);
2109 a->end->accepting = 0;
2110 b->end->accepting = 1;
2112 new_nfa = nfa_fragment_create (NULL, NULL);
2113 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2114 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2115 new_nfa->start = a->start;
2116 new_nfa->end = b->end;
2117 new_nfa->state_count += a->state_count + b->state_count;
2118 automaton_fragment_clear (a);
2119 automaton_fragment_clear (b);
2121 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2126 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2128 * @param ctx context
2131 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
2133 struct GNUNET_REGEX_Automaton *a;
2134 struct GNUNET_REGEX_Automaton *new_nfa;
2135 struct GNUNET_REGEX_State *start;
2136 struct GNUNET_REGEX_State *end;
2138 a = ctx->stack_tail;
2142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2143 "nfa_add_star_op failed, because there was no element on the stack");
2147 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2149 start = nfa_state_create (ctx, 0);
2150 end = nfa_state_create (ctx, 1);
2152 state_add_transition (ctx, start, NULL, a->start);
2153 state_add_transition (ctx, start, NULL, end);
2154 state_add_transition (ctx, a->end, NULL, a->start);
2155 state_add_transition (ctx, a->end, NULL, end);
2157 a->end->accepting = 0;
2160 new_nfa = nfa_fragment_create (start, end);
2161 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2162 automaton_fragment_clear (a);
2164 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2169 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2171 * @param ctx context
2174 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
2176 struct GNUNET_REGEX_Automaton *a;
2178 a = ctx->stack_tail;
2182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2183 "nfa_add_plus_op failed, because there was no element on the stack");
2187 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2189 state_add_transition (ctx, a->end, NULL, a->start);
2191 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2196 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2198 * @param ctx context
2201 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
2203 struct GNUNET_REGEX_Automaton *a;
2204 struct GNUNET_REGEX_Automaton *new_nfa;
2205 struct GNUNET_REGEX_State *start;
2206 struct GNUNET_REGEX_State *end;
2208 a = ctx->stack_tail;
2212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2213 "nfa_add_question_op failed, because there was no element on the stack");
2217 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2219 start = nfa_state_create (ctx, 0);
2220 end = nfa_state_create (ctx, 1);
2222 state_add_transition (ctx, start, NULL, a->start);
2223 state_add_transition (ctx, start, NULL, end);
2224 state_add_transition (ctx, a->end, NULL, end);
2226 a->end->accepting = 0;
2228 new_nfa = nfa_fragment_create (start, end);
2229 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2230 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2231 automaton_fragment_clear (a);
2236 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2237 * alternates between a and b (a|b)
2239 * @param ctx context
2242 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
2244 struct GNUNET_REGEX_Automaton *a;
2245 struct GNUNET_REGEX_Automaton *b;
2246 struct GNUNET_REGEX_Automaton *new_nfa;
2247 struct GNUNET_REGEX_State *start;
2248 struct GNUNET_REGEX_State *end;
2250 b = ctx->stack_tail;
2251 GNUNET_assert (NULL != b);
2252 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2253 a = ctx->stack_tail;
2254 GNUNET_assert (NULL != a);
2255 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2257 start = nfa_state_create (ctx, 0);
2258 end = nfa_state_create (ctx, 1);
2259 state_add_transition (ctx, start, NULL, a->start);
2260 state_add_transition (ctx, start, NULL, b->start);
2262 state_add_transition (ctx, a->end, NULL, end);
2263 state_add_transition (ctx, b->end, NULL, end);
2265 a->end->accepting = 0;
2266 b->end->accepting = 0;
2269 new_nfa = nfa_fragment_create (start, end);
2270 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2271 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2272 automaton_fragment_clear (a);
2273 automaton_fragment_clear (b);
2275 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2280 * Adds a new nfa fragment to the stack
2282 * @param ctx context
2283 * @param label label for nfa transition
2286 nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char *label)
2288 struct GNUNET_REGEX_Automaton *n;
2289 struct GNUNET_REGEX_State *start;
2290 struct GNUNET_REGEX_State *end;
2292 GNUNET_assert (NULL != ctx);
2294 start = nfa_state_create (ctx, 0);
2295 end = nfa_state_create (ctx, 1);
2296 state_add_transition (ctx, start, label, end);
2297 n = nfa_fragment_create (start, end);
2298 GNUNET_assert (NULL != n);
2299 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2304 * Initialize a new context
2306 * @param ctx context
2309 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
2313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2317 ctx->transition_id = 0;
2318 ctx->stack_head = NULL;
2319 ctx->stack_tail = NULL;
2324 * Construct an NFA by parsing the regex string of length 'len'.
2326 * @param regex regular expression string
2327 * @param len length of the string
2329 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
2331 struct GNUNET_REGEX_Automaton *
2332 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2334 struct GNUNET_REGEX_Context ctx;
2335 struct GNUNET_REGEX_Automaton *nfa;
2340 unsigned int altcount;
2341 unsigned int atomcount;
2342 unsigned int pcount;
2349 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2352 "Could not parse regex. Empty regex string provided.\n");
2357 GNUNET_REGEX_context_init (&ctx);
2367 for (count = 0; count < len && *regexp; count++, regexp++)
2375 nfa_add_concatenation (&ctx);
2377 GNUNET_array_grow (p, pcount, pcount + 1);
2378 p[pcount - 1].altcount = altcount;
2379 p[pcount - 1].atomcount = atomcount;
2386 error_msg = "Cannot append '|' to nothing";
2389 while (--atomcount > 0)
2390 nfa_add_concatenation (&ctx);
2396 error_msg = "Missing opening '('";
2401 /* Ignore this: "()" */
2403 altcount = p[pcount].altcount;
2404 atomcount = p[pcount].atomcount;
2407 while (--atomcount > 0)
2408 nfa_add_concatenation (&ctx);
2409 for (; altcount > 0; altcount--)
2410 nfa_add_alternation (&ctx);
2412 altcount = p[pcount].altcount;
2413 atomcount = p[pcount].atomcount;
2419 error_msg = "Cannot append '*' to nothing";
2422 nfa_add_star_op (&ctx);
2427 error_msg = "Cannot append '+' to nothing";
2430 nfa_add_plus_op (&ctx);
2435 error_msg = "Cannot append '?' to nothing";
2438 nfa_add_question_op (&ctx);
2444 nfa_add_concatenation (&ctx);
2446 curlabel[0] = *regexp;
2447 nfa_add_label (&ctx, curlabel);
2454 error_msg = "Unbalanced parenthesis";
2457 while (--atomcount > 0)
2458 nfa_add_concatenation (&ctx);
2459 for (; altcount > 0; altcount--)
2460 nfa_add_alternation (&ctx);
2462 GNUNET_free_non_null (p);
2464 nfa = ctx.stack_tail;
2465 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2467 if (NULL != ctx.stack_head)
2469 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2473 /* Remember the regex that was used to generate this NFA */
2474 nfa->regex = GNUNET_strdup (regex);
2476 /* create depth-first numbering of the states for pretty printing */
2477 GNUNET_REGEX_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2479 /* No multistriding added so far */
2480 nfa->is_multistrided = GNUNET_NO;
2485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
2486 if (NULL != error_msg)
2487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2489 GNUNET_free_non_null (p);
2491 while (NULL != (nfa = ctx.stack_head))
2493 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2494 GNUNET_REGEX_automaton_destroy (nfa);
2502 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2504 * @param ctx context.
2505 * @param nfa NFA automaton.
2506 * @param dfa DFA automaton.
2507 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2511 construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
2512 struct GNUNET_REGEX_Automaton *nfa,
2513 struct GNUNET_REGEX_Automaton *dfa,
2514 struct GNUNET_REGEX_State *dfa_state)
2516 struct GNUNET_REGEX_Transition *ctran;
2517 struct GNUNET_REGEX_State *state_iter;
2518 struct GNUNET_REGEX_State *new_dfa_state;
2519 struct GNUNET_REGEX_State *state_contains;
2520 struct GNUNET_REGEX_StateSet *tmp;
2521 struct GNUNET_REGEX_StateSet *nfa_set;
2523 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2525 if (NULL == ctran->label || NULL != ctran->to_state)
2528 tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
2529 nfa_set = nfa_closure_set_create (nfa, tmp, NULL);
2530 state_set_clear (tmp);
2531 new_dfa_state = dfa_state_create (ctx, nfa_set);
2532 state_contains = NULL;
2533 for (state_iter = dfa->states_head; NULL != state_iter;
2534 state_iter = state_iter->next)
2536 if (0 == state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
2537 state_contains = state_iter;
2540 if (NULL == state_contains)
2542 automaton_add_state (dfa, new_dfa_state);
2543 ctran->to_state = new_dfa_state;
2544 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
2548 ctran->to_state = state_contains;
2549 automaton_destroy_state (new_dfa_state);
2556 * Construct DFA for the given 'regex' of length 'len'.
2558 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
2559 * compressed to o -> abc -> o. Note that this parameter influences the
2560 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
2561 * edges with the same label). For example for an application that stores IPv4
2562 * addresses as bitstrings it could make sense to limit the path compression to
2565 * @param regex regular expression string.
2566 * @param len length of the regular expression.
2567 * @param max_path_len limit the path compression length to the
2568 * given value. If set to 1, no path compression is applied. Set to 0 for
2569 * maximal possible path compression (generally not desireable).
2570 * @return DFA, needs to be freed using GNUNET_REGEX_automaton_destroy.
2572 struct GNUNET_REGEX_Automaton *
2573 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len,
2574 unsigned int max_path_len)
2576 struct GNUNET_REGEX_Context ctx;
2577 struct GNUNET_REGEX_Automaton *dfa;
2578 struct GNUNET_REGEX_Automaton *nfa;
2579 struct GNUNET_REGEX_StateSet *nfa_start_eps_cls;
2581 GNUNET_REGEX_context_init (&ctx);
2584 nfa = GNUNET_REGEX_construct_nfa (regex, len);
2588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2589 "Could not create DFA, because NFA creation failed\n");
2593 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
2595 dfa->regex = GNUNET_strdup (regex);
2597 /* Create DFA start state from epsilon closure */
2598 nfa_start_eps_cls = nfa_closure_create (nfa, nfa->start, NULL);
2599 dfa->start = dfa_state_create (&ctx, nfa_start_eps_cls);
2600 automaton_add_state (dfa, dfa->start);
2602 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
2604 GNUNET_REGEX_automaton_destroy (nfa);
2607 dfa_minimize (&ctx, dfa);
2609 /* Create proofs and hashes for all states */
2610 automaton_create_proofs (dfa);
2612 /* Compress linear DFA paths */
2613 if (1 != max_path_len)
2614 dfa_compress_paths (&ctx, dfa, max_path_len);
2621 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
2624 * @param a automaton to be destroyed
2627 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
2629 struct GNUNET_REGEX_State *s;
2630 struct GNUNET_REGEX_State *next_state;
2635 GNUNET_free_non_null (a->regex);
2636 GNUNET_free_non_null (a->canonical_regex);
2638 for (s = a->states_head; NULL != s; s = next_state)
2640 next_state = s->next;
2641 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
2642 automaton_destroy_state (s);
2650 * Evaluates the given string using the given DFA automaton
2652 * @param a automaton, type must be DFA
2653 * @param string string that should be evaluated
2655 * @return 0 if string matches, non 0 otherwise
2658 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2661 struct GNUNET_REGEX_State *s;
2662 unsigned int step_len;
2666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2667 "Tried to evaluate DFA, but NFA automaton given");
2673 /* If the string is empty but the starting state is accepting, we accept. */
2674 if ((NULL == string || 0 == strlen (string)) && s->accepting)
2677 for (strp = string; NULL != strp && *strp; strp += step_len)
2679 step_len = dfa_move (&s, strp);
2685 if (NULL != s && s->accepting)
2693 * Evaluates the given string using the given NFA automaton
2695 * @param a automaton, type must be NFA
2696 * @param string string that should be evaluated
2698 * @return 0 if string matches, non 0 otherwise
2701 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
2705 struct GNUNET_REGEX_State *s;
2706 struct GNUNET_REGEX_StateSet *sset;
2707 struct GNUNET_REGEX_StateSet *new_sset;
2713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2714 "Tried to evaluate NFA, but DFA automaton given");
2718 /* If the string is empty but the starting state is accepting, we accept. */
2719 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
2723 sset = nfa_closure_create (a, a->start, 0);
2726 for (strp = string; NULL != strp && *strp; strp++)
2729 new_sset = nfa_closure_set_create (a, sset, str);
2730 state_set_clear (sset);
2731 sset = nfa_closure_set_create (a, new_sset, 0);
2732 state_set_clear (new_sset);
2735 for (i = 0; i < sset->len; i++)
2737 s = sset->states[i];
2738 if (NULL != s && s->accepting)
2745 state_set_clear (sset);
2751 * Evaluates the given 'string' against the given compiled regex
2753 * @param a automaton
2754 * @param string string to check
2756 * @return 0 if string matches, non 0 otherwise
2759 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
2766 result = evaluate_dfa (a, string);
2769 result = evaluate_nfa (a, string);
2772 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2773 "Evaluating regex failed, automaton has no type!\n");
2774 result = GNUNET_SYSERR;
2783 * Get the canonical regex of the given automaton.
2784 * When constructing the automaton a proof is computed for each state,
2785 * consisting of the regular expression leading to this state. A complete
2786 * regex for the automaton can be computed by combining these proofs.
2787 * As of now this function is only useful for testing.
2789 * @param a automaton for which the canonical regex should be returned.
2794 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
2799 return a->canonical_regex;
2804 * Get the number of transitions that are contained in the given automaton.
2806 * @param a automaton for which the number of transitions should be returned.
2808 * @return number of transitions in the given automaton.
2811 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a)
2813 unsigned int t_count;
2814 struct GNUNET_REGEX_State *s;
2820 for (s = a->states_head; NULL != s; s = s->next)
2821 t_count += s->transition_count;
2828 * Get the first key for the given 'input_string'. This hashes the first x bits
2829 * of the 'input_string'.
2831 * @param input_string string.
2832 * @param string_len length of the 'input_string'.
2833 * @param key pointer to where to write the hash code.
2835 * @return number of bits of 'input_string' that have been consumed
2836 * to construct the key
2839 GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
2840 struct GNUNET_HashCode * key)
2846 GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES;
2848 if (NULL == input_string)
2850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
2854 GNUNET_CRYPTO_hash (input_string, size, key);
2861 * Check if the given 'proof' matches the given 'key'.
2863 * @param proof partial regex of a state.
2864 * @param key hash of a state.
2866 * @return GNUNET_OK if the proof is valid for the given key.
2869 GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
2871 struct GNUNET_HashCode key_check;
2873 if (NULL == proof || NULL == key)
2875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
2879 GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
2881 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
2886 * Recursive function that calls the iterator for each synthetic start state.
2888 * @param min_len minimum length of the path in the graph.
2889 * @param max_len maximum length of the path in the graph.
2890 * @param consumed_string string consumed by traversing the graph till this state.
2891 * @param state current state of the automaton.
2892 * @param iterator iterator function called for each edge.
2893 * @param iterator_cls closure for the iterator function.
2896 iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
2897 char *consumed_string, struct GNUNET_REGEX_State *state,
2898 GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
2902 struct GNUNET_REGEX_Transition *t;
2903 unsigned int num_edges = state->transition_count;
2904 struct GNUNET_REGEX_Edge edges[num_edges];
2905 struct GNUNET_REGEX_Edge edge[1];
2906 struct GNUNET_HashCode hash;
2907 struct GNUNET_HashCode hash_new;
2909 unsigned int cur_len;
2911 if (NULL != consumed_string)
2912 cur_len = strlen (consumed_string);
2916 if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
2917 NULL != consumed_string)
2919 if (cur_len <= max_len)
2921 if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
2923 for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
2926 edges[i].label = t->label;
2927 edges[i].destination = t->to_state->hash;
2929 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
2930 iterator (iterator_cls, &hash, consumed_string, state->accepting,
2934 if (GNUNET_YES == state->accepting && cur_len > 1 &&
2935 state->transition_count < 1 && cur_len < max_len)
2937 /* Special case for regex consisting of just a string that is shorter than
2939 edge[0].label = &consumed_string[cur_len - 1];
2940 edge[0].destination = state->hash;
2941 temp = GNUNET_strdup (consumed_string);
2942 temp[cur_len - 1] = '\0';
2943 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
2944 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
2948 else if (max_len < cur_len)
2950 /* Case where the concatenated labels are longer than max_len, then split. */
2951 edge[0].label = &consumed_string[max_len];
2952 edge[0].destination = state->hash;
2953 temp = GNUNET_strdup (consumed_string);
2954 temp[max_len] = '\0';
2955 GNUNET_CRYPTO_hash (temp, max_len, &hash);
2956 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
2961 if (cur_len < max_len)
2963 for (t = state->transitions_head; NULL != t; t = t->next)
2965 if (NULL != consumed_string)
2966 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label);
2968 GNUNET_asprintf (&temp, "%s", t->label);
2970 iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
2979 * Iterate over all edges starting from start state of automaton 'a'. Calling
2980 * iterator for each edge.
2982 * @param a automaton.
2983 * @param iterator iterator called for each edge.
2984 * @param iterator_cls closure.
2987 GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
2988 GNUNET_REGEX_KeyIterator iterator,
2991 struct GNUNET_REGEX_State *s;
2993 for (s = a->states_head; NULL != s; s = s->next)
2995 struct GNUNET_REGEX_Edge edges[s->transition_count];
2996 unsigned int num_edges;
2998 num_edges = state_get_edges (s, edges);
3000 if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
3001 iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
3004 s->marked = GNUNET_NO;
3007 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, GNUNET_REGEX_INITIAL_BYTES,
3008 NULL, a->start, iterator, iterator_cls);
3013 * Create a string with binary IP notation for the given 'addr' in 'str'.
3015 * @param af address family of the given 'addr'.
3016 * @param addr address that should be converted to a string.
3017 * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
3018 * @param str string that will contain binary notation of 'addr'. Expected
3019 * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
3022 iptobinstr (const int af, const void *addr, char *str)
3030 uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
3034 for (i = 31; i >= 0; i--)
3036 *str = (b & 1) + '0';
3044 struct in6_addr b = *(const struct in6_addr *) addr;
3048 for (i = 127; i >= 0; i--)
3050 *str = (b.s6_addr[i / 8] & 1) + '0';
3052 b.s6_addr[i / 8] >>= 1;
3061 * Get the ipv4 network prefix from the given 'netmask'.
3063 * @param netmask netmask for which to get the prefix len.
3065 * @return length of ipv4 prefix for 'netmask'.
3068 ipv4netmasktoprefixlen (const char *netmask)
3074 if (1 != inet_pton (AF_INET, netmask, &a))
3077 for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
3084 * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
3086 * @param ip IPv4 representation.
3087 * @param netmask netmask for the ip.
3088 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
3092 GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
3095 unsigned int pfxlen;
3097 pfxlen = ipv4netmasktoprefixlen (netmask);
3098 iptobinstr (AF_INET, ip, rxstr);
3099 rxstr[pfxlen] = '\0';
3101 strcat (rxstr, "(0|1)+");
3106 * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
3108 * @param ipv6 IPv6 representation.
3109 * @param prefixlen length of the ipv6 prefix.
3110 * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
3114 GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
3117 iptobinstr (AF_INET6, ipv6, rxstr);
3118 rxstr[prefixlen] = '\0';
3119 if (prefixlen < 128)
3120 strcat (rxstr, "(0|1)+");