moving away from DEFAULTSERVICES to per-section FORCESTART, thus addressing #3565...
[oweals/gnunet.git] / src / regex / regex_internal.h
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file src/regex/regex_internal.h
22  * @brief common internal definitions for regex library.
23  * @author Maximilian Szengel
24  */
25 #ifndef REGEX_INTERNAL_H
26 #define REGEX_INTERNAL_H
27
28 #include "regex_internal_lib.h"
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 /**
39  * char array of literals that are allowed inside a regex (apart from the
40  * operators)
41  */
42 #define ALLOWED_LITERALS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
43
44
45 /**
46  * Transition between two states. Transitions are stored at the states from
47  * which they origin ('from_state'). Each state can have 0-n transitions.
48  * If label is NULL, this is considered to be an epsilon transition.
49  */
50 struct REGEX_INTERNAL_Transition
51 {
52   /**
53    * This is a linked list.
54    */
55   struct REGEX_INTERNAL_Transition *prev;
56
57   /**
58    * This is a linked list.
59    */
60   struct REGEX_INTERNAL_Transition *next;
61
62   /**
63    * Unique id of this transition.
64    */
65   unsigned int id;
66
67   /**
68    * Label for this transition. This is basically the edge label for the graph.
69    */
70   char *label;
71
72   /**
73    * State to which this transition leads.
74    */
75   struct REGEX_INTERNAL_State *to_state;
76
77   /**
78    * State from which this transition origins.
79    */
80   struct REGEX_INTERNAL_State *from_state;
81 };
82
83
84 /**
85  * A state. Can be used in DFA and NFA automatons.
86  */
87 struct REGEX_INTERNAL_State;
88
89
90 /**
91  * Set of states.
92  */
93 struct REGEX_INTERNAL_StateSet
94 {
95   /**
96    * Array of states.
97    */
98   struct REGEX_INTERNAL_State **states;
99
100   /**
101    * Number of entries in *use* in the 'states' array.
102    */
103   unsigned int off;
104
105   /**
106    * Length of the 'states' array.
107    */
108   unsigned int size;
109 };
110
111
112 /**
113  * A state. Can be used in DFA and NFA automatons.
114  */
115 struct REGEX_INTERNAL_State
116 {
117   /**
118    * This is a linked list to keep states in an automaton.
119    */
120   struct REGEX_INTERNAL_State *prev;
121
122   /**
123    * This is a linked list to keep states in an automaton.
124    */
125   struct REGEX_INTERNAL_State *next;
126
127   /**
128    * This is a multi DLL for StateSet_MDLL.
129    */
130   struct REGEX_INTERNAL_State *prev_SS;
131
132   /**
133    * This is a multi DLL for StateSet_MDLL.
134    */
135   struct REGEX_INTERNAL_State *next_SS;
136
137   /**
138    * This is a multi DLL for StateSet_MDLL Stack.
139    */
140   struct REGEX_INTERNAL_State *prev_ST;
141
142   /**
143    * This is a multi DLL for StateSet_MDLL Stack.
144    */
145   struct REGEX_INTERNAL_State *next_ST;
146
147   /**
148    * Unique state id.
149    */
150   unsigned int id;
151
152   /**
153    * Unique state id that is used for traversing the automaton. It is guaranteed
154    * to be > 0 and < state_count.
155    */
156   unsigned int traversal_id;
157
158   /**
159    * If this is an accepting state or not.
160    */
161   int accepting;
162
163   /**
164    * Marking of the state. This is used for marking all visited states when
165    * traversing all states of an automaton and for cases where the state id
166    * cannot be used (dfa minimization).
167    */
168   int marked;
169
170   /**
171    * Marking the state as contained. This is used for checking, if the state is
172    * contained in a set in constant time.
173    */
174   int contained;
175
176   /**
177    * Marking the state as part of an SCC (Strongly Connected Component).  All
178    * states with the same scc_id are part of the same SCC. scc_id is 0, if state
179    * is not a part of any SCC.
180    */
181   unsigned int scc_id;
182
183   /**
184    * Used for SCC detection.
185    */
186   int index;
187
188   /**
189    * Used for SCC detection.
190    */
191   int lowlink;
192
193   /**
194    * Human readable name of the state. Used for debugging and graph
195    * creation.
196    */
197   char *name;
198
199   /**
200    * Hash of the state.
201    */
202   struct GNUNET_HashCode hash;
203
204   /**
205    * Linear state ID accquired by depth-first-search. This ID should be used for
206    * storing information about the state in an array, because the 'id' of the
207    * state is not guaranteed to be linear. The 'dfs_id' is guaranteed to be > 0
208    * and < 'state_count'.
209    */
210   unsigned int dfs_id;
211
212   /**
213    * Proof for this state.
214    */
215   char *proof;
216
217   /**
218    * Number of transitions from this state to other states.
219    */
220   unsigned int transition_count;
221
222   /**
223    * DLL of transitions.
224    */
225   struct REGEX_INTERNAL_Transition *transitions_head;
226
227   /**
228    * DLL of transitions.
229    */
230   struct REGEX_INTERNAL_Transition *transitions_tail;
231
232   /**
233    * Number of incoming transitions. Used for compressing DFA paths.
234    */
235   unsigned int incoming_transition_count;
236
237   /**
238    * Set of states on which this state is based on. Used when creating a DFA out
239    * of several NFA states.
240    */
241   struct REGEX_INTERNAL_StateSet nfa_set;
242 };
243
244
245 /**
246  * Type of an automaton.
247  */
248 enum REGEX_INTERNAL_AutomatonType
249 {
250   NFA,
251   DFA
252 };
253
254
255 /**
256  * Automaton representation.
257  */
258 struct REGEX_INTERNAL_Automaton
259 {
260   /**
261    * Linked list of NFAs used for partial NFA creation.
262    */
263   struct REGEX_INTERNAL_Automaton *prev;
264
265   /**
266    * Linked list of NFAs used for partial NFA creation.
267    */
268   struct REGEX_INTERNAL_Automaton *next;
269
270   /**
271    * First state of the automaton. This is mainly used for constructing an NFA,
272    * where each NFA itself consists of one or more NFAs linked together.
273    */
274   struct REGEX_INTERNAL_State *start;
275
276   /**
277    * End state of the partial NFA. This is undefined for DFAs
278    */
279   struct REGEX_INTERNAL_State *end;
280
281   /**
282    * Number of states in the automaton.
283    */
284   unsigned int state_count;
285
286   /**
287    * DLL of states.
288    */
289   struct REGEX_INTERNAL_State *states_head;
290
291   /**
292    * DLL of states
293    */
294   struct REGEX_INTERNAL_State *states_tail;
295
296   /**
297    * Type of the automaton.
298    */
299   enum REGEX_INTERNAL_AutomatonType type;
300
301   /**
302    * Regex
303    */
304   char *regex;
305
306   /**
307    * Canonical regex (result of RX->NFA->DFA->RX)
308    */
309   char *canonical_regex;
310
311   /**
312    * GNUNET_YES, if multi strides have been added to the Automaton.
313    */
314   int is_multistrided;
315 };
316
317
318 /**
319  * Construct an NFA by parsing the regex string of length 'len'.
320  *
321  * @param regex regular expression string.
322  * @param len length of the string.
323  *
324  * @return NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
325  */
326 struct REGEX_INTERNAL_Automaton *
327 REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len);
328
329
330 /**
331  * Function that get's passed to automaton traversal and is called before each
332  * next traversal from state 's' using transition 't' to check if traversal
333  * should proceed. Return GNUNET_NO to stop traversal or GNUNET_YES to continue.
334  *
335  * @param cls closure for the check.
336  * @param s current state in the traversal.
337  * @param t current transition from state 's' that will be used for the next
338  *          step.
339  *
340  * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop.
341  */
342 typedef int (*REGEX_INTERNAL_traverse_check) (void *cls,
343                                             struct REGEX_INTERNAL_State * s,
344                                             struct REGEX_INTERNAL_Transition * t);
345
346
347 /**
348  * Function that is called with each state, when traversing an automaton.
349  *
350  * @param cls closure.
351  * @param count current count of the state, from 0 to a->state_count -1.
352  * @param s state.
353  */
354 typedef void (*REGEX_INTERNAL_traverse_action) (void *cls,
355                                               const unsigned int count,
356                                               struct REGEX_INTERNAL_State * s);
357
358
359 /**
360  * Traverses the given automaton using depth-first-search (DFS) from it's start
361  * state, visiting all reachable states and calling 'action' on each one of
362  * them.
363  *
364  * @param a automaton to be traversed.
365  * @param start start state, pass a->start or NULL to traverse the whole automaton.
366  * @param check function that is checked before advancing on each transition
367  *              in the DFS.
368  * @param check_cls closure for check.
369  * @param action action to be performed on each state.
370  * @param action_cls closure for action
371  */
372 void
373 REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
374                                  struct REGEX_INTERNAL_State *start,
375                                  REGEX_INTERNAL_traverse_check check,
376                                  void *check_cls,
377                                  REGEX_INTERNAL_traverse_action action,
378                                  void *action_cls);
379
380 /**
381  * Get the canonical regex of the given automaton.
382  * When constructing the automaton a proof is computed for each state,
383  * consisting of the regular expression leading to this state. A complete
384  * regex for the automaton can be computed by combining these proofs.
385  * As of now this function is only useful for testing.
386  *
387  * @param a automaton for which the canonical regex should be returned.
388  *
389  * @return canonical regex string.
390  */
391 const char *
392 REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a);
393
394
395 /**
396  * Get the number of transitions that are contained in the given automaton.
397  *
398  * @param a automaton for which the number of transitions should be returned.
399  *
400  * @return number of transitions in the given automaton.
401  */
402 unsigned int
403 REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a);
404
405
406 /**
407  * Context that contains an id counter for states and transitions as well as a
408  * DLL of automatons used as a stack for NFA construction.
409  */
410 struct REGEX_INTERNAL_Context
411 {
412   /**
413    * Unique state id.
414    */
415   unsigned int state_id;
416
417   /**
418    * Unique transition id.
419    */
420   unsigned int transition_id;
421
422   /**
423    * DLL of REGEX_INTERNAL_Automaton's used as a stack.
424    */
425   struct REGEX_INTERNAL_Automaton *stack_head;
426
427   /**
428    * DLL of REGEX_INTERNAL_Automaton's used as a stack.
429    */
430   struct REGEX_INTERNAL_Automaton *stack_tail;
431 };
432
433
434 /**
435  * Adds multi-strided transitions to the given 'dfa'.
436  *
437  * @param regex_ctx regex context needed to add transitions to the automaton.
438  * @param dfa DFA to which the multi strided transitions should be added.
439  * @param stride_len length of the strides.
440  */
441 void
442 REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
443                                     struct REGEX_INTERNAL_Automaton *dfa,
444                                     const unsigned int stride_len);
445
446
447
448 #if 0                           /* keep Emacsens' auto-indent happy */
449 {
450 #endif
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif