2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file src/regex/regex_internal.h
20 * @brief common internal definitions for regex library.
21 * @author Maximilian Szengel
23 #ifndef REGEX_INTERNAL_H
24 #define REGEX_INTERNAL_H
26 #include "regex_internal_lib.h"
31 #if 0 /* keep Emacsens' auto-indent happy */
37 * char array of literals that are allowed inside a regex (apart from the
40 #define ALLOWED_LITERALS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
44 * Transition between two states. Transitions are stored at the states from
45 * which they origin ('from_state'). Each state can have 0-n transitions.
46 * If label is NULL, this is considered to be an epsilon transition.
48 struct REGEX_INTERNAL_Transition
51 * This is a linked list.
53 struct REGEX_INTERNAL_Transition *prev;
56 * This is a linked list.
58 struct REGEX_INTERNAL_Transition *next;
61 * Unique id of this transition.
66 * Label for this transition. This is basically the edge label for the graph.
71 * State to which this transition leads.
73 struct REGEX_INTERNAL_State *to_state;
76 * State from which this transition origins.
78 struct REGEX_INTERNAL_State *from_state;
83 * A state. Can be used in DFA and NFA automatons.
85 struct REGEX_INTERNAL_State;
91 struct REGEX_INTERNAL_StateSet
96 struct REGEX_INTERNAL_State **states;
99 * Number of entries in *use* in the 'states' array.
104 * Length of the 'states' array.
111 * A state. Can be used in DFA and NFA automatons.
113 struct REGEX_INTERNAL_State
116 * This is a linked list to keep states in an automaton.
118 struct REGEX_INTERNAL_State *prev;
121 * This is a linked list to keep states in an automaton.
123 struct REGEX_INTERNAL_State *next;
126 * This is a multi DLL for StateSet_MDLL.
128 struct REGEX_INTERNAL_State *prev_SS;
131 * This is a multi DLL for StateSet_MDLL.
133 struct REGEX_INTERNAL_State *next_SS;
136 * This is a multi DLL for StateSet_MDLL Stack.
138 struct REGEX_INTERNAL_State *prev_ST;
141 * This is a multi DLL for StateSet_MDLL Stack.
143 struct REGEX_INTERNAL_State *next_ST;
151 * Unique state id that is used for traversing the automaton. It is guaranteed
152 * to be > 0 and < state_count.
154 unsigned int traversal_id;
157 * If this is an accepting state or not.
162 * Marking of the state. This is used for marking all visited states when
163 * traversing all states of an automaton and for cases where the state id
164 * cannot be used (dfa minimization).
169 * Marking the state as contained. This is used for checking, if the state is
170 * contained in a set in constant time.
175 * Marking the state as part of an SCC (Strongly Connected Component). All
176 * states with the same scc_id are part of the same SCC. scc_id is 0, if state
177 * is not a part of any SCC.
182 * Used for SCC detection.
187 * Used for SCC detection.
192 * Human readable name of the state. Used for debugging and graph
200 struct GNUNET_HashCode hash;
203 * Linear state ID accquired by depth-first-search. This ID should be used for
204 * storing information about the state in an array, because the 'id' of the
205 * state is not guaranteed to be linear. The 'dfs_id' is guaranteed to be > 0
206 * and < 'state_count'.
211 * Proof for this state.
216 * Number of transitions from this state to other states.
218 unsigned int transition_count;
221 * DLL of transitions.
223 struct REGEX_INTERNAL_Transition *transitions_head;
226 * DLL of transitions.
228 struct REGEX_INTERNAL_Transition *transitions_tail;
231 * Number of incoming transitions. Used for compressing DFA paths.
233 unsigned int incoming_transition_count;
236 * Set of states on which this state is based on. Used when creating a DFA out
237 * of several NFA states.
239 struct REGEX_INTERNAL_StateSet nfa_set;
244 * Type of an automaton.
246 enum REGEX_INTERNAL_AutomatonType
254 * Automaton representation.
256 struct REGEX_INTERNAL_Automaton
259 * Linked list of NFAs used for partial NFA creation.
261 struct REGEX_INTERNAL_Automaton *prev;
264 * Linked list of NFAs used for partial NFA creation.
266 struct REGEX_INTERNAL_Automaton *next;
269 * First state of the automaton. This is mainly used for constructing an NFA,
270 * where each NFA itself consists of one or more NFAs linked together.
272 struct REGEX_INTERNAL_State *start;
275 * End state of the partial NFA. This is undefined for DFAs
277 struct REGEX_INTERNAL_State *end;
280 * Number of states in the automaton.
282 unsigned int state_count;
287 struct REGEX_INTERNAL_State *states_head;
292 struct REGEX_INTERNAL_State *states_tail;
295 * Type of the automaton.
297 enum REGEX_INTERNAL_AutomatonType type;
305 * Canonical regex (result of RX->NFA->DFA->RX)
307 char *canonical_regex;
310 * GNUNET_YES, if multi strides have been added to the Automaton.
317 * Construct an NFA by parsing the regex string of length 'len'.
319 * @param regex regular expression string.
320 * @param len length of the string.
322 * @return NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
324 struct REGEX_INTERNAL_Automaton *
325 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len);
329 * Function that get's passed to automaton traversal and is called before each
330 * next traversal from state 's' using transition 't' to check if traversal
331 * should proceed. Return GNUNET_NO to stop traversal or GNUNET_YES to continue.
333 * @param cls closure for the check.
334 * @param s current state in the traversal.
335 * @param t current transition from state 's' that will be used for the next
338 * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop.
340 typedef int (*REGEX_INTERNAL_traverse_check) (void *cls,
341 struct REGEX_INTERNAL_State * s,
342 struct REGEX_INTERNAL_Transition * t);
346 * Function that is called with each state, when traversing an automaton.
348 * @param cls closure.
349 * @param count current count of the state, from 0 to a->state_count -1.
352 typedef void (*REGEX_INTERNAL_traverse_action) (void *cls,
353 const unsigned int count,
354 struct REGEX_INTERNAL_State * s);
358 * Traverses the given automaton using depth-first-search (DFS) from it's start
359 * state, visiting all reachable states and calling 'action' on each one of
362 * @param a automaton to be traversed.
363 * @param start start state, pass a->start or NULL to traverse the whole automaton.
364 * @param check function that is checked before advancing on each transition
366 * @param check_cls closure for check.
367 * @param action action to be performed on each state.
368 * @param action_cls closure for action
371 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
372 struct REGEX_INTERNAL_State *start,
373 REGEX_INTERNAL_traverse_check check,
375 REGEX_INTERNAL_traverse_action action,
379 * Get the canonical regex of the given automaton.
380 * When constructing the automaton a proof is computed for each state,
381 * consisting of the regular expression leading to this state. A complete
382 * regex for the automaton can be computed by combining these proofs.
383 * As of now this function is only useful for testing.
385 * @param a automaton for which the canonical regex should be returned.
387 * @return canonical regex string.
390 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a);
394 * Get the number of transitions that are contained in the given automaton.
396 * @param a automaton for which the number of transitions should be returned.
398 * @return number of transitions in the given automaton.
401 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a);
405 * Context that contains an id counter for states and transitions as well as a
406 * DLL of automatons used as a stack for NFA construction.
408 struct REGEX_INTERNAL_Context
413 unsigned int state_id;
416 * Unique transition id.
418 unsigned int transition_id;
421 * DLL of REGEX_INTERNAL_Automaton's used as a stack.
423 struct REGEX_INTERNAL_Automaton *stack_head;
426 * DLL of REGEX_INTERNAL_Automaton's used as a stack.
428 struct REGEX_INTERNAL_Automaton *stack_tail;
433 * Adds multi-strided transitions to the given 'dfa'.
435 * @param regex_ctx regex context needed to add transitions to the automaton.
436 * @param dfa DFA to which the multi strided transitions should be added.
437 * @param stride_len length of the strides.
440 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
441 struct REGEX_INTERNAL_Automaton *dfa,
442 const unsigned int stride_len);
446 #if 0 /* keep Emacsens' auto-indent happy */