continue to fix extract result
[oweals/gnunet.git] / src / regex / regex_internal_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file regex/regex_internal_lib.h
22  * @brief library to parse regular expressions into dfa
23  * @author Maximilian Szengel
24  */
25
26 #ifndef REGEX_INTERNAL_LIB_H
27 #define REGEX_INTERNAL_LIB_H
28
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "regex_block_lib.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * Automaton (NFA/DFA) representation.
45  */
46 struct REGEX_INTERNAL_Automaton;
47
48
49 /**
50  * Construct DFA for the given 'regex' of length 'len'.
51  *
52  * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
53  * compressed to o -> abc -> o. Note that this parameter influences the
54  * non-determinism of states of the resulting NFA in the DHT (number of outgoing
55  * edges with the same label). For example for an application that stores IPv4
56  * addresses as bitstrings it could make sense to limit the path compression to
57  * 4 or 8.
58  *
59  * @param regex regular expression string.
60  * @param len length of the regular expression.
61  * @param max_path_len limit the path compression length to the
62  *        given value. If set to 1, no path compression is applied. Set to 0 for
63  *        maximal possible path compression (generally not desireable).
64  * @return DFA, needs to be freed using #REGEX_INTERNAL_automaton_destroy().
65  */
66 struct REGEX_INTERNAL_Automaton *
67 REGEX_INTERNAL_construct_dfa (const char *regex,
68                               const size_t len,
69                               unsigned int max_path_len);
70
71
72 /**
73  * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton.
74  * data structure.
75  *
76  * @param a automaton to be destroyed.
77  */
78 void
79 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a);
80
81
82 /**
83  * Evaluates the given 'string' against the given compiled regex.
84  *
85  * @param a automaton.
86  * @param string string to check.
87  *
88  * @return 0 if string matches, non 0 otherwise.
89  */
90 int
91 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
92                      const char *string);
93
94
95 /**
96  * Get the first key for the given @a input_string. This hashes
97  * the first x bits of the @a input_string.
98  *
99  * @param input_string string.
100  * @param string_len length of the @a input_string.
101  * @param key pointer to where to write the hash code.
102  * @return number of bits of @a input_string that have been consumed
103  *         to construct the key
104  */
105 size_t
106 REGEX_INTERNAL_get_first_key (const char *input_string,
107                               size_t string_len,
108                               struct GNUNET_HashCode * key);
109
110
111 /**
112  * Iterator callback function.
113  *
114  * @param cls closure.
115  * @param key hash for current state.
116  * @param proof proof for current state
117  * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
118  * @param num_edges number of edges leaving current state.
119  * @param edges edges leaving current state.
120  */
121 typedef void
122 (*REGEX_INTERNAL_KeyIterator)(void *cls,
123                               const struct GNUNET_HashCode *key,
124                               const char *proof,
125                               int accepting,
126                               unsigned int num_edges,
127                               const struct REGEX_BLOCK_Edge *edges);
128
129
130 /**
131  * Iterate over all edges starting from start state of automaton 'a'. Calling
132  * iterator for each edge.
133  *
134  * @param a automaton.
135  * @param iterator iterator called for each edge.
136  * @param iterator_cls closure.
137  */
138 void
139 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
140                                   REGEX_INTERNAL_KeyIterator iterator,
141                                   void *iterator_cls);
142
143
144 /**
145  * Iterate over all edges of automaton 'a' that are reachable from a state with
146  * a proof of at least #GNUNET_REGEX_INITIAL_BYTES characters.
147  *
148  * Call the iterator for each such edge.
149  *
150  * @param a automaton.
151  * @param iterator iterator called for each reachable edge.
152  * @param iterator_cls closure.
153  */
154 void
155 REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a,
156                                         REGEX_INTERNAL_KeyIterator iterator,
157                                         void *iterator_cls);
158
159
160
161 /**
162  * Handle to store cached data about a regex announce.
163  */
164 struct REGEX_INTERNAL_Announcement;
165
166 /**
167  * Handle to store data about a regex search.
168  */
169 struct REGEX_INTERNAL_Search;
170
171
172 /**
173  * Announce a regular expression: put all states of the automaton in the DHT.
174  * Does not free resources, must call #REGEX_INTERNAL_announce_cancel() for that.
175  *
176  * @param dht An existing and valid DHT service handle. CANNOT be NULL.
177  * @param priv our private key, must remain valid until the announcement is cancelled
178  * @param regex Regular expression to announce.
179  * @param compression How many characters per edge can we squeeze?
180  * @param stats Optional statistics handle to report usage. Can be NULL.
181  * @return Handle to reuse o free cached resources.
182  *         Must be freed by calling #REGEX_INTERNAL_announce_cancel().
183  */
184 struct REGEX_INTERNAL_Announcement *
185 REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
186                          const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
187                          const char *regex,
188                          uint16_t compression,
189                          struct GNUNET_STATISTICS_Handle *stats);
190
191
192 /**
193  * Announce again a regular expression previously announced.
194  * Does use caching to speed up process.
195  *
196  * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
197  */
198 void
199 REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h);
200
201
202 /**
203  * Clear all cached data used by a regex announce.
204  * Does not close DHT connection.
205  *
206  * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
207  */
208 void
209 REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h);
210
211
212 /**
213  * Search callback function.
214  *
215  * @param cls Closure provided in #REGEX_INTERNAL_search().
216  * @param id Peer providing a regex that matches the string.
217  * @param get_path Path of the get request.
218  * @param get_path_length Length of @a get_path.
219  * @param put_path Path of the put request.
220  * @param put_path_length Length of the @a put_path.
221  */
222 typedef void
223 (*REGEX_INTERNAL_Found)(void *cls,
224                         const struct GNUNET_PeerIdentity *id,
225                         const struct GNUNET_PeerIdentity *get_path,
226                         unsigned int get_path_length,
227                         const struct GNUNET_PeerIdentity *put_path,
228                         unsigned int put_path_length);
229
230
231 /**
232  * Search for a peer offering a regex matching certain string in the DHT.
233  * The search runs until #REGEX_INTERNAL_search_cancel() is called, even if results
234  * are returned.
235  *
236  * @param dht An existing and valid DHT service handle.
237  * @param string String to match against the regexes in the DHT.
238  * @param callback Callback for found peers.
239  * @param callback_cls Closure for @c callback.
240  * @param stats Optional statistics handle to report usage. Can be NULL.
241  * @return Handle to stop search and free resources.
242  *         Must be freed by calling #REGEX_INTERNAL_search_cancel().
243  */
244 struct REGEX_INTERNAL_Search *
245 REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
246                        const char *string,
247                        REGEX_INTERNAL_Found callback,
248                        void *callback_cls,
249                        struct GNUNET_STATISTICS_Handle *stats);
250
251 /**
252  * Stop search and free all data used by a #REGEX_INTERNAL_search() call.
253  * Does not close DHT connection.
254  *
255  * @param h Handle returned by a previous #REGEX_INTERNAL_search() call.
256  */
257 void
258 REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h);
259
260
261 #if 0                           /* keep Emacsens' auto-indent happy */
262 {
263 #endif
264 #ifdef __cplusplus
265 }
266 #endif
267
268 /* end of regex_internal_lib.h */
269 #endif