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;
306 * Frees the memory used by State 's'
308 * @param s state that should be destroyed
311 automaton_destroy_state (struct State *s)
313 struct Transition *t;
314 struct Transition *next_t;
317 GNUNET_free (s->name);
319 for (t = s->transitions_head; NULL != t;)
322 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
327 state_set_clear (s->nfa_set);
333 automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
337 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
339 automaton_destroy_state (ss);
343 automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
345 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
350 * Creates a new DFA state based on a set of NFA states. Needs to be freed
351 * using automaton_destroy_state.
354 * @param nfa_states set of NFA states on which the DFA should be based on
356 * @return new DFA state
358 static struct State *
359 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
364 struct State *cstate;
365 struct Transition *ctran;
367 struct Transition *t;
370 s = GNUNET_malloc (sizeof (struct State));
371 s->id = ctx->state_id++;
376 if (NULL == nfa_states)
378 GNUNET_asprintf (&s->name, "s%i", s->id);
382 s->nfa_set = nfa_states;
384 if (nfa_states->len < 1)
387 // Create a name based on 'sset'
388 s->name = GNUNET_malloc (sizeof (char) * 2);
389 strcat (s->name, "{");
392 for (i = 0; i < nfa_states->len; i++)
394 cstate = nfa_states->states[i];
395 GNUNET_asprintf (&name, "%i,", cstate->id);
399 len = strlen (s->name) + strlen (name) + 1;
400 s->name = GNUNET_realloc (s->name, len);
401 strcat (s->name, name);
406 // Add a transition for each distinct literal to NULL state
407 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
409 if (0 != ctran->literal)
413 for (t = s->transitions_head; NULL != t; t = t->next)
415 if (t->literal == ctran->literal)
423 add_transition (ctx, s, ctran->literal, NULL);
427 // If the nfa_states contain an accepting state, the new dfa state is also accepting
428 if (cstate->accepting)
432 s->name[strlen (s->name) - 1] = '}';
438 * Move from the given state 's' to the next state on
439 * transition 'literal'
441 * @param s starting state
442 * @param literal edge label to follow
444 * @return new state or NULL, if transition on literal not possible
446 static struct State *
447 dfa_move (struct State *s, const char literal)
449 struct Transition *t;
457 for (t = s->transitions_head; NULL != t; t = t->next)
459 if (literal == t->literal)
470 * Remove all unreachable states from DFA 'a'. Unreachable states
471 * are those states that are not reachable from the starting state.
473 * @param a DFA automaton
476 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
478 struct State *stack[a->state_count];
481 struct Transition *t;
485 // 1. unmark all states
486 for (s = a->states_head; NULL != s; s = s->next)
491 // 2. traverse dfa from start state and mark all visited states
492 stack[stack_len] = a->start;
494 while (stack_len > 0)
496 s = stack[stack_len-1];
498 s->marked = 1; // mark s as visited
499 for (t = s->transitions_head; NULL != t; t = t->next)
501 if (NULL != t->state && 0 == t->state->marked)
503 // add next states to stack
504 stack[stack_len] = t->state;
510 // 3. delete all states that were not visited
511 for (s = a->states_head; NULL != s; s = s->next)
514 automaton_remove_state (a, s);
519 * Remove all dead states from the DFA 'a'. Dead states are those
520 * states that do not transition to any other state but themselfes.
522 * @param a DFA automaton
525 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
528 struct State *s_check;
529 struct Transition *t;
530 struct Transition *t_check;
533 GNUNET_assert (DFA == a->type);
535 for (s = a->states_head; NULL != s; s = s->next)
541 for (t = s->transitions_head; NULL != t; t = t->next)
543 if (NULL != t->state && t->state != s)
553 // state s is dead, remove it
554 // 1. remove all transitions to this state
555 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
557 for (t_check = s_check->transitions_head; NULL != t_check;
558 t_check = t_check->next)
560 if (t_check->state == s)
562 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
563 s_check->transitions_tail, t_check);
568 automaton_remove_state (a, s);
573 * Merge all non distinguishable states in the DFA 'a'
575 * @param a DFA automaton
578 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Automaton *a)
584 * Minimize the given DFA 'a' by removing all unreachable states,
585 * removing all dead states and merging all non distinguishable states
587 * @param a DFA automaton
590 dfa_minimize (struct GNUNET_REGEX_Automaton *a)
592 GNUNET_assert (DFA == a->type);
594 // 1. remove unreachable states
595 dfa_remove_unreachable_states (a);
597 // 2. remove dead states
598 dfa_remove_dead_states (a);
600 // 3. Merge nondistinguishable states
601 dfa_merge_nondistinguishable_states (a);
605 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
607 * @param start starting state
608 * @param end end state
610 * @return new NFA fragment
612 static struct GNUNET_REGEX_Automaton *
613 nfa_fragment_create (struct State *start, struct State *end)
615 struct GNUNET_REGEX_Automaton *n;
617 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
623 if (NULL == start && NULL == end)
626 automaton_add_state (n, end);
627 automaton_add_state (n, start);
636 * Adds a list of states to the given automaton 'n'.
638 * @param n automaton to which the states should be added
639 * @param states_head head of the DLL of states
640 * @param states_tail tail of the DLL of states
643 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
644 struct State *states_tail)
648 if (NULL == n || NULL == states_head)
650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
654 if (NULL == n->states_head)
656 n->states_head = states_head;
657 n->states_tail = states_tail;
661 if (NULL != states_head)
663 n->states_tail->next = states_head;
664 n->states_tail = states_tail;
667 for (s = states_head; NULL != s; s = s->next)
672 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
675 * @param accepting is it an accepting state or not
677 * @return new NFA state
679 static struct State *
680 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
684 s = GNUNET_malloc (sizeof (struct State));
685 s->id = ctx->state_id++;
686 s->accepting = accepting;
689 GNUNET_asprintf (&s->name, "s%i", s->id);
695 * Calculates the NFA closure set for the given state
697 * @param s starting point state
698 * @param literal transitioning literal on which to base the closure on,
699 * pass 0 for epsilon transition
701 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
703 static struct StateSet *
704 nfa_closure_create (struct State *s, const char literal)
706 struct StateSet *cls;
707 struct StateSet *cls_check;
708 struct State *clsstate;
709 struct State *currentstate;
710 struct Transition *ctran;
715 cls = GNUNET_malloc (sizeof (struct StateSet));
716 cls_check = GNUNET_malloc (sizeof (struct StateSet));
718 // Add start state to closure only for epsilon closure
720 GNUNET_array_append (cls->states, cls->len, s);
722 GNUNET_array_append (cls_check->states, cls_check->len, s);
723 while (cls_check->len > 0)
725 currentstate = cls_check->states[cls_check->len - 1];
726 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
728 for (ctran = currentstate->transitions_head; NULL != ctran;
731 if (NULL != ctran->state && literal == ctran->literal)
733 clsstate = ctran->state;
735 if (NULL != clsstate &&
736 GNUNET_YES != state_set_contains (cls, clsstate))
738 GNUNET_array_append (cls->states, cls->len, clsstate);
739 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
744 GNUNET_assert (0 == cls_check->len);
745 GNUNET_free (cls_check);
748 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
754 * Calculates the closure set for the given set of states.
756 * @param states list of states on which to base the closure on
757 * @param literal transitioning literal for which to base the closure on,
758 * pass 0 for epsilon transition
760 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
762 static struct StateSet *
763 nfa_closure_set_create (struct StateSet *states, const char literal)
766 struct StateSet *sset;
767 struct StateSet *cls;
776 cls = GNUNET_malloc (sizeof (struct StateSet));
778 for (i = 0; i < states->len; i++)
780 s = states->states[i];
781 sset = nfa_closure_create (s, literal);
783 for (j = 0; j < sset->len; j++)
786 for (k = 0; k < cls->len; k++)
788 if (sset->states[j]->id == cls->states[k]->id)
795 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
797 state_set_clear (sset);
801 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
807 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
812 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
814 struct GNUNET_REGEX_Automaton *a;
815 struct GNUNET_REGEX_Automaton *b;
816 struct GNUNET_REGEX_Automaton *new;
819 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
821 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
823 add_transition (ctx, a->end, 0, b->start);
824 a->end->accepting = 0;
825 b->end->accepting = 1;
827 new = nfa_fragment_create (NULL, NULL);
828 nfa_add_states (new, a->states_head, a->states_tail);
829 nfa_add_states (new, b->states_head, b->states_tail);
830 new->start = a->start;
832 automaton_fragment_clear (a);
833 automaton_fragment_clear (b);
835 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
839 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
844 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
846 struct GNUNET_REGEX_Automaton *a;
847 struct GNUNET_REGEX_Automaton *new;
852 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
857 "nfa_add_star_op failed, because there was no element on the stack");
861 start = nfa_state_create (ctx, 0);
862 end = nfa_state_create (ctx, 1);
864 add_transition (ctx, start, 0, a->start);
865 add_transition (ctx, start, 0, end);
866 add_transition (ctx, a->end, 0, a->start);
867 add_transition (ctx, a->end, 0, end);
869 a->end->accepting = 0;
872 new = nfa_fragment_create (start, end);
873 nfa_add_states (new, a->states_head, a->states_tail);
874 automaton_fragment_clear (a);
876 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
880 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
885 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
887 struct GNUNET_REGEX_Automaton *a;
890 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
892 add_transition (ctx, a->end, 0, a->start);
894 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
898 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
899 * that alternates between a and b (a|b)
904 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
906 struct GNUNET_REGEX_Automaton *a;
907 struct GNUNET_REGEX_Automaton *b;
908 struct GNUNET_REGEX_Automaton *new;
913 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
915 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
917 start = nfa_state_create (ctx, 0);
918 end = nfa_state_create (ctx, 1);
919 add_transition (ctx, start, 0, a->start);
920 add_transition (ctx, start, 0, b->start);
922 add_transition (ctx, a->end, 0, end);
923 add_transition (ctx, b->end, 0, end);
925 a->end->accepting = 0;
926 b->end->accepting = 0;
929 new = nfa_fragment_create (start, end);
930 nfa_add_states (new, a->states_head, a->states_tail);
931 nfa_add_states (new, b->states_head, b->states_tail);
932 automaton_fragment_clear (a);
933 automaton_fragment_clear (b);
935 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
939 * Adds a new nfa fragment to the stack
942 * @param lit literal for nfa transition
945 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
947 struct GNUNET_REGEX_Automaton *n;
951 GNUNET_assert (NULL != ctx);
953 start = nfa_state_create (ctx, 0);
954 end = nfa_state_create (ctx, 1);
955 add_transition (ctx, start, lit, end);
956 n = nfa_fragment_create (start, end);
957 GNUNET_assert (NULL != n);
958 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
962 * Construct an NFA by parsing the regex string of length 'len'.
964 * @param regex regular expression string
965 * @param len length of the string
967 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
969 struct GNUNET_REGEX_Automaton *
970 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
972 struct GNUNET_REGEX_Context ctx;
973 struct GNUNET_REGEX_Automaton *nfa;
977 unsigned int altcount;
978 unsigned int atomcount;
986 GNUNET_REGEX_context_init (&ctx);
995 for (count = 0; count < len && *regexp; count++, regexp++)
1003 nfa_add_concatenation (&ctx);
1005 GNUNET_array_grow (p, pcount, pcount + 1);
1006 p[pcount - 1].altcount = altcount;
1007 p[pcount - 1].atomcount = atomcount;
1014 error_msg = "Cannot append '|' to nothing";
1017 while (--atomcount > 0)
1018 nfa_add_concatenation (&ctx);
1024 error_msg = "Missing opening '('";
1029 // Ignore this: "()"
1031 altcount = p[pcount].altcount;
1032 atomcount = p[pcount].atomcount;
1035 while (--atomcount > 0)
1036 nfa_add_concatenation (&ctx);
1037 for (; altcount > 0; altcount--)
1038 nfa_add_alternation (&ctx);
1040 altcount = p[pcount].altcount;
1041 atomcount = p[pcount].atomcount;
1047 error_msg = "Cannot append '+' to nothing";
1050 nfa_add_star_op (&ctx);
1055 error_msg = "Cannot append '+' to nothing";
1058 nfa_add_plus_op (&ctx);
1060 case 92: /* escape: \ */
1067 nfa_add_concatenation (&ctx);
1069 nfa_add_literal (&ctx, *regexp);
1076 error_msg = "Unbalanced parenthesis";
1079 while (--atomcount > 0)
1080 nfa_add_concatenation (&ctx);
1081 for (; altcount > 0; altcount--)
1082 nfa_add_alternation (&ctx);
1087 nfa = ctx.stack_tail;
1088 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1091 if (NULL != ctx.stack_head)
1093 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1100 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1101 if (NULL != error_msg)
1102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1104 while (NULL != ctx.stack_tail)
1106 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1107 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1114 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1117 * @param a automaton to be destroyed
1120 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1123 struct State *next_state;
1128 for (s = a->states_head; NULL != s;)
1130 next_state = s->next;
1131 automaton_destroy_state (s);
1139 * Construct DFA for the given 'regex' of length 'len'
1141 * @param regex regular expression string
1142 * @param len length of the regular expression
1144 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1146 struct GNUNET_REGEX_Automaton *
1147 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1149 struct GNUNET_REGEX_Context ctx;
1150 struct GNUNET_REGEX_Automaton *dfa;
1151 struct GNUNET_REGEX_Automaton *nfa;
1152 struct StateSet *tmp;
1153 struct StateSet *nfa_set;
1154 struct StateSet *dfa_stack;
1155 struct Transition *ctran;
1156 struct State *dfa_state;
1157 struct State *new_dfa_state;
1158 struct State *state_contains;
1159 struct State *state_iter;
1161 GNUNET_REGEX_context_init (&ctx);
1164 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1166 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1169 // Create DFA start state from epsilon closure
1170 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1171 nfa_set = nfa_closure_create (nfa->start, 0);
1172 dfa->start = dfa_state_create (&ctx, nfa_set);
1173 automaton_add_state (dfa, dfa->start);
1174 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1175 while (dfa_stack->len > 0)
1177 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1178 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1180 for (ctran = dfa_state->transitions_head; NULL != ctran;
1181 ctran = ctran->next)
1183 if (0 != ctran->literal && NULL == ctran->state)
1185 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1186 nfa_set = nfa_closure_set_create (tmp, 0);
1187 state_set_clear (tmp);
1188 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1189 state_contains = NULL;
1190 for (state_iter = dfa->states_head; NULL != state_iter;
1191 state_iter = state_iter->next)
1194 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1195 state_contains = state_iter;
1198 if (NULL == state_contains)
1200 automaton_add_state (dfa, new_dfa_state);
1201 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1203 ctran->state = new_dfa_state;
1207 ctran->state = state_contains;
1208 automaton_destroy_state (new_dfa_state);
1214 GNUNET_free (dfa_stack);
1215 GNUNET_REGEX_automaton_destroy (nfa);
1223 * Save the given automaton as a GraphViz dot file
1225 * @param a the automaton to be saved
1226 * @param filename where to save the file
1229 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1230 const char *filename)
1233 struct Transition *ctran;
1235 char *s_tran = NULL;
1242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1246 if (NULL == filename || strlen (filename) < 1)
1248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1252 p = fopen (filename, "w");
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1261 start = "digraph G {\nrankdir=LR\n";
1262 fwrite (start, strlen (start), 1, p);
1264 for (s = a->states_head; NULL != s; s = s->next)
1268 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1269 fwrite (s_acc, strlen (s_acc), 1, p);
1270 GNUNET_free (s_acc);
1275 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1277 if (NULL == ctran->state)
1279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1280 "Transition from State %i has has no state for transitioning\n",
1285 if (ctran->literal == 0)
1287 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1288 s->name, ctran->state->name);
1292 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1293 s->name, ctran->state->name, ctran->literal);
1296 fwrite (s_tran, strlen (s_tran), 1, p);
1297 GNUNET_free (s_tran);
1302 fwrite (end, strlen (end), 1, p);
1307 * Evaluates the given string using the given DFA automaton
1309 * @param a automaton, type must be DFA
1310 * @param string string that should be evaluated
1312 * @return 0 if string matches, non 0 otherwise
1315 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1323 "Tried to evaluate DFA, but NFA automaton given");
1329 for (strp = string; NULL != strp && *strp; strp++)
1331 s = dfa_move (s, *strp);
1336 if (NULL != s && s->accepting)
1343 * Evaluates the given string using the given NFA automaton
1345 * @param a automaton, type must be NFA
1346 * @param string string that should be evaluated
1348 * @return 0 if string matches, non 0 otherwise
1351 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1355 struct StateSet *sset;
1356 struct StateSet *new_sset;
1362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1363 "Tried to evaluate NFA, but DFA automaton given");
1369 sset = nfa_closure_create (a->start, 0);
1371 for (strp = string; NULL != strp && *strp; strp++)
1373 new_sset = nfa_closure_set_create (sset, *strp);
1374 state_set_clear (sset);
1375 sset = nfa_closure_set_create (new_sset, 0);
1376 state_set_clear (new_sset);
1379 for (i = 0; i < sset->len; i++)
1381 s = sset->states[i];
1382 if (NULL != s && s->accepting)
1389 state_set_clear (sset);
1395 * Evaluates the given 'string' against the given compiled regex
1397 * @param a automaton
1398 * @param string string to check
1400 * @return 0 if string matches, non 0 otherwise
1403 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1410 result = evaluate_dfa (a, string);
1413 result = evaluate_nfa (a, string);
1416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1417 "Evaluating regex failed, automaton has no type!\n");
1418 result = GNUNET_SYSERR;