ip/prefix to regex
[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 "gnunet_regex_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. Each state can have 0-n transitions.  If label
47  * is 0, this is considered to be an epsilon transition.
48  */
49 struct GNUNET_REGEX_Transition
50 {
51   /**
52    * This is a linked list.
53    */
54   struct GNUNET_REGEX_Transition *prev;
55
56   /**
57    * This is a linked list.
58    */
59   struct GNUNET_REGEX_Transition *next;
60
61   /**
62    * Unique id of this transition.
63    */
64   unsigned int id;
65
66   /**
67    * Label for this transition. This is basically the edge label for the graph.
68    */
69   char *label;
70
71   /**
72    * State to which this transition leads.
73    */
74   struct GNUNET_REGEX_State *to_state;
75
76   /**
77    * State from which this transition origins.
78    */
79   struct GNUNET_REGEX_State *from_state;
80 };
81
82
83 /**
84  * A state. Can be used in DFA and NFA automatons.
85  */
86 struct GNUNET_REGEX_State
87 {
88   /**
89    * This is a linked list.
90    */
91   struct GNUNET_REGEX_State *prev;
92
93   /**
94    * This is a linked list.
95    */
96   struct GNUNET_REGEX_State *next;
97
98   /**
99    * Unique state id.
100    */
101   unsigned int id;
102
103   /**
104    * Unique state id that is used for traversing the automaton. It is guaranteed
105    * to be > 0 and < state_count.
106    */
107   unsigned int traversal_id;
108
109   /**
110    * If this is an accepting state or not.
111    */
112   int accepting;
113
114   /**
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).
118    */
119   int marked;
120
121   /**
122    * Marking the state as contained. This is used for checking, if the state is
123    * contained in a set in constant time.
124    */
125   int contained;
126
127   /**
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.
131    */
132   unsigned int scc_id;
133
134   /**
135    * Used for SCC detection.
136    */
137   int index;
138
139   /**
140    * Used for SCC detection.
141    */
142   int lowlink;
143
144   /**
145    * Human readable name of the automaton. Used for debugging and graph
146    * creation.
147    */
148   char *name;
149
150   /**
151    * Hash of the state.
152    */
153   struct GNUNET_HashCode hash;
154
155   /**
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'.
160    */
161   unsigned int dfs_id;
162
163   /**
164    * Proof for this state.
165    */
166   char *proof;
167
168   /**
169    * Number of transitions from this state to other states.
170    */
171   unsigned int transition_count;
172
173   /**
174    * DLL of transitions.
175    */
176   struct GNUNET_REGEX_Transition *transitions_head;
177
178   /**
179    * DLL of transitions.
180    */
181   struct GNUNET_REGEX_Transition *transitions_tail;
182
183   /**
184    * Number of incoming transitions. Used for compressing DFA paths.
185    */
186   unsigned int incoming_transition_count;
187
188   /**
189    * Set of states on which this state is based on. Used when creating a DFA out
190    * of several NFA states.
191    */
192   struct GNUNET_REGEX_StateSet *nfa_set;
193 };
194
195
196 /**
197  * Type of an automaton.
198  */
199 enum GNUNET_REGEX_AutomatonType
200 {
201   NFA,
202   DFA
203 };
204
205
206 /**
207  * Automaton representation.
208  */
209 struct GNUNET_REGEX_Automaton
210 {
211   /**
212    * Linked list of NFAs used for partial NFA creation.
213    */
214   struct GNUNET_REGEX_Automaton *prev;
215
216   /**
217    * Linked list of NFAs used for partial NFA creation.
218    */
219   struct GNUNET_REGEX_Automaton *next;
220
221   /**
222    * First state of the automaton. This is mainly used for constructing an NFA,
223    * where each NFA itself consists of one or more NFAs linked together.
224    */
225   struct GNUNET_REGEX_State *start;
226
227   /**
228    * End state of the partial NFA. This is undefined for DFAs
229    */
230   struct GNUNET_REGEX_State *end;
231
232   /**
233    * Number of states in the automaton.
234    */
235   unsigned int state_count;
236
237   /**
238    * DLL of states.
239    */
240   struct GNUNET_REGEX_State *states_head;
241
242   /**
243    * DLL of states
244    */
245   struct GNUNET_REGEX_State *states_tail;
246
247   /**
248    * Type of the automaton.
249    */
250   enum GNUNET_REGEX_AutomatonType type;
251
252   /**
253    * Regex
254    */
255   char *regex;
256
257   /**
258    * Canonical regex (result of RX->NFA->DFA->RX)
259    */
260   char *canonical_regex;
261
262   /**
263    * GNUNET_YES, if multi strides have been added to the Automaton.
264    */
265   int is_multistrided;
266 };
267
268
269 /**
270  * Function that get's passed to automaton traversal and is called before each
271  * next traversal from state 's' using transition 't' to check if traversal
272  * should proceed. Return GNUNET_NO to stop traversal or GNUNET_YES to continue.
273  *
274  * @param cls closure for the check.
275  * @param s current state in the traversal.
276  * @param t current transition from state 's' that will be used for the next
277  *          step.
278  *
279  * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop.
280  */
281 typedef int (*GNUNET_REGEX_traverse_check) (void *cls,
282                                             struct GNUNET_REGEX_State * s,
283                                             struct GNUNET_REGEX_Transition * t);
284
285
286 /**
287  * Function that is called with each state, when traversing an automaton.
288  *
289  * @param cls closure.
290  * @param count current count of the state, from 0 to a->state_count -1.
291  * @param s state.
292  */
293 typedef void (*GNUNET_REGEX_traverse_action) (void *cls,
294                                               const unsigned int count,
295                                               struct GNUNET_REGEX_State * s);
296
297
298 /**
299  * Traverses the given automaton using depth-first-search (DFS) from it's start
300  * state, visiting all reachable states and calling 'action' on each one of
301  * them.
302  *
303  * @param a automaton to be traversed.
304  * @param start start state, pass a->start or NULL to traverse the whole automaton.
305  * @param check function that is checked before advancing on each transition
306  *              in the DFS.
307  * @param check_cls closure for check.
308  * @param action action to be performed on each state.
309  * @param action_cls closure for action
310  */
311 void
312 GNUNET_REGEX_automaton_traverse (const struct GNUNET_REGEX_Automaton *a,
313                                  struct GNUNET_REGEX_State *start,
314                                  GNUNET_REGEX_traverse_check check,
315                                  void *check_cls,
316                                  GNUNET_REGEX_traverse_action action,
317                                  void *action_cls);
318
319 /**
320  * Get the canonical regex of the given automaton.
321  * When constructing the automaton a proof is computed for each state,
322  * consisting of the regular expression leading to this state. A complete
323  * regex for the automaton can be computed by combining these proofs.
324  * As of now this function is only useful for testing.
325  *
326  * @param a automaton for which the canonical regex should be returned.
327  *
328  * @return canonical regex string.
329  */
330 const char *
331 GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a);
332
333
334 /**
335  * Get the number of transitions that are contained in the given automaton.
336  *
337  * @param a automaton for which the number of transitions should be returned.
338  *
339  * @return number of transitions in the given automaton.
340  */
341 unsigned int
342 GNUNET_REGEX_get_transition_count (struct GNUNET_REGEX_Automaton *a);
343
344
345 /**
346  * Context that contains an id counter for states and transitions as well as a
347  * DLL of automatons used as a stack for NFA construction.
348  */
349 struct GNUNET_REGEX_Context
350 {
351   /**
352    * Unique state id.
353    */
354   unsigned int state_id;
355
356   /**
357    * Unique transition id.
358    */
359   unsigned int transition_id;
360
361   /**
362    * DLL of GNUNET_REGEX_Automaton's used as a stack.
363    */
364   struct GNUNET_REGEX_Automaton *stack_head;
365
366   /**
367    * DLL of GNUNET_REGEX_Automaton's used as a stack.
368    */
369   struct GNUNET_REGEX_Automaton *stack_tail;
370 };
371
372
373 /**
374  * Adds multi-strided transitions to the given 'dfa'.
375  *
376  * @param regex_ctx regex context needed to add transitions to the automaton.
377  * @param dfa DFA to which the multi strided transitions should be added.
378  * @param stride_len length of the strides.
379  */
380 void
381 GNUNET_REGEX_dfa_add_multi_strides (struct GNUNET_REGEX_Context *regex_ctx,
382                                     struct GNUNET_REGEX_Automaton *dfa,
383                                     const unsigned int stride_len);
384
385
386 /**
387  * Generate a (pseudo) random regular expression of length 'rx_length', as well
388  * as a (optional) string that will be matched by the generated regex. The
389  * returned regex needs to be freed.
390  *
391  * @param rx_length length of the random regex.
392  * @param matching_str (optional) pointer to a string that will contain a string
393  *                     that will be matched by the generated regex, if
394  *                     'matching_str' pointer was not NULL.
395  *
396  * @return NULL if 'rx_length' is 0, a random regex of length 'rx_length', which
397  *         needs to be freed, otherwise.
398  */
399 char *
400 GNUNET_REGEX_generate_random_regex (size_t rx_length, char *matching_str);
401
402
403 /**
404  * Generate a random string of maximum length 'max_len' that only contains literals allowed
405  * in a regular expression. The string might be 0 chars long but is garantueed
406  * to be shorter or equal to 'max_len'.
407  *
408  * @param max_len maximum length of the string that should be generated.
409  *
410  * @return random string that needs to be freed.
411  */
412 char *
413 GNUNET_REGEX_generate_random_string (size_t max_len);
414
415
416 #if 0                           /* keep Emacsens' auto-indent happy */
417 {
418 #endif
419 #ifdef __cplusplus
420 }
421 #endif
422
423 #endif