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.
16 * @file src/regex/regex_internal.c
17 * @brief library to create Deterministic Finite Automatons (DFAs) from regular
18 * expressions (regexes).
19 * @author Maximilian Szengel
22 #include "gnunet_util_lib.h"
23 #include "gnunet_regex_service.h"
24 #include "regex_internal_lib.h"
25 #include "regex_internal.h"
29 * Set this to #GNUNET_YES to enable state naming. Used to debug NFA->DFA
30 * creation. Disabled by default for better performance.
32 #define REGEX_DEBUG_DFA GNUNET_NO
35 * Set of states using MDLL API.
37 struct REGEX_INTERNAL_StateSet_MDLL
42 struct REGEX_INTERNAL_State *head;
47 struct REGEX_INTERNAL_State *tail;
57 * Append state to the given StateSet.
59 * @param set set to be modified
60 * @param state state to be appended
63 state_set_append (struct REGEX_INTERNAL_StateSet *set,
64 struct REGEX_INTERNAL_State *state)
66 if (set->off == set->size)
67 GNUNET_array_grow (set->states, set->size, set->size * 2 + 4);
68 set->states[set->off++] = state;
73 * Compare two strings for equality. If either is NULL they are not equal.
75 * @param str1 first string for comparison.
76 * @param str2 second string for comparison.
78 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
81 nullstrcmp (const char *str1, const char *str2)
83 if ((NULL == str1) != (NULL == str2))
85 if ((NULL == str1) && (NULL == str2))
88 return strcmp (str1, str2);
93 * Adds a transition from one state to another on @a label. Does not add
97 * @param from_state starting state for the transition
98 * @param label transition label
99 * @param to_state state to where the transition should point to
102 state_add_transition (struct REGEX_INTERNAL_Context *ctx,
103 struct REGEX_INTERNAL_State *from_state,
105 struct REGEX_INTERNAL_State *to_state)
107 struct REGEX_INTERNAL_Transition *t;
108 struct REGEX_INTERNAL_Transition *oth;
110 if (NULL == from_state)
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113 "Could not create Transition.\n");
117 /* Do not add duplicate state transitions */
118 for (t = from_state->transitions_head; NULL != t; t = t->next)
120 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) &&
121 t->from_state == from_state)
125 /* sort transitions by label */
126 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
128 if (0 < nullstrcmp (oth->label, label))
132 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
134 t->id = ctx->transition_id++;
136 t->label = GNUNET_strdup (label);
139 t->to_state = to_state;
140 t->from_state = from_state;
142 /* Add outgoing transition to 'from_state' */
143 from_state->transition_count++;
144 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head,
145 from_state->transitions_tail, oth, t);
150 * Remove a 'transition' from 'state'.
152 * @param state state from which the to-be-removed transition originates.
153 * @param transition transition that should be removed from state 'state'.
156 state_remove_transition (struct REGEX_INTERNAL_State *state,
157 struct REGEX_INTERNAL_Transition *transition)
159 if (NULL == state || NULL == transition)
162 if (transition->from_state != state)
165 GNUNET_free_non_null (transition->label);
167 state->transition_count--;
168 GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
171 GNUNET_free (transition);
176 * Compare two states. Used for sorting.
178 * @param a first state
179 * @param b second state
181 * @return an integer less than, equal to, or greater than zero
182 * if the first argument is considered to be respectively
183 * less than, equal to, or greater than the second.
186 state_compare (const void *a, const void *b)
188 struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a;
189 struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b;
191 return (*s1)->id - (*s2)->id;
196 * Get all edges leaving state @a s.
199 * @param edges all edges leaving @a s, expected to be allocated and have enough
200 * space for `s->transitions_count` elements.
202 * @return number of edges.
205 state_get_edges (struct REGEX_INTERNAL_State *s,
206 struct REGEX_BLOCK_Edge *edges)
208 struct REGEX_INTERNAL_Transition *t;
216 for (t = s->transitions_head; NULL != t; t = t->next)
218 if (NULL != t->to_state)
220 edges[count].label = t->label;
221 edges[count].destination = t->to_state->hash;
230 * Compare to state sets by comparing the id's of the states that are contained
231 * in each set. Both sets are expected to be sorted by id!
233 * @param sset1 first state set
234 * @param sset2 second state set
235 * @return 0 if the sets are equal, otherwise non-zero
238 state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
239 struct REGEX_INTERNAL_StateSet *sset2)
244 if (NULL == sset1 || NULL == sset2)
247 result = sset1->off - sset2->off;
252 for (i = 0; i < sset1->off; i++)
253 if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i])))
260 * Clears the given StateSet 'set'
262 * @param set set to be cleared
265 state_set_clear (struct REGEX_INTERNAL_StateSet *set)
267 GNUNET_array_grow (set->states, set->size, 0);
273 * Clears an automaton fragment. Does not destroy the states inside the
276 * @param a automaton to be cleared
279 automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
286 a->states_head = NULL;
287 a->states_tail = NULL;
294 * Frees the memory used by State @a s
296 * @param s state that should be destroyed
299 automaton_destroy_state (struct REGEX_INTERNAL_State *s)
301 struct REGEX_INTERNAL_Transition *t;
302 struct REGEX_INTERNAL_Transition *next_t;
307 GNUNET_free_non_null (s->name);
308 GNUNET_free_non_null (s->proof);
309 state_set_clear (&s->nfa_set);
310 for (t = s->transitions_head; NULL != t; t = next_t)
313 state_remove_transition (s, t);
321 * Remove a state from the given automaton 'a'. Always use this function when
322 * altering the states of an automaton. Will also remove all transitions leading
323 * to this state, before destroying it.
326 * @param s state to remove
329 automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
330 struct REGEX_INTERNAL_State *s)
332 struct REGEX_INTERNAL_State *s_check;
333 struct REGEX_INTERNAL_Transition *t_check;
334 struct REGEX_INTERNAL_Transition *t_check_next;
336 if (NULL == a || NULL == s)
339 /* remove all transitions leading to this state */
340 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
342 for (t_check = s_check->transitions_head; NULL != t_check;
343 t_check = t_check_next)
345 t_check_next = t_check->next;
346 if (t_check->to_state == s)
347 state_remove_transition (s_check, t_check);
352 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
355 automaton_destroy_state (s);
360 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy
361 * 's2'. 's1' will contain all (non-duplicate) outgoing transitions of 's2'.
365 * @param s1 first state
366 * @param s2 second state, will be destroyed
369 automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
370 struct REGEX_INTERNAL_Automaton *a,
371 struct REGEX_INTERNAL_State *s1,
372 struct REGEX_INTERNAL_State *s2)
374 struct REGEX_INTERNAL_State *s_check;
375 struct REGEX_INTERNAL_Transition *t_check;
376 struct REGEX_INTERNAL_Transition *t;
377 struct REGEX_INTERNAL_Transition *t_next;
383 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
384 * does not already exists, if it already exists remove transition. */
385 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
387 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
389 t_next = t_check->next;
391 if (s2 == t_check->to_state)
394 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
396 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label))
399 if (GNUNET_NO == is_dup)
400 t_check->to_state = s1;
402 state_remove_transition (t_check->from_state, t_check);
407 /* 2. Add all transitions from s2 to sX to s1 */
408 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
410 if (t_check->to_state != s1)
411 state_add_transition (ctx, s1, t_check->label, t_check->to_state);
414 /* 3. Rename s1 to {s1,s2} */
419 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name);
420 GNUNET_free (new_name);
424 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2);
426 automaton_destroy_state (s2);
431 * Add a state to the automaton 'a', always use this function to alter the
432 * states DLL of the automaton.
434 * @param a automaton to add the state to
435 * @param s state that should be added
438 automaton_add_state (struct REGEX_INTERNAL_Automaton *a,
439 struct REGEX_INTERNAL_State *s)
441 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
447 * Depth-first traversal (DFS) of all states that are reachable from state
448 * 's'. Performs 'action' on each visited state.
450 * @param s start state.
451 * @param marks an array of size a->state_count to remember which state was
453 * @param count current count of the state.
454 * @param check function that is checked before advancing on each transition
456 * @param check_cls closure for check.
457 * @param action action to be performed on each state.
458 * @param action_cls closure for action.
461 automaton_state_traverse (struct REGEX_INTERNAL_State *s, int *marks,
463 REGEX_INTERNAL_traverse_check check, void *check_cls,
464 REGEX_INTERNAL_traverse_action action, void *action_cls)
466 struct REGEX_INTERNAL_Transition *t;
468 if (GNUNET_YES == marks[s->traversal_id])
471 marks[s->traversal_id] = GNUNET_YES;
474 action (action_cls, *count, s);
478 for (t = s->transitions_head; NULL != t; t = t->next)
481 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
483 automaton_state_traverse (t->to_state, marks, count, check, check_cls,
491 * Traverses the given automaton using depth-first-search (DFS) from it's start
492 * state, visiting all reachable states and calling 'action' on each one of
495 * @param a automaton to be traversed.
496 * @param start start state, pass a->start or NULL to traverse the whole automaton.
497 * @param check function that is checked before advancing on each transition
499 * @param check_cls closure for @a check.
500 * @param action action to be performed on each state.
501 * @param action_cls closure for @a action
504 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
505 struct REGEX_INTERNAL_State *start,
506 REGEX_INTERNAL_traverse_check check,
508 REGEX_INTERNAL_traverse_action action,
512 struct REGEX_INTERNAL_State *s;
514 if (NULL == a || 0 == a->state_count)
517 int marks[a->state_count];
519 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
520 s = s->next, count++)
522 s->traversal_id = count;
523 marks[s->traversal_id] = GNUNET_NO;
533 automaton_state_traverse (s, marks, &count,
540 * String container for faster string operations.
545 * Buffer holding the string (may start in the middle!);
556 * Length of the string in the buffer.
561 * Number of bytes allocated for @e sbuf
566 * Buffer currently represents "NULL" (not the empty string!)
571 * If this entry is part of the last/current generation array,
572 * this flag is #GNUNET_YES if the last and current generation are
573 * identical (and thus copying is unnecessary if the value didn't
574 * change). This is used in an optimization that improves
575 * performance by about 1% --- if we use int16_t here. With just
576 * "int" for both flags, performance drops (on my system) significantly,
577 * most likely due to increased cache misses.
585 * Compare two strings for equality. If either is NULL they are not equal.
587 * @param s1 first string for comparison.
588 * @param s2 second string for comparison.
590 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
593 sb_nullstrcmp (const struct StringBuffer *s1,
594 const struct StringBuffer *s2)
596 if ( (GNUNET_YES == s1->null_flag) &&
597 (GNUNET_YES == s2->null_flag) )
599 if ( (GNUNET_YES == s1->null_flag) ||
600 (GNUNET_YES == s2->null_flag) )
602 if (s1->slen != s2->slen)
606 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
611 * Compare two strings for equality.
613 * @param s1 first string for comparison.
614 * @param s2 second string for comparison.
616 * @return 0 if the strings are the same, 1 or -1 if not.
619 sb_strcmp (const struct StringBuffer *s1,
620 const struct StringBuffer *s2)
622 if (s1->slen != s2->slen)
626 return memcmp (s1->sbuf, s2->sbuf, s1->slen);
631 * Reallocate the buffer of 'ret' to fit 'nlen' characters;
632 * move the existing string to the beginning of the new buffer.
634 * @param ret current buffer, to be updated
635 * @param nlen target length for the buffer, must be at least ret->slen
638 sb_realloc (struct StringBuffer *ret,
643 GNUNET_assert (nlen >= ret->slen);
645 ret->abuf = GNUNET_malloc (nlen);
647 GNUNET_memcpy (ret->abuf,
650 ret->sbuf = ret->abuf;
651 GNUNET_free_non_null (old);
658 * @param ret where to write the result
659 * @param sarg string to append
662 sb_append (struct StringBuffer *ret,
663 const struct StringBuffer *sarg)
665 if (GNUNET_YES == ret->null_flag)
667 ret->null_flag = GNUNET_NO;
668 if (ret->blen < sarg->slen + ret->slen)
669 sb_realloc (ret, ret->blen + sarg->slen + 128);
670 GNUNET_memcpy (&ret->sbuf[ret->slen],
673 ret->slen += sarg->slen;
680 * @param ret where to write the result
681 * @param cstr string to append
684 sb_append_cstr (struct StringBuffer *ret,
687 size_t cstr_len = strlen (cstr);
689 if (GNUNET_YES == ret->null_flag)
691 ret->null_flag = GNUNET_NO;
692 if (ret->blen < cstr_len + ret->slen)
693 sb_realloc (ret, ret->blen + cstr_len + 128);
694 GNUNET_memcpy (&ret->sbuf[ret->slen],
697 ret->slen += cstr_len;
702 * Wrap a string buffer, that is, set ret to the format string
703 * which contains an "%s" which is to be replaced with the original
704 * content of 'ret'. Note that optimizing this function is not
705 * really worth it, it is rarely called.
707 * @param ret where to write the result and take the input for %.*s from
708 * @param format format string, fprintf-style, with exactly one "%.*s"
709 * @param extra_chars how long will the result be, in addition to 'sarg' length
712 sb_wrap (struct StringBuffer *ret,
718 if (GNUNET_YES == ret->null_flag)
720 ret->null_flag = GNUNET_NO;
721 temp = GNUNET_malloc (ret->slen + extra_chars + 1);
722 GNUNET_snprintf (temp,
723 ret->slen + extra_chars + 1,
727 GNUNET_free_non_null (ret->abuf);
730 ret->blen = ret->slen + extra_chars + 1;
731 ret->slen = ret->slen + extra_chars;
736 * Format a string buffer. Note that optimizing this function is not
737 * really worth it, it is rarely called.
739 * @param ret where to write the result
740 * @param format format string, fprintf-style, with exactly one "%.*s"
741 * @param extra_chars how long will the result be, in addition to 'sarg' length
742 * @param sarg string to print into the format
745 sb_printf1 (struct StringBuffer *ret,
748 const struct StringBuffer *sarg)
750 if (ret->blen < sarg->slen + extra_chars + 1)
752 sarg->slen + extra_chars + 1);
753 ret->null_flag = GNUNET_NO;
754 ret->sbuf = ret->abuf;
755 ret->slen = sarg->slen + extra_chars;
756 GNUNET_snprintf (ret->sbuf,
765 * Format a string buffer.
767 * @param ret where to write the result
768 * @param format format string, fprintf-style, with exactly two "%.*s"
769 * @param extra_chars how long will the result be, in addition to 'sarg1/2' length
770 * @param sarg1 first string to print into the format
771 * @param sarg2 second string to print into the format
774 sb_printf2 (struct StringBuffer *ret,
777 const struct StringBuffer *sarg1,
778 const struct StringBuffer *sarg2)
780 if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1)
782 sarg1->slen + sarg2->slen + extra_chars + 1);
783 ret->null_flag = GNUNET_NO;
784 ret->slen = sarg1->slen + sarg2->slen + extra_chars;
785 ret->sbuf = ret->abuf;
786 GNUNET_snprintf (ret->sbuf,
797 * Format a string buffer. Note that optimizing this function is not
798 * really worth it, it is rarely called.
800 * @param ret where to write the result
801 * @param format format string, fprintf-style, with exactly three "%.*s"
802 * @param extra_chars how long will the result be, in addition to 'sarg1/2/3' length
803 * @param sarg1 first string to print into the format
804 * @param sarg2 second string to print into the format
805 * @param sarg3 third string to print into the format
808 sb_printf3 (struct StringBuffer *ret,
811 const struct StringBuffer *sarg1,
812 const struct StringBuffer *sarg2,
813 const struct StringBuffer *sarg3)
815 if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1)
817 sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1);
818 ret->null_flag = GNUNET_NO;
819 ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars;
820 ret->sbuf = ret->abuf;
821 GNUNET_snprintf (ret->sbuf,
834 * Free resources of the given string buffer.
836 * @param sb buffer to free (actual pointer is not freed, as they
837 * should not be individually allocated)
840 sb_free (struct StringBuffer *sb)
842 GNUNET_array_grow (sb->abuf,
847 sb->null_flag= GNUNET_YES;
852 * Copy the given string buffer from 'in' to 'out'.
854 * @param in input string
855 * @param out output string
858 sb_strdup (struct StringBuffer *out,
859 const struct StringBuffer *in)
862 out->null_flag = in->null_flag;
863 if (GNUNET_YES == out->null_flag)
865 if (out->blen < in->slen)
867 GNUNET_array_grow (out->abuf,
871 out->sbuf = out->abuf;
872 out->slen = in->slen;
873 GNUNET_memcpy (out->sbuf, in->sbuf, out->slen);
878 * Copy the given string buffer from 'in' to 'out'.
880 * @param cstr input string
881 * @param out output string
884 sb_strdup_cstr (struct StringBuffer *out,
889 out->null_flag = GNUNET_YES;
892 out->null_flag = GNUNET_NO;
893 out->slen = strlen (cstr);
894 if (out->blen < out->slen)
896 GNUNET_array_grow (out->abuf,
900 out->sbuf = out->abuf;
901 GNUNET_memcpy (out->sbuf, cstr, out->slen);
906 * Check if the given string @a str needs parentheses around it when
907 * using it to generate a regex.
911 * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise
914 needs_parentheses (const struct StringBuffer *str)
923 if ((GNUNET_YES == str->null_flag) || ((slen = str->slen) < 2))
928 end = str->sbuf + slen;
933 cl = memchr (pos, ')', end - pos);
939 /* while '(' before ')', count opening parens */
940 while ( (NULL != (op = memchr (pos, '(', end - pos))) &&
950 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
955 * Remove parentheses surrounding string @a str.
956 * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
957 * You need to #GNUNET_free() the returned string.
959 * @param str string, modified to contain a
960 * @return string without surrounding parentheses, string 'str' if no preceding
961 * epsilon could be found, NULL if 'str' was NULL
964 remove_parentheses (struct StringBuffer *str)
977 if ( (GNUNET_YES == str->null_flag) ||
978 (1 >= (slen = str->slen)) ||
979 ('(' != str->sbuf[0]) ||
980 (')' != str->sbuf[slen - 1]) )
984 end = &sbuf[slen - 1];
985 op = memchr (pos, '(', end - pos);
986 cp = memchr (pos, ')', end - pos);
989 while ( (NULL != op) &&
994 op = memchr (pos, '(', end - pos);
996 while ( (NULL != cp) &&
1001 return; /* can't strip parens */
1004 cp = memchr (pos, ')', end - pos);
1018 * Check if the string 'str' starts with an epsilon (empty string).
1019 * Example: "(|a)" is starting with an epsilon.
1021 * @param str string to test
1023 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
1026 has_epsilon (const struct StringBuffer *str)
1029 (GNUNET_YES != str->null_flag) &&
1031 ('(' == str->sbuf[0]) &&
1032 ('|' == str->sbuf[1]) &&
1033 (')' == str->sbuf[str->slen - 1]);
1038 * Remove an epsilon from the string str. Where epsilon is an empty string
1039 * Example: str = "(|a|b|c)", result: "a|b|c"
1040 * The returned string needs to be freed.
1042 * @param str original string
1043 * @param ret where to return string without preceding epsilon, string 'str' if no preceding
1044 * epsilon could be found, NULL if 'str' was NULL
1047 remove_epsilon (const struct StringBuffer *str,
1048 struct StringBuffer *ret)
1050 if (GNUNET_YES == str->null_flag)
1052 ret->null_flag = GNUNET_YES;
1055 if ( (str->slen > 1) &&
1056 ('(' == str->sbuf[0]) &&
1057 ('|' == str->sbuf[1]) &&
1058 (')' == str->sbuf[str->slen - 1]) )
1060 /* remove epsilon */
1061 if (ret->blen < str->slen - 3)
1063 GNUNET_array_grow (ret->abuf,
1067 ret->sbuf = ret->abuf;
1068 ret->slen = str->slen - 3;
1069 GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen);
1072 sb_strdup (ret, str);
1077 * Compare n bytes of 'str1' and 'str2'
1079 * @param str1 first string to compare
1080 * @param str2 second string for comparison
1081 * @param n number of bytes to compare
1083 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1086 sb_strncmp (const struct StringBuffer *str1,
1087 const struct StringBuffer *str2, size_t n)
1091 if ( (str1->slen != str2->slen) &&
1092 ( (str1->slen < n) ||
1093 (str2->slen < n) ) )
1095 max = GNUNET_MAX (str1->slen, str2->slen);
1098 return memcmp (str1->sbuf, str2->sbuf, max);
1103 * Compare n bytes of 'str1' and 'str2'
1105 * @param str1 first string to compare
1106 * @param str2 second C string for comparison
1107 * @param n number of bytes to compare (and length of str2)
1109 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1112 sb_strncmp_cstr (const struct StringBuffer *str1,
1113 const char *str2, size_t n)
1117 return memcmp (str1->sbuf, str2, n);
1122 * Initialize string buffer for storing strings of up to n
1125 * @param sb buffer to initialize
1126 * @param n desired target length
1129 sb_init (struct StringBuffer *sb,
1132 sb->null_flag = GNUNET_NO;
1133 sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n);
1140 * Compare 'str1', starting from position 'k', with whole 'str2'
1142 * @param str1 first string to compare, starting from position 'k'
1143 * @param str2 second string for comparison
1144 * @param k starting position in 'str1'
1146 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1149 sb_strkcmp (const struct StringBuffer *str1,
1150 const struct StringBuffer *str2, size_t k)
1152 if ( (GNUNET_YES == str1->null_flag) ||
1153 (GNUNET_YES == str2->null_flag) ||
1155 (str1->slen - k != str2->slen) )
1157 return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen);
1162 * Helper function used as 'action' in 'REGEX_INTERNAL_automaton_traverse'
1163 * function to create the depth-first numbering of the states.
1165 * @param cls states array.
1166 * @param count current state counter.
1167 * @param s current state.
1170 number_states (void *cls, const unsigned int count,
1171 struct REGEX_INTERNAL_State *s)
1173 struct REGEX_INTERNAL_State **states = cls;
1183 ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \
1184 ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf)
1188 * Construct the regular expression given the inductive step,
1189 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
1190 * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
1192 * @param R_last_ij value of $R^{(k-1)_{ij}.
1193 * @param R_last_ik value of $R^{(k-1)_{ik}.
1194 * @param R_last_kk value of $R^{(k-1)_{kk}.
1195 * @param R_last_kj value of $R^{(k-1)_{kj}.
1196 * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
1197 * is expected to be NULL when called!
1198 * @param R_cur_l optimization -- kept between iterations to avoid realloc
1199 * @param R_cur_r optimization -- kept between iterations to avoid realloc
1202 automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1203 const struct StringBuffer *R_last_ik,
1204 const struct StringBuffer *R_last_kk,
1205 const struct StringBuffer *R_last_kj,
1206 struct StringBuffer *R_cur_ij,
1207 struct StringBuffer *R_cur_l,
1208 struct StringBuffer *R_cur_r)
1210 struct StringBuffer R_temp_ij;
1211 struct StringBuffer R_temp_ik;
1212 struct StringBuffer R_temp_kj;
1213 struct StringBuffer R_temp_kk;
1219 int clean_ik_kk_cmp;
1220 int clean_kk_kj_cmp;
1226 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1227 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1228 * R_cur_ij = R_cur_l | R_cur_r
1229 * R_cur_l == R^{(k-1)}_{ij}
1230 * R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1233 if ( (GNUNET_YES == R_last_ij->null_flag) &&
1234 ( (GNUNET_YES == R_last_ik->null_flag) ||
1235 (GNUNET_YES == R_last_kj->null_flag)))
1237 /* R^{(k)}_{ij} = N | N */
1238 R_cur_ij->null_flag = GNUNET_YES;
1239 R_cur_ij->synced = GNUNET_NO;
1243 if ( (GNUNET_YES == R_last_ik->null_flag) ||
1244 (GNUNET_YES == R_last_kj->null_flag) )
1246 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1247 if (GNUNET_YES == R_last_ij->synced)
1249 R_cur_ij->synced = GNUNET_YES;
1250 R_cur_ij->null_flag = GNUNET_NO;
1253 R_cur_ij->synced = GNUNET_YES;
1254 sb_strdup (R_cur_ij, R_last_ij);
1257 R_cur_ij->synced = GNUNET_NO;
1259 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
1260 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} */
1262 R_cur_r->null_flag = GNUNET_YES;
1264 R_cur_l->null_flag = GNUNET_YES;
1267 /* cache results from strcmp, we might need these many times */
1268 ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj);
1269 ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik);
1270 ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk);
1271 kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj);
1273 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1274 * as parentheses, so we can better compare the contents */
1276 memset (&R_temp_ij, 0, sizeof (struct StringBuffer));
1277 memset (&R_temp_ik, 0, sizeof (struct StringBuffer));
1278 memset (&R_temp_kk, 0, sizeof (struct StringBuffer));
1279 memset (&R_temp_kj, 0, sizeof (struct StringBuffer));
1280 remove_epsilon (R_last_ik, &R_temp_ik);
1281 remove_epsilon (R_last_kk, &R_temp_kk);
1282 remove_epsilon (R_last_kj, &R_temp_kj);
1283 remove_parentheses (&R_temp_ik);
1284 remove_parentheses (&R_temp_kk);
1285 remove_parentheses (&R_temp_kj);
1286 clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk);
1287 clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj);
1289 /* construct R_cur_l (and, if necessary R_cur_r) */
1290 if (GNUNET_YES != R_last_ij->null_flag)
1292 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1293 * as parentheses, so we can better compare the contents */
1294 remove_epsilon (R_last_ij, &R_temp_ij);
1295 remove_parentheses (&R_temp_ij);
1297 if ( (0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) &&
1298 (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) &&
1299 (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) )
1301 if (0 == R_temp_ij.slen)
1303 R_cur_r->null_flag = GNUNET_NO;
1305 else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) ||
1306 (0 == sb_strncmp_cstr (R_last_ik, "(|", 2) &&
1307 0 == sb_strncmp_cstr (R_last_kj, "(|", 2)))
1310 * a|(e|a)a*(e|a) = a*
1311 * a|(e|a)(e|a)*(e|a) = a*
1313 * (e|a)|aa*(e|a) = a*
1314 * (e|a)|(e|a)a*a = a*
1315 * (e|a)|(e|a)a*(e|a) = a*
1316 * (e|a)|(e|a)(e|a)*(e|a) = a*
1318 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1319 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1321 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij);
1329 * a|(e|a)(e|a)*a = a+
1330 * a|a(e|a)*(e|a) = a+
1332 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1333 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1335 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij);
1338 else if ( (0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && (0 != clean_ik_kk_cmp) )
1341 if (0 == R_last_kk->slen)
1342 sb_strdup (R_cur_r, R_last_ij);
1343 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1344 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1346 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk);
1347 R_cur_l->null_flag = GNUNET_YES;
1349 else if ( (0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && (0 != clean_kk_kj_cmp))
1352 if (R_last_kk->slen < 1)
1354 sb_strdup (R_cur_r, R_last_kj);
1356 else if (GNUNET_YES == needs_parentheses (&R_temp_kk))
1357 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1359 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1361 R_cur_l->null_flag = GNUNET_YES;
1363 else if ( (0 == ij_ik_cmp) && (0 == kk_kj_cmp) && (! has_epsilon (R_last_ij)) &&
1364 has_epsilon (R_last_kk))
1366 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
1367 if (needs_parentheses (&R_temp_kk))
1368 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1370 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk);
1371 R_cur_l->null_flag = GNUNET_YES;
1373 else if ( (0 == ij_kj_cmp) && (0 == ik_kk_cmp) && (! has_epsilon (R_last_ij)) &&
1374 has_epsilon (R_last_kk))
1376 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
1377 if (needs_parentheses (&R_temp_kk))
1378 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1380 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1381 R_cur_l->null_flag = GNUNET_YES;
1385 sb_strdup (R_cur_l, R_last_ij);
1386 remove_parentheses (R_cur_l);
1391 /* we have no left side */
1392 R_cur_l->null_flag = GNUNET_YES;
1395 /* construct R_cur_r, if not already constructed */
1396 if (GNUNET_YES == R_cur_r->null_flag)
1398 length = R_temp_kk.slen - R_last_ik->slen;
1400 /* a(ba)*bx = (ab)+x */
1401 if ( (length > 0) &&
1402 (GNUNET_YES != R_last_kk->null_flag) &&
1403 (0 < R_last_kk->slen) &&
1404 (GNUNET_YES != R_last_kj->null_flag) &&
1405 (0 < R_last_kj->slen) &&
1406 (GNUNET_YES != R_last_ik->null_flag) &&
1407 (0 < R_last_ik->slen) &&
1408 (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) &&
1409 (0 == sb_strncmp (&R_temp_kk, R_last_kj, length)) )
1411 struct StringBuffer temp_a;
1412 struct StringBuffer temp_b;
1414 sb_init (&temp_a, length);
1415 sb_init (&temp_b, R_last_kj->slen - length);
1418 temp_a.sbuf = temp_a.abuf;
1419 GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l);
1420 temp_a.slen = length_l;
1422 length_r = R_last_kj->slen - length;
1423 temp_b.sbuf = temp_b.abuf;
1424 GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1425 temp_b.slen = length_r;
1427 /* e|(ab)+ = (ab)* */
1428 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1429 (0 == R_cur_l->slen) &&
1430 (0 == temp_b.slen) )
1432 sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1434 R_cur_l->null_flag = GNUNET_YES;
1438 sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1443 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk) &&
1444 0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1448 * (e|a)(e|a)*(e|a) = a*
1450 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
1452 if (needs_parentheses (&R_temp_kk))
1453 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk);
1455 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk);
1458 else if ( (0 == clean_ik_kk_cmp) &&
1459 (0 == clean_kk_kj_cmp) &&
1460 (! has_epsilon (R_last_ik)) )
1462 if (needs_parentheses (&R_temp_kk))
1463 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk);
1465 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk);
1476 (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1477 has_epsilon (R_last_kj));
1481 if (needs_parentheses (&R_temp_kk))
1482 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk);
1484 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk);
1490 * (e|a)(e|a)*b = a*b
1492 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk))
1494 if (has_epsilon (R_last_ik))
1496 if (needs_parentheses (&R_temp_kk))
1497 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1499 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1503 if (needs_parentheses (&R_temp_kk))
1504 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1506 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1511 * b(e|a)*(e|a) = ba*
1513 else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1515 if (has_epsilon (R_last_kj))
1517 if (needs_parentheses (&R_temp_kk))
1518 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1520 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1524 if (needs_parentheses (&R_temp_kk))
1525 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1527 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1532 if (0 < R_temp_kk.slen)
1534 if (needs_parentheses (&R_temp_kk))
1536 sb_printf3 (R_cur_r, "%.*s(%.*s)*%.*s", 3, R_last_ik, &R_temp_kk,
1541 sb_printf3 (R_cur_r, "%.*s%.*s*%.*s", 1, R_last_ik, &R_temp_kk,
1547 sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1551 sb_free (&R_temp_ij);
1552 sb_free (&R_temp_ik);
1553 sb_free (&R_temp_kk);
1554 sb_free (&R_temp_kj);
1556 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1557 (GNUNET_YES == R_cur_r->null_flag) )
1559 R_cur_ij->null_flag = GNUNET_YES;
1563 if ( (GNUNET_YES != R_cur_l->null_flag) &&
1564 (GNUNET_YES == R_cur_r->null_flag) )
1566 struct StringBuffer tmp;
1569 *R_cur_ij = *R_cur_l;
1574 if ( (GNUNET_YES == R_cur_l->null_flag) &&
1575 (GNUNET_YES != R_cur_r->null_flag) )
1577 struct StringBuffer tmp;
1580 *R_cur_ij = *R_cur_r;
1585 if (0 == sb_nullstrcmp (R_cur_l, R_cur_r))
1587 struct StringBuffer tmp;
1590 *R_cur_ij = *R_cur_l;
1594 sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r);
1599 * Create proofs for all states in the given automaton. Implementation of the
1600 * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
1601 * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
1603 * Each state in the automaton gets assigned 'proof' and 'hash' (hash of the
1604 * proof) fields. The starting state will only have a valid proof/hash if it has
1605 * any incoming transitions.
1607 * @param a automaton for which to assign proofs and hashes, must not be NULL
1610 automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1612 unsigned int n = a->state_count;
1613 struct REGEX_INTERNAL_State *states[n];
1614 struct StringBuffer *R_last;
1615 struct StringBuffer *R_cur;
1616 struct StringBuffer R_cur_r;
1617 struct StringBuffer R_cur_l;
1618 struct StringBuffer *R_swap;
1619 struct REGEX_INTERNAL_Transition *t;
1620 struct StringBuffer complete_regex;
1625 R_last = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1626 R_cur = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n);
1627 if ( (NULL == R_last) ||
1630 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
1631 GNUNET_free_non_null (R_cur);
1632 GNUNET_free_non_null (R_last);
1633 return GNUNET_SYSERR;
1636 /* create depth-first numbering of the states, initializes 'state' */
1637 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &number_states,
1640 for (i = 0; i < n; i++)
1641 GNUNET_assert (NULL != states[i]);
1642 for (i = 0; i < n; i++)
1643 for (j = 0; j < n; j++)
1644 R_last[i *n + j].null_flag = GNUNET_YES;
1646 /* Compute regular expressions of length "1" between each pair of states */
1647 for (i = 0; i < n; i++)
1649 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1651 j = t->to_state->dfs_id;
1652 if (GNUNET_YES == R_last[i * n + j].null_flag)
1654 sb_strdup_cstr (&R_last[i * n + j], t->label);
1658 sb_append_cstr (&R_last[i * n + j], "|");
1659 sb_append_cstr (&R_last[i * n + j], t->label);
1662 /* add self-loop: i is reachable from i via epsilon-transition */
1663 if (GNUNET_YES == R_last[i * n + i].null_flag)
1665 R_last[i * n + i].slen = 0;
1666 R_last[i * n + i].null_flag = GNUNET_NO;
1670 sb_wrap (&R_last[i * n + i], "(|%.*s)", 3);
1673 for (i = 0; i < n; i++)
1674 for (j = 0; j < n; j++)
1675 if (needs_parentheses (&R_last[i * n + j]))
1676 sb_wrap (&R_last[i * n + j], "(%.*s)", 2);
1677 /* Compute regular expressions of length "k" between each pair of states per
1679 memset (&R_cur_l, 0, sizeof (struct StringBuffer));
1680 memset (&R_cur_r, 0, sizeof (struct StringBuffer));
1681 for (k = 0; k < n; k++)
1683 for (i = 0; i < n; i++)
1685 for (j = 0; j < n; j++)
1687 /* Basis for the recursion:
1688 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1689 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1692 /* Create R_cur[i][j] and simplify the expression */
1693 automaton_create_proofs_simplify (&R_last[i * n + j], &R_last[i * n + k],
1694 &R_last[k * n + k], &R_last[k * n + j],
1696 &R_cur_l, &R_cur_r);
1699 /* set R_last = R_cur */
1703 /* clear 'R_cur' for next iteration */
1704 for (i = 0; i < n; i++)
1705 for (j = 0; j < n; j++)
1706 R_cur[i * n + j].null_flag = GNUNET_YES;
1710 /* assign proofs and hashes */
1711 for (i = 0; i < n; i++)
1713 if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1715 states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf,
1716 R_last[a->start->dfs_id * n + i].slen);
1717 GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
1722 /* complete regex for whole DFA: union of all pairs (start state/accepting
1724 sb_init (&complete_regex, 16 * n);
1725 for (i = 0; i < n; i++)
1727 if (states[i]->accepting)
1729 if ( (0 == complete_regex.slen) &&
1730 (0 < R_last[a->start->dfs_id * n + i].slen) )
1732 sb_append (&complete_regex,
1733 &R_last[a->start->dfs_id * n + i]);
1735 else if ( (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) &&
1736 (0 < R_last[a->start->dfs_id * n + i].slen) )
1738 sb_append_cstr (&complete_regex, "|");
1739 sb_append (&complete_regex,
1740 &R_last[a->start->dfs_id * n + i]);
1744 a->canonical_regex = GNUNET_strndup (complete_regex.sbuf, complete_regex.slen);
1747 sb_free (&complete_regex);
1748 for (i = 0; i < n; i++)
1749 for (j = 0; j < n; j++)
1751 sb_free (&R_cur[i * n + j]);
1752 sb_free (&R_last[i * n + j]);
1754 GNUNET_free (R_cur);
1755 GNUNET_free (R_last);
1761 * Creates a new DFA state based on a set of NFA states. Needs to be freed using
1762 * automaton_destroy_state.
1764 * @param ctx context
1765 * @param nfa_states set of NFA states on which the DFA should be based on
1767 * @return new DFA state
1769 static struct REGEX_INTERNAL_State *
1770 dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1771 struct REGEX_INTERNAL_StateSet *nfa_states)
1773 struct REGEX_INTERNAL_State *s;
1776 struct REGEX_INTERNAL_State *cstate;
1777 struct REGEX_INTERNAL_Transition *ctran;
1780 s = GNUNET_new (struct REGEX_INTERNAL_State);
1781 s->id = ctx->state_id++;
1785 if (NULL == nfa_states)
1787 GNUNET_asprintf (&s->name, "s%i", s->id);
1791 s->nfa_set = *nfa_states;
1793 if (nfa_states->off < 1)
1796 /* Create a name based on 'nfa_states' */
1797 len = nfa_states->off * 14 + 4;
1798 s->name = GNUNET_malloc (len);
1799 strcat (s->name, "{");
1802 for (i = 0; i < nfa_states->off; i++)
1804 cstate = nfa_states->states[i];
1805 GNUNET_snprintf (pos,
1806 pos - s->name + len,
1809 pos += strlen (pos);
1811 /* Add a transition for each distinct label to NULL state */
1812 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1813 if (NULL != ctran->label)
1814 state_add_transition (ctx, s, ctran->label, NULL);
1816 /* If the nfa_states contain an accepting state, the new dfa state is also
1818 if (cstate->accepting)
1822 s->name = GNUNET_realloc (s->name, strlen (s->name) + 1);
1824 memset (nfa_states, 0, sizeof (struct REGEX_INTERNAL_StateSet));
1830 * Move from the given state 's' to the next state on transition 'str'. Consumes
1831 * as much of the given 'str' as possible (usefull for strided DFAs). On return
1832 * 's' will point to the next state, and the length of the substring used for
1833 * this transition will be returned. If no transition possible 0 is returned and
1834 * 's' points to NULL.
1836 * @param s starting state, will point to the next state or NULL (if no
1837 * transition possible)
1838 * @param str edge label to follow (will match longest common prefix)
1840 * @return length of the substring comsumed from 'str'
1843 dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1845 struct REGEX_INTERNAL_Transition *t;
1846 struct REGEX_INTERNAL_State *new_s;
1848 unsigned int max_len;
1855 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1857 len = strlen (t->label);
1859 if (0 == strncmp (t->label, str, len))
1864 new_s = t->to_state;
1875 * Set the given state 'marked' to #GNUNET_YES. Used by the
1876 * #dfa_remove_unreachable_states() function to detect unreachable states in the
1879 * @param cls closure, not used.
1880 * @param count count, not used.
1881 * @param s state where the marked attribute will be set to #GNUNET_YES.
1884 mark_states (void *cls,
1885 const unsigned int count,
1886 struct REGEX_INTERNAL_State *s)
1888 s->marked = GNUNET_YES;
1893 * Remove all unreachable states from DFA 'a'. Unreachable states are those
1894 * states that are not reachable from the starting state.
1896 * @param a DFA automaton
1899 dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1901 struct REGEX_INTERNAL_State *s;
1902 struct REGEX_INTERNAL_State *s_next;
1904 /* 1. unmark all states */
1905 for (s = a->states_head; NULL != s; s = s->next)
1906 s->marked = GNUNET_NO;
1908 /* 2. traverse dfa from start state and mark all visited states */
1909 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, &mark_states, NULL);
1911 /* 3. delete all states that were not visited */
1912 for (s = a->states_head; NULL != s; s = s_next)
1915 if (GNUNET_NO == s->marked)
1916 automaton_remove_state (a, s);
1922 * Remove all dead states from the DFA 'a'. Dead states are those states that do
1923 * not transition to any other state but themselves.
1925 * @param a DFA automaton
1928 dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a)
1930 struct REGEX_INTERNAL_State *s;
1931 struct REGEX_INTERNAL_State *s_next;
1932 struct REGEX_INTERNAL_Transition *t;
1935 GNUNET_assert (DFA == a->type);
1937 for (s = a->states_head; NULL != s; s = s_next)
1945 for (t = s->transitions_head; NULL != t; t = t->next)
1947 if (NULL != t->to_state && t->to_state != s)
1957 /* state s is dead, remove it */
1958 automaton_remove_state (a, s);
1964 * Merge all non distinguishable states in the DFA 'a'
1966 * @param ctx context
1967 * @param a DFA automaton
1968 * @return #GNUNET_OK on success
1971 dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1972 struct REGEX_INTERNAL_Automaton *a)
1975 struct REGEX_INTERNAL_State *s1;
1976 struct REGEX_INTERNAL_State *s2;
1977 struct REGEX_INTERNAL_Transition *t1;
1978 struct REGEX_INTERNAL_Transition *t2;
1979 struct REGEX_INTERNAL_State *s1_next;
1980 struct REGEX_INTERNAL_State *s2_next;
1982 unsigned int num_equal_edges;
1984 unsigned int state_cnt;
1985 unsigned long long idx;
1986 unsigned long long idx1;
1988 if ( (NULL == a) || (0 == a->state_count) )
1990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1991 "Could not merge nondistinguishable states, automaton was NULL.\n");
1992 return GNUNET_SYSERR;
1995 state_cnt = a->state_count;
1996 table = GNUNET_malloc_large ((sizeof (uint32_t) * state_cnt * state_cnt / 32) + sizeof (uint32_t));
1999 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
2000 return GNUNET_SYSERR;
2003 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
2006 /* Mark all pairs of accepting/!accepting states */
2007 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2008 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
2009 if ( (s1->accepting && !s2->accepting) ||
2010 (!s1->accepting && s2->accepting) )
2012 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2013 table[idx / 32] |= (1U << (idx % 32));
2016 /* Find all equal states */
2021 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2023 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
2025 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2026 if (0 != (table[idx / 32] & (1U << (idx % 32))))
2028 num_equal_edges = 0;
2029 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2031 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
2033 if (0 == strcmp (t1->label, t2->label))
2036 /* same edge, but targets definitively different, so we're different
2038 if (t1->to_state->marked > t2->to_state->marked)
2039 idx1 = (unsigned long long) t1->to_state->marked * state_cnt + t2->to_state->marked;
2041 idx1 = (unsigned long long) t2->to_state->marked * state_cnt + t1->to_state->marked;
2042 if (0 != (table[idx1 / 32] & (1U << (idx1 % 32))))
2044 table[idx / 32] |= (1U << (idx % 32));
2045 change = 1; /* changed a marker, need to run again */
2050 if ( (num_equal_edges != s1->transition_count) ||
2051 (num_equal_edges != s2->transition_count) )
2053 /* Make sure ALL edges of possible equal states are the same */
2054 table[idx / 32] |= (1U << (idx % 32));
2055 change = 1; /* changed a marker, need to run again */
2061 /* Merge states that are equal */
2062 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
2065 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2068 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2069 if (0 == (table[idx / 32] & (1U << (idx % 32))))
2070 automaton_merge_states (ctx, a, s1, s2);
2074 GNUNET_free (table);
2080 * Minimize the given DFA 'a' by removing all unreachable states, removing all
2081 * dead states and merging all non distinguishable states
2083 * @param ctx context
2084 * @param a DFA automaton
2085 * @return GNUNET_OK on success
2088 dfa_minimize (struct REGEX_INTERNAL_Context *ctx,
2089 struct REGEX_INTERNAL_Automaton *a)
2092 return GNUNET_SYSERR;
2094 GNUNET_assert (DFA == a->type);
2096 /* 1. remove unreachable states */
2097 dfa_remove_unreachable_states (a);
2099 /* 2. remove dead states */
2100 dfa_remove_dead_states (a);
2102 /* 3. Merge nondistinguishable states */
2103 if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a))
2104 return GNUNET_SYSERR;
2110 * Context for adding strided transitions to a DFA.
2112 struct REGEX_INTERNAL_Strided_Context
2115 * Length of the strides.
2117 const unsigned int stride;
2120 * Strided transitions DLL. New strided transitions will be stored in this DLL
2121 * and afterwards added to the DFA.
2123 struct REGEX_INTERNAL_Transition *transitions_head;
2126 * Strided transitions DLL.
2128 struct REGEX_INTERNAL_Transition *transitions_tail;
2133 * Recursive helper function to add strides to a DFA.
2135 * @param cls context, contains stride length and strided transitions DLL.
2136 * @param depth current depth of the depth-first traversal of the graph.
2137 * @param label current label, string that contains all labels on the path from
2139 * @param start start state for the depth-first traversal of the graph.
2140 * @param s current state in the depth-first traversal
2143 dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
2144 struct REGEX_INTERNAL_State *start,
2145 struct REGEX_INTERNAL_State *s)
2147 struct REGEX_INTERNAL_Strided_Context *ctx = cls;
2148 struct REGEX_INTERNAL_Transition *t;
2151 if (depth == ctx->stride)
2153 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2154 t->label = GNUNET_strdup (label);
2156 t->from_state = start;
2157 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
2162 for (t = s->transitions_head; NULL != t; t = t->next)
2164 /* Do not consider self-loops, because it end's up in too many
2166 if (t->to_state == t->from_state)
2171 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2174 new_label = GNUNET_strdup (t->label);
2176 dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
2180 GNUNET_free_non_null (label);
2185 * Function called for each state in the DFA. Starts a traversal of depth set in
2186 * context starting from state 's'.
2188 * @param cls context.
2189 * @param count not used.
2190 * @param s current state.
2193 dfa_add_multi_strides (void *cls, const unsigned int count,
2194 struct REGEX_INTERNAL_State *s)
2196 dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
2201 * Adds multi-strided transitions to the given 'dfa'.
2203 * @param regex_ctx regex context needed to add transitions to the automaton.
2204 * @param dfa DFA to which the multi strided transitions should be added.
2205 * @param stride_len length of the strides.
2208 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2209 struct REGEX_INTERNAL_Automaton *dfa,
2210 const unsigned int stride_len)
2212 struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2213 struct REGEX_INTERNAL_Transition *t;
2214 struct REGEX_INTERNAL_Transition *t_next;
2216 if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
2219 /* Compute the new transitions of given stride_len */
2220 REGEX_INTERNAL_automaton_traverse (dfa, dfa->start, NULL, NULL,
2221 &dfa_add_multi_strides, &ctx);
2223 /* Add all the new transitions to the automaton. */
2224 for (t = ctx.transitions_head; NULL != t; t = t_next)
2227 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2228 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
2229 GNUNET_free_non_null (t->label);
2233 /* Mark this automaton as multistrided */
2234 dfa->is_multistrided = GNUNET_YES;
2238 * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
2239 * and adds new transitions to the given transitions DLL and marks states that
2240 * should be removed by setting state->contained to GNUNET_YES.
2242 * @param dfa DFA for which the paths should be compressed.
2243 * @param start starting state for linear path search.
2244 * @param cur current state in the recursive DFS.
2245 * @param label current label (string of traversed labels).
2246 * @param max_len maximal path compression length.
2247 * @param transitions_head transitions DLL.
2248 * @param transitions_tail transitions DLL.
2251 dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2252 struct REGEX_INTERNAL_State *start,
2253 struct REGEX_INTERNAL_State *cur, char *label,
2254 unsigned int max_len,
2255 struct REGEX_INTERNAL_Transition **transitions_head,
2256 struct REGEX_INTERNAL_Transition **transitions_tail)
2258 struct REGEX_INTERNAL_Transition *t;
2262 if (NULL != label &&
2263 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
2264 GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
2265 max_len == strlen (label)) ||
2266 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label))))
2268 t = GNUNET_new (struct REGEX_INTERNAL_Transition);
2269 t->label = GNUNET_strdup (label);
2271 t->from_state = start;
2272 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
2274 if (GNUNET_NO == cur->marked)
2276 dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
2281 else if (cur != start)
2282 cur->contained = GNUNET_YES;
2284 if (GNUNET_YES == cur->marked && cur != start)
2287 cur->marked = GNUNET_YES;
2290 for (t = cur->transitions_head; NULL != t; t = t->next)
2293 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2295 new_label = GNUNET_strdup (t->label);
2297 if (t->to_state != cur)
2299 dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
2300 transitions_head, transitions_tail);
2302 GNUNET_free (new_label);
2308 * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
2309 * compressed to 0->3 by combining transitions.
2311 * @param regex_ctx context for adding new transitions.
2312 * @param dfa DFA representation, will directly modify the given DFA.
2313 * @param max_len maximal length of the compressed paths.
2316 dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2317 struct REGEX_INTERNAL_Automaton *dfa, unsigned int max_len)
2319 struct REGEX_INTERNAL_State *s;
2320 struct REGEX_INTERNAL_State *s_next;
2321 struct REGEX_INTERNAL_Transition *t;
2322 struct REGEX_INTERNAL_Transition *t_next;
2323 struct REGEX_INTERNAL_Transition *transitions_head = NULL;
2324 struct REGEX_INTERNAL_Transition *transitions_tail = NULL;
2329 /* Count the incoming transitions on each state. */
2330 for (s = dfa->states_head; NULL != s; s = s->next)
2332 for (t = s->transitions_head; NULL != t; t = t->next)
2334 if (NULL != t->to_state)
2335 t->to_state->incoming_transition_count++;
2339 /* Unmark all states. */
2340 for (s = dfa->states_head; NULL != s; s = s->next)
2342 s->marked = GNUNET_NO;
2343 s->contained = GNUNET_NO;
2346 /* Add strides and mark states that can be deleted. */
2347 dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
2348 &transitions_head, &transitions_tail);
2350 /* Add all the new transitions to the automaton. */
2351 for (t = transitions_head; NULL != t; t = t_next)
2354 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
2355 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
2356 GNUNET_free_non_null (t->label);
2360 /* Remove marked states (including their incoming and outgoing transitions). */
2361 for (s = dfa->states_head; NULL != s; s = s_next)
2364 if (GNUNET_YES == s->contained)
2365 automaton_remove_state (dfa, s);
2371 * Creates a new NFA fragment. Needs to be cleared using
2372 * automaton_fragment_clear.
2374 * @param start starting state
2375 * @param end end state
2377 * @return new NFA fragment
2379 static struct REGEX_INTERNAL_Automaton *
2380 nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2381 struct REGEX_INTERNAL_State *end)
2383 struct REGEX_INTERNAL_Automaton *n;
2385 n = GNUNET_new (struct REGEX_INTERNAL_Automaton);
2392 if (NULL == start || NULL == end)
2395 automaton_add_state (n, end);
2396 automaton_add_state (n, start);
2408 * Adds a list of states to the given automaton 'n'.
2410 * @param n automaton to which the states should be added
2411 * @param states_head head of the DLL of states
2412 * @param states_tail tail of the DLL of states
2415 nfa_add_states (struct REGEX_INTERNAL_Automaton *n,
2416 struct REGEX_INTERNAL_State *states_head,
2417 struct REGEX_INTERNAL_State *states_tail)
2419 struct REGEX_INTERNAL_State *s;
2421 if (NULL == n || NULL == states_head)
2423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
2427 if (NULL == n->states_head)
2429 n->states_head = states_head;
2430 n->states_tail = states_tail;
2434 if (NULL != states_head)
2436 n->states_tail->next = states_head;
2437 n->states_tail = states_tail;
2440 for (s = states_head; NULL != s; s = s->next)
2446 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
2448 * @param ctx context
2449 * @param accepting is it an accepting state or not
2451 * @return new NFA state
2453 static struct REGEX_INTERNAL_State *
2454 nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2456 struct REGEX_INTERNAL_State *s;
2458 s = GNUNET_new (struct REGEX_INTERNAL_State);
2459 s->id = ctx->state_id++;
2460 s->accepting = accepting;
2461 s->marked = GNUNET_NO;
2467 GNUNET_asprintf (&s->name, "s%i", s->id);
2474 * Calculates the closure set for the given set of states.
2476 * @param ret set to sorted nfa closure on 'label' (epsilon closure if 'label' is NULL)
2477 * @param nfa the NFA containing 's'
2478 * @param states list of states on which to base the closure on
2479 * @param label transitioning label for which to base the closure on,
2480 * pass NULL for epsilon transition
2483 nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2484 struct REGEX_INTERNAL_Automaton *nfa,
2485 struct REGEX_INTERNAL_StateSet *states, const char *label)
2487 struct REGEX_INTERNAL_State *s;
2489 struct REGEX_INTERNAL_StateSet_MDLL cls_stack;
2490 struct REGEX_INTERNAL_State *clsstate;
2491 struct REGEX_INTERNAL_State *currentstate;
2492 struct REGEX_INTERNAL_Transition *ctran;
2494 memset (ret, 0, sizeof (struct REGEX_INTERNAL_StateSet));
2498 for (i = 0; i < states->off; i++)
2500 s = states->states[i];
2502 /* Add start state to closure only for epsilon closure */
2504 state_set_append (ret, s);
2506 /* initialize work stack */
2507 cls_stack.head = NULL;
2508 cls_stack.tail = NULL;
2509 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s);
2512 while (NULL != (currentstate = cls_stack.tail))
2514 GNUNET_CONTAINER_MDLL_remove (ST, cls_stack.head, cls_stack.tail,
2517 for (ctran = currentstate->transitions_head; NULL != ctran;
2518 ctran = ctran->next)
2520 if (NULL == (clsstate = ctran->to_state))
2522 if (0 != clsstate->contained)
2524 if (0 != nullstrcmp (label, ctran->label))
2526 state_set_append (ret, clsstate);
2527 GNUNET_CONTAINER_MDLL_insert_tail (ST, cls_stack.head, cls_stack.tail,
2530 clsstate->contained = 1;
2534 for (i = 0; i < ret->off; i++)
2535 ret->states[i]->contained = 0;
2538 qsort (ret->states, ret->off, sizeof (struct REGEX_INTERNAL_State *),
2544 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
2546 * @param ctx context
2549 nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx)
2551 struct REGEX_INTERNAL_Automaton *a;
2552 struct REGEX_INTERNAL_Automaton *b;
2553 struct REGEX_INTERNAL_Automaton *new_nfa;
2555 b = ctx->stack_tail;
2556 GNUNET_assert (NULL != b);
2557 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2558 a = ctx->stack_tail;
2559 GNUNET_assert (NULL != a);
2560 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2562 state_add_transition (ctx, a->end, NULL, b->start);
2563 a->end->accepting = 0;
2564 b->end->accepting = 1;
2566 new_nfa = nfa_fragment_create (NULL, NULL);
2567 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2568 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2569 new_nfa->start = a->start;
2570 new_nfa->end = b->end;
2571 new_nfa->state_count += a->state_count + b->state_count;
2572 automaton_fragment_clear (a);
2573 automaton_fragment_clear (b);
2575 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2580 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
2582 * @param ctx context
2585 nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2587 struct REGEX_INTERNAL_Automaton *a;
2588 struct REGEX_INTERNAL_Automaton *new_nfa;
2589 struct REGEX_INTERNAL_State *start;
2590 struct REGEX_INTERNAL_State *end;
2592 a = ctx->stack_tail;
2596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2597 "nfa_add_star_op failed, because there was no element on the stack");
2601 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2603 start = nfa_state_create (ctx, 0);
2604 end = nfa_state_create (ctx, 1);
2606 state_add_transition (ctx, start, NULL, a->start);
2607 state_add_transition (ctx, start, NULL, end);
2608 state_add_transition (ctx, a->end, NULL, a->start);
2609 state_add_transition (ctx, a->end, NULL, end);
2611 a->end->accepting = 0;
2614 new_nfa = nfa_fragment_create (start, end);
2615 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2616 automaton_fragment_clear (a);
2618 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2623 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
2625 * @param ctx context
2628 nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx)
2630 struct REGEX_INTERNAL_Automaton *a;
2632 a = ctx->stack_tail;
2636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2637 "nfa_add_plus_op failed, because there was no element on the stack");
2641 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2643 state_add_transition (ctx, a->end, NULL, a->start);
2645 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
2650 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
2652 * @param ctx context
2655 nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2657 struct REGEX_INTERNAL_Automaton *a;
2658 struct REGEX_INTERNAL_Automaton *new_nfa;
2659 struct REGEX_INTERNAL_State *start;
2660 struct REGEX_INTERNAL_State *end;
2662 a = ctx->stack_tail;
2665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2666 "nfa_add_question_op failed, because there was no element on the stack");
2670 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2672 start = nfa_state_create (ctx, 0);
2673 end = nfa_state_create (ctx, 1);
2675 state_add_transition (ctx, start, NULL, a->start);
2676 state_add_transition (ctx, start, NULL, end);
2677 state_add_transition (ctx, a->end, NULL, end);
2679 a->end->accepting = 0;
2681 new_nfa = nfa_fragment_create (start, end);
2682 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2683 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2684 automaton_fragment_clear (a);
2689 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
2690 * alternates between a and b (a|b)
2692 * @param ctx context
2695 nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2697 struct REGEX_INTERNAL_Automaton *a;
2698 struct REGEX_INTERNAL_Automaton *b;
2699 struct REGEX_INTERNAL_Automaton *new_nfa;
2700 struct REGEX_INTERNAL_State *start;
2701 struct REGEX_INTERNAL_State *end;
2703 b = ctx->stack_tail;
2704 GNUNET_assert (NULL != b);
2705 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
2706 a = ctx->stack_tail;
2707 GNUNET_assert (NULL != a);
2708 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
2710 start = nfa_state_create (ctx, 0);
2711 end = nfa_state_create (ctx, 1);
2712 state_add_transition (ctx, start, NULL, a->start);
2713 state_add_transition (ctx, start, NULL, b->start);
2715 state_add_transition (ctx, a->end, NULL, end);
2716 state_add_transition (ctx, b->end, NULL, end);
2718 a->end->accepting = 0;
2719 b->end->accepting = 0;
2722 new_nfa = nfa_fragment_create (start, end);
2723 nfa_add_states (new_nfa, a->states_head, a->states_tail);
2724 nfa_add_states (new_nfa, b->states_head, b->states_tail);
2725 automaton_fragment_clear (a);
2726 automaton_fragment_clear (b);
2728 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
2733 * Adds a new nfa fragment to the stack
2735 * @param ctx context
2736 * @param label label for nfa transition
2739 nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label)
2741 struct REGEX_INTERNAL_Automaton *n;
2742 struct REGEX_INTERNAL_State *start;
2743 struct REGEX_INTERNAL_State *end;
2745 GNUNET_assert (NULL != ctx);
2747 start = nfa_state_create (ctx, 0);
2748 end = nfa_state_create (ctx, 1);
2749 state_add_transition (ctx, start, label, end);
2750 n = nfa_fragment_create (start, end);
2751 GNUNET_assert (NULL != n);
2752 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
2757 * Initialize a new context
2759 * @param ctx context
2762 REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx)
2766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2770 ctx->transition_id = 0;
2771 ctx->stack_head = NULL;
2772 ctx->stack_tail = NULL;
2777 * Construct an NFA by parsing the regex string of length 'len'.
2779 * @param regex regular expression string
2780 * @param len length of the string
2782 * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton
2784 struct REGEX_INTERNAL_Automaton *
2785 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2787 struct REGEX_INTERNAL_Context ctx;
2788 struct REGEX_INTERNAL_Automaton *nfa;
2793 unsigned int altcount;
2794 unsigned int atomcount;
2803 if (NULL == regex || 0 == strlen (regex) || 0 == len)
2805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2806 "Could not parse regex. Empty regex string provided.\n");
2810 REGEX_INTERNAL_context_init (&ctx);
2821 for (count = 0; count < len && *regexp; count++, regexp++)
2829 nfa_add_concatenation (&ctx);
2832 GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2833 p[poff].altcount = altcount;
2834 p[poff].atomcount = atomcount;
2842 error_msg = "Cannot append '|' to nothing";
2845 while (--atomcount > 0)
2846 nfa_add_concatenation (&ctx);
2852 error_msg = "Missing opening '('";
2857 /* Ignore this: "()" */
2859 altcount = p[poff].altcount;
2860 atomcount = p[poff].atomcount;
2863 while (--atomcount > 0)
2864 nfa_add_concatenation (&ctx);
2865 for (; altcount > 0; altcount--)
2866 nfa_add_alternation (&ctx);
2868 altcount = p[poff].altcount;
2869 atomcount = p[poff].atomcount;
2875 error_msg = "Cannot append '*' to nothing";
2878 nfa_add_star_op (&ctx);
2883 error_msg = "Cannot append '+' to nothing";
2886 nfa_add_plus_op (&ctx);
2891 error_msg = "Cannot append '?' to nothing";
2894 nfa_add_question_op (&ctx);
2900 nfa_add_concatenation (&ctx);
2902 curlabel[0] = *regexp;
2903 nfa_add_label (&ctx, curlabel);
2910 error_msg = "Unbalanced parenthesis";
2913 while (--atomcount > 0)
2914 nfa_add_concatenation (&ctx);
2915 for (; altcount > 0; altcount--)
2916 nfa_add_alternation (&ctx);
2918 GNUNET_array_grow (p, psize, 0);
2920 nfa = ctx.stack_tail;
2921 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2923 if (NULL != ctx.stack_head)
2925 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2929 /* Remember the regex that was used to generate this NFA */
2930 nfa->regex = GNUNET_strdup (regex);
2932 /* create depth-first numbering of the states for pretty printing */
2933 REGEX_INTERNAL_automaton_traverse (nfa, NULL, NULL, NULL, &number_states, NULL);
2935 /* No multistriding added so far */
2936 nfa->is_multistrided = GNUNET_NO;
2941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2942 if (NULL != error_msg)
2943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2945 GNUNET_free_non_null (p);
2947 while (NULL != (nfa = ctx.stack_head))
2949 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
2950 REGEX_INTERNAL_automaton_destroy (nfa);
2958 * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
2960 * @param ctx context.
2961 * @param nfa NFA automaton.
2962 * @param dfa DFA automaton.
2963 * @param dfa_state current dfa state, pass epsilon closure of first nfa state
2967 construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
2968 struct REGEX_INTERNAL_Automaton *nfa,
2969 struct REGEX_INTERNAL_Automaton *dfa,
2970 struct REGEX_INTERNAL_State *dfa_state)
2972 struct REGEX_INTERNAL_Transition *ctran;
2973 struct REGEX_INTERNAL_State *new_dfa_state;
2974 struct REGEX_INTERNAL_State *state_contains;
2975 struct REGEX_INTERNAL_State *state_iter;
2976 struct REGEX_INTERNAL_StateSet tmp;
2977 struct REGEX_INTERNAL_StateSet nfa_set;
2979 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
2981 if (NULL == ctran->label || NULL != ctran->to_state)
2984 nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label);
2985 nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL);
2986 state_set_clear (&tmp);
2988 state_contains = NULL;
2989 for (state_iter = dfa->states_head; NULL != state_iter;
2990 state_iter = state_iter->next)
2992 if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set))
2994 state_contains = state_iter;
2998 if (NULL == state_contains)
3000 new_dfa_state = dfa_state_create (ctx, &nfa_set);
3001 automaton_add_state (dfa, new_dfa_state);
3002 ctran->to_state = new_dfa_state;
3003 construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
3007 ctran->to_state = state_contains;
3008 state_set_clear (&nfa_set);
3015 * Construct DFA for the given 'regex' of length 'len'.
3017 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
3018 * compressed to o -> abc -> o. Note that this parameter influences the
3019 * non-determinism of states of the resulting NFA in the DHT (number of outgoing
3020 * edges with the same label). For example for an application that stores IPv4
3021 * addresses as bitstrings it could make sense to limit the path compression to
3024 * @param regex regular expression string.
3025 * @param len length of the regular expression.
3026 * @param max_path_len limit the path compression length to the
3027 * given value. If set to 1, no path compression is applied. Set to 0 for
3028 * maximal possible path compression (generally not desireable).
3029 * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
3031 struct REGEX_INTERNAL_Automaton *
3032 REGEX_INTERNAL_construct_dfa (const char *regex, const size_t len,
3033 unsigned int max_path_len)
3035 struct REGEX_INTERNAL_Context ctx;
3036 struct REGEX_INTERNAL_Automaton *dfa;
3037 struct REGEX_INTERNAL_Automaton *nfa;
3038 struct REGEX_INTERNAL_StateSet nfa_start_eps_cls;
3039 struct REGEX_INTERNAL_StateSet singleton_set;
3041 REGEX_INTERNAL_context_init (&ctx);
3044 nfa = REGEX_INTERNAL_construct_nfa (regex, len);
3048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3049 "Could not create DFA, because NFA creation failed\n");
3053 dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton);
3055 dfa->regex = GNUNET_strdup (regex);
3057 /* Create DFA start state from epsilon closure */
3058 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3059 state_set_append (&singleton_set, nfa->start);
3060 nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL);
3061 state_set_clear (&singleton_set);
3062 dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls);
3063 automaton_add_state (dfa, dfa->start);
3065 construct_dfa_states (&ctx, nfa, dfa, dfa->start);
3066 REGEX_INTERNAL_automaton_destroy (nfa);
3069 if (GNUNET_OK != dfa_minimize (&ctx, dfa))
3071 REGEX_INTERNAL_automaton_destroy (dfa);
3075 /* Create proofs and hashes for all states */
3076 if (GNUNET_OK != automaton_create_proofs (dfa))
3078 REGEX_INTERNAL_automaton_destroy (dfa);
3082 /* Compress linear DFA paths */
3083 if (1 != max_path_len)
3084 dfa_compress_paths (&ctx, dfa, max_path_len);
3091 * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton data
3094 * @param a automaton to be destroyed
3097 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3099 struct REGEX_INTERNAL_State *s;
3100 struct REGEX_INTERNAL_State *next_state;
3105 GNUNET_free_non_null (a->regex);
3106 GNUNET_free_non_null (a->canonical_regex);
3108 for (s = a->states_head; NULL != s; s = next_state)
3110 next_state = s->next;
3111 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
3112 automaton_destroy_state (s);
3120 * Evaluates the given string using the given DFA automaton
3122 * @param a automaton, type must be DFA
3123 * @param string string that should be evaluated
3125 * @return 0 if string matches, non-0 otherwise
3128 evaluate_dfa (struct REGEX_INTERNAL_Automaton *a,
3132 struct REGEX_INTERNAL_State *s;
3133 unsigned int step_len;
3137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3138 "Tried to evaluate DFA, but NFA automaton given");
3144 /* If the string is empty but the starting state is accepting, we accept. */
3145 if ((NULL == string || 0 == strlen (string)) && s->accepting)
3148 for (strp = string; NULL != strp && *strp; strp += step_len)
3150 step_len = dfa_move (&s, strp);
3156 if (NULL != s && s->accepting)
3164 * Evaluates the given string using the given NFA automaton
3166 * @param a automaton, type must be NFA
3167 * @param string string that should be evaluated
3168 * @return 0 if string matches, non-0 otherwise
3171 evaluate_nfa (struct REGEX_INTERNAL_Automaton *a,
3176 struct REGEX_INTERNAL_State *s;
3177 struct REGEX_INTERNAL_StateSet sset;
3178 struct REGEX_INTERNAL_StateSet new_sset;
3179 struct REGEX_INTERNAL_StateSet singleton_set;
3185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3186 "Tried to evaluate NFA, but DFA automaton given");
3190 /* If the string is empty but the starting state is accepting, we accept. */
3191 if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
3195 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet));
3196 state_set_append (&singleton_set, a->start);
3197 nfa_closure_set_create (&sset, a, &singleton_set, NULL);
3198 state_set_clear (&singleton_set);
3201 for (strp = string; NULL != strp && *strp; strp++)
3204 nfa_closure_set_create (&new_sset, a, &sset, str);
3205 state_set_clear (&sset);
3206 nfa_closure_set_create (&sset, a, &new_sset, 0);
3207 state_set_clear (&new_sset);
3210 for (i = 0; i < sset.off; i++)
3213 if ( (NULL != s) && (s->accepting) )
3220 state_set_clear (&sset);
3226 * Evaluates the given @a string against the given compiled regex @a a
3228 * @param a automaton
3229 * @param string string to check
3230 * @return 0 if string matches, non-0 otherwise
3233 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
3241 result = evaluate_dfa (a, string);
3244 result = evaluate_nfa (a, string);
3247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3248 "Evaluating regex failed, automaton has no type!\n");
3249 result = GNUNET_SYSERR;
3258 * Get the canonical regex of the given automaton.
3259 * When constructing the automaton a proof is computed for each state,
3260 * consisting of the regular expression leading to this state. A complete
3261 * regex for the automaton can be computed by combining these proofs.
3262 * As of now this function is only useful for testing.
3264 * @param a automaton for which the canonical regex should be returned.
3269 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
3274 return a->canonical_regex;
3279 * Get the number of transitions that are contained in the given automaton.
3281 * @param a automaton for which the number of transitions should be returned.
3283 * @return number of transitions in the given automaton.
3286 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
3288 unsigned int t_count;
3289 struct REGEX_INTERNAL_State *s;
3295 for (s = a->states_head; NULL != s; s = s->next)
3296 t_count += s->transition_count;
3303 * Get the first key for the given @a input_string. This hashes the first x bits
3304 * of the @a input_string.
3306 * @param input_string string.
3307 * @param string_len length of the @a input_string.
3308 * @param key pointer to where to write the hash code.
3309 * @return number of bits of @a input_string that have been consumed
3310 * to construct the key
3313 REGEX_INTERNAL_get_first_key (const char *input_string,
3315 struct GNUNET_HashCode *key)
3319 size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len :
3320 GNUNET_REGEX_INITIAL_BYTES;
3321 if (NULL == input_string)
3323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3324 "Given input string was NULL!\n");
3327 GNUNET_CRYPTO_hash (input_string, size, key);
3334 * Recursive function that calls the iterator for each synthetic start state.
3336 * @param min_len minimum length of the path in the graph.
3337 * @param max_len maximum length of the path in the graph.
3338 * @param consumed_string string consumed by traversing the graph till this state.
3339 * @param state current state of the automaton.
3340 * @param iterator iterator function called for each edge.
3341 * @param iterator_cls closure for the @a iterator function.
3344 iterate_initial_edge (unsigned int min_len,
3345 unsigned int max_len,
3346 char *consumed_string,
3347 struct REGEX_INTERNAL_State *state,
3348 REGEX_INTERNAL_KeyIterator iterator,
3352 struct REGEX_INTERNAL_Transition *t;
3353 unsigned int num_edges = state->transition_count;
3354 struct REGEX_BLOCK_Edge edges[num_edges];
3355 struct REGEX_BLOCK_Edge edge[1];
3356 struct GNUNET_HashCode hash;
3357 struct GNUNET_HashCode hash_new;
3358 unsigned int cur_len;
3360 if (NULL != consumed_string)
3361 cur_len = strlen (consumed_string);
3365 if ( ( (cur_len >= min_len) ||
3366 (GNUNET_YES == state->accepting) ) &&
3368 (NULL != consumed_string) )
3370 if (cur_len <= max_len)
3372 if ( (NULL != state->proof) &&
3373 (0 != strcmp (consumed_string,
3376 (void) state_get_edges (state, edges);
3377 GNUNET_CRYPTO_hash (consumed_string,
3378 strlen (consumed_string),
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 "Start state for string `%s' is %s\n",
3383 GNUNET_h2s (&hash));
3384 iterator (iterator_cls,
3391 if ( (GNUNET_YES == state->accepting) &&
3393 (state->transition_count < 1) &&
3394 (cur_len < max_len) )
3396 /* Special case for regex consisting of just a string that is shorter than
3398 edge[0].label = &consumed_string[cur_len - 1];
3399 edge[0].destination = state->hash;
3400 temp = GNUNET_strdup (consumed_string);
3401 temp[cur_len - 1] = '\0';
3402 GNUNET_CRYPTO_hash (temp,
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3406 "Start state for short string `%s' is %s\n",
3408 GNUNET_h2s (&hash_new));
3409 iterator (iterator_cls,
3417 else /* cur_len > max_len */
3419 /* Case where the concatenated labels are longer than max_len, then split. */
3420 edge[0].label = &consumed_string[max_len];
3421 edge[0].destination = state->hash;
3422 temp = GNUNET_strdup (consumed_string);
3423 temp[max_len] = '\0';
3424 GNUNET_CRYPTO_hash (temp, max_len, &hash);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3426 "Start state at split edge `%s'-`%s` is %s\n",
3429 GNUNET_h2s (&hash_new));
3430 iterator (iterator_cls,
3440 if (cur_len < max_len)
3442 for (t = state->transitions_head; NULL != t; t = t->next)
3444 if (NULL != strchr (t->label,
3447 /* Wildcards not allowed during starting states */
3451 if (NULL != consumed_string)
3452 GNUNET_asprintf (&temp,
3457 GNUNET_asprintf (&temp,
3460 iterate_initial_edge (min_len,
3473 * Iterate over all edges starting from start state of automaton 'a'. Calling
3474 * iterator for each edge.
3476 * @param a automaton.
3477 * @param iterator iterator called for each edge.
3478 * @param iterator_cls closure.
3481 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
3482 REGEX_INTERNAL_KeyIterator iterator,
3485 struct REGEX_INTERNAL_State *s;
3487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3488 "Iterating over starting edges\n");
3489 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES,
3490 GNUNET_REGEX_INITIAL_BYTES,
3492 iterator, iterator_cls);
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 "Iterating over DFA edges\n");
3495 for (s = a->states_head; NULL != s; s = s->next)
3497 struct REGEX_BLOCK_Edge edges[s->transition_count];
3498 unsigned int num_edges;
3500 num_edges = state_get_edges (s, edges);
3501 if ( ( (NULL != s->proof) &&
3502 (0 < strlen (s->proof)) ) || s->accepting)
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3505 "Creating DFA edges at `%s' under key %s\n",
3507 GNUNET_h2s (&s->hash));
3508 iterator (iterator_cls, &s->hash, s->proof,
3512 s->marked = GNUNET_NO;
3518 * Struct to hold all the relevant state information in the HashMap.
3520 * Contains the same info as the Regex Iterator parametes except the key,
3521 * which comes directly from the HashMap iterator.
3523 struct temporal_state_store {
3528 struct REGEX_BLOCK_Edge *edges;
3533 * Store regex iterator and cls in one place to pass to the hashmap iterator.
3535 struct client_iterator {
3536 REGEX_INTERNAL_KeyIterator iterator;
3542 * Iterator over all edges of a dfa. Stores all of them in a HashMap
3543 * for later reachability marking.
3545 * @param cls Closure (HashMap)
3546 * @param key hash for current state.
3547 * @param proof proof for current state
3548 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
3549 * @param num_edges number of edges leaving current state.
3550 * @param edges edges leaving current state.
3553 store_all_states (void *cls,
3554 const struct GNUNET_HashCode *key,
3557 unsigned int num_edges,
3558 const struct REGEX_BLOCK_Edge *edges)
3560 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3561 struct temporal_state_store *tmp;
3564 tmp = GNUNET_new (struct temporal_state_store);
3565 tmp->reachable = GNUNET_NO;
3566 tmp->proof = GNUNET_strdup (proof);
3567 tmp->accepting = accepting;
3568 tmp->num_edges = num_edges;
3569 edges_size = sizeof (struct REGEX_BLOCK_Edge) * num_edges;
3570 tmp->edges = GNUNET_malloc (edges_size);
3571 GNUNET_memcpy(tmp->edges, edges, edges_size);
3572 GNUNET_CONTAINER_multihashmap_put (hm, key, tmp,
3573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3578 * Mark state as reachable and call recursively on all its edges.
3580 * If already marked as reachable, do nothing.
3582 * @param state State to mark as reachable.
3583 * @param hm HashMap which stores all the states indexed by key.
3586 mark_as_reachable (struct temporal_state_store *state,
3587 struct GNUNET_CONTAINER_MultiHashMap *hm)
3589 struct temporal_state_store *child;
3592 if (GNUNET_YES == state->reachable)
3596 state->reachable = GNUNET_YES;
3597 for (i = 0; i < state->num_edges; i++)
3599 child = GNUNET_CONTAINER_multihashmap_get (hm,
3600 &state->edges[i].destination);
3606 mark_as_reachable (child, hm);
3612 * Iterator over hash map entries to mark the ones that are reachable.
3614 * @param cls closure
3615 * @param key current key code
3616 * @param value value in the hash map
3617 * @return #GNUNET_YES if we should continue to iterate,
3618 * #GNUNET_NO if not.
3621 reachability_iterator (void *cls,
3622 const struct GNUNET_HashCode *key,
3625 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3626 struct temporal_state_store *state = value;
3628 if (GNUNET_YES == state->reachable)
3629 /* already visited and marked */
3632 if (GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof) &&
3633 GNUNET_NO == state->accepting)
3634 /* not directly reachable */
3637 mark_as_reachable (state, hm);
3643 * Iterator over hash map entries.
3644 * Calling the callback on the ones marked as reachables.
3646 * @param cls closure
3647 * @param key current key code
3648 * @param value value in the hash map
3649 * @return #GNUNET_YES if we should continue to iterate,
3650 * #GNUNET_NO if not.
3653 iterate_reachables (void *cls,
3654 const struct GNUNET_HashCode *key,
3657 struct client_iterator *ci = cls;
3658 struct temporal_state_store *state = value;
3660 if (GNUNET_YES == state->reachable)
3662 ci->iterator (ci->iterator_cls, key,
3663 state->proof, state->accepting,
3664 state->num_edges, state->edges);
3666 GNUNET_free (state->edges);
3667 GNUNET_free (state->proof);
3668 GNUNET_free (state);
3674 * Iterate over all edges of automaton 'a' that are reachable from a state with
3675 * a proof of at least GNUNET_REGEX_INITIAL_BYTES characters.
3677 * Call the iterator for each such edge.
3679 * @param a automaton.
3680 * @param iterator iterator called for each reachable edge.
3681 * @param iterator_cls closure.
3684 REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a,
3685 REGEX_INTERNAL_KeyIterator iterator,
3688 struct GNUNET_CONTAINER_MultiHashMap *hm;
3689 struct client_iterator ci;
3691 hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO);
3692 ci.iterator = iterator;
3693 ci.iterator_cls = iterator_cls;
3695 REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm);
3696 GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm);
3697 GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci);
3699 GNUNET_CONTAINER_multihashmap_destroy (hm);
3703 /* end of regex_internal.c */