*/
#include "platform.h"
#include "gnunet_container_lib.h"
+#include "gnunet_crypto_lib.h"
#include "gnunet_regex_lib.h"
-#include "regex.h"
+#include "regex_internal.h"
+
+/**
+ * Constant for how many bits the initial string regex should have.
+ */
+#define INITIAL_BITS 8
+
+
+/**
+ * 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;
+};
+
+
+/**
+ * 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
+ */
+
+/**
+ * Print all the transitions of state 's'.
+ *
+ * @param s state for which to print it's transitions.
+ */
void
-stack_push (struct GNUNET_CONTAINER_SList *stack, const void *buf, size_t len)
+debug_print_transitions (struct GNUNET_REGEX_State *s);
+
+
+/**
+ * Print information of the given state 's'.
+ *
+ * @param s state for which debug information should be printed.
+ */
+void
+debug_print_state (struct GNUNET_REGEX_State *s)
{
- GNUNET_CONTAINER_slist_add (stack, GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- buf, len);
+ 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);
}
-int
-stack_empty (struct GNUNET_CONTAINER_SList *stack)
+
+/**
+ * Print debug information for all states contained in the automaton 'a'.
+ *
+ * @param a automaton for which debug information of it's states should be printed.
+ */
+void
+debug_print_states (struct GNUNET_REGEX_Automaton *a)
{
- return 0 == GNUNET_CONTAINER_slist_count (stack);
+ struct GNUNET_REGEX_State *s;
+
+ for (s = a->states_head; NULL != s; s = s->next)
+ debug_print_state (s);
}
-void *
-stack_pop (struct GNUNET_CONTAINER_SList *stack, size_t length)
+
+/**
+ * Print debug information for given transition 't'.
+ *
+ * @param t transition for which to print debug info.
+ */
+void
+debug_print_transition (struct GNUNET_REGEX_Transition *t)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
- void *val;
- size_t len;
+ 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);
+}
+
- it = GNUNET_CONTAINER_slist_begin (stack);
- val = GNUNET_CONTAINER_slist_get (&it, &len);
- GNUNET_assert (length == len);
- GNUNET_CONTAINER_slist_erase (&it);
- GNUNET_CONTAINER_slist_iter_destroy (&it);
+void
+debug_print_transitions (struct GNUNET_REGEX_State *s)
+{
+ struct GNUNET_REGEX_Transition *t;
- return val;
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ debug_print_transition (t);
}
-void *
-stack_top (struct GNUNET_CONTAINER_SList *stack, size_t * len)
+
+/**
+ * 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)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
+ int is_dup;
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Transition *oth;
- if (stack_empty (stack))
- return NULL;
+ if (NULL == from_state)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
+ return;
+ }
- return GNUNET_CONTAINER_slist_get (&it, len);
+ // 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 (GNUNET_YES == is_dup)
+ return;
+
+ // sort transitions by label
+ for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
+ {
+ if (oth->label > label)
+ break;
+ }
+
+ t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_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_before (from_state->transitions_head,
+ from_state->transitions_tail, oth, t);
}
-struct State
+
+/**
+ * Remove a 'transition' from 'state'.
+ *
+ * @param state state from which the to-be-removed transition originates.
+ * @param transition transition that should be removed from state 'state'.
+ */
+static void
+state_remove_transition (struct GNUNET_REGEX_State *state,
+ struct GNUNET_REGEX_Transition *transition)
{
- unsigned int id;
- int accepting;
- int marked;
- char *name;
- struct GNUNET_CONTAINER_SList *transitions;
- struct GNUNET_CONTAINER_SList *nfa_set;
-};
+ if (NULL == state || NULL == transition)
+ return;
+
+ if (transition->from_state != state)
+ return;
-struct GNUNET_REGEX_Automaton
+ state->transition_count--;
+ GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
+ transition);
+ GNUNET_free (transition);
+}
+
+
+/**
+ * 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 State *start;
- struct State *end;
- struct GNUNET_CONTAINER_SList *states;
-};
+ struct GNUNET_REGEX_State **s1;
+ struct GNUNET_REGEX_State **s2;
-struct Transition
+ 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', expected to be allocated and have enough
+ * space for s->transitions_count elements.
+ *
+ * @return number of edges.
+ */
+static unsigned int
+state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges)
{
- unsigned int id;
- char literal;
- struct State *state;
-};
+ struct GNUNET_REGEX_Transition *t;
+ unsigned int count;
-struct GNUNET_REGEX_Context
+ 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)
{
- unsigned int state_id;
- unsigned int transition_id;
- struct GNUNET_CONTAINER_SList *stack;
-};
+ int result;
+ unsigned int i;
-void
-GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
+ 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 == ctx)
+ if (NULL != set)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
+ GNUNET_free_non_null (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 GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Transition *next_t;
+
+ if (NULL == s)
return;
+
+ GNUNET_free_non_null (s->name);
+ GNUNET_free_non_null (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);
}
- ctx->state_id = 0;
- ctx->transition_id = 0;
- ctx->stack = GNUNET_CONTAINER_slist_create ();
+
+ state_set_clear (s->nfa_set);
+
+ GNUNET_free (s);
}
-void
-GNUNET_REGEX_context_destroy (struct GNUNET_REGEX_Context *ctx)
+
+/**
+ * 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)
{
- if (NULL != ctx->stack)
- GNUNET_CONTAINER_slist_destroy (ctx->stack);
+ struct GNUNET_REGEX_State *ss;
+ struct GNUNET_REGEX_State *s_check;
+ struct GNUNET_REGEX_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);
}
-void
-debug_print_state (struct State *s)
+
+/**
+ * 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)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "State %i: %s marked: %i accepting: %i\n", s->id, s->name,
- s->marked, s->accepting);
+ struct GNUNET_REGEX_State *s_check;
+ struct GNUNET_REGEX_Transition *t_check;
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Transition *t_next;
+ char *new_name;
+ int is_dup;
+
+ GNUNET_assert (NULL != ctx && NULL != a && NULL != s1 && NULL != s2);
+
+ if (s1 == s2)
+ return;
+
+ // 1. Make all transitions pointing to s2 point to s1, unless this transition
+ // does not already exists, if it already exists remove transition.
+ 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_next)
+ {
+ t_next = t_check->next;
+
+ if (s2 == t_check->to_state)
+ {
+ is_dup = GNUNET_NO;
+ for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
+ {
+ if (t->to_state == s1 && t_check->label == t->label)
+ is_dup = GNUNET_YES;
+ }
+ if (GNUNET_NO == is_dup)
+ t_check->to_state = s1;
+ else
+ state_remove_transition (t_check->from_state, t_check);
+ }
+ }
+ }
+
+ // 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 = s1->name;
+ 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++;
}
+
+/**
+ * Depth-first traversal of 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 s start state.
+ * @param count current count of the state.
+ * @param action action to be performed on each state.
+ * @param action_cls closure for action
+ */
+static void
+automaton_state_traverse (struct GNUNET_REGEX_State *s, unsigned int *count,
+ GNUNET_REGEX_traverse_action action, void *action_cls)
+{
+ struct GNUNET_REGEX_Transition *t;
+
+ if (GNUNET_NO != s->marked)
+ return;
+ s->marked = GNUNET_YES;
+ if (NULL != action)
+ action (action_cls, *count, s);
+ (*count)++;
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ automaton_state_traverse (t->to_state, count, action, action_cls);
+}
+
+
+/**
+ * Traverses the given automaton from it's start state, visiting all reachable
+ * states and calling 'action' on each one of them.
+ *
+ * @param a automaton.
+ * @param action action to be performed on each state.
+ * @param action_cls closure for action
+ */
void
-debug_print_states (struct GNUNET_CONTAINER_SList *states)
+GNUNET_REGEX_automaton_traverse (struct GNUNET_REGEX_Automaton *a,
+ GNUNET_REGEX_traverse_action action,
+ void *action_cls)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
- struct State *s;
+ unsigned int count;
+ struct GNUNET_REGEX_State *s;
+
+ for (s = a->states_head; NULL != s; s = s->next)
+ s->marked = GNUNET_NO;
+ count = 0;
+ automaton_state_traverse (a->start, &count, action, action_cls);
+}
+
- for (it = GNUNET_CONTAINER_slist_begin (states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+/**
+ * Check if the given string 'str' needs parentheses around it when
+ * using it to generate a regex.
+ *
+ * @param str string
+ *
+ * @return GNUNET_YES if parentheses are needed, GNUNET_NO otherwise
+ */
+static int
+needs_parentheses (const char *str)
+{
+ size_t slen;
+ const char *op;
+ const char *cl;
+ const char *pos;
+ unsigned int cnt;
+
+ if ((NULL == str) || ((slen = strlen (str)) < 2))
+ return GNUNET_NO;
+
+ if ('(' != str[0])
+ return GNUNET_YES;
+ cnt = 1;
+ pos = &str[1];
+ while (cnt > 0)
{
- s = GNUNET_CONTAINER_slist_get (&it, NULL);
- debug_print_state (s);
+ cl = strchr (pos, ')');
+ if (NULL == cl)
+ {
+ GNUNET_break (0);
+ return GNUNET_YES;
+ }
+ op = strchr (pos, '(');
+ if ((NULL != op) && (op < cl))
+ {
+ cnt++;
+ pos = op + 1;
+ continue;
+ }
+ /* got ')' first */
+ cnt--;
+ pos = cl + 1;
}
- GNUNET_CONTAINER_slist_iter_destroy (&it);
+ return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
}
-void
-debug_print_transitions (struct State *s)
+
+/**
+ * Remove parentheses surrounding string 'str'.
+ * Example: "(a)" becomes "a", "(a|b)|(a|c)" stays the same.
+ * You need to GNUNET_free the returned string.
+ *
+ * @param str string, free'd or re-used by this function, can be NULL
+ *
+ * @return string without surrounding parentheses, string 'str' if no preceding
+ * epsilon could be found, NULL if 'str' was NULL
+ */
+static char *
+remove_parentheses (char *str)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
- struct Transition *t;
- char *state;
- char literal;
-
- for (it = GNUNET_CONTAINER_slist_begin (s->transitions);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+ size_t slen;
+ const char *pos;
+
+ if ((NULL == str) || ('(' != str[0]) ||
+ (str[(slen = strlen (str)) - 1] != ')'))
+ return str;
+
+ pos = strchr (&str[1], ')');
+ if (pos == &str[slen - 1])
{
- t = GNUNET_CONTAINER_slist_get (&it, NULL);
+ memmove (str, &str[1], slen - 2);
+ str[slen - 2] = '\0';
+ }
+ return str;
+}
- if (0 == t->literal)
- literal = '0';
- else
- literal = t->literal;
- if (NULL == t->state)
- state = "NULL";
- else
- state = t->state->name;
+/**
+ * Check if the string 'str' starts with an epsilon (empty string).
+ * Example: "(|a)" is starting with an epsilon.
+ *
+ * @param str string to test
+ *
+ * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
+ */
+static int
+has_epsilon (const char *str)
+{
+ return (NULL != str) && ('(' == str[0]) && ('|' == str[1]) &&
+ (')' == str[strlen (str) - 1]);
+}
+
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: On %c to %s\n", t->id,
- literal, state);
+/**
+ * Remove an epsilon from the string str. Where epsilon is an empty string
+ * Example: str = "(|a|b|c)", result: "a|b|c"
+ * The returned string needs to be freed.
+ *
+ * @param str string
+ *
+ * @return string without preceding epsilon, string 'str' if no preceding epsilon
+ * could be found, NULL if 'str' was NULL
+ */
+static char *
+remove_epsilon (const char *str)
+{
+ size_t len;
+
+ if (NULL == str)
+ return NULL;
+ if (('(' == str[0]) && ('|' == str[1]))
+ {
+ len = strlen (str);
+ if (')' == str[len - 1])
+ return GNUNET_strndup (&str[2], len - 3);
}
+ return GNUNET_strdup (str);
+}
+
- GNUNET_CONTAINER_slist_iter_destroy (&it);
+/**
+ * Compare 'str1', starting from position 'k', with whole 'str2'
+ *
+ * @param str1 first string to compare, starting from position 'k'
+ * @param str2 second string for comparison
+ * @param k starting position in 'str1'
+ *
+ * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
+ */
+static int
+strkcmp (const char *str1, const char *str2, size_t k)
+{
+ if ((NULL == str1) || (NULL == str2) || (strlen (str1) < k))
+ return -1;
+ return strcmp (&str1[k], str2);
}
-int
-set_compare (const void *buf1, const size_t len1, const void *buf2,
- const size_t len2)
+
+/**
+ * Compare two strings for equality. If either is NULL (or if both are
+ * NULL), they are not equal.
+ *
+ * @param str1 first string for comparison.
+ * @param str2 second string for comparison.
+ *
+ * @return 0 if the strings are the same, 1 or -1 if not
+ */
+static int
+nullstrcmp (const char *str1, const char *str2)
{
- int c1;
- int c2;
- struct GNUNET_CONTAINER_SList_Iterator it1;
- struct GNUNET_CONTAINER_SList_Iterator it2;
- struct State *s1;
- struct State *s2;
- struct GNUNET_CONTAINER_SList *l1;
- struct GNUNET_CONTAINER_SList *l2;
- const void *el1;
- const void *el2;
- size_t length1;
- size_t length2;
- int rslt;
- int contains;
-
- if (len1 != len2 && len1 != sizeof (struct State) &&
- len2 != sizeof (struct State))
- return 1;
+ if ((NULL == str1) || (NULL == str2))
+ return -1;
+ return strcmp (str1, str2);
+}
- s1 = (struct State *) buf1;
- s2 = (struct State *) buf2;
- l1 = s1->nfa_set;
- l2 = s2->nfa_set;
+/**
+ * Helper function used as 'action' in 'GNUNET_REGEX_automaton_traverse' function to create
+ * the depth-first numbering of the states.
+ *
+ * @param cls states array.
+ * @param count current state counter.
+ * @param s current state.
+ */
+static void
+number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
+{
+ struct GNUNET_REGEX_State **states = cls;
+
+ s->proof_id = count;
+ states[count] = s;
+}
- c1 = GNUNET_CONTAINER_slist_count (l1);
- c2 = GNUNET_CONTAINER_slist_count (l2);
- if (c1 != c2)
- return ((c1 > c2) ? 1 : -1);
+/**
+ * Construct the regular expression given the inductive step,
+ * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^*
+ * R^{(k-1)}_{kj}, and simplify the resulting expression saved in R_cur_ij.
+ *
+ * @param R_last_ij value of $R^{(k-1)_{ij}.
+ * @param R_last_ik value of $R^{(k-1)_{ik}.
+ * @param R_last_kk value of $R^{(k-1)_{kk}.
+ * @param R_last_kj value of $R^{(k-1)_{kj}.
+ * @param R_cur_ij result for this inductive step is saved in R_cur_ij, R_cur_ij
+ * is expected to be NULL when called!
+ */
+static void
+automaton_create_proofs_simplify (char *R_last_ij, char *R_last_ik,
+ char *R_last_kk, char *R_last_kj,
+ char **R_cur_ij)
+{
+ char *R_cur_l;
+ char *R_cur_r;
+ char *temp_a;
+ char *temp_b;
+ char *R_temp_ij;
+ char *R_temp_ik;
+ char *R_temp_kj;
+ char *R_temp_kk;
+
+ int eps_check;
+ int ij_ik_cmp;
+ int ij_kj_cmp;
+
+ int ik_kk_cmp;
+ int kk_kj_cmp;
+ int clean_ik_kk_cmp;
+ int clean_kk_kj_cmp;
+ unsigned int cnt;
+
+ size_t length;
+ size_t length_l;
+ size_t length_r;
+
+ GNUNET_assert (NULL == *R_cur_ij && NULL != R_cur_ij);
+
+ // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+ // R_last == R^{(k-1)}, R_cur == R^{(k)}
+ // R_cur_ij = R_cur_l | R_cur_r
+ // R_cur_l == R^{(k-1)}_{ij}
+ // R_cur_r == R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+
+ if ((NULL == R_last_ij) && ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
+ (NULL == R_last_kj)))
+ {
+ /* R^{(k)}_{ij} = N | N */
+ *R_cur_ij = NULL;
+ return;
+ }
- rslt = 0;
+ if ((NULL == R_last_ik) || (NULL == R_last_kk) || /* technically cannot happen, but looks saner */
+ (NULL == R_last_kj))
+ {
+ /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
+ *R_cur_ij = GNUNET_strdup (R_last_ij);
+ return;
+ }
+
+ // $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
+ // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+
+ R_cur_r = NULL;
+ R_cur_l = NULL;
+
+ // cache results from strcmp, we might need these many times
+ ij_kj_cmp = nullstrcmp (R_last_ij, R_last_kj);
+ ij_ik_cmp = nullstrcmp (R_last_ij, R_last_ik);
+ ik_kk_cmp = nullstrcmp (R_last_ik, R_last_kk);
+ kk_kj_cmp = nullstrcmp (R_last_kk, R_last_kj);
+
+ // Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
+ // as parentheses, so we can better compare the contents
+ R_temp_ik = remove_parentheses (remove_epsilon (R_last_ik));
+ R_temp_kk = remove_parentheses (remove_epsilon (R_last_kk));
+ R_temp_kj = remove_parentheses (remove_epsilon (R_last_kj));
+
+ clean_ik_kk_cmp = nullstrcmp (R_last_ik, R_temp_kk);
+ clean_kk_kj_cmp = nullstrcmp (R_temp_kk, R_last_kj);
+
+ // construct R_cur_l (and, if necessary R_cur_r)
+ if (NULL != R_last_ij)
+ {
+ // Assign R_temp_ij to R_last_ij and remove epsilon as well
+ // as parentheses, so we can better compare the contents
+ R_temp_ij = remove_parentheses (remove_epsilon (R_last_ij));
+
+ if (0 == strcmp (R_temp_ij, R_temp_ik) && 0 == strcmp (R_temp_ik, R_temp_kk)
+ && 0 == strcmp (R_temp_kk, R_temp_kj))
+ {
+ if (0 == strlen (R_temp_ij))
+ {
+ R_cur_r = GNUNET_strdup ("");
+ }
+ else if ((0 == strncmp (R_last_ij, "(|", 2)) ||
+ (0 == strncmp (R_last_ik, "(|", 2) &&
+ 0 == strncmp (R_last_kj, "(|", 2)))
+ {
+ // a|(e|a)a*(e|a) = a*
+ // a|(e|a)(e|a)*(e|a) = a*
+ // (e|a)|aa*a = a*
+ // (e|a)|aa*(e|a) = a*
+ // (e|a)|(e|a)a*a = a*
+ // (e|a)|(e|a)a*(e|a) = a*
+ // (e|a)|(e|a)(e|a)*(e|a) = a*
+ if (GNUNET_YES == needs_parentheses (R_temp_ij))
+ GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_ij);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s*", R_temp_ij);
+ }
+ else
+ {
+ // a|aa*a = a+
+ // a|(e|a)a*a = a+
+ // a|aa*(e|a) = a+
+ // a|(e|a)(e|a)*a = a+
+ // a|a(e|a)*(e|a) = a+
+ if (GNUNET_YES == needs_parentheses (R_temp_ij))
+ GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_ij);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s+", R_temp_ij);
+ }
+ }
+ else if (0 == ij_ik_cmp && 0 == clean_kk_kj_cmp && 0 != clean_ik_kk_cmp)
+ {
+ // a|ab*b = ab*
+ if (strlen (R_last_kk) < 1)
+ R_cur_r = GNUNET_strdup (R_last_ij);
+ else if (GNUNET_YES == needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_last_kk);
+
+ R_cur_l = NULL;
+ }
+ else if (0 == ij_kj_cmp && 0 == clean_ik_kk_cmp && 0 != clean_kk_kj_cmp)
+ {
+ // a|bb*a = b*a
+ if (strlen (R_last_kk) < 1)
+ R_cur_r = GNUNET_strdup (R_last_kj);
+ else if (GNUNET_YES == needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
+
+ R_cur_l = NULL;
+ }
+ else if (0 == ij_ik_cmp && 0 == kk_kj_cmp && !has_epsilon (R_last_ij) &&
+ has_epsilon (R_last_kk))
+ {
+ // a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab*
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ij, R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ij, R_temp_kk);
+
+ R_cur_l = NULL;
+ }
+ else if (0 == ij_kj_cmp && 0 == ik_kk_cmp && !has_epsilon (R_last_ij) &&
+ has_epsilon (R_last_kk))
+ {
+ // a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_ij);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_ij);
+
+ R_cur_l = NULL;
+ }
+ else
+ {
+ temp_a = (NULL == R_last_ij) ? NULL : GNUNET_strdup (R_last_ij);
+ temp_a = remove_parentheses (temp_a);
+ R_cur_l = temp_a;
+ }
+
+ GNUNET_free_non_null (R_temp_ij);
+ }
+ else
+ {
+ // we have no left side
+ R_cur_l = NULL;
+ }
+
+ // construct R_cur_r, if not already constructed
+ if (NULL == R_cur_r)
+ {
+ length = strlen (R_temp_kk) - strlen (R_last_ik);
+
+ // a(ba)*bx = (ab)+x
+ if (length > 0 && NULL != R_last_kk && 0 < strlen (R_last_kk) &&
+ NULL != R_last_kj && 0 < strlen (R_last_kj) && NULL != R_last_ik &&
+ 0 < strlen (R_last_ik) && 0 == strkcmp (R_temp_kk, R_last_ik, length) &&
+ 0 == strncmp (R_temp_kk, R_last_kj, length))
+ {
+ temp_a = GNUNET_malloc (length + 1);
+ temp_b = GNUNET_malloc ((strlen (R_last_kj) - length) + 1);
+
+ length_l = 0;
+ length_r = 0;
+
+ for (cnt = 0; cnt < strlen (R_last_kj); cnt++)
+ {
+ if (cnt < length)
+ {
+ temp_a[length_l] = R_last_kj[cnt];
+ length_l++;
+ }
+ else
+ {
+ temp_b[length_r] = R_last_kj[cnt];
+ length_r++;
+ }
+ }
+ temp_a[length_l] = '\0';
+ temp_b[length_r] = '\0';
+
+ // e|(ab)+ = (ab)*
+ if (NULL != R_cur_l && 0 == strlen (R_cur_l) && 0 == strlen (temp_b))
+ {
+ GNUNET_asprintf (&R_cur_r, "(%s%s)*", R_last_ik, temp_a);
+ GNUNET_free (R_cur_l);
+ R_cur_l = NULL;
+ }
+ else
+ {
+ GNUNET_asprintf (&R_cur_r, "(%s%s)+%s", R_last_ik, temp_a, temp_b);
+ }
+ GNUNET_free (temp_a);
+ GNUNET_free (temp_b);
+ }
+ else if (0 == strcmp (R_temp_ik, R_temp_kk) &&
+ 0 == strcmp (R_temp_kk, R_temp_kj))
+ {
+ // (e|a)a*(e|a) = a*
+ // (e|a)(e|a)*(e|a) = a*
+ if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj))
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)*", R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s*", R_temp_kk);
+ }
+ // aa*a = a+a
+ else if (0 == clean_ik_kk_cmp && 0 == clean_kk_kj_cmp &&
+ !has_epsilon (R_last_ik))
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_temp_kk);
+ }
+ // (e|a)a*a = a+
+ // aa*(e|a) = a+
+ // a(e|a)*(e|a) = a+
+ // (e|a)a*a = a+
+ else
+ {
+ eps_check =
+ (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
+ has_epsilon (R_last_kj));
+
+ if (eps_check == 1)
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)+", R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s+", R_temp_kk);
+ }
+ }
+ }
+ // aa*b = a+b
+ // (e|a)(e|a)*b = a*b
+ else if (0 == strcmp (R_temp_ik, R_temp_kk))
+ {
+ if (has_epsilon (R_last_ik))
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)*%s", R_temp_kk, R_last_kj);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s*%s", R_temp_kk, R_last_kj);
+ }
+ else
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_temp_kk, R_last_kj);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s+%s", R_temp_kk, R_last_kj);
+ }
+ }
+ // ba*a = ba+
+ // b(e|a)*(e|a) = ba*
+ else if (0 == strcmp (R_temp_kk, R_temp_kj))
+ {
+ if (has_epsilon (R_last_kj))
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "%s(%s)*", R_last_ik, R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s%s*", R_last_ik, R_temp_kk);
+ }
+ else
+ {
+ if (needs_parentheses (R_temp_kk))
+ GNUNET_asprintf (&R_cur_r, "(%s)+%s", R_last_ik, R_temp_kk);
+ else
+ GNUNET_asprintf (&R_cur_r, "%s+%s", R_last_ik, R_temp_kk);
+ }
+ }
+ else
+ {
+ if (strlen (R_temp_kk) > 0)
+ {
+ if (needs_parentheses (R_temp_kk))
+ {
+ GNUNET_asprintf (&R_cur_r, "%s(%s)*%s", R_last_ik, R_temp_kk,
+ R_last_kj);
+ }
+ else
+ {
+ GNUNET_asprintf (&R_cur_r, "%s%s*%s", R_last_ik, R_temp_kk,
+ R_last_kj);
+ }
+ }
+ else
+ {
+ GNUNET_asprintf (&R_cur_r, "%s%s", R_last_ik, R_last_kj);
+ }
+ }
+ }
+
+ GNUNET_free_non_null (R_temp_ik);
+ GNUNET_free_non_null (R_temp_kk);
+ GNUNET_free_non_null (R_temp_kj);
+
+ if (NULL == R_cur_l && NULL == R_cur_r)
+ {
+ *R_cur_ij = NULL;
+ return;
+ }
+
+ if (NULL != R_cur_l && NULL == R_cur_r)
+ {
+ *R_cur_ij = R_cur_l;
+ return;
+ }
+
+ if (NULL == R_cur_l && NULL != R_cur_r)
+ {
+ *R_cur_ij = R_cur_r;
+ return;
+ }
+
+ if (0 == nullstrcmp (R_cur_l, R_cur_r))
+ {
+ *R_cur_ij = R_cur_l;
+ GNUNET_free (R_cur_r);
+ return;
+ }
+
+ GNUNET_asprintf (R_cur_ij, "(%s|%s)", R_cur_l, R_cur_r);
+
+ GNUNET_free (R_cur_l);
+ GNUNET_free (R_cur_r);
+}
+
+
+/**
+ * create proofs for all states in the given automaton. Implementation of the
+ * algorithm descriped in chapter 3.2.1 of "Automata Theory, Languages, and
+ * Computation 3rd Edition" by Hopcroft, Motwani and Ullman.
+ *
+ * @param a automaton.
+ */
+static void
+automaton_create_proofs (struct GNUNET_REGEX_Automaton *a)
+{
+ unsigned int n = a->state_count;
+ struct GNUNET_REGEX_State *states[n];
+ char *R_last[n][n];
+ char *R_cur[n][n];
+ char *temp;
+ struct GNUNET_REGEX_Transition *t;
+ char *complete_regex;
+ unsigned int i;
+ unsigned int j;
+ unsigned int k;
+
+
+ /* create depth-first numbering of the states, initializes 'state' */
+ GNUNET_REGEX_automaton_traverse (a, &number_states, states);
+
+ /* Compute regular expressions of length "1" between each pair of states */
+ for (i = 0; i < n; i++)
+ {
+ for (j = 0; j < n; j++)
+ {
+ R_cur[i][j] = NULL;
+ R_last[i][j] = NULL;
+ }
+ for (t = states[i]->transitions_head; NULL != t; t = t->next)
+ {
+ j = t->to_state->proof_id;
+ if (NULL == R_last[i][j])
+ GNUNET_asprintf (&R_last[i][j], "%c", t->label);
+ else
+ {
+ temp = R_last[i][j];
+ GNUNET_asprintf (&R_last[i][j], "%s|%c", R_last[i][j], t->label);
+ GNUNET_free (temp);
+ }
+ }
+ if (NULL == R_last[i][i])
+ GNUNET_asprintf (&R_last[i][i], "");
+ else
+ {
+ temp = R_last[i][i];
+ GNUNET_asprintf (&R_last[i][i], "(|%s)", R_last[i][i]);
+ GNUNET_free (temp);
+ }
+ }
+ for (i = 0; i < n; i++)
+ for (j = 0; j < n; j++)
+ if (needs_parentheses (R_last[i][j]))
+ {
+ temp = R_last[i][j];
+ GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]);
+ GNUNET_free (temp);
+ }
+
+ /* Compute regular expressions of length "k" between each pair of states per induction */
+ for (k = 0; k < n; k++)
+ {
+ for (i = 0; i < n; i++)
+ {
+ for (j = 0; j < n; j++)
+ {
+ // Basis for the recursion:
+ // $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
+ // R_last == R^{(k-1)}, R_cur == R^{(k)}
+
+ // Create R_cur[i][j] and simplify the expression
+ automaton_create_proofs_simplify (R_last[i][j], R_last[i][k],
+ R_last[k][k], R_last[k][j],
+ &R_cur[i][j]);
+ }
+ }
+
+ // set R_last = R_cur
+ for (i = 0; i < n; i++)
+ {
+ for (j = 0; j < n; j++)
+ {
+ GNUNET_free_non_null (R_last[i][j]);
+ R_last[i][j] = R_cur[i][j];
+ R_cur[i][j] = NULL;
+ }
+ }
+ }
+
+ // assign proofs and hashes
+ for (i = 0; i < n; i++)
+ {
+ if (NULL != R_last[a->start->proof_id][i])
+ {
+ states[i]->proof = GNUNET_strdup (R_last[a->start->proof_id][i]);
+ GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof),
+ &states[i]->hash);
+ }
+ }
- for (it1 = GNUNET_CONTAINER_slist_begin (l1);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it1);
- GNUNET_CONTAINER_slist_next (&it1))
+ // complete regex for whole DFA: union of all pairs (start state/accepting state(s)).
+ complete_regex = NULL;
+ for (i = 0; i < n; i++)
{
- el1 = GNUNET_CONTAINER_slist_get (&it1, &length1);
- contains = 0;
-
- for (it2 = GNUNET_CONTAINER_slist_begin (l2);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it2);
- GNUNET_CONTAINER_slist_next (&it2))
+ if (states[i]->accepting)
{
- el2 = GNUNET_CONTAINER_slist_get (&it2, &length2);
-
- if (length1 != length2)
+ if (NULL == complete_regex && 0 < strlen (R_last[a->start->proof_id][i]))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Comparing lists failed, element size mismatch\n");
- break;
+ GNUNET_asprintf (&complete_regex, "%s", R_last[a->start->proof_id][i]);
}
- if (((struct State *) el1)->id == ((struct State *) el2)->id)
+ else if (NULL != R_last[a->start->proof_id][i] &&
+ 0 < strlen (R_last[a->start->proof_id][i]))
{
- contains = 1;
- break;
+ temp = complete_regex;
+ GNUNET_asprintf (&complete_regex, "%s|%s", complete_regex,
+ R_last[a->start->proof_id][i]);
+ GNUNET_free (temp);
}
}
- GNUNET_CONTAINER_slist_iter_destroy (&it2);
-
- if (0 == contains)
- {
- rslt = 1;
- break;
- }
}
- GNUNET_CONTAINER_slist_iter_destroy (&it1);
+ a->canonical_regex = complete_regex;
- return rslt;
-}
-
-int
-transition_literal_compare (const void *buf1, const size_t len1,
- const void *buf2, const size_t len2)
-{
- struct Transition *t1;
- struct Transition *t2;
-
- if (len1 != len2 && len1 != sizeof (struct Transition) &&
- len2 != sizeof (struct Transition))
+ // cleanup
+ for (i = 0; i < n; i++)
{
- return 1;
+ for (j = 0; j < n; j++)
+ GNUNET_free_non_null (R_last[i][j]);
}
-
- t1 = (struct Transition *) buf1;
- t2 = (struct Transition *) buf2;
-
- if (t1->literal == t2->literal)
- return 0;
- else if (t1->literal > t2->literal)
- return 1;
- else
- return -1;
}
-void
-add_transition (struct GNUNET_REGEX_Context *ctx, struct State *from_state,
- const char literal, struct State *to_state)
-{
- struct Transition t;
-
- if (NULL == from_state)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
- return;
- }
-
- t.id = ctx->transition_id++;
- t.literal = literal;
- t.state = to_state;
-
- GNUNET_CONTAINER_slist_add (from_state->transitions,
- GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, &t,
- sizeof t);
-}
-struct State *
-dfa_create_state (struct GNUNET_REGEX_Context *ctx,
- struct GNUNET_CONTAINER_SList *states)
+/**
+ * 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 State *s;
+ struct GNUNET_REGEX_State *s;
char *name;
- struct GNUNET_CONTAINER_SList_Iterator stateit;
- struct GNUNET_CONTAINER_SList_Iterator tranit;
int len = 0;
- struct State *cstate;
- struct Transition *ctran;
+ struct GNUNET_REGEX_State *cstate;
+ struct GNUNET_REGEX_Transition *ctran;
+ unsigned int i;
- s = GNUNET_malloc (sizeof (struct State));
+ s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
s->id = ctx->state_id++;
s->accepting = 0;
- s->transitions = GNUNET_CONTAINER_slist_create ();
s->marked = 0;
s->name = NULL;
+ s->scc_id = 0;
+ s->index = -1;
+ s->lowlink = -1;
+ s->contained = 0;
+ s->proof = NULL;
- if (NULL == states)
+ if (NULL == nfa_states)
+ {
+ GNUNET_asprintf (&s->name, "s%i", s->id);
return s;
+ }
- s->nfa_set = states;
+ s->nfa_set = nfa_states;
- if (0 == GNUNET_CONTAINER_slist_count (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 (stateit = GNUNET_CONTAINER_slist_begin (states);
- GNUNET_NO == GNUNET_CONTAINER_slist_end (&stateit);
- GNUNET_CONTAINER_slist_next (&stateit))
+ for (i = 0; i < nfa_states->len; i++)
{
- cstate = GNUNET_CONTAINER_slist_get (&stateit, NULL);
- GNUNET_CONTAINER_slist_iter_destroy (&tranit);
+ cstate = nfa_states->states[i];
GNUNET_asprintf (&name, "%i,", cstate->id);
if (NULL != name)
name = NULL;
}
- // Add a transition for each distinct literal to NULL state
- for (tranit = GNUNET_CONTAINER_slist_begin (cstate->transitions);
- GNUNET_NO == GNUNET_CONTAINER_slist_end (&tranit);
- GNUNET_CONTAINER_slist_next (&tranit))
+ // Add a transition for each distinct label to NULL state
+ for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
{
- ctran = GNUNET_CONTAINER_slist_get (&tranit, NULL);
- if (0 != ctran->literal &&
- NULL == GNUNET_CONTAINER_slist_contains2 (s->transitions, ctran,
- sizeof *ctran,
- &transition_literal_compare))
- {
- add_transition (ctx, s, ctran->literal, NULL);
- }
+ if (0 != ctran->label)
+ 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;
}
- GNUNET_CONTAINER_slist_iter_destroy (&stateit);
s->name[strlen (s->name) - 1] = '}';
return s;
}
-void
-dfa_clear_nfa_set (struct GNUNET_CONTAINER_SList *states)
+
+/**
+ * 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 GNUNET_CONTAINER_SList_Iterator it;
- struct State *s;
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_State *new_s;
+
+ if (NULL == s)
+ return NULL;
+
+ new_s = NULL;
- for (it = GNUNET_CONTAINER_slist_begin (states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+ for (t = s->transitions_head; NULL != t; t = t->next)
{
- s = GNUNET_CONTAINER_slist_get (&it, NULL);
- if (NULL != s->nfa_set)
+ if (label == t->label)
{
- GNUNET_CONTAINER_slist_destroy (s->nfa_set);
- s->nfa_set = NULL;
+ new_s = t->to_state;
+ break;
}
}
- GNUNET_CONTAINER_slist_iter_destroy (&it);
+ return new_s;
}
-struct GNUNET_REGEX_Automaton *
-nfa_create (struct State *start, struct State *end)
+
+/**
+ * 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
+ GNUNET_REGEX_automaton_traverse (a, NULL, 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 GNUNET_REGEX_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)
+{
+ unsigned int i;
+ int table[a->state_count][a->state_count];
+ struct GNUNET_REGEX_State *s1;
+ struct GNUNET_REGEX_State *s2;
+ struct GNUNET_REGEX_Transition *t1;
+ struct GNUNET_REGEX_Transition *t2;
+ struct GNUNET_REGEX_State *s1_next;
+ struct GNUNET_REGEX_State *s2_next;
+ int change;
+ unsigned 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;
- n->states = GNUNET_CONTAINER_slist_create ();
if (NULL == start && NULL == end)
return n;
- GNUNET_CONTAINER_slist_add (n->states,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, end,
- sizeof *end);
-
- GNUNET_CONTAINER_slist_add (n->states,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, start,
- sizeof *start);
+ automaton_add_state (n, end);
+ automaton_add_state (n, start);
n->start = start;
n->end = end;
return n;
}
-void
+
+/**
+ * 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 GNUNET_CONTAINER_SList *states)
+ struct GNUNET_REGEX_State *states_head,
+ struct GNUNET_REGEX_State *states_tail)
{
- // This isn't very pretty. Would be better to use GNUNET_CONTAINER_slist_append, but
- // this function adds to the beginning of dst, which currently breaks "pretty"
- // printing of the graph...
- struct GNUNET_CONTAINER_SList_Iterator i;
- struct State *s;
+ struct GNUNET_REGEX_State *s;
- for (i = GNUNET_CONTAINER_slist_begin (states);
- GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
- GNUNET_CONTAINER_slist_next (&i))
+ if (NULL == n || NULL == states_head)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
+ return;
+ }
+
+ if (NULL == n->states_head)
+ {
+ n->states_head = states_head;
+ n->states_tail = states_tail;
+ return;
+ }
+ if (NULL != states_head)
{
- s = GNUNET_CONTAINER_slist_get (&i, NULL);
- GNUNET_CONTAINER_slist_add_end (n->states,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- s, sizeof *s);
+ n->states_tail->next = states_head;
+ n->states_tail = states_tail;
}
- GNUNET_CONTAINER_slist_iter_destroy (&i);
+
+ 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 GNUNET_REGEX_State *
+nfa_state_create (struct GNUNET_REGEX_Context *ctx, int accepting)
{
- struct State *s;
+ struct GNUNET_REGEX_State *s;
- s = GNUNET_malloc (sizeof (struct State));
+ s = GNUNET_malloc (sizeof (struct GNUNET_REGEX_State));
s->id = ctx->state_id++;
s->accepting = accepting;
- s->transitions = GNUNET_CONTAINER_slist_create ();
s->marked = 0;
+ s->contained = 0;
+ s->index = -1;
+ s->lowlink = -1;
+ s->scc_id = 0;
s->name = NULL;
GNUNET_asprintf (&s->name, "s%i", s->id);
return s;
}
-void
-automaton_fragment_clear (struct GNUNET_REGEX_Automaton *a)
-{
- GNUNET_CONTAINER_slist_destroy (a->states);
- a->start = NULL;
- a->end = NULL;
- GNUNET_free (a);
-}
-void
-automaton_destroy_state (struct State *s)
+/**
+ * Calculates the NFA closure set for the given state.
+ *
+ * @param nfa the NFA containing 's'
+ * @param s starting point state
+ * @param label transitioning label on which to base the closure on,
+ * pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
+ */
+static struct GNUNET_REGEX_StateSet *
+nfa_closure_create (struct GNUNET_REGEX_Automaton *nfa,
+ struct GNUNET_REGEX_State *s, const char label)
{
- if (NULL != s->transitions)
- GNUNET_CONTAINER_slist_destroy (s->transitions);
- if (NULL != s->name)
- GNUNET_free (s->name);
- if (NULL != s->nfa_set)
- GNUNET_CONTAINER_slist_destroy (s->nfa_set);
- GNUNET_free (s);
+ struct GNUNET_REGEX_StateSet *cls;
+ struct GNUNET_REGEX_StateSet *cls_check;
+ struct GNUNET_REGEX_State *clsstate;
+ struct GNUNET_REGEX_State *currentstate;
+ struct GNUNET_REGEX_Transition *ctran;
+
+ if (NULL == s)
+ return NULL;
+
+ cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
+ cls_check = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
+
+ for (clsstate = nfa->states_head; NULL != clsstate; clsstate = clsstate->next)
+ clsstate->contained = 0;
+
+ // Add start state to closure only for epsilon closure
+ if (0 == label)
+ GNUNET_array_append (cls->states, cls->len, s);
+
+ GNUNET_array_append (cls_check->states, cls_check->len, s);
+ while (cls_check->len > 0)
+ {
+ 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->to_state && label == ctran->label)
+ {
+ clsstate = ctran->to_state;
+
+ if (NULL != clsstate && 0 == clsstate->contained)
+ {
+ GNUNET_array_append (cls->states, cls->len, clsstate);
+ GNUNET_array_append (cls_check->states, cls_check->len, clsstate);
+ clsstate->contained = 1;
+ }
+ }
+ }
+ }
+ GNUNET_assert (0 == cls_check->len);
+ GNUNET_free (cls_check);
+
+ // sort the states
+ if (cls->len > 1)
+ qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_State *),
+ state_compare);
+
+ return cls;
}
-void
-mark_all_states (struct GNUNET_REGEX_Automaton *n, int marked)
+
+/**
+ * Calculates the closure set for the given set of states.
+ *
+ * @param nfa the NFA containing 's'
+ * @param states list of states on which to base the closure on
+ * @param label transitioning label for which to base the closure on,
+ * pass 0 for epsilon transition
+ *
+ * @return sorted nfa closure on 'label' (epsilon closure if 'label' is 0)
+ */
+static struct GNUNET_REGEX_StateSet *
+nfa_closure_set_create (struct GNUNET_REGEX_Automaton *nfa,
+ struct GNUNET_REGEX_StateSet *states, const char label)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
- struct State *s;
+ struct GNUNET_REGEX_State *s;
+ struct GNUNET_REGEX_StateSet *sset;
+ struct GNUNET_REGEX_StateSet *cls;
+ unsigned int i;
+ unsigned int j;
+ unsigned int k;
+ unsigned int contains;
+
+ if (NULL == states)
+ return NULL;
+
+ cls = GNUNET_malloc (sizeof (struct GNUNET_REGEX_StateSet));
- for (it = GNUNET_CONTAINER_slist_begin (n->states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+ for (i = 0; i < states->len; i++)
{
- s = GNUNET_CONTAINER_slist_get (&it, NULL);
- s->marked = marked;
+ s = states->states[i];
+ sset = nfa_closure_create (nfa, s, label);
+
+ 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);
}
- GNUNET_CONTAINER_slist_iter_destroy (&it);
+ if (cls->len > 1)
+ qsort (cls->states, cls->len, sizeof (struct GNUNET_REGEX_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 *new;
- b = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
- a = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
+ b = ctx->stack_tail;
+ GNUNET_assert (NULL != b);
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
+ a = ctx->stack_tail;
+ GNUNET_assert (NULL != a);
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- add_transition (ctx, a->end, 0, b->start);
+ state_add_transition (ctx, a->end, 0, b->start);
a->end->accepting = 0;
b->end->accepting = 1;
- new = nfa_create (NULL, NULL);
- nfa_add_states (new, a->states);
- nfa_add_states (new, b->states);
- new->start = a->start;
- new->end = b->end;
- automaton_fragment_clear (a);
- automaton_fragment_clear (b);
+ 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);
+
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+}
+
+
+/**
+ * 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 *new;
+ struct GNUNET_REGEX_State *start;
+ struct GNUNET_REGEX_State *end;
+
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, 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_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
+
+ state_add_transition (ctx, start, 0, a->start);
+ state_add_transition (ctx, start, 0, end);
+ state_add_transition (ctx, a->end, 0, a->start);
+ state_add_transition (ctx, a->end, 0, end);
+
+ a->end->accepting = 0;
+ end->accepting = 1;
+
+ new = nfa_fragment_create (start, end);
+ nfa_add_states (new, a->states_head, a->states_tail);
+ automaton_fragment_clear (a);
+
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+}
+
+
+/**
+ * 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;
+
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
+ state_add_transition (ctx, a->end, 0, a->start);
- stack_push (ctx->stack, new, sizeof *new);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a);
}
-void
-nfa_add_star_op (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 *new;
- struct State *start;
- struct State *end;
+ struct GNUNET_REGEX_State *start;
+ struct GNUNET_REGEX_State *end;
- a = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
+ a = ctx->stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
if (NULL == a)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "nfa_add_star_op failed, because there was no element 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);
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
- 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);
+ state_add_transition (ctx, start, 0, a->start);
+ state_add_transition (ctx, start, 0, end);
+ state_add_transition (ctx, a->end, 0, end);
a->end->accepting = 0;
- end->accepting = 1;
- new = nfa_create (start, end);
- nfa_add_states (new, a->states);
+ new = nfa_fragment_create (start, end);
+ nfa_add_states (new, a->states_head, a->states_tail);
automaton_fragment_clear (a);
- stack_push (ctx->stack, new, sizeof *new);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
}
-void
-nfa_add_plus_op (struct GNUNET_REGEX_Context *ctx)
-{
- struct GNUNET_REGEX_Automaton *a;
-
- a = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
-
- add_transition (ctx, a->end, 0, a->start);
-
- stack_push (ctx->stack, a, sizeof *a);
-}
-void
+/**
+ * Pops two NFA fragments (a, b) from the stack and adds a new NFA fragment that
+ * alternates between a and b (a|b)
+ *
+ * @param ctx context
+ */
+static void
nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
{
struct GNUNET_REGEX_Automaton *a;
struct GNUNET_REGEX_Automaton *b;
struct GNUNET_REGEX_Automaton *new;
- struct State *start;
- struct State *end;
+ struct GNUNET_REGEX_State *start;
+ struct GNUNET_REGEX_State *end;
- b = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
- a = stack_pop (ctx->stack, sizeof (struct GNUNET_REGEX_Automaton));
+ b = ctx->stack_tail;
+ GNUNET_assert (NULL != b);
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b);
+ a = ctx->stack_tail;
+ GNUNET_assert (NULL != a);
+ GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
- start = nfa_create_state (ctx, 0);
- end = nfa_create_state (ctx, 1);
- add_transition (ctx, start, 0, a->start);
- add_transition (ctx, start, 0, b->start);
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
+ state_add_transition (ctx, start, 0, a->start);
+ state_add_transition (ctx, start, 0, b->start);
- add_transition (ctx, a->end, 0, end);
- add_transition (ctx, b->end, 0, end);
+ state_add_transition (ctx, a->end, 0, end);
+ state_add_transition (ctx, b->end, 0, end);
a->end->accepting = 0;
b->end->accepting = 0;
end->accepting = 1;
- new = nfa_create (start, end);
- nfa_add_states (new, a->states);
- nfa_add_states (new, b->states);
+ 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);
- stack_push (ctx->stack, new, sizeof *new);
+ GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
}
-void
-nfa_add_literal (struct GNUNET_REGEX_Context *ctx, const char lit)
-{
- struct GNUNET_REGEX_Automaton *n;
- struct State *start;
- struct State *end;
-
- start = nfa_create_state (ctx, 0);
- end = nfa_create_state (ctx, 1);
- add_transition (ctx, start, lit, end);
- n = nfa_create (start, end);
- stack_push (ctx->stack, n, sizeof *n);
-}
/**
- * Calculates the closure set for the given set of states.
- *
- * @param states set of states for which to calculate the closure
- * @param count number of states in 'states'
- * @param literal for the transition
+ * Adds a new nfa fragment to the stack
*
- * @return set of states that can be reached from the given 'states' when
- * using only 'literal' transitions
+ * @param ctx context
+ * @param lit label for nfa transition
*/
-struct GNUNET_CONTAINER_SList *
-create_nfa_closure (struct GNUNET_CONTAINER_SList *states, const char literal)
+static void
+nfa_add_label (struct GNUNET_REGEX_Context *ctx, const char lit)
{
- struct GNUNET_CONTAINER_SList_Iterator stateit;
- struct GNUNET_CONTAINER_SList_Iterator tranit;
- struct GNUNET_CONTAINER_SList *cls;
- struct GNUNET_CONTAINER_SList *cls_check;
- struct State *s;
- struct State *currentstate;
- struct Transition *currenttransition;
- struct State *clsstate;
-
- cls = GNUNET_CONTAINER_slist_create ();
-
- for (stateit = GNUNET_CONTAINER_slist_begin (states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&stateit);
- GNUNET_CONTAINER_slist_next (&stateit))
- {
- s = GNUNET_CONTAINER_slist_get (&stateit, NULL);
- cls_check = GNUNET_CONTAINER_slist_create ();
-
- // Add start state to closure only for epsilon closure
- if (0 == literal)
- {
- GNUNET_CONTAINER_slist_add (cls,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC, s,
- sizeof *s);
- }
-
- stack_push (cls_check, s, sizeof *s);
-
- while (!stack_empty (cls_check))
- {
- currentstate = stack_pop (cls_check, sizeof (struct State));
-
- for (tranit = GNUNET_CONTAINER_slist_begin (currentstate->transitions);
- GNUNET_CONTAINER_slist_end (&tranit) != GNUNET_YES;
- GNUNET_CONTAINER_slist_next (&tranit))
- {
- currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL);
-
- if (NULL != currenttransition->state &&
- literal == currenttransition->literal)
- {
- clsstate = currenttransition->state;
-
- if (NULL == clsstate)
- break;
-
- if (GNUNET_YES !=
- GNUNET_CONTAINER_slist_contains (cls, clsstate, sizeof *clsstate))
- {
- GNUNET_CONTAINER_slist_add (cls,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- clsstate, sizeof *clsstate);
- stack_push (cls_check, clsstate, sizeof *clsstate);
- }
- }
- }
- GNUNET_CONTAINER_slist_iter_destroy (&tranit);
- }
+ struct GNUNET_REGEX_Automaton *n;
+ struct GNUNET_REGEX_State *start;
+ struct GNUNET_REGEX_State *end;
- GNUNET_assert (stack_empty (cls_check));
- GNUNET_CONTAINER_slist_destroy (cls_check);
- }
- GNUNET_CONTAINER_slist_iter_destroy (&stateit);
+ GNUNET_assert (NULL != ctx);
- return cls;
+ start = nfa_state_create (ctx, 0);
+ end = nfa_state_create (ctx, 1);
+ state_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);
}
-struct GNUNET_CONTAINER_SList *
-GNUNET_REGEX_move (struct GNUNET_REGEX_Automaton *a, struct State *s,
- const char literal)
-{
- struct GNUNET_CONTAINER_SList *l;
- struct GNUNET_CONTAINER_SList_Iterator it;
- struct Transition *ctran;
-
- if (!GNUNET_CONTAINER_slist_contains (a->states, s, sizeof *s))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "State %s is not part of the given automaton", s->name);
- return NULL;
- }
-
- l = GNUNET_CONTAINER_slist_create ();
- for (it = GNUNET_CONTAINER_slist_begin (s->transitions);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+/**
+ * Initialize a new context
+ *
+ * @param ctx context
+ */
+static void
+GNUNET_REGEX_context_init (struct GNUNET_REGEX_Context *ctx)
+{
+ if (NULL == ctx)
{
- ctran = GNUNET_CONTAINER_slist_get (&it, NULL);
- if (literal == ctran->literal)
- GNUNET_CONTAINER_slist_add (l, GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- ctran->state, sizeof *(ctran->state));
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
+ return;
}
- GNUNET_CONTAINER_slist_iter_destroy (&it);
-
- return l;
+ ctx->state_id = 0;
+ ctx->transition_id = 0;
+ ctx->stack_head = NULL;
+ ctx->stack_tail = NULL;
}
+
/**
* 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
+ * @return NFA, needs to be freed using GNUNET_REGEX_destroy_automaton
*/
struct GNUNET_REGEX_Automaton *
-GNUNET_REGEX_construct_nfa(const char *regex, const 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;
GNUNET_REGEX_context_init (&ctx);
+ regexp = regex;
p = NULL;
error_msg = NULL;
altcount = 0;
atomcount = 0;
pcount = 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)
case '*':
if (atomcount == 0)
{
- error_msg = "Cannot append '+' to nothing";
+ error_msg = "Cannot append '*' to nothing";
goto error;
}
nfa_add_star_op (&ctx);
}
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_label (&ctx, *regexp);
atomcount++;
break;
}
for (; altcount > 0; altcount--)
nfa_add_alternation (&ctx);
- if (NULL != p)
- GNUNET_free (p);
+ GNUNET_free_non_null (p);
- nfa = stack_pop (ctx.stack, sizeof (struct GNUNET_REGEX_Automaton));
+ nfa = ctx.stack_tail;
+ GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
- if (!stack_empty (ctx.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",
- GNUNET_CONTAINER_slist_count (nfa->states), ctx.transition_id);
-
- GNUNET_REGEX_context_destroy (&ctx);
+ nfa->regex = GNUNET_strdup (regex);
return nfa;
error:
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: %s\n", regex);
if (NULL != error_msg)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
- GNUNET_free (p);
- while (!stack_empty (ctx.stack))
- GNUNET_REGEX_automaton_destroy (stack_pop (ctx.stack,
- sizeof (struct GNUNET_REGEX_Automaton)));
- GNUNET_REGEX_context_destroy (&ctx);
+
+ GNUNET_free_non_null (p);
+
+ while (NULL != (nfa = ctx.stack_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa);
+ GNUNET_REGEX_automaton_destroy (nfa);
+ }
+
return NULL;
}
+
/**
- * Free the memory allocated by constructing the GNUNET_REGEX_Automaton
- * data structure.
+ * Create DFA states based on given 'nfa' and starting with 'dfa_state'.
*
- * @param a automaton to be destroyed
+ * @param ctx context.
+ * @param nfa NFA automaton.
+ * @param dfa DFA automaton.
+ * @param dfa_state current dfa state, pass epsilon closure of first nfa state
+ * for starting.
*/
-void
-GNUNET_REGEX_automaton_destroy(struct GNUNET_REGEX_Automaton *a)
+static void
+construct_dfa_states (struct GNUNET_REGEX_Context *ctx,
+ struct GNUNET_REGEX_Automaton *nfa,
+ struct GNUNET_REGEX_Automaton *dfa,
+ struct GNUNET_REGEX_State *dfa_state)
{
- struct GNUNET_CONTAINER_SList_Iterator it;
-
- if (NULL == a)
- return;
-
- for (it = GNUNET_CONTAINER_slist_begin (a->states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&it);
- GNUNET_CONTAINER_slist_next (&it))
+ struct GNUNET_REGEX_Transition *ctran;
+ struct GNUNET_REGEX_State *state_iter;
+ struct GNUNET_REGEX_State *new_dfa_state;
+ struct GNUNET_REGEX_State *state_contains;
+ struct GNUNET_REGEX_StateSet *tmp;
+ struct GNUNET_REGEX_StateSet *nfa_set;
+
+ for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
{
- automaton_destroy_state (GNUNET_CONTAINER_slist_get (&it, NULL));
+ if (0 == ctran->label || NULL != ctran->to_state)
+ continue;
+
+ tmp = nfa_closure_set_create (nfa, dfa_state->nfa_set, ctran->label);
+ nfa_set = nfa_closure_set_create (nfa, 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);
+ ctran->to_state = new_dfa_state;
+ construct_dfa_states (ctx, nfa, dfa, new_dfa_state);
+ }
+ else
+ {
+ ctran->to_state = state_contains;
+ automaton_destroy_state (new_dfa_state);
+ }
}
- GNUNET_CONTAINER_slist_iter_destroy (&it);
- GNUNET_CONTAINER_slist_destroy (a->states);
- GNUNET_free (a);
}
+
/**
- * Construct DFA for the given 'regex' of lenght 'len'
+ * 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
+ * @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 GNUNET_CONTAINER_SList *tmp;
- struct GNUNET_CONTAINER_SList *nfa_set;
- struct GNUNET_CONTAINER_SList *sset;
- struct GNUNET_CONTAINER_SList *dfa_stack;
- struct GNUNET_CONTAINER_SList_Iterator tranit;
- struct Transition *currenttransition;
- struct State *dfa_state;
- struct State *new_dfa_state;
- struct State *state_contains;
+ struct GNUNET_REGEX_StateSet *nfa_set;
GNUNET_REGEX_context_init (&ctx);
// Create NFA
nfa = GNUNET_REGEX_construct_nfa (regex, len);
- dfa_stack = GNUNET_CONTAINER_slist_create ();
+ if (NULL == nfa)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Could not create DFA, because NFA creation failed\n");
+ return NULL;
+ }
- // Initialize new dfa
dfa = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Automaton));
- dfa->states = GNUNET_CONTAINER_slist_create ();
+ dfa->type = DFA;
+ dfa->regex = GNUNET_strdup (regex);
// Create DFA start state from epsilon closure
- sset = GNUNET_CONTAINER_slist_create ();
- GNUNET_CONTAINER_slist_add (sset, GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- nfa->start, sizeof *(nfa->start));
- nfa_set = create_nfa_closure (sset, 0);
- GNUNET_CONTAINER_slist_destroy (sset);
- dfa->start = dfa_create_state (&ctx, nfa_set);
- GNUNET_CONTAINER_slist_add (dfa->states,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- dfa->start, sizeof *(dfa->start));
- stack_push (dfa_stack, dfa->start, sizeof *(dfa->start));
-
- while (!stack_empty (dfa_stack))
- {
- dfa_state = stack_pop (dfa_stack, sizeof (struct State));
+ nfa_set = nfa_closure_create (nfa, nfa->start, 0);
+ dfa->start = dfa_state_create (&ctx, nfa_set);
+ automaton_add_state (dfa, dfa->start);
- for (tranit = GNUNET_CONTAINER_slist_begin (dfa_state->transitions);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit);
- GNUNET_CONTAINER_slist_next (&tranit))
- {
- currenttransition = GNUNET_CONTAINER_slist_get (&tranit, NULL);
-
- if (0 != currenttransition->literal && NULL == currenttransition->state)
- {
- tmp = create_nfa_closure (dfa_state->nfa_set,
- currenttransition->literal);
- nfa_set = create_nfa_closure (tmp, 0);
- new_dfa_state = dfa_create_state (&ctx, nfa_set);
- GNUNET_CONTAINER_slist_destroy (tmp);
-
- state_contains =
- GNUNET_CONTAINER_slist_contains2 (dfa->states, new_dfa_state,
- sizeof *new_dfa_state,
- &set_compare);
- if (NULL == state_contains)
- {
- GNUNET_CONTAINER_slist_add_end (dfa->states,
- GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC,
- new_dfa_state, sizeof *new_dfa_state);
- stack_push (dfa_stack, new_dfa_state, sizeof *new_dfa_state);
- currenttransition->state = new_dfa_state;
- }
- else
- currenttransition->state = state_contains;
- }
- }
+ construct_dfa_states (&ctx, nfa, dfa, dfa->start);
- GNUNET_CONTAINER_slist_iter_destroy (&tranit);
- }
- GNUNET_CONTAINER_slist_destroy (dfa_stack);
GNUNET_REGEX_automaton_destroy (nfa);
- GNUNET_REGEX_context_destroy (&ctx);
- dfa_clear_nfa_set (dfa->states);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created DFA with %i States\n",
- GNUNET_CONTAINER_slist_count (dfa->states));
+ // Minimize DFA
+ dfa_minimize (&ctx, dfa);
+
+ // Create proofs for all states
+ automaton_create_proofs (dfa);
return dfa;
}
+
/**
- * Save the given automaton as a GraphViz dot file
+ * Free the memory allocated by constructing the GNUNET_REGEX_Automaton data
+ * structure.
*
- * @param a the automaton to be saved
- * @param filename where to save the file
+ * @param a automaton to be destroyed
*/
void
-GNUNET_REGEX_automaton_save_graph(struct GNUNET_REGEX_Automaton *a,
- const char *filename)
+GNUNET_REGEX_automaton_destroy (struct GNUNET_REGEX_Automaton *a)
{
- struct GNUNET_CONTAINER_SList_Iterator stateit;
- struct GNUNET_CONTAINER_SList_Iterator tranit;
- struct State *s;
- struct Transition *ctran;
- char *s_acc = NULL;
- char *s_tran = NULL;
- char *start;
- char *end;
- FILE *p;
+ struct GNUNET_REGEX_State *s;
+ struct GNUNET_REGEX_State *next_state;
if (NULL == a)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
return;
+
+ GNUNET_free_non_null (a->regex);
+ GNUNET_free_non_null (a->canonical_regex);
+
+ for (s = a->states_head; NULL != s;)
+ {
+ next_state = s->next;
+ automaton_destroy_state (s);
+ s = next_state;
}
- if (NULL == filename || strlen (filename) < 1)
+ GNUNET_free (a);
+}
+
+
+/**
+ * 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)
+{
+ const char *strp;
+ struct GNUNET_REGEX_State *s;
+
+ if (DFA != a->type)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Tried to evaluate DFA, but NFA automaton given");
+ return -1;
}
- p = fopen (filename, "w");
+ s = a->start;
+
+ // If the string is empty but the starting state is accepting, we accept.
+ if ((NULL == string || 0 == strlen (string)) && s->accepting)
+ return 0;
- if (p == NULL)
+ for (strp = string; NULL != strp && *strp; strp++)
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
- filename);
- return;
+ s = dfa_move (s, *strp);
+ if (NULL == s)
+ break;
}
- start = "digraph G {\nrankdir=LR\n";
- fwrite (start, strlen (start), 1, p);
+ if (NULL != s && s->accepting)
+ return 0;
+
+ return 1;
+}
+
- for (stateit = GNUNET_CONTAINER_slist_begin (a->states);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&stateit);
- GNUNET_CONTAINER_slist_next (&stateit))
+/**
+ * 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 GNUNET_REGEX_State *s;
+ struct GNUNET_REGEX_StateSet *sset;
+ struct GNUNET_REGEX_StateSet *new_sset;
+ unsigned int i;
+ int result;
+
+ if (NFA != a->type)
{
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Tried to evaluate NFA, but DFA automaton given");
+ return -1;
+ }
+
+ // If the string is empty but the starting state is accepting, we accept.
+ if ((NULL == string || 0 == strlen (string)) && a->start->accepting)
+ return 0;
- s = GNUNET_CONTAINER_slist_get (&stateit, NULL);
+ result = 1;
+ sset = nfa_closure_create (a, a->start, 0);
- if (s->accepting)
+ for (strp = string; NULL != strp && *strp; strp++)
+ {
+ new_sset = nfa_closure_set_create (a, sset, *strp);
+ state_set_clear (sset);
+ sset = nfa_closure_set_create (a, new_sset, 0);
+ state_set_clear (new_sset);
+ }
+
+ for (i = 0; i < sset->len; i++)
+ {
+ s = sset->states[i];
+ if (NULL != s && s->accepting)
{
- GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", s->name);
- fwrite (s_acc, strlen (s_acc), 1, p);
- GNUNET_free (s_acc);
+ result = 0;
+ break;
}
+ }
- s->marked = 1;
+ state_set_clear (sset);
+ return result;
+}
- for (tranit = GNUNET_CONTAINER_slist_begin (s->transitions);
- GNUNET_YES != GNUNET_CONTAINER_slist_end (&tranit);
- GNUNET_CONTAINER_slist_next (&tranit))
- {
- ctran = GNUNET_CONTAINER_slist_get (&tranit, NULL);
- if (NULL == ctran->state)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Transition from State %i has has no state for transitioning\n",
- s->id);
- continue;
- }
+/**
+ * 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;
- if (ctran->literal == 0)
- {
- GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"epsilon\"];\n",
- s->name, ctran->state->name);
- }
+ 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;
+}
+
+
+/**
+ * Get the canonical regex of the given automaton.
+ * When constructing the automaton a proof is computed for each state,
+ * consisting of the regular expression leading to this state. A complete
+ * regex for the automaton can be computed by combining these proofs.
+ * As of now this function is only useful for testing.
+ *
+ * @param a automaton for which the canonical regex should be returned.
+ *
+ * @return
+ */
+const char *
+GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a)
+{
+ if (NULL == a)
+ return NULL;
+
+ return a->canonical_regex;
+}
+
+
+/**
+ * Get the first key for the given 'input_string'. This hashes the first x bits
+ * of the 'input_string'.
+ *
+ * @param input_string string.
+ * @param string_len length of the 'input_string'.
+ * @param key pointer to where to write the hash code.
+ *
+ * @return number of bits of 'input_string' that have been consumed
+ * to construct the key
+ */
+size_t
+GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len,
+ struct GNUNET_HashCode * key)
+{
+ unsigned int size;
+
+ size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
+
+ if (NULL == input_string)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
+ return 0;
+ }
+
+ GNUNET_CRYPTO_hash (input_string, size, key);
+
+ return size;
+}
+
+
+/**
+ * Check if the given 'proof' matches the given 'key'.
+ *
+ * @param proof partial regex of a state.
+ * @param key hash of a state.
+ *
+ * @return GNUNET_OK if the proof is valid for the given key.
+ */
+int
+GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key)
+{
+ struct GNUNET_HashCode key_check;
+
+ GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check);
+ return (0 ==
+ GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
+}
+
+/**
+ * Recursive helper function for iterate_initial_edges. Will call iterator
+ * function for each initial state.
+ *
+ * @param max_len maximum length of the path in the graph.
+ * @param cur_len current length of the path already traversed.
+ * @param consumed_string string consumed by traversing the graph till this state.
+ * @param next_state next state in the graph that is reachable with 'label' transition.
+ * @param label label of the transition to the next state.
+ * @param iterator iterator function called for each edge.
+ * @param iterator_cls closure for the iterator function.
+ */
+static void
+iterate_initial_edge (const unsigned int max_len, unsigned int cur_len,
+ char *consumed_string,
+ struct GNUNET_REGEX_State *next_state, const char label,
+ GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
+{
+ char *temp;
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Edge edge;
+ struct GNUNET_HashCode hash;
+ size_t constr_len;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "max_len: %u, cur_len: %u, consumed_string: %s\n", max_len,
+ cur_len, consumed_string);
+
+ if (max_len == cur_len)
+ {
+ constr_len = strlen (consumed_string);
+ GNUNET_CRYPTO_hash (consumed_string, constr_len - 1, &hash);
+ GNUNET_asprintf (&temp, "%c", label);
+ edge.label = temp;
+ edge.destination = next_state->hash;
+ consumed_string[constr_len - 1] = '\0';
+ iterator (iterator_cls, &hash, consumed_string, 0, 1, &edge);
+ GNUNET_free (temp);
+
+ return;
+ }
+ else if (cur_len <= max_len)
+ {
+ cur_len++;
+ for (t = next_state->transitions_head; NULL != t; t = t->next)
+ {
+ if (NULL != consumed_string)
+ GNUNET_asprintf (&temp, "%s%c", consumed_string, t->label);
else
- {
- GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%c\"];\n",
- s->name, ctran->state->name, ctran->literal);
- }
+ GNUNET_asprintf (&temp, "%c", t->label);
- fwrite (s_tran, strlen (s_tran), 1, p);
- GNUNET_free (s_tran);
+ iterate_initial_edge (max_len, cur_len, temp, t->to_state, t->label,
+ iterator, iterator_cls);
+ GNUNET_free (temp);
}
- GNUNET_CONTAINER_slist_iter_destroy (&tranit);
}
- GNUNET_CONTAINER_slist_iter_destroy (&stateit);
+}
+
+/**
+ * Iterate over all initial edges that aren't actually part of the automaton.
+ * This is needed to find the initial states returned by
+ * GNUNET_REGEX_get_first_key.
+ *
+ * @param a the automaton for which the initial states should be computed.
+ * @param initial_len length of the initial state string.
+ * @param iterator iterator function called for each edge.
+ * @param iterator_cls closure for the iterator function.
+ */
+void
+iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
+ const unsigned int initial_len,
+ GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
+{
+ iterate_initial_edge (initial_len + 1, 0, NULL, a->start, 0, iterator,
+ iterator_cls);
+}
+
+
+/**
+ * Iterate over all edges helper function starting from state 's', calling
+ * iterator on for each edge.
+ *
+ * @param s state.
+ * @param iterator iterator function called for each edge.
+ * @param iterator_cls closure.
+ */
+static void
+iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
+ void *iterator_cls)
+{
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Edge edges[s->transition_count];
+ unsigned int num_edges;
+
+ if (GNUNET_YES != s->marked)
+ {
+ s->marked = GNUNET_YES;
+
+ num_edges = state_get_edges (s, edges);
+
+ iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges, edges);
+
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ iterate_edge (t->to_state, iterator, iterator_cls);
+ }
+}
+
+
+/**
+ * Iterate over all edges starting from start state of automaton 'a'. Calling
+ * iterator for each edge.
+ *
+ * @param a automaton.
+ * @param iterator iterator called for each edge.
+ * @param iterator_cls closure.
+ */
+void
+GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
+ GNUNET_REGEX_KeyIterator iterator,
+ void *iterator_cls)
+{
+ struct GNUNET_REGEX_State *s;
+
+ for (s = a->states_head; NULL != s; s = s->next)
+ s->marked = GNUNET_NO;
- end = "\n}\n";
- fwrite (end, strlen (end), 1, p);
- fclose (p);
+ iterate_initial_edges (a, INITIAL_BITS, iterator, iterator_cls);
+ iterate_edge (a->start, iterator, iterator_cls);
}