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
39 unsigned int state_id;
42 * Unique transition id.
44 unsigned int transition_id;
47 * DLL of GNUNET_REGEX_Automaton's used as a stack.
49 struct GNUNET_REGEX_Automaton *stack_head;
52 * DLL of GNUNET_REGEX_Automaton's used as a stack.
54 struct GNUNET_REGEX_Automaton *stack_tail;
58 * Type of an automaton.
60 enum GNUNET_REGEX_automaton_type
67 * Automaton representation.
69 struct GNUNET_REGEX_Automaton
72 * This is a linked list.
74 struct GNUNET_REGEX_Automaton *prev;
77 * This is a linked list.
79 struct GNUNET_REGEX_Automaton *next;
82 * First state of the automaton. This is mainly
83 * used for constructing an NFA, where each NFA
84 * itself consists of one or more NFAs linked
90 * End state of the automaton.
95 * Number of states in the automaton.
97 unsigned int state_count;
102 struct State *states_head;
107 struct State *states_tail;
110 * Type of the automaton.
112 enum GNUNET_REGEX_automaton_type type;
116 * A state. Can be used in DFA and NFA automatons.
121 * This is a linked list.
126 * This is a linked list.
136 * If this is an accepting state or not.
141 * Marking of the state. This is used for marking all visited
142 * states when traversing all states of an automaton and for
143 * cases where the state id cannot be used (dfa minimization).
148 * Human readable name of the automaton. Used for debugging
149 * and graph creation.
154 * Number of transitions from this state to other states.
156 unsigned int transition_count;
159 * DLL of transitions.
161 struct Transition *transitions_head;
164 * DLL of transitions.
166 struct Transition *transitions_tail;
169 * Set of states on which this state is based on. Used when
170 * creating a DFA out of several NFA states.
172 struct StateSet *nfa_set;
176 * Transition between two states. Each state can have 0-n transitions.
177 * If literal is 0, this is considered to be an epsilon transition.
182 * This is a linked list.
184 struct Transition *prev;
187 * This is a linked list.
189 struct Transition *next;
192 * Unique id of this transition.
197 * Literal for this transition. This is basically the edge label for
203 * State to which this transition leads.
205 struct State *to_state;
208 * State from which this transition origins.
210 struct State *from_state;
221 struct State **states;
224 * Length of the 'states' array.
230 * Initialize a new context
235 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
243 ctx->transition_id = 0;
244 ctx->stack_head = NULL;
245 ctx->stack_tail = NULL;
249 debug_print_state (struct State *s)
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
253 s->marked, s->accepting);
257 debug_print_states (struct StateSet *sset)
262 for (i = 0; i < sset->len; i++)
265 debug_print_state (s);
270 debug_print_transitions (struct State *s)
272 struct Transition *t;
276 for (t = s->transitions_head; NULL != t; t = t->next)
281 literal = t->literal;
283 if (NULL == t->to_state)
286 state = t->to_state->name;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
294 * Compare two states. Used for sorting.
296 * @param a first state
297 * @param b second state
299 * @return an integer less than, equal to, or greater than zero
300 * if the first argument is considered to be respectively
301 * less than, equal to, or greater than the second.
304 state_compare (const void *a, const void *b)
309 s1 = (struct State **) a;
310 s2 = (struct State **) b;
312 return (*s1)->id - (*s2)->id;
316 * Compare to state sets by comparing the id's of the states that are
317 * contained in each set. Both sets are expected to be sorted by id!
319 * @param sset1 first state set
320 * @param sset2 second state set
322 * @return an integer less than, equal to, or greater than zero
323 * if the first argument is considered to be respectively
324 * less than, equal to, or greater than the second.
327 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
332 if (NULL == sset1 || NULL == sset2)
335 result = sset1->len - sset2->len;
337 for (i = 0; i < sset1->len; i++)
342 result = state_compare (&sset1->states[i], &sset2->states[i]);
348 * Checks if 'elem' is contained in 'set'
350 * @param set set of states
353 * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
356 state_set_contains (struct StateSet *set, struct State *elem)
361 for (i = 0; i < set->len; i++)
364 if (0 == memcmp (s, elem, sizeof (struct State)))
371 * Clears the given StateSet 'set'
373 * @param set set to be cleared
376 state_set_clear (struct StateSet *set)
380 if (NULL != set->states)
381 GNUNET_free (set->states);
387 * Adds a transition from one state to another on 'literal'
390 * @param from_state starting state for the transition
391 * @param literal transition label
392 * @param to_state state to where the transition should point to
395 state_add_transition (struct GNUNET_REGEX_Context *ctx,
396 struct State *from_state, const char literal,
397 struct State *to_state)
399 struct Transition *t;
401 if (NULL == from_state)
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
407 t = GNUNET_malloc (sizeof (struct Transition));
409 t->id = ctx->transition_id++;
410 t->literal = literal;
411 t->to_state = to_state;
412 t->from_state = from_state;
414 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
415 from_state->transitions_tail, t);
419 * Clears an automaton fragment. Does not destroy the states inside
422 * @param a automaton to be cleared
425 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
432 a->states_head = NULL;
433 a->states_tail = NULL;
439 * Frees the memory used by State 's'
441 * @param s state that should be destroyed
444 automaton_destroy_state (struct State *s)
446 struct Transition *t;
447 struct Transition *next_t;
453 GNUNET_free (s->name);
455 for (t = s->transitions_head; NULL != t;)
458 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
463 state_set_clear (s->nfa_set);
469 * Remove a state from the given automaton 'a'. Always use this function
470 * when altering the states of an automaton. Will also remove all transitions
471 * leading to this state, before destroying it.
474 * @param s state to remove
477 automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
480 struct State *s_check;
481 struct Transition *t_check;
483 if (NULL == a || NULL == s)
488 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
491 // remove all transitions leading to this state
492 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
494 for (t_check = s_check->transitions_head; NULL != t_check;
495 t_check = t_check->next)
497 if (t_check->to_state == ss)
499 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
500 s_check->transitions_tail, t_check);
501 s_check->transition_count--;
506 automaton_destroy_state (ss);
510 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
514 * @param s1 first state
515 * @param s2 second state, will be destroyed
518 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
519 struct GNUNET_REGEX_Automaton *a, struct State *s1,
522 struct State *s_check;
523 struct Transition *t_check;
524 struct Transition *t;
527 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
529 // 1. Make all transitions pointing to s2 point to s1
530 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
532 for (t_check = s_check->transitions_head; NULL != t_check;
533 t_check = t_check->next)
535 if (s_check != s1 && s2 == t_check->to_state)
536 t_check->to_state = s1;
540 // 2. Add all transitions from s2 to sX to s1
541 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
543 for (t = s1->transitions_head; NULL != t; t = t->next)
545 if (t_check->literal != t->literal && NULL != t_check->to_state &&
546 t_check->to_state != t->to_state && t_check->to_state != s2)
548 state_add_transition (ctx, s1, t_check->literal, t_check->to_state);
553 // 3. Rename s1 to {s1,s2}
554 new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
555 strncat (new_name, s1->name, strlen (s1->name));
556 strncat (new_name, s2->name, strlen (s2->name));
557 if (NULL != s1->name)
558 GNUNET_free (s1->name);
563 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
565 automaton_destroy_state (s_check);
569 * Add a state to the automaton 'a', always use this function to
570 * alter the states DLL of the automaton.
572 * @param a automaton to add the state to
573 * @param s state that should be added
576 automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
578 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
583 * Creates a new DFA state based on a set of NFA states. Needs to be freed
584 * using automaton_destroy_state.
587 * @param nfa_states set of NFA states on which the DFA should be based on
589 * @return new DFA state
591 static struct State *
592 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
597 struct State *cstate;
598 struct Transition *ctran;
600 struct Transition *t;
603 s = GNUNET_malloc (sizeof (struct State));
604 s->id = ctx->state_id++;
609 if (NULL == nfa_states)
611 GNUNET_asprintf (&s->name, "s%i", s->id);
615 s->nfa_set = nfa_states;
617 if (nfa_states->len < 1)
620 // Create a name based on 'sset'
621 s->name = GNUNET_malloc (sizeof (char) * 2);
622 strcat (s->name, "{");
625 for (i = 0; i < nfa_states->len; i++)
627 cstate = nfa_states->states[i];
628 GNUNET_asprintf (&name, "%i,", cstate->id);
632 len = strlen (s->name) + strlen (name) + 1;
633 s->name = GNUNET_realloc (s->name, len);
634 strcat (s->name, name);
639 // Add a transition for each distinct literal to NULL state
640 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
642 if (0 != ctran->literal)
646 for (t = s->transitions_head; NULL != t; t = t->next)
648 if (t->literal == ctran->literal)
656 state_add_transition (ctx, s, ctran->literal, NULL);
660 // If the nfa_states contain an accepting state, the new dfa state is also accepting
661 if (cstate->accepting)
665 s->name[strlen (s->name) - 1] = '}';
671 * Move from the given state 's' to the next state on
672 * transition 'literal'
674 * @param s starting state
675 * @param literal edge label to follow
677 * @return new state or NULL, if transition on literal not possible
679 static struct State *
680 dfa_move (struct State *s, const char literal)
682 struct Transition *t;
690 for (t = s->transitions_head; NULL != t; t = t->next)
692 if (literal == t->literal)
703 * Remove all unreachable states from DFA 'a'. Unreachable states
704 * are those states that are not reachable from the starting state.
706 * @param a DFA automaton
709 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
711 struct State *stack[a->state_count * a->state_count];
714 struct State *s_next;
715 struct Transition *t;
719 // 1. unmark all states
720 for (s = a->states_head; NULL != s; s = s->next)
723 // 2. traverse dfa from start state and mark all visited states
724 stack[stack_len++] = a->start;
725 while (stack_len > 0)
727 s = stack[--stack_len];
728 s->marked = 1; // mark s as visited
729 for (t = s->transitions_head; NULL != t; t = t->next)
731 // add next states to stack
732 if (NULL != t->to_state && 0 == t->to_state->marked)
733 stack[stack_len++] = t->to_state;
737 // 3. delete all states that were not visited
738 for (s = a->states_head; NULL != s; s = s_next)
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removed state %s\n", s->name);
744 automaton_remove_state (a, s);
750 * Remove all dead states from the DFA 'a'. Dead states are those
751 * states that do not transition to any other state but themselfes.
753 * @param a DFA automaton
756 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
759 struct Transition *t;
762 GNUNET_assert (DFA == a->type);
764 for (s = a->states_head; NULL != s; s = s->next)
770 for (t = s->transitions_head; NULL != t; t = t->next)
772 if (NULL != t->to_state && t->to_state != s)
782 // state s is dead, remove it
783 automaton_remove_state (a, s);
788 * Merge all non distinguishable states in the DFA 'a'
791 * @param a DFA automaton
794 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
795 struct GNUNET_REGEX_Automaton *a)
798 int table[a->state_count][a->state_count];
801 struct Transition *t1;
802 struct Transition *t2;
806 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
810 // Mark all pairs of accepting/!accepting states
811 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
813 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
815 if ((s1->accepting && !s2->accepting) ||
816 (!s1->accepting && s2->accepting))
818 table[s1->marked][s2->marked] = 1;
821 table[s1->marked][s2->marked] = 0;
828 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
830 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
832 if (0 != table[s1->marked][s2->marked])
835 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
837 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
839 if (t1->literal == t2->literal && t1->to_state == t2->to_state &&
840 (0 != table[t1->to_state->marked][t2->to_state->marked] ||
841 0 != table[t2->to_state->marked][t1->to_state->marked]))
843 table[s1->marked][s2->marked] = t1->literal;
846 else if (t1->literal != t2->literal && t1->to_state != t2->to_state)
848 table[s1->marked][s2->marked] = -1;
857 struct State *s2_next;
859 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
861 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
864 if (s1 != s2 && table[s1->marked][s2->marked] == 0)
865 automaton_merge_states (ctx, a, s1, s2);
871 * Minimize the given DFA 'a' by removing all unreachable states,
872 * removing all dead states and merging all non distinguishable states
875 * @param a DFA automaton
878 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
879 struct GNUNET_REGEX_Automaton *a)
884 GNUNET_assert (DFA == a->type);
886 // 1. remove unreachable states
887 dfa_remove_unreachable_states (a);
889 // 2. remove dead states
890 dfa_remove_dead_states (a);
892 // 3. Merge nondistinguishable states
893 dfa_merge_nondistinguishable_states (ctx, a);
897 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
899 * @param start starting state
900 * @param end end state
902 * @return new NFA fragment
904 static struct GNUNET_REGEX_Automaton *
905 nfa_fragment_create (struct State *start, struct State *end)
907 struct GNUNET_REGEX_Automaton *n;
909 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
915 if (NULL == start && NULL == end)
918 automaton_add_state (n, end);
919 automaton_add_state (n, start);
928 * Adds a list of states to the given automaton 'n'.
930 * @param n automaton to which the states should be added
931 * @param states_head head of the DLL of states
932 * @param states_tail tail of the DLL of states
935 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
936 struct State *states_tail)
940 if (NULL == n || NULL == states_head)
942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
946 if (NULL == n->states_head)
948 n->states_head = states_head;
949 n->states_tail = states_tail;
953 if (NULL != states_head)
955 n->states_tail->next = states_head;
956 n->states_tail = states_tail;
959 for (s = states_head; NULL != s; s = s->next)
964 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
967 * @param accepting is it an accepting state or not
969 * @return new NFA state
971 static struct State *
972 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
976 s = GNUNET_malloc (sizeof (struct State));
977 s->id = ctx->state_id++;
978 s->accepting = accepting;
981 GNUNET_asprintf (&s->name, "s%i", s->id);
987 * Calculates the NFA closure set for the given state.
989 * @param s starting point state
990 * @param literal transitioning literal on which to base the closure on,
991 * pass 0 for epsilon transition
993 * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
995 static struct StateSet *
996 nfa_closure_create (struct State *s, const char literal)
998 struct StateSet *cls;
999 struct StateSet *cls_check;
1000 struct State *clsstate;
1001 struct State *currentstate;
1002 struct Transition *ctran;
1007 cls = GNUNET_malloc (sizeof (struct StateSet));
1008 cls_check = GNUNET_malloc (sizeof (struct StateSet));
1010 // Add start state to closure only for epsilon closure
1012 GNUNET_array_append (cls->states, cls->len, s);
1014 GNUNET_array_append (cls_check->states, cls_check->len, s);
1015 while (cls_check->len > 0)
1017 currentstate = cls_check->states[cls_check->len - 1];
1018 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1020 for (ctran = currentstate->transitions_head; NULL != ctran;
1021 ctran = ctran->next)
1023 if (NULL != ctran->to_state && literal == ctran->literal)
1025 clsstate = ctran->to_state;
1027 if (NULL != clsstate &&
1028 GNUNET_YES != state_set_contains (cls, clsstate))
1030 GNUNET_array_append (cls->states, cls->len, clsstate);
1031 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1036 GNUNET_assert (0 == cls_check->len);
1037 GNUNET_free (cls_check);
1040 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
1046 * Calculates the closure set for the given set of states.
1048 * @param states list of states on which to base the closure on
1049 * @param literal transitioning literal for which to base the closure on,
1050 * pass 0 for epsilon transition
1052 * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
1054 static struct StateSet *
1055 nfa_closure_set_create (struct StateSet *states, const char literal)
1058 struct StateSet *sset;
1059 struct StateSet *cls;
1068 cls = GNUNET_malloc (sizeof (struct StateSet));
1070 for (i = 0; i < states->len; i++)
1072 s = states->states[i];
1073 sset = nfa_closure_create (s, literal);
1075 for (j = 0; j < sset->len; j++)
1078 for (k = 0; k < cls->len; k++)
1080 if (sset->states[j]->id == cls->states[k]->id)
1087 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1089 state_set_clear (sset);
1093 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
1099 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1101 * @param ctx context
1104 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1106 struct GNUNET_REGEX_Automaton *a;
1107 struct GNUNET_REGEX_Automaton *b;
1108 struct GNUNET_REGEX_Automaton *new;
1110 b = ctx->stack_tail;
1111 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1112 a = ctx->stack_tail;
1113 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1115 state_add_transition (ctx, a->end, 0, b->start);
1116 a->end->accepting = 0;
1117 b->end->accepting = 1;
1119 new = nfa_fragment_create (NULL, NULL);
1120 nfa_add_states (new, a->states_head, a->states_tail);
1121 nfa_add_states (new, b->states_head, b->states_tail);
1122 new->start = a->start;
1124 automaton_fragment_clear (a);
1125 automaton_fragment_clear (b);
1127 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1131 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1133 * @param ctx context
1136 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1138 struct GNUNET_REGEX_Automaton *a;
1139 struct GNUNET_REGEX_Automaton *new;
1140 struct State *start;
1143 a = ctx->stack_tail;
1144 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1149 "nfa_add_star_op failed, because there was no element on the stack");
1153 start = nfa_state_create (ctx, 0);
1154 end = nfa_state_create (ctx, 1);
1156 state_add_transition (ctx, start, 0, a->start);
1157 state_add_transition (ctx, start, 0, end);
1158 state_add_transition (ctx, a->end, 0, a->start);
1159 state_add_transition (ctx, a->end, 0, end);
1161 a->end->accepting = 0;
1164 new = nfa_fragment_create (start, end);
1165 nfa_add_states (new, a->states_head, a->states_tail);
1166 automaton_fragment_clear (a);
1168 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1172 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1174 * @param ctx context
1177 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1179 struct GNUNET_REGEX_Automaton *a;
1181 a = ctx->stack_tail;
1182 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1184 state_add_transition (ctx, a->end, 0, a->start);
1186 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1190 * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
1192 * @param ctx context
1195 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
1197 struct GNUNET_REGEX_Automaton *a;
1198 struct GNUNET_REGEX_Automaton *new;
1199 struct State *start;
1202 a = ctx->stack_tail;
1203 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1208 "nfa_add_question_op failed, because there was no element on the stack");
1212 start = nfa_state_create (ctx, 0);
1213 end = nfa_state_create (ctx, 1);
1215 state_add_transition (ctx, start, 0, a->start);
1216 state_add_transition (ctx, start, 0, end);
1217 state_add_transition (ctx, a->end, 0, end);
1219 a->end->accepting = 0;
1221 new = nfa_fragment_create (start, end);
1222 nfa_add_states (new, a->states_head, a->states_tail);
1223 automaton_fragment_clear (a);
1225 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1229 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1230 * that alternates between a and b (a|b)
1232 * @param ctx context
1235 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1237 struct GNUNET_REGEX_Automaton *a;
1238 struct GNUNET_REGEX_Automaton *b;
1239 struct GNUNET_REGEX_Automaton *new;
1240 struct State *start;
1243 b = ctx->stack_tail;
1244 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1245 a = ctx->stack_tail;
1246 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1248 start = nfa_state_create (ctx, 0);
1249 end = nfa_state_create (ctx, 1);
1250 state_add_transition (ctx, start, 0, a->start);
1251 state_add_transition (ctx, start, 0, b->start);
1253 state_add_transition (ctx, a->end, 0, end);
1254 state_add_transition (ctx, b->end, 0, end);
1256 a->end->accepting = 0;
1257 b->end->accepting = 0;
1260 new = nfa_fragment_create (start, end);
1261 nfa_add_states (new, a->states_head, a->states_tail);
1262 nfa_add_states (new, b->states_head, b->states_tail);
1263 automaton_fragment_clear (a);
1264 automaton_fragment_clear (b);
1266 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1270 * Adds a new nfa fragment to the stack
1272 * @param ctx context
1273 * @param lit literal for nfa transition
1276 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
1278 struct GNUNET_REGEX_Automaton *n;
1279 struct State *start;
1282 GNUNET_assert (NULL != ctx);
1284 start = nfa_state_create (ctx, 0);
1285 end = nfa_state_create (ctx, 1);
1286 state_add_transition (ctx, start, lit, end);
1287 n = nfa_fragment_create (start, end);
1288 GNUNET_assert (NULL != n);
1289 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1293 * Construct an NFA by parsing the regex string of length 'len'.
1295 * @param regex regular expression string
1296 * @param len length of the string
1298 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1300 struct GNUNET_REGEX_Automaton *
1301 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1303 struct GNUNET_REGEX_Context ctx;
1304 struct GNUNET_REGEX_Automaton *nfa;
1308 unsigned int altcount;
1309 unsigned int atomcount;
1310 unsigned int pcount;
1317 GNUNET_REGEX_context_init (&ctx);
1326 for (count = 0; count < len && *regexp; count++, regexp++)
1334 nfa_add_concatenation (&ctx);
1336 GNUNET_array_grow (p, pcount, pcount + 1);
1337 p[pcount - 1].altcount = altcount;
1338 p[pcount - 1].atomcount = atomcount;
1345 error_msg = "Cannot append '|' to nothing";
1348 while (--atomcount > 0)
1349 nfa_add_concatenation (&ctx);
1355 error_msg = "Missing opening '('";
1360 // Ignore this: "()"
1362 altcount = p[pcount].altcount;
1363 atomcount = p[pcount].atomcount;
1366 while (--atomcount > 0)
1367 nfa_add_concatenation (&ctx);
1368 for (; altcount > 0; altcount--)
1369 nfa_add_alternation (&ctx);
1371 altcount = p[pcount].altcount;
1372 atomcount = p[pcount].atomcount;
1378 error_msg = "Cannot append '+' to nothing";
1381 nfa_add_star_op (&ctx);
1386 error_msg = "Cannot append '+' to nothing";
1389 nfa_add_plus_op (&ctx);
1394 error_msg = "Cannot append '?' to nothing";
1397 nfa_add_question_op (&ctx);
1399 case 92: /* escape: \ */
1406 nfa_add_concatenation (&ctx);
1408 nfa_add_literal (&ctx, *regexp);
1415 error_msg = "Unbalanced parenthesis";
1418 while (--atomcount > 0)
1419 nfa_add_concatenation (&ctx);
1420 for (; altcount > 0; altcount--)
1421 nfa_add_alternation (&ctx);
1426 nfa = ctx.stack_tail;
1427 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1430 if (NULL != ctx.stack_head)
1432 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1440 if (NULL != error_msg)
1441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1444 while (NULL != ctx.stack_tail)
1446 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1447 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1454 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1457 * @param a automaton to be destroyed
1460 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1463 struct State *next_state;
1468 for (s = a->states_head; NULL != s;)
1470 next_state = s->next;
1471 automaton_destroy_state (s);
1479 * Construct DFA for the given 'regex' of length 'len'
1481 * @param regex regular expression string
1482 * @param len length of the regular expression
1484 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1486 struct GNUNET_REGEX_Automaton *
1487 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1489 struct GNUNET_REGEX_Context ctx;
1490 struct GNUNET_REGEX_Automaton *dfa;
1491 struct GNUNET_REGEX_Automaton *nfa;
1492 struct StateSet *tmp;
1493 struct StateSet *nfa_set;
1494 struct StateSet *dfa_stack;
1495 struct Transition *ctran;
1496 struct State *dfa_state;
1497 struct State *new_dfa_state;
1498 struct State *state_contains;
1499 struct State *state_iter;
1501 GNUNET_REGEX_context_init (&ctx);
1504 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1509 "Could not create DFA, because NFA creation failed\n");
1513 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1516 // Create DFA start state from epsilon closure
1517 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1518 nfa_set = nfa_closure_create (nfa->start, 0);
1519 dfa->start = dfa_state_create (&ctx, nfa_set);
1520 automaton_add_state (dfa, dfa->start);
1521 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1523 // Create dfa states by combining nfa states
1524 while (dfa_stack->len > 0)
1526 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1527 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1529 for (ctran = dfa_state->transitions_head; NULL != ctran;
1530 ctran = ctran->next)
1532 if (0 != ctran->literal && NULL == ctran->to_state)
1534 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1535 nfa_set = nfa_closure_set_create (tmp, 0);
1536 state_set_clear (tmp);
1537 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1538 state_contains = NULL;
1539 for (state_iter = dfa->states_head; NULL != state_iter;
1540 state_iter = state_iter->next)
1543 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1544 state_contains = state_iter;
1547 if (NULL == state_contains)
1549 automaton_add_state (dfa, new_dfa_state);
1550 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1552 ctran->to_state = new_dfa_state;
1556 ctran->to_state = state_contains;
1557 automaton_destroy_state (new_dfa_state);
1563 GNUNET_free (dfa_stack);
1564 GNUNET_REGEX_automaton_destroy (nfa);
1566 dfa_minimize (&ctx, dfa);
1572 * Save the given automaton as a GraphViz dot file
1574 * @param a the automaton to be saved
1575 * @param filename where to save the file
1578 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1579 const char *filename)
1582 struct Transition *ctran;
1584 char *s_tran = NULL;
1591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1595 if (NULL == filename || strlen (filename) < 1)
1597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1601 p = fopen (filename, "w");
1605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1610 start = "digraph G {\nrankdir=LR\n";
1611 fwrite (start, strlen (start), 1, p);
1613 for (s = a->states_head; NULL != s; s = s->next)
1617 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1618 fwrite (s_acc, strlen (s_acc), 1, p);
1619 GNUNET_free (s_acc);
1624 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1626 if (NULL == ctran->to_state)
1628 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1629 "Transition from State %i has has no state for transitioning\n",
1634 if (ctran->literal == 0)
1636 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1637 s->name, ctran->to_state->name);
1641 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1642 s->name, ctran->to_state->name, ctran->literal);
1645 fwrite (s_tran, strlen (s_tran), 1, p);
1646 GNUNET_free (s_tran);
1651 fwrite (end, strlen (end), 1, p);
1656 * Evaluates the given string using the given DFA automaton
1658 * @param a automaton, type must be DFA
1659 * @param string string that should be evaluated
1661 * @return 0 if string matches, non 0 otherwise
1664 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1672 "Tried to evaluate DFA, but NFA automaton given");
1678 for (strp = string; NULL != strp && *strp; strp++)
1680 s = dfa_move (s, *strp);
1685 if (NULL != s && s->accepting)
1692 * Evaluates the given string using the given NFA automaton
1694 * @param a automaton, type must be NFA
1695 * @param string string that should be evaluated
1697 * @return 0 if string matches, non 0 otherwise
1700 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1704 struct StateSet *sset;
1705 struct StateSet *new_sset;
1711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1712 "Tried to evaluate NFA, but DFA automaton given");
1718 sset = nfa_closure_create (a->start, 0);
1720 for (strp = string; NULL != strp && *strp; strp++)
1722 new_sset = nfa_closure_set_create (sset, *strp);
1723 state_set_clear (sset);
1724 sset = nfa_closure_set_create (new_sset, 0);
1725 state_set_clear (new_sset);
1728 for (i = 0; i < sset->len; i++)
1730 s = sset->states[i];
1731 if (NULL != s && s->accepting)
1738 state_set_clear (sset);
1743 * Evaluates the given 'string' against the given compiled regex
1745 * @param a automaton
1746 * @param string string to check
1748 * @return 0 if string matches, non 0 otherwise
1751 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1758 result = evaluate_dfa (a, string);
1761 result = evaluate_nfa (a, string);
1764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1765 "Evaluating regex failed, automaton has no type!\n");
1766 result = GNUNET_SYSERR;