2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file src/regex/regex_internal.c
22 * @brief library to create Deterministic Finite Automatons (DFAs) from regular
23 * expressions (regexes).
24 * @author Maximilian Szengel
27 #include "gnunet_util_lib.h"
28 #include "gnunet_regex_service.h"
29 #include "regex_internal_lib.h"
30 #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
40 * Set of states using MDLL API.
42 struct REGEX_INTERNAL_StateSet_MDLL
47 struct REGEX_INTERNAL_State *head;
52 struct REGEX_INTERNAL_State *tail;
62 * Append state to the given StateSet.
64 * @param set set to be modified
65 * @param state state to be appended
68 state_set_append (struct REGEX_INTERNAL_StateSet *set,
69 struct REGEX_INTERNAL_State *state)
71 if (set->off == set->size)
72 GNUNET_array_grow (set->states, set->size, set->size * 2 + 4);
73 set->states[set->off++] = state;
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 @a 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 REGEX_INTERNAL_Context *ctx,
108 struct REGEX_INTERNAL_State *from_state,
110 struct REGEX_INTERNAL_State *to_state)
112 struct REGEX_INTERNAL_Transition *t;
113 struct REGEX_INTERNAL_Transition *oth;
115 if (NULL == from_state)
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 "Could not create Transition.\n");
122 /* Do not add duplicate state transitions */
123 for (t = from_state->transitions_head; NULL != t; t = t->next)
125 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
126 t->from_state == from_state)
130 /* sort transitions by label */
131 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
133 if (0 < nullstrcmp (oth->label, label))
137 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
139 t->id = ctx->transition_id++;
141 t->label = GNUNET_strdup (label);
144 t->to_state = to_state;
145 t->from_state = from_state;
147 /* Add outgoing transition to 'from_state' */
148 from_state->transition_count++;
149 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
150 from_state->transitions_tail, oth, t);
155 * Remove a 'transition' from 'state'.
157 * @param state state from which the to-be-removed transition originates.
158 * @param transition transition that should be removed from state 'state'.
161 state_remove_transition (struct REGEX_INTERNAL_State *state,
162 struct REGEX_INTERNAL_Transition *transition)
164 if (NULL == state || NULL == transition)
167 if (transition->from_state != state)
170 GNUNET_free_non_null (transition->label);
172 state->transition_count--;
173 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
176 GNUNET_free (transition);
181 * Compare two states. Used for sorting.
183 * @param a first state
184 * @param b second state
186 * @return an integer less than, equal to, or greater than zero
187 * if the first argument is considered to be respectively
188 * less than, equal to, or greater than the second.
191 state_compare (const void *a, const void *b)
193 struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a;
194 struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b;
196 return (*s1)->id - (*s2)->id;
201 * Get all edges leaving state @a s.
204 * @param edges all edges leaving @a s, expected to be allocated and have enough
205 * space for `s->transitions_count` elements.
207 * @return number of edges.
210 state_get_edges (struct REGEX_INTERNAL_State *s,
211 struct REGEX_BLOCK_Edge *edges)
213 struct REGEX_INTERNAL_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
240 * @return 0 if the sets are equal, otherwise non-zero
243 state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
244 struct REGEX_INTERNAL_StateSet *sset2)
249 if (NULL == sset1 || NULL == sset2)
252 result = sset1->off - sset2->off;
257 for (i = 0; i < sset1->off; i++)
258 if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i])))
265 * Clears the given StateSet 'set'
267 * @param set set to be cleared
270 state_set_clear (struct REGEX_INTERNAL_StateSet *set)
272 GNUNET_array_grow (set->states, set->size, 0);
278 * Clears an automaton fragment. Does not destroy the states inside the
281 * @param a automaton to be cleared
284 automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
291 a->states_head = NULL;
292 a->states_tail = NULL;
299 * Frees the memory used by State @a s
301 * @param s state that should be destroyed
304 automaton_destroy_state (struct REGEX_INTERNAL_State *s)
306 struct REGEX_INTERNAL_Transition *t;
307 struct REGEX_INTERNAL_Transition *next_t;
312 GNUNET_free_non_null (s->name);
313 GNUNET_free_non_null (s->proof);
314 state_set_clear (&s->nfa_set);
315 for (t = s->transitions_head; NULL != t; t = next_t)
318 state_remove_transition (s, t);
326 * Remove a state from the given automaton 'a'. Always use this function when
327 * altering the states of an automaton. Will also remove all transitions leading
328 * to this state, before destroying it.
331 * @param s state to remove
334 automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
335 struct REGEX_INTERNAL_State *s)
337 struct REGEX_INTERNAL_State *s_check;
338 struct REGEX_INTERNAL_Transition *t_check;
339 struct REGEX_INTERNAL_Transition *t_check_next;
341 if (NULL == a || NULL == s)
344 /* remove all transitions leading to this state */
345 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
347 for (t_check = s_check->transitions_head; NULL != t_check;
348 t_check = t_check_next)
350 t_check_next = t_check->next;
351 if (t_check->to_state == s)
352 state_remove_transition (s_check, t_check);
357 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
360 automaton_destroy_state (s);
365 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
366 * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
370 * @param s1 first state
371 * @param s2 second state, will be destroyed
374 automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
375 struct REGEX_INTERNAL_Automaton *a,
376 struct REGEX_INTERNAL_State *s1,
377 struct REGEX_INTERNAL_State *s2)
379 struct REGEX_INTERNAL_State *s_check;
380 struct REGEX_INTERNAL_Transition *t_check;
381 struct REGEX_INTERNAL_Transition *t;
382 struct REGEX_INTERNAL_Transition *t_next;
388 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
389 * does not already exists, if it already exists remove transition. */
390 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
392 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
394 t_next = t_check->next;
396 if (s2 == t_check->to_state)
399 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
401 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
404 if (GNUNET_NO == is_dup)
405 t_check->to_state = s1;
407 state_remove_transition (t_check->from_state, t_check);
412 /* 2. Add all transitions from s2 to sX to s1 */
413 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
415 if (t_check->to_state != s1)
416 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
419 /* 3. Rename s1 to {s1,s2} */
424 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
425 GNUNET_free (new_name);
429 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
431 automaton_destroy_state (s2);
436 * Add a state to the automaton 'a', always use this function to alter the
437 * states DLL of the automaton.
439 * @param a automaton to add the state to
440 * @param s state that should be added
443 automaton_add_state (struct REGEX_INTERNAL_Automaton *a,
444 struct REGEX_INTERNAL_State *s)
446 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
452 * Depth-first traversal (DFS) of all states that are reachable from state
453 * 's'. Performs 'action' on each visited state.
455 * @param s start state.
456 * @param marks an array of size a->state_count to remember which state was
458 * @param count current count of the state.
459 * @param check function that is checked before advancing on each transition
461 * @param check_cls closure for check.
462 * @param action action to be performed on each state.
463 * @param action_cls closure for action.
466 automaton_state_traverse (struct REGEX_INTERNAL_State *s, int *marks,
468 REGEX_INTERNAL_traverse_check check, void *check_cls,
469 REGEX_INTERNAL_traverse_action action, void *action_cls)
471 struct REGEX_INTERNAL_Transition *t;
473 if (GNUNET_YES == marks[s->traversal_id])
476 marks[s->traversal_id] = GNUNET_YES;
479 action (action_cls, *count, s);
483 for (t = s->transitions_head; NULL != t; t = t->next)
486 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
488 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
496 * Traverses the given automaton using depth-first-search (DFS) from it's start
497 * state, visiting all reachable states and calling 'action' on each one of
500 * @param a automaton to be traversed.
501 * @param start start state, pass a->start or NULL to traverse the whole automaton.
502 * @param check function that is checked before advancing on each transition
504 * @param check_cls closure for @a check.
505 * @param action action to be performed on each state.
506 * @param action_cls closure for @a action
509 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
510 struct REGEX_INTERNAL_State *start,
511 REGEX_INTERNAL_traverse_check check,
513 REGEX_INTERNAL_traverse_action action,
517 struct REGEX_INTERNAL_State *s;
519 if (NULL == a || 0 == a->state_count)
522 int marks[a->state_count];
524 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
525 s = s->next, count++)
527 s->traversal_id = count;
528 marks[s->traversal_id] = GNUNET_NO;
538 automaton_state_traverse (s, marks, &count,
545 * String container for faster string operations.
550 * Buffer holding the string (may start in the middle!);
561 * Length of the string in the buffer.
566 * Number of bytes allocated for @e sbuf
571 * Buffer currently represents "NULL" (not the empty string!)
576 * If this entry is part of the last/current generation array,
577 * this flag is #GNUNET_YES if the last and current generation are
578 * identical (and thus copying is unnecessary if the value didn't
579 * change). This is used in an optimization that improves
580 * performance by about 1% --- if we use int16_t here. With just
581 * "int" for both flags, performance drops (on my system) significantly,
582 * most likely due to increased cache misses.
590 * Compare two strings for equality. If either is NULL they are not equal.
592 * @param s1 first string for comparison.
593 * @param s2 second string for comparison.
595 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
598 sb_nullstrcmp (const struct StringBuffer *s1,
599 const struct StringBuffer *s2)
601 if ( (GNUNET_YES == s1->null_flag) &&
602 (GNUNET_YES == s2->null_flag) )
604 if ( (GNUNET_YES == s1->null_flag) ||
605 (GNUNET_YES == s2->null_flag) )
607 if (s1->slen != s2->slen)
611 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
616 * Compare two strings for equality.
618 * @param s1 first string for comparison.
619 * @param s2 second string for comparison.
621 * @return 0 if the strings are the same, 1 or -1 if not.
624 sb_strcmp (const struct StringBuffer *s1,
625 const struct StringBuffer *s2)
627 if (s1->slen != s2->slen)
631 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
636 * Reallocate the buffer of 'ret' to fit 'nlen' characters;
637 * move the existing string to the beginning of the new buffer.
639 * @param ret current buffer, to be updated
640 * @param nlen target length for the buffer, must be at least ret->slen
643 sb_realloc (struct StringBuffer *ret,
648 GNUNET_assert (nlen >= ret->slen);
650 ret->abuf = GNUNET_malloc (nlen);
652 GNUNET_memcpy (ret->abuf,
655 ret->sbuf = ret->abuf;
656 GNUNET_free_non_null (old);
663 * @param ret where to write the result
664 * @param sarg string to append
667 sb_append (struct StringBuffer *ret,
668 const struct StringBuffer *sarg)
670 if (GNUNET_YES == ret->null_flag)
672 ret->null_flag = GNUNET_NO;
673 if (ret->blen < sarg->slen + ret->slen)
674 sb_realloc (ret, ret->blen + sarg->slen + 128);
675 GNUNET_memcpy (&ret->sbuf[ret->slen],
678 ret->slen += sarg->slen;
685 * @param ret where to write the result
686 * @param cstr string to append
689 sb_append_cstr (struct StringBuffer *ret,
692 size_t cstr_len = strlen (cstr);
694 if (GNUNET_YES == ret->null_flag)
696 ret->null_flag = GNUNET_NO;
697 if (ret->blen < cstr_len + ret->slen)
698 sb_realloc (ret, ret->blen + cstr_len + 128);
699 GNUNET_memcpy (&ret->sbuf[ret->slen],
702 ret->slen += cstr_len;
707 * Wrap a string buffer, that is, set ret to the format string
708 * which contains an "%s" which is to be replaced with the original
709 * content of 'ret'. Note that optimizing this function is not
710 * really worth it, it is rarely called.
712 * @param ret where to write the result and take the input for %.*s from
713 * @param format format string, fprintf-style, with exactly one "%.*s"
714 * @param extra_chars how long will the result be, in addition to 'sarg' length
717 sb_wrap (struct StringBuffer *ret,
723 if (GNUNET_YES == ret->null_flag)
725 ret->null_flag = GNUNET_NO;
726 temp = GNUNET_malloc (ret->slen + extra_chars + 1);
727 GNUNET_snprintf (temp,
728 ret->slen + extra_chars + 1,
732 GNUNET_free_non_null (ret->abuf);
735 ret->blen = ret->slen + extra_chars + 1;
736 ret->slen = ret->slen + extra_chars;
741 * Format a string buffer. Note that optimizing this function is not
742 * really worth it, it is rarely called.
744 * @param ret where to write the result
745 * @param format format string, fprintf-style, with exactly one "%.*s"
746 * @param extra_chars how long will the result be, in addition to 'sarg' length
747 * @param sarg string to print into the format
750 sb_printf1 (struct StringBuffer *ret,
753 const struct StringBuffer *sarg)
755 if (ret->blen < sarg->slen + extra_chars + 1)
757 sarg->slen + extra_chars + 1);
758 ret->null_flag = GNUNET_NO;
759 ret->sbuf = ret->abuf;
760 ret->slen = sarg->slen + extra_chars;
761 GNUNET_snprintf (ret->sbuf,
770 * Format a string buffer.
772 * @param ret where to write the result
773 * @param format format string, fprintf-style, with exactly two "%.*s"
774 * @param extra_chars how long will the result be, in addition to 'sarg1/2' length
775 * @param sarg1 first string to print into the format
776 * @param sarg2 second string to print into the format
779 sb_printf2 (struct StringBuffer *ret,
782 const struct StringBuffer *sarg1,
783 const struct StringBuffer *sarg2)
785 if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1)
787 sarg1->slen + sarg2->slen + extra_chars + 1);
788 ret->null_flag = GNUNET_NO;
789 ret->slen = sarg1->slen + sarg2->slen + extra_chars;
790 ret->sbuf = ret->abuf;
791 GNUNET_snprintf (ret->sbuf,
802 * Format a string buffer. Note that optimizing this function is not
803 * really worth it, it is rarely called.
805 * @param ret where to write the result
806 * @param format format string, fprintf-style, with exactly three "%.*s"
807 * @param extra_chars how long will the result be, in addition to 'sarg1/2/3' length
808 * @param sarg1 first string to print into the format
809 * @param sarg2 second string to print into the format
810 * @param sarg3 third string to print into the format
813 sb_printf3 (struct StringBuffer *ret,
816 const struct StringBuffer *sarg1,
817 const struct StringBuffer *sarg2,
818 const struct StringBuffer *sarg3)
820 if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1)
822 sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1);
823 ret->null_flag = GNUNET_NO;
824 ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars;
825 ret->sbuf = ret->abuf;
826 GNUNET_snprintf (ret->sbuf,
839 * Free resources of the given string buffer.
841 * @param sb buffer to free (actual pointer is not freed, as they
842 * should not be individually allocated)
845 sb_free (struct StringBuffer *sb)
847 GNUNET_array_grow (sb->abuf,
852 sb->null_flag= GNUNET_YES;
857 * Copy the given string buffer from 'in' to 'out'.
859 * @param in input string
860 * @param out output string
863 sb_strdup (struct StringBuffer *out,
864 const struct StringBuffer *in)
867 out->null_flag = in->null_flag;
868 if (GNUNET_YES == out->null_flag)
870 if (out->blen < in->slen)
872 GNUNET_array_grow (out->abuf,
876 out->sbuf = out->abuf;
877 out->slen = in->slen;
878 GNUNET_memcpy (out->sbuf, in->sbuf, out->slen);
883 * Copy the given string buffer from 'in' to 'out'.
885 * @param cstr input string
886 * @param out output string
889 sb_strdup_cstr (struct StringBuffer *out,
894 out->null_flag = GNUNET_YES;
897 out->null_flag = GNUNET_NO;
898 out->slen = strlen (cstr);
899 if (out->blen < out->slen)
901 GNUNET_array_grow (out->abuf,
905 out->sbuf = out->abuf;
906 GNUNET_memcpy (out->sbuf, cstr, out->slen);
911 * Check if the given string @a str needs parentheses around it when
912 * using it to generate a regex.
916 * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise
919 needs_parentheses (const struct StringBuffer *str)
928 if ((GNUNET_YES == str->null_flag) || ((slen = str->slen) < 2))
933 end = str->sbuf + slen;
938 cl = memchr (pos, ')', end - pos);
944 /* while '(' before ')', count opening parens */
945 while ( (NULL != (op = memchr (pos, '(', end - pos))) &&
955 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
960 * Remove parentheses surrounding string @a str.
961 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
962 * You need to #GNUNET_free() the returned string.
964 * @param str string, modified to contain a
965 * @return string without surrounding parentheses, string 'str' if no preceding
966 * epsilon could be found, NULL if 'str' was NULL
969 remove_parentheses (struct StringBuffer *str)
982 if ( (GNUNET_YES == str->null_flag) ||
983 (1 >= (slen = str->slen)) ||
984 ('(' != str->sbuf[0]) ||
985 (')' != str->sbuf[slen - 1]) )
989 end = &sbuf[slen - 1];
990 op = memchr (pos, '(', end - pos);
991 cp = memchr (pos, ')', end - pos);
994 while ( (NULL != op) &&
999 op = memchr (pos, '(', end - pos);
1001 while ( (NULL != cp) &&
1006 return; /* can't strip parens */
1009 cp = memchr (pos, ')', end - pos);
1023 * Check if the string 'str' starts with an epsilon (empty string).
1024 * Example: "(|a)" is starting with an epsilon.
1026 * @param str string to test
1028 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
1031 has_epsilon (const struct StringBuffer *str)
1034 (GNUNET_YES != str->null_flag) &&
1036 ('(' == str->sbuf[0]) &&
1037 ('|' == str->sbuf[1]) &&
1038 (')' == str->sbuf[str->slen - 1]);
1043 * Remove an epsilon from the string str. Where epsilon is an empty string
1044 * Example: str = "(|a|b|c)", result: "a|b|c"
1045 * The returned string needs to be freed.
1047 * @param str original string
1048 * @param ret where to return string without preceding epsilon, string 'str' if no preceding
1049 * epsilon could be found, NULL if 'str' was NULL
1052 remove_epsilon (const struct StringBuffer *str,
1053 struct StringBuffer *ret)
1055 if (GNUNET_YES == str->null_flag)
1057 ret->null_flag = GNUNET_YES;
1060 if ( (str->slen > 1) &&
1061 ('(' == str->sbuf[0]) &&
1062 ('|' == str->sbuf[1]) &&
1063 (')' == str->sbuf[str->slen - 1]) )
1065 /* remove epsilon */
1066 if (ret->blen < str->slen - 3)
1068 GNUNET_array_grow (ret->abuf,
1072 ret->sbuf = ret->abuf;
1073 ret->slen = str->slen - 3;
1074 GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen);
1077 sb_strdup (ret, str);
1082 * Compare n bytes of 'str1' and 'str2'
1084 * @param str1 first string to compare
1085 * @param str2 second string for comparison
1086 * @param n number of bytes to compare
1088 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1091 sb_strncmp (const struct StringBuffer *str1,
1092 const struct StringBuffer *str2, size_t n)
1096 if ( (str1->slen != str2->slen) &&
1097 ( (str1->slen < n) ||
1098 (str2->slen < n) ) )
1100 max = GNUNET_MAX (str1->slen, str2->slen);
1103 return memcmp (str1->sbuf, str2->sbuf, max);
1108 * Compare n bytes of 'str1' and 'str2'
1110 * @param str1 first string to compare
1111 * @param str2 second C string for comparison
1112 * @param n number of bytes to compare (and length of str2)
1114 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1117 sb_strncmp_cstr (const struct StringBuffer *str1,
1118 const char *str2, size_t n)
1122 return memcmp (str1->sbuf, str2, n);
1127 * Initialize string buffer for storing strings of up to n
1130 * @param sb buffer to initialize
1131 * @param n desired target length
1134 sb_init (struct StringBuffer *sb,
1137 sb->null_flag = GNUNET_NO;
1138 sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n);
1145 * Compare 'str1', starting from position 'k', with whole 'str2'
1147 * @param str1 first string to compare, starting from position 'k'
1148 * @param str2 second string for comparison
1149 * @param k starting position in 'str1'
1151 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1154 sb_strkcmp (const struct StringBuffer *str1,
1155 const struct StringBuffer *str2, size_t k)
1157 if ( (GNUNET_YES == str1->null_flag) ||
1158 (GNUNET_YES == str2->null_flag) ||
1160 (str1->slen - k != str2->slen) )
1162 return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen);
1167 * Helper function used as 'action' in 'REGEX_INTERNAL_automaton_traverse'
1168 * function to create the depth-first numbering of the states.
1170 * @param cls states array.
1171 * @param count current state counter.
1172 * @param s current state.
1175 number_states (void *cls, const unsigned int count,
1176 struct REGEX_INTERNAL_State *s)
1178 struct REGEX_INTERNAL_State **states = cls;
1188 ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \
1189 ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf)
1193 * Construct the regular expression given the inductive step,
1194 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
1195 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
1197 * @param R_last_ij value of $R^{(k-1)_{ij}.
1198 * @param R_last_ik value of $R^{(k-1)_{ik}.
1199 * @param R_last_kk value of $R^{(k-1)_{kk}.
1200 * @param R_last_kj value of $R^{(k-1)_{kj}.
1201 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
1202 * is expected to be NULL when called!
1203 * @param R_cur_l optimization -- kept between iterations to avoid realloc
1204 * @param R_cur_r optimization -- kept between iterations to avoid realloc
1207 automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1208 const struct StringBuffer *R_last_ik,
1209 const struct StringBuffer *R_last_kk,
1210 const struct StringBuffer *R_last_kj,
1211 struct StringBuffer *R_cur_ij,
1212 struct StringBuffer *R_cur_l,
1213 struct StringBuffer *R_cur_r)
1215 struct StringBuffer R_temp_ij;
1216 struct StringBuffer R_temp_ik;
1217 struct StringBuffer R_temp_kj;
1218 struct StringBuffer R_temp_kk;
1224 int clean_ik_kk_cmp;
1225 int clean_kk_kj_cmp;
1231 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1232 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1233 * R_cur_ij = R_cur_l | R_cur_r
1234 * R_cur_l == R^{(k-1)}_{ij}
1235 * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1238 if ( (GNUNET_YES == R_last_ij->null_flag) &&
1239 ( (GNUNET_YES == R_last_ik->null_flag) ||
1240 (GNUNET_YES == R_last_kj->null_flag)))
1242 /* R^{(k)}_{ij} = N | N */
1243 R_cur_ij->null_flag = GNUNET_YES;
1244 R_cur_ij->synced = GNUNET_NO;
1248 if ( (GNUNET_YES == R_last_ik->null_flag) ||
1249 (GNUNET_YES == R_last_kj->null_flag) )
1251 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1252 if (GNUNET_YES == R_last_ij->synced)
1254 R_cur_ij->synced = GNUNET_YES;
1255 R_cur_ij->null_flag = GNUNET_NO;
1258 R_cur_ij->synced = GNUNET_YES;
1259 sb_strdup (R_cur_ij, R_last_ij);
1262 R_cur_ij->synced = GNUNET_NO;
1264 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
1265 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
1267 R_cur_r->null_flag = GNUNET_YES;
1269 R_cur_l->null_flag = GNUNET_YES;
1272 /* cache results from strcmp, we might need these many times */
1273 ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj);
1274 ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik);
1275 ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk);
1276 kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj);
1278 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1279 * as parentheses, so we can better compare the contents */
1281 memset (&R_temp_ij, 0, sizeof (struct StringBuffer));
1282 memset (&R_temp_ik, 0, sizeof (struct StringBuffer));
1283 memset (&R_temp_kk, 0, sizeof (struct StringBuffer));
1284 memset (&R_temp_kj, 0, sizeof (struct StringBuffer));
1285 remove_epsilon (R_last_ik, &R_temp_ik);
1286 remove_epsilon (R_last_kk, &R_temp_kk);
1287 remove_epsilon (R_last_kj, &R_temp_kj);
1288 remove_parentheses (&R_temp_ik);
1289 remove_parentheses (&R_temp_kk);
1290 remove_parentheses (&R_temp_kj);
1291 clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk);
1292 clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj);
1294 /* construct R_cur_l (and, if necessary R_cur_r) */
1295 if (GNUNET_YES != R_last_ij->null_flag)
1297 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1298 * as parentheses, so we can better compare the contents */
1299 remove_epsilon (R_last_ij, &R_temp_ij);
1300 remove_parentheses (&R_temp_ij);
1302 if ( (0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) &&
1303 (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) &&
1304 (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) )
1306 if (0 == R_temp_ij.slen)
1308 R_cur_r->null_flag = GNUNET_NO;
1310 else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) ||
1311 (0 == sb_strncmp_cstr (R_last_ik, "(|", 2) &&
1312 0 == sb_strncmp_cstr (R_last_kj, "(|", 2)))
1315 * a|(e|a)a*(e|a) = a*
1316 * a|(e|a)(e|a)*(e|a) = a*
1318 * (e|a)|aa*(e|a) = a*
1319 * (e|a)|(e|a)a*a = a*
1320 * (e|a)|(e|a)a*(e|a) = a*
1321 * (e|a)|(e|a)(e|a)*(e|a) = a*
1323 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1324 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1326 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij);
1334 * a|(e|a)(e|a)*a = a+
1335 * a|a(e|a)*(e|a) = a+
1337 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1338 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1340 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij);
1343 else if ( (0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && (0 != clean_ik_kk_cmp) )
1346 if (0 == R_last_kk->slen)
1347 sb_strdup (R_cur_r, R_last_ij);
1348 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1349 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1351 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk);
1352 R_cur_l->null_flag = GNUNET_YES;
1354 else if ( (0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && (0 != clean_kk_kj_cmp))
1357 if (R_last_kk->slen < 1)
1359 sb_strdup (R_cur_r, R_last_kj);
1361 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1362 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1364 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1366 R_cur_l->null_flag = GNUNET_YES;
1368 else if ( (0 == ij_ik_cmp) && (0 == kk_kj_cmp) && (! has_epsilon (R_last_ij)) &&
1369 has_epsilon (R_last_kk))
1371 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
1372 if (needs_parentheses (&R_temp_kk))
1373 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1375 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk);
1376 R_cur_l->null_flag = GNUNET_YES;
1378 else if ( (0 == ij_kj_cmp) && (0 == ik_kk_cmp) && (! has_epsilon (R_last_ij)) &&
1379 has_epsilon (R_last_kk))
1381 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
1382 if (needs_parentheses (&R_temp_kk))
1383 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1385 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1386 R_cur_l->null_flag = GNUNET_YES;
1390 sb_strdup (R_cur_l, R_last_ij);
1391 remove_parentheses (R_cur_l);
1396 /* we have no left side */
1397 R_cur_l->null_flag = GNUNET_YES;
1400 /* construct R_cur_r, if not already constructed */
1401 if (GNUNET_YES == R_cur_r->null_flag)
1403 length = R_temp_kk.slen - R_last_ik->slen;
1405 /* a(ba)*bx = (ab)+x */
1406 if ( (length > 0) &&
1407 (GNUNET_YES != R_last_kk->null_flag) &&
1408 (0 < R_last_kk->slen) &&
1409 (GNUNET_YES != R_last_kj->null_flag) &&
1410 (0 < R_last_kj->slen) &&
1411 (GNUNET_YES != R_last_ik->null_flag) &&
1412 (0 < R_last_ik->slen) &&
1413 (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) &&
1414 (0 == sb_strncmp (&R_temp_kk, R_last_kj, length)) )
1416 struct StringBuffer temp_a;
1417 struct StringBuffer temp_b;
1419 sb_init (&temp_a, length);
1420 sb_init (&temp_b, R_last_kj->slen - length);
1423 temp_a.sbuf = temp_a.abuf;
1424 GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l);
1425 temp_a.slen = length_l;
1427 length_r = R_last_kj->slen - length;
1428 temp_b.sbuf = temp_b.abuf;
1429 GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1430 temp_b.slen = length_r;
1432 /* e|(ab)+ = (ab)* */
1433 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1434 (0 == R_cur_l->slen) &&
1435 (0 == temp_b.slen) )
1437 sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1439 R_cur_l->null_flag = GNUNET_YES;
1443 sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1448 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk) &&
1449 0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1453 * (e|a)(e|a)*(e|a) = a*
1455 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1457 if (needs_parentheses (&R_temp_kk))
1458 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk);
1460 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk);
1463 else if ( (0 == clean_ik_kk_cmp) &&
1464 (0 == clean_kk_kj_cmp) &&
1465 (! has_epsilon (R_last_ik)) )
1467 if (needs_parentheses (&R_temp_kk))
1468 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk);
1470 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk);
1481 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1482 has_epsilon (R_last_kj));
1486 if (needs_parentheses (&R_temp_kk))
1487 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk);
1489 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk);
1495 * (e|a)(e|a)*b = a*b
1497 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk))
1499 if (has_epsilon (R_last_ik))
1501 if (needs_parentheses (&R_temp_kk))
1502 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1504 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1508 if (needs_parentheses (&R_temp_kk))
1509 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1511 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1516 * b(e|a)*(e|a) = ba*
1518 else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1520 if (has_epsilon (R_last_kj))
1522 if (needs_parentheses (&R_temp_kk))
1523 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1525 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1529 if (needs_parentheses (&R_temp_kk))
1530 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1532 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1537 if (0 < R_temp_kk.slen)
1539 if (needs_parentheses (&R_temp_kk))
1541 sb_printf3 (R_cur_r, "%.*s(%.*s)*%.*s", 3, R_last_ik, &R_temp_kk,
1546 sb_printf3 (R_cur_r, "%.*s%.*s*%.*s", 1, R_last_ik, &R_temp_kk,
1552 sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1556 sb_free (&R_temp_ij);
1557 sb_free (&R_temp_ik);
1558 sb_free (&R_temp_kk);
1559 sb_free (&R_temp_kj);
1561 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1562 (GNUNET_YES == R_cur_r->null_flag) )
1564 R_cur_ij->null_flag = GNUNET_YES;
1568 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1569 (GNUNET_YES == R_cur_r->null_flag) )
1571 struct StringBuffer tmp;
1574 *R_cur_ij = *R_cur_l;
1579 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1580 (GNUNET_YES != R_cur_r->null_flag) )
1582 struct StringBuffer tmp;
1585 *R_cur_ij = *R_cur_r;
1590 if (0 == sb_nullstrcmp (R_cur_l, R_cur_r))
1592 struct StringBuffer tmp;
1595 *R_cur_ij = *R_cur_l;
1599 sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r);
1604 * Create proofs for all states in the given automaton. Implementation of the
1605 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1606 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1608 * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1609 * proof) fields. The starting state will only have a valid proof/hash if it has
1610 * any incoming transitions.
1612 * @param a automaton for which to assign proofs and hashes, must not be NULL
1615 automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1617 unsigned int n = a->state_count;
1618 struct REGEX_INTERNAL_State *states[n];
1619 struct StringBuffer *R_last;
1620 struct StringBuffer *R_cur;
1621 struct StringBuffer R_cur_r;
1622 struct StringBuffer R_cur_l;
1623 struct StringBuffer *R_swap;
1624 struct REGEX_INTERNAL_Transition *t;
1625 struct StringBuffer complete_regex;
1630 R_last = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1631 R_cur = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1632 if ( (NULL == R_last) ||
1635 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
1636 GNUNET_free_non_null (R_cur);
1637 GNUNET_free_non_null (R_last);
1638 return GNUNET_SYSERR;
1641 /* create depth-first numbering of the states, initializes 'state' */
1642 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1645 for (i = 0; i < n; i++)
1646 GNUNET_assert (NULL != states[i]);
1647 for (i = 0; i < n; i++)
1648 for (j = 0; j < n; j++)
1649 R_last[i *n + j].null_flag = GNUNET_YES;
1651 /* Compute regular expressions of length "1" between each pair of states */
1652 for (i = 0; i < n; i++)
1654 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1656 j = t->to_state->dfs_id;
1657 if (GNUNET_YES == R_last[i * n + j].null_flag)
1659 sb_strdup_cstr (&R_last[i * n + j], t->label);
1663 sb_append_cstr (&R_last[i * n + j], "|");
1664 sb_append_cstr (&R_last[i * n + j], t->label);
1667 /* add self-loop: i is reachable from i via epsilon-transition */
1668 if (GNUNET_YES == R_last[i * n + i].null_flag)
1670 R_last[i * n + i].slen = 0;
1671 R_last[i * n + i].null_flag = GNUNET_NO;
1675 sb_wrap (&R_last[i * n + i], "(|%.*s)", 3);
1678 for (i = 0; i < n; i++)
1679 for (j = 0; j < n; j++)
1680 if (needs_parentheses (&R_last[i * n + j]))
1681 sb_wrap (&R_last[i * n + j], "(%.*s)", 2);
1682 /* Compute regular expressions of length "k" between each pair of states per
1684 memset (&R_cur_l, 0, sizeof (struct StringBuffer));
1685 memset (&R_cur_r, 0, sizeof (struct StringBuffer));
1686 for (k = 0; k < n; k++)
1688 for (i = 0; i < n; i++)
1690 for (j = 0; j < n; j++)
1692 /* Basis for the recursion:
1693 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1694 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1697 /* Create R_cur[i][j] and simplify the expression */
1698 automaton_create_proofs_simplify (&R_last[i * n + j], &R_last[i * n + k],
1699 &R_last[k * n + k], &R_last[k * n + j],
1701 &R_cur_l, &R_cur_r);
1704 /* set R_last = R_cur */
1708 /* clear 'R_cur' for next iteration */
1709 for (i = 0; i < n; i++)
1710 for (j = 0; j < n; j++)
1711 R_cur[i * n + j].null_flag = GNUNET_YES;
1715 /* assign proofs and hashes */
1716 for (i = 0; i < n; i++)
1718 if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1720 states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf,
1721 R_last[a->start->dfs_id * n + i].slen);
1722 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1727 /* complete regex for whole DFA: union of all pairs (start state/accepting
1729 sb_init (&complete_regex, 16 * n);
1730 for (i = 0; i < n; i++)
1732 if (states[i]->accepting)
1734 if ( (0 == complete_regex.slen) &&
1735 (0 < R_last[a->start->dfs_id * n + i].slen) )
1737 sb_append (&complete_regex,
1738 &R_last[a->start->dfs_id * n + i]);
1740 else if ( (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) &&
1741 (0 < R_last[a->start->dfs_id * n + i].slen) )
1743 sb_append_cstr (&complete_regex, "|");
1744 sb_append (&complete_regex,
1745 &R_last[a->start->dfs_id * n + i]);
1749 a->canonical_regex = GNUNET_strndup (complete_regex.sbuf, complete_regex.slen);
1752 sb_free (&complete_regex);
1753 for (i = 0; i < n; i++)
1754 for (j = 0; j < n; j++)
1756 sb_free (&R_cur[i * n + j]);
1757 sb_free (&R_last[i * n + j]);
1759 GNUNET_free (R_cur);
1760 GNUNET_free (R_last);
1766 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1767 * automaton_destroy_state.
1769 * @param ctx context
1770 * @param nfa_states set of NFA states on which the DFA should be based on
1772 * @return new DFA state
1774 static struct REGEX_INTERNAL_State *
1775 dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1776 struct REGEX_INTERNAL_StateSet *nfa_states)
1778 struct REGEX_INTERNAL_State *s;
1781 struct REGEX_INTERNAL_State *cstate;
1782 struct REGEX_INTERNAL_Transition *ctran;
1785 s = GNUNET_new (struct REGEX_INTERNAL_State);
1786 s->id = ctx->state_id++;
1790 if (NULL == nfa_states)
1792 GNUNET_asprintf (&s->name, "s%i", s->id);
1796 s->nfa_set = *nfa_states;
1798 if (nfa_states->off < 1)
1801 /* Create a name based on 'nfa_states' */
1802 len = nfa_states->off * 14 + 4;
1803 s->name = GNUNET_malloc (len);
1804 strcat (s->name, "{");
1807 for (i = 0; i < nfa_states->off; i++)
1809 cstate = nfa_states->states[i];
1810 GNUNET_snprintf (pos,
1811 pos - s->name + len,
1814 pos += strlen (pos);
1816 /* Add a transition for each distinct label to NULL state */
1817 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1818 if (NULL != ctran->label)
1819 state_add_transition (ctx, s, ctran->label, NULL);
1821 /* If the nfa_states contain an accepting state, the new dfa state is also
1823 if (cstate->accepting)
1827 s->name = GNUNET_realloc (s->name, strlen (s->name) + 1);
1829 memset (nfa_states, 0, sizeof (struct REGEX_INTERNAL_StateSet));
1835 * Move from the given state 's' to the next state on transition 'str'. Consumes
1836 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1837 * 's' will point to the next state, and the length of the substring used for
1838 * this transition will be returned. If no transition possible 0 is returned and
1839 * 's' points to NULL.
1841 * @param s starting state, will point to the next state or NULL (if no
1842 * transition possible)
1843 * @param str edge label to follow (will match longest common prefix)
1845 * @return length of the substring comsumed from 'str'
1848 dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1850 struct REGEX_INTERNAL_Transition *t;
1851 struct REGEX_INTERNAL_State *new_s;
1853 unsigned int max_len;
1860 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1862 len = strlen (t->label);
1864 if (0 == strncmp (t->label, str, len))
1869 new_s = t->to_state;
1880 * Set the given state 'marked' to #GNUNET_YES. Used by the
1881 * #dfa_remove_unreachable_states() function to detect unreachable states in the
1884 * @param cls closure, not used.
1885 * @param count count, not used.
1886 * @param s state where the marked attribute will be set to #GNUNET_YES.
1889 mark_states (void *cls,
1890 const unsigned int count,
1891 struct REGEX_INTERNAL_State *s)
1893 s->marked = GNUNET_YES;
1898 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1899 * states that are not reachable from the starting state.
1901 * @param a DFA automaton
1904 dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1906 struct REGEX_INTERNAL_State *s;
1907 struct REGEX_INTERNAL_State *s_next;
1909 /* 1. unmark all states */
1910 for (s = a->states_head; NULL != s; s = s->next)
1911 s->marked = GNUNET_NO;
1913 /* 2. traverse dfa from start state and mark all visited states */
1914 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1916 /* 3. delete all states that were not visited */
1917 for (s = a->states_head; NULL != s; s = s_next)
1920 if (GNUNET_NO == s->marked)
1921 automaton_remove_state (a, s);
1927 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1928 * not transition to any other state but themselves.
1930 * @param a DFA automaton
1933 dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a)
1935 struct REGEX_INTERNAL_State *s;
1936 struct REGEX_INTERNAL_State *s_next;
1937 struct REGEX_INTERNAL_Transition *t;
1940 GNUNET_assert (DFA == a->type);
1942 for (s = a->states_head; NULL != s; s = s_next)
1950 for (t = s->transitions_head; NULL != t; t = t->next)
1952 if (NULL != t->to_state && t->to_state != s)
1962 /* state s is dead, remove it */
1963 automaton_remove_state (a, s);
1969 * Merge all non distinguishable states in the DFA 'a'
1971 * @param ctx context
1972 * @param a DFA automaton
1973 * @return #GNUNET_OK on success
1976 dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1977 struct REGEX_INTERNAL_Automaton *a)
1980 struct REGEX_INTERNAL_State *s1;
1981 struct REGEX_INTERNAL_State *s2;
1982 struct REGEX_INTERNAL_Transition *t1;
1983 struct REGEX_INTERNAL_Transition *t2;
1984 struct REGEX_INTERNAL_State *s1_next;
1985 struct REGEX_INTERNAL_State *s2_next;
1987 unsigned int num_equal_edges;
1989 unsigned int state_cnt;
1990 unsigned long long idx;
1991 unsigned long long idx1;
1993 if ( (NULL == a) || (0 == a->state_count) )
1995 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1996 "Could not merge nondistinguishable states, automaton was NULL.\n");
1997 return GNUNET_SYSERR;
2000 state_cnt = a->state_count;
2001 table = GNUNET_malloc_large ((sizeof (uint32_t) * state_cnt * state_cnt / 32) + sizeof (uint32_t));
2004 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
2005 return GNUNET_SYSERR;
2008 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
2011 /* Mark all pairs of accepting/!accepting states */
2012 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2013 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
2014 if ( (s1->accepting && !s2->accepting) ||
2015 (!s1->accepting && s2->accepting) )
2017 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2018 table[idx / 32] |= (1U << (idx % 32));
2021 /* Find all equal states */
2026 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2028 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
2030 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2031 if (0 != (table[idx / 32] & (1U << (idx % 32))))
2033 num_equal_edges = 0;
2034 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2036 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
2038 if (0 == strcmp (t1->label, t2->label))
2041 /* same edge, but targets definitively different, so we're different
2043 if (t1->to_state->marked > t2->to_state->marked)
2044 idx1 = (unsigned long long) t1->to_state->marked * state_cnt + t2->to_state->marked;
2046 idx1 = (unsigned long long) t2->to_state->marked * state_cnt + t1->to_state->marked;
2047 if (0 != (table[idx1 / 32] & (1U << (idx1 % 32))))
2049 table[idx / 32] |= (1U << (idx % 32));
2050 change = 1; /* changed a marker, need to run again */
2055 if ( (num_equal_edges != s1->transition_count) ||
2056 (num_equal_edges != s2->transition_count) )
2058 /* Make sure ALL edges of possible equal states are the same */
2059 table[idx / 32] |= (1U << (idx % 32));
2060 change = 1; /* changed a marker, need to run again */
2066 /* Merge states that are equal */
2067 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
2070 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2073 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2074 if (0 == (table[idx / 32] & (1U << (idx % 32))))
2075 automaton_merge_states (ctx, a, s1, s2);
2079 GNUNET_free (table);
2085 * Minimize the given DFA 'a' by removing all unreachable states, removing all
2086 * dead states and merging all non distinguishable states
2088 * @param ctx context
2089 * @param a DFA automaton
2090 * @return GNUNET_OK on success
2093 dfa_minimize (struct REGEX_INTERNAL_Context *ctx,
2094 struct REGEX_INTERNAL_Automaton *a)
2097 return GNUNET_SYSERR;
2099 GNUNET_assert (DFA == a->type);
2101 /* 1. remove unreachable states */
2102 dfa_remove_unreachable_states (a);
2104 /* 2. remove dead states */
2105 dfa_remove_dead_states (a);
2107 /* 3. Merge nondistinguishable states */
2108 if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a))
2109 return GNUNET_SYSERR;
2115 * Context for adding strided transitions to a DFA.
2117 struct REGEX_INTERNAL_Strided_Context
2120 * Length of the strides.
2122 const unsigned int stride;
2125 * Strided transitions DLL. New strided transitions will be stored in this DLL
2126 * and afterwards added to the DFA.
2128 struct REGEX_INTERNAL_Transition *transitions_head;
2131 * Strided transitions DLL.
2133 struct REGEX_INTERNAL_Transition *transitions_tail;
2138 * Recursive helper function to add strides to a DFA.
2140 * @param cls context, contains stride length and strided transitions DLL.
2141 * @param depth current depth of the depth-first traversal of the graph.
2142 * @param label current label, string that contains all labels on the path from
2144 * @param start start state for the depth-first traversal of the graph.
2145 * @param s current state in the depth-first traversal
2148 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
2149 struct REGEX_INTERNAL_State *start,
2150 struct REGEX_INTERNAL_State *s)
2152 struct REGEX_INTERNAL_Strided_Context *ctx = cls;
2153 struct REGEX_INTERNAL_Transition *t;
2156 if (depth == ctx->stride)
2158 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2159 t->label = GNUNET_strdup (label);
2161 t->from_state = start;
2162 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
2167 for (t = s->transitions_head; NULL != t; t = t->next)
2169 /* Do not consider self-loops, because it end's up in too many
2171 if (t->to_state == t->from_state)
2176 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2179 new_label = GNUNET_strdup (t->label);
2181 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
2185 GNUNET_free_non_null (label);
2190 * Function called for each state in the DFA. Starts a traversal of depth set in
2191 * context starting from state 's'.
2193 * @param cls context.
2194 * @param count not used.
2195 * @param s current state.
2198 dfa_add_multi_strides (void *cls, const unsigned int count,
2199 struct REGEX_INTERNAL_State *s)
2201 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
2206 * Adds multi-strided transitions to the given 'dfa'.
2208 * @param regex_ctx regex context needed to add transitions to the automaton.
2209 * @param dfa DFA to which the multi strided transitions should be added.
2210 * @param stride_len length of the strides.
2213 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2214 struct REGEX_INTERNAL_Automaton *dfa,
2215 const unsigned int stride_len)
2217 struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2218 struct REGEX_INTERNAL_Transition *t;
2219 struct REGEX_INTERNAL_Transition *t_next;
2221 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
2224 /* Compute the new transitions of given stride_len */
2225 REGEX_INTERNAL_automaton_traverse (dfa, dfa->start, NULL, NULL,
2226 &dfa_add_multi_strides, &ctx);
2228 /* Add all the new transitions to the automaton. */
2229 for (t = ctx.transitions_head; NULL != t; t = t_next)
2232 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2233 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
2234 GNUNET_free_non_null (t->label);
2238 /* Mark this automaton as multistrided */
2239 dfa->is_multistrided = GNUNET_YES;
2243 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
2244 * and adds new transitions to the given transitions DLL and marks states that
2245 * should be removed by setting state->contained to GNUNET_YES.
2247 * @param dfa DFA for which the paths should be compressed.
2248 * @param start starting state for linear path search.
2249 * @param cur current state in the recursive DFS.
2250 * @param label current label (string of traversed labels).
2251 * @param max_len maximal path compression length.
2252 * @param transitions_head transitions DLL.
2253 * @param transitions_tail transitions DLL.
2256 dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2257 struct REGEX_INTERNAL_State *start,
2258 struct REGEX_INTERNAL_State *cur, char *label,
2259 unsigned int max_len,
2260 struct REGEX_INTERNAL_Transition **transitions_head,
2261 struct REGEX_INTERNAL_Transition **transitions_tail)
2263 struct REGEX_INTERNAL_Transition *t;
2267 if (NULL != label &&
2268 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
2269 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
2270 max_len == strlen (label)) ||
2271 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
2273 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2274 t->label = GNUNET_strdup (label);
2276 t->from_state = start;
2277 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
2279 if (GNUNET_NO == cur->marked)
2281 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
2286 else if (cur != start)
2287 cur->contained = GNUNET_YES;
2289 if (GNUNET_YES == cur->marked && cur != start)
2292 cur->marked = GNUNET_YES;
2295 for (t = cur->transitions_head; NULL != t; t = t->next)
2298 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2300 new_label = GNUNET_strdup (t->label);
2302 if (t->to_state != cur)
2304 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
2305 transitions_head, transitions_tail);
2307 GNUNET_free (new_label);
2313 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
2314 * compressed to 0->3 by combining transitions.
2316 * @param regex_ctx context for adding new transitions.
2317 * @param dfa DFA representation, will directly modify the given DFA.
2318 * @param max_len maximal length of the compressed paths.
2321 dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2322 struct REGEX_INTERNAL_Automaton *dfa, unsigned int max_len)
2324 struct REGEX_INTERNAL_State *s;
2325 struct REGEX_INTERNAL_State *s_next;
2326 struct REGEX_INTERNAL_Transition *t;
2327 struct REGEX_INTERNAL_Transition *t_next;
2328 struct REGEX_INTERNAL_Transition *transitions_head = NULL;
2329 struct REGEX_INTERNAL_Transition *transitions_tail = NULL;
2334 /* Count the incoming transitions on each state. */
2335 for (s = dfa->states_head; NULL != s; s = s->next)
2337 for (t = s->transitions_head; NULL != t; t = t->next)
2339 if (NULL != t->to_state)
2340 t->to_state->incoming_transition_count++;
2344 /* Unmark all states. */
2345 for (s = dfa->states_head; NULL != s; s = s->next)
2347 s->marked = GNUNET_NO;
2348 s->contained = GNUNET_NO;
2351 /* Add strides and mark states that can be deleted. */
2352 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
2353 &transitions_head, &transitions_tail);
2355 /* Add all the new transitions to the automaton. */
2356 for (t = transitions_head; NULL != t; t = t_next)
2359 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2360 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
2361 GNUNET_free_non_null (t->label);
2365 /* Remove marked states (including their incoming and outgoing transitions). */
2366 for (s = dfa->states_head; NULL != s; s = s_next)
2369 if (GNUNET_YES == s->contained)
2370 automaton_remove_state (dfa, s);
2376 * Creates a new NFA fragment. Needs to be cleared using
2377 * automaton_fragment_clear.
2379 * @param start starting state
2380 * @param end end state
2382 * @return new NFA fragment
2384 static struct REGEX_INTERNAL_Automaton *
2385 nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2386 struct REGEX_INTERNAL_State *end)
2388 struct REGEX_INTERNAL_Automaton *n;
2390 n = GNUNET_new (struct REGEX_INTERNAL_Automaton);
2397 if (NULL == start || NULL == end)
2400 automaton_add_state (n, end);
2401 automaton_add_state (n, start);
2413 * Adds a list of states to the given automaton 'n'.
2415 * @param n automaton to which the states should be added
2416 * @param states_head head of the DLL of states
2417 * @param states_tail tail of the DLL of states
2420 nfa_add_states (struct REGEX_INTERNAL_Automaton *n,
2421 struct REGEX_INTERNAL_State *states_head,
2422 struct REGEX_INTERNAL_State *states_tail)
2424 struct REGEX_INTERNAL_State *s;
2426 if (NULL == n || NULL == states_head)
2428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
2432 if (NULL == n->states_head)
2434 n->states_head = states_head;
2435 n->states_tail = states_tail;
2439 if (NULL != states_head)
2441 n->states_tail->next = states_head;
2442 n->states_tail = states_tail;
2445 for (s = states_head; NULL != s; s = s->next)
2451 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
2453 * @param ctx context
2454 * @param accepting is it an accepting state or not
2456 * @return new NFA state
2458 static struct REGEX_INTERNAL_State *
2459 nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2461 struct REGEX_INTERNAL_State *s;
2463 s = GNUNET_new (struct REGEX_INTERNAL_State);
2464 s->id = ctx->state_id++;
2465 s->accepting = accepting;
2466 s->marked = GNUNET_NO;
2472 GNUNET_asprintf (&s->name, "s%i", s->id);
2479 * Calculates the closure set for the given set of states.
2481 * @param ret set to sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2482 * @param nfa the NFA containing 's'
2483 * @param states list of states on which to base the closure on
2484 * @param label transitioning label for which to base the closure on,
2485 * pass NULL for epsilon transition
2488 nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2489 struct REGEX_INTERNAL_Automaton *nfa,
2490 struct REGEX_INTERNAL_StateSet *states, const char *label)
2492 struct REGEX_INTERNAL_State *s;
2494 struct REGEX_INTERNAL_StateSet_MDLL cls_stack;
2495 struct REGEX_INTERNAL_State *clsstate;
2496 struct REGEX_INTERNAL_State *currentstate;
2497 struct REGEX_INTERNAL_Transition *ctran;
2499 memset (ret, 0, sizeof (struct REGEX_INTERNAL_StateSet));
2503 for (i = 0; i < states->off; i++)
2505 s = states->states[i];
2507 /* Add start state to closure only for epsilon closure */
2509 state_set_append (ret, s);
2511 /* initialize work stack */
2512 cls_stack.head = NULL;
2513 cls_stack.tail = NULL;
2514 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
2517 while (NULL != (currentstate = cls_stack.tail))
2519 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2522 for (ctran = currentstate->transitions_head; NULL != ctran;
2523 ctran = ctran->next)
2525 if (NULL == (clsstate = ctran->to_state))
2527 if (0 != clsstate->contained)
2529 if (0 != nullstrcmp (label, ctran->label))
2531 state_set_append (ret, clsstate);
2532 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2535 clsstate->contained = 1;
2539 for (i = 0; i < ret->off; i++)
2540 ret->states[i]->contained = 0;
2543 qsort (ret->states, ret->off, sizeof (struct REGEX_INTERNAL_State *),
2549 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2551 * @param ctx context
2554 nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx)
2556 struct REGEX_INTERNAL_Automaton *a;
2557 struct REGEX_INTERNAL_Automaton *b;
2558 struct REGEX_INTERNAL_Automaton *new_nfa;
2560 b = ctx->stack_tail;
2561 GNUNET_assert (NULL != b);
2562 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2563 a = ctx->stack_tail;
2564 GNUNET_assert (NULL != a);
2565 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2567 state_add_transition (ctx, a->end, NULL, b->start);
2568 a->end->accepting = 0;
2569 b->end->accepting = 1;
2571 new_nfa = nfa_fragment_create (NULL, NULL);
2572 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2573 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2574 new_nfa->start = a->start;
2575 new_nfa->end = b->end;
2576 new_nfa->state_count += a->state_count + b->state_count;
2577 automaton_fragment_clear (a);
2578 automaton_fragment_clear (b);
2580 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2585 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2587 * @param ctx context
2590 nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2592 struct REGEX_INTERNAL_Automaton *a;
2593 struct REGEX_INTERNAL_Automaton *new_nfa;
2594 struct REGEX_INTERNAL_State *start;
2595 struct REGEX_INTERNAL_State *end;
2597 a = ctx->stack_tail;
2601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2602 "nfa_add_star_op failed, because there was no element on the stack");
2606 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2608 start = nfa_state_create (ctx, 0);
2609 end = nfa_state_create (ctx, 1);
2611 state_add_transition (ctx, start, NULL, a->start);
2612 state_add_transition (ctx, start, NULL, end);
2613 state_add_transition (ctx, a->end, NULL, a->start);
2614 state_add_transition (ctx, a->end, NULL, end);
2616 a->end->accepting = 0;
2619 new_nfa = nfa_fragment_create (start, end);
2620 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2621 automaton_fragment_clear (a);
2623 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2628 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2630 * @param ctx context
2633 nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx)
2635 struct REGEX_INTERNAL_Automaton *a;
2637 a = ctx->stack_tail;
2641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2642 "nfa_add_plus_op failed, because there was no element on the stack");
2646 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2648 state_add_transition (ctx, a->end, NULL, a->start);
2650 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2655 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2657 * @param ctx context
2660 nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2662 struct REGEX_INTERNAL_Automaton *a;
2663 struct REGEX_INTERNAL_Automaton *new_nfa;
2664 struct REGEX_INTERNAL_State *start;
2665 struct REGEX_INTERNAL_State *end;
2667 a = ctx->stack_tail;
2670 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2671 "nfa_add_question_op failed, because there was no element on the stack");
2675 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2677 start = nfa_state_create (ctx, 0);
2678 end = nfa_state_create (ctx, 1);
2680 state_add_transition (ctx, start, NULL, a->start);
2681 state_add_transition (ctx, start, NULL, end);
2682 state_add_transition (ctx, a->end, NULL, end);
2684 a->end->accepting = 0;
2686 new_nfa = nfa_fragment_create (start, end);
2687 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2688 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2689 automaton_fragment_clear (a);
2694 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2695 * alternates between a and b (a|b)
2697 * @param ctx context
2700 nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2702 struct REGEX_INTERNAL_Automaton *a;
2703 struct REGEX_INTERNAL_Automaton *b;
2704 struct REGEX_INTERNAL_Automaton *new_nfa;
2705 struct REGEX_INTERNAL_State *start;
2706 struct REGEX_INTERNAL_State *end;
2708 b = ctx->stack_tail;
2709 GNUNET_assert (NULL != b);
2710 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2711 a = ctx->stack_tail;
2712 GNUNET_assert (NULL != a);
2713 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2715 start = nfa_state_create (ctx, 0);
2716 end = nfa_state_create (ctx, 1);
2717 state_add_transition (ctx, start, NULL, a->start);
2718 state_add_transition (ctx, start, NULL, b->start);
2720 state_add_transition (ctx, a->end, NULL, end);
2721 state_add_transition (ctx, b->end, NULL, end);
2723 a->end->accepting = 0;
2724 b->end->accepting = 0;
2727 new_nfa = nfa_fragment_create (start, end);
2728 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2729 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2730 automaton_fragment_clear (a);
2731 automaton_fragment_clear (b);
2733 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2738 * Adds a new nfa fragment to the stack
2740 * @param ctx context
2741 * @param label label for nfa transition
2744 nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label)
2746 struct REGEX_INTERNAL_Automaton *n;
2747 struct REGEX_INTERNAL_State *start;
2748 struct REGEX_INTERNAL_State *end;
2750 GNUNET_assert (NULL != ctx);
2752 start = nfa_state_create (ctx, 0);
2753 end = nfa_state_create (ctx, 1);
2754 state_add_transition (ctx, start, label, end);
2755 n = nfa_fragment_create (start, end);
2756 GNUNET_assert (NULL != n);
2757 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2762 * Initialize a new context
2764 * @param ctx context
2767 REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx)
2771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2775 ctx->transition_id = 0;
2776 ctx->stack_head = NULL;
2777 ctx->stack_tail = NULL;
2782 * Construct an NFA by parsing the regex string of length 'len'.
2784 * @param regex regular expression string
2785 * @param len length of the string
2787 * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton
2789 struct REGEX_INTERNAL_Automaton *
2790 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2792 struct REGEX_INTERNAL_Context ctx;
2793 struct REGEX_INTERNAL_Automaton *nfa;
2798 unsigned int altcount;
2799 unsigned int atomcount;
2808 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2811 "Could not parse regex. Empty regex string provided.\n");
2815 REGEX_INTERNAL_context_init (&ctx);
2826 for (count = 0; count < len && *regexp; count++, regexp++)
2834 nfa_add_concatenation (&ctx);
2837 GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2838 p[poff].altcount = altcount;
2839 p[poff].atomcount = atomcount;
2847 error_msg = "Cannot append '|' to nothing";
2850 while (--atomcount > 0)
2851 nfa_add_concatenation (&ctx);
2857 error_msg = "Missing opening '('";
2862 /* Ignore this: "()" */
2864 altcount = p[poff].altcount;
2865 atomcount = p[poff].atomcount;
2868 while (--atomcount > 0)
2869 nfa_add_concatenation (&ctx);
2870 for (; altcount > 0; altcount--)
2871 nfa_add_alternation (&ctx);
2873 altcount = p[poff].altcount;
2874 atomcount = p[poff].atomcount;
2880 error_msg = "Cannot append '*' to nothing";
2883 nfa_add_star_op (&ctx);
2888 error_msg = "Cannot append '+' to nothing";
2891 nfa_add_plus_op (&ctx);
2896 error_msg = "Cannot append '?' to nothing";
2899 nfa_add_question_op (&ctx);
2905 nfa_add_concatenation (&ctx);
2907 curlabel[0] = *regexp;
2908 nfa_add_label (&ctx, curlabel);
2915 error_msg = "Unbalanced parenthesis";
2918 while (--atomcount > 0)
2919 nfa_add_concatenation (&ctx);
2920 for (; altcount > 0; altcount--)
2921 nfa_add_alternation (&ctx);
2923 GNUNET_array_grow (p, psize, 0);
2925 nfa = ctx.stack_tail;
2926 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2928 if (NULL != ctx.stack_head)
2930 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2934 /* Remember the regex that was used to generate this NFA */
2935 nfa->regex = GNUNET_strdup (regex);
2937 /* create depth-first numbering of the states for pretty printing */
2938 REGEX_INTERNAL_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2940 /* No multistriding added so far */
2941 nfa->is_multistrided = GNUNET_NO;
2946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2947 if (NULL != error_msg)
2948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2950 GNUNET_free_non_null (p);
2952 while (NULL != (nfa = ctx.stack_head))
2954 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2955 REGEX_INTERNAL_automaton_destroy (nfa);
2963 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2965 * @param ctx context.
2966 * @param nfa NFA automaton.
2967 * @param dfa DFA automaton.
2968 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2972 construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
2973 struct REGEX_INTERNAL_Automaton *nfa,
2974 struct REGEX_INTERNAL_Automaton *dfa,
2975 struct REGEX_INTERNAL_State *dfa_state)
2977 struct REGEX_INTERNAL_Transition *ctran;
2978 struct REGEX_INTERNAL_State *new_dfa_state;
2979 struct REGEX_INTERNAL_State *state_contains;
2980 struct REGEX_INTERNAL_State *state_iter;
2981 struct REGEX_INTERNAL_StateSet tmp;
2982 struct REGEX_INTERNAL_StateSet nfa_set;
2984 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2986 if (NULL == ctran->label || NULL != ctran->to_state)
2989 nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label);
2990 nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL);
2991 state_set_clear (&tmp);
2993 state_contains = NULL;
2994 for (state_iter = dfa->states_head; NULL != state_iter;
2995 state_iter = state_iter->next)
2997 if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set))
2999 state_contains = state_iter;
3003 if (NULL == state_contains)
3005 new_dfa_state = dfa_state_create (ctx, &nfa_set);
3006 automaton_add_state (dfa, new_dfa_state);
3007 ctran->to_state = new_dfa_state;
3008 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
3012 ctran->to_state = state_contains;
3013 state_set_clear (&nfa_set);
3020 * Construct DFA for the given 'regex' of length 'len'.
3022 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
3023 * compressed to o -> abc -> o. Note that this parameter influences the
3024 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
3025 * edges with the same label). For example for an application that stores IPv4
3026 * addresses as bitstrings it could make sense to limit the path compression to
3029 * @param regex regular expression string.
3030 * @param len length of the regular expression.
3031 * @param max_path_len limit the path compression length to the
3032 * given value. If set to 1, no path compression is applied. Set to 0 for
3033 * maximal possible path compression (generally not desireable).
3034 * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
3036 struct REGEX_INTERNAL_Automaton *
3037 REGEX_INTERNAL_construct_dfa (const char *regex, const size_t len,
3038 unsigned int max_path_len)
3040 struct REGEX_INTERNAL_Context ctx;
3041 struct REGEX_INTERNAL_Automaton *dfa;
3042 struct REGEX_INTERNAL_Automaton *nfa;
3043 struct REGEX_INTERNAL_StateSet nfa_start_eps_cls;
3044 struct REGEX_INTERNAL_StateSet singleton_set;
3046 REGEX_INTERNAL_context_init (&ctx);
3049 nfa = REGEX_INTERNAL_construct_nfa (regex, len);
3053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3054 "Could not create DFA, because NFA creation failed\n");
3058 dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton);
3060 dfa->regex = GNUNET_strdup (regex);
3062 /* Create DFA start state from epsilon closure */
3063 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3064 state_set_append (&singleton_set, nfa->start);
3065 nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL);
3066 state_set_clear (&singleton_set);
3067 dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls);
3068 automaton_add_state (dfa, dfa->start);
3070 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
3071 REGEX_INTERNAL_automaton_destroy (nfa);
3074 if (GNUNET_OK != dfa_minimize (&ctx, dfa))
3076 REGEX_INTERNAL_automaton_destroy (dfa);
3080 /* Create proofs and hashes for all states */
3081 if (GNUNET_OK != automaton_create_proofs (dfa))
3083 REGEX_INTERNAL_automaton_destroy (dfa);
3087 /* Compress linear DFA paths */
3088 if (1 != max_path_len)
3089 dfa_compress_paths (&ctx, dfa, max_path_len);
3096 * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton data
3099 * @param a automaton to be destroyed
3102 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3104 struct REGEX_INTERNAL_State *s;
3105 struct REGEX_INTERNAL_State *next_state;
3110 GNUNET_free_non_null (a->regex);
3111 GNUNET_free_non_null (a->canonical_regex);
3113 for (s = a->states_head; NULL != s; s = next_state)
3115 next_state = s->next;
3116 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
3117 automaton_destroy_state (s);
3125 * Evaluates the given string using the given DFA automaton
3127 * @param a automaton, type must be DFA
3128 * @param string string that should be evaluated
3130 * @return 0 if string matches, non-0 otherwise
3133 evaluate_dfa (struct REGEX_INTERNAL_Automaton *a,
3137 struct REGEX_INTERNAL_State *s;
3138 unsigned int step_len;
3142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3143 "Tried to evaluate DFA, but NFA automaton given");
3149 /* If the string is empty but the starting state is accepting, we accept. */
3150 if ((NULL == string || 0 == strlen (string)) && s->accepting)
3153 for (strp = string; NULL != strp && *strp; strp += step_len)
3155 step_len = dfa_move (&s, strp);
3161 if (NULL != s && s->accepting)
3169 * Evaluates the given string using the given NFA automaton
3171 * @param a automaton, type must be NFA
3172 * @param string string that should be evaluated
3173 * @return 0 if string matches, non-0 otherwise
3176 evaluate_nfa (struct REGEX_INTERNAL_Automaton *a,
3181 struct REGEX_INTERNAL_State *s;
3182 struct REGEX_INTERNAL_StateSet sset;
3183 struct REGEX_INTERNAL_StateSet new_sset;
3184 struct REGEX_INTERNAL_StateSet singleton_set;
3190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3191 "Tried to evaluate NFA, but DFA automaton given");
3195 /* If the string is empty but the starting state is accepting, we accept. */
3196 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
3200 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3201 state_set_append (&singleton_set, a->start);
3202 nfa_closure_set_create (&sset, a, &singleton_set, NULL);
3203 state_set_clear (&singleton_set);
3206 for (strp = string; NULL != strp && *strp; strp++)
3209 nfa_closure_set_create (&new_sset, a, &sset, str);
3210 state_set_clear (&sset);
3211 nfa_closure_set_create (&sset, a, &new_sset, 0);
3212 state_set_clear (&new_sset);
3215 for (i = 0; i < sset.off; i++)
3218 if ( (NULL != s) && (s->accepting) )
3225 state_set_clear (&sset);
3231 * Evaluates the given @a string against the given compiled regex @a a
3233 * @param a automaton
3234 * @param string string to check
3235 * @return 0 if string matches, non-0 otherwise
3238 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
3246 result = evaluate_dfa (a, string);
3249 result = evaluate_nfa (a, string);
3252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3253 "Evaluating regex failed, automaton has no type!\n");
3254 result = GNUNET_SYSERR;
3263 * Get the canonical regex of the given automaton.
3264 * When constructing the automaton a proof is computed for each state,
3265 * consisting of the regular expression leading to this state. A complete
3266 * regex for the automaton can be computed by combining these proofs.
3267 * As of now this function is only useful for testing.
3269 * @param a automaton for which the canonical regex should be returned.
3274 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
3279 return a->canonical_regex;
3284 * Get the number of transitions that are contained in the given automaton.
3286 * @param a automaton for which the number of transitions should be returned.
3288 * @return number of transitions in the given automaton.
3291 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
3293 unsigned int t_count;
3294 struct REGEX_INTERNAL_State *s;
3300 for (s = a->states_head; NULL != s; s = s->next)
3301 t_count += s->transition_count;
3308 * Get the first key for the given @a input_string. This hashes the first x bits
3309 * of the @a input_string.
3311 * @param input_string string.
3312 * @param string_len length of the @a input_string.
3313 * @param key pointer to where to write the hash code.
3314 * @return number of bits of @a input_string that have been consumed
3315 * to construct the key
3318 REGEX_INTERNAL_get_first_key (const char *input_string,
3320 struct GNUNET_HashCode *key)
3324 size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len :
3325 GNUNET_REGEX_INITIAL_BYTES;
3326 if (NULL == input_string)
3328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3329 "Given input string was NULL!\n");
3332 GNUNET_CRYPTO_hash (input_string, size, key);
3339 * Recursive function that calls the iterator for each synthetic start state.
3341 * @param min_len minimum length of the path in the graph.
3342 * @param max_len maximum length of the path in the graph.
3343 * @param consumed_string string consumed by traversing the graph till this state.
3344 * @param state current state of the automaton.
3345 * @param iterator iterator function called for each edge.
3346 * @param iterator_cls closure for the @a iterator function.
3349 iterate_initial_edge (unsigned int min_len,
3350 unsigned int max_len,
3351 char *consumed_string,
3352 struct REGEX_INTERNAL_State *state,
3353 REGEX_INTERNAL_KeyIterator iterator,
3357 struct REGEX_INTERNAL_Transition *t;
3358 unsigned int num_edges = state->transition_count;
3359 struct REGEX_BLOCK_Edge edges[num_edges];
3360 struct REGEX_BLOCK_Edge edge[1];
3361 struct GNUNET_HashCode hash;
3362 struct GNUNET_HashCode hash_new;
3363 unsigned int cur_len;
3365 if (NULL != consumed_string)
3366 cur_len = strlen (consumed_string);
3370 if ( ( (cur_len >= min_len) ||
3371 (GNUNET_YES == state->accepting) ) &&
3373 (NULL != consumed_string) )
3375 if (cur_len <= max_len)
3377 if ( (NULL != state->proof) &&
3378 (0 != strcmp (consumed_string,
3381 (void) state_get_edges (state, edges);
3382 GNUNET_CRYPTO_hash (consumed_string,
3383 strlen (consumed_string),
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 "Start state for string `%s' is %s\n",
3388 GNUNET_h2s (&hash));
3389 iterator (iterator_cls,
3396 if ( (GNUNET_YES == state->accepting) &&
3398 (state->transition_count < 1) &&
3399 (cur_len < max_len) )
3401 /* Special case for regex consisting of just a string that is shorter than
3403 edge[0].label = &consumed_string[cur_len - 1];
3404 edge[0].destination = state->hash;
3405 temp = GNUNET_strdup (consumed_string);
3406 temp[cur_len - 1] = '\0';
3407 GNUNET_CRYPTO_hash (temp,
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3411 "Start state for short string `%s' is %s\n",
3413 GNUNET_h2s (&hash_new));
3414 iterator (iterator_cls,
3422 else /* cur_len > max_len */
3424 /* Case where the concatenated labels are longer than max_len, then split. */
3425 edge[0].label = &consumed_string[max_len];
3426 edge[0].destination = state->hash;
3427 temp = GNUNET_strdup (consumed_string);
3428 temp[max_len] = '\0';
3429 GNUNET_CRYPTO_hash (temp, max_len, &hash);
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3431 "Start state at split edge `%s'-`%s` is %s\n",
3434 GNUNET_h2s (&hash_new));
3435 iterator (iterator_cls,
3445 if (cur_len < max_len)
3447 for (t = state->transitions_head; NULL != t; t = t->next)
3449 if (NULL != strchr (t->label,
3452 /* Wildcards not allowed during starting states */
3456 if (NULL != consumed_string)
3457 GNUNET_asprintf (&temp,
3462 GNUNET_asprintf (&temp,
3465 iterate_initial_edge (min_len,
3478 * Iterate over all edges starting from start state of automaton 'a'. Calling
3479 * iterator for each edge.
3481 * @param a automaton.
3482 * @param iterator iterator called for each edge.
3483 * @param iterator_cls closure.
3486 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
3487 REGEX_INTERNAL_KeyIterator iterator,
3490 struct REGEX_INTERNAL_State *s;
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 "Iterating over starting edges\n");
3494 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES,
3495 GNUNET_REGEX_INITIAL_BYTES,
3497 iterator, iterator_cls);
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3499 "Iterating over DFA edges\n");
3500 for (s = a->states_head; NULL != s; s = s->next)
3502 struct REGEX_BLOCK_Edge edges[s->transition_count];
3503 unsigned int num_edges;
3505 num_edges = state_get_edges (s, edges);
3506 if ( ( (NULL != s->proof) &&
3507 (0 < strlen (s->proof)) ) || s->accepting)
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3510 "Creating DFA edges at `%s' under key %s\n",
3512 GNUNET_h2s (&s->hash));
3513 iterator (iterator_cls, &s->hash, s->proof,
3517 s->marked = GNUNET_NO;
3523 * Struct to hold all the relevant state information in the HashMap.
3525 * Contains the same info as the Regex Iterator parametes except the key,
3526 * which comes directly from the HashMap iterator.
3528 struct temporal_state_store {
3533 struct REGEX_BLOCK_Edge *edges;
3538 * Store regex iterator and cls in one place to pass to the hashmap iterator.
3540 struct client_iterator {
3541 REGEX_INTERNAL_KeyIterator iterator;
3547 * Iterator over all edges of a dfa. Stores all of them in a HashMap
3548 * for later reachability marking.
3550 * @param cls Closure (HashMap)
3551 * @param key hash for current state.
3552 * @param proof proof for current state
3553 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
3554 * @param num_edges number of edges leaving current state.
3555 * @param edges edges leaving current state.
3558 store_all_states (void *cls,
3559 const struct GNUNET_HashCode *key,
3562 unsigned int num_edges,
3563 const struct REGEX_BLOCK_Edge *edges)
3565 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3566 struct temporal_state_store *tmp;
3569 tmp = GNUNET_new (struct temporal_state_store);
3570 tmp->reachable = GNUNET_NO;
3571 tmp->proof = GNUNET_strdup (proof);
3572 tmp->accepting = accepting;
3573 tmp->num_edges = num_edges;
3574 edges_size = sizeof (struct REGEX_BLOCK_Edge) * num_edges;
3575 tmp->edges = GNUNET_malloc (edges_size);
3576 GNUNET_memcpy(tmp->edges, edges, edges_size);
3577 GNUNET_CONTAINER_multihashmap_put (hm, key, tmp,
3578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3583 * Mark state as reachable and call recursively on all its edges.
3585 * If already marked as reachable, do nothing.
3587 * @param state State to mark as reachable.
3588 * @param hm HashMap which stores all the states indexed by key.
3591 mark_as_reachable (struct temporal_state_store *state,
3592 struct GNUNET_CONTAINER_MultiHashMap *hm)
3594 struct temporal_state_store *child;
3597 if (GNUNET_YES == state->reachable)
3601 state->reachable = GNUNET_YES;
3602 for (i = 0; i < state->num_edges; i++)
3604 child = GNUNET_CONTAINER_multihashmap_get (hm,
3605 &state->edges[i].destination);
3611 mark_as_reachable (child, hm);
3617 * Iterator over hash map entries to mark the ones that are reachable.
3619 * @param cls closure
3620 * @param key current key code
3621 * @param value value in the hash map
3622 * @return #GNUNET_YES if we should continue to iterate,
3623 * #GNUNET_NO if not.
3626 reachability_iterator (void *cls,
3627 const struct GNUNET_HashCode *key,
3630 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3631 struct temporal_state_store *state = value;
3633 if (GNUNET_YES == state->reachable)
3634 /* already visited and marked */
3637 if (GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof) &&
3638 GNUNET_NO == state->accepting)
3639 /* not directly reachable */
3642 mark_as_reachable (state, hm);
3648 * Iterator over hash map entries.
3649 * Calling the callback on the ones marked as reachables.
3651 * @param cls closure
3652 * @param key current key code
3653 * @param value value in the hash map
3654 * @return #GNUNET_YES if we should continue to iterate,
3655 * #GNUNET_NO if not.
3658 iterate_reachables (void *cls,
3659 const struct GNUNET_HashCode *key,
3662 struct client_iterator *ci = cls;
3663 struct temporal_state_store *state = value;
3665 if (GNUNET_YES == state->reachable)
3667 ci->iterator (ci->iterator_cls, key,
3668 state->proof, state->accepting,
3669 state->num_edges, state->edges);
3671 GNUNET_free (state->edges);
3672 GNUNET_free (state->proof);
3673 GNUNET_free (state);
3679 * Iterate over all edges of automaton 'a' that are reachable from a state with
3680 * a proof of at least GNUNET_REGEX_INITIAL_BYTES characters.
3682 * Call the iterator for each such edge.
3684 * @param a automaton.
3685 * @param iterator iterator called for each reachable edge.
3686 * @param iterator_cls closure.
3689 REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a,
3690 REGEX_INTERNAL_KeyIterator iterator,
3693 struct GNUNET_CONTAINER_MultiHashMap *hm;
3694 struct client_iterator ci;
3696 hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO);
3697 ci.iterator = iterator;
3698 ci.iterator_cls = iterator_cls;
3700 REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm);
3701 GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm);
3702 GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci);
3704 GNUNET_CONTAINER_multihashmap_destroy (hm);
3708 /* end of regex_internal.c */