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 * Compare two states. Used for sorting.
183 * @param a first state
184 * @param b second state
186 * @return an integer less than, equal to, or greater than zero
187 * if the first argument is considered to be respectively
188 * less than, equal to, or greater than the second.
191 state_compare (const void *a, const void *b)
196 s1 = (struct State **) a;
197 s2 = (struct State **) b;
199 return (*s1)->id - (*s2)->id;
203 * Compare to state sets by comparing the id's of the states that are
204 * contained in each set. Both sets are expected to be sorted by id!
206 * @param sset1 first state set
207 * @param sset2 second state set
209 * @return 0 if they are equal, non 0 otherwise
212 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
216 if (sset1->len != sset2->len)
219 for (i = 0; i < sset1->len; i++)
221 if (sset1->states[i]->id != sset2->states[i]->id)
230 * Checks if 'elem' is contained in 'set'
232 * @param set set of states
235 * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
238 state_set_contains (struct StateSet *set, struct State *elem)
243 for (i = 0; i < set->len; i++)
246 if (0 == memcmp (s, elem, sizeof (struct State)))
253 * Clears the given StateSet 'set'
255 * @param set set to be cleared
258 state_set_clear (struct StateSet *set)
262 if (NULL != set->states)
263 GNUNET_free (set->states);
269 * Adds a transition from one state to another on 'literal'
272 * @param from_state starting state for the transition
273 * @param literal transition label
274 * @param to_state state to where the transition should point to
277 add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
278 const char literal, struct State *to_state)
280 struct Transition *t;
282 if (NULL == from_state)
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
288 t = GNUNET_malloc (sizeof (struct Transition));
290 t->id = ctx->transition_id++;
291 t->literal = literal;
294 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
295 from_state->transitions_tail, t);
299 * Clears an automaton fragment. Does not destroy the states inside
302 * @param a automaton to be cleared
305 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
309 a->states_head = NULL;
310 a->states_tail = NULL;
316 * Frees the memory used by State 's'
318 * @param s state that should be destroyed
321 automaton_destroy_state (struct State *s)
323 struct Transition *t;
324 struct Transition *next_t;
327 GNUNET_free (s->name);
329 for (t = s->transitions_head; NULL != t;)
332 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
337 state_set_clear (s->nfa_set);
343 * Remove a state from the given automaton 'a'. Always use this function
344 * when altering the states of an automaton. Will also remove all transitions
345 * leading to this state, before destroying it.
348 * @param s state to remove
351 automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
354 struct State *s_check;
355 struct Transition *t_check;
359 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
362 // remove all transitions leading to this state
363 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
365 for (t_check = s_check->transitions_head; NULL != t_check;
366 t_check = t_check->next)
368 if (t_check->state == ss)
370 GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
371 s_check->transitions_tail, t_check);
372 s_check->transition_count--;
377 automaton_destroy_state (ss);
381 * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
385 * @param s1 first state
386 * @param s2 second state, will be destroyed
389 automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
390 struct GNUNET_REGEX_Automaton *a, struct State *s1,
393 struct State *s_check;
394 struct Transition *t_check;
395 struct Transition *t;
398 GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
400 // 1. Make all transitions pointing to s2 point to s1
401 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
403 for (t_check = s_check->transitions_head; NULL != t_check;
404 t_check = t_check->next)
406 if (s_check != s1 && s2 == t_check->state)
411 // 2. Add all transitions from s2 to sX to s1
412 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
414 for (t = s1->transitions_head; NULL != t; t = t->next)
416 if (t_check->literal != t->literal && NULL != t_check->state &&
417 t_check->state != t->state && t_check->state != s2)
419 add_transition (ctx, s1, t_check->literal, t_check->state);
424 // 3. Rename s1 to {s1,s2}
425 new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
426 strncat (new_name, s1->name, strlen (s1->name));
427 strncat (new_name, s2->name, strlen (s2->name));
428 if (NULL != s1->name)
429 GNUNET_free (s1->name);
434 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
436 automaton_destroy_state (s_check);
440 * Add a state to the automaton 'a', always use this function to
441 * alter the states DLL of the automaton.
443 * @param a automaton to add the state to
444 * @param s state that should be added
447 automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
449 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
454 * Creates a new DFA state based on a set of NFA states. Needs to be freed
455 * using automaton_destroy_state.
458 * @param nfa_states set of NFA states on which the DFA should be based on
460 * @return new DFA state
462 static struct State *
463 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
468 struct State *cstate;
469 struct Transition *ctran;
471 struct Transition *t;
474 s = GNUNET_malloc (sizeof (struct State));
475 s->id = ctx->state_id++;
480 if (NULL == nfa_states)
482 GNUNET_asprintf (&s->name, "s%i", s->id);
486 s->nfa_set = nfa_states;
488 if (nfa_states->len < 1)
491 // Create a name based on 'sset'
492 s->name = GNUNET_malloc (sizeof (char) * 2);
493 strcat (s->name, "{");
496 for (i = 0; i < nfa_states->len; i++)
498 cstate = nfa_states->states[i];
499 GNUNET_asprintf (&name, "%i,", cstate->id);
503 len = strlen (s->name) + strlen (name) + 1;
504 s->name = GNUNET_realloc (s->name, len);
505 strcat (s->name, name);
510 // Add a transition for each distinct literal to NULL state
511 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
513 if (0 != ctran->literal)
517 for (t = s->transitions_head; NULL != t; t = t->next)
519 if (t->literal == ctran->literal)
527 add_transition (ctx, s, ctran->literal, NULL);
531 // If the nfa_states contain an accepting state, the new dfa state is also accepting
532 if (cstate->accepting)
536 s->name[strlen (s->name) - 1] = '}';
542 * Move from the given state 's' to the next state on
543 * transition 'literal'
545 * @param s starting state
546 * @param literal edge label to follow
548 * @return new state or NULL, if transition on literal not possible
550 static struct State *
551 dfa_move (struct State *s, const char literal)
553 struct Transition *t;
561 for (t = s->transitions_head; NULL != t; t = t->next)
563 if (literal == t->literal)
574 * Remove all unreachable states from DFA 'a'. Unreachable states
575 * are those states that are not reachable from the starting state.
577 * @param a DFA automaton
580 dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
582 struct State *stack[a->state_count];
585 struct Transition *t;
589 // 1. unmark all states
590 for (s = a->states_head; NULL != s; s = s->next)
595 // 2. traverse dfa from start state and mark all visited states
596 stack[stack_len] = a->start;
598 while (stack_len > 0)
600 s = stack[stack_len - 1];
602 s->marked = 1; // mark s as visited
603 for (t = s->transitions_head; NULL != t; t = t->next)
605 if (NULL != t->state && 0 == t->state->marked)
607 // add next states to stack
608 stack[stack_len] = t->state;
614 // 3. delete all states that were not visited
615 for (s = a->states_head; NULL != s; s = s->next)
618 automaton_remove_state (a, s);
623 * Remove all dead states from the DFA 'a'. Dead states are those
624 * states that do not transition to any other state but themselfes.
626 * @param a DFA automaton
629 dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
632 struct Transition *t;
635 GNUNET_assert (DFA == a->type);
637 for (s = a->states_head; NULL != s; s = s->next)
643 for (t = s->transitions_head; NULL != t; t = t->next)
645 if (NULL != t->state && t->state != s)
655 // state s is dead, remove it
656 automaton_remove_state (a, s);
661 * Merge all non distinguishable states in the DFA 'a'
663 * @param a DFA automaton
666 dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
667 struct GNUNET_REGEX_Automaton *a)
670 int table[a->state_count][a->state_count];
673 struct Transition *t1;
674 struct Transition *t2;
678 for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
682 // Mark all pairs of accepting/!accepting states
683 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
685 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
687 if ((s1->accepting && !s2->accepting) ||
688 (!s1->accepting && s2->accepting))
690 table[s1->marked][s2->marked] = 1;
693 table[s1->marked][s2->marked] = 0;
700 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
702 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
704 if (0 != table[s1->marked][s2->marked])
707 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
709 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
711 if (t1->literal == t2->literal && t1->state == t2->state &&
712 (0 != table[t1->state->marked][t2->state->marked] ||
713 0 != table[t2->state->marked][t1->state->marked]))
715 table[s1->marked][s2->marked] = t1->literal;
718 else if (t1->literal != t2->literal && t1->state != t2->state)
720 table[s1->marked][s2->marked] = -1;
729 struct State *s2_next;
731 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
733 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
736 if (s1 != s2 && table[s1->marked][s2->marked] == 0)
737 automaton_merge_states (ctx, a, s1, s2);
743 * Minimize the given DFA 'a' by removing all unreachable states,
744 * removing all dead states and merging all non distinguishable states
746 * @param a DFA automaton
749 dfa_minimize (struct GNUNET_REGEX_Context *ctx,
750 struct GNUNET_REGEX_Automaton *a)
755 GNUNET_assert (DFA == a->type);
757 // 1. remove unreachable states
758 dfa_remove_unreachable_states (a);
760 // 2. remove dead states
761 dfa_remove_dead_states (a);
763 // 3. Merge nondistinguishable states
764 dfa_merge_nondistinguishable_states (ctx, a);
768 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
770 * @param start starting state
771 * @param end end state
773 * @return new NFA fragment
775 static struct GNUNET_REGEX_Automaton *
776 nfa_fragment_create (struct State *start, struct State *end)
778 struct GNUNET_REGEX_Automaton *n;
780 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
786 if (NULL == start && NULL == end)
789 automaton_add_state (n, end);
790 automaton_add_state (n, start);
799 * Adds a list of states to the given automaton 'n'.
801 * @param n automaton to which the states should be added
802 * @param states_head head of the DLL of states
803 * @param states_tail tail of the DLL of states
806 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
807 struct State *states_tail)
811 if (NULL == n || NULL == states_head)
813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
817 if (NULL == n->states_head)
819 n->states_head = states_head;
820 n->states_tail = states_tail;
824 if (NULL != states_head)
826 n->states_tail->next = states_head;
827 n->states_tail = states_tail;
830 for (s = states_head; NULL != s; s = s->next)
835 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
838 * @param accepting is it an accepting state or not
840 * @return new NFA state
842 static struct State *
843 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
847 s = GNUNET_malloc (sizeof (struct State));
848 s->id = ctx->state_id++;
849 s->accepting = accepting;
852 GNUNET_asprintf (&s->name, "s%i", s->id);
858 * Calculates the NFA closure set for the given state
860 * @param s starting point state
861 * @param literal transitioning literal on which to base the closure on,
862 * pass 0 for epsilon transition
864 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
866 static struct StateSet *
867 nfa_closure_create (struct State *s, const char literal)
869 struct StateSet *cls;
870 struct StateSet *cls_check;
871 struct State *clsstate;
872 struct State *currentstate;
873 struct Transition *ctran;
878 cls = GNUNET_malloc (sizeof (struct StateSet));
879 cls_check = GNUNET_malloc (sizeof (struct StateSet));
881 // Add start state to closure only for epsilon closure
883 GNUNET_array_append (cls->states, cls->len, s);
885 GNUNET_array_append (cls_check->states, cls_check->len, s);
886 while (cls_check->len > 0)
888 currentstate = cls_check->states[cls_check->len - 1];
889 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
891 for (ctran = currentstate->transitions_head; NULL != ctran;
894 if (NULL != ctran->state && literal == ctran->literal)
896 clsstate = ctran->state;
898 if (NULL != clsstate &&
899 GNUNET_YES != state_set_contains (cls, clsstate))
901 GNUNET_array_append (cls->states, cls->len, clsstate);
902 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
907 GNUNET_assert (0 == cls_check->len);
908 GNUNET_free (cls_check);
911 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
917 * Calculates the closure set for the given set of states.
919 * @param states list of states on which to base the closure on
920 * @param literal transitioning literal for which to base the closure on,
921 * pass 0 for epsilon transition
923 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
925 static struct StateSet *
926 nfa_closure_set_create (struct StateSet *states, const char literal)
929 struct StateSet *sset;
930 struct StateSet *cls;
939 cls = GNUNET_malloc (sizeof (struct StateSet));
941 for (i = 0; i < states->len; i++)
943 s = states->states[i];
944 sset = nfa_closure_create (s, literal);
946 for (j = 0; j < sset->len; j++)
949 for (k = 0; k < cls->len; k++)
951 if (sset->states[j]->id == cls->states[k]->id)
958 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
960 state_set_clear (sset);
964 qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
970 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
975 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
977 struct GNUNET_REGEX_Automaton *a;
978 struct GNUNET_REGEX_Automaton *b;
979 struct GNUNET_REGEX_Automaton *new;
982 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
984 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
986 add_transition (ctx, a->end, 0, b->start);
987 a->end->accepting = 0;
988 b->end->accepting = 1;
990 new = nfa_fragment_create (NULL, NULL);
991 nfa_add_states (new, a->states_head, a->states_tail);
992 nfa_add_states (new, b->states_head, b->states_tail);
993 new->start = a->start;
995 automaton_fragment_clear (a);
996 automaton_fragment_clear (b);
998 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1002 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
1004 * @param ctx context
1007 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
1009 struct GNUNET_REGEX_Automaton *a;
1010 struct GNUNET_REGEX_Automaton *new;
1011 struct State *start;
1014 a = ctx->stack_tail;
1015 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1020 "nfa_add_star_op failed, because there was no element on the stack");
1024 start = nfa_state_create (ctx, 0);
1025 end = nfa_state_create (ctx, 1);
1027 add_transition (ctx, start, 0, a->start);
1028 add_transition (ctx, start, 0, end);
1029 add_transition (ctx, a->end, 0, a->start);
1030 add_transition (ctx, a->end, 0, end);
1032 a->end->accepting = 0;
1035 new = nfa_fragment_create (start, end);
1036 nfa_add_states (new, a->states_head, a->states_tail);
1037 automaton_fragment_clear (a);
1039 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1043 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
1045 * @param ctx context
1048 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
1050 struct GNUNET_REGEX_Automaton *a;
1052 a = ctx->stack_tail;
1053 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1055 add_transition (ctx, a->end, 0, a->start);
1057 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
1061 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
1062 * that alternates between a and b (a|b)
1064 * @param ctx context
1067 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
1069 struct GNUNET_REGEX_Automaton *a;
1070 struct GNUNET_REGEX_Automaton *b;
1071 struct GNUNET_REGEX_Automaton *new;
1072 struct State *start;
1075 b = ctx->stack_tail;
1076 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
1077 a = ctx->stack_tail;
1078 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
1080 start = nfa_state_create (ctx, 0);
1081 end = nfa_state_create (ctx, 1);
1082 add_transition (ctx, start, 0, a->start);
1083 add_transition (ctx, start, 0, b->start);
1085 add_transition (ctx, a->end, 0, end);
1086 add_transition (ctx, b->end, 0, end);
1088 a->end->accepting = 0;
1089 b->end->accepting = 0;
1092 new = nfa_fragment_create (start, end);
1093 nfa_add_states (new, a->states_head, a->states_tail);
1094 nfa_add_states (new, b->states_head, b->states_tail);
1095 automaton_fragment_clear (a);
1096 automaton_fragment_clear (b);
1098 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
1102 * Adds a new nfa fragment to the stack
1104 * @param ctx context
1105 * @param lit literal for nfa transition
1108 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
1110 struct GNUNET_REGEX_Automaton *n;
1111 struct State *start;
1114 GNUNET_assert (NULL != ctx);
1116 start = nfa_state_create (ctx, 0);
1117 end = nfa_state_create (ctx, 1);
1118 add_transition (ctx, start, lit, end);
1119 n = nfa_fragment_create (start, end);
1120 GNUNET_assert (NULL != n);
1121 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
1125 * Construct an NFA by parsing the regex string of length 'len'.
1127 * @param regex regular expression string
1128 * @param len length of the string
1130 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1132 struct GNUNET_REGEX_Automaton *
1133 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
1135 struct GNUNET_REGEX_Context ctx;
1136 struct GNUNET_REGEX_Automaton *nfa;
1140 unsigned int altcount;
1141 unsigned int atomcount;
1142 unsigned int pcount;
1149 GNUNET_REGEX_context_init (&ctx);
1158 for (count = 0; count < len && *regexp; count++, regexp++)
1166 nfa_add_concatenation (&ctx);
1168 GNUNET_array_grow (p, pcount, pcount + 1);
1169 p[pcount - 1].altcount = altcount;
1170 p[pcount - 1].atomcount = atomcount;
1177 error_msg = "Cannot append '|' to nothing";
1180 while (--atomcount > 0)
1181 nfa_add_concatenation (&ctx);
1187 error_msg = "Missing opening '('";
1192 // Ignore this: "()"
1194 altcount = p[pcount].altcount;
1195 atomcount = p[pcount].atomcount;
1198 while (--atomcount > 0)
1199 nfa_add_concatenation (&ctx);
1200 for (; altcount > 0; altcount--)
1201 nfa_add_alternation (&ctx);
1203 altcount = p[pcount].altcount;
1204 atomcount = p[pcount].atomcount;
1210 error_msg = "Cannot append '+' to nothing";
1213 nfa_add_star_op (&ctx);
1218 error_msg = "Cannot append '+' to nothing";
1221 nfa_add_plus_op (&ctx);
1223 case 92: /* escape: \ */
1230 nfa_add_concatenation (&ctx);
1232 nfa_add_literal (&ctx, *regexp);
1239 error_msg = "Unbalanced parenthesis";
1242 while (--atomcount > 0)
1243 nfa_add_concatenation (&ctx);
1244 for (; altcount > 0; altcount--)
1245 nfa_add_alternation (&ctx);
1250 nfa = ctx.stack_tail;
1251 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
1254 if (NULL != ctx.stack_head)
1256 error_msg = "Creating the NFA failed. NFA stack was not empty!";
1263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
1264 if (NULL != error_msg)
1265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
1268 while (NULL != ctx.stack_tail)
1270 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
1271 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
1278 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
1281 * @param a automaton to be destroyed
1284 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
1287 struct State *next_state;
1292 for (s = a->states_head; NULL != s;)
1294 next_state = s->next;
1295 automaton_destroy_state (s);
1303 * Construct DFA for the given 'regex' of length 'len'
1305 * @param regex regular expression string
1306 * @param len length of the regular expression
1308 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
1310 struct GNUNET_REGEX_Automaton *
1311 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
1313 struct GNUNET_REGEX_Context ctx;
1314 struct GNUNET_REGEX_Automaton *dfa;
1315 struct GNUNET_REGEX_Automaton *nfa;
1316 struct StateSet *tmp;
1317 struct StateSet *nfa_set;
1318 struct StateSet *dfa_stack;
1319 struct Transition *ctran;
1320 struct State *dfa_state;
1321 struct State *new_dfa_state;
1322 struct State *state_contains;
1323 struct State *state_iter;
1325 GNUNET_REGEX_context_init (&ctx);
1328 nfa = GNUNET_REGEX_construct_nfa (regex, len);
1332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1333 "Could not create DFA, because NFA creation failed\n");
1337 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
1340 // Create DFA start state from epsilon closure
1341 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
1342 nfa_set = nfa_closure_create (nfa->start, 0);
1343 dfa->start = dfa_state_create (&ctx, nfa_set);
1344 automaton_add_state (dfa, dfa->start);
1345 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
1347 // Create dfa states by combining nfa states
1348 while (dfa_stack->len > 0)
1350 dfa_state = dfa_stack->states[dfa_stack->len - 1];
1351 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1353 for (ctran = dfa_state->transitions_head; NULL != ctran;
1354 ctran = ctran->next)
1356 if (0 != ctran->literal && NULL == ctran->state)
1358 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1359 nfa_set = nfa_closure_set_create (tmp, 0);
1360 state_set_clear (tmp);
1361 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1362 state_contains = NULL;
1363 for (state_iter = dfa->states_head; NULL != state_iter;
1364 state_iter = state_iter->next)
1367 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1368 state_contains = state_iter;
1371 if (NULL == state_contains)
1373 automaton_add_state (dfa, new_dfa_state);
1374 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1376 ctran->state = new_dfa_state;
1380 ctran->state = state_contains;
1381 automaton_destroy_state (new_dfa_state);
1387 GNUNET_free (dfa_stack);
1388 GNUNET_REGEX_automaton_destroy (nfa);
1390 dfa_minimize (&ctx, dfa);
1396 * Save the given automaton as a GraphViz dot file
1398 * @param a the automaton to be saved
1399 * @param filename where to save the file
1402 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1403 const char *filename)
1406 struct Transition *ctran;
1408 char *s_tran = NULL;
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1419 if (NULL == filename || strlen (filename) < 1)
1421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1425 p = fopen (filename, "w");
1429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1434 start = "digraph G {\nrankdir=LR\n";
1435 fwrite (start, strlen (start), 1, p);
1437 for (s = a->states_head; NULL != s; s = s->next)
1441 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1442 fwrite (s_acc, strlen (s_acc), 1, p);
1443 GNUNET_free (s_acc);
1448 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1450 if (NULL == ctran->state)
1452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1453 "Transition from State %i has has no state for transitioning\n",
1458 if (ctran->literal == 0)
1460 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1461 s->name, ctran->state->name);
1465 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1466 s->name, ctran->state->name, ctran->literal);
1469 fwrite (s_tran, strlen (s_tran), 1, p);
1470 GNUNET_free (s_tran);
1475 fwrite (end, strlen (end), 1, p);
1480 * Evaluates the given string using the given DFA automaton
1482 * @param a automaton, type must be DFA
1483 * @param string string that should be evaluated
1485 * @return 0 if string matches, non 0 otherwise
1488 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1496 "Tried to evaluate DFA, but NFA automaton given");
1502 for (strp = string; NULL != strp && *strp; strp++)
1504 s = dfa_move (s, *strp);
1509 if (NULL != s && s->accepting)
1516 * Evaluates the given string using the given NFA automaton
1518 * @param a automaton, type must be NFA
1519 * @param string string that should be evaluated
1521 * @return 0 if string matches, non 0 otherwise
1524 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1528 struct StateSet *sset;
1529 struct StateSet *new_sset;
1535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1536 "Tried to evaluate NFA, but DFA automaton given");
1542 sset = nfa_closure_create (a->start, 0);
1544 for (strp = string; NULL != strp && *strp; strp++)
1546 new_sset = nfa_closure_set_create (sset, *strp);
1547 state_set_clear (sset);
1548 sset = nfa_closure_set_create (new_sset, 0);
1549 state_set_clear (new_sset);
1552 for (i = 0; i < sset->len; i++)
1554 s = sset->states[i];
1555 if (NULL != s && s->accepting)
1562 state_set_clear (sset);
1567 * Evaluates the given 'string' against the given compiled regex
1569 * @param a automaton
1570 * @param string string to check
1572 * @return 0 if string matches, non 0 otherwise
1575 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1582 result = evaluate_dfa (a, string);
1585 result = evaluate_nfa (a, string);
1588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1589 "Evaluating regex failed, automaton has no type!\n");
1590 result = GNUNET_SYSERR;