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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file src/regex/regex_internal.c
20 * @brief library to create Deterministic Finite Automatons (DFAs) from regular
21 * expressions (regexes).
22 * @author Maximilian Szengel
25 #include "gnunet_util_lib.h"
26 #include "gnunet_regex_service.h"
27 #include "regex_internal_lib.h"
28 #include "regex_internal.h"
32 * Set this to #GNUNET_YES to enable state naming. Used to debug NFA->DFA
33 * creation. Disabled by default for better performance.
35 #define REGEX_DEBUG_DFA GNUNET_NO
38 * Set of states using MDLL API.
40 struct REGEX_INTERNAL_StateSet_MDLL
45 struct REGEX_INTERNAL_State *head;
50 struct REGEX_INTERNAL_State *tail;
60 * Append state to the given StateSet.
62 * @param set set to be modified
63 * @param state state to be appended
66 state_set_append (struct REGEX_INTERNAL_StateSet *set,
67 struct REGEX_INTERNAL_State *state)
69 if (set->off == set->size)
70 GNUNET_array_grow (set->states, set->size, set->size * 2 + 4);
71 set->states[set->off++] = state;
76 * Compare two strings for equality. If either is NULL they are not equal.
78 * @param str1 first string for comparison.
79 * @param str2 second string for comparison.
81 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
84 nullstrcmp (const char *str1, const char *str2)
86 if ((NULL == str1) != (NULL == str2))
88 if ((NULL == str1) && (NULL == str2))
91 return strcmp (str1, str2);
96 * Adds a transition from one state to another on @a label. Does not add
100 * @param from_state starting state for the transition
101 * @param label transition label
102 * @param to_state state to where the transition should point to
105 state_add_transition (struct REGEX_INTERNAL_Context *ctx,
106 struct REGEX_INTERNAL_State *from_state,
108 struct REGEX_INTERNAL_State *to_state)
110 struct REGEX_INTERNAL_Transition *t;
111 struct REGEX_INTERNAL_Transition *oth;
113 if (NULL == from_state)
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "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_new (struct REGEX_INTERNAL_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 REGEX_INTERNAL_State *state,
160 struct REGEX_INTERNAL_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 REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a;
192 struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b;
194 return (*s1)->id - (*s2)->id;
199 * Get all edges leaving state @a s.
202 * @param edges all edges leaving @a s, expected to be allocated and have enough
203 * space for `s->transitions_count` elements.
205 * @return number of edges.
208 state_get_edges (struct REGEX_INTERNAL_State *s,
209 struct REGEX_BLOCK_Edge *edges)
211 struct REGEX_INTERNAL_Transition *t;
219 for (t = s->transitions_head; NULL != t; t = t->next)
221 if (NULL != t->to_state)
223 edges[count].label = t->label;
224 edges[count].destination = t->to_state->hash;
233 * Compare to state sets by comparing the id's of the states that are contained
234 * in each set. Both sets are expected to be sorted by id!
236 * @param sset1 first state set
237 * @param sset2 second state set
238 * @return 0 if the sets are equal, otherwise non-zero
241 state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
242 struct REGEX_INTERNAL_StateSet *sset2)
247 if (NULL == sset1 || NULL == sset2)
250 result = sset1->off - sset2->off;
255 for (i = 0; i < sset1->off; i++)
256 if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i])))
263 * Clears the given StateSet 'set'
265 * @param set set to be cleared
268 state_set_clear (struct REGEX_INTERNAL_StateSet *set)
270 GNUNET_array_grow (set->states, set->size, 0);
276 * Clears an automaton fragment. Does not destroy the states inside the
279 * @param a automaton to be cleared
282 automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
289 a->states_head = NULL;
290 a->states_tail = NULL;
297 * Frees the memory used by State @a s
299 * @param s state that should be destroyed
302 automaton_destroy_state (struct REGEX_INTERNAL_State *s)
304 struct REGEX_INTERNAL_Transition *t;
305 struct REGEX_INTERNAL_Transition *next_t;
310 GNUNET_free_non_null (s->name);
311 GNUNET_free_non_null (s->proof);
312 state_set_clear (&s->nfa_set);
313 for (t = s->transitions_head; NULL != t; t = next_t)
316 state_remove_transition (s, t);
324 * Remove a state from the given automaton 'a'. Always use this function when
325 * altering the states of an automaton. Will also remove all transitions leading
326 * to this state, before destroying it.
329 * @param s state to remove
332 automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
333 struct REGEX_INTERNAL_State *s)
335 struct REGEX_INTERNAL_State *s_check;
336 struct REGEX_INTERNAL_Transition *t_check;
337 struct REGEX_INTERNAL_Transition *t_check_next;
339 if (NULL == a || NULL == s)
342 /* remove all transitions leading to this state */
343 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
345 for (t_check = s_check->transitions_head; NULL != t_check;
346 t_check = t_check_next)
348 t_check_next = t_check->next;
349 if (t_check->to_state == s)
350 state_remove_transition (s_check, t_check);
355 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
358 automaton_destroy_state (s);
363 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
364 * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
368 * @param s1 first state
369 * @param s2 second state, will be destroyed
372 automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
373 struct REGEX_INTERNAL_Automaton *a,
374 struct REGEX_INTERNAL_State *s1,
375 struct REGEX_INTERNAL_State *s2)
377 struct REGEX_INTERNAL_State *s_check;
378 struct REGEX_INTERNAL_Transition *t_check;
379 struct REGEX_INTERNAL_Transition *t;
380 struct REGEX_INTERNAL_Transition *t_next;
386 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
387 * does not already exists, if it already exists remove transition. */
388 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
390 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
392 t_next = t_check->next;
394 if (s2 == t_check->to_state)
397 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
399 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
402 if (GNUNET_NO == is_dup)
403 t_check->to_state = s1;
405 state_remove_transition (t_check->from_state, t_check);
410 /* 2. Add all transitions from s2 to sX to s1 */
411 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
413 if (t_check->to_state != s1)
414 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
417 /* 3. Rename s1 to {s1,s2} */
422 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
423 GNUNET_free (new_name);
427 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
429 automaton_destroy_state (s2);
434 * Add a state to the automaton 'a', always use this function to alter the
435 * states DLL of the automaton.
437 * @param a automaton to add the state to
438 * @param s state that should be added
441 automaton_add_state (struct REGEX_INTERNAL_Automaton *a,
442 struct REGEX_INTERNAL_State *s)
444 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
450 * Depth-first traversal (DFS) of all states that are reachable from state
451 * 's'. Performs 'action' on each visited state.
453 * @param s start state.
454 * @param marks an array of size a->state_count to remember which state was
456 * @param count current count of the state.
457 * @param check function that is checked before advancing on each transition
459 * @param check_cls closure for check.
460 * @param action action to be performed on each state.
461 * @param action_cls closure for action.
464 automaton_state_traverse (struct REGEX_INTERNAL_State *s, int *marks,
466 REGEX_INTERNAL_traverse_check check, void *check_cls,
467 REGEX_INTERNAL_traverse_action action, void *action_cls)
469 struct REGEX_INTERNAL_Transition *t;
471 if (GNUNET_YES == marks[s->traversal_id])
474 marks[s->traversal_id] = GNUNET_YES;
477 action (action_cls, *count, s);
481 for (t = s->transitions_head; NULL != t; t = t->next)
484 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
486 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
494 * Traverses the given automaton using depth-first-search (DFS) from it's start
495 * state, visiting all reachable states and calling 'action' on each one of
498 * @param a automaton to be traversed.
499 * @param start start state, pass a->start or NULL to traverse the whole automaton.
500 * @param check function that is checked before advancing on each transition
502 * @param check_cls closure for @a check.
503 * @param action action to be performed on each state.
504 * @param action_cls closure for @a action
507 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
508 struct REGEX_INTERNAL_State *start,
509 REGEX_INTERNAL_traverse_check check,
511 REGEX_INTERNAL_traverse_action action,
515 struct REGEX_INTERNAL_State *s;
517 if (NULL == a || 0 == a->state_count)
520 int marks[a->state_count];
522 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
523 s = s->next, count++)
525 s->traversal_id = count;
526 marks[s->traversal_id] = GNUNET_NO;
536 automaton_state_traverse (s, marks, &count,
543 * String container for faster string operations.
548 * Buffer holding the string (may start in the middle!);
559 * Length of the string in the buffer.
564 * Number of bytes allocated for @e sbuf
569 * Buffer currently represents "NULL" (not the empty string!)
574 * If this entry is part of the last/current generation array,
575 * this flag is #GNUNET_YES if the last and current generation are
576 * identical (and thus copying is unnecessary if the value didn't
577 * change). This is used in an optimization that improves
578 * performance by about 1% --- if we use int16_t here. With just
579 * "int" for both flags, performance drops (on my system) significantly,
580 * most likely due to increased cache misses.
588 * Compare two strings for equality. If either is NULL they are not equal.
590 * @param s1 first string for comparison.
591 * @param s2 second string for comparison.
593 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
596 sb_nullstrcmp (const struct StringBuffer *s1,
597 const struct StringBuffer *s2)
599 if ( (GNUNET_YES == s1->null_flag) &&
600 (GNUNET_YES == s2->null_flag) )
602 if ( (GNUNET_YES == s1->null_flag) ||
603 (GNUNET_YES == s2->null_flag) )
605 if (s1->slen != s2->slen)
609 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
614 * Compare two strings for equality.
616 * @param s1 first string for comparison.
617 * @param s2 second string for comparison.
619 * @return 0 if the strings are the same, 1 or -1 if not.
622 sb_strcmp (const struct StringBuffer *s1,
623 const struct StringBuffer *s2)
625 if (s1->slen != s2->slen)
629 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
634 * Reallocate the buffer of 'ret' to fit 'nlen' characters;
635 * move the existing string to the beginning of the new buffer.
637 * @param ret current buffer, to be updated
638 * @param nlen target length for the buffer, must be at least ret->slen
641 sb_realloc (struct StringBuffer *ret,
646 GNUNET_assert (nlen >= ret->slen);
648 ret->abuf = GNUNET_malloc (nlen);
650 GNUNET_memcpy (ret->abuf,
653 ret->sbuf = ret->abuf;
654 GNUNET_free_non_null (old);
661 * @param ret where to write the result
662 * @param sarg string to append
665 sb_append (struct StringBuffer *ret,
666 const struct StringBuffer *sarg)
668 if (GNUNET_YES == ret->null_flag)
670 ret->null_flag = GNUNET_NO;
671 if (ret->blen < sarg->slen + ret->slen)
672 sb_realloc (ret, ret->blen + sarg->slen + 128);
673 GNUNET_memcpy (&ret->sbuf[ret->slen],
676 ret->slen += sarg->slen;
683 * @param ret where to write the result
684 * @param cstr string to append
687 sb_append_cstr (struct StringBuffer *ret,
690 size_t cstr_len = strlen (cstr);
692 if (GNUNET_YES == ret->null_flag)
694 ret->null_flag = GNUNET_NO;
695 if (ret->blen < cstr_len + ret->slen)
696 sb_realloc (ret, ret->blen + cstr_len + 128);
697 GNUNET_memcpy (&ret->sbuf[ret->slen],
700 ret->slen += cstr_len;
705 * Wrap a string buffer, that is, set ret to the format string
706 * which contains an "%s" which is to be replaced with the original
707 * content of 'ret'. Note that optimizing this function is not
708 * really worth it, it is rarely called.
710 * @param ret where to write the result and take the input for %.*s from
711 * @param format format string, fprintf-style, with exactly one "%.*s"
712 * @param extra_chars how long will the result be, in addition to 'sarg' length
715 sb_wrap (struct StringBuffer *ret,
721 if (GNUNET_YES == ret->null_flag)
723 ret->null_flag = GNUNET_NO;
724 temp = GNUNET_malloc (ret->slen + extra_chars + 1);
725 GNUNET_snprintf (temp,
726 ret->slen + extra_chars + 1,
730 GNUNET_free_non_null (ret->abuf);
733 ret->blen = ret->slen + extra_chars + 1;
734 ret->slen = ret->slen + extra_chars;
739 * Format a string buffer. Note that optimizing this function is not
740 * really worth it, it is rarely called.
742 * @param ret where to write the result
743 * @param format format string, fprintf-style, with exactly one "%.*s"
744 * @param extra_chars how long will the result be, in addition to 'sarg' length
745 * @param sarg string to print into the format
748 sb_printf1 (struct StringBuffer *ret,
751 const struct StringBuffer *sarg)
753 if (ret->blen < sarg->slen + extra_chars + 1)
755 sarg->slen + extra_chars + 1);
756 ret->null_flag = GNUNET_NO;
757 ret->sbuf = ret->abuf;
758 ret->slen = sarg->slen + extra_chars;
759 GNUNET_snprintf (ret->sbuf,
768 * Format a string buffer.
770 * @param ret where to write the result
771 * @param format format string, fprintf-style, with exactly two "%.*s"
772 * @param extra_chars how long will the result be, in addition to 'sarg1/2' length
773 * @param sarg1 first string to print into the format
774 * @param sarg2 second string to print into the format
777 sb_printf2 (struct StringBuffer *ret,
780 const struct StringBuffer *sarg1,
781 const struct StringBuffer *sarg2)
783 if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1)
785 sarg1->slen + sarg2->slen + extra_chars + 1);
786 ret->null_flag = GNUNET_NO;
787 ret->slen = sarg1->slen + sarg2->slen + extra_chars;
788 ret->sbuf = ret->abuf;
789 GNUNET_snprintf (ret->sbuf,
800 * Format a string buffer. Note that optimizing this function is not
801 * really worth it, it is rarely called.
803 * @param ret where to write the result
804 * @param format format string, fprintf-style, with exactly three "%.*s"
805 * @param extra_chars how long will the result be, in addition to 'sarg1/2/3' length
806 * @param sarg1 first string to print into the format
807 * @param sarg2 second string to print into the format
808 * @param sarg3 third string to print into the format
811 sb_printf3 (struct StringBuffer *ret,
814 const struct StringBuffer *sarg1,
815 const struct StringBuffer *sarg2,
816 const struct StringBuffer *sarg3)
818 if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1)
820 sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1);
821 ret->null_flag = GNUNET_NO;
822 ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars;
823 ret->sbuf = ret->abuf;
824 GNUNET_snprintf (ret->sbuf,
837 * Free resources of the given string buffer.
839 * @param sb buffer to free (actual pointer is not freed, as they
840 * should not be individually allocated)
843 sb_free (struct StringBuffer *sb)
845 GNUNET_array_grow (sb->abuf,
850 sb->null_flag= GNUNET_YES;
855 * Copy the given string buffer from 'in' to 'out'.
857 * @param in input string
858 * @param out output string
861 sb_strdup (struct StringBuffer *out,
862 const struct StringBuffer *in)
865 out->null_flag = in->null_flag;
866 if (GNUNET_YES == out->null_flag)
868 if (out->blen < in->slen)
870 GNUNET_array_grow (out->abuf,
874 out->sbuf = out->abuf;
875 out->slen = in->slen;
876 GNUNET_memcpy (out->sbuf, in->sbuf, out->slen);
881 * Copy the given string buffer from 'in' to 'out'.
883 * @param cstr input string
884 * @param out output string
887 sb_strdup_cstr (struct StringBuffer *out,
892 out->null_flag = GNUNET_YES;
895 out->null_flag = GNUNET_NO;
896 out->slen = strlen (cstr);
897 if (out->blen < out->slen)
899 GNUNET_array_grow (out->abuf,
903 out->sbuf = out->abuf;
904 GNUNET_memcpy (out->sbuf, cstr, out->slen);
909 * Check if the given string @a str needs parentheses around it when
910 * using it to generate a regex.
914 * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise
917 needs_parentheses (const struct StringBuffer *str)
926 if ((GNUNET_YES == str->null_flag) || ((slen = str->slen) < 2))
931 end = str->sbuf + slen;
936 cl = memchr (pos, ')', end - pos);
942 /* while '(' before ')', count opening parens */
943 while ( (NULL != (op = memchr (pos, '(', end - pos))) &&
953 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
958 * Remove parentheses surrounding string @a str.
959 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
960 * You need to #GNUNET_free() the returned string.
962 * @param str string, modified to contain a
963 * @return string without surrounding parentheses, string 'str' if no preceding
964 * epsilon could be found, NULL if 'str' was NULL
967 remove_parentheses (struct StringBuffer *str)
980 if ( (GNUNET_YES == str->null_flag) ||
981 (1 >= (slen = str->slen)) ||
982 ('(' != str->sbuf[0]) ||
983 (')' != str->sbuf[slen - 1]) )
987 end = &sbuf[slen - 1];
988 op = memchr (pos, '(', end - pos);
989 cp = memchr (pos, ')', end - pos);
992 while ( (NULL != op) &&
997 op = memchr (pos, '(', end - pos);
999 while ( (NULL != cp) &&
1004 return; /* can't strip parens */
1007 cp = memchr (pos, ')', end - pos);
1021 * Check if the string 'str' starts with an epsilon (empty string).
1022 * Example: "(|a)" is starting with an epsilon.
1024 * @param str string to test
1026 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
1029 has_epsilon (const struct StringBuffer *str)
1032 (GNUNET_YES != str->null_flag) &&
1034 ('(' == str->sbuf[0]) &&
1035 ('|' == str->sbuf[1]) &&
1036 (')' == str->sbuf[str->slen - 1]);
1041 * Remove an epsilon from the string str. Where epsilon is an empty string
1042 * Example: str = "(|a|b|c)", result: "a|b|c"
1043 * The returned string needs to be freed.
1045 * @param str original string
1046 * @param ret where to return string without preceding epsilon, string 'str' if no preceding
1047 * epsilon could be found, NULL if 'str' was NULL
1050 remove_epsilon (const struct StringBuffer *str,
1051 struct StringBuffer *ret)
1053 if (GNUNET_YES == str->null_flag)
1055 ret->null_flag = GNUNET_YES;
1058 if ( (str->slen > 1) &&
1059 ('(' == str->sbuf[0]) &&
1060 ('|' == str->sbuf[1]) &&
1061 (')' == str->sbuf[str->slen - 1]) )
1063 /* remove epsilon */
1064 if (ret->blen < str->slen - 3)
1066 GNUNET_array_grow (ret->abuf,
1070 ret->sbuf = ret->abuf;
1071 ret->slen = str->slen - 3;
1072 GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen);
1075 sb_strdup (ret, str);
1080 * Compare n bytes of 'str1' and 'str2'
1082 * @param str1 first string to compare
1083 * @param str2 second string for comparison
1084 * @param n number of bytes to compare
1086 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1089 sb_strncmp (const struct StringBuffer *str1,
1090 const struct StringBuffer *str2, size_t n)
1094 if ( (str1->slen != str2->slen) &&
1095 ( (str1->slen < n) ||
1096 (str2->slen < n) ) )
1098 max = GNUNET_MAX (str1->slen, str2->slen);
1101 return memcmp (str1->sbuf, str2->sbuf, max);
1106 * Compare n bytes of 'str1' and 'str2'
1108 * @param str1 first string to compare
1109 * @param str2 second C string for comparison
1110 * @param n number of bytes to compare (and length of str2)
1112 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1115 sb_strncmp_cstr (const struct StringBuffer *str1,
1116 const char *str2, size_t n)
1120 return memcmp (str1->sbuf, str2, n);
1125 * Initialize string buffer for storing strings of up to n
1128 * @param sb buffer to initialize
1129 * @param n desired target length
1132 sb_init (struct StringBuffer *sb,
1135 sb->null_flag = GNUNET_NO;
1136 sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n);
1143 * Compare 'str1', starting from position 'k', with whole 'str2'
1145 * @param str1 first string to compare, starting from position 'k'
1146 * @param str2 second string for comparison
1147 * @param k starting position in 'str1'
1149 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1152 sb_strkcmp (const struct StringBuffer *str1,
1153 const struct StringBuffer *str2, size_t k)
1155 if ( (GNUNET_YES == str1->null_flag) ||
1156 (GNUNET_YES == str2->null_flag) ||
1158 (str1->slen - k != str2->slen) )
1160 return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen);
1165 * Helper function used as 'action' in 'REGEX_INTERNAL_automaton_traverse'
1166 * function to create the depth-first numbering of the states.
1168 * @param cls states array.
1169 * @param count current state counter.
1170 * @param s current state.
1173 number_states (void *cls, const unsigned int count,
1174 struct REGEX_INTERNAL_State *s)
1176 struct REGEX_INTERNAL_State **states = cls;
1186 ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \
1187 ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf)
1191 * Construct the regular expression given the inductive step,
1192 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
1193 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
1195 * @param R_last_ij value of $R^{(k-1)_{ij}.
1196 * @param R_last_ik value of $R^{(k-1)_{ik}.
1197 * @param R_last_kk value of $R^{(k-1)_{kk}.
1198 * @param R_last_kj value of $R^{(k-1)_{kj}.
1199 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
1200 * is expected to be NULL when called!
1201 * @param R_cur_l optimization -- kept between iterations to avoid realloc
1202 * @param R_cur_r optimization -- kept between iterations to avoid realloc
1205 automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1206 const struct StringBuffer *R_last_ik,
1207 const struct StringBuffer *R_last_kk,
1208 const struct StringBuffer *R_last_kj,
1209 struct StringBuffer *R_cur_ij,
1210 struct StringBuffer *R_cur_l,
1211 struct StringBuffer *R_cur_r)
1213 struct StringBuffer R_temp_ij;
1214 struct StringBuffer R_temp_ik;
1215 struct StringBuffer R_temp_kj;
1216 struct StringBuffer R_temp_kk;
1222 int clean_ik_kk_cmp;
1223 int clean_kk_kj_cmp;
1229 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1230 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1231 * R_cur_ij = R_cur_l | R_cur_r
1232 * R_cur_l == R^{(k-1)}_{ij}
1233 * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1236 if ( (GNUNET_YES == R_last_ij->null_flag) &&
1237 ( (GNUNET_YES == R_last_ik->null_flag) ||
1238 (GNUNET_YES == R_last_kj->null_flag)))
1240 /* R^{(k)}_{ij} = N | N */
1241 R_cur_ij->null_flag = GNUNET_YES;
1242 R_cur_ij->synced = GNUNET_NO;
1246 if ( (GNUNET_YES == R_last_ik->null_flag) ||
1247 (GNUNET_YES == R_last_kj->null_flag) )
1249 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1250 if (GNUNET_YES == R_last_ij->synced)
1252 R_cur_ij->synced = GNUNET_YES;
1253 R_cur_ij->null_flag = GNUNET_NO;
1256 R_cur_ij->synced = GNUNET_YES;
1257 sb_strdup (R_cur_ij, R_last_ij);
1260 R_cur_ij->synced = GNUNET_NO;
1262 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
1263 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
1265 R_cur_r->null_flag = GNUNET_YES;
1267 R_cur_l->null_flag = GNUNET_YES;
1270 /* cache results from strcmp, we might need these many times */
1271 ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj);
1272 ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik);
1273 ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk);
1274 kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj);
1276 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1277 * as parentheses, so we can better compare the contents */
1279 memset (&R_temp_ij, 0, sizeof (struct StringBuffer));
1280 memset (&R_temp_ik, 0, sizeof (struct StringBuffer));
1281 memset (&R_temp_kk, 0, sizeof (struct StringBuffer));
1282 memset (&R_temp_kj, 0, sizeof (struct StringBuffer));
1283 remove_epsilon (R_last_ik, &R_temp_ik);
1284 remove_epsilon (R_last_kk, &R_temp_kk);
1285 remove_epsilon (R_last_kj, &R_temp_kj);
1286 remove_parentheses (&R_temp_ik);
1287 remove_parentheses (&R_temp_kk);
1288 remove_parentheses (&R_temp_kj);
1289 clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk);
1290 clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj);
1292 /* construct R_cur_l (and, if necessary R_cur_r) */
1293 if (GNUNET_YES != R_last_ij->null_flag)
1295 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1296 * as parentheses, so we can better compare the contents */
1297 remove_epsilon (R_last_ij, &R_temp_ij);
1298 remove_parentheses (&R_temp_ij);
1300 if ( (0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) &&
1301 (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) &&
1302 (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) )
1304 if (0 == R_temp_ij.slen)
1306 R_cur_r->null_flag = GNUNET_NO;
1308 else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) ||
1309 (0 == sb_strncmp_cstr (R_last_ik, "(|", 2) &&
1310 0 == sb_strncmp_cstr (R_last_kj, "(|", 2)))
1313 * a|(e|a)a*(e|a) = a*
1314 * a|(e|a)(e|a)*(e|a) = a*
1316 * (e|a)|aa*(e|a) = a*
1317 * (e|a)|(e|a)a*a = a*
1318 * (e|a)|(e|a)a*(e|a) = a*
1319 * (e|a)|(e|a)(e|a)*(e|a) = a*
1321 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1322 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1324 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij);
1332 * a|(e|a)(e|a)*a = a+
1333 * a|a(e|a)*(e|a) = a+
1335 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1336 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1338 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij);
1341 else if ( (0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && (0 != clean_ik_kk_cmp) )
1344 if (0 == R_last_kk->slen)
1345 sb_strdup (R_cur_r, R_last_ij);
1346 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1347 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1349 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk);
1350 R_cur_l->null_flag = GNUNET_YES;
1352 else if ( (0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && (0 != clean_kk_kj_cmp))
1355 if (R_last_kk->slen < 1)
1357 sb_strdup (R_cur_r, R_last_kj);
1359 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1360 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1362 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1364 R_cur_l->null_flag = GNUNET_YES;
1366 else if ( (0 == ij_ik_cmp) && (0 == kk_kj_cmp) && (! has_epsilon (R_last_ij)) &&
1367 has_epsilon (R_last_kk))
1369 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
1370 if (needs_parentheses (&R_temp_kk))
1371 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1373 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk);
1374 R_cur_l->null_flag = GNUNET_YES;
1376 else if ( (0 == ij_kj_cmp) && (0 == ik_kk_cmp) && (! has_epsilon (R_last_ij)) &&
1377 has_epsilon (R_last_kk))
1379 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
1380 if (needs_parentheses (&R_temp_kk))
1381 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1383 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1384 R_cur_l->null_flag = GNUNET_YES;
1388 sb_strdup (R_cur_l, R_last_ij);
1389 remove_parentheses (R_cur_l);
1394 /* we have no left side */
1395 R_cur_l->null_flag = GNUNET_YES;
1398 /* construct R_cur_r, if not already constructed */
1399 if (GNUNET_YES == R_cur_r->null_flag)
1401 length = R_temp_kk.slen - R_last_ik->slen;
1403 /* a(ba)*bx = (ab)+x */
1404 if ( (length > 0) &&
1405 (GNUNET_YES != R_last_kk->null_flag) &&
1406 (0 < R_last_kk->slen) &&
1407 (GNUNET_YES != R_last_kj->null_flag) &&
1408 (0 < R_last_kj->slen) &&
1409 (GNUNET_YES != R_last_ik->null_flag) &&
1410 (0 < R_last_ik->slen) &&
1411 (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) &&
1412 (0 == sb_strncmp (&R_temp_kk, R_last_kj, length)) )
1414 struct StringBuffer temp_a;
1415 struct StringBuffer temp_b;
1417 sb_init (&temp_a, length);
1418 sb_init (&temp_b, R_last_kj->slen - length);
1421 temp_a.sbuf = temp_a.abuf;
1422 GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l);
1423 temp_a.slen = length_l;
1425 length_r = R_last_kj->slen - length;
1426 temp_b.sbuf = temp_b.abuf;
1427 GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1428 temp_b.slen = length_r;
1430 /* e|(ab)+ = (ab)* */
1431 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1432 (0 == R_cur_l->slen) &&
1433 (0 == temp_b.slen) )
1435 sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1437 R_cur_l->null_flag = GNUNET_YES;
1441 sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1446 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk) &&
1447 0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1451 * (e|a)(e|a)*(e|a) = a*
1453 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1455 if (needs_parentheses (&R_temp_kk))
1456 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk);
1458 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk);
1461 else if ( (0 == clean_ik_kk_cmp) &&
1462 (0 == clean_kk_kj_cmp) &&
1463 (! has_epsilon (R_last_ik)) )
1465 if (needs_parentheses (&R_temp_kk))
1466 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk);
1468 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk);
1479 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1480 has_epsilon (R_last_kj));
1484 if (needs_parentheses (&R_temp_kk))
1485 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk);
1487 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk);
1493 * (e|a)(e|a)*b = a*b
1495 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk))
1497 if (has_epsilon (R_last_ik))
1499 if (needs_parentheses (&R_temp_kk))
1500 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1502 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1506 if (needs_parentheses (&R_temp_kk))
1507 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1509 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1514 * b(e|a)*(e|a) = ba*
1516 else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1518 if (has_epsilon (R_last_kj))
1520 if (needs_parentheses (&R_temp_kk))
1521 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1523 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1527 if (needs_parentheses (&R_temp_kk))
1528 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1530 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1535 if (0 < R_temp_kk.slen)
1537 if (needs_parentheses (&R_temp_kk))
1539 sb_printf3 (R_cur_r, "%.*s(%.*s)*%.*s", 3, R_last_ik, &R_temp_kk,
1544 sb_printf3 (R_cur_r, "%.*s%.*s*%.*s", 1, R_last_ik, &R_temp_kk,
1550 sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1554 sb_free (&R_temp_ij);
1555 sb_free (&R_temp_ik);
1556 sb_free (&R_temp_kk);
1557 sb_free (&R_temp_kj);
1559 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1560 (GNUNET_YES == R_cur_r->null_flag) )
1562 R_cur_ij->null_flag = GNUNET_YES;
1566 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1567 (GNUNET_YES == R_cur_r->null_flag) )
1569 struct StringBuffer tmp;
1572 *R_cur_ij = *R_cur_l;
1577 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1578 (GNUNET_YES != R_cur_r->null_flag) )
1580 struct StringBuffer tmp;
1583 *R_cur_ij = *R_cur_r;
1588 if (0 == sb_nullstrcmp (R_cur_l, R_cur_r))
1590 struct StringBuffer tmp;
1593 *R_cur_ij = *R_cur_l;
1597 sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r);
1602 * Create proofs for all states in the given automaton. Implementation of the
1603 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1604 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1606 * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1607 * proof) fields. The starting state will only have a valid proof/hash if it has
1608 * any incoming transitions.
1610 * @param a automaton for which to assign proofs and hashes, must not be NULL
1613 automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1615 unsigned int n = a->state_count;
1616 struct REGEX_INTERNAL_State *states[n];
1617 struct StringBuffer *R_last;
1618 struct StringBuffer *R_cur;
1619 struct StringBuffer R_cur_r;
1620 struct StringBuffer R_cur_l;
1621 struct StringBuffer *R_swap;
1622 struct REGEX_INTERNAL_Transition *t;
1623 struct StringBuffer complete_regex;
1628 R_last = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1629 R_cur = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1630 if ( (NULL == R_last) ||
1633 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
1634 GNUNET_free_non_null (R_cur);
1635 GNUNET_free_non_null (R_last);
1636 return GNUNET_SYSERR;
1639 /* create depth-first numbering of the states, initializes 'state' */
1640 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1643 for (i = 0; i < n; i++)
1644 GNUNET_assert (NULL != states[i]);
1645 for (i = 0; i < n; i++)
1646 for (j = 0; j < n; j++)
1647 R_last[i *n + j].null_flag = GNUNET_YES;
1649 /* Compute regular expressions of length "1" between each pair of states */
1650 for (i = 0; i < n; i++)
1652 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1654 j = t->to_state->dfs_id;
1655 if (GNUNET_YES == R_last[i * n + j].null_flag)
1657 sb_strdup_cstr (&R_last[i * n + j], t->label);
1661 sb_append_cstr (&R_last[i * n + j], "|");
1662 sb_append_cstr (&R_last[i * n + j], t->label);
1665 /* add self-loop: i is reachable from i via epsilon-transition */
1666 if (GNUNET_YES == R_last[i * n + i].null_flag)
1668 R_last[i * n + i].slen = 0;
1669 R_last[i * n + i].null_flag = GNUNET_NO;
1673 sb_wrap (&R_last[i * n + i], "(|%.*s)", 3);
1676 for (i = 0; i < n; i++)
1677 for (j = 0; j < n; j++)
1678 if (needs_parentheses (&R_last[i * n + j]))
1679 sb_wrap (&R_last[i * n + j], "(%.*s)", 2);
1680 /* Compute regular expressions of length "k" between each pair of states per
1682 memset (&R_cur_l, 0, sizeof (struct StringBuffer));
1683 memset (&R_cur_r, 0, sizeof (struct StringBuffer));
1684 for (k = 0; k < n; k++)
1686 for (i = 0; i < n; i++)
1688 for (j = 0; j < n; j++)
1690 /* Basis for the recursion:
1691 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1692 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1695 /* Create R_cur[i][j] and simplify the expression */
1696 automaton_create_proofs_simplify (&R_last[i * n + j], &R_last[i * n + k],
1697 &R_last[k * n + k], &R_last[k * n + j],
1699 &R_cur_l, &R_cur_r);
1702 /* set R_last = R_cur */
1706 /* clear 'R_cur' for next iteration */
1707 for (i = 0; i < n; i++)
1708 for (j = 0; j < n; j++)
1709 R_cur[i * n + j].null_flag = GNUNET_YES;
1713 /* assign proofs and hashes */
1714 for (i = 0; i < n; i++)
1716 if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1718 states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf,
1719 R_last[a->start->dfs_id * n + i].slen);
1720 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1725 /* complete regex for whole DFA: union of all pairs (start state/accepting
1727 sb_init (&complete_regex, 16 * n);
1728 for (i = 0; i < n; i++)
1730 if (states[i]->accepting)
1732 if ( (0 == complete_regex.slen) &&
1733 (0 < R_last[a->start->dfs_id * n + i].slen) )
1735 sb_append (&complete_regex,
1736 &R_last[a->start->dfs_id * n + i]);
1738 else if ( (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) &&
1739 (0 < R_last[a->start->dfs_id * n + i].slen) )
1741 sb_append_cstr (&complete_regex, "|");
1742 sb_append (&complete_regex,
1743 &R_last[a->start->dfs_id * n + i]);
1747 a->canonical_regex = GNUNET_strndup (complete_regex.sbuf, complete_regex.slen);
1750 sb_free (&complete_regex);
1751 for (i = 0; i < n; i++)
1752 for (j = 0; j < n; j++)
1754 sb_free (&R_cur[i * n + j]);
1755 sb_free (&R_last[i * n + j]);
1757 GNUNET_free (R_cur);
1758 GNUNET_free (R_last);
1764 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1765 * automaton_destroy_state.
1767 * @param ctx context
1768 * @param nfa_states set of NFA states on which the DFA should be based on
1770 * @return new DFA state
1772 static struct REGEX_INTERNAL_State *
1773 dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1774 struct REGEX_INTERNAL_StateSet *nfa_states)
1776 struct REGEX_INTERNAL_State *s;
1779 struct REGEX_INTERNAL_State *cstate;
1780 struct REGEX_INTERNAL_Transition *ctran;
1783 s = GNUNET_new (struct REGEX_INTERNAL_State);
1784 s->id = ctx->state_id++;
1788 if (NULL == nfa_states)
1790 GNUNET_asprintf (&s->name, "s%i", s->id);
1794 s->nfa_set = *nfa_states;
1796 if (nfa_states->off < 1)
1799 /* Create a name based on 'nfa_states' */
1800 len = nfa_states->off * 14 + 4;
1801 s->name = GNUNET_malloc (len);
1802 strcat (s->name, "{");
1805 for (i = 0; i < nfa_states->off; i++)
1807 cstate = nfa_states->states[i];
1808 GNUNET_snprintf (pos,
1809 pos - s->name + len,
1812 pos += strlen (pos);
1814 /* Add a transition for each distinct label to NULL state */
1815 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1816 if (NULL != ctran->label)
1817 state_add_transition (ctx, s, ctran->label, NULL);
1819 /* If the nfa_states contain an accepting state, the new dfa state is also
1821 if (cstate->accepting)
1825 s->name = GNUNET_realloc (s->name, strlen (s->name) + 1);
1827 memset (nfa_states, 0, sizeof (struct REGEX_INTERNAL_StateSet));
1833 * Move from the given state 's' to the next state on transition 'str'. Consumes
1834 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1835 * 's' will point to the next state, and the length of the substring used for
1836 * this transition will be returned. If no transition possible 0 is returned and
1837 * 's' points to NULL.
1839 * @param s starting state, will point to the next state or NULL (if no
1840 * transition possible)
1841 * @param str edge label to follow (will match longest common prefix)
1843 * @return length of the substring comsumed from 'str'
1846 dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1848 struct REGEX_INTERNAL_Transition *t;
1849 struct REGEX_INTERNAL_State *new_s;
1851 unsigned int max_len;
1858 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1860 len = strlen (t->label);
1862 if (0 == strncmp (t->label, str, len))
1867 new_s = t->to_state;
1878 * Set the given state 'marked' to #GNUNET_YES. Used by the
1879 * #dfa_remove_unreachable_states() function to detect unreachable states in the
1882 * @param cls closure, not used.
1883 * @param count count, not used.
1884 * @param s state where the marked attribute will be set to #GNUNET_YES.
1887 mark_states (void *cls,
1888 const unsigned int count,
1889 struct REGEX_INTERNAL_State *s)
1891 s->marked = GNUNET_YES;
1896 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1897 * states that are not reachable from the starting state.
1899 * @param a DFA automaton
1902 dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1904 struct REGEX_INTERNAL_State *s;
1905 struct REGEX_INTERNAL_State *s_next;
1907 /* 1. unmark all states */
1908 for (s = a->states_head; NULL != s; s = s->next)
1909 s->marked = GNUNET_NO;
1911 /* 2. traverse dfa from start state and mark all visited states */
1912 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1914 /* 3. delete all states that were not visited */
1915 for (s = a->states_head; NULL != s; s = s_next)
1918 if (GNUNET_NO == s->marked)
1919 automaton_remove_state (a, s);
1925 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1926 * not transition to any other state but themselves.
1928 * @param a DFA automaton
1931 dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a)
1933 struct REGEX_INTERNAL_State *s;
1934 struct REGEX_INTERNAL_State *s_next;
1935 struct REGEX_INTERNAL_Transition *t;
1938 GNUNET_assert (DFA == a->type);
1940 for (s = a->states_head; NULL != s; s = s_next)
1948 for (t = s->transitions_head; NULL != t; t = t->next)
1950 if (NULL != t->to_state && t->to_state != s)
1960 /* state s is dead, remove it */
1961 automaton_remove_state (a, s);
1967 * Merge all non distinguishable states in the DFA 'a'
1969 * @param ctx context
1970 * @param a DFA automaton
1971 * @return #GNUNET_OK on success
1974 dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1975 struct REGEX_INTERNAL_Automaton *a)
1978 struct REGEX_INTERNAL_State *s1;
1979 struct REGEX_INTERNAL_State *s2;
1980 struct REGEX_INTERNAL_Transition *t1;
1981 struct REGEX_INTERNAL_Transition *t2;
1982 struct REGEX_INTERNAL_State *s1_next;
1983 struct REGEX_INTERNAL_State *s2_next;
1985 unsigned int num_equal_edges;
1987 unsigned int state_cnt;
1988 unsigned long long idx;
1989 unsigned long long idx1;
1991 if ( (NULL == a) || (0 == a->state_count) )
1993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1994 "Could not merge nondistinguishable states, automaton was NULL.\n");
1995 return GNUNET_SYSERR;
1998 state_cnt = a->state_count;
1999 table = GNUNET_malloc_large ((sizeof (uint32_t) * state_cnt * state_cnt / 32) + sizeof (uint32_t));
2002 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
2003 return GNUNET_SYSERR;
2006 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
2009 /* Mark all pairs of accepting/!accepting states */
2010 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2011 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
2012 if ( (s1->accepting && !s2->accepting) ||
2013 (!s1->accepting && s2->accepting) )
2015 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2016 table[idx / 32] |= (1U << (idx % 32));
2019 /* Find all equal states */
2024 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2026 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
2028 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2029 if (0 != (table[idx / 32] & (1U << (idx % 32))))
2031 num_equal_edges = 0;
2032 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2034 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
2036 if (0 == strcmp (t1->label, t2->label))
2039 /* same edge, but targets definitively different, so we're different
2041 if (t1->to_state->marked > t2->to_state->marked)
2042 idx1 = (unsigned long long) t1->to_state->marked * state_cnt + t2->to_state->marked;
2044 idx1 = (unsigned long long) t2->to_state->marked * state_cnt + t1->to_state->marked;
2045 if (0 != (table[idx1 / 32] & (1U << (idx1 % 32))))
2047 table[idx / 32] |= (1U << (idx % 32));
2048 change = 1; /* changed a marker, need to run again */
2053 if ( (num_equal_edges != s1->transition_count) ||
2054 (num_equal_edges != s2->transition_count) )
2056 /* Make sure ALL edges of possible equal states are the same */
2057 table[idx / 32] |= (1U << (idx % 32));
2058 change = 1; /* changed a marker, need to run again */
2064 /* Merge states that are equal */
2065 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
2068 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2071 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2072 if (0 == (table[idx / 32] & (1U << (idx % 32))))
2073 automaton_merge_states (ctx, a, s1, s2);
2077 GNUNET_free (table);
2083 * Minimize the given DFA 'a' by removing all unreachable states, removing all
2084 * dead states and merging all non distinguishable states
2086 * @param ctx context
2087 * @param a DFA automaton
2088 * @return GNUNET_OK on success
2091 dfa_minimize (struct REGEX_INTERNAL_Context *ctx,
2092 struct REGEX_INTERNAL_Automaton *a)
2095 return GNUNET_SYSERR;
2097 GNUNET_assert (DFA == a->type);
2099 /* 1. remove unreachable states */
2100 dfa_remove_unreachable_states (a);
2102 /* 2. remove dead states */
2103 dfa_remove_dead_states (a);
2105 /* 3. Merge nondistinguishable states */
2106 if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a))
2107 return GNUNET_SYSERR;
2113 * Context for adding strided transitions to a DFA.
2115 struct REGEX_INTERNAL_Strided_Context
2118 * Length of the strides.
2120 const unsigned int stride;
2123 * Strided transitions DLL. New strided transitions will be stored in this DLL
2124 * and afterwards added to the DFA.
2126 struct REGEX_INTERNAL_Transition *transitions_head;
2129 * Strided transitions DLL.
2131 struct REGEX_INTERNAL_Transition *transitions_tail;
2136 * Recursive helper function to add strides to a DFA.
2138 * @param cls context, contains stride length and strided transitions DLL.
2139 * @param depth current depth of the depth-first traversal of the graph.
2140 * @param label current label, string that contains all labels on the path from
2142 * @param start start state for the depth-first traversal of the graph.
2143 * @param s current state in the depth-first traversal
2146 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
2147 struct REGEX_INTERNAL_State *start,
2148 struct REGEX_INTERNAL_State *s)
2150 struct REGEX_INTERNAL_Strided_Context *ctx = cls;
2151 struct REGEX_INTERNAL_Transition *t;
2154 if (depth == ctx->stride)
2156 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2157 t->label = GNUNET_strdup (label);
2159 t->from_state = start;
2160 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
2165 for (t = s->transitions_head; NULL != t; t = t->next)
2167 /* Do not consider self-loops, because it end's up in too many
2169 if (t->to_state == t->from_state)
2174 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2177 new_label = GNUNET_strdup (t->label);
2179 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
2183 GNUNET_free_non_null (label);
2188 * Function called for each state in the DFA. Starts a traversal of depth set in
2189 * context starting from state 's'.
2191 * @param cls context.
2192 * @param count not used.
2193 * @param s current state.
2196 dfa_add_multi_strides (void *cls, const unsigned int count,
2197 struct REGEX_INTERNAL_State *s)
2199 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
2204 * Adds multi-strided transitions to the given 'dfa'.
2206 * @param regex_ctx regex context needed to add transitions to the automaton.
2207 * @param dfa DFA to which the multi strided transitions should be added.
2208 * @param stride_len length of the strides.
2211 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2212 struct REGEX_INTERNAL_Automaton *dfa,
2213 const unsigned int stride_len)
2215 struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2216 struct REGEX_INTERNAL_Transition *t;
2217 struct REGEX_INTERNAL_Transition *t_next;
2219 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
2222 /* Compute the new transitions of given stride_len */
2223 REGEX_INTERNAL_automaton_traverse (dfa, dfa->start, NULL, NULL,
2224 &dfa_add_multi_strides, &ctx);
2226 /* Add all the new transitions to the automaton. */
2227 for (t = ctx.transitions_head; NULL != t; t = t_next)
2230 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2231 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
2232 GNUNET_free_non_null (t->label);
2236 /* Mark this automaton as multistrided */
2237 dfa->is_multistrided = GNUNET_YES;
2241 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
2242 * and adds new transitions to the given transitions DLL and marks states that
2243 * should be removed by setting state->contained to GNUNET_YES.
2245 * @param dfa DFA for which the paths should be compressed.
2246 * @param start starting state for linear path search.
2247 * @param cur current state in the recursive DFS.
2248 * @param label current label (string of traversed labels).
2249 * @param max_len maximal path compression length.
2250 * @param transitions_head transitions DLL.
2251 * @param transitions_tail transitions DLL.
2254 dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2255 struct REGEX_INTERNAL_State *start,
2256 struct REGEX_INTERNAL_State *cur, char *label,
2257 unsigned int max_len,
2258 struct REGEX_INTERNAL_Transition **transitions_head,
2259 struct REGEX_INTERNAL_Transition **transitions_tail)
2261 struct REGEX_INTERNAL_Transition *t;
2265 if (NULL != label &&
2266 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
2267 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
2268 max_len == strlen (label)) ||
2269 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
2271 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2272 t->label = GNUNET_strdup (label);
2274 t->from_state = start;
2275 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
2277 if (GNUNET_NO == cur->marked)
2279 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
2284 else if (cur != start)
2285 cur->contained = GNUNET_YES;
2287 if (GNUNET_YES == cur->marked && cur != start)
2290 cur->marked = GNUNET_YES;
2293 for (t = cur->transitions_head; NULL != t; t = t->next)
2296 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2298 new_label = GNUNET_strdup (t->label);
2300 if (t->to_state != cur)
2302 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
2303 transitions_head, transitions_tail);
2305 GNUNET_free (new_label);
2311 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
2312 * compressed to 0->3 by combining transitions.
2314 * @param regex_ctx context for adding new transitions.
2315 * @param dfa DFA representation, will directly modify the given DFA.
2316 * @param max_len maximal length of the compressed paths.
2319 dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2320 struct REGEX_INTERNAL_Automaton *dfa, unsigned int max_len)
2322 struct REGEX_INTERNAL_State *s;
2323 struct REGEX_INTERNAL_State *s_next;
2324 struct REGEX_INTERNAL_Transition *t;
2325 struct REGEX_INTERNAL_Transition *t_next;
2326 struct REGEX_INTERNAL_Transition *transitions_head = NULL;
2327 struct REGEX_INTERNAL_Transition *transitions_tail = NULL;
2332 /* Count the incoming transitions on each state. */
2333 for (s = dfa->states_head; NULL != s; s = s->next)
2335 for (t = s->transitions_head; NULL != t; t = t->next)
2337 if (NULL != t->to_state)
2338 t->to_state->incoming_transition_count++;
2342 /* Unmark all states. */
2343 for (s = dfa->states_head; NULL != s; s = s->next)
2345 s->marked = GNUNET_NO;
2346 s->contained = GNUNET_NO;
2349 /* Add strides and mark states that can be deleted. */
2350 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
2351 &transitions_head, &transitions_tail);
2353 /* Add all the new transitions to the automaton. */
2354 for (t = transitions_head; NULL != t; t = t_next)
2357 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2358 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
2359 GNUNET_free_non_null (t->label);
2363 /* Remove marked states (including their incoming and outgoing transitions). */
2364 for (s = dfa->states_head; NULL != s; s = s_next)
2367 if (GNUNET_YES == s->contained)
2368 automaton_remove_state (dfa, s);
2374 * Creates a new NFA fragment. Needs to be cleared using
2375 * automaton_fragment_clear.
2377 * @param start starting state
2378 * @param end end state
2380 * @return new NFA fragment
2382 static struct REGEX_INTERNAL_Automaton *
2383 nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2384 struct REGEX_INTERNAL_State *end)
2386 struct REGEX_INTERNAL_Automaton *n;
2388 n = GNUNET_new (struct REGEX_INTERNAL_Automaton);
2395 if (NULL == start || NULL == end)
2398 automaton_add_state (n, end);
2399 automaton_add_state (n, start);
2411 * Adds a list of states to the given automaton 'n'.
2413 * @param n automaton to which the states should be added
2414 * @param states_head head of the DLL of states
2415 * @param states_tail tail of the DLL of states
2418 nfa_add_states (struct REGEX_INTERNAL_Automaton *n,
2419 struct REGEX_INTERNAL_State *states_head,
2420 struct REGEX_INTERNAL_State *states_tail)
2422 struct REGEX_INTERNAL_State *s;
2424 if (NULL == n || NULL == states_head)
2426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
2430 if (NULL == n->states_head)
2432 n->states_head = states_head;
2433 n->states_tail = states_tail;
2437 if (NULL != states_head)
2439 n->states_tail->next = states_head;
2440 n->states_tail = states_tail;
2443 for (s = states_head; NULL != s; s = s->next)
2449 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
2451 * @param ctx context
2452 * @param accepting is it an accepting state or not
2454 * @return new NFA state
2456 static struct REGEX_INTERNAL_State *
2457 nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2459 struct REGEX_INTERNAL_State *s;
2461 s = GNUNET_new (struct REGEX_INTERNAL_State);
2462 s->id = ctx->state_id++;
2463 s->accepting = accepting;
2464 s->marked = GNUNET_NO;
2470 GNUNET_asprintf (&s->name, "s%i", s->id);
2477 * Calculates the closure set for the given set of states.
2479 * @param ret set to sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2480 * @param nfa the NFA containing 's'
2481 * @param states list of states on which to base the closure on
2482 * @param label transitioning label for which to base the closure on,
2483 * pass NULL for epsilon transition
2486 nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2487 struct REGEX_INTERNAL_Automaton *nfa,
2488 struct REGEX_INTERNAL_StateSet *states, const char *label)
2490 struct REGEX_INTERNAL_State *s;
2492 struct REGEX_INTERNAL_StateSet_MDLL cls_stack;
2493 struct REGEX_INTERNAL_State *clsstate;
2494 struct REGEX_INTERNAL_State *currentstate;
2495 struct REGEX_INTERNAL_Transition *ctran;
2497 memset (ret, 0, sizeof (struct REGEX_INTERNAL_StateSet));
2501 for (i = 0; i < states->off; i++)
2503 s = states->states[i];
2505 /* Add start state to closure only for epsilon closure */
2507 state_set_append (ret, s);
2509 /* initialize work stack */
2510 cls_stack.head = NULL;
2511 cls_stack.tail = NULL;
2512 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
2515 while (NULL != (currentstate = cls_stack.tail))
2517 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2520 for (ctran = currentstate->transitions_head; NULL != ctran;
2521 ctran = ctran->next)
2523 if (NULL == (clsstate = ctran->to_state))
2525 if (0 != clsstate->contained)
2527 if (0 != nullstrcmp (label, ctran->label))
2529 state_set_append (ret, clsstate);
2530 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2533 clsstate->contained = 1;
2537 for (i = 0; i < ret->off; i++)
2538 ret->states[i]->contained = 0;
2541 qsort (ret->states, ret->off, sizeof (struct REGEX_INTERNAL_State *),
2547 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2549 * @param ctx context
2552 nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx)
2554 struct REGEX_INTERNAL_Automaton *a;
2555 struct REGEX_INTERNAL_Automaton *b;
2556 struct REGEX_INTERNAL_Automaton *new_nfa;
2558 b = ctx->stack_tail;
2559 GNUNET_assert (NULL != b);
2560 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2561 a = ctx->stack_tail;
2562 GNUNET_assert (NULL != a);
2563 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2565 state_add_transition (ctx, a->end, NULL, b->start);
2566 a->end->accepting = 0;
2567 b->end->accepting = 1;
2569 new_nfa = nfa_fragment_create (NULL, NULL);
2570 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2571 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2572 new_nfa->start = a->start;
2573 new_nfa->end = b->end;
2574 new_nfa->state_count += a->state_count + b->state_count;
2575 automaton_fragment_clear (a);
2576 automaton_fragment_clear (b);
2578 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2583 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2585 * @param ctx context
2588 nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2590 struct REGEX_INTERNAL_Automaton *a;
2591 struct REGEX_INTERNAL_Automaton *new_nfa;
2592 struct REGEX_INTERNAL_State *start;
2593 struct REGEX_INTERNAL_State *end;
2595 a = ctx->stack_tail;
2599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2600 "nfa_add_star_op failed, because there was no element on the stack");
2604 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2606 start = nfa_state_create (ctx, 0);
2607 end = nfa_state_create (ctx, 1);
2609 state_add_transition (ctx, start, NULL, a->start);
2610 state_add_transition (ctx, start, NULL, end);
2611 state_add_transition (ctx, a->end, NULL, a->start);
2612 state_add_transition (ctx, a->end, NULL, end);
2614 a->end->accepting = 0;
2617 new_nfa = nfa_fragment_create (start, end);
2618 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2619 automaton_fragment_clear (a);
2621 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2626 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2628 * @param ctx context
2631 nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx)
2633 struct REGEX_INTERNAL_Automaton *a;
2635 a = ctx->stack_tail;
2639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2640 "nfa_add_plus_op failed, because there was no element on the stack");
2644 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2646 state_add_transition (ctx, a->end, NULL, a->start);
2648 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2653 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2655 * @param ctx context
2658 nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2660 struct REGEX_INTERNAL_Automaton *a;
2661 struct REGEX_INTERNAL_Automaton *new_nfa;
2662 struct REGEX_INTERNAL_State *start;
2663 struct REGEX_INTERNAL_State *end;
2665 a = ctx->stack_tail;
2668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2669 "nfa_add_question_op failed, because there was no element on the stack");
2673 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2675 start = nfa_state_create (ctx, 0);
2676 end = nfa_state_create (ctx, 1);
2678 state_add_transition (ctx, start, NULL, a->start);
2679 state_add_transition (ctx, start, NULL, end);
2680 state_add_transition (ctx, a->end, NULL, end);
2682 a->end->accepting = 0;
2684 new_nfa = nfa_fragment_create (start, end);
2685 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2686 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2687 automaton_fragment_clear (a);
2692 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2693 * alternates between a and b (a|b)
2695 * @param ctx context
2698 nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2700 struct REGEX_INTERNAL_Automaton *a;
2701 struct REGEX_INTERNAL_Automaton *b;
2702 struct REGEX_INTERNAL_Automaton *new_nfa;
2703 struct REGEX_INTERNAL_State *start;
2704 struct REGEX_INTERNAL_State *end;
2706 b = ctx->stack_tail;
2707 GNUNET_assert (NULL != b);
2708 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2709 a = ctx->stack_tail;
2710 GNUNET_assert (NULL != a);
2711 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2713 start = nfa_state_create (ctx, 0);
2714 end = nfa_state_create (ctx, 1);
2715 state_add_transition (ctx, start, NULL, a->start);
2716 state_add_transition (ctx, start, NULL, b->start);
2718 state_add_transition (ctx, a->end, NULL, end);
2719 state_add_transition (ctx, b->end, NULL, end);
2721 a->end->accepting = 0;
2722 b->end->accepting = 0;
2725 new_nfa = nfa_fragment_create (start, end);
2726 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2727 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2728 automaton_fragment_clear (a);
2729 automaton_fragment_clear (b);
2731 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2736 * Adds a new nfa fragment to the stack
2738 * @param ctx context
2739 * @param label label for nfa transition
2742 nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label)
2744 struct REGEX_INTERNAL_Automaton *n;
2745 struct REGEX_INTERNAL_State *start;
2746 struct REGEX_INTERNAL_State *end;
2748 GNUNET_assert (NULL != ctx);
2750 start = nfa_state_create (ctx, 0);
2751 end = nfa_state_create (ctx, 1);
2752 state_add_transition (ctx, start, label, end);
2753 n = nfa_fragment_create (start, end);
2754 GNUNET_assert (NULL != n);
2755 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2760 * Initialize a new context
2762 * @param ctx context
2765 REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx)
2769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2773 ctx->transition_id = 0;
2774 ctx->stack_head = NULL;
2775 ctx->stack_tail = NULL;
2780 * Construct an NFA by parsing the regex string of length 'len'.
2782 * @param regex regular expression string
2783 * @param len length of the string
2785 * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton
2787 struct REGEX_INTERNAL_Automaton *
2788 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2790 struct REGEX_INTERNAL_Context ctx;
2791 struct REGEX_INTERNAL_Automaton *nfa;
2796 unsigned int altcount;
2797 unsigned int atomcount;
2806 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2809 "Could not parse regex. Empty regex string provided.\n");
2813 REGEX_INTERNAL_context_init (&ctx);
2824 for (count = 0; count < len && *regexp; count++, regexp++)
2832 nfa_add_concatenation (&ctx);
2835 GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2836 p[poff].altcount = altcount;
2837 p[poff].atomcount = atomcount;
2845 error_msg = "Cannot append '|' to nothing";
2848 while (--atomcount > 0)
2849 nfa_add_concatenation (&ctx);
2855 error_msg = "Missing opening '('";
2860 /* Ignore this: "()" */
2862 altcount = p[poff].altcount;
2863 atomcount = p[poff].atomcount;
2866 while (--atomcount > 0)
2867 nfa_add_concatenation (&ctx);
2868 for (; altcount > 0; altcount--)
2869 nfa_add_alternation (&ctx);
2871 altcount = p[poff].altcount;
2872 atomcount = p[poff].atomcount;
2878 error_msg = "Cannot append '*' to nothing";
2881 nfa_add_star_op (&ctx);
2886 error_msg = "Cannot append '+' to nothing";
2889 nfa_add_plus_op (&ctx);
2894 error_msg = "Cannot append '?' to nothing";
2897 nfa_add_question_op (&ctx);
2903 nfa_add_concatenation (&ctx);
2905 curlabel[0] = *regexp;
2906 nfa_add_label (&ctx, curlabel);
2913 error_msg = "Unbalanced parenthesis";
2916 while (--atomcount > 0)
2917 nfa_add_concatenation (&ctx);
2918 for (; altcount > 0; altcount--)
2919 nfa_add_alternation (&ctx);
2921 GNUNET_array_grow (p, psize, 0);
2923 nfa = ctx.stack_tail;
2924 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2926 if (NULL != ctx.stack_head)
2928 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2932 /* Remember the regex that was used to generate this NFA */
2933 nfa->regex = GNUNET_strdup (regex);
2935 /* create depth-first numbering of the states for pretty printing */
2936 REGEX_INTERNAL_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2938 /* No multistriding added so far */
2939 nfa->is_multistrided = GNUNET_NO;
2944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2945 if (NULL != error_msg)
2946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2948 GNUNET_free_non_null (p);
2950 while (NULL != (nfa = ctx.stack_head))
2952 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2953 REGEX_INTERNAL_automaton_destroy (nfa);
2961 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2963 * @param ctx context.
2964 * @param nfa NFA automaton.
2965 * @param dfa DFA automaton.
2966 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2970 construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
2971 struct REGEX_INTERNAL_Automaton *nfa,
2972 struct REGEX_INTERNAL_Automaton *dfa,
2973 struct REGEX_INTERNAL_State *dfa_state)
2975 struct REGEX_INTERNAL_Transition *ctran;
2976 struct REGEX_INTERNAL_State *new_dfa_state;
2977 struct REGEX_INTERNAL_State *state_contains;
2978 struct REGEX_INTERNAL_State *state_iter;
2979 struct REGEX_INTERNAL_StateSet tmp;
2980 struct REGEX_INTERNAL_StateSet nfa_set;
2982 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2984 if (NULL == ctran->label || NULL != ctran->to_state)
2987 nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label);
2988 nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL);
2989 state_set_clear (&tmp);
2991 state_contains = NULL;
2992 for (state_iter = dfa->states_head; NULL != state_iter;
2993 state_iter = state_iter->next)
2995 if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set))
2997 state_contains = state_iter;
3001 if (NULL == state_contains)
3003 new_dfa_state = dfa_state_create (ctx, &nfa_set);
3004 automaton_add_state (dfa, new_dfa_state);
3005 ctran->to_state = new_dfa_state;
3006 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
3010 ctran->to_state = state_contains;
3011 state_set_clear (&nfa_set);
3018 * Construct DFA for the given 'regex' of length 'len'.
3020 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
3021 * compressed to o -> abc -> o. Note that this parameter influences the
3022 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
3023 * edges with the same label). For example for an application that stores IPv4
3024 * addresses as bitstrings it could make sense to limit the path compression to
3027 * @param regex regular expression string.
3028 * @param len length of the regular expression.
3029 * @param max_path_len limit the path compression length to the
3030 * given value. If set to 1, no path compression is applied. Set to 0 for
3031 * maximal possible path compression (generally not desireable).
3032 * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
3034 struct REGEX_INTERNAL_Automaton *
3035 REGEX_INTERNAL_construct_dfa (const char *regex, const size_t len,
3036 unsigned int max_path_len)
3038 struct REGEX_INTERNAL_Context ctx;
3039 struct REGEX_INTERNAL_Automaton *dfa;
3040 struct REGEX_INTERNAL_Automaton *nfa;
3041 struct REGEX_INTERNAL_StateSet nfa_start_eps_cls;
3042 struct REGEX_INTERNAL_StateSet singleton_set;
3044 REGEX_INTERNAL_context_init (&ctx);
3047 nfa = REGEX_INTERNAL_construct_nfa (regex, len);
3051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3052 "Could not create DFA, because NFA creation failed\n");
3056 dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton);
3058 dfa->regex = GNUNET_strdup (regex);
3060 /* Create DFA start state from epsilon closure */
3061 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3062 state_set_append (&singleton_set, nfa->start);
3063 nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL);
3064 state_set_clear (&singleton_set);
3065 dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls);
3066 automaton_add_state (dfa, dfa->start);
3068 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
3069 REGEX_INTERNAL_automaton_destroy (nfa);
3072 if (GNUNET_OK != dfa_minimize (&ctx, dfa))
3074 REGEX_INTERNAL_automaton_destroy (dfa);
3078 /* Create proofs and hashes for all states */
3079 if (GNUNET_OK != automaton_create_proofs (dfa))
3081 REGEX_INTERNAL_automaton_destroy (dfa);
3085 /* Compress linear DFA paths */
3086 if (1 != max_path_len)
3087 dfa_compress_paths (&ctx, dfa, max_path_len);
3094 * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton data
3097 * @param a automaton to be destroyed
3100 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3102 struct REGEX_INTERNAL_State *s;
3103 struct REGEX_INTERNAL_State *next_state;
3108 GNUNET_free_non_null (a->regex);
3109 GNUNET_free_non_null (a->canonical_regex);
3111 for (s = a->states_head; NULL != s; s = next_state)
3113 next_state = s->next;
3114 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
3115 automaton_destroy_state (s);
3123 * Evaluates the given string using the given DFA automaton
3125 * @param a automaton, type must be DFA
3126 * @param string string that should be evaluated
3128 * @return 0 if string matches, non-0 otherwise
3131 evaluate_dfa (struct REGEX_INTERNAL_Automaton *a,
3135 struct REGEX_INTERNAL_State *s;
3136 unsigned int step_len;
3140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3141 "Tried to evaluate DFA, but NFA automaton given");
3147 /* If the string is empty but the starting state is accepting, we accept. */
3148 if ((NULL == string || 0 == strlen (string)) && s->accepting)
3151 for (strp = string; NULL != strp && *strp; strp += step_len)
3153 step_len = dfa_move (&s, strp);
3159 if (NULL != s && s->accepting)
3167 * Evaluates the given string using the given NFA automaton
3169 * @param a automaton, type must be NFA
3170 * @param string string that should be evaluated
3171 * @return 0 if string matches, non-0 otherwise
3174 evaluate_nfa (struct REGEX_INTERNAL_Automaton *a,
3179 struct REGEX_INTERNAL_State *s;
3180 struct REGEX_INTERNAL_StateSet sset;
3181 struct REGEX_INTERNAL_StateSet new_sset;
3182 struct REGEX_INTERNAL_StateSet singleton_set;
3188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3189 "Tried to evaluate NFA, but DFA automaton given");
3193 /* If the string is empty but the starting state is accepting, we accept. */
3194 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
3198 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3199 state_set_append (&singleton_set, a->start);
3200 nfa_closure_set_create (&sset, a, &singleton_set, NULL);
3201 state_set_clear (&singleton_set);
3204 for (strp = string; NULL != strp && *strp; strp++)
3207 nfa_closure_set_create (&new_sset, a, &sset, str);
3208 state_set_clear (&sset);
3209 nfa_closure_set_create (&sset, a, &new_sset, 0);
3210 state_set_clear (&new_sset);
3213 for (i = 0; i < sset.off; i++)
3216 if ( (NULL != s) && (s->accepting) )
3223 state_set_clear (&sset);
3229 * Evaluates the given @a string against the given compiled regex @a a
3231 * @param a automaton
3232 * @param string string to check
3233 * @return 0 if string matches, non-0 otherwise
3236 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
3244 result = evaluate_dfa (a, string);
3247 result = evaluate_nfa (a, string);
3250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3251 "Evaluating regex failed, automaton has no type!\n");
3252 result = GNUNET_SYSERR;
3261 * Get the canonical regex of the given automaton.
3262 * When constructing the automaton a proof is computed for each state,
3263 * consisting of the regular expression leading to this state. A complete
3264 * regex for the automaton can be computed by combining these proofs.
3265 * As of now this function is only useful for testing.
3267 * @param a automaton for which the canonical regex should be returned.
3272 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
3277 return a->canonical_regex;
3282 * Get the number of transitions that are contained in the given automaton.
3284 * @param a automaton for which the number of transitions should be returned.
3286 * @return number of transitions in the given automaton.
3289 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
3291 unsigned int t_count;
3292 struct REGEX_INTERNAL_State *s;
3298 for (s = a->states_head; NULL != s; s = s->next)
3299 t_count += s->transition_count;
3306 * Get the first key for the given @a input_string. This hashes the first x bits
3307 * of the @a input_string.
3309 * @param input_string string.
3310 * @param string_len length of the @a input_string.
3311 * @param key pointer to where to write the hash code.
3312 * @return number of bits of @a input_string that have been consumed
3313 * to construct the key
3316 REGEX_INTERNAL_get_first_key (const char *input_string,
3318 struct GNUNET_HashCode *key)
3322 size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len :
3323 GNUNET_REGEX_INITIAL_BYTES;
3324 if (NULL == input_string)
3326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3327 "Given input string was NULL!\n");
3330 GNUNET_CRYPTO_hash (input_string, size, key);
3337 * Recursive function that calls the iterator for each synthetic start state.
3339 * @param min_len minimum length of the path in the graph.
3340 * @param max_len maximum length of the path in the graph.
3341 * @param consumed_string string consumed by traversing the graph till this state.
3342 * @param state current state of the automaton.
3343 * @param iterator iterator function called for each edge.
3344 * @param iterator_cls closure for the @a iterator function.
3347 iterate_initial_edge (unsigned int min_len,
3348 unsigned int max_len,
3349 char *consumed_string,
3350 struct REGEX_INTERNAL_State *state,
3351 REGEX_INTERNAL_KeyIterator iterator,
3355 struct REGEX_INTERNAL_Transition *t;
3356 unsigned int num_edges = state->transition_count;
3357 struct REGEX_BLOCK_Edge edges[num_edges];
3358 struct REGEX_BLOCK_Edge edge[1];
3359 struct GNUNET_HashCode hash;
3360 struct GNUNET_HashCode hash_new;
3361 unsigned int cur_len;
3363 if (NULL != consumed_string)
3364 cur_len = strlen (consumed_string);
3368 if ( ( (cur_len >= min_len) ||
3369 (GNUNET_YES == state->accepting) ) &&
3371 (NULL != consumed_string) )
3373 if (cur_len <= max_len)
3375 if ( (NULL != state->proof) &&
3376 (0 != strcmp (consumed_string,
3379 (void) state_get_edges (state, edges);
3380 GNUNET_CRYPTO_hash (consumed_string,
3381 strlen (consumed_string),
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 "Start state for string `%s' is %s\n",
3386 GNUNET_h2s (&hash));
3387 iterator (iterator_cls,
3394 if ( (GNUNET_YES == state->accepting) &&
3396 (state->transition_count < 1) &&
3397 (cur_len < max_len) )
3399 /* Special case for regex consisting of just a string that is shorter than
3401 edge[0].label = &consumed_string[cur_len - 1];
3402 edge[0].destination = state->hash;
3403 temp = GNUNET_strdup (consumed_string);
3404 temp[cur_len - 1] = '\0';
3405 GNUNET_CRYPTO_hash (temp,
3408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3409 "Start state for short string `%s' is %s\n",
3411 GNUNET_h2s (&hash_new));
3412 iterator (iterator_cls,
3420 else /* cur_len > max_len */
3422 /* Case where the concatenated labels are longer than max_len, then split. */
3423 edge[0].label = &consumed_string[max_len];
3424 edge[0].destination = state->hash;
3425 temp = GNUNET_strdup (consumed_string);
3426 temp[max_len] = '\0';
3427 GNUNET_CRYPTO_hash (temp, max_len, &hash);
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3429 "Start state at split edge `%s'-`%s` is %s\n",
3432 GNUNET_h2s (&hash_new));
3433 iterator (iterator_cls,
3443 if (cur_len < max_len)
3445 for (t = state->transitions_head; NULL != t; t = t->next)
3447 if (NULL != strchr (t->label,
3450 /* Wildcards not allowed during starting states */
3454 if (NULL != consumed_string)
3455 GNUNET_asprintf (&temp,
3460 GNUNET_asprintf (&temp,
3463 iterate_initial_edge (min_len,
3476 * Iterate over all edges starting from start state of automaton 'a'. Calling
3477 * iterator for each edge.
3479 * @param a automaton.
3480 * @param iterator iterator called for each edge.
3481 * @param iterator_cls closure.
3484 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
3485 REGEX_INTERNAL_KeyIterator iterator,
3488 struct REGEX_INTERNAL_State *s;
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 "Iterating over starting edges\n");
3492 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES,
3493 GNUNET_REGEX_INITIAL_BYTES,
3495 iterator, iterator_cls);
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3497 "Iterating over DFA edges\n");
3498 for (s = a->states_head; NULL != s; s = s->next)
3500 struct REGEX_BLOCK_Edge edges[s->transition_count];
3501 unsigned int num_edges;
3503 num_edges = state_get_edges (s, edges);
3504 if ( ( (NULL != s->proof) &&
3505 (0 < strlen (s->proof)) ) || s->accepting)
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3508 "Creating DFA edges at `%s' under key %s\n",
3510 GNUNET_h2s (&s->hash));
3511 iterator (iterator_cls, &s->hash, s->proof,
3515 s->marked = GNUNET_NO;
3521 * Struct to hold all the relevant state information in the HashMap.
3523 * Contains the same info as the Regex Iterator parametes except the key,
3524 * which comes directly from the HashMap iterator.
3526 struct temporal_state_store {
3531 struct REGEX_BLOCK_Edge *edges;
3536 * Store regex iterator and cls in one place to pass to the hashmap iterator.
3538 struct client_iterator {
3539 REGEX_INTERNAL_KeyIterator iterator;
3545 * Iterator over all edges of a dfa. Stores all of them in a HashMap
3546 * for later reachability marking.
3548 * @param cls Closure (HashMap)
3549 * @param key hash for current state.
3550 * @param proof proof for current state
3551 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
3552 * @param num_edges number of edges leaving current state.
3553 * @param edges edges leaving current state.
3556 store_all_states (void *cls,
3557 const struct GNUNET_HashCode *key,
3560 unsigned int num_edges,
3561 const struct REGEX_BLOCK_Edge *edges)
3563 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3564 struct temporal_state_store *tmp;
3567 tmp = GNUNET_new (struct temporal_state_store);
3568 tmp->reachable = GNUNET_NO;
3569 tmp->proof = GNUNET_strdup (proof);
3570 tmp->accepting = accepting;
3571 tmp->num_edges = num_edges;
3572 edges_size = sizeof (struct REGEX_BLOCK_Edge) * num_edges;
3573 tmp->edges = GNUNET_malloc (edges_size);
3574 GNUNET_memcpy(tmp->edges, edges, edges_size);
3575 GNUNET_CONTAINER_multihashmap_put (hm, key, tmp,
3576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3581 * Mark state as reachable and call recursively on all its edges.
3583 * If already marked as reachable, do nothing.
3585 * @param state State to mark as reachable.
3586 * @param hm HashMap which stores all the states indexed by key.
3589 mark_as_reachable (struct temporal_state_store *state,
3590 struct GNUNET_CONTAINER_MultiHashMap *hm)
3592 struct temporal_state_store *child;
3595 if (GNUNET_YES == state->reachable)
3599 state->reachable = GNUNET_YES;
3600 for (i = 0; i < state->num_edges; i++)
3602 child = GNUNET_CONTAINER_multihashmap_get (hm,
3603 &state->edges[i].destination);
3609 mark_as_reachable (child, hm);
3615 * Iterator over hash map entries to mark the ones that are reachable.
3617 * @param cls closure
3618 * @param key current key code
3619 * @param value value in the hash map
3620 * @return #GNUNET_YES if we should continue to iterate,
3621 * #GNUNET_NO if not.
3624 reachability_iterator (void *cls,
3625 const struct GNUNET_HashCode *key,
3628 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3629 struct temporal_state_store *state = value;
3631 if (GNUNET_YES == state->reachable)
3632 /* already visited and marked */
3635 if (GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof) &&
3636 GNUNET_NO == state->accepting)
3637 /* not directly reachable */
3640 mark_as_reachable (state, hm);
3646 * Iterator over hash map entries.
3647 * Calling the callback on the ones marked as reachables.
3649 * @param cls closure
3650 * @param key current key code
3651 * @param value value in the hash map
3652 * @return #GNUNET_YES if we should continue to iterate,
3653 * #GNUNET_NO if not.
3656 iterate_reachables (void *cls,
3657 const struct GNUNET_HashCode *key,
3660 struct client_iterator *ci = cls;
3661 struct temporal_state_store *state = value;
3663 if (GNUNET_YES == state->reachable)
3665 ci->iterator (ci->iterator_cls, key,
3666 state->proof, state->accepting,
3667 state->num_edges, state->edges);
3669 GNUNET_free (state->edges);
3670 GNUNET_free (state->proof);
3671 GNUNET_free (state);
3677 * Iterate over all edges of automaton 'a' that are reachable from a state with
3678 * a proof of at least GNUNET_REGEX_INITIAL_BYTES characters.
3680 * Call the iterator for each such edge.
3682 * @param a automaton.
3683 * @param iterator iterator called for each reachable edge.
3684 * @param iterator_cls closure.
3687 REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a,
3688 REGEX_INTERNAL_KeyIterator iterator,
3691 struct GNUNET_CONTAINER_MultiHashMap *hm;
3692 struct client_iterator ci;
3694 hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO);
3695 ci.iterator = iterator;
3696 ci.iterator_cls = iterator_cls;
3698 REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm);
3699 GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm);
3700 GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci);
3702 GNUNET_CONTAINER_multihashmap_destroy (hm);
3706 /* end of regex_internal.c */