diff options
author | Bertrand Marc <beberking@gmail.com> | 2012-06-06 20:47:48 +0200 |
---|---|---|
committer | Bertrand Marc <beberking@gmail.com> | 2012-06-06 20:47:48 +0200 |
commit | 740b30688bd745a527f96f9116c19acb3480971a (patch) | |
tree | 2709a3f4dba11c174aa9e1ba3612e30c578e76a9 /src/regex/regex.c | |
parent | 2b81464a43485fcc8ce079fafdee7b7a171835f4 (diff) |
Imported Upstream version 0.9.3upstream/0.9.3
Diffstat (limited to 'src/regex/regex.c')
-rw-r--r-- | src/regex/regex.c | 2252 |
1 files changed, 2252 insertions, 0 deletions
diff --git a/src/regex/regex.c b/src/regex/regex.c new file mode 100644 index 0000000..5244c26 --- /dev/null +++ b/src/regex/regex.c @@ -0,0 +1,2252 @@ +/* + This file is part of GNUnet + (C) 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/** + * @file src/regex/regex.c + * @brief library to create automatons from regular expressions + * @author Maximilian Szengel + */ +#include "platform.h" +#include "gnunet_container_lib.h" +#include "gnunet_crypto_lib.h" +#include "gnunet_regex_lib.h" +#include "regex.h" + +#define initial_bits 10 + +/** + * 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; + + /** + * Unique SCC (Strongly Connected Component) id. + */ + unsigned int scc_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 +{ + NFA, + DFA +}; + +/** + * 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 GNUNET_REGEX_State *start; + + /** + * End state of the automaton. + */ + struct GNUNET_REGEX_State *end; + + /** + * Number of states in the automaton. + */ + unsigned int state_count; + + /** + * DLL of states. + */ + struct GNUNET_REGEX_State *states_head; + + /** + * DLL of states + */ + struct GNUNET_REGEX_State *states_tail; + + /** + * Type of the automaton. + */ + enum GNUNET_REGEX_automaton_type type; +}; + +/** + * A state. Can be used in DFA and NFA automatons. + */ +struct GNUNET_REGEX_State +{ + /** + * This is a linked list. + */ + struct GNUNET_REGEX_State *prev; + + /** + * This is a linked list. + */ + struct GNUNET_REGEX_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; + + /** + * Marking the state as contained. This is used for checking, if the state is + * contained in a set in constant time + */ + int contained; + + /** + * Marking the state as part of an SCC (Strongly Connected Component). All + * states with the same scc_id are part of the same SCC. scc_id is 0, if state + * is not a part of any SCC. + */ + unsigned int scc_id; + + /** + * Used for SCC detection. + */ + int index; + + /** + * Used for SCC detection. + */ + int lowlink; + + /** + * Human readable name of the automaton. Used for debugging and graph + * creation. + */ + char *name; + + /** + * Hash of the state. + */ + GNUNET_HashCode hash; + + /** + * Proof for this state. + */ + char *proof; + + /** + * 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 GNUNET_REGEX_StateSet *nfa_set; +}; + +/** + * Transition between two states. Each state can have 0-n transitions. If label + * 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; + + /** + * Label for this transition. This is basically the edge label for the graph. + */ + char label; + + /** + * State to which this transition leads. + */ + struct GNUNET_REGEX_State *to_state; + + /** + * State from which this transition origins. + */ + struct GNUNET_REGEX_State *from_state; + + /** + * Mark this transition. For example when reversing the automaton. + */ + int mark; +}; + +/** + * Set of states. + */ +struct GNUNET_REGEX_StateSet +{ + /** + * Array of states. + */ + struct GNUNET_REGEX_State **states; + + /** + * Length of the 'states' array. + */ + unsigned int len; +}; + +/* + * Debug helper functions + */ +void +debug_print_transitions (struct GNUNET_REGEX_State *); + +void +debug_print_state (struct GNUNET_REGEX_State *s) +{ + char *proof; + + if (NULL == s->proof) + proof = "NULL"; + else + proof = s->proof; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n", + s->id, s->name, s->marked, s->accepting, s->scc_id, + s->transition_count, proof); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n"); + debug_print_transitions (s); +} + +void +debug_print_states (struct GNUNET_REGEX_Automaton *a) +{ + struct GNUNET_REGEX_State *s; + + for (s = a->states_head; NULL != s; s = s->next) + debug_print_state (s); +} + +void +debug_print_transition (struct Transition *t) +{ + char *to_state; + char *from_state; + char label; + + if (NULL == t) + return; + + if (0 == t->label) + label = '0'; + else + label = t->label; + + if (NULL == t->to_state) + to_state = "NULL"; + else + to_state = t->to_state->name; + + if (NULL == t->from_state) + from_state = "NULL"; + else + from_state = t->from_state->name; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %c to %s\n", + t->id, from_state, label, to_state); +} + +void +debug_print_transitions (struct GNUNET_REGEX_State *s) +{ + struct Transition *t; + + for (t = s->transitions_head; NULL != t; t = t->next) + debug_print_transition (t); +} + +/** + * Recursive function doing DFS with 'v' as a start, detecting all SCCs inside + * the subgraph reachable from 'v'. Used with scc_tarjan function to detect all + * SCCs inside an automaton. + * + * @param ctx context + * @param v start vertex + * @param index current index + * @param stack stack for saving all SCCs + * @param stack_size current size of the stack + */ +static void +scc_tarjan_strongconnect (struct GNUNET_REGEX_Context *ctx, + struct GNUNET_REGEX_State *v, int *index, + struct GNUNET_REGEX_State **stack, + unsigned int *stack_size) +{ + struct GNUNET_REGEX_State *w; + struct Transition *t; + + v->index = *index; + v->lowlink = *index; + (*index)++; + stack[(*stack_size)++] = v; + v->contained = 1; + + for (t = v->transitions_head; NULL != t; t = t->next) + { + w = t->to_state; + if (NULL != w && w->index < 0) + { + scc_tarjan_strongconnect (ctx, w, index, stack, stack_size); + v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink; + } + else if (0 != w->contained) + v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink; + } + + if (v->lowlink == v->index) + { + w = stack[--(*stack_size)]; + w->contained = 0; + + if (v != w) + { + ctx->scc_id++; + while (v != w) + { + w->scc_id = ctx->scc_id; + w = stack[--(*stack_size)]; + w->contained = 0; + } + w->scc_id = ctx->scc_id; + } + } +} + +/** + * Detect all SCCs (Strongly Connected Components) inside the given automaton. + * SCCs will be marked using the scc_id on each state. + * + * @param ctx context + * @param a automaton + */ +static void +scc_tarjan (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_Automaton *a) +{ + int index; + struct GNUNET_REGEX_State *v; + struct GNUNET_REGEX_State *stack[a->state_count]; + unsigned int stack_size; + + for (v = a->states_head; NULL != v; v = v->next) + { + v->contained = 0; + v->index = -1; + v->lowlink = -1; + } + + stack_size = 0; + index = 0; + + for (v = a->states_head; NULL != v; v = v->next) + { + if (v->index < 0) + scc_tarjan_strongconnect (ctx, v, &index, stack, &stack_size); + } +} + +/** + * Adds a transition from one state to another on 'label'. Does not add + * duplicate states. + * + * @param ctx context + * @param from_state starting state for the transition + * @param label transition label + * @param to_state state to where the transition should point to + */ +static void +state_add_transition (struct GNUNET_REGEX_Context *ctx, + struct GNUNET_REGEX_State *from_state, const char label, + struct GNUNET_REGEX_State *to_state) +{ + int is_dup; + struct Transition *t; + + if (NULL == from_state) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n"); + return; + } + + // Do not add duplicate state transitions + is_dup = GNUNET_NO; + for (t = from_state->transitions_head; NULL != t; t = t->next) + { + if (t->to_state == to_state && t->label == label && + t->from_state == from_state) + { + is_dup = GNUNET_YES; + break; + } + } + + if (is_dup) + return; + + t = GNUNET_malloc (sizeof (struct Transition)); + t->id = ctx->transition_id++; + t->label = label; + t->to_state = to_state; + t->from_state = from_state; + + // Add outgoing transition to 'from_state' + from_state->transition_count++; + GNUNET_CONTAINER_DLL_insert (from_state->transitions_head, + from_state->transitions_tail, t); +} + +/** + * 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) +{ + struct GNUNET_REGEX_State **s1; + struct GNUNET_REGEX_State **s2; + + s1 = (struct GNUNET_REGEX_State **) a; + s2 = (struct GNUNET_REGEX_State **) b; + + return (*s1)->id - (*s2)->id; +} + +/** + * Get all edges leaving state 's'. + * + * @param s state. + * @param edges all edges leaving 's'. + * + * @return number of edges. + */ +static unsigned int +state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges) +{ + struct Transition *t; + unsigned int count; + + if (NULL == s) + return 0; + + count = 0; + + for (t = s->transitions_head; NULL != t; t = t->next) + { + if (NULL != t->to_state) + { + edges[count].label = &t->label; + edges[count].destination = t->to_state->hash; + count++; + } + } + return count; +} + +/** + * 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 GNUNET_REGEX_StateSet *sset1, + struct GNUNET_REGEX_StateSet *sset2) +{ + int result; + int i; + + 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; +} + +/** + * Clears the given StateSet 'set' + * + * @param set set to be cleared + */ +static void +state_set_clear (struct GNUNET_REGEX_StateSet *set) +{ + if (NULL != set) + { + if (NULL != set->states) + GNUNET_free (set->states); + GNUNET_free (set); + } +} + +/** + * 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) +{ + if (NULL == a) + return; + + 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 GNUNET_REGEX_State *s) +{ + struct Transition *t; + struct Transition *next_t; + + if (NULL == s) + return; + + if (NULL != s->name) + GNUNET_free (s->name); + + if (NULL != s->proof) + GNUNET_free (s->proof); + + for (t = s->transitions_head; NULL != t; t = next_t) + { + next_t = t->next; + GNUNET_CONTAINER_DLL_remove (s->transitions_head, s->transitions_tail, t); + GNUNET_free (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 GNUNET_REGEX_State *s) +{ + struct GNUNET_REGEX_State *ss; + struct GNUNET_REGEX_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->to_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 GNUNET_REGEX_State *s1, + struct GNUNET_REGEX_State *s2) +{ + struct GNUNET_REGEX_State *s_check; + struct Transition *t_check; + char *new_name; + + GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2); + + if (s1 == s2) + return; + + // 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 (s2 == t_check->to_state) + t_check->to_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) + { + if (t_check->to_state != s1) + state_add_transition (ctx, s1, t_check->label, t_check->to_state); + } + + // 3. Rename s1 to {s1,s2} + new_name = GNUNET_strdup (s1->name); + if (NULL != s1->name) + { + GNUNET_free (s1->name); + s1->name = NULL; + } + GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name); + GNUNET_free (new_name); + + // remove state + GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2); + a->state_count--; + automaton_destroy_state (s2); +} + +/** + * 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 GNUNET_REGEX_State *s) +{ + GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s); + a->state_count++; +} + +/** + * Function that is called with each state, when traversing an automaton. + * + * @param cls closure + * @param s state + */ +typedef void (*GNUNET_REGEX_traverse_action) (void *cls, + struct GNUNET_REGEX_State * s); + +/** + * Traverses all states that are reachable from state 's'. Expects the states to + * be unmarked (s->marked == GNUNET_NO). Performs 'action' on each visited + * state. + * + * @param cls closure. + * @param s start state. + * @param action action to be performed on each state. + */ +static void +automaton_state_traverse (void *cls, struct GNUNET_REGEX_State *s, + GNUNET_REGEX_traverse_action action) +{ + struct Transition *t; + + if (GNUNET_NO == s->marked) + { + s->marked = GNUNET_YES; + + if (action > 0) + action (cls, s); + + for (t = s->transitions_head; NULL != t; t = t->next) + automaton_state_traverse (cls, t->to_state, action); + } +} + +/** + * Traverses the given automaton from it's start state, visiting all reachable + * states and calling 'action' on each one of them. + * + * @param cls closure. + * @param a automaton. + * @param action action to be performed on each state. + */ +static void +automaton_traverse (void *cls, struct GNUNET_REGEX_Automaton *a, + GNUNET_REGEX_traverse_action action) +{ + struct GNUNET_REGEX_State *s; + + for (s = a->states_head; NULL != s; s = s->next) + s->marked = GNUNET_NO; + + automaton_state_traverse (cls, a->start, action); +} + +/** + * Reverses all transitions of the given automaton. + * + * @param a automaton. + */ +static void +automaton_reverse (struct GNUNET_REGEX_Automaton *a) +{ + struct GNUNET_REGEX_State *s; + struct Transition *t; + struct Transition *t_next; + struct GNUNET_REGEX_State *s_swp; + + for (s = a->states_head; NULL != s; s = s->next) + for (t = s->transitions_head; NULL != t; t = t->next) + t->mark = GNUNET_NO; + + for (s = a->states_head; NULL != s; s = s->next) + { + for (t = s->transitions_head; NULL != t; t = t_next) + { + t_next = t->next; + + if (GNUNET_YES == t->mark || t->from_state == t->to_state) + continue; + + t->mark = GNUNET_YES; + + GNUNET_CONTAINER_DLL_remove (t->from_state->transitions_head, + t->from_state->transitions_tail, t); + t->from_state->transition_count--; + GNUNET_CONTAINER_DLL_insert (t->to_state->transitions_head, + t->to_state->transitions_tail, t); + t->to_state->transition_count++; + + s_swp = t->from_state; + t->from_state = t->to_state; + t->to_state = s_swp; + } + } +} + +/** + * Create proof for the given state. + * + * @param cls closure. + * @param s state. + */ +static void +automaton_create_proofs_step (void *cls, struct GNUNET_REGEX_State *s) +{ + struct Transition *t; + int i; + char *tmp; + + for (i = 0, t = s->transitions_head; NULL != t; t = t->next, i++) + { + if (t->to_state == s) + GNUNET_asprintf (&tmp, "%c*", t->label); + else if (i != s->transition_count - 1) + GNUNET_asprintf (&tmp, "%c|", t->label); + else + GNUNET_asprintf (&tmp, "%c", t->label); + + if (NULL != s->proof) + s->proof = + GNUNET_realloc (s->proof, strlen (s->proof) + strlen (tmp) + 1); + else + s->proof = GNUNET_malloc (strlen (tmp) + 1); + strcat (s->proof, tmp); + GNUNET_free (tmp); + } +} + +/** + * Create proofs for all states in the given automaton. + * + * @param a automaton. + */ +static void +automaton_create_proofs (struct GNUNET_REGEX_Automaton *a) +{ + struct GNUNET_REGEX_State *s; + + automaton_reverse (a); + + for (s = a->states_head; NULL != s; s = s->next) + automaton_create_proofs_step (NULL, s); + + automaton_reverse (a); +} + +/** + * 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 GNUNET_REGEX_State * +dfa_state_create (struct GNUNET_REGEX_Context *ctx, + struct GNUNET_REGEX_StateSet *nfa_states) +{ + struct GNUNET_REGEX_State *s; + char *name; + int len = 0; + struct GNUNET_REGEX_State *cstate; + struct Transition *ctran; + int insert = 1; + struct Transition *t; + int i; + + s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State)); + s->id = ctx->state_id++; + s->accepting = 0; + s->marked = 0; + s->name = NULL; + s->scc_id = 0; + s->index = -1; + s->lowlink = -1; + s->contained = 0; + s->proof = NULL; + + 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; + + // Create a name based on 'sset' + s->name = GNUNET_malloc (sizeof (char) * 2); + strcat (s->name, "{"); + name = NULL; + + for (i = 0; i < nfa_states->len; i++) + { + 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 label to NULL state + for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next) + { + if (0 != ctran->label) + { + insert = 1; + + for (t = s->transitions_head; NULL != t; t = t->next) + { + if (t->label == ctran->label) + { + insert = 0; + break; + } + } + + if (insert) + state_add_transition (ctx, s, ctran->label, 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] = '}'; + + return s; +} + +/** + * Move from the given state 's' to the next state on transition 'label' + * + * @param s starting state + * @param label edge label to follow + * + * @return new state or NULL, if transition on label not possible + */ +static struct GNUNET_REGEX_State * +dfa_move (struct GNUNET_REGEX_State *s, const char label) +{ + struct Transition *t; + struct GNUNET_REGEX_State *new_s; + + if (NULL == s) + return NULL; + + new_s = NULL; + + for (t = s->transitions_head; NULL != t; t = t->next) + { + if (label == t->label) + { + new_s = t->to_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 GNUNET_REGEX_State *s; + struct GNUNET_REGEX_State *s_next; + + // 1. unmark all states + for (s = a->states_head; NULL != s; s = s->next) + s->marked = GNUNET_NO; + + // 2. traverse dfa from start state and mark all visited states + automaton_traverse (NULL, a, NULL); + + // 3. delete all states that were not visited + for (s = a->states_head; NULL != s; s = s_next) + { + s_next = s->next; + if (GNUNET_NO == 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 GNUNET_REGEX_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->to_state && t->to_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 GNUNET_REGEX_State *s1; + struct GNUNET_REGEX_State *s2; + struct Transition *t1; + struct Transition *t2; + struct GNUNET_REGEX_State *s1_next; + struct GNUNET_REGEX_State *s2_next; + int change; + int num_equal_edges; + + 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; s2 = s2->next) + { + table[s1->marked][s2->marked] = 0; + + if ((s1->accepting && !s2->accepting) || + (!s1->accepting && s2->accepting)) + { + table[s1->marked][s2->marked] = 1; + } + } + } + + // Find all equal states + change = 1; + 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; + + num_equal_edges = 0; + for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next) + { + for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next) + { + if (t1->label == t2->label) + { + num_equal_edges++; + if (0 != table[t1->to_state->marked][t2->to_state->marked] || + 0 != table[t2->to_state->marked][t1->to_state->marked]) + { + table[s1->marked][s2->marked] = t1->label != 0 ? t1->label : 1; + change = 1; + } + } + } + } + if (num_equal_edges != s1->transition_count || + num_equal_edges != s2->transition_count) + { + // Make sure ALL edges of possible equal states are the same + table[s1->marked][s2->marked] = -2; + } + } + } + } + + // Merge states that are equal + for (s1 = a->states_head; NULL != s1; s1 = s1_next) + { + s1_next = s1->next; + for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next) + { + s2_next = s2->next; + if (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 GNUNET_REGEX_State *start, + struct GNUNET_REGEX_State *end) +{ + struct GNUNET_REGEX_Automaton *n; + + n = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton)); + + n->type = NFA; + n->start = NULL; + n->end = NULL; + + if (NULL == start && NULL == end) + return n; + + 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_state |