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