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.
216 struct State **states;
219 * Length of the 'states' array.
225 * Initialize a new context
230 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
238 ctx->transition_id = 0;
239 ctx->stack_head = NULL;
240 ctx->stack_tail = NULL;
244 debug_print_state (struct State *s)
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
248 s->marked, s->accepting);
252 debug_print_states (struct StateSet *sset)
257 for (i = 0; i < sset->len; i++)
260 debug_print_state (s);
265 debug_print_transitions (struct State *s)
267 struct Transition *t;
271 for (t = s->transitions_head; NULL != t; t = t->next)
276 literal = t->literal;
278 if (NULL == t->state)
281 state = t->state->name;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
289 * Compare two states. Used for sorting.
291 * @param a first state
292 * @param b second state
294 * @return an integer less than, equal to, or greater than zero
295 * if the first argument is considered to be respectively
296 * less than, equal to, or greater than the second.
299 state_compare (const void *a, const void *b)
304 s1 = (struct State **) a;
305 s2 = (struct State **) b;
307 return (*s1)->id - (*s2)->id;
311 * Compare to state sets by comparing the id's of the states that are
312 * contained in each set. Both sets are expected to be sorted by id!
314 * @param sset1 first state set
315 * @param sset2 second state set
317 * @return 0 if they are equal, non 0 otherwise
320 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
324 if (sset1->len != sset2->len)
327 for (i = 0; i < sset1->len; i++)
329 if (sset1->states[i]->id != sset2->states[i]->id)
338 * Checks if 'elem' is contained in 'set'
340 * @param set set of states
343 * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
346 state_set_contains (struct StateSet *set, struct State *elem)
351 for (i = 0; i < set->len; i++)
354 if (0 == memcmp (s, elem, sizeof (struct State)))
361 * Clears the given StateSet 'set'
363 * @param set set to be cleared
366 state_set_clear (struct StateSet *set)
370 if (NULL != set->states)
371 GNUNET_free (set->states);
377 * Adds a transition from one state to another on 'literal'
380 * @param from_state starting state for the transition
381 * @param literal transition label
382 * @param to_state state to where the transition should point to
385 add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
386 const char literal, struct State *to_state)
388 struct Transition *t;
390 if (NULL == from_state)
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
396 t = GNUNET_malloc (sizeof (struct Transition));
398 t->id = ctx->transition_id++;
399 t->literal = literal;
402 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
403 from_state->transitions_tail, t);
407 * Clears an automaton fragment. Does not destroy the states inside
410 * @param a automaton to be cleared
413 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
417 a->states_head = NULL;
418 a->states_tail = NULL;
424 * Frees the memory used by State 's'
426 * @param s state that should be destroyed
429 automaton_destroy_state (struct State *s)
431 struct Transition *t;
432 struct Transition *next_t;
435 GNUNET_free (s->name);
437 for (t = s->transitions_head; NULL != t;)
440 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
445 state_set_clear (s->nfa_set);
451 * Remove a state from the given automaton 'a'. Always use this function
452 * when altering the states of an automaton. Will also remove all transitions
453 * leading to this state, before destroying it.
456 * @param s state to remove
459 automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
462 struct State *s_check;
463 struct Transition *t_check;
467 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
470 // remove all transitions leading to this state
471 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
473 for (t_check = s_check->transitions_head; NULL != t_check;
474 t_check = t_check->next)
476 if (t_check->state == ss)
478 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
479 s_check->transitions_tail, t_check);
480 s_check->transition_count--;
485 automaton_destroy_state (ss);
489 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
493 * @param s1 first state
494 * @param s2 second state, will be destroyed
497 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
498 struct GNUNET_REGEX_Automaton *a, struct State *s1,
501 struct State *s_check;
502 struct Transition *t_check;
503 struct Transition *t;
506 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
508 // 1. Make all transitions pointing to s2 point to s1
509 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
511 for (t_check = s_check->transitions_head; NULL != t_check;
512 t_check = t_check->next)
514 if (s_check != s1 && s2 == t_check->state)
519 // 2. Add all transitions from s2 to sX to s1
520 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
522 for (t = s1->transitions_head; NULL != t; t = t->next)
524 if (t_check->literal != t->literal && NULL != t_check->state &&
525 t_check->state != t->state && t_check->state != s2)
527 add_transition (ctx, s1, t_check->literal, t_check->state);
532 // 3. Rename s1 to {s1,s2}
533 new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
534 strncat (new_name, s1->name, strlen (s1->name));
535 strncat (new_name, s2->name, strlen (s2->name));
536 if (NULL != s1->name)
537 GNUNET_free (s1->name);
542 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
544 automaton_destroy_state (s_check);
548 * Add a state to the automaton 'a', always use this function to
549 * alter the states DLL of the automaton.
551 * @param a automaton to add the state to
552 * @param s state that should be added
555 automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
557 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
562 * Creates a new DFA state based on a set of NFA states. Needs to be freed
563 * using automaton_destroy_state.
566 * @param nfa_states set of NFA states on which the DFA should be based on
568 * @return new DFA state
570 static struct State *
571 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
576 struct State *cstate;
577 struct Transition *ctran;
579 struct Transition *t;
582 s = GNUNET_malloc (sizeof (struct State));
583 s->id = ctx->state_id++;
588 if (NULL == nfa_states)
590 GNUNET_asprintf (&s->name, "s%i", s->id);
594 s->nfa_set = nfa_states;
596 if (nfa_states->len < 1)
599 // Create a name based on 'sset'
600 s->name = GNUNET_malloc (sizeof (char) * 2);
601 strcat (s->name, "{");
604 for (i = 0; i < nfa_states->len; i++)
606 cstate = nfa_states->states[i];
607 GNUNET_asprintf (&name, "%i,", cstate->id);
611 len = strlen (s->name) + strlen (name) + 1;
612 s->name = GNUNET_realloc (s->name, len);
613 strcat (s->name, name);
618 // Add a transition for each distinct literal to NULL state
619 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
621 if (0 != ctran->literal)
625 for (t = s->transitions_head; NULL != t; t = t->next)
627 if (t->literal == ctran->literal)
635 add_transition (ctx, s, ctran->literal, NULL);
639 // If the nfa_states contain an accepting state, the new dfa state is also accepting
640 if (cstate->accepting)
644 s->name[strlen (s->name) - 1] = '}';
650 * Move from the given state 's' to the next state on
651 * transition 'literal'
653 * @param s starting state
654 * @param literal edge label to follow
656 * @return new state or NULL, if transition on literal not possible
658 static struct State *
659 dfa_move (struct State *s, const char literal)
661 struct Transition *t;
669 for (t = s->transitions_head; NULL != t; t = t->next)
671 if (literal == t->literal)
682 * Remove all unreachable states from DFA 'a'. Unreachable states
683 * are those states that are not reachable from the starting state.
685 * @param a DFA automaton
688 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
690 struct State *stack[a->state_count];
693 struct Transition *t;
697 // 1. unmark all states
698 for (s = a->states_head; NULL != s; s = s->next)
703 // 2. traverse dfa from start state and mark all visited states
704 stack[stack_len] = a->start;
706 while (stack_len > 0)
708 s = stack[stack_len - 1];
710 s->marked = 1; // mark s as visited
711 for (t = s->transitions_head; NULL != t; t = t->next)
713 if (NULL != t->state && 0 == t->state->marked)
715 // add next states to stack
716 stack[stack_len] = t->state;
722 // 3. delete all states that were not visited
723 for (s = a->states_head; NULL != s; s = s->next)
726 automaton_remove_state (a, s);
731 * Remove all dead states from the DFA 'a'. Dead states are those
732 * states that do not transition to any other state but themselfes.
734 * @param a DFA automaton
737 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
740 struct Transition *t;
743 GNUNET_assert (DFA == a->type);
745 for (s = a->states_head; NULL != s; s = s->next)
751 for (t = s->transitions_head; NULL != t; t = t->next)
753 if (NULL != t->state && t->state != s)
763 // state s is dead, remove it
764 automaton_remove_state (a, s);
769 * Merge all non distinguishable states in the DFA 'a'
772 * @param a DFA automaton
775 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
776 struct GNUNET_REGEX_Automaton *a)
779 int table[a->state_count][a->state_count];
782 struct Transition *t1;
783 struct Transition *t2;
787 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
791 // Mark all pairs of accepting/!accepting states
792 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
794 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
796 if ((s1->accepting && !s2->accepting) ||
797 (!s1->accepting && s2->accepting))
799 table[s1->marked][s2->marked] = 1;
802 table[s1->marked][s2->marked] = 0;
809 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
811 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
813 if (0 != table[s1->marked][s2->marked])
816 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
818 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
820 if (t1->literal == t2->literal && t1->state == t2->state &&
821 (0 != table[t1->state->marked][t2->state->marked] ||
822 0 != table[t2->state->marked][t1->state->marked]))
824 table[s1->marked][s2->marked] = t1->literal;
827 else if (t1->literal != t2->literal && t1->state != t2->state)
829 table[s1->marked][s2->marked] = -1;
838 struct State *s2_next;
840 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
842 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
845 if (s1 != s2 && table[s1->marked][s2->marked] == 0)
846 automaton_merge_states (ctx, a, s1, s2);
852 * Minimize the given DFA 'a' by removing all unreachable states,
853 * removing all dead states and merging all non distinguishable states
856 * @param a DFA automaton
859 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
860 struct GNUNET_REGEX_Automaton *a)
865 GNUNET_assert (DFA == a->type);
867 // 1. remove unreachable states
868 dfa_remove_unreachable_states (a);
870 // 2. remove dead states
871 dfa_remove_dead_states (a);
873 // 3. Merge nondistinguishable states
874 dfa_merge_nondistinguishable_states (ctx, a);
878 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
880 * @param start starting state
881 * @param end end state
883 * @return new NFA fragment
885 static struct GNUNET_REGEX_Automaton *
886 nfa_fragment_create (struct State *start, struct State *end)
888 struct GNUNET_REGEX_Automaton *n;
890 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
896 if (NULL == start && NULL == end)
899 automaton_add_state (n, end);
900 automaton_add_state (n, start);
909 * Adds a list of states to the given automaton 'n'.
911 * @param n automaton to which the states should be added
912 * @param states_head head of the DLL of states
913 * @param states_tail tail of the DLL of states
916 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
917 struct State *states_tail)
921 if (NULL == n || NULL == states_head)
923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
927 if (NULL == n->states_head)
929 n->states_head = states_head;
930 n->states_tail = states_tail;
934 if (NULL != states_head)
936 n->states_tail->next = states_head;
937 n->states_tail = states_tail;
940 for (s = states_head; NULL != s; s = s->next)
945 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
948 * @param accepting is it an accepting state or not
950 * @return new NFA state
952 static struct State *
953 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
957 s = GNUNET_malloc (sizeof (struct State));
958 s->id = ctx->state_id++;
959 s->accepting = accepting;
962 GNUNET_asprintf (&s->name, "s%i", s->id);
968 * Calculates the NFA closure set for the given state
970 * @param s starting point state
971 * @param literal transitioning literal on which to base the closure on,
972 * pass 0 for epsilon transition
974 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
976 static struct StateSet *
977 nfa_closure_create (struct State *s, const char literal)
979 struct StateSet *cls;
980 struct StateSet *cls_check;
981 struct State *clsstate;
982 struct State *currentstate;
983 struct Transition *ctran;
988 cls = GNUNET_malloc (sizeof (struct StateSet));
989 cls_check = GNUNET_malloc (sizeof (struct StateSet));
991 // Add start state to closure only for epsilon closure
993 GNUNET_array_append (cls->states, cls->len, s);
995 GNUNET_array_append (cls_check->states, cls_check->len, s);
996 while (cls_check->len > 0)
998 currentstate = cls_check->states[cls_check->len - 1];
999 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
1001 for (ctran = currentstate->transitions_head; NULL != ctran;
1002 ctran = ctran->next)
1004 if (NULL != ctran->state && literal == ctran->literal)
1006 clsstate = ctran->state;
1008 if (NULL != clsstate &&
1009 GNUNET_YES != state_set_contains (cls, clsstate))
1011 GNUNET_array_append (cls->states, cls->len, clsstate);
1012 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
1017 GNUNET_assert (0 == cls_check->len);
1018 GNUNET_free (cls_check);
1021 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
1027 * Calculates the closure set for the given set of states.
1029 * @param states list of states on which to base the closure on
1030 * @param literal transitioning literal for which to base the closure on,
1031 * pass 0 for epsilon transition
1033 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
1035 static struct StateSet *
1036 nfa_closure_set_create (struct StateSet *states, const char literal)
1039 struct StateSet *sset;
1040 struct StateSet *cls;
1049 cls = GNUNET_malloc (sizeof (struct StateSet));
1051 for (i = 0; i < states->len; i++)
1053 s = states->states[i];
1054 sset = nfa_closure_create (s, literal);
1056 for (j = 0; j < sset->len; j++)
1059 for (k = 0; k < cls->len; k++)
1061 if (sset->states[j]->id == cls->states[k]->id)
1068 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
1070 state_set_clear (sset);
1074 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
1080 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
1082 * @param ctx context
1085 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
1087 struct GNUNET_REGEX_Automaton *a;
1088 struct GNUNET_REGEX_Automaton *b;
1089 struct GNUNET_REGEX_Automaton *new;
1091 b = ctx->stack_tail;
1092 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1093 a = ctx->stack_tail;
1094 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1096 add_transition (ctx, a->end, 0, b->start);
1097 a->end->accepting = 0;
1098 b->end->accepting = 1;
1100 new = nfa_fragment_create (NULL, NULL);
1101 nfa_add_states (new, a->states_head, a->states_tail);
1102 nfa_add_states (new, b->states_head, b->states_tail);
1103 new->start = a->start;
1105 automaton_fragment_clear (a);
1106 automaton_fragment_clear (b);
1108 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1112 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1114 * @param ctx context
1117 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1119 struct GNUNET_REGEX_Automaton *a;
1120 struct GNUNET_REGEX_Automaton *new;
1121 struct State *start;
1124 a = ctx->stack_tail;
1125 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1130 "nfa_add_star_op failed, because there was no element on the stack");
1134 start = nfa_state_create (ctx, 0);
1135 end = nfa_state_create (ctx, 1);
1137 add_transition (ctx, start, 0, a->start);
1138 add_transition (ctx, start, 0, end);
1139 add_transition (ctx, a->end, 0, a->start);
1140 add_transition (ctx, a->end, 0, end);
1142 a->end->accepting = 0;
1145 new = nfa_fragment_create (start, end);
1146 nfa_add_states (new, a->states_head, a->states_tail);
1147 automaton_fragment_clear (a);
1149 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1153 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1155 * @param ctx context
1158 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1160 struct GNUNET_REGEX_Automaton *a;
1162 a = ctx->stack_tail;
1163 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1165 add_transition (ctx, a->end, 0, a->start);
1167 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1171 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1172 * that alternates between a and b (a|b)
1174 * @param ctx context
1177 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1179 struct GNUNET_REGEX_Automaton *a;
1180 struct GNUNET_REGEX_Automaton *b;
1181 struct GNUNET_REGEX_Automaton *new;
1182 struct State *start;
1185 b = ctx->stack_tail;
1186 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1187 a = ctx->stack_tail;
1188 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1190 start = nfa_state_create (ctx, 0);
1191 end = nfa_state_create (ctx, 1);
1192 add_transition (ctx, start, 0, a->start);
1193 add_transition (ctx, start, 0, b->start);
1195 add_transition (ctx, a->end, 0, end);
1196 add_transition (ctx, b->end, 0, end);
1198 a->end->accepting = 0;
1199 b->end->accepting = 0;
1202 new = nfa_fragment_create (start, end);
1203 nfa_add_states (new, a->states_head, a->states_tail);
1204 nfa_add_states (new, b->states_head, b->states_tail);
1205 automaton_fragment_clear (a);
1206 automaton_fragment_clear (b);
1208 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1212 * Adds a new nfa fragment to the stack
1214 * @param ctx context
1215 * @param lit literal for nfa transition
1218 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
1220 struct GNUNET_REGEX_Automaton *n;
1221 struct State *start;
1224 GNUNET_assert (NULL != ctx);
1226 start = nfa_state_create (ctx, 0);
1227 end = nfa_state_create (ctx, 1);
1228 add_transition (ctx, start, lit, end);
1229 n = nfa_fragment_create (start, end);
1230 GNUNET_assert (NULL != n);
1231 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1235 * Construct an NFA by parsing the regex string of length 'len'.
1237 * @param regex regular expression string
1238 * @param len length of the string
1240 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1242 struct GNUNET_REGEX_Automaton *
1243 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1245 struct GNUNET_REGEX_Context ctx;
1246 struct GNUNET_REGEX_Automaton *nfa;
1250 unsigned int altcount;
1251 unsigned int atomcount;
1252 unsigned int pcount;
1259 GNUNET_REGEX_context_init (&ctx);
1268 for (count = 0; count < len && *regexp; count++, regexp++)
1276 nfa_add_concatenation (&ctx);
1278 GNUNET_array_grow (p, pcount, pcount + 1);
1279 p[pcount - 1].altcount = altcount;
1280 p[pcount - 1].atomcount = atomcount;
1287 error_msg = "Cannot append '|' to nothing";
1290 while (--atomcount > 0)
1291 nfa_add_concatenation (&ctx);
1297 error_msg = "Missing opening '('";
1302 // Ignore this: "()"
1304 altcount = p[pcount].altcount;
1305 atomcount = p[pcount].atomcount;
1308 while (--atomcount > 0)
1309 nfa_add_concatenation (&ctx);
1310 for (; altcount > 0; altcount--)
1311 nfa_add_alternation (&ctx);
1313 altcount = p[pcount].altcount;
1314 atomcount = p[pcount].atomcount;
1320 error_msg = "Cannot append '+' to nothing";
1323 nfa_add_star_op (&ctx);
1328 error_msg = "Cannot append '+' to nothing";
1331 nfa_add_plus_op (&ctx);
1333 case 92: /* escape: \ */
1340 nfa_add_concatenation (&ctx);
1342 nfa_add_literal (&ctx, *regexp);
1349 error_msg = "Unbalanced parenthesis";
1352 while (--atomcount > 0)
1353 nfa_add_concatenation (&ctx);
1354 for (; altcount > 0; altcount--)
1355 nfa_add_alternation (&ctx);
1360 nfa = ctx.stack_tail;
1361 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1364 if (NULL != ctx.stack_head)
1366 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1374 if (NULL != error_msg)
1375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1378 while (NULL != ctx.stack_tail)
1380 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1381 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1388 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1391 * @param a automaton to be destroyed
1394 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1397 struct State *next_state;
1402 for (s = a->states_head; NULL != s;)
1404 next_state = s->next;
1405 automaton_destroy_state (s);
1413 * Construct DFA for the given 'regex' of length 'len'
1415 * @param regex regular expression string
1416 * @param len length of the regular expression
1418 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1420 struct GNUNET_REGEX_Automaton *
1421 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1423 struct GNUNET_REGEX_Context ctx;
1424 struct GNUNET_REGEX_Automaton *dfa;
1425 struct GNUNET_REGEX_Automaton *nfa;
1426 struct StateSet *tmp;
1427 struct StateSet *nfa_set;
1428 struct StateSet *dfa_stack;
1429 struct Transition *ctran;
1430 struct State *dfa_state;
1431 struct State *new_dfa_state;
1432 struct State *state_contains;
1433 struct State *state_iter;
1435 GNUNET_REGEX_context_init (&ctx);
1438 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1443 "Could not create DFA, because NFA creation failed\n");
1447 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1450 // Create DFA start state from epsilon closure
1451 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1452 nfa_set = nfa_closure_create (nfa->start, 0);
1453 dfa->start = dfa_state_create (&ctx, nfa_set);
1454 automaton_add_state (dfa, dfa->start);
1455 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1457 // Create dfa states by combining nfa states
1458 while (dfa_stack->len > 0)
1460 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1461 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1463 for (ctran = dfa_state->transitions_head; NULL != ctran;
1464 ctran = ctran->next)
1466 if (0 != ctran->literal && NULL == ctran->state)
1468 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1469 nfa_set = nfa_closure_set_create (tmp, 0);
1470 state_set_clear (tmp);
1471 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1472 state_contains = NULL;
1473 for (state_iter = dfa->states_head; NULL != state_iter;
1474 state_iter = state_iter->next)
1477 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1478 state_contains = state_iter;
1481 if (NULL == state_contains)
1483 automaton_add_state (dfa, new_dfa_state);
1484 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1486 ctran->state = new_dfa_state;
1490 ctran->state = state_contains;
1491 automaton_destroy_state (new_dfa_state);
1497 GNUNET_free (dfa_stack);
1498 GNUNET_REGEX_automaton_destroy (nfa);
1500 dfa_minimize (&ctx, dfa);
1506 * Save the given automaton as a GraphViz dot file
1508 * @param a the automaton to be saved
1509 * @param filename where to save the file
1512 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1513 const char *filename)
1516 struct Transition *ctran;
1518 char *s_tran = NULL;
1525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1529 if (NULL == filename || strlen (filename) < 1)
1531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1535 p = fopen (filename, "w");
1539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1544 start = "digraph G {\nrankdir=LR\n";
1545 fwrite (start, strlen (start), 1, p);
1547 for (s = a->states_head; NULL != s; s = s->next)
1551 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1552 fwrite (s_acc, strlen (s_acc), 1, p);
1553 GNUNET_free (s_acc);
1558 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1560 if (NULL == ctran->state)
1562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1563 "Transition from State %i has has no state for transitioning\n",
1568 if (ctran->literal == 0)
1570 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1571 s->name, ctran->state->name);
1575 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1576 s->name, ctran->state->name, ctran->literal);
1579 fwrite (s_tran, strlen (s_tran), 1, p);
1580 GNUNET_free (s_tran);
1585 fwrite (end, strlen (end), 1, p);
1590 * Evaluates the given string using the given DFA automaton
1592 * @param a automaton, type must be DFA
1593 * @param string string that should be evaluated
1595 * @return 0 if string matches, non 0 otherwise
1598 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1606 "Tried to evaluate DFA, but NFA automaton given");
1612 for (strp = string; NULL != strp && *strp; strp++)
1614 s = dfa_move (s, *strp);
1619 if (NULL != s && s->accepting)
1626 * Evaluates the given string using the given NFA automaton
1628 * @param a automaton, type must be NFA
1629 * @param string string that should be evaluated
1631 * @return 0 if string matches, non 0 otherwise
1634 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1638 struct StateSet *sset;
1639 struct StateSet *new_sset;
1645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1646 "Tried to evaluate NFA, but DFA automaton given");
1652 sset = nfa_closure_create (a->start, 0);
1654 for (strp = string; NULL != strp && *strp; strp++)
1656 new_sset = nfa_closure_set_create (sset, *strp);
1657 state_set_clear (sset);
1658 sset = nfa_closure_set_create (new_sset, 0);
1659 state_set_clear (new_sset);
1662 for (i = 0; i < sset->len; i++)
1664 s = sset->states[i];
1665 if (NULL != s && s->accepting)
1672 state_set_clear (sset);
1677 * Evaluates the given 'string' against the given compiled regex
1679 * @param a automaton
1680 * @param string string to check
1682 * @return 0 if string matches, non 0 otherwise
1685 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1692 result = evaluate_dfa (a, string);
1695 result = evaluate_nfa (a, string);
1698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1699 "Evaluating regex failed, automaton has no type!\n");
1700 result = GNUNET_SYSERR;