2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file src/regex/regex_internal.h
22 * @brief common internal definitions for regex library
23 * @author Maximilian Szengel
25 #ifndef REGEX_INTERNAL_H
26 #define REGEX_INTERNAL_H
28 #include "gnunet_regex_lib.h"
33 #if 0 /* keep Emacsens' auto-indent happy */
39 * char array of literals that are allowed inside a regex (apart from the
42 #define ALLOWED_LITERALS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
46 * Transition between two states. Each state can have 0-n transitions. If label
47 * is 0, this is considered to be an epsilon transition.
49 struct GNUNET_REGEX_Transition
52 * This is a linked list.
54 struct GNUNET_REGEX_Transition *prev;
57 * This is a linked list.
59 struct GNUNET_REGEX_Transition *next;
62 * Unique id of this transition.
67 * Label for this transition. This is basically the edge label for the graph.
72 * State to which this transition leads.
74 struct GNUNET_REGEX_State *to_state;
77 * State from which this transition origins.
79 struct GNUNET_REGEX_State *from_state;
84 * A state. Can be used in DFA and NFA automatons.
86 struct GNUNET_REGEX_State
89 * This is a linked list.
91 struct GNUNET_REGEX_State *prev;
94 * This is a linked list.
96 struct GNUNET_REGEX_State *next;
104 * Unique state id that is used for traversing the automaton. It is guaranteed
105 * to be > 0 and < state_count.
107 unsigned int traversal_id;
110 * If this is an accepting state or not.
115 * Marking of the state. This is used for marking all visited states when
116 * traversing all states of an automaton and for cases where the state id
117 * cannot be used (dfa minimization).
122 * Marking the state as contained. This is used for checking, if the state is
123 * contained in a set in constant time
128 * Marking the state as part of an SCC (Strongly Connected Component). All
129 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
130 * is not a part of any SCC.
135 * Used for SCC detection.
140 * Used for SCC detection.
145 * Human readable name of the automaton. Used for debugging and graph
153 struct GNUNET_HashCode hash;
156 * Linear state ID accquired by depth-first-search. This ID should be used for
157 * storing information about the state in an array, because the 'id' of the
158 * state is not guaranteed to be linear. The 'dfs_id' is guaranteed to be > 0
159 * and < 'state_count'.
164 * Proof for this state.
169 * Number of transitions from this state to other states.
171 unsigned int transition_count;
174 * DLL of transitions.
176 struct GNUNET_REGEX_Transition *transitions_head;
179 * DLL of transitions.
181 struct GNUNET_REGEX_Transition *transitions_tail;
184 * Set of states on which this state is based on. Used when creating a DFA out
185 * of several NFA states.
187 struct GNUNET_REGEX_StateSet *nfa_set;
192 * Type of an automaton.
194 enum GNUNET_REGEX_AutomatonType
202 * Automaton representation.
204 struct GNUNET_REGEX_Automaton
207 * Linked list of NFAs used for partial NFA creation.
209 struct GNUNET_REGEX_Automaton *prev;
212 * Linked list of NFAs used for partial NFA creation.
214 struct GNUNET_REGEX_Automaton *next;
217 * First state of the automaton. This is mainly used for constructing an NFA,
218 * where each NFA itself consists of one or more NFAs linked together.
220 struct GNUNET_REGEX_State *start;
223 * End state of the partial NFA. This is undefined for DFAs
225 struct GNUNET_REGEX_State *end;
228 * Number of states in the automaton.
230 unsigned int state_count;
235 struct GNUNET_REGEX_State *states_head;
240 struct GNUNET_REGEX_State *states_tail;
243 * Type of the automaton.
245 enum GNUNET_REGEX_AutomatonType type;
253 * Canonical regex (result of RX->NFA->DFA->RX)
255 char *canonical_regex;
260 * Function that get's passed to automaton traversal and is called before each
261 * next traversal from state 's' using transition 't' to check if traversal
262 * should proceed. Return GNUNET_NO to stop traversal or GNUNET_YES to continue.
264 * @param cls closure for the check.
265 * @param s current state in the traversal.
266 * @param t current transition from state 's' that will be used for the next
269 * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop.
271 typedef int (*GNUNET_REGEX_traverse_check) (void *cls,
272 struct GNUNET_REGEX_State * s,
273 struct GNUNET_REGEX_Transition * t);
277 * Function that is called with each state, when traversing an automaton.
279 * @param cls closure.
280 * @param count current count of the state, from 0 to a->state_count -1.
283 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
284 const unsigned int count,
285 struct GNUNET_REGEX_State * s);
289 * Traverses the given automaton using depth-first-search (DFS) from it's start
290 * state, visiting all reachable states and calling 'action' on each one of
293 * @param a automaton to be traversed.
294 * @param start start state, pass a->start or NULL to traverse the whole automaton.
295 * @param check function that is checked before advancing on each transition
297 * @param check_cls closure for check.
298 * @param action action to be performed on each state.
299 * @param action_cls closure for action
302 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
303 struct GNUNET_REGEX_State *start,
304 GNUNET_REGEX_traverse_check check,
306 GNUNET_REGEX_traverse_action action,
310 * Get the canonical regex of the given automaton.
311 * When constructing the automaton a proof is computed for each state,
312 * consisting of the regular expression leading to this state. A complete
313 * regex for the automaton can be computed by combining these proofs.
314 * As of now this function is only useful for testing.
316 * @param a automaton for which the canonical regex should be returned.
318 * @return canonical regex string.
321 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a);
325 * Get the number of transitions that are contained in the given automaton.
327 * @param a automaton for which the number of transitions should be returned.
329 * @return number of transitions in the given automaton.
332 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a);
336 * Context that contains an id counter for states and transitions as well as a
337 * DLL of automatons used as a stack for NFA construction.
339 struct GNUNET_REGEX_Context
344 unsigned int state_id;
347 * Unique transition id.
349 unsigned int transition_id;
352 * DLL of GNUNET_REGEX_Automaton's used as a stack.
354 struct GNUNET_REGEX_Automaton *stack_head;
357 * DLL of GNUNET_REGEX_Automaton's used as a stack.
359 struct GNUNET_REGEX_Automaton *stack_tail;
364 * Adds multi-strided transitions to the given 'dfa'.
366 * @param regex_ctx regex context needed to add transitions to the automaton.
367 * @param dfa DFA to which the multi strided transitions should be added.
368 * @param stride_len length of the strides.
371 GNUNET_REGEX_add_multi_strides_to_dfa (struct GNUNET_REGEX_Context *regex_ctx,
372 struct GNUNET_REGEX_Automaton *dfa,
373 const unsigned int stride_len);
377 * Generate a (pseudo) random regular expression of length 'rx_length', as well
378 * as a (optional) string that will be matched by the generated regex. The
379 * returned regex needs to be freed.
381 * @param rx_length length of the random regex.
382 * @param matching_str (optional) pointer to a string that will contain a string
383 * that will be matched by the generated regex, if
384 * 'matching_str' pointer was not NULL.
386 * @return NULL if 'rx_length' is 0, a random regex of length 'rx_length', which
387 * needs to be freed, otherwise.
390 GNUNET_REGEX_generate_random_regex (size_t rx_length, char *matching_str);
394 * Generate a random string of maximum length 'max_len' that only contains literals allowed
395 * in a regular expression. The string might be 0 chars long but is garantueed
396 * to be shorter or equal to 'max_len'.
398 * @param max_len maximum length of the string that should be generated.
400 * @return random string that needs to be freed.
403 GNUNET_REGEX_generate_random_string (size_t max_len);
406 #if 0 /* keep Emacsens' auto-indent happy */