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 struct State *states_head;
64 struct State *states_tail;
66 enum GNUNET_REGEX_automaton_type type;
70 * A state. Can be used in DFA and NFA automatons.
82 struct Transition *transitions_head;
83 struct Transition *transitions_tail;
85 struct StateSet *nfa_set;
89 * Transition between two states. Each state can have 0-n transitions.
90 * If literal is 0, this is considered to be an epsilon transition.
94 struct Transition *prev;
95 struct Transition *next;
110 struct State **states;
115 * Initialize a new context
120 GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
128 ctx->transition_id = 0;
129 ctx->stack_head = NULL;
130 ctx->stack_tail = NULL;
134 debug_print_state (struct State *s)
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
138 s->marked, s->accepting);
142 debug_print_states (struct StateSet *sset)
147 for (i = 0; i < sset->len; i++)
150 debug_print_state (s);
155 debug_print_transitions (struct State *s)
157 struct Transition *t;
161 for (t = s->transitions_head; NULL != t; t = t->next)
166 literal = t->literal;
168 if (NULL == t->state)
171 state = t->state->name;
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
179 * Compare to state sets by comparing the id's of the states that are
180 * contained in each set.
182 * @param sset1 first state set
183 * @param sset2 second state set
185 * @return 0 if they are equal, non 0 otherwise
188 state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
197 if (sset1->len < 1 || sset2->len < 1)
202 for (i1 = 0; i1 < sset1->len; i1++)
204 s1 = sset1->states[i1];
206 for (i2 = 0; i2 < sset2->len; i2++)
208 s2 = sset2->states[i2];
209 if (s1->id == s2->id)
226 * Checks if 'elem' is contained in 'set'
228 * @param set set of states
231 * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
234 state_set_contains (struct StateSet *set, struct State *elem)
239 for (i = 0; i < set->len; i++)
242 if (0 == memcmp (s, elem, sizeof (struct State)))
249 * Clears the given StateSet 'set'
251 * @param set set to be cleared
254 state_set_clear (struct StateSet *set)
258 if (NULL != set->states)
259 GNUNET_free (set->states);
265 * Adds a transition from one state to another on 'literal'
268 * @param from_state starting state for the transition
269 * @param literal transition label
270 * @param to_state state to where the transition should point to
273 add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
274 const char literal, struct State *to_state)
276 struct Transition *t;
278 if (NULL == from_state)
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
284 t = GNUNET_malloc (sizeof (struct Transition));
286 t->id = ctx->transition_id++;
287 t->literal = literal;
290 GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
291 from_state->transitions_tail, t);
295 * Clears an automaton fragment. Does not destroy the states inside
298 * @param a automaton to be cleared
301 automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
305 a->states_head = NULL;
306 a->states_tail = NULL;
311 * Frees the memory used by State 's'
313 * @param s state that should be destroyed
316 automaton_destroy_state (struct State *s)
318 struct Transition *t;
319 struct Transition *next_t;
322 GNUNET_free (s->name);
324 for (t = s->transitions_head; NULL != t;)
327 GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
332 state_set_clear (s->nfa_set);
338 * Creates a new DFA state based on a set of NFA states. Needs to be freed
339 * using automaton_destroy_state.
342 * @param nfa_states set of NFA states on which the DFA should be based on
344 * @return new DFA state
347 dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
352 struct State *cstate;
353 struct Transition *ctran;
355 struct Transition *t;
358 s = GNUNET_malloc (sizeof (struct State));
359 s->id = ctx->state_id++;
364 if (NULL == nfa_states)
367 s->nfa_set = nfa_states;
369 if (nfa_states->len < 1)
372 // Create a name based on 'sset'
373 s->name = GNUNET_malloc (sizeof (char) * 2);
374 strcat (s->name, "{");
377 for (i = 0; i < nfa_states->len; i++)
379 cstate = nfa_states->states[i];
380 GNUNET_asprintf (&name, "%i,", cstate->id);
384 len = strlen (s->name) + strlen (name) + 1;
385 s->name = GNUNET_realloc (s->name, len);
386 strcat (s->name, name);
391 // Add a transition for each distinct literal to NULL state
392 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
394 if (0 != ctran->literal)
398 for (t = s->transitions_head; NULL != t; t = t->next)
400 if (t->literal == ctran->literal)
408 add_transition (ctx, s, ctran->literal, NULL);
412 // If the nfa_states contain an accepting state, the new dfa state is also accepting
413 if (cstate->accepting)
417 s->name[strlen (s->name) - 1] = '}';
423 dfa_move (struct State *s, const char literal)
425 struct Transition *t;
433 for (t = s->transitions_head; NULL != t; t = t->next)
435 if (literal == t->literal)
446 * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
448 * @param start starting state
449 * @param end end state
451 * @return new NFA fragment
453 struct GNUNET_REGEX_Automaton *
454 nfa_fragment_create (struct State *start, struct State *end)
456 struct GNUNET_REGEX_Automaton *n;
458 n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
464 if (NULL == start && NULL == end)
467 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, end);
468 GNUNET_CONTAINER_DLL_insert (n->states_head, n->states_tail, start);
477 * Adds a list of states to the given automaton 'n'.
479 * @param n automaton to which the states should be added
480 * @param states_head head of the DLL of states
481 * @param states_tail tail of the DLL of states
484 nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
485 struct State *states_tail)
487 if (NULL == n || NULL == states_head)
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
493 if (NULL == n->states_head)
495 n->states_head = states_head;
496 n->states_tail = states_tail;
500 if (NULL != states_head)
502 n->states_tail->next = states_head;
503 n->states_tail = states_tail;
508 * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
511 * @param accepting is it an accepting state or not
513 * @return new NFA state
516 nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
520 s = GNUNET_malloc (sizeof (struct State));
521 s->id = ctx->state_id++;
522 s->accepting = accepting;
525 GNUNET_asprintf (&s->name, "s%i", s->id);
531 * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
536 nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
538 struct GNUNET_REGEX_Automaton *a;
539 struct GNUNET_REGEX_Automaton *b;
540 struct GNUNET_REGEX_Automaton *new;
543 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
545 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
547 add_transition (ctx, a->end, 0, b->start);
548 a->end->accepting = 0;
549 b->end->accepting = 1;
551 new = nfa_fragment_create (NULL, NULL);
552 nfa_add_states (new, a->states_head, a->states_tail);
553 nfa_add_states (new, b->states_head, b->states_tail);
554 new->start = a->start;
556 automaton_fragment_clear (a);
557 automaton_fragment_clear (b);
559 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
563 * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
568 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
570 struct GNUNET_REGEX_Automaton *a;
571 struct GNUNET_REGEX_Automaton *new;
576 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 "nfa_add_star_op failed, because there was no element on the stack");
585 start = nfa_state_create (ctx, 0);
586 end = nfa_state_create (ctx, 1);
588 add_transition (ctx, start, 0, a->start);
589 add_transition (ctx, start, 0, end);
590 add_transition (ctx, a->end, 0, a->start);
591 add_transition (ctx, a->end, 0, end);
593 a->end->accepting = 0;
596 new = nfa_fragment_create (start, end);
597 nfa_add_states (new, a->states_head, a->states_tail);
598 automaton_fragment_clear (a);
600 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
604 * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
609 nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
611 struct GNUNET_REGEX_Automaton *a;
614 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
616 add_transition (ctx, a->end, 0, a->start);
618 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
622 * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
623 * that alternates between a and b (a|b)
628 nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
630 struct GNUNET_REGEX_Automaton *a;
631 struct GNUNET_REGEX_Automaton *b;
632 struct GNUNET_REGEX_Automaton *new;
637 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
639 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
641 start = nfa_state_create (ctx, 0);
642 end = nfa_state_create (ctx, 1);
643 add_transition (ctx, start, 0, a->start);
644 add_transition (ctx, start, 0, b->start);
646 add_transition (ctx, a->end, 0, end);
647 add_transition (ctx, b->end, 0, end);
649 a->end->accepting = 0;
650 b->end->accepting = 0;
653 new = nfa_fragment_create (start, end);
654 nfa_add_states (new, a->states_head, a->states_tail);
655 nfa_add_states (new, b->states_head, b->states_tail);
656 automaton_fragment_clear (a);
657 automaton_fragment_clear (b);
659 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
663 * Adds a new nfa fragment to the stack
666 * @param lit literal for nfa transition
669 nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
671 struct GNUNET_REGEX_Automaton *n;
675 GNUNET_assert (NULL != ctx);
677 start = nfa_state_create (ctx, 0);
678 end = nfa_state_create (ctx, 1);
679 add_transition (ctx, start, lit, end);
680 n = nfa_fragment_create (start, end);
681 GNUNET_assert (NULL != n);
682 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
686 * Calculates the NFA closure set for the given state
688 * @param s starting point state
689 * @param literal transitioning literal on which to base the closure on,
690 * pass 0 for epsilon transition
692 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
695 nfa_closure_create (struct State *s, const char literal)
697 struct StateSet *cls;
698 struct StateSet *cls_check;
699 struct State *clsstate;
700 struct State *currentstate;
701 struct Transition *ctran;
706 cls = GNUNET_malloc (sizeof (struct StateSet));
707 cls_check = GNUNET_malloc (sizeof (struct StateSet));
709 // Add start state to closure only for epsilon closure
711 GNUNET_array_append (cls->states, cls->len, s);
713 GNUNET_array_append (cls_check->states, cls_check->len, s);
714 while (cls_check->len > 0)
716 currentstate = cls_check->states[cls_check->len - 1];
717 GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
719 for (ctran = currentstate->transitions_head; NULL != ctran;
722 if (NULL != ctran->state && literal == ctran->literal)
724 clsstate = ctran->state;
726 if (NULL != clsstate &&
727 GNUNET_YES != state_set_contains (cls, clsstate))
729 GNUNET_array_append (cls->states, cls->len, clsstate);
730 GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
735 GNUNET_assert (0 == cls_check->len);
736 GNUNET_free (cls_check);
742 * Calculates the closure set for the given set of states.
744 * @param states list of states on which to base the closure on
745 * @param literal transitioning literal for which to base the closure on,
746 * pass 0 for epsilon transition
748 * @return nfa closure on 'literal' (epsilon closure if 'literal' is 0)
751 nfa_closure_set_create (struct StateSet *states, const char literal)
754 struct StateSet *sset;
755 struct StateSet *cls;
762 cls = GNUNET_malloc (sizeof (struct StateSet));
764 for (i = 0; i < states->len; i++)
766 s = states->states[i];
767 sset = nfa_closure_create (s, literal);
769 for (j = 0; j < sset->len; j++)
770 GNUNET_array_append (cls->states, cls->len, sset->states[j]);
772 state_set_clear (sset);
779 * Construct an NFA by parsing the regex string of length 'len'.
781 * @param regex regular expression string
782 * @param len length of the string
784 * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
786 struct GNUNET_REGEX_Automaton *
787 GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
789 struct GNUNET_REGEX_Context ctx;
790 struct GNUNET_REGEX_Automaton *nfa;
794 unsigned int altcount;
795 unsigned int atomcount;
803 GNUNET_REGEX_context_init (&ctx);
812 for (count = 0; count < len && *regexp; count++, regexp++)
820 nfa_add_concatenation (&ctx);
822 GNUNET_array_grow (p, pcount, pcount + 1);
823 p[pcount - 1].altcount = altcount;
824 p[pcount - 1].atomcount = atomcount;
831 error_msg = "Cannot append '|' to nothing";
834 while (--atomcount > 0)
835 nfa_add_concatenation (&ctx);
841 error_msg = "Missing opening '('";
848 altcount = p[pcount].altcount;
849 atomcount = p[pcount].atomcount;
852 while (--atomcount > 0)
853 nfa_add_concatenation (&ctx);
854 for (; altcount > 0; altcount--)
855 nfa_add_alternation (&ctx);
857 altcount = p[pcount].altcount;
858 atomcount = p[pcount].atomcount;
864 error_msg = "Cannot append '+' to nothing";
867 nfa_add_star_op (&ctx);
872 error_msg = "Cannot append '+' to nothing";
875 nfa_add_plus_op (&ctx);
877 case 92: /* escape: \ */
884 nfa_add_concatenation (&ctx);
886 nfa_add_literal (&ctx, *regexp);
893 error_msg = "Unbalanced parenthesis";
896 while (--atomcount > 0)
897 nfa_add_concatenation (&ctx);
898 for (; altcount > 0; altcount--)
899 nfa_add_alternation (&ctx);
904 nfa = ctx.stack_tail;
905 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
908 if (NULL != ctx.stack_head)
910 error_msg = "Creating the NFA failed. NFA stack was not empty!";
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Created NFA with %i States and a total of %i Transitions\n",
916 ctx.state_id, ctx.transition_id);
921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
922 if (NULL != error_msg)
923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
925 while (NULL != ctx.stack_tail)
927 GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
928 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
935 * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
938 * @param a automaton to be destroyed
941 GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
944 struct State *next_state;
949 for (s = a->states_head; NULL != s;)
951 next_state = s->next;
952 automaton_destroy_state (s);
960 * Construct DFA for the given 'regex' of length 'len'
962 * @param regex regular expression string
963 * @param len length of the regular expression
965 * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
967 struct GNUNET_REGEX_Automaton *
968 GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
970 struct GNUNET_REGEX_Context ctx;
971 struct GNUNET_REGEX_Automaton *dfa;
972 struct GNUNET_REGEX_Automaton *nfa;
973 struct StateSet *tmp;
974 struct StateSet *nfa_set;
975 struct StateSet *dfa_stack;
976 struct Transition *ctran;
977 struct State *dfa_state;
978 struct State *new_dfa_state;
979 struct State *state_contains;
980 struct State *state_iter;
982 GNUNET_REGEX_context_init (&ctx);
985 nfa = GNUNET_REGEX_construct_nfa (regex, len);
987 dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
990 // Create DFA start state from epsilon closure
991 dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
992 nfa_set = nfa_closure_create (nfa->start, 0);
993 dfa->start = dfa_state_create (&ctx, nfa_set);
994 GNUNET_CONTAINER_DLL_insert (dfa->states_head, dfa->states_tail, dfa->start);
995 GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
996 while (dfa_stack->len > 0)
998 dfa_state = dfa_stack->states[dfa_stack->len - 1];
999 GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
1001 for (ctran = dfa_state->transitions_head; NULL != ctran;
1002 ctran = ctran->next)
1004 if (0 != ctran->literal && NULL == ctran->state)
1006 tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
1007 nfa_set = nfa_closure_set_create (tmp, 0);
1008 state_set_clear (tmp);
1009 new_dfa_state = dfa_state_create (&ctx, nfa_set);
1010 state_contains = NULL;
1011 for (state_iter = dfa->states_head; NULL != state_iter;
1012 state_iter = state_iter->next)
1015 state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
1016 state_contains = state_iter;
1019 if (NULL == state_contains)
1021 GNUNET_CONTAINER_DLL_insert_tail (dfa->states_head, dfa->states_tail,
1023 GNUNET_array_append (dfa_stack->states, dfa_stack->len,
1025 ctran->state = new_dfa_state;
1029 ctran->state = state_contains;
1030 automaton_destroy_state (new_dfa_state);
1036 GNUNET_free (dfa_stack);
1037 GNUNET_REGEX_automaton_destroy (nfa);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA with %i States\n",
1046 * Save the given automaton as a GraphViz dot file
1048 * @param a the automaton to be saved
1049 * @param filename where to save the file
1052 GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
1053 const char *filename)
1056 struct Transition *ctran;
1058 char *s_tran = NULL;
1065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
1069 if (NULL == filename || strlen (filename) < 1)
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
1075 p = fopen (filename, "w");
1079 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
1084 start = "digraph G {\nrankdir=LR\n";
1085 fwrite (start, strlen (start), 1, p);
1087 for (s = a->states_head; NULL != s; s = s->next)
1091 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
1092 fwrite (s_acc, strlen (s_acc), 1, p);
1093 GNUNET_free (s_acc);
1098 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
1100 if (NULL == ctran->state)
1102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1103 "Transition from State %i has has no state for transitioning\n",
1108 if (ctran->literal == 0)
1110 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
1111 s->name, ctran->state->name);
1115 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
1116 s->name, ctran->state->name, ctran->literal);
1119 fwrite (s_tran, strlen (s_tran), 1, p);
1120 GNUNET_free (s_tran);
1125 fwrite (end, strlen (end), 1, p);
1130 * Evaluates the given string using the given DFA automaton
1132 * @param a automaton, type must be DFA
1133 * @param string string that should be evaluated
1135 * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR otherwise
1138 evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1146 "Tried to evaluate NFA, but DFA automaton given");
1147 return GNUNET_SYSERR;
1152 for (strp = string; NULL != strp && *strp; strp++)
1154 s = dfa_move (s, *strp);
1159 if (NULL != s && s->accepting)
1166 * Evaluates the given string using the given NFA automaton
1168 * @param a automaton, type must be NFA
1169 * @param string string that should be evaluated
1171 * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR otherwise
1174 evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
1178 struct StateSet *sset;
1179 struct StateSet *new_sset;
1185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1186 "Tried to evaluate NFA, but DFA automaton given");
1187 return GNUNET_SYSERR;
1192 sset = GNUNET_malloc (sizeof (struct StateSet));
1193 GNUNET_array_append (sset->states, sset->len, a->start);
1195 for (strp = string; NULL != strp && *strp; strp++)
1197 new_sset = nfa_closure_set_create (sset, *strp);
1198 state_set_clear (sset);
1199 sset = nfa_closure_set_create (new_sset, 0);
1200 state_set_clear (new_sset);
1203 for (i = 0; i < sset->len; i++)
1205 s = sset->states[i];
1206 if (NULL != s && s->accepting)
1213 state_set_clear (sset);
1219 * Evaluates the given 'string' against the given compiled regex
1221 * @param a automaton
1222 * @param string string to check
1224 * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise
1227 GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
1234 eval = evaluate_dfa (a, string);
1237 eval = evaluate_nfa (a, string);
1240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1241 "Evaluating regex failed, automaton has no type!\n");
1242 eval = GNUNET_SYSERR;