aboutsummaryrefslogtreecommitdiff
path: root/src/regex
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/regex
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/regex')
-rw-r--r--src/regex/gnunet-daemon-regexprofiler.c330
-rw-r--r--src/regex/gnunet-regex-profiler.c1308
-rw-r--r--src/regex/gnunet-regex-simulation-profiler.c634
-rw-r--r--src/regex/gnunet-service-regex.c303
-rw-r--r--src/regex/perf-regex.c102
-rw-r--r--src/regex/plugin_block_regex.c347
-rw-r--r--src/regex/regex_api_announce.c105
-rw-r--r--src/regex/regex_api_search.c167
-rw-r--r--src/regex/regex_block_lib.c380
-rw-r--r--src/regex/regex_block_lib.h51
-rw-r--r--src/regex/regex_internal.c3107
-rw-r--r--src/regex/regex_internal.h54
-rw-r--r--src/regex/regex_internal_dht.c662
-rw-r--r--src/regex/regex_internal_lib.h60
-rw-r--r--src/regex/regex_ipc.h16
-rw-r--r--src/regex/regex_test_graph.c289
-rw-r--r--src/regex/regex_test_lib.c484
-rw-r--r--src/regex/regex_test_lib.h21
-rw-r--r--src/regex/regex_test_random.c115
-rw-r--r--src/regex/test_regex_api.c72
-rw-r--r--src/regex/test_regex_eval_api.c436
-rw-r--r--src/regex/test_regex_graph_api.c161
-rw-r--r--src/regex/test_regex_integration.c186
-rw-r--r--src/regex/test_regex_iterate_api.c314
-rw-r--r--src/regex/test_regex_proofs.c92
25 files changed, 4885 insertions, 4911 deletions
diff --git a/src/regex/gnunet-daemon-regexprofiler.c b/src/regex/gnunet-daemon-regexprofiler.c
index f1c00a554..b9c32f9c7 100644
--- a/src/regex/gnunet-daemon-regexprofiler.c
+++ b/src/regex/gnunet-daemon-regexprofiler.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/gnunet-daemon-regexprofiler.c 22 * @file regex/gnunet-daemon-regexprofiler.c
@@ -108,31 +108,31 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
108 * @param cls unused 108 * @param cls unused
109 */ 109 */
110static void 110static void
111shutdown_task (void *cls) 111shutdown_task(void *cls)
112{ 112{
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); 113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
114 114
115 if (NULL != announce_handle) 115 if (NULL != announce_handle)
116 { 116 {
117 REGEX_INTERNAL_announce_cancel (announce_handle); 117 REGEX_INTERNAL_announce_cancel(announce_handle);
118 announce_handle = NULL; 118 announce_handle = NULL;
119 } 119 }
120 if (NULL != reannounce_task) 120 if (NULL != reannounce_task)
121 { 121 {
122 GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task)); 122 GNUNET_free(GNUNET_SCHEDULER_cancel(reannounce_task));
123 reannounce_task = NULL; 123 reannounce_task = NULL;
124 } 124 }
125 if (NULL != dht_handle) 125 if (NULL != dht_handle)
126 { 126 {
127 GNUNET_DHT_disconnect (dht_handle); 127 GNUNET_DHT_disconnect(dht_handle);
128 dht_handle = NULL; 128 dht_handle = NULL;
129 } 129 }
130 GNUNET_free (my_private_key); 130 GNUNET_free(my_private_key);
131 my_private_key = NULL; 131 my_private_key = NULL;
132 132
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 133 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
134 "Daemon for %s shutting down\n", 134 "Daemon for %s shutting down\n",
135 policy_filename); 135 policy_filename);
136} 136}
137 137
138 138
@@ -142,45 +142,45 @@ shutdown_task (void *cls)
142 * @param cls Closure (regex to announce if needed). 142 * @param cls Closure (regex to announce if needed).
143 */ 143 */
144static void 144static void
145reannounce_regex (void *cls) 145reannounce_regex(void *cls)
146{ 146{
147 char *regex = cls; 147 char *regex = cls;
148 struct GNUNET_TIME_Relative random_delay; 148 struct GNUNET_TIME_Relative random_delay;
149 149
150 reannounce_task = NULL; 150 reannounce_task = NULL;
151 if (0 == rounds--) 151 if (0 == rounds--)
152 { 152 {
153 global_ret = 0; 153 global_ret = 0;
154 GNUNET_SCHEDULER_shutdown (); 154 GNUNET_SCHEDULER_shutdown();
155 GNUNET_free (regex); 155 GNUNET_free(regex);
156 return; 156 return;
157 } 157 }
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex); 158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
159 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO); 159 GNUNET_STATISTICS_update(stats_handle, "# regexes announced", 1, GNUNET_NO);
160 if (NULL == announce_handle && NULL != regex) 160 if (NULL == announce_handle && NULL != regex)
161 { 161 {
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
163 "First time, creating regex: %s\n", 163 "First time, creating regex: %s\n",
164 regex); 164 regex);
165 announce_handle = REGEX_INTERNAL_announce (dht_handle, 165 announce_handle = REGEX_INTERNAL_announce(dht_handle,
166 my_private_key, 166 my_private_key,
167 regex, 167 regex,
168 (unsigned int) max_path_compression, 168 (unsigned int)max_path_compression,
169 stats_handle); 169 stats_handle);
170 } 170 }
171 else 171 else
172 { 172 {
173 GNUNET_assert (NULL != announce_handle); 173 GNUNET_assert(NULL != announce_handle);
174 REGEX_INTERNAL_reannounce (announce_handle); 174 REGEX_INTERNAL_reannounce(announce_handle);
175 } 175 }
176 176
177 random_delay = 177 random_delay =
178 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 178 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS,
179 GNUNET_CRYPTO_random_u32 ( 179 GNUNET_CRYPTO_random_u32(
180 GNUNET_CRYPTO_QUALITY_WEAK, 180 GNUNET_CRYPTO_QUALITY_WEAK,
181 reannounce_period_max.rel_value_us)); 181 reannounce_period_max.rel_value_us));
182 reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay, 182 reannounce_task = GNUNET_SCHEDULER_add_delayed(random_delay,
183 &reannounce_regex, cls); 183 &reannounce_regex, cls);
184} 184}
185 185
186 186
@@ -191,23 +191,23 @@ reannounce_regex (void *cls)
191 * @param regex regular expression to announce on this peer's cadet. 191 * @param regex regular expression to announce on this peer's cadet.
192 */ 192 */
193static void 193static void
194announce_regex (const char *regex) 194announce_regex(const char *regex)
195{ 195{
196 char *copy; 196 char *copy;
197 197
198 if (NULL == regex || 0 == strlen (regex)) 198 if (NULL == regex || 0 == strlen(regex))
199 { 199 {
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n"); 200 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
201 return; 201 return;
202 } 202 }
203 203
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 204 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
205 "Daemon for %s starting\n", 205 "Daemon for %s starting\n",
206 policy_filename); 206 policy_filename);
207 GNUNET_assert (NULL == reannounce_task); 207 GNUNET_assert(NULL == reannounce_task);
208 copy = GNUNET_strdup (regex); 208 copy = GNUNET_strdup(regex);
209 reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex, 209 reannounce_task = GNUNET_SCHEDULER_add_now(&reannounce_regex,
210 (void *) copy); 210 (void *)copy);
211} 211}
212 212
213 213
@@ -220,16 +220,16 @@ announce_regex (const char *regex)
220 * GNUNET_NO to stop when found 220 * GNUNET_NO to stop when found
221 */ 221 */
222static int 222static int
223scan (void *cls, const char *filename) 223scan(void *cls, const char *filename)
224{ 224{
225 long n = (long) cls; 225 long n = (long)cls;
226 static long c = 0; 226 static long c = 0;
227 227
228 if (c == n) 228 if (c == n)
229 { 229 {
230 policy_filename = GNUNET_strdup (filename); 230 policy_filename = GNUNET_strdup(filename);
231 return GNUNET_NO; 231 return GNUNET_NO;
232 } 232 }
233 c++; 233 c++;
234 return GNUNET_OK; 234 return GNUNET_OK;
235} 235}
@@ -244,9 +244,9 @@ scan (void *cls, const char *filename)
244 * @param cfg_ configuration 244 * @param cfg_ configuration
245 */ 245 */
246static void 246static void
247run (void *cls, char *const *args GNUNET_UNUSED, 247run(void *cls, char *const *args GNUNET_UNUSED,
248 const char *cfgfile GNUNET_UNUSED, 248 const char *cfgfile GNUNET_UNUSED,
249 const struct GNUNET_CONFIGURATION_Handle *cfg_) 249 const struct GNUNET_CONFIGURATION_Handle *cfg_)
250{ 250{
251 char *regex = NULL; 251 char *regex = NULL;
252 char **components; 252 char **components;
@@ -255,106 +255,106 @@ run (void *cls, char *const *args GNUNET_UNUSED,
255 255
256 cfg = cfg_; 256 cfg = cfg_;
257 257
258 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 258 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
259 GNUNET_assert (NULL != my_private_key); 259 GNUNET_assert(NULL != my_private_key);
260 if (GNUNET_OK != 260 if (GNUNET_OK !=
261 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", 261 GNUNET_CONFIGURATION_get_value_number(cfg, "REGEXPROFILER",
262 "MAX_PATH_COMPRESSION", 262 "MAX_PATH_COMPRESSION",
263 &max_path_compression)) 263 &max_path_compression))
264 { 264 {
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
266 _ 266 _
267 ("%s service is lacking key configuration settings (%s). Exiting.\n"), 267 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
268 "regexprofiler", "max_path_compression"); 268 "regexprofiler", "max_path_compression");
269 global_ret = GNUNET_SYSERR; 269 global_ret = GNUNET_SYSERR;
270 GNUNET_SCHEDULER_shutdown (); 270 GNUNET_SCHEDULER_shutdown();
271 return; 271 return;
272 } 272 }
273 if (GNUNET_OK != 273 if (GNUNET_OK !=
274 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", 274 GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER",
275 "POLICY_DIR", &policy_dir)) 275 "POLICY_DIR", &policy_dir))
276 { 276 {
277 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR"); 277 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR");
278 global_ret = GNUNET_SYSERR; 278 global_ret = GNUNET_SYSERR;
279 GNUNET_SCHEDULER_shutdown (); 279 GNUNET_SCHEDULER_shutdown();
280 return; 280 return;
281 } 281 }
282 if (GNUNET_OK != 282 if (GNUNET_OK !=
283 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED", 283 GNUNET_CONFIGURATION_get_value_number(cfg, "TESTBED",
284 "PEERID", &peer_id)) 284 "PEERID", &peer_id))
285 { 285 {
286 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID"); 286 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID");
287 global_ret = GNUNET_SYSERR; 287 global_ret = GNUNET_SYSERR;
288 GNUNET_free (policy_dir); 288 GNUNET_free(policy_dir);
289 GNUNET_SCHEDULER_shutdown (); 289 GNUNET_SCHEDULER_shutdown();
290 return; 290 return;
291 } 291 }
292 292
293 if (GNUNET_OK != 293 if (GNUNET_OK !=
294 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", 294 GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER",
295 "REGEX_PREFIX", &regex_prefix)) 295 "REGEX_PREFIX", &regex_prefix))
296 { 296 {
297 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX"); 297 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX");
298 global_ret = GNUNET_SYSERR; 298 global_ret = GNUNET_SYSERR;
299 GNUNET_free (policy_dir); 299 GNUNET_free(policy_dir);
300 GNUNET_SCHEDULER_shutdown (); 300 GNUNET_SCHEDULER_shutdown();
301 return; 301 return;
302 } 302 }
303 303
304 if (GNUNET_OK != 304 if (GNUNET_OK !=
305 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER", 305 GNUNET_CONFIGURATION_get_value_time(cfg, "REGEXPROFILER",
306 "REANNOUNCE_PERIOD_MAX", 306 "REANNOUNCE_PERIOD_MAX",
307 &reannounce_period_max)) 307 &reannounce_period_max))
308 { 308 {
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 309 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
310 "reannounce_period_max not given. Using 10 minutes.\n"); 310 "reannounce_period_max not given. Using 10 minutes.\n");
311 reannounce_period_max = 311 reannounce_period_max =
312 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10); 312 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10);
313 } 313 }
314 314
315 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg); 315 stats_handle = GNUNET_STATISTICS_create("regexprofiler", cfg);
316 316
317 dht_handle = GNUNET_DHT_connect (cfg, 1); 317 dht_handle = GNUNET_DHT_connect(cfg, 1);
318 318
319 if (NULL == dht_handle) 319 if (NULL == dht_handle)
320 { 320 {
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 321 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
322 "Could not acquire dht handle. Exiting.\n"); 322 "Could not acquire dht handle. Exiting.\n");
323 global_ret = GNUNET_SYSERR; 323 global_ret = GNUNET_SYSERR;
324 GNUNET_free (policy_dir); 324 GNUNET_free(policy_dir);
325 GNUNET_SCHEDULER_shutdown (); 325 GNUNET_SCHEDULER_shutdown();
326 return; 326 return;
327 } 327 }
328 328
329 /* Read regexes from policy files */ 329 /* Read regexes from policy files */
330 GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan, 330 GNUNET_assert(-1 != GNUNET_DISK_directory_scan(policy_dir, &scan,
331 (void *) (long) peer_id)); 331 (void *)(long)peer_id));
332 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename))) 332 if (NULL == (components = REGEX_TEST_read_from_file(policy_filename)))
333 { 333 {
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 334 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
335 "Policy file %s contains no policies. Exiting.\n", 335 "Policy file %s contains no policies. Exiting.\n",
336 policy_filename); 336 policy_filename);
337 global_ret = GNUNET_SYSERR; 337 global_ret = GNUNET_SYSERR;
338 GNUNET_free (policy_dir); 338 GNUNET_free(policy_dir);
339 GNUNET_SCHEDULER_shutdown (); 339 GNUNET_SCHEDULER_shutdown();
340 return; 340 return;
341 } 341 }
342 GNUNET_free (policy_dir); 342 GNUNET_free(policy_dir);
343 regex = REGEX_TEST_combine (components, 16); 343 regex = REGEX_TEST_combine(components, 16);
344 REGEX_TEST_free_from_file (components); 344 REGEX_TEST_free_from_file(components);
345 345
346 /* Announcing regexes from policy_filename */ 346 /* Announcing regexes from policy_filename */
347 GNUNET_asprintf (&rx_with_pfx, 347 GNUNET_asprintf(&rx_with_pfx,
348 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*", 348 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
349 regex_prefix, 349 regex_prefix,
350 regex); 350 regex);
351 announce_regex (rx_with_pfx); 351 announce_regex(rx_with_pfx);
352 GNUNET_free (regex); 352 GNUNET_free(regex);
353 GNUNET_free (rx_with_pfx); 353 GNUNET_free(rx_with_pfx);
354 354
355 /* Scheduled the task to clean up when shutdown is called */ 355 /* Scheduled the task to clean up when shutdown is called */
356 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 356 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
357 NULL); 357 NULL);
358} 358}
359 359
360 360
@@ -366,19 +366,19 @@ run (void *cls, char *const *args GNUNET_UNUSED,
366 * @return 0 ok, 1 on error 366 * @return 0 ok, 1 on error
367 */ 367 */
368int 368int
369main (int argc, char *const *argv) 369main(int argc, char *const *argv)
370{ 370{
371 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 371 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END 372 GNUNET_GETOPT_OPTION_END
373 }; 373 };
374 374
375 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 375 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
376 return 2; 376 return 2;
377 return (GNUNET_OK == 377 return (GNUNET_OK ==
378 GNUNET_PROGRAM_run (argc, argv, "regexprofiler", 378 GNUNET_PROGRAM_run(argc, argv, "regexprofiler",
379 gettext_noop 379 gettext_noop
380 ("Daemon to announce regular expressions for the peer using cadet."), 380 ("Daemon to announce regular expressions for the peer using cadet."),
381 options, &run, NULL)) ? global_ret : 1; 381 options, &run, NULL)) ? global_ret : 1;
382} 382}
383 383
384 384
@@ -388,11 +388,11 @@ main (int argc, char *const *argv)
388/** 388/**
389 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 389 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
390 */ 390 */
391void __attribute__ ((constructor)) GNUNET_ARM_memory_init () 391void __attribute__ ((constructor)) GNUNET_ARM_memory_init()
392{ 392{
393 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 393 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
394 mallopt (M_TOP_PAD, 1 * 1024); 394 mallopt(M_TOP_PAD, 1 * 1024);
395 malloc_trim (0); 395 malloc_trim(0);
396} 396}
397#endif 397#endif
398 398
diff --git a/src/regex/gnunet-regex-profiler.c b/src/regex/gnunet-regex-profiler.c
index a4d4ce99b..4262107d7 100644
--- a/src/regex/gnunet-regex-profiler.c
+++ b/src/regex/gnunet-regex-profiler.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/gnunet-regex-profiler.c 22 * @file regex/gnunet-regex-profiler.c
@@ -37,13 +37,12 @@
37#include "gnunet_testbed_service.h" 37#include "gnunet_testbed_service.h"
38 38
39#define FIND_TIMEOUT \ 39#define FIND_TIMEOUT \
40 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90) 40 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
41 41
42/** 42/**
43 * DLL of operations 43 * DLL of operations
44 */ 44 */
45struct DLLOperation 45struct DLLOperation {
46{
47 /** 46 /**
48 * The testbed operation handle 47 * The testbed operation handle
49 */ 48 */
@@ -69,8 +68,7 @@ struct DLLOperation
69/** 68/**
70 * Available states during profiling 69 * Available states during profiling
71 */ 70 */
72enum State 71enum State {
73{
74 /** 72 /**
75 * Initial state 73 * Initial state
76 */ 74 */
@@ -112,8 +110,7 @@ enum State
112/** 110/**
113 * Peer handles. 111 * Peer handles.
114 */ 112 */
115struct RegexPeer 113struct RegexPeer {
116{
117 /** 114 /**
118 * Peer id. 115 * Peer id.
119 */ 116 */
@@ -150,7 +147,7 @@ struct RegexPeer
150 /** 147 /**
151 * Handle to a running regex search. 148 * Handle to a running regex search.
152 */ 149 */
153 struct REGEX_INTERNAL_Search *search_handle; 150 struct REGEX_INTERNAL_Search *search_handle;
154 151
155 /** 152 /**
156 * Testbed operation handle for DHT. 153 * Testbed operation handle for DHT.
@@ -333,8 +330,8 @@ static struct GNUNET_TIME_Relative reannounce_period_max;
333 * @param emsg error message. 330 * @param emsg error message.
334 */ 331 */
335static void 332static void
336dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, 333dht_connect_cb(void *cls, struct GNUNET_TESTBED_Operation *op,
337 void *ca_result, const char *emsg); 334 void *ca_result, const char *emsg);
338 335
339/** 336/**
340 * DHT connect adapter. 337 * DHT connect adapter.
@@ -345,7 +342,7 @@ dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
345 * @return 342 * @return
346 */ 343 */
347static void * 344static void *
348dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); 345dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
349 346
350 347
351/** 348/**
@@ -356,7 +353,7 @@ dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
356 * @param op_result service handle returned from the connect adapter 353 * @param op_result service handle returned from the connect adapter
357 */ 354 */
358static void 355static void
359dht_da (void *cls, void *op_result); 356dht_da(void *cls, void *op_result);
360 357
361 358
362/** 359/**
@@ -369,10 +366,10 @@ dht_da (void *cls, void *op_result);
369 * @param emsg error message on failure 366 * @param emsg error message on failure
370 */ 367 */
371static void 368static void
372stats_connect_cb (void *cls, 369stats_connect_cb(void *cls,
373 struct GNUNET_TESTBED_Operation *op, 370 struct GNUNET_TESTBED_Operation *op,
374 void *ca_result, 371 void *ca_result,
375 const char *emsg); 372 const char *emsg);
376 373
377 374
378/** 375/**
@@ -381,7 +378,7 @@ stats_connect_cb (void *cls,
381 * @param cls Index of the next peer in the peers array. 378 * @param cls Index of the next peer in the peers array.
382 */ 379 */
383static void 380static void
384announce_next_regex (void *cls); 381announce_next_regex(void *cls);
385 382
386 383
387/******************************************************************************/ 384/******************************************************************************/
@@ -395,7 +392,7 @@ announce_next_regex (void *cls);
395 * @param cls NULL 392 * @param cls NULL
396 */ 393 */
397static void 394static void
398do_shutdown (void *cls) 395do_shutdown(void *cls)
399{ 396{
400 struct RegexPeer *peer; 397 struct RegexPeer *peer;
401 unsigned int peer_cnt; 398 unsigned int peer_cnt;
@@ -404,75 +401,75 @@ do_shutdown (void *cls)
404 size_t size; 401 size_t size;
405 402
406 if (NULL != abort_task) 403 if (NULL != abort_task)
407 { 404 {
408 GNUNET_SCHEDULER_cancel (abort_task); 405 GNUNET_SCHEDULER_cancel(abort_task);
409 abort_task = NULL; 406 abort_task = NULL;
410 } 407 }
411 if (NULL != register_hosts_task) 408 if (NULL != register_hosts_task)
412 { 409 {
413 GNUNET_SCHEDULER_cancel (register_hosts_task); 410 GNUNET_SCHEDULER_cancel(register_hosts_task);
414 register_hosts_task = NULL; 411 register_hosts_task = NULL;
415 } 412 }
416 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++) 413 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
417 {
418 peer = &peers[peer_cnt];
419
420 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
421 { 414 {
422 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time); 415 peer = &peers[peer_cnt];
423 size = 416
424 GNUNET_snprintf (output_buffer, 417 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
425 sizeof (output_buffer), 418 {
426 "%p Search string not found: %s (%d)\n" 419 prof_time = GNUNET_TIME_absolute_get_duration(peer->prof_start_time);
427 "%p On peer: %u (%p)\n" 420 size =
428 "%p After: %s\n", 421 GNUNET_snprintf(output_buffer,
429 peer, peer->search_str, peer->search_str_matched, 422 sizeof(output_buffer),
430 peer, peer->id, peer, 423 "%p Search string not found: %s (%d)\n"
431 peer, 424 "%p On peer: %u (%p)\n"
432 GNUNET_STRINGS_relative_time_to_string (prof_time, 425 "%p After: %s\n",
433 GNUNET_NO)); 426 peer, peer->search_str, peer->search_str_matched,
434 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) 427 peer, peer->id, peer,
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); 428 peer,
429 GNUNET_STRINGS_relative_time_to_string(prof_time,
430 GNUNET_NO));
431 if (size != GNUNET_DISK_file_write(data_file, output_buffer, size))
432 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
433 }
434
435 if (NULL != peers[peer_cnt].op_handle)
436 GNUNET_TESTBED_operation_done(peers[peer_cnt].op_handle);
436 } 437 }
437 438
438 if (NULL != peers[peer_cnt].op_handle)
439 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
440 }
441
442 if (NULL != data_file) 439 if (NULL != data_file)
443 { 440 {
444 GNUNET_DISK_file_close (data_file); 441 GNUNET_DISK_file_close(data_file);
445 data_file = NULL; 442 data_file = NULL;
446 } 443 }
447 for (search_str_cnt = 0; 444 for (search_str_cnt = 0;
448 search_str_cnt < num_peers && NULL != search_strings; 445 search_str_cnt < num_peers && NULL != search_strings;
449 search_str_cnt++) 446 search_str_cnt++)
450 { 447 {
451 GNUNET_free_non_null (search_strings[search_str_cnt]); 448 GNUNET_free_non_null(search_strings[search_str_cnt]);
452 } 449 }
453 GNUNET_free_non_null (search_strings); 450 GNUNET_free_non_null(search_strings);
454 search_strings = NULL; 451 search_strings = NULL;
455 452
456 if (NULL != reg_handle) 453 if (NULL != reg_handle)
457 { 454 {
458 GNUNET_TESTBED_cancel_registration (reg_handle); 455 GNUNET_TESTBED_cancel_registration(reg_handle);
459 reg_handle = NULL; 456 reg_handle = NULL;
460 } 457 }
461 if (NULL != mc) 458 if (NULL != mc)
462 { 459 {
463 GNUNET_TESTBED_controller_disconnect (mc); 460 GNUNET_TESTBED_controller_disconnect(mc);
464 mc = NULL; 461 mc = NULL;
465 } 462 }
466 if (NULL != mc_proc) 463 if (NULL != mc_proc)
467 { 464 {
468 GNUNET_TESTBED_controller_stop (mc_proc); 465 GNUNET_TESTBED_controller_stop(mc_proc);
469 mc_proc = NULL; 466 mc_proc = NULL;
470 } 467 }
471 if (NULL != cfg) 468 if (NULL != cfg)
472 { 469 {
473 GNUNET_CONFIGURATION_destroy (cfg); 470 GNUNET_CONFIGURATION_destroy(cfg);
474 cfg = NULL; 471 cfg = NULL;
475 } 472 }
476} 473}
477 474
478 475
@@ -482,15 +479,15 @@ do_shutdown (void *cls)
482 * @param cls NULL 479 * @param cls NULL
483 */ 480 */
484static void 481static void
485do_abort (void *cls) 482do_abort(void *cls)
486{ 483{
487 unsigned long i = (unsigned long) cls; 484 unsigned long i = (unsigned long)cls;
488 485
489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 486 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
490 "Aborting from line %lu...\n", i); 487 "Aborting from line %lu...\n", i);
491 abort_task = NULL; 488 abort_task = NULL;
492 result = GNUNET_SYSERR; 489 result = GNUNET_SYSERR;
493 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 490 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
494} 491}
495 492
496 493
@@ -509,10 +506,10 @@ do_abort (void *cls)
509 * @return service handle to return in 'op_result', NULL on error 506 * @return service handle to return in 'op_result', NULL on error
510 */ 507 */
511static void * 508static void *
512stats_ca (void *cls, 509stats_ca(void *cls,
513 const struct GNUNET_CONFIGURATION_Handle *cfg) 510 const struct GNUNET_CONFIGURATION_Handle *cfg)
514{ 511{
515 return GNUNET_STATISTICS_create ("<driver>", cfg); 512 return GNUNET_STATISTICS_create("<driver>", cfg);
516} 513}
517 514
518 515
@@ -524,13 +521,13 @@ stats_ca (void *cls,
524 * @param op_result service handle returned from the connect adapter 521 * @param op_result service handle returned from the connect adapter
525 */ 522 */
526static void 523static void
527stats_da (void *cls, void *op_result) 524stats_da(void *cls, void *op_result)
528{ 525{
529 struct RegexPeer *peer = cls; 526 struct RegexPeer *peer = cls;
530 527
531 GNUNET_assert (op_result == peer->stats_handle); 528 GNUNET_assert(op_result == peer->stats_handle);
532 529
533 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO); 530 GNUNET_STATISTICS_destroy(peer->stats_handle, GNUNET_NO);
534 peer->stats_handle = NULL; 531 peer->stats_handle = NULL;
535} 532}
536 533
@@ -546,34 +543,34 @@ stats_da (void *cls, void *op_result)
546 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration 543 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
547 */ 544 */
548static int 545static int
549stats_iterator (void *cls, 546stats_iterator(void *cls,
550 const char *subsystem, 547 const char *subsystem,
551 const char *name, 548 const char *name,
552 uint64_t value, int is_persistent) 549 uint64_t value, int is_persistent)
553{ 550{
554 struct RegexPeer *peer = cls; 551 struct RegexPeer *peer = cls;
555 char output_buffer[512]; 552 char output_buffer[512];
556 size_t size; 553 size_t size;
557 554
558 if (NULL == data_file) 555 if (NULL == data_file)
559 { 556 {
560 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 557 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
561 "%p -> %s [%s]: %llu\n", 558 "%p -> %s [%s]: %llu\n",
562 peer, 559 peer,
563 subsystem, 560 subsystem,
564 name, 561 name,
565 (unsigned long long) value); 562 (unsigned long long)value);
566 return GNUNET_OK; 563 return GNUNET_OK;
567 } 564 }
568 size = 565 size =
569 GNUNET_snprintf (output_buffer, 566 GNUNET_snprintf(output_buffer,
570 sizeof (output_buffer), 567 sizeof(output_buffer),
571 "%p [%s] %llu %s\n", 568 "%p [%s] %llu %s\n",
572 peer, 569 peer,
573 subsystem, value, name); 570 subsystem, value, name);
574 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) 571 if (size != GNUNET_DISK_file_write(data_file, output_buffer, size))
575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 572 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
576 "Unable to write to file!\n"); 573 "Unable to write to file!\n");
577 574
578 return GNUNET_OK; 575 return GNUNET_OK;
579} 576}
@@ -588,48 +585,48 @@ stats_iterator (void *cls,
588 * successfully obtained, GNUNET_SYSERR if not. 585 * successfully obtained, GNUNET_SYSERR if not.
589 */ 586 */
590static void 587static void
591stats_cb (void *cls, 588stats_cb(void *cls,
592 int success) 589 int success)
593{ 590{
594 static unsigned int peer_cnt; 591 static unsigned int peer_cnt;
595 struct RegexPeer *peer = cls; 592 struct RegexPeer *peer = cls;
596 593
597 if (GNUNET_OK != success) 594 if (GNUNET_OK != success)
598 { 595 {
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 596 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
600 "Getting statistics for peer %u failed!\n", 597 "Getting statistics for peer %u failed!\n",
601 peer->id); 598 peer->id);
602 return; 599 return;
603 } 600 }
604 601
605 GNUNET_assert (NULL != peer->op_handle); 602 GNUNET_assert(NULL != peer->op_handle);
606 603
607 GNUNET_TESTBED_operation_done (peer->op_handle); 604 GNUNET_TESTBED_operation_done(peer->op_handle);
608 peer->op_handle = NULL; 605 peer->op_handle = NULL;
609 606
610 peer_cnt++; 607 peer_cnt++;
611 peer = &peers[peer_cnt]; 608 peer = &peers[peer_cnt];
612 609
613 fprintf (stderr, "s"); 610 fprintf(stderr, "s");
614 if (peer_cnt == num_peers) 611 if (peer_cnt == num_peers)
615 { 612 {
616 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 613 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
617 "\nCollecting stats finished. Shutting down.\n"); 614 "\nCollecting stats finished. Shutting down.\n");
618 GNUNET_SCHEDULER_shutdown (); 615 GNUNET_SCHEDULER_shutdown();
619 result = GNUNET_OK; 616 result = GNUNET_OK;
620 } 617 }
621 else 618 else
622 { 619 {
623 peer->op_handle = 620 peer->op_handle =
624 GNUNET_TESTBED_service_connect (NULL, 621 GNUNET_TESTBED_service_connect(NULL,
625 peer->peer_handle, 622 peer->peer_handle,
626 "statistics", 623 "statistics",
627 &stats_connect_cb, 624 &stats_connect_cb,
628 peer, 625 peer,
629 &stats_ca, 626 &stats_ca,
630 &stats_da, 627 &stats_da,
631 peer); 628 peer);
632 } 629 }
633} 630}
634 631
635 632
@@ -643,32 +640,32 @@ stats_cb (void *cls,
643 * @param emsg error message on failure 640 * @param emsg error message on failure
644 */ 641 */
645static void 642static void
646stats_connect_cb (void *cls, 643stats_connect_cb(void *cls,
647 struct GNUNET_TESTBED_Operation *op, 644 struct GNUNET_TESTBED_Operation *op,
648 void *ca_result, 645 void *ca_result,
649 const char *emsg) 646 const char *emsg)
650{ 647{
651 struct RegexPeer *peer = cls; 648 struct RegexPeer *peer = cls;
652 649
653 if (NULL == ca_result || NULL != emsg) 650 if (NULL == ca_result || NULL != emsg)
654 { 651 {
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 652 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
656 "Failed to connect to statistics service on peer %u: %s\n", 653 "Failed to connect to statistics service on peer %u: %s\n",
657 peer->id, emsg); 654 peer->id, emsg);
658 655
659 peer->stats_handle = NULL; 656 peer->stats_handle = NULL;
660 return; 657 return;
661 } 658 }
662 659
663 peer->stats_handle = ca_result; 660 peer->stats_handle = ca_result;
664 661
665 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL, 662 if (NULL == GNUNET_STATISTICS_get(peer->stats_handle, NULL, NULL,
666 &stats_cb, 663 &stats_cb,
667 &stats_iterator, peer)) 664 &stats_iterator, peer))
668 { 665 {
669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 666 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
670 "Could not get statistics of peer %u!\n", peer->id); 667 "Could not get statistics of peer %u!\n", peer->id);
671 } 668 }
672} 669}
673 670
674 671
@@ -679,21 +676,21 @@ stats_connect_cb (void *cls,
679 * @param cls NULL 676 * @param cls NULL
680 */ 677 */
681static void 678static void
682do_collect_stats (void *cls) 679do_collect_stats(void *cls)
683{ 680{
684 struct RegexPeer *peer = &peers[0]; 681 struct RegexPeer *peer = &peers[0];
685 682
686 GNUNET_assert (NULL != peer->peer_handle); 683 GNUNET_assert(NULL != peer->peer_handle);
687 684
688 peer->op_handle = 685 peer->op_handle =
689 GNUNET_TESTBED_service_connect (NULL, 686 GNUNET_TESTBED_service_connect(NULL,
690 peer->peer_handle, 687 peer->peer_handle,
691 "statistics", 688 "statistics",
692 &stats_connect_cb, 689 &stats_connect_cb,
693 peer, 690 peer,
694 &stats_ca, 691 &stats_ca,
695 &stats_da, 692 &stats_da,
696 peer); 693 peer);
697} 694}
698 695
699 696
@@ -708,7 +705,7 @@ do_collect_stats (void *cls)
708 * @param cls Index of the next peer in the peers array. 705 * @param cls Index of the next peer in the peers array.
709 */ 706 */
710static void 707static void
711find_string (void *cls); 708find_string(void *cls);
712 709
713 710
714/** 711/**
@@ -723,94 +720,94 @@ find_string (void *cls);
723 * @param put_path_length Length of the put_path. 720 * @param put_path_length Length of the put_path.
724 */ 721 */
725static void 722static void
726regex_found_handler (void *cls, 723regex_found_handler(void *cls,
727 const struct GNUNET_PeerIdentity *id, 724 const struct GNUNET_PeerIdentity *id,
728 const struct GNUNET_PeerIdentity *get_path, 725 const struct GNUNET_PeerIdentity *get_path,
729 unsigned int get_path_length, 726 unsigned int get_path_length,
730 const struct GNUNET_PeerIdentity *put_path, 727 const struct GNUNET_PeerIdentity *put_path,
731 unsigned int put_path_length) 728 unsigned int put_path_length)
732{ 729{
733 struct RegexPeer *peer = cls; 730 struct RegexPeer *peer = cls;
734 char output_buffer[512]; 731 char output_buffer[512];
735 size_t size; 732 size_t size;
736 733
737 if (GNUNET_YES == peer->search_str_matched) 734 if (GNUNET_YES == peer->search_str_matched)
738 { 735 {
739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 736 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
740 "String %s on peer %u already matched!\n", 737 "String %s on peer %u already matched!\n",
741 peer->search_str, peer->id); 738 peer->search_str, peer->id);
742 return; 739 return;
743 } 740 }
744 741
745 strings_found++; 742 strings_found++;
746 parallel_searches--; 743 parallel_searches--;
747 744
748 if (NULL != peer->timeout) 745 if (NULL != peer->timeout)
749 { 746 {
750 GNUNET_SCHEDULER_cancel (peer->timeout); 747 GNUNET_SCHEDULER_cancel(peer->timeout);
751 peer->timeout = NULL; 748 peer->timeout = NULL;
752 if (GNUNET_NO == in_shutdown) 749 if (GNUNET_NO == in_shutdown)
753 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); 750 GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL);
754 } 751 }
755 752
756 if (NULL == id) 753 if (NULL == id)
757 { 754 {
758 // FIXME not possible right now 755 // FIXME not possible right now
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 756 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
760 "String matching timed out for string %s on peer %u (%i/%i)\n", 757 "String matching timed out for string %s on peer %u (%i/%i)\n",
761 peer->search_str, peer->id, strings_found, num_peers); 758 peer->search_str, peer->id, strings_found, num_peers);
762 peer->search_str_matched = GNUNET_SYSERR; 759 peer->search_str_matched = GNUNET_SYSERR;
763 } 760 }
764 else 761 else
765 {
766 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
767
768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
769 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
770 peer->search_str, peer->id,
771 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
772 strings_found, num_peers, parallel_searches);
773
774 peer->search_str_matched = GNUNET_YES;
775
776 if (NULL != data_file)
777 { 762 {
778 size = 763 prof_time = GNUNET_TIME_absolute_get_duration(peer->prof_start_time);
779 GNUNET_snprintf (output_buffer, 764
780 sizeof (output_buffer), 765 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
781 "%p Peer: %u\n" 766 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
782 "%p Search string: %s\n" 767 peer->search_str, peer->id,
783 "%p Search duration: %s\n\n", 768 GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO),
784 peer, peer->id, 769 strings_found, num_peers, parallel_searches);
785 peer, peer->search_str, 770
786 peer, 771 peer->search_str_matched = GNUNET_YES;
787 GNUNET_STRINGS_relative_time_to_string (prof_time, 772
788 GNUNET_NO)); 773 if (NULL != data_file)
789 774 {
790 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) 775 size =
791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); 776 GNUNET_snprintf(output_buffer,
777 sizeof(output_buffer),
778 "%p Peer: %u\n"
779 "%p Search string: %s\n"
780 "%p Search duration: %s\n\n",
781 peer, peer->id,
782 peer, peer->search_str,
783 peer,
784 GNUNET_STRINGS_relative_time_to_string(prof_time,
785 GNUNET_NO));
786
787 if (size != GNUNET_DISK_file_write(data_file, output_buffer, size))
788 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
789 }
792 } 790 }
793 }
794 791
795 GNUNET_TESTBED_operation_done (peer->op_handle); 792 GNUNET_TESTBED_operation_done(peer->op_handle);
796 peer->op_handle = NULL; 793 peer->op_handle = NULL;
797 794
798 if (strings_found == num_peers) 795 if (strings_found == num_peers)
799 {
800 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802 "All strings successfully matched in %s\n",
803 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
804
805 if (NULL != search_timeout_task)
806 { 796 {
807 GNUNET_SCHEDULER_cancel (search_timeout_task); 797 prof_time = GNUNET_TIME_absolute_get_duration(prof_start_time);
808 search_timeout_task = NULL; 798 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
809 } 799 "All strings successfully matched in %s\n",
800 GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO));
801
802 if (NULL != search_timeout_task)
803 {
804 GNUNET_SCHEDULER_cancel(search_timeout_task);
805 search_timeout_task = NULL;
806 }
810 807
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n"); 808 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n");
812 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL); 809 GNUNET_SCHEDULER_add_now(&do_collect_stats, NULL);
813 } 810 }
814} 811}
815 812
816 813
@@ -821,33 +818,33 @@ regex_found_handler (void *cls,
821 * @param cls NULL 818 * @param cls NULL
822 */ 819 */
823static void 820static void
824search_timed_out (void *cls) 821search_timed_out(void *cls)
825{ 822{
826 unsigned int i; 823 unsigned int i;
827 824
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 825 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
829 "Finding matches to all strings did not succeed after %s.\n", 826 "Finding matches to all strings did not succeed after %s.\n",
830 GNUNET_STRINGS_relative_time_to_string (search_timeout_time, 827 GNUNET_STRINGS_relative_time_to_string(search_timeout_time,
831 GNUNET_NO)); 828 GNUNET_NO));
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 829 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
833 "Found %i of %i strings\n", strings_found, num_peers); 830 "Found %i of %i strings\n", strings_found, num_peers);
834 831
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 832 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
836 "Search timed out after %s." 833 "Search timed out after %s."
837 "Collecting stats and shutting down.\n", 834 "Collecting stats and shutting down.\n",
838 GNUNET_STRINGS_relative_time_to_string (search_timeout_time, 835 GNUNET_STRINGS_relative_time_to_string(search_timeout_time,
839 GNUNET_NO)); 836 GNUNET_NO));
840 837
841 in_shutdown = GNUNET_YES; 838 in_shutdown = GNUNET_YES;
842 for (i = 0; i < num_peers; i++) 839 for (i = 0; i < num_peers; i++)
843 {
844 if (NULL != peers[i].op_handle)
845 { 840 {
846 GNUNET_TESTBED_operation_done (peers[i].op_handle); 841 if (NULL != peers[i].op_handle)
847 peers[i].op_handle = NULL; 842 {
843 GNUNET_TESTBED_operation_done(peers[i].op_handle);
844 peers[i].op_handle = NULL;
845 }
848 } 846 }
849 } 847 GNUNET_SCHEDULER_add_now(&do_collect_stats, NULL);
850 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
851} 848}
852 849
853 850
@@ -858,17 +855,17 @@ search_timed_out (void *cls)
858 * @param cls Index of the next peer in the peers array. 855 * @param cls Index of the next peer in the peers array.
859 */ 856 */
860static void 857static void
861find_timed_out (void *cls) 858find_timed_out(void *cls)
862{ 859{
863 struct RegexPeer *p = cls; 860 struct RegexPeer *p = cls;
864 861
865 p->timeout = NULL; 862 p->timeout = NULL;
866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 863 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
867 "Searching for string \"%s\" on peer %d timed out.\n", 864 "Searching for string \"%s\" on peer %d timed out.\n",
868 p->search_str, 865 p->search_str,
869 p->id); 866 p->id);
870 if (GNUNET_NO == in_shutdown) 867 if (GNUNET_NO == in_shutdown)
871 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); 868 GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL);
872} 869}
873 870
874 871
@@ -878,34 +875,34 @@ find_timed_out (void *cls)
878 * @param cls Index of the next peer in the peers array. 875 * @param cls Index of the next peer in the peers array.
879 */ 876 */
880static void 877static void
881find_string (void *cls) 878find_string(void *cls)
882{ 879{
883 unsigned int search_peer = (unsigned int) (long) cls; 880 unsigned int search_peer = (unsigned int)(long)cls;
884 881
885 if ( (search_peer >= num_peers) || 882 if ((search_peer >= num_peers) ||
886 (GNUNET_YES == in_shutdown) ) 883 (GNUNET_YES == in_shutdown))
887 return; 884 return;
888 885
889 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 886 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
890 "Searching for string \"%s\" on peer %d (%u||)\n", 887 "Searching for string \"%s\" on peer %d (%u||)\n",
891 peers[search_peer].search_str, 888 peers[search_peer].search_str,
892 search_peer, 889 search_peer,
893 parallel_searches); 890 parallel_searches);
894 891
895 peers[search_peer].op_handle = 892 peers[search_peer].op_handle =
896 GNUNET_TESTBED_service_connect (NULL, 893 GNUNET_TESTBED_service_connect(NULL,
897 peers[search_peer].peer_handle, 894 peers[search_peer].peer_handle,
898 "dht", 895 "dht",
899 &dht_connect_cb, 896 &dht_connect_cb,
900 &peers[search_peer], 897 &peers[search_peer],
901 &dht_ca, 898 &dht_ca,
902 &dht_da, 899 &dht_da,
903 &peers[search_peer]); 900 &peers[search_peer]);
904 GNUNET_assert (NULL != peers[search_peer].op_handle); 901 GNUNET_assert(NULL != peers[search_peer].op_handle);
905 peers[search_peer].timeout 902 peers[search_peer].timeout
906 = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT, 903 = GNUNET_SCHEDULER_add_delayed(FIND_TIMEOUT,
907 &find_timed_out, 904 &find_timed_out,
908 &peers[search_peer]); 905 &peers[search_peer]);
909} 906}
910 907
911 908
@@ -917,44 +914,44 @@ find_string (void *cls)
917 * @param emsg NULL on success; otherwise an error description 914 * @param emsg NULL on success; otherwise an error description
918 */ 915 */
919static void 916static void
920daemon_started (void *cls, 917daemon_started(void *cls,
921 struct GNUNET_TESTBED_Operation *op, 918 struct GNUNET_TESTBED_Operation *op,
922 const char *emsg) 919 const char *emsg)
923{ 920{
924 struct RegexPeer *peer = (struct RegexPeer *) cls; 921 struct RegexPeer *peer = (struct RegexPeer *)cls;
925 unsigned long search_peer; 922 unsigned long search_peer;
926 unsigned int i; 923 unsigned int i;
927 924
928 GNUNET_TESTBED_operation_done (peer->daemon_op); 925 GNUNET_TESTBED_operation_done(peer->daemon_op);
929 peer->daemon_op = NULL; 926 peer->daemon_op = NULL;
930 if (NULL != emsg) 927 if (NULL != emsg)
931 { 928 {
932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 929 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
933 "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg); 930 "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg);
934 GNUNET_assert (0); 931 GNUNET_assert(0);
935 } 932 }
936 else 933 else
937 { 934 {
938 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 935 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
939 "Deamon %u started successfully\n", peer->id); 936 "Deamon %u started successfully\n", peer->id);
940 } 937 }
941 938
942 /* Find a peer to look for a string matching the regex announced */ 939 /* Find a peer to look for a string matching the regex announced */
943 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 940 search_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
944 num_peers); 941 num_peers);
945 for (i = 0; peers[search_peer].search_str != NULL; i++) 942 for (i = 0; peers[search_peer].search_str != NULL; i++)
946 { 943 {
947 search_peer = (search_peer + 1) % num_peers; 944 search_peer = (search_peer + 1) % num_peers;
948 if (i > num_peers) 945 if (i > num_peers)
949 GNUNET_assert (0); /* we ran out of peers, must be a bug */ 946 GNUNET_assert(0); /* we ran out of peers, must be a bug */
950 } 947 }
951 peers[search_peer].search_str = search_strings[peer->id]; 948 peers[search_peer].search_str = search_strings[peer->id];
952 peers[search_peer].search_str_matched = GNUNET_NO; 949 peers[search_peer].search_str_matched = GNUNET_NO;
953 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_saturating_multiply( 950 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_saturating_multiply(
954 reannounce_period_max, 951 reannounce_period_max,
955 2), 952 2),
956 &find_string, 953 &find_string,
957 (void *) search_peer); 954 (void *)search_peer);
958} 955}
959 956
960 957
@@ -966,21 +963,21 @@ daemon_started (void *cls,
966 * @param tc the task context 963 * @param tc the task context
967 */ 964 */
968static void 965static void
969do_announce (void *cls) 966do_announce(void *cls)
970{ 967{
971 unsigned int i; 968 unsigned int i;
972 969
973 if (GNUNET_YES == in_shutdown) 970 if (GNUNET_YES == in_shutdown)
974 return; 971 return;
975 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 972 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
976 "Starting announce.\n"); 973 "Starting announce.\n");
977 for (i = 0; i < init_parallel_searches; i++) 974 for (i = 0; i < init_parallel_searches; i++)
978 { 975 {
979 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 976 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
980 " scheduling announce %u\n", 977 " scheduling announce %u\n",
981 i); 978 i);
982 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); 979 (void)GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL);
983 } 980 }
984} 981}
985 982
986 983
@@ -990,36 +987,36 @@ do_announce (void *cls)
990 * @param cls Closure (unused). 987 * @param cls Closure (unused).
991 */ 988 */
992static void 989static void
993announce_next_regex (void *cls) 990announce_next_regex(void *cls)
994{ 991{
995 struct RegexPeer *peer; 992 struct RegexPeer *peer;
996 993
997 if (GNUNET_YES == in_shutdown) 994 if (GNUNET_YES == in_shutdown)
998 return; 995 return;
999 if (next_search >= num_peers) 996 if (next_search >= num_peers)
1000 {
1001 if (strings_found != num_peers)
1002 { 997 {
1003 struct GNUNET_TIME_Relative new_delay; 998 if (strings_found != num_peers)
1004 if (NULL != search_timeout_task) 999 {
1005 GNUNET_SCHEDULER_cancel (search_timeout_task); 1000 struct GNUNET_TIME_Relative new_delay;
1006 new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15); 1001 if (NULL != search_timeout_task)
1007 search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay, 1002 GNUNET_SCHEDULER_cancel(search_timeout_task);
1008 &search_timed_out, 1003 new_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15);
1009 NULL); 1004 search_timeout_task = GNUNET_SCHEDULER_add_delayed(new_delay,
1005 &search_timed_out,
1006 NULL);
1007 }
1008 return;
1010 } 1009 }
1011 return;
1012 }
1013 1010
1014 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search); 1011 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1015 peer = &peers[next_search]; 1012 peer = &peers[next_search];
1016 peer->daemon_op = 1013 peer->daemon_op =
1017 GNUNET_TESTBED_peer_manage_service (NULL, 1014 GNUNET_TESTBED_peer_manage_service(NULL,
1018 peer->peer_handle, 1015 peer->peer_handle,
1019 "regexprofiler", 1016 "regexprofiler",
1020 &daemon_started, 1017 &daemon_started,
1021 peer, 1018 peer,
1022 1); 1019 1);
1023 next_search++; 1020 next_search++;
1024 parallel_searches++; 1021 parallel_searches++;
1025} 1022}
@@ -1036,29 +1033,29 @@ announce_next_regex (void *cls)
1036 * @param emsg error message. 1033 * @param emsg error message.
1037 */ 1034 */
1038static void 1035static void
1039dht_connect_cb (void *cls, 1036dht_connect_cb(void *cls,
1040 struct GNUNET_TESTBED_Operation *op, 1037 struct GNUNET_TESTBED_Operation *op,
1041 void *ca_result, 1038 void *ca_result,
1042 const char *emsg) 1039 const char *emsg)
1043{ 1040{
1044 struct RegexPeer *peer = (struct RegexPeer *) cls; 1041 struct RegexPeer *peer = (struct RegexPeer *)cls;
1045 1042
1046 if (NULL != emsg || NULL == op || NULL == ca_result) 1043 if (NULL != emsg || NULL == op || NULL == ca_result)
1047 { 1044 {
1048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg); 1045 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1049 GNUNET_assert (0); 1046 GNUNET_assert(0);
1050 } 1047 }
1051 1048
1052 GNUNET_assert (NULL != peer->dht_handle); 1049 GNUNET_assert(NULL != peer->dht_handle);
1053 GNUNET_assert (peer->op_handle == op); 1050 GNUNET_assert(peer->op_handle == op);
1054 GNUNET_assert (peer->dht_handle == ca_result); 1051 GNUNET_assert(peer->dht_handle == ca_result);
1055 1052
1056 peer->search_str_matched = GNUNET_NO; 1053 peer->search_str_matched = GNUNET_NO;
1057 peer->search_handle = REGEX_INTERNAL_search (peer->dht_handle, 1054 peer->search_handle = REGEX_INTERNAL_search(peer->dht_handle,
1058 peer->search_str, 1055 peer->search_str,
1059 &regex_found_handler, peer, 1056 &regex_found_handler, peer,
1060 NULL); 1057 NULL);
1061 peer->prof_start_time = GNUNET_TIME_absolute_get (); 1058 peer->prof_start_time = GNUNET_TIME_absolute_get();
1062} 1059}
1063 1060
1064 1061
@@ -1071,11 +1068,11 @@ dht_connect_cb (void *cls,
1071 * @return 1068 * @return
1072 */ 1069 */
1073static void * 1070static void *
1074dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) 1071dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1075{ 1072{
1076 struct RegexPeer *peer = cls; 1073 struct RegexPeer *peer = cls;
1077 1074
1078 peer->dht_handle = GNUNET_DHT_connect (cfg, 32); 1075 peer->dht_handle = GNUNET_DHT_connect(cfg, 32);
1079 1076
1080 return peer->dht_handle; 1077 return peer->dht_handle;
1081} 1078}
@@ -1088,23 +1085,23 @@ dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1088 * @param op_result Service handle returned from the connect adapter. 1085 * @param op_result Service handle returned from the connect adapter.
1089 */ 1086 */
1090static void 1087static void
1091dht_da (void *cls, void *op_result) 1088dht_da(void *cls, void *op_result)
1092{ 1089{
1093 struct RegexPeer *peer = (struct RegexPeer *) cls; 1090 struct RegexPeer *peer = (struct RegexPeer *)cls;
1094 1091
1095 GNUNET_assert (peer->dht_handle == op_result); 1092 GNUNET_assert(peer->dht_handle == op_result);
1096 1093
1097 if (NULL != peer->search_handle) 1094 if (NULL != peer->search_handle)
1098 { 1095 {
1099 REGEX_INTERNAL_search_cancel (peer->search_handle); 1096 REGEX_INTERNAL_search_cancel(peer->search_handle);
1100 peer->search_handle = NULL; 1097 peer->search_handle = NULL;
1101 } 1098 }
1102 1099
1103 if (NULL != peer->dht_handle) 1100 if (NULL != peer->dht_handle)
1104 { 1101 {
1105 GNUNET_DHT_disconnect (peer->dht_handle); 1102 GNUNET_DHT_disconnect(peer->dht_handle);
1106 peer->dht_handle = NULL; 1103 peer->dht_handle = NULL;
1107 } 1104 }
1108} 1105}
1109 1106
1110 1107
@@ -1122,51 +1119,51 @@ dht_da (void *cls, void *op_result)
1122 * failed 1119 * failed
1123 */ 1120 */
1124static void 1121static void
1125test_master (void *cls, 1122test_master(void *cls,
1126 struct GNUNET_TESTBED_RunHandle *h, 1123 struct GNUNET_TESTBED_RunHandle *h,
1127 unsigned int num_peers_, 1124 unsigned int num_peers_,
1128 struct GNUNET_TESTBED_Peer **testbed_peers, 1125 struct GNUNET_TESTBED_Peer **testbed_peers,
1129 unsigned int links_succeeded, 1126 unsigned int links_succeeded,
1130 unsigned int links_failed) 1127 unsigned int links_failed)
1131{ 1128{
1132 unsigned int i; 1129 unsigned int i;
1133 1130
1134 GNUNET_assert (num_peers_ == num_peers); 1131 GNUNET_assert(num_peers_ == num_peers);
1135 1132
1136 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time); 1133 prof_time = GNUNET_TIME_absolute_get_duration(prof_start_time);
1137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1134 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1138 "Testbed started in %s\n", 1135 "Testbed started in %s\n",
1139 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO)); 1136 GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO));
1140 1137
1141 if (NULL != abort_task) 1138 if (NULL != abort_task)
1142 { 1139 {
1143 GNUNET_SCHEDULER_cancel (abort_task); 1140 GNUNET_SCHEDULER_cancel(abort_task);
1144 abort_task = NULL; 1141 abort_task = NULL;
1145 } 1142 }
1146 1143
1147 for (i = 0; i < num_peers; i++) 1144 for (i = 0; i < num_peers; i++)
1148 { 1145 {
1149 peers[i].peer_handle = testbed_peers[i]; 1146 peers[i].peer_handle = testbed_peers[i];
1150 } 1147 }
1151 if (GNUNET_NO == 1148 if (GNUNET_NO ==
1152 GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT")) 1149 GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT", "DISABLE_TRY_CONNECT"))
1153 { 1150 {
1154 struct GNUNET_TIME_Relative settle_time; 1151 struct GNUNET_TIME_Relative settle_time;
1155 1152
1156 settle_time = 1153 settle_time =
1157 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1154 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1158 10 * num_peers); 1155 10 * num_peers);
1159 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1156 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1160 "Waiting for DHT for %s to settle new connections.\n\n", 1157 "Waiting for DHT for %s to settle new connections.\n\n",
1161 GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO)); 1158 GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO));
1162 GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL); 1159 GNUNET_SCHEDULER_add_delayed(settle_time, &do_announce, NULL);
1163 } 1160 }
1164 else 1161 else
1165 { 1162 {
1166 GNUNET_SCHEDULER_add_now (&do_announce, NULL); 1163 GNUNET_SCHEDULER_add_now(&do_announce, NULL);
1167 } 1164 }
1168 search_timeout_task = 1165 search_timeout_task =
1169 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL); 1166 GNUNET_SCHEDULER_add_delayed(search_timeout_time, &search_timed_out, NULL);
1170} 1167}
1171 1168
1172/** 1169/**
@@ -1176,20 +1173,22 @@ test_master (void *cls,
1176 * @param event information on what is happening 1173 * @param event information on what is happening
1177 */ 1174 */
1178static void 1175static void
1179master_controller_cb (void *cls, 1176master_controller_cb(void *cls,
1180 const struct GNUNET_TESTBED_EventInformation *event) 1177 const struct GNUNET_TESTBED_EventInformation *event)
1181{ 1178{
1182 switch (event->type) 1179 switch (event->type)
1183 { 1180 {
1184 case GNUNET_TESTBED_ET_CONNECT: 1181 case GNUNET_TESTBED_ET_CONNECT:
1185 printf("."); 1182 printf(".");
1186 break; 1183 break;
1187 case GNUNET_TESTBED_ET_PEER_START: 1184
1188 printf("#"); 1185 case GNUNET_TESTBED_ET_PEER_START:
1189 break; 1186 printf("#");
1190 default: 1187 break;
1191 break; 1188
1192 } 1189 default:
1190 break;
1191 }
1193 fflush(stdout); 1192 fflush(stdout);
1194} 1193}
1195 1194
@@ -1209,9 +1208,9 @@ master_controller_cb (void *cls,
1209 * #GNUNET_SYSERR otherwise. 1208 * #GNUNET_SYSERR otherwise.
1210 */ 1209 */
1211static int 1210static int
1212count_and_separate_strings (char *data, 1211count_and_separate_strings(char *data,
1213 uint64_t data_size, 1212 uint64_t data_size,
1214 unsigned int str_max) 1213 unsigned int str_max)
1215{ 1214{
1216 char *buf; // Keep track of last string to skip blank lines 1215 char *buf; // Keep track of last string to skip blank lines
1217 unsigned int offset; 1216 unsigned int offset;
@@ -1220,20 +1219,20 @@ count_and_separate_strings (char *data,
1220 buf = data; 1219 buf = data;
1221 offset = 0; 1220 offset = 0;
1222 str_cnt = 0; 1221 str_cnt = 0;
1223 while ( (offset < (data_size - 1)) && (str_cnt < str_max) ) 1222 while ((offset < (data_size - 1)) && (str_cnt < str_max))
1224 {
1225 offset++;
1226 if ( ((data[offset] == '\n')) &&
1227 (buf != &data[offset]) )
1228 { 1223 {
1229 data[offset] = '\0'; 1224 offset++;
1230 str_cnt++; 1225 if (((data[offset] == '\n')) &&
1231 buf = &data[offset + 1]; 1226 (buf != &data[offset]))
1227 {
1228 data[offset] = '\0';
1229 str_cnt++;
1230 buf = &data[offset + 1];
1231 }
1232 else if ((data[offset] == '\n') ||
1233 (data[offset] == '\0'))
1234 buf = &data[offset + 1];
1232 } 1235 }
1233 else if ( (data[offset] == '\n') ||
1234 (data[offset] == '\0') )
1235 buf = &data[offset + 1];
1236 }
1237 return str_cnt; 1236 return str_cnt;
1238} 1237}
1239 1238
@@ -1252,37 +1251,37 @@ count_and_separate_strings (char *data,
1252 * In case of error @a strings must not be freed. 1251 * In case of error @a strings must not be freed.
1253 */ 1252 */
1254static int 1253static int
1255create_string_array (char *data, uint64_t data_size, 1254create_string_array(char *data, uint64_t data_size,
1256 char ***strings, unsigned int str_cnt) 1255 char ***strings, unsigned int str_cnt)
1257{ 1256{
1258 uint64_t offset; 1257 uint64_t offset;
1259 uint64_t len; 1258 uint64_t len;
1260 unsigned int i; 1259 unsigned int i;
1261 1260
1262 *strings = GNUNET_malloc (sizeof (char *) * str_cnt); 1261 *strings = GNUNET_malloc(sizeof(char *) * str_cnt);
1263 offset = 0; 1262 offset = 0;
1264 for (i = 0; i < str_cnt; i++) 1263 for (i = 0; i < str_cnt; i++)
1265 {
1266 len = strlen (&data[offset]);
1267 if (offset + len >= data_size)
1268 {
1269 GNUNET_free (*strings);
1270 *strings = NULL;
1271 return GNUNET_SYSERR;
1272 }
1273 if (0 == len) // empty line
1274 { 1264 {
1275 offset++; 1265 len = strlen(&data[offset]);
1276 i--; 1266 if (offset + len >= data_size)
1277 continue; 1267 {
1268 GNUNET_free(*strings);
1269 *strings = NULL;
1270 return GNUNET_SYSERR;
1271 }
1272 if (0 == len) // empty line
1273 {
1274 offset++;
1275 i--;
1276 continue;
1277 }
1278
1279 GNUNET_asprintf(&(*strings)[i],
1280 "%s%s",
1281 regex_prefix,
1282 &data[offset]);
1283 offset += len + 1;
1278 } 1284 }
1279
1280 GNUNET_asprintf (&(*strings)[i],
1281 "%s%s",
1282 regex_prefix,
1283 &data[offset]);
1284 offset += len + 1;
1285 }
1286 return GNUNET_OK; 1285 return GNUNET_OK;
1287} 1286}
1288 1287
@@ -1297,9 +1296,9 @@ create_string_array (char *data, uint64_t data_size,
1297 * @return number of strings found in the file. #GNUNET_SYSERR on error. 1296 * @return number of strings found in the file. #GNUNET_SYSERR on error.
1298 */ 1297 */
1299static int 1298static int
1300load_search_strings (const char *filename, 1299load_search_strings(const char *filename,
1301 char ***strings, 1300 char ***strings,
1302 unsigned int limit) 1301 unsigned int limit)
1303{ 1302{
1304 char *data; 1303 char *data;
1305 uint64_t filesize; 1304 uint64_t filesize;
@@ -1307,54 +1306,54 @@ load_search_strings (const char *filename,
1307 1306
1308 /* Sanity checks */ 1307 /* Sanity checks */
1309 if (NULL == filename) 1308 if (NULL == filename)
1310 { 1309 {
1311 return GNUNET_SYSERR; 1310 return GNUNET_SYSERR;
1312 } 1311 }
1313 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 1312 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
1314 { 1313 {
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1314 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1316 "Could not find search strings file %s\n", filename); 1315 "Could not find search strings file %s\n", filename);
1317 return GNUNET_SYSERR; 1316 return GNUNET_SYSERR;
1318 } 1317 }
1319 if (GNUNET_OK != 1318 if (GNUNET_OK !=
1320 GNUNET_DISK_file_size (filename, 1319 GNUNET_DISK_file_size(filename,
1321 &filesize, 1320 &filesize,
1322 GNUNET_YES, 1321 GNUNET_YES,
1323 GNUNET_YES)) 1322 GNUNET_YES))
1324 { 1323 {
1325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1324 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1326 "Search strings file %s cannot be read.\n", 1325 "Search strings file %s cannot be read.\n",
1327 filename); 1326 filename);
1328 return GNUNET_SYSERR; 1327 return GNUNET_SYSERR;
1329 } 1328 }
1330 if (0 == filesize) 1329 if (0 == filesize)
1331 { 1330 {
1332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1331 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1333 "Search strings file %s is empty.\n", 1332 "Search strings file %s is empty.\n",
1334 filename); 1333 filename);
1335 return GNUNET_SYSERR; 1334 return GNUNET_SYSERR;
1336 } 1335 }
1337 1336
1338 /* Read data into memory */ 1337 /* Read data into memory */
1339 data = GNUNET_malloc (filesize + 1); 1338 data = GNUNET_malloc(filesize + 1);
1340 if (filesize != GNUNET_DISK_fn_read (filename, 1339 if (filesize != GNUNET_DISK_fn_read(filename,
1341 data, 1340 data,
1342 filesize)) 1341 filesize))
1343 { 1342 {
1344 GNUNET_free (data); 1343 GNUNET_free(data);
1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1344 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1346 "Could not read search strings file %s.\n", 1345 "Could not read search strings file %s.\n",
1347 filename); 1346 filename);
1348 return GNUNET_SYSERR; 1347 return GNUNET_SYSERR;
1349 } 1348 }
1350 1349
1351 /* Process buffer and build array */ 1350 /* Process buffer and build array */
1352 str_cnt = count_and_separate_strings (data, filesize, limit); 1351 str_cnt = count_and_separate_strings(data, filesize, limit);
1353 if (GNUNET_OK != create_string_array (data, filesize, strings, str_cnt)) 1352 if (GNUNET_OK != create_string_array(data, filesize, strings, str_cnt))
1354 { 1353 {
1355 str_cnt = GNUNET_SYSERR; 1354 str_cnt = GNUNET_SYSERR;
1356 } 1355 }
1357 GNUNET_free (data); 1356 GNUNET_free(data);
1358 return str_cnt; 1357 return str_cnt;
1359} 1358}
1360 1359
@@ -1368,10 +1367,10 @@ load_search_strings (const char *filename,
1368 * @param config configuration 1367 * @param config configuration
1369 */ 1368 */
1370static void 1369static void
1371run (void *cls, 1370run(void *cls,
1372 char *const *args, 1371 char *const *args,
1373 const char *cfgfile, 1372 const char *cfgfile,
1374 const struct GNUNET_CONFIGURATION_Handle *config) 1373 const struct GNUNET_CONFIGURATION_Handle *config)
1375{ 1374{
1376 unsigned int nsearchstrs; 1375 unsigned int nsearchstrs;
1377 unsigned int i; 1376 unsigned int i;
@@ -1381,161 +1380,161 @@ run (void *cls,
1381 1380
1382 /* Check config */ 1381 /* Check config */
1383 if (NULL == config) 1382 if (NULL == config)
1384 { 1383 {
1385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1386 _("No configuration file given. Exiting\n")); 1385 _("No configuration file given. Exiting\n"));
1387 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1386 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1388 return; 1387 return;
1389 } 1388 }
1390 cfg = GNUNET_CONFIGURATION_dup (config); 1389 cfg = GNUNET_CONFIGURATION_dup(config);
1391 if (GNUNET_OK != 1390 if (GNUNET_OK !=
1392 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", 1391 GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER",
1393 "REGEX_PREFIX", 1392 "REGEX_PREFIX",
1394 &regex_prefix)) 1393 &regex_prefix))
1395 { 1394 {
1396 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 1395 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
1397 "regexprofiler", 1396 "regexprofiler",
1398 "regex_prefix"); 1397 "regex_prefix");
1399 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1398 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1400 return; 1399 return;
1401 } 1400 }
1402 if (GNUNET_OK != 1401 if (GNUNET_OK !=
1403 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", 1402 GNUNET_CONFIGURATION_get_value_number(cfg, "REGEXPROFILER",
1404 "PARALLEL_SEARCHES", 1403 "PARALLEL_SEARCHES",
1405 &init_parallel_searches)) 1404 &init_parallel_searches))
1406 { 1405 {
1407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1406 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1408 "Configuration option \"PARALLEL_SEARCHES\" missing." 1407 "Configuration option \"PARALLEL_SEARCHES\" missing."
1409 " Using default (%d)\n", 10); 1408 " Using default (%d)\n", 10);
1410 init_parallel_searches = 10; 1409 init_parallel_searches = 10;
1411 } 1410 }
1412 if (GNUNET_OK != 1411 if (GNUNET_OK !=
1413 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER", 1412 GNUNET_CONFIGURATION_get_value_time(cfg, "REGEXPROFILER",
1414 "REANNOUNCE_PERIOD_MAX", 1413 "REANNOUNCE_PERIOD_MAX",
1415 &reannounce_period_max)) 1414 &reannounce_period_max))
1416 { 1415 {
1417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1416 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1418 "reannounce_period_max not given. Using 10 minutes.\n"); 1417 "reannounce_period_max not given. Using 10 minutes.\n");
1419 reannounce_period_max = 1418 reannounce_period_max =
1420 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10); 1419 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10);
1421 } 1420 }
1422 1421
1423 /* Check arguments */ 1422 /* Check arguments */
1424 if (NULL == policy_dir) 1423 if (NULL == policy_dir)
1425 { 1424 {
1426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1427 _("No policy directory specified on command line. Exiting.\n")); 1426 _("No policy directory specified on command line. Exiting.\n"));
1428 return; 1427 return;
1429 } 1428 }
1430 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES)) 1429 if (GNUNET_YES != GNUNET_DISK_directory_test(policy_dir, GNUNET_YES))
1431 { 1430 {
1432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1431 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1433 _("Specified policies directory does not exist. Exiting.\n")); 1432 _("Specified policies directory does not exist. Exiting.\n"));
1434 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1433 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1435 return; 1434 return;
1436 } 1435 }
1437 if (0 >= (int) (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL))) 1436 if (0 >= (int)(num_peers = GNUNET_DISK_directory_scan(policy_dir, NULL, NULL)))
1438 { 1437 {
1439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1438 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1440 _("No files found in `%s'\n"), 1439 _("No files found in `%s'\n"),
1441 policy_dir); 1440 policy_dir);
1442 return; 1441 return;
1443 } 1442 }
1444 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER", 1443 GNUNET_CONFIGURATION_set_value_string(cfg, "REGEXPROFILER",
1445 "POLICY_DIR", policy_dir); 1444 "POLICY_DIR", policy_dir);
1446 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file)) 1445 if (GNUNET_YES != GNUNET_DISK_file_test(strings_file))
1447 { 1446 {
1448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1447 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1449 _("No search strings file given. Exiting.\n")); 1448 _("No search strings file given. Exiting.\n"));
1450 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1449 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1451 return; 1450 return;
1452 } 1451 }
1453 nsearchstrs = load_search_strings (strings_file, 1452 nsearchstrs = load_search_strings(strings_file,
1454 &search_strings, 1453 &search_strings,
1455 num_peers); 1454 num_peers);
1456 if (num_peers != nsearchstrs) 1455 if (num_peers != nsearchstrs)
1457 { 1456 {
1458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1457 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1459 "Error loading search strings.\n"); 1458 "Error loading search strings.\n");
1460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1459 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1461 "File (%s) does not contain enough strings (%u/%u).\n", 1460 "File (%s) does not contain enough strings (%u/%u).\n",
1462 strings_file, nsearchstrs, num_peers); 1461 strings_file, nsearchstrs, num_peers);
1463 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1462 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1464 return; 1463 return;
1465 } 1464 }
1466 if ( (0 == num_peers) || (NULL == search_strings)) 1465 if ((0 == num_peers) || (NULL == search_strings))
1467 { 1466 {
1468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1467 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1469 _("Error loading search strings. Exiting.\n")); 1468 _("Error loading search strings. Exiting.\n"));
1470 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); 1469 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL);
1471 return; 1470 return;
1472 } 1471 }
1473 for (i = 0; i < num_peers; i++) 1472 for (i = 0; i < num_peers; i++)
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1473 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1475 "search string: %s\n", 1474 "search string: %s\n",
1476 search_strings[i]); 1475 search_strings[i]);
1477 1476
1478 /* Check logfile */ 1477 /* Check logfile */
1479 if ( (NULL != data_filename) && 1478 if ((NULL != data_filename) &&
1480 (NULL == (data_file = 1479 (NULL == (data_file =
1481 GNUNET_DISK_file_open (data_filename, 1480 GNUNET_DISK_file_open(data_filename,
1482 GNUNET_DISK_OPEN_READWRITE | 1481 GNUNET_DISK_OPEN_READWRITE |
1483 GNUNET_DISK_OPEN_TRUNCATE | 1482 GNUNET_DISK_OPEN_TRUNCATE |
1484 GNUNET_DISK_OPEN_CREATE, 1483 GNUNET_DISK_OPEN_CREATE,
1485 GNUNET_DISK_PERM_USER_READ | 1484 GNUNET_DISK_PERM_USER_READ |
1486 GNUNET_DISK_PERM_USER_WRITE))) ) 1485 GNUNET_DISK_PERM_USER_WRITE))))
1487 { 1486 {
1488 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 1487 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR,
1489 "open", 1488 "open",
1490 data_filename); 1489 data_filename);
1491 return; 1490 return;
1492 } 1491 }
1493 1492
1494 /* Initialize peers */ 1493 /* Initialize peers */
1495 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers); 1494 peers = GNUNET_malloc(sizeof(struct RegexPeer) * num_peers);
1496 for (i = 0; i < num_peers; i++) 1495 for (i = 0; i < num_peers; i++)
1497 peers[i].id = i; 1496 peers[i].id = i;
1498 1497
1499 GNUNET_CONFIGURATION_set_value_number (cfg, 1498 GNUNET_CONFIGURATION_set_value_number(cfg,
1500 "TESTBED", "OVERLAY_RANDOM_LINKS", 1499 "TESTBED", "OVERLAY_RANDOM_LINKS",
1501 num_peers * 20); 1500 num_peers * 20);
1502 GNUNET_CONFIGURATION_set_value_number (cfg, 1501 GNUNET_CONFIGURATION_set_value_number(cfg,
1503 "DHT", "FORCE_NSE", 1502 "DHT", "FORCE_NSE",
1504 (long long unsigned) 1503 (long long unsigned)
1505 (log (num_peers) / log (2.0))); 1504 (log(num_peers) / log(2.0)));
1506 event_mask = 0LL; 1505 event_mask = 0LL;
1507/* For feedback about the start process activate these and pass master_cb */ 1506/* For feedback about the start process activate these and pass master_cb */
1508 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START); 1507 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1509// event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP); 1508// event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1510 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT); 1509 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1511// event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT); 1510// event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1512 prof_start_time = GNUNET_TIME_absolute_get (); 1511 prof_start_time = GNUNET_TIME_absolute_get();
1513 GNUNET_TESTBED_run (hosts_file, 1512 GNUNET_TESTBED_run(hosts_file,
1514 cfg, 1513 cfg,
1515 num_peers, 1514 num_peers,
1516 event_mask, 1515 event_mask,
1517 &master_controller_cb, 1516 &master_controller_cb,
1518 NULL, /* master_controller_cb cls */ 1517 NULL, /* master_controller_cb cls */
1519 &test_master, 1518 &test_master,
1520 NULL); /* test_master cls */ 1519 NULL); /* test_master cls */
1521 if (GNUNET_OK != 1520 if (GNUNET_OK !=
1522 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED", 1521 GNUNET_CONFIGURATION_get_value_time(cfg, "TESTBED",
1523 "SETUP_TIMEOUT", 1522 "SETUP_TIMEOUT",
1524 &abort_time)) 1523 &abort_time))
1525 { 1524 {
1526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1525 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1527 "SETUP_TIMEOUT not given. Using 15 minutes.\n"); 1526 "SETUP_TIMEOUT not given. Using 15 minutes.\n");
1528 abort_time = 1527 abort_time =
1529 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15); 1528 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15);
1530 } 1529 }
1531 abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES); 1530 abort_time = GNUNET_TIME_relative_add(abort_time, GNUNET_TIME_UNIT_MINUTES);
1532 abort_task = 1531 abort_task =
1533 GNUNET_SCHEDULER_add_delayed (abort_time, 1532 GNUNET_SCHEDULER_add_delayed(abort_time,
1534 &do_abort, 1533 &do_abort,
1535 (void*) __LINE__); 1534 (void*)__LINE__);
1536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1535 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1537 "setup_timeout: %s\n", 1536 "setup_timeout: %s\n",
1538 GNUNET_STRINGS_relative_time_to_string (abort_time, GNUNET_YES)); 1537 GNUNET_STRINGS_relative_time_to_string(abort_time, GNUNET_YES));
1539} 1538}
1540 1539
1541 1540
@@ -1547,53 +1546,52 @@ run (void *cls,
1547 * @return 0 on success 1546 * @return 0 on success
1548 */ 1547 */
1549int 1548int
1550main (int argc, char *const *argv) 1549main(int argc, char *const *argv)
1551{ 1550{
1552 struct GNUNET_GETOPT_CommandLineOption options[] = { 1551 struct GNUNET_GETOPT_CommandLineOption options[] = {
1553 1552 GNUNET_GETOPT_option_filename('o',
1554 GNUNET_GETOPT_option_filename ('o', 1553 "output-file",
1555 "output-file", 1554 "FILENAME",
1556 "FILENAME", 1555 gettext_noop("name of the file for writing statistics"),
1557 gettext_noop ("name of the file for writing statistics"), 1556 &data_filename),
1558 &data_filename), 1557
1559 1558 GNUNET_GETOPT_option_relative_time('t',
1560 GNUNET_GETOPT_option_relative_time ('t', 1559 "matching-timeout",
1561 "matching-timeout", 1560 "TIMEOUT",
1562 "TIMEOUT", 1561 gettext_noop("wait TIMEOUT before ending the experiment"),
1563 gettext_noop ("wait TIMEOUT before ending the experiment"), 1562 &search_timeout_time),
1564 &search_timeout_time), 1563
1565 1564 GNUNET_GETOPT_option_filename('p',
1566 GNUNET_GETOPT_option_filename ('p', 1565 "policy-dir",
1567 "policy-dir", 1566 "DIRECTORY",
1568 "DIRECTORY", 1567 gettext_noop("directory with policy files"),
1569 gettext_noop ("directory with policy files"), 1568 &policy_dir),
1570 &policy_dir), 1569
1571 1570
1572 1571 GNUNET_GETOPT_option_filename('s',
1573 GNUNET_GETOPT_option_filename ('s', 1572 "strings-file",
1574 "strings-file", 1573 "FILENAME",
1575 "FILENAME", 1574 gettext_noop("name of file with input strings"),
1576 gettext_noop ("name of file with input strings"), 1575 &strings_file),
1577 &strings_file), 1576
1578 1577 GNUNET_GETOPT_option_filename('H',
1579 GNUNET_GETOPT_option_filename ('H', 1578 "hosts-file",
1580 "hosts-file", 1579 "FILENAME",
1581 "FILENAME", 1580 gettext_noop("name of file with hosts' names"),
1582 gettext_noop ("name of file with hosts' names"), 1581 &hosts_file),
1583 &hosts_file),
1584 1582
1585 GNUNET_GETOPT_OPTION_END 1583 GNUNET_GETOPT_OPTION_END
1586 }; 1584 };
1587 int ret; 1585 int ret;
1588 1586
1589 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 1587 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
1590 return 2; 1588 return 2;
1591 result = GNUNET_SYSERR; 1589 result = GNUNET_SYSERR;
1592 ret = 1590 ret =
1593 GNUNET_PROGRAM_run (argc, argv, 1591 GNUNET_PROGRAM_run(argc, argv,
1594 "gnunet-regex-profiler", 1592 "gnunet-regex-profiler",
1595 _("Profiler for regex"), 1593 _("Profiler for regex"),
1596 options, &run, NULL); 1594 options, &run, NULL);
1597 if (GNUNET_OK != ret) 1595 if (GNUNET_OK != ret)
1598 return ret; 1596 return ret;
1599 if (GNUNET_OK != result) 1597 if (GNUNET_OK != result)
diff --git a/src/regex/gnunet-regex-simulation-profiler.c b/src/regex/gnunet-regex-simulation-profiler.c
index d82b7800a..aa297d2ea 100644
--- a/src/regex/gnunet-regex-simulation-profiler.c
+++ b/src/regex/gnunet-regex-simulation-profiler.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21 21
22/** 22/**
@@ -38,22 +38,21 @@
38/** 38/**
39 * MySQL statement to insert an edge. 39 * MySQL statement to insert an edge.
40 */ 40 */
41#define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` "\ 41#define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` " \
42 "(`key`, `label`, `to_key`, `accepting`) "\ 42 "(`key`, `label`, `to_key`, `accepting`) " \
43 "VALUES (?, ?, ?, ?);" 43 "VALUES (?, ?, ?, ?);"
44 44
45/** 45/**
46 * MySQL statement to select a key count. 46 * MySQL statement to select a key count.
47 */ 47 */
48#define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` "\ 48#define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` " \
49 "WHERE `key` = ? AND `label` = ?;" 49 "WHERE `key` = ? AND `label` = ?;"
50 50
51/** 51/**
52 * Simple struct to keep track of progress, and print a 52 * Simple struct to keep track of progress, and print a
53 * nice little percentage meter for long running tasks. 53 * nice little percentage meter for long running tasks.
54 */ 54 */
55struct ProgressMeter 55struct ProgressMeter {
56{
57 /** 56 /**
58 * Total number of elements. 57 * Total number of elements.
59 */ 58 */
@@ -168,11 +167,11 @@ static char *regex_prefix;
168 * @return the progress meter 167 * @return the progress meter
169 */ 168 */
170static struct ProgressMeter * 169static struct ProgressMeter *
171create_meter (unsigned int total, char *start_string, int print) 170create_meter(unsigned int total, char *start_string, int print)
172{ 171{
173 struct ProgressMeter *ret; 172 struct ProgressMeter *ret;
174 173
175 ret = GNUNET_new (struct ProgressMeter); 174 ret = GNUNET_new(struct ProgressMeter);
176 ret->print = print; 175 ret->print = print;
177 ret->total = total; 176 ret->total = total;
178 ret->modnum = total / 4; 177 ret->modnum = total / 4;
@@ -180,9 +179,9 @@ create_meter (unsigned int total, char *start_string, int print)
180 ret->modnum = 1; 179 ret->modnum = 1;
181 ret->dotnum = (total / 50) + 1; 180 ret->dotnum = (total / 50) + 1;
182 if (start_string != NULL) 181 if (start_string != NULL)
183 ret->startup_string = GNUNET_strdup (start_string); 182 ret->startup_string = GNUNET_strdup(start_string);
184 else 183 else
185 ret->startup_string = GNUNET_strdup (""); 184 ret->startup_string = GNUNET_strdup("");
186 185
187 return ret; 186 return ret;
188} 187}
@@ -197,33 +196,33 @@ create_meter (unsigned int total, char *start_string, int print)
197 * GNUNET_NO if more items expected 196 * GNUNET_NO if more items expected
198 */ 197 */
199static int 198static int
200update_meter (struct ProgressMeter *meter) 199update_meter(struct ProgressMeter *meter)
201{ 200{
202 if (meter->print == GNUNET_YES) 201 if (meter->print == GNUNET_YES)
203 {
204 if (meter->completed % meter->modnum == 0)
205 { 202 {
206 if (meter->completed == 0) 203 if (meter->completed % meter->modnum == 0)
207 { 204 {
208 fprintf (stdout, "%sProgress: [0%%", meter->startup_string); 205 if (meter->completed == 0)
209 } 206 {
210 else 207 fprintf(stdout, "%sProgress: [0%%", meter->startup_string);
211 fprintf (stdout, "%d%%", 208 }
212 (int) (((float) meter->completed / meter->total) * 100)); 209 else
210 fprintf(stdout, "%d%%",
211 (int)(((float)meter->completed / meter->total) * 100));
212 }
213 else if (meter->completed % meter->dotnum == 0)
214 fprintf(stdout, "%s", ".");
215
216 if (meter->completed + 1 == meter->total)
217 fprintf(stdout, "%d%%]\n", 100);
218 fflush(stdout);
213 } 219 }
214 else if (meter->completed % meter->dotnum == 0)
215 fprintf (stdout, "%s", ".");
216
217 if (meter->completed + 1 == meter->total)
218 fprintf (stdout, "%d%%]\n", 100);
219 fflush (stdout);
220 }
221 meter->completed++; 220 meter->completed++;
222 221
223 if (meter->completed == meter->total) 222 if (meter->completed == meter->total)
224 return GNUNET_YES; 223 return GNUNET_YES;
225 if (meter->completed > meter->total) 224 if (meter->completed > meter->total)
226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n"); 225 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
227 return GNUNET_NO; 226 return GNUNET_NO;
228} 227}
229 228
@@ -237,7 +236,7 @@ update_meter (struct ProgressMeter *meter)
237 * #GNUNET_SYSERR on error 236 * #GNUNET_SYSERR on error
238 */ 237 */
239static int 238static int
240reset_meter (struct ProgressMeter *meter) 239reset_meter(struct ProgressMeter *meter)
241{ 240{
242 if (meter == NULL) 241 if (meter == NULL)
243 return GNUNET_SYSERR; 242 return GNUNET_SYSERR;
@@ -253,10 +252,10 @@ reset_meter (struct ProgressMeter *meter)
253 * @param meter the meter to free 252 * @param meter the meter to free
254 */ 253 */
255static void 254static void
256free_meter (struct ProgressMeter *meter) 255free_meter(struct ProgressMeter *meter)
257{ 256{
258 GNUNET_free_non_null (meter->startup_string); 257 GNUNET_free_non_null(meter->startup_string);
259 GNUNET_free (meter); 258 GNUNET_free(meter);
260} 259}
261 260
262 261
@@ -266,18 +265,18 @@ free_meter (struct ProgressMeter *meter)
266 * @param cls NULL 265 * @param cls NULL
267 */ 266 */
268static void 267static void
269do_shutdown (void *cls) 268do_shutdown(void *cls)
270{ 269{
271 if (NULL != mysql_ctx) 270 if (NULL != mysql_ctx)
272 { 271 {
273 GNUNET_MYSQL_context_destroy (mysql_ctx); 272 GNUNET_MYSQL_context_destroy(mysql_ctx);
274 mysql_ctx = NULL; 273 mysql_ctx = NULL;
275 } 274 }
276 if (NULL != meter) 275 if (NULL != meter)
277 { 276 {
278 free_meter (meter); 277 free_meter(meter);
279 meter = NULL; 278 meter = NULL;
280 } 279 }
281} 280}
282 281
283 282
@@ -293,16 +292,16 @@ do_shutdown (void *cls)
293 * @param cls NULL 292 * @param cls NULL
294 */ 293 */
295static void 294static void
296do_abort (void *cls) 295do_abort(void *cls)
297{ 296{
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n"); 297 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
299 if (NULL != scan_task) 298 if (NULL != scan_task)
300 { 299 {
301 GNUNET_SCHEDULER_cancel (scan_task); 300 GNUNET_SCHEDULER_cancel(scan_task);
302 scan_task = NULL; 301 scan_task = NULL;
303 } 302 }
304 result = GNUNET_SYSERR; 303 result = GNUNET_SYSERR;
305 GNUNET_SCHEDULER_shutdown (); 304 GNUNET_SCHEDULER_shutdown();
306} 305}
307 306
308/** 307/**
@@ -316,12 +315,12 @@ do_abort (void *cls)
316 * @param edges edges leaving current state. 315 * @param edges edges leaving current state.
317 */ 316 */
318static void 317static void
319regex_iterator (void *cls, 318regex_iterator(void *cls,
320 const struct GNUNET_HashCode *key, 319 const struct GNUNET_HashCode *key,
321 const char *proof, 320 const char *proof,
322 int accepting, 321 int accepting,
323 unsigned int num_edges, 322 unsigned int num_edges,
324 const struct REGEX_BLOCK_Edge *edges) 323 const struct REGEX_BLOCK_Edge *edges)
325{ 324{
326 unsigned int i; 325 unsigned int i;
327 int result; 326 int result;
@@ -329,118 +328,118 @@ regex_iterator (void *cls,
329 uint32_t iaccepting = (uint32_t)accepting; 328 uint32_t iaccepting = (uint32_t)accepting;
330 uint64_t total; 329 uint64_t total;
331 330
332 GNUNET_assert (NULL != mysql_ctx); 331 GNUNET_assert(NULL != mysql_ctx);
333 332
334 for (i = 0; i < num_edges; i++) 333 for (i = 0; i < num_edges; i++)
335 {
336 struct GNUNET_MY_QueryParam params_select[] = {
337 GNUNET_MY_query_param_auto_from_type (key),
338 GNUNET_MY_query_param_string (edges[i].label),
339 GNUNET_MY_query_param_end
340 };
341
342 struct GNUNET_MY_ResultSpec results_select[] = {
343 GNUNET_MY_result_spec_uint64 (&total),
344 GNUNET_MY_result_spec_end
345 };
346
347 result =
348 GNUNET_MY_exec_prepared (mysql_ctx,
349 select_stmt_handle,
350 params_select);
351
352 if (GNUNET_SYSERR == result)
353 {
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 "Error executing prepared mysql select statement\n");
356 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
357 return;
358 }
359
360 result =
361 GNUNET_MY_extract_result (select_stmt_handle,
362 results_select);
363
364 if (GNUNET_SYSERR == result)
365 {
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "Error extracting result mysql select statement\n");
368 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
369 return;
370 }
371
372 if (-1 != total && total > 0)
373 {
374 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n",
375 (unsigned long long)total,
376 GNUNET_h2s (key), edges[i].label);
377 }
378
379 struct GNUNET_MY_QueryParam params_stmt[] = {
380 GNUNET_MY_query_param_auto_from_type (&key),
381 GNUNET_MY_query_param_string (edges[i].label),
382 GNUNET_MY_query_param_auto_from_type (&edges[i].destination),
383 GNUNET_MY_query_param_uint32 (&iaccepting),
384 GNUNET_MY_query_param_end
385 };
386
387 result =
388 GNUNET_MY_exec_prepared (mysql_ctx,
389 stmt_handle,
390 params_stmt);
391
392 if (0 == result)
393 {
394 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
395 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
396
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n",
398 key_str,
399 edges[i].label,
400 to_key_str,
401 accepting);
402
403 GNUNET_free (key_str);
404 GNUNET_free (to_key_str);
405 num_merged_transitions++;
406 }
407 else if (-1 != total)
408 {
409 num_merged_states++;
410 }
411
412 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
413 { 334 {
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 335 struct GNUNET_MY_QueryParam params_select[] = {
415 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", 336 GNUNET_MY_query_param_auto_from_type(key),
416 result); 337 GNUNET_MY_query_param_string(edges[i].label),
417 GNUNET_SCHEDULER_add_now (&do_abort, NULL); 338 GNUNET_MY_query_param_end
339 };
340
341 struct GNUNET_MY_ResultSpec results_select[] = {
342 GNUNET_MY_result_spec_uint64(&total),
343 GNUNET_MY_result_spec_end
344 };
345
346 result =
347 GNUNET_MY_exec_prepared(mysql_ctx,
348 select_stmt_handle,
349 params_select);
350
351 if (GNUNET_SYSERR == result)
352 {
353 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
354 "Error executing prepared mysql select statement\n");
355 GNUNET_SCHEDULER_add_now(&do_abort, NULL);
356 return;
357 }
358
359 result =
360 GNUNET_MY_extract_result(select_stmt_handle,
361 results_select);
362
363 if (GNUNET_SYSERR == result)
364 {
365 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
366 "Error extracting result mysql select statement\n");
367 GNUNET_SCHEDULER_add_now(&do_abort, NULL);
368 return;
369 }
370
371 if (-1 != total && total > 0)
372 {
373 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n",
374 (unsigned long long)total,
375 GNUNET_h2s(key), edges[i].label);
376 }
377
378 struct GNUNET_MY_QueryParam params_stmt[] = {
379 GNUNET_MY_query_param_auto_from_type(&key),
380 GNUNET_MY_query_param_string(edges[i].label),
381 GNUNET_MY_query_param_auto_from_type(&edges[i].destination),
382 GNUNET_MY_query_param_uint32(&iaccepting),
383 GNUNET_MY_query_param_end
384 };
385
386 result =
387 GNUNET_MY_exec_prepared(mysql_ctx,
388 stmt_handle,
389 params_stmt);
390
391 if (0 == result)
392 {
393 char *key_str = GNUNET_strdup(GNUNET_h2s(key));
394 char *to_key_str = GNUNET_strdup(GNUNET_h2s(&edges[i].destination));
395
396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n",
397 key_str,
398 edges[i].label,
399 to_key_str,
400 accepting);
401
402 GNUNET_free(key_str);
403 GNUNET_free(to_key_str);
404 num_merged_transitions++;
405 }
406 else if (-1 != total)
407 {
408 num_merged_states++;
409 }
410
411 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
412 {
413 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
414 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
415 result);
416 GNUNET_SCHEDULER_add_now(&do_abort, NULL);
417 }
418 } 418 }
419 }
420 419
421 if (0 == num_edges) 420 if (0 == num_edges)
422 {
423 struct GNUNET_MY_QueryParam params_stmt[] = {
424 GNUNET_MY_query_param_auto_from_type (key),
425 GNUNET_MY_query_param_string (""),
426 GNUNET_MY_query_param_fixed_size (NULL, 0),
427 GNUNET_MY_query_param_uint32 (&iaccepting),
428 GNUNET_MY_query_param_end
429 };
430
431 result =
432 GNUNET_MY_exec_prepared (mysql_ctx,
433 stmt_handle,
434 params_stmt);
435
436 if (1 != result && 0 != result)
437 { 421 {
438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 422 struct GNUNET_MY_QueryParam params_stmt[] = {
439 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", 423 GNUNET_MY_query_param_auto_from_type(key),
440 result); 424 GNUNET_MY_query_param_string(""),
441 GNUNET_SCHEDULER_add_now (&do_abort, NULL); 425 GNUNET_MY_query_param_fixed_size(NULL, 0),
426 GNUNET_MY_query_param_uint32(&iaccepting),
427 GNUNET_MY_query_param_end
428 };
429
430 result =
431 GNUNET_MY_exec_prepared(mysql_ctx,
432 stmt_handle,
433 params_stmt);
434
435 if (1 != result && 0 != result)
436 {
437 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
438 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
439 result);
440 GNUNET_SCHEDULER_add_now(&do_abort, NULL);
441 }
442 } 442 }
443 }
444} 443}
445 444
446 445
@@ -452,26 +451,26 @@ regex_iterator (void *cls,
452 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure. 451 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
453 */ 452 */
454static int 453static int
455announce_regex (const char *regex) 454announce_regex(const char *regex)
456{ 455{
457 struct REGEX_INTERNAL_Automaton *dfa; 456 struct REGEX_INTERNAL_Automaton *dfa;
458 457
459 dfa = 458 dfa =
460 REGEX_INTERNAL_construct_dfa (regex, 459 REGEX_INTERNAL_construct_dfa(regex,
461 strlen (regex), 460 strlen(regex),
462 max_path_compression); 461 max_path_compression);
463 462
464 if (NULL == dfa) 463 if (NULL == dfa)
465 { 464 {
466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 465 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
467 "Failed to create DFA for regex %s\n", 466 "Failed to create DFA for regex %s\n",
468 regex); 467 regex);
469 GNUNET_SCHEDULER_add_now (&do_abort, NULL); 468 GNUNET_SCHEDULER_add_now(&do_abort, NULL);
470 return GNUNET_SYSERR; 469 return GNUNET_SYSERR;
471 } 470 }
472 REGEX_INTERNAL_iterate_all_edges (dfa, 471 REGEX_INTERNAL_iterate_all_edges(dfa,
473 &regex_iterator, NULL); 472 &regex_iterator, NULL);
474 REGEX_INTERNAL_automaton_destroy (dfa); 473 REGEX_INTERNAL_automaton_destroy(dfa);
475 474
476 return GNUNET_OK; 475 return GNUNET_OK;
477} 476}
@@ -486,7 +485,7 @@ announce_regex (const char *regex)
486 * #GNUNET_SYSERR to abort iteration with error! 485 * #GNUNET_SYSERR to abort iteration with error!
487 */ 486 */
488static int 487static int
489policy_filename_cb (void *cls, const char *filename) 488policy_filename_cb(void *cls, const char *filename)
490{ 489{
491 char *regex; 490 char *regex;
492 char *data; 491 char *data;
@@ -494,70 +493,70 @@ policy_filename_cb (void *cls, const char *filename)
494 uint64_t filesize; 493 uint64_t filesize;
495 unsigned int offset; 494 unsigned int offset;
496 495
497 GNUNET_assert (NULL != filename); 496 GNUNET_assert(NULL != filename);
498 497
499 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 498 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
500 "Announcing regexes from file %s\n", 499 "Announcing regexes from file %s\n",
501 filename); 500 filename);
502 501
503 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 502 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
504 { 503 {
505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 504 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
506 "Could not find policy file %s\n", 505 "Could not find policy file %s\n",
507 filename); 506 filename);
508 return GNUNET_OK; 507 return GNUNET_OK;
509 } 508 }
510 if (GNUNET_OK != 509 if (GNUNET_OK !=
511 GNUNET_DISK_file_size (filename, &filesize, 510 GNUNET_DISK_file_size(filename, &filesize,
512 GNUNET_YES, GNUNET_YES)) 511 GNUNET_YES, GNUNET_YES))
513 filesize = 0; 512 filesize = 0;
514 if (0 == filesize) 513 if (0 == filesize)
515 { 514 {
516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", 515 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
517 filename); 516 filename);
518 return GNUNET_OK; 517 return GNUNET_OK;
519 } 518 }
520 data = GNUNET_malloc (filesize); 519 data = GNUNET_malloc(filesize);
521 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize)) 520 if (filesize != GNUNET_DISK_fn_read(filename, data, filesize))
522 { 521 {
523 GNUNET_free (data); 522 GNUNET_free(data);
524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 523 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
525 "Could not read policy file %s.\n", 524 "Could not read policy file %s.\n",
526 filename); 525 filename);
527 return GNUNET_OK; 526 return GNUNET_OK;
528 } 527 }
529 528
530 update_meter (meter); 529 update_meter(meter);
531 530
532 buf = data; 531 buf = data;
533 offset = 0; 532 offset = 0;
534 regex = NULL; 533 regex = NULL;
535 while (offset < (filesize - 1)) 534 while (offset < (filesize - 1))
536 {
537 offset++;
538 if (((data[offset] == '\n')) && (buf != &data[offset]))
539 { 535 {
540 data[offset] = '|'; 536 offset++;
541 num_policies++; 537 if (((data[offset] == '\n')) && (buf != &data[offset]))
542 buf = &data[offset + 1]; 538 {
539 data[offset] = '|';
540 num_policies++;
541 buf = &data[offset + 1];
542 }
543 else if ((data[offset] == '\n') || (data[offset] == '\0'))
544 buf = &data[offset + 1];
543 } 545 }
544 else if ((data[offset] == '\n') || (data[offset] == '\0'))
545 buf = &data[offset + 1];
546 }
547 data[offset] = '\0'; 546 data[offset] = '\0';
548 GNUNET_asprintf (&regex, "%s(%s)", regex_prefix, data); 547 GNUNET_asprintf(&regex, "%s(%s)", regex_prefix, data);
549 GNUNET_assert (NULL != regex); 548 GNUNET_assert(NULL != regex);
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
551 "Announcing regex: %s\n", regex); 550 "Announcing regex: %s\n", regex);
552 551
553 if (GNUNET_OK != announce_regex (regex)) 552 if (GNUNET_OK != announce_regex(regex))
554 { 553 {
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 554 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
556 "Could not announce regex %s\n", 555 "Could not announce regex %s\n",
557 regex); 556 regex);
558 } 557 }
559 GNUNET_free (regex); 558 GNUNET_free(regex);
560 GNUNET_free (data); 559 GNUNET_free(data);
561 return GNUNET_OK; 560 return GNUNET_OK;
562} 561}
563 562
@@ -568,7 +567,7 @@ policy_filename_cb (void *cls, const char *filename)
568 * @param cls NULL 567 * @param cls NULL
569 */ 568 */
570static void 569static void
571do_directory_scan (void *cls) 570do_directory_scan(void *cls)
572{ 571{
573 struct GNUNET_TIME_Absolute start_time; 572 struct GNUNET_TIME_Absolute start_time;
574 struct GNUNET_TIME_Relative duration; 573 struct GNUNET_TIME_Relative duration;
@@ -576,37 +575,37 @@ do_directory_scan (void *cls)
576 575
577 /* Create an MySQL prepared statement for the inserts */ 576 /* Create an MySQL prepared statement for the inserts */
578 scan_task = NULL; 577 scan_task = NULL;
579 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name); 578 GNUNET_asprintf(&stmt, INSERT_EDGE_STMT, table_name);
580 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt); 579 stmt_handle = GNUNET_MYSQL_statement_prepare(mysql_ctx, stmt);
581 GNUNET_free (stmt); 580 GNUNET_free(stmt);
582 581
583 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name); 582 GNUNET_asprintf(&stmt, SELECT_KEY_STMT, table_name);
584 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt); 583 select_stmt_handle = GNUNET_MYSQL_statement_prepare(mysql_ctx, stmt);
585 GNUNET_free (stmt); 584 GNUNET_free(stmt);
586 585
587 GNUNET_assert (NULL != stmt_handle); 586 GNUNET_assert(NULL != stmt_handle);
588 587
589 meter = create_meter (num_policy_files, 588 meter = create_meter(num_policy_files,
590 "Announcing policy files\n", 589 "Announcing policy files\n",
591 GNUNET_YES); 590 GNUNET_YES);
592 start_time = GNUNET_TIME_absolute_get (); 591 start_time = GNUNET_TIME_absolute_get();
593 GNUNET_DISK_directory_scan (policy_dir, 592 GNUNET_DISK_directory_scan(policy_dir,
594 &policy_filename_cb, 593 &policy_filename_cb,
595 stmt_handle); 594 stmt_handle);
596 duration = GNUNET_TIME_absolute_get_duration (start_time); 595 duration = GNUNET_TIME_absolute_get_duration(start_time);
597 reset_meter (meter); 596 reset_meter(meter);
598 free_meter (meter); 597 free_meter(meter);
599 meter = NULL; 598 meter = NULL;
600 599
601 printf ("Announced %u files containing %u policies in %s\n" 600 printf("Announced %u files containing %u policies in %s\n"
602 "Duplicate transitions: %llu\nMerged states: %llu\n", 601 "Duplicate transitions: %llu\nMerged states: %llu\n",
603 num_policy_files, 602 num_policy_files,
604 num_policies, 603 num_policies,
605 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO), 604 GNUNET_STRINGS_relative_time_to_string(duration, GNUNET_NO),
606 num_merged_transitions, 605 num_merged_transitions,
607 num_merged_states); 606 num_merged_states);
608 result = GNUNET_OK; 607 result = GNUNET_OK;
609 GNUNET_SCHEDULER_shutdown (); 608 GNUNET_SCHEDULER_shutdown();
610} 609}
611 610
612 611
@@ -619,65 +618,65 @@ do_directory_scan (void *cls)
619 * @param config configuration 618 * @param config configuration
620 */ 619 */
621static void 620static void
622run (void *cls, 621run(void *cls,
623 char *const *args, 622 char *const *args,
624 const char *cfgfile, 623 const char *cfgfile,
625 const struct GNUNET_CONFIGURATION_Handle *config) 624 const struct GNUNET_CONFIGURATION_Handle *config)
626{ 625{
627 if (NULL == args[0]) 626 if (NULL == args[0])
628 { 627 {
629 fprintf (stderr, 628 fprintf(stderr,
630 _("No policy directory specified on command line. Exiting.\n")); 629 _("No policy directory specified on command line. Exiting.\n"));
631 result = GNUNET_SYSERR; 630 result = GNUNET_SYSERR;
632 return; 631 return;
633 } 632 }
634 if (GNUNET_YES != 633 if (GNUNET_YES !=
635 GNUNET_DISK_directory_test (args[0], GNUNET_YES)) 634 GNUNET_DISK_directory_test(args[0], GNUNET_YES))
636 { 635 {
637 fprintf (stderr, 636 fprintf(stderr,
638 _("Specified policies directory does not exist. Exiting.\n")); 637 _("Specified policies directory does not exist. Exiting.\n"));
639 result = GNUNET_SYSERR; 638 result = GNUNET_SYSERR;
640 return; 639 return;
641 } 640 }
642 policy_dir = args[0]; 641 policy_dir = args[0];
643 642
644 num_policy_files = GNUNET_DISK_directory_scan (policy_dir, 643 num_policy_files = GNUNET_DISK_directory_scan(policy_dir,
645 NULL, NULL); 644 NULL, NULL);
646 meter = NULL; 645 meter = NULL;
647 646
648 if (NULL == table_name) 647 if (NULL == table_name)
649 { 648 {
650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 649 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
651 "No table name specified, using default \"NFA\".\n"); 650 "No table name specified, using default \"NFA\".\n");
652 table_name = "NFA"; 651 table_name = "NFA";
653 } 652 }
654 653
655 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql"); 654 mysql_ctx = GNUNET_MYSQL_context_create(config, "regex-mysql");
656 if (NULL == mysql_ctx) 655 if (NULL == mysql_ctx)
657 { 656 {
658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 657 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
659 "Failed to create mysql context\n"); 658 "Failed to create mysql context\n");
660 result = GNUNET_SYSERR; 659 result = GNUNET_SYSERR;
661 return; 660 return;
662 } 661 }
663 662
664 if (GNUNET_OK != 663 if (GNUNET_OK !=
665 GNUNET_CONFIGURATION_get_value_string (config, 664 GNUNET_CONFIGURATION_get_value_string(config,
666 "regex-mysql", 665 "regex-mysql",
667 "REGEX_PREFIX", 666 "REGEX_PREFIX",
668 &regex_prefix)) 667 &regex_prefix))
669 { 668 {
670 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 669 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
671 "regex-mysql", 670 "regex-mysql",
672 "REGEX_PREFIX"); 671 "REGEX_PREFIX");
673 result = GNUNET_SYSERR; 672 result = GNUNET_SYSERR;
674 return; 673 return;
675 } 674 }
676 675
677 result = GNUNET_OK; 676 result = GNUNET_OK;
678 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 677 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
679 NULL); 678 NULL);
680 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL); 679 scan_task = GNUNET_SCHEDULER_add_now(&do_directory_scan, NULL);
681} 680}
682 681
683 682
@@ -689,34 +688,33 @@ run (void *cls,
689 * @return 0 on success 688 * @return 0 on success
690 */ 689 */
691int 690int
692main (int argc, char *const *argv) 691main(int argc, char *const *argv)
693{ 692{
694 struct GNUNET_GETOPT_CommandLineOption options[] = { 693 struct GNUNET_GETOPT_CommandLineOption options[] = {
695 694 GNUNET_GETOPT_option_string('t',
696 GNUNET_GETOPT_option_string ('t', 695 "table",
697 "table", 696 "TABLENAME",
698 "TABLENAME", 697 gettext_noop("name of the table to write DFAs"),
699 gettext_noop ("name of the table to write DFAs"), 698 &table_name),
700 &table_name), 699
701 700 GNUNET_GETOPT_option_uint('p',
702 GNUNET_GETOPT_option_uint ('p', 701 "max-path-compression",
703 "max-path-compression", 702 "MAX_PATH_COMPRESSION",
704 "MAX_PATH_COMPRESSION", 703 gettext_noop("maximum path compression length"),
705 gettext_noop ("maximum path compression length"), 704 &max_path_compression),
706 &max_path_compression),
707 705
708 GNUNET_GETOPT_OPTION_END 706 GNUNET_GETOPT_OPTION_END
709 }; 707 };
710 int ret; 708 int ret;
711 709
712 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 710 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
713 return 2; 711 return 2;
714 712
715 result = GNUNET_SYSERR; 713 result = GNUNET_SYSERR;
716 ret = 714 ret =
717 GNUNET_PROGRAM_run (argc, argv, 715 GNUNET_PROGRAM_run(argc, argv,
718 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir", 716 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
719 _("Profiler for regex library"), options, &run, NULL); 717 _("Profiler for regex library"), options, &run, NULL);
720 if (GNUNET_OK != ret) 718 if (GNUNET_OK != ret)
721 return ret; 719 return ret;
722 if (GNUNET_OK != result) 720 if (GNUNET_OK != result)
diff --git a/src/regex/gnunet-service-regex.c b/src/regex/gnunet-service-regex.c
index a866288a3..90ea91780 100644
--- a/src/regex/gnunet-service-regex.c
+++ b/src/regex/gnunet-service-regex.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/gnunet-service-regex.c 22 * @file regex/gnunet-service-regex.c
@@ -33,9 +33,7 @@
33/** 33/**
34 * Information about one of our clients. 34 * Information about one of our clients.
35 */ 35 */
36struct ClientEntry 36struct ClientEntry {
37{
38
39 /** 37 /**
40 * Queue for transmissions to @e client. 38 * Queue for transmissions to @e client.
41 */ 39 */
@@ -65,7 +63,6 @@ struct ClientEntry
65 * Task for re-announcing. 63 * Task for re-announcing.
66 */ 64 */
67 struct GNUNET_SCHEDULER_Task *refresh_task; 65 struct GNUNET_SCHEDULER_Task *refresh_task;
68
69}; 66};
70 67
71 68
@@ -91,14 +88,14 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
91 * @param cls unused 88 * @param cls unused
92 */ 89 */
93static void 90static void
94cleanup_task (void *cls) 91cleanup_task(void *cls)
95{ 92{
96 GNUNET_DHT_disconnect (dht); 93 GNUNET_DHT_disconnect(dht);
97 dht = NULL; 94 dht = NULL;
98 GNUNET_STATISTICS_destroy (stats, 95 GNUNET_STATISTICS_destroy(stats,
99 GNUNET_NO); 96 GNUNET_NO);
100 stats = NULL; 97 stats = NULL;
101 GNUNET_free (my_private_key); 98 GNUNET_free(my_private_key);
102 my_private_key = NULL; 99 my_private_key = NULL;
103} 100}
104 101
@@ -110,14 +107,14 @@ cleanup_task (void *cls)
110 * announcement 107 * announcement
111 */ 108 */
112static void 109static void
113reannounce (void *cls) 110reannounce(void *cls)
114{ 111{
115 struct ClientEntry *ce = cls; 112 struct ClientEntry *ce = cls;
116 113
117 REGEX_INTERNAL_reannounce (ce->ah); 114 REGEX_INTERNAL_reannounce(ce->ah);
118 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, 115 ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency,
119 &reannounce, 116 &reannounce,
120 ce); 117 ce);
121} 118}
122 119
123 120
@@ -129,18 +126,18 @@ reannounce (void *cls)
129 * @return #GNUNET_OK if @am is well-formed 126 * @return #GNUNET_OK if @am is well-formed
130 */ 127 */
131static int 128static int
132check_announce (void *cls, 129check_announce(void *cls,
133 const struct AnnounceMessage *am) 130 const struct AnnounceMessage *am)
134{ 131{
135 struct ClientEntry *ce = cls; 132 struct ClientEntry *ce = cls;
136 133
137 GNUNET_MQ_check_zero_termination (am); 134 GNUNET_MQ_check_zero_termination(am);
138 if (NULL != ce->ah) 135 if (NULL != ce->ah)
139 { 136 {
140 /* only one announcement per client allowed */ 137 /* only one announcement per client allowed */
141 GNUNET_break (0); 138 GNUNET_break(0);
142 return GNUNET_SYSERR; 139 return GNUNET_SYSERR;
143 } 140 }
144 return GNUNET_OK; 141 return GNUNET_OK;
145} 142}
146 143
@@ -152,36 +149,36 @@ check_announce (void *cls,
152 * @param am the actual message 149 * @param am the actual message
153 */ 150 */
154static void 151static void
155handle_announce (void *cls, 152handle_announce(void *cls,
156 const struct AnnounceMessage *am) 153 const struct AnnounceMessage *am)
157{ 154{
158 struct ClientEntry *ce = cls; 155 struct ClientEntry *ce = cls;
159 const char *regex; 156 const char *regex;
160 157
161 regex = (const char *) &am[1]; 158 regex = (const char *)&am[1];
162 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay); 159 ce->frequency = GNUNET_TIME_relative_ntoh(am->refresh_delay);
163 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, 160 ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency,
164 &reannounce, 161 &reannounce,
165 ce); 162 ce);
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 "Starting to announce regex `%s' every %s\n", 164 "Starting to announce regex `%s' every %s\n",
168 regex, 165 regex,
169 GNUNET_STRINGS_relative_time_to_string (ce->frequency, 166 GNUNET_STRINGS_relative_time_to_string(ce->frequency,
170 GNUNET_NO)); 167 GNUNET_NO));
171 ce->ah = REGEX_INTERNAL_announce (dht, 168 ce->ah = REGEX_INTERNAL_announce(dht,
172 my_private_key, 169 my_private_key,
173 regex, 170 regex,
174 ntohs (am->compression), 171 ntohs(am->compression),
175 stats); 172 stats);
176 if (NULL == ce->ah) 173 if (NULL == ce->ah)
177 { 174 {
178 GNUNET_break (0); 175 GNUNET_break(0);
179 GNUNET_SCHEDULER_cancel (ce->refresh_task); 176 GNUNET_SCHEDULER_cancel(ce->refresh_task);
180 ce->refresh_task = NULL; 177 ce->refresh_task = NULL;
181 GNUNET_SERVICE_client_drop (ce->client); 178 GNUNET_SERVICE_client_drop(ce->client);
182 return; 179 return;
183 } 180 }
184 GNUNET_SERVICE_client_continue (ce->client); 181 GNUNET_SERVICE_client_continue(ce->client);
185} 182}
186 183
187 184
@@ -196,12 +193,12 @@ handle_announce (void *cls,
196 * @param put_path_length Length of the @a put_path. 193 * @param put_path_length Length of the @a put_path.
197 */ 194 */
198static void 195static void
199handle_search_result (void *cls, 196handle_search_result(void *cls,
200 const struct GNUNET_PeerIdentity *id, 197 const struct GNUNET_PeerIdentity *id,
201 const struct GNUNET_PeerIdentity *get_path, 198 const struct GNUNET_PeerIdentity *get_path,
202 unsigned int get_path_length, 199 unsigned int get_path_length,
203 const struct GNUNET_PeerIdentity *put_path, 200 const struct GNUNET_PeerIdentity *put_path,
204 unsigned int put_path_length) 201 unsigned int put_path_length)
205{ 202{
206 struct ClientEntry *ce = cls; 203 struct ClientEntry *ce = cls;
207 struct GNUNET_MQ_Envelope *env; 204 struct GNUNET_MQ_Envelope *env;
@@ -209,30 +206,30 @@ handle_search_result (void *cls,
209 struct GNUNET_PeerIdentity *gp; 206 struct GNUNET_PeerIdentity *gp;
210 uint16_t size; 207 uint16_t size;
211 208
212 if ( (get_path_length >= 65536) || 209 if ((get_path_length >= 65536) ||
213 (put_path_length >= 65536) || 210 (put_path_length >= 65536) ||
214 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) 211 ((get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity))
215 + sizeof (struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) 212 + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE)
216 { 213 {
217 GNUNET_break (0); 214 GNUNET_break(0);
218 return; 215 return;
219 } 216 }
220 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity); 217 size = (get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity);
221 env = GNUNET_MQ_msg_extra (result, 218 env = GNUNET_MQ_msg_extra(result,
222 size, 219 size,
223 GNUNET_MESSAGE_TYPE_REGEX_RESULT); 220 GNUNET_MESSAGE_TYPE_REGEX_RESULT);
224 result->get_path_length = htons ((uint16_t) get_path_length); 221 result->get_path_length = htons((uint16_t)get_path_length);
225 result->put_path_length = htons ((uint16_t) put_path_length); 222 result->put_path_length = htons((uint16_t)put_path_length);
226 result->id = *id; 223 result->id = *id;
227 gp = &result->id; 224 gp = &result->id;
228 GNUNET_memcpy (&gp[1], 225 GNUNET_memcpy(&gp[1],
229 get_path, 226 get_path,
230 get_path_length * sizeof (struct GNUNET_PeerIdentity)); 227 get_path_length * sizeof(struct GNUNET_PeerIdentity));
231 GNUNET_memcpy (&gp[1 + get_path_length], 228 GNUNET_memcpy(&gp[1 + get_path_length],
232 put_path, 229 put_path,
233 put_path_length * sizeof (struct GNUNET_PeerIdentity)); 230 put_path_length * sizeof(struct GNUNET_PeerIdentity));
234 GNUNET_MQ_send (ce->mq, 231 GNUNET_MQ_send(ce->mq,
235 env); 232 env);
236} 233}
237 234
238 235
@@ -243,26 +240,26 @@ handle_search_result (void *cls,
243 * @param message the actual message 240 * @param message the actual message
244 */ 241 */
245static int 242static int
246check_search (void *cls, 243check_search(void *cls,
247 const struct RegexSearchMessage *sm) 244 const struct RegexSearchMessage *sm)
248{ 245{
249 struct ClientEntry *ce = cls; 246 struct ClientEntry *ce = cls;
250 const char *string; 247 const char *string;
251 uint16_t size; 248 uint16_t size;
252 249
253 size = ntohs (sm->header.size) - sizeof (*sm); 250 size = ntohs(sm->header.size) - sizeof(*sm);
254 string = (const char *) &sm[1]; 251 string = (const char *)&sm[1];
255 if ('\0' != string[size - 1]) 252 if ('\0' != string[size - 1])
256 { 253 {
257 GNUNET_break (0); 254 GNUNET_break(0);
258 return GNUNET_SYSERR; 255 return GNUNET_SYSERR;
259 } 256 }
260 if (NULL != ce->sh) 257 if (NULL != ce->sh)
261 { 258 {
262 /* only one search allowed per client */ 259 /* only one search allowed per client */
263 GNUNET_break (0); 260 GNUNET_break(0);
264 return GNUNET_SYSERR; 261 return GNUNET_SYSERR;
265 } 262 }
266 return GNUNET_OK; 263 return GNUNET_OK;
267} 264}
268 265
@@ -274,28 +271,28 @@ check_search (void *cls,
274 * @param message the actual message 271 * @param message the actual message
275 */ 272 */
276static void 273static void
277handle_search (void *cls, 274handle_search(void *cls,
278 const struct RegexSearchMessage *sm) 275 const struct RegexSearchMessage *sm)
279{ 276{
280 struct ClientEntry *ce = cls; 277 struct ClientEntry *ce = cls;
281 const char *string; 278 const char *string;
282 279
283 string = (const char *) &sm[1]; 280 string = (const char *)&sm[1];
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
285 "Starting to search for `%s'\n", 282 "Starting to search for `%s'\n",
286 string); 283 string);
287 ce->sh = REGEX_INTERNAL_search (dht, 284 ce->sh = REGEX_INTERNAL_search(dht,
288 string, 285 string,
289 &handle_search_result, 286 &handle_search_result,
290 ce, 287 ce,
291 stats); 288 stats);
292 if (NULL == ce->sh) 289 if (NULL == ce->sh)
293 { 290 {
294 GNUNET_break (0); 291 GNUNET_break(0);
295 GNUNET_SERVICE_client_drop (ce->client); 292 GNUNET_SERVICE_client_drop(ce->client);
296 return; 293 return;
297 } 294 }
298 GNUNET_SERVICE_client_continue (ce->client); 295 GNUNET_SERVICE_client_continue(ce->client);
299} 296}
300 297
301 298
@@ -307,27 +304,27 @@ handle_search (void *cls,
307 * @param service the initialized service 304 * @param service the initialized service
308 */ 305 */
309static void 306static void
310run (void *cls, 307run(void *cls,
311 const struct GNUNET_CONFIGURATION_Handle *cfg, 308 const struct GNUNET_CONFIGURATION_Handle *cfg,
312 struct GNUNET_SERVICE_Handle *service) 309 struct GNUNET_SERVICE_Handle *service)
313{ 310{
314 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 311 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
315 if (NULL == my_private_key) 312 if (NULL == my_private_key)
316 { 313 {
317 GNUNET_SCHEDULER_shutdown (); 314 GNUNET_SCHEDULER_shutdown();
318 return; 315 return;
319 } 316 }
320 dht = GNUNET_DHT_connect (cfg, 1024); 317 dht = GNUNET_DHT_connect(cfg, 1024);
321 if (NULL == dht) 318 if (NULL == dht)
322 { 319 {
323 GNUNET_free (my_private_key); 320 GNUNET_free(my_private_key);
324 my_private_key = NULL; 321 my_private_key = NULL;
325 GNUNET_SCHEDULER_shutdown (); 322 GNUNET_SCHEDULER_shutdown();
326 return; 323 return;
327 } 324 }
328 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, 325 GNUNET_SCHEDULER_add_shutdown(&cleanup_task,
329 NULL); 326 NULL);
330 stats = GNUNET_STATISTICS_create ("regex", cfg); 327 stats = GNUNET_STATISTICS_create("regex", cfg);
331} 328}
332 329
333 330
@@ -340,13 +337,13 @@ run (void *cls,
340 * @return @a c 337 * @return @a c
341 */ 338 */
342static void * 339static void *
343client_connect_cb (void *cls, 340client_connect_cb(void *cls,
344 struct GNUNET_SERVICE_Client *c, 341 struct GNUNET_SERVICE_Client *c,
345 struct GNUNET_MQ_Handle *mq) 342 struct GNUNET_MQ_Handle *mq)
346{ 343{
347 struct ClientEntry *ce; 344 struct ClientEntry *ce;
348 345
349 ce = GNUNET_new (struct ClientEntry); 346 ce = GNUNET_new(struct ClientEntry);
350 ce->client = c; 347 ce->client = c;
351 ce->mq = mq; 348 ce->mq = mq;
352 return ce; 349 return ce;
@@ -361,28 +358,28 @@ client_connect_cb (void *cls,
361 * @param internal_cls should be equal to @a c 358 * @param internal_cls should be equal to @a c
362 */ 359 */
363static void 360static void
364client_disconnect_cb (void *cls, 361client_disconnect_cb(void *cls,
365 struct GNUNET_SERVICE_Client *c, 362 struct GNUNET_SERVICE_Client *c,
366 void *internal_cls) 363 void *internal_cls)
367{ 364{
368 struct ClientEntry *ce = internal_cls; 365 struct ClientEntry *ce = internal_cls;
369 366
370 if (NULL != ce->refresh_task) 367 if (NULL != ce->refresh_task)
371 { 368 {
372 GNUNET_SCHEDULER_cancel (ce->refresh_task); 369 GNUNET_SCHEDULER_cancel(ce->refresh_task);
373 ce->refresh_task = NULL; 370 ce->refresh_task = NULL;
374 } 371 }
375 if (NULL != ce->ah) 372 if (NULL != ce->ah)
376 { 373 {
377 REGEX_INTERNAL_announce_cancel (ce->ah); 374 REGEX_INTERNAL_announce_cancel(ce->ah);
378 ce->ah = NULL; 375 ce->ah = NULL;
379 } 376 }
380 if (NULL != ce->sh) 377 if (NULL != ce->sh)
381 { 378 {
382 REGEX_INTERNAL_search_cancel (ce->sh); 379 REGEX_INTERNAL_search_cancel(ce->sh);
383 ce->sh = NULL; 380 ce->sh = NULL;
384 } 381 }
385 GNUNET_free (ce); 382 GNUNET_free(ce);
386} 383}
387 384
388 385
@@ -390,21 +387,21 @@ client_disconnect_cb (void *cls,
390 * Define "main" method using service macro. 387 * Define "main" method using service macro.
391 */ 388 */
392GNUNET_SERVICE_MAIN 389GNUNET_SERVICE_MAIN
393("regex", 390 ("regex",
394 GNUNET_SERVICE_OPTION_NONE, 391 GNUNET_SERVICE_OPTION_NONE,
395 &run, 392 &run,
396 &client_connect_cb, 393 &client_connect_cb,
397 &client_disconnect_cb, 394 &client_disconnect_cb,
398 NULL, 395 NULL,
399 GNUNET_MQ_hd_var_size (announce, 396 GNUNET_MQ_hd_var_size(announce,
400 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 397 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE,
401 struct AnnounceMessage, 398 struct AnnounceMessage,
402 NULL), 399 NULL),
403 GNUNET_MQ_hd_var_size (search, 400 GNUNET_MQ_hd_var_size(search,
404 GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 401 GNUNET_MESSAGE_TYPE_REGEX_SEARCH,
405 struct RegexSearchMessage, 402 struct RegexSearchMessage,
406 NULL), 403 NULL),
407 GNUNET_MQ_handler_end ()); 404 GNUNET_MQ_handler_end());
408 405
409 406
410/* end of gnunet-service-regex.c */ 407/* end of gnunet-service-regex.c */
diff --git a/src/regex/perf-regex.c b/src/regex/perf-regex.c
index ea331eed0..25960dc9f 100644
--- a/src/regex/perf-regex.c
+++ b/src/regex/perf-regex.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/regex/perf-regex.c 22 * @file src/regex/perf-regex.c
@@ -42,23 +42,23 @@
42 * @param edges edges leaving current state. 42 * @param edges edges leaving current state.
43 */ 43 */
44static void 44static void
45print_edge (void *cls, 45print_edge(void *cls,
46 const struct GNUNET_HashCode *key, 46 const struct GNUNET_HashCode *key,
47 const char *proof, 47 const char *proof,
48 int accepting, 48 int accepting,
49 unsigned int num_edges, 49 unsigned int num_edges,
50 const struct REGEX_BLOCK_Edge *edges) 50 const struct REGEX_BLOCK_Edge *edges)
51{ 51{
52 unsigned int i; 52 unsigned int i;
53 53
54 printf ("%s: %s, proof: `%s'\n", 54 printf("%s: %s, proof: `%s'\n",
55 GNUNET_h2s (key), 55 GNUNET_h2s(key),
56 accepting ? "ACCEPTING" : "", 56 accepting ? "ACCEPTING" : "",
57 proof); 57 proof);
58 for (i = 0; i < num_edges; i++) 58 for (i = 0; i < num_edges; i++)
59 printf (" `%s': %s\n", 59 printf(" `%s': %s\n",
60 edges[i].label, 60 edges[i].label,
61 GNUNET_h2s (&edges[i].destination)); 61 GNUNET_h2s(&edges[i].destination));
62} 62}
63 63
64 64
@@ -73,7 +73,7 @@ print_edge (void *cls,
73 * @return 0 ok, 1 on error 73 * @return 0 ok, 1 on error
74 */ 74 */
75int 75int
76main (int argc, char *const *argv) 76main(int argc, char *const *argv)
77{ 77{
78 struct REGEX_INTERNAL_Automaton* dfa; 78 struct REGEX_INTERNAL_Automaton* dfa;
79 char **regexes; 79 char **regexes;
@@ -83,45 +83,45 @@ main (int argc, char *const *argv)
83 unsigned int alphabet_size; 83 unsigned int alphabet_size;
84 long size; 84 long size;
85 85
86 GNUNET_log_setup ("perf-regex", "DEBUG", NULL); 86 GNUNET_log_setup("perf-regex", "DEBUG", NULL);
87 if (4 != argc) 87 if (4 != argc)
88 { 88 {
89 fprintf (stderr, 89 fprintf(stderr,
90 "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n", 90 "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n",
91 argv[0]); 91 argv[0]);
92 return 1; 92 return 1;
93 } 93 }
94 regexes = REGEX_TEST_read_from_file (argv[1]); 94 regexes = REGEX_TEST_read_from_file(argv[1]);
95 if (NULL == regexes) 95 if (NULL == regexes)
96 { 96 {
97 fprintf (stderr, 97 fprintf(stderr,
98 "Failed to read regexes from `%s'\n", 98 "Failed to read regexes from `%s'\n",
99 argv[1]); 99 argv[1]);
100 return 2; 100 return 2;
101 } 101 }
102 alphabet_size = atoi (argv[2]); 102 alphabet_size = atoi(argv[2]);
103 compression = atoi (argv[3]); 103 compression = atoi(argv[3]);
104 printf ("********* PERF-REGEX *********'\n"); 104 printf("********* PERF-REGEX *********'\n");
105 printf ("Using:\n file '%s'\n Alphabet size %u\n compression %d\n", 105 printf("Using:\n file '%s'\n Alphabet size %u\n compression %d\n",
106 argv[1], alphabet_size, compression); 106 argv[1], alphabet_size, compression);
107 fflush(stdout); 107 fflush(stdout);
108 buffer = REGEX_TEST_combine (regexes, alphabet_size); 108 buffer = REGEX_TEST_combine(regexes, alphabet_size);
109 GNUNET_asprintf (&regex, "GNUNET_REGEX_PROFILER_(%s)(0|1)*", buffer); 109 GNUNET_asprintf(&regex, "GNUNET_REGEX_PROFILER_(%s)(0|1)*", buffer);
110 size = strlen (regex); 110 size = strlen(regex);
111 111
112 fprintf (stderr, 112 fprintf(stderr,
113 "Combined regex (%ld bytes):\n%s\n", 113 "Combined regex (%ld bytes):\n%s\n",
114 size, 114 size,
115 regex); 115 regex);
116 dfa = REGEX_INTERNAL_construct_dfa (regex, size, compression); 116 dfa = REGEX_INTERNAL_construct_dfa(regex, size, compression);
117 printf ("********* ALL EDGES *********'\n"); 117 printf("********* ALL EDGES *********'\n");
118 REGEX_INTERNAL_iterate_all_edges (dfa, &print_edge, NULL); 118 REGEX_INTERNAL_iterate_all_edges(dfa, &print_edge, NULL);
119 printf ("\n\n********* REACHABLE EDGES *********'\n"); 119 printf("\n\n********* REACHABLE EDGES *********'\n");
120 REGEX_INTERNAL_iterate_reachable_edges (dfa, &print_edge, NULL); 120 REGEX_INTERNAL_iterate_reachable_edges(dfa, &print_edge, NULL);
121 REGEX_INTERNAL_automaton_destroy (dfa); 121 REGEX_INTERNAL_automaton_destroy(dfa);
122 GNUNET_free (buffer); 122 GNUNET_free(buffer);
123 REGEX_TEST_free_from_file (regexes); 123 REGEX_TEST_free_from_file(regexes);
124 GNUNET_free (regex); 124 GNUNET_free(regex);
125 return 0; 125 return 0;
126} 126}
127 127
diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c
index bc2d287c5..9467c02b9 100644
--- a/src/regex/plugin_block_regex.c
+++ b/src/regex/plugin_block_regex.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/plugin_block_regex.c 22 * @file regex/plugin_block_regex.c
@@ -57,37 +57,37 @@
57 * by this @a type of block (this is not an error) 57 * by this @a type of block (this is not an error)
58 */ 58 */
59static struct GNUNET_BLOCK_Group * 59static struct GNUNET_BLOCK_Group *
60block_plugin_regex_create_group (void *cls, 60block_plugin_regex_create_group(void *cls,
61 enum GNUNET_BLOCK_Type type, 61 enum GNUNET_BLOCK_Type type,
62 uint32_t nonce, 62 uint32_t nonce,
63 const void *raw_data, 63 const void *raw_data,
64 size_t raw_data_size, 64 size_t raw_data_size,
65 va_list va) 65 va_list va)
66{ 66{
67 unsigned int bf_size; 67 unsigned int bf_size;
68 const char *guard; 68 const char *guard;
69 69
70 guard = va_arg (va, const char *); 70 guard = va_arg(va, const char *);
71 if (0 == strcmp (guard, 71 if (0 == strcmp(guard,
72 "seen-set-size")) 72 "seen-set-size"))
73 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int), 73 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size(va_arg(va, unsigned int),
74 BLOOMFILTER_K); 74 BLOOMFILTER_K);
75 else if (0 == strcmp (guard, 75 else if (0 == strcmp(guard,
76 "filter-size")) 76 "filter-size"))
77 bf_size = va_arg (va, unsigned int); 77 bf_size = va_arg(va, unsigned int);
78 else 78 else
79 { 79 {
80 GNUNET_break (0); 80 GNUNET_break(0);
81 bf_size = REGEX_BF_SIZE; 81 bf_size = REGEX_BF_SIZE;
82 } 82 }
83 GNUNET_break (NULL == va_arg (va, const char *)); 83 GNUNET_break(NULL == va_arg(va, const char *));
84 return GNUNET_BLOCK_GROUP_bf_create (cls, 84 return GNUNET_BLOCK_GROUP_bf_create(cls,
85 bf_size, 85 bf_size,
86 BLOOMFILTER_K, 86 BLOOMFILTER_K,
87 type, 87 type,
88 nonce, 88 nonce,
89 raw_data, 89 raw_data,
90 raw_data_size); 90 raw_data_size);
91} 91}
92 92
93 93
@@ -111,72 +111,74 @@ block_plugin_regex_create_group (void *cls,
111 * @return characterization of result 111 * @return characterization of result
112 */ 112 */
113static enum GNUNET_BLOCK_EvaluationResult 113static enum GNUNET_BLOCK_EvaluationResult
114evaluate_block_regex (void *cls, 114evaluate_block_regex(void *cls,
115 enum GNUNET_BLOCK_Type type, 115 enum GNUNET_BLOCK_Type type,
116 struct GNUNET_BLOCK_Group *bg, 116 struct GNUNET_BLOCK_Group *bg,
117 enum GNUNET_BLOCK_EvaluationOptions eo, 117 enum GNUNET_BLOCK_EvaluationOptions eo,
118 const struct GNUNET_HashCode *query, 118 const struct GNUNET_HashCode *query,
119 const void *xquery, 119 const void *xquery,
120 size_t xquery_size, 120 size_t xquery_size,
121 const void *reply_block, 121 const void *reply_block,
122 size_t reply_block_size) 122 size_t reply_block_size)
123{ 123{
124 struct GNUNET_HashCode chash; 124 struct GNUNET_HashCode chash;
125 125
126 if (NULL == reply_block) 126 if (NULL == reply_block)
127 { 127 {
128 if (0 != xquery_size) 128 if (0 != xquery_size)
129 { 129 {
130 const char *s; 130 const char *s;
131 131
132 s = (const char *) xquery; 132 s = (const char *)xquery;
133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ 133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
134 { 134 {
135 GNUNET_break_op (0); 135 GNUNET_break_op(0);
136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 } 137 }
138 } 138 }
139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; 139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
140 } 140 }
141 if (0 != xquery_size) 141 if (0 != xquery_size)
142 {
143 const char *s;
144
145 s = (const char *) xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 { 142 {
148 GNUNET_break_op (0); 143 const char *s;
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 144
145 s = (const char *)xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 {
148 GNUNET_break_op(0);
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150 }
150 } 151 }
151 }
152 else if (NULL != query) 152 else if (NULL != query)
153 { 153 {
154 /* xquery is required for regex GETs, at least an empty string */ 154 /* xquery is required for regex GETs, at least an empty string */
155 GNUNET_break_op (0); 155 GNUNET_break_op(0);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n", 156 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
157 type, query, xquery); 157 type, query, xquery);
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159 } 159 }
160 switch (REGEX_BLOCK_check (reply_block, 160 switch (REGEX_BLOCK_check(reply_block,
161 reply_block_size, 161 reply_block_size,
162 query, 162 query,
163 xquery)) 163 xquery))
164 { 164 {
165 case GNUNET_SYSERR: 165 case GNUNET_SYSERR:
166 GNUNET_break_op(0); 166 GNUNET_break_op(0);
167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
168
168 case GNUNET_NO: 169 case GNUNET_NO:
169 /* xquery missmatch, can happen */ 170 /* xquery missmatch, can happen */
170 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT; 171 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
172
171 default: 173 default:
172 break; 174 break;
173 } 175 }
174 GNUNET_CRYPTO_hash (reply_block, 176 GNUNET_CRYPTO_hash(reply_block,
175 reply_block_size, 177 reply_block_size,
176 &chash); 178 &chash);
177 if (GNUNET_YES == 179 if (GNUNET_YES ==
178 GNUNET_BLOCK_GROUP_bf_test_and_set (bg, 180 GNUNET_BLOCK_GROUP_bf_test_and_set(bg,
179 &chash)) 181 &chash))
180 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 182 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
181 return GNUNET_BLOCK_EVALUATION_OK_MORE; 183 return GNUNET_BLOCK_EVALUATION_OK_MORE;
182} 184}
@@ -202,60 +204,60 @@ evaluate_block_regex (void *cls,
202 * @return characterization of result 204 * @return characterization of result
203 */ 205 */
204static enum GNUNET_BLOCK_EvaluationResult 206static enum GNUNET_BLOCK_EvaluationResult
205evaluate_block_regex_accept (void *cls, 207evaluate_block_regex_accept(void *cls,
206 enum GNUNET_BLOCK_Type type, 208 enum GNUNET_BLOCK_Type type,
207 struct GNUNET_BLOCK_Group *bg, 209 struct GNUNET_BLOCK_Group *bg,
208 enum GNUNET_BLOCK_EvaluationOptions eo, 210 enum GNUNET_BLOCK_EvaluationOptions eo,
209 const struct GNUNET_HashCode *query, 211 const struct GNUNET_HashCode *query,
210 const void *xquery, 212 const void *xquery,
211 size_t xquery_size, const void *reply_block, 213 size_t xquery_size, const void *reply_block,
212 size_t reply_block_size) 214 size_t reply_block_size)
213{ 215{
214 const struct RegexAcceptBlock *rba; 216 const struct RegexAcceptBlock *rba;
215 struct GNUNET_HashCode chash; 217 struct GNUNET_HashCode chash;
216 218
217 if (0 != xquery_size) 219 if (0 != xquery_size)
218 { 220 {
219 GNUNET_break_op (0); 221 GNUNET_break_op(0);
220 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 222 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
221 } 223 }
222 if (NULL == reply_block) 224 if (NULL == reply_block)
223 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; 225 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
224 if (sizeof (struct RegexAcceptBlock) != reply_block_size) 226 if (sizeof(struct RegexAcceptBlock) != reply_block_size)
225 { 227 {
226 GNUNET_break_op(0); 228 GNUNET_break_op(0);
227 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 229 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
228 } 230 }
229 rba = reply_block; 231 rba = reply_block;
230 if (ntohl (rba->purpose.size) != 232 if (ntohl(rba->purpose.size) !=
231 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 233 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
232 sizeof (struct GNUNET_TIME_AbsoluteNBO) + 234 sizeof(struct GNUNET_TIME_AbsoluteNBO) +
233 sizeof (struct GNUNET_HashCode)) 235 sizeof(struct GNUNET_HashCode))
234 { 236 {
235 GNUNET_break_op(0); 237 GNUNET_break_op(0);
236 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 238 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
237 } 239 }
238 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us) 240 if (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(rba->expiration_time)).rel_value_us)
239 { 241 {
240 /* technically invalid, but can happen without an error, so 242 /* technically invalid, but can happen without an error, so
241 we're nice by reporting it as a 'duplicate' */ 243 we're nice by reporting it as a 'duplicate' */
242 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 244 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
243 } 245 }
244 if (GNUNET_OK != 246 if (GNUNET_OK !=
245 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, 247 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
246 &rba->purpose, 248 &rba->purpose,
247 &rba->signature, 249 &rba->signature,
248 &rba->peer.public_key)) 250 &rba->peer.public_key))
249 { 251 {
250 GNUNET_break_op(0); 252 GNUNET_break_op(0);
251 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 253 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
252 } 254 }
253 GNUNET_CRYPTO_hash (reply_block, 255 GNUNET_CRYPTO_hash(reply_block,
254 reply_block_size, 256 reply_block_size,
255 &chash); 257 &chash);
256 if (GNUNET_YES == 258 if (GNUNET_YES ==
257 GNUNET_BLOCK_GROUP_bf_test_and_set (bg, 259 GNUNET_BLOCK_GROUP_bf_test_and_set(bg,
258 &chash)) 260 &chash))
259 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 261 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
260 return GNUNET_BLOCK_EVALUATION_OK_MORE; 262 return GNUNET_BLOCK_EVALUATION_OK_MORE;
261} 263}
@@ -281,43 +283,44 @@ evaluate_block_regex_accept (void *cls,
281 * @return characterization of result 283 * @return characterization of result
282 */ 284 */
283static enum GNUNET_BLOCK_EvaluationResult 285static enum GNUNET_BLOCK_EvaluationResult
284block_plugin_regex_evaluate (void *cls, 286block_plugin_regex_evaluate(void *cls,
285 struct GNUNET_BLOCK_Context *ctx, 287 struct GNUNET_BLOCK_Context *ctx,
286 enum GNUNET_BLOCK_Type type, 288 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_BLOCK_Group *bg, 289 struct GNUNET_BLOCK_Group *bg,
288 enum GNUNET_BLOCK_EvaluationOptions eo, 290 enum GNUNET_BLOCK_EvaluationOptions eo,
289 const struct GNUNET_HashCode *query, 291 const struct GNUNET_HashCode *query,
290 const void *xquery, 292 const void *xquery,
291 size_t xquery_size, 293 size_t xquery_size,
292 const void *reply_block, 294 const void *reply_block,
293 size_t reply_block_size) 295 size_t reply_block_size)
294{ 296{
295 enum GNUNET_BLOCK_EvaluationResult result; 297 enum GNUNET_BLOCK_EvaluationResult result;
296 298
297 switch (type) 299 switch (type)
298 { 300 {
299 case GNUNET_BLOCK_TYPE_REGEX: 301 case GNUNET_BLOCK_TYPE_REGEX:
300 result = evaluate_block_regex (cls, 302 result = evaluate_block_regex(cls,
301 type, 303 type,
302 bg, 304 bg,
303 eo, 305 eo,
304 query, 306 query,
305 xquery, xquery_size, 307 xquery, xquery_size,
306 reply_block, reply_block_size); 308 reply_block, reply_block_size);
307 break; 309 break;
310
308 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: 311 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
309 result = evaluate_block_regex_accept (cls, 312 result = evaluate_block_regex_accept(cls,
310 type, 313 type,
311 bg, 314 bg,
312 eo, 315 eo,
313 query, 316 query,
314 xquery, xquery_size, 317 xquery, xquery_size,
315 reply_block, reply_block_size); 318 reply_block, reply_block_size);
316 break; 319 break;
317 320
318 default: 321 default:
319 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 322 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
320 } 323 }
321 return result; 324 return result;
322} 325}
323 326
@@ -334,35 +337,37 @@ block_plugin_regex_evaluate (void *cls,
334 * (or if extracting a key from a block of this type does not work) 337 * (or if extracting a key from a block of this type does not work)
335 */ 338 */
336static int 339static int
337block_plugin_regex_get_key (void *cls, 340block_plugin_regex_get_key(void *cls,
338 enum GNUNET_BLOCK_Type type, 341 enum GNUNET_BLOCK_Type type,
339 const void *block, 342 const void *block,
340 size_t block_size, 343 size_t block_size,
341 struct GNUNET_HashCode *key) 344 struct GNUNET_HashCode *key)
342{ 345{
343 switch (type) 346 switch (type)
344 { 347 {
345 case GNUNET_BLOCK_TYPE_REGEX: 348 case GNUNET_BLOCK_TYPE_REGEX:
346 if (GNUNET_OK != 349 if (GNUNET_OK !=
347 REGEX_BLOCK_get_key (block, block_size, 350 REGEX_BLOCK_get_key(block, block_size,
348 key)) 351 key))
349 { 352 {
350 GNUNET_break_op (0); 353 GNUNET_break_op(0);
351 return GNUNET_NO; 354 return GNUNET_NO;
352 } 355 }
353 return GNUNET_OK; 356 return GNUNET_OK;
357
354 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: 358 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
355 if (sizeof (struct RegexAcceptBlock) != block_size) 359 if (sizeof(struct RegexAcceptBlock) != block_size)
356 { 360 {
357 GNUNET_break_op (0); 361 GNUNET_break_op(0);
358 return GNUNET_NO; 362 return GNUNET_NO;
359 } 363 }
360 *key = ((struct RegexAcceptBlock *) block)->key; 364 *key = ((struct RegexAcceptBlock *)block)->key;
361 return GNUNET_OK; 365 return GNUNET_OK;
366
362 default: 367 default:
363 GNUNET_break (0); 368 GNUNET_break(0);
364 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
365 } 370 }
366} 371}
367 372
368 373
@@ -370,7 +375,7 @@ block_plugin_regex_get_key (void *cls,
370 * Entry point for the plugin. 375 * Entry point for the plugin.
371 */ 376 */
372void * 377void *
373libgnunet_plugin_block_regex_init (void *cls) 378libgnunet_plugin_block_regex_init(void *cls)
374{ 379{
375 static enum GNUNET_BLOCK_Type types[] = 380 static enum GNUNET_BLOCK_Type types[] =
376 { 381 {
@@ -380,7 +385,7 @@ libgnunet_plugin_block_regex_init (void *cls)
380 }; 385 };
381 struct GNUNET_BLOCK_PluginFunctions *api; 386 struct GNUNET_BLOCK_PluginFunctions *api;
382 387
383 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); 388 api = GNUNET_new(struct GNUNET_BLOCK_PluginFunctions);
384 api->evaluate = &block_plugin_regex_evaluate; 389 api->evaluate = &block_plugin_regex_evaluate;
385 api->get_key = &block_plugin_regex_get_key; 390 api->get_key = &block_plugin_regex_get_key;
386 api->create_group = &block_plugin_regex_create_group; 391 api->create_group = &block_plugin_regex_create_group;
@@ -393,11 +398,11 @@ libgnunet_plugin_block_regex_init (void *cls)
393 * Exit point from the plugin. 398 * Exit point from the plugin.
394 */ 399 */
395void * 400void *
396libgnunet_plugin_block_regex_done (void *cls) 401libgnunet_plugin_block_regex_done(void *cls)
397{ 402{
398 struct GNUNET_BLOCK_PluginFunctions *api = cls; 403 struct GNUNET_BLOCK_PluginFunctions *api = cls;
399 404
400 GNUNET_free (api); 405 GNUNET_free(api);
401 return NULL; 406 return NULL;
402} 407}
403 408
diff --git a/src/regex/regex_api_announce.c b/src/regex/regex_api_announce.c
index e1af0b7a9..3b5801994 100644
--- a/src/regex/regex_api_announce.c
+++ b/src/regex/regex_api_announce.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/regex_api_announce.c 21 * @file regex/regex_api_announce.c
22 * @brief access regex service to advertise capabilities via regex 22 * @brief access regex service to advertise capabilities via regex
@@ -29,13 +29,12 @@
29#include "gnunet_regex_service.h" 29#include "gnunet_regex_service.h"
30#include "regex_ipc.h" 30#include "regex_ipc.h"
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind, "regex-api", __VA_ARGS__)
33 33
34/** 34/**
35 * Handle to store cached data about a regex announce. 35 * Handle to store cached data about a regex announce.
36 */ 36 */
37struct GNUNET_REGEX_Announcement 37struct GNUNET_REGEX_Announcement {
38{
39 /** 38 /**
40 * Connection to the regex service. 39 * Connection to the regex service.
41 */ 40 */
@@ -70,7 +69,7 @@ struct GNUNET_REGEX_Announcement
70 * @param a REGEX to announce. 69 * @param a REGEX to announce.
71 */ 70 */
72static void 71static void
73announce_reconnect (struct GNUNET_REGEX_Announcement *a); 72announce_reconnect(struct GNUNET_REGEX_Announcement *a);
74 73
75 74
76/** 75/**
@@ -81,14 +80,14 @@ announce_reconnect (struct GNUNET_REGEX_Announcement *a);
81 * @param error error code 80 * @param error error code
82 */ 81 */
83static void 82static void
84announce_mq_error_handler (void *cls, 83announce_mq_error_handler(void *cls,
85 enum GNUNET_MQ_Error error) 84 enum GNUNET_MQ_Error error)
86{ 85{
87 struct GNUNET_REGEX_Announcement *a = cls; 86 struct GNUNET_REGEX_Announcement *a = cls;
88 87
89 GNUNET_MQ_destroy (a->mq); 88 GNUNET_MQ_destroy(a->mq);
90 a->mq = NULL; 89 a->mq = NULL;
91 announce_reconnect (a); 90 announce_reconnect(a);
92} 91}
93 92
94 93
@@ -98,31 +97,31 @@ announce_mq_error_handler (void *cls,
98 * @param a REGEX to announce. 97 * @param a REGEX to announce.
99 */ 98 */
100static void 99static void
101announce_reconnect (struct GNUNET_REGEX_Announcement *a) 100announce_reconnect(struct GNUNET_REGEX_Announcement *a)
102{ 101{
103 struct GNUNET_MQ_Envelope *env; 102 struct GNUNET_MQ_Envelope *env;
104 struct AnnounceMessage *am; 103 struct AnnounceMessage *am;
105 size_t slen; 104 size_t slen;
106 105
107 a->mq = GNUNET_CLIENT_connect (a->cfg, 106 a->mq = GNUNET_CLIENT_connect(a->cfg,
108 "regex", 107 "regex",
109 NULL, 108 NULL,
110 &announce_mq_error_handler, 109 &announce_mq_error_handler,
111 a); 110 a);
112 if (NULL == a->mq) 111 if (NULL == a->mq)
113 return; 112 return;
114 slen = strlen (a->regex) + 1; 113 slen = strlen(a->regex) + 1;
115 env = GNUNET_MQ_msg_extra (am, 114 env = GNUNET_MQ_msg_extra(am,
116 slen, 115 slen,
117 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE); 116 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE);
118 am->compression = htons (a->compression); 117 am->compression = htons(a->compression);
119 am->reserved = htons (0); 118 am->reserved = htons(0);
120 am->refresh_delay = GNUNET_TIME_relative_hton (a->refresh_delay); 119 am->refresh_delay = GNUNET_TIME_relative_hton(a->refresh_delay);
121 GNUNET_memcpy (&am[1], 120 GNUNET_memcpy(&am[1],
122 a->regex, 121 a->regex,
123 slen); 122 slen);
124 GNUNET_MQ_send (a->mq, 123 GNUNET_MQ_send(a->mq,
125 env); 124 env);
126} 125}
127 126
128 127
@@ -137,35 +136,35 @@ announce_reconnect (struct GNUNET_REGEX_Announcement *a)
137 * Must be freed by calling #GNUNET_REGEX_announce_cancel(). 136 * Must be freed by calling #GNUNET_REGEX_announce_cancel().
138 */ 137 */
139struct GNUNET_REGEX_Announcement * 138struct GNUNET_REGEX_Announcement *
140GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg, 139GNUNET_REGEX_announce(const struct GNUNET_CONFIGURATION_Handle *cfg,
141 const char *regex, 140 const char *regex,
142 struct GNUNET_TIME_Relative refresh_delay, 141 struct GNUNET_TIME_Relative refresh_delay,
143 uint16_t compression) 142 uint16_t compression)
144{ 143{
145 struct GNUNET_REGEX_Announcement *a; 144 struct GNUNET_REGEX_Announcement *a;
146 size_t slen; 145 size_t slen;
147 146
148 slen = strlen (regex) + 1; 147 slen = strlen(regex) + 1;
149 if (slen + sizeof (struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE) 148 if (slen + sizeof(struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE)
150 { 149 {
151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 150 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
152 _("Regex `%s' is too long!\n"), 151 _("Regex `%s' is too long!\n"),
153 regex); 152 regex);
154 GNUNET_break (0); 153 GNUNET_break(0);
155 return NULL; 154 return NULL;
156 } 155 }
157 a = GNUNET_new (struct GNUNET_REGEX_Announcement); 156 a = GNUNET_new(struct GNUNET_REGEX_Announcement);
158 a->cfg = cfg; 157 a->cfg = cfg;
159 a->refresh_delay = refresh_delay; 158 a->refresh_delay = refresh_delay;
160 a->compression = compression; 159 a->compression = compression;
161 a->regex = GNUNET_strdup (regex); 160 a->regex = GNUNET_strdup(regex);
162 announce_reconnect (a); 161 announce_reconnect(a);
163 if (NULL == a->mq) 162 if (NULL == a->mq)
164 { 163 {
165 GNUNET_free (a->regex); 164 GNUNET_free(a->regex);
166 GNUNET_free (a); 165 GNUNET_free(a);
167 return NULL; 166 return NULL;
168 } 167 }
169 return a; 168 return a;
170} 169}
171 170
@@ -176,11 +175,11 @@ GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg,
176 * @param a handle returned by a previous #GNUNET_REGEX_announce() call. 175 * @param a handle returned by a previous #GNUNET_REGEX_announce() call.
177 */ 176 */
178void 177void
179GNUNET_REGEX_announce_cancel (struct GNUNET_REGEX_Announcement *a) 178GNUNET_REGEX_announce_cancel(struct GNUNET_REGEX_Announcement *a)
180{ 179{
181 GNUNET_MQ_destroy (a->mq); 180 GNUNET_MQ_destroy(a->mq);
182 GNUNET_free (a->regex); 181 GNUNET_free(a->regex);
183 GNUNET_free (a); 182 GNUNET_free(a);
184} 183}
185 184
186/* end of regex_api_announce.c */ 185/* end of regex_api_announce.c */
diff --git a/src/regex/regex_api_search.c b/src/regex/regex_api_search.c
index ebd1a69f3..6a6a49a81 100644
--- a/src/regex/regex_api_search.c
+++ b/src/regex/regex_api_search.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/regex_api_search.c 21 * @file regex/regex_api_search.c
22 * @brief access regex service to discover 22 * @brief access regex service to discover
@@ -30,14 +30,13 @@
30#include "gnunet_regex_service.h" 30#include "gnunet_regex_service.h"
31#include "regex_ipc.h" 31#include "regex_ipc.h"
32 32
33#define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from(kind, "regex-api", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * Handle to store data about a regex search. 37 * Handle to store data about a regex search.
38 */ 38 */
39struct GNUNET_REGEX_Search 39struct GNUNET_REGEX_Search {
40{
41 /** 40 /**
42 * Connection to the regex service. 41 * Connection to the regex service.
43 */ 42 */
@@ -71,7 +70,7 @@ struct GNUNET_REGEX_Search
71 * @param s context for the search search for 70 * @param s context for the search search for
72 */ 71 */
73static void 72static void
74search_reconnect (struct GNUNET_REGEX_Search *s); 73search_reconnect(struct GNUNET_REGEX_Search *s);
75 74
76 75
77/** 76/**
@@ -83,18 +82,18 @@ search_reconnect (struct GNUNET_REGEX_Search *s);
83 * @return #GNUNET_SYSERR if @a rm is not well-formed. 82 * @return #GNUNET_SYSERR if @a rm is not well-formed.
84 */ 83 */
85static int 84static int
86check_search_response (void *cls, 85check_search_response(void *cls,
87 const struct ResultMessage *result) 86 const struct ResultMessage *result)
88{ 87{
89 uint16_t size = ntohs (result->header.size) - sizeof (*result); 88 uint16_t size = ntohs(result->header.size) - sizeof(*result);
90 uint16_t gpl = ntohs (result->get_path_length); 89 uint16_t gpl = ntohs(result->get_path_length);
91 uint16_t ppl = ntohs (result->put_path_length); 90 uint16_t ppl = ntohs(result->put_path_length);
92 91
93 if (size != (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity)) 92 if (size != (gpl + ppl) * sizeof(struct GNUNET_PeerIdentity))
94 { 93 {
95 GNUNET_break (0); 94 GNUNET_break(0);
96 return GNUNET_SYSERR; 95 return GNUNET_SYSERR;
97 } 96 }
98 return GNUNET_OK; 97 return GNUNET_OK;
99} 98}
100 99
@@ -107,24 +106,24 @@ check_search_response (void *cls,
107 * @param result the message 106 * @param result the message
108 */ 107 */
109static void 108static void
110handle_search_response (void *cls, 109handle_search_response(void *cls,
111 const struct ResultMessage *result) 110 const struct ResultMessage *result)
112{ 111{
113 struct GNUNET_REGEX_Search *s = cls; 112 struct GNUNET_REGEX_Search *s = cls;
114 uint16_t gpl = ntohs (result->get_path_length); 113 uint16_t gpl = ntohs(result->get_path_length);
115 uint16_t ppl = ntohs (result->put_path_length); 114 uint16_t ppl = ntohs(result->put_path_length);
116 const struct GNUNET_PeerIdentity *pid; 115 const struct GNUNET_PeerIdentity *pid;
117 116
118 pid = &result->id; 117 pid = &result->id;
119 LOG (GNUNET_ERROR_TYPE_DEBUG, 118 LOG(GNUNET_ERROR_TYPE_DEBUG,
120 "Got regex result %s\n", 119 "Got regex result %s\n",
121 GNUNET_i2s (pid)); 120 GNUNET_i2s(pid));
122 s->callback (s->callback_cls, 121 s->callback(s->callback_cls,
123 pid, 122 pid,
124 &pid[1], 123 &pid[1],
125 gpl, 124 gpl,
126 &pid[1 + gpl], 125 &pid[1 + gpl],
127 ppl); 126 ppl);
128} 127}
129 128
130 129
@@ -136,14 +135,14 @@ handle_search_response (void *cls,
136 * @param error error code 135 * @param error error code
137 */ 136 */
138static void 137static void
139mq_error_handler (void *cls, 138mq_error_handler(void *cls,
140 enum GNUNET_MQ_Error error) 139 enum GNUNET_MQ_Error error)
141{ 140{
142 struct GNUNET_REGEX_Search *s = cls; 141 struct GNUNET_REGEX_Search *s = cls;
143 142
144 GNUNET_MQ_destroy (s->mq); 143 GNUNET_MQ_destroy(s->mq);
145 s->mq = NULL; 144 s->mq = NULL;
146 search_reconnect (s); 145 search_reconnect(s);
147} 146}
148 147
149 148
@@ -153,35 +152,35 @@ mq_error_handler (void *cls,
153 * @param s context for the search search for 152 * @param s context for the search search for
154 */ 153 */
155static void 154static void
156search_reconnect (struct GNUNET_REGEX_Search *s) 155search_reconnect(struct GNUNET_REGEX_Search *s)
157{ 156{
158 struct GNUNET_MQ_MessageHandler handlers[] = { 157 struct GNUNET_MQ_MessageHandler handlers[] = {
159 GNUNET_MQ_hd_var_size (search_response, 158 GNUNET_MQ_hd_var_size(search_response,
160 GNUNET_MESSAGE_TYPE_REGEX_RESULT, 159 GNUNET_MESSAGE_TYPE_REGEX_RESULT,
161 struct ResultMessage, 160 struct ResultMessage,
162 s), 161 s),
163 GNUNET_MQ_handler_end () 162 GNUNET_MQ_handler_end()
164 }; 163 };
165 size_t slen = strlen (s->string) + 1; 164 size_t slen = strlen(s->string) + 1;
166 struct GNUNET_MQ_Envelope *env; 165 struct GNUNET_MQ_Envelope *env;
167 struct RegexSearchMessage *rsm; 166 struct RegexSearchMessage *rsm;
168 167
169 GNUNET_assert (NULL == s->mq); 168 GNUNET_assert(NULL == s->mq);
170 s->mq = GNUNET_CLIENT_connect (s->cfg, 169 s->mq = GNUNET_CLIENT_connect(s->cfg,
171 "regex", 170 "regex",
172 handlers, 171 handlers,
173 &mq_error_handler, 172 &mq_error_handler,
174 s); 173 s);
175 if (NULL == s->mq) 174 if (NULL == s->mq)
176 return; 175 return;
177 env = GNUNET_MQ_msg_extra (rsm, 176 env = GNUNET_MQ_msg_extra(rsm,
178 slen, 177 slen,
179 GNUNET_MESSAGE_TYPE_REGEX_SEARCH); 178 GNUNET_MESSAGE_TYPE_REGEX_SEARCH);
180 GNUNET_memcpy (&rsm[1], 179 GNUNET_memcpy(&rsm[1],
181 s->string, 180 s->string,
182 slen); 181 slen);
183 GNUNET_MQ_send (s->mq, 182 GNUNET_MQ_send(s->mq,
184 env); 183 env);
185} 184}
186 185
187 186
@@ -198,37 +197,37 @@ search_reconnect (struct GNUNET_REGEX_Search *s)
198 * Must be freed by calling #GNUNET_REGEX_search_cancel(). 197 * Must be freed by calling #GNUNET_REGEX_search_cancel().
199 */ 198 */
200struct GNUNET_REGEX_Search * 199struct GNUNET_REGEX_Search *
201GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg, 200GNUNET_REGEX_search(const struct GNUNET_CONFIGURATION_Handle *cfg,
202 const char *string, 201 const char *string,
203 GNUNET_REGEX_Found callback, 202 GNUNET_REGEX_Found callback,
204 void *callback_cls) 203 void *callback_cls)
205{ 204{
206 struct GNUNET_REGEX_Search *s; 205 struct GNUNET_REGEX_Search *s;
207 size_t slen = strlen (string) + 1; 206 size_t slen = strlen(string) + 1;
208 207
209 if (slen + sizeof (struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE) 208 if (slen + sizeof(struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE)
210 { 209 {
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 210 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
212 _("Search string `%s' is too long!\n"), 211 _("Search string `%s' is too long!\n"),
213 string); 212 string);
214 GNUNET_break (0); 213 GNUNET_break(0);
215 return NULL; 214 return NULL;
216 } 215 }
217 LOG (GNUNET_ERROR_TYPE_DEBUG, 216 LOG(GNUNET_ERROR_TYPE_DEBUG,
218 "Starting regex search for %s\n", 217 "Starting regex search for %s\n",
219 string); 218 string);
220 s = GNUNET_new (struct GNUNET_REGEX_Search); 219 s = GNUNET_new(struct GNUNET_REGEX_Search);
221 s->cfg = cfg; 220 s->cfg = cfg;
222 s->string = GNUNET_strdup (string); 221 s->string = GNUNET_strdup(string);
223 s->callback = callback; 222 s->callback = callback;
224 s->callback_cls = callback_cls; 223 s->callback_cls = callback_cls;
225 search_reconnect (s); 224 search_reconnect(s);
226 if (NULL == s->mq) 225 if (NULL == s->mq)
227 { 226 {
228 GNUNET_free (s->string); 227 GNUNET_free(s->string);
229 GNUNET_free (s); 228 GNUNET_free(s);
230 return NULL; 229 return NULL;
231 } 230 }
232 return s; 231 return s;
233} 232}
234 233
@@ -239,11 +238,11 @@ GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg,
239 * @param s Handle returned by a previous #GNUNET_REGEX_search() call. 238 * @param s Handle returned by a previous #GNUNET_REGEX_search() call.
240 */ 239 */
241void 240void
242GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s) 241GNUNET_REGEX_search_cancel(struct GNUNET_REGEX_Search *s)
243{ 242{
244 GNUNET_MQ_destroy (s->mq); 243 GNUNET_MQ_destroy(s->mq);
245 GNUNET_free (s->string); 244 GNUNET_free(s->string);
246 GNUNET_free (s); 245 GNUNET_free(s);
247} 246}
248 247
249 248
diff --git a/src/regex/regex_block_lib.c b/src/regex/regex_block_lib.c
index 77efa6180..f0feb3628 100644
--- a/src/regex/regex_block_lib.c
+++ b/src/regex/regex_block_lib.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @author Bartlomiej Polot 21 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c 22 * @file regex/regex_block_lib.c
@@ -27,15 +27,14 @@
27#include "regex_block_lib.h" 27#include "regex_block_lib.h"
28#include "gnunet_constants.h" 28#include "gnunet_constants.h"
29 29
30#define LOG(kind,...) GNUNET_log_from (kind,"regex-bck",__VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from(kind, "regex-bck", __VA_ARGS__)
31 31
32GNUNET_NETWORK_STRUCT_BEGIN 32GNUNET_NETWORK_STRUCT_BEGIN
33 33
34/** 34/**
35 * Information for each edge. 35 * Information for each edge.
36 */ 36 */
37struct EdgeInfo 37struct EdgeInfo {
38{
39 /** 38 /**
40 * Index of the destination of this edge in the 39 * Index of the destination of this edge in the
41 * unique destinations array. 40 * unique destinations array.
@@ -53,9 +52,7 @@ struct EdgeInfo
53/** 52/**
54 * @brief Block to announce a regex state. 53 * @brief Block to announce a regex state.
55 */ 54 */
56struct RegexBlock 55struct RegexBlock {
57{
58
59 /** 56 /**
60 * Length of the proof regex string. 57 * Length of the proof regex string.
61 */ 58 */
@@ -85,7 +82,6 @@ struct RegexBlock
85 /* followed by 'char tokens[num_edges][edge_info[k].token_length]'; 82 /* followed by 'char tokens[num_edges][edge_info[k].token_length]';
86 essentially all of the tokens one after the other in the 83 essentially all of the tokens one after the other in the
87 order of the edges; tokens are NOT 0-terminated */ 84 order of the edges; tokens are NOT 0-terminated */
88
89}; 85};
90 86
91 87
@@ -100,15 +96,15 @@ GNUNET_NETWORK_STRUCT_END
100 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not 96 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
101 */ 97 */
102int 98int
103GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, 99GNUNET_BLOCK_is_accepting(const struct RegexBlock *block,
104 size_t size) 100 size_t size)
105{ 101{
106 if (size < sizeof (struct RegexBlock)) 102 if (size < sizeof(struct RegexBlock))
107 { 103 {
108 GNUNET_break_op (0); 104 GNUNET_break_op(0);
109 return GNUNET_SYSERR; 105 return GNUNET_SYSERR;
110 } 106 }
111 return ntohs (block->is_accepting); 107 return ntohs(block->is_accepting);
112} 108}
113 109
114 110
@@ -121,28 +117,27 @@ GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
121 * @return #GNUNET_OK if the proof is valid for the given key. 117 * @return #GNUNET_OK if the proof is valid for the given key.
122 */ 118 */
123int 119int
124REGEX_BLOCK_check_proof (const char *proof, 120REGEX_BLOCK_check_proof(const char *proof,
125 size_t proof_len, 121 size_t proof_len,
126 const struct GNUNET_HashCode *key) 122 const struct GNUNET_HashCode *key)
127{ 123{
128 struct GNUNET_HashCode key_check; 124 struct GNUNET_HashCode key_check;
129 125
130 if ( (NULL == proof) || (NULL == key)) 126 if ((NULL == proof) || (NULL == key))
131 { 127 {
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n"); 128 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
133 return GNUNET_NO; 129 return GNUNET_NO;
134 } 130 }
135 GNUNET_CRYPTO_hash (proof, proof_len, &key_check); 131 GNUNET_CRYPTO_hash(proof, proof_len, &key_check);
136 return (0 == 132 return (0 ==
137 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO; 133 GNUNET_CRYPTO_hash_cmp(key, &key_check)) ? GNUNET_OK : GNUNET_NO;
138} 134}
139 135
140 136
141/** 137/**
142 * Struct to keep track of the xquery while iterating all the edges in a block. 138 * Struct to keep track of the xquery while iterating all the edges in a block.
143 */ 139 */
144struct CheckEdgeContext 140struct CheckEdgeContext {
145{
146 /** 141 /**
147 * Xquery: string we are looking for. 142 * Xquery: string we are looking for.
148 */ 143 */
@@ -152,7 +147,6 @@ struct CheckEdgeContext
152 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO) 147 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
153 */ 148 */
154 int found; 149 int found;
155
156}; 150};
157 151
158 152
@@ -167,24 +161,24 @@ struct CheckEdgeContext
167 * @return #GNUNET_YES, to keep iterating 161 * @return #GNUNET_YES, to keep iterating
168 */ 162 */
169static int 163static int
170check_edge (void *cls, 164check_edge(void *cls,
171 const char *token, 165 const char *token,
172 size_t len, 166 size_t len,
173 const struct GNUNET_HashCode *key) 167 const struct GNUNET_HashCode *key)
174{ 168{
175 struct CheckEdgeContext *ctx = cls; 169 struct CheckEdgeContext *ctx = cls;
176 170
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
178 "edge %.*s [%u]: %s\n", 172 "edge %.*s [%u]: %s\n",
179 (int) len, 173 (int)len,
180 token, 174 token,
181 (unsigned int) len, 175 (unsigned int)len,
182 GNUNET_h2s (key)); 176 GNUNET_h2s(key));
183 if (NULL == ctx->xquery) 177 if (NULL == ctx->xquery)
184 return GNUNET_YES; 178 return GNUNET_YES;
185 if (strlen (ctx->xquery) < len) 179 if (strlen(ctx->xquery) < len)
186 return GNUNET_YES; /* too long */ 180 return GNUNET_YES; /* too long */
187 if (0 == strncmp (ctx->xquery, token, len)) 181 if (0 == strncmp(ctx->xquery, token, len))
188 ctx->found = GNUNET_OK; 182 ctx->found = GNUNET_OK;
189 return GNUNET_YES; /* keep checking for malformed data! */ 183 return GNUNET_YES; /* keep checking for malformed data! */
190} 184}
@@ -203,48 +197,48 @@ check_edge (void *cls,
203 * #GNUNET_SYSERR if the block is invalid. 197 * #GNUNET_SYSERR if the block is invalid.
204 */ 198 */
205int 199int
206REGEX_BLOCK_check (const struct RegexBlock *block, 200REGEX_BLOCK_check(const struct RegexBlock *block,
207 size_t size, 201 size_t size,
208 const struct GNUNET_HashCode *query, 202 const struct GNUNET_HashCode *query,
209 const char *xquery) 203 const char *xquery)
210{ 204{
211 struct GNUNET_HashCode key; 205 struct GNUNET_HashCode key;
212 struct CheckEdgeContext ctx; 206 struct CheckEdgeContext ctx;
213 int res; 207 int res;
214 208
215 LOG (GNUNET_ERROR_TYPE_DEBUG, 209 LOG(GNUNET_ERROR_TYPE_DEBUG,
216 "Block check\n"); 210 "Block check\n");
217 if (GNUNET_OK != 211 if (GNUNET_OK !=
218 REGEX_BLOCK_get_key (block, size, 212 REGEX_BLOCK_get_key(block, size,
219 &key)) 213 &key))
220 { 214 {
221 GNUNET_break_op (0); 215 GNUNET_break_op(0);
222 return GNUNET_SYSERR; 216 return GNUNET_SYSERR;
223 } 217 }
224 if (NULL != query && 218 if (NULL != query &&
225 0 != GNUNET_memcmp (&key, 219 0 != GNUNET_memcmp(&key,
226 query)) 220 query))
227 { 221 {
228 GNUNET_break_op (0); 222 GNUNET_break_op(0);
229 return GNUNET_SYSERR; 223 return GNUNET_SYSERR;
230 } 224 }
231 if ( (GNUNET_YES == ntohs (block->is_accepting)) && 225 if ((GNUNET_YES == ntohs(block->is_accepting)) &&
232 ( (NULL == xquery) || ('\0' == xquery[0]) ) ) 226 ((NULL == xquery) || ('\0' == xquery[0])))
233 { 227 {
234 LOG (GNUNET_ERROR_TYPE_DEBUG, 228 LOG(GNUNET_ERROR_TYPE_DEBUG,
235 " out! Is accepting: %u, xquery %p\n", 229 " out! Is accepting: %u, xquery %p\n",
236 ntohs(block->is_accepting), 230 ntohs(block->is_accepting),
237 xquery); 231 xquery);
238 return GNUNET_OK; 232 return GNUNET_OK;
239 } 233 }
240 ctx.xquery = xquery; 234 ctx.xquery = xquery;
241 ctx.found = GNUNET_NO; 235 ctx.found = GNUNET_NO;
242 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx); 236 res = REGEX_BLOCK_iterate(block, size, &check_edge, &ctx);
243 if (GNUNET_SYSERR == res) 237 if (GNUNET_SYSERR == res)
244 return GNUNET_SYSERR; 238 return GNUNET_SYSERR;
245 if (NULL == xquery) 239 if (NULL == xquery)
246 return GNUNET_YES; 240 return GNUNET_YES;
247 LOG (GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found); 241 LOG(GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found);
248 return ctx.found; 242 return ctx.found;
249} 243}
250 244
@@ -258,9 +252,9 @@ REGEX_BLOCK_check (const struct RegexBlock *block,
258 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed 252 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
259 */ 253 */
260int 254int
261REGEX_BLOCK_get_key (const struct RegexBlock *block, 255REGEX_BLOCK_get_key(const struct RegexBlock *block,
262 size_t block_len, 256 size_t block_len,
263 struct GNUNET_HashCode *key) 257 struct GNUNET_HashCode *key)
264{ 258{
265 uint16_t len; 259 uint16_t len;
266 const struct GNUNET_HashCode *destinations; 260 const struct GNUNET_HashCode *destinations;
@@ -269,23 +263,23 @@ REGEX_BLOCK_get_key (const struct RegexBlock *block,
269 uint16_t num_edges; 263 uint16_t num_edges;
270 size_t total; 264 size_t total;
271 265
272 if (block_len < sizeof (struct RegexBlock)) 266 if (block_len < sizeof(struct RegexBlock))
273 { 267 {
274 GNUNET_break_op (0); 268 GNUNET_break_op(0);
275 return GNUNET_SYSERR; 269 return GNUNET_SYSERR;
276 } 270 }
277 num_destinations = ntohs (block->num_destinations); 271 num_destinations = ntohs(block->num_destinations);
278 num_edges = ntohs (block->num_edges); 272 num_edges = ntohs(block->num_edges);
279 len = ntohs (block->proof_len); 273 len = ntohs(block->proof_len);
280 destinations = (const struct GNUNET_HashCode *) &block[1]; 274 destinations = (const struct GNUNET_HashCode *)&block[1];
281 edges = (const struct EdgeInfo *) &destinations[num_destinations]; 275 edges = (const struct EdgeInfo *)&destinations[num_destinations];
282 total = sizeof (struct RegexBlock) + num_destinations * sizeof (struct GNUNET_HashCode) + num_edges * sizeof (struct EdgeInfo) + len; 276 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len;
283 if (block_len < total) 277 if (block_len < total)
284 { 278 {
285 GNUNET_break_op (0); 279 GNUNET_break_op(0);
286 return GNUNET_SYSERR; 280 return GNUNET_SYSERR;
287 } 281 }
288 GNUNET_CRYPTO_hash (&edges[num_edges], len, key); 282 GNUNET_CRYPTO_hash(&edges[num_edges], len, key);
289 return GNUNET_OK; 283 return GNUNET_OK;
290} 284}
291 285
@@ -306,10 +300,10 @@ REGEX_BLOCK_get_key (const struct RegexBlock *block,
306 * be errors in further edges. 300 * be errors in further edges.
307 */ 301 */
308int 302int
309REGEX_BLOCK_iterate (const struct RegexBlock *block, 303REGEX_BLOCK_iterate(const struct RegexBlock *block,
310 size_t size, 304 size_t size,
311 REGEX_INTERNAL_EgdeIterator iterator, 305 REGEX_INTERNAL_EgdeIterator iterator,
312 void *iter_cls) 306 void *iter_cls)
313{ 307{
314 uint16_t len; 308 uint16_t len;
315 const struct GNUNET_HashCode *destinations; 309 const struct GNUNET_HashCode *destinations;
@@ -321,54 +315,54 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
321 unsigned int n; 315 unsigned int n;
322 size_t off; 316 size_t off;
323 317
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n"); 318 LOG(GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n");
325 if (size < sizeof (struct RegexBlock)) 319 if (size < sizeof(struct RegexBlock))
326 { 320 {
327 GNUNET_break_op (0); 321 GNUNET_break_op(0);
328 return GNUNET_SYSERR; 322 return GNUNET_SYSERR;
329 } 323 }
330 num_destinations = ntohs (block->num_destinations); 324 num_destinations = ntohs(block->num_destinations);
331 num_edges = ntohs (block->num_edges); 325 num_edges = ntohs(block->num_edges);
332 len = ntohs (block->proof_len); 326 len = ntohs(block->proof_len);
333 destinations = (const struct GNUNET_HashCode *) &block[1]; 327 destinations = (const struct GNUNET_HashCode *)&block[1];
334 edges = (const struct EdgeInfo *) &destinations[num_destinations]; 328 edges = (const struct EdgeInfo *)&destinations[num_destinations];
335 aux = (const char *) &edges[num_edges]; 329 aux = (const char *)&edges[num_edges];
336 total = sizeof (struct RegexBlock) + num_destinations * sizeof (struct GNUNET_HashCode) + num_edges * sizeof (struct EdgeInfo) + len; 330 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len;
337 if (size < total) 331 if (size < total)
338 { 332 {
339 GNUNET_break_op (0); 333 GNUNET_break_op(0);
340 return GNUNET_SYSERR; 334 return GNUNET_SYSERR;
341 } 335 }
342 for (n=0;n<num_edges;n++) 336 for (n = 0; n < num_edges; n++)
343 total += ntohs (edges[n].token_length); 337 total += ntohs(edges[n].token_length);
344 if (size != total) 338 if (size != total)
345 { 339 {
346 fprintf (stderr, "Expected %u, got %u\n", 340 fprintf(stderr, "Expected %u, got %u\n",
347 (unsigned int) size, 341 (unsigned int)size,
348 (unsigned int) total); 342 (unsigned int)total);
349 GNUNET_break_op (0); 343 GNUNET_break_op(0);
350 return GNUNET_SYSERR; 344 return GNUNET_SYSERR;
351 } 345 }
352 off = len; 346 off = len;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, 347 LOG(GNUNET_ERROR_TYPE_DEBUG,
354 "Start iterating block of size %u, proof %u, off %u edges %u\n", 348 "Start iterating block of size %u, proof %u, off %u edges %u\n",
355 size, len, off, n); 349 size, len, off, n);
356 /* &aux[off] always points to our token */ 350 /* &aux[off] always points to our token */
357 for (n=0;n<num_edges;n++) 351 for (n = 0; n < num_edges; n++)
358 { 352 {
359 LOG (GNUNET_ERROR_TYPE_DEBUG, 353 LOG(GNUNET_ERROR_TYPE_DEBUG,
360 "Edge %u/%u, off %u tokenlen %u (%.*s)\n", 354 "Edge %u/%u, off %u tokenlen %u (%.*s)\n",
361 n+1, num_edges, off, 355 n + 1, num_edges, off,
362 ntohs (edges[n].token_length), ntohs (edges[n].token_length), 356 ntohs(edges[n].token_length), ntohs(edges[n].token_length),
363 &aux[off]); 357 &aux[off]);
364 if (NULL != iterator) 358 if (NULL != iterator)
365 if (GNUNET_NO == iterator (iter_cls, 359 if (GNUNET_NO == iterator(iter_cls,
366 &aux[off], 360 &aux[off],
367 ntohs (edges[n].token_length), 361 ntohs(edges[n].token_length),
368 &destinations[ntohs (edges[n].destination_index)])) 362 &destinations[ntohs(edges[n].destination_index)]))
369 return GNUNET_OK; 363 return GNUNET_OK;
370 off += ntohs (edges[n].token_length); 364 off += ntohs(edges[n].token_length);
371 } 365 }
372 return GNUNET_OK; 366 return GNUNET_OK;
373} 367}
374 368
@@ -384,11 +378,11 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
384 * @return the regex block, NULL on error 378 * @return the regex block, NULL on error
385 */ 379 */
386struct RegexBlock * 380struct RegexBlock *
387REGEX_BLOCK_create (const char *proof, 381REGEX_BLOCK_create(const char *proof,
388 unsigned int num_edges, 382 unsigned int num_edges,
389 const struct REGEX_BLOCK_Edge *edges, 383 const struct REGEX_BLOCK_Edge *edges,
390 int accepting, 384 int accepting,
391 size_t *rsize) 385 size_t *rsize)
392{ 386{
393 struct RegexBlock *block; 387 struct RegexBlock *block;
394 struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */ 388 struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */
@@ -404,64 +398,64 @@ REGEX_BLOCK_create (const char *proof,
404 unsigned int i; 398 unsigned int i;
405 char *aux; 399 char *aux;
406 400
407 len = strlen (proof); 401 len = strlen(proof);
408 if (len > UINT16_MAX) 402 if (len > UINT16_MAX)
409 {
410 GNUNET_break (0);
411 return NULL;
412 }
413 unique_destinations = 0;
414 total = sizeof (struct RegexBlock) + len;
415 for (i=0;i<num_edges;i++)
416 {
417 slen = strlen (edges[i].label);
418 if (slen > UINT16_MAX)
419 { 403 {
420 GNUNET_break (0); 404 GNUNET_break(0);
421 return NULL; 405 return NULL;
422 } 406 }
423 total += slen; 407 unique_destinations = 0;
424 for (j=0;j<unique_destinations;j++) 408 total = sizeof(struct RegexBlock) + len;
425 if (0 == memcmp (&destinations[j], 409 for (i = 0; i < num_edges; i++)
426 &edges[i].destination,
427 sizeof (struct GNUNET_HashCode)))
428 break;
429 if (j >= 1024)
430 { 410 {
431 GNUNET_break (0); 411 slen = strlen(edges[i].label);
432 return NULL; 412 if (slen > UINT16_MAX)
413 {
414 GNUNET_break(0);
415 return NULL;
416 }
417 total += slen;
418 for (j = 0; j < unique_destinations; j++)
419 if (0 == memcmp(&destinations[j],
420 &edges[i].destination,
421 sizeof(struct GNUNET_HashCode)))
422 break;
423 if (j >= 1024)
424 {
425 GNUNET_break(0);
426 return NULL;
427 }
428 destination_indices[i] = j;
429 if (j == unique_destinations)
430 destinations[unique_destinations++] = edges[i].destination;
433 } 431 }
434 destination_indices[i] = j; 432 total += num_edges * sizeof(struct EdgeInfo) + unique_destinations * sizeof(struct GNUNET_HashCode);
435 if (j == unique_destinations)
436 destinations[unique_destinations++] = edges[i].destination;
437 }
438 total += num_edges * sizeof (struct EdgeInfo) + unique_destinations * sizeof (struct GNUNET_HashCode);
439 if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE) 433 if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE)
440 { 434 {
441 GNUNET_break (0); 435 GNUNET_break(0);
442 return NULL; 436 return NULL;
443 } 437 }
444 block = GNUNET_malloc (total); 438 block = GNUNET_malloc(total);
445 block->proof_len = htons (len); 439 block->proof_len = htons(len);
446 block->is_accepting = htons (accepting); 440 block->is_accepting = htons(accepting);
447 block->num_edges = htons (num_edges); 441 block->num_edges = htons(num_edges);
448 block->num_destinations = htons (unique_destinations); 442 block->num_destinations = htons(unique_destinations);
449 dests = (struct GNUNET_HashCode *) &block[1]; 443 dests = (struct GNUNET_HashCode *)&block[1];
450 GNUNET_memcpy (dests, destinations, sizeof (struct GNUNET_HashCode) * unique_destinations); 444 GNUNET_memcpy(dests, destinations, sizeof(struct GNUNET_HashCode) * unique_destinations);
451 edgeinfos = (struct EdgeInfo *) &dests[unique_destinations]; 445 edgeinfos = (struct EdgeInfo *)&dests[unique_destinations];
452 aux = (char *) &edgeinfos[num_edges]; 446 aux = (char *)&edgeinfos[num_edges];
453 off = len; 447 off = len;
454 GNUNET_memcpy (aux, proof, len); 448 GNUNET_memcpy(aux, proof, len);
455 for (i=0;i<num_edges;i++) 449 for (i = 0; i < num_edges; i++)
456 { 450 {
457 slen = strlen (edges[i].label); 451 slen = strlen(edges[i].label);
458 edgeinfos[i].token_length = htons ((uint16_t) slen); 452 edgeinfos[i].token_length = htons((uint16_t)slen);
459 edgeinfos[i].destination_index = htons (destination_indices[i]); 453 edgeinfos[i].destination_index = htons(destination_indices[i]);
460 GNUNET_memcpy (&aux[off], 454 GNUNET_memcpy(&aux[off],
461 edges[i].label, 455 edges[i].label,
462 slen); 456 slen);
463 off += slen; 457 off += slen;
464 } 458 }
465 *rsize = total; 459 *rsize = total;
466 return block; 460 return block;
467} 461}
diff --git a/src/regex/regex_block_lib.h b/src/regex/regex_block_lib.h
index 920c9d1d0..6b2d3bf26 100644
--- a/src/regex/regex_block_lib.h
+++ b/src/regex/regex_block_lib.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @author Bartlomiej Polot 22 * @author Bartlomiej Polot
@@ -31,7 +31,7 @@
31extern "C" 31extern "C"
32{ 32{
33#if 0 33#if 0
34 /* keep Emacsens' auto-indent happy */ 34/* keep Emacsens' auto-indent happy */
35} 35}
36#endif 36#endif
37#endif 37#endif
@@ -49,8 +49,7 @@ struct RegexBlock;
49/** 49/**
50 * Edge representation. 50 * Edge representation.
51 */ 51 */
52struct REGEX_BLOCK_Edge 52struct REGEX_BLOCK_Edge {
53{
54 /** 53 /**
55 * Label of the edge. FIXME: might want to not consume exactly 54 * Label of the edge. FIXME: might want to not consume exactly
56 * multiples of 8 bits, need length! 55 * multiples of 8 bits, need length!
@@ -73,9 +72,9 @@ struct REGEX_BLOCK_Edge
73 * @return #GNUNET_OK if the proof is valid for the given key. 72 * @return #GNUNET_OK if the proof is valid for the given key.
74 */ 73 */
75int 74int
76REGEX_BLOCK_check_proof (const char *proof, 75REGEX_BLOCK_check_proof(const char *proof,
77 size_t proof_len, 76 size_t proof_len,
78 const struct GNUNET_HashCode *key); 77 const struct GNUNET_HashCode *key);
79 78
80 79
81/** 80/**
@@ -91,10 +90,10 @@ REGEX_BLOCK_check_proof (const char *proof,
91 * #GNUNET_SYSERR if the block is invalid. 90 * #GNUNET_SYSERR if the block is invalid.
92 */ 91 */
93int 92int
94REGEX_BLOCK_check (const struct RegexBlock *block, 93REGEX_BLOCK_check(const struct RegexBlock *block,
95 size_t size, 94 size_t size,
96 const struct GNUNET_HashCode *query, 95 const struct GNUNET_HashCode *query,
97 const char *xquery); 96 const char *xquery);
98 97
99 98
100/* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */ 99/* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */
@@ -131,10 +130,10 @@ typedef int
131 * be errors in further edges. 130 * be errors in further edges.
132 */ 131 */
133int 132int
134REGEX_BLOCK_iterate (const struct RegexBlock *block, 133REGEX_BLOCK_iterate(const struct RegexBlock *block,
135 size_t size, 134 size_t size,
136 REGEX_INTERNAL_EgdeIterator iterator, 135 REGEX_INTERNAL_EgdeIterator iterator,
137 void *iter_cls); 136 void *iter_cls);
138 137
139 138
140/** 139/**
@@ -146,9 +145,9 @@ REGEX_BLOCK_iterate (const struct RegexBlock *block,
146 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed 145 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
147 */ 146 */
148int 147int
149REGEX_BLOCK_get_key (const struct RegexBlock *block, 148REGEX_BLOCK_get_key(const struct RegexBlock *block,
150 size_t block_len, 149 size_t block_len,
151 struct GNUNET_HashCode *key); 150 struct GNUNET_HashCode *key);
152 151
153 152
154/** 153/**
@@ -159,8 +158,8 @@ REGEX_BLOCK_get_key (const struct RegexBlock *block,
159 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not 158 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
160 */ 159 */
161int 160int
162GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, 161GNUNET_BLOCK_is_accepting(const struct RegexBlock *block,
163 size_t block_len); 162 size_t block_len);
164 163
165 164
166/** 165/**
@@ -174,11 +173,11 @@ GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
174 * @return the regex block, NULL on error 173 * @return the regex block, NULL on error
175 */ 174 */
176struct RegexBlock * 175struct RegexBlock *
177REGEX_BLOCK_create (const char *proof, 176REGEX_BLOCK_create(const char *proof,
178 unsigned int num_edges, 177 unsigned int num_edges,
179 const struct REGEX_BLOCK_Edge *edges, 178 const struct REGEX_BLOCK_Edge *edges,
180 int accepting, 179 int accepting,
181 size_t *rsize); 180 size_t *rsize);
182 181
183 182
184#if 0 /* keep Emacsens' auto-indent happy */ 183#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/regex/regex_internal.c b/src/regex/regex_internal.c
index 55d16129f..34f0d9c42 100644
--- a/src/regex/regex_internal.c
+++ b/src/regex/regex_internal.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_internal.c 21 * @file src/regex/regex_internal.c
22 * @brief library to create Deterministic Finite Automatons (DFAs) from regular 22 * @brief library to create Deterministic Finite Automatons (DFAs) from regular
@@ -39,8 +39,7 @@
39/** 39/**
40 * Set of states using MDLL API. 40 * Set of states using MDLL API.
41 */ 41 */
42struct REGEX_INTERNAL_StateSet_MDLL 42struct REGEX_INTERNAL_StateSet_MDLL {
43{
44 /** 43 /**
45 * MDLL of states. 44 * MDLL of states.
46 */ 45 */
@@ -65,11 +64,11 @@ struct REGEX_INTERNAL_StateSet_MDLL
65 * @param state state to be appended 64 * @param state state to be appended
66 */ 65 */
67static void 66static void
68state_set_append (struct REGEX_INTERNAL_StateSet *set, 67state_set_append(struct REGEX_INTERNAL_StateSet *set,
69 struct REGEX_INTERNAL_State *state) 68 struct REGEX_INTERNAL_State *state)
70{ 69{
71 if (set->off == set->size) 70 if (set->off == set->size)
72 GNUNET_array_grow (set->states, set->size, set->size * 2 + 4); 71 GNUNET_array_grow(set->states, set->size, set->size * 2 + 4);
73 set->states[set->off++] = state; 72 set->states[set->off++] = state;
74} 73}
75 74
@@ -83,14 +82,14 @@ state_set_append (struct REGEX_INTERNAL_StateSet *set,
83 * @return 0 if the strings are the same or both NULL, 1 or -1 if not. 82 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
84 */ 83 */
85static int 84static int
86nullstrcmp (const char *str1, const char *str2) 85nullstrcmp(const char *str1, const char *str2)
87{ 86{
88 if ((NULL == str1) != (NULL == str2)) 87 if ((NULL == str1) != (NULL == str2))
89 return -1; 88 return -1;
90 if ((NULL == str1) && (NULL == str2)) 89 if ((NULL == str1) && (NULL == str2))
91 return 0; 90 return 0;
92 91
93 return strcmp (str1, str2); 92 return strcmp(str1, str2);
94} 93}
95 94
96 95
@@ -104,40 +103,40 @@ nullstrcmp (const char *str1, const char *str2)
104 * @param to_state state to where the transition should point to 103 * @param to_state state to where the transition should point to
105 */ 104 */
106static void 105static void
107state_add_transition (struct REGEX_INTERNAL_Context *ctx, 106state_add_transition(struct REGEX_INTERNAL_Context *ctx,
108 struct REGEX_INTERNAL_State *from_state, 107 struct REGEX_INTERNAL_State *from_state,
109 const char *label, 108 const char *label,
110 struct REGEX_INTERNAL_State *to_state) 109 struct REGEX_INTERNAL_State *to_state)
111{ 110{
112 struct REGEX_INTERNAL_Transition *t; 111 struct REGEX_INTERNAL_Transition *t;
113 struct REGEX_INTERNAL_Transition *oth; 112 struct REGEX_INTERNAL_Transition *oth;
114 113
115 if (NULL == from_state) 114 if (NULL == from_state)
116 { 115 {
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n"); 116 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n");
118 return; 117 return;
119 } 118 }
120 119
121 /* Do not add duplicate state transitions */ 120 /* Do not add duplicate state transitions */
122 for (t = from_state->transitions_head; NULL != t; t = t->next) 121 for (t = from_state->transitions_head; NULL != t; t = t->next)
123 { 122 {
124 if (t->to_state == to_state && 0 == nullstrcmp (t->label, label) && 123 if (t->to_state == to_state && 0 == nullstrcmp(t->label, label) &&
125 t->from_state == from_state) 124 t->from_state == from_state)
126 return; 125 return;
127 } 126 }
128 127
129 /* sort transitions by label */ 128 /* sort transitions by label */
130 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next) 129 for (oth = from_state->transitions_head; NULL != oth; oth = oth->next)
131 { 130 {
132 if (0 < nullstrcmp (oth->label, label)) 131 if (0 < nullstrcmp(oth->label, label))
133 break; 132 break;
134 } 133 }
135 134
136 t = GNUNET_new (struct REGEX_INTERNAL_Transition); 135 t = GNUNET_new(struct REGEX_INTERNAL_Transition);
137 if (NULL != ctx) 136 if (NULL != ctx)
138 t->id = ctx->transition_id++; 137 t->id = ctx->transition_id++;
139 if (NULL != label) 138 if (NULL != label)
140 t->label = GNUNET_strdup (label); 139 t->label = GNUNET_strdup(label);
141 else 140 else
142 t->label = NULL; 141 t->label = NULL;
143 t->to_state = to_state; 142 t->to_state = to_state;
@@ -145,10 +144,10 @@ state_add_transition (struct REGEX_INTERNAL_Context *ctx,
145 144
146 /* Add outgoing transition to 'from_state' */ 145 /* Add outgoing transition to 'from_state' */
147 from_state->transition_count++; 146 from_state->transition_count++;
148 GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head, 147 GNUNET_CONTAINER_DLL_insert_before(from_state->transitions_head,
149 from_state->transitions_tail, 148 from_state->transitions_tail,
150 oth, 149 oth,
151 t); 150 t);
152} 151}
153 152
154 153
@@ -159,8 +158,8 @@ state_add_transition (struct REGEX_INTERNAL_Context *ctx,
159 * @param transition transition that should be removed from state 'state'. 158 * @param transition transition that should be removed from state 'state'.
160 */ 159 */
161static void 160static void
162state_remove_transition (struct REGEX_INTERNAL_State *state, 161state_remove_transition(struct REGEX_INTERNAL_State *state,
163 struct REGEX_INTERNAL_Transition *transition) 162 struct REGEX_INTERNAL_Transition *transition)
164{ 163{
165 if (NULL == state || NULL == transition) 164 if (NULL == state || NULL == transition)
166 return; 165 return;
@@ -168,14 +167,14 @@ state_remove_transition (struct REGEX_INTERNAL_State *state,
168 if (transition->from_state != state) 167 if (transition->from_state != state)
169 return; 168 return;
170 169
171 GNUNET_free_non_null (transition->label); 170 GNUNET_free_non_null(transition->label);
172 171
173 state->transition_count--; 172 state->transition_count--;
174 GNUNET_CONTAINER_DLL_remove (state->transitions_head, 173 GNUNET_CONTAINER_DLL_remove(state->transitions_head,
175 state->transitions_tail, 174 state->transitions_tail,
176 transition); 175 transition);
177 176
178 GNUNET_free (transition); 177 GNUNET_free(transition);
179} 178}
180 179
181 180
@@ -190,10 +189,10 @@ state_remove_transition (struct REGEX_INTERNAL_State *state,
190 * less than, equal to, or greater than the second. 189 * less than, equal to, or greater than the second.
191 */ 190 */
192static int 191static int
193state_compare (const void *a, const void *b) 192state_compare(const void *a, const void *b)
194{ 193{
195 struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a; 194 struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **)a;
196 struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b; 195 struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **)b;
197 196
198 return (*s1)->id - (*s2)->id; 197 return (*s1)->id - (*s2)->id;
199} 198}
@@ -209,7 +208,7 @@ state_compare (const void *a, const void *b)
209 * @return number of edges. 208 * @return number of edges.
210 */ 209 */
211static unsigned int 210static unsigned int
212state_get_edges (struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges) 211state_get_edges(struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges)
213{ 212{
214 struct REGEX_INTERNAL_Transition *t; 213 struct REGEX_INTERNAL_Transition *t;
215 unsigned int count; 214 unsigned int count;
@@ -220,14 +219,14 @@ state_get_edges (struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges)
220 count = 0; 219 count = 0;
221 220
222 for (t = s->transitions_head; NULL != t; t = t->next) 221 for (t = s->transitions_head; NULL != t; t = t->next)
223 {
224 if (NULL != t->to_state)
225 { 222 {
226 edges[count].label = t->label; 223 if (NULL != t->to_state)
227 edges[count].destination = t->to_state->hash; 224 {
228 count++; 225 edges[count].label = t->label;
226 edges[count].destination = t->to_state->hash;
227 count++;
228 }
229 } 229 }
230 }
231 return count; 230 return count;
232} 231}
233 232
@@ -241,8 +240,8 @@ state_get_edges (struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges)
241 * @return 0 if the sets are equal, otherwise non-zero 240 * @return 0 if the sets are equal, otherwise non-zero
242 */ 241 */
243static int 242static int
244state_set_compare (struct REGEX_INTERNAL_StateSet *sset1, 243state_set_compare(struct REGEX_INTERNAL_StateSet *sset1,
245 struct REGEX_INTERNAL_StateSet *sset2) 244 struct REGEX_INTERNAL_StateSet *sset2)
246{ 245{
247 int result; 246 int result;
248 unsigned int i; 247 unsigned int i;
@@ -256,7 +255,7 @@ state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
256 if (result > 0) 255 if (result > 0)
257 return 1; 256 return 1;
258 for (i = 0; i < sset1->off; i++) 257 for (i = 0; i < sset1->off; i++)
259 if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i]))) 258 if (0 != (result = state_compare(&sset1->states[i], &sset2->states[i])))
260 break; 259 break;
261 return result; 260 return result;
262} 261}
@@ -268,9 +267,9 @@ state_set_compare (struct REGEX_INTERNAL_StateSet *sset1,
268 * @param set set to be cleared 267 * @param set set to be cleared
269 */ 268 */
270static void 269static void
271state_set_clear (struct REGEX_INTERNAL_StateSet *set) 270state_set_clear(struct REGEX_INTERNAL_StateSet *set)
272{ 271{
273 GNUNET_array_grow (set->states, set->size, 0); 272 GNUNET_array_grow(set->states, set->size, 0);
274 set->off = 0; 273 set->off = 0;
275} 274}
276 275
@@ -282,7 +281,7 @@ state_set_clear (struct REGEX_INTERNAL_StateSet *set)
282 * @param a automaton to be cleared 281 * @param a automaton to be cleared
283 */ 282 */
284static void 283static void
285automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a) 284automaton_fragment_clear(struct REGEX_INTERNAL_Automaton *a)
286{ 285{
287 if (NULL == a) 286 if (NULL == a)
288 return; 287 return;
@@ -292,7 +291,7 @@ automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
292 a->states_head = NULL; 291 a->states_head = NULL;
293 a->states_tail = NULL; 292 a->states_tail = NULL;
294 a->state_count = 0; 293 a->state_count = 0;
295 GNUNET_free (a); 294 GNUNET_free(a);
296} 295}
297 296
298 297
@@ -302,7 +301,7 @@ automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a)
302 * @param s state that should be destroyed 301 * @param s state that should be destroyed
303 */ 302 */
304static void 303static void
305automaton_destroy_state (struct REGEX_INTERNAL_State *s) 304automaton_destroy_state(struct REGEX_INTERNAL_State *s)
306{ 305{
307 struct REGEX_INTERNAL_Transition *t; 306 struct REGEX_INTERNAL_Transition *t;
308 struct REGEX_INTERNAL_Transition *next_t; 307 struct REGEX_INTERNAL_Transition *next_t;
@@ -310,16 +309,16 @@ automaton_destroy_state (struct REGEX_INTERNAL_State *s)
310 if (NULL == s) 309 if (NULL == s)
311 return; 310 return;
312 311
313 GNUNET_free_non_null (s->name); 312 GNUNET_free_non_null(s->name);
314 GNUNET_free_non_null (s->proof); 313 GNUNET_free_non_null(s->proof);
315 state_set_clear (&s->nfa_set); 314 state_set_clear(&s->nfa_set);
316 for (t = s->transitions_head; NULL != t; t = next_t) 315 for (t = s->transitions_head; NULL != t; t = next_t)
317 { 316 {
318 next_t = t->next; 317 next_t = t->next;
319 state_remove_transition (s, t); 318 state_remove_transition(s, t);
320 } 319 }
321 320
322 GNUNET_free (s); 321 GNUNET_free(s);
323} 322}
324 323
325 324
@@ -332,8 +331,8 @@ automaton_destroy_state (struct REGEX_INTERNAL_State *s)
332 * @param s state to remove 331 * @param s state to remove
333 */ 332 */
334static void 333static void
335automaton_remove_state (struct REGEX_INTERNAL_Automaton *a, 334automaton_remove_state(struct REGEX_INTERNAL_Automaton *a,
336 struct REGEX_INTERNAL_State *s) 335 struct REGEX_INTERNAL_State *s)
337{ 336{
338 struct REGEX_INTERNAL_State *s_check; 337 struct REGEX_INTERNAL_State *s_check;
339 struct REGEX_INTERNAL_Transition *t_check; 338 struct REGEX_INTERNAL_Transition *t_check;
@@ -344,21 +343,21 @@ automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
344 343
345 /* remove all transitions leading to this state */ 344 /* remove all transitions leading to this state */
346 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next) 345 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
347 {
348 for (t_check = s_check->transitions_head; NULL != t_check;
349 t_check = t_check_next)
350 { 346 {
351 t_check_next = t_check->next; 347 for (t_check = s_check->transitions_head; NULL != t_check;
352 if (t_check->to_state == s) 348 t_check = t_check_next)
353 state_remove_transition (s_check, t_check); 349 {
350 t_check_next = t_check->next;
351 if (t_check->to_state == s)
352 state_remove_transition(s_check, t_check);
353 }
354 } 354 }
355 }
356 355
357 /* remove state */ 356 /* remove state */
358 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s); 357 GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s);
359 a->state_count--; 358 a->state_count--;
360 359
361 automaton_destroy_state (s); 360 automaton_destroy_state(s);
362} 361}
363 362
364 363
@@ -372,10 +371,10 @@ automaton_remove_state (struct REGEX_INTERNAL_Automaton *a,
372 * @param s2 second state, will be destroyed 371 * @param s2 second state, will be destroyed
373 */ 372 */
374static void 373static void
375automaton_merge_states (struct REGEX_INTERNAL_Context *ctx, 374automaton_merge_states(struct REGEX_INTERNAL_Context *ctx,
376 struct REGEX_INTERNAL_Automaton *a, 375 struct REGEX_INTERNAL_Automaton *a,
377 struct REGEX_INTERNAL_State *s1, 376 struct REGEX_INTERNAL_State *s1,
378 struct REGEX_INTERNAL_State *s2) 377 struct REGEX_INTERNAL_State *s2)
379{ 378{
380 struct REGEX_INTERNAL_State *s_check; 379 struct REGEX_INTERNAL_State *s_check;
381 struct REGEX_INTERNAL_Transition *t_check; 380 struct REGEX_INTERNAL_Transition *t_check;
@@ -389,47 +388,47 @@ automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
389 /* 1. Make all transitions pointing to s2 point to s1, unless this transition 388 /* 1. Make all transitions pointing to s2 point to s1, unless this transition
390 * does not already exists, if it already exists remove transition. */ 389 * does not already exists, if it already exists remove transition. */
391 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next) 390 for (s_check = a->states_head; NULL != s_check; s_check = s_check->next)
392 {
393 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
394 { 391 {
395 t_next = t_check->next; 392 for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next)
396
397 if (s2 == t_check->to_state)
398 {
399 is_dup = GNUNET_NO;
400 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
401 { 393 {
402 if (t->to_state == s1 && 0 == strcmp (t_check->label, t->label)) 394 t_next = t_check->next;
403 is_dup = GNUNET_YES; 395
396 if (s2 == t_check->to_state)
397 {
398 is_dup = GNUNET_NO;
399 for (t = t_check->from_state->transitions_head; NULL != t; t = t->next)
400 {
401 if (t->to_state == s1 && 0 == strcmp(t_check->label, t->label))
402 is_dup = GNUNET_YES;
403 }
404 if (GNUNET_NO == is_dup)
405 t_check->to_state = s1;
406 else
407 state_remove_transition(t_check->from_state, t_check);
408 }
404 } 409 }
405 if (GNUNET_NO == is_dup)
406 t_check->to_state = s1;
407 else
408 state_remove_transition (t_check->from_state, t_check);
409 }
410 } 410 }
411 }
412 411
413 /* 2. Add all transitions from s2 to sX to s1 */ 412 /* 2. Add all transitions from s2 to sX to s1 */
414 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next) 413 for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next)
415 { 414 {
416 if (t_check->to_state != s1) 415 if (t_check->to_state != s1)
417 state_add_transition (ctx, s1, t_check->label, t_check->to_state); 416 state_add_transition(ctx, s1, t_check->label, t_check->to_state);
418 } 417 }
419 418
420 /* 3. Rename s1 to {s1,s2} */ 419 /* 3. Rename s1 to {s1,s2} */
421#if REGEX_DEBUG_DFA 420#if REGEX_DEBUG_DFA
422 char *new_name; 421 char *new_name;
423 422
424 new_name = s1->name; 423 new_name = s1->name;
425 GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name); 424 GNUNET_asprintf(&s1->name, "{%s,%s}", new_name, s2->name);
426 GNUNET_free (new_name); 425 GNUNET_free(new_name);
427#endif 426#endif
428 427
429 /* remove state */ 428 /* remove state */
430 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2); 429 GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s2);
431 a->state_count--; 430 a->state_count--;
432 automaton_destroy_state (s2); 431 automaton_destroy_state(s2);
433} 432}
434 433
435 434
@@ -441,10 +440,10 @@ automaton_merge_states (struct REGEX_INTERNAL_Context *ctx,
441 * @param s state that should be added 440 * @param s state that should be added
442 */ 441 */
443static void 442static void
444automaton_add_state (struct REGEX_INTERNAL_Automaton *a, 443automaton_add_state(struct REGEX_INTERNAL_Automaton *a,
445 struct REGEX_INTERNAL_State *s) 444 struct REGEX_INTERNAL_State *s)
446{ 445{
447 GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s); 446 GNUNET_CONTAINER_DLL_insert(a->states_head, a->states_tail, s);
448 a->state_count++; 447 a->state_count++;
449} 448}
450 449
@@ -464,13 +463,13 @@ automaton_add_state (struct REGEX_INTERNAL_Automaton *a,
464 * @param action_cls closure for action. 463 * @param action_cls closure for action.
465 */ 464 */
466static void 465static void
467automaton_state_traverse (struct REGEX_INTERNAL_State *s, 466automaton_state_traverse(struct REGEX_INTERNAL_State *s,
468 int *marks, 467 int *marks,
469 unsigned int *count, 468 unsigned int *count,
470 REGEX_INTERNAL_traverse_check check, 469 REGEX_INTERNAL_traverse_check check,
471 void *check_cls, 470 void *check_cls,
472 REGEX_INTERNAL_traverse_action action, 471 REGEX_INTERNAL_traverse_action action,
473 void *action_cls) 472 void *action_cls)
474{ 473{
475 struct REGEX_INTERNAL_Transition *t; 474 struct REGEX_INTERNAL_Transition *t;
476 475
@@ -480,24 +479,24 @@ automaton_state_traverse (struct REGEX_INTERNAL_State *s,
480 marks[s->traversal_id] = GNUNET_YES; 479 marks[s->traversal_id] = GNUNET_YES;
481 480
482 if (NULL != action) 481 if (NULL != action)
483 action (action_cls, *count, s); 482 action(action_cls, *count, s);
484 483
485 (*count)++; 484 (*count)++;
486 485
487 for (t = s->transitions_head; NULL != t; t = t->next) 486 for (t = s->transitions_head; NULL != t; t = t->next)
488 {
489 if (NULL == check ||
490 (NULL != check && GNUNET_YES == check (check_cls, s, t)))
491 { 487 {
492 automaton_state_traverse (t->to_state, 488 if (NULL == check ||
493 marks, 489 (NULL != check && GNUNET_YES == check(check_cls, s, t)))
494 count, 490 {
495 check, 491 automaton_state_traverse(t->to_state,
496 check_cls, 492 marks,
497 action, 493 count,
498 action_cls); 494 check,
495 check_cls,
496 action,
497 action_cls);
498 }
499 } 499 }
500 }
501} 500}
502 501
503 502
@@ -515,12 +514,12 @@ automaton_state_traverse (struct REGEX_INTERNAL_State *s,
515 * @param action_cls closure for @a action 514 * @param action_cls closure for @a action
516 */ 515 */
517void 516void
518REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a, 517REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a,
519 struct REGEX_INTERNAL_State *start, 518 struct REGEX_INTERNAL_State *start,
520 REGEX_INTERNAL_traverse_check check, 519 REGEX_INTERNAL_traverse_check check,
521 void *check_cls, 520 void *check_cls,
522 REGEX_INTERNAL_traverse_action action, 521 REGEX_INTERNAL_traverse_action action,
523 void *action_cls) 522 void *action_cls)
524{ 523{
525 unsigned int count; 524 unsigned int count;
526 struct REGEX_INTERNAL_State *s; 525 struct REGEX_INTERNAL_State *s;
@@ -532,10 +531,10 @@ REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
532 531
533 for (count = 0, s = a->states_head; NULL != s && count < a->state_count; 532 for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
534 s = s->next, count++) 533 s = s->next, count++)
535 { 534 {
536 s->traversal_id = count; 535 s->traversal_id = count;
537 marks[s->traversal_id] = GNUNET_NO; 536 marks[s->traversal_id] = GNUNET_NO;
538 } 537 }
539 538
540 count = 0; 539 count = 0;
541 540
@@ -544,21 +543,20 @@ REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
544 else 543 else
545 s = start; 544 s = start;
546 545
547 automaton_state_traverse (s, 546 automaton_state_traverse(s,
548 marks, 547 marks,
549 &count, 548 &count,
550 check, 549 check,
551 check_cls, 550 check_cls,
552 action, 551 action,
553 action_cls); 552 action_cls);
554} 553}
555 554
556 555
557/** 556/**
558 * String container for faster string operations. 557 * String container for faster string operations.
559 */ 558 */
560struct StringBuffer 559struct StringBuffer {
561{
562 /** 560 /**
563 * Buffer holding the string (may start in the middle!); 561 * Buffer holding the string (may start in the middle!);
564 * NOT 0-terminated! 562 * NOT 0-terminated!
@@ -607,7 +605,7 @@ struct StringBuffer
607 * @return 0 if the strings are the same or both NULL, 1 or -1 if not. 605 * @return 0 if the strings are the same or both NULL, 1 or -1 if not.
608 */ 606 */
609static int 607static int
610sb_nullstrcmp (const struct StringBuffer *s1, const struct StringBuffer *s2) 608sb_nullstrcmp(const struct StringBuffer *s1, const struct StringBuffer *s2)
611{ 609{
612 if ((GNUNET_YES == s1->null_flag) && (GNUNET_YES == s2->null_flag)) 610 if ((GNUNET_YES == s1->null_flag) && (GNUNET_YES == s2->null_flag))
613 return 0; 611 return 0;
@@ -617,7 +615,7 @@ sb_nullstrcmp (const struct StringBuffer *s1, const struct StringBuffer *s2)
617 return -1; 615 return -1;
618 if (0 == s1->slen) 616 if (0 == s1->slen)
619 return 0; 617 return 0;
620 return memcmp (s1->sbuf, s2->sbuf, s1->slen); 618 return memcmp(s1->sbuf, s2->sbuf, s1->slen);
621} 619}
622 620
623 621
@@ -630,13 +628,13 @@ sb_nullstrcmp (const struct StringBuffer *s1, const struct StringBuffer *s2)
630 * @return 0 if the strings are the same, 1 or -1 if not. 628 * @return 0 if the strings are the same, 1 or -1 if not.
631 */ 629 */
632static int 630static int
633sb_strcmp (const struct StringBuffer *s1, const struct StringBuffer *s2) 631sb_strcmp(const struct StringBuffer *s1, const struct StringBuffer *s2)
634{ 632{
635 if (s1->slen != s2->slen) 633 if (s1->slen != s2->slen)
636 return -1; 634 return -1;
637 if (0 == s1->slen) 635 if (0 == s1->slen)
638 return 0; 636 return 0;
639 return memcmp (s1->sbuf, s2->sbuf, s1->slen); 637 return memcmp(s1->sbuf, s2->sbuf, s1->slen);
640} 638}
641 639
642 640
@@ -648,17 +646,17 @@ sb_strcmp (const struct StringBuffer *s1, const struct StringBuffer *s2)
648 * @param nlen target length for the buffer, must be at least ret->slen 646 * @param nlen target length for the buffer, must be at least ret->slen
649 */ 647 */
650static void 648static void
651sb_realloc (struct StringBuffer *ret, size_t nlen) 649sb_realloc(struct StringBuffer *ret, size_t nlen)
652{ 650{
653 char *old; 651 char *old;
654 652
655 GNUNET_assert (nlen >= ret->slen); 653 GNUNET_assert(nlen >= ret->slen);
656 old = ret->abuf; 654 old = ret->abuf;
657 ret->abuf = GNUNET_malloc (nlen); 655 ret->abuf = GNUNET_malloc(nlen);
658 ret->blen = nlen; 656 ret->blen = nlen;
659 GNUNET_memcpy (ret->abuf, ret->sbuf, ret->slen); 657 GNUNET_memcpy(ret->abuf, ret->sbuf, ret->slen);
660 ret->sbuf = ret->abuf; 658 ret->sbuf = ret->abuf;
661 GNUNET_free_non_null (old); 659 GNUNET_free_non_null(old);
662} 660}
663 661
664 662
@@ -669,14 +667,14 @@ sb_realloc (struct StringBuffer *ret, size_t nlen)
669 * @param sarg string to append 667 * @param sarg string to append
670 */ 668 */
671static void 669static void
672sb_append (struct StringBuffer *ret, const struct StringBuffer *sarg) 670sb_append(struct StringBuffer *ret, const struct StringBuffer *sarg)
673{ 671{
674 if (GNUNET_YES == ret->null_flag) 672 if (GNUNET_YES == ret->null_flag)
675 ret->slen = 0; 673 ret->slen = 0;
676 ret->null_flag = GNUNET_NO; 674 ret->null_flag = GNUNET_NO;
677 if (ret->blen < sarg->slen + ret->slen) 675 if (ret->blen < sarg->slen + ret->slen)
678 sb_realloc (ret, ret->blen + sarg->slen + 128); 676 sb_realloc(ret, ret->blen + sarg->slen + 128);
679 GNUNET_memcpy (&ret->sbuf[ret->slen], sarg->sbuf, sarg->slen); 677 GNUNET_memcpy(&ret->sbuf[ret->slen], sarg->sbuf, sarg->slen);
680 ret->slen += sarg->slen; 678 ret->slen += sarg->slen;
681} 679}
682 680
@@ -688,16 +686,16 @@ sb_append (struct StringBuffer *ret, const struct StringBuffer *sarg)
688 * @param cstr string to append 686 * @param cstr string to append
689 */ 687 */
690static void 688static void
691sb_append_cstr (struct StringBuffer *ret, const char *cstr) 689sb_append_cstr(struct StringBuffer *ret, const char *cstr)
692{ 690{
693 size_t cstr_len = strlen (cstr); 691 size_t cstr_len = strlen(cstr);
694 692
695 if (GNUNET_YES == ret->null_flag) 693 if (GNUNET_YES == ret->null_flag)
696 ret->slen = 0; 694 ret->slen = 0;
697 ret->null_flag = GNUNET_NO; 695 ret->null_flag = GNUNET_NO;
698 if (ret->blen < cstr_len + ret->slen) 696 if (ret->blen < cstr_len + ret->slen)
699 sb_realloc (ret, ret->blen + cstr_len + 128); 697 sb_realloc(ret, ret->blen + cstr_len + 128);
700 GNUNET_memcpy (&ret->sbuf[ret->slen], cstr, cstr_len); 698 GNUNET_memcpy(&ret->sbuf[ret->slen], cstr, cstr_len);
701 ret->slen += cstr_len; 699 ret->slen += cstr_len;
702} 700}
703 701
@@ -713,20 +711,20 @@ sb_append_cstr (struct StringBuffer *ret, const char *cstr)
713 * @param extra_chars how long will the result be, in addition to 'sarg' length 711 * @param extra_chars how long will the result be, in addition to 'sarg' length
714 */ 712 */
715static void 713static void
716sb_wrap (struct StringBuffer *ret, const char *format, size_t extra_chars) 714sb_wrap(struct StringBuffer *ret, const char *format, size_t extra_chars)
717{ 715{
718 char *temp; 716 char *temp;
719 717
720 if (GNUNET_YES == ret->null_flag) 718 if (GNUNET_YES == ret->null_flag)
721 ret->slen = 0; 719 ret->slen = 0;
722 ret->null_flag = GNUNET_NO; 720 ret->null_flag = GNUNET_NO;
723 temp = GNUNET_malloc (ret->slen + extra_chars + 1); 721 temp = GNUNET_malloc(ret->slen + extra_chars + 1);
724 GNUNET_snprintf (temp, 722 GNUNET_snprintf(temp,
725 ret->slen + extra_chars + 1, 723 ret->slen + extra_chars + 1,
726 format, 724 format,
727 (int) ret->slen, 725 (int)ret->slen,
728 ret->sbuf); 726 ret->sbuf);
729 GNUNET_free_non_null (ret->abuf); 727 GNUNET_free_non_null(ret->abuf);
730 ret->abuf = temp; 728 ret->abuf = temp;
731 ret->sbuf = temp; 729 ret->sbuf = temp;
732 ret->blen = ret->slen + extra_chars + 1; 730 ret->blen = ret->slen + extra_chars + 1;
@@ -744,17 +742,17 @@ sb_wrap (struct StringBuffer *ret, const char *format, size_t extra_chars)
744 * @param sarg string to print into the format 742 * @param sarg string to print into the format
745 */ 743 */
746static void 744static void
747sb_printf1 (struct StringBuffer *ret, 745sb_printf1(struct StringBuffer *ret,
748 const char *format, 746 const char *format,
749 size_t extra_chars, 747 size_t extra_chars,
750 const struct StringBuffer *sarg) 748 const struct StringBuffer *sarg)
751{ 749{
752 if (ret->blen < sarg->slen + extra_chars + 1) 750 if (ret->blen < sarg->slen + extra_chars + 1)
753 sb_realloc (ret, sarg->slen + extra_chars + 1); 751 sb_realloc(ret, sarg->slen + extra_chars + 1);
754 ret->null_flag = GNUNET_NO; 752 ret->null_flag = GNUNET_NO;
755 ret->sbuf = ret->abuf; 753 ret->sbuf = ret->abuf;
756 ret->slen = sarg->slen + extra_chars; 754 ret->slen = sarg->slen + extra_chars;
757 GNUNET_snprintf (ret->sbuf, ret->blen, format, (int) sarg->slen, sarg->sbuf); 755 GNUNET_snprintf(ret->sbuf, ret->blen, format, (int)sarg->slen, sarg->sbuf);
758} 756}
759 757
760 758
@@ -768,24 +766,24 @@ sb_printf1 (struct StringBuffer *ret,
768 * @param sarg2 second string to print into the format 766 * @param sarg2 second string to print into the format
769 */ 767 */
770static void 768static void
771sb_printf2 (struct StringBuffer *ret, 769sb_printf2(struct StringBuffer *ret,
772 const char *format, 770 const char *format,
773 size_t extra_chars, 771 size_t extra_chars,
774 const struct StringBuffer *sarg1, 772 const struct StringBuffer *sarg1,
775 const struct StringBuffer *sarg2) 773 const struct StringBuffer *sarg2)
776{ 774{
777 if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1) 775 if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1)
778 sb_realloc (ret, sarg1->slen + sarg2->slen + extra_chars + 1); 776 sb_realloc(ret, sarg1->slen + sarg2->slen + extra_chars + 1);
779 ret->null_flag = GNUNET_NO; 777 ret->null_flag = GNUNET_NO;
780 ret->slen = sarg1->slen + sarg2->slen + extra_chars; 778 ret->slen = sarg1->slen + sarg2->slen + extra_chars;
781 ret->sbuf = ret->abuf; 779 ret->sbuf = ret->abuf;
782 GNUNET_snprintf (ret->sbuf, 780 GNUNET_snprintf(ret->sbuf,
783 ret->blen, 781 ret->blen,
784 format, 782 format,
785 (int) sarg1->slen, 783 (int)sarg1->slen,
786 sarg1->sbuf, 784 sarg1->sbuf,
787 (int) sarg2->slen, 785 (int)sarg2->slen,
788 sarg2->sbuf); 786 sarg2->sbuf);
789} 787}
790 788
791 789
@@ -801,27 +799,27 @@ sb_printf2 (struct StringBuffer *ret,
801 * @param sarg3 third string to print into the format 799 * @param sarg3 third string to print into the format
802 */ 800 */
803static void 801static void
804sb_printf3 (struct StringBuffer *ret, 802sb_printf3(struct StringBuffer *ret,
805 const char *format, 803 const char *format,
806 size_t extra_chars, 804 size_t extra_chars,
807 const struct StringBuffer *sarg1, 805 const struct StringBuffer *sarg1,
808 const struct StringBuffer *sarg2, 806 const struct StringBuffer *sarg2,
809 const struct StringBuffer *sarg3) 807 const struct StringBuffer *sarg3)
810{ 808{
811 if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1) 809 if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1)
812 sb_realloc (ret, sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1); 810 sb_realloc(ret, sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1);
813 ret->null_flag = GNUNET_NO; 811 ret->null_flag = GNUNET_NO;
814 ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars; 812 ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars;
815 ret->sbuf = ret->abuf; 813 ret->sbuf = ret->abuf;
816 GNUNET_snprintf (ret->sbuf, 814 GNUNET_snprintf(ret->sbuf,
817 ret->blen, 815 ret->blen,
818 format, 816 format,
819 (int) sarg1->slen, 817 (int)sarg1->slen,
820 sarg1->sbuf, 818 sarg1->sbuf,
821 (int) sarg2->slen, 819 (int)sarg2->slen,
822 sarg2->sbuf, 820 sarg2->sbuf,
823 (int) sarg3->slen, 821 (int)sarg3->slen,
824 sarg3->sbuf); 822 sarg3->sbuf);
825} 823}
826 824
827 825
@@ -832,9 +830,9 @@ sb_printf3 (struct StringBuffer *ret,
832 * should not be individually allocated) 830 * should not be individually allocated)
833 */ 831 */
834static void 832static void
835sb_free (struct StringBuffer *sb) 833sb_free(struct StringBuffer *sb)
836{ 834{
837 GNUNET_array_grow (sb->abuf, sb->blen, 0); 835 GNUNET_array_grow(sb->abuf, sb->blen, 0);
838 sb->slen = 0; 836 sb->slen = 0;
839 sb->sbuf = NULL; 837 sb->sbuf = NULL;
840 sb->null_flag = GNUNET_YES; 838 sb->null_flag = GNUNET_YES;
@@ -848,19 +846,19 @@ sb_free (struct StringBuffer *sb)
848 * @param out output string 846 * @param out output string
849 */ 847 */
850static void 848static void
851sb_strdup (struct StringBuffer *out, const struct StringBuffer *in) 849sb_strdup(struct StringBuffer *out, const struct StringBuffer *in)
852 850
853{ 851{
854 out->null_flag = in->null_flag; 852 out->null_flag = in->null_flag;
855 if (GNUNET_YES == out->null_flag) 853 if (GNUNET_YES == out->null_flag)
856 return; 854 return;
857 if (out->blen < in->slen) 855 if (out->blen < in->slen)
858 { 856 {
859 GNUNET_array_grow (out->abuf, out->blen, in->slen); 857 GNUNET_array_grow(out->abuf, out->blen, in->slen);
860 } 858 }
861 out->sbuf = out->abuf; 859 out->sbuf = out->abuf;
862 out->slen = in->slen; 860 out->slen = in->slen;
863 GNUNET_memcpy (out->sbuf, in->sbuf, out->slen); 861 GNUNET_memcpy(out->sbuf, in->sbuf, out->slen);
864} 862}
865 863
866 864
@@ -871,21 +869,21 @@ sb_strdup (struct StringBuffer *out, const struct StringBuffer *in)
871 * @param out output string 869 * @param out output string
872 */ 870 */
873static void 871static void
874sb_strdup_cstr (struct StringBuffer *out, const char *cstr) 872sb_strdup_cstr(struct StringBuffer *out, const char *cstr)
875{ 873{
876 if (NULL == cstr) 874 if (NULL == cstr)
877 { 875 {
878 out->null_flag = GNUNET_YES; 876 out->null_flag = GNUNET_YES;
879 return; 877 return;
880 } 878 }
881 out->null_flag = GNUNET_NO; 879 out->null_flag = GNUNET_NO;
882 out->slen = strlen (cstr); 880 out->slen = strlen(cstr);
883 if (out->blen < out->slen) 881 if (out->blen < out->slen)
884 { 882 {
885 GNUNET_array_grow (out->abuf, out->blen, out->slen); 883 GNUNET_array_grow(out->abuf, out->blen, out->slen);
886 } 884 }
887 out->sbuf = out->abuf; 885 out->sbuf = out->abuf;
888 GNUNET_memcpy (out->sbuf, cstr, out->slen); 886 GNUNET_memcpy(out->sbuf, cstr, out->slen);
889} 887}
890 888
891 889
@@ -898,7 +896,7 @@ sb_strdup_cstr (struct StringBuffer *out, const char *cstr)
898 * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise 896 * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise
899 */ 897 */
900static int 898static int
901needs_parentheses (const struct StringBuffer *str) 899needs_parentheses(const struct StringBuffer *str)
902{ 900{
903 size_t slen; 901 size_t slen;
904 const char *op; 902 const char *op;
@@ -916,23 +914,23 @@ needs_parentheses (const struct StringBuffer *str)
916 cnt = 1; 914 cnt = 1;
917 pos++; 915 pos++;
918 while (cnt > 0) 916 while (cnt > 0)
919 {
920 cl = memchr (pos, ')', end - pos);
921 if (NULL == cl)
922 { 917 {
923 GNUNET_break (0); 918 cl = memchr(pos, ')', end - pos);
924 return GNUNET_YES; 919 if (NULL == cl)
925 } 920 {
926 /* while '(' before ')', count opening parens */ 921 GNUNET_break(0);
927 while ((NULL != (op = memchr (pos, '(', end - pos))) && (op < cl)) 922 return GNUNET_YES;
928 { 923 }
929 cnt++; 924 /* while '(' before ')', count opening parens */
930 pos = op + 1; 925 while ((NULL != (op = memchr(pos, '(', end - pos))) && (op < cl))
926 {
927 cnt++;
928 pos = op + 1;
929 }
930 /* got ')' first */
931 cnt--;
932 pos = cl + 1;
931 } 933 }
932 /* got ')' first */
933 cnt--;
934 pos = cl + 1;
935 }
936 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES; 934 return (*pos == '\0') ? GNUNET_NO : GNUNET_YES;
937} 935}
938 936
@@ -947,7 +945,7 @@ needs_parentheses (const struct StringBuffer *str)
947 * epsilon could be found, NULL if 'str' was NULL 945 * epsilon could be found, NULL if 'str' was NULL
948 */ 946 */
949static void 947static void
950remove_parentheses (struct StringBuffer *str) 948remove_parentheses(struct StringBuffer *str)
951{ 949{
952 size_t slen; 950 size_t slen;
953 const char *pos; 951 const char *pos;
@@ -966,30 +964,30 @@ remove_parentheses (struct StringBuffer *str)
966 cnt = 0; 964 cnt = 0;
967 pos = &sbuf[1]; 965 pos = &sbuf[1];
968 end = &sbuf[slen - 1]; 966 end = &sbuf[slen - 1];
969 op = memchr (pos, '(', end - pos); 967 op = memchr(pos, '(', end - pos);
970 cp = memchr (pos, ')', end - pos); 968 cp = memchr(pos, ')', end - pos);
971 while (NULL != cp) 969 while (NULL != cp)
972 {
973 while ((NULL != op) && (op < cp))
974 { 970 {
975 cnt++; 971 while ((NULL != op) && (op < cp))
976 pos = op + 1; 972 {
977 op = memchr (pos, '(', end - pos); 973 cnt++;
974 pos = op + 1;
975 op = memchr(pos, '(', end - pos);
976 }
977 while ((NULL != cp) && ((NULL == op) || (cp < op)))
978 {
979 if (0 == cnt)
980 return; /* can't strip parens */
981 cnt--;
982 pos = cp + 1;
983 cp = memchr(pos, ')', end - pos);
984 }
978 } 985 }
979 while ((NULL != cp) && ((NULL == op) || (cp < op))) 986 if (0 != cnt)
980 { 987 {
981 if (0 == cnt) 988 GNUNET_break(0);
982 return; /* can't strip parens */ 989 return;
983 cnt--;
984 pos = cp + 1;
985 cp = memchr (pos, ')', end - pos);
986 } 990 }
987 }
988 if (0 != cnt)
989 {
990 GNUNET_break (0);
991 return;
992 }
993 str->sbuf++; 991 str->sbuf++;
994 str->slen -= 2; 992 str->slen -= 2;
995} 993}
@@ -1004,7 +1002,7 @@ remove_parentheses (struct StringBuffer *str)
1004 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')' 1002 * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')'
1005 */ 1003 */
1006static int 1004static int
1007has_epsilon (const struct StringBuffer *str) 1005has_epsilon(const struct StringBuffer *str)
1008{ 1006{
1009 return (GNUNET_YES != str->null_flag) && (0 < str->slen) && 1007 return (GNUNET_YES != str->null_flag) && (0 < str->slen) &&
1010 ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) && 1008 ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) &&
@@ -1022,27 +1020,27 @@ has_epsilon (const struct StringBuffer *str)
1022 * epsilon could be found, NULL if 'str' was NULL 1020 * epsilon could be found, NULL if 'str' was NULL
1023 */ 1021 */
1024static void 1022static void
1025remove_epsilon (const struct StringBuffer *str, struct StringBuffer *ret) 1023remove_epsilon(const struct StringBuffer *str, struct StringBuffer *ret)
1026{ 1024{
1027 if (GNUNET_YES == str->null_flag) 1025 if (GNUNET_YES == str->null_flag)
1028 { 1026 {
1029 ret->null_flag = GNUNET_YES; 1027 ret->null_flag = GNUNET_YES;
1030 return; 1028 return;
1031 } 1029 }
1032 if ((str->slen > 1) && ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) && 1030 if ((str->slen > 1) && ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) &&
1033 (')' == str->sbuf[str->slen - 1])) 1031 (')' == str->sbuf[str->slen - 1]))
1034 {
1035 /* remove epsilon */
1036 if (ret->blen < str->slen - 3)
1037 { 1032 {
1038 GNUNET_array_grow (ret->abuf, ret->blen, str->slen - 3); 1033 /* remove epsilon */
1034 if (ret->blen < str->slen - 3)
1035 {
1036 GNUNET_array_grow(ret->abuf, ret->blen, str->slen - 3);
1037 }
1038 ret->sbuf = ret->abuf;
1039 ret->slen = str->slen - 3;
1040 GNUNET_memcpy(ret->sbuf, &str->sbuf[2], ret->slen);
1041 return;
1039 } 1042 }
1040 ret->sbuf = ret->abuf; 1043 sb_strdup(ret, str);
1041 ret->slen = str->slen - 3;
1042 GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen);
1043 return;
1044 }
1045 sb_strdup (ret, str);
1046} 1044}
1047 1045
1048 1046
@@ -1056,18 +1054,18 @@ remove_epsilon (const struct StringBuffer *str, struct StringBuffer *ret)
1056 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise 1054 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1057 */ 1055 */
1058static int 1056static int
1059sb_strncmp (const struct StringBuffer *str1, 1057sb_strncmp(const struct StringBuffer *str1,
1060 const struct StringBuffer *str2, 1058 const struct StringBuffer *str2,
1061 size_t n) 1059 size_t n)
1062{ 1060{
1063 size_t max; 1061 size_t max;
1064 1062
1065 if ((str1->slen != str2->slen) && ((str1->slen < n) || (str2->slen < n))) 1063 if ((str1->slen != str2->slen) && ((str1->slen < n) || (str2->slen < n)))
1066 return -1; 1064 return -1;
1067 max = GNUNET_MAX (str1->slen, str2->slen); 1065 max = GNUNET_MAX(str1->slen, str2->slen);
1068 if (max > n) 1066 if (max > n)
1069 max = n; 1067 max = n;
1070 return memcmp (str1->sbuf, str2->sbuf, max); 1068 return memcmp(str1->sbuf, str2->sbuf, max);
1071} 1069}
1072 1070
1073 1071
@@ -1081,11 +1079,11 @@ sb_strncmp (const struct StringBuffer *str1,
1081 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise 1079 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1082 */ 1080 */
1083static int 1081static int
1084sb_strncmp_cstr (const struct StringBuffer *str1, const char *str2, size_t n) 1082sb_strncmp_cstr(const struct StringBuffer *str1, const char *str2, size_t n)
1085{ 1083{
1086 if (str1->slen < n) 1084 if (str1->slen < n)
1087 return -1; 1085 return -1;
1088 return memcmp (str1->sbuf, str2, n); 1086 return memcmp(str1->sbuf, str2, n);
1089} 1087}
1090 1088
1091 1089
@@ -1097,10 +1095,10 @@ sb_strncmp_cstr (const struct StringBuffer *str1, const char *str2, size_t n)
1097 * @param n desired target length 1095 * @param n desired target length
1098 */ 1096 */
1099static void 1097static void
1100sb_init (struct StringBuffer *sb, size_t n) 1098sb_init(struct StringBuffer *sb, size_t n)
1101{ 1099{
1102 sb->null_flag = GNUNET_NO; 1100 sb->null_flag = GNUNET_NO;
1103 sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n); 1101 sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc(n);
1104 sb->blen = n; 1102 sb->blen = n;
1105 sb->slen = 0; 1103 sb->slen = 0;
1106} 1104}
@@ -1116,14 +1114,14 @@ sb_init (struct StringBuffer *sb, size_t n)
1116 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise 1114 * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise
1117 */ 1115 */
1118static int 1116static int
1119sb_strkcmp (const struct StringBuffer *str1, 1117sb_strkcmp(const struct StringBuffer *str1,
1120 const struct StringBuffer *str2, 1118 const struct StringBuffer *str2,
1121 size_t k) 1119 size_t k)
1122{ 1120{
1123 if ((GNUNET_YES == str1->null_flag) || (GNUNET_YES == str2->null_flag) || 1121 if ((GNUNET_YES == str1->null_flag) || (GNUNET_YES == str2->null_flag) ||
1124 (k > str1->slen) || (str1->slen - k != str2->slen)) 1122 (k > str1->slen) || (str1->slen - k != str2->slen))
1125 return -1; 1123 return -1;
1126 return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen); 1124 return memcmp(&str1->sbuf[k], str2->sbuf, str2->slen);
1127} 1125}
1128 1126
1129 1127
@@ -1136,9 +1134,9 @@ sb_strkcmp (const struct StringBuffer *str1,
1136 * @param s current state. 1134 * @param s current state.
1137 */ 1135 */
1138static void 1136static void
1139number_states (void *cls, 1137number_states(void *cls,
1140 const unsigned int count, 1138 const unsigned int count,
1141 struct REGEX_INTERNAL_State *s) 1139 struct REGEX_INTERNAL_State *s)
1142{ 1140{
1143 struct REGEX_INTERNAL_State **states = cls; 1141 struct REGEX_INTERNAL_State **states = cls;
1144 1142
@@ -1149,8 +1147,8 @@ number_states (void *cls,
1149 1147
1150 1148
1151#define PRIS(a) \ 1149#define PRIS(a) \
1152 ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \ 1150 ((GNUNET_YES == a.null_flag) ? 6 : (int)a.slen), \
1153 ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf) 1151 ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf)
1154 1152
1155 1153
1156/** 1154/**
@@ -1168,13 +1166,13 @@ number_states (void *cls,
1168 * @param R_cur_r optimization -- kept between iterations to avoid realloc 1166 * @param R_cur_r optimization -- kept between iterations to avoid realloc
1169 */ 1167 */
1170static void 1168static void
1171automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij, 1169automaton_create_proofs_simplify(const struct StringBuffer *R_last_ij,
1172 const struct StringBuffer *R_last_ik, 1170 const struct StringBuffer *R_last_ik,
1173 const struct StringBuffer *R_last_kk, 1171 const struct StringBuffer *R_last_kk,
1174 const struct StringBuffer *R_last_kj, 1172 const struct StringBuffer *R_last_kj,
1175 struct StringBuffer *R_cur_ij, 1173 struct StringBuffer *R_cur_ij,
1176 struct StringBuffer *R_cur_l, 1174 struct StringBuffer *R_cur_l,
1177 struct StringBuffer *R_cur_r) 1175 struct StringBuffer *R_cur_r)
1178{ 1176{
1179 struct StringBuffer R_temp_ij; 1177 struct StringBuffer R_temp_ij;
1180 struct StringBuffer R_temp_ik; 1178 struct StringBuffer R_temp_ik;
@@ -1202,27 +1200,27 @@ automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1202 if ((GNUNET_YES == R_last_ij->null_flag) && 1200 if ((GNUNET_YES == R_last_ij->null_flag) &&
1203 ((GNUNET_YES == R_last_ik->null_flag) || 1201 ((GNUNET_YES == R_last_ik->null_flag) ||
1204 (GNUNET_YES == R_last_kj->null_flag))) 1202 (GNUNET_YES == R_last_kj->null_flag)))
1205 { 1203 {
1206 /* R^{(k)}_{ij} = N | N */ 1204 /* R^{(k)}_{ij} = N | N */
1207 R_cur_ij->null_flag = GNUNET_YES; 1205 R_cur_ij->null_flag = GNUNET_YES;
1208 R_cur_ij->synced = GNUNET_NO; 1206 R_cur_ij->synced = GNUNET_NO;
1209 return; 1207 return;
1210 } 1208 }
1211 1209
1212 if ((GNUNET_YES == R_last_ik->null_flag) || 1210 if ((GNUNET_YES == R_last_ik->null_flag) ||
1213 (GNUNET_YES == R_last_kj->null_flag)) 1211 (GNUNET_YES == R_last_kj->null_flag))
1214 {
1215 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1216 if (GNUNET_YES == R_last_ij->synced)
1217 { 1212 {
1213 /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */
1214 if (GNUNET_YES == R_last_ij->synced)
1215 {
1216 R_cur_ij->synced = GNUNET_YES;
1217 R_cur_ij->null_flag = GNUNET_NO;
1218 return;
1219 }
1218 R_cur_ij->synced = GNUNET_YES; 1220 R_cur_ij->synced = GNUNET_YES;
1219 R_cur_ij->null_flag = GNUNET_NO; 1221 sb_strdup(R_cur_ij, R_last_ij);
1220 return; 1222 return;
1221 } 1223 }
1222 R_cur_ij->synced = GNUNET_YES;
1223 sb_strdup (R_cur_ij, R_last_ij);
1224 return;
1225 }
1226 R_cur_ij->synced = GNUNET_NO; 1224 R_cur_ij->synced = GNUNET_NO;
1227 1225
1228 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR 1226 /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR
@@ -1234,334 +1232,334 @@ automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1234 R_cur_l->slen = 0; 1232 R_cur_l->slen = 0;
1235 1233
1236 /* cache results from strcmp, we might need these many times */ 1234 /* cache results from strcmp, we might need these many times */
1237 ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj); 1235 ij_kj_cmp = sb_nullstrcmp(R_last_ij, R_last_kj);
1238 ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik); 1236 ij_ik_cmp = sb_nullstrcmp(R_last_ij, R_last_ik);
1239 ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk); 1237 ik_kk_cmp = sb_nullstrcmp(R_last_ik, R_last_kk);
1240 kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj); 1238 kk_kj_cmp = sb_nullstrcmp(R_last_kk, R_last_kj);
1241 1239
1242 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well 1240 /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well
1243 * as parentheses, so we can better compare the contents */ 1241 * as parentheses, so we can better compare the contents */
1244 1242
1245 memset (&R_temp_ij, 0, sizeof (struct StringBuffer)); 1243 memset(&R_temp_ij, 0, sizeof(struct StringBuffer));
1246 memset (&R_temp_ik, 0, sizeof (struct StringBuffer)); 1244 memset(&R_temp_ik, 0, sizeof(struct StringBuffer));
1247 memset (&R_temp_kk, 0, sizeof (struct StringBuffer)); 1245 memset(&R_temp_kk, 0, sizeof(struct StringBuffer));
1248 memset (&R_temp_kj, 0, sizeof (struct StringBuffer)); 1246 memset(&R_temp_kj, 0, sizeof(struct StringBuffer));
1249 remove_epsilon (R_last_ik, &R_temp_ik); 1247 remove_epsilon(R_last_ik, &R_temp_ik);
1250 remove_epsilon (R_last_kk, &R_temp_kk); 1248 remove_epsilon(R_last_kk, &R_temp_kk);
1251 remove_epsilon (R_last_kj, &R_temp_kj); 1249 remove_epsilon(R_last_kj, &R_temp_kj);
1252 remove_parentheses (&R_temp_ik); 1250 remove_parentheses(&R_temp_ik);
1253 remove_parentheses (&R_temp_kk); 1251 remove_parentheses(&R_temp_kk);
1254 remove_parentheses (&R_temp_kj); 1252 remove_parentheses(&R_temp_kj);
1255 clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk); 1253 clean_ik_kk_cmp = sb_nullstrcmp(R_last_ik, &R_temp_kk);
1256 clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj); 1254 clean_kk_kj_cmp = sb_nullstrcmp(&R_temp_kk, R_last_kj);
1257 1255
1258 /* construct R_cur_l (and, if necessary R_cur_r) */ 1256 /* construct R_cur_l (and, if necessary R_cur_r) */
1259 if (GNUNET_YES != R_last_ij->null_flag) 1257 if (GNUNET_YES != R_last_ij->null_flag)
1260 {
1261 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1262 * as parentheses, so we can better compare the contents */
1263 remove_epsilon (R_last_ij, &R_temp_ij);
1264 remove_parentheses (&R_temp_ij);
1265
1266 if ((0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) &&
1267 (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) &&
1268 (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)))
1269 {
1270 if (0 == R_temp_ij.slen)
1271 {
1272 R_cur_r->null_flag = GNUNET_NO;
1273 }
1274 else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) ||
1275 (0 == sb_strncmp_cstr (R_last_ik, "(|", 2) &&
1276 0 == sb_strncmp_cstr (R_last_kj, "(|", 2)))
1277 {
1278 /*
1279 * a|(e|a)a*(e|a) = a*
1280 * a|(e|a)(e|a)*(e|a) = a*
1281 * (e|a)|aa*a = a*
1282 * (e|a)|aa*(e|a) = a*
1283 * (e|a)|(e|a)a*a = a*
1284 * (e|a)|(e|a)a*(e|a) = a*
1285 * (e|a)|(e|a)(e|a)*(e|a) = a*
1286 */
1287 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1288 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1289 else
1290 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij);
1291 }
1292 else
1293 {
1294 /*
1295 * a|aa*a = a+
1296 * a|(e|a)a*a = a+
1297 * a|aa*(e|a) = a+
1298 * a|(e|a)(e|a)*a = a+
1299 * a|a(e|a)*(e|a) = a+
1300 */
1301 if (GNUNET_YES == needs_parentheses (&R_temp_ij))
1302 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1303 else
1304 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij);
1305 }
1306 }
1307 else if ((0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) &&
1308 (0 != clean_ik_kk_cmp))
1309 { 1258 {
1310 /* a|ab*b = ab* */ 1259 /* Assign R_temp_ij to R_last_ij and remove epsilon as well
1311 if (0 == R_last_kk->slen) 1260 * as parentheses, so we can better compare the contents */
1312 sb_strdup (R_cur_r, R_last_ij); 1261 remove_epsilon(R_last_ij, &R_temp_ij);
1313 else if (GNUNET_YES == needs_parentheses (&R_temp_kk)) 1262 remove_parentheses(&R_temp_ij);
1314 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); 1263
1315 else 1264 if ((0 == sb_strcmp(&R_temp_ij, &R_temp_ik)) &&
1316 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk); 1265 (0 == sb_strcmp(&R_temp_ik, &R_temp_kk)) &&
1317 R_cur_l->null_flag = GNUNET_YES; 1266 (0 == sb_strcmp(&R_temp_kk, &R_temp_kj)))
1318 } 1267 {
1319 else if ((0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && 1268 if (0 == R_temp_ij.slen)
1320 (0 != clean_kk_kj_cmp)) 1269 {
1321 { 1270 R_cur_r->null_flag = GNUNET_NO;
1322 /* a|bb*a = b*a */ 1271 }
1323 if (R_last_kk->slen < 1) 1272 else if ((0 == sb_strncmp_cstr(R_last_ij, "(|", 2)) ||
1324 { 1273 (0 == sb_strncmp_cstr(R_last_ik, "(|", 2) &&
1325 sb_strdup (R_cur_r, R_last_kj); 1274 0 == sb_strncmp_cstr(R_last_kj, "(|", 2)))
1326 } 1275 {
1327 else if (GNUNET_YES == needs_parentheses (&R_temp_kk)) 1276 /*
1328 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj); 1277 * a|(e|a)a*(e|a) = a*
1329 else 1278 * a|(e|a)(e|a)*(e|a) = a*
1330 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj); 1279 * (e|a)|aa*a = a*
1280 * (e|a)|aa*(e|a) = a*
1281 * (e|a)|(e|a)a*a = a*
1282 * (e|a)|(e|a)a*(e|a) = a*
1283 * (e|a)|(e|a)(e|a)*(e|a) = a*
1284 */
1285 if (GNUNET_YES == needs_parentheses(&R_temp_ij))
1286 sb_printf1(R_cur_r, "(%.*s)*", 3, &R_temp_ij);
1287 else
1288 sb_printf1(R_cur_r, "%.*s*", 1, &R_temp_ij);
1289 }
1290 else
1291 {
1292 /*
1293 * a|aa*a = a+
1294 * a|(e|a)a*a = a+
1295 * a|aa*(e|a) = a+
1296 * a|(e|a)(e|a)*a = a+
1297 * a|a(e|a)*(e|a) = a+
1298 */
1299 if (GNUNET_YES == needs_parentheses(&R_temp_ij))
1300 sb_printf1(R_cur_r, "(%.*s)+", 3, &R_temp_ij);
1301 else
1302 sb_printf1(R_cur_r, "%.*s+", 1, &R_temp_ij);
1303 }
1304 }
1305 else if ((0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) &&
1306 (0 != clean_ik_kk_cmp))
1307 {
1308 /* a|ab*b = ab* */
1309 if (0 == R_last_kk->slen)
1310 sb_strdup(R_cur_r, R_last_ij);
1311 else if (GNUNET_YES == needs_parentheses(&R_temp_kk))
1312 sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1313 else
1314 sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk);
1315 R_cur_l->null_flag = GNUNET_YES;
1316 }
1317 else if ((0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) &&
1318 (0 != clean_kk_kj_cmp))
1319 {
1320 /* a|bb*a = b*a */
1321 if (R_last_kk->slen < 1)
1322 {
1323 sb_strdup(R_cur_r, R_last_kj);
1324 }
1325 else if (GNUNET_YES == needs_parentheses(&R_temp_kk))
1326 sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1327 else
1328 sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1331 1329
1332 R_cur_l->null_flag = GNUNET_YES; 1330 R_cur_l->null_flag = GNUNET_YES;
1333 } 1331 }
1334 else if ((0 == ij_ik_cmp) && (0 == kk_kj_cmp) && 1332 else if ((0 == ij_ik_cmp) && (0 == kk_kj_cmp) &&
1335 (! has_epsilon (R_last_ij)) && has_epsilon (R_last_kk)) 1333 (!has_epsilon(R_last_ij)) && has_epsilon(R_last_kk))
1336 { 1334 {
1337 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */ 1335 /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */
1338 if (needs_parentheses (&R_temp_kk)) 1336 if (needs_parentheses(&R_temp_kk))
1339 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); 1337 sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk);
1338 else
1339 sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk);
1340 R_cur_l->null_flag = GNUNET_YES;
1341 }
1342 else if ((0 == ij_kj_cmp) && (0 == ik_kk_cmp) &&
1343 (!has_epsilon(R_last_ij)) && has_epsilon(R_last_kk))
1344 {
1345 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */
1346 if (needs_parentheses(&R_temp_kk))
1347 sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1348 else
1349 sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1350 R_cur_l->null_flag = GNUNET_YES;
1351 }
1340 else 1352 else
1341 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk); 1353 {
1342 R_cur_l->null_flag = GNUNET_YES; 1354 sb_strdup(R_cur_l, R_last_ij);
1355 remove_parentheses(R_cur_l);
1356 }
1343 } 1357 }
1344 else if ((0 == ij_kj_cmp) && (0 == ik_kk_cmp) && 1358 else
1345 (! has_epsilon (R_last_ij)) && has_epsilon (R_last_kk))
1346 { 1359 {
1347 /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */ 1360 /* we have no left side */
1348 if (needs_parentheses (&R_temp_kk))
1349 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij);
1350 else
1351 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij);
1352 R_cur_l->null_flag = GNUNET_YES; 1361 R_cur_l->null_flag = GNUNET_YES;
1353 } 1362 }
1354 else
1355 {
1356 sb_strdup (R_cur_l, R_last_ij);
1357 remove_parentheses (R_cur_l);
1358 }
1359 }
1360 else
1361 {
1362 /* we have no left side */
1363 R_cur_l->null_flag = GNUNET_YES;
1364 }
1365 1363
1366 /* construct R_cur_r, if not already constructed */ 1364 /* construct R_cur_r, if not already constructed */
1367 if (GNUNET_YES == R_cur_r->null_flag) 1365 if (GNUNET_YES == R_cur_r->null_flag)
1368 {
1369 length = R_temp_kk.slen - R_last_ik->slen;
1370
1371 /* a(ba)*bx = (ab)+x */
1372 if ((length > 0) && (GNUNET_YES != R_last_kk->null_flag) &&
1373 (0 < R_last_kk->slen) && (GNUNET_YES != R_last_kj->null_flag) &&
1374 (0 < R_last_kj->slen) && (GNUNET_YES != R_last_ik->null_flag) &&
1375 (0 < R_last_ik->slen) &&
1376 (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) &&
1377 (0 == sb_strncmp (&R_temp_kk, R_last_kj, length)))
1378 {
1379 struct StringBuffer temp_a;
1380 struct StringBuffer temp_b;
1381
1382 sb_init (&temp_a, length);
1383 sb_init (&temp_b, R_last_kj->slen - length);
1384
1385 length_l = length;
1386 temp_a.sbuf = temp_a.abuf;
1387 GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l);
1388 temp_a.slen = length_l;
1389
1390 length_r = R_last_kj->slen - length;
1391 temp_b.sbuf = temp_b.abuf;
1392 GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1393 temp_b.slen = length_r;
1394
1395 /* e|(ab)+ = (ab)* */
1396 if ((GNUNET_YES != R_cur_l->null_flag) && (0 == R_cur_l->slen) &&
1397 (0 == temp_b.slen))
1398 {
1399 sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1400 sb_free (R_cur_l);
1401 R_cur_l->null_flag = GNUNET_YES;
1402 }
1403 else
1404 {
1405 sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1406 }
1407 sb_free (&temp_a);
1408 sb_free (&temp_b);
1409 }
1410 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk) &&
1411 0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1412 { 1366 {
1367 length = R_temp_kk.slen - R_last_ik->slen;
1368
1369 /* a(ba)*bx = (ab)+x */
1370 if ((length > 0) && (GNUNET_YES != R_last_kk->null_flag) &&
1371 (0 < R_last_kk->slen) && (GNUNET_YES != R_last_kj->null_flag) &&
1372 (0 < R_last_kj->slen) && (GNUNET_YES != R_last_ik->null_flag) &&
1373 (0 < R_last_ik->slen) &&
1374 (0 == sb_strkcmp(&R_temp_kk, R_last_ik, length)) &&
1375 (0 == sb_strncmp(&R_temp_kk, R_last_kj, length)))
1376 {
1377 struct StringBuffer temp_a;
1378 struct StringBuffer temp_b;
1379
1380 sb_init(&temp_a, length);
1381 sb_init(&temp_b, R_last_kj->slen - length);
1382
1383 length_l = length;
1384 temp_a.sbuf = temp_a.abuf;
1385 GNUNET_memcpy(temp_a.sbuf, R_last_kj->sbuf, length_l);
1386 temp_a.slen = length_l;
1387
1388 length_r = R_last_kj->slen - length;
1389 temp_b.sbuf = temp_b.abuf;
1390 GNUNET_memcpy(temp_b.sbuf, &R_last_kj->sbuf[length], length_r);
1391 temp_b.slen = length_r;
1392
1393 /* e|(ab)+ = (ab)* */
1394 if ((GNUNET_YES != R_cur_l->null_flag) && (0 == R_cur_l->slen) &&
1395 (0 == temp_b.slen))
1396 {
1397 sb_printf2(R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a);
1398 sb_free(R_cur_l);
1399 R_cur_l->null_flag = GNUNET_YES;
1400 }
1401 else
1402 {
1403 sb_printf3(R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b);
1404 }
1405 sb_free(&temp_a);
1406 sb_free(&temp_b);
1407 }
1408 else if (0 == sb_strcmp(&R_temp_ik, &R_temp_kk) &&
1409 0 == sb_strcmp(&R_temp_kk, &R_temp_kj))
1410 {
1411 /*
1412 * (e|a)a*(e|a) = a*
1413 * (e|a)(e|a)*(e|a) = a*
1414 */
1415 if (has_epsilon(R_last_ik) && has_epsilon(R_last_kj))
1416 {
1417 if (needs_parentheses(&R_temp_kk))
1418 sb_printf1(R_cur_r, "(%.*s)*", 3, &R_temp_kk);
1419 else
1420 sb_printf1(R_cur_r, "%.*s*", 1, &R_temp_kk);
1421 }
1422 /* aa*a = a+a */
1423 else if ((0 == clean_ik_kk_cmp) && (0 == clean_kk_kj_cmp) &&
1424 (!has_epsilon(R_last_ik)))
1425 {
1426 if (needs_parentheses(&R_temp_kk))
1427 sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk);
1428 else
1429 sb_printf2(R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk);
1430 }
1431 /*
1432 * (e|a)a*a = a+
1433 * aa*(e|a) = a+
1434 * a(e|a)*(e|a) = a+
1435 * (e|a)a*a = a+
1436 */
1437 else
1438 {
1439 eps_check = (has_epsilon(R_last_ik) + has_epsilon(R_last_kk) +
1440 has_epsilon(R_last_kj));
1441
1442 if (1 == eps_check)
1443 {
1444 if (needs_parentheses(&R_temp_kk))
1445 sb_printf1(R_cur_r, "(%.*s)+", 3, &R_temp_kk);
1446 else
1447 sb_printf1(R_cur_r, "%.*s+", 1, &R_temp_kk);
1448 }
1449 }
1450 }
1413 /* 1451 /*
1414 * (e|a)a*(e|a) = a* 1452 * aa*b = a+b
1415 * (e|a)(e|a)*(e|a) = a* 1453 * (e|a)(e|a)*b = a*b
1416 */ 1454 */
1417 if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj)) 1455 else if (0 == sb_strcmp(&R_temp_ik, &R_temp_kk))
1418 { 1456 {
1419 if (needs_parentheses (&R_temp_kk)) 1457 if (has_epsilon(R_last_ik))
1420 sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk); 1458 {
1421 else 1459 if (needs_parentheses(&R_temp_kk))
1422 sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk); 1460 sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1423 } 1461 else
1424 /* aa*a = a+a */ 1462 sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1425 else if ((0 == clean_ik_kk_cmp) && (0 == clean_kk_kj_cmp) && 1463 }
1426 (! has_epsilon (R_last_ik))) 1464 else
1427 { 1465 {
1428 if (needs_parentheses (&R_temp_kk)) 1466 if (needs_parentheses(&R_temp_kk))
1429 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk); 1467 sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1430 else 1468 else
1431 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk); 1469 sb_printf2(R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1432 } 1470 }
1471 }
1433 /* 1472 /*
1434 * (e|a)a*a = a+ 1473 * ba*a = ba+
1435 * aa*(e|a) = a+ 1474 * b(e|a)*(e|a) = ba*
1436 * a(e|a)*(e|a) = a+
1437 * (e|a)a*a = a+
1438 */ 1475 */
1439 else 1476 else if (0 == sb_strcmp(&R_temp_kk, &R_temp_kj))
1440 {
1441 eps_check = (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) +
1442 has_epsilon (R_last_kj));
1443
1444 if (1 == eps_check)
1445 { 1477 {
1446 if (needs_parentheses (&R_temp_kk)) 1478 if (has_epsilon(R_last_kj))
1447 sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk); 1479 {
1480 if (needs_parentheses(&R_temp_kk))
1481 sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1482 else
1483 sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1484 }
1448 else 1485 else
1449 sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk); 1486 {
1487 if (needs_parentheses(&R_temp_kk))
1488 sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1489 else
1490 sb_printf2(R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1491 }
1450 } 1492 }
1451 }
1452 }
1453 /*
1454 * aa*b = a+b
1455 * (e|a)(e|a)*b = a*b
1456 */
1457 else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk))
1458 {
1459 if (has_epsilon (R_last_ik))
1460 {
1461 if (needs_parentheses (&R_temp_kk))
1462 sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj);
1463 else
1464 sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj);
1465 }
1466 else 1493 else
1467 {
1468 if (needs_parentheses (&R_temp_kk))
1469 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj);
1470 else
1471 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj);
1472 }
1473 }
1474 /*
1475 * ba*a = ba+
1476 * b(e|a)*(e|a) = ba*
1477 */
1478 else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))
1479 {
1480 if (has_epsilon (R_last_kj))
1481 {
1482 if (needs_parentheses (&R_temp_kk))
1483 sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk);
1484 else
1485 sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk);
1486 }
1487 else
1488 {
1489 if (needs_parentheses (&R_temp_kk))
1490 sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk);
1491 else
1492 sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk);
1493 }
1494 }
1495 else
1496 {
1497 if (0 < R_temp_kk.slen)
1498 {
1499 if (needs_parentheses (&R_temp_kk))
1500 { 1494 {
1501 sb_printf3 (R_cur_r, 1495 if (0 < R_temp_kk.slen)
1502 "%.*s(%.*s)*%.*s", 1496 {
1503 3, 1497 if (needs_parentheses(&R_temp_kk))
1504 R_last_ik, 1498 {
1505 &R_temp_kk, 1499 sb_printf3(R_cur_r,
1506 R_last_kj); 1500 "%.*s(%.*s)*%.*s",
1507 } 1501 3,
1508 else 1502 R_last_ik,
1509 { 1503 &R_temp_kk,
1510 sb_printf3 (R_cur_r, 1504 R_last_kj);
1511 "%.*s%.*s*%.*s", 1505 }
1512 1, 1506 else
1513 R_last_ik, 1507 {
1514 &R_temp_kk, 1508 sb_printf3(R_cur_r,
1515 R_last_kj); 1509 "%.*s%.*s*%.*s",
1510 1,
1511 R_last_ik,
1512 &R_temp_kk,
1513 R_last_kj);
1514 }
1515 }
1516 else
1517 {
1518 sb_printf2(R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1519 }
1516 } 1520 }
1517 }
1518 else
1519 {
1520 sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj);
1521 }
1522 } 1521 }
1523 } 1522 sb_free(&R_temp_ij);
1524 sb_free (&R_temp_ij); 1523 sb_free(&R_temp_ik);
1525 sb_free (&R_temp_ik); 1524 sb_free(&R_temp_kk);
1526 sb_free (&R_temp_kk); 1525 sb_free(&R_temp_kj);
1527 sb_free (&R_temp_kj);
1528 1526
1529 if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag)) 1527 if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag))
1530 { 1528 {
1531 R_cur_ij->null_flag = GNUNET_YES; 1529 R_cur_ij->null_flag = GNUNET_YES;
1532 return; 1530 return;
1533 } 1531 }
1534 1532
1535 if ((GNUNET_YES != R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag)) 1533 if ((GNUNET_YES != R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag))
1536 { 1534 {
1537 struct StringBuffer tmp; 1535 struct StringBuffer tmp;
1538 1536
1539 tmp = *R_cur_ij; 1537 tmp = *R_cur_ij;
1540 *R_cur_ij = *R_cur_l; 1538 *R_cur_ij = *R_cur_l;
1541 *R_cur_l = tmp; 1539 *R_cur_l = tmp;
1542 return; 1540 return;
1543 } 1541 }
1544 1542
1545 if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES != R_cur_r->null_flag)) 1543 if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES != R_cur_r->null_flag))
1546 { 1544 {
1547 struct StringBuffer tmp; 1545 struct StringBuffer tmp;
1548 1546
1549 tmp = *R_cur_ij; 1547 tmp = *R_cur_ij;
1550 *R_cur_ij = *R_cur_r; 1548 *R_cur_ij = *R_cur_r;
1551 *R_cur_r = tmp; 1549 *R_cur_r = tmp;
1552 return; 1550 return;
1553 } 1551 }
1554 1552
1555 if (0 == sb_nullstrcmp (R_cur_l, R_cur_r)) 1553 if (0 == sb_nullstrcmp(R_cur_l, R_cur_r))
1556 { 1554 {
1557 struct StringBuffer tmp; 1555 struct StringBuffer tmp;
1558 1556
1559 tmp = *R_cur_ij; 1557 tmp = *R_cur_ij;
1560 *R_cur_ij = *R_cur_l; 1558 *R_cur_ij = *R_cur_l;
1561 *R_cur_l = tmp; 1559 *R_cur_l = tmp;
1562 return; 1560 return;
1563 } 1561 }
1564 sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r); 1562 sb_printf2(R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r);
1565} 1563}
1566 1564
1567 1565
@@ -1577,7 +1575,7 @@ automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij,
1577 * @param a automaton for which to assign proofs and hashes, must not be NULL 1575 * @param a automaton for which to assign proofs and hashes, must not be NULL
1578 */ 1576 */
1579static int 1577static int
1580automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a) 1578automaton_create_proofs(struct REGEX_INTERNAL_Automaton *a)
1581{ 1579{
1582 unsigned int n = a->state_count; 1580 unsigned int n = a->state_count;
1583 struct REGEX_INTERNAL_State *states[n]; 1581 struct REGEX_INTERNAL_State *states[n];
@@ -1592,143 +1590,143 @@ automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1592 unsigned int j; 1590 unsigned int j;
1593 unsigned int k; 1591 unsigned int k;
1594 1592
1595 R_last = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n); 1593 R_last = GNUNET_malloc_large(sizeof(struct StringBuffer) * n * n);
1596 R_cur = GNUNET_malloc_large (sizeof (struct StringBuffer) * n * n); 1594 R_cur = GNUNET_malloc_large(sizeof(struct StringBuffer) * n * n);
1597 if ((NULL == R_last) || (NULL == R_cur)) 1595 if ((NULL == R_last) || (NULL == R_cur))
1598 { 1596 {
1599 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); 1597 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc");
1600 GNUNET_free_non_null (R_cur); 1598 GNUNET_free_non_null(R_cur);
1601 GNUNET_free_non_null (R_last); 1599 GNUNET_free_non_null(R_last);
1602 return GNUNET_SYSERR; 1600 return GNUNET_SYSERR;
1603 } 1601 }
1604 1602
1605 /* create depth-first numbering of the states, initializes 'state' */ 1603 /* create depth-first numbering of the states, initializes 'state' */
1606 REGEX_INTERNAL_automaton_traverse (a, 1604 REGEX_INTERNAL_automaton_traverse(a,
1607 a->start, 1605 a->start,
1608 NULL, 1606 NULL,
1609 NULL, 1607 NULL,
1610 &number_states, 1608 &number_states,
1611 states); 1609 states);
1612 1610
1613 for (i = 0; i < n; i++) 1611 for (i = 0; i < n; i++)
1614 GNUNET_assert (NULL != states[i]); 1612 GNUNET_assert(NULL != states[i]);
1615 for (i = 0; i < n; i++) 1613 for (i = 0; i < n; i++)
1616 for (j = 0; j < n; j++) 1614 for (j = 0; j < n; j++)
1617 R_last[i * n + j].null_flag = GNUNET_YES; 1615 R_last[i * n + j].null_flag = GNUNET_YES;
1618 1616
1619 /* Compute regular expressions of length "1" between each pair of states */ 1617 /* Compute regular expressions of length "1" between each pair of states */
1620 for (i = 0; i < n; i++) 1618 for (i = 0; i < n; i++)
1621 {
1622 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1623 { 1619 {
1624 j = t->to_state->dfs_id; 1620 for (t = states[i]->transitions_head; NULL != t; t = t->next)
1625 if (GNUNET_YES == R_last[i * n + j].null_flag) 1621 {
1626 { 1622 j = t->to_state->dfs_id;
1627 sb_strdup_cstr (&R_last[i * n + j], t->label); 1623 if (GNUNET_YES == R_last[i * n + j].null_flag)
1628 } 1624 {
1625 sb_strdup_cstr(&R_last[i * n + j], t->label);
1626 }
1627 else
1628 {
1629 sb_append_cstr(&R_last[i * n + j], "|");
1630 sb_append_cstr(&R_last[i * n + j], t->label);
1631 }
1632 }
1633 /* add self-loop: i is reachable from i via epsilon-transition */
1634 if (GNUNET_YES == R_last[i * n + i].null_flag)
1635 {
1636 R_last[i * n + i].slen = 0;
1637 R_last[i * n + i].null_flag = GNUNET_NO;
1638 }
1629 else 1639 else
1630 { 1640 {
1631 sb_append_cstr (&R_last[i * n + j], "|"); 1641 sb_wrap(&R_last[i * n + i], "(|%.*s)", 3);
1632 sb_append_cstr (&R_last[i * n + j], t->label); 1642 }
1633 }
1634 }
1635 /* add self-loop: i is reachable from i via epsilon-transition */
1636 if (GNUNET_YES == R_last[i * n + i].null_flag)
1637 {
1638 R_last[i * n + i].slen = 0;
1639 R_last[i * n + i].null_flag = GNUNET_NO;
1640 }
1641 else
1642 {
1643 sb_wrap (&R_last[i * n + i], "(|%.*s)", 3);
1644 } 1643 }
1645 }
1646 for (i = 0; i < n; i++) 1644 for (i = 0; i < n; i++)
1647 for (j = 0; j < n; j++) 1645 for (j = 0; j < n; j++)
1648 if (needs_parentheses (&R_last[i * n + j])) 1646 if (needs_parentheses(&R_last[i * n + j]))
1649 sb_wrap (&R_last[i * n + j], "(%.*s)", 2); 1647 sb_wrap(&R_last[i * n + j], "(%.*s)", 2);
1650 /* Compute regular expressions of length "k" between each pair of states per 1648 /* Compute regular expressions of length "k" between each pair of states per
1651 * induction */ 1649 * induction */
1652 memset (&R_cur_l, 0, sizeof (struct StringBuffer)); 1650 memset(&R_cur_l, 0, sizeof(struct StringBuffer));
1653 memset (&R_cur_r, 0, sizeof (struct StringBuffer)); 1651 memset(&R_cur_r, 0, sizeof(struct StringBuffer));
1654 for (k = 0; k < n; k++) 1652 for (k = 0; k < n; k++)
1655 {
1656 for (i = 0; i < n; i++)
1657 { 1653 {
1658 for (j = 0; j < n; j++) 1654 for (i = 0; i < n; i++)
1659 { 1655 {
1660 /* Basis for the recursion: 1656 for (j = 0; j < n; j++)
1661 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} 1657 {
1662 * R_last == R^{(k-1)}, R_cur == R^{(k)} 1658 /* Basis for the recursion:
1663 */ 1659 * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj}
1664 1660 * R_last == R^{(k-1)}, R_cur == R^{(k)}
1665 /* Create R_cur[i][j] and simplify the expression */ 1661 */
1666 automaton_create_proofs_simplify (&R_last[i * n + j], 1662
1667 &R_last[i * n + k], 1663 /* Create R_cur[i][j] and simplify the expression */
1668 &R_last[k * n + k], 1664 automaton_create_proofs_simplify(&R_last[i * n + j],
1669 &R_last[k * n + j], 1665 &R_last[i * n + k],
1670 &R_cur[i * n + j], 1666 &R_last[k * n + k],
1671 &R_cur_l, 1667 &R_last[k * n + j],
1672 &R_cur_r); 1668 &R_cur[i * n + j],
1673 } 1669 &R_cur_l,
1670 &R_cur_r);
1671 }
1672 }
1673 /* set R_last = R_cur */
1674 R_swap = R_last;
1675 R_last = R_cur;
1676 R_cur = R_swap;
1677 /* clear 'R_cur' for next iteration */
1678 for (i = 0; i < n; i++)
1679 for (j = 0; j < n; j++)
1680 R_cur[i * n + j].null_flag = GNUNET_YES;
1674 } 1681 }
1675 /* set R_last = R_cur */ 1682 sb_free(&R_cur_l);
1676 R_swap = R_last; 1683 sb_free(&R_cur_r);
1677 R_last = R_cur;
1678 R_cur = R_swap;
1679 /* clear 'R_cur' for next iteration */
1680 for (i = 0; i < n; i++)
1681 for (j = 0; j < n; j++)
1682 R_cur[i * n + j].null_flag = GNUNET_YES;
1683 }
1684 sb_free (&R_cur_l);
1685 sb_free (&R_cur_r);
1686 /* assign proofs and hashes */ 1684 /* assign proofs and hashes */
1687 for (i = 0; i < n; i++) 1685 for (i = 0; i < n; i++)
1688 {
1689 if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1690 { 1686 {
1691 states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf, 1687 if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag)
1692 R_last[a->start->dfs_id * n + i].slen); 1688 {
1693 GNUNET_CRYPTO_hash (states[i]->proof, 1689 states[i]->proof = GNUNET_strndup(R_last[a->start->dfs_id * n + i].sbuf,
1694 strlen (states[i]->proof), 1690 R_last[a->start->dfs_id * n + i].slen);
1695 &states[i]->hash); 1691 GNUNET_CRYPTO_hash(states[i]->proof,
1692 strlen(states[i]->proof),
1693 &states[i]->hash);
1694 }
1696 } 1695 }
1697 }
1698 1696
1699 /* complete regex for whole DFA: union of all pairs (start state/accepting 1697 /* complete regex for whole DFA: union of all pairs (start state/accepting
1700 * state(s)). */ 1698 * state(s)). */
1701 sb_init (&complete_regex, 16 * n); 1699 sb_init(&complete_regex, 16 * n);
1702 for (i = 0; i < n; i++) 1700 for (i = 0; i < n; i++)
1703 {
1704 if (states[i]->accepting)
1705 { 1701 {
1706 if ((0 == complete_regex.slen) && 1702 if (states[i]->accepting)
1707 (0 < R_last[a->start->dfs_id * n + i].slen)) 1703 {
1708 { 1704 if ((0 == complete_regex.slen) &&
1709 sb_append (&complete_regex, &R_last[a->start->dfs_id * n + i]); 1705 (0 < R_last[a->start->dfs_id * n + i].slen))
1710 } 1706 {
1711 else if ((GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) && 1707 sb_append(&complete_regex, &R_last[a->start->dfs_id * n + i]);
1712 (0 < R_last[a->start->dfs_id * n + i].slen)) 1708 }
1713 { 1709 else if ((GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) &&
1714 sb_append_cstr (&complete_regex, "|"); 1710 (0 < R_last[a->start->dfs_id * n + i].slen))
1715 sb_append (&complete_regex, &R_last[a->start->dfs_id * n + i]); 1711 {
1716 } 1712 sb_append_cstr(&complete_regex, "|");
1713 sb_append(&complete_regex, &R_last[a->start->dfs_id * n + i]);
1714 }
1715 }
1717 } 1716 }
1718 }
1719 a->canonical_regex = 1717 a->canonical_regex =
1720 GNUNET_strndup (complete_regex.sbuf, complete_regex.slen); 1718 GNUNET_strndup(complete_regex.sbuf, complete_regex.slen);
1721 1719
1722 /* cleanup */ 1720 /* cleanup */
1723 sb_free (&complete_regex); 1721 sb_free(&complete_regex);
1724 for (i = 0; i < n; i++) 1722 for (i = 0; i < n; i++)
1725 for (j = 0; j < n; j++) 1723 for (j = 0; j < n; j++)
1726 { 1724 {
1727 sb_free (&R_cur[i * n + j]); 1725 sb_free(&R_cur[i * n + j]);
1728 sb_free (&R_last[i * n + j]); 1726 sb_free(&R_last[i * n + j]);
1729 } 1727 }
1730 GNUNET_free (R_cur); 1728 GNUNET_free(R_cur);
1731 GNUNET_free (R_last); 1729 GNUNET_free(R_last);
1732 return GNUNET_OK; 1730 return GNUNET_OK;
1733} 1731}
1734 1732
@@ -1743,8 +1741,8 @@ automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a)
1743 * @return new DFA state 1741 * @return new DFA state
1744 */ 1742 */
1745static struct REGEX_INTERNAL_State * 1743static struct REGEX_INTERNAL_State *
1746dfa_state_create (struct REGEX_INTERNAL_Context *ctx, 1744dfa_state_create(struct REGEX_INTERNAL_Context *ctx,
1747 struct REGEX_INTERNAL_StateSet *nfa_states) 1745 struct REGEX_INTERNAL_StateSet *nfa_states)
1748{ 1746{
1749 struct REGEX_INTERNAL_State *s; 1747 struct REGEX_INTERNAL_State *s;
1750 char *pos; 1748 char *pos;
@@ -1753,16 +1751,16 @@ dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1753 struct REGEX_INTERNAL_Transition *ctran; 1751 struct REGEX_INTERNAL_Transition *ctran;
1754 unsigned int i; 1752 unsigned int i;
1755 1753
1756 s = GNUNET_new (struct REGEX_INTERNAL_State); 1754 s = GNUNET_new(struct REGEX_INTERNAL_State);
1757 s->id = ctx->state_id++; 1755 s->id = ctx->state_id++;
1758 s->index = -1; 1756 s->index = -1;
1759 s->lowlink = -1; 1757 s->lowlink = -1;
1760 1758
1761 if (NULL == nfa_states) 1759 if (NULL == nfa_states)
1762 { 1760 {
1763 GNUNET_asprintf (&s->name, "s%i", s->id); 1761 GNUNET_asprintf(&s->name, "s%i", s->id);
1764 return s; 1762 return s;
1765 } 1763 }
1766 1764
1767 s->nfa_set = *nfa_states; 1765 s->nfa_set = *nfa_states;
1768 1766
@@ -1771,30 +1769,30 @@ dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1771 1769
1772 /* Create a name based on 'nfa_states' */ 1770 /* Create a name based on 'nfa_states' */
1773 len = nfa_states->off * 14 + 4; 1771 len = nfa_states->off * 14 + 4;
1774 s->name = GNUNET_malloc (len); 1772 s->name = GNUNET_malloc(len);
1775 strcat (s->name, "{"); 1773 strcat(s->name, "{");
1776 pos = s->name + 1; 1774 pos = s->name + 1;
1777 1775
1778 for (i = 0; i < nfa_states->off; i++) 1776 for (i = 0; i < nfa_states->off; i++)
1779 { 1777 {
1780 cstate = nfa_states->states[i]; 1778 cstate = nfa_states->states[i];
1781 GNUNET_snprintf (pos, pos - s->name + len, "%i,", cstate->id); 1779 GNUNET_snprintf(pos, pos - s->name + len, "%i,", cstate->id);
1782 pos += strlen (pos); 1780 pos += strlen(pos);
1783 1781
1784 /* Add a transition for each distinct label to NULL state */ 1782 /* Add a transition for each distinct label to NULL state */
1785 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next) 1783 for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next)
1786 if (NULL != ctran->label) 1784 if (NULL != ctran->label)
1787 state_add_transition (ctx, s, ctran->label, NULL); 1785 state_add_transition(ctx, s, ctran->label, NULL);
1788 1786
1789 /* If the nfa_states contain an accepting state, the new dfa state is also 1787 /* If the nfa_states contain an accepting state, the new dfa state is also
1790 * accepting. */ 1788 * accepting. */
1791 if (cstate->accepting) 1789 if (cstate->accepting)
1792 s->accepting = 1; 1790 s->accepting = 1;
1793 } 1791 }
1794 pos[-1] = '}'; 1792 pos[-1] = '}';
1795 s->name = GNUNET_realloc (s->name, strlen (s->name) + 1); 1793 s->name = GNUNET_realloc(s->name, strlen(s->name) + 1);
1796 1794
1797 memset (nfa_states, 0, sizeof (struct REGEX_INTERNAL_StateSet)); 1795 memset(nfa_states, 0, sizeof(struct REGEX_INTERNAL_StateSet));
1798 return s; 1796 return s;
1799} 1797}
1800 1798
@@ -1813,7 +1811,7 @@ dfa_state_create (struct REGEX_INTERNAL_Context *ctx,
1813 * @return length of the substring comsumed from 'str' 1811 * @return length of the substring comsumed from 'str'
1814 */ 1812 */
1815static unsigned int 1813static unsigned int
1816dfa_move (struct REGEX_INTERNAL_State **s, const char *str) 1814dfa_move(struct REGEX_INTERNAL_State **s, const char *str)
1817{ 1815{
1818 struct REGEX_INTERNAL_Transition *t; 1816 struct REGEX_INTERNAL_Transition *t;
1819 struct REGEX_INTERNAL_State *new_s; 1817 struct REGEX_INTERNAL_State *new_s;
@@ -1826,18 +1824,18 @@ dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1826 new_s = NULL; 1824 new_s = NULL;
1827 max_len = 0; 1825 max_len = 0;
1828 for (t = (*s)->transitions_head; NULL != t; t = t->next) 1826 for (t = (*s)->transitions_head; NULL != t; t = t->next)
1829 {
1830 len = strlen (t->label);
1831
1832 if (0 == strncmp (t->label, str, len))
1833 { 1827 {
1834 if (len >= max_len) 1828 len = strlen(t->label);
1835 { 1829
1836 max_len = len; 1830 if (0 == strncmp(t->label, str, len))
1837 new_s = t->to_state; 1831 {
1838 } 1832 if (len >= max_len)
1833 {
1834 max_len = len;
1835 new_s = t->to_state;
1836 }
1837 }
1839 } 1838 }
1840 }
1841 1839
1842 *s = new_s; 1840 *s = new_s;
1843 return max_len; 1841 return max_len;
@@ -1854,9 +1852,9 @@ dfa_move (struct REGEX_INTERNAL_State **s, const char *str)
1854 * @param s state where the marked attribute will be set to #GNUNET_YES. 1852 * @param s state where the marked attribute will be set to #GNUNET_YES.
1855 */ 1853 */
1856static void 1854static void
1857mark_states (void *cls, 1855mark_states(void *cls,
1858 const unsigned int count, 1856 const unsigned int count,
1859 struct REGEX_INTERNAL_State *s) 1857 struct REGEX_INTERNAL_State *s)
1860{ 1858{
1861 s->marked = GNUNET_YES; 1859 s->marked = GNUNET_YES;
1862} 1860}
@@ -1869,7 +1867,7 @@ mark_states (void *cls,
1869 * @param a DFA automaton 1867 * @param a DFA automaton
1870 */ 1868 */
1871static void 1869static void
1872dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a) 1870dfa_remove_unreachable_states(struct REGEX_INTERNAL_Automaton *a)
1873{ 1871{
1874 struct REGEX_INTERNAL_State *s; 1872 struct REGEX_INTERNAL_State *s;
1875 struct REGEX_INTERNAL_State *s_next; 1873 struct REGEX_INTERNAL_State *s_next;
@@ -1879,20 +1877,20 @@ dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1879 s->marked = GNUNET_NO; 1877 s->marked = GNUNET_NO;
1880 1878
1881 /* 2. traverse dfa from start state and mark all visited states */ 1879 /* 2. traverse dfa from start state and mark all visited states */
1882 REGEX_INTERNAL_automaton_traverse (a, 1880 REGEX_INTERNAL_automaton_traverse(a,
1883 a->start, 1881 a->start,
1884 NULL, 1882 NULL,
1885 NULL, 1883 NULL,
1886 &mark_states, 1884 &mark_states,
1887 NULL); 1885 NULL);
1888 1886
1889 /* 3. delete all states that were not visited */ 1887 /* 3. delete all states that were not visited */
1890 for (s = a->states_head; NULL != s; s = s_next) 1888 for (s = a->states_head; NULL != s; s = s_next)
1891 { 1889 {
1892 s_next = s->next; 1890 s_next = s->next;
1893 if (GNUNET_NO == s->marked) 1891 if (GNUNET_NO == s->marked)
1894 automaton_remove_state (a, s); 1892 automaton_remove_state(a, s);
1895 } 1893 }
1896} 1894}
1897 1895
1898 1896
@@ -1903,38 +1901,38 @@ dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a)
1903 * @param a DFA automaton 1901 * @param a DFA automaton
1904 */ 1902 */
1905static void 1903static void
1906dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a) 1904dfa_remove_dead_states(struct REGEX_INTERNAL_Automaton *a)
1907{ 1905{
1908 struct REGEX_INTERNAL_State *s; 1906 struct REGEX_INTERNAL_State *s;
1909 struct REGEX_INTERNAL_State *s_next; 1907 struct REGEX_INTERNAL_State *s_next;
1910 struct REGEX_INTERNAL_Transition *t; 1908 struct REGEX_INTERNAL_Transition *t;
1911 int dead; 1909 int dead;
1912 1910
1913 GNUNET_assert (DFA == a->type); 1911 GNUNET_assert(DFA == a->type);
1914 1912
1915 for (s = a->states_head; NULL != s; s = s_next) 1913 for (s = a->states_head; NULL != s; s = s_next)
1916 { 1914 {
1917 s_next = s->next; 1915 s_next = s->next;
1918 1916
1919 if (s->accepting) 1917 if (s->accepting)
1920 continue; 1918 continue;
1921 1919
1922 dead = 1; 1920 dead = 1;
1923 for (t = s->transitions_head; NULL != t; t = t->next) 1921 for (t = s->transitions_head; NULL != t; t = t->next)
1924 { 1922 {
1925 if (NULL != t->to_state && t->to_state != s) 1923 if (NULL != t->to_state && t->to_state != s)
1926 { 1924 {
1927 dead = 0; 1925 dead = 0;
1928 break; 1926 break;
1929 } 1927 }
1930 } 1928 }
1931 1929
1932 if (0 == dead) 1930 if (0 == dead)
1933 continue; 1931 continue;
1934 1932
1935 /* state s is dead, remove it */ 1933 /* state s is dead, remove it */
1936 automaton_remove_state (a, s); 1934 automaton_remove_state(a, s);
1937 } 1935 }
1938} 1936}
1939 1937
1940 1938
@@ -1946,8 +1944,8 @@ dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a)
1946 * @return #GNUNET_OK on success 1944 * @return #GNUNET_OK on success
1947 */ 1945 */
1948static int 1946static int
1949dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx, 1947dfa_merge_nondistinguishable_states(struct REGEX_INTERNAL_Context *ctx,
1950 struct REGEX_INTERNAL_Automaton *a) 1948 struct REGEX_INTERNAL_Automaton *a)
1951{ 1949{
1952 uint32_t *table; 1950 uint32_t *table;
1953 struct REGEX_INTERNAL_State *s1; 1951 struct REGEX_INTERNAL_State *s1;
@@ -1964,20 +1962,20 @@ dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1964 unsigned long long idx1; 1962 unsigned long long idx1;
1965 1963
1966 if ((NULL == a) || (0 == a->state_count)) 1964 if ((NULL == a) || (0 == a->state_count))
1967 { 1965 {
1968 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1966 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1969 "Could not merge nondistinguishable states, automaton was NULL.\n"); 1967 "Could not merge nondistinguishable states, automaton was NULL.\n");
1970 return GNUNET_SYSERR; 1968 return GNUNET_SYSERR;
1971 } 1969 }
1972 1970
1973 state_cnt = a->state_count; 1971 state_cnt = a->state_count;
1974 table = GNUNET_malloc_large ( 1972 table = GNUNET_malloc_large(
1975 (sizeof (uint32_t) * state_cnt * state_cnt / 32) + sizeof (uint32_t)); 1973 (sizeof(uint32_t) * state_cnt * state_cnt / 32) + sizeof(uint32_t));
1976 if (NULL == table) 1974 if (NULL == table)
1977 { 1975 {
1978 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); 1976 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc");
1979 return GNUNET_SYSERR; 1977 return GNUNET_SYSERR;
1980 } 1978 }
1981 1979
1982 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next) 1980 for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next)
1983 s1->marked = i++; 1981 s1->marked = i++;
@@ -1985,74 +1983,74 @@ dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
1985 /* Mark all pairs of accepting/!accepting states */ 1983 /* Mark all pairs of accepting/!accepting states */
1986 for (s1 = a->states_head; NULL != s1; s1 = s1->next) 1984 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
1987 for (s2 = a->states_head; NULL != s2; s2 = s2->next) 1985 for (s2 = a->states_head; NULL != s2; s2 = s2->next)
1988 if ((s1->accepting && ! s2->accepting) || 1986 if ((s1->accepting && !s2->accepting) ||
1989 (! s1->accepting && s2->accepting)) 1987 (!s1->accepting && s2->accepting))
1990 { 1988 {
1991 idx = (unsigned long long) s1->marked * state_cnt + s2->marked; 1989 idx = (unsigned long long)s1->marked * state_cnt + s2->marked;
1992 table[idx / 32] |= (1U << (idx % 32)); 1990 table[idx / 32] |= (1U << (idx % 32));
1993 } 1991 }
1994 1992
1995 /* Find all equal states */ 1993 /* Find all equal states */
1996 change = 1; 1994 change = 1;
1997 while (0 != change) 1995 while (0 != change)
1998 {
1999 change = 0;
2000 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2001 { 1996 {
2002 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next) 1997 change = 0;
2003 { 1998 for (s1 = a->states_head; NULL != s1; s1 = s1->next)
2004 idx = (unsigned long long) s1->marked * state_cnt + s2->marked;
2005 if (0 != (table[idx / 32] & (1U << (idx % 32))))
2006 continue;
2007 num_equal_edges = 0;
2008 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2009 { 1999 {
2010 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next) 2000 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next)
2011 {
2012 if (0 == strcmp (t1->label, t2->label))
2013 { 2001 {
2014 num_equal_edges++; 2002 idx = (unsigned long long)s1->marked * state_cnt + s2->marked;
2015 /* same edge, but targets definitively different, so we're different 2003 if (0 != (table[idx / 32] & (1U << (idx % 32))))
2016 as well */ 2004 continue;
2017 if (t1->to_state->marked > t2->to_state->marked) 2005 num_equal_edges = 0;
2018 idx1 = (unsigned long long) t1->to_state->marked * state_cnt + 2006 for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next)
2019 t2->to_state->marked; 2007 {
2020 else 2008 for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next)
2021 idx1 = (unsigned long long) t2->to_state->marked * state_cnt + 2009 {
2022 t1->to_state->marked; 2010 if (0 == strcmp(t1->label, t2->label))
2023 if (0 != (table[idx1 / 32] & (1U << (idx1 % 32)))) 2011 {
2024 { 2012 num_equal_edges++;
2025 table[idx / 32] |= (1U << (idx % 32)); 2013 /* same edge, but targets definitively different, so we're different
2026 change = 1; /* changed a marker, need to run again */ 2014 as well */
2027 } 2015 if (t1->to_state->marked > t2->to_state->marked)
2016 idx1 = (unsigned long long)t1->to_state->marked * state_cnt +
2017 t2->to_state->marked;
2018 else
2019 idx1 = (unsigned long long)t2->to_state->marked * state_cnt +
2020 t1->to_state->marked;
2021 if (0 != (table[idx1 / 32] & (1U << (idx1 % 32))))
2022 {
2023 table[idx / 32] |= (1U << (idx % 32));
2024 change = 1; /* changed a marker, need to run again */
2025 }
2026 }
2027 }
2028 }
2029 if ((num_equal_edges != s1->transition_count) ||
2030 (num_equal_edges != s2->transition_count))
2031 {
2032 /* Make sure ALL edges of possible equal states are the same */
2033 table[idx / 32] |= (1U << (idx % 32));
2034 change = 1; /* changed a marker, need to run again */
2035 }
2028 } 2036 }
2029 }
2030 } 2037 }
2031 if ((num_equal_edges != s1->transition_count) ||
2032 (num_equal_edges != s2->transition_count))
2033 {
2034 /* Make sure ALL edges of possible equal states are the same */
2035 table[idx / 32] |= (1U << (idx % 32));
2036 change = 1; /* changed a marker, need to run again */
2037 }
2038 }
2039 } 2038 }
2040 }
2041 2039
2042 /* Merge states that are equal */ 2040 /* Merge states that are equal */
2043 for (s1 = a->states_head; NULL != s1; s1 = s1_next) 2041 for (s1 = a->states_head; NULL != s1; s1 = s1_next)
2044 {
2045 s1_next = s1->next;
2046 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2047 { 2042 {
2048 s2_next = s2->next; 2043 s1_next = s1->next;
2049 idx = (unsigned long long) s1->marked * state_cnt + s2->marked; 2044 for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next)
2050 if (0 == (table[idx / 32] & (1U << (idx % 32)))) 2045 {
2051 automaton_merge_states (ctx, a, s1, s2); 2046 s2_next = s2->next;
2047 idx = (unsigned long long)s1->marked * state_cnt + s2->marked;
2048 if (0 == (table[idx / 32] & (1U << (idx % 32))))
2049 automaton_merge_states(ctx, a, s1, s2);
2050 }
2052 } 2051 }
2053 }
2054 2052
2055 GNUNET_free (table); 2053 GNUNET_free(table);
2056 return GNUNET_OK; 2054 return GNUNET_OK;
2057} 2055}
2058 2056
@@ -2066,22 +2064,22 @@ dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx,
2066 * @return GNUNET_OK on success 2064 * @return GNUNET_OK on success
2067 */ 2065 */
2068static int 2066static int
2069dfa_minimize (struct REGEX_INTERNAL_Context *ctx, 2067dfa_minimize(struct REGEX_INTERNAL_Context *ctx,
2070 struct REGEX_INTERNAL_Automaton *a) 2068 struct REGEX_INTERNAL_Automaton *a)
2071{ 2069{
2072 if (NULL == a) 2070 if (NULL == a)
2073 return GNUNET_SYSERR; 2071 return GNUNET_SYSERR;
2074 2072
2075 GNUNET_assert (DFA == a->type); 2073 GNUNET_assert(DFA == a->type);
2076 2074
2077 /* 1. remove unreachable states */ 2075 /* 1. remove unreachable states */
2078 dfa_remove_unreachable_states (a); 2076 dfa_remove_unreachable_states(a);
2079 2077
2080 /* 2. remove dead states */ 2078 /* 2. remove dead states */
2081 dfa_remove_dead_states (a); 2079 dfa_remove_dead_states(a);
2082 2080
2083 /* 3. Merge nondistinguishable states */ 2081 /* 3. Merge nondistinguishable states */
2084 if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a)) 2082 if (GNUNET_OK != dfa_merge_nondistinguishable_states(ctx, a))
2085 return GNUNET_SYSERR; 2083 return GNUNET_SYSERR;
2086 return GNUNET_OK; 2084 return GNUNET_OK;
2087} 2085}
@@ -2090,8 +2088,7 @@ dfa_minimize (struct REGEX_INTERNAL_Context *ctx,
2090/** 2088/**
2091 * Context for adding strided transitions to a DFA. 2089 * Context for adding strided transitions to a DFA.
2092 */ 2090 */
2093struct REGEX_INTERNAL_Strided_Context 2091struct REGEX_INTERNAL_Strided_Context {
2094{
2095 /** 2092 /**
2096 * Length of the strides. 2093 * Length of the strides.
2097 */ 2094 */
@@ -2121,50 +2118,50 @@ struct REGEX_INTERNAL_Strided_Context
2121 * @param s current state in the depth-first traversal 2118 * @param s current state in the depth-first traversal
2122 */ 2119 */
2123static void 2120static void
2124dfa_add_multi_strides_helper (void *cls, 2121dfa_add_multi_strides_helper(void *cls,
2125 const unsigned int depth, 2122 const unsigned int depth,
2126 char *label, 2123 char *label,
2127 struct REGEX_INTERNAL_State *start, 2124 struct REGEX_INTERNAL_State *start,
2128 struct REGEX_INTERNAL_State *s) 2125 struct REGEX_INTERNAL_State *s)
2129{ 2126{
2130 struct REGEX_INTERNAL_Strided_Context *ctx = cls; 2127 struct REGEX_INTERNAL_Strided_Context *ctx = cls;
2131 struct REGEX_INTERNAL_Transition *t; 2128 struct REGEX_INTERNAL_Transition *t;
2132 char *new_label; 2129 char *new_label;
2133 2130
2134 if (depth == ctx->stride) 2131 if (depth == ctx->stride)
2135 { 2132 {
2136 t = GNUNET_new (struct REGEX_INTERNAL_Transition); 2133 t = GNUNET_new(struct REGEX_INTERNAL_Transition);
2137 t->label = GNUNET_strdup (label); 2134 t->label = GNUNET_strdup(label);
2138 t->to_state = s; 2135 t->to_state = s;
2139 t->from_state = start; 2136 t->from_state = start;
2140 GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, 2137 GNUNET_CONTAINER_DLL_insert(ctx->transitions_head,
2141 ctx->transitions_tail, 2138 ctx->transitions_tail,
2142 t); 2139 t);
2143 } 2140 }
2144 else 2141 else
2145 {
2146 for (t = s->transitions_head; NULL != t; t = t->next)
2147 { 2142 {
2148 /* Do not consider self-loops, because it end's up in too many 2143 for (t = s->transitions_head; NULL != t; t = t->next)
2149 * transitions */ 2144 {
2150 if (t->to_state == t->from_state) 2145 /* Do not consider self-loops, because it end's up in too many
2151 continue; 2146 * transitions */
2147 if (t->to_state == t->from_state)
2148 continue;
2152 2149
2153 if (NULL != label) 2150 if (NULL != label)
2154 { 2151 {
2155 GNUNET_asprintf (&new_label, "%s%s", label, t->label); 2152 GNUNET_asprintf(&new_label, "%s%s", label, t->label);
2156 } 2153 }
2157 else 2154 else
2158 new_label = GNUNET_strdup (t->label); 2155 new_label = GNUNET_strdup(t->label);
2159 2156
2160 dfa_add_multi_strides_helper (cls, 2157 dfa_add_multi_strides_helper(cls,
2161 (depth + 1), 2158 (depth + 1),
2162 new_label, 2159 new_label,
2163 start, 2160 start,
2164 t->to_state); 2161 t->to_state);
2162 }
2165 } 2163 }
2166 } 2164 GNUNET_free_non_null(label);
2167 GNUNET_free_non_null (label);
2168} 2165}
2169 2166
2170 2167
@@ -2177,11 +2174,11 @@ dfa_add_multi_strides_helper (void *cls,
2177 * @param s current state. 2174 * @param s current state.
2178 */ 2175 */
2179static void 2176static void
2180dfa_add_multi_strides (void *cls, 2177dfa_add_multi_strides(void *cls,
2181 const unsigned int count, 2178 const unsigned int count,
2182 struct REGEX_INTERNAL_State *s) 2179 struct REGEX_INTERNAL_State *s)
2183{ 2180{
2184 dfa_add_multi_strides_helper (cls, 0, NULL, s, s); 2181 dfa_add_multi_strides_helper(cls, 0, NULL, s, s);
2185} 2182}
2186 2183
2187 2184
@@ -2193,11 +2190,11 @@ dfa_add_multi_strides (void *cls,
2193 * @param stride_len length of the strides. 2190 * @param stride_len length of the strides.
2194 */ 2191 */
2195void 2192void
2196REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx, 2193REGEX_INTERNAL_dfa_add_multi_strides(struct REGEX_INTERNAL_Context *regex_ctx,
2197 struct REGEX_INTERNAL_Automaton *dfa, 2194 struct REGEX_INTERNAL_Automaton *dfa,
2198 const unsigned int stride_len) 2195 const unsigned int stride_len)
2199{ 2196{
2200 struct REGEX_INTERNAL_Strided_Context ctx = {stride_len, NULL, NULL}; 2197 struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL };
2201 struct REGEX_INTERNAL_Transition *t; 2198 struct REGEX_INTERNAL_Transition *t;
2202 struct REGEX_INTERNAL_Transition *t_next; 2199 struct REGEX_INTERNAL_Transition *t_next;
2203 2200
@@ -2205,22 +2202,22 @@ REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2205 return; 2202 return;
2206 2203
2207 /* Compute the new transitions of given stride_len */ 2204 /* Compute the new transitions of given stride_len */
2208 REGEX_INTERNAL_automaton_traverse (dfa, 2205 REGEX_INTERNAL_automaton_traverse(dfa,
2209 dfa->start, 2206 dfa->start,
2210 NULL, 2207 NULL,
2211 NULL, 2208 NULL,
2212 &dfa_add_multi_strides, 2209 &dfa_add_multi_strides,
2213 &ctx); 2210 &ctx);
2214 2211
2215 /* Add all the new transitions to the automaton. */ 2212 /* Add all the new transitions to the automaton. */
2216 for (t = ctx.transitions_head; NULL != t; t = t_next) 2213 for (t = ctx.transitions_head; NULL != t; t = t_next)
2217 { 2214 {
2218 t_next = t->next; 2215 t_next = t->next;
2219 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state); 2216 state_add_transition(regex_ctx, t->from_state, t->label, t->to_state);
2220 GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t); 2217 GNUNET_CONTAINER_DLL_remove(ctx.transitions_head, ctx.transitions_tail, t);
2221 GNUNET_free_non_null (t->label); 2218 GNUNET_free_non_null(t->label);
2222 GNUNET_free (t); 2219 GNUNET_free(t);
2223 } 2220 }
2224 2221
2225 /* Mark this automaton as multistrided */ 2222 /* Mark this automaton as multistrided */
2226 dfa->is_multistrided = GNUNET_YES; 2223 dfa->is_multistrided = GNUNET_YES;
@@ -2240,13 +2237,13 @@ REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx,
2240 * @param transitions_tail transitions DLL. 2237 * @param transitions_tail transitions DLL.
2241 */ 2238 */
2242void 2239void
2243dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa, 2240dfa_compress_paths_helper(struct REGEX_INTERNAL_Automaton *dfa,
2244 struct REGEX_INTERNAL_State *start, 2241 struct REGEX_INTERNAL_State *start,
2245 struct REGEX_INTERNAL_State *cur, 2242 struct REGEX_INTERNAL_State *cur,
2246 char *label, 2243 char *label,
2247 unsigned int max_len, 2244 unsigned int max_len,
2248 struct REGEX_INTERNAL_Transition **transitions_head, 2245 struct REGEX_INTERNAL_Transition **transitions_head,
2249 struct REGEX_INTERNAL_Transition **transitions_tail) 2246 struct REGEX_INTERNAL_Transition **transitions_tail)
2250{ 2247{
2251 struct REGEX_INTERNAL_Transition *t; 2248 struct REGEX_INTERNAL_Transition *t;
2252 char *new_label; 2249 char *new_label;
@@ -2255,27 +2252,27 @@ dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2255 if (NULL != label && 2252 if (NULL != label &&
2256 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting || 2253 ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting ||
2257 GNUNET_YES == cur->marked) || 2254 GNUNET_YES == cur->marked) ||
2258 (start != dfa->start && max_len > 0 && max_len == strlen (label)) || 2255 (start != dfa->start && max_len > 0 && max_len == strlen(label)) ||
2259 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen (label)))) 2256 (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen(label))))
2260 { 2257 {
2261 t = GNUNET_new (struct REGEX_INTERNAL_Transition); 2258 t = GNUNET_new(struct REGEX_INTERNAL_Transition);
2262 t->label = GNUNET_strdup (label); 2259 t->label = GNUNET_strdup(label);
2263 t->to_state = cur; 2260 t->to_state = cur;
2264 t->from_state = start; 2261 t->from_state = start;
2265 GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t); 2262 GNUNET_CONTAINER_DLL_insert(*transitions_head, *transitions_tail, t);
2266 2263
2267 if (GNUNET_NO == cur->marked) 2264 if (GNUNET_NO == cur->marked)
2268 { 2265 {
2269 dfa_compress_paths_helper (dfa, 2266 dfa_compress_paths_helper(dfa,
2270 cur, 2267 cur,
2271 cur, 2268 cur,
2272 NULL, 2269 NULL,
2273 max_len, 2270 max_len,
2274 transitions_head, 2271 transitions_head,
2275 transitions_tail); 2272 transitions_tail);
2273 }
2274 return;
2276 } 2275 }
2277 return;
2278 }
2279 else if (cur != start) 2276 else if (cur != start)
2280 cur->contained = GNUNET_YES; 2277 cur->contained = GNUNET_YES;
2281 2278
@@ -2286,24 +2283,24 @@ dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2286 2283
2287 2284
2288 for (t = cur->transitions_head; NULL != t; t = t->next) 2285 for (t = cur->transitions_head; NULL != t; t = t->next)
2289 {
2290 if (NULL != label)
2291 GNUNET_asprintf (&new_label, "%s%s", label, t->label);
2292 else
2293 new_label = GNUNET_strdup (t->label);
2294
2295 if (t->to_state != cur)
2296 { 2286 {
2297 dfa_compress_paths_helper (dfa, 2287 if (NULL != label)
2298 start, 2288 GNUNET_asprintf(&new_label, "%s%s", label, t->label);
2299 t->to_state, 2289 else
2300 new_label, 2290 new_label = GNUNET_strdup(t->label);
2301 max_len, 2291
2302 transitions_head, 2292 if (t->to_state != cur)
2303 transitions_tail); 2293 {
2294 dfa_compress_paths_helper(dfa,
2295 start,
2296 t->to_state,
2297 new_label,
2298 max_len,
2299 transitions_head,
2300 transitions_tail);
2301 }
2302 GNUNET_free(new_label);
2304 } 2303 }
2305 GNUNET_free (new_label);
2306 }
2307} 2304}
2308 2305
2309 2306
@@ -2316,9 +2313,9 @@ dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa,
2316 * @param max_len maximal length of the compressed paths. 2313 * @param max_len maximal length of the compressed paths.
2317 */ 2314 */
2318static void 2315static void
2319dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx, 2316dfa_compress_paths(struct REGEX_INTERNAL_Context *regex_ctx,
2320 struct REGEX_INTERNAL_Automaton *dfa, 2317 struct REGEX_INTERNAL_Automaton *dfa,
2321 unsigned int max_len) 2318 unsigned int max_len)
2322{ 2319{
2323 struct REGEX_INTERNAL_State *s; 2320 struct REGEX_INTERNAL_State *s;
2324 struct REGEX_INTERNAL_State *s_next; 2321 struct REGEX_INTERNAL_State *s_next;
@@ -2332,47 +2329,47 @@ dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2332 2329
2333 /* Count the incoming transitions on each state. */ 2330 /* Count the incoming transitions on each state. */
2334 for (s = dfa->states_head; NULL != s; s = s->next) 2331 for (s = dfa->states_head; NULL != s; s = s->next)
2335 {
2336 for (t = s->transitions_head; NULL != t; t = t->next)
2337 { 2332 {
2338 if (NULL != t->to_state) 2333 for (t = s->transitions_head; NULL != t; t = t->next)
2339 t->to_state->incoming_transition_count++; 2334 {
2335 if (NULL != t->to_state)
2336 t->to_state->incoming_transition_count++;
2337 }
2340 } 2338 }
2341 }
2342 2339
2343 /* Unmark all states. */ 2340 /* Unmark all states. */
2344 for (s = dfa->states_head; NULL != s; s = s->next) 2341 for (s = dfa->states_head; NULL != s; s = s->next)
2345 { 2342 {
2346 s->marked = GNUNET_NO; 2343 s->marked = GNUNET_NO;
2347 s->contained = GNUNET_NO; 2344 s->contained = GNUNET_NO;
2348 } 2345 }
2349 2346
2350 /* Add strides and mark states that can be deleted. */ 2347 /* Add strides and mark states that can be deleted. */
2351 dfa_compress_paths_helper (dfa, 2348 dfa_compress_paths_helper(dfa,
2352 dfa->start, 2349 dfa->start,
2353 dfa->start, 2350 dfa->start,
2354 NULL, 2351 NULL,
2355 max_len, 2352 max_len,
2356 &transitions_head, 2353 &transitions_head,
2357 &transitions_tail); 2354 &transitions_tail);
2358 2355
2359 /* Add all the new transitions to the automaton. */ 2356 /* Add all the new transitions to the automaton. */
2360 for (t = transitions_head; NULL != t; t = t_next) 2357 for (t = transitions_head; NULL != t; t = t_next)
2361 { 2358 {
2362 t_next = t->next; 2359 t_next = t->next;
2363 state_add_transition (regex_ctx, t->from_state, t->label, t->to_state); 2360 state_add_transition(regex_ctx, t->from_state, t->label, t->to_state);
2364 GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t); 2361 GNUNET_CONTAINER_DLL_remove(transitions_head, transitions_tail, t);
2365 GNUNET_free_non_null (t->label); 2362 GNUNET_free_non_null(t->label);
2366 GNUNET_free (t); 2363 GNUNET_free(t);
2367 } 2364 }
2368 2365
2369 /* Remove marked states (including their incoming and outgoing transitions). */ 2366 /* Remove marked states (including their incoming and outgoing transitions). */
2370 for (s = dfa->states_head; NULL != s; s = s_next) 2367 for (s = dfa->states_head; NULL != s; s = s_next)
2371 { 2368 {
2372 s_next = s->next; 2369 s_next = s->next;
2373 if (GNUNET_YES == s->contained) 2370 if (GNUNET_YES == s->contained)
2374 automaton_remove_state (dfa, s); 2371 automaton_remove_state(dfa, s);
2375 } 2372 }
2376} 2373}
2377 2374
2378 2375
@@ -2386,12 +2383,12 @@ dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx,
2386 * @return new NFA fragment 2383 * @return new NFA fragment
2387 */ 2384 */
2388static struct REGEX_INTERNAL_Automaton * 2385static struct REGEX_INTERNAL_Automaton *
2389nfa_fragment_create (struct REGEX_INTERNAL_State *start, 2386nfa_fragment_create(struct REGEX_INTERNAL_State *start,
2390 struct REGEX_INTERNAL_State *end) 2387 struct REGEX_INTERNAL_State *end)
2391{ 2388{
2392 struct REGEX_INTERNAL_Automaton *n; 2389 struct REGEX_INTERNAL_Automaton *n;
2393 2390
2394 n = GNUNET_new (struct REGEX_INTERNAL_Automaton); 2391 n = GNUNET_new(struct REGEX_INTERNAL_Automaton);
2395 2392
2396 n->type = NFA; 2393 n->type = NFA;
2397 n->start = NULL; 2394 n->start = NULL;
@@ -2401,8 +2398,8 @@ nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2401 if (NULL == start || NULL == end) 2398 if (NULL == start || NULL == end)
2402 return n; 2399 return n;
2403 2400
2404 automaton_add_state (n, end); 2401 automaton_add_state(n, end);
2405 automaton_add_state (n, start); 2402 automaton_add_state(n, start);
2406 2403
2407 n->state_count = 2; 2404 n->state_count = 2;
2408 2405
@@ -2421,30 +2418,30 @@ nfa_fragment_create (struct REGEX_INTERNAL_State *start,
2421 * @param states_tail tail of the DLL of states 2418 * @param states_tail tail of the DLL of states
2422 */ 2419 */
2423static void 2420static void
2424nfa_add_states (struct REGEX_INTERNAL_Automaton *n, 2421nfa_add_states(struct REGEX_INTERNAL_Automaton *n,
2425 struct REGEX_INTERNAL_State *states_head, 2422 struct REGEX_INTERNAL_State *states_head,
2426 struct REGEX_INTERNAL_State *states_tail) 2423 struct REGEX_INTERNAL_State *states_tail)
2427{ 2424{
2428 struct REGEX_INTERNAL_State *s; 2425 struct REGEX_INTERNAL_State *s;
2429 2426
2430 if (NULL == n || NULL == states_head) 2427 if (NULL == n || NULL == states_head)
2431 { 2428 {
2432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n"); 2429 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not add states\n");
2433 return; 2430 return;
2434 } 2431 }
2435 2432
2436 if (NULL == n->states_head) 2433 if (NULL == n->states_head)
2437 { 2434 {
2438 n->states_head = states_head; 2435 n->states_head = states_head;
2439 n->states_tail = states_tail; 2436 n->states_tail = states_tail;
2440 return; 2437 return;
2441 } 2438 }
2442 2439
2443 if (NULL != states_head) 2440 if (NULL != states_head)
2444 { 2441 {
2445 n->states_tail->next = states_head; 2442 n->states_tail->next = states_head;
2446 n->states_tail = states_tail; 2443 n->states_tail = states_tail;
2447 } 2444 }
2448 2445
2449 for (s = states_head; NULL != s; s = s->next) 2446 for (s = states_head; NULL != s; s = s->next)
2450 n->state_count++; 2447 n->state_count++;
@@ -2460,11 +2457,11 @@ nfa_add_states (struct REGEX_INTERNAL_Automaton *n,
2460 * @return new NFA state 2457 * @return new NFA state
2461 */ 2458 */
2462static struct REGEX_INTERNAL_State * 2459static struct REGEX_INTERNAL_State *
2463nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting) 2460nfa_state_create(struct REGEX_INTERNAL_Context *ctx, int accepting)
2464{ 2461{
2465 struct REGEX_INTERNAL_State *s; 2462 struct REGEX_INTERNAL_State *s;
2466 2463
2467 s = GNUNET_new (struct REGEX_INTERNAL_State); 2464 s = GNUNET_new(struct REGEX_INTERNAL_State);
2468 s->id = ctx->state_id++; 2465 s->id = ctx->state_id++;
2469 s->accepting = accepting; 2466 s->accepting = accepting;
2470 s->marked = GNUNET_NO; 2467 s->marked = GNUNET_NO;
@@ -2473,7 +2470,7 @@ nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2473 s->lowlink = -1; 2470 s->lowlink = -1;
2474 s->scc_id = 0; 2471 s->scc_id = 0;
2475 s->name = NULL; 2472 s->name = NULL;
2476 GNUNET_asprintf (&s->name, "s%i", s->id); 2473 GNUNET_asprintf(&s->name, "s%i", s->id);
2477 2474
2478 return s; 2475 return s;
2479} 2476}
@@ -2489,10 +2486,10 @@ nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting)
2489 * pass NULL for epsilon transition 2486 * pass NULL for epsilon transition
2490 */ 2487 */
2491static void 2488static void
2492nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret, 2489nfa_closure_set_create(struct REGEX_INTERNAL_StateSet *ret,
2493 struct REGEX_INTERNAL_Automaton *nfa, 2490 struct REGEX_INTERNAL_Automaton *nfa,
2494 struct REGEX_INTERNAL_StateSet *states, 2491 struct REGEX_INTERNAL_StateSet *states,
2495 const char *label) 2492 const char *label)
2496{ 2493{
2497 struct REGEX_INTERNAL_State *s; 2494 struct REGEX_INTERNAL_State *s;
2498 unsigned int i; 2495 unsigned int i;
@@ -2501,58 +2498,58 @@ nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2501 struct REGEX_INTERNAL_State *currentstate; 2498 struct REGEX_INTERNAL_State *currentstate;
2502 struct REGEX_INTERNAL_Transition *ctran; 2499 struct REGEX_INTERNAL_Transition *ctran;
2503 2500
2504 memset (ret, 0, sizeof (struct REGEX_INTERNAL_StateSet)); 2501 memset(ret, 0, sizeof(struct REGEX_INTERNAL_StateSet));
2505 if (NULL == states) 2502 if (NULL == states)
2506 return; 2503 return;
2507 2504
2508 for (i = 0; i < states->off; i++) 2505 for (i = 0; i < states->off; i++)
2509 { 2506 {
2510 s = states->states[i]; 2507 s = states->states[i];
2511 2508
2512 /* Add start state to closure only for epsilon closure */ 2509 /* Add start state to closure only for epsilon closure */
2513 if (NULL == label) 2510 if (NULL == label)
2514 state_set_append (ret, s); 2511 state_set_append(ret, s);
2515 2512
2516 /* initialize work stack */ 2513 /* initialize work stack */
2517 cls_stack.head = NULL; 2514 cls_stack.head = NULL;
2518 cls_stack.tail = NULL; 2515 cls_stack.tail = NULL;
2519 GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s); 2516 GNUNET_CONTAINER_MDLL_insert(ST, cls_stack.head, cls_stack.tail, s);
2520 cls_stack.len = 1; 2517 cls_stack.len = 1;
2521 2518
2522 while (NULL != (currentstate = cls_stack.tail)) 2519 while (NULL != (currentstate = cls_stack.tail))
2523 { 2520 {
2524 GNUNET_CONTAINER_MDLL_remove (ST, 2521 GNUNET_CONTAINER_MDLL_remove(ST,
2525 cls_stack.head, 2522 cls_stack.head,
2526 cls_stack.tail, 2523 cls_stack.tail,
2527 currentstate); 2524 currentstate);
2528 cls_stack.len--; 2525 cls_stack.len--;
2529 for (ctran = currentstate->transitions_head; NULL != ctran; 2526 for (ctran = currentstate->transitions_head; NULL != ctran;
2530 ctran = ctran->next) 2527 ctran = ctran->next)
2531 { 2528 {
2532 if (NULL == (clsstate = ctran->to_state)) 2529 if (NULL == (clsstate = ctran->to_state))
2533 continue; 2530 continue;
2534 if (0 != clsstate->contained) 2531 if (0 != clsstate->contained)
2535 continue; 2532 continue;
2536 if (0 != nullstrcmp (label, ctran->label)) 2533 if (0 != nullstrcmp(label, ctran->label))
2537 continue; 2534 continue;
2538 state_set_append (ret, clsstate); 2535 state_set_append(ret, clsstate);
2539 GNUNET_CONTAINER_MDLL_insert_tail (ST, 2536 GNUNET_CONTAINER_MDLL_insert_tail(ST,
2540 cls_stack.head, 2537 cls_stack.head,
2541 cls_stack.tail, 2538 cls_stack.tail,
2542 clsstate); 2539 clsstate);
2543 cls_stack.len++; 2540 cls_stack.len++;
2544 clsstate->contained = 1; 2541 clsstate->contained = 1;
2545 } 2542 }
2543 }
2546 } 2544 }
2547 }
2548 for (i = 0; i < ret->off; i++) 2545 for (i = 0; i < ret->off; i++)
2549 ret->states[i]->contained = 0; 2546 ret->states[i]->contained = 0;
2550 2547
2551 if (ret->off > 1) 2548 if (ret->off > 1)
2552 qsort (ret->states, 2549 qsort(ret->states,
2553 ret->off, 2550 ret->off,
2554 sizeof (struct REGEX_INTERNAL_State *), 2551 sizeof(struct REGEX_INTERNAL_State *),
2555 &state_compare); 2552 &state_compare);
2556} 2553}
2557 2554
2558 2555
@@ -2562,33 +2559,33 @@ nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret,
2562 * @param ctx context 2559 * @param ctx context
2563 */ 2560 */
2564static void 2561static void
2565nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx) 2562nfa_add_concatenation(struct REGEX_INTERNAL_Context *ctx)
2566{ 2563{
2567 struct REGEX_INTERNAL_Automaton *a; 2564 struct REGEX_INTERNAL_Automaton *a;
2568 struct REGEX_INTERNAL_Automaton *b; 2565 struct REGEX_INTERNAL_Automaton *b;
2569 struct REGEX_INTERNAL_Automaton *new_nfa; 2566 struct REGEX_INTERNAL_Automaton *new_nfa;
2570 2567
2571 b = ctx->stack_tail; 2568 b = ctx->stack_tail;
2572 GNUNET_assert (NULL != b); 2569 GNUNET_assert(NULL != b);
2573 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b); 2570 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, b);
2574 a = ctx->stack_tail; 2571 a = ctx->stack_tail;
2575 GNUNET_assert (NULL != a); 2572 GNUNET_assert(NULL != a);
2576 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); 2573 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a);
2577 2574
2578 state_add_transition (ctx, a->end, NULL, b->start); 2575 state_add_transition(ctx, a->end, NULL, b->start);
2579 a->end->accepting = 0; 2576 a->end->accepting = 0;
2580 b->end->accepting = 1; 2577 b->end->accepting = 1;
2581 2578
2582 new_nfa = nfa_fragment_create (NULL, NULL); 2579 new_nfa = nfa_fragment_create(NULL, NULL);
2583 nfa_add_states (new_nfa, a->states_head, a->states_tail); 2580 nfa_add_states(new_nfa, a->states_head, a->states_tail);
2584 nfa_add_states (new_nfa, b->states_head, b->states_tail); 2581 nfa_add_states(new_nfa, b->states_head, b->states_tail);
2585 new_nfa->start = a->start; 2582 new_nfa->start = a->start;
2586 new_nfa->end = b->end; 2583 new_nfa->end = b->end;
2587 new_nfa->state_count += a->state_count + b->state_count; 2584 new_nfa->state_count += a->state_count + b->state_count;
2588 automaton_fragment_clear (a); 2585 automaton_fragment_clear(a);
2589 automaton_fragment_clear (b); 2586 automaton_fragment_clear(b);
2590 2587
2591 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); 2588 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa);
2592} 2589}
2593 2590
2594 2591
@@ -2598,7 +2595,7 @@ nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx)
2598 * @param ctx context 2595 * @param ctx context
2599 */ 2596 */
2600static void 2597static void
2601nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx) 2598nfa_add_star_op(struct REGEX_INTERNAL_Context *ctx)
2602{ 2599{
2603 struct REGEX_INTERNAL_Automaton *a; 2600 struct REGEX_INTERNAL_Automaton *a;
2604 struct REGEX_INTERNAL_Automaton *new_nfa; 2601 struct REGEX_INTERNAL_Automaton *new_nfa;
@@ -2608,31 +2605,31 @@ nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2608 a = ctx->stack_tail; 2605 a = ctx->stack_tail;
2609 2606
2610 if (NULL == a) 2607 if (NULL == a)
2611 { 2608 {
2612 GNUNET_log ( 2609 GNUNET_log(
2613 GNUNET_ERROR_TYPE_ERROR, 2610 GNUNET_ERROR_TYPE_ERROR,
2614 "nfa_add_star_op failed, because there was no element on the stack"); 2611 "nfa_add_star_op failed, because there was no element on the stack");
2615 return; 2612 return;
2616 } 2613 }
2617 2614
2618 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); 2615 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a);
2619 2616
2620 start = nfa_state_create (ctx, 0); 2617 start = nfa_state_create(ctx, 0);
2621 end = nfa_state_create (ctx, 1); 2618 end = nfa_state_create(ctx, 1);
2622 2619
2623 state_add_transition (ctx, start, NULL, a->start); 2620 state_add_transition(ctx, start, NULL, a->start);
2624 state_add_transition (ctx, start, NULL, end); 2621 state_add_transition(ctx, start, NULL, end);
2625 state_add_transition (ctx, a->end, NULL, a->start); 2622 state_add_transition(ctx, a->end, NULL, a->start);
2626 state_add_transition (ctx, a->end, NULL, end); 2623 state_add_transition(ctx, a->end, NULL, end);
2627 2624
2628 a->end->accepting = 0; 2625 a->end->accepting = 0;
2629 end->accepting = 1; 2626 end->accepting = 1;
2630 2627
2631 new_nfa = nfa_fragment_create (start, end); 2628 new_nfa = nfa_fragment_create(start, end);
2632 nfa_add_states (new_nfa, a->states_head, a->states_tail); 2629 nfa_add_states(new_nfa, a->states_head, a->states_tail);
2633 automaton_fragment_clear (a); 2630 automaton_fragment_clear(a);
2634 2631
2635 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); 2632 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa);
2636} 2633}
2637 2634
2638 2635
@@ -2642,25 +2639,25 @@ nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx)
2642 * @param ctx context 2639 * @param ctx context
2643 */ 2640 */
2644static void 2641static void
2645nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx) 2642nfa_add_plus_op(struct REGEX_INTERNAL_Context *ctx)
2646{ 2643{
2647 struct REGEX_INTERNAL_Automaton *a; 2644 struct REGEX_INTERNAL_Automaton *a;
2648 2645
2649 a = ctx->stack_tail; 2646 a = ctx->stack_tail;
2650 2647
2651 if (NULL == a) 2648 if (NULL == a)
2652 { 2649 {
2653 GNUNET_log ( 2650 GNUNET_log(
2654 GNUNET_ERROR_TYPE_ERROR, 2651 GNUNET_ERROR_TYPE_ERROR,
2655 "nfa_add_plus_op failed, because there was no element on the stack"); 2652 "nfa_add_plus_op failed, because there was no element on the stack");
2656 return; 2653 return;
2657 } 2654 }
2658 2655
2659 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); 2656 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a);
2660 2657
2661 state_add_transition (ctx, a->end, NULL, a->start); 2658 state_add_transition(ctx, a->end, NULL, a->start);
2662 2659
2663 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a); 2660 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, a);
2664} 2661}
2665 2662
2666 2663
@@ -2670,7 +2667,7 @@ nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx)
2670 * @param ctx context 2667 * @param ctx context
2671 */ 2668 */
2672static void 2669static void
2673nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx) 2670nfa_add_question_op(struct REGEX_INTERNAL_Context *ctx)
2674{ 2671{
2675 struct REGEX_INTERNAL_Automaton *a; 2672 struct REGEX_INTERNAL_Automaton *a;
2676 struct REGEX_INTERNAL_Automaton *new_nfa; 2673 struct REGEX_INTERNAL_Automaton *new_nfa;
@@ -2679,28 +2676,28 @@ nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2679 2676
2680 a = ctx->stack_tail; 2677 a = ctx->stack_tail;
2681 if (NULL == a) 2678 if (NULL == a)
2682 { 2679 {
2683 GNUNET_log ( 2680 GNUNET_log(
2684 GNUNET_ERROR_TYPE_ERROR, 2681 GNUNET_ERROR_TYPE_ERROR,
2685 "nfa_add_question_op failed, because there was no element on the stack"); 2682 "nfa_add_question_op failed, because there was no element on the stack");
2686 return; 2683 return;
2687 } 2684 }
2688 2685
2689 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); 2686 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a);
2690 2687
2691 start = nfa_state_create (ctx, 0); 2688 start = nfa_state_create(ctx, 0);
2692 end = nfa_state_create (ctx, 1); 2689 end = nfa_state_create(ctx, 1);
2693 2690
2694 state_add_transition (ctx, start, NULL, a->start); 2691 state_add_transition(ctx, start, NULL, a->start);
2695 state_add_transition (ctx, start, NULL, end); 2692 state_add_transition(ctx, start, NULL, end);
2696 state_add_transition (ctx, a->end, NULL, end); 2693 state_add_transition(ctx, a->end, NULL, end);
2697 2694
2698 a->end->accepting = 0; 2695 a->end->accepting = 0;
2699 2696
2700 new_nfa = nfa_fragment_create (start, end); 2697 new_nfa = nfa_fragment_create(start, end);
2701 nfa_add_states (new_nfa, a->states_head, a->states_tail); 2698 nfa_add_states(new_nfa, a->states_head, a->states_tail);
2702 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); 2699 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa);
2703 automaton_fragment_clear (a); 2700 automaton_fragment_clear(a);
2704} 2701}
2705 2702
2706 2703
@@ -2711,7 +2708,7 @@ nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx)
2711 * @param ctx context 2708 * @param ctx context
2712 */ 2709 */
2713static void 2710static void
2714nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx) 2711nfa_add_alternation(struct REGEX_INTERNAL_Context *ctx)
2715{ 2712{
2716 struct REGEX_INTERNAL_Automaton *a; 2713 struct REGEX_INTERNAL_Automaton *a;
2717 struct REGEX_INTERNAL_Automaton *b; 2714 struct REGEX_INTERNAL_Automaton *b;
@@ -2720,31 +2717,31 @@ nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2720 struct REGEX_INTERNAL_State *end; 2717 struct REGEX_INTERNAL_State *end;
2721 2718
2722 b = ctx->stack_tail; 2719 b = ctx->stack_tail;
2723 GNUNET_assert (NULL != b); 2720 GNUNET_assert(NULL != b);
2724 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b); 2721 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, b);
2725 a = ctx->stack_tail; 2722 a = ctx->stack_tail;
2726 GNUNET_assert (NULL != a); 2723 GNUNET_assert(NULL != a);
2727 GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); 2724 GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a);
2728 2725
2729 start = nfa_state_create (ctx, 0); 2726 start = nfa_state_create(ctx, 0);
2730 end = nfa_state_create (ctx, 1); 2727 end = nfa_state_create(ctx, 1);
2731 state_add_transition (ctx, start, NULL, a->start); 2728 state_add_transition(ctx, start, NULL, a->start);
2732 state_add_transition (ctx, start, NULL, b->start); 2729 state_add_transition(ctx, start, NULL, b->start);
2733 2730
2734 state_add_transition (ctx, a->end, NULL, end); 2731 state_add_transition(ctx, a->end, NULL, end);
2735 state_add_transition (ctx, b->end, NULL, end); 2732 state_add_transition(ctx, b->end, NULL, end);
2736 2733
2737 a->end->accepting = 0; 2734 a->end->accepting = 0;
2738 b->end->accepting = 0; 2735 b->end->accepting = 0;
2739 end->accepting = 1; 2736 end->accepting = 1;
2740 2737
2741 new_nfa = nfa_fragment_create (start, end); 2738 new_nfa = nfa_fragment_create(start, end);
2742 nfa_add_states (new_nfa, a->states_head, a->states_tail); 2739 nfa_add_states(new_nfa, a->states_head, a->states_tail);
2743 nfa_add_states (new_nfa, b->states_head, b->states_tail); 2740 nfa_add_states(new_nfa, b->states_head, b->states_tail);
2744 automaton_fragment_clear (a); 2741 automaton_fragment_clear(a);
2745 automaton_fragment_clear (b); 2742 automaton_fragment_clear(b);
2746 2743
2747 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); 2744 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa);
2748} 2745}
2749 2746
2750 2747
@@ -2755,20 +2752,20 @@ nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx)
2755 * @param label label for nfa transition 2752 * @param label label for nfa transition
2756 */ 2753 */
2757static void 2754static void
2758nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label) 2755nfa_add_label(struct REGEX_INTERNAL_Context *ctx, const char *label)
2759{ 2756{
2760 struct REGEX_INTERNAL_Automaton *n; 2757 struct REGEX_INTERNAL_Automaton *n;
2761 struct REGEX_INTERNAL_State *start; 2758 struct REGEX_INTERNAL_State *start;
2762 struct REGEX_INTERNAL_State *end; 2759 struct REGEX_INTERNAL_State *end;
2763 2760
2764 GNUNET_assert (NULL != ctx); 2761 GNUNET_assert(NULL != ctx);
2765 2762
2766 start = nfa_state_create (ctx, 0); 2763 start = nfa_state_create(ctx, 0);
2767 end = nfa_state_create (ctx, 1); 2764 end = nfa_state_create(ctx, 1);
2768 state_add_transition (ctx, start, label, end); 2765 state_add_transition(ctx, start, label, end);
2769 n = nfa_fragment_create (start, end); 2766 n = nfa_fragment_create(start, end);
2770 GNUNET_assert (NULL != n); 2767 GNUNET_assert(NULL != n);
2771 GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n); 2768 GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, n);
2772} 2769}
2773 2770
2774 2771
@@ -2778,13 +2775,13 @@ nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label)
2778 * @param ctx context 2775 * @param ctx context
2779 */ 2776 */
2780static void 2777static void
2781REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx) 2778REGEX_INTERNAL_context_init(struct REGEX_INTERNAL_Context *ctx)
2782{ 2779{
2783 if (NULL == ctx) 2780 if (NULL == ctx)
2784 { 2781 {
2785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!"); 2782 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Context was NULL!");
2786 return; 2783 return;
2787 } 2784 }
2788 ctx->state_id = 0; 2785 ctx->state_id = 0;
2789 ctx->transition_id = 0; 2786 ctx->transition_id = 0;
2790 ctx->stack_head = NULL; 2787 ctx->stack_head = NULL;
@@ -2801,7 +2798,7 @@ REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx)
2801 * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton 2798 * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton
2802 */ 2799 */
2803struct REGEX_INTERNAL_Automaton * 2800struct REGEX_INTERNAL_Automaton *
2804REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len) 2801REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len)
2805{ 2802{
2806 struct REGEX_INTERNAL_Context ctx; 2803 struct REGEX_INTERNAL_Context ctx;
2807 struct REGEX_INTERNAL_Automaton *nfa; 2804 struct REGEX_INTERNAL_Automaton *nfa;
@@ -2813,20 +2810,20 @@ REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2813 unsigned int atomcount; 2810 unsigned int atomcount;
2814 unsigned int poff; 2811 unsigned int poff;
2815 unsigned int psize; 2812 unsigned int psize;
2816 struct 2813
2817 { 2814 struct {
2818 int altcount; 2815 int altcount;
2819 int atomcount; 2816 int atomcount;
2820 } * p; 2817 } * p;
2821 2818
2822 if (NULL == regex || 0 == strlen (regex) || 0 == len) 2819 if (NULL == regex || 0 == strlen(regex) || 0 == len)
2823 { 2820 {
2824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2821 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2825 "Could not parse regex. Empty regex string provided.\n"); 2822 "Could not parse regex. Empty regex string provided.\n");
2826 2823
2827 return NULL; 2824 return NULL;
2828 } 2825 }
2829 REGEX_INTERNAL_context_init (&ctx); 2826 REGEX_INTERNAL_context_init(&ctx);
2830 2827
2831 regexp = regex; 2828 regexp = regex;
2832 curlabel[1] = '\0'; 2829 curlabel[1] = '\0';
@@ -2838,123 +2835,129 @@ REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2838 psize = 0; 2835 psize = 0;
2839 2836
2840 for (count = 0; count < len && *regexp; count++, regexp++) 2837 for (count = 0; count < len && *regexp; count++, regexp++)
2841 {
2842 switch (*regexp)
2843 { 2838 {
2844 case '(': 2839 switch (*regexp)
2845 if (atomcount > 1) 2840 {
2846 { 2841 case '(':
2847 --atomcount; 2842 if (atomcount > 1)
2848 nfa_add_concatenation (&ctx); 2843 {
2849 } 2844 --atomcount;
2850 if (poff == psize) 2845 nfa_add_concatenation(&ctx);
2851 GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */ 2846 }
2852 p[poff].altcount = altcount; 2847 if (poff == psize)
2853 p[poff].atomcount = atomcount; 2848 GNUNET_array_grow(p, psize, psize * 2 + 4); /* FIXME why *2 +4? */
2854 poff++; 2849 p[poff].altcount = altcount;
2855 altcount = 0; 2850 p[poff].atomcount = atomcount;
2856 atomcount = 0; 2851 poff++;
2857 break; 2852 altcount = 0;
2858 case '|': 2853 atomcount = 0;
2859 if (0 == atomcount) 2854 break;
2860 { 2855
2861 error_msg = "Cannot append '|' to nothing"; 2856 case '|':
2862 goto error; 2857 if (0 == atomcount)
2863 } 2858 {
2864 while (--atomcount > 0) 2859 error_msg = "Cannot append '|' to nothing";
2865 nfa_add_concatenation (&ctx); 2860 goto error;
2866 altcount++; 2861 }
2867 break; 2862 while (--atomcount > 0)
2868 case ')': 2863 nfa_add_concatenation(&ctx);
2869 if (0 == poff) 2864 altcount++;
2870 { 2865 break;
2871 error_msg = "Missing opening '('"; 2866
2872 goto error; 2867 case ')':
2873 } 2868 if (0 == poff)
2874 if (0 == atomcount) 2869 {
2875 { 2870 error_msg = "Missing opening '('";
2876 /* Ignore this: "()" */ 2871 goto error;
2877 poff--; 2872 }
2878 altcount = p[poff].altcount; 2873 if (0 == atomcount)
2879 atomcount = p[poff].atomcount; 2874 {
2880 break; 2875 /* Ignore this: "()" */
2881 } 2876 poff--;
2882 while (--atomcount > 0) 2877 altcount = p[poff].altcount;
2883 nfa_add_concatenation (&ctx); 2878 atomcount = p[poff].atomcount;
2884 for (; altcount > 0; altcount--) 2879 break;
2885 nfa_add_alternation (&ctx); 2880 }
2886 poff--; 2881 while (--atomcount > 0)
2887 altcount = p[poff].altcount; 2882 nfa_add_concatenation(&ctx);
2888 atomcount = p[poff].atomcount; 2883 for (; altcount > 0; altcount--)
2889 atomcount++; 2884 nfa_add_alternation(&ctx);
2890 break; 2885 poff--;
2891 case '*': 2886 altcount = p[poff].altcount;
2892 if (atomcount == 0) 2887 atomcount = p[poff].atomcount;
2893 { 2888 atomcount++;
2894 error_msg = "Cannot append '*' to nothing"; 2889 break;
2895 goto error; 2890
2896 } 2891 case '*':
2897 nfa_add_star_op (&ctx); 2892 if (atomcount == 0)
2898 break; 2893 {
2899 case '+': 2894 error_msg = "Cannot append '*' to nothing";
2900 if (atomcount == 0) 2895 goto error;
2901 { 2896 }
2902 error_msg = "Cannot append '+' to nothing"; 2897 nfa_add_star_op(&ctx);
2903 goto error; 2898 break;
2904 } 2899
2905 nfa_add_plus_op (&ctx); 2900 case '+':
2906 break; 2901 if (atomcount == 0)
2907 case '?': 2902 {
2908 if (atomcount == 0) 2903 error_msg = "Cannot append '+' to nothing";
2909 { 2904 goto error;
2910 error_msg = "Cannot append '?' to nothing"; 2905 }
2911 goto error; 2906 nfa_add_plus_op(&ctx);
2912 } 2907 break;
2913 nfa_add_question_op (&ctx); 2908
2914 break; 2909 case '?':
2915 default: 2910 if (atomcount == 0)
2916 if (atomcount > 1) 2911 {
2917 { 2912 error_msg = "Cannot append '?' to nothing";
2918 --atomcount; 2913 goto error;
2919 nfa_add_concatenation (&ctx); 2914 }
2920 } 2915 nfa_add_question_op(&ctx);
2921 curlabel[0] = *regexp; 2916 break;
2922 nfa_add_label (&ctx, curlabel); 2917
2923 atomcount++; 2918 default:
2924 break; 2919 if (atomcount > 1)
2920 {
2921 --atomcount;
2922 nfa_add_concatenation(&ctx);
2923 }
2924 curlabel[0] = *regexp;
2925 nfa_add_label(&ctx, curlabel);
2926 atomcount++;
2927 break;
2928 }
2925 } 2929 }
2926 }
2927 if (0 != poff) 2930 if (0 != poff)
2928 { 2931 {
2929 error_msg = "Unbalanced parenthesis"; 2932 error_msg = "Unbalanced parenthesis";
2930 goto error; 2933 goto error;
2931 } 2934 }
2932 while (--atomcount > 0) 2935 while (--atomcount > 0)
2933 nfa_add_concatenation (&ctx); 2936 nfa_add_concatenation(&ctx);
2934 for (; altcount > 0; altcount--) 2937 for (; altcount > 0; altcount--)
2935 nfa_add_alternation (&ctx); 2938 nfa_add_alternation(&ctx);
2936 2939
2937 GNUNET_array_grow (p, psize, 0); 2940 GNUNET_array_grow(p, psize, 0);
2938 2941
2939 nfa = ctx.stack_tail; 2942 nfa = ctx.stack_tail;
2940 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa); 2943 GNUNET_CONTAINER_DLL_remove(ctx.stack_head, ctx.stack_tail, nfa);
2941 2944
2942 if (NULL != ctx.stack_head) 2945 if (NULL != ctx.stack_head)
2943 { 2946 {
2944 error_msg = "Creating the NFA failed. NFA stack was not empty!"; 2947 error_msg = "Creating the NFA failed. NFA stack was not empty!";
2945 goto error; 2948 goto error;
2946 } 2949 }
2947 2950
2948 /* Remember the regex that was used to generate this NFA */ 2951 /* Remember the regex that was used to generate this NFA */
2949 nfa->regex = GNUNET_strdup (regex); 2952 nfa->regex = GNUNET_strdup(regex);
2950 2953
2951 /* create depth-first numbering of the states for pretty printing */ 2954 /* create depth-first numbering of the states for pretty printing */
2952 REGEX_INTERNAL_automaton_traverse (nfa, 2955 REGEX_INTERNAL_automaton_traverse(nfa,
2953 NULL, 2956 NULL,
2954 NULL, 2957 NULL,
2955 NULL, 2958 NULL,
2956 &number_states, 2959 &number_states,
2957 NULL); 2960 NULL);
2958 2961
2959 /* No multistriding added so far */ 2962 /* No multistriding added so far */
2960 nfa->is_multistrided = GNUNET_NO; 2963 nfa->is_multistrided = GNUNET_NO;
@@ -2962,17 +2965,17 @@ REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len)
2962 return nfa; 2965 return nfa;
2963 2966
2964error: 2967error:
2965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex); 2968 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex);
2966 if (NULL != error_msg) 2969 if (NULL != error_msg)
2967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg); 2970 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg);
2968 2971
2969 GNUNET_free_non_null (p); 2972 GNUNET_free_non_null(p);
2970 2973
2971 while (NULL != (nfa = ctx.stack_head)) 2974 while (NULL != (nfa = ctx.stack_head))
2972 { 2975 {
2973 GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa); 2976 GNUNET_CONTAINER_DLL_remove(ctx.stack_head, ctx.stack_tail, nfa);
2974 REGEX_INTERNAL_automaton_destroy (nfa); 2977 REGEX_INTERNAL_automaton_destroy(nfa);
2975 } 2978 }
2976 2979
2977 return NULL; 2980 return NULL;
2978} 2981}
@@ -2988,10 +2991,10 @@ error:
2988 * for starting. 2991 * for starting.
2989 */ 2992 */
2990static void 2993static void
2991construct_dfa_states (struct REGEX_INTERNAL_Context *ctx, 2994construct_dfa_states(struct REGEX_INTERNAL_Context *ctx,
2992 struct REGEX_INTERNAL_Automaton *nfa, 2995 struct REGEX_INTERNAL_Automaton *nfa,
2993 struct REGEX_INTERNAL_Automaton *dfa, 2996 struct REGEX_INTERNAL_Automaton *dfa,
2994 struct REGEX_INTERNAL_State *dfa_state) 2997 struct REGEX_INTERNAL_State *dfa_state)
2995{ 2998{
2996 struct REGEX_INTERNAL_Transition *ctran; 2999 struct REGEX_INTERNAL_Transition *ctran;
2997 struct REGEX_INTERNAL_State *new_dfa_state; 3000 struct REGEX_INTERNAL_State *new_dfa_state;
@@ -3001,37 +3004,37 @@ construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
3001 struct REGEX_INTERNAL_StateSet nfa_set; 3004 struct REGEX_INTERNAL_StateSet nfa_set;
3002 3005
3003 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next) 3006 for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next)
3004 { 3007 {
3005 if (NULL == ctran->label || NULL != ctran->to_state) 3008 if (NULL == ctran->label || NULL != ctran->to_state)
3006 continue; 3009 continue;
3007 3010
3008 nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label); 3011 nfa_closure_set_create(&tmp, nfa, &dfa_state->nfa_set, ctran->label);
3009 nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL); 3012 nfa_closure_set_create(&nfa_set, nfa, &tmp, NULL);
3010 state_set_clear (&tmp); 3013 state_set_clear(&tmp);
3011 3014
3012 state_contains = NULL; 3015 state_contains = NULL;
3013 for (state_iter = dfa->states_head; NULL != state_iter; 3016 for (state_iter = dfa->states_head; NULL != state_iter;
3014 state_iter = state_iter->next) 3017 state_iter = state_iter->next)
3015 { 3018 {
3016 if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set)) 3019 if (0 == state_set_compare(&state_iter->nfa_set, &nfa_set))
3017 { 3020 {
3018 state_contains = state_iter; 3021 state_contains = state_iter;
3019 break; 3022 break;
3020 } 3023 }
3021 } 3024 }
3022 if (NULL == state_contains) 3025 if (NULL == state_contains)
3023 { 3026 {
3024 new_dfa_state = dfa_state_create (ctx, &nfa_set); 3027 new_dfa_state = dfa_state_create(ctx, &nfa_set);
3025 automaton_add_state (dfa, new_dfa_state); 3028 automaton_add_state(dfa, new_dfa_state);
3026 ctran->to_state = new_dfa_state; 3029 ctran->to_state = new_dfa_state;
3027 construct_dfa_states (ctx, nfa, dfa, new_dfa_state); 3030 construct_dfa_states(ctx, nfa, dfa, new_dfa_state);
3028 } 3031 }
3029 else 3032 else
3030 { 3033 {
3031 ctran->to_state = state_contains; 3034 ctran->to_state = state_contains;
3032 state_set_clear (&nfa_set); 3035 state_set_clear(&nfa_set);
3036 }
3033 } 3037 }
3034 }
3035} 3038}
3036 3039
3037 3040
@@ -3053,9 +3056,9 @@ construct_dfa_states (struct REGEX_INTERNAL_Context *ctx,
3053 * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy. 3056 * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
3054 */ 3057 */
3055struct REGEX_INTERNAL_Automaton * 3058struct REGEX_INTERNAL_Automaton *
3056REGEX_INTERNAL_construct_dfa (const char *regex, 3059REGEX_INTERNAL_construct_dfa(const char *regex,
3057 const size_t len, 3060 const size_t len,
3058 unsigned int max_path_len) 3061 unsigned int max_path_len)
3059{ 3062{
3060 struct REGEX_INTERNAL_Context ctx; 3063 struct REGEX_INTERNAL_Context ctx;
3061 struct REGEX_INTERNAL_Automaton *dfa; 3064 struct REGEX_INTERNAL_Automaton *dfa;
@@ -3063,50 +3066,50 @@ REGEX_INTERNAL_construct_dfa (const char *regex,
3063 struct REGEX_INTERNAL_StateSet nfa_start_eps_cls; 3066 struct REGEX_INTERNAL_StateSet nfa_start_eps_cls;
3064 struct REGEX_INTERNAL_StateSet singleton_set; 3067 struct REGEX_INTERNAL_StateSet singleton_set;
3065 3068
3066 REGEX_INTERNAL_context_init (&ctx); 3069 REGEX_INTERNAL_context_init(&ctx);
3067 3070
3068 /* Create NFA */ 3071 /* Create NFA */
3069 nfa = REGEX_INTERNAL_construct_nfa (regex, len); 3072 nfa = REGEX_INTERNAL_construct_nfa(regex, len);
3070 3073
3071 if (NULL == nfa) 3074 if (NULL == nfa)
3072 { 3075 {
3073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 3076 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
3074 "Could not create DFA, because NFA creation failed\n"); 3077 "Could not create DFA, because NFA creation failed\n");
3075 return NULL; 3078 return NULL;
3076 } 3079 }
3077 3080
3078 dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton); 3081 dfa = GNUNET_new(struct REGEX_INTERNAL_Automaton);
3079 dfa->type = DFA; 3082 dfa->type = DFA;
3080 dfa->regex = GNUNET_strdup (regex); 3083 dfa->regex = GNUNET_strdup(regex);
3081 3084
3082 /* Create DFA start state from epsilon closure */ 3085 /* Create DFA start state from epsilon closure */
3083 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet)); 3086 memset(&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet));
3084 state_set_append (&singleton_set, nfa->start); 3087 state_set_append(&singleton_set, nfa->start);
3085 nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL); 3088 nfa_closure_set_create(&nfa_start_eps_cls, nfa, &singleton_set, NULL);
3086 state_set_clear (&singleton_set); 3089 state_set_clear(&singleton_set);
3087 dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls); 3090 dfa->start = dfa_state_create(&ctx, &nfa_start_eps_cls);
3088 automaton_add_state (dfa, dfa->start); 3091 automaton_add_state(dfa, dfa->start);
3089 3092
3090 construct_dfa_states (&ctx, nfa, dfa, dfa->start); 3093 construct_dfa_states(&ctx, nfa, dfa, dfa->start);
3091 REGEX_INTERNAL_automaton_destroy (nfa); 3094 REGEX_INTERNAL_automaton_destroy(nfa);
3092 3095
3093 /* Minimize DFA */ 3096 /* Minimize DFA */
3094 if (GNUNET_OK != dfa_minimize (&ctx, dfa)) 3097 if (GNUNET_OK != dfa_minimize(&ctx, dfa))
3095 { 3098 {
3096 REGEX_INTERNAL_automaton_destroy (dfa); 3099 REGEX_INTERNAL_automaton_destroy(dfa);
3097 return NULL; 3100 return NULL;
3098 } 3101 }
3099 3102
3100 /* Create proofs and hashes for all states */ 3103 /* Create proofs and hashes for all states */
3101 if (GNUNET_OK != automaton_create_proofs (dfa)) 3104 if (GNUNET_OK != automaton_create_proofs(dfa))
3102 { 3105 {
3103 REGEX_INTERNAL_automaton_destroy (dfa); 3106 REGEX_INTERNAL_automaton_destroy(dfa);
3104 return NULL; 3107 return NULL;
3105 } 3108 }
3106 3109
3107 /* Compress linear DFA paths */ 3110 /* Compress linear DFA paths */
3108 if (1 != max_path_len) 3111 if (1 != max_path_len)
3109 dfa_compress_paths (&ctx, dfa, max_path_len); 3112 dfa_compress_paths(&ctx, dfa, max_path_len);
3110 3113
3111 return dfa; 3114 return dfa;
3112} 3115}
@@ -3119,7 +3122,7 @@ REGEX_INTERNAL_construct_dfa (const char *regex,
3119 * @param a automaton to be destroyed 3122 * @param a automaton to be destroyed
3120 */ 3123 */
3121void 3124void
3122REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a) 3125REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a)
3123{ 3126{
3124 struct REGEX_INTERNAL_State *s; 3127 struct REGEX_INTERNAL_State *s;
3125 struct REGEX_INTERNAL_State *next_state; 3128 struct REGEX_INTERNAL_State *next_state;
@@ -3127,17 +3130,17 @@ REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3127 if (NULL == a) 3130 if (NULL == a)
3128 return; 3131 return;
3129 3132
3130 GNUNET_free_non_null (a->regex); 3133 GNUNET_free_non_null(a->regex);
3131 GNUNET_free_non_null (a->canonical_regex); 3134 GNUNET_free_non_null(a->canonical_regex);
3132 3135
3133 for (s = a->states_head; NULL != s; s = next_state) 3136 for (s = a->states_head; NULL != s; s = next_state)
3134 { 3137 {
3135 next_state = s->next; 3138 next_state = s->next;
3136 GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s); 3139 GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s);
3137 automaton_destroy_state (s); 3140 automaton_destroy_state(s);
3138 } 3141 }
3139 3142
3140 GNUNET_free (a); 3143 GNUNET_free(a);
3141} 3144}
3142 3145
3143 3146
@@ -3150,32 +3153,32 @@ REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a)
3150 * @return 0 if string matches, non-0 otherwise 3153 * @return 0 if string matches, non-0 otherwise
3151 */ 3154 */
3152static int 3155static int
3153evaluate_dfa (struct REGEX_INTERNAL_Automaton *a, const char *string) 3156evaluate_dfa(struct REGEX_INTERNAL_Automaton *a, const char *string)
3154{ 3157{
3155 const char *strp; 3158 const char *strp;
3156 struct REGEX_INTERNAL_State *s; 3159 struct REGEX_INTERNAL_State *s;
3157 unsigned int step_len; 3160 unsigned int step_len;
3158 3161
3159 if (DFA != a->type) 3162 if (DFA != a->type)
3160 { 3163 {
3161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 3164 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
3162 "Tried to evaluate DFA, but NFA automaton given"); 3165 "Tried to evaluate DFA, but NFA automaton given");
3163 return -1; 3166 return -1;
3164 } 3167 }
3165 3168
3166 s = a->start; 3169 s = a->start;
3167 3170
3168 /* If the string is empty but the starting state is accepting, we accept. */ 3171 /* If the string is empty but the starting state is accepting, we accept. */
3169 if ((NULL == string || 0 == strlen (string)) && s->accepting) 3172 if ((NULL == string || 0 == strlen(string)) && s->accepting)
3170 return 0; 3173 return 0;
3171 3174
3172 for (strp = string; NULL != strp && *strp; strp += step_len) 3175 for (strp = string; NULL != strp && *strp; strp += step_len)
3173 { 3176 {
3174 step_len = dfa_move (&s, strp); 3177 step_len = dfa_move(&s, strp);
3175 3178
3176 if (NULL == s) 3179 if (NULL == s)
3177 break; 3180 break;
3178 } 3181 }
3179 3182
3180 if (NULL != s && s->accepting) 3183 if (NULL != s && s->accepting)
3181 return 0; 3184 return 0;
@@ -3192,7 +3195,7 @@ evaluate_dfa (struct REGEX_INTERNAL_Automaton *a, const char *string)
3192 * @return 0 if string matches, non-0 otherwise 3195 * @return 0 if string matches, non-0 otherwise
3193 */ 3196 */
3194static int 3197static int
3195evaluate_nfa (struct REGEX_INTERNAL_Automaton *a, const char *string) 3198evaluate_nfa(struct REGEX_INTERNAL_Automaton *a, const char *string)
3196{ 3199{
3197 const char *strp; 3200 const char *strp;
3198 char str[2]; 3201 char str[2];
@@ -3204,43 +3207,43 @@ evaluate_nfa (struct REGEX_INTERNAL_Automaton *a, const char *string)
3204 int result; 3207 int result;
3205 3208
3206 if (NFA != a->type) 3209 if (NFA != a->type)
3207 { 3210 {
3208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 3211 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
3209 "Tried to evaluate NFA, but DFA automaton given"); 3212 "Tried to evaluate NFA, but DFA automaton given");
3210 return -1; 3213 return -1;
3211 } 3214 }
3212 3215
3213 /* If the string is empty but the starting state is accepting, we accept. */ 3216 /* If the string is empty but the starting state is accepting, we accept. */
3214 if ((NULL == string || 0 == strlen (string)) && a->start->accepting) 3217 if ((NULL == string || 0 == strlen(string)) && a->start->accepting)
3215 return 0; 3218 return 0;
3216 3219
3217 result = 1; 3220 result = 1;
3218 memset (&singleton_set, 0, sizeof (struct REGEX_INTERNAL_StateSet)); 3221 memset(&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet));
3219 state_set_append (&singleton_set, a->start); 3222 state_set_append(&singleton_set, a->start);
3220 nfa_closure_set_create (&sset, a, &singleton_set, NULL); 3223 nfa_closure_set_create(&sset, a, &singleton_set, NULL);
3221 state_set_clear (&singleton_set); 3224 state_set_clear(&singleton_set);
3222 3225
3223 str[1] = '\0'; 3226 str[1] = '\0';
3224 for (strp = string; NULL != strp && *strp; strp++) 3227 for (strp = string; NULL != strp && *strp; strp++)
3225 { 3228 {
3226 str[0] = *strp; 3229 str[0] = *strp;
3227 nfa_closure_set_create (&new_sset, a, &sset, str); 3230 nfa_closure_set_create(&new_sset, a, &sset, str);
3228 state_set_clear (&sset); 3231 state_set_clear(&sset);
3229 nfa_closure_set_create (&sset, a, &new_sset, 0); 3232 nfa_closure_set_create(&sset, a, &new_sset, 0);
3230 state_set_clear (&new_sset); 3233 state_set_clear(&new_sset);
3231 } 3234 }
3232 3235
3233 for (i = 0; i < sset.off; i++) 3236 for (i = 0; i < sset.off; i++)
3234 {
3235 s = sset.states[i];
3236 if ((NULL != s) && (s->accepting))
3237 { 3237 {
3238 result = 0; 3238 s = sset.states[i];
3239 break; 3239 if ((NULL != s) && (s->accepting))
3240 {
3241 result = 0;
3242 break;
3243 }
3240 } 3244 }
3241 }
3242 3245
3243 state_set_clear (&sset); 3246 state_set_clear(&sset);
3244 return result; 3247 return result;
3245} 3248}
3246 3249
@@ -3253,24 +3256,26 @@ evaluate_nfa (struct REGEX_INTERNAL_Automaton *a, const char *string)
3253 * @return 0 if string matches, non-0 otherwise 3256 * @return 0 if string matches, non-0 otherwise
3254 */ 3257 */
3255int 3258int
3256REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a, const char *string) 3259REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a, const char *string)
3257{ 3260{
3258 int result; 3261 int result;
3259 3262
3260 switch (a->type) 3263 switch (a->type)
3261 { 3264 {
3262 case DFA: 3265 case DFA:
3263 result = evaluate_dfa (a, string); 3266 result = evaluate_dfa(a, string);
3264 break; 3267 break;
3265 case NFA: 3268
3266 result = evaluate_nfa (a, string); 3269 case NFA:
3267 break; 3270 result = evaluate_nfa(a, string);
3268 default: 3271 break;
3269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 3272
3270 "Evaluating regex failed, automaton has no type!\n"); 3273 default:
3271 result = GNUNET_SYSERR; 3274 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
3272 break; 3275 "Evaluating regex failed, automaton has no type!\n");
3273 } 3276 result = GNUNET_SYSERR;
3277 break;
3278 }
3274 3279
3275 return result; 3280 return result;
3276} 3281}
@@ -3288,7 +3293,7 @@ REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a, const char *string)
3288 * @return 3293 * @return
3289 */ 3294 */
3290const char * 3295const char *
3291REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a) 3296REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a)
3292{ 3297{
3293 if (NULL == a) 3298 if (NULL == a)
3294 return NULL; 3299 return NULL;
@@ -3305,7 +3310,7 @@ REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a)
3305 * @return number of transitions in the given automaton. 3310 * @return number of transitions in the given automaton.
3306 */ 3311 */
3307unsigned int 3312unsigned int
3308REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a) 3313REGEX_INTERNAL_get_transition_count(struct REGEX_INTERNAL_Automaton *a)
3309{ 3314{
3310 unsigned int t_count; 3315 unsigned int t_count;
3311 struct REGEX_INTERNAL_State *s; 3316 struct REGEX_INTERNAL_State *s;
@@ -3332,20 +3337,20 @@ REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a)
3332 * to construct the key 3337 * to construct the key
3333 */ 3338 */
3334size_t 3339size_t
3335REGEX_INTERNAL_get_first_key (const char *input_string, 3340REGEX_INTERNAL_get_first_key(const char *input_string,
3336 size_t string_len, 3341 size_t string_len,
3337 struct GNUNET_HashCode *key) 3342 struct GNUNET_HashCode *key)
3338{ 3343{
3339 size_t size; 3344 size_t size;
3340 3345
3341 size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len 3346 size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len
3342 : GNUNET_REGEX_INITIAL_BYTES; 3347 : GNUNET_REGEX_INITIAL_BYTES;
3343 if (NULL == input_string) 3348 if (NULL == input_string)
3344 { 3349 {
3345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n"); 3350 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n");
3346 return 0; 3351 return 0;
3347 } 3352 }
3348 GNUNET_CRYPTO_hash (input_string, size, key); 3353 GNUNET_CRYPTO_hash(input_string, size, key);
3349 3354
3350 return size; 3355 return size;
3351} 3356}
@@ -3362,12 +3367,12 @@ REGEX_INTERNAL_get_first_key (const char *input_string,
3362 * @param iterator_cls closure for the @a iterator function. 3367 * @param iterator_cls closure for the @a iterator function.
3363 */ 3368 */
3364static void 3369static void
3365iterate_initial_edge (unsigned int min_len, 3370iterate_initial_edge(unsigned int min_len,
3366 unsigned int max_len, 3371 unsigned int max_len,
3367 char *consumed_string, 3372 char *consumed_string,
3368 struct REGEX_INTERNAL_State *state, 3373 struct REGEX_INTERNAL_State *state,
3369 REGEX_INTERNAL_KeyIterator iterator, 3374 REGEX_INTERNAL_KeyIterator iterator,
3370 void *iterator_cls) 3375 void *iterator_cls)
3371{ 3376{
3372 char *temp; 3377 char *temp;
3373 struct REGEX_INTERNAL_Transition *t; 3378 struct REGEX_INTERNAL_Transition *t;
@@ -3379,91 +3384,91 @@ iterate_initial_edge (unsigned int min_len,
3379 unsigned int cur_len; 3384 unsigned int cur_len;
3380 3385
3381 if (NULL != consumed_string) 3386 if (NULL != consumed_string)
3382 cur_len = strlen (consumed_string); 3387 cur_len = strlen(consumed_string);
3383 else 3388 else
3384 cur_len = 0; 3389 cur_len = 0;
3385 3390
3386 if (((cur_len >= min_len) || (GNUNET_YES == state->accepting)) && 3391 if (((cur_len >= min_len) || (GNUNET_YES == state->accepting)) &&
3387 (cur_len > 0) && (NULL != consumed_string)) 3392 (cur_len > 0) && (NULL != consumed_string))
3388 {
3389 if (cur_len <= max_len)
3390 { 3393 {
3391 if ((NULL != state->proof) && 3394 if (cur_len <= max_len)
3392 (0 != strcmp (consumed_string, state->proof))) 3395 {
3393 { 3396 if ((NULL != state->proof) &&
3394 (void) state_get_edges (state, edges); 3397 (0 != strcmp(consumed_string, state->proof)))
3395 GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash); 3398 {
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3399 (void)state_get_edges(state, edges);
3397 "Start state for string `%s' is %s\n", 3400 GNUNET_CRYPTO_hash(consumed_string, strlen(consumed_string), &hash);
3398 consumed_string, 3401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3399 GNUNET_h2s (&hash)); 3402 "Start state for string `%s' is %s\n",
3400 iterator (iterator_cls, 3403 consumed_string,
3401 &hash, 3404 GNUNET_h2s(&hash));
3402 consumed_string, 3405 iterator(iterator_cls,
3403 state->accepting, 3406 &hash,
3404 num_edges, 3407 consumed_string,
3405 edges); 3408 state->accepting,
3406 } 3409 num_edges,
3410 edges);
3411 }
3407 3412
3408 if ((GNUNET_YES == state->accepting) && (cur_len > 1) && 3413 if ((GNUNET_YES == state->accepting) && (cur_len > 1) &&
3409 (state->transition_count < 1) && (cur_len < max_len)) 3414 (state->transition_count < 1) && (cur_len < max_len))
3410 { 3415 {
3411 /* Special case for regex consisting of just a string that is shorter than 3416 /* Special case for regex consisting of just a string that is shorter than
3412 * max_len */ 3417 * max_len */
3413 edge[0].label = &consumed_string[cur_len - 1]; 3418 edge[0].label = &consumed_string[cur_len - 1];
3414 edge[0].destination = state->hash; 3419 edge[0].destination = state->hash;
3415 temp = GNUNET_strdup (consumed_string); 3420 temp = GNUNET_strdup(consumed_string);
3416 temp[cur_len - 1] = '\0'; 3421 temp[cur_len - 1] = '\0';
3417 GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new); 3422 GNUNET_CRYPTO_hash(temp, cur_len - 1, &hash_new);
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3419 "Start state for short string `%s' is %s\n", 3424 "Start state for short string `%s' is %s\n",
3420 temp, 3425 temp,
3421 GNUNET_h2s (&hash_new)); 3426 GNUNET_h2s(&hash_new));
3422 iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge); 3427 iterator(iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge);
3423 GNUNET_free (temp); 3428 GNUNET_free(temp);
3424 } 3429 }
3425 } 3430 }
3426 else /* cur_len > max_len */ 3431 else /* cur_len > max_len */
3427 { 3432 {
3428 /* Case where the concatenated labels are longer than max_len, then split. */ 3433 /* Case where the concatenated labels are longer than max_len, then split. */
3429 edge[0].label = &consumed_string[max_len]; 3434 edge[0].label = &consumed_string[max_len];
3430 edge[0].destination = state->hash; 3435 edge[0].destination = state->hash;
3431 temp = GNUNET_strdup (consumed_string); 3436 temp = GNUNET_strdup(consumed_string);
3432 temp[max_len] = '\0'; 3437 temp[max_len] = '\0';
3433 GNUNET_CRYPTO_hash (temp, max_len, &hash); 3438 GNUNET_CRYPTO_hash(temp, max_len, &hash);
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3435 "Start state at split edge `%s'-`%s` is %s\n", 3440 "Start state at split edge `%s'-`%s` is %s\n",
3436 temp, 3441 temp,
3437 edge[0].label, 3442 edge[0].label,
3438 GNUNET_h2s (&hash_new)); 3443 GNUNET_h2s(&hash_new));
3439 iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge); 3444 iterator(iterator_cls, &hash, temp, GNUNET_NO, 1, edge);
3440 GNUNET_free (temp); 3445 GNUNET_free(temp);
3446 }
3441 } 3447 }
3442 }
3443 3448
3444 if (cur_len < max_len) 3449 if (cur_len < max_len)
3445 {
3446 for (t = state->transitions_head; NULL != t; t = t->next)
3447 { 3450 {
3448 if (NULL != strchr (t->label, (int) '.')) 3451 for (t = state->transitions_head; NULL != t; t = t->next)
3449 { 3452 {
3450 /* Wildcards not allowed during starting states */ 3453 if (NULL != strchr(t->label, (int)'.'))
3451 GNUNET_break (0); 3454 {
3452 continue; 3455 /* Wildcards not allowed during starting states */
3453 } 3456 GNUNET_break(0);
3454 if (NULL != consumed_string) 3457 continue;
3455 GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label); 3458 }
3456 else 3459 if (NULL != consumed_string)
3457 GNUNET_asprintf (&temp, "%s", t->label); 3460 GNUNET_asprintf(&temp, "%s%s", consumed_string, t->label);
3458 iterate_initial_edge (min_len, 3461 else
3459 max_len, 3462 GNUNET_asprintf(&temp, "%s", t->label);
3460 temp, 3463 iterate_initial_edge(min_len,
3461 t->to_state, 3464 max_len,
3462 iterator, 3465 temp,
3463 iterator_cls); 3466 t->to_state,
3464 GNUNET_free (temp); 3467 iterator,
3468 iterator_cls);
3469 GNUNET_free(temp);
3470 }
3465 } 3471 }
3466 }
3467} 3472}
3468 3473
3469 3474
@@ -3476,41 +3481,41 @@ iterate_initial_edge (unsigned int min_len,
3476 * @param iterator_cls closure. 3481 * @param iterator_cls closure.
3477 */ 3482 */
3478void 3483void
3479REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a, 3484REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a,
3480 REGEX_INTERNAL_KeyIterator iterator, 3485 REGEX_INTERNAL_KeyIterator iterator,
3481 void *iterator_cls) 3486 void *iterator_cls)
3482{ 3487{
3483 struct REGEX_INTERNAL_State *s; 3488 struct REGEX_INTERNAL_State *s;
3484 3489
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating over starting edges\n"); 3490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating over starting edges\n");
3486 iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, 3491 iterate_initial_edge(GNUNET_REGEX_INITIAL_BYTES,
3487 GNUNET_REGEX_INITIAL_BYTES, 3492 GNUNET_REGEX_INITIAL_BYTES,
3488 NULL, 3493 NULL,
3489 a->start, 3494 a->start,
3490 iterator, 3495 iterator,
3491 iterator_cls); 3496 iterator_cls);
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating over DFA edges\n"); 3497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating over DFA edges\n");
3493 for (s = a->states_head; NULL != s; s = s->next) 3498 for (s = a->states_head; NULL != s; s = s->next)
3494 { 3499 {
3495 struct REGEX_BLOCK_Edge edges[s->transition_count]; 3500 struct REGEX_BLOCK_Edge edges[s->transition_count];
3496 unsigned int num_edges; 3501 unsigned int num_edges;
3497 3502
3498 num_edges = state_get_edges (s, edges); 3503 num_edges = state_get_edges(s, edges);
3499 if (((NULL != s->proof) && (0 < strlen (s->proof))) || s->accepting) 3504 if (((NULL != s->proof) && (0 < strlen(s->proof))) || s->accepting)
3500 { 3505 {
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3506 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3502 "Creating DFA edges at `%s' under key %s\n", 3507 "Creating DFA edges at `%s' under key %s\n",
3503 s->proof, 3508 s->proof,
3504 GNUNET_h2s (&s->hash)); 3509 GNUNET_h2s(&s->hash));
3505 iterator (iterator_cls, 3510 iterator(iterator_cls,
3506 &s->hash, 3511 &s->hash,
3507 s->proof, 3512 s->proof,
3508 s->accepting, 3513 s->accepting,
3509 num_edges, 3514 num_edges,
3510 edges); 3515 edges);
3516 }
3517 s->marked = GNUNET_NO;
3511 } 3518 }
3512 s->marked = GNUNET_NO;
3513 }
3514} 3519}
3515 3520
3516 3521
@@ -3520,8 +3525,7 @@ REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
3520 * Contains the same info as the Regex Iterator parametes except the key, 3525 * Contains the same info as the Regex Iterator parametes except the key,
3521 * which comes directly from the HashMap iterator. 3526 * which comes directly from the HashMap iterator.
3522 */ 3527 */
3523struct temporal_state_store 3528struct temporal_state_store {
3524{
3525 int reachable; 3529 int reachable;
3526 char *proof; 3530 char *proof;
3527 int accepting; 3531 int accepting;
@@ -3533,8 +3537,7 @@ struct temporal_state_store
3533/** 3537/**
3534 * Store regex iterator and cls in one place to pass to the hashmap iterator. 3538 * Store regex iterator and cls in one place to pass to the hashmap iterator.
3535 */ 3539 */
3536struct client_iterator 3540struct client_iterator {
3537{
3538 REGEX_INTERNAL_KeyIterator iterator; 3541 REGEX_INTERNAL_KeyIterator iterator;
3539 void *iterator_cls; 3542 void *iterator_cls;
3540}; 3543};
@@ -3552,31 +3555,31 @@ struct client_iterator
3552 * @param edges edges leaving current state. 3555 * @param edges edges leaving current state.
3553 */ 3556 */
3554static void 3557static void
3555store_all_states (void *cls, 3558store_all_states(void *cls,
3556 const struct GNUNET_HashCode *key, 3559 const struct GNUNET_HashCode *key,
3557 const char *proof, 3560 const char *proof,
3558 int accepting, 3561 int accepting,
3559 unsigned int num_edges, 3562 unsigned int num_edges,
3560 const struct REGEX_BLOCK_Edge *edges) 3563 const struct REGEX_BLOCK_Edge *edges)
3561{ 3564{
3562 struct GNUNET_CONTAINER_MultiHashMap *hm = cls; 3565 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3563 struct temporal_state_store *tmp; 3566 struct temporal_state_store *tmp;
3564 size_t edges_size; 3567 size_t edges_size;
3565 3568
3566 tmp = GNUNET_new (struct temporal_state_store); 3569 tmp = GNUNET_new(struct temporal_state_store);
3567 tmp->reachable = GNUNET_NO; 3570 tmp->reachable = GNUNET_NO;
3568 tmp->proof = GNUNET_strdup (proof); 3571 tmp->proof = GNUNET_strdup(proof);
3569 tmp->accepting = accepting; 3572 tmp->accepting = accepting;
3570 tmp->num_edges = num_edges; 3573 tmp->num_edges = num_edges;
3571 edges_size = sizeof (struct REGEX_BLOCK_Edge) * num_edges; 3574 edges_size = sizeof(struct REGEX_BLOCK_Edge) * num_edges;
3572 tmp->edges = GNUNET_malloc (edges_size); 3575 tmp->edges = GNUNET_malloc(edges_size);
3573 GNUNET_memcpy (tmp->edges, edges, edges_size); 3576 GNUNET_memcpy(tmp->edges, edges, edges_size);
3574 GNUNET_assert (GNUNET_YES == 3577 GNUNET_assert(GNUNET_YES ==
3575 GNUNET_CONTAINER_multihashmap_put ( 3578 GNUNET_CONTAINER_multihashmap_put(
3576 hm, 3579 hm,
3577 key, 3580 key,
3578 tmp, 3581 tmp,
3579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 3582 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3580} 3583}
3581 3584
3582 3585
@@ -3589,8 +3592,8 @@ store_all_states (void *cls,
3589 * @param hm HashMap which stores all the states indexed by key. 3592 * @param hm HashMap which stores all the states indexed by key.
3590 */ 3593 */
3591static void 3594static void
3592mark_as_reachable (struct temporal_state_store *state, 3595mark_as_reachable(struct temporal_state_store *state,
3593 struct GNUNET_CONTAINER_MultiHashMap *hm) 3596 struct GNUNET_CONTAINER_MultiHashMap *hm)
3594{ 3597{
3595 struct temporal_state_store *child; 3598 struct temporal_state_store *child;
3596 unsigned int i; 3599 unsigned int i;
@@ -3601,16 +3604,16 @@ mark_as_reachable (struct temporal_state_store *state,
3601 3604
3602 state->reachable = GNUNET_YES; 3605 state->reachable = GNUNET_YES;
3603 for (i = 0; i < state->num_edges; i++) 3606 for (i = 0; i < state->num_edges; i++)
3604 {
3605 child =
3606 GNUNET_CONTAINER_multihashmap_get (hm, &state->edges[i].destination);
3607 if (NULL == child)
3608 { 3607 {
3609 GNUNET_break (0); 3608 child =
3610 continue; 3609 GNUNET_CONTAINER_multihashmap_get(hm, &state->edges[i].destination);
3610 if (NULL == child)
3611 {
3612 GNUNET_break(0);
3613 continue;
3614 }
3615 mark_as_reachable(child, hm);
3611 } 3616 }
3612 mark_as_reachable (child, hm);
3613 }
3614} 3617}
3615 3618
3616 3619
@@ -3624,9 +3627,9 @@ mark_as_reachable (struct temporal_state_store *state,
3624 * #GNUNET_NO if not. 3627 * #GNUNET_NO if not.
3625 */ 3628 */
3626static int 3629static int
3627reachability_iterator (void *cls, 3630reachability_iterator(void *cls,
3628 const struct GNUNET_HashCode *key, 3631 const struct GNUNET_HashCode *key,
3629 void *value) 3632 void *value)
3630{ 3633{
3631 struct GNUNET_CONTAINER_MultiHashMap *hm = cls; 3634 struct GNUNET_CONTAINER_MultiHashMap *hm = cls;
3632 struct temporal_state_store *state = value; 3635 struct temporal_state_store *state = value;
@@ -3635,12 +3638,12 @@ reachability_iterator (void *cls,
3635 /* already visited and marked */ 3638 /* already visited and marked */
3636 return GNUNET_YES; 3639 return GNUNET_YES;
3637 3640
3638 if (GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof) && 3641 if (GNUNET_REGEX_INITIAL_BYTES > strlen(state->proof) &&
3639 GNUNET_NO == state->accepting) 3642 GNUNET_NO == state->accepting)
3640 /* not directly reachable */ 3643 /* not directly reachable */
3641 return GNUNET_YES; 3644 return GNUNET_YES;
3642 3645
3643 mark_as_reachable (state, hm); 3646 mark_as_reachable(state, hm);
3644 return GNUNET_YES; 3647 return GNUNET_YES;
3645} 3648}
3646 3649
@@ -3656,23 +3659,23 @@ reachability_iterator (void *cls,
3656 * #GNUNET_NO if not. 3659 * #GNUNET_NO if not.
3657 */ 3660 */
3658static int 3661static int
3659iterate_reachables (void *cls, const struct GNUNET_HashCode *key, void *value) 3662iterate_reachables(void *cls, const struct GNUNET_HashCode *key, void *value)
3660{ 3663{
3661 struct client_iterator *ci = cls; 3664 struct client_iterator *ci = cls;
3662 struct temporal_state_store *state = value; 3665 struct temporal_state_store *state = value;
3663 3666
3664 if (GNUNET_YES == state->reachable) 3667 if (GNUNET_YES == state->reachable)
3665 { 3668 {
3666 ci->iterator (ci->iterator_cls, 3669 ci->iterator(ci->iterator_cls,
3667 key, 3670 key,
3668 state->proof, 3671 state->proof,
3669 state->accepting, 3672 state->accepting,
3670 state->num_edges, 3673 state->num_edges,
3671 state->edges); 3674 state->edges);
3672 } 3675 }
3673 GNUNET_free (state->edges); 3676 GNUNET_free(state->edges);
3674 GNUNET_free (state->proof); 3677 GNUNET_free(state->proof);
3675 GNUNET_free (state); 3678 GNUNET_free(state);
3676 return GNUNET_YES; 3679 return GNUNET_YES;
3677} 3680}
3678 3681
@@ -3687,22 +3690,22 @@ iterate_reachables (void *cls, const struct GNUNET_HashCode *key, void *value)
3687 * @param iterator_cls closure. 3690 * @param iterator_cls closure.
3688 */ 3691 */
3689void 3692void
3690REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a, 3693REGEX_INTERNAL_iterate_reachable_edges(struct REGEX_INTERNAL_Automaton *a,
3691 REGEX_INTERNAL_KeyIterator iterator, 3694 REGEX_INTERNAL_KeyIterator iterator,
3692 void *iterator_cls) 3695 void *iterator_cls)
3693{ 3696{
3694 struct GNUNET_CONTAINER_MultiHashMap *hm; 3697 struct GNUNET_CONTAINER_MultiHashMap *hm;
3695 struct client_iterator ci; 3698 struct client_iterator ci;
3696 3699
3697 hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO); 3700 hm = GNUNET_CONTAINER_multihashmap_create(a->state_count * 2, GNUNET_NO);
3698 ci.iterator = iterator; 3701 ci.iterator = iterator;
3699 ci.iterator_cls = iterator_cls; 3702 ci.iterator_cls = iterator_cls;
3700 3703
3701 REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm); 3704 REGEX_INTERNAL_iterate_all_edges(a, &store_all_states, hm);
3702 GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm); 3705 GNUNET_CONTAINER_multihashmap_iterate(hm, &reachability_iterator, hm);
3703 GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci); 3706 GNUNET_CONTAINER_multihashmap_iterate(hm, &iterate_reachables, &ci);
3704 3707
3705 GNUNET_CONTAINER_multihashmap_destroy (hm); 3708 GNUNET_CONTAINER_multihashmap_destroy(hm);
3706} 3709}
3707 3710
3708 3711
diff --git a/src/regex/regex_internal.h b/src/regex/regex_internal.h
index 43686b5e2..ab8b6f22d 100644
--- a/src/regex/regex_internal.h
+++ b/src/regex/regex_internal.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_internal.h 21 * @file src/regex/regex_internal.h
22 * @brief common internal definitions for regex library. 22 * @brief common internal definitions for regex library.
@@ -47,8 +47,7 @@ extern "C"
47 * which they origin ('from_state'). Each state can have 0-n transitions. 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. 48 * If label is NULL, this is considered to be an epsilon transition.
49 */ 49 */
50struct REGEX_INTERNAL_Transition 50struct REGEX_INTERNAL_Transition {
51{
52 /** 51 /**
53 * This is a linked list. 52 * This is a linked list.
54 */ 53 */
@@ -90,8 +89,7 @@ struct REGEX_INTERNAL_State;
90/** 89/**
91 * Set of states. 90 * Set of states.
92 */ 91 */
93struct REGEX_INTERNAL_StateSet 92struct REGEX_INTERNAL_StateSet {
94{
95 /** 93 /**
96 * Array of states. 94 * Array of states.
97 */ 95 */
@@ -112,8 +110,7 @@ struct REGEX_INTERNAL_StateSet
112/** 110/**
113 * A state. Can be used in DFA and NFA automatons. 111 * A state. Can be used in DFA and NFA automatons.
114 */ 112 */
115struct REGEX_INTERNAL_State 113struct REGEX_INTERNAL_State {
116{
117 /** 114 /**
118 * This is a linked list to keep states in an automaton. 115 * This is a linked list to keep states in an automaton.
119 */ 116 */
@@ -245,8 +242,7 @@ struct REGEX_INTERNAL_State
245/** 242/**
246 * Type of an automaton. 243 * Type of an automaton.
247 */ 244 */
248enum REGEX_INTERNAL_AutomatonType 245enum REGEX_INTERNAL_AutomatonType {
249{
250 NFA, 246 NFA,
251 DFA 247 DFA
252}; 248};
@@ -255,8 +251,7 @@ enum REGEX_INTERNAL_AutomatonType
255/** 251/**
256 * Automaton representation. 252 * Automaton representation.
257 */ 253 */
258struct REGEX_INTERNAL_Automaton 254struct REGEX_INTERNAL_Automaton {
259{
260 /** 255 /**
261 * Linked list of NFAs used for partial NFA creation. 256 * Linked list of NFAs used for partial NFA creation.
262 */ 257 */
@@ -324,7 +319,7 @@ struct REGEX_INTERNAL_Automaton
324 * @return NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy. 319 * @return NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
325 */ 320 */
326struct REGEX_INTERNAL_Automaton * 321struct REGEX_INTERNAL_Automaton *
327REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len); 322REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len);
328 323
329 324
330/** 325/**
@@ -340,8 +335,8 @@ REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len);
340 * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop. 335 * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop.
341 */ 336 */
342typedef int (*REGEX_INTERNAL_traverse_check) (void *cls, 337typedef int (*REGEX_INTERNAL_traverse_check) (void *cls,
343 struct REGEX_INTERNAL_State * s, 338 struct REGEX_INTERNAL_State * s,
344 struct REGEX_INTERNAL_Transition * t); 339 struct REGEX_INTERNAL_Transition * t);
345 340
346 341
347/** 342/**
@@ -352,8 +347,8 @@ typedef int (*REGEX_INTERNAL_traverse_check) (void *cls,
352 * @param s state. 347 * @param s state.
353 */ 348 */
354typedef void (*REGEX_INTERNAL_traverse_action) (void *cls, 349typedef void (*REGEX_INTERNAL_traverse_action) (void *cls,
355 const unsigned int count, 350 const unsigned int count,
356 struct REGEX_INTERNAL_State * s); 351 struct REGEX_INTERNAL_State * s);
357 352
358 353
359/** 354/**
@@ -370,12 +365,12 @@ typedef void (*REGEX_INTERNAL_traverse_action) (void *cls,
370 * @param action_cls closure for action 365 * @param action_cls closure for action
371 */ 366 */
372void 367void
373REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a, 368REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a,
374 struct REGEX_INTERNAL_State *start, 369 struct REGEX_INTERNAL_State *start,
375 REGEX_INTERNAL_traverse_check check, 370 REGEX_INTERNAL_traverse_check check,
376 void *check_cls, 371 void *check_cls,
377 REGEX_INTERNAL_traverse_action action, 372 REGEX_INTERNAL_traverse_action action,
378 void *action_cls); 373 void *action_cls);
379 374
380/** 375/**
381 * Get the canonical regex of the given automaton. 376 * Get the canonical regex of the given automaton.
@@ -389,7 +384,7 @@ REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a,
389 * @return canonical regex string. 384 * @return canonical regex string.
390 */ 385 */
391const char * 386const char *
392REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a); 387REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a);
393 388
394 389
395/** 390/**
@@ -400,15 +395,14 @@ REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a);
400 * @return number of transitions in the given automaton. 395 * @return number of transitions in the given automaton.
401 */ 396 */
402unsigned int 397unsigned int
403REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a); 398REGEX_INTERNAL_get_transition_count(struct REGEX_INTERNAL_Automaton *a);
404 399
405 400
406/** 401/**
407 * Context that contains an id counter for states and transitions as well as a 402 * 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. 403 * DLL of automatons used as a stack for NFA construction.
409 */ 404 */
410struct REGEX_INTERNAL_Context 405struct REGEX_INTERNAL_Context {
411{
412 /** 406 /**
413 * Unique state id. 407 * Unique state id.
414 */ 408 */
@@ -439,9 +433,9 @@ struct REGEX_INTERNAL_Context
439 * @param stride_len length of the strides. 433 * @param stride_len length of the strides.
440 */ 434 */
441void 435void
442REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx, 436REGEX_INTERNAL_dfa_add_multi_strides(struct REGEX_INTERNAL_Context *regex_ctx,
443 struct REGEX_INTERNAL_Automaton *dfa, 437 struct REGEX_INTERNAL_Automaton *dfa,
444 const unsigned int stride_len); 438 const unsigned int stride_len);
445 439
446 440
447 441
diff --git a/src/regex/regex_internal_dht.c b/src/regex/regex_internal_dht.c
index 7cfaea894..611fb0a23 100644
--- a/src/regex/regex_internal_dht.c
+++ b/src/regex/regex_internal_dht.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_internal_dht.c 21 * @file src/regex/regex_internal_dht.c
22 * @brief library to announce regexes in the network and match strings 22 * @brief library to announce regexes in the network and match strings
@@ -32,7 +32,7 @@
32#include "gnunet_signatures.h" 32#include "gnunet_signatures.h"
33 33
34 34
35#define LOG(kind,...) GNUNET_log_from (kind,"regex-dht",__VA_ARGS__) 35#define LOG(kind, ...) GNUNET_log_from(kind, "regex-dht", __VA_ARGS__)
36 36
37/** 37/**
38 * DHT replication level to use. 38 * DHT replication level to use.
@@ -53,8 +53,7 @@
53/** 53/**
54 * Handle to store cached data about a regex announce. 54 * Handle to store cached data about a regex announce.
55 */ 55 */
56struct REGEX_INTERNAL_Announcement 56struct REGEX_INTERNAL_Announcement {
57{
58 /** 57 /**
59 * DHT handle to use, must be initialized externally. 58 * DHT handle to use, must be initialized externally.
60 */ 59 */
@@ -93,92 +92,92 @@ struct REGEX_INTERNAL_Announcement
93 * @param edges edges leaving current state. 92 * @param edges edges leaving current state.
94 */ 93 */
95static void 94static void
96regex_iterator (void *cls, 95regex_iterator(void *cls,
97 const struct GNUNET_HashCode *key, 96 const struct GNUNET_HashCode *key,
98 const char *proof, 97 const char *proof,
99 int accepting, 98 int accepting,
100 unsigned int num_edges, 99 unsigned int num_edges,
101 const struct REGEX_BLOCK_Edge *edges) 100 const struct REGEX_BLOCK_Edge *edges)
102{ 101{
103 struct REGEX_INTERNAL_Announcement *h = cls; 102 struct REGEX_INTERNAL_Announcement *h = cls;
104 struct RegexBlock *block; 103 struct RegexBlock *block;
105 size_t size; 104 size_t size;
106 unsigned int i; 105 unsigned int i;
107 106
108 LOG (GNUNET_ERROR_TYPE_INFO, 107 LOG(GNUNET_ERROR_TYPE_INFO,
109 "DHT PUT for state %s with proof `%s' and %u edges:\n", 108 "DHT PUT for state %s with proof `%s' and %u edges:\n",
110 GNUNET_h2s (key), 109 GNUNET_h2s(key),
111 proof, 110 proof,
112 num_edges); 111 num_edges);
113 for (i = 0; i < num_edges; i++) 112 for (i = 0; i < num_edges; i++)
114 { 113 {
115 LOG (GNUNET_ERROR_TYPE_INFO, 114 LOG(GNUNET_ERROR_TYPE_INFO,
116 "Edge %u `%s' towards %s\n", 115 "Edge %u `%s' towards %s\n",
117 i, 116 i,
118 edges[i].label, 117 edges[i].label,
119 GNUNET_h2s (&edges[i].destination)); 118 GNUNET_h2s(&edges[i].destination));
120 } 119 }
121 if (GNUNET_YES == accepting) 120 if (GNUNET_YES == accepting)
122 { 121 {
123 struct RegexAcceptBlock ab; 122 struct RegexAcceptBlock ab;
124 123
125 LOG (GNUNET_ERROR_TYPE_INFO, 124 LOG(GNUNET_ERROR_TYPE_INFO,
126 "State %s is accepting, putting own id\n", 125 "State %s is accepting, putting own id\n",
127 GNUNET_h2s (key)); 126 GNUNET_h2s(key));
128 size = sizeof (struct RegexAcceptBlock); 127 size = sizeof(struct RegexAcceptBlock);
129 ab.purpose.size = ntohl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 128 ab.purpose.size = ntohl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
130 sizeof (struct GNUNET_TIME_AbsoluteNBO) + 129 sizeof(struct GNUNET_TIME_AbsoluteNBO) +
131 sizeof (struct GNUNET_HashCode)); 130 sizeof(struct GNUNET_HashCode));
132 ab.purpose.purpose = ntohl (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT); 131 ab.purpose.purpose = ntohl(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT);
133 ab.expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_DHT_MAX_EXPIRATION)); 132 ab.expiration_time = GNUNET_TIME_absolute_hton(GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_DHT_MAX_EXPIRATION));
134 ab.key = *key; 133 ab.key = *key;
135 GNUNET_CRYPTO_eddsa_key_get_public (h->priv, 134 GNUNET_CRYPTO_eddsa_key_get_public(h->priv,
136 &ab.peer.public_key); 135 &ab.peer.public_key);
137 GNUNET_assert (GNUNET_OK == 136 GNUNET_assert(GNUNET_OK ==
138 GNUNET_CRYPTO_eddsa_sign (h->priv, 137 GNUNET_CRYPTO_eddsa_sign(h->priv,
139 &ab.purpose, 138 &ab.purpose,
140 &ab.signature)); 139 &ab.signature));
141 140
142 GNUNET_STATISTICS_update (h->stats, "# regex accepting blocks stored", 141 GNUNET_STATISTICS_update(h->stats, "# regex accepting blocks stored",
143 1, GNUNET_NO); 142 1, GNUNET_NO);
144 GNUNET_STATISTICS_update (h->stats, "# regex accepting block bytes stored", 143 GNUNET_STATISTICS_update(h->stats, "# regex accepting block bytes stored",
145 sizeof (struct RegexAcceptBlock), GNUNET_NO); 144 sizeof(struct RegexAcceptBlock), GNUNET_NO);
146 (void) 145 (void)
147 GNUNET_DHT_put (h->dht, key, 146 GNUNET_DHT_put(h->dht, key,
148 DHT_REPLICATION, 147 DHT_REPLICATION,
149 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, 148 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE,
150 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, 149 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
151 size, 150 size,
152 &ab, 151 &ab,
153 GNUNET_TIME_relative_to_absolute (DHT_TTL), 152 GNUNET_TIME_relative_to_absolute(DHT_TTL),
154 NULL, NULL); 153 NULL, NULL);
155 } 154 }
156 block = REGEX_BLOCK_create (proof, 155 block = REGEX_BLOCK_create(proof,
157 num_edges, 156 num_edges,
158 edges, 157 edges,
159 accepting, 158 accepting,
160 &size); 159 &size);
161 if (NULL == block) 160 if (NULL == block)
162 return; 161 return;
163 (void) GNUNET_DHT_put (h->dht, 162 (void)GNUNET_DHT_put(h->dht,
164 key, 163 key,
165 DHT_REPLICATION, 164 DHT_REPLICATION,
166 DHT_OPT, 165 DHT_OPT,
167 GNUNET_BLOCK_TYPE_REGEX, 166 GNUNET_BLOCK_TYPE_REGEX,
168 size, 167 size,
169 block, 168 block,
170 GNUNET_TIME_relative_to_absolute (DHT_TTL), 169 GNUNET_TIME_relative_to_absolute(DHT_TTL),
171 NULL, 170 NULL,
172 NULL); 171 NULL);
173 GNUNET_STATISTICS_update (h->stats, 172 GNUNET_STATISTICS_update(h->stats,
174 "# regex blocks stored", 173 "# regex blocks stored",
175 1, 174 1,
176 GNUNET_NO); 175 GNUNET_NO);
177 GNUNET_STATISTICS_update (h->stats, 176 GNUNET_STATISTICS_update(h->stats,
178 "# regex block bytes stored", 177 "# regex block bytes stored",
179 size, 178 size,
180 GNUNET_NO); 179 GNUNET_NO);
181 GNUNET_free (block); 180 GNUNET_free(block);
182} 181}
183 182
184 183
@@ -195,22 +194,22 @@ regex_iterator (void *cls,
195 * Must be freed by calling #REGEX_INTERNAL_announce_cancel(). 194 * Must be freed by calling #REGEX_INTERNAL_announce_cancel().
196 */ 195 */
197struct REGEX_INTERNAL_Announcement * 196struct REGEX_INTERNAL_Announcement *
198REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht, 197REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht,
199 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, 198 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
200 const char *regex, 199 const char *regex,
201 uint16_t compression, 200 uint16_t compression,
202 struct GNUNET_STATISTICS_Handle *stats) 201 struct GNUNET_STATISTICS_Handle *stats)
203{ 202{
204 struct REGEX_INTERNAL_Announcement *h; 203 struct REGEX_INTERNAL_Announcement *h;
205 204
206 GNUNET_assert (NULL != dht); 205 GNUNET_assert(NULL != dht);
207 h = GNUNET_new (struct REGEX_INTERNAL_Announcement); 206 h = GNUNET_new(struct REGEX_INTERNAL_Announcement);
208 h->regex = regex; 207 h->regex = regex;
209 h->dht = dht; 208 h->dht = dht;
210 h->stats = stats; 209 h->stats = stats;
211 h->priv = priv; 210 h->priv = priv;
212 h->dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), compression); 211 h->dfa = REGEX_INTERNAL_construct_dfa(regex, strlen(regex), compression);
213 REGEX_INTERNAL_reannounce (h); 212 REGEX_INTERNAL_reannounce(h);
214 return h; 213 return h;
215} 214}
216 215
@@ -222,15 +221,15 @@ REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
222 * @param h Handle returned by a previous #REGEX_INTERNAL_announce call(). 221 * @param h Handle returned by a previous #REGEX_INTERNAL_announce call().
223 */ 222 */
224void 223void
225REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h) 224REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h)
226{ 225{
227 GNUNET_assert (NULL != h->dfa); /* make sure to call announce first */ 226 GNUNET_assert(NULL != h->dfa); /* make sure to call announce first */
228 LOG (GNUNET_ERROR_TYPE_INFO, 227 LOG(GNUNET_ERROR_TYPE_INFO,
229 "REGEX_INTERNAL_reannounce: %s\n", 228 "REGEX_INTERNAL_reannounce: %s\n",
230 h->regex); 229 h->regex);
231 REGEX_INTERNAL_iterate_reachable_edges (h->dfa, 230 REGEX_INTERNAL_iterate_reachable_edges(h->dfa,
232 &regex_iterator, 231 &regex_iterator,
233 h); 232 h);
234} 233}
235 234
236 235
@@ -241,10 +240,10 @@ REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h)
241 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. 240 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
242 */ 241 */
243void 242void
244REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h) 243REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h)
245{ 244{
246 REGEX_INTERNAL_automaton_destroy (h->dfa); 245 REGEX_INTERNAL_automaton_destroy(h->dfa);
247 GNUNET_free (h); 246 GNUNET_free(h);
248} 247}
249 248
250 249
@@ -255,8 +254,7 @@ REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h)
255 * Struct to keep state of running searches that have consumed a part of 254 * Struct to keep state of running searches that have consumed a part of
256 * the inital string. 255 * the inital string.
257 */ 256 */
258struct RegexSearchContext 257struct RegexSearchContext {
259{
260 /** 258 /**
261 * Part of the description already consumed by 259 * Part of the description already consumed by
262 * this particular search branch. 260 * this particular search branch.
@@ -284,8 +282,7 @@ struct RegexSearchContext
284/** 282/**
285 * Type of values in `dht_get_results`. 283 * Type of values in `dht_get_results`.
286 */ 284 */
287struct Result 285struct Result {
288{
289 /** 286 /**
290 * Number of bytes in data. 287 * Number of bytes in data.
291 */ 288 */
@@ -302,8 +299,7 @@ struct Result
302 * Struct to keep information of searches of services described by a regex 299 * Struct to keep information of searches of services described by a regex
303 * using a user-provided string service description. 300 * using a user-provided string service description.
304 */ 301 */
305struct REGEX_INTERNAL_Search 302struct REGEX_INTERNAL_Search {
306{
307 /** 303 /**
308 * DHT handle to use, must be initialized externally. 304 * DHT handle to use, must be initialized externally.
309 */ 305 */
@@ -360,9 +356,9 @@ struct REGEX_INTERNAL_Search
360 * @param ctx Context of the search. 356 * @param ctx Context of the search.
361 */ 357 */
362static void 358static void
363regex_next_edge (const struct RegexBlock *block, 359regex_next_edge(const struct RegexBlock *block,
364 size_t size, 360 size_t size,
365 struct RegexSearchContext *ctx); 361 struct RegexSearchContext *ctx);
366 362
367 363
368/** 364/**
@@ -381,33 +377,33 @@ regex_next_edge (const struct RegexBlock *block,
381 * @param data Pointer to the result data. 377 * @param data Pointer to the result data.
382 */ 378 */
383static void 379static void
384dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp, 380dht_get_string_accept_handler(void *cls, struct GNUNET_TIME_Absolute exp,
385 const struct GNUNET_HashCode *key, 381 const struct GNUNET_HashCode *key,
386 const struct GNUNET_PeerIdentity *get_path, 382 const struct GNUNET_PeerIdentity *get_path,
387 unsigned int get_path_length, 383 unsigned int get_path_length,
388 const struct GNUNET_PeerIdentity *put_path, 384 const struct GNUNET_PeerIdentity *put_path,
389 unsigned int put_path_length, 385 unsigned int put_path_length,
390 enum GNUNET_BLOCK_Type type, 386 enum GNUNET_BLOCK_Type type,
391 size_t size, const void *data) 387 size_t size, const void *data)
392{ 388{
393 const struct RegexAcceptBlock *block = data; 389 const struct RegexAcceptBlock *block = data;
394 struct RegexSearchContext *ctx = cls; 390 struct RegexSearchContext *ctx = cls;
395 struct REGEX_INTERNAL_Search *info = ctx->info; 391 struct REGEX_INTERNAL_Search *info = ctx->info;
396 392
397 LOG (GNUNET_ERROR_TYPE_DEBUG, 393 LOG(GNUNET_ERROR_TYPE_DEBUG,
398 "Regex result accept for %s (key %s)\n", 394 "Regex result accept for %s (key %s)\n",
399 info->description, GNUNET_h2s(key)); 395 info->description, GNUNET_h2s(key));
400 396
401 GNUNET_STATISTICS_update (info->stats, 397 GNUNET_STATISTICS_update(info->stats,
402 "# regex accepting blocks found", 398 "# regex accepting blocks found",
403 1, GNUNET_NO); 399 1, GNUNET_NO);
404 GNUNET_STATISTICS_update (info->stats, 400 GNUNET_STATISTICS_update(info->stats,
405 "# regex accepting block bytes found", 401 "# regex accepting block bytes found",
406 size, GNUNET_NO); 402 size, GNUNET_NO);
407 info->callback (info->callback_cls, 403 info->callback(info->callback_cls,
408 &block->peer, 404 &block->peer,
409 get_path, get_path_length, 405 get_path, get_path_length,
410 put_path, put_path_length); 406 put_path, put_path_length);
411} 407}
412 408
413 409
@@ -419,28 +415,28 @@ dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
419 * @param ctx Context containing info about the string, tunnel, etc. 415 * @param ctx Context containing info about the string, tunnel, etc.
420 */ 416 */
421static void 417static void
422regex_find_path (const struct GNUNET_HashCode *key, 418regex_find_path(const struct GNUNET_HashCode *key,
423 struct RegexSearchContext *ctx) 419 struct RegexSearchContext *ctx)
424{ 420{
425 struct GNUNET_DHT_GetHandle *get_h; 421 struct GNUNET_DHT_GetHandle *get_h;
426 422
427 LOG (GNUNET_ERROR_TYPE_DEBUG, 423 LOG(GNUNET_ERROR_TYPE_DEBUG,
428 "Accept state found, now searching for paths to %s\n", 424 "Accept state found, now searching for paths to %s\n",
429 GNUNET_h2s (key), 425 GNUNET_h2s(key),
430 (unsigned int) ctx->position); 426 (unsigned int)ctx->position);
431 get_h = GNUNET_DHT_get_start (ctx->info->dht, /* handle */ 427 get_h = GNUNET_DHT_get_start(ctx->info->dht, /* handle */
432 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, /* type */ 428 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, /* type */
433 key, /* key to search */ 429 key, /* key to search */
434 DHT_REPLICATION, /* replication level */ 430 DHT_REPLICATION, /* replication level */
435 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, 431 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE,
436 NULL, /* xquery */ // FIXME BLOOMFILTER 432 NULL, /* xquery */ // FIXME BLOOMFILTER
437 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE 433 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
438 &dht_get_string_accept_handler, ctx); 434 &dht_get_string_accept_handler, ctx);
439 GNUNET_break (GNUNET_OK == 435 GNUNET_break(GNUNET_OK ==
440 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles, 436 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
441 key, 437 key,
442 get_h, 438 get_h,
443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
444} 440}
445 441
446 442
@@ -462,14 +458,14 @@ regex_find_path (const struct GNUNET_HashCode *key,
462 * TODO: re-issue the request after certain time? cancel after X results? 458 * TODO: re-issue the request after certain time? cancel after X results?
463 */ 459 */
464static void 460static void
465dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp, 461dht_get_string_handler(void *cls, struct GNUNET_TIME_Absolute exp,
466 const struct GNUNET_HashCode *key, 462 const struct GNUNET_HashCode *key,
467 const struct GNUNET_PeerIdentity *get_path, 463 const struct GNUNET_PeerIdentity *get_path,
468 unsigned int get_path_length, 464 unsigned int get_path_length,
469 const struct GNUNET_PeerIdentity *put_path, 465 const struct GNUNET_PeerIdentity *put_path,
470 unsigned int put_path_length, 466 unsigned int put_path_length,
471 enum GNUNET_BLOCK_Type type, 467 enum GNUNET_BLOCK_Type type,
472 size_t size, const void *data) 468 size_t size, const void *data)
473{ 469{
474 const struct RegexBlock *block = data; 470 const struct RegexBlock *block = data;
475 struct RegexSearchContext *ctx = cls; 471 struct RegexSearchContext *ctx = cls;
@@ -477,32 +473,32 @@ dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
477 size_t len; 473 size_t len;
478 struct Result *copy; 474 struct Result *copy;
479 475
480 LOG (GNUNET_ERROR_TYPE_INFO, 476 LOG(GNUNET_ERROR_TYPE_INFO,
481 "DHT GET result for %s (%s)\n", 477 "DHT GET result for %s (%s)\n",
482 GNUNET_h2s (key), ctx->info->description); 478 GNUNET_h2s(key), ctx->info->description);
483 copy = GNUNET_malloc (sizeof (struct Result) + size); 479 copy = GNUNET_malloc(sizeof(struct Result) + size);
484 copy->size = size; 480 copy->size = size;
485 copy->data = &copy[1]; 481 copy->data = &copy[1];
486 GNUNET_memcpy (&copy[1], block, size); 482 GNUNET_memcpy(&copy[1], block, size);
487 GNUNET_break (GNUNET_OK == 483 GNUNET_break(GNUNET_OK ==
488 GNUNET_CONTAINER_multihashmap_put (info->dht_get_results, 484 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results,
489 key, copy, 485 key, copy,
490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); 486 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
491 len = strlen (info->description); 487 len = strlen(info->description);
492 if (len == ctx->position) // String processed 488 if (len == ctx->position) // String processed
493 {
494 if (GNUNET_YES == GNUNET_BLOCK_is_accepting (block, size))
495 { 489 {
496 regex_find_path (key, ctx); 490 if (GNUNET_YES == GNUNET_BLOCK_is_accepting(block, size))
491 {
492 regex_find_path(key, ctx);
493 }
494 else
495 {
496 LOG(GNUNET_ERROR_TYPE_INFO, "block not accepting!\n");
497 /* FIXME REGEX this block not successful, wait for more? start timeout? */
498 }
499 return;
497 } 500 }
498 else 501 regex_next_edge(block, size, ctx);
499 {
500 LOG (GNUNET_ERROR_TYPE_INFO, "block not accepting!\n");
501 /* FIXME REGEX this block not successful, wait for more? start timeout? */
502 }
503 return;
504 }
505 regex_next_edge (block, size, ctx);
506} 502}
507 503
508 504
@@ -515,32 +511,32 @@ dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
515 * @return #GNUNET_YES: we should always continue to iterate. 511 * @return #GNUNET_YES: we should always continue to iterate.
516 */ 512 */
517static int 513static int
518regex_result_iterator (void *cls, 514regex_result_iterator(void *cls,
519 const struct GNUNET_HashCode * key, 515 const struct GNUNET_HashCode * key,
520 void *value) 516 void *value)
521{ 517{
522 struct Result *result = value; 518 struct Result *result = value;
523 const struct RegexBlock *block = result->data; 519 const struct RegexBlock *block = result->data;
524 struct RegexSearchContext *ctx = cls; 520 struct RegexSearchContext *ctx = cls;
525 521
526 if ( (GNUNET_YES == 522 if ((GNUNET_YES ==
527 GNUNET_BLOCK_is_accepting (block, result->size)) && 523 GNUNET_BLOCK_is_accepting(block, result->size)) &&
528 (ctx->position == strlen (ctx->info->description)) ) 524 (ctx->position == strlen(ctx->info->description)))
529 { 525 {
530 LOG (GNUNET_ERROR_TYPE_INFO, 526 LOG(GNUNET_ERROR_TYPE_INFO,
531 "Found accepting known block\n"); 527 "Found accepting known block\n");
532 regex_find_path (key, ctx); 528 regex_find_path(key, ctx);
533 return GNUNET_YES; // We found an accept state! 529 return GNUNET_YES; // We found an accept state!
534 } 530 }
535 LOG (GNUNET_ERROR_TYPE_DEBUG, 531 LOG(GNUNET_ERROR_TYPE_DEBUG,
536 "* %u, %u, [%u]\n", 532 "* %u, %u, [%u]\n",
537 ctx->position, 533 ctx->position,
538 strlen (ctx->info->description), 534 strlen(ctx->info->description),
539 GNUNET_BLOCK_is_accepting (block, result->size)); 535 GNUNET_BLOCK_is_accepting(block, result->size));
540 regex_next_edge (block, result->size, ctx); 536 regex_next_edge(block, result->size, ctx);
541 537
542 GNUNET_STATISTICS_update (ctx->info->stats, "# regex cadet blocks iterated", 538 GNUNET_STATISTICS_update(ctx->info->stats, "# regex cadet blocks iterated",
543 1, GNUNET_NO); 539 1, GNUNET_NO);
544 540
545 return GNUNET_YES; 541 return GNUNET_YES;
546} 542}
@@ -556,43 +552,43 @@ regex_result_iterator (void *cls,
556 * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise. 552 * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise.
557 */ 553 */
558static int 554static int
559regex_edge_iterator (void *cls, 555regex_edge_iterator(void *cls,
560 const char *token, 556 const char *token,
561 size_t len, 557 size_t len,
562 const struct GNUNET_HashCode *key) 558 const struct GNUNET_HashCode *key)
563{ 559{
564 struct RegexSearchContext *ctx = cls; 560 struct RegexSearchContext *ctx = cls;
565 struct REGEX_INTERNAL_Search *info = ctx->info; 561 struct REGEX_INTERNAL_Search *info = ctx->info;
566 const char *current; 562 const char *current;
567 size_t current_len; 563 size_t current_len;
568 564
569 GNUNET_STATISTICS_update (info->stats, "# regex edges iterated", 565 GNUNET_STATISTICS_update(info->stats, "# regex edges iterated",
570 1, GNUNET_NO); 566 1, GNUNET_NO);
571 current = &info->description[ctx->position]; 567 current = &info->description[ctx->position];
572 current_len = strlen (info->description) - ctx->position; 568 current_len = strlen(info->description) - ctx->position;
573 if (len > current_len) 569 if (len > current_len)
574 { 570 {
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "Token too long, END\n"); 571 LOG(GNUNET_ERROR_TYPE_DEBUG, "Token too long, END\n");
576 return GNUNET_YES; 572 return GNUNET_YES;
577 } 573 }
578 if (0 != strncmp (current, token, len)) 574 if (0 != strncmp(current, token, len))
579 { 575 {
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Token doesn't match, END\n"); 576 LOG(GNUNET_ERROR_TYPE_DEBUG, "Token doesn't match, END\n");
581 return GNUNET_YES; 577 return GNUNET_YES;
582 } 578 }
583 579
584 if (len > ctx->longest_match) 580 if (len > ctx->longest_match)
585 { 581 {
586 LOG (GNUNET_ERROR_TYPE_DEBUG, "Token is longer, KEEP\n"); 582 LOG(GNUNET_ERROR_TYPE_DEBUG, "Token is longer, KEEP\n");
587 ctx->longest_match = len; 583 ctx->longest_match = len;
588 ctx->hash = *key; 584 ctx->hash = *key;
589 } 585 }
590 else 586 else
591 { 587 {
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "Token is not longer, IGNORE\n"); 588 LOG(GNUNET_ERROR_TYPE_DEBUG, "Token is not longer, IGNORE\n");
593 } 589 }
594 590
595 LOG (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n"); 591 LOG(GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
596 return GNUNET_YES; 592 return GNUNET_YES;
597} 593}
598 594
@@ -605,9 +601,9 @@ regex_edge_iterator (void *cls,
605 * @param ctx Context of the search. 601 * @param ctx Context of the search.
606 */ 602 */
607static void 603static void
608regex_next_edge (const struct RegexBlock *block, 604regex_next_edge(const struct RegexBlock *block,
609 size_t size, 605 size_t size,
610 struct RegexSearchContext *ctx) 606 struct RegexSearchContext *ctx)
611{ 607{
612 struct RegexSearchContext *new_ctx; 608 struct RegexSearchContext *new_ctx;
613 struct REGEX_INTERNAL_Search *info = ctx->info; 609 struct REGEX_INTERNAL_Search *info = ctx->info;
@@ -616,69 +612,69 @@ regex_next_edge (const struct RegexBlock *block,
616 const char *rest; 612 const char *rest;
617 int result; 613 int result;
618 614
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next edge\n"); 615 LOG(GNUNET_ERROR_TYPE_DEBUG, "Next edge\n");
620 /* Find the longest match for the current string position, 616 /* Find the longest match for the current string position,
621 * among tokens in the given block */ 617 * among tokens in the given block */
622 ctx->longest_match = 0; 618 ctx->longest_match = 0;
623 result = REGEX_BLOCK_iterate (block, size, 619 result = REGEX_BLOCK_iterate(block, size,
624 &regex_edge_iterator, ctx); 620 &regex_edge_iterator, ctx);
625 GNUNET_break (GNUNET_OK == result); 621 GNUNET_break(GNUNET_OK == result);
626 622
627 /* Did anything match? */ 623 /* Did anything match? */
628 if (0 == ctx->longest_match) 624 if (0 == ctx->longest_match)
629 { 625 {
630 LOG (GNUNET_ERROR_TYPE_DEBUG, 626 LOG(GNUNET_ERROR_TYPE_DEBUG,
631 "no match in block\n"); 627 "no match in block\n");
632 return; 628 return;
633 } 629 }
634 630
635 hash = &ctx->hash; 631 hash = &ctx->hash;
636 new_ctx = GNUNET_new (struct RegexSearchContext); 632 new_ctx = GNUNET_new(struct RegexSearchContext);
637 new_ctx->info = info; 633 new_ctx->info = info;
638 new_ctx->position = ctx->position + ctx->longest_match; 634 new_ctx->position = ctx->position + ctx->longest_match;
639 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx); 635 GNUNET_array_append(info->contexts, info->n_contexts, new_ctx);
640 636
641 /* Check whether we already have a DHT GET running for it */ 637 /* Check whether we already have a DHT GET running for it */
642 if (GNUNET_YES == 638 if (GNUNET_YES ==
643 GNUNET_CONTAINER_multihashmap_contains (info->dht_get_handles, hash)) 639 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, hash))
644 { 640 {
645 LOG (GNUNET_ERROR_TYPE_DEBUG, 641 LOG(GNUNET_ERROR_TYPE_DEBUG,
646 "GET for %s running, END\n", 642 "GET for %s running, END\n",
647 GNUNET_h2s (hash)); 643 GNUNET_h2s(hash));
648 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, 644 GNUNET_CONTAINER_multihashmap_get_multiple(info->dht_get_results,
649 hash, 645 hash,
650 &regex_result_iterator, 646 &regex_result_iterator,
651 new_ctx); 647 new_ctx);
652 return; /* We are already looking for it */ 648 return; /* We are already looking for it */
653 } 649 }
654 650
655 GNUNET_STATISTICS_update (info->stats, "# regex nodes traversed", 651 GNUNET_STATISTICS_update(info->stats, "# regex nodes traversed",
656 1, GNUNET_NO); 652 1, GNUNET_NO);
657 653
658 LOG (GNUNET_ERROR_TYPE_DEBUG, 654 LOG(GNUNET_ERROR_TYPE_DEBUG,
659 "Following edges at %s for offset %u in `%s'\n", 655 "Following edges at %s for offset %u in `%s'\n",
660 GNUNET_h2s (hash), 656 GNUNET_h2s(hash),
661 (unsigned int) ctx->position, 657 (unsigned int)ctx->position,
662 info->description); 658 info->description);
663 rest = &new_ctx->info->description[new_ctx->position]; 659 rest = &new_ctx->info->description[new_ctx->position];
664 get_h = 660 get_h =
665 GNUNET_DHT_get_start (info->dht, /* handle */ 661 GNUNET_DHT_get_start(info->dht, /* handle */
666 GNUNET_BLOCK_TYPE_REGEX, /* type */ 662 GNUNET_BLOCK_TYPE_REGEX, /* type */
667 hash, /* key to search */ 663 hash, /* key to search */
668 DHT_REPLICATION, /* replication level */ 664 DHT_REPLICATION, /* replication level */
669 DHT_OPT, 665 DHT_OPT,
670 rest, /* xquery */ 666 rest, /* xquery */
671 strlen (rest) + 1, /* xquery bits */ 667 strlen(rest) + 1, /* xquery bits */
672 &dht_get_string_handler, new_ctx); 668 &dht_get_string_handler, new_ctx);
673 if (GNUNET_OK != 669 if (GNUNET_OK !=
674 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, 670 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
675 hash, 671 hash,
676 get_h, 672 get_h,
677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
678 { 674 {
679 GNUNET_break (0); 675 GNUNET_break(0);
680 return; 676 return;
681 } 677 }
682} 678}
683 679
684 680
@@ -696,11 +692,11 @@ regex_next_edge (const struct RegexBlock *block,
696 * Must be freed by calling #REGEX_INTERNAL_search_cancel(). 692 * Must be freed by calling #REGEX_INTERNAL_search_cancel().
697 */ 693 */
698struct REGEX_INTERNAL_Search * 694struct REGEX_INTERNAL_Search *
699REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht, 695REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht,
700 const char *string, 696 const char *string,
701 REGEX_INTERNAL_Found callback, 697 REGEX_INTERNAL_Found callback,
702 void *callback_cls, 698 void *callback_cls,
703 struct GNUNET_STATISTICS_Handle *stats) 699 struct GNUNET_STATISTICS_Handle *stats)
704{ 700{
705 struct REGEX_INTERNAL_Search *h; 701 struct REGEX_INTERNAL_Search *h;
706 struct GNUNET_DHT_GetHandle *get_h; 702 struct GNUNET_DHT_GetHandle *get_h;
@@ -710,50 +706,50 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
710 size_t len; 706 size_t len;
711 707
712 /* Initialize handle */ 708 /* Initialize handle */
713 GNUNET_assert (NULL != dht); 709 GNUNET_assert(NULL != dht);
714 GNUNET_assert (NULL != callback); 710 GNUNET_assert(NULL != callback);
715 h = GNUNET_new (struct REGEX_INTERNAL_Search); 711 h = GNUNET_new(struct REGEX_INTERNAL_Search);
716 h->dht = dht; 712 h->dht = dht;
717 h->description = GNUNET_strdup (string); 713 h->description = GNUNET_strdup(string);
718 h->callback = callback; 714 h->callback = callback;
719 h->callback_cls = callback_cls; 715 h->callback_cls = callback_cls;
720 h->stats = stats; 716 h->stats = stats;
721 h->dht_get_handles = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); 717 h->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
722 h->dht_get_results = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); 718 h->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
723 719
724 /* Initialize context */ 720 /* Initialize context */
725 len = strlen (string); 721 len = strlen(string);
726 size = REGEX_INTERNAL_get_first_key (string, len, &key); 722 size = REGEX_INTERNAL_get_first_key(string, len, &key);
727 LOG (GNUNET_ERROR_TYPE_INFO, 723 LOG(GNUNET_ERROR_TYPE_INFO,
728 "Initial key for `%s' is %s (based on `%.*s')\n", 724 "Initial key for `%s' is %s (based on `%.*s')\n",
729 string, 725 string,
730 GNUNET_h2s (&key), 726 GNUNET_h2s(&key),
731 size, 727 size,
732 string); 728 string);
733 ctx = GNUNET_new (struct RegexSearchContext); 729 ctx = GNUNET_new(struct RegexSearchContext);
734 ctx->position = size; 730 ctx->position = size;
735 ctx->info = h; 731 ctx->info = h;
736 GNUNET_array_append (h->contexts, 732 GNUNET_array_append(h->contexts,
737 h->n_contexts, 733 h->n_contexts,
738 ctx); 734 ctx);
739 /* Start search in DHT */ 735 /* Start search in DHT */
740 get_h = GNUNET_DHT_get_start (h->dht, /* handle */ 736 get_h = GNUNET_DHT_get_start(h->dht, /* handle */
741 GNUNET_BLOCK_TYPE_REGEX, /* type */ 737 GNUNET_BLOCK_TYPE_REGEX, /* type */
742 &key, /* key to search */ 738 &key, /* key to search */
743 DHT_REPLICATION, /* replication level */ 739 DHT_REPLICATION, /* replication level */
744 DHT_OPT, 740 DHT_OPT,
745 &h->description[size], /* xquery */ 741 &h->description[size], /* xquery */
746 // FIXME add BLOOMFILTER to exclude filtered peers 742 // FIXME add BLOOMFILTER to exclude filtered peers
747 len + 1 - size, /* xquery bits */ 743 len + 1 - size, /* xquery bits */
748 // FIXME add BLOOMFILTER SIZE 744 // FIXME add BLOOMFILTER SIZE
749 &dht_get_string_handler, ctx); 745 &dht_get_string_handler, ctx);
750 GNUNET_break ( 746 GNUNET_break(
751 GNUNET_OK == 747 GNUNET_OK ==
752 GNUNET_CONTAINER_multihashmap_put (h->dht_get_handles, 748 GNUNET_CONTAINER_multihashmap_put(h->dht_get_handles,
753 &key, 749 &key,
754 get_h, 750 get_h,
755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST) 751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)
756 ); 752 );
757 753
758 return h; 754 return h;
759} 755}
@@ -770,13 +766,13 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
770 * #GNUNET_NO if not. 766 * #GNUNET_NO if not.
771 */ 767 */
772static int 768static int
773regex_cancel_dht_get (void *cls, 769regex_cancel_dht_get(void *cls,
774 const struct GNUNET_HashCode * key, 770 const struct GNUNET_HashCode * key,
775 void *value) 771 void *value)
776{ 772{
777 struct GNUNET_DHT_GetHandle *h = value; 773 struct GNUNET_DHT_GetHandle *h = value;
778 774
779 GNUNET_DHT_get_stop (h); 775 GNUNET_DHT_get_stop(h);
780 return GNUNET_YES; 776 return GNUNET_YES;
781} 777}
782 778
@@ -792,11 +788,11 @@ regex_cancel_dht_get (void *cls,
792 * #GNUNET_NO if not. 788 * #GNUNET_NO if not.
793 */ 789 */
794static int 790static int
795regex_free_result (void *cls, 791regex_free_result(void *cls,
796 const struct GNUNET_HashCode * key, 792 const struct GNUNET_HashCode * key,
797 void *value) 793 void *value)
798{ 794{
799 GNUNET_free (value); 795 GNUNET_free(value);
800 return GNUNET_YES; 796 return GNUNET_YES;
801} 797}
802 798
@@ -807,24 +803,24 @@ regex_free_result (void *cls,
807 * @param h the search context. 803 * @param h the search context.
808 */ 804 */
809void 805void
810REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h) 806REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h)
811{ 807{
812 unsigned int i; 808 unsigned int i;
813 809
814 GNUNET_free (h->description); 810 GNUNET_free(h->description);
815 GNUNET_CONTAINER_multihashmap_iterate (h->dht_get_handles, 811 GNUNET_CONTAINER_multihashmap_iterate(h->dht_get_handles,
816 &regex_cancel_dht_get, NULL); 812 &regex_cancel_dht_get, NULL);
817 GNUNET_CONTAINER_multihashmap_iterate (h->dht_get_results, 813 GNUNET_CONTAINER_multihashmap_iterate(h->dht_get_results,
818 &regex_free_result, NULL); 814 &regex_free_result, NULL);
819 GNUNET_CONTAINER_multihashmap_destroy (h->dht_get_results); 815 GNUNET_CONTAINER_multihashmap_destroy(h->dht_get_results);
820 GNUNET_CONTAINER_multihashmap_destroy (h->dht_get_handles); 816 GNUNET_CONTAINER_multihashmap_destroy(h->dht_get_handles);
821 if (0 < h->n_contexts) 817 if (0 < h->n_contexts)
822 { 818 {
823 for (i = 0; i < h->n_contexts; i++) 819 for (i = 0; i < h->n_contexts; i++)
824 GNUNET_free (h->contexts[i]); 820 GNUNET_free(h->contexts[i]);
825 GNUNET_free (h->contexts); 821 GNUNET_free(h->contexts);
826 } 822 }
827 GNUNET_free (h); 823 GNUNET_free(h);
828} 824}
829 825
830 826
diff --git a/src/regex/regex_internal_lib.h b/src/regex/regex_internal_lib.h
index bbf89758e..9cb925a17 100644
--- a/src/regex/regex_internal_lib.h
+++ b/src/regex/regex_internal_lib.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/regex_internal_lib.h 21 * @file regex/regex_internal_lib.h
22 * @brief library to parse regular expressions into dfa 22 * @brief library to parse regular expressions into dfa
@@ -64,9 +64,9 @@ struct REGEX_INTERNAL_Automaton;
64 * @return DFA, needs to be freed using #REGEX_INTERNAL_automaton_destroy(). 64 * @return DFA, needs to be freed using #REGEX_INTERNAL_automaton_destroy().
65 */ 65 */
66struct REGEX_INTERNAL_Automaton * 66struct REGEX_INTERNAL_Automaton *
67REGEX_INTERNAL_construct_dfa (const char *regex, 67REGEX_INTERNAL_construct_dfa(const char *regex,
68 const size_t len, 68 const size_t len,
69 unsigned int max_path_len); 69 unsigned int max_path_len);
70 70
71 71
72/** 72/**
@@ -76,7 +76,7 @@ REGEX_INTERNAL_construct_dfa (const char *regex,
76 * @param a automaton to be destroyed. 76 * @param a automaton to be destroyed.
77 */ 77 */
78void 78void
79REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a); 79REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a);
80 80
81 81
82/** 82/**
@@ -88,8 +88,8 @@ REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a);
88 * @return 0 if string matches, non 0 otherwise. 88 * @return 0 if string matches, non 0 otherwise.
89 */ 89 */
90int 90int
91REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a, 91REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a,
92 const char *string); 92 const char *string);
93 93
94 94
95/** 95/**
@@ -103,9 +103,9 @@ REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
103 * to construct the key 103 * to construct the key
104 */ 104 */
105size_t 105size_t
106REGEX_INTERNAL_get_first_key (const char *input_string, 106REGEX_INTERNAL_get_first_key(const char *input_string,
107 size_t string_len, 107 size_t string_len,
108 struct GNUNET_HashCode * key); 108 struct GNUNET_HashCode * key);
109 109
110 110
111/** 111/**
@@ -136,9 +136,9 @@ typedef void
136 * @param iterator_cls closure. 136 * @param iterator_cls closure.
137 */ 137 */
138void 138void
139REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a, 139REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a,
140 REGEX_INTERNAL_KeyIterator iterator, 140 REGEX_INTERNAL_KeyIterator iterator,
141 void *iterator_cls); 141 void *iterator_cls);
142 142
143 143
144/** 144/**
@@ -152,9 +152,9 @@ REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
152 * @param iterator_cls closure. 152 * @param iterator_cls closure.
153 */ 153 */
154void 154void
155REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a, 155REGEX_INTERNAL_iterate_reachable_edges(struct REGEX_INTERNAL_Automaton *a,
156 REGEX_INTERNAL_KeyIterator iterator, 156 REGEX_INTERNAL_KeyIterator iterator,
157 void *iterator_cls); 157 void *iterator_cls);
158 158
159 159
160 160
@@ -182,11 +182,11 @@ struct REGEX_INTERNAL_Search;
182 * Must be freed by calling #REGEX_INTERNAL_announce_cancel(). 182 * Must be freed by calling #REGEX_INTERNAL_announce_cancel().
183 */ 183 */
184struct REGEX_INTERNAL_Announcement * 184struct REGEX_INTERNAL_Announcement *
185REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht, 185REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht,
186 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, 186 const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
187 const char *regex, 187 const char *regex,
188 uint16_t compression, 188 uint16_t compression,
189 struct GNUNET_STATISTICS_Handle *stats); 189 struct GNUNET_STATISTICS_Handle *stats);
190 190
191 191
192/** 192/**
@@ -196,7 +196,7 @@ REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
196 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. 196 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
197 */ 197 */
198void 198void
199REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h); 199REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h);
200 200
201 201
202/** 202/**
@@ -206,7 +206,7 @@ REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h);
206 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. 206 * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call.
207 */ 207 */
208void 208void
209REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h); 209REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h);
210 210
211 211
212/** 212/**
@@ -242,11 +242,11 @@ typedef void
242 * Must be freed by calling #REGEX_INTERNAL_search_cancel(). 242 * Must be freed by calling #REGEX_INTERNAL_search_cancel().
243 */ 243 */
244struct REGEX_INTERNAL_Search * 244struct REGEX_INTERNAL_Search *
245REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht, 245REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht,
246 const char *string, 246 const char *string,
247 REGEX_INTERNAL_Found callback, 247 REGEX_INTERNAL_Found callback,
248 void *callback_cls, 248 void *callback_cls,
249 struct GNUNET_STATISTICS_Handle *stats); 249 struct GNUNET_STATISTICS_Handle *stats);
250 250
251/** 251/**
252 * Stop search and free all data used by a #REGEX_INTERNAL_search() call. 252 * Stop search and free all data used by a #REGEX_INTERNAL_search() call.
@@ -255,7 +255,7 @@ REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
255 * @param h Handle returned by a previous #REGEX_INTERNAL_search() call. 255 * @param h Handle returned by a previous #REGEX_INTERNAL_search() call.
256 */ 256 */
257void 257void
258REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h); 258REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h);
259 259
260 260
261#if 0 /* keep Emacsens' auto-indent happy */ 261#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/regex/regex_ipc.h b/src/regex/regex_ipc.h
index 11c570178..5106ead24 100644
--- a/src/regex/regex_ipc.h
+++ b/src/regex/regex_ipc.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/regex_ipc.h 21 * @file regex/regex_ipc.h
22 * @brief regex IPC messages (not called 'regex.h' due to conflict with 22 * @brief regex IPC messages (not called 'regex.h' due to conflict with
@@ -31,9 +31,7 @@
31/** 31/**
32 * Request for regex service to announce capability. 32 * Request for regex service to announce capability.
33 */ 33 */
34struct AnnounceMessage 34struct AnnounceMessage {
35{
36
37 /** 35 /**
38 * Type is GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE 36 * Type is GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE
39 */ 37 */
@@ -61,23 +59,20 @@ struct AnnounceMessage
61/** 59/**
62 * Message to initiate regex search. 60 * Message to initiate regex search.
63 */ 61 */
64struct RegexSearchMessage 62struct RegexSearchMessage {
65{
66 /** 63 /**
67 * Type is GNUNET_MESSAGE_TYPE_REGEX_SEARCH 64 * Type is GNUNET_MESSAGE_TYPE_REGEX_SEARCH
68 */ 65 */
69 struct GNUNET_MessageHeader header; 66 struct GNUNET_MessageHeader header;
70 67
71 /* followed by 0-terminated search string */ 68 /* followed by 0-terminated search string */
72
73}; 69};
74 70
75 71
76/** 72/**
77 * Result from regex search. 73 * Result from regex search.
78 */ 74 */
79struct ResultMessage 75struct ResultMessage {
80{
81 /** 76 /**
82 * Type is GNUNET_MESSAGE_TYPE_REGEX_RESULT 77 * Type is GNUNET_MESSAGE_TYPE_REGEX_RESULT
83 */ 78 */
@@ -99,7 +94,6 @@ struct ResultMessage
99 struct GNUNET_PeerIdentity id; 94 struct GNUNET_PeerIdentity id;
100 95
101 /* followed by GET path and PUT path arrays */ 96 /* followed by GET path and PUT path arrays */
102
103}; 97};
104 98
105 99
diff --git a/src/regex/regex_test_graph.c b/src/regex/regex_test_graph.c
index 54ee1d17f..e809d578d 100644
--- a/src/regex/regex_test_graph.c
+++ b/src/regex/regex_test_graph.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_test_graph.c 21 * @file src/regex/regex_test_graph.c
22 * @brief functions for creating .dot graphs from regexes 22 * @brief functions for creating .dot graphs from regexes
@@ -31,8 +31,7 @@
31 * Context for graph creation. Passed as the cls to 31 * Context for graph creation. Passed as the cls to
32 * REGEX_TEST_automaton_save_graph_step. 32 * REGEX_TEST_automaton_save_graph_step.
33 */ 33 */
34struct REGEX_TEST_Graph_Context 34struct REGEX_TEST_Graph_Context {
35{
36 /** 35 /**
37 * File pointer to the dot file used for output. 36 * File pointer to the dot file used for output.
38 */ 37 */
@@ -63,10 +62,10 @@ struct REGEX_TEST_Graph_Context
63 * @param stack_size current size of the stack 62 * @param stack_size current size of the stack
64 */ 63 */
65static void 64static void
66scc_tarjan_strongconnect (unsigned int *scc_counter, 65scc_tarjan_strongconnect(unsigned int *scc_counter,
67 struct REGEX_INTERNAL_State *v, unsigned int *index, 66 struct REGEX_INTERNAL_State *v, unsigned int *index,
68 struct REGEX_INTERNAL_State **stack, 67 struct REGEX_INTERNAL_State **stack,
69 unsigned int *stack_size) 68 unsigned int *stack_size)
70{ 69{
71 struct REGEX_INTERNAL_State *w; 70 struct REGEX_INTERNAL_State *w;
72 struct REGEX_INTERNAL_Transition *t; 71 struct REGEX_INTERNAL_Transition *t;
@@ -78,32 +77,32 @@ scc_tarjan_strongconnect (unsigned int *scc_counter,
78 v->contained = 1; 77 v->contained = 1;
79 78
80 for (t = v->transitions_head; NULL != t; t = t->next) 79 for (t = v->transitions_head; NULL != t; t = t->next)
81 {
82 w = t->to_state;
83
84 if (NULL == w)
85 continue;
86
87 if (w->index < 0)
88 { 80 {
89 scc_tarjan_strongconnect (scc_counter, w, index, stack, stack_size); 81 w = t->to_state;
90 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink; 82
83 if (NULL == w)
84 continue;
85
86 if (w->index < 0)
87 {
88 scc_tarjan_strongconnect(scc_counter, w, index, stack, stack_size);
89 v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink;
90 }
91 else if (1 == w->contained)
92 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
91 } 93 }
92 else if (1 == w->contained)
93 v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink;
94 }
95 94
96 if (v->lowlink == v->index) 95 if (v->lowlink == v->index)
97 {
98 (*scc_counter)++;
99 do
100 { 96 {
101 w = stack[--(*stack_size)]; 97 (*scc_counter)++;
102 w->contained = 0; 98 do
103 w->scc_id = *scc_counter; 99 {
100 w = stack[--(*stack_size)];
101 w->contained = 0;
102 w->scc_id = *scc_counter;
103 }
104 while (w != v);
104 } 105 }
105 while (w != v);
106 }
107} 106}
108 107
109 108
@@ -114,7 +113,7 @@ scc_tarjan_strongconnect (unsigned int *scc_counter,
114 * @param a the automaton for which SCCs should be computed and assigned. 113 * @param a the automaton for which SCCs should be computed and assigned.
115 */ 114 */
116static void 115static void
117scc_tarjan (struct REGEX_INTERNAL_Automaton *a) 116scc_tarjan(struct REGEX_INTERNAL_Automaton *a)
118{ 117{
119 unsigned int index; 118 unsigned int index;
120 unsigned int scc_counter; 119 unsigned int scc_counter;
@@ -123,21 +122,21 @@ scc_tarjan (struct REGEX_INTERNAL_Automaton *a)
123 unsigned int stack_size; 122 unsigned int stack_size;
124 123
125 for (v = a->states_head; NULL != v; v = v->next) 124 for (v = a->states_head; NULL != v; v = v->next)
126 { 125 {
127 v->contained = 0; 126 v->contained = 0;
128 v->index = -1; 127 v->index = -1;
129 v->lowlink = -1; 128 v->lowlink = -1;
130 } 129 }
131 130
132 stack_size = 0; 131 stack_size = 0;
133 index = 0; 132 index = 0;
134 scc_counter = 0; 133 scc_counter = 0;
135 134
136 for (v = a->states_head; NULL != v; v = v->next) 135 for (v = a->states_head; NULL != v; v = v->next)
137 { 136 {
138 if (v->index < 0) 137 if (v->index < 0)
139 scc_tarjan_strongconnect (&scc_counter, v, &index, stack, &stack_size); 138 scc_tarjan_strongconnect(&scc_counter, v, &index, stack, &stack_size);
140 } 139 }
141} 140}
142 141
143 142
@@ -151,8 +150,8 @@ scc_tarjan (struct REGEX_INTERNAL_Automaton *a)
151 * @param s state. 150 * @param s state.
152 */ 151 */
153void 152void
154REGEX_TEST_automaton_save_graph_step (void *cls, unsigned int count, 153REGEX_TEST_automaton_save_graph_step(void *cls, unsigned int count,
155 struct REGEX_INTERNAL_State *s) 154 struct REGEX_INTERNAL_State *s)
156{ 155{
157 struct REGEX_TEST_Graph_Context *ctx = cls; 156 struct REGEX_TEST_Graph_Context *ctx = cls;
158 struct REGEX_INTERNAL_Transition *ctran; 157 struct REGEX_INTERNAL_Transition *ctran;
@@ -162,100 +161,100 @@ REGEX_TEST_automaton_save_graph_step (void *cls, unsigned int count,
162 char *to_name; 161 char *to_name;
163 162
164 if (GNUNET_YES == ctx->verbose) 163 if (GNUNET_YES == ctx->verbose)
165 GNUNET_asprintf (&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof, 164 GNUNET_asprintf(&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof,
166 GNUNET_h2s (&s->hash)); 165 GNUNET_h2s(&s->hash));
167 else 166 else
168 GNUNET_asprintf (&name, "%i", s->dfs_id); 167 GNUNET_asprintf(&name, "%i", s->dfs_id);
169 168
170 if (s->accepting) 169 if (s->accepting)
171 {
172 if (GNUNET_YES == ctx->coloring)
173 { 170 {
174 GNUNET_asprintf (&s_acc, 171 if (GNUNET_YES == ctx->coloring)
175 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n", 172 {
176 name, s->scc_id * s->scc_id); 173 GNUNET_asprintf(&s_acc,
174 "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n",
175 name, s->scc_id * s->scc_id);
176 }
177 else
178 {
179 GNUNET_asprintf(&s_acc, "\"%s\" [shape=doublecircle];\n", name,
180 s->scc_id);
181 }
177 } 182 }
178 else 183 else if (GNUNET_YES == ctx->coloring)
179 { 184 {
180 GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", name, 185 GNUNET_asprintf(&s_acc,
181 s->scc_id); 186 "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name,
187 s->scc_id * s->scc_id);
182 } 188 }
183 }
184 else if (GNUNET_YES == ctx->coloring)
185 {
186 GNUNET_asprintf (&s_acc,
187 "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name,
188 s->scc_id * s->scc_id);
189 }
190 else 189 else
191 { 190 {
192 GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id); 191 GNUNET_asprintf(&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id);
193 } 192 }
194 193
195 GNUNET_assert (NULL != s_acc); 194 GNUNET_assert(NULL != s_acc);
196 195
197 fwrite (s_acc, strlen (s_acc), 1, ctx->filep); 196 fwrite(s_acc, strlen(s_acc), 1, ctx->filep);
198 GNUNET_free (s_acc); 197 GNUNET_free(s_acc);
199 s_acc = NULL; 198 s_acc = NULL;
200 199
201 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next) 200 for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next)
202 {
203 if (NULL == ctran->to_state)
204 {
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 "Transition from State %i has no state for transitioning\n",
207 s->id);
208 continue;
209 }
210
211 if (GNUNET_YES == ctx->verbose)
212 { 201 {
213 GNUNET_asprintf (&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id, 202 if (NULL == ctran->to_state)
214 ctran->to_state->name, ctran->to_state->proof, 203 {
215 GNUNET_h2s (&ctran->to_state->hash)); 204 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
216 } 205 "Transition from State %i has no state for transitioning\n",
217 else 206 s->id);
218 GNUNET_asprintf (&to_name, "%i", ctran->to_state->dfs_id); 207 continue;
219 208 }
220 if (NULL == ctran->label) 209
221 { 210 if (GNUNET_YES == ctx->verbose)
222 if (GNUNET_YES == ctx->coloring) 211 {
223 { 212 GNUNET_asprintf(&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id,
224 GNUNET_asprintf (&s_tran, 213 ctran->to_state->name, ctran->to_state->proof,
225 "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n", 214 GNUNET_h2s(&ctran->to_state->hash));
226 name, to_name, s->scc_id * s->scc_id); 215 }
227 }
228 else 216 else
229 { 217 GNUNET_asprintf(&to_name, "%i", ctran->to_state->dfs_id);
230 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name, 218
231 to_name, s->scc_id); 219 if (NULL == ctran->label)
232 } 220 {
233 } 221 if (GNUNET_YES == ctx->coloring)
234 else 222 {
235 { 223 GNUNET_asprintf(&s_tran,
236 if (GNUNET_YES == ctx->coloring) 224 "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n",
237 { 225 name, to_name, s->scc_id * s->scc_id);
238 GNUNET_asprintf (&s_tran, 226 }
239 "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n", 227 else
240 name, to_name, ctran->label, s->scc_id * s->scc_id); 228 {
241 } 229 GNUNET_asprintf(&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name,
230 to_name, s->scc_id);
231 }
232 }
242 else 233 else
243 { 234 {
244 GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name, 235 if (GNUNET_YES == ctx->coloring)
245 to_name, ctran->label, s->scc_id); 236 {
246 } 237 GNUNET_asprintf(&s_tran,
238 "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n",
239 name, to_name, ctran->label, s->scc_id * s->scc_id);
240 }
241 else
242 {
243 GNUNET_asprintf(&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name,
244 to_name, ctran->label, s->scc_id);
245 }
246 }
247
248 GNUNET_free(to_name);
249
250 GNUNET_assert(NULL != s_tran);
251
252 fwrite(s_tran, strlen(s_tran), 1, ctx->filep);
253 GNUNET_free(s_tran);
254 s_tran = NULL;
247 } 255 }
248 256
249 GNUNET_free (to_name); 257 GNUNET_free(name);
250
251 GNUNET_assert (NULL != s_tran);
252
253 fwrite (s_tran, strlen (s_tran), 1, ctx->filep);
254 GNUNET_free (s_tran);
255 s_tran = NULL;
256 }
257
258 GNUNET_free (name);
259} 258}
260 259
261 260
@@ -268,51 +267,51 @@ REGEX_TEST_automaton_save_graph_step (void *cls, unsigned int count,
268 * mode 267 * mode
269 */ 268 */
270void 269void
271REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a, 270REGEX_TEST_automaton_save_graph(struct REGEX_INTERNAL_Automaton *a,
272 const char *filename, 271 const char *filename,
273 enum REGEX_TEST_GraphSavingOptions options) 272 enum REGEX_TEST_GraphSavingOptions options)
274{ 273{
275 char *start; 274 char *start;
276 char *end; 275 char *end;
277 struct REGEX_TEST_Graph_Context ctx; 276 struct REGEX_TEST_Graph_Context ctx;
278 277
279 if (NULL == a) 278 if (NULL == a)
280 { 279 {
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!"); 280 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!");
282 return; 281 return;
283 } 282 }
284 283
285 if (NULL == filename || strlen (filename) < 1) 284 if (NULL == filename || strlen(filename) < 1)
286 { 285 {
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!"); 286 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No Filename given!");
288 return; 287 return;
289 } 288 }
290 289
291 ctx.filep = fopen (filename, "w"); 290 ctx.filep = fopen(filename, "w");
292 ctx.verbose = 291 ctx.verbose =
293 (0 == (options & REGEX_TEST_GRAPH_VERBOSE)) ? GNUNET_NO : GNUNET_YES; 292 (0 == (options & REGEX_TEST_GRAPH_VERBOSE)) ? GNUNET_NO : GNUNET_YES;
294 ctx.coloring = 293 ctx.coloring =
295 (0 == (options & REGEX_TEST_GRAPH_COLORING)) ? GNUNET_NO : GNUNET_YES; 294 (0 == (options & REGEX_TEST_GRAPH_COLORING)) ? GNUNET_NO : GNUNET_YES;
296 295
297 if (NULL == ctx.filep) 296 if (NULL == ctx.filep)
298 { 297 {
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s", 298 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s",
300 filename); 299 filename);
301 return; 300 return;
302 } 301 }
303 302
304 /* First add the SCCs to the automaton, so we can color them nicely */ 303 /* First add the SCCs to the automaton, so we can color them nicely */
305 if (GNUNET_YES == ctx.coloring) 304 if (GNUNET_YES == ctx.coloring)
306 scc_tarjan (a); 305 scc_tarjan(a);
307 306
308 start = "digraph G {\nrankdir=LR\n"; 307 start = "digraph G {\nrankdir=LR\n";
309 fwrite (start, strlen (start), 1, ctx.filep); 308 fwrite(start, strlen(start), 1, ctx.filep);
310 309
311 REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, 310 REGEX_INTERNAL_automaton_traverse(a, a->start, NULL, NULL,
312 &REGEX_TEST_automaton_save_graph_step, 311 &REGEX_TEST_automaton_save_graph_step,
313 &ctx); 312 &ctx);
314 313
315 end = "\n}\n"; 314 end = "\n}\n";
316 fwrite (end, strlen (end), 1, ctx.filep); 315 fwrite(end, strlen(end), 1, ctx.filep);
317 fclose (ctx.filep); 316 fclose(ctx.filep);
318} 317}
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c
index 7becd567c..d8eb22370 100644
--- a/src/regex/regex_test_lib.c
+++ b/src/regex/regex_test_lib.c
@@ -11,7 +11,7 @@
11 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details. 13 * Affero General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Affero General Public License 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/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -32,8 +32,7 @@
32/** 32/**
33 * Struct to hold the tree formed by prefix-combining the regexes. 33 * Struct to hold the tree formed by prefix-combining the regexes.
34 */ 34 */
35struct RegexCombineCtx 35struct RegexCombineCtx {
36{
37 /** 36 /**
38 * Child nodes with same prefix and token. 37 * Child nodes with same prefix and token.
39 */ 38 */
@@ -62,14 +61,15 @@ struct RegexCombineCtx
62 * @return Int in range [0, (base-1)] 61 * @return Int in range [0, (base-1)]
63 */ 62 */
64static int 63static int
65c2i (char c, int size) 64c2i(char c, int size)
66{ 65{
67 switch (size) 66 switch (size)
68 { 67 {
69 case 2: 68 case 2:
70 case 8: 69 case 8:
71 return c - '0'; 70 return c - '0';
72 break; 71 break;
72
73 case 16: 73 case 16:
74 if (c >= '0' && c <= '9') 74 if (c >= '0' && c <= '9')
75 return c - '0'; 75 return c - '0';
@@ -78,16 +78,17 @@ c2i (char c, int size)
78 else if (c >= 'a' && c <= 'f') 78 else if (c >= 'a' && c <= 'f')
79 return c - 'a' + 10; 79 return c - 'a' + 10;
80 else 80 else
81 { 81 {
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 82 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
83 "Cannot convert char %c in base %u\n", 83 "Cannot convert char %c in base %u\n",
84 c, size); 84 c, size);
85 GNUNET_assert (0); 85 GNUNET_assert(0);
86 } 86 }
87 break; 87 break;
88
88 default: 89 default:
89 GNUNET_assert (0); 90 GNUNET_assert(0);
90 } 91 }
91} 92}
92 93
93 94
@@ -97,10 +98,10 @@ c2i (char c, int size)
97 * @param n Indentation level 98 * @param n Indentation level
98 */ 99 */
99static void 100static void
100space (int n) 101space(int n)
101{ 102{
102 for (int i = 0; i < n; i++) 103 for (int i = 0; i < n; i++)
103 fprintf (stderr, "| "); 104 fprintf(stderr, "| ");
104} 105}
105 106
106 107
@@ -111,24 +112,24 @@ space (int n)
111 * @param level Indentation level to start with 112 * @param level Indentation level to start with
112 */ 113 */
113static void 114static void
114debugctx (struct RegexCombineCtx *ctx, int level) 115debugctx(struct RegexCombineCtx *ctx, int level)
115{ 116{
116#if DEBUG_REGEX 117#if DEBUG_REGEX
117 if (NULL != ctx->s) 118 if (NULL != ctx->s)
118 { 119 {
119 space (level - 1); 120 space(level - 1);
120 fprintf (stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s); 121 fprintf(stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s);
121 } 122 }
122 else 123 else
123 fprintf (stderr, "ROOT (base %u)\n", ctx->size); 124 fprintf(stderr, "ROOT (base %u)\n", ctx->size);
124 for (unsigned int i = 0; i < ctx->size; i++) 125 for (unsigned int i = 0; i < ctx->size; i++)
125 {
126 if (NULL != ctx->children[i])
127 { 126 {
128 space (level); 127 if (NULL != ctx->children[i])
129 debugctx (ctx->children[i], level + 1); 128 {
129 space(level);
130 debugctx(ctx->children[i], level + 1);
131 }
130 } 132 }
131 }
132 fflush(stderr); 133 fflush(stderr);
133#endif 134#endif
134} 135}
@@ -141,8 +142,8 @@ debugctx (struct RegexCombineCtx *ctx, int level)
141 * @param regex Regex to add. 142 * @param regex Regex to add.
142 */ 143 */
143static void 144static void
144regex_add (struct RegexCombineCtx *ctx, 145regex_add(struct RegexCombineCtx *ctx,
145 const char *regex); 146 const char *regex);
146 147
147 148
148/** 149/**
@@ -151,14 +152,14 @@ regex_add (struct RegexCombineCtx *ctx,
151 * @param alphabet_size Size of the alphabet (and the Trie array) 152 * @param alphabet_size Size of the alphabet (and the Trie array)
152 */ 153 */
153static struct RegexCombineCtx * 154static struct RegexCombineCtx *
154new_regex_ctx (unsigned int alphabet_size) 155new_regex_ctx(unsigned int alphabet_size)
155{ 156{
156 struct RegexCombineCtx *ctx; 157 struct RegexCombineCtx *ctx;
157 size_t array_size; 158 size_t array_size;
158 159
159 array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; 160 array_size = sizeof(struct RegexCombineCtx *) * alphabet_size;
160 ctx = GNUNET_new (struct RegexCombineCtx); 161 ctx = GNUNET_new(struct RegexCombineCtx);
161 ctx->children = GNUNET_malloc (array_size); 162 ctx->children = GNUNET_malloc(array_size);
162 ctx->size = alphabet_size; 163 ctx->size = alphabet_size;
163 164
164 return ctx; 165 return ctx;
@@ -166,19 +167,19 @@ new_regex_ctx (unsigned int alphabet_size)
166 167
167 168
168static void 169static void
169move_children (struct RegexCombineCtx *dst, 170move_children(struct RegexCombineCtx *dst,
170 const struct RegexCombineCtx *src) 171 const struct RegexCombineCtx *src)
171{ 172{
172 size_t array_size; 173 size_t array_size;
173 174
174 array_size = sizeof(struct RegexCombineCtx *) * src->size; 175 array_size = sizeof(struct RegexCombineCtx *) * src->size;
175 GNUNET_memcpy (dst->children, 176 GNUNET_memcpy(dst->children,
176 src->children, 177 src->children,
177 array_size); 178 array_size);
178 for (unsigned int i = 0; i < src->size; i++) 179 for (unsigned int i = 0; i < src->size; i++)
179 { 180 {
180 src->children[i] = NULL; 181 src->children[i] = NULL;
181 } 182 }
182} 183}
183 184
184 185
@@ -190,7 +191,7 @@ move_children (struct RegexCombineCtx *dst,
190 * @return Regex that matches any of the added regexes. 191 * @return Regex that matches any of the added regexes.
191 */ 192 */
192static char * 193static char *
193regex_combine (struct RegexCombineCtx *ctx) 194regex_combine(struct RegexCombineCtx *ctx)
194{ 195{
195 struct RegexCombineCtx *p; 196 struct RegexCombineCtx *p;
196 unsigned int i; 197 unsigned int i;
@@ -200,56 +201,56 @@ regex_combine (struct RegexCombineCtx *ctx)
200 char *s; 201 char *s;
201 int opt; 202 int opt;
202 203
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); 204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s);
204 regex = GNUNET_strdup (""); 205 regex = GNUNET_strdup("");
205 opt = GNUNET_NO; 206 opt = GNUNET_NO;
206 for (i = 0; i < ctx->size; i++) 207 for (i = 0; i < ctx->size; i++)
207 {
208 p = ctx->children[i];
209 if (NULL == p)
210 continue;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "adding '%s' to innner %s\n",
213 p->s, ctx->s);
214 s = regex_combine (p);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s);
216 if (strlen(s) == 0)
217 { 208 {
218 opt = GNUNET_YES; 209 p = ctx->children[i];
219 } 210 if (NULL == p)
220 else 211 continue;
221 { 212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
222 GNUNET_asprintf (&tmp, "%s%s|", regex, s); 213 "adding '%s' to innner %s\n",
223 GNUNET_free_non_null (regex); 214 p->s, ctx->s);
224 regex = tmp; 215 s = regex_combine(p);
216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s);
217 if (strlen(s) == 0)
218 {
219 opt = GNUNET_YES;
220 }
221 else
222 {
223 GNUNET_asprintf(&tmp, "%s%s|", regex, s);
224 GNUNET_free_non_null(regex);
225 regex = tmp;
226 }
227 GNUNET_free_non_null(s);
228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s);
225 } 229 }
226 GNUNET_free_non_null (s);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s);
228 }
229 230
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); 231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex);
231 len = strlen (regex); 232 len = strlen(regex);
232 if (0 == len) 233 if (0 == len)
233 { 234 {
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); 235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n");
235 GNUNET_free (regex); 236 GNUNET_free(regex);
236 return NULL == ctx->s ? NULL : GNUNET_strdup (ctx->s); 237 return NULL == ctx->s ? NULL : GNUNET_strdup(ctx->s);
237 } 238 }
238 239
239 if ('|' == regex[len - 1]) 240 if ('|' == regex[len - 1])
240 regex[len - 1] = '\0'; 241 regex[len - 1] = '\0';
241 242
242 if (NULL != ctx->s) 243 if (NULL != ctx->s)
243 { 244 {
244 if (opt) 245 if (opt)
245 GNUNET_asprintf (&s, "%s(%s)?", ctx->s, regex); 246 GNUNET_asprintf(&s, "%s(%s)?", ctx->s, regex);
246 else 247 else
247 GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex); 248 GNUNET_asprintf(&s, "%s(%s)", ctx->s, regex);
248 GNUNET_free (regex); 249 GNUNET_free(regex);
249 regex = s; 250 regex = s;
250 } 251 }
251 252
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); 253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex);
253 return regex; 254 return regex;
254} 255}
255 256
@@ -263,22 +264,22 @@ regex_combine (struct RegexCombineCtx *ctx)
263 * @return Number of characters of matching prefix. 264 * @return Number of characters of matching prefix.
264 */ 265 */
265static unsigned int 266static unsigned int
266get_prefix_length (const char *s1, const char *s2) 267get_prefix_length(const char *s1, const char *s2)
267{ 268{
268 unsigned int l1; 269 unsigned int l1;
269 unsigned int l2; 270 unsigned int l2;
270 unsigned int limit; 271 unsigned int limit;
271 unsigned int i; 272 unsigned int i;
272 273
273 l1 = strlen (s1); 274 l1 = strlen(s1);
274 l2 = strlen (s2); 275 l2 = strlen(s2);
275 limit = l1 > l2 ? l2 : l1; 276 limit = l1 > l2 ? l2 : l1;
276 277
277 for (i = 0; i < limit; i++) 278 for (i = 0; i < limit; i++)
278 { 279 {
279 if (s1[i] != s2[i]) 280 if (s1[i] != s2[i])
280 return i; 281 return i;
281 } 282 }
282 return limit; 283 return limit;
283} 284}
284 285
@@ -293,7 +294,7 @@ get_prefix_length (const char *s1, const char *s2)
293 * @return Child with the longest prefix, NULL if no child matches. 294 * @return Child with the longest prefix, NULL if no child matches.
294 */ 295 */
295static struct RegexCombineCtx * 296static struct RegexCombineCtx *
296get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex) 297get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex)
297{ 298{
298 struct RegexCombineCtx *p; 299 struct RegexCombineCtx *p;
299 struct RegexCombineCtx *best; 300 struct RegexCombineCtx *best;
@@ -305,26 +306,26 @@ get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex)
305 best = NULL; 306 best = NULL;
306 307
307 for (i = 0; i < ctx->size; i++) 308 for (i = 0; i < ctx->size; i++)
308 {
309 p = ctx->children[i];
310 if (NULL == p)
311 continue;
312
313 l = get_prefix_length (p->s, regex);
314 if (l > best_l)
315 { 309 {
316 GNUNET_break (0 == best_l); 310 p = ctx->children[i];
317 best = p; 311 if (NULL == p)
318 best_l = l; 312 continue;
313
314 l = get_prefix_length(p->s, regex);
315 if (l > best_l)
316 {
317 GNUNET_break(0 == best_l);
318 best = p;
319 best_l = l;
320 }
319 } 321 }
320 }
321 return best; 322 return best;
322} 323}
323 324
324static void 325static void
325regex_add_multiple (struct RegexCombineCtx *ctx, 326regex_add_multiple(struct RegexCombineCtx *ctx,
326 const char *regex, 327 const char *regex,
327 struct RegexCombineCtx **children) 328 struct RegexCombineCtx **children)
328{ 329{
329 char tmp[2]; 330 char tmp[2];
330 long unsigned int i; 331 long unsigned int i;
@@ -333,43 +334,43 @@ regex_add_multiple (struct RegexCombineCtx *ctx,
333 unsigned int count; 334 unsigned int count;
334 335
335 if ('(' != regex[0]) 336 if ('(' != regex[0])
336 { 337 {
337 GNUNET_assert (0); 338 GNUNET_assert(0);
338 } 339 }
339 340
340 /* Does the regex cover *all* possible children? Then don't add any, 341 /* Does the regex cover *all* possible children? Then don't add any,
341 * as it will be covered by the post-regex "(a-z)*" 342 * as it will be covered by the post-regex "(a-z)*"
342 */ 343 */
343 l = strlen (regex); 344 l = strlen(regex);
344 count = 0; 345 count = 0;
345 for (i = 1UL; i < l; i++) 346 for (i = 1UL; i < l; i++)
346 {
347 if (regex[i] != '|' && regex[i] != ')')
348 { 347 {
349 count++; 348 if (regex[i] != '|' && regex[i] != ')')
349 {
350 count++;
351 }
350 } 352 }
351 }
352 if (count == ctx->size) 353 if (count == ctx->size)
353 { 354 {
354 return; 355 return;
355 } 356 }
356 357
357 /* Add every component as a child node */ 358 /* Add every component as a child node */
358 tmp[1] = '\0'; 359 tmp[1] = '\0';
359 for (i = 1UL; i < l; i++) 360 for (i = 1UL; i < l; i++)
360 {
361 if (regex[i] != '|' && regex[i] != ')')
362 { 361 {
363 tmp[0] = regex[i]; 362 if (regex[i] != '|' && regex[i] != ')')
364 newctx = new_regex_ctx(ctx->size); 363 {
365 newctx->s = GNUNET_strdup (tmp); 364 tmp[0] = regex[i];
366 if (children != NULL) 365 newctx = new_regex_ctx(ctx->size);
367 GNUNET_memcpy (newctx->children, 366 newctx->s = GNUNET_strdup(tmp);
368 children, 367 if (children != NULL)
369 sizeof (*children) * ctx->size); 368 GNUNET_memcpy(newctx->children,
370 ctx->children[c2i(tmp[0], ctx->size)] = newctx; 369 children,
370 sizeof(*children) * ctx->size);
371 ctx->children[c2i(tmp[0], ctx->size)] = newctx;
372 }
371 } 373 }
372 }
373} 374}
374 375
375/** 376/**
@@ -383,19 +384,19 @@ regex_add_multiple (struct RegexCombineCtx *ctx,
383 * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s 384 * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s
384 */ 385 */
385static void 386static void
386regex_split (struct RegexCombineCtx *ctx, 387regex_split(struct RegexCombineCtx *ctx,
387 unsigned int len, 388 unsigned int len,
388 unsigned int prefix_l) 389 unsigned int prefix_l)
389{ 390{
390 struct RegexCombineCtx *newctx; 391 struct RegexCombineCtx *newctx;
391 unsigned int idx; 392 unsigned int idx;
392 char *suffix; 393 char *suffix;
393 394
394 suffix = GNUNET_malloc (len - prefix_l + 1); 395 suffix = GNUNET_malloc(len - prefix_l + 1);
395 /* 396 /*
396 * We can use GNUNET_strlcpy because ctx->s is null-terminated 397 * We can use GNUNET_strlcpy because ctx->s is null-terminated
397 */ 398 */
398 GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1); 399 GNUNET_strlcpy(suffix, &ctx->s[prefix_l], len - prefix_l + 1);
399 400
400 /* Suffix saved, truncate current node so it only contains the prefix, 401 /* Suffix saved, truncate current node so it only contains the prefix,
401 * copy any children nodes to put as grandchildren and initialize new empty 402 * copy any children nodes to put as grandchildren and initialize new empty
@@ -405,21 +406,21 @@ regex_split (struct RegexCombineCtx *ctx,
405 406
406 /* If the suffix is an OR expression, add multiple children */ 407 /* If the suffix is an OR expression, add multiple children */
407 if ('(' == suffix[0]) 408 if ('(' == suffix[0])
408 { 409 {
409 struct RegexCombineCtx **tmp; 410 struct RegexCombineCtx **tmp;
410 411
411 tmp = ctx->children; 412 tmp = ctx->children;
412 ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size); 413 ctx->children = GNUNET_malloc(sizeof(*tmp) * ctx->size);
413 regex_add_multiple (ctx, suffix, tmp); 414 regex_add_multiple(ctx, suffix, tmp);
414 GNUNET_free (suffix); 415 GNUNET_free(suffix);
415 GNUNET_free (tmp); 416 GNUNET_free(tmp);
416 return; 417 return;
417 } 418 }
418 419
419 /* The suffix is a normal string, add as one node */ 420 /* The suffix is a normal string, add as one node */
420 newctx = new_regex_ctx (ctx->size); 421 newctx = new_regex_ctx(ctx->size);
421 newctx->s = suffix; 422 newctx->s = suffix;
422 move_children (newctx, ctx); 423 move_children(newctx, ctx);
423 idx = c2i(suffix[0], ctx->size); 424 idx = c2i(suffix[0], ctx->size);
424 ctx->children[idx] = newctx; 425 ctx->children[idx] = newctx;
425} 426}
@@ -432,7 +433,7 @@ regex_split (struct RegexCombineCtx *ctx,
432 * @param regex Regex to add. 433 * @param regex Regex to add.
433 */ 434 */
434static void 435static void
435regex_add (struct RegexCombineCtx *ctx, const char *regex) 436regex_add(struct RegexCombineCtx *ctx, const char *regex)
436{ 437{
437 struct RegexCombineCtx *p; 438 struct RegexCombineCtx *p;
438 struct RegexCombineCtx *newctx; 439 struct RegexCombineCtx *newctx;
@@ -443,54 +444,54 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex)
443 size_t len; 444 size_t len;
444 int idx; 445 int idx;
445 446
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447 "regex_add '%s' into '%s'\n", 448 "regex_add '%s' into '%s'\n",
448 regex, ctx->s); 449 regex, ctx->s);
449 l = strlen (regex); 450 l = strlen(regex);
450 if (0UL == l) 451 if (0UL == l)
451 return; 452 return;
452 453
453 /* If the regex is in the form of (a|b|c), add every character separately */ 454 /* If the regex is in the form of (a|b|c), add every character separately */
454 if ('(' == regex[0]) 455 if ('(' == regex[0])
455 { 456 {
456 regex_add_multiple (ctx, regex, NULL); 457 regex_add_multiple(ctx, regex, NULL);
457 return; 458 return;
458 } 459 }
459 460
460 p = get_longest_prefix (ctx, regex); 461 p = get_longest_prefix(ctx, regex);
461 if (NULL != p) 462 if (NULL != p)
462 {
463 /* There is some prefix match, reduce regex and try again */
464 prefix_l = get_prefix_length (p->s, regex);
465 rest_s = &p->s[prefix_l];
466 rest_r = &regex[prefix_l];
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l);
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r);
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s);
471 len = strlen (p->s);
472 if (prefix_l < len)
473 { 463 {
474 regex_split (p, len, prefix_l); 464 /* There is some prefix match, reduce regex and try again */
465 prefix_l = get_prefix_length(p->s, regex);
466 rest_s = &p->s[prefix_l];
467 rest_r = &regex[prefix_l];
468 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l);
469 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s);
470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r);
471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s);
472 len = strlen(p->s);
473 if (prefix_l < len)
474 {
475 regex_split(p, len, prefix_l);
476 }
477 regex_add(p, rest_r);
478 return;
475 } 479 }
476 regex_add (p, rest_r);
477 return;
478 }
479 480
480 /* There is no prefix match, add new */ 481 /* There is no prefix match, add new */
481 idx = c2i(regex[0], ctx->size); 482 idx = c2i(regex[0], ctx->size);
482 if (NULL == ctx->children[idx] && NULL != ctx->s) 483 if (NULL == ctx->children[idx] && NULL != ctx->s)
483 { 484 {
484 /* this was the end before, add empty string */ 485 /* this was the end before, add empty string */
485 newctx = new_regex_ctx (ctx->size); 486 newctx = new_regex_ctx(ctx->size);
486 newctx->s = GNUNET_strdup (""); 487 newctx->s = GNUNET_strdup("");
487 ctx->children[idx] = newctx; 488 ctx->children[idx] = newctx;
488 } 489 }
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); 490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " no match\n");
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); 491 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex);
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); 492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s);
492 newctx = new_regex_ctx(ctx->size); 493 newctx = new_regex_ctx(ctx->size);
493 newctx->s = GNUNET_strdup (regex); 494 newctx->s = GNUNET_strdup(regex);
494 ctx->children[idx] = newctx; 495 ctx->children[idx] = newctx;
495} 496}
496 497
@@ -501,7 +502,7 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex)
501 * @param ctx Context to free. 502 * @param ctx Context to free.
502 */ 503 */
503static void 504static void
504regex_ctx_destroy (struct RegexCombineCtx *ctx) 505regex_ctx_destroy(struct RegexCombineCtx *ctx)
505{ 506{
506 unsigned int i; 507 unsigned int i;
507 508
@@ -509,12 +510,12 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx)
509 return; 510 return;
510 511
511 for (i = 0; i < ctx->size; i++) 512 for (i = 0; i < ctx->size; i++)
512 { 513 {
513 regex_ctx_destroy (ctx->children[i]); 514 regex_ctx_destroy(ctx->children[i]);
514 } 515 }
515 GNUNET_free_non_null (ctx->s); /* 's' on root node is null */ 516 GNUNET_free_non_null(ctx->s); /* 's' on root node is null */
516 GNUNET_free (ctx->children); 517 GNUNET_free(ctx->children);
517 GNUNET_free (ctx); 518 GNUNET_free(ctx);
518} 519}
519 520
520 521
@@ -533,27 +534,27 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx)
533 * @return A string with a single regex that matches any of the original regexes 534 * @return A string with a single regex that matches any of the original regexes
534 */ 535 */
535char * 536char *
536REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size) 537REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size)
537{ 538{
538 unsigned int i; 539 unsigned int i;
539 char *combined; 540 char *combined;
540 const char *current; 541 const char *current;
541 struct RegexCombineCtx *ctx; 542 struct RegexCombineCtx *ctx;
542 543
543 ctx = new_regex_ctx (alphabet_size); 544 ctx = new_regex_ctx(alphabet_size);
544 for (i = 0; regexes[i]; i++) 545 for (i = 0; regexes[i]; i++)
545 { 546 {
546 current = regexes[i]; 547 current = regexes[i];
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); 548 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current);
548 regex_add (ctx, current); 549 regex_add(ctx, current);
549 debugctx (ctx, 0); 550 debugctx(ctx, 0);
550 } 551 }
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); 552 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n");
552 debugctx (ctx, 0); 553 debugctx(ctx, 0);
553 554
554 combined = regex_combine (ctx); 555 combined = regex_combine(ctx);
555 556
556 regex_ctx_destroy (ctx); 557 regex_ctx_destroy(ctx);
557 558
558 return combined; 559 return combined;
559} 560}
@@ -569,7 +570,7 @@ REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size)
569 * @return A newly allocated, NULL terminated array of regexes. 570 * @return A newly allocated, NULL terminated array of regexes.
570 */ 571 */
571char ** 572char **
572REGEX_TEST_read_from_file (const char *filename) 573REGEX_TEST_read_from_file(const char *filename)
573{ 574{
574 struct GNUNET_DISK_FileHandle *f; 575 struct GNUNET_DISK_FileHandle *f;
575 unsigned int nr; 576 unsigned int nr;
@@ -580,54 +581,55 @@ REGEX_TEST_read_from_file (const char *filename)
580 char *regex; 581 char *regex;
581 char **regexes; 582 char **regexes;
582 583
583 f = GNUNET_DISK_file_open (filename, 584 f = GNUNET_DISK_file_open(filename,
584 GNUNET_DISK_OPEN_READ, 585 GNUNET_DISK_OPEN_READ,
585 GNUNET_DISK_PERM_NONE); 586 GNUNET_DISK_PERM_NONE);
586 if (NULL == f) 587 if (NULL == f)
587 { 588 {
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 589 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
589 "Can't open file %s for reading\n", filename); 590 "Can't open file %s for reading\n", filename);
590 return NULL; 591 return NULL;
591 } 592 }
592 if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size)) 593 if (GNUNET_OK != GNUNET_DISK_file_handle_size(f, &size))
593 { 594 {
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
595 "Can't get size of file %s\n", filename); 596 "Can't get size of file %s\n", filename);
596 GNUNET_DISK_file_close (f); 597 GNUNET_DISK_file_close(f);
597 return NULL; 598 return NULL;
598 } 599 }
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
600 "using file %s, size %llu\n", 601 "using file %s, size %llu\n",
601 filename, (unsigned long long) size); 602 filename, (unsigned long long)size);
602 603
603 buffer = GNUNET_malloc (size + 1); 604 buffer = GNUNET_malloc(size + 1);
604 GNUNET_DISK_file_read (f, buffer, size); 605 GNUNET_DISK_file_read(f, buffer, size);
605 GNUNET_DISK_file_close (f); 606 GNUNET_DISK_file_close(f);
606 regexes = GNUNET_malloc (sizeof (char *)); 607 regexes = GNUNET_malloc(sizeof(char *));
607 nr = 1; 608 nr = 1;
608 offset = 0; 609 offset = 0;
609 regex = NULL; 610 regex = NULL;
610 do 611 do
611 { 612 {
612 if (NULL == regex) 613 if (NULL == regex)
613 regex = GNUNET_malloc (size + 1); 614 regex = GNUNET_malloc(size + 1);
614 len = (size_t) sscanf (&buffer[offset], "%s", regex); 615 len = (size_t)sscanf(&buffer[offset], "%s", regex);
615 if (0 == len) 616 if (0 == len)
616 break; 617 break;
617 len = strlen (regex); 618 len = strlen(regex);
618 offset += len + 1; 619 offset += len + 1;
619 if (len < 1) 620 if (len < 1)
620 continue; 621 continue;
621 regex[len] = '\0'; 622 regex[len] = '\0';
622 regex = GNUNET_realloc (regex, len + 1); 623 regex = GNUNET_realloc(regex, len + 1);
623 GNUNET_array_grow (regexes, nr, nr + 1); 624 GNUNET_array_grow(regexes, nr, nr + 1);
624 GNUNET_assert (NULL == regexes[nr - 2]); 625 GNUNET_assert(NULL == regexes[nr - 2]);
625 regexes[nr - 2] = regex; 626 regexes[nr - 2] = regex;
626 regexes[nr - 1] = NULL; 627 regexes[nr - 1] = NULL;
627 regex = NULL; 628 regex = NULL;
628 } while (offset < size); 629 }
629 GNUNET_free_non_null (regex); 630 while (offset < size);
630 GNUNET_free (buffer); 631 GNUNET_free_non_null(regex);
632 GNUNET_free(buffer);
631 633
632 return regexes; 634 return regexes;
633} 635}
@@ -639,13 +641,13 @@ REGEX_TEST_read_from_file (const char *filename)
639 * @param regexes NULL-terminated array of regexes. 641 * @param regexes NULL-terminated array of regexes.
640 */ 642 */
641void 643void
642REGEX_TEST_free_from_file (char **regexes) 644REGEX_TEST_free_from_file(char **regexes)
643{ 645{
644 unsigned int i; 646 unsigned int i;
645 647
646 for (i = 0; regexes[i]; i++) 648 for (i = 0; regexes[i]; i++)
647 GNUNET_free (regexes[i]); 649 GNUNET_free(regexes[i]);
648 GNUNET_free (regexes); 650 GNUNET_free(regexes);
649} 651}
650 652
651/* end of regex_test_lib.c */ 653/* end of regex_test_lib.c */
diff --git a/src/regex/regex_test_lib.h b/src/regex/regex_test_lib.h
index 226a0fbb6..fe4b30498 100644
--- a/src/regex/regex_test_lib.h
+++ b/src/regex/regex_test_lib.h
@@ -11,7 +11,7 @@
11 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details. 13 * Affero General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Affero General Public License 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/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -54,7 +54,7 @@ extern "C"
54 * @return A string with a single regex that matches any of the original regexes 54 * @return A string with a single regex that matches any of the original regexes
55 */ 55 */
56char * 56char *
57REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size); 57REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size);
58 58
59 59
60/** 60/**
@@ -67,7 +67,7 @@ REGEX_TEST_combine (char * const regexes[], unsigned int alphabet_size);
67 * @return A newly allocated, NULL terminated array of regexes. 67 * @return A newly allocated, NULL terminated array of regexes.
68 */ 68 */
69char ** 69char **
70REGEX_TEST_read_from_file (const char *filename); 70REGEX_TEST_read_from_file(const char *filename);
71 71
72 72
73/** 73/**
@@ -76,7 +76,7 @@ REGEX_TEST_read_from_file (const char *filename);
76 * @param regexes NULL-terminated array of regexes. 76 * @param regexes NULL-terminated array of regexes.
77 */ 77 */
78void 78void
79REGEX_TEST_free_from_file (char **regexes); 79REGEX_TEST_free_from_file(char **regexes);
80 80
81 81
82/** 82/**
@@ -93,7 +93,7 @@ REGEX_TEST_free_from_file (char **regexes);
93 * needs to be freed, otherwise. 93 * needs to be freed, otherwise.
94 */ 94 */
95char * 95char *
96REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str); 96REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str);
97 97
98 98
99/** 99/**
@@ -106,15 +106,14 @@ REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str);
106 * @return random string that needs to be freed. 106 * @return random string that needs to be freed.
107 */ 107 */
108char * 108char *
109REGEX_TEST_generate_random_string (size_t max_len); 109REGEX_TEST_generate_random_string(size_t max_len);
110 110
111 111
112/** 112/**
113 * Options for graph creation function 113 * Options for graph creation function
114 * REGEX_TEST_automaton_save_graph. 114 * REGEX_TEST_automaton_save_graph.
115 */ 115 */
116enum REGEX_TEST_GraphSavingOptions 116enum REGEX_TEST_GraphSavingOptions {
117{
118 /** 117 /**
119 * Default. Do nothing special. 118 * Default. Do nothing special.
120 */ 119 */
@@ -142,9 +141,9 @@ enum REGEX_TEST_GraphSavingOptions
142 * mode 141 * mode
143 */ 142 */
144void 143void
145REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a, 144REGEX_TEST_automaton_save_graph(struct REGEX_INTERNAL_Automaton *a,
146 const char *filename, 145 const char *filename,
147 enum REGEX_TEST_GraphSavingOptions options); 146 enum REGEX_TEST_GraphSavingOptions options);
148 147
149 148
150 149
diff --git a/src/regex/regex_test_random.c b/src/regex/regex_test_random.c
index 7fcdd1e63..9a1e9665a 100644
--- a/src/regex/regex_test_random.c
+++ b/src/regex/regex_test_random.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file src/regex/regex_test_random.c 21 * @file src/regex/regex_test_random.c
22 * @brief functions for creating random regular expressions and strings 22 * @brief functions for creating random regular expressions and strings
@@ -34,13 +34,13 @@
34 * @return random valid literal 34 * @return random valid literal
35 */ 35 */
36static char 36static char
37get_random_literal () 37get_random_literal()
38{ 38{
39 uint32_t ridx; 39 uint32_t ridx;
40 40
41 ridx = 41 ridx =
42 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 42 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
43 (uint32_t) strlen (ALLOWED_LITERALS)); 43 (uint32_t)strlen(ALLOWED_LITERALS));
44 44
45 return ALLOWED_LITERALS[ridx]; 45 return ALLOWED_LITERALS[ridx];
46} 46}
@@ -61,7 +61,7 @@ get_random_literal ()
61 * needs to be freed, otherwise. 61 * needs to be freed, otherwise.
62 */ 62 */
63char * 63char *
64REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str) 64REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str)
65{ 65{
66 char *rx; 66 char *rx;
67 char *rx_p; 67 char *rx_p;
@@ -80,56 +80,59 @@ REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
80 else 80 else
81 matching_strp = NULL; 81 matching_strp = NULL;
82 82
83 rx = GNUNET_malloc (rx_length + 1); 83 rx = GNUNET_malloc(rx_length + 1);
84 rx_p = rx; 84 rx_p = rx;
85 current_char = 0; 85 current_char = 0;
86 last_was_op = 1; 86 last_was_op = 1;
87 87
88 for (i = 0; i < rx_length; i++) 88 for (i = 0; i < rx_length; i++)
89 {
90 char_op_switch = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
91
92 if (0 == char_op_switch && !last_was_op)
93 { 89 {
94 last_was_op = 1; 90 char_op_switch = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
95 rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4); 91
96 92 if (0 == char_op_switch && !last_was_op)
97 switch (rx_op) 93 {
98 { 94 last_was_op = 1;
99 case 0: 95 rx_op = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 4);
100 current_char = '+'; 96
101 break; 97 switch (rx_op)
102 case 1: 98 {
103 current_char = '*'; 99 case 0:
104 break; 100 current_char = '+';
105 case 2: 101 break;
106 current_char = '?'; 102
107 break; 103 case 1:
108 case 3: 104 current_char = '*';
109 if (i < rx_length - 1) /* '|' cannot be at the end */ 105 break;
110 current_char = '|'; 106
111 else 107 case 2:
112 current_char = get_random_literal (); 108 current_char = '?';
113 break; 109 break;
114 } 110
111 case 3:
112 if (i < rx_length - 1) /* '|' cannot be at the end */
113 current_char = '|';
114 else
115 current_char = get_random_literal();
116 break;
117 }
118 }
119 else
120 {
121 current_char = get_random_literal();
122 last_was_op = 0;
123 }
124
125 if (NULL != matching_strp &&
126 (current_char != '+' && current_char != '*' && current_char != '?' &&
127 current_char != '|'))
128 {
129 *matching_strp = current_char;
130 matching_strp++;
131 }
132
133 *rx_p = current_char;
134 rx_p++;
115 } 135 }
116 else
117 {
118 current_char = get_random_literal ();
119 last_was_op = 0;
120 }
121
122 if (NULL != matching_strp &&
123 (current_char != '+' && current_char != '*' && current_char != '?' &&
124 current_char != '|'))
125 {
126 *matching_strp = current_char;
127 matching_strp++;
128 }
129
130 *rx_p = current_char;
131 rx_p++;
132 }
133 *rx_p = '\0'; 136 *rx_p = '\0';
134 if (NULL != matching_strp) 137 if (NULL != matching_strp)
135 *matching_strp = '\0'; 138 *matching_strp = '\0';
@@ -148,22 +151,22 @@ REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str)
148 * @return random string that needs to be freed. 151 * @return random string that needs to be freed.
149 */ 152 */
150char * 153char *
151REGEX_TEST_generate_random_string (size_t max_len) 154REGEX_TEST_generate_random_string(size_t max_len)
152{ 155{
153 unsigned int i; 156 unsigned int i;
154 char *str; 157 char *str;
155 size_t len; 158 size_t len;
156 159
157 if (1 > max_len) 160 if (1 > max_len)
158 return GNUNET_strdup (""); 161 return GNUNET_strdup("");
159 162
160 len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len); 163 len = (size_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max_len);
161 str = GNUNET_malloc (len + 1); 164 str = GNUNET_malloc(len + 1);
162 165
163 for (i = 0; i < len; i++) 166 for (i = 0; i < len; i++)
164 { 167 {
165 str[i] = get_random_literal (); 168 str[i] = get_random_literal();
166 } 169 }
167 170
168 str[i] = '\0'; 171 str[i] = '\0';
169 172
diff --git a/src/regex/test_regex_api.c b/src/regex/test_regex_api.c
index 7016bf9c4..2ee24800a 100644
--- a/src/regex/test_regex_api.c
+++ b/src/regex/test_regex_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_api.c 21 * @file regex/test_regex_api.c
22 * @brief base test case for regex api (and DHT functions) 22 * @brief base test case for regex api (and DHT functions)
@@ -31,12 +31,12 @@
31/** 31/**
32 * How long until we really give up on a particular testcase portion? 32 * How long until we really give up on a particular testcase portion?
33 */ 33 */
34#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600) 34#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 600)
35 35
36/** 36/**
37 * How long until we give up on any particular operation (and retry)? 37 * How long until we give up on any particular operation (and retry)?
38 */ 38 */
39#define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) 39#define BASE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
40 40
41 41
42static struct GNUNET_REGEX_Announcement *a; 42static struct GNUNET_REGEX_Announcement *a;
@@ -49,25 +49,25 @@ static struct GNUNET_SCHEDULER_Task *die_task;
49 49
50 50
51static void 51static void
52end (void *cls) 52end(void *cls)
53{ 53{
54 die_task = NULL; 54 die_task = NULL;
55 GNUNET_REGEX_announce_cancel (a); 55 GNUNET_REGEX_announce_cancel(a);
56 a = NULL; 56 a = NULL;
57 GNUNET_REGEX_search_cancel (s); 57 GNUNET_REGEX_search_cancel(s);
58 s = NULL; 58 s = NULL;
59 ok = 0; 59 ok = 0;
60} 60}
61 61
62 62
63static void 63static void
64end_badly () 64end_badly()
65{ 65{
66 die_task = NULL; 66 die_task = NULL;
67 fprintf (stderr, "%s", "Testcase failed (timeout).\n"); 67 fprintf(stderr, "%s", "Testcase failed (timeout).\n");
68 GNUNET_REGEX_announce_cancel (a); 68 GNUNET_REGEX_announce_cancel(a);
69 a = NULL; 69 a = NULL;
70 GNUNET_REGEX_search_cancel (s); 70 GNUNET_REGEX_search_cancel(s);
71 s = NULL; 71 s = NULL;
72 ok = 1; 72 ok = 1;
73} 73}
@@ -84,44 +84,44 @@ end_badly ()
84 * @param put_path_length Length of the put_path. 84 * @param put_path_length Length of the put_path.
85 */ 85 */
86static void 86static void
87found_cb (void *cls, 87found_cb(void *cls,
88 const struct GNUNET_PeerIdentity *id, 88 const struct GNUNET_PeerIdentity *id,
89 const struct GNUNET_PeerIdentity *get_path, 89 const struct GNUNET_PeerIdentity *get_path,
90 unsigned int get_path_length, 90 unsigned int get_path_length,
91 const struct GNUNET_PeerIdentity *put_path, 91 const struct GNUNET_PeerIdentity *put_path,
92 unsigned int put_path_length) 92 unsigned int put_path_length)
93{ 93{
94 GNUNET_SCHEDULER_cancel (die_task); 94 GNUNET_SCHEDULER_cancel(die_task);
95 die_task = 95 die_task =
96 GNUNET_SCHEDULER_add_now (&end, NULL); 96 GNUNET_SCHEDULER_add_now(&end, NULL);
97} 97}
98 98
99 99
100static void 100static void
101run (void *cls, 101run(void *cls,
102 const struct GNUNET_CONFIGURATION_Handle *cfg, 102 const struct GNUNET_CONFIGURATION_Handle *cfg,
103 struct GNUNET_TESTING_Peer *peer) 103 struct GNUNET_TESTING_Peer *peer)
104{ 104{
105 die_task = 105 die_task =
106 GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, 106 GNUNET_SCHEDULER_add_delayed(TOTAL_TIMEOUT,
107 &end_badly, NULL); 107 &end_badly, NULL);
108 a = GNUNET_REGEX_announce (cfg, 108 a = GNUNET_REGEX_announce(cfg,
109 "my long prefix - hello world(0|1)*", 109 "my long prefix - hello world(0|1)*",
110 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 110 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
111 5), 111 5),
112 1); 112 1);
113 s = GNUNET_REGEX_search (cfg, 113 s = GNUNET_REGEX_search(cfg,
114 "my long prefix - hello world0101", 114 "my long prefix - hello world0101",
115 &found_cb, NULL); 115 &found_cb, NULL);
116} 116}
117 117
118 118
119int 119int
120main (int argc, char *argv[]) 120main(int argc, char *argv[])
121{ 121{
122 if (0 != GNUNET_TESTING_peer_run ("test-regex-api", 122 if (0 != GNUNET_TESTING_peer_run("test-regex-api",
123 "test_regex_api_data.conf", 123 "test_regex_api_data.conf",
124 &run, NULL)) 124 &run, NULL))
125 return 1; 125 return 1;
126 return ok; 126 return ok;
127} 127}
diff --git a/src/regex/test_regex_eval_api.c b/src/regex/test_regex_eval_api.c
index f8565b380..88a05f912 100644
--- a/src/regex/test_regex_eval_api.c
+++ b/src/regex/test_regex_eval_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_eval_api.c 21 * @file regex/test_regex_eval_api.c
22 * @brief test for regex.c 22 * @brief test for regex.c
@@ -29,14 +29,12 @@
29#include "regex_test_lib.h" 29#include "regex_test_lib.h"
30#include "regex_internal.h" 30#include "regex_internal.h"
31 31
32enum Match_Result 32enum Match_Result {
33{
34 match = 0, 33 match = 0,
35 nomatch = 1 34 nomatch = 1
36}; 35};
37 36
38struct Regex_String_Pair 37struct Regex_String_Pair {
39{
40 char *regex; 38 char *regex;
41 int string_count; 39 int string_count;
42 char *strings[20]; 40 char *strings[20];
@@ -57,8 +55,8 @@ struct Regex_String_Pair
57 * @return 0 on success, non 0 otherwise. 55 * @return 0 on success, non 0 otherwise.
58 */ 56 */
59int 57int
60test_random (unsigned int rx_length, unsigned int max_str_len, 58test_random(unsigned int rx_length, unsigned int max_str_len,
61 unsigned int str_count) 59 unsigned int str_count)
62{ 60{
63 unsigned int i; 61 unsigned int i;
64 char *rand_rx; 62 char *rand_rx;
@@ -75,108 +73,108 @@ test_random (unsigned int rx_length, unsigned int max_str_len,
75 char *canonical_regex = NULL; 73 char *canonical_regex = NULL;
76 74
77 /* At least one string is needed for matching */ 75 /* At least one string is needed for matching */
78 GNUNET_assert (str_count > 0); 76 GNUNET_assert(str_count > 0);
79 /* The string should be at least as long as the regex itself */ 77 /* The string should be at least as long as the regex itself */
80 GNUNET_assert (max_str_len >= rx_length); 78 GNUNET_assert(max_str_len >= rx_length);
81 79
82 /* Generate random regex and a string that matches the regex */ 80 /* Generate random regex and a string that matches the regex */
83 matching_str = GNUNET_malloc (rx_length + 1); 81 matching_str = GNUNET_malloc(rx_length + 1);
84 rand_rx = REGEX_TEST_generate_random_regex (rx_length, matching_str); 82 rand_rx = REGEX_TEST_generate_random_regex(rx_length, matching_str);
85 83
86 /* Now match */ 84 /* Now match */
87 result = 0; 85 result = 0;
88 for (i = 0; i < str_count; i++) 86 for (i = 0; i < str_count; i++)
89 {
90 if (0 < i)
91 { 87 {
92 matching_str = REGEX_TEST_generate_random_string (max_str_len); 88 if (0 < i)
93 } 89 {
94 90 matching_str = REGEX_TEST_generate_random_string(max_str_len);
95 /* Match string using DFA */ 91 }
96 dfa = REGEX_INTERNAL_construct_dfa (rand_rx, strlen (rand_rx), 0); 92
97 if (NULL == dfa) 93 /* Match string using DFA */
98 { 94 dfa = REGEX_INTERNAL_construct_dfa(rand_rx, strlen(rand_rx), 0);
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); 95 if (NULL == dfa)
100 goto error; 96 {
101 } 97 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
102 98 goto error;
103 eval = REGEX_INTERNAL_eval (dfa, matching_str); 99 }
104 /* save the canonical regex for later comparison */ 100
105 canonical_regex = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa)); 101 eval = REGEX_INTERNAL_eval(dfa, matching_str);
106 REGEX_INTERNAL_automaton_destroy (dfa); 102 /* save the canonical regex for later comparison */
107 103 canonical_regex = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(dfa));
108 /* Match string using glibc regex */ 104 REGEX_INTERNAL_automaton_destroy(dfa);
109 if (0 != regcomp (&rx, rand_rx, REG_EXTENDED)) 105
110 { 106 /* Match string using glibc regex */
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 107 if (0 != regcomp(&rx, rand_rx, REG_EXTENDED))
112 "Could not compile regex using regcomp: %s\n", rand_rx); 108 {
113 goto error; 109 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
114 } 110 "Could not compile regex using regcomp: %s\n", rand_rx);
115 111 goto error;
116 eval_check = regexec (&rx, matching_str, 1, matchptr, 0); 112 }
117 regfree (&rx); 113
118 114 eval_check = regexec(&rx, matching_str, 1, matchptr, 0);
119 /* We only want to match the whole string, because that's what our DFA does, 115 regfree(&rx);
120 * too. */ 116
121 if (eval_check == 0 && 117 /* We only want to match the whole string, because that's what our DFA does,
122 (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str))) 118 * too. */
123 eval_check = 1; 119 if (eval_check == 0 &&
124 120 (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen(matching_str)))
125 /* Match canonical regex */ 121 eval_check = 1;
126 dfa = 122
127 REGEX_INTERNAL_construct_dfa (canonical_regex, strlen (canonical_regex), 123 /* Match canonical regex */
128 0); 124 dfa =
129 if (NULL == dfa) 125 REGEX_INTERNAL_construct_dfa(canonical_regex, strlen(canonical_regex),
130 { 126 0);
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); 127 if (NULL == dfa)
132 goto error; 128 {
133 } 129 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
134 130 goto error;
135 eval_canonical = REGEX_INTERNAL_eval (dfa, matching_str); 131 }
136 REGEX_INTERNAL_automaton_destroy (dfa); 132
137 133 eval_canonical = REGEX_INTERNAL_eval(dfa, matching_str);
138 if (0 != regcomp (&rx, canonical_regex, REG_EXTENDED)) 134 REGEX_INTERNAL_automaton_destroy(dfa);
139 { 135
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 136 if (0 != regcomp(&rx, canonical_regex, REG_EXTENDED))
141 "Could not compile regex using regcomp: %s\n", 137 {
142 canonical_regex); 138 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
143 goto error; 139 "Could not compile regex using regcomp: %s\n",
144 } 140 canonical_regex);
145 141 goto error;
146 eval_canonical_check = regexec (&rx, matching_str, 1, matchptr, 0); 142 }
147 regfree (&rx); 143
148 144 eval_canonical_check = regexec(&rx, matching_str, 1, matchptr, 0);
149 /* We only want to match the whole string, because that's what our DFA does, 145 regfree(&rx);
150 * too. */ 146
151 if (eval_canonical_check == 0 && 147 /* We only want to match the whole string, because that's what our DFA does,
152 (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str))) 148 * too. */
153 eval_canonical_check = 1; 149 if (eval_canonical_check == 0 &&
154 150 (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen(matching_str)))
155 /* compare results */ 151 eval_canonical_check = 1;
156 if (eval_check != eval || eval_canonical != eval_canonical_check) 152
157 { 153 /* compare results */
158 regerror (eval_check, &rx, error, sizeof error); 154 if (eval_check != eval || eval_canonical != eval_canonical_check)
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected result:\nregex: %s\ncanonical_regex: %s\n\ 155 {
156 regerror(eval_check, &rx, error, sizeof error);
157 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unexpected result:\nregex: %s\ncanonical_regex: %s\n\
160 string: %s\ngnunet regex: %i\nglibc regex: %i\n\ 158 string: %s\ngnunet regex: %i\nglibc regex: %i\n\
161 canonical regex: %i\ncanonical regex glibc: %i\n\ 159 canonical regex: %i\ncanonical regex glibc: %i\n\
162 glibc error: %s\n\n", rand_rx, canonical_regex, matching_str, 160 glibc error: %s\n\n", rand_rx, canonical_regex, matching_str,
163 eval, eval_check, eval_canonical, eval_canonical_check, error); 161 eval, eval_check, eval_canonical, eval_canonical_check, error);
164 result += 1; 162 result += 1;
163 }
164 GNUNET_free(canonical_regex);
165 GNUNET_free(matching_str);
166 canonical_regex = NULL;
167 matching_str = NULL;
165 } 168 }
166 GNUNET_free (canonical_regex);
167 GNUNET_free (matching_str);
168 canonical_regex = NULL;
169 matching_str = NULL;
170 }
171 169
172 GNUNET_free (rand_rx); 170 GNUNET_free(rand_rx);
173 171
174 return result; 172 return result;
175 173
176error: 174error:
177 GNUNET_free_non_null (matching_str); 175 GNUNET_free_non_null(matching_str);
178 GNUNET_free_non_null (rand_rx); 176 GNUNET_free_non_null(rand_rx);
179 GNUNET_free_non_null (canonical_regex); 177 GNUNET_free_non_null(canonical_regex);
180 return -1; 178 return -1;
181} 179}
182 180
@@ -193,8 +191,8 @@ error:
193 * @return 0 on successfull, non 0 otherwise 191 * @return 0 on successfull, non 0 otherwise
194 */ 192 */
195int 193int
196test_automaton (struct REGEX_INTERNAL_Automaton *a, regex_t * rx, 194test_automaton(struct REGEX_INTERNAL_Automaton *a, regex_t * rx,
197 struct Regex_String_Pair *rxstr) 195 struct Regex_String_Pair *rxstr)
198{ 196{
199 int result; 197 int result;
200 int eval; 198 int eval;
@@ -204,48 +202,48 @@ test_automaton (struct REGEX_INTERNAL_Automaton *a, regex_t * rx,
204 int i; 202 int i;
205 203
206 if (NULL == a) 204 if (NULL == a)
207 { 205 {
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n"); 206 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n");
209 return 1; 207 return 1;
210 } 208 }
211 209
212 result = 0; 210 result = 0;
213 211
214 for (i = 0; i < rxstr->string_count; i++) 212 for (i = 0; i < rxstr->string_count; i++)
215 {
216 eval = REGEX_INTERNAL_eval (a, rxstr->strings[i]);
217 eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
218
219 /* We only want to match the whole string, because that's what our DFA does,
220 * too. */
221 if (eval_check == 0 &&
222 (matchptr[0].rm_so != 0 ||
223 matchptr[0].rm_eo != strlen (rxstr->strings[i])))
224 eval_check = 1;
225
226 if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
227 || (rxstr->expected_results[i] == nomatch &&
228 (0 == eval || 0 == eval_check)))
229 { 213 {
230 result = 1; 214 eval = REGEX_INTERNAL_eval(a, rxstr->strings[i]);
231 regerror (eval_check, rx, error, sizeof error); 215 eval_check = regexec(rx, rxstr->strings[i], 1, matchptr, 0);
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 216
233 "Unexpected result:\nregex: %s\ncanonical_regex: %s\n" 217 /* We only want to match the whole string, because that's what our DFA does,
234 "string: %s\nexpected result: %i\n" 218 * too. */
235 "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n" 219 if (eval_check == 0 &&
236 "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex, 220 (matchptr[0].rm_so != 0 ||
237 REGEX_INTERNAL_get_canonical_regex (a), rxstr->strings[i], 221 matchptr[0].rm_eo != strlen(rxstr->strings[i])))
238 rxstr->expected_results[i], eval, eval_check, error, 222 eval_check = 1;
239 matchptr[0].rm_so, matchptr[0].rm_eo); 223
224 if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
225 || (rxstr->expected_results[i] == nomatch &&
226 (0 == eval || 0 == eval_check)))
227 {
228 result = 1;
229 regerror(eval_check, rx, error, sizeof error);
230 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
231 "Unexpected result:\nregex: %s\ncanonical_regex: %s\n"
232 "string: %s\nexpected result: %i\n"
233 "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n"
234 "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex,
235 REGEX_INTERNAL_get_canonical_regex(a), rxstr->strings[i],
236 rxstr->expected_results[i], eval, eval_check, error,
237 matchptr[0].rm_so, matchptr[0].rm_eo);
238 }
240 } 239 }
241 }
242 return result; 240 return result;
243} 241}
244 242
245int 243int
246main (int argc, char *argv[]) 244main(int argc, char *argv[])
247{ 245{
248 GNUNET_log_setup ("test-regex", "WARNING", NULL); 246 GNUNET_log_setup("test-regex", "WARNING", NULL);
249 247
250 struct REGEX_INTERNAL_Automaton *a; 248 struct REGEX_INTERNAL_Automaton *a;
251 regex_t rx; 249 regex_t rx;
@@ -256,71 +254,71 @@ main (int argc, char *argv[])
256 char *check_proof; 254 char *check_proof;
257 255
258 struct Regex_String_Pair rxstr[19] = { 256 struct Regex_String_Pair rxstr[19] = {
259 {"ab?(abcd)?", 5, 257 { "ab?(abcd)?", 5,
260 {"ababcd", "abab", "aabcd", "a", "abb"}, 258 { "ababcd", "abab", "aabcd", "a", "abb" },
261 {match, nomatch, match, match, nomatch}}, 259 { match, nomatch, match, match, nomatch } },
262 {"ab(c|d)+c*(a(b|c)d)+", 5, 260 { "ab(c|d)+c*(a(b|c)d)+", 5,
263 {"abcdcdcdcdddddabd", "abcd", 261 { "abcdcdcdcdddddabd", "abcd",
264 "abcddddddccccccccccccccccccccccccabdacdabd", 262 "abcddddddccccccccccccccccccccccccabdacdabd",
265 "abccccca", "abcdcdcdccdabdabd"}, 263 "abccccca", "abcdcdcdccdabdabd" },
266 {match, nomatch, match, nomatch, match}}, 264 { match, nomatch, match, nomatch, match } },
267 {"ab+c*(a(bx|c)d)+", 5, 265 { "ab+c*(a(bx|c)d)+", 5,
268 {"abcdcdcdcdddddabd", "abcd", 266 { "abcdcdcdcdddddabd", "abcd",
269 "abcddddddccccccccccccccccccccccccabdacdabd", 267 "abcddddddccccccccccccccccccccccccabdacdabd",
270 "abccccca", "abcdcdcdccdabdabd"}, 268 "abccccca", "abcdcdcdccdabdabd" },
271 {nomatch, nomatch, nomatch, nomatch, nomatch}}, 269 { nomatch, nomatch, nomatch, nomatch, nomatch } },
272 {"a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1, 270 { "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1,
273 {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"}, 271 { "kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg" },
274 {nomatch}}, 272 { nomatch } },
275 {"k|a+X*y+c|Q*e|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*g|N+V|t+L|P*j*3*9+X*h*J|J*6|b|E*i*f*R+S|Z|R|Y*Z|g*", 1, 273 { "k|a+X*y+c|Q*e|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*g|N+V|t+L|P*j*3*9+X*h*J|J*6|b|E*i*f*R+S|Z|R|Y*Z|g*", 1,
276 {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"}, 274 { "kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg" },
277 {nomatch}}, 275 { nomatch } },
278 {"F?W+m+2*6*c*s|P?U?a|B|y*i+t+A|V|6*C*7*e?Z*n*i|J?5+g?W*V?7*j?p?1|r?B?C+E+3+6*i+W*P?K?0|D+7?y*m+3?g?K?", 1, 276 { "F?W+m+2*6*c*s|P?U?a|B|y*i+t+A|V|6*C*7*e?Z*n*i|J?5+g?W*V?7*j?p?1|r?B?C+E+3+6*i+W*P?K?0|D+7?y*m+3?g?K?", 1,
279 {"osfjsodfonONONOnosndfsdnfsd"}, 277 { "osfjsodfonONONOnosndfsdnfsd" },
280 {nomatch}}, 278 { nomatch } },
281 {"V|M*o?x*p*d+h+b|E*m?h?Y*E*O?W*W*P+o?Z+H*M|I*q+C*a+5?5*9|b?z|G*y*k?R|p+u|8*h?B+l*H|e|L*O|1|F?v*0?5|C+", 1, 279 { "V|M*o?x*p*d+h+b|E*m?h?Y*E*O?W*W*P+o?Z+H*M|I*q+C*a+5?5*9|b?z|G*y*k?R|p+u|8*h?B+l*H|e|L*O|1|F?v*0?5|C+", 1,
282 {"VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C"}, 280 { "VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C" },
283 {nomatch}}, 281 { nomatch } },
284 {"(bla)*", 8, 282 { "(bla)*", 8,
285 {"", "bla", "blabla", "bl", "la", "b", "l", "a"}, 283 { "", "bla", "blabla", "bl", "la", "b", "l", "a" },
286 {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}}, 284 { match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch } },
287 {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8, 285 { "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
288 {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b", 286 { "ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b",
289 "l", 287 "l",
290 "a"}, 288 "a" },
291 {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}}, 289 { nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch } },
292 {"a|aa*a", 6, 290 { "a|aa*a", 6,
293 {"", "a", "aa", "aaa", "aaaa", "aaaaa"}, 291 { "", "a", "aa", "aaa", "aaaa", "aaaaa" },
294 {nomatch, match, match, match, match, match}}, 292 { nomatch, match, match, match, match, match } },
295 {"ab(c|d)+c*(a(b|c)+d)+(bla)+", 1, 293 { "ab(c|d)+c*(a(b|c)+d)+(bla)+", 1,
296 {"abcabdblaacdbla"}, 294 { "abcabdblaacdbla" },
297 {nomatch}}, 295 { nomatch } },
298 {"(ac|b)+", 8, 296 { "(ac|b)+", 8,
299 {"b", "bb", "ac", "", "acb", "bacbacac", "acacac", "abc"}, 297 { "b", "bb", "ac", "", "acb", "bacbacac", "acacac", "abc" },
300 {match, match, match, nomatch, match, match, match, nomatch}}, 298 { match, match, match, nomatch, match, match, match, nomatch } },
301 {"(ab|c)+", 7, 299 { "(ab|c)+", 7,
302 {"", "ab", "c", "abc", "ababcc", "acc", "abac"}, 300 { "", "ab", "c", "abc", "ababcc", "acc", "abac" },
303 {nomatch, match, match, match, match, nomatch, nomatch}}, 301 { nomatch, match, match, match, match, nomatch, nomatch } },
304 {"((j|2j)K|(j|2j)AK|(j|2j)(D|e|(j|2j)A(D|e))D*K)", 1, 302 { "((j|2j)K|(j|2j)AK|(j|2j)(D|e|(j|2j)A(D|e))D*K)", 1,
305 {"", "2j2jADK", "j2jADK"}, 303 { "", "2j2jADK", "j2jADK" },
306 {nomatch, match, match}}, 304 { nomatch, match, match } },
307 {"((j|2j)K|(j|2j)(D|e|((j|2j)j|(j|2j)2j)A(D|e))D*K|(j|2j)AK)", 2, 305 { "((j|2j)K|(j|2j)(D|e|((j|2j)j|(j|2j)2j)A(D|e))D*K|(j|2j)AK)", 2,
308 {"", "2j2jjADK", "j2jADK"}, 306 { "", "2j2jjADK", "j2jADK" },
309 {nomatch, match, match}}, 307 { nomatch, match, match } },
310 {"ab(c|d)+c*(a(b|c)d)+", 1, 308 { "ab(c|d)+c*(a(b|c)d)+", 1,
311 {"abacd"}, 309 { "abacd" },
312 {nomatch}}, 310 { nomatch } },
313 {"d|5kl", 1, 311 { "d|5kl", 1,
314 {"d5kl"}, 312 { "d5kl" },
315 {nomatch}}, 313 { nomatch } },
316 {"a()b", 1, 314 { "a()b", 1,
317 {"ab"}, 315 { "ab" },
318 {match}}, 316 { match } },
319 {"GNVPN-0001-PAD(001110101001001010(0|1)*|001110101001001010000(0|1)*|001110101001001010001(0|1)*|001110101001001010010(0|1)*|001110101001001010011(0|1)*|001110101001001010100(0|1)*|001110101001001010101(0|1)*|001110101001001010110(0|1)*|001110101001001010111(0|1)*|0011101010110110(0|1)*|001110101011011000000(0|1)*|001110101011011000001(0|1)*|001110101011011000010(0|1)*|001110101011011000011(0|1)*|001110101011011000100(0|1)*|001110101011011000101(0|1)*|001110101011011000110(0|1)*|001110101011011000111(0|1)*|001110101011011001000(0|1)*|001110101011011001001(0|1)*|001110101011011001010(0|1)*|001110101011011001011(0|1)*|001110101011011001100(0|1)*|001110101011011001101(0|1)*|001110101011011001110(0|1)*|001110101011011001111(0|1)*|001110101011011010000(0|1)*|001110101011011010001(0|1)*|001110101011011010010(0|1)*|001110101011011010011(0|1)*|001110101011011010100(0|1)*|001110101011011010101(0|1)*|001110101011011010110(0|1)*|001110101011011010111(0|1)*|001110101011011011000(0|1)*|001110101011011011001(0|1)*|001110101011011011010(0|1)*|001110101011011011011(0|1)*|001110101011011011100(0|1)*|001110101011011011101(0|1)*|001110101011011011110(0|1)*|001110101011011011111(0|1)*|0011101110111101(0|1)*|001110111011110100000(0|1)*|001110111011110100001(0|1)*|001110111011110100010(0|1)*|001110111011110100011(0|1)*|001110111011110100100(0|1)*|001110111011110100101(0|1)*|001110111011110100110(0|1)*|001110111011110100111(0|1)*|001110111011110101000(0|1)*|001110111011110101001(0|1)*|001110111011110101010(0|1)*|001110111011110101011(0|1)*|001110111011110101100(0|1)*|001110111011110101101(0|1)*|001110111011110101110(0|1)*|001110111011110101111(0|1)*|001110111011110110000(0|1)*|001110111011110110001(0|1)*|001110111011110110010(0|1)*|001110111011110110011(0|1)*|001110111011110110100(0|1)*|001110111011110110101(0|1)*|001110111011110110110(0|1)*|001110111011110110111(0|1)*|001110111011110111000(0|1)*|001110111011110111001(0|1)*|001110111011110111010(0|1)*|001110111011110111011(0|1)*|001110111011110111100(0|1)*|001110111011110111101(0|1)*|001110111011110111110(0|1)*|0111010001010110(0|1)*|011101000101011000000(0|1)*|011101000101011000001(0|1)*|011101000101011000010(0|1)*|011101000101011000011(0|1)*|011101000101011000100(0|1)*|011101000101011000101(0|1)*|011101000101011000110(0|1)*|011101000101011000111(0|1)*|011101000101011001000(0|1)*|011101000101011001001(0|1)*|011101000101011001010(0|1)*|011101000101011001011(0|1)*|011101000101011001100(0|1)*|011101000101011001101(0|1)*|011101000101011001110(0|1)*|011101000101011001111(0|1)*|011101000101011010000(0|1)*|011101000101011010001(0|1)*|011101000101011010010(0|1)*|011101000101011010011(0|1)*|011101000101011010100(0|1)*|011101000101011010101(0|1)*|011101000101011010110(0|1)*|011101000101011010111(0|1)*|011101000101011011000(0|1)*|011101000101011011001(0|1)*|011101000101011011010(0|1)*|011101000101011011011(0|1)*|011101000101011011100(0|1)*|011101000101011011101(0|1)*|011101000101011011110(0|1)*|011101000101011011111(0|1)*|0111010001010111(0|1)*|011101000101011100000(0|1)*|011101000101011100001(0|1)*|011101000101011100010(0|1)*|011101000101011100011(0|1)*|011101000101011100100(0|1)*|011101000101011100101(0|1)*|011101000101011100110(0|1)*|011101000101011100111(0|1)*|011101000101011101000(0|1)*|011101000101011101001(0|1)*|011101000101011101010(0|1)*|011101000101011101011(0|1)*|011101000101011101100(0|1)*|011101000101011101101(0|1)*|011101000101011101110(0|1)*|011101000101011101111(0|1)*|011101000101011110000(0|1)*|011101000101011110001(0|1)*|011101000101011110010(0|1)*|011101000101011110011(0|1)*|011101000101011110100(0|1)*|011101000101011110101(0|1)*|011101000101011110110(0|1)*|011101000101011110111(0|1)*|011101000101011111000(0|1)*|011101000101011111001(0|1)*|011101000101011111010(0|1)*|011101000101011111011(0|1)*|011101000101011111100(0|1)*|011101000101011111101(0|1)*|011101000101011111110(0|1)*|011101000101011111111(0|1)*|0111010001011000(0|1)*|011101000101100000000(0|1)*|011101000101100000001(0|1)*|011101000101100000010(0|1)*|011101000101100000011(0|1)*|011101000101100000100(0|1)*|011101000101100000101(0|1)*|011101000101100000110(0|1)*|011101000101100000111(0|1)*|011101000101100001000(0|1)*|011101000101100001001(0|1)*|011101000101100001010(0|1)*|011101000101100001011(0|1)*|011101000101100001100(0|1)*|011101000101100001101(0|1)*|011101000101100001110(0|1)*|011101000101100001111(0|1)*|011101000101100010000(0|1)*|011101000101100010001(0|1)*|011101000101100010010(0|1)*|011101000101100010011(0|1)*|011101000101100010100(0|1)*|011101000101100010101(0|1)*|011101000101100010110(0|1)*|011101000101100010111(0|1)*|011101000101100011000(0|1)*|011101000101100011001(0|1)*|011101000101100011010(0|1)*|011101000101100011011(0|1)*|011101000101100011100(0|1)*|011101000101100011101(0|1)*|011101000101100011110(0|1)*|011101000101100011111(0|1)*|01110100010110010(0|1)*|011101000101100100000(0|1)*|011101000101100100001(0|1)*|011101000101100100010(0|1)*|011101000101100100011(0|1)*|011101000101100100100(0|1)*|011101000101100100101(0|1)*|011101000101100100110(0|1)*|011101000101100100111(0|1)*|011101000101100101000(0|1)*|011101000101100101001(0|1)*|011101000101100101010(0|1)*|011101000101100101011(0|1)*|011101000101100101100(0|1)*|011101000101100101101(0|1)*|011101000101100101110(0|1)*|011101000101100101111(0|1)*|011101000101100101111000(0|1)*|1100101010011100(0|1)*|110010101001110000000(0|1)*|110010101001110000000001(0|1)*|110010101001110000000010(0|1)*|110010101001110000000110(0|1)*|110010101001110000001(0|1)*|110010101001110000001000(0|1)*|110010101001110000001001(0|1)*|110010101001110000001010(0|1)*|110010101001110000001011(0|1)*|110010101001110000001101(0|1)*|110010101001110000001110(0|1)*|110010101001110000010(0|1)*|110010101001110000011(0|1)*|110010101001110000100(0|1)*|110010101001110000101(0|1)*|110010101001110000110(0|1)*|110010101001110000111(0|1)*|110010101001110001000(0|1)*|110010101001110001001(0|1)*|110010101001110001010(0|1)*|110010101001110001011(0|1)*|110010101001110001100(0|1)*|110010101001110001101(0|1)*|110010101001110001110(0|1)*|110010101001110001111(0|1)*|110010101001110010000(0|1)*|110010101001110010001(0|1)*|110010101001110010010(0|1)*|110010101001110010011(0|1)*|110010101001110010100(0|1)*|110010101001110010101(0|1)*|110010101001110010110(0|1)*|110010101001110010111(0|1)*|110010101001110011000(0|1)*|110010101001110011001(0|1)*|110010101001110011010(0|1)*|110010101001110011011(0|1)*|110010101001110011100(0|1)*|110010101001110011101(0|1)*|110010101001110011110(0|1)*|110010101001110011111(0|1)*|1101101010111010(0|1)*|110110101011101000000(0|1)*|110110101011101000000001(0|1)*|110110101011101000001000(0|1)*|110110101011101000001001(0|1)*|110110101011101000001010(0|1)*|110110101011101000001011(0|1)*|110110101011101000001100(0|1)*|110110101011101000001110(0|1)*|110110101011101000001111(0|1)*|110110101011101000010(0|1)*|110110101011101000010000(0|1)*|110110101011101000010001(0|1)*|110110101011101000010010(0|1)*|110110101011101000010011(0|1)*|110110101011101000011(0|1)*|110110101011101000100(0|1)*|110110101011101000101(0|1)*|110110101011101000110(0|1)*|110110101011101000111(0|1)*|110110101011101001000(0|1)*|110110101011101001001(0|1)*|110110101011101001010(0|1)*|110110101011101001011(0|1)*|110110101011101001100(0|1)*|110110101011101001101(0|1)*|110110101011101001110(0|1)*|110110101011101001111(0|1)*|110110101011101010000(0|1)*|110110101011101010001(0|1)*|110110101011101010010(0|1)*|110110101011101010011(0|1)*|110110101011101010100(0|1)*|110110101011101010101(0|1)*|110110101011101010110(0|1)*|110110101011101010111(0|1)*|110110101011101011000(0|1)*|110110101011101011001(0|1)*|110110101011101011010(0|1)*|110110101011101011011(0|1)*|110110101011101011100(0|1)*|110110101011101011101(0|1)*|110110101011101011110(0|1)*|110110101011101011111(0|1)*|1101101011010100(0|1)*|110110101101010000000(0|1)*|110110101101010000001(0|1)*|110110101101010000010(0|1)*|110110101101010000011(0|1)*|110110101101010000100(0|1)*|110110101101010000101(0|1)*|110110101101010000110(0|1)*|110110101101010000111(0|1)*|110110101101010001000(0|1)*|110110101101010001001(0|1)*|110110101101010001010(0|1)*|110110101101010001011(0|1)*|110110101101010001100(0|1)*|110110101101010001101(0|1)*|110110101101010001110(0|1)*|110110101101010001111(0|1)*|110110101101010010000(0|1)*|110110101101010010001(0|1)*|110110101101010010010(0|1)*|110110101101010010011(0|1)*|110110101101010010100(0|1)*|1101101011010100101000(0|1)*|110110101101010010101(0|1)*|110110101101010010110(0|1)*|110110101101010010111(0|1)*|110110101101010011000(0|1)*|110110101101010011010(0|1)*|110110101101010011011(0|1)*|110110101101010011100(0|1)*|110110101101010011101(0|1)*|110110101101010011110(0|1)*|110110101101010011111(0|1)*|1101111010100100(0|1)*|110111101010010000000(0|1)*|110111101010010000001(0|1)*|110111101010010000010(0|1)*|110111101010010000011(0|1)*|110111101010010000100(0|1)*|110111101010010000101(0|1)*|110111101010010000110(0|1)*|110111101010010000111(0|1)*|110111101010010001000(0|1)*|110111101010010001001(0|1)*|110111101010010001010(0|1)*|110111101010010001011(0|1)*|110111101010010001100(0|1)*|110111101010010001101(0|1)*|110111101010010001110(0|1)*|110111101010010001111(0|1)*|110111101010010010000(0|1)*|110111101010010010001(0|1)*|110111101010010010010(0|1)*|110111101010010010011(0|1)*|110111101010010010100(0|1)*|110111101010010010101(0|1)*|110111101010010010110(0|1)*|110111101010010010111(0|1)*|110111101010010011000(0|1)*|110111101010010011001(0|1)*|110111101010010011010(0|1)*|110111101010010011011(0|1)*|110111101010010011100(0|1)*|110111101010010011101(0|1)*|110111101010010011110(0|1)*|110111101010010011111(0|1)*|11011110101001010(0|1)*|110111101010010100000(0|1)*|110111101010010100001(0|1)*|110111101010010100010(0|1)*|110111101010010100011(0|1)*|110111101010010100100(0|1)*|110111101010010100101(0|1)*|110111101010010100110(0|1)*|110111101010010100111(0|1)*|110111101010010101000(0|1)*|110111101010010101001(0|1)*|110111101010010101010(0|1)*|110111101010010101011(0|1)*|110111101010010101100(0|1)*|110111101010010101101(0|1)*|110111101010010101110(0|1)*|110111101010010101111(0|1)*)", 317 { "GNVPN-0001-PAD(001110101001001010(0|1)*|001110101001001010000(0|1)*|001110101001001010001(0|1)*|001110101001001010010(0|1)*|001110101001001010011(0|1)*|001110101001001010100(0|1)*|001110101001001010101(0|1)*|001110101001001010110(0|1)*|001110101001001010111(0|1)*|0011101010110110(0|1)*|001110101011011000000(0|1)*|001110101011011000001(0|1)*|001110101011011000010(0|1)*|001110101011011000011(0|1)*|001110101011011000100(0|1)*|001110101011011000101(0|1)*|001110101011011000110(0|1)*|001110101011011000111(0|1)*|001110101011011001000(0|1)*|001110101011011001001(0|1)*|001110101011011001010(0|1)*|001110101011011001011(0|1)*|001110101011011001100(0|1)*|001110101011011001101(0|1)*|001110101011011001110(0|1)*|001110101011011001111(0|1)*|001110101011011010000(0|1)*|001110101011011010001(0|1)*|001110101011011010010(0|1)*|001110101011011010011(0|1)*|001110101011011010100(0|1)*|001110101011011010101(0|1)*|001110101011011010110(0|1)*|001110101011011010111(0|1)*|001110101011011011000(0|1)*|001110101011011011001(0|1)*|001110101011011011010(0|1)*|001110101011011011011(0|1)*|001110101011011011100(0|1)*|001110101011011011101(0|1)*|001110101011011011110(0|1)*|001110101011011011111(0|1)*|0011101110111101(0|1)*|001110111011110100000(0|1)*|001110111011110100001(0|1)*|001110111011110100010(0|1)*|001110111011110100011(0|1)*|001110111011110100100(0|1)*|001110111011110100101(0|1)*|001110111011110100110(0|1)*|001110111011110100111(0|1)*|001110111011110101000(0|1)*|001110111011110101001(0|1)*|001110111011110101010(0|1)*|001110111011110101011(0|1)*|001110111011110101100(0|1)*|001110111011110101101(0|1)*|001110111011110101110(0|1)*|001110111011110101111(0|1)*|001110111011110110000(0|1)*|001110111011110110001(0|1)*|001110111011110110010(0|1)*|001110111011110110011(0|1)*|001110111011110110100(0|1)*|001110111011110110101(0|1)*|001110111011110110110(0|1)*|001110111011110110111(0|1)*|001110111011110111000(0|1)*|001110111011110111001(0|1)*|001110111011110111010(0|1)*|001110111011110111011(0|1)*|001110111011110111100(0|1)*|001110111011110111101(0|1)*|001110111011110111110(0|1)*|0111010001010110(0|1)*|011101000101011000000(0|1)*|011101000101011000001(0|1)*|011101000101011000010(0|1)*|011101000101011000011(0|1)*|011101000101011000100(0|1)*|011101000101011000101(0|1)*|011101000101011000110(0|1)*|011101000101011000111(0|1)*|011101000101011001000(0|1)*|011101000101011001001(0|1)*|011101000101011001010(0|1)*|011101000101011001011(0|1)*|011101000101011001100(0|1)*|011101000101011001101(0|1)*|011101000101011001110(0|1)*|011101000101011001111(0|1)*|011101000101011010000(0|1)*|011101000101011010001(0|1)*|011101000101011010010(0|1)*|011101000101011010011(0|1)*|011101000101011010100(0|1)*|011101000101011010101(0|1)*|011101000101011010110(0|1)*|011101000101011010111(0|1)*|011101000101011011000(0|1)*|011101000101011011001(0|1)*|011101000101011011010(0|1)*|011101000101011011011(0|1)*|011101000101011011100(0|1)*|011101000101011011101(0|1)*|011101000101011011110(0|1)*|011101000101011011111(0|1)*|0111010001010111(0|1)*|011101000101011100000(0|1)*|011101000101011100001(0|1)*|011101000101011100010(0|1)*|011101000101011100011(0|1)*|011101000101011100100(0|1)*|011101000101011100101(0|1)*|011101000101011100110(0|1)*|011101000101011100111(0|1)*|011101000101011101000(0|1)*|011101000101011101001(0|1)*|011101000101011101010(0|1)*|011101000101011101011(0|1)*|011101000101011101100(0|1)*|011101000101011101101(0|1)*|011101000101011101110(0|1)*|011101000101011101111(0|1)*|011101000101011110000(0|1)*|011101000101011110001(0|1)*|011101000101011110010(0|1)*|011101000101011110011(0|1)*|011101000101011110100(0|1)*|011101000101011110101(0|1)*|011101000101011110110(0|1)*|011101000101011110111(0|1)*|011101000101011111000(0|1)*|011101000101011111001(0|1)*|011101000101011111010(0|1)*|011101000101011111011(0|1)*|011101000101011111100(0|1)*|011101000101011111101(0|1)*|011101000101011111110(0|1)*|011101000101011111111(0|1)*|0111010001011000(0|1)*|011101000101100000000(0|1)*|011101000101100000001(0|1)*|011101000101100000010(0|1)*|011101000101100000011(0|1)*|011101000101100000100(0|1)*|011101000101100000101(0|1)*|011101000101100000110(0|1)*|011101000101100000111(0|1)*|011101000101100001000(0|1)*|011101000101100001001(0|1)*|011101000101100001010(0|1)*|011101000101100001011(0|1)*|011101000101100001100(0|1)*|011101000101100001101(0|1)*|011101000101100001110(0|1)*|011101000101100001111(0|1)*|011101000101100010000(0|1)*|011101000101100010001(0|1)*|011101000101100010010(0|1)*|011101000101100010011(0|1)*|011101000101100010100(0|1)*|011101000101100010101(0|1)*|011101000101100010110(0|1)*|011101000101100010111(0|1)*|011101000101100011000(0|1)*|011101000101100011001(0|1)*|011101000101100011010(0|1)*|011101000101100011011(0|1)*|011101000101100011100(0|1)*|011101000101100011101(0|1)*|011101000101100011110(0|1)*|011101000101100011111(0|1)*|01110100010110010(0|1)*|011101000101100100000(0|1)*|011101000101100100001(0|1)*|011101000101100100010(0|1)*|011101000101100100011(0|1)*|011101000101100100100(0|1)*|011101000101100100101(0|1)*|011101000101100100110(0|1)*|011101000101100100111(0|1)*|011101000101100101000(0|1)*|011101000101100101001(0|1)*|011101000101100101010(0|1)*|011101000101100101011(0|1)*|011101000101100101100(0|1)*|011101000101100101101(0|1)*|011101000101100101110(0|1)*|011101000101100101111(0|1)*|011101000101100101111000(0|1)*|1100101010011100(0|1)*|110010101001110000000(0|1)*|110010101001110000000001(0|1)*|110010101001110000000010(0|1)*|110010101001110000000110(0|1)*|110010101001110000001(0|1)*|110010101001110000001000(0|1)*|110010101001110000001001(0|1)*|110010101001110000001010(0|1)*|110010101001110000001011(0|1)*|110010101001110000001101(0|1)*|110010101001110000001110(0|1)*|110010101001110000010(0|1)*|110010101001110000011(0|1)*|110010101001110000100(0|1)*|110010101001110000101(0|1)*|110010101001110000110(0|1)*|110010101001110000111(0|1)*|110010101001110001000(0|1)*|110010101001110001001(0|1)*|110010101001110001010(0|1)*|110010101001110001011(0|1)*|110010101001110001100(0|1)*|110010101001110001101(0|1)*|110010101001110001110(0|1)*|110010101001110001111(0|1)*|110010101001110010000(0|1)*|110010101001110010001(0|1)*|110010101001110010010(0|1)*|110010101001110010011(0|1)*|110010101001110010100(0|1)*|110010101001110010101(0|1)*|110010101001110010110(0|1)*|110010101001110010111(0|1)*|110010101001110011000(0|1)*|110010101001110011001(0|1)*|110010101001110011010(0|1)*|110010101001110011011(0|1)*|110010101001110011100(0|1)*|110010101001110011101(0|1)*|110010101001110011110(0|1)*|110010101001110011111(0|1)*|1101101010111010(0|1)*|110110101011101000000(0|1)*|110110101011101000000001(0|1)*|110110101011101000001000(0|1)*|110110101011101000001001(0|1)*|110110101011101000001010(0|1)*|110110101011101000001011(0|1)*|110110101011101000001100(0|1)*|110110101011101000001110(0|1)*|110110101011101000001111(0|1)*|110110101011101000010(0|1)*|110110101011101000010000(0|1)*|110110101011101000010001(0|1)*|110110101011101000010010(0|1)*|110110101011101000010011(0|1)*|110110101011101000011(0|1)*|110110101011101000100(0|1)*|110110101011101000101(0|1)*|110110101011101000110(0|1)*|110110101011101000111(0|1)*|110110101011101001000(0|1)*|110110101011101001001(0|1)*|110110101011101001010(0|1)*|110110101011101001011(0|1)*|110110101011101001100(0|1)*|110110101011101001101(0|1)*|110110101011101001110(0|1)*|110110101011101001111(0|1)*|110110101011101010000(0|1)*|110110101011101010001(0|1)*|110110101011101010010(0|1)*|110110101011101010011(0|1)*|110110101011101010100(0|1)*|110110101011101010101(0|1)*|110110101011101010110(0|1)*|110110101011101010111(0|1)*|110110101011101011000(0|1)*|110110101011101011001(0|1)*|110110101011101011010(0|1)*|110110101011101011011(0|1)*|110110101011101011100(0|1)*|110110101011101011101(0|1)*|110110101011101011110(0|1)*|110110101011101011111(0|1)*|1101101011010100(0|1)*|110110101101010000000(0|1)*|110110101101010000001(0|1)*|110110101101010000010(0|1)*|110110101101010000011(0|1)*|110110101101010000100(0|1)*|110110101101010000101(0|1)*|110110101101010000110(0|1)*|110110101101010000111(0|1)*|110110101101010001000(0|1)*|110110101101010001001(0|1)*|110110101101010001010(0|1)*|110110101101010001011(0|1)*|110110101101010001100(0|1)*|110110101101010001101(0|1)*|110110101101010001110(0|1)*|110110101101010001111(0|1)*|110110101101010010000(0|1)*|110110101101010010001(0|1)*|110110101101010010010(0|1)*|110110101101010010011(0|1)*|110110101101010010100(0|1)*|1101101011010100101000(0|1)*|110110101101010010101(0|1)*|110110101101010010110(0|1)*|110110101101010010111(0|1)*|110110101101010011000(0|1)*|110110101101010011010(0|1)*|110110101101010011011(0|1)*|110110101101010011100(0|1)*|110110101101010011101(0|1)*|110110101101010011110(0|1)*|110110101101010011111(0|1)*|1101111010100100(0|1)*|110111101010010000000(0|1)*|110111101010010000001(0|1)*|110111101010010000010(0|1)*|110111101010010000011(0|1)*|110111101010010000100(0|1)*|110111101010010000101(0|1)*|110111101010010000110(0|1)*|110111101010010000111(0|1)*|110111101010010001000(0|1)*|110111101010010001001(0|1)*|110111101010010001010(0|1)*|110111101010010001011(0|1)*|110111101010010001100(0|1)*|110111101010010001101(0|1)*|110111101010010001110(0|1)*|110111101010010001111(0|1)*|110111101010010010000(0|1)*|110111101010010010001(0|1)*|110111101010010010010(0|1)*|110111101010010010011(0|1)*|110111101010010010100(0|1)*|110111101010010010101(0|1)*|110111101010010010110(0|1)*|110111101010010010111(0|1)*|110111101010010011000(0|1)*|110111101010010011001(0|1)*|110111101010010011010(0|1)*|110111101010010011011(0|1)*|110111101010010011100(0|1)*|110111101010010011101(0|1)*|110111101010010011110(0|1)*|110111101010010011111(0|1)*|11011110101001010(0|1)*|110111101010010100000(0|1)*|110111101010010100001(0|1)*|110111101010010100010(0|1)*|110111101010010100011(0|1)*|110111101010010100100(0|1)*|110111101010010100101(0|1)*|110111101010010100110(0|1)*|110111101010010100111(0|1)*|110111101010010101000(0|1)*|110111101010010101001(0|1)*|110111101010010101010(0|1)*|110111101010010101011(0|1)*|110111101010010101100(0|1)*|110111101010010101101(0|1)*|110111101010010101110(0|1)*|110111101010010101111(0|1)*)",
320 2, 318 2,
321 {"GNVPN-0001-PAD1101111010100101011101010101010101", 319 { "GNVPN-0001-PAD1101111010100101011101010101010101",
322 "GNVPN-0001-PAD11001010100111000101101010101"}, 320 "GNVPN-0001-PAD11001010100111000101101010101" },
323 {match, match}} 321 { match, match } }
324 }; 322 };
325 323
326 check_nfa = 0; 324 check_nfa = 0;
@@ -328,39 +326,39 @@ main (int argc, char *argv[])
328 check_rand = 0; 326 check_rand = 0;
329 327
330 for (i = 0; i < 19; i++) 328 for (i = 0; i < 19; i++)
331 {
332 if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
333 { 329 {
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 330 if (0 != regcomp(&rx, rxstr[i].regex, REG_EXTENDED))
335 "Could not compile regex using regcomp()\n"); 331 {
336 return 1; 332 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
333 "Could not compile regex using regcomp()\n");
334 return 1;
335 }
336
337 /* NFA test */
338 a = REGEX_INTERNAL_construct_nfa(rxstr[i].regex, strlen(rxstr[i].regex));
339 check_nfa += test_automaton(a, &rx, &rxstr[i]);
340 REGEX_INTERNAL_automaton_destroy(a);
341
342 /* DFA test */
343 a = REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0);
344 check_dfa += test_automaton(a, &rx, &rxstr[i]);
345 check_proof = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(a));
346 REGEX_INTERNAL_automaton_destroy(a);
347
348 a = REGEX_INTERNAL_construct_dfa(check_proof, strlen(check_proof), 0);
349 check_dfa += test_automaton(a, &rx, &rxstr[i]);
350 REGEX_INTERNAL_automaton_destroy(a);
351 if (0 != check_dfa)
352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
353 GNUNET_free_non_null(check_proof);
354
355 regfree(&rx);
337 } 356 }
338 357
339 /* NFA test */
340 a = REGEX_INTERNAL_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
341 check_nfa += test_automaton (a, &rx, &rxstr[i]);
342 REGEX_INTERNAL_automaton_destroy (a);
343
344 /* DFA test */
345 a = REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
346 check_dfa += test_automaton (a, &rx, &rxstr[i]);
347 check_proof = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (a));
348 REGEX_INTERNAL_automaton_destroy (a);
349
350 a = REGEX_INTERNAL_construct_dfa (check_proof, strlen (check_proof), 0);
351 check_dfa += test_automaton (a, &rx, &rxstr[i]);
352 REGEX_INTERNAL_automaton_destroy (a);
353 if (0 != check_dfa)
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
355 GNUNET_free_non_null (check_proof);
356
357 regfree (&rx);
358 }
359
360 /* Random tests */ 358 /* Random tests */
361 srand (time (NULL)); 359 srand(time(NULL));
362 for (i = 0; i < 20; i++) 360 for (i = 0; i < 20; i++)
363 check_rand += test_random (50, 60, 10); 361 check_rand += test_random(50, 60, 10);
364 362
365 return check_nfa + check_dfa + check_rand; 363 return check_nfa + check_dfa + check_rand;
366} 364}
diff --git a/src/regex/test_regex_graph_api.c b/src/regex/test_regex_graph_api.c
index 92792c19f..356782dec 100644
--- a/src/regex/test_regex_graph_api.c
+++ b/src/regex/test_regex_graph_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_graph_api.c 21 * @file regex/test_regex_graph_api.c
22 * @brief test for regex_graph.c 22 * @brief test for regex_graph.c
@@ -39,39 +39,39 @@
39 * @return 0 if ok, non 0 on error. 39 * @return 0 if ok, non 0 on error.
40 */ 40 */
41static int 41static int
42filecheck (const char *filename) 42filecheck(const char *filename)
43{ 43{
44 int error = 0; 44 int error = 0;
45 FILE *fp; 45 FILE *fp;
46 46
47 /* Check if file was created and delete it again */ 47 /* Check if file was created and delete it again */
48 if (NULL == (fp = fopen (filename, "r"))) 48 if (NULL == (fp = fopen(filename, "r")))
49 { 49 {
50 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename); 50 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
51 return 1; 51 return 1;
52 } 52 }
53 53
54 GNUNET_break (0 == fseek (fp, 0L, SEEK_END)); 54 GNUNET_break(0 == fseek(fp, 0L, SEEK_END));
55 if (1 > ftell (fp)) 55 if (1 > ftell(fp))
56 { 56 {
57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 57 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
58 "Graph writing failed, got empty file (%s)!\n", filename); 58 "Graph writing failed, got empty file (%s)!\n", filename);
59 error = 2; 59 error = 2;
60 } 60 }
61 61
62 GNUNET_assert (0 == fclose (fp)); 62 GNUNET_assert(0 == fclose(fp));
63 63
64 if (!KEEP_FILES) 64 if (!KEEP_FILES)
65 { 65 {
66 if (0 != unlink (filename)) 66 if (0 != unlink(filename))
67 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", filename); 67 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "unlink", filename);
68 } 68 }
69 return error; 69 return error;
70} 70}
71 71
72 72
73int 73int
74main (int argc, char *argv[]) 74main(int argc, char *argv[])
75{ 75{
76 int error; 76 int error;
77 struct REGEX_INTERNAL_Automaton *a; 77 struct REGEX_INTERNAL_Automaton *a;
@@ -93,66 +93,65 @@ main (int argc, char *argv[])
93 "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd" 93 "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd"
94 }; 94 };
95 95
96 GNUNET_log_setup ("test-regex", "WARNING", NULL); 96 GNUNET_log_setup("test-regex", "WARNING", NULL);
97 error = 0; 97 error = 0;
98 for (i = 0; i < 12; i++) 98 for (i = 0; i < 12; i++)
99 { 99 {
100 /* Check NFA graph creation */ 100 /* Check NFA graph creation */
101 a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); 101 a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i]));
102 REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); 102 REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT);
103 REGEX_INTERNAL_automaton_destroy (a); 103 REGEX_INTERNAL_automaton_destroy(a);
104 error += filecheck (filename); 104 error += filecheck(filename);
105 105
106 a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); 106 a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i]));
107 REGEX_TEST_automaton_save_graph (a, filename, 107 REGEX_TEST_automaton_save_graph(a, filename,
108 REGEX_TEST_GRAPH_DEFAULT | 108 REGEX_TEST_GRAPH_DEFAULT |
109 REGEX_TEST_GRAPH_VERBOSE); 109 REGEX_TEST_GRAPH_VERBOSE);
110 REGEX_INTERNAL_automaton_destroy (a); 110 REGEX_INTERNAL_automaton_destroy(a);
111 error += filecheck (filename); 111 error += filecheck(filename);
112 112
113 a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); 113 a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i]));
114 REGEX_TEST_automaton_save_graph (a, filename, 114 REGEX_TEST_automaton_save_graph(a, filename,
115 REGEX_TEST_GRAPH_DEFAULT | 115 REGEX_TEST_GRAPH_DEFAULT |
116 REGEX_TEST_GRAPH_COLORING); 116 REGEX_TEST_GRAPH_COLORING);
117 REGEX_INTERNAL_automaton_destroy (a); 117 REGEX_INTERNAL_automaton_destroy(a);
118 error += filecheck (filename); 118 error += filecheck(filename);
119 119
120 a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); 120 a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i]));
121 REGEX_TEST_automaton_save_graph (a, filename, 121 REGEX_TEST_automaton_save_graph(a, filename,
122 REGEX_TEST_GRAPH_DEFAULT | 122 REGEX_TEST_GRAPH_DEFAULT |
123 REGEX_TEST_GRAPH_VERBOSE | 123 REGEX_TEST_GRAPH_VERBOSE |
124 REGEX_TEST_GRAPH_COLORING); 124 REGEX_TEST_GRAPH_COLORING);
125 REGEX_INTERNAL_automaton_destroy (a); 125 REGEX_INTERNAL_automaton_destroy(a);
126 error += filecheck (filename); 126 error += filecheck(filename);
127 127
128 128
129 /* Check DFA graph creation */ 129 /* Check DFA graph creation */
130 a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); 130 a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0);
131 REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); 131 REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT);
132 REGEX_INTERNAL_automaton_destroy (a); 132 REGEX_INTERNAL_automaton_destroy(a);
133 error += filecheck (filename); 133 error += filecheck(filename);
134 134
135 a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); 135 a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0);
136 REGEX_TEST_automaton_save_graph (a, filename, 136 REGEX_TEST_automaton_save_graph(a, filename,
137 REGEX_TEST_GRAPH_DEFAULT | 137 REGEX_TEST_GRAPH_DEFAULT |
138 REGEX_TEST_GRAPH_VERBOSE); 138 REGEX_TEST_GRAPH_VERBOSE);
139 REGEX_INTERNAL_automaton_destroy (a); 139 REGEX_INTERNAL_automaton_destroy(a);
140 error += filecheck (filename); 140 error += filecheck(filename);
141 141
142 a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); 142 a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0);
143 REGEX_TEST_automaton_save_graph (a, filename, 143 REGEX_TEST_automaton_save_graph(a, filename,
144 REGEX_TEST_GRAPH_DEFAULT | 144 REGEX_TEST_GRAPH_DEFAULT |
145 REGEX_TEST_GRAPH_COLORING); 145 REGEX_TEST_GRAPH_COLORING);
146 REGEX_INTERNAL_automaton_destroy (a); 146 REGEX_INTERNAL_automaton_destroy(a);
147 error += filecheck (filename); 147 error += filecheck(filename);
148 148
149 149
150 a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 4); 150 a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 4);
151 REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); 151 REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT);
152 REGEX_INTERNAL_automaton_destroy (a); 152 REGEX_INTERNAL_automaton_destroy(a);
153 error += filecheck (filename); 153 error += filecheck(filename);
154 154 }
155 }
156 155
157 return error; 156 return error;
158} 157}
diff --git a/src/regex/test_regex_integration.c b/src/regex/test_regex_integration.c
index f264c4fae..b107c6d09 100644
--- a/src/regex/test_regex_integration.c
+++ b/src/regex/test_regex_integration.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_integration.c 21 * @file regex/test_regex_integration.c
22 * @brief base test case for regex integration with VPN; 22 * @brief base test case for regex integration with VPN;
@@ -35,12 +35,12 @@
35/** 35/**
36 * How long until we really give up on a particular testcase portion? 36 * How long until we really give up on a particular testcase portion?
37 */ 37 */
38#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600) 38#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 600)
39 39
40/** 40/**
41 * How long until we give up on any particular operation (and retry)? 41 * How long until we give up on any particular operation (and retry)?
42 */ 42 */
43#define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) 43#define BASE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
44 44
45 45
46static struct GNUNET_REGEX_Announcement *a4; 46static struct GNUNET_REGEX_Announcement *a4;
@@ -57,26 +57,26 @@ static struct GNUNET_SCHEDULER_Task *die_task;
57 57
58 58
59static void 59static void
60end (void *cls) 60end(void *cls)
61{ 61{
62 die_task = NULL; 62 die_task = NULL;
63 GNUNET_REGEX_announce_cancel (a4); 63 GNUNET_REGEX_announce_cancel(a4);
64 a4 = NULL; 64 a4 = NULL;
65 GNUNET_REGEX_search_cancel (s4); 65 GNUNET_REGEX_search_cancel(s4);
66 s4 = NULL; 66 s4 = NULL;
67 GNUNET_REGEX_announce_cancel (a6); 67 GNUNET_REGEX_announce_cancel(a6);
68 a6 = NULL; 68 a6 = NULL;
69 GNUNET_REGEX_search_cancel (s6); 69 GNUNET_REGEX_search_cancel(s6);
70 s6 = NULL; 70 s6 = NULL;
71 ok = 0; 71 ok = 0;
72} 72}
73 73
74 74
75static void 75static void
76end_badly () 76end_badly()
77{ 77{
78 fprintf (stderr, "%s", "Testcase failed (timeout).\n"); 78 fprintf(stderr, "%s", "Testcase failed (timeout).\n");
79 end (NULL); 79 end(NULL);
80 ok = 1; 80 ok = 1;
81} 81}
82 82
@@ -92,36 +92,36 @@ end_badly ()
92 * @param put_path_length Length of the @a put_path. 92 * @param put_path_length Length of the @a put_path.
93 */ 93 */
94static void 94static void
95found_cb (void *cls, 95found_cb(void *cls,
96 const struct GNUNET_PeerIdentity *id, 96 const struct GNUNET_PeerIdentity *id,
97 const struct GNUNET_PeerIdentity *get_path, 97 const struct GNUNET_PeerIdentity *get_path,
98 unsigned int get_path_length, 98 unsigned int get_path_length,
99 const struct GNUNET_PeerIdentity *put_path, 99 const struct GNUNET_PeerIdentity *put_path,
100 unsigned int put_path_length) 100 unsigned int put_path_length)
101{ 101{
102 const char *str = cls; 102 const char *str = cls;
103 static int found; 103 static int found;
104 104
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
106 "IPv%s-exit found\n", 106 "IPv%s-exit found\n",
107 str); 107 str);
108 if (0 == strcmp (str, "4")) 108 if (0 == strcmp(str, "4"))
109 found |= 4; 109 found |= 4;
110 if (0 == strcmp (str, "6")) 110 if (0 == strcmp(str, "6"))
111 found |= 2; 111 found |= 2;
112 if ((4|2) == found) 112 if ((4 | 2) == found)
113 { 113 {
114 GNUNET_SCHEDULER_cancel (die_task); 114 GNUNET_SCHEDULER_cancel(die_task);
115 die_task = 115 die_task =
116 GNUNET_SCHEDULER_add_now (&end, NULL); 116 GNUNET_SCHEDULER_add_now(&end, NULL);
117 } 117 }
118} 118}
119 119
120 120
121static void 121static void
122run (void *cls, 122run(void *cls,
123 const struct GNUNET_CONFIGURATION_Handle *cfg, 123 const struct GNUNET_CONFIGURATION_Handle *cfg,
124 struct GNUNET_TESTING_Peer *peer) 124 struct GNUNET_TESTING_Peer *peer)
125{ 125{
126 char rxstr4[GNUNET_TUN_IPV4_REGEXLEN]; 126 char rxstr4[GNUNET_TUN_IPV4_REGEXLEN];
127 char rxstr6[GNUNET_TUN_IPV6_REGEXLEN]; 127 char rxstr6[GNUNET_TUN_IPV6_REGEXLEN];
@@ -135,72 +135,72 @@ run (void *cls,
135 struct in6_addr i6; 135 struct in6_addr i6;
136 136
137 die_task = 137 die_task =
138 GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, 138 GNUNET_SCHEDULER_add_delayed(TOTAL_TIMEOUT,
139 &end_badly, NULL); 139 &end_badly, NULL);
140 GNUNET_assert (1 == 140 GNUNET_assert(1 ==
141 inet_pton (AF_INET, 141 inet_pton(AF_INET,
142 "127.0.0.1", 142 "127.0.0.1",
143 &i4)); 143 &i4));
144 GNUNET_assert (1 == 144 GNUNET_assert(1 ==
145 inet_pton (AF_INET6, 145 inet_pton(AF_INET6,
146 "::1:5", 146 "::1:5",
147 &i6)); 147 &i6));
148 GNUNET_TUN_ipv4toregexsearch (&i4, 148 GNUNET_TUN_ipv4toregexsearch(&i4,
149 8080, 149 8080,
150 rxstr4); 150 rxstr4);
151 GNUNET_TUN_ipv6toregexsearch (&i6, 151 GNUNET_TUN_ipv6toregexsearch(&i6,
152 8686, 152 8686,
153 rxstr6); 153 rxstr6);
154 GNUNET_asprintf (&ss4, 154 GNUNET_asprintf(&ss4,
155 "%s%s", 155 "%s%s",
156 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, 156 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
157 rxstr4); 157 rxstr4);
158 GNUNET_asprintf (&ss6, 158 GNUNET_asprintf(&ss6,
159 "%s%s", 159 "%s%s",
160 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, 160 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
161 rxstr6); 161 rxstr6);
162 p4r = GNUNET_TUN_ipv4policy2regex ("0.0.0.0/0:!25;"); 162 p4r = GNUNET_TUN_ipv4policy2regex("0.0.0.0/0:!25;");
163 p6r = GNUNET_TUN_ipv6policy2regex ("::/0:!25;"); 163 p6r = GNUNET_TUN_ipv6policy2regex("::/0:!25;");
164 GNUNET_asprintf (&p4, 164 GNUNET_asprintf(&p4,
165 "%s%s", 165 "%s%s",
166 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, 166 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
167 p4r); 167 p4r);
168 GNUNET_asprintf (&p6, 168 GNUNET_asprintf(&p6,
169 "%s%s", 169 "%s%s",
170 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, 170 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
171 p6r); 171 p6r);
172 GNUNET_free (p4r); 172 GNUNET_free(p4r);
173 GNUNET_free (p6r); 173 GNUNET_free(p6r);
174 a4 = GNUNET_REGEX_announce (cfg, 174 a4 = GNUNET_REGEX_announce(cfg,
175 p4, 175 p4,
176 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 176 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
177 5), 177 5),
178 1); 178 1);
179 a6 = GNUNET_REGEX_announce (cfg, 179 a6 = GNUNET_REGEX_announce(cfg,
180 p6, 180 p6,
181 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 181 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
182 5), 182 5),
183 1); 183 1);
184 GNUNET_free (p4); 184 GNUNET_free(p4);
185 GNUNET_free (p6); 185 GNUNET_free(p6);
186 186
187 s4 = GNUNET_REGEX_search (cfg, 187 s4 = GNUNET_REGEX_search(cfg,
188 ss4, 188 ss4,
189 &found_cb, "4"); 189 &found_cb, "4");
190 s6 = GNUNET_REGEX_search (cfg, 190 s6 = GNUNET_REGEX_search(cfg,
191 ss6, 191 ss6,
192 &found_cb, "6"); 192 &found_cb, "6");
193 GNUNET_free (ss4); 193 GNUNET_free(ss4);
194 GNUNET_free (ss6); 194 GNUNET_free(ss6);
195} 195}
196 196
197 197
198int 198int
199main (int argc, char *argv[]) 199main(int argc, char *argv[])
200{ 200{
201 if (0 != GNUNET_TESTING_peer_run ("test-regex-integration", 201 if (0 != GNUNET_TESTING_peer_run("test-regex-integration",
202 "test_regex_api_data.conf", 202 "test_regex_api_data.conf",
203 &run, NULL)) 203 &run, NULL))
204 return 1; 204 return 1;
205 return ok; 205 return ok;
206} 206}
diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c
index bbc60475e..7711a1154 100644
--- a/src/regex/test_regex_iterate_api.c
+++ b/src/regex/test_regex_iterate_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_iterate_api.c 21 * @file regex/test_regex_iterate_api.c
22 * @brief test for regex.c 22 * @brief test for regex.c
@@ -41,8 +41,7 @@
41 41
42static unsigned int transition_counter; 42static unsigned int transition_counter;
43 43
44struct IteratorContext 44struct IteratorContext {
45{
46 int error; 45 int error;
47 int should_save_graph; 46 int should_save_graph;
48 FILE *graph_filep; 47 FILE *graph_filep;
@@ -51,8 +50,7 @@ struct IteratorContext
51 unsigned int match_count; 50 unsigned int match_count;
52}; 51};
53 52
54struct RegexStringPair 53struct RegexStringPair {
55{
56 char *regex; 54 char *regex;
57 unsigned int string_count; 55 unsigned int string_count;
58 char *strings[20]; 56 char *strings[20];
@@ -60,63 +58,63 @@ struct RegexStringPair
60 58
61 59
62static void 60static void
63key_iterator (void *cls, const struct GNUNET_HashCode *key, 61key_iterator(void *cls, const struct GNUNET_HashCode *key,
64 const char *proof, 62 const char *proof,
65 int accepting, unsigned int num_edges, 63 int accepting, unsigned int num_edges,
66 const struct REGEX_BLOCK_Edge *edges) 64 const struct REGEX_BLOCK_Edge *edges)
67{ 65{
68 unsigned int i; 66 unsigned int i;
69 struct IteratorContext *ctx = cls; 67 struct IteratorContext *ctx = cls;
70 char *out_str; 68 char *out_str;
71 char *state_id = GNUNET_strdup (GNUNET_h2s (key)); 69 char *state_id = GNUNET_strdup(GNUNET_h2s(key));
72 70
73 GNUNET_assert (NULL != proof); 71 GNUNET_assert(NULL != proof);
74 if (GNUNET_YES == ctx->should_save_graph) 72 if (GNUNET_YES == ctx->should_save_graph)
75 {
76 if (GNUNET_YES == accepting)
77 GNUNET_asprintf (&out_str, "\"%s\" [shape=doublecircle]\n", state_id);
78 else
79 GNUNET_asprintf (&out_str, "\"%s\" [shape=circle]\n", state_id);
80 fwrite (out_str, strlen (out_str), 1, ctx->graph_filep);
81 GNUNET_free (out_str);
82
83 for (i = 0; i < num_edges; i++)
84 { 73 {
85 transition_counter++; 74 if (GNUNET_YES == accepting)
86 GNUNET_asprintf (&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n", 75 GNUNET_asprintf(&out_str, "\"%s\" [shape=doublecircle]\n", state_id);
87 state_id, GNUNET_h2s (&edges[i].destination), 76 else
88 edges[i].label, proof); 77 GNUNET_asprintf(&out_str, "\"%s\" [shape=circle]\n", state_id);
89 fwrite (out_str, strlen (out_str), 1, ctx->graph_filep); 78 fwrite(out_str, strlen(out_str), 1, ctx->graph_filep);
90 79 GNUNET_free(out_str);
91 GNUNET_free (out_str); 80
81 for (i = 0; i < num_edges; i++)
82 {
83 transition_counter++;
84 GNUNET_asprintf(&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n",
85 state_id, GNUNET_h2s(&edges[i].destination),
86 edges[i].label, proof);
87 fwrite(out_str, strlen(out_str), 1, ctx->graph_filep);
88
89 GNUNET_free(out_str);
90 }
92 } 91 }
93 }
94 else 92 else
95 { 93 {
96 for (i = 0; i < num_edges; i++) 94 for (i = 0; i < num_edges; i++)
97 transition_counter++; 95 transition_counter++;
98 } 96 }
99 97
100 for (i = 0; i < ctx->string_count; i++) 98 for (i = 0; i < ctx->string_count; i++)
101 { 99 {
102 if (0 == strcmp (proof, ctx->strings[i])) 100 if (0 == strcmp(proof, ctx->strings[i]))
103 ctx->match_count++; 101 ctx->match_count++;
104 } 102 }
105 103
106 if (GNUNET_OK != REGEX_BLOCK_check_proof (proof, strlen (proof), key)) 104 if (GNUNET_OK != REGEX_BLOCK_check_proof(proof, strlen(proof), key))
107 { 105 {
108 ctx->error++; 106 ctx->error++;
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 107 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
110 "Proof check failed: proof: %s key: %s\n", proof, state_id); 108 "Proof check failed: proof: %s key: %s\n", proof, state_id);
111 } 109 }
112 GNUNET_free (state_id); 110 GNUNET_free(state_id);
113} 111}
114 112
115 113
116int 114int
117main (int argc, char *argv[]) 115main(int argc, char *argv[])
118{ 116{
119 GNUNET_log_setup ("test-regex", "WARNING", NULL); 117 GNUNET_log_setup("test-regex", "WARNING", NULL);
120 118
121 int error; 119 int error;
122 struct REGEX_INTERNAL_Automaton *dfa; 120 struct REGEX_INTERNAL_Automaton *dfa;
@@ -128,132 +126,132 @@ main (int argc, char *argv[])
128 error = 0; 126 error = 0;
129 127
130 const struct RegexStringPair rxstr[13] = { 128 const struct RegexStringPair rxstr[13] = {
131 {INITIAL_PADDING "ab(c|d)+c*(a(b|c)+d)+(bla)+", 2, 129 { INITIAL_PADDING "ab(c|d)+c*(a(b|c)+d)+(bla)+", 2,
132 {INITIAL_PADDING "abcdcdca", INITIAL_PADDING "abcabdbl"}}, 130 { INITIAL_PADDING "abcdcdca", INITIAL_PADDING "abcabdbl" } },
133 {INITIAL_PADDING 131 { INITIAL_PADDING
134 "abcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd", 1, 132 "abcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd", 1,
135 {INITIAL_PADDING "abcdefgh"}}, 133 { INITIAL_PADDING "abcdefgh" } },
136 {INITIAL_PADDING "VPN-4-1(0|1)*", 2, 134 { INITIAL_PADDING "VPN-4-1(0|1)*", 2,
137 {INITIAL_PADDING "VPN-4-10", INITIAL_PADDING "VPN-4-11"}}, 135 { INITIAL_PADDING "VPN-4-10", INITIAL_PADDING "VPN-4-11" } },
138 {INITIAL_PADDING "(a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*)", 2, 136 { INITIAL_PADDING "(a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*)", 2,
139 {INITIAL_PADDING "aaaaaaaa", INITIAL_PADDING "aaXXyyyc"}}, 137 { INITIAL_PADDING "aaaaaaaa", INITIAL_PADDING "aaXXyyyc" } },
140 {INITIAL_PADDING "a*", 1, {INITIAL_PADDING "aaaaaaaa"}}, 138 { INITIAL_PADDING "a*", 1, { INITIAL_PADDING "aaaaaaaa" } },
141 {INITIAL_PADDING "xzxzxzxzxz", 1, {INITIAL_PADDING "xzxzxzxz"}}, 139 { INITIAL_PADDING "xzxzxzxzxz", 1, { INITIAL_PADDING "xzxzxzxz" } },
142 {INITIAL_PADDING "xyz*", 1, {INITIAL_PADDING "xyzzzzzz"}}, 140 { INITIAL_PADDING "xyz*", 1, { INITIAL_PADDING "xyzzzzzz" } },
143 {INITIAL_PADDING 141 { INITIAL_PADDING
144 "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)", 142 "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)",
145 2, {INITIAL_PADDING "abcd:000", INITIAL_PADDING "abcd:101"}}, 143 2, { INITIAL_PADDING "abcd:000", INITIAL_PADDING "abcd:101" } },
146 {INITIAL_PADDING "(x*|(0|1|2)(a|b|c|d)+)", 2, 144 { INITIAL_PADDING "(x*|(0|1|2)(a|b|c|d)+)", 2,
147 {INITIAL_PADDING "xxxxxxxx", INITIAL_PADDING "0abcdbad"}}, 145 { INITIAL_PADDING "xxxxxxxx", INITIAL_PADDING "0abcdbad" } },
148 {INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, {INITIAL_PADDING "11234567"}}, 146 { INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, { INITIAL_PADDING "11234567" } },
149 {INITIAL_PADDING "0*123456789ABC*", 3, 147 { INITIAL_PADDING "0*123456789ABC*", 3,
150 {INITIAL_PADDING "00123456", INITIAL_PADDING "00000000", 148 { INITIAL_PADDING "00123456", INITIAL_PADDING "00000000",
151 INITIAL_PADDING "12345678"}}, 149 INITIAL_PADDING "12345678" } },
152 {INITIAL_PADDING "0123456789A*BC", 1, {INITIAL_PADDING "01234567"}}, 150 { INITIAL_PADDING "0123456789A*BC", 1, { INITIAL_PADDING "01234567" } },
153 {"GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, {"GNUNETVPN000100000IPEX6-"}} 151 { "GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, { "GNUNETVPN000100000IPEX6-" } }
154 }; 152 };
155 153
156 const char *graph_start_str = "digraph G {\nrankdir=LR\n"; 154 const char *graph_start_str = "digraph G {\nrankdir=LR\n";
157 const char *graph_end_str = "\n}\n"; 155 const char *graph_end_str = "\n}\n";
158 156
159 for (i = 0; i < 13; i++) 157 for (i = 0; i < 13; i++)
160 {
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n",
162 rxstr[i].regex);
163
164
165 /* Create graph */
166 if (GNUNET_YES == REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH)
167 { 158 {
168 GNUNET_asprintf (&filename, "iteration_graph_%u.dot", i); 159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n",
169 ctx.graph_filep = fopen (filename, "w"); 160 rxstr[i].regex);
170 if (NULL == ctx.graph_filep) 161
171 { 162
172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 163 /* Create graph */
173 "Could not open file %s for saving iteration graph.\n", 164 if (GNUNET_YES == REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH)
174 filename); 165 {
175 ctx.should_save_graph = GNUNET_NO; 166 GNUNET_asprintf(&filename, "iteration_graph_%u.dot", i);
176 } 167 ctx.graph_filep = fopen(filename, "w");
168 if (NULL == ctx.graph_filep)
169 {
170 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
171 "Could not open file %s for saving iteration graph.\n",
172 filename);
173 ctx.should_save_graph = GNUNET_NO;
174 }
175 else
176 {
177 ctx.should_save_graph = GNUNET_YES;
178 fwrite(graph_start_str, strlen(graph_start_str), 1, ctx.graph_filep);
179 }
180 GNUNET_free(filename);
181 }
177 else 182 else
178 { 183 {
179 ctx.should_save_graph = GNUNET_YES; 184 ctx.should_save_graph = GNUNET_NO;
180 fwrite (graph_start_str, strlen (graph_start_str), 1, ctx.graph_filep); 185 ctx.graph_filep = NULL;
181 } 186 }
182 GNUNET_free (filename); 187
183 } 188 /* Iterate over DFA edges */
184 else 189 transition_counter = 0;
185 { 190 ctx.string_count = rxstr[i].string_count;
186 ctx.should_save_graph = GNUNET_NO; 191 ctx.strings = rxstr[i].strings;
187 ctx.graph_filep = NULL; 192 ctx.match_count = 0;
188 } 193 dfa =
189 194 REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0);
190 /* Iterate over DFA edges */ 195 REGEX_INTERNAL_iterate_all_edges(dfa, key_iterator, &ctx);
191 transition_counter = 0; 196 num_transitions =
192 ctx.string_count = rxstr[i].string_count; 197 REGEX_INTERNAL_get_transition_count(dfa) - dfa->start->transition_count;
193 ctx.strings = rxstr[i].strings; 198
194 ctx.match_count = 0; 199 if (transition_counter < num_transitions)
195 dfa = 200 {
196 REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0); 201 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
197 REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx); 202 "Automaton has %d transitions, iterated over %d transitions\n",
198 num_transitions = 203 num_transitions, transition_counter);
199 REGEX_INTERNAL_get_transition_count (dfa) - dfa->start->transition_count; 204 error += 1;
200 205 }
201 if (transition_counter < num_transitions) 206
202 { 207 if (ctx.match_count < ctx.string_count)
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 208 {
204 "Automaton has %d transitions, iterated over %d transitions\n", 209 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
205 num_transitions, transition_counter); 210 "Missing initial states for regex %s\n", rxstr[i].regex);
206 error += 1; 211 error += (ctx.string_count - ctx.match_count);
207 } 212 }
208 213 else if (ctx.match_count > ctx.string_count)
209 if (ctx.match_count < ctx.string_count) 214 {
210 { 215 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 216 "Duplicate initial transitions for regex %s\n",
212 "Missing initial states for regex %s\n", rxstr[i].regex); 217 rxstr[i].regex);
213 error += (ctx.string_count - ctx.match_count); 218 error += (ctx.string_count - ctx.match_count);
214 } 219 }
215 else if (ctx.match_count > ctx.string_count) 220
216 { 221 REGEX_INTERNAL_automaton_destroy(dfa);
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 222
218 "Duplicate initial transitions for regex %s\n", 223 /* Finish graph */
219 rxstr[i].regex); 224 if (GNUNET_YES == ctx.should_save_graph)
220 error += (ctx.string_count - ctx.match_count); 225 {
221 } 226 fwrite(graph_end_str, strlen(graph_end_str), 1, ctx.graph_filep);
222 227 fclose(ctx.graph_filep);
223 REGEX_INTERNAL_automaton_destroy (dfa); 228 ctx.graph_filep = NULL;
224 229 ctx.should_save_graph = GNUNET_NO;
225 /* Finish graph */ 230 }
226 if (GNUNET_YES == ctx.should_save_graph)
227 {
228 fwrite (graph_end_str, strlen (graph_end_str), 1, ctx.graph_filep);
229 fclose (ctx.graph_filep);
230 ctx.graph_filep = NULL;
231 ctx.should_save_graph = GNUNET_NO;
232 } 231 }
233 }
234 232
235 233
236 for (i = 0; i < 13; i++) 234 for (i = 0; i < 13; i++)
237 {
238 ctx.string_count = rxstr[i].string_count;
239 ctx.strings = rxstr[i].strings;
240 ctx.match_count = 0;
241
242 dfa =
243 REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
244 REGEX_INTERNAL_dfa_add_multi_strides (NULL, dfa, 2);
245 REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx);
246
247 if (ctx.match_count < ctx.string_count)
248 { 235 {
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 236 ctx.string_count = rxstr[i].string_count;
250 "Missing initial states for regex %s\n", rxstr[i].regex); 237 ctx.strings = rxstr[i].strings;
251 error += (ctx.string_count - ctx.match_count); 238 ctx.match_count = 0;
239
240 dfa =
241 REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0);
242 REGEX_INTERNAL_dfa_add_multi_strides(NULL, dfa, 2);
243 REGEX_INTERNAL_iterate_all_edges(dfa, key_iterator, &ctx);
244
245 if (ctx.match_count < ctx.string_count)
246 {
247 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
248 "Missing initial states for regex %s\n", rxstr[i].regex);
249 error += (ctx.string_count - ctx.match_count);
250 }
251
252 REGEX_INTERNAL_automaton_destroy(dfa);
252 } 253 }
253 254
254 REGEX_INTERNAL_automaton_destroy (dfa);
255 }
256
257 error += ctx.error; 255 error += ctx.error;
258 256
259 return error; 257 return error;
diff --git a/src/regex/test_regex_proofs.c b/src/regex/test_regex_proofs.c
index 4f49ef656..3649ac8aa 100644
--- a/src/regex/test_regex_proofs.c
+++ b/src/regex/test_regex_proofs.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file regex/test_regex_proofs.c 21 * @file regex/test_regex_proofs.c
22 * @brief test for regex.c 22 * @brief test for regex.c
@@ -40,32 +40,32 @@
40 * @return 0 on success, 1 on failure 40 * @return 0 on success, 1 on failure
41 */ 41 */
42static unsigned int 42static unsigned int
43test_proof (const char *regex) 43test_proof(const char *regex)
44{ 44{
45 unsigned int error; 45 unsigned int error;
46 struct REGEX_INTERNAL_Automaton *dfa; 46 struct REGEX_INTERNAL_Automaton *dfa;
47 char *c_rx1; 47 char *c_rx1;
48 const char *c_rx2; 48 const char *c_rx2;
49 49
50 dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), 1); 50 dfa = REGEX_INTERNAL_construct_dfa(regex, strlen(regex), 1);
51 GNUNET_assert (NULL != dfa); 51 GNUNET_assert(NULL != dfa);
52 c_rx1 = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa)); 52 c_rx1 = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(dfa));
53 REGEX_INTERNAL_automaton_destroy (dfa); 53 REGEX_INTERNAL_automaton_destroy(dfa);
54 dfa = REGEX_INTERNAL_construct_dfa (c_rx1, strlen (c_rx1), 1); 54 dfa = REGEX_INTERNAL_construct_dfa(c_rx1, strlen(c_rx1), 1);
55 GNUNET_assert (NULL != dfa); 55 GNUNET_assert(NULL != dfa);
56 c_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa); 56 c_rx2 = REGEX_INTERNAL_get_canonical_regex(dfa);
57 57
58 error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1; 58 error = (0 == strcmp(c_rx1, c_rx2)) ? 0 : 1;
59 59
60 if (error > 0) 60 if (error > 0)
61 { 61 {
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 62 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
63 "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n", 63 "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n",
64 regex, c_rx1, c_rx2); 64 regex, c_rx1, c_rx2);
65 } 65 }
66 66
67 GNUNET_free (c_rx1); 67 GNUNET_free(c_rx1);
68 REGEX_INTERNAL_automaton_destroy (dfa); 68 REGEX_INTERNAL_automaton_destroy(dfa);
69 69
70 return error; 70 return error;
71} 71}
@@ -81,7 +81,7 @@ test_proof (const char *regex)
81 * @return 0 on succes, number of failures otherwise. 81 * @return 0 on succes, number of failures otherwise.
82 */ 82 */
83static unsigned int 83static unsigned int
84test_proofs_random (unsigned int count, size_t rx_length) 84test_proofs_random(unsigned int count, size_t rx_length)
85{ 85{
86 unsigned int i; 86 unsigned int i;
87 char *rand_rx; 87 char *rand_rx;
@@ -90,11 +90,11 @@ test_proofs_random (unsigned int count, size_t rx_length)
90 failures = 0; 90 failures = 0;
91 91
92 for (i = 0; i < count; i++) 92 for (i = 0; i < count; i++)
93 { 93 {
94 rand_rx = REGEX_TEST_generate_random_regex (rx_length, NULL); 94 rand_rx = REGEX_TEST_generate_random_regex(rx_length, NULL);
95 failures += test_proof (rand_rx); 95 failures += test_proof(rand_rx);
96 GNUNET_free (rand_rx); 96 GNUNET_free(rand_rx);
97 } 97 }
98 98
99 return failures; 99 return failures;
100} 100}
@@ -106,7 +106,7 @@ test_proofs_random (unsigned int count, size_t rx_length)
106 * @return 0 on success, number of failures otherwise. 106 * @return 0 on success, number of failures otherwise.
107 */ 107 */
108static unsigned int 108static unsigned int
109test_proofs_static () 109test_proofs_static()
110{ 110{
111 unsigned int i; 111 unsigned int i;
112 unsigned int error; 112 unsigned int error;
@@ -130,43 +130,43 @@ test_proofs_static ()
130 error = 0; 130 error = 0;
131 131
132 for (i = 0; i < 8; i += 2) 132 for (i = 0; i < 8; i += 2)
133 { 133 {
134 dfa1 = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 1); 134 dfa1 = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 1);
135 dfa2 = REGEX_INTERNAL_construct_dfa (regex[i + 1], strlen (regex[i + 1]), 1); 135 dfa2 = REGEX_INTERNAL_construct_dfa(regex[i + 1], strlen(regex[i + 1]), 1);
136 GNUNET_assert (NULL != dfa1); 136 GNUNET_assert(NULL != dfa1);
137 GNUNET_assert (NULL != dfa2); 137 GNUNET_assert(NULL != dfa2);
138 138
139 canon_rx1 = REGEX_INTERNAL_get_canonical_regex (dfa1); 139 canon_rx1 = REGEX_INTERNAL_get_canonical_regex(dfa1);
140 canon_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa2); 140 canon_rx2 = REGEX_INTERNAL_get_canonical_regex(dfa2);
141 141
142 error += (0 == strcmp (canon_rx1, canon_rx2)) ? 0 : 1; 142 error += (0 == strcmp(canon_rx1, canon_rx2)) ? 0 : 1;
143 143
144 if (error > 0) 144 if (error > 0)
145 { 145 {
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 146 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
147 "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n", 147 "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n",
148 regex[i], canon_rx1, regex[i + 1], canon_rx2); 148 regex[i], canon_rx1, regex[i + 1], canon_rx2);
149 } 149 }
150 150
151 REGEX_INTERNAL_automaton_destroy (dfa1); 151 REGEX_INTERNAL_automaton_destroy(dfa1);
152 REGEX_INTERNAL_automaton_destroy (dfa2); 152 REGEX_INTERNAL_automaton_destroy(dfa2);
153 } 153 }
154 154
155 return error; 155 return error;
156} 156}
157 157
158 158
159int 159int
160main (int argc, char *argv[]) 160main(int argc, char *argv[])
161{ 161{
162 GNUNET_log_setup ("test-regex", "WARNING", NULL); 162 GNUNET_log_setup("test-regex", "WARNING", NULL);
163 163
164 int error; 164 int error;
165 165
166 error = 0; 166 error = 0;
167 167
168 error += test_proofs_static (); 168 error += test_proofs_static();
169 error += test_proofs_random (100, 30); 169 error += test_proofs_random(100, 30);
170 170
171 return error; 171 return error;
172} 172}