* @author Maximilian Szengel
*/
#include "platform.h"
+#include "gnunet_container_lib.h"
#include "gnunet_regex_lib.h"
#include "regex.h"
-struct Stack
+/**
+ * Context that contains an id counter for states and transitions
+ * as well as a DLL of automatons used as a stack for NFA construction.
+ */
+struct GNUNET_REGEX_Context
+{
+ /**
+ * Unique state id.
+ */
+ unsigned int state_id;
+
+ /**
+ * Unique transition id.
+ */
+ unsigned int transition_id;
+
+ /**
+ * DLL of GNUNET_REGEX_Automaton's used as a stack.
+ */
+ struct GNUNET_REGEX_Automaton *stack_head;
+
+ /**
+ * DLL of GNUNET_REGEX_Automaton's used as a stack.
+ */
+ struct GNUNET_REGEX_Automaton *stack_tail;
+};
+
+/**
+ * Type of an automaton.
+ */
+enum GNUNET_REGEX_automaton_type
{
- void *data;
- struct Stack *next;
+ NFA,
+ DFA
};
-static struct Stack *nfa_stack = NULL;
+/**
+ * Automaton representation.
+ */
+struct GNUNET_REGEX_Automaton
+{
+ /**
+ * This is a linked list.
+ */
+ struct GNUNET_REGEX_Automaton *prev;
+
+ /**
+ * This is a linked list.
+ */
+ struct GNUNET_REGEX_Automaton *next;
+
+ /**
+ * First state of the automaton. This is mainly
+ * used for constructing an NFA, where each NFA
+ * itself consists of one or more NFAs linked
+ * together.
+ */
+ struct State *start;
-void
-push (void *val, struct Stack **stack)
+ /**
+ * End state of the automaton.
+ */
+ struct State *end;
+
+ /**
+ * Number of states in the automaton.
+ */
+ unsigned int state_count;
+
+ /**
+ * DLL of states.
+ */
+ struct State *states_head;
+
+ /**
+ * DLL of states
+ */
+ struct State *states_tail;
+
+ /**
+ * Type of the automaton.
+ */
+ enum GNUNET_REGEX_automaton_type type;
+};
+
+/**
+ * A state. Can be used in DFA and NFA automatons.
+ */
+struct State
+{
+ /**
+ * This is a linked list.
+ */
+ struct State *prev;
+
+ /**
+ * This is a linked list.
+ */
+ struct State *next;
+
+ /**
+ * Unique state id.
+ */
+ unsigned int id;
+
+ /**
+ * If this is an accepting state or not.
+ */
+ int accepting;
+
+ /**
+ * Marking of the state. This is used for marking all visited
+ * states when traversing all states of an automaton and for
+ * cases where the state id cannot be used (dfa minimization).
+ */
+ int marked;
+
+ /**
+ * Human readable name of the automaton. Used for debugging
+ * and graph creation.
+ */
+ char *name;
+
+ /**
+ * Number of transitions from this state to other states.
+ */
+ unsigned int transition_count;
+
+ /**
+ * DLL of transitions.
+ */
+ struct Transition *transitions_head;
+
+ /**
+ * DLL of transitions.
+ */
+ struct Transition *transitions_tail;
+
+ /**
+ * Set of states on which this state is based on. Used when
+ * creating a DFA out of several NFA states.
+ */
+ struct StateSet *nfa_set;
+};
+
+/**
+ * Transition between two states. Each state can have 0-n transitions.
+ * If literal is 0, this is considered to be an epsilon transition.
+ */
+struct Transition
+{
+ /**
+ * This is a linked list.
+ */
+ struct Transition *prev;
+
+ /**
+ * This is a linked list.
+ */
+ struct Transition *next;
+
+ /**
+ * Unique id of this transition.
+ */
+ unsigned int id;
+
+ /**
+ * Literal for this transition. This is basically the edge label for
+ * the graph.
+ */
+ char literal;
+
+ /**
+ * State to which this transition leads.
+ */
+ struct State *state;
+};
+
+/**
+ * Set of states.
+ */
+struct StateSet
{
- struct Stack *new = GNUNET_malloc (sizeof (struct Stack *));
+ /**
+ * Array of states.
+ */
+ struct State **states;
- if (NULL == new)
+ /**
+ * Length of the 'states' array.
+ */
+ unsigned int len;
+};
+
+/**
+ * Initialize a new context
+ *
+ * @param ctx context
+ */
+static void
+GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
+{
+ if (NULL == ctx)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not push to stack\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
return;
}
- new->data = val;
- new->next = *stack;
- *stack = new;
+ ctx->state_id = 0;
+ ctx->transition_id = 0;
+ ctx->stack_head = NULL;
+ ctx->stack_tail = NULL;
}
-int
-empty (struct Stack **stack)
+static void
+debug_print_state (struct State *s)
{
- return (NULL == *stack || NULL == stack);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
+ s->marked, s->accepting);
}
-void *
-pop (struct Stack **stack)
+static void
+debug_print_states (struct StateSet *sset)
{
- struct Stack *top;
- void *val;
+ struct State *s;
+ int i;
- if (empty (stack))
- return NULL;
+ for (i = 0; i < sset->len; i++)
+ {
+ s = sset->states[i];
+ debug_print_state (s);
+ }
+}
- top = *stack;
- val = top->data;
- *stack = top->next;
- GNUNET_free (top);
- return val;
+static void
+debug_print_transitions (struct State *s)
+{
+ struct Transition *t;
+ char *state;
+ char literal;
+
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ if (0 == t->literal)
+ literal = '0';
+ else
+ literal = t->literal;
+
+ if (NULL == t->state)
+ state = "NULL";
+ else
+ state = t->state->name;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
+ literal, state);
+ }
}
-void *
-top (struct Stack **stack)
+/**
+ * Compare two states. Used for sorting.
+ *
+ * @param a first state
+ * @param b second state
+ *
+ * @return an integer less than, equal to, or greater than zero
+ * if the first argument is considered to be respectively
+ * less than, equal to, or greater than the second.
+ */
+static int
+state_compare (const void *a, const void *b)
{
- if (empty (stack))
- return NULL;
+ struct State **s1;
+ struct State **s2;
+
+ s1 = (struct State **) a;
+ s2 = (struct State **) b;
- return (*stack)->data;
+ return (*s1)->id - (*s2)->id;
}
-struct State
+/**
+ * Compare to state sets by comparing the id's of the states that are
+ * contained in each set. Both sets are expected to be sorted by id!
+ *
+ * @param sset1 first state set
+ * @param sset2 second state set
+ *
+ * @return an integer less than, equal to, or greater than zero
+ * if the first argument is considered to be respectively
+ * less than, equal to, or greater than the second.
+ */
+static int
+state_set_compare (struct StateSet *sset1, struct StateSet *sset2)
{
- unsigned int id;
- int accepting;
- unsigned int tcnt;
- struct Transition *transitions;
- int marked;
- char *name;
-};
+ int result;
+ int i;
-struct StateSet
+ if (NULL == sset1 || NULL == sset2)
+ return 1;
+
+ result = sset1->len - sset2->len;
+
+ for (i = 0; i < sset1->len; i++)
+ {
+ if (0 != result)
+ break;
+
+ result = state_compare (&sset1->states[i], &sset2->states[i]);
+ }
+ return result;
+}
+
+/**
+ * Checks if 'elem' is contained in 'set'
+ *
+ * @param set set of states
+ * @param elem state
+ *
+ * @return GNUNET_YES if 'set' contains 'elem, GNUNET_NO otherwise
+ */
+static int
+state_set_contains (struct StateSet *set, struct State *elem)
{
- struct State **states;
- unsigned int count;
-};
+ struct State *s;
+ int i;
-struct GNUNET_REGEX_Automaton
+ for (i = 0; i < set->len; i++)
+ {
+ s = set->states[i];
+ if (0 == memcmp (s, elem, sizeof (struct State)))
+ return GNUNET_YES;
+ }
+ return GNUNET_NO;
+}
+
+/**
+ * Clears the given StateSet 'set'
+ *
+ * @param set set to be cleared
+ */
+static void
+state_set_clear (struct StateSet *set)
{
- struct State *start;
- struct State *end;
+ if (NULL != set)
+ {
+ if (NULL != set->states)
+ GNUNET_free (set->states);
+ GNUNET_free (set);
+ }
+}
- struct StateSet sset;
-};
+/**
+ * Adds a transition from one state to another on 'literal'
+ *
+ * @param ctx context
+ * @param from_state starting state for the transition
+ * @param literal transition label
+ * @param to_state state to where the transition should point to
+ */
+static void
+add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
+ const char literal, struct State *to_state)
+{
+ struct Transition *t;
-struct Transition
+ if (NULL == from_state)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
+ return;
+ }
+
+ t = GNUNET_malloc (sizeof (struct Transition));
+
+ t->id = ctx->transition_id++;
+ t->literal = literal;
+ t->state = to_state;
+
+ GNUNET_CONTAINER_DLL_insert (from_state->transitions_head,
+ from_state->transitions_tail, t);
+}
+
+/**
+ * Clears an automaton fragment. Does not destroy the states inside
+ * the automaton.
+ *
+ * @param a automaton to be cleared
+ */
+static void
+automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
{
- unsigned int id;
- char literal;
- struct State *state;
-};
+ if (NULL == a)
+ return;
-struct GNUNET_REGEX_Context
+ a->start = NULL;
+ a->end = NULL;
+ a->states_head = NULL;
+ a->states_tail = NULL;
+ a->state_count = 0;
+ GNUNET_free (a);
+}
+
+/**
+ * Frees the memory used by State 's'
+ *
+ * @param s state that should be destroyed
+ */
+static void
+automaton_destroy_state (struct State *s)
{
- unsigned int state_id;
- unsigned int transition_id;
-};
+ struct Transition *t;
+ struct Transition *next_t;
-struct State *
-dfa_create_state (struct GNUNET_REGEX_Context *ctx, struct StateSet *sset, int accepting)
+ if (NULL == s)
+ return;
+
+ if (NULL != s->name)
+ GNUNET_free (s->name);
+
+ for (t = s->transitions_head; NULL != t;)
+ {
+ next_t = t->next;
+ GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t);
+ GNUNET_free (t);
+ t = next_t;
+ }
+
+ state_set_clear (s->nfa_set);
+
+ GNUNET_free (s);
+}
+
+/**
+ * Remove a state from the given automaton 'a'. Always use this function
+ * when altering the states of an automaton. Will also remove all transitions
+ * leading to this state, before destroying it.
+ *
+ * @param a automaton
+ * @param s state to remove
+ */
+static void
+automaton_remove_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
+{
+ struct State *ss;
+ struct State *s_check;
+ struct Transition *t_check;
+
+ if (NULL == a || NULL == s)
+ return;
+
+ // remove state
+ ss = s;
+ GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
+ a->state_count--;
+
+ // remove all transitions leading to this state
+ for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
+ {
+ for (t_check = s_check->transitions_head; NULL != t_check;
+ t_check = t_check->next)
+ {
+ if (t_check->state == ss)
+ {
+ GNUNET_CONTAINER_DLL_remove (s_check->transitions_head,
+ s_check->transitions_tail, t_check);
+ s_check->transition_count--;
+ }
+ }
+ }
+
+ automaton_destroy_state (ss);
+}
+
+/**
+ * Merge two states into one. Will merge 's1' and 's2' into 's1' and destroy 's2'.
+ *
+ * @param ctx context
+ * @param a automaton
+ * @param s1 first state
+ * @param s2 second state, will be destroyed
+ */
+static void
+automaton_merge_states (struct GNUNET_REGEX_Context *ctx,
+ struct GNUNET_REGEX_Automaton *a, struct State *s1,
+ struct State *s2)
+{
+ struct State *s_check;
+ struct Transition *t_check;
+ struct Transition *t;
+ char *new_name;
+
+ GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
+
+ // 1. Make all transitions pointing to s2 point to s1
+ for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
+ {
+ for (t_check = s_check->transitions_head; NULL != t_check;
+ t_check = t_check->next)
+ {
+ if (s_check != s1 && s2 == t_check->state)
+ t_check->state = s1;
+ }
+ }
+
+ // 2. Add all transitions from s2 to sX to s1
+ for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
+ {
+ for (t = s1->transitions_head; NULL != t; t = t->next)
+ {
+ if (t_check->literal != t->literal && NULL != t_check->state &&
+ t_check->state != t->state && t_check->state != s2)
+ {
+ add_transition (ctx, s1, t_check->literal, t_check->state);
+ }
+ }
+ }
+
+ // 3. Rename s1 to {s1,s2}
+ new_name = GNUNET_malloc (strlen (s1->name) + strlen (s2->name) + 1);
+ strncat (new_name, s1->name, strlen (s1->name));
+ strncat (new_name, s2->name, strlen (s2->name));
+ if (NULL != s1->name)
+ GNUNET_free (s1->name);
+ s1->name = new_name;
+
+ // remove state
+ s_check = s2;
+ GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s_check);
+ a->state_count--;
+ automaton_destroy_state (s_check);
+}
+
+/**
+ * Add a state to the automaton 'a', always use this function to
+ * alter the states DLL of the automaton.
+ *
+ * @param a automaton to add the state to
+ * @param s state that should be added
+ */
+static void
+automaton_add_state (struct GNUNET_REGEX_Automaton *a, struct State *s)
+{
+ GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s);
+ a->state_count++;
+}
+
+/**
+ * Creates a new DFA state based on a set of NFA states. Needs to be freed
+ * using automaton_destroy_state.
+ *
+ * @param ctx context
+ * @param nfa_states set of NFA states on which the DFA should be based on
+ *
+ * @return new DFA state
+ */
+static struct State *
+dfa_state_create (struct GNUNET_REGEX_Context *ctx, struct StateSet *nfa_states)
{
- int i;
struct State *s;
char *name;
+ int len = 0;
+ struct State *cstate;
+ struct Transition *ctran;
+ int insert = 1;
+ struct Transition *t;
+ int i;
s = GNUNET_malloc (sizeof (struct State));
s->id = ctx->state_id++;
- s->accepting = accepting;
- s->tcnt = 0;
- s->transitions = NULL;
+ s->accepting = 0;
s->marked = 0;
s->name = NULL;
- if (0 == sset->count)
+ if (NULL == nfa_states)
+ {
+ GNUNET_asprintf (&s->name, "s%i", s->id);
+ return s;
+ }
+
+ s->nfa_set = nfa_states;
+
+ if (nfa_states->len < 1)
return s;
- s->name = GNUNET_malloc ( strlen ("{"));
+ // Create a name based on 'sset'
+ s->name = GNUNET_malloc (sizeof (char) * 2);
strcat (s->name, "{");
+ name = NULL;
- for (i=0; i<sset->count; i++)
+ for (i = 0; i < nfa_states->len; i++)
{
- name = GNUNET_malloc (sizeof (char));
- GNUNET_asprintf (&name, "%i,", sset->states[i]->id);
- s->name = GNUNET_realloc (s->name, strlen (s->name) + strlen (name) + 1);
- strcat (s->name, name);
- GNUNET_free (name);
+ cstate = nfa_states->states[i];
+ GNUNET_asprintf (&name, "%i,", cstate->id);
+
+ if (NULL != name)
+ {
+ len = strlen (s->name) + strlen (name) + 1;
+ s->name = GNUNET_realloc (s->name, len);
+ strcat (s->name, name);
+ GNUNET_free (name);
+ name = NULL;
+ }
+
+ // Add a transition for each distinct literal to NULL state
+ for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
+ {
+ if (0 != ctran->literal)
+ {
+ insert = 1;
+
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ if (t->literal == ctran->literal)
+ {
+ insert = 0;
+ break;
+ }
+ }
+
+ if (insert)
+ add_transition (ctx, s, ctran->literal, NULL);
+ }
+ }
+
+ // If the nfa_states contain an accepting state, the new dfa state is also accepting
+ if (cstate->accepting)
+ s->accepting = 1;
}
- s->name[strlen (s->name)-1] = '}';
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA state with name: %s\n", s->name);
+ s->name[strlen (s->name) - 1] = '}';
return s;
}
-struct GNUNET_REGEX_Automaton *
-nfa_create (struct State *start, struct State *end)
+/**
+ * Move from the given state 's' to the next state on
+ * transition 'literal'
+ *
+ * @param s starting state
+ * @param literal edge label to follow
+ *
+ * @return new state or NULL, if transition on literal not possible
+ */
+static struct State *
+dfa_move (struct State *s, const char literal)
+{
+ struct Transition *t;
+ struct State *new_s;
+
+ if (NULL == s)
+ return NULL;
+
+ new_s = NULL;
+
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ if (literal == t->literal)
+ {
+ new_s = t->state;
+ break;
+ }
+ }
+
+ return new_s;
+}
+
+/**
+ * Remove all unreachable states from DFA 'a'. Unreachable states
+ * are those states that are not reachable from the starting state.
+ *
+ * @param a DFA automaton
+ */
+static void
+dfa_remove_unreachable_states (struct GNUNET_REGEX_Automaton *a)
+{
+ struct State *stack[a->state_count];
+ int stack_len;
+ struct State *s;
+ struct Transition *t;
+
+ stack_len = 0;
+
+ // 1. unmark all states
+ for (s = a->states_head; NULL != s; s = s->next)
+ {
+ s->marked = 0;
+ }
+
+ // 2. traverse dfa from start state and mark all visited states
+ stack[stack_len] = a->start;
+ stack_len++;
+ while (stack_len > 0)
+ {
+ s = stack[stack_len - 1];
+ stack_len--;
+ s->marked = 1; // mark s as visited
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ // add next states to stack
+ if (NULL != t->state && 0 == t->state->marked)
+ stack[++stack_len] = t->state;
+ }
+ }
+
+ // 3. delete all states that were not visited
+ for (s = a->states_head; NULL != s; s = s->next)
+ {
+ if (0 == s->marked)
+ automaton_remove_state (a, s);
+ }
+}
+
+/**
+ * Remove all dead states from the DFA 'a'. Dead states are those
+ * states that do not transition to any other state but themselfes.
+ *
+ * @param a DFA automaton
+ */
+static void
+dfa_remove_dead_states (struct GNUNET_REGEX_Automaton *a)
+{
+ struct State *s;
+ struct Transition *t;
+ int dead;
+
+ GNUNET_assert (DFA == a->type);
+
+ for (s = a->states_head; NULL != s; s = s->next)
+ {
+ if (s->accepting)
+ continue;
+
+ dead = 1;
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ if (NULL != t->state && t->state != s)
+ {
+ dead = 0;
+ break;
+ }
+ }
+
+ if (0 == dead)
+ continue;
+
+ // state s is dead, remove it
+ automaton_remove_state (a, s);
+ }
+}
+
+/**
+ * Merge all non distinguishable states in the DFA 'a'
+ *
+ * @param ctx context
+ * @param a DFA automaton
+ */
+static void
+dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx,
+ struct GNUNET_REGEX_Automaton *a)
+{
+ int i;
+ int table[a->state_count][a->state_count];
+ struct State *s1;
+ struct State *s2;
+ struct Transition *t1;
+ struct Transition *t2;
+ int change;
+
+ change = 1;
+ for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1;
+ i++, s1 = s1->next)
+ s1->marked = i;
+
+ // Mark all pairs of accepting/!accepting states
+ for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+ {
+ for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+ {
+ if ((s1->accepting && !s2->accepting) ||
+ (!s1->accepting && s2->accepting))
+ {
+ table[s1->marked][s2->marked] = 1;
+ }
+ else
+ table[s1->marked][s2->marked] = 0;
+ }
+ }
+
+ while (0 != change)
+ {
+ change = 0;
+ for (s1 = a->states_head; NULL != s1; s1 = s1->next)
+ {
+ for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
+ {
+ if (0 != table[s1->marked][s2->marked])
+ continue;
+
+ for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
+ {
+ for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
+ {
+ if (t1->literal == t2->literal && t1->state == t2->state &&
+ (0 != table[t1->state->marked][t2->state->marked] ||
+ 0 != table[t2->state->marked][t1->state->marked]))
+ {
+ table[s1->marked][s2->marked] = t1->literal;
+ change = 1;
+ }
+ else if (t1->literal != t2->literal && t1->state != t2->state)
+ {
+ table[s1->marked][s2->marked] = -1;
+ change = 1;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ struct State *s2_next;
+
+ for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
+ {
+ for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
+ {
+ s2_next = s2->next;
+ if (s1 != s2 && table[s1->marked][s2->marked] == 0)
+ automaton_merge_states (ctx, a, s1, s2);
+ }
+ }
+}
+
+/**
+ * Minimize the given DFA 'a' by removing all unreachable states,
+ * removing all dead states and merging all non distinguishable states
+ *
+ * @param ctx context
+ * @param a DFA automaton
+ */
+static void
+dfa_minimize (struct GNUNET_REGEX_Context *ctx,
+ struct GNUNET_REGEX_Automaton *a)
+{
+ if (NULL == a)
+ return;
+
+ GNUNET_assert (DFA == a->type);
+
+ // 1. remove unreachable states
+ dfa_remove_unreachable_states (a);
+
+ // 2. remove dead states
+ dfa_remove_dead_states (a);
+
+ // 3. Merge nondistinguishable states
+ dfa_merge_nondistinguishable_states (ctx, a);
+}
+
+/**
+ * Creates a new NFA fragment. Needs to be cleared using automaton_fragment_clear.
+ *
+ * @param start starting state
+ * @param end end state
+ *
+ * @return new NFA fragment
+ */
+static struct GNUNET_REGEX_Automaton *
+nfa_fragment_create (struct State *start, struct State *end)
{
struct GNUNET_REGEX_Automaton *n;
- struct StateSet sset;
n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
- if (NULL == start && NULL == end)
- {
- sset.states = NULL;
- sset.count = 0;
- n->sset = sset;
- n->start = NULL;
- n->end = NULL;
+ n->type = NFA;
+ n->start = NULL;
+ n->end = NULL;
+ if (NULL == start && NULL == end)
return n;
- }
- sset.states = GNUNET_malloc ((sizeof (struct State *)) * 2);
- sset.states[0] = start;
- sset.states[1] = end;
- sset.count = 2;
- n->sset = sset;
+ automaton_add_state (n, end);
+ automaton_add_state (n, start);
n->start = start;
n->end = end;
return n;
}
+/**
+ * Adds a list of states to the given automaton 'n'.
+ *
+ * @param n automaton to which the states should be added
+ * @param states_head head of the DLL of states
+ * @param states_tail tail of the DLL of states
+ */
+static void
+nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct State *states_head,
+ struct State *states_tail)
+{
+ struct State *s;
+
+ if (NULL == n || NULL == states_head)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
+ return;
+ }
-void
-nfa_add_states (struct GNUNET_REGEX_Automaton *n, struct StateSet *sset)
-{
- unsigned int i;
- unsigned int j;
+ if (NULL == n->states_head)
+ {
+ n->states_head = states_head;
+ n->states_tail = states_tail;
+ return;
+ }
- i = n->sset.count;
- GNUNET_array_grow (n->sset.states, n->sset.count, n->sset.count + sset->count);
- for (j = 0; i < n->sset.count && j < sset->count; i++, j++)
+ if (NULL != states_head)
{
- n->sset.states[i] = sset->states[j];
+ n->states_tail->next = states_head;
+ n->states_tail = states_tail;
}
-}
+ for (s = states_head; NULL != s; s = s->next)
+ n->state_count++;
+}
-struct State *
-nfa_create_state (struct GNUNET_REGEX_Context *ctx, int accepting)
+/**
+ * Creates a new NFA state. Needs to be freed using automaton_destroy_state.
+ *
+ * @param ctx context
+ * @param accepting is it an accepting state or not
+ *
+ * @return new NFA state
+ */
+static struct State *
+nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
{
struct State *s;
s = GNUNET_malloc (sizeof (struct State));
s->id = ctx->state_id++;
s->accepting = accepting;
- s->tcnt = 0;
- s->transitions = NULL;
s->marked = 0;
s->name = NULL;
GNUNET_asprintf (&s->name, "s%i", s->id);
return s;
}
-void
-automaton_destroy_state (struct State *s)
+/**
+ * Calculates the NFA closure set for the given state.
+ *
+ * @param s starting point state
+ * @param literal transitioning literal on which to base the closure on,
+ * pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
+ */
+static struct StateSet *
+nfa_closure_create (struct State *s, const char literal)
{
- if (s->tcnt > 0)
- GNUNET_free (s->transitions);
- if (NULL != s->name)
- GNUNET_free (s->name);
- GNUNET_free (s);
-}
+ struct StateSet *cls;
+ struct StateSet *cls_check;
+ struct State *clsstate;
+ struct State *currentstate;
+ struct Transition *ctran;
-void
-nfa_add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state, const char literal,
- struct State *to_state)
-{
- struct Transition t;
+ if (NULL == s)
+ return NULL;
+
+ cls = GNUNET_malloc (sizeof (struct StateSet));
+ cls_check = GNUNET_malloc (sizeof (struct StateSet));
+
+ // Add start state to closure only for epsilon closure
+ if (0 == literal)
+ GNUNET_array_append (cls->states, cls->len, s);
- if (NULL == from_state || NULL == to_state)
+ GNUNET_array_append (cls_check->states, cls_check->len, s);
+ while (cls_check->len > 0)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
- return;
- }
+ currentstate = cls_check->states[cls_check->len - 1];
+ GNUNET_array_grow (cls_check->states, cls_check->len, cls_check->len - 1);
+
+ for (ctran = currentstate->transitions_head; NULL != ctran;
+ ctran = ctran->next)
+ {
+ if (NULL != ctran->state && literal == ctran->literal)
+ {
+ clsstate = ctran->state;
- t.id = ctx->transition_id++;
- t.literal = literal;
- t.state = to_state;
+ if (NULL != clsstate &&
+ GNUNET_YES != state_set_contains (cls, clsstate))
+ {
+ GNUNET_array_append (cls->states, cls->len, clsstate);
+ GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
+ }
+ }
+ }
+ }
+ GNUNET_assert (0 == cls_check->len);
+ GNUNET_free (cls_check);
- if (0 == from_state->tcnt)
- from_state->transitions = NULL;
+ if (cls->len > 1)
+ qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
- GNUNET_array_append (from_state->transitions, from_state->tcnt, t);
+ return cls;
}
-void
-mark_all_states (struct GNUNET_REGEX_Automaton *n, int marked)
+/**
+ * Calculates the closure set for the given set of states.
+ *
+ * @param states list of states on which to base the closure on
+ * @param literal transitioning literal for which to base the closure on,
+ * pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'literal' (epsilon closure if 'literal' is 0)
+ */
+static struct StateSet *
+nfa_closure_set_create (struct StateSet *states, const char literal)
{
+ struct State *s;
+ struct StateSet *sset;
+ struct StateSet *cls;
int i;
+ int j;
+ int k;
+ int contains;
+
+ if (NULL == states)
+ return NULL;
+
+ cls = GNUNET_malloc (sizeof (struct StateSet));
- for (i = 0; i < n->sset.count; i++)
+ for (i = 0; i < states->len; i++)
{
- n->sset.states[i]->marked = marked;
+ s = states->states[i];
+ sset = nfa_closure_create (s, literal);
+
+ for (j = 0; j < sset->len; j++)
+ {
+ contains = 0;
+ for (k = 0; k < cls->len; k++)
+ {
+ if (sset->states[j]->id == cls->states[k]->id)
+ {
+ contains = 1;
+ break;
+ }
+ }
+ if (!contains)
+ GNUNET_array_append (cls->states, cls->len, sset->states[j]);
+ }
+ state_set_clear (sset);
}
+
+ if (cls->len > 1)
+ qsort (cls->states, cls->len, sizeof (struct State *), state_compare);
+
+ return cls;
}
-void
+/**
+ * Pops two NFA fragments (a, b) from the stack and concatenates them (ab)
+ *
+ * @param ctx context
+ */
+static void
nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
{
- struct GNUNET_REGEX_Automaton *A;
- struct GNUNET_REGEX_Automaton *B;
+ struct GNUNET_REGEX_Automaton *a;
+ struct GNUNET_REGEX_Automaton *b;
struct GNUNET_REGEX_Automaton *new;
- B = pop (&nfa_stack);
- A = pop (&nfa_stack);
+ b = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- if (NULL == A || NULL == B)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "nfa_add_concatenationenation failed, because there were not enough elements on the stack");
- return;
- }
+ add_transition (ctx, a->end, 0, b->start);
+ a->end->accepting = 0;
+ b->end->accepting = 1;
- nfa_add_transition (ctx, A->end, 0, B->start);
- A->end->accepting = 0;
- B->end->accepting = 1;
+ new = nfa_fragment_create (NULL, NULL);
+ nfa_add_states (new, a->states_head, a->states_tail);
+ nfa_add_states (new, b->states_head, b->states_tail);
+ new->start = a->start;
+ new->end = b->end;
+ automaton_fragment_clear (a);
+ automaton_fragment_clear (b);
- new = nfa_create (NULL, NULL);
- nfa_add_states (new, &A->sset);
- nfa_add_states (new, &B->sset);
- new->start = A->start;
- new->end = B->end;
- GNUNET_free (A);
- GNUNET_free (B);
-
- push (new, &nfa_stack);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
}
-void
+/**
+ * Pops a NFA fragment from the stack (a) and adds a new fragment (a*)
+ *
+ * @param ctx context
+ */
+static void
nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
{
- struct GNUNET_REGEX_Automaton *A;
+ struct GNUNET_REGEX_Automaton *a;
struct GNUNET_REGEX_Automaton *new;
struct State *start;
struct State *end;
- A = pop (&nfa_stack);
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- if (NULL == A)
+ if (NULL == a)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"nfa_add_star_op failed, because there was no element on the stack");
return;
}
- start = nfa_create_state (ctx, 0);
- end = nfa_create_state (ctx, 1);
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
- nfa_add_transition (ctx, start, 0, A->start);
- nfa_add_transition (ctx, start, 0, end);
- nfa_add_transition (ctx, A->end, 0, A->start);
- nfa_add_transition (ctx, A->end, 0, end);
+ add_transition (ctx, start, 0, a->start);
+ add_transition (ctx, start, 0, end);
+ add_transition (ctx, a->end, 0, a->start);
+ add_transition (ctx, a->end, 0, end);
- A->end->accepting = 0;
+ a->end->accepting = 0;
end->accepting = 1;
- new = nfa_create (start, end);
- nfa_add_states (new, &A->sset);
- GNUNET_free (A);
+ new = nfa_fragment_create (start, end);
+ nfa_add_states (new, a->states_head, a->states_tail);
+ automaton_fragment_clear (a);
- push (new, &nfa_stack);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
}
-void
+/**
+ * Pops an NFA fragment (a) from the stack and adds a new fragment (a+)
+ *
+ * @param ctx context
+ */
+static void
nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
{
- struct GNUNET_REGEX_Automaton *A;
+ struct GNUNET_REGEX_Automaton *a;
- A = pop (&nfa_stack);
-
- if (NULL == A)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "nfa_add_plus_op failed, because there was no element on the stack");
- return;
- }
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- nfa_add_transition (ctx, A->end, 0, A->start);
+ add_transition (ctx, a->end, 0, a->start);
- push (A, &nfa_stack);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
}
-void
-nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
+/**
+ * Pops an NFA fragment (a) from the stack and adds a new fragment (a?)
+ *
+ * @param ctx context
+ */
+static void
+nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
{
- struct GNUNET_REGEX_Automaton *A;
- struct GNUNET_REGEX_Automaton *B;
+ struct GNUNET_REGEX_Automaton *a;
struct GNUNET_REGEX_Automaton *new;
struct State *start;
struct State *end;
- B = pop (&nfa_stack);
- A = pop (&nfa_stack);
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- if (NULL == A || NULL == B)
+ if (NULL == a)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "alternation failed, because there were not enough elements on the stack");
+ "nfa_add_question_op failed, because there was no element on the stack");
return;
}
- start = nfa_create_state (ctx, 0);
- end = nfa_create_state (ctx, 1);
- nfa_add_transition (ctx, start, 0, A->start);
- nfa_add_transition (ctx, start, 0, B->start);
-
- nfa_add_transition (ctx, A->end, 0, end);
- nfa_add_transition (ctx, B->end, 0, end);
-
- A->end->accepting = 0;
- B->end->accepting = 0;
- end->accepting = 1;
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
- new = nfa_create (start, end);
- nfa_add_states (new, &A->sset);
- nfa_add_states (new, &B->sset);
- GNUNET_free (A);
- GNUNET_free (B);
+ add_transition (ctx, start, 0, a->start);
+ add_transition (ctx, start, 0, end);
+ add_transition (ctx, a->end, 0, end);
- push (new, &nfa_stack);
-}
+ a->end->accepting = 0;
-void
-nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
-{
- struct GNUNET_REGEX_Automaton *n;
- struct State *start;
- struct State *end;
+ new = nfa_fragment_create (start, end);
+ nfa_add_states (new, a->states_head, a->states_tail);
+ automaton_fragment_clear (a);
- start = nfa_create_state (ctx, 0);
- end = nfa_create_state (ctx, 1);
- nfa_add_transition (ctx, start, lit, end);
- n = nfa_create (start, end);
- push (n, &nfa_stack);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
}
/**
- * Calculates the closure set for the given set of states.
+ * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment
+ * that alternates between a and b (a|b)
*
- * @param states set of states for which to calculate the closure
- * @param count number of states in 'states'
- * @param literal for the transition
- *
- * @return set of states that can be reached from the given 'states' when
- * using only 'literal' transitions
+ * @param ctx context
*/
-struct StateSet
-nfa_closure (struct State **states, unsigned int count, const char literal)
+static void
+nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
{
- struct Stack *cls_check;
- unsigned int scnt;
- unsigned int tcnt;
- struct StateSet cls;
- struct State *s;
- struct State *currentstate;
- struct State *clsstate;
+ struct GNUNET_REGEX_Automaton *a;
+ struct GNUNET_REGEX_Automaton *b;
+ struct GNUNET_REGEX_Automaton *new;
+ struct State *start;
+ struct State *end;
+ b = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- for (scnt=0; scnt < count; scnt++)
- {
- s = states[scnt];
- cls_check = NULL;
- cls.states = NULL;
- cls.count = 0;
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
+ add_transition (ctx, start, 0, a->start);
+ add_transition (ctx, start, 0, b->start);
- // Add start state to closure
- GNUNET_array_append (cls.states, cls.count, s);
- push (s, &cls_check);
+ add_transition (ctx, a->end, 0, end);
+ add_transition (ctx, b->end, 0, end);
- while (!empty(&cls_check))
- {
- currentstate = pop(&cls_check);
+ a->end->accepting = 0;
+ b->end->accepting = 0;
+ end->accepting = 1;
- for (tcnt=0; tcnt<currentstate->tcnt; tcnt++)
- {
- if (NULL != currentstate->transitions[tcnt].state
- && literal == currentstate->transitions[tcnt].literal)
- {
- clsstate = currentstate->transitions[tcnt].state;
+ new = nfa_fragment_create (start, end);
+ nfa_add_states (new, a->states_head, a->states_tail);
+ nfa_add_states (new, b->states_head, b->states_tail);
+ automaton_fragment_clear (a);
+ automaton_fragment_clear (b);
- if (NULL == clsstate)
- break;
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+}
- GNUNET_array_append (cls.states, cls.count, clsstate);
- push (clsstate, &cls_check);
- }
- }
- }
- }
+/**
+ * Adds a new nfa fragment to the stack
+ *
+ * @param ctx context
+ * @param lit literal for nfa transition
+ */
+static void
+nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
+{
+ struct GNUNET_REGEX_Automaton *n;
+ struct State *start;
+ struct State *end;
- return cls;
+ GNUNET_assert (NULL != ctx);
+
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
+ add_transition (ctx, start, lit, end);
+ n = nfa_fragment_create (start, end);
+ GNUNET_assert (NULL != n);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n);
}
+/**
+ * Construct an NFA by parsing the regex string of length 'len'.
+ *
+ * @param regex regular expression string
+ * @param len length of the string
+ *
+ * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
+ */
struct GNUNET_REGEX_Automaton *
-GNUNET_REGEX_construct_nfa (const char *regex, size_t len)
+GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
{
struct GNUNET_REGEX_Context ctx;
struct GNUNET_REGEX_Automaton *nfa;
+ const char *regexp;
char *error_msg;
unsigned int count;
unsigned int altcount;
{
int altcount;
int atomcount;
- }*p;
+ } *p;
+ GNUNET_REGEX_context_init (&ctx);
+
+ regexp = regex;
p = NULL;
error_msg = NULL;
altcount = 0;
atomcount = 0;
pcount = 0;
- ctx.state_id = 0;
- ctx.transition_id = 0;
- for (count = 0; count < len && *regex; count++, regex++)
+ for (count = 0; count < len && *regexp; count++, regexp++)
{
- switch (*regex)
+ switch (*regexp)
{
case '(':
if (atomcount > 1)
}
nfa_add_plus_op (&ctx);
break;
+ case '?':
+ if (atomcount == 0)
+ {
+ error_msg = "Cannot append '?' to nothing";
+ goto error;
+ }
+ nfa_add_question_op (&ctx);
+ break;
case 92: /* escape: \ */
- regex++;
+ regexp++;
count++;
default:
if (atomcount > 1)
--atomcount;
nfa_add_concatenation (&ctx);
}
- nfa_add_literal (&ctx, *regex);
+ nfa_add_literal (&ctx, *regexp);
atomcount++;
break;
}
if (NULL != p)
GNUNET_free (p);
- nfa = pop (&nfa_stack);
+ nfa = ctx.stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
- if (!empty (&nfa_stack))
+
+ if (NULL != ctx.stack_head)
{
error_msg = "Creating the NFA failed. NFA stack was not empty!";
goto error;
}
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Created NFA with %i States and a total of %i Transitions\n",
- ctx.state_id, ctx.transition_id);
-
return nfa;
error:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
if (NULL != error_msg)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
- GNUNET_free (p);
- while (!empty (&nfa_stack))
- GNUNET_REGEX_destroy_automaton (pop (&nfa_stack));
+ if (NULL != p)
+ GNUNET_free (p);
+ while (NULL != ctx.stack_tail)
+ {
+ GNUNET_REGEX_automaton_destroy (ctx.stack_tail);
+ GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail,
+ ctx.stack_tail);
+ }
return NULL;
}
+/**
+ * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
+ * data structure.
+ *
+ * @param a automaton to be destroyed
+ */
void
-GNUNET_REGEX_destroy_automaton (struct GNUNET_REGEX_Automaton *a)
+GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
{
- int i;
+ struct State *s;
+ struct State *next_state;
if (NULL == a)
return;
- for (i = 0; i < a->sset.count; i++)
+ for (s = a->states_head; NULL != s;)
{
- automaton_destroy_state (a->sset.states[i]);
+ next_state = s->next;
+ automaton_destroy_state (s);
+ s = next_state;
}
- if (NULL != a->sset.states)
- GNUNET_free (a->sset.states);
GNUNET_free (a);
}
+/**
+ * Construct DFA for the given 'regex' of length 'len'
+ *
+ * @param regex regular expression string
+ * @param len length of the regular expression
+ *
+ * @return DFA, needs to be freed using GNUNET_REGEX_destroy_automaton
+ */
+struct GNUNET_REGEX_Automaton *
+GNUNET_REGEX_construct_dfa (const char *regex, const size_t len)
+{
+ struct GNUNET_REGEX_Context ctx;
+ struct GNUNET_REGEX_Automaton *dfa;
+ struct GNUNET_REGEX_Automaton *nfa;
+ struct StateSet *tmp;
+ struct StateSet *nfa_set;
+ struct StateSet *dfa_stack;
+ struct Transition *ctran;
+ struct State *dfa_state;
+ struct State *new_dfa_state;
+ struct State *state_contains;
+ struct State *state_iter;
+
+ GNUNET_REGEX_context_init (&ctx);
+
+ // Create NFA
+ nfa = GNUNET_REGEX_construct_nfa (regex, len);
+
+ if (NULL == nfa)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Could not create DFA, because NFA creation failed\n");
+ return NULL;
+ }
+
+ dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
+ dfa->type = DFA;
+
+ // Create DFA start state from epsilon closure
+ dfa_stack = GNUNET_malloc (sizeof (struct StateSet));
+ nfa_set = nfa_closure_create (nfa->start, 0);
+ dfa->start = dfa_state_create (&ctx, nfa_set);
+ automaton_add_state (dfa, dfa->start);
+ GNUNET_array_append (dfa_stack->states, dfa_stack->len, dfa->start);
+
+ // Create dfa states by combining nfa states
+ while (dfa_stack->len > 0)
+ {
+ dfa_state = dfa_stack->states[dfa_stack->len - 1];
+ GNUNET_array_grow (dfa_stack->states, dfa_stack->len, dfa_stack->len - 1);
+
+ for (ctran = dfa_state->transitions_head; NULL != ctran;
+ ctran = ctran->next)
+ {
+ if (0 != ctran->literal && NULL == ctran->state)
+ {
+ tmp = nfa_closure_set_create (dfa_state->nfa_set, ctran->literal);
+ nfa_set = nfa_closure_set_create (tmp, 0);
+ state_set_clear (tmp);
+ new_dfa_state = dfa_state_create (&ctx, nfa_set);
+ state_contains = NULL;
+ for (state_iter = dfa->states_head; NULL != state_iter;
+ state_iter = state_iter->next)
+ {
+ if (0 ==
+ state_set_compare (state_iter->nfa_set, new_dfa_state->nfa_set))
+ state_contains = state_iter;
+ }
+
+ if (NULL == state_contains)
+ {
+ automaton_add_state (dfa, new_dfa_state);
+ GNUNET_array_append (dfa_stack->states, dfa_stack->len,
+ new_dfa_state);
+ ctran->state = new_dfa_state;
+ }
+ else
+ {
+ ctran->state = state_contains;
+ automaton_destroy_state (new_dfa_state);
+ }
+ }
+ }
+ }
+
+ GNUNET_free (dfa_stack);
+ GNUNET_REGEX_automaton_destroy (nfa);
+
+ dfa_minimize (&ctx, dfa);
+
+ return dfa;
+}
+
+/**
+ * Save the given automaton as a GraphViz dot file
+ *
+ * @param a the automaton to be saved
+ * @param filename where to save the file
+ */
void
-GNUNET_REGEX_save_nfa_graph (struct GNUNET_REGEX_Automaton *n, const char *filename)
+GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a,
+ const char *filename)
{
struct State *s;
+ struct Transition *ctran;
+ char *s_acc = NULL;
+ char *s_tran = NULL;
char *start;
char *end;
FILE *p;
- int scnt;
- int tcnt;
- if (NULL == n)
+ if (NULL == a)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
return;
start = "digraph G {\nrankdir=LR\n";
fwrite (start, strlen (start), 1, p);
- for (scnt = 0; scnt < n->sset.count; scnt++)
+ for (s = a->states_head; NULL != s; s = s->next)
{
- struct Transition *ctran;
- char *s_acc = NULL;
- char *s_tran = NULL;
-
- s = n->sset.states[scnt];
-
if (s->accepting)
{
- GNUNET_asprintf (&s_acc, "s%i [shape=doublecircle];\n", s->id);
+ GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
fwrite (s_acc, strlen (s_acc), 1, p);
GNUNET_free (s_acc);
}
- ctran = s->transitions;
s->marked = 1;
- for (tcnt = 0; tcnt < s->tcnt && NULL != ctran; tcnt++)
+ for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
{
if (NULL == ctran->state)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Transition from State %i has has no state for transitioning\n",
s->id);
+ continue;
}
if (ctran->literal == 0)
{
- GNUNET_asprintf (&s_tran, "s%i -> s%i [label = \"epsilon\"];\n", s->id,
- ctran->state->id);
+ GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
+ s->name, ctran->state->name);
}
else
{
- GNUNET_asprintf (&s_tran, "s%i -> s%i [label = \"%c\"];\n", s->id,
- ctran->state->id, ctran->literal);
+ GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
+ s->name, ctran->state->name, ctran->literal);
}
fwrite (s_tran, strlen (s_tran), 1, p);
-
- ctran++;
+ GNUNET_free (s_tran);
}
}
fclose (p);
}
-struct GNUNET_REGEX_Automaton *
-GNUNET_REGEX_construct_dfa (const char *regex, size_t len)
+/**
+ * Evaluates the given string using the given DFA automaton
+ *
+ * @param a automaton, type must be DFA
+ * @param string string that should be evaluated
+ *
+ * @return 0 if string matches, non 0 otherwise
+ */
+static int
+evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
{
- struct GNUNET_REGEX_Context ctx;
- struct GNUNET_REGEX_Automaton *dfa;
- struct GNUNET_REGEX_Automaton *nfa;
- struct StateSet dfa_start_set;
- struct State *dfa_start;
+ const char *strp;
+ struct State *s;
- ctx.state_id = 0;
- ctx.transition_id = 0;
+ if (DFA != a->type)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Tried to evaluate DFA, but NFA automaton given");
+ return -1;
+ }
- // Create NFA
- nfa = GNUNET_REGEX_construct_nfa (regex, len);
+ s = a->start;
- // Create DFA start state from epsilon closure
- dfa_start_set = nfa_closure (&nfa->start, 1, 0);
- dfa_start = dfa_create_state (&ctx, &dfa_start_set, 0);
+ for (strp = string; NULL != strp && *strp; strp++)
+ {
+ s = dfa_move (s, *strp);
+ if (NULL == s)
+ break;
+ }
- // ecls (move (dfa_start, lit))
+ if (NULL != s && s->accepting)
+ return 0;
- return dfa;
+ return 1;
+}
+
+/**
+ * Evaluates the given string using the given NFA automaton
+ *
+ * @param a automaton, type must be NFA
+ * @param string string that should be evaluated
+ *
+ * @return 0 if string matches, non 0 otherwise
+ */
+static int
+evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string)
+{
+ const char *strp;
+ struct State *s;
+ struct StateSet *sset;
+ struct StateSet *new_sset;
+ int i;
+ int result;
+
+ if (NFA != a->type)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Tried to evaluate NFA, but DFA automaton given");
+ return -1;
+ }
+
+ result = 1;
+ strp = string;
+ sset = nfa_closure_create (a->start, 0);
+
+ for (strp = string; NULL != strp && *strp; strp++)
+ {
+ new_sset = nfa_closure_set_create (sset, *strp);
+ state_set_clear (sset);
+ sset = nfa_closure_set_create (new_sset, 0);
+ state_set_clear (new_sset);
+ }
+
+ for (i = 0; i < sset->len; i++)
+ {
+ s = sset->states[i];
+ if (NULL != s && s->accepting)
+ {
+ result = 0;
+ break;
+ }
+ }
+
+ state_set_clear (sset);
+ return result;
+}
+
+/**
+ * Evaluates the given 'string' against the given compiled regex
+ *
+ * @param a automaton
+ * @param string string to check
+ *
+ * @return 0 if string matches, non 0 otherwise
+ */
+int
+GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string)
+{
+ int result;
+
+ switch (a->type)
+ {
+ case DFA:
+ result = evaluate_dfa (a, string);
+ break;
+ case NFA:
+ result = evaluate_nfa (a, string);
+ break;
+ default:
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Evaluating regex failed, automaton has no type!\n");
+ result = GNUNET_SYSERR;
+ break;
+ }
+
+ return result;
}