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] = '}';
419 static struct State *
420 dfa_move (struct State *s, const char literal)
422 struct Transition *t;
430 for (t = s->transitions_head; NULL != t; t = t->next)
432 if (literal == t->literal)
443 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
446 * struct StateSet *unreachable;
447 * struct State *stack[a->size];
450 * unreachable = GNUNET_malloc (sizeof (struct StateSet));
452 * // 1. add all states to unreachable set
453 * for (s = a->states_head; NULL != s; s = s->next)
455 * GNUNET_array_append (unreachable->states, unreachable->len; s);
458 * // 2. traverse dfa from start state and remove every visited state from unreachable set
461 * while (stack->len > 0)
463 * s = stack->states[stack->len - 1];
464 * GNUNET_array_grow (stack->states; stack->len; stack->len-1);
466 * for (t = s->transitions_head; NULL != t; t = t->next)
471 * // 3. delete all states that are still in the unreachable set
476 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
479 struct State *s_check;
480 struct Transition *t;
481 struct Transition *t_check;
484 GNUNET_assert (DFA == a->type);
486 for (s = a->states_head; NULL != s; s = s->next)
492 for (t = s->transitions_head; NULL != t; t = t->next)
494 if (NULL != t->state && t->state != s)
504 // state s is dead, remove it
505 // 1. remove all transitions to this state
506 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
508 for (t_check = s_check->transitions_head; NULL != t_check;
509 t_check = t_check->next)
511 if (t_check->state == s)
513 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
514 s_check->transitions_tail, t_check);
519 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
524 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Automaton *a)
530 dfa_minimize (struct GNUNET_REGEX_Automaton *a)
532 GNUNET_assert (DFA == a->type);
534 // 1. remove unreachable states
535 dfa_remove_unreachable_states (a);
537 // 2. remove dead states
538 dfa_remove_dead_states (a);
540 // 3. Merge nondistinguishable states
541 dfa_merge_nondistinguishable_states (a);
545 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
547 * @param start starting state
548 * @param end end state
550 * @return new NFA fragment
552 static struct GNUNET_REGEX_Automaton *
553 nfa_fragment_create (struct State *start, struct State *end)
555 struct GNUNET_REGEX_Automaton *n;
557 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
563 if (NULL == start && NULL == end)
566 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, end);
567 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, start);
576 * Adds a list of states to the given automaton 'n'.
578 * @param n automaton to which the states should be added
579 * @param states_head head of the DLL of states
580 * @param states_tail tail of the DLL of states
583 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
584 struct State *states_tail)
586 if (NULL == n || NULL == states_head)
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
592 if (NULL == n->states_head)
594 n->states_head = states_head;
595 n->states_tail = states_tail;
599 if (NULL != states_head)
601 n->states_tail->next = states_head;
602 n->states_tail = states_tail;
607 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
610 * @param accepting is it an accepting state or not
612 * @return new NFA state
614 static struct State *
615 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
619 s = GNUNET_malloc (sizeof (struct State));
620 s->id = ctx->state_id++;
621 s->accepting = accepting;
624 GNUNET_asprintf (&s->name, "s%i", s->id);
630 * Calculates the NFA closure set for the given state
632 * @param s starting point state
633 * @param literal transitioning literal on which to base the closure on,
634 * pass 0 for epsilon transition
636 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
638 static struct StateSet *
639 nfa_closure_create (struct State *s, const char literal)
641 struct StateSet *cls;
642 struct StateSet *cls_check;
643 struct State *clsstate;
644 struct State *currentstate;
645 struct Transition *ctran;
650 cls = GNUNET_malloc (sizeof (struct StateSet));
651 cls_check = GNUNET_malloc (sizeof (struct StateSet));
653 // Add start state to closure only for epsilon closure
655 GNUNET_array_append (cls->states, cls->len, s);
657 GNUNET_array_append (cls_check->states, cls_check->len, s);
658 while (cls_check->len > 0)
660 currentstate = cls_check->states[cls_check->len - 1];
661 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
663 for (ctran = currentstate->transitions_head; NULL != ctran;
666 if (NULL != ctran->state && literal == ctran->literal)
668 clsstate = ctran->state;
670 if (NULL != clsstate &&
671 GNUNET_YES != state_set_contains (cls, clsstate))
673 GNUNET_array_append (cls->states, cls->len, clsstate);
674 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
679 GNUNET_assert (0 == cls_check->len);
680 GNUNET_free (cls_check);
683 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
689 * Calculates the closure set for the given set of states.
691 * @param states list of states on which to base the closure on
692 * @param literal transitioning literal for which to base the closure on,
693 * pass 0 for epsilon transition
695 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
697 static struct StateSet *
698 nfa_closure_set_create (struct StateSet *states, const char literal)
701 struct StateSet *sset;
702 struct StateSet *cls;
711 cls = GNUNET_malloc (sizeof (struct StateSet));
713 for (i = 0; i < states->len; i++)
715 s = states->states[i];
716 sset = nfa_closure_create (s, literal);
718 for (j = 0; j < sset->len; j++)
721 for (k = 0; k < cls->len; k++)
723 if (sset->states[j]->id == cls->states[k]->id)
730 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
732 state_set_clear (sset);
736 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
742 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
747 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
749 struct GNUNET_REGEX_Automaton *a;
750 struct GNUNET_REGEX_Automaton *b;
751 struct GNUNET_REGEX_Automaton *new;
754 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
756 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
758 add_transition (ctx, a->end, 0, b->start);
759 a->end->accepting = 0;
760 b->end->accepting = 1;
762 new = nfa_fragment_create (NULL, NULL);
763 nfa_add_states (new, a->states_head, a->states_tail);
764 nfa_add_states (new, b->states_head, b->states_tail);
765 new->start = a->start;
767 automaton_fragment_clear (a);
768 automaton_fragment_clear (b);
770 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
774 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
779 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
781 struct GNUNET_REGEX_Automaton *a;
782 struct GNUNET_REGEX_Automaton *new;
787 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
792 "nfa_add_star_op failed, because there was no element on the stack");
796 start = nfa_state_create (ctx, 0);
797 end = nfa_state_create (ctx, 1);
799 add_transition (ctx, start, 0, a->start);
800 add_transition (ctx, start, 0, end);
801 add_transition (ctx, a->end, 0, a->start);
802 add_transition (ctx, a->end, 0, end);
804 a->end->accepting = 0;
807 new = nfa_fragment_create (start, end);
808 nfa_add_states (new, a->states_head, a->states_tail);
809 automaton_fragment_clear (a);
811 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
815 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
820 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
822 struct GNUNET_REGEX_Automaton *a;
825 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
827 add_transition (ctx, a->end, 0, a->start);
829 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
833 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
834 * that alternates between a and b (a|b)
839 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
841 struct GNUNET_REGEX_Automaton *a;
842 struct GNUNET_REGEX_Automaton *b;
843 struct GNUNET_REGEX_Automaton *new;
848 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
850 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
852 start = nfa_state_create (ctx, 0);
853 end = nfa_state_create (ctx, 1);
854 add_transition (ctx, start, 0, a->start);
855 add_transition (ctx, start, 0, b->start);
857 add_transition (ctx, a->end, 0, end);
858 add_transition (ctx, b->end, 0, end);
860 a->end->accepting = 0;
861 b->end->accepting = 0;
864 new = nfa_fragment_create (start, end);
865 nfa_add_states (new, a->states_head, a->states_tail);
866 nfa_add_states (new, b->states_head, b->states_tail);
867 automaton_fragment_clear (a);
868 automaton_fragment_clear (b);
870 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
874 * Adds a new nfa fragment to the stack
877 * @param lit literal for nfa transition
880 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
882 struct GNUNET_REGEX_Automaton *n;
886 GNUNET_assert (NULL != ctx);
888 start = nfa_state_create (ctx, 0);
889 end = nfa_state_create (ctx, 1);
890 add_transition (ctx, start, lit, end);
891 n = nfa_fragment_create (start, end);
892 GNUNET_assert (NULL != n);
893 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
897 * Construct an NFA by parsing the regex string of length 'len'.
899 * @param regex regular expression string
900 * @param len length of the string
902 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
904 struct GNUNET_REGEX_Automaton *
905 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
907 struct GNUNET_REGEX_Context ctx;
908 struct GNUNET_REGEX_Automaton *nfa;
912 unsigned int altcount;
913 unsigned int atomcount;
921 GNUNET_REGEX_context_init (&ctx);
930 for (count = 0; count < len && *regexp; count++, regexp++)
938 nfa_add_concatenation (&ctx);
940 GNUNET_array_grow (p, pcount, pcount + 1);
941 p[pcount - 1].altcount = altcount;
942 p[pcount - 1].atomcount = atomcount;
949 error_msg = "Cannot append '|' to nothing";
952 while (--atomcount > 0)
953 nfa_add_concatenation (&ctx);
959 error_msg = "Missing opening '('";
966 altcount = p[pcount].altcount;
967 atomcount = p[pcount].atomcount;
970 while (--atomcount > 0)
971 nfa_add_concatenation (&ctx);
972 for (; altcount > 0; altcount--)
973 nfa_add_alternation (&ctx);
975 altcount = p[pcount].altcount;
976 atomcount = p[pcount].atomcount;
982 error_msg = "Cannot append '+' to nothing";
985 nfa_add_star_op (&ctx);
990 error_msg = "Cannot append '+' to nothing";
993 nfa_add_plus_op (&ctx);
995 case 92: /* escape: \ */
1002 nfa_add_concatenation (&ctx);
1004 nfa_add_literal (&ctx, *regexp);
1011 error_msg = "Unbalanced parenthesis";
1014 while (--atomcount > 0)
1015 nfa_add_concatenation (&ctx);
1016 for (; altcount > 0; altcount--)
1017 nfa_add_alternation (&ctx);
1022 nfa = ctx.stack_tail;
1023 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1026 if (NULL != ctx.stack_head)
1028 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Created NFA with %i States and a total of %i Transitions\n",
1034 ctx.state_id, ctx.transition_id);
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1040 if (NULL != error_msg)
1041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1043 while (NULL != ctx.stack_tail)
1045 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1046 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1053 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1056 * @param a automaton to be destroyed
1059 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1062 struct State *next_state;
1067 for (s = a->states_head; NULL != s;)
1069 next_state = s->next;
1070 automaton_destroy_state (s);
1078 * Construct DFA for the given 'regex' of length 'len'
1080 * @param regex regular expression string
1081 * @param len length of the regular expression
1083 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1085 struct GNUNET_REGEX_Automaton *
1086 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1088 struct GNUNET_REGEX_Context ctx;
1089 struct GNUNET_REGEX_Automaton *dfa;
1090 struct GNUNET_REGEX_Automaton *nfa;
1091 struct StateSet *tmp;
1092 struct StateSet *nfa_set;
1093 struct StateSet *dfa_stack;
1094 struct Transition *ctran;
1095 struct State *dfa_state;
1096 struct State *new_dfa_state;
1097 struct State *state_contains;
1098 struct State *state_iter;
1100 GNUNET_REGEX_context_init (&ctx);
1103 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1105 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1108 // Create DFA start state from epsilon closure
1109 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1110 nfa_set = nfa_closure_create (nfa->start, 0);
1111 dfa->start = dfa_state_create (&ctx, nfa_set);
1112 GNUNET_CONTAINER_DLL_insert (dfa->states_head, dfa->states_tail, dfa->start);
1113 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1114 while (dfa_stack->len > 0)
1116 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1117 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1119 for (ctran = dfa_state->transitions_head; NULL != ctran;
1120 ctran = ctran->next)
1122 if (0 != ctran->literal && NULL == ctran->state)
1124 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1125 nfa_set = nfa_closure_set_create (tmp, 0);
1126 state_set_clear (tmp);
1127 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1128 state_contains = NULL;
1129 for (state_iter = dfa->states_head; NULL != state_iter;
1130 state_iter = state_iter->next)
1133 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1134 state_contains = state_iter;
1137 if (NULL == state_contains)
1139 GNUNET_CONTAINER_DLL_insert_tail (dfa->states_head, dfa->states_tail,
1141 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1143 ctran->state = new_dfa_state;
1147 ctran->state = state_contains;
1148 automaton_destroy_state (new_dfa_state);
1154 GNUNET_free (dfa_stack);
1155 GNUNET_REGEX_automaton_destroy (nfa);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA with %i States\n",
1166 * Save the given automaton as a GraphViz dot file
1168 * @param a the automaton to be saved
1169 * @param filename where to save the file
1172 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1173 const char *filename)
1176 struct Transition *ctran;
1178 char *s_tran = NULL;
1185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1189 if (NULL == filename || strlen (filename) < 1)
1191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1195 p = fopen (filename, "w");
1199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1204 start = "digraph G {\nrankdir=LR\n";
1205 fwrite (start, strlen (start), 1, p);
1207 for (s = a->states_head; NULL != s; s = s->next)
1211 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1212 fwrite (s_acc, strlen (s_acc), 1, p);
1213 GNUNET_free (s_acc);
1218 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1220 if (NULL == ctran->state)
1222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1223 "Transition from State %i has has no state for transitioning\n",
1228 if (ctran->literal == 0)
1230 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1231 s->name, ctran->state->name);
1235 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1236 s->name, ctran->state->name, ctran->literal);
1239 fwrite (s_tran, strlen (s_tran), 1, p);
1240 GNUNET_free (s_tran);
1245 fwrite (end, strlen (end), 1, p);
1250 * Evaluates the given string using the given DFA automaton
1252 * @param a automaton, type must be DFA
1253 * @param string string that should be evaluated
1255 * @return 0 if string matches, non 0 otherwise
1258 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1266 "Tried to evaluate DFA, but NFA automaton given");
1272 for (strp = string; NULL != strp && *strp; strp++)
1274 s = dfa_move (s, *strp);
1279 if (NULL != s && s->accepting)
1286 * Evaluates the given string using the given NFA automaton
1288 * @param a automaton, type must be NFA
1289 * @param string string that should be evaluated
1291 * @return 0 if string matches, non 0 otherwise
1294 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1298 struct StateSet *sset;
1299 struct StateSet *new_sset;
1305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1306 "Tried to evaluate NFA, but DFA automaton given");
1312 sset = nfa_closure_create (a->start, 0);
1314 for (strp = string; NULL != strp && *strp; strp++)
1316 new_sset = nfa_closure_set_create (sset, *strp);
1317 state_set_clear (sset);
1318 sset = nfa_closure_set_create (new_sset, 0);
1319 state_set_clear (new_sset);
1322 for (i = 0; i < sset->len; i++)
1324 s = sset->states[i];
1325 if (NULL != s && s->accepting)
1332 state_set_clear (sset);
1338 * Evaluates the given 'string' against the given compiled regex
1340 * @param a automaton
1341 * @param string string to check
1343 * @return 0 if string matches, non 0 otherwise
1346 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1353 result = evaluate_dfa (a, string);
1356 result = evaluate_nfa (a, string);
1359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1360 "Evaluating regex failed, automaton has no type!\n");
1361 result = GNUNET_SYSERR;