#include "regex_internal.h"
-/**
- * Constant for how many bits the initial string regex should have.
- */
-#define INITIAL_BITS 8
-
-
/**
* Set of states.
*/
struct GNUNET_REGEX_State *from_state, const char *label,
struct GNUNET_REGEX_State *to_state)
{
- int is_dup;
struct GNUNET_REGEX_Transition *t;
struct GNUNET_REGEX_Transition *oth;
}
// 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 && 0 == nullstrcmp (t->label, label) &&
t->from_state == from_state)
- {
- is_dup = GNUNET_YES;
- break;
- }
+ return;
}
- if (GNUNET_YES == is_dup)
- return;
-
// sort transitions by label
for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
{
if (transition->from_state != state)
return;
+ GNUNET_free_non_null (transition->label);
+
state->transition_count--;
GNUNET_CONTAINER_DLL_remove (state->transitions_head, state->transitions_tail,
transition);
- GNUNET_free_non_null (transition->label);
+
GNUNET_free (transition);
}
static void
state_set_clear (struct GNUNET_REGEX_StateSet *set)
{
- if (NULL != set)
- {
- GNUNET_free_non_null (set->states);
- GNUNET_free (set);
- }
+ if (NULL == set)
+ return;
+
+ if (set->len > 0)
+ GNUNET_array_grow (set->states, set->len, 0);
+ GNUNET_free (set);
}
GNUNET_free_non_null (s->name);
GNUNET_free_non_null (s->proof);
+ state_set_clear (s->nfa_set);
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_non_null (t->label);
- GNUNET_free (t);
+ state_remove_transition (s, t);
}
- state_set_clear (s->nfa_set);
-
GNUNET_free (s);
}
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 GNUNET_REGEX_Transition *t_check;
+ struct GNUNET_REGEX_Transition *t_check_next;
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)
+ 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--;
- }
+ t_check_next = t_check->next;
+ if (t_check->to_state == s)
+ state_remove_transition (s_check, t_check);
}
}
- automaton_destroy_state (ss);
+ // remove state
+ GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
+ a->state_count--;
+
+ automaton_destroy_state (s);
}
}
-/**
- * Context for adding strided transitions to a DFA.
- */
-struct GNUNET_REGEX_Strided_Context
-{
- /**
- * Length of the strides.
- */
- const unsigned int stride;
-
- /**
- * Strided transitions DLL. New strided transitions will be stored in this DLL
- * and afterwards added to the DFA.
- */
- struct GNUNET_REGEX_Transition *transitions_head;
-
- /**
- * Strided transitions DLL.
- */
- struct GNUNET_REGEX_Transition *transitions_tail;
-};
-
-
-/**
- * Recursive helper function to add strides to a DFA.
- *
- * @param cls context, contains stride length and strided transitions DLL.
- * @param depth current depth of the depth-first traversal of the graph.
- * @param label current label, string that contains all labels on the path from
- * 'start' to 's'.
- * @param start start state for the depth-first traversal of the graph.
- * @param s current state in the depth-first traversal
- */
-void
-add_multi_strides_to_dfa_helper (void *cls, const unsigned int depth,
- char *label, struct GNUNET_REGEX_State *start,
- struct GNUNET_REGEX_State *s)
-{
- struct GNUNET_REGEX_Strided_Context *ctx = cls;
- struct GNUNET_REGEX_Transition *t;
- char *new_label;
-
- if (depth == ctx->stride)
- {
- t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
- t->label = GNUNET_strdup (label);
- t->to_state = s;
- t->from_state = start;
- GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
- t);
- }
- else
- {
- for (t = s->transitions_head; NULL != t; t = t->next)
- {
- /* Do not consider self-loops, because it end's up in too many
- * transitions */
- if (t->to_state == t->from_state)
- continue;
-
- if (NULL != label)
- {
- GNUNET_asprintf (&new_label, "%s%s", label, t->label);
- }
- else
- new_label = GNUNET_strdup (t->label);
-
- add_multi_strides_to_dfa_helper (cls, (depth + 1), new_label, start,
- t->to_state);
- }
- }
- GNUNET_free_non_null (label);
-}
-
-
-/**
- * Function called for each state in the DFA. Starts a traversal of depth set in
- * context starting from state 's'.
- *
- * @param cls context.
- * @param count not used.
- * @param s current state.
- */
-void
-add_multi_strides_to_dfa (void *cls, const unsigned int count,
- struct GNUNET_REGEX_State *s)
-{
- add_multi_strides_to_dfa_helper (cls, 0, NULL, s, s);
-}
-
-
-/**
- * Adds multi-strided transitions to the given 'dfa'.
- *
- * @param regex_ctx regex context needed to add transitions to the automaton.
- * @param dfa DFA to which the multi strided transitions should be added.
- * @param stride_len length of the strides.
- */
-void
-GNUNET_REGEX_add_multi_strides_to_dfa (struct GNUNET_REGEX_Context *regex_ctx,
- struct GNUNET_REGEX_Automaton *dfa,
- const unsigned int stride_len)
-{
- struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
- struct GNUNET_REGEX_Transition *t;
- struct GNUNET_REGEX_Transition *t_next;
-
- if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
- return;
-
- // Compute the new transitions.
- GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
- &add_multi_strides_to_dfa, &ctx);
-
- // Add all the new transitions to the automaton.
- for (t = ctx.transitions_head; NULL != t; t = t_next)
- {
- t_next = t->next;
- state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
- GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
- GNUNET_free_non_null (t->label);
- GNUNET_free (t);
- }
-
- dfa->is_multistrided = GNUNET_YES;
-}
-
-
-
/**
* Check if the given string 'str' needs parentheses around it when
* using it to generate a regex.
/**
- * Move from the given state 's' to the next state on transition 'label'
+ * Move from the given state 's' to the next state on transition 'str'. Consumes
+ * as much of the given 'str' as possible (usefull for strided DFAs). On return
+ * 's' will point to the next state, and the length of the substring used for
+ * this transition will be returned. If no transition possible 0 is returned and
+ * 's' points to NULL.
*
- * @param s starting state
- * @param label edge label to follow
+ * @param s starting state, will point to the next state or NULL (if no
+ * transition possible)
+ * @param str edge label to follow (will match longest common prefix)
*
- * @return new state or NULL, if transition on label not possible
+ * @return length of the substring comsumed from 'str'
*/
-static struct GNUNET_REGEX_State *
-dfa_move (struct GNUNET_REGEX_State *s, const char *label)
+static unsigned int
+dfa_move (struct GNUNET_REGEX_State **s, const char *str)
{
struct GNUNET_REGEX_Transition *t;
struct GNUNET_REGEX_State *new_s;
+ unsigned int len;
+ unsigned int max_len;
if (NULL == s)
- return NULL;
+ return 0;
new_s = NULL;
-
- for (t = s->transitions_head; NULL != t; t = t->next)
+ max_len = 0;
+ for (t = (*s)->transitions_head; NULL != t; t = t->next)
{
- // TODO: Use strstr to match substring and return number of char's that have
- // been consumed'
- if (0 == strcmp (label, t->label))
+ len = strlen (t->label);
+
+ if (0 == strncmp (t->label, str, len))
{
- new_s = t->to_state;
- break;
+ if (len >= max_len)
+ {
+ max_len = len;
+ new_s = t->to_state;
+ }
}
}
- return new_s;
+ *s = new_s;
+ return max_len;
}
/**
}
+/**
+ * Context for adding strided transitions to a DFA.
+ */
+struct GNUNET_REGEX_Strided_Context
+{
+ /**
+ * Length of the strides.
+ */
+ const unsigned int stride;
+
+ /**
+ * Strided transitions DLL. New strided transitions will be stored in this DLL
+ * and afterwards added to the DFA.
+ */
+ struct GNUNET_REGEX_Transition *transitions_head;
+
+ /**
+ * Strided transitions DLL.
+ */
+ struct GNUNET_REGEX_Transition *transitions_tail;
+};
+
+
+/**
+ * Recursive helper function to add strides to a DFA.
+ *
+ * @param cls context, contains stride length and strided transitions DLL.
+ * @param depth current depth of the depth-first traversal of the graph.
+ * @param label current label, string that contains all labels on the path from
+ * 'start' to 's'.
+ * @param start start state for the depth-first traversal of the graph.
+ * @param s current state in the depth-first traversal
+ */
+void
+dfa_add_multi_strides_helper (void *cls, const unsigned int depth, char *label,
+ struct GNUNET_REGEX_State *start,
+ struct GNUNET_REGEX_State *s)
+{
+ struct GNUNET_REGEX_Strided_Context *ctx = cls;
+ struct GNUNET_REGEX_Transition *t;
+ char *new_label;
+
+ if (depth == ctx->stride)
+ {
+ t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
+ t->label = GNUNET_strdup (label);
+ t->to_state = s;
+ t->from_state = start;
+ GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, ctx->transitions_tail,
+ t);
+ }
+ else
+ {
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ /* Do not consider self-loops, because it end's up in too many
+ * transitions */
+ if (t->to_state == t->from_state)
+ continue;
+
+ if (NULL != label)
+ {
+ GNUNET_asprintf (&new_label, "%s%s", label, t->label);
+ }
+ else
+ new_label = GNUNET_strdup (t->label);
+
+ dfa_add_multi_strides_helper (cls, (depth + 1), new_label, start,
+ t->to_state);
+ }
+ }
+ GNUNET_free_non_null (label);
+}
+
+
+/**
+ * Function called for each state in the DFA. Starts a traversal of depth set in
+ * context starting from state 's'.
+ *
+ * @param cls context.
+ * @param count not used.
+ * @param s current state.
+ */
+void
+dfa_add_multi_strides (void *cls, const unsigned int count,
+ struct GNUNET_REGEX_State *s)
+{
+ dfa_add_multi_strides_helper (cls, 0, NULL, s, s);
+}
+
+
+/**
+ * Adds multi-strided transitions to the given 'dfa'.
+ *
+ * @param regex_ctx regex context needed to add transitions to the automaton.
+ * @param dfa DFA to which the multi strided transitions should be added.
+ * @param stride_len length of the strides.
+ */
+void
+GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
+ struct GNUNET_REGEX_Automaton *dfa,
+ const unsigned int stride_len)
+{
+ struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Transition *t_next;
+
+ if (1 > stride_len || GNUNET_YES == dfa->is_multistrided)
+ return;
+
+ // Compute the new transitions of given stride_len
+ GNUNET_REGEX_automaton_traverse (dfa, dfa->start, NULL, NULL,
+ &dfa_add_multi_strides, &ctx);
+
+ // Add all the new transitions to the automaton.
+ for (t = ctx.transitions_head; NULL != t; t = t_next)
+ {
+ t_next = t->next;
+ state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
+ GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t);
+ GNUNET_free_non_null (t->label);
+ GNUNET_free (t);
+ }
+
+ // Mark this automaton as multistrided
+ dfa->is_multistrided = GNUNET_YES;
+}
+
+/**
+ * Recursive Helper function for DFA path compression. Does DFS on the DFA graph
+ * and adds new transitions to the given transitions DLL and marks states that
+ * should be removed by setting state->contained to GNUNET_YES.
+ *
+ * @param dfa DFA for which the paths should be compressed.
+ * @param start starting state for linear path search.
+ * @param cur current state in the recursive DFS.
+ * @param label current label (string of traversed labels).
+ * @param max_len maximal path compression length.
+ * @param transitions_head transitions DLL.
+ * @param transitions_tail transitions DLL.
+ */
+void
+dfa_compress_paths_helper (struct GNUNET_REGEX_Automaton *dfa,
+ struct GNUNET_REGEX_State *start,
+ struct GNUNET_REGEX_State *cur, char *label,
+ unsigned int max_len,
+ struct GNUNET_REGEX_Transition **transitions_head,
+ struct GNUNET_REGEX_Transition **transitions_tail)
+{
+ struct GNUNET_REGEX_Transition *t;
+ char *new_label;
+
+
+ if (NULL != label &&
+ ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting
+// || cur->transition_count > 1
+ || GNUNET_YES == cur->marked) || (start != dfa->start && max_len > 0 &&
+ max_len == strlen (label)) ||
+ (start == dfa->start && GNUNET_REGEX_INITIAL_BITS == strlen (label))))
+ {
+ t = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Transition));
+ t->label = GNUNET_strdup (label);
+ t->to_state = cur;
+ t->from_state = start;
+ GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t);
+
+ if (GNUNET_NO == cur->marked)
+ {
+ dfa_compress_paths_helper (dfa, cur, cur, NULL, max_len, transitions_head,
+ transitions_tail);
+ }
+ return;
+ }
+ else if (cur != start)
+ cur->contained = GNUNET_YES;
+
+ if (GNUNET_YES == cur->marked && cur != start)
+ return;
+
+ cur->marked = GNUNET_YES;
+
+
+ for (t = cur->transitions_head; NULL != t; t = t->next)
+ {
+ if (NULL != label)
+ GNUNET_asprintf (&new_label, "%s%s", label, t->label);
+ else
+ new_label = GNUNET_strdup (t->label);
+
+ if (t->to_state != cur)
+ {
+ dfa_compress_paths_helper (dfa, start, t->to_state, new_label, max_len,
+ transitions_head, transitions_tail);
+ }
+ GNUNET_free (new_label);
+ }
+}
+
+/**
+ * Compress paths in the given 'dfa'. Linear paths like 0->1->2->3 will be
+ * compressed to 0->3 by combining transitions.
+ *
+ * @param regex_ctx context for adding new transitions.
+ * @param dfa DFA representation, will directly modify the given DFA.
+ * @param max_len maximal length of the compressed paths.
+ */
+static void
+dfa_compress_paths (struct GNUNET_REGEX_Context *regex_ctx,
+ struct GNUNET_REGEX_Automaton *dfa, unsigned int max_len)
+{
+ struct GNUNET_REGEX_State *s;
+ struct GNUNET_REGEX_State *s_next;
+ struct GNUNET_REGEX_Transition *t;
+ struct GNUNET_REGEX_Transition *t_next;
+ struct GNUNET_REGEX_Transition *transitions_head = NULL;
+ struct GNUNET_REGEX_Transition *transitions_tail = NULL;
+
+ if (NULL == dfa)
+ return;
+
+ // Count the incoming transitions on each state.
+ for (s = dfa->states_head; NULL != s; s = s->next)
+ {
+ for (t = s->transitions_head; NULL != t; t = t->next)
+ {
+ if (NULL != t->to_state)
+ t->to_state->incoming_transition_count++;
+ }
+ }
+
+ // Unmark all states.
+ for (s = dfa->states_head; NULL != s; s = s->next)
+ {
+ s->marked = GNUNET_NO;
+ s->contained = GNUNET_NO;
+ }
+
+ // Add strides and mark states that can be deleted.
+ dfa_compress_paths_helper (dfa, dfa->start, dfa->start, NULL, max_len,
+ &transitions_head, &transitions_tail);
+
+ // Add all the new transitions to the automaton.
+ for (t = transitions_head; NULL != t; t = t_next)
+ {
+ t_next = t->next;
+ state_add_transition (regex_ctx, t->from_state, t->label, t->to_state);
+ GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t);
+ GNUNET_free_non_null (t->label);
+ GNUNET_free (t);
+ }
+
+ // Remove marked states (including their incoming and outgoing transitions).
+ for (s = dfa->states_head; NULL != s; s = s_next)
+ {
+ s_next = s->next;
+ if (GNUNET_YES == s->contained)
+ automaton_remove_state (dfa, s);
+ }
+}
+
+
/**
* Creates a new NFA fragment. Needs to be cleared using
* automaton_fragment_clear.
struct GNUNET_REGEX_Automaton *a;
a = ctx->stack_tail;
+
+ if (NULL == a)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "nfa_add_plus_op failed, because there was no element on the stack");
+ return;
+ }
+
GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
state_add_transition (ctx, a->end, NULL, a->start);
int atomcount;
} *p;
+ if (NULL == regex || 0 == strlen (regex) || 0 == len)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Could not parse regex. Empty regex string provided.\n");
+
+ return NULL;
+ }
+
GNUNET_REGEX_context_init (&ctx);
regexp = regex;
// Create proofs for all states
automaton_create_proofs (dfa);
+ // Compress DFA paths
+ dfa_compress_paths (&ctx, dfa, 8);
+
// Add strides to DFA
- // GNUNET_REGEX_add_multi_strides_to_dfa (&ctx, dfa, 2);
+ //GNUNET_REGEX_dfa_add_multi_strides (&ctx, dfa, 2);
return dfa;
}
GNUNET_free_non_null (a->regex);
GNUNET_free_non_null (a->canonical_regex);
- for (s = a->states_head; NULL != s;)
+ for (s = a->states_head; NULL != s; s = next_state)
{
next_state = s->next;
+ GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s);
automaton_destroy_state (s);
- s = next_state;
}
GNUNET_free (a);
evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string)
{
const char *strp;
- char str[2];
struct GNUNET_REGEX_State *s;
+ unsigned int step_len;
if (DFA != a->type)
{
if ((NULL == string || 0 == strlen (string)) && s->accepting)
return 0;
- str[1] = '\0';
- for (strp = string; NULL != strp && *strp; strp++)
+ for (strp = string; NULL != strp && *strp; strp += step_len)
{
- str[0] = *strp;
- s = dfa_move (s, str);
+ step_len = dfa_move (&s, strp);
+
if (NULL == s)
break;
}
if (NULL == a)
return 0;
- for (t_count = 0, s = a->states_head; NULL != s; s = s->next)
- {
+ t_count = 0;
+ for (s = a->states_head; NULL != s; s = s->next)
t_count += s->transition_count;
- }
return t_count;
}
{
unsigned int size;
- size = string_len < INITIAL_BITS ? string_len : INITIAL_BITS;
+ size =
+ string_len <
+ GNUNET_REGEX_INITIAL_BITS ? string_len : GNUNET_REGEX_INITIAL_BITS;
if (NULL == input_string)
{
/**
- * Recursive helper function for iterate_initial_edges. Will call iterator
- * function for each initial state.
+ * Recursive function that calls the iterator for each synthetic start state.
*
* @param min_len minimum length of the path in the graph.
* @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 state current state of the automaton.
* @param iterator iterator function called for each edge.
*/
static void
iterate_initial_edge (const unsigned int min_len, const unsigned int max_len,
- unsigned int cur_len, char *consumed_string,
- struct GNUNET_REGEX_State *state,
+ char *consumed_string, struct GNUNET_REGEX_State *state,
GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
{
unsigned int i;
struct GNUNET_REGEX_Transition *t;
unsigned int num_edges = state->transition_count;
struct GNUNET_REGEX_Edge edges[num_edges];
+ struct GNUNET_REGEX_Edge edge[1];
struct GNUNET_HashCode hash;
+ struct GNUNET_HashCode hash_new;
+
+ unsigned int cur_len;
+
+ if (NULL != consumed_string)
+ cur_len = strlen (consumed_string);
+ else
+ cur_len = 0;
- if (cur_len > min_len && NULL != consumed_string && cur_len <= max_len)
+ if ((cur_len >= min_len || GNUNET_YES == state->accepting) && cur_len > 0 &&
+ NULL != consumed_string)
{
- for (i = 0, t = state->transitions_head; NULL != t; t = t->next, i++)
+ if (cur_len <= max_len)
{
- edges[i].label = t->label;
- edges[i].destination = t->to_state->hash;
- }
+ if (state->proof != NULL && 0 != strcmp (consumed_string, state->proof))
+ {
+ for (i = 0, t = state->transitions_head; NULL != t && i < num_edges;
+ t = t->next, i++)
+ {
+ edges[i].label = t->label;
+ edges[i].destination = t->to_state->hash;
+ }
+ GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
+ iterator (iterator_cls, &hash, consumed_string, state->accepting,
+ num_edges, edges);
+ }
- GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash);
- iterator (iterator_cls, &hash, consumed_string, state->accepting, num_edges,
- edges);
+ if (GNUNET_YES == state->accepting && cur_len > 1 &&
+ state->transition_count < 1 && cur_len < max_len)
+ {
+ // Special case for regex consisting of just a string that is shorter than
+ // max_len
+ edge[0].label = &consumed_string[cur_len - 1];
+ edge[0].destination = state->hash;
+ temp = GNUNET_strdup (consumed_string);
+ temp[cur_len - 1] = '\0';
+ GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new);
+ iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
+ GNUNET_free (temp);
+ }
+ }
+ else if (max_len < cur_len)
+ {
+ // Case where the concatenated labels are longer than max_len, then split.
+ edge[0].label = &consumed_string[max_len];
+ edge[0].destination = state->hash;
+ temp = GNUNET_strdup (consumed_string);
+ temp[max_len] = '\0';
+ GNUNET_CRYPTO_hash (temp, max_len, &hash);
+ iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
+ GNUNET_free (temp);
+ }
}
if (cur_len < max_len)
{
- cur_len++;
for (t = state->transitions_head; NULL != t; t = t->next)
{
if (NULL != consumed_string)
else
GNUNET_asprintf (&temp, "%s", t->label);
- iterate_initial_edge (min_len, max_len, cur_len, temp, t->to_state,
- iterator, iterator_cls);
+ iterate_initial_edge (min_len, max_len, temp, t->to_state, iterator,
+ iterator_cls);
GNUNET_free (temp);
}
}
/**
- * 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. Iteration will start at the first state that has
- * more than one outgoing edge, i.e. the state that branches the graph.
- * For example consider the following graph:
- * a -> b -> c -> d -> ...
- * \-> e -> ...
- *
- * This function will not iterate over the edges leading to "c", because these
- * will be covered by the iterate_edges function.
+ * Iterate over all edges starting from start state of automaton 'a'. Calling
+ * iterator for each edge.
*
- * @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.
+ * @param a automaton.
+ * @param iterator iterator called for each edge.
+ * @param iterator_cls closure.
*/
void
-iterate_initial_edges (struct GNUNET_REGEX_Automaton *a,
- const unsigned int initial_len,
- GNUNET_REGEX_KeyIterator iterator, void *iterator_cls)
+GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
+ GNUNET_REGEX_KeyIterator iterator,
+ void *iterator_cls)
{
- char *consumed_string;
- char *temp;
struct GNUNET_REGEX_State *s;
- unsigned int cur_len;
- if (1 > initial_len)
- return;
+ for (s = a->states_head; NULL != s; s = s->next)
+ {
+ struct GNUNET_REGEX_Edge edges[s->transition_count];
+ unsigned int num_edges;
- consumed_string = NULL;
- s = a->start;
- cur_len = 0;
+ num_edges = state_get_edges (s, edges);
- if (1 == s->transition_count)
- {
- do
- {
- if (NULL != consumed_string)
- {
- temp = consumed_string;
- GNUNET_asprintf (&consumed_string, "%s%s", consumed_string,
- s->transitions_head->label);
- GNUNET_free (temp);
- }
- else
- GNUNET_asprintf (&consumed_string, "%s", s->transitions_head->label);
+ if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
+ iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
+ edges);
- s = s->transitions_head->to_state;
- cur_len += strlen (s->transitions_head->label);
- }
- while (cur_len < initial_len && 1 == s->transition_count);
+ s->marked = GNUNET_NO;
}
- iterate_initial_edge (cur_len, initial_len, cur_len, consumed_string, s,
- iterator, iterator_cls);
-
- GNUNET_free_non_null (consumed_string);
+ iterate_initial_edge (GNUNET_REGEX_INITIAL_BITS, GNUNET_REGEX_INITIAL_BITS,
+ NULL, a->start, iterator, iterator_cls);
}
/**
- * Iterate over all edges helper function starting from state 's', calling
- * iterator function for each edge.
+ * Create a string with binary IP notation for the given 'addr' in 'str'.
*
- * @param s state.
- * @param iterator iterator function called for each edge.
- * @param iterator_cls closure.
+ * @param af address family of the given 'addr'.
+ * @param addr address that should be converted to a string.
+ * struct in_addr * for IPv4 and struct in6_addr * for IPv6.
+ * @param str string that will contain binary notation of 'addr'. Expected
+ * to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
*/
static void
-iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator,
- void *iterator_cls)
+iptobinstr (const int af, const void *addr, char *str)
{
- struct GNUNET_REGEX_Transition *t;
- struct GNUNET_REGEX_Edge edges[s->transition_count];
- unsigned int num_edges;
+ int i;
- if (GNUNET_YES != s->marked)
+ switch (af)
{
- s->marked = GNUNET_YES;
-
- num_edges = state_get_edges (s, edges);
+ case AF_INET:
+ {
+ uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
- if ((NULL != s->proof && 0 < strlen (s->proof)) || s->accepting)
- iterator (iterator_cls, &s->hash, s->proof, s->accepting, num_edges,
- edges);
+ str[32] = '\0';
+ str += 31;
+ for (i = 31; i >= 0; i--)
+ {
+ *str = (b & 1) + '0';
+ str--;
+ b >>= 1;
+ }
+ break;
+ }
+ case AF_INET6:
+ {
+ struct in6_addr b = *(const struct in6_addr *) addr;
- for (t = s->transitions_head; NULL != t; t = t->next)
- iterate_edge (t->to_state, iterator, iterator_cls);
+ str[128] = '\0';
+ str += 127;
+ for (i = 127; i >= 0; i--)
+ {
+ *str = (b.s6_addr[i / 8] & 1) + '0';
+ str--;
+ b.s6_addr[i / 8] >>= 1;
+ }
+ break;
+ }
}
}
/**
- * Iterate over all edges starting from start state of automaton 'a'. Calling
- * iterator for each edge.
+ * Get the ipv4 network prefix from the given 'netmask'.
*
- * @param a automaton.
- * @param iterator iterator called for each edge.
- * @param iterator_cls closure.
+ * @param netmask netmask for which to get the prefix len.
+ *
+ * @return length of ipv4 prefix for 'netmask'.
+ */
+static unsigned int
+ipv4netmasktoprefixlen (const char *netmask)
+{
+ struct in_addr a;
+ unsigned int len;
+ uint32_t t;
+
+ if (1 != inet_pton (AF_INET, netmask, &a))
+ return 0;
+ len = 32;
+ for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
+ len--;
+ return len;
+}
+
+
+/**
+ * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
+ *
+ * @param ip IPv4 representation.
+ * @param netmask netmask for the ip.
+ * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
+ * bytes long.
*/
void
-GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a,
- GNUNET_REGEX_KeyIterator iterator,
- void *iterator_cls)
+GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask,
+ char *rxstr)
{
- struct GNUNET_REGEX_State *s;
+ unsigned int pfxlen;
+
+ pfxlen = ipv4netmasktoprefixlen (netmask);
+ iptobinstr (AF_INET, ip, rxstr);
+ rxstr[pfxlen] = '\0';
+ if (pfxlen < 32)
+ strcat (rxstr, "(0|1)+");
+}
- for (s = a->states_head; NULL != s; s = s->next)
- s->marked = GNUNET_NO;
- iterate_initial_edges (a, INITIAL_BITS, iterator, iterator_cls);
- iterate_edge (a->start, iterator, iterator_cls);
+/**
+ * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
+ *
+ * @param ipv6 IPv6 representation.
+ * @param prefixlen length of the ipv6 prefix.
+ * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
+ * bytes long.
+ */
+void
+GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
+ char *rxstr)
+{
+ iptobinstr (AF_INET6, ipv6, rxstr);
+ rxstr[prefixlen] = '\0';
+ if (prefixlen < 128)
+ strcat (rxstr, "(0|1)+");
}