2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file src/regex/regex.c
22 * @brief library to create automatons from regular expressions
23 * @author Maximilian Szengel
26 #include "gnunet_container_lib.h"
27 #include "gnunet_regex_lib.h"
31 * Context that contains an id counter for states and transitions
32 * as well as a DLL of automatons used as a stack for NFA construction.
34 struct GNUNET_REGEX_Context
36 unsigned int state_id;
37 unsigned int transition_id;
40 * DLL of GNUNET_REGEX_Automaton's used as a stack
42 struct GNUNET_REGEX_Automaton *stack_head;
43 struct GNUNET_REGEX_Automaton *stack_tail;
46 enum GNUNET_REGEX_automaton_type
53 * Automaton representation
55 struct GNUNET_REGEX_Automaton
57 struct GNUNET_REGEX_Automaton *prev;
58 struct GNUNET_REGEX_Automaton *next;
63 unsigned int state_count;
64 struct State *states_head;
65 struct State *states_tail;
67 enum GNUNET_REGEX_automaton_type type;
71 * A state. Can be used in DFA and NFA automatons.
83 unsigned int transition_count;
84 struct Transition *transitions_head;
85 struct Transition *transitions_tail;
87 struct StateSet *nfa_set;
91 * Transition between two states. Each state can have 0-n transitions.
92 * If literal is 0, this is considered to be an epsilon transition.
96 struct Transition *prev;
97 struct Transition *next;
112 struct State **states;
117 * Initialize a new context
122 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
130 ctx->transition_id = 0;
131 ctx->stack_head = NULL;
132 ctx->stack_tail = NULL;
136 debug_print_state (struct State *s)
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
140 s->marked, s->accepting);
144 debug_print_states (struct StateSet *sset)
149 for (i = 0; i < sset->len; i++)
152 debug_print_state (s);
157 debug_print_transitions (struct State *s)
159 struct Transition *t;
163 for (t = s->transitions_head; NULL != t; t = t->next)
168 literal = t->literal;
170 if (NULL == t->state)
173 state = t->state->name;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
181 state_compare (const void *a, const void *b)
186 s1 = (struct State **) a;
187 s2 = (struct State **) b;
189 return (*s1)->id - (*s2)->id;
193 * Compare to state sets by comparing the id's of the states that are
194 * contained in each set. Both sets are expected to be sorted by id!
196 * @param sset1 first state set
197 * @param sset2 second state set
199 * @return 0 if they are equal, non 0 otherwise
202 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
206 if (sset1->len != sset2->len)
209 for (i = 0; i < sset1->len; i++)
211 if (sset1->states[i]->id != sset2->states[i]->id)
220 * Checks if 'elem' is contained in 'set'
222 * @param set set of states
225 * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
228 state_set_contains (struct StateSet *set, struct State *elem)
233 for (i = 0; i < set->len; i++)
236 if (0 == memcmp (s, elem, sizeof (struct State)))
243 * Clears the given StateSet 'set'
245 * @param set set to be cleared
248 state_set_clear (struct StateSet *set)
252 if (NULL != set->states)
253 GNUNET_free (set->states);
259 * Adds a transition from one state to another on 'literal'
262 * @param from_state starting state for the transition
263 * @param literal transition label
264 * @param to_state state to where the transition should point to
267 add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
268 const char literal, struct State *to_state)
270 struct Transition *t;
272 if (NULL == from_state)
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
278 t = GNUNET_malloc (sizeof (struct Transition));
280 t->id = ctx->transition_id++;
281 t->literal = literal;
284 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
285 from_state->transitions_tail, t);
289 * Clears an automaton fragment. Does not destroy the states inside
292 * @param a automaton to be cleared
295 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
299 a->states_head = NULL;
300 a->states_tail = NULL;
305 * Frees the memory used by State 's'
307 * @param s state that should be destroyed
310 automaton_destroy_state (struct State *s)
312 struct Transition *t;
313 struct Transition *next_t;
316 GNUNET_free (s->name);
318 for (t = s->transitions_head; NULL != t;)
321 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
326 state_set_clear (s->nfa_set);
332 * Creates a new DFA state based on a set of NFA states. Needs to be freed
333 * using automaton_destroy_state.
336 * @param nfa_states set of NFA states on which the DFA should be based on
338 * @return new DFA state
340 static struct State *
341 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
346 struct State *cstate;
347 struct Transition *ctran;
349 struct Transition *t;
352 s = GNUNET_malloc (sizeof (struct State));
353 s->id = ctx->state_id++;
358 if (NULL == nfa_states)
360 GNUNET_asprintf (&s->name, "s%i", s->id);
364 s->nfa_set = nfa_states;
366 if (nfa_states->len < 1)
369 // Create a name based on 'sset'
370 s->name = GNUNET_malloc (sizeof (char) * 2);
371 strcat (s->name, "{");
374 for (i = 0; i < nfa_states->len; i++)
376 cstate = nfa_states->states[i];
377 GNUNET_asprintf (&name, "%i,", cstate->id);
381 len = strlen (s->name) + strlen (name) + 1;
382 s->name = GNUNET_realloc (s->name, len);
383 strcat (s->name, name);
388 // Add a transition for each distinct literal to NULL state
389 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
391 if (0 != ctran->literal)
395 for (t = s->transitions_head; NULL != t; t = t->next)
397 if (t->literal == ctran->literal)
405 add_transition (ctx, s, ctran->literal, NULL);
409 // If the nfa_states contain an accepting state, the new dfa state is also accepting
410 if (cstate->accepting)
414 s->name[strlen (s->name) - 1] = '}';
420 * Move from the given state 's' to the next state on
421 * transition 'literal'
423 * @param s starting state
424 * @param literal edge label to follow
426 * @return new state or NULL, if transition on literal not possible
428 static struct State *
429 dfa_move (struct State *s, const char literal)
431 struct Transition *t;
439 for (t = s->transitions_head; NULL != t; t = t->next)
441 if (literal == t->literal)
452 * Remove all unreachable states from DFA 'a'. Unreachable states
453 * are those states that are not reachable from the starting state.
455 * @param a DFA automaton
458 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
461 * struct StateSet *unreachable;
462 * struct State *stack[a->size];
465 * unreachable = GNUNET_malloc (sizeof (struct StateSet));
467 * // 1. add all states to unreachable set
468 * for (s = a->states_head; NULL != s; s = s->next)
470 * GNUNET_array_append (unreachable->states, unreachable->len; s);
473 * // 2. traverse dfa from start state and remove every visited state from unreachable set
476 * while (stack->len > 0)
478 * s = stack->states[stack->len - 1];
479 * GNUNET_array_grow (stack->states; stack->len; stack->len-1);
481 * for (t = s->transitions_head; NULL != t; t = t->next)
486 * // 3. delete all states that are still in the unreachable set
491 * Remove all dead states from the DFA 'a'. Dead states are those
492 * states that do not transition to any other state but themselfes.
494 * @param a DFA automaton
497 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
500 struct State *s_check;
501 struct Transition *t;
502 struct Transition *t_check;
505 GNUNET_assert (DFA == a->type);
507 for (s = a->states_head; NULL != s; s = s->next)
513 for (t = s->transitions_head; NULL != t; t = t->next)
515 if (NULL != t->state && t->state != s)
525 // state s is dead, remove it
526 // 1. remove all transitions to this state
527 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
529 for (t_check = s_check->transitions_head; NULL != t_check;
530 t_check = t_check->next)
532 if (t_check->state == s)
534 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
535 s_check->transitions_tail, t_check);
540 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
545 * Merge all non distinguishable states in the DFA 'a'
547 * @param a DFA automaton
550 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Automaton *a)
556 * Minimize the given DFA 'a' by removing all unreachable states,
557 * removing all dead states and merging all non distinguishable states
559 * @param a DFA automaton
562 dfa_minimize (struct GNUNET_REGEX_Automaton *a)
564 GNUNET_assert (DFA == a->type);
566 // 1. remove unreachable states
567 dfa_remove_unreachable_states (a);
569 // 2. remove dead states
570 dfa_remove_dead_states (a);
572 // 3. Merge nondistinguishable states
573 dfa_merge_nondistinguishable_states (a);
577 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
579 * @param start starting state
580 * @param end end state
582 * @return new NFA fragment
584 static struct GNUNET_REGEX_Automaton *
585 nfa_fragment_create (struct State *start, struct State *end)
587 struct GNUNET_REGEX_Automaton *n;
589 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
595 if (NULL == start && NULL == end)
598 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, end);
599 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, start);
608 * Adds a list of states to the given automaton 'n'.
610 * @param n automaton to which the states should be added
611 * @param states_head head of the DLL of states
612 * @param states_tail tail of the DLL of states
615 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
616 struct State *states_tail)
618 if (NULL == n || NULL == states_head)
620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
624 if (NULL == n->states_head)
626 n->states_head = states_head;
627 n->states_tail = states_tail;
631 if (NULL != states_head)
633 n->states_tail->next = states_head;
634 n->states_tail = states_tail;
639 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
642 * @param accepting is it an accepting state or not
644 * @return new NFA state
646 static struct State *
647 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
651 s = GNUNET_malloc (sizeof (struct State));
652 s->id = ctx->state_id++;
653 s->accepting = accepting;
656 GNUNET_asprintf (&s->name, "s%i", s->id);
662 * Calculates the NFA closure set for the given state
664 * @param s starting point state
665 * @param literal transitioning literal on which to base the closure on,
666 * pass 0 for epsilon transition
668 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
670 static struct StateSet *
671 nfa_closure_create (struct State *s, const char literal)
673 struct StateSet *cls;
674 struct StateSet *cls_check;
675 struct State *clsstate;
676 struct State *currentstate;
677 struct Transition *ctran;
682 cls = GNUNET_malloc (sizeof (struct StateSet));
683 cls_check = GNUNET_malloc (sizeof (struct StateSet));
685 // Add start state to closure only for epsilon closure
687 GNUNET_array_append (cls->states, cls->len, s);
689 GNUNET_array_append (cls_check->states, cls_check->len, s);
690 while (cls_check->len > 0)
692 currentstate = cls_check->states[cls_check->len - 1];
693 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
695 for (ctran = currentstate->transitions_head; NULL != ctran;
698 if (NULL != ctran->state && literal == ctran->literal)
700 clsstate = ctran->state;
702 if (NULL != clsstate &&
703 GNUNET_YES != state_set_contains (cls, clsstate))
705 GNUNET_array_append (cls->states, cls->len, clsstate);
706 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
711 GNUNET_assert (0 == cls_check->len);
712 GNUNET_free (cls_check);
715 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
721 * Calculates the closure set for the given set of states.
723 * @param states list of states on which to base the closure on
724 * @param literal transitioning literal for which to base the closure on,
725 * pass 0 for epsilon transition
727 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
729 static struct StateSet *
730 nfa_closure_set_create (struct StateSet *states, const char literal)
733 struct StateSet *sset;
734 struct StateSet *cls;
743 cls = GNUNET_malloc (sizeof (struct StateSet));
745 for (i = 0; i < states->len; i++)
747 s = states->states[i];
748 sset = nfa_closure_create (s, literal);
750 for (j = 0; j < sset->len; j++)
753 for (k = 0; k < cls->len; k++)
755 if (sset->states[j]->id == cls->states[k]->id)
762 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
764 state_set_clear (sset);
768 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
774 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
779 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
781 struct GNUNET_REGEX_Automaton *a;
782 struct GNUNET_REGEX_Automaton *b;
783 struct GNUNET_REGEX_Automaton *new;
786 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
788 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
790 add_transition (ctx, a->end, 0, b->start);
791 a->end->accepting = 0;
792 b->end->accepting = 1;
794 new = nfa_fragment_create (NULL, NULL);
795 nfa_add_states (new, a->states_head, a->states_tail);
796 nfa_add_states (new, b->states_head, b->states_tail);
797 new->start = a->start;
799 automaton_fragment_clear (a);
800 automaton_fragment_clear (b);
802 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
806 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
811 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
813 struct GNUNET_REGEX_Automaton *a;
814 struct GNUNET_REGEX_Automaton *new;
819 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 "nfa_add_star_op failed, because there was no element on the stack");
828 start = nfa_state_create (ctx, 0);
829 end = nfa_state_create (ctx, 1);
831 add_transition (ctx, start, 0, a->start);
832 add_transition (ctx, start, 0, end);
833 add_transition (ctx, a->end, 0, a->start);
834 add_transition (ctx, a->end, 0, end);
836 a->end->accepting = 0;
839 new = nfa_fragment_create (start, end);
840 nfa_add_states (new, a->states_head, a->states_tail);
841 automaton_fragment_clear (a);
843 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
847 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
852 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
854 struct GNUNET_REGEX_Automaton *a;
857 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
859 add_transition (ctx, a->end, 0, a->start);
861 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
865 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
866 * that alternates between a and b (a|b)
871 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
873 struct GNUNET_REGEX_Automaton *a;
874 struct GNUNET_REGEX_Automaton *b;
875 struct GNUNET_REGEX_Automaton *new;
880 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
882 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
884 start = nfa_state_create (ctx, 0);
885 end = nfa_state_create (ctx, 1);
886 add_transition (ctx, start, 0, a->start);
887 add_transition (ctx, start, 0, b->start);
889 add_transition (ctx, a->end, 0, end);
890 add_transition (ctx, b->end, 0, end);
892 a->end->accepting = 0;
893 b->end->accepting = 0;
896 new = nfa_fragment_create (start, end);
897 nfa_add_states (new, a->states_head, a->states_tail);
898 nfa_add_states (new, b->states_head, b->states_tail);
899 automaton_fragment_clear (a);
900 automaton_fragment_clear (b);
902 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
906 * Adds a new nfa fragment to the stack
909 * @param lit literal for nfa transition
912 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
914 struct GNUNET_REGEX_Automaton *n;
918 GNUNET_assert (NULL != ctx);
920 start = nfa_state_create (ctx, 0);
921 end = nfa_state_create (ctx, 1);
922 add_transition (ctx, start, lit, end);
923 n = nfa_fragment_create (start, end);
924 GNUNET_assert (NULL != n);
925 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
929 * Construct an NFA by parsing the regex string of length 'len'.
931 * @param regex regular expression string
932 * @param len length of the string
934 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
936 struct GNUNET_REGEX_Automaton *
937 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
939 struct GNUNET_REGEX_Context ctx;
940 struct GNUNET_REGEX_Automaton *nfa;
944 unsigned int altcount;
945 unsigned int atomcount;
953 GNUNET_REGEX_context_init (&ctx);
962 for (count = 0; count < len && *regexp; count++, regexp++)
970 nfa_add_concatenation (&ctx);
972 GNUNET_array_grow (p, pcount, pcount + 1);
973 p[pcount - 1].altcount = altcount;
974 p[pcount - 1].atomcount = atomcount;
981 error_msg = "Cannot append '|' to nothing";
984 while (--atomcount > 0)
985 nfa_add_concatenation (&ctx);
991 error_msg = "Missing opening '('";
998 altcount = p[pcount].altcount;
999 atomcount = p[pcount].atomcount;
1002 while (--atomcount > 0)
1003 nfa_add_concatenation (&ctx);
1004 for (; altcount > 0; altcount--)
1005 nfa_add_alternation (&ctx);
1007 altcount = p[pcount].altcount;
1008 atomcount = p[pcount].atomcount;
1014 error_msg = "Cannot append '+' to nothing";
1017 nfa_add_star_op (&ctx);
1022 error_msg = "Cannot append '+' to nothing";
1025 nfa_add_plus_op (&ctx);
1027 case 92: /* escape: \ */
1034 nfa_add_concatenation (&ctx);
1036 nfa_add_literal (&ctx, *regexp);
1043 error_msg = "Unbalanced parenthesis";
1046 while (--atomcount > 0)
1047 nfa_add_concatenation (&ctx);
1048 for (; altcount > 0; altcount--)
1049 nfa_add_alternation (&ctx);
1054 nfa = ctx.stack_tail;
1055 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1058 if (NULL != ctx.stack_head)
1060 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1068 if (NULL != error_msg)
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1071 while (NULL != ctx.stack_tail)
1073 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1074 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1081 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1084 * @param a automaton to be destroyed
1087 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1090 struct State *next_state;
1095 for (s = a->states_head; NULL != s;)
1097 next_state = s->next;
1098 automaton_destroy_state (s);
1106 * Construct DFA for the given 'regex' of length 'len'
1108 * @param regex regular expression string
1109 * @param len length of the regular expression
1111 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1113 struct GNUNET_REGEX_Automaton *
1114 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1116 struct GNUNET_REGEX_Context ctx;
1117 struct GNUNET_REGEX_Automaton *dfa;
1118 struct GNUNET_REGEX_Automaton *nfa;
1119 struct StateSet *tmp;
1120 struct StateSet *nfa_set;
1121 struct StateSet *dfa_stack;
1122 struct Transition *ctran;
1123 struct State *dfa_state;
1124 struct State *new_dfa_state;
1125 struct State *state_contains;
1126 struct State *state_iter;
1128 GNUNET_REGEX_context_init (&ctx);
1131 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1133 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1136 // Create DFA start state from epsilon closure
1137 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1138 nfa_set = nfa_closure_create (nfa->start, 0);
1139 dfa->start = dfa_state_create (&ctx, nfa_set);
1140 GNUNET_CONTAINER_DLL_insert (dfa->states_head, dfa->states_tail, dfa->start);
1141 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1142 while (dfa_stack->len > 0)
1144 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1145 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1147 for (ctran = dfa_state->transitions_head; NULL != ctran;
1148 ctran = ctran->next)
1150 if (0 != ctran->literal && NULL == ctran->state)
1152 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1153 nfa_set = nfa_closure_set_create (tmp, 0);
1154 state_set_clear (tmp);
1155 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1156 state_contains = NULL;
1157 for (state_iter = dfa->states_head; NULL != state_iter;
1158 state_iter = state_iter->next)
1161 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1162 state_contains = state_iter;
1165 if (NULL == state_contains)
1167 GNUNET_CONTAINER_DLL_insert_tail (dfa->states_head, dfa->states_tail,
1169 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1171 ctran->state = new_dfa_state;
1175 ctran->state = state_contains;
1176 automaton_destroy_state (new_dfa_state);
1182 GNUNET_free (dfa_stack);
1183 GNUNET_REGEX_automaton_destroy (nfa);
1191 * Save the given automaton as a GraphViz dot file
1193 * @param a the automaton to be saved
1194 * @param filename where to save the file
1197 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1198 const char *filename)
1201 struct Transition *ctran;
1203 char *s_tran = NULL;
1210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1214 if (NULL == filename || strlen (filename) < 1)
1216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1220 p = fopen (filename, "w");
1224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1229 start = "digraph G {\nrankdir=LR\n";
1230 fwrite (start, strlen (start), 1, p);
1232 for (s = a->states_head; NULL != s; s = s->next)
1236 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1237 fwrite (s_acc, strlen (s_acc), 1, p);
1238 GNUNET_free (s_acc);
1243 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1245 if (NULL == ctran->state)
1247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1248 "Transition from State %i has has no state for transitioning\n",
1253 if (ctran->literal == 0)
1255 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1256 s->name, ctran->state->name);
1260 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1261 s->name, ctran->state->name, ctran->literal);
1264 fwrite (s_tran, strlen (s_tran), 1, p);
1265 GNUNET_free (s_tran);
1270 fwrite (end, strlen (end), 1, p);
1275 * Evaluates the given string using the given DFA automaton
1277 * @param a automaton, type must be DFA
1278 * @param string string that should be evaluated
1280 * @return 0 if string matches, non 0 otherwise
1283 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1291 "Tried to evaluate DFA, but NFA automaton given");
1297 for (strp = string; NULL != strp && *strp; strp++)
1299 s = dfa_move (s, *strp);
1304 if (NULL != s && s->accepting)
1311 * Evaluates the given string using the given NFA automaton
1313 * @param a automaton, type must be NFA
1314 * @param string string that should be evaluated
1316 * @return 0 if string matches, non 0 otherwise
1319 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1323 struct StateSet *sset;
1324 struct StateSet *new_sset;
1330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1331 "Tried to evaluate NFA, but DFA automaton given");
1337 sset = nfa_closure_create (a->start, 0);
1339 for (strp = string; NULL != strp && *strp; strp++)
1341 new_sset = nfa_closure_set_create (sset, *strp);
1342 state_set_clear (sset);
1343 sset = nfa_closure_set_create (new_sset, 0);
1344 state_set_clear (new_sset);
1347 for (i = 0; i < sset->len; i++)
1349 s = sset->states[i];
1350 if (NULL != s && s->accepting)
1357 state_set_clear (sset);
1363 * Evaluates the given 'string' against the given compiled regex
1365 * @param a automaton
1366 * @param string string to check
1368 * @return 0 if string matches, non 0 otherwise
1371 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1378 result = evaluate_dfa (a, string);
1381 result = evaluate_nfa (a, string);
1384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1385 "Evaluating regex failed, automaton has no type!\n");
1386 result = GNUNET_SYSERR;