From c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 5 Oct 2019 15:09:28 +0200 Subject: global reindent, now with uncrustify hook enabled --- src/regex/gnunet-daemon-regexprofiler.c | 339 +-- src/regex/gnunet-regex-profiler.c | 1324 +++++------ src/regex/gnunet-regex-simulation-profiler.c | 622 ++--- src/regex/gnunet-service-regex.c | 297 +-- src/regex/perf-regex.c | 102 +- src/regex/plugin_block_regex.c | 386 ++-- src/regex/regex_api_announce.c | 99 +- src/regex/regex_api_search.c | 155 +- src/regex/regex_block_lib.c | 362 +-- src/regex/regex_block_lib.h | 45 +- src/regex/regex_internal.c | 3122 +++++++++++++------------- src/regex/regex_internal.h | 52 +- src/regex/regex_internal_dht.c | 657 +++--- src/regex/regex_internal_lib.h | 56 +- src/regex/regex_ipc.h | 9 +- src/regex/regex_test_graph.c | 281 +-- src/regex/regex_test_lib.c | 518 ++--- src/regex/regex_test_lib.h | 19 +- src/regex/regex_test_random.c | 115 +- src/regex/test_regex_api.c | 70 +- src/regex/test_regex_eval_api.c | 335 +-- src/regex/test_regex_graph_api.c | 160 +- src/regex/test_regex_integration.c | 183 +- src/regex/test_regex_iterate_api.c | 270 +-- src/regex/test_regex_proofs.c | 89 +- 25 files changed, 4879 insertions(+), 4788 deletions(-) (limited to 'src/regex') diff --git a/src/regex/gnunet-daemon-regexprofiler.c b/src/regex/gnunet-daemon-regexprofiler.c index b9c32f9c7..a23336808 100644 --- a/src/regex/gnunet-daemon-regexprofiler.c +++ b/src/regex/gnunet-daemon-regexprofiler.c @@ -62,7 +62,7 @@ static struct REGEX_INTERNAL_Announcement *announce_handle; /** * Periodically reannounce regex. */ -static struct GNUNET_SCHEDULER_Task * reannounce_task; +static struct GNUNET_SCHEDULER_Task *reannounce_task; /** * What's the maximum reannounce period. @@ -78,12 +78,12 @@ static unsigned long long max_path_compression; * Name of the file containing policies that this peer should announce. One * policy per line. */ -static char * policy_filename; +static char *policy_filename; /** * Prefix to add before every regex we're announcing. */ -static char * regex_prefix; +static char *regex_prefix; /** * Regex with prefix. @@ -108,31 +108,31 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; * @param cls unused */ static void -shutdown_task(void *cls) +shutdown_task (void *cls) { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); if (NULL != announce_handle) - { - REGEX_INTERNAL_announce_cancel(announce_handle); - announce_handle = NULL; - } + { + REGEX_INTERNAL_announce_cancel (announce_handle); + announce_handle = NULL; + } if (NULL != reannounce_task) - { - GNUNET_free(GNUNET_SCHEDULER_cancel(reannounce_task)); - reannounce_task = NULL; - } + { + GNUNET_free (GNUNET_SCHEDULER_cancel (reannounce_task)); + reannounce_task = NULL; + } if (NULL != dht_handle) - { - GNUNET_DHT_disconnect(dht_handle); - dht_handle = NULL; - } - GNUNET_free(my_private_key); + { + GNUNET_DHT_disconnect (dht_handle); + dht_handle = NULL; + } + GNUNET_free (my_private_key); my_private_key = NULL; - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Daemon for %s shutting down\n", - policy_filename); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Daemon for %s shutting down\n", + policy_filename); } @@ -142,45 +142,46 @@ shutdown_task(void *cls) * @param cls Closure (regex to announce if needed). */ static void -reannounce_regex(void *cls) +reannounce_regex (void *cls) { char *regex = cls; struct GNUNET_TIME_Relative random_delay; reannounce_task = NULL; if (0 == rounds--) - { - global_ret = 0; - GNUNET_SCHEDULER_shutdown(); - GNUNET_free(regex); - return; - } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex); - GNUNET_STATISTICS_update(stats_handle, "# regexes announced", 1, GNUNET_NO); - if (NULL == announce_handle && NULL != regex) - { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "First time, creating regex: %s\n", - regex); - announce_handle = REGEX_INTERNAL_announce(dht_handle, - my_private_key, - regex, - (unsigned int)max_path_compression, - stats_handle); - } + { + global_ret = 0; + GNUNET_SCHEDULER_shutdown (); + GNUNET_free (regex); + return; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex); + GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO); + if ((NULL == announce_handle)&&(NULL != regex)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "First time, creating regex: %s\n", + regex); + announce_handle = REGEX_INTERNAL_announce (dht_handle, + my_private_key, + regex, + (unsigned + int) max_path_compression, + stats_handle); + } else - { - GNUNET_assert(NULL != announce_handle); - REGEX_INTERNAL_reannounce(announce_handle); - } + { + GNUNET_assert (NULL != announce_handle); + REGEX_INTERNAL_reannounce (announce_handle); + } random_delay = - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MICROSECONDS, - GNUNET_CRYPTO_random_u32( - GNUNET_CRYPTO_QUALITY_WEAK, - reannounce_period_max.rel_value_us)); - reannounce_task = GNUNET_SCHEDULER_add_delayed(random_delay, - &reannounce_regex, cls); + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, + GNUNET_CRYPTO_random_u32 ( + GNUNET_CRYPTO_QUALITY_WEAK, + reannounce_period_max.rel_value_us)); + reannounce_task = GNUNET_SCHEDULER_add_delayed (random_delay, + &reannounce_regex, cls); } @@ -191,23 +192,23 @@ reannounce_regex(void *cls) * @param regex regular expression to announce on this peer's cadet. */ static void -announce_regex(const char *regex) +announce_regex (const char *regex) { char *copy; - if (NULL == regex || 0 == strlen(regex)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n"); - return; - } - - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Daemon for %s starting\n", - policy_filename); - GNUNET_assert(NULL == reannounce_task); - copy = GNUNET_strdup(regex); - reannounce_task = GNUNET_SCHEDULER_add_now(&reannounce_regex, - (void *)copy); + if ((NULL == regex)||(0 == strlen (regex))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n"); + return; + } + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Daemon for %s starting\n", + policy_filename); + GNUNET_assert (NULL == reannounce_task); + copy = GNUNET_strdup (regex); + reannounce_task = GNUNET_SCHEDULER_add_now (&reannounce_regex, + (void *) copy); } @@ -220,16 +221,16 @@ announce_regex(const char *regex) * GNUNET_NO to stop when found */ static int -scan(void *cls, const char *filename) +scan (void *cls, const char *filename) { - long n = (long)cls; + long n = (long) cls; static long c = 0; if (c == n) - { - policy_filename = GNUNET_strdup(filename); - return GNUNET_NO; - } + { + policy_filename = GNUNET_strdup (filename); + return GNUNET_NO; + } c++; return GNUNET_OK; } @@ -244,9 +245,9 @@ scan(void *cls, const char *filename) * @param cfg_ configuration */ static void -run(void *cls, char *const *args GNUNET_UNUSED, - const char *cfgfile GNUNET_UNUSED, - const struct GNUNET_CONFIGURATION_Handle *cfg_) +run (void *cls, char *const *args GNUNET_UNUSED, + const char *cfgfile GNUNET_UNUSED, + const struct GNUNET_CONFIGURATION_Handle *cfg_) { char *regex = NULL; char **components; @@ -255,106 +256,109 @@ run(void *cls, char *const *args GNUNET_UNUSED, cfg = cfg_; - my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg); - GNUNET_assert(NULL != my_private_key); + my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); + GNUNET_assert (NULL != my_private_key); if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number(cfg, "REGEXPROFILER", - "MAX_PATH_COMPRESSION", - &max_path_compression)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _ - ("%s service is lacking key configuration settings (%s). Exiting.\n"), - "regexprofiler", "max_path_compression"); - global_ret = GNUNET_SYSERR; - GNUNET_SCHEDULER_shutdown(); - return; - } + GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", + "MAX_PATH_COMPRESSION", + &max_path_compression)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ + ( + "%s service is lacking key configuration settings (%s). Exiting.\n"), + "regexprofiler", "max_path_compression"); + global_ret = GNUNET_SYSERR; + GNUNET_SCHEDULER_shutdown (); + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER", - "POLICY_DIR", &policy_dir)) - { - GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "POLICY_DIR"); - global_ret = GNUNET_SYSERR; - GNUNET_SCHEDULER_shutdown(); - return; - } + GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", + "POLICY_DIR", &policy_dir)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", + "POLICY_DIR"); + global_ret = GNUNET_SYSERR; + GNUNET_SCHEDULER_shutdown (); + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number(cfg, "TESTBED", - "PEERID", &peer_id)) - { - GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID"); - global_ret = GNUNET_SYSERR; - GNUNET_free(policy_dir); - GNUNET_SCHEDULER_shutdown(); - return; - } + GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED", + "PEERID", &peer_id)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "TESTBED", "PEERID"); + global_ret = GNUNET_SYSERR; + GNUNET_free (policy_dir); + GNUNET_SCHEDULER_shutdown (); + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER", - "REGEX_PREFIX", ®ex_prefix)) - { - GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", "REGEX_PREFIX"); - global_ret = GNUNET_SYSERR; - GNUNET_free(policy_dir); - GNUNET_SCHEDULER_shutdown(); - return; - } + GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", + "REGEX_PREFIX", ®ex_prefix)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "REGEXPROFILER", + "REGEX_PREFIX"); + global_ret = GNUNET_SYSERR; + GNUNET_free (policy_dir); + GNUNET_SCHEDULER_shutdown (); + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time(cfg, "REGEXPROFILER", - "REANNOUNCE_PERIOD_MAX", - &reannounce_period_max)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "reannounce_period_max not given. Using 10 minutes.\n"); - reannounce_period_max = - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10); - } + GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER", + "REANNOUNCE_PERIOD_MAX", + &reannounce_period_max)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "reannounce_period_max not given. Using 10 minutes.\n"); + reannounce_period_max = + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10); + } - stats_handle = GNUNET_STATISTICS_create("regexprofiler", cfg); + stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg); - dht_handle = GNUNET_DHT_connect(cfg, 1); + dht_handle = GNUNET_DHT_connect (cfg, 1); if (NULL == dht_handle) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not acquire dht handle. Exiting.\n"); - global_ret = GNUNET_SYSERR; - GNUNET_free(policy_dir); - GNUNET_SCHEDULER_shutdown(); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not acquire dht handle. Exiting.\n"); + global_ret = GNUNET_SYSERR; + GNUNET_free (policy_dir); + GNUNET_SCHEDULER_shutdown (); + return; + } /* Read regexes from policy files */ - GNUNET_assert(-1 != GNUNET_DISK_directory_scan(policy_dir, &scan, - (void *)(long)peer_id)); - if (NULL == (components = REGEX_TEST_read_from_file(policy_filename))) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Policy file %s contains no policies. Exiting.\n", - policy_filename); - global_ret = GNUNET_SYSERR; - GNUNET_free(policy_dir); - GNUNET_SCHEDULER_shutdown(); - return; - } - GNUNET_free(policy_dir); - regex = REGEX_TEST_combine(components, 16); - REGEX_TEST_free_from_file(components); + GNUNET_assert (-1 != GNUNET_DISK_directory_scan (policy_dir, &scan, + (void *) (long) peer_id)); + if (NULL == (components = REGEX_TEST_read_from_file (policy_filename))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Policy file %s contains no policies. Exiting.\n", + policy_filename); + global_ret = GNUNET_SYSERR; + GNUNET_free (policy_dir); + GNUNET_SCHEDULER_shutdown (); + return; + } + GNUNET_free (policy_dir); + regex = REGEX_TEST_combine (components, 16); + REGEX_TEST_free_from_file (components); /* Announcing regexes from policy_filename */ - GNUNET_asprintf(&rx_with_pfx, - "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*", - regex_prefix, - regex); - announce_regex(rx_with_pfx); - GNUNET_free(regex); - GNUNET_free(rx_with_pfx); + GNUNET_asprintf (&rx_with_pfx, + "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*", + regex_prefix, + regex); + announce_regex (rx_with_pfx); + GNUNET_free (regex); + GNUNET_free (rx_with_pfx); /* Scheduled the task to clean up when shutdown is called */ - GNUNET_SCHEDULER_add_shutdown(&shutdown_task, - NULL); + GNUNET_SCHEDULER_add_shutdown (&shutdown_task, + NULL); } @@ -366,19 +370,20 @@ run(void *cls, char *const *args GNUNET_UNUSED, * @return 0 ok, 1 on error */ int -main(int argc, char *const *argv) +main (int argc, char *const *argv) { static const struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END }; - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 2; return (GNUNET_OK == - GNUNET_PROGRAM_run(argc, argv, "regexprofiler", - gettext_noop - ("Daemon to announce regular expressions for the peer using cadet."), - options, &run, NULL)) ? global_ret : 1; + GNUNET_PROGRAM_run (argc, argv, "regexprofiler", + gettext_noop + ( + "Daemon to announce regular expressions for the peer using cadet."), + options, &run, NULL)) ? global_ret : 1; } @@ -388,11 +393,11 @@ main(int argc, char *const *argv) /** * MINIMIZE heap size (way below 128k) since this process doesn't need much. */ -void __attribute__ ((constructor)) GNUNET_ARM_memory_init() +void __attribute__ ((constructor)) GNUNET_ARM_memory_init () { - mallopt(M_TRIM_THRESHOLD, 4 * 1024); - mallopt(M_TOP_PAD, 1 * 1024); - malloc_trim(0); + mallopt (M_TRIM_THRESHOLD, 4 * 1024); + mallopt (M_TOP_PAD, 1 * 1024); + malloc_trim (0); } #endif diff --git a/src/regex/gnunet-regex-profiler.c b/src/regex/gnunet-regex-profiler.c index 4262107d7..16b7a7f0e 100644 --- a/src/regex/gnunet-regex-profiler.c +++ b/src/regex/gnunet-regex-profiler.c @@ -37,12 +37,13 @@ #include "gnunet_testbed_service.h" #define FIND_TIMEOUT \ - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90) + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90) /** * DLL of operations */ -struct DLLOperation { +struct DLLOperation +{ /** * The testbed operation handle */ @@ -68,7 +69,8 @@ struct DLLOperation { /** * Available states during profiling */ -enum State { +enum State +{ /** * Initial state */ @@ -110,7 +112,8 @@ enum State { /** * Peer handles. */ -struct RegexPeer { +struct RegexPeer +{ /** * Peer id. */ @@ -167,7 +170,7 @@ struct RegexPeer { /** * Operation timeout */ - struct GNUNET_SCHEDULER_Task * timeout; + struct GNUNET_SCHEDULER_Task *timeout; /** * Deamon start @@ -208,12 +211,12 @@ static struct GNUNET_CONFIGURATION_Handle *cfg; /** * Abort task identifier */ -static struct GNUNET_SCHEDULER_Task * abort_task; +static struct GNUNET_SCHEDULER_Task *abort_task; /** * Host registration task identifier */ -static struct GNUNET_SCHEDULER_Task * register_hosts_task; +static struct GNUNET_SCHEDULER_Task *register_hosts_task; /** * Global event mask for all testbed events @@ -248,7 +251,7 @@ enum State state; /** * Folder where policy files are stored. */ -static char * policy_dir; +static char *policy_dir; /** * File with hostnames where to execute the test. @@ -288,7 +291,7 @@ static unsigned int next_search; /** * Search timeout task identifier. */ -static struct GNUNET_SCHEDULER_Task * search_timeout_task; +static struct GNUNET_SCHEDULER_Task *search_timeout_task; /** * Search timeout in seconds. @@ -309,7 +312,7 @@ static char *data_filename; * Prefix used for regex announcing. We need to prefix the search * strings with it, in order to find something. */ -static char * regex_prefix; +static char *regex_prefix; /** * What's the maximum regex reannounce period. @@ -330,8 +333,8 @@ static struct GNUNET_TIME_Relative reannounce_period_max; * @param emsg error message. */ static void -dht_connect_cb(void *cls, struct GNUNET_TESTBED_Operation *op, - void *ca_result, const char *emsg); +dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, + void *ca_result, const char *emsg); /** * DHT connect adapter. @@ -342,7 +345,7 @@ dht_connect_cb(void *cls, struct GNUNET_TESTBED_Operation *op, * @return */ static void * -dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); +dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); /** @@ -353,7 +356,7 @@ dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); * @param op_result service handle returned from the connect adapter */ static void -dht_da(void *cls, void *op_result); +dht_da (void *cls, void *op_result); /** @@ -366,10 +369,10 @@ dht_da(void *cls, void *op_result); * @param emsg error message on failure */ static void -stats_connect_cb(void *cls, - struct GNUNET_TESTBED_Operation *op, - void *ca_result, - const char *emsg); +stats_connect_cb (void *cls, + struct GNUNET_TESTBED_Operation *op, + void *ca_result, + const char *emsg); /** @@ -378,7 +381,7 @@ stats_connect_cb(void *cls, * @param cls Index of the next peer in the peers array. */ static void -announce_next_regex(void *cls); +announce_next_regex (void *cls); /******************************************************************************/ @@ -392,7 +395,7 @@ announce_next_regex(void *cls); * @param cls NULL */ static void -do_shutdown(void *cls) +do_shutdown (void *cls) { struct RegexPeer *peer; unsigned int peer_cnt; @@ -401,75 +404,75 @@ do_shutdown(void *cls) size_t size; if (NULL != abort_task) - { - GNUNET_SCHEDULER_cancel(abort_task); - abort_task = NULL; - } + { + GNUNET_SCHEDULER_cancel (abort_task); + abort_task = NULL; + } if (NULL != register_hosts_task) - { - GNUNET_SCHEDULER_cancel(register_hosts_task); - register_hosts_task = NULL; - } + { + GNUNET_SCHEDULER_cancel (register_hosts_task); + register_hosts_task = NULL; + } for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++) + { + peer = &peers[peer_cnt]; + + if ((GNUNET_YES != peer->search_str_matched) &&(NULL != data_file) ) { - peer = &peers[peer_cnt]; - - if (GNUNET_YES != peer->search_str_matched && NULL != data_file) - { - prof_time = GNUNET_TIME_absolute_get_duration(peer->prof_start_time); - size = - GNUNET_snprintf(output_buffer, - sizeof(output_buffer), - "%p Search string not found: %s (%d)\n" - "%p On peer: %u (%p)\n" - "%p After: %s\n", - peer, peer->search_str, peer->search_str_matched, - peer, peer->id, peer, - peer, - GNUNET_STRINGS_relative_time_to_string(prof_time, - GNUNET_NO)); - if (size != GNUNET_DISK_file_write(data_file, output_buffer, size)) - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); - } - - if (NULL != peers[peer_cnt].op_handle) - GNUNET_TESTBED_operation_done(peers[peer_cnt].op_handle); + prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time); + size = + GNUNET_snprintf (output_buffer, + sizeof(output_buffer), + "%p Search string not found: %s (%d)\n" + "%p On peer: %u (%p)\n" + "%p After: %s\n", + peer, peer->search_str, peer->search_str_matched, + peer, peer->id, peer, + peer, + GNUNET_STRINGS_relative_time_to_string (prof_time, + GNUNET_NO)); + if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); } + if (NULL != peers[peer_cnt].op_handle) + GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle); + } + if (NULL != data_file) - { - GNUNET_DISK_file_close(data_file); - data_file = NULL; - } + { + GNUNET_DISK_file_close (data_file); + data_file = NULL; + } for (search_str_cnt = 0; search_str_cnt < num_peers && NULL != search_strings; search_str_cnt++) - { - GNUNET_free_non_null(search_strings[search_str_cnt]); - } - GNUNET_free_non_null(search_strings); + { + GNUNET_free_non_null (search_strings[search_str_cnt]); + } + GNUNET_free_non_null (search_strings); search_strings = NULL; if (NULL != reg_handle) - { - GNUNET_TESTBED_cancel_registration(reg_handle); - reg_handle = NULL; - } + { + GNUNET_TESTBED_cancel_registration (reg_handle); + reg_handle = NULL; + } if (NULL != mc) - { - GNUNET_TESTBED_controller_disconnect(mc); - mc = NULL; - } + { + GNUNET_TESTBED_controller_disconnect (mc); + mc = NULL; + } if (NULL != mc_proc) - { - GNUNET_TESTBED_controller_stop(mc_proc); - mc_proc = NULL; - } + { + GNUNET_TESTBED_controller_stop (mc_proc); + mc_proc = NULL; + } if (NULL != cfg) - { - GNUNET_CONFIGURATION_destroy(cfg); - cfg = NULL; - } + { + GNUNET_CONFIGURATION_destroy (cfg); + cfg = NULL; + } } @@ -479,15 +482,15 @@ do_shutdown(void *cls) * @param cls NULL */ static void -do_abort(void *cls) +do_abort (void *cls) { - unsigned long i = (unsigned long)cls; + unsigned long i = (unsigned long) cls; - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Aborting from line %lu...\n", i); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Aborting from line %lu...\n", i); abort_task = NULL; result = GNUNET_SYSERR; - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); } @@ -506,10 +509,10 @@ do_abort(void *cls) * @return service handle to return in 'op_result', NULL on error */ static void * -stats_ca(void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg) +stats_ca (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg) { - return GNUNET_STATISTICS_create("", cfg); + return GNUNET_STATISTICS_create ("", cfg); } @@ -521,13 +524,13 @@ stats_ca(void *cls, * @param op_result service handle returned from the connect adapter */ static void -stats_da(void *cls, void *op_result) +stats_da (void *cls, void *op_result) { struct RegexPeer *peer = cls; - GNUNET_assert(op_result == peer->stats_handle); + GNUNET_assert (op_result == peer->stats_handle); - GNUNET_STATISTICS_destroy(peer->stats_handle, GNUNET_NO); + GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO); peer->stats_handle = NULL; } @@ -543,34 +546,34 @@ stats_da(void *cls, void *op_result) * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration */ static int -stats_iterator(void *cls, - const char *subsystem, - const char *name, - uint64_t value, int is_persistent) +stats_iterator (void *cls, + const char *subsystem, + const char *name, + uint64_t value, int is_persistent) { struct RegexPeer *peer = cls; char output_buffer[512]; size_t size; if (NULL == data_file) - { - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "%p -> %s [%s]: %llu\n", - peer, - subsystem, - name, - (unsigned long long)value); - return GNUNET_OK; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "%p -> %s [%s]: %llu\n", + peer, + subsystem, + name, + (unsigned long long) value); + return GNUNET_OK; + } size = - GNUNET_snprintf(output_buffer, - sizeof(output_buffer), - "%p [%s] %llu %s\n", - peer, - subsystem, value, name); - if (size != GNUNET_DISK_file_write(data_file, output_buffer, size)) - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Unable to write to file!\n"); + GNUNET_snprintf (output_buffer, + sizeof(output_buffer), + "%p [%s] %llu %s\n", + peer, + subsystem, value, name); + if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Unable to write to file!\n"); return GNUNET_OK; } @@ -585,48 +588,48 @@ stats_iterator(void *cls, * successfully obtained, GNUNET_SYSERR if not. */ static void -stats_cb(void *cls, - int success) +stats_cb (void *cls, + int success) { static unsigned int peer_cnt; struct RegexPeer *peer = cls; if (GNUNET_OK != success) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Getting statistics for peer %u failed!\n", - peer->id); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Getting statistics for peer %u failed!\n", + peer->id); + return; + } - GNUNET_assert(NULL != peer->op_handle); + GNUNET_assert (NULL != peer->op_handle); - GNUNET_TESTBED_operation_done(peer->op_handle); + GNUNET_TESTBED_operation_done (peer->op_handle); peer->op_handle = NULL; peer_cnt++; peer = &peers[peer_cnt]; - fprintf(stderr, "s"); + fprintf (stderr, "s"); if (peer_cnt == num_peers) - { - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "\nCollecting stats finished. Shutting down.\n"); - GNUNET_SCHEDULER_shutdown(); - result = GNUNET_OK; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "\nCollecting stats finished. Shutting down.\n"); + GNUNET_SCHEDULER_shutdown (); + result = GNUNET_OK; + } else - { - peer->op_handle = - GNUNET_TESTBED_service_connect(NULL, - peer->peer_handle, - "statistics", - &stats_connect_cb, - peer, - &stats_ca, - &stats_da, - peer); - } + { + peer->op_handle = + GNUNET_TESTBED_service_connect (NULL, + peer->peer_handle, + "statistics", + &stats_connect_cb, + peer, + &stats_ca, + &stats_da, + peer); + } } @@ -640,32 +643,32 @@ stats_cb(void *cls, * @param emsg error message on failure */ static void -stats_connect_cb(void *cls, - struct GNUNET_TESTBED_Operation *op, - void *ca_result, - const char *emsg) +stats_connect_cb (void *cls, + struct GNUNET_TESTBED_Operation *op, + void *ca_result, + const char *emsg) { struct RegexPeer *peer = cls; - if (NULL == ca_result || NULL != emsg) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to connect to statistics service on peer %u: %s\n", - peer->id, emsg); + if ((NULL == ca_result)||(NULL != emsg)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to connect to statistics service on peer %u: %s\n", + peer->id, emsg); - peer->stats_handle = NULL; - return; - } + peer->stats_handle = NULL; + return; + } peer->stats_handle = ca_result; - if (NULL == GNUNET_STATISTICS_get(peer->stats_handle, NULL, NULL, - &stats_cb, - &stats_iterator, peer)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not get statistics of peer %u!\n", peer->id); - } + if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL, + &stats_cb, + &stats_iterator, peer)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not get statistics of peer %u!\n", peer->id); + } } @@ -676,21 +679,21 @@ stats_connect_cb(void *cls, * @param cls NULL */ static void -do_collect_stats(void *cls) +do_collect_stats (void *cls) { struct RegexPeer *peer = &peers[0]; - GNUNET_assert(NULL != peer->peer_handle); + GNUNET_assert (NULL != peer->peer_handle); peer->op_handle = - GNUNET_TESTBED_service_connect(NULL, - peer->peer_handle, - "statistics", - &stats_connect_cb, - peer, - &stats_ca, - &stats_da, - peer); + GNUNET_TESTBED_service_connect (NULL, + peer->peer_handle, + "statistics", + &stats_connect_cb, + peer, + &stats_ca, + &stats_da, + peer); } @@ -705,7 +708,7 @@ do_collect_stats(void *cls) * @param cls Index of the next peer in the peers array. */ static void -find_string(void *cls); +find_string (void *cls); /** @@ -720,94 +723,94 @@ find_string(void *cls); * @param put_path_length Length of the put_path. */ static void -regex_found_handler(void *cls, - const struct GNUNET_PeerIdentity *id, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length) +regex_found_handler (void *cls, + const struct GNUNET_PeerIdentity *id, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length) { struct RegexPeer *peer = cls; char output_buffer[512]; size_t size; if (GNUNET_YES == peer->search_str_matched) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "String %s on peer %u already matched!\n", - peer->search_str, peer->id); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "String %s on peer %u already matched!\n", + peer->search_str, peer->id); + return; + } strings_found++; parallel_searches--; if (NULL != peer->timeout) - { - GNUNET_SCHEDULER_cancel(peer->timeout); - peer->timeout = NULL; - if (GNUNET_NO == in_shutdown) - GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL); - } + { + GNUNET_SCHEDULER_cancel (peer->timeout); + peer->timeout = NULL; + if (GNUNET_NO == in_shutdown) + GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); + } if (NULL == id) - { - // FIXME not possible right now - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "String matching timed out for string %s on peer %u (%i/%i)\n", - peer->search_str, peer->id, strings_found, num_peers); - peer->search_str_matched = GNUNET_SYSERR; - } + { + // FIXME not possible right now + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "String matching timed out for string %s on peer %u (%i/%i)\n", + peer->search_str, peer->id, strings_found, num_peers); + peer->search_str_matched = GNUNET_SYSERR; + } else + { + prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "String %s found on peer %u after %s (%i/%i) (%u||)\n", + peer->search_str, peer->id, + GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO), + strings_found, num_peers, parallel_searches); + + peer->search_str_matched = GNUNET_YES; + + if (NULL != data_file) { - prof_time = GNUNET_TIME_absolute_get_duration(peer->prof_start_time); - - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "String %s found on peer %u after %s (%i/%i) (%u||)\n", - peer->search_str, peer->id, - GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO), - strings_found, num_peers, parallel_searches); - - peer->search_str_matched = GNUNET_YES; - - if (NULL != data_file) - { - size = - GNUNET_snprintf(output_buffer, - sizeof(output_buffer), - "%p Peer: %u\n" - "%p Search string: %s\n" - "%p Search duration: %s\n\n", - peer, peer->id, - peer, peer->search_str, - peer, - GNUNET_STRINGS_relative_time_to_string(prof_time, - GNUNET_NO)); - - if (size != GNUNET_DISK_file_write(data_file, output_buffer, size)) - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); - } + size = + GNUNET_snprintf (output_buffer, + sizeof(output_buffer), + "%p Peer: %u\n" + "%p Search string: %s\n" + "%p Search duration: %s\n\n", + peer, peer->id, + peer, peer->search_str, + peer, + GNUNET_STRINGS_relative_time_to_string (prof_time, + GNUNET_NO)); + + if (size != GNUNET_DISK_file_write (data_file, output_buffer, size)) + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n"); } + } - GNUNET_TESTBED_operation_done(peer->op_handle); + GNUNET_TESTBED_operation_done (peer->op_handle); peer->op_handle = NULL; if (strings_found == num_peers) - { - prof_time = GNUNET_TIME_absolute_get_duration(prof_start_time); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "All strings successfully matched in %s\n", - GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO)); - - if (NULL != search_timeout_task) - { - GNUNET_SCHEDULER_cancel(search_timeout_task); - search_timeout_task = NULL; - } + { + prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "All strings successfully matched in %s\n", + GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO)); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n"); - GNUNET_SCHEDULER_add_now(&do_collect_stats, NULL); + if (NULL != search_timeout_task) + { + GNUNET_SCHEDULER_cancel (search_timeout_task); + search_timeout_task = NULL; } + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n"); + GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL); + } } @@ -818,33 +821,33 @@ regex_found_handler(void *cls, * @param cls NULL */ static void -search_timed_out(void *cls) +search_timed_out (void *cls) { unsigned int i; - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Finding matches to all strings did not succeed after %s.\n", - GNUNET_STRINGS_relative_time_to_string(search_timeout_time, - GNUNET_NO)); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Found %i of %i strings\n", strings_found, num_peers); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Finding matches to all strings did not succeed after %s.\n", + GNUNET_STRINGS_relative_time_to_string (search_timeout_time, + GNUNET_NO)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Found %i of %i strings\n", strings_found, num_peers); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Search timed out after %s." - "Collecting stats and shutting down.\n", - GNUNET_STRINGS_relative_time_to_string(search_timeout_time, - GNUNET_NO)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Search timed out after %s." + "Collecting stats and shutting down.\n", + GNUNET_STRINGS_relative_time_to_string (search_timeout_time, + GNUNET_NO)); in_shutdown = GNUNET_YES; for (i = 0; i < num_peers; i++) + { + if (NULL != peers[i].op_handle) { - if (NULL != peers[i].op_handle) - { - GNUNET_TESTBED_operation_done(peers[i].op_handle); - peers[i].op_handle = NULL; - } + GNUNET_TESTBED_operation_done (peers[i].op_handle); + peers[i].op_handle = NULL; } - GNUNET_SCHEDULER_add_now(&do_collect_stats, NULL); + } + GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL); } @@ -855,17 +858,17 @@ search_timed_out(void *cls) * @param cls Index of the next peer in the peers array. */ static void -find_timed_out(void *cls) +find_timed_out (void *cls) { struct RegexPeer *p = cls; p->timeout = NULL; - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Searching for string \"%s\" on peer %d timed out.\n", - p->search_str, - p->id); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Searching for string \"%s\" on peer %d timed out.\n", + p->search_str, + p->id); if (GNUNET_NO == in_shutdown) - GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL); + GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); } @@ -875,34 +878,34 @@ find_timed_out(void *cls) * @param cls Index of the next peer in the peers array. */ static void -find_string(void *cls) +find_string (void *cls) { - unsigned int search_peer = (unsigned int)(long)cls; + unsigned int search_peer = (unsigned int) (long) cls; if ((search_peer >= num_peers) || (GNUNET_YES == in_shutdown)) return; - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Searching for string \"%s\" on peer %d (%u||)\n", - peers[search_peer].search_str, - search_peer, - parallel_searches); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Searching for string \"%s\" on peer %d (%u||)\n", + peers[search_peer].search_str, + search_peer, + parallel_searches); peers[search_peer].op_handle = - GNUNET_TESTBED_service_connect(NULL, - peers[search_peer].peer_handle, - "dht", - &dht_connect_cb, - &peers[search_peer], - &dht_ca, - &dht_da, - &peers[search_peer]); - GNUNET_assert(NULL != peers[search_peer].op_handle); + GNUNET_TESTBED_service_connect (NULL, + peers[search_peer].peer_handle, + "dht", + &dht_connect_cb, + &peers[search_peer], + &dht_ca, + &dht_da, + &peers[search_peer]); + GNUNET_assert (NULL != peers[search_peer].op_handle); peers[search_peer].timeout - = GNUNET_SCHEDULER_add_delayed(FIND_TIMEOUT, - &find_timed_out, - &peers[search_peer]); + = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT, + &find_timed_out, + &peers[search_peer]); } @@ -914,44 +917,44 @@ find_string(void *cls) * @param emsg NULL on success; otherwise an error description */ static void -daemon_started(void *cls, - struct GNUNET_TESTBED_Operation *op, - const char *emsg) +daemon_started (void *cls, + struct GNUNET_TESTBED_Operation *op, + const char *emsg) { - struct RegexPeer *peer = (struct RegexPeer *)cls; + struct RegexPeer *peer = (struct RegexPeer *) cls; unsigned long search_peer; unsigned int i; - GNUNET_TESTBED_operation_done(peer->daemon_op); + GNUNET_TESTBED_operation_done (peer->daemon_op); peer->daemon_op = NULL; if (NULL != emsg) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg); - GNUNET_assert(0); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg); + GNUNET_assert (0); + } else - { - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Deamon %u started successfully\n", peer->id); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Deamon %u started successfully\n", peer->id); + } /* Find a peer to look for a string matching the regex announced */ - search_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, - num_peers); + search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + num_peers); for (i = 0; peers[search_peer].search_str != NULL; i++) - { - search_peer = (search_peer + 1) % num_peers; - if (i > num_peers) - GNUNET_assert(0); /* we ran out of peers, must be a bug */ - } + { + search_peer = (search_peer + 1) % num_peers; + if (i > num_peers) + GNUNET_assert (0); /* we ran out of peers, must be a bug */ + } peers[search_peer].search_str = search_strings[peer->id]; peers[search_peer].search_str_matched = GNUNET_NO; - GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_saturating_multiply( - reannounce_period_max, - 2), - &find_string, - (void *)search_peer); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_saturating_multiply ( + reannounce_period_max, + 2), + &find_string, + (void *) search_peer); } @@ -963,21 +966,21 @@ daemon_started(void *cls, * @param tc the task context */ static void -do_announce(void *cls) +do_announce (void *cls) { unsigned int i; if (GNUNET_YES == in_shutdown) return; - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Starting announce.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Starting announce.\n"); for (i = 0; i < init_parallel_searches; i++) - { - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - " scheduling announce %u\n", - i); - (void)GNUNET_SCHEDULER_add_now(&announce_next_regex, NULL); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + " scheduling announce %u\n", + i); + (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL); + } } @@ -987,36 +990,36 @@ do_announce(void *cls) * @param cls Closure (unused). */ static void -announce_next_regex(void *cls) +announce_next_regex (void *cls) { struct RegexPeer *peer; if (GNUNET_YES == in_shutdown) return; if (next_search >= num_peers) + { + if (strings_found != num_peers) { - if (strings_found != num_peers) - { - struct GNUNET_TIME_Relative new_delay; - if (NULL != search_timeout_task) - GNUNET_SCHEDULER_cancel(search_timeout_task); - new_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15); - search_timeout_task = GNUNET_SCHEDULER_add_delayed(new_delay, - &search_timed_out, - NULL); - } - return; + struct GNUNET_TIME_Relative new_delay; + if (NULL != search_timeout_task) + GNUNET_SCHEDULER_cancel (search_timeout_task); + new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15); + search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay, + &search_timed_out, + NULL); } + return; + } - GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search); peer = &peers[next_search]; peer->daemon_op = - GNUNET_TESTBED_peer_manage_service(NULL, - peer->peer_handle, - "regexprofiler", - &daemon_started, - peer, - 1); + GNUNET_TESTBED_peer_manage_service (NULL, + peer->peer_handle, + "regexprofiler", + &daemon_started, + peer, + 1); next_search++; parallel_searches++; } @@ -1033,29 +1036,29 @@ announce_next_regex(void *cls) * @param emsg error message. */ static void -dht_connect_cb(void *cls, - struct GNUNET_TESTBED_Operation *op, - void *ca_result, - const char *emsg) +dht_connect_cb (void *cls, + struct GNUNET_TESTBED_Operation *op, + void *ca_result, + const char *emsg) { - struct RegexPeer *peer = (struct RegexPeer *)cls; + struct RegexPeer *peer = (struct RegexPeer *) cls; - if (NULL != emsg || NULL == op || NULL == ca_result) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg); - GNUNET_assert(0); - } + if ((NULL != emsg)||(NULL == op)||(NULL == ca_result)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg); + GNUNET_assert (0); + } - GNUNET_assert(NULL != peer->dht_handle); - GNUNET_assert(peer->op_handle == op); - GNUNET_assert(peer->dht_handle == ca_result); + GNUNET_assert (NULL != peer->dht_handle); + GNUNET_assert (peer->op_handle == op); + GNUNET_assert (peer->dht_handle == ca_result); peer->search_str_matched = GNUNET_NO; - peer->search_handle = REGEX_INTERNAL_search(peer->dht_handle, - peer->search_str, - ®ex_found_handler, peer, - NULL); - peer->prof_start_time = GNUNET_TIME_absolute_get(); + peer->search_handle = REGEX_INTERNAL_search (peer->dht_handle, + peer->search_str, + ®ex_found_handler, peer, + NULL); + peer->prof_start_time = GNUNET_TIME_absolute_get (); } @@ -1068,11 +1071,11 @@ dht_connect_cb(void *cls, * @return */ static void * -dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) +dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) { struct RegexPeer *peer = cls; - peer->dht_handle = GNUNET_DHT_connect(cfg, 32); + peer->dht_handle = GNUNET_DHT_connect (cfg, 32); return peer->dht_handle; } @@ -1085,23 +1088,23 @@ dht_ca(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg) * @param op_result Service handle returned from the connect adapter. */ static void -dht_da(void *cls, void *op_result) +dht_da (void *cls, void *op_result) { - struct RegexPeer *peer = (struct RegexPeer *)cls; + struct RegexPeer *peer = (struct RegexPeer *) cls; - GNUNET_assert(peer->dht_handle == op_result); + GNUNET_assert (peer->dht_handle == op_result); if (NULL != peer->search_handle) - { - REGEX_INTERNAL_search_cancel(peer->search_handle); - peer->search_handle = NULL; - } + { + REGEX_INTERNAL_search_cancel (peer->search_handle); + peer->search_handle = NULL; + } if (NULL != peer->dht_handle) - { - GNUNET_DHT_disconnect(peer->dht_handle); - peer->dht_handle = NULL; - } + { + GNUNET_DHT_disconnect (peer->dht_handle); + peer->dht_handle = NULL; + } } @@ -1119,51 +1122,52 @@ dht_da(void *cls, void *op_result) * failed */ static void -test_master(void *cls, - struct GNUNET_TESTBED_RunHandle *h, - unsigned int num_peers_, - struct GNUNET_TESTBED_Peer **testbed_peers, - unsigned int links_succeeded, - unsigned int links_failed) +test_master (void *cls, + struct GNUNET_TESTBED_RunHandle *h, + unsigned int num_peers_, + struct GNUNET_TESTBED_Peer **testbed_peers, + unsigned int links_succeeded, + unsigned int links_failed) { unsigned int i; - GNUNET_assert(num_peers_ == num_peers); + GNUNET_assert (num_peers_ == num_peers); - prof_time = GNUNET_TIME_absolute_get_duration(prof_start_time); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Testbed started in %s\n", - GNUNET_STRINGS_relative_time_to_string(prof_time, GNUNET_NO)); + prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Testbed started in %s\n", + GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO)); if (NULL != abort_task) - { - GNUNET_SCHEDULER_cancel(abort_task); - abort_task = NULL; - } + { + GNUNET_SCHEDULER_cancel (abort_task); + abort_task = NULL; + } for (i = 0; i < num_peers; i++) - { - peers[i].peer_handle = testbed_peers[i]; - } + { + peers[i].peer_handle = testbed_peers[i]; + } if (GNUNET_NO == - GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT", "DISABLE_TRY_CONNECT")) - { - struct GNUNET_TIME_Relative settle_time; - - settle_time = - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, - 10 * num_peers); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Waiting for DHT for %s to settle new connections.\n\n", - GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO)); - GNUNET_SCHEDULER_add_delayed(settle_time, &do_announce, NULL); - } + GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT")) + { + struct GNUNET_TIME_Relative settle_time; + + settle_time = + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, + 10 * num_peers); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Waiting for DHT for %s to settle new connections.\n\n", + GNUNET_STRINGS_relative_time_to_string (settle_time, + GNUNET_NO)); + GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL); + } else - { - GNUNET_SCHEDULER_add_now(&do_announce, NULL); - } + { + GNUNET_SCHEDULER_add_now (&do_announce, NULL); + } search_timeout_task = - GNUNET_SCHEDULER_add_delayed(search_timeout_time, &search_timed_out, NULL); + GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL); } /** @@ -1173,23 +1177,23 @@ test_master(void *cls, * @param event information on what is happening */ static void -master_controller_cb(void *cls, - const struct GNUNET_TESTBED_EventInformation *event) +master_controller_cb (void *cls, + const struct GNUNET_TESTBED_EventInformation *event) { switch (event->type) - { - case GNUNET_TESTBED_ET_CONNECT: - printf("."); - break; - - case GNUNET_TESTBED_ET_PEER_START: - printf("#"); - break; - - default: - break; - } - fflush(stdout); + { + case GNUNET_TESTBED_ET_CONNECT: + printf ("."); + break; + + case GNUNET_TESTBED_ET_PEER_START: + printf ("#"); + break; + + default: + break; + } + fflush (stdout); } @@ -1208,9 +1212,9 @@ master_controller_cb(void *cls, * #GNUNET_SYSERR otherwise. */ static int -count_and_separate_strings(char *data, - uint64_t data_size, - unsigned int str_max) +count_and_separate_strings (char *data, + uint64_t data_size, + unsigned int str_max) { char *buf; // Keep track of last string to skip blank lines unsigned int offset; @@ -1220,19 +1224,19 @@ count_and_separate_strings(char *data, offset = 0; str_cnt = 0; while ((offset < (data_size - 1)) && (str_cnt < str_max)) + { + offset++; + if (((data[offset] == '\n')) && + (buf != &data[offset])) { - offset++; - if (((data[offset] == '\n')) && - (buf != &data[offset])) - { - data[offset] = '\0'; - str_cnt++; - buf = &data[offset + 1]; - } - else if ((data[offset] == '\n') || - (data[offset] == '\0')) - buf = &data[offset + 1]; + data[offset] = '\0'; + str_cnt++; + buf = &data[offset + 1]; } + else if ((data[offset] == '\n') || + (data[offset] == '\0')) + buf = &data[offset + 1]; + } return str_cnt; } @@ -1251,37 +1255,37 @@ count_and_separate_strings(char *data, * In case of error @a strings must not be freed. */ static int -create_string_array(char *data, uint64_t data_size, - char ***strings, unsigned int str_cnt) +create_string_array (char *data, uint64_t data_size, + char ***strings, unsigned int str_cnt) { uint64_t offset; uint64_t len; unsigned int i; - *strings = GNUNET_malloc(sizeof(char *) * str_cnt); + *strings = GNUNET_malloc (sizeof(char *) * str_cnt); offset = 0; for (i = 0; i < str_cnt; i++) + { + len = strlen (&data[offset]); + if (offset + len >= data_size) + { + GNUNET_free (*strings); + *strings = NULL; + return GNUNET_SYSERR; + } + if (0 == len) // empty line { - len = strlen(&data[offset]); - if (offset + len >= data_size) - { - GNUNET_free(*strings); - *strings = NULL; - return GNUNET_SYSERR; - } - if (0 == len) // empty line - { - offset++; - i--; - continue; - } - - GNUNET_asprintf(&(*strings)[i], - "%s%s", - regex_prefix, - &data[offset]); - offset += len + 1; + offset++; + i--; + continue; } + + GNUNET_asprintf (&(*strings)[i], + "%s%s", + regex_prefix, + &data[offset]); + offset += len + 1; + } return GNUNET_OK; } @@ -1296,9 +1300,9 @@ create_string_array(char *data, uint64_t data_size, * @return number of strings found in the file. #GNUNET_SYSERR on error. */ static int -load_search_strings(const char *filename, - char ***strings, - unsigned int limit) +load_search_strings (const char *filename, + char ***strings, + unsigned int limit) { char *data; uint64_t filesize; @@ -1306,54 +1310,54 @@ load_search_strings(const char *filename, /* Sanity checks */ if (NULL == filename) - { - return GNUNET_SYSERR; - } - if (GNUNET_YES != GNUNET_DISK_file_test(filename)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Could not find search strings file %s\n", filename); - return GNUNET_SYSERR; - } + { + return GNUNET_SYSERR; + } + if (GNUNET_YES != GNUNET_DISK_file_test (filename)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Could not find search strings file %s\n", filename); + return GNUNET_SYSERR; + } if (GNUNET_OK != - GNUNET_DISK_file_size(filename, - &filesize, - GNUNET_YES, - GNUNET_YES)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Search strings file %s cannot be read.\n", - filename); - return GNUNET_SYSERR; - } + GNUNET_DISK_file_size (filename, + &filesize, + GNUNET_YES, + GNUNET_YES)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Search strings file %s cannot be read.\n", + filename); + return GNUNET_SYSERR; + } if (0 == filesize) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Search strings file %s is empty.\n", - filename); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Search strings file %s is empty.\n", + filename); + return GNUNET_SYSERR; + } /* Read data into memory */ - data = GNUNET_malloc(filesize + 1); - if (filesize != GNUNET_DISK_fn_read(filename, - data, - filesize)) - { - GNUNET_free(data); - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Could not read search strings file %s.\n", - filename); - return GNUNET_SYSERR; - } + data = GNUNET_malloc (filesize + 1); + if (filesize != GNUNET_DISK_fn_read (filename, + data, + filesize)) + { + GNUNET_free (data); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Could not read search strings file %s.\n", + filename); + return GNUNET_SYSERR; + } /* Process buffer and build array */ - str_cnt = count_and_separate_strings(data, filesize, limit); - if (GNUNET_OK != create_string_array(data, filesize, strings, str_cnt)) - { - str_cnt = GNUNET_SYSERR; - } - GNUNET_free(data); + str_cnt = count_and_separate_strings (data, filesize, limit); + if (GNUNET_OK != create_string_array (data, filesize, strings, str_cnt)) + { + str_cnt = GNUNET_SYSERR; + } + GNUNET_free (data); return str_cnt; } @@ -1367,10 +1371,10 @@ load_search_strings(const char *filename, * @param config configuration */ static void -run(void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *config) +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *config) { unsigned int nsearchstrs; unsigned int i; @@ -1380,161 +1384,163 @@ run(void *cls, /* Check config */ if (NULL == config) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("No configuration file given. Exiting\n")); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } - cfg = GNUNET_CONFIGURATION_dup(config); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ("No configuration file given. Exiting\n")); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } + cfg = GNUNET_CONFIGURATION_dup (config); if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string(cfg, "REGEXPROFILER", - "REGEX_PREFIX", - ®ex_prefix)) - { - GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, - "regexprofiler", - "regex_prefix"); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } + GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER", + "REGEX_PREFIX", + ®ex_prefix)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + "regexprofiler", + "regex_prefix"); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number(cfg, "REGEXPROFILER", - "PARALLEL_SEARCHES", - &init_parallel_searches)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Configuration option \"PARALLEL_SEARCHES\" missing." - " Using default (%d)\n", 10); - init_parallel_searches = 10; - } + GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER", + "PARALLEL_SEARCHES", + &init_parallel_searches)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Configuration option \"PARALLEL_SEARCHES\" missing." + " Using default (%d)\n", 10); + init_parallel_searches = 10; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time(cfg, "REGEXPROFILER", - "REANNOUNCE_PERIOD_MAX", - &reannounce_period_max)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "reannounce_period_max not given. Using 10 minutes.\n"); - reannounce_period_max = - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10); - } + GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER", + "REANNOUNCE_PERIOD_MAX", + &reannounce_period_max)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "reannounce_period_max not given. Using 10 minutes.\n"); + reannounce_period_max = + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10); + } /* Check arguments */ if (NULL == policy_dir) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("No policy directory specified on command line. Exiting.\n")); - return; - } - if (GNUNET_YES != GNUNET_DISK_directory_test(policy_dir, GNUNET_YES)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("Specified policies directory does not exist. Exiting.\n")); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } - if (0 >= (int)(num_peers = GNUNET_DISK_directory_scan(policy_dir, NULL, NULL))) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("No files found in `%s'\n"), - policy_dir); - return; - } - GNUNET_CONFIGURATION_set_value_string(cfg, "REGEXPROFILER", - "POLICY_DIR", policy_dir); - if (GNUNET_YES != GNUNET_DISK_file_test(strings_file)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("No search strings file given. Exiting.\n")); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } - nsearchstrs = load_search_strings(strings_file, - &search_strings, - num_peers); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ( + "No policy directory specified on command line. Exiting.\n")); + return; + } + if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ("Specified policies directory does not exist. Exiting.\n")); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } + if (0 >= (int) (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, + NULL))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ("No files found in `%s'\n"), + policy_dir); + return; + } + GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER", + "POLICY_DIR", policy_dir); + if (GNUNET_YES != GNUNET_DISK_file_test (strings_file)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ("No search strings file given. Exiting.\n")); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } + nsearchstrs = load_search_strings (strings_file, + &search_strings, + num_peers); if (num_peers != nsearchstrs) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Error loading search strings.\n"); - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "File (%s) does not contain enough strings (%u/%u).\n", - strings_file, nsearchstrs, num_peers); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Error loading search strings.\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "File (%s) does not contain enough strings (%u/%u).\n", + strings_file, nsearchstrs, num_peers); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } if ((0 == num_peers) || (NULL == search_strings)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - _("Error loading search strings. Exiting.\n")); - GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _ ("Error loading search strings. Exiting.\n")); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + return; + } for (i = 0; i < num_peers; i++) - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "search string: %s\n", - search_strings[i]); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "search string: %s\n", + search_strings[i]); /* Check logfile */ if ((NULL != data_filename) && (NULL == (data_file = - GNUNET_DISK_file_open(data_filename, - GNUNET_DISK_OPEN_READWRITE | - GNUNET_DISK_OPEN_TRUNCATE | - GNUNET_DISK_OPEN_CREATE, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE)))) - { - GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, - "open", - data_filename); - return; - } + GNUNET_DISK_file_open (data_filename, + GNUNET_DISK_OPEN_READWRITE + | GNUNET_DISK_OPEN_TRUNCATE + | GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ + | GNUNET_DISK_PERM_USER_WRITE)))) + { + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, + "open", + data_filename); + return; + } /* Initialize peers */ - peers = GNUNET_malloc(sizeof(struct RegexPeer) * num_peers); + peers = GNUNET_malloc (sizeof(struct RegexPeer) * num_peers); for (i = 0; i < num_peers; i++) peers[i].id = i; - GNUNET_CONFIGURATION_set_value_number(cfg, - "TESTBED", "OVERLAY_RANDOM_LINKS", - num_peers * 20); - GNUNET_CONFIGURATION_set_value_number(cfg, - "DHT", "FORCE_NSE", - (long long unsigned) - (log(num_peers) / log(2.0))); + GNUNET_CONFIGURATION_set_value_number (cfg, + "TESTBED", "OVERLAY_RANDOM_LINKS", + num_peers * 20); + GNUNET_CONFIGURATION_set_value_number (cfg, + "DHT", "FORCE_NSE", + (long long unsigned) + (log (num_peers) / log (2.0))); event_mask = 0LL; /* For feedback about the start process activate these and pass master_cb */ event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START); // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP); event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT); // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT); - prof_start_time = GNUNET_TIME_absolute_get(); - GNUNET_TESTBED_run(hosts_file, - cfg, - num_peers, - event_mask, - &master_controller_cb, - NULL, /* master_controller_cb cls */ - &test_master, - NULL); /* test_master cls */ + prof_start_time = GNUNET_TIME_absolute_get (); + GNUNET_TESTBED_run (hosts_file, + cfg, + num_peers, + event_mask, + &master_controller_cb, + NULL, /* master_controller_cb cls */ + &test_master, + NULL); /* test_master cls */ if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time(cfg, "TESTBED", - "SETUP_TIMEOUT", - &abort_time)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "SETUP_TIMEOUT not given. Using 15 minutes.\n"); - abort_time = - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15); - } - abort_time = GNUNET_TIME_relative_add(abort_time, GNUNET_TIME_UNIT_MINUTES); + GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED", + "SETUP_TIMEOUT", + &abort_time)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "SETUP_TIMEOUT not given. Using 15 minutes.\n"); + abort_time = + GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15); + } + abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES); abort_task = - GNUNET_SCHEDULER_add_delayed(abort_time, - &do_abort, - (void*)__LINE__); - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "setup_timeout: %s\n", - GNUNET_STRINGS_relative_time_to_string(abort_time, GNUNET_YES)); + GNUNET_SCHEDULER_add_delayed (abort_time, + &do_abort, + (void*) __LINE__); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "setup_timeout: %s\n", + GNUNET_STRINGS_relative_time_to_string (abort_time, GNUNET_YES)); } @@ -1546,52 +1552,56 @@ run(void *cls, * @return 0 on success */ int -main(int argc, char *const *argv) +main (int argc, char *const *argv) { struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_option_filename('o', - "output-file", - "FILENAME", - gettext_noop("name of the file for writing statistics"), - &data_filename), - - GNUNET_GETOPT_option_relative_time('t', - "matching-timeout", - "TIMEOUT", - gettext_noop("wait TIMEOUT before ending the experiment"), - &search_timeout_time), - - GNUNET_GETOPT_option_filename('p', - "policy-dir", - "DIRECTORY", - gettext_noop("directory with policy files"), - &policy_dir), - - - GNUNET_GETOPT_option_filename('s', - "strings-file", - "FILENAME", - gettext_noop("name of file with input strings"), - &strings_file), - - GNUNET_GETOPT_option_filename('H', - "hosts-file", - "FILENAME", - gettext_noop("name of file with hosts' names"), - &hosts_file), + GNUNET_GETOPT_option_filename ('o', + "output-file", + "FILENAME", + gettext_noop ( + "name of the file for writing statistics"), + &data_filename), + + GNUNET_GETOPT_option_relative_time ('t', + "matching-timeout", + "TIMEOUT", + gettext_noop ( + "wait TIMEOUT before ending the experiment"), + &search_timeout_time), + + GNUNET_GETOPT_option_filename ('p', + "policy-dir", + "DIRECTORY", + gettext_noop ("directory with policy files"), + &policy_dir), + + + GNUNET_GETOPT_option_filename ('s', + "strings-file", + "FILENAME", + gettext_noop ( + "name of file with input strings"), + &strings_file), + + GNUNET_GETOPT_option_filename ('H', + "hosts-file", + "FILENAME", + gettext_noop ( + "name of file with hosts' names"), + &hosts_file), GNUNET_GETOPT_OPTION_END }; int ret; - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 2; result = GNUNET_SYSERR; ret = - GNUNET_PROGRAM_run(argc, argv, - "gnunet-regex-profiler", - _("Profiler for regex"), - options, &run, NULL); + GNUNET_PROGRAM_run (argc, argv, + "gnunet-regex-profiler", + _ ("Profiler for regex"), + options, &run, NULL); if (GNUNET_OK != ret) return ret; if (GNUNET_OK != result) diff --git a/src/regex/gnunet-regex-simulation-profiler.c b/src/regex/gnunet-regex-simulation-profiler.c index aa297d2ea..6637bafed 100644 --- a/src/regex/gnunet-regex-simulation-profiler.c +++ b/src/regex/gnunet-regex-simulation-profiler.c @@ -52,7 +52,8 @@ * Simple struct to keep track of progress, and print a * nice little percentage meter for long running tasks. */ -struct ProgressMeter { +struct ProgressMeter +{ /** * Total number of elements. */ @@ -167,11 +168,11 @@ static char *regex_prefix; * @return the progress meter */ static struct ProgressMeter * -create_meter(unsigned int total, char *start_string, int print) +create_meter (unsigned int total, char *start_string, int print) { struct ProgressMeter *ret; - ret = GNUNET_new(struct ProgressMeter); + ret = GNUNET_new (struct ProgressMeter); ret->print = print; ret->total = total; ret->modnum = total / 4; @@ -179,9 +180,9 @@ create_meter(unsigned int total, char *start_string, int print) ret->modnum = 1; ret->dotnum = (total / 50) + 1; if (start_string != NULL) - ret->startup_string = GNUNET_strdup(start_string); + ret->startup_string = GNUNET_strdup (start_string); else - ret->startup_string = GNUNET_strdup(""); + ret->startup_string = GNUNET_strdup (""); return ret; } @@ -196,33 +197,33 @@ create_meter(unsigned int total, char *start_string, int print) * GNUNET_NO if more items expected */ static int -update_meter(struct ProgressMeter *meter) +update_meter (struct ProgressMeter *meter) { if (meter->print == GNUNET_YES) + { + if (meter->completed % meter->modnum == 0) { - if (meter->completed % meter->modnum == 0) - { - if (meter->completed == 0) - { - fprintf(stdout, "%sProgress: [0%%", meter->startup_string); - } - else - fprintf(stdout, "%d%%", - (int)(((float)meter->completed / meter->total) * 100)); - } - else if (meter->completed % meter->dotnum == 0) - fprintf(stdout, "%s", "."); - - if (meter->completed + 1 == meter->total) - fprintf(stdout, "%d%%]\n", 100); - fflush(stdout); + if (meter->completed == 0) + { + fprintf (stdout, "%sProgress: [0%%", meter->startup_string); + } + else + fprintf (stdout, "%d%%", + (int) (((float) meter->completed / meter->total) * 100)); } + else if (meter->completed % meter->dotnum == 0) + fprintf (stdout, "%s", "."); + + if (meter->completed + 1 == meter->total) + fprintf (stdout, "%d%%]\n", 100); + fflush (stdout); + } meter->completed++; if (meter->completed == meter->total) return GNUNET_YES; if (meter->completed > meter->total) - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n"); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n"); return GNUNET_NO; } @@ -236,7 +237,7 @@ update_meter(struct ProgressMeter *meter) * #GNUNET_SYSERR on error */ static int -reset_meter(struct ProgressMeter *meter) +reset_meter (struct ProgressMeter *meter) { if (meter == NULL) return GNUNET_SYSERR; @@ -252,10 +253,10 @@ reset_meter(struct ProgressMeter *meter) * @param meter the meter to free */ static void -free_meter(struct ProgressMeter *meter) +free_meter (struct ProgressMeter *meter) { - GNUNET_free_non_null(meter->startup_string); - GNUNET_free(meter); + GNUNET_free_non_null (meter->startup_string); + GNUNET_free (meter); } @@ -265,18 +266,18 @@ free_meter(struct ProgressMeter *meter) * @param cls NULL */ static void -do_shutdown(void *cls) +do_shutdown (void *cls) { if (NULL != mysql_ctx) - { - GNUNET_MYSQL_context_destroy(mysql_ctx); - mysql_ctx = NULL; - } + { + GNUNET_MYSQL_context_destroy (mysql_ctx); + mysql_ctx = NULL; + } if (NULL != meter) - { - free_meter(meter); - meter = NULL; - } + { + free_meter (meter); + meter = NULL; + } } @@ -292,16 +293,16 @@ do_shutdown(void *cls) * @param cls NULL */ static void -do_abort(void *cls) +do_abort (void *cls) { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Aborting\n"); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n"); if (NULL != scan_task) - { - GNUNET_SCHEDULER_cancel(scan_task); - scan_task = NULL; - } + { + GNUNET_SCHEDULER_cancel (scan_task); + scan_task = NULL; + } result = GNUNET_SYSERR; - GNUNET_SCHEDULER_shutdown(); + GNUNET_SCHEDULER_shutdown (); } /** @@ -315,131 +316,131 @@ do_abort(void *cls) * @param edges edges leaving current state. */ static void -regex_iterator(void *cls, - const struct GNUNET_HashCode *key, - const char *proof, - int accepting, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges) +regex_iterator (void *cls, + const struct GNUNET_HashCode *key, + const char *proof, + int accepting, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges) { unsigned int i; int result; - uint32_t iaccepting = (uint32_t)accepting; + uint32_t iaccepting = (uint32_t) accepting; uint64_t total; - GNUNET_assert(NULL != mysql_ctx); + GNUNET_assert (NULL != mysql_ctx); for (i = 0; i < num_edges; i++) + { + struct GNUNET_MY_QueryParam params_select[] = { + GNUNET_MY_query_param_auto_from_type (key), + GNUNET_MY_query_param_string (edges[i].label), + GNUNET_MY_query_param_end + }; + + struct GNUNET_MY_ResultSpec results_select[] = { + GNUNET_MY_result_spec_uint64 (&total), + GNUNET_MY_result_spec_end + }; + + result = + GNUNET_MY_exec_prepared (mysql_ctx, + select_stmt_handle, + params_select); + + if (GNUNET_SYSERR == result) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Error executing prepared mysql select statement\n"); + GNUNET_SCHEDULER_add_now (&do_abort, NULL); + return; + } + + result = + GNUNET_MY_extract_result (select_stmt_handle, + results_select); + + if (GNUNET_SYSERR == result) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Error extracting result mysql select statement\n"); + GNUNET_SCHEDULER_add_now (&do_abort, NULL); + return; + } + + if ((-1 != total) &&(total > 0) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", + (unsigned long long) total, + GNUNET_h2s (key), edges[i].label); + } + + struct GNUNET_MY_QueryParam params_stmt[] = { + GNUNET_MY_query_param_auto_from_type (&key), + GNUNET_MY_query_param_string (edges[i].label), + GNUNET_MY_query_param_auto_from_type (&edges[i].destination), + GNUNET_MY_query_param_uint32 (&iaccepting), + GNUNET_MY_query_param_end + }; + + result = + GNUNET_MY_exec_prepared (mysql_ctx, + stmt_handle, + params_stmt); + + if (0 == result) + { + char *key_str = GNUNET_strdup (GNUNET_h2s (key)); + char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination)); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", + key_str, + edges[i].label, + to_key_str, + accepting); + + GNUNET_free (key_str); + GNUNET_free (to_key_str); + num_merged_transitions++; + } + else if (-1 != total) + { + num_merged_states++; + } + + if ((GNUNET_SYSERR == result) || ((1 != result) &&(0 != result) )) { - struct GNUNET_MY_QueryParam params_select[] = { - GNUNET_MY_query_param_auto_from_type(key), - GNUNET_MY_query_param_string(edges[i].label), - GNUNET_MY_query_param_end - }; - - struct GNUNET_MY_ResultSpec results_select[] = { - GNUNET_MY_result_spec_uint64(&total), - GNUNET_MY_result_spec_end - }; - - result = - GNUNET_MY_exec_prepared(mysql_ctx, - select_stmt_handle, - params_select); - - if (GNUNET_SYSERR == result) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Error executing prepared mysql select statement\n"); - GNUNET_SCHEDULER_add_now(&do_abort, NULL); - return; - } - - result = - GNUNET_MY_extract_result(select_stmt_handle, - results_select); - - if (GNUNET_SYSERR == result) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Error extracting result mysql select statement\n"); - GNUNET_SCHEDULER_add_now(&do_abort, NULL); - return; - } - - if (-1 != total && total > 0) - { - GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", - (unsigned long long)total, - GNUNET_h2s(key), edges[i].label); - } - - struct GNUNET_MY_QueryParam params_stmt[] = { - GNUNET_MY_query_param_auto_from_type(&key), - GNUNET_MY_query_param_string(edges[i].label), - GNUNET_MY_query_param_auto_from_type(&edges[i].destination), - GNUNET_MY_query_param_uint32(&iaccepting), - GNUNET_MY_query_param_end - }; - - result = - GNUNET_MY_exec_prepared(mysql_ctx, - stmt_handle, - params_stmt); - - if (0 == result) - { - char *key_str = GNUNET_strdup(GNUNET_h2s(key)); - char *to_key_str = GNUNET_strdup(GNUNET_h2s(&edges[i].destination)); - - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", - key_str, - edges[i].label, - to_key_str, - accepting); - - GNUNET_free(key_str); - GNUNET_free(to_key_str); - num_merged_transitions++; - } - else if (-1 != total) - { - num_merged_states++; - } - - if (GNUNET_SYSERR == result || (1 != result && 0 != result)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", - result); - GNUNET_SCHEDULER_add_now(&do_abort, NULL); - } + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", + result); + GNUNET_SCHEDULER_add_now (&do_abort, NULL); } + } if (0 == num_edges) + { + struct GNUNET_MY_QueryParam params_stmt[] = { + GNUNET_MY_query_param_auto_from_type (key), + GNUNET_MY_query_param_string (""), + GNUNET_MY_query_param_fixed_size (NULL, 0), + GNUNET_MY_query_param_uint32 (&iaccepting), + GNUNET_MY_query_param_end + }; + + result = + GNUNET_MY_exec_prepared (mysql_ctx, + stmt_handle, + params_stmt); + + if ((1 != result) &&(0 != result) ) { - struct GNUNET_MY_QueryParam params_stmt[] = { - GNUNET_MY_query_param_auto_from_type(key), - GNUNET_MY_query_param_string(""), - GNUNET_MY_query_param_fixed_size(NULL, 0), - GNUNET_MY_query_param_uint32(&iaccepting), - GNUNET_MY_query_param_end - }; - - result = - GNUNET_MY_exec_prepared(mysql_ctx, - stmt_handle, - params_stmt); - - if (1 != result && 0 != result) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", - result); - GNUNET_SCHEDULER_add_now(&do_abort, NULL); - } + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n", + result); + GNUNET_SCHEDULER_add_now (&do_abort, NULL); } + } } @@ -451,26 +452,26 @@ regex_iterator(void *cls, * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure. */ static int -announce_regex(const char *regex) +announce_regex (const char *regex) { struct REGEX_INTERNAL_Automaton *dfa; dfa = - REGEX_INTERNAL_construct_dfa(regex, - strlen(regex), - max_path_compression); + REGEX_INTERNAL_construct_dfa (regex, + strlen (regex), + max_path_compression); if (NULL == dfa) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to create DFA for regex %s\n", - regex); - GNUNET_SCHEDULER_add_now(&do_abort, NULL); - return GNUNET_SYSERR; - } - REGEX_INTERNAL_iterate_all_edges(dfa, - ®ex_iterator, NULL); - REGEX_INTERNAL_automaton_destroy(dfa); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to create DFA for regex %s\n", + regex); + GNUNET_SCHEDULER_add_now (&do_abort, NULL); + return GNUNET_SYSERR; + } + REGEX_INTERNAL_iterate_all_edges (dfa, + ®ex_iterator, NULL); + REGEX_INTERNAL_automaton_destroy (dfa); return GNUNET_OK; } @@ -485,7 +486,7 @@ announce_regex(const char *regex) * #GNUNET_SYSERR to abort iteration with error! */ static int -policy_filename_cb(void *cls, const char *filename) +policy_filename_cb (void *cls, const char *filename) { char *regex; char *data; @@ -493,70 +494,70 @@ policy_filename_cb(void *cls, const char *filename) uint64_t filesize; unsigned int offset; - GNUNET_assert(NULL != filename); + GNUNET_assert (NULL != filename); - GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Announcing regexes from file %s\n", - filename); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Announcing regexes from file %s\n", + filename); - if (GNUNET_YES != GNUNET_DISK_file_test(filename)) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Could not find policy file %s\n", - filename); - return GNUNET_OK; - } + if (GNUNET_YES != GNUNET_DISK_file_test (filename)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Could not find policy file %s\n", + filename); + return GNUNET_OK; + } if (GNUNET_OK != - GNUNET_DISK_file_size(filename, &filesize, - GNUNET_YES, GNUNET_YES)) + GNUNET_DISK_file_size (filename, &filesize, + GNUNET_YES, GNUNET_YES)) filesize = 0; if (0 == filesize) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", - filename); - return GNUNET_OK; - } - data = GNUNET_malloc(filesize); - if (filesize != GNUNET_DISK_fn_read(filename, data, filesize)) - { - GNUNET_free(data); - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Could not read policy file %s.\n", - filename); - return GNUNET_OK; - } - - update_meter(meter); + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", + filename); + return GNUNET_OK; + } + data = GNUNET_malloc (filesize); + if (filesize != GNUNET_DISK_fn_read (filename, data, filesize)) + { + GNUNET_free (data); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Could not read policy file %s.\n", + filename); + return GNUNET_OK; + } + + update_meter (meter); buf = data; offset = 0; regex = NULL; while (offset < (filesize - 1)) + { + offset++; + if (((data[offset] == '\n')) && (buf != &data[offset])) { - offset++; - if (((data[offset] == '\n')) && (buf != &data[offset])) - { - data[offset] = '|'; - num_policies++; - buf = &data[offset + 1]; - } - else if ((data[offset] == '\n') || (data[offset] == '\0')) - buf = &data[offset + 1]; + data[offset] = '|'; + num_policies++; + buf = &data[offset + 1]; } + else if ((data[offset] == '\n') || (data[offset] == '\0')) + buf = &data[offset + 1]; + } data[offset] = '\0'; - GNUNET_asprintf(®ex, "%s(%s)", regex_prefix, data); - GNUNET_assert(NULL != regex); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Announcing regex: %s\n", regex); - - if (GNUNET_OK != announce_regex(regex)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not announce regex %s\n", - regex); - } - GNUNET_free(regex); - GNUNET_free(data); + GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data); + GNUNET_assert (NULL != regex); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Announcing regex: %s\n", regex); + + if (GNUNET_OK != announce_regex (regex)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not announce regex %s\n", + regex); + } + GNUNET_free (regex); + GNUNET_free (data); return GNUNET_OK; } @@ -567,7 +568,7 @@ policy_filename_cb(void *cls, const char *filename) * @param cls NULL */ static void -do_directory_scan(void *cls) +do_directory_scan (void *cls) { struct GNUNET_TIME_Absolute start_time; struct GNUNET_TIME_Relative duration; @@ -575,37 +576,37 @@ do_directory_scan(void *cls) /* Create an MySQL prepared statement for the inserts */ scan_task = NULL; - GNUNET_asprintf(&stmt, INSERT_EDGE_STMT, table_name); - stmt_handle = GNUNET_MYSQL_statement_prepare(mysql_ctx, stmt); - GNUNET_free(stmt); - - GNUNET_asprintf(&stmt, SELECT_KEY_STMT, table_name); - select_stmt_handle = GNUNET_MYSQL_statement_prepare(mysql_ctx, stmt); - GNUNET_free(stmt); - - GNUNET_assert(NULL != stmt_handle); - - meter = create_meter(num_policy_files, - "Announcing policy files\n", - GNUNET_YES); - start_time = GNUNET_TIME_absolute_get(); - GNUNET_DISK_directory_scan(policy_dir, - &policy_filename_cb, - stmt_handle); - duration = GNUNET_TIME_absolute_get_duration(start_time); - reset_meter(meter); - free_meter(meter); + GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name); + stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt); + GNUNET_free (stmt); + + GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name); + select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt); + GNUNET_free (stmt); + + GNUNET_assert (NULL != stmt_handle); + + meter = create_meter (num_policy_files, + "Announcing policy files\n", + GNUNET_YES); + start_time = GNUNET_TIME_absolute_get (); + GNUNET_DISK_directory_scan (policy_dir, + &policy_filename_cb, + stmt_handle); + duration = GNUNET_TIME_absolute_get_duration (start_time); + reset_meter (meter); + free_meter (meter); meter = NULL; - printf("Announced %u files containing %u policies in %s\n" - "Duplicate transitions: %llu\nMerged states: %llu\n", - num_policy_files, - num_policies, - GNUNET_STRINGS_relative_time_to_string(duration, GNUNET_NO), - num_merged_transitions, - num_merged_states); + printf ("Announced %u files containing %u policies in %s\n" + "Duplicate transitions: %llu\nMerged states: %llu\n", + num_policy_files, + num_policies, + GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO), + num_merged_transitions, + num_merged_states); result = GNUNET_OK; - GNUNET_SCHEDULER_shutdown(); + GNUNET_SCHEDULER_shutdown (); } @@ -618,65 +619,65 @@ do_directory_scan(void *cls) * @param config configuration */ static void -run(void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *config) +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *config) { if (NULL == args[0]) - { - fprintf(stderr, - _("No policy directory specified on command line. Exiting.\n")); - result = GNUNET_SYSERR; - return; - } + { + fprintf (stderr, + _ ("No policy directory specified on command line. Exiting.\n")); + result = GNUNET_SYSERR; + return; + } if (GNUNET_YES != - GNUNET_DISK_directory_test(args[0], GNUNET_YES)) - { - fprintf(stderr, - _("Specified policies directory does not exist. Exiting.\n")); - result = GNUNET_SYSERR; - return; - } + GNUNET_DISK_directory_test (args[0], GNUNET_YES)) + { + fprintf (stderr, + _ ("Specified policies directory does not exist. Exiting.\n")); + result = GNUNET_SYSERR; + return; + } policy_dir = args[0]; - num_policy_files = GNUNET_DISK_directory_scan(policy_dir, - NULL, NULL); + num_policy_files = GNUNET_DISK_directory_scan (policy_dir, + NULL, NULL); meter = NULL; if (NULL == table_name) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "No table name specified, using default \"NFA\".\n"); - table_name = "NFA"; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "No table name specified, using default \"NFA\".\n"); + table_name = "NFA"; + } - mysql_ctx = GNUNET_MYSQL_context_create(config, "regex-mysql"); + mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql"); if (NULL == mysql_ctx) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Failed to create mysql context\n"); - result = GNUNET_SYSERR; - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to create mysql context\n"); + result = GNUNET_SYSERR; + return; + } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string(config, - "regex-mysql", - "REGEX_PREFIX", - ®ex_prefix)) - { - GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, - "regex-mysql", - "REGEX_PREFIX"); - result = GNUNET_SYSERR; - return; - } + GNUNET_CONFIGURATION_get_value_string (config, + "regex-mysql", + "REGEX_PREFIX", + ®ex_prefix)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + "regex-mysql", + "REGEX_PREFIX"); + result = GNUNET_SYSERR; + return; + } result = GNUNET_OK; - GNUNET_SCHEDULER_add_shutdown(&do_shutdown, - NULL); - scan_task = GNUNET_SCHEDULER_add_now(&do_directory_scan, NULL); + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + NULL); + scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL); } @@ -688,33 +689,34 @@ run(void *cls, * @return 0 on success */ int -main(int argc, char *const *argv) +main (int argc, char *const *argv) { struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_option_string('t', - "table", - "TABLENAME", - gettext_noop("name of the table to write DFAs"), - &table_name), - - GNUNET_GETOPT_option_uint('p', - "max-path-compression", - "MAX_PATH_COMPRESSION", - gettext_noop("maximum path compression length"), - &max_path_compression), + GNUNET_GETOPT_option_string ('t', + "table", + "TABLENAME", + gettext_noop ( + "name of the table to write DFAs"), + &table_name), + + GNUNET_GETOPT_option_uint ('p', + "max-path-compression", + "MAX_PATH_COMPRESSION", + gettext_noop ("maximum path compression length"), + &max_path_compression), GNUNET_GETOPT_OPTION_END }; int ret; - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 2; result = GNUNET_SYSERR; ret = - GNUNET_PROGRAM_run(argc, argv, - "gnunet-regex-simulationprofiler [OPTIONS] policy-dir", - _("Profiler for regex library"), options, &run, NULL); + GNUNET_PROGRAM_run (argc, argv, + "gnunet-regex-simulationprofiler [OPTIONS] policy-dir", + _ ("Profiler for regex library"), options, &run, NULL); if (GNUNET_OK != ret) return ret; if (GNUNET_OK != result) diff --git a/src/regex/gnunet-service-regex.c b/src/regex/gnunet-service-regex.c index 90ea91780..19250d427 100644 --- a/src/regex/gnunet-service-regex.c +++ b/src/regex/gnunet-service-regex.c @@ -33,7 +33,8 @@ /** * Information about one of our clients. */ -struct ClientEntry { +struct ClientEntry +{ /** * Queue for transmissions to @e client. */ @@ -88,14 +89,14 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; * @param cls unused */ static void -cleanup_task(void *cls) +cleanup_task (void *cls) { - GNUNET_DHT_disconnect(dht); + GNUNET_DHT_disconnect (dht); dht = NULL; - GNUNET_STATISTICS_destroy(stats, - GNUNET_NO); + GNUNET_STATISTICS_destroy (stats, + GNUNET_NO); stats = NULL; - GNUNET_free(my_private_key); + GNUNET_free (my_private_key); my_private_key = NULL; } @@ -107,14 +108,14 @@ cleanup_task(void *cls) * announcement */ static void -reannounce(void *cls) +reannounce (void *cls) { struct ClientEntry *ce = cls; - REGEX_INTERNAL_reannounce(ce->ah); - ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency, - &reannounce, - ce); + REGEX_INTERNAL_reannounce (ce->ah); + ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, + &reannounce, + ce); } @@ -126,18 +127,18 @@ reannounce(void *cls) * @return #GNUNET_OK if @am is well-formed */ static int -check_announce(void *cls, - const struct AnnounceMessage *am) +check_announce (void *cls, + const struct AnnounceMessage *am) { struct ClientEntry *ce = cls; - GNUNET_MQ_check_zero_termination(am); + GNUNET_MQ_check_zero_termination (am); if (NULL != ce->ah) - { - /* only one announcement per client allowed */ - GNUNET_break(0); - return GNUNET_SYSERR; - } + { + /* only one announcement per client allowed */ + GNUNET_break (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -149,36 +150,36 @@ check_announce(void *cls, * @param am the actual message */ static void -handle_announce(void *cls, - const struct AnnounceMessage *am) +handle_announce (void *cls, + const struct AnnounceMessage *am) { struct ClientEntry *ce = cls; const char *regex; - regex = (const char *)&am[1]; - ce->frequency = GNUNET_TIME_relative_ntoh(am->refresh_delay); - ce->refresh_task = GNUNET_SCHEDULER_add_delayed(ce->frequency, - &reannounce, - ce); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Starting to announce regex `%s' every %s\n", - regex, - GNUNET_STRINGS_relative_time_to_string(ce->frequency, - GNUNET_NO)); - ce->ah = REGEX_INTERNAL_announce(dht, - my_private_key, - regex, - ntohs(am->compression), - stats); + regex = (const char *) &am[1]; + ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay); + ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency, + &reannounce, + ce); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting to announce regex `%s' every %s\n", + regex, + GNUNET_STRINGS_relative_time_to_string (ce->frequency, + GNUNET_NO)); + ce->ah = REGEX_INTERNAL_announce (dht, + my_private_key, + regex, + ntohs (am->compression), + stats); if (NULL == ce->ah) - { - GNUNET_break(0); - GNUNET_SCHEDULER_cancel(ce->refresh_task); - ce->refresh_task = NULL; - GNUNET_SERVICE_client_drop(ce->client); - return; - } - GNUNET_SERVICE_client_continue(ce->client); + { + GNUNET_break (0); + GNUNET_SCHEDULER_cancel (ce->refresh_task); + ce->refresh_task = NULL; + GNUNET_SERVICE_client_drop (ce->client); + return; + } + GNUNET_SERVICE_client_continue (ce->client); } @@ -193,12 +194,12 @@ handle_announce(void *cls, * @param put_path_length Length of the @a put_path. */ static void -handle_search_result(void *cls, - const struct GNUNET_PeerIdentity *id, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length) +handle_search_result (void *cls, + const struct GNUNET_PeerIdentity *id, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length) { struct ClientEntry *ce = cls; struct GNUNET_MQ_Envelope *env; @@ -208,28 +209,30 @@ handle_search_result(void *cls, if ((get_path_length >= 65536) || (put_path_length >= 65536) || - ((get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity)) - + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) - { - GNUNET_break(0); - return; - } - size = (get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity); - env = GNUNET_MQ_msg_extra(result, - size, - GNUNET_MESSAGE_TYPE_REGEX_RESULT); - result->get_path_length = htons((uint16_t)get_path_length); - result->put_path_length = htons((uint16_t)put_path_length); + ( ((get_path_length + put_path_length) * sizeof(struct + GNUNET_PeerIdentity)) + + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) ) + { + GNUNET_break (0); + return; + } + size = (get_path_length + put_path_length) * sizeof(struct + GNUNET_PeerIdentity); + env = GNUNET_MQ_msg_extra (result, + size, + GNUNET_MESSAGE_TYPE_REGEX_RESULT); + result->get_path_length = htons ((uint16_t) get_path_length); + result->put_path_length = htons ((uint16_t) put_path_length); result->id = *id; gp = &result->id; - GNUNET_memcpy(&gp[1], - get_path, - get_path_length * sizeof(struct GNUNET_PeerIdentity)); - GNUNET_memcpy(&gp[1 + get_path_length], - put_path, - put_path_length * sizeof(struct GNUNET_PeerIdentity)); - GNUNET_MQ_send(ce->mq, - env); + GNUNET_memcpy (&gp[1], + get_path, + get_path_length * sizeof(struct GNUNET_PeerIdentity)); + GNUNET_memcpy (&gp[1 + get_path_length], + put_path, + put_path_length * sizeof(struct GNUNET_PeerIdentity)); + GNUNET_MQ_send (ce->mq, + env); } @@ -240,26 +243,26 @@ handle_search_result(void *cls, * @param message the actual message */ static int -check_search(void *cls, - const struct RegexSearchMessage *sm) +check_search (void *cls, + const struct RegexSearchMessage *sm) { struct ClientEntry *ce = cls; const char *string; uint16_t size; - size = ntohs(sm->header.size) - sizeof(*sm); - string = (const char *)&sm[1]; + size = ntohs (sm->header.size) - sizeof(*sm); + string = (const char *) &sm[1]; if ('\0' != string[size - 1]) - { - GNUNET_break(0); - return GNUNET_SYSERR; - } + { + GNUNET_break (0); + return GNUNET_SYSERR; + } if (NULL != ce->sh) - { - /* only one search allowed per client */ - GNUNET_break(0); - return GNUNET_SYSERR; - } + { + /* only one search allowed per client */ + GNUNET_break (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -271,28 +274,28 @@ check_search(void *cls, * @param message the actual message */ static void -handle_search(void *cls, - const struct RegexSearchMessage *sm) +handle_search (void *cls, + const struct RegexSearchMessage *sm) { struct ClientEntry *ce = cls; const char *string; - string = (const char *)&sm[1]; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Starting to search for `%s'\n", - string); - ce->sh = REGEX_INTERNAL_search(dht, - string, - &handle_search_result, - ce, - stats); + string = (const char *) &sm[1]; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting to search for `%s'\n", + string); + ce->sh = REGEX_INTERNAL_search (dht, + string, + &handle_search_result, + ce, + stats); if (NULL == ce->sh) - { - GNUNET_break(0); - GNUNET_SERVICE_client_drop(ce->client); - return; - } - GNUNET_SERVICE_client_continue(ce->client); + { + GNUNET_break (0); + GNUNET_SERVICE_client_drop (ce->client); + return; + } + GNUNET_SERVICE_client_continue (ce->client); } @@ -304,27 +307,27 @@ handle_search(void *cls, * @param service the initialized service */ static void -run(void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_SERVICE_Handle *service) +run (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_SERVICE_Handle *service) { - my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg); + my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); if (NULL == my_private_key) - { - GNUNET_SCHEDULER_shutdown(); - return; - } - dht = GNUNET_DHT_connect(cfg, 1024); + { + GNUNET_SCHEDULER_shutdown (); + return; + } + dht = GNUNET_DHT_connect (cfg, 1024); if (NULL == dht) - { - GNUNET_free(my_private_key); - my_private_key = NULL; - GNUNET_SCHEDULER_shutdown(); - return; - } - GNUNET_SCHEDULER_add_shutdown(&cleanup_task, - NULL); - stats = GNUNET_STATISTICS_create("regex", cfg); + { + GNUNET_free (my_private_key); + my_private_key = NULL; + GNUNET_SCHEDULER_shutdown (); + return; + } + GNUNET_SCHEDULER_add_shutdown (&cleanup_task, + NULL); + stats = GNUNET_STATISTICS_create ("regex", cfg); } @@ -337,13 +340,13 @@ run(void *cls, * @return @a c */ static void * -client_connect_cb(void *cls, - struct GNUNET_SERVICE_Client *c, - struct GNUNET_MQ_Handle *mq) +client_connect_cb (void *cls, + struct GNUNET_SERVICE_Client *c, + struct GNUNET_MQ_Handle *mq) { struct ClientEntry *ce; - ce = GNUNET_new(struct ClientEntry); + ce = GNUNET_new (struct ClientEntry); ce->client = c; ce->mq = mq; return ce; @@ -358,28 +361,28 @@ client_connect_cb(void *cls, * @param internal_cls should be equal to @a c */ static void -client_disconnect_cb(void *cls, - struct GNUNET_SERVICE_Client *c, - void *internal_cls) +client_disconnect_cb (void *cls, + struct GNUNET_SERVICE_Client *c, + void *internal_cls) { struct ClientEntry *ce = internal_cls; if (NULL != ce->refresh_task) - { - GNUNET_SCHEDULER_cancel(ce->refresh_task); - ce->refresh_task = NULL; - } + { + GNUNET_SCHEDULER_cancel (ce->refresh_task); + ce->refresh_task = NULL; + } if (NULL != ce->ah) - { - REGEX_INTERNAL_announce_cancel(ce->ah); - ce->ah = NULL; - } + { + REGEX_INTERNAL_announce_cancel (ce->ah); + ce->ah = NULL; + } if (NULL != ce->sh) - { - REGEX_INTERNAL_search_cancel(ce->sh); - ce->sh = NULL; - } - GNUNET_free(ce); + { + REGEX_INTERNAL_search_cancel (ce->sh); + ce->sh = NULL; + } + GNUNET_free (ce); } @@ -393,15 +396,15 @@ GNUNET_SERVICE_MAIN &client_connect_cb, &client_disconnect_cb, NULL, - GNUNET_MQ_hd_var_size(announce, - GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, - struct AnnounceMessage, - NULL), - GNUNET_MQ_hd_var_size(search, - GNUNET_MESSAGE_TYPE_REGEX_SEARCH, - struct RegexSearchMessage, - NULL), - GNUNET_MQ_handler_end()); + GNUNET_MQ_hd_var_size (announce, + GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, + struct AnnounceMessage, + NULL), + GNUNET_MQ_hd_var_size (search, + GNUNET_MESSAGE_TYPE_REGEX_SEARCH, + struct RegexSearchMessage, + NULL), + GNUNET_MQ_handler_end ()); /* end of gnunet-service-regex.c */ diff --git a/src/regex/perf-regex.c b/src/regex/perf-regex.c index 25960dc9f..d41887f6b 100644 --- a/src/regex/perf-regex.c +++ b/src/regex/perf-regex.c @@ -42,23 +42,23 @@ * @param edges edges leaving current state. */ static void -print_edge(void *cls, - const struct GNUNET_HashCode *key, - const char *proof, - int accepting, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges) +print_edge (void *cls, + const struct GNUNET_HashCode *key, + const char *proof, + int accepting, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges) { unsigned int i; - printf("%s: %s, proof: `%s'\n", - GNUNET_h2s(key), - accepting ? "ACCEPTING" : "", - proof); + printf ("%s: %s, proof: `%s'\n", + GNUNET_h2s (key), + accepting ? "ACCEPTING" : "", + proof); for (i = 0; i < num_edges; i++) - printf(" `%s': %s\n", - edges[i].label, - GNUNET_h2s(&edges[i].destination)); + printf (" `%s': %s\n", + edges[i].label, + GNUNET_h2s (&edges[i].destination)); } @@ -73,9 +73,9 @@ print_edge(void *cls, * @return 0 ok, 1 on error */ int -main(int argc, char *const *argv) +main (int argc, char *const *argv) { - struct REGEX_INTERNAL_Automaton* dfa; + struct REGEX_INTERNAL_Automaton*dfa; char **regexes; char *buffer; char *regex; @@ -83,45 +83,45 @@ main(int argc, char *const *argv) unsigned int alphabet_size; long size; - GNUNET_log_setup("perf-regex", "DEBUG", NULL); + GNUNET_log_setup ("perf-regex", "DEBUG", NULL); if (4 != argc) - { - fprintf(stderr, - "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n", - argv[0]); - return 1; - } - regexes = REGEX_TEST_read_from_file(argv[1]); + { + fprintf (stderr, + "Usage: %s REGEX_FILE ALPHABET_SIZE COMPRESSION\n", + argv[0]); + return 1; + } + regexes = REGEX_TEST_read_from_file (argv[1]); if (NULL == regexes) - { - fprintf(stderr, - "Failed to read regexes from `%s'\n", - argv[1]); - return 2; - } - alphabet_size = atoi(argv[2]); - compression = atoi(argv[3]); - printf("********* PERF-REGEX *********'\n"); - printf("Using:\n file '%s'\n Alphabet size %u\n compression %d\n", - argv[1], alphabet_size, compression); - fflush(stdout); - buffer = REGEX_TEST_combine(regexes, alphabet_size); - GNUNET_asprintf(®ex, "GNUNET_REGEX_PROFILER_(%s)(0|1)*", buffer); - size = strlen(regex); + { + fprintf (stderr, + "Failed to read regexes from `%s'\n", + argv[1]); + return 2; + } + alphabet_size = atoi (argv[2]); + compression = atoi (argv[3]); + printf ("********* PERF-REGEX *********'\n"); + printf ("Using:\n file '%s'\n Alphabet size %u\n compression %d\n", + argv[1], alphabet_size, compression); + fflush (stdout); + buffer = REGEX_TEST_combine (regexes, alphabet_size); + GNUNET_asprintf (®ex, "GNUNET_REGEX_PROFILER_(%s)(0|1)*", buffer); + size = strlen (regex); - fprintf(stderr, - "Combined regex (%ld bytes):\n%s\n", - size, - regex); - dfa = REGEX_INTERNAL_construct_dfa(regex, size, compression); - printf("********* ALL EDGES *********'\n"); - REGEX_INTERNAL_iterate_all_edges(dfa, &print_edge, NULL); - printf("\n\n********* REACHABLE EDGES *********'\n"); - REGEX_INTERNAL_iterate_reachable_edges(dfa, &print_edge, NULL); - REGEX_INTERNAL_automaton_destroy(dfa); - GNUNET_free(buffer); - REGEX_TEST_free_from_file(regexes); - GNUNET_free(regex); + fprintf (stderr, + "Combined regex (%ld bytes):\n%s\n", + size, + regex); + dfa = REGEX_INTERNAL_construct_dfa (regex, size, compression); + printf ("********* ALL EDGES *********'\n"); + REGEX_INTERNAL_iterate_all_edges (dfa, &print_edge, NULL); + printf ("\n\n********* REACHABLE EDGES *********'\n"); + REGEX_INTERNAL_iterate_reachable_edges (dfa, &print_edge, NULL); + REGEX_INTERNAL_automaton_destroy (dfa); + GNUNET_free (buffer); + REGEX_TEST_free_from_file (regexes); + GNUNET_free (regex); return 0; } diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c index 9467c02b9..e8ff158dd 100644 --- a/src/regex/plugin_block_regex.c +++ b/src/regex/plugin_block_regex.c @@ -57,37 +57,38 @@ * by this @a type of block (this is not an error) */ static struct GNUNET_BLOCK_Group * -block_plugin_regex_create_group(void *cls, - enum GNUNET_BLOCK_Type type, - uint32_t nonce, - const void *raw_data, - size_t raw_data_size, - va_list va) +block_plugin_regex_create_group (void *cls, + enum GNUNET_BLOCK_Type type, + uint32_t nonce, + const void *raw_data, + size_t raw_data_size, + va_list va) { unsigned int bf_size; const char *guard; - guard = va_arg(va, const char *); - if (0 == strcmp(guard, - "seen-set-size")) - bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size(va_arg(va, unsigned int), - BLOOMFILTER_K); - else if (0 == strcmp(guard, - "filter-size")) - bf_size = va_arg(va, unsigned int); + guard = va_arg (va, const char *); + if (0 == strcmp (guard, + "seen-set-size")) + bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned + int), + BLOOMFILTER_K); + else if (0 == strcmp (guard, + "filter-size")) + bf_size = va_arg (va, unsigned int); else - { - GNUNET_break(0); - bf_size = REGEX_BF_SIZE; - } - GNUNET_break(NULL == va_arg(va, const char *)); - return GNUNET_BLOCK_GROUP_bf_create(cls, - bf_size, - BLOOMFILTER_K, - type, - nonce, - raw_data, - raw_data_size); + { + GNUNET_break (0); + bf_size = REGEX_BF_SIZE; + } + GNUNET_break (NULL == va_arg (va, const char *)); + return GNUNET_BLOCK_GROUP_bf_create (cls, + bf_size, + BLOOMFILTER_K, + type, + nonce, + raw_data, + raw_data_size); } @@ -111,74 +112,74 @@ block_plugin_regex_create_group(void *cls, * @return characterization of result */ static enum GNUNET_BLOCK_EvaluationResult -evaluate_block_regex(void *cls, - enum GNUNET_BLOCK_Type type, - struct GNUNET_BLOCK_Group *bg, - enum GNUNET_BLOCK_EvaluationOptions eo, - const struct GNUNET_HashCode *query, - const void *xquery, - size_t xquery_size, - const void *reply_block, - size_t reply_block_size) +evaluate_block_regex (void *cls, + enum GNUNET_BLOCK_Type type, + struct GNUNET_BLOCK_Group *bg, + enum GNUNET_BLOCK_EvaluationOptions eo, + const struct GNUNET_HashCode *query, + const void *xquery, + size_t xquery_size, + const void *reply_block, + size_t reply_block_size) { struct GNUNET_HashCode chash; if (NULL == reply_block) - { - if (0 != xquery_size) - { - const char *s; - - s = (const char *)xquery; - if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; - } - } - return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; - } - if (0 != xquery_size) + { + if (0 != xquery_size) { const char *s; - s = (const char *)xquery; - if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; - } + s = (const char *) xquery; + if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; + } } - else if (NULL != query) + return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; + } + if (0 != xquery_size) + { + const char *s; + + s = (const char *) xquery; + if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ { - /* xquery is required for regex GETs, at least an empty string */ - GNUNET_break_op(0); - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n", - type, query, xquery); + GNUNET_break_op (0); return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; } - switch (REGEX_BLOCK_check(reply_block, - reply_block_size, - query, - xquery)) - { - case GNUNET_SYSERR: - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; - - case GNUNET_NO: - /* xquery missmatch, can happen */ - return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT; - - default: - break; - } - GNUNET_CRYPTO_hash(reply_block, - reply_block_size, - &chash); + } + else if (NULL != query) + { + /* xquery is required for regex GETs, at least an empty string */ + GNUNET_break_op (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n", + type, query, xquery); + return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; + } + switch (REGEX_BLOCK_check (reply_block, + reply_block_size, + query, + xquery)) + { + case GNUNET_SYSERR: + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + + case GNUNET_NO: + /* xquery missmatch, can happen */ + return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT; + + default: + break; + } + GNUNET_CRYPTO_hash (reply_block, + reply_block_size, + &chash); if (GNUNET_YES == - GNUNET_BLOCK_GROUP_bf_test_and_set(bg, - &chash)) + GNUNET_BLOCK_GROUP_bf_test_and_set (bg, + &chash)) return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; return GNUNET_BLOCK_EVALUATION_OK_MORE; } @@ -204,60 +205,62 @@ evaluate_block_regex(void *cls, * @return characterization of result */ static enum GNUNET_BLOCK_EvaluationResult -evaluate_block_regex_accept(void *cls, - enum GNUNET_BLOCK_Type type, - struct GNUNET_BLOCK_Group *bg, - enum GNUNET_BLOCK_EvaluationOptions eo, - const struct GNUNET_HashCode *query, - const void *xquery, - size_t xquery_size, const void *reply_block, - size_t reply_block_size) +evaluate_block_regex_accept (void *cls, + enum GNUNET_BLOCK_Type type, + struct GNUNET_BLOCK_Group *bg, + enum GNUNET_BLOCK_EvaluationOptions eo, + const struct GNUNET_HashCode *query, + const void *xquery, + size_t xquery_size, const void *reply_block, + size_t reply_block_size) { const struct RegexAcceptBlock *rba; struct GNUNET_HashCode chash; if (0 != xquery_size) - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; - } + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; + } if (NULL == reply_block) return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; if (sizeof(struct RegexAcceptBlock) != reply_block_size) - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; - } + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } rba = reply_block; - if (ntohl(rba->purpose.size) != - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + - sizeof(struct GNUNET_TIME_AbsoluteNBO) + - sizeof(struct GNUNET_HashCode)) - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; - } - if (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(rba->expiration_time)).rel_value_us) - { - /* technically invalid, but can happen without an error, so - we're nice by reporting it as a 'duplicate' */ - return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; - } + if (ntohl (rba->purpose.size) != + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + + sizeof(struct GNUNET_TIME_AbsoluteNBO) + + sizeof(struct GNUNET_HashCode)) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } + if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh ( + rba->expiration_time)). + rel_value_us) + { + /* technically invalid, but can happen without an error, so + we're nice by reporting it as a 'duplicate' */ + return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; + } if (GNUNET_OK != - GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, - &rba->purpose, - &rba->signature, - &rba->peer.public_key)) - { - GNUNET_break_op(0); - return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; - } - GNUNET_CRYPTO_hash(reply_block, - reply_block_size, - &chash); + GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, + &rba->purpose, + &rba->signature, + &rba->peer.public_key)) + { + GNUNET_break_op (0); + return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; + } + GNUNET_CRYPTO_hash (reply_block, + reply_block_size, + &chash); if (GNUNET_YES == - GNUNET_BLOCK_GROUP_bf_test_and_set(bg, - &chash)) + GNUNET_BLOCK_GROUP_bf_test_and_set (bg, + &chash)) return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; return GNUNET_BLOCK_EVALUATION_OK_MORE; } @@ -283,44 +286,44 @@ evaluate_block_regex_accept(void *cls, * @return characterization of result */ static enum GNUNET_BLOCK_EvaluationResult -block_plugin_regex_evaluate(void *cls, - struct GNUNET_BLOCK_Context *ctx, - enum GNUNET_BLOCK_Type type, - struct GNUNET_BLOCK_Group *bg, - enum GNUNET_BLOCK_EvaluationOptions eo, - const struct GNUNET_HashCode *query, - const void *xquery, - size_t xquery_size, - const void *reply_block, - size_t reply_block_size) +block_plugin_regex_evaluate (void *cls, + struct GNUNET_BLOCK_Context *ctx, + enum GNUNET_BLOCK_Type type, + struct GNUNET_BLOCK_Group *bg, + enum GNUNET_BLOCK_EvaluationOptions eo, + const struct GNUNET_HashCode *query, + const void *xquery, + size_t xquery_size, + const void *reply_block, + size_t reply_block_size) { enum GNUNET_BLOCK_EvaluationResult result; switch (type) - { - case GNUNET_BLOCK_TYPE_REGEX: - result = evaluate_block_regex(cls, - type, - bg, - eo, - query, - xquery, xquery_size, - reply_block, reply_block_size); - break; - - case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: - result = evaluate_block_regex_accept(cls, - type, - bg, - eo, - query, - xquery, xquery_size, - reply_block, reply_block_size); - break; - - default: - result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; - } + { + case GNUNET_BLOCK_TYPE_REGEX: + result = evaluate_block_regex (cls, + type, + bg, + eo, + query, + xquery, xquery_size, + reply_block, reply_block_size); + break; + + case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: + result = evaluate_block_regex_accept (cls, + type, + bg, + eo, + query, + xquery, xquery_size, + reply_block, reply_block_size); + break; + + default: + result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; + } return result; } @@ -337,37 +340,37 @@ block_plugin_regex_evaluate(void *cls, * (or if extracting a key from a block of this type does not work) */ static int -block_plugin_regex_get_key(void *cls, - enum GNUNET_BLOCK_Type type, - const void *block, - size_t block_size, - struct GNUNET_HashCode *key) +block_plugin_regex_get_key (void *cls, + enum GNUNET_BLOCK_Type type, + const void *block, + size_t block_size, + struct GNUNET_HashCode *key) { switch (type) + { + case GNUNET_BLOCK_TYPE_REGEX: + if (GNUNET_OK != + REGEX_BLOCK_get_key (block, block_size, + key)) + { + GNUNET_break_op (0); + return GNUNET_NO; + } + return GNUNET_OK; + + case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: + if (sizeof(struct RegexAcceptBlock) != block_size) { - case GNUNET_BLOCK_TYPE_REGEX: - if (GNUNET_OK != - REGEX_BLOCK_get_key(block, block_size, - key)) - { - GNUNET_break_op(0); - return GNUNET_NO; - } - return GNUNET_OK; - - case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: - if (sizeof(struct RegexAcceptBlock) != block_size) - { - GNUNET_break_op(0); - return GNUNET_NO; - } - *key = ((struct RegexAcceptBlock *)block)->key; - return GNUNET_OK; - - default: - GNUNET_break(0); - return GNUNET_SYSERR; + GNUNET_break_op (0); + return GNUNET_NO; } + *key = ((struct RegexAcceptBlock *) block)->key; + return GNUNET_OK; + + default: + GNUNET_break (0); + return GNUNET_SYSERR; + } } @@ -375,17 +378,16 @@ block_plugin_regex_get_key(void *cls, * Entry point for the plugin. */ void * -libgnunet_plugin_block_regex_init(void *cls) +libgnunet_plugin_block_regex_init (void *cls) { - static enum GNUNET_BLOCK_Type types[] = - { + static enum GNUNET_BLOCK_Type types[] = { GNUNET_BLOCK_TYPE_REGEX, GNUNET_BLOCK_TYPE_REGEX_ACCEPT, GNUNET_BLOCK_TYPE_ANY /* end of list */ }; struct GNUNET_BLOCK_PluginFunctions *api; - api = GNUNET_new(struct GNUNET_BLOCK_PluginFunctions); + api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); api->evaluate = &block_plugin_regex_evaluate; api->get_key = &block_plugin_regex_get_key; api->create_group = &block_plugin_regex_create_group; @@ -398,11 +400,11 @@ libgnunet_plugin_block_regex_init(void *cls) * Exit point from the plugin. */ void * -libgnunet_plugin_block_regex_done(void *cls) +libgnunet_plugin_block_regex_done (void *cls) { struct GNUNET_BLOCK_PluginFunctions *api = cls; - GNUNET_free(api); + GNUNET_free (api); return NULL; } diff --git a/src/regex/regex_api_announce.c b/src/regex/regex_api_announce.c index 3b5801994..318d73263 100644 --- a/src/regex/regex_api_announce.c +++ b/src/regex/regex_api_announce.c @@ -29,12 +29,13 @@ #include "gnunet_regex_service.h" #include "regex_ipc.h" -#define LOG(kind, ...) GNUNET_log_from(kind, "regex-api", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from (kind, "regex-api", __VA_ARGS__) /** * Handle to store cached data about a regex announce. */ -struct GNUNET_REGEX_Announcement { +struct GNUNET_REGEX_Announcement +{ /** * Connection to the regex service. */ @@ -69,7 +70,7 @@ struct GNUNET_REGEX_Announcement { * @param a REGEX to announce. */ static void -announce_reconnect(struct GNUNET_REGEX_Announcement *a); +announce_reconnect (struct GNUNET_REGEX_Announcement *a); /** @@ -80,14 +81,14 @@ announce_reconnect(struct GNUNET_REGEX_Announcement *a); * @param error error code */ static void -announce_mq_error_handler(void *cls, - enum GNUNET_MQ_Error error) +announce_mq_error_handler (void *cls, + enum GNUNET_MQ_Error error) { struct GNUNET_REGEX_Announcement *a = cls; - GNUNET_MQ_destroy(a->mq); + GNUNET_MQ_destroy (a->mq); a->mq = NULL; - announce_reconnect(a); + announce_reconnect (a); } @@ -97,31 +98,31 @@ announce_mq_error_handler(void *cls, * @param a REGEX to announce. */ static void -announce_reconnect(struct GNUNET_REGEX_Announcement *a) +announce_reconnect (struct GNUNET_REGEX_Announcement *a) { struct GNUNET_MQ_Envelope *env; struct AnnounceMessage *am; size_t slen; - a->mq = GNUNET_CLIENT_connect(a->cfg, - "regex", - NULL, - &announce_mq_error_handler, - a); + a->mq = GNUNET_CLIENT_connect (a->cfg, + "regex", + NULL, + &announce_mq_error_handler, + a); if (NULL == a->mq) return; - slen = strlen(a->regex) + 1; - env = GNUNET_MQ_msg_extra(am, - slen, - GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE); - am->compression = htons(a->compression); - am->reserved = htons(0); - am->refresh_delay = GNUNET_TIME_relative_hton(a->refresh_delay); - GNUNET_memcpy(&am[1], - a->regex, - slen); - GNUNET_MQ_send(a->mq, - env); + slen = strlen (a->regex) + 1; + env = GNUNET_MQ_msg_extra (am, + slen, + GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE); + am->compression = htons (a->compression); + am->reserved = htons (0); + am->refresh_delay = GNUNET_TIME_relative_hton (a->refresh_delay); + GNUNET_memcpy (&am[1], + a->regex, + slen); + GNUNET_MQ_send (a->mq, + env); } @@ -136,35 +137,35 @@ announce_reconnect(struct GNUNET_REGEX_Announcement *a) * Must be freed by calling #GNUNET_REGEX_announce_cancel(). */ struct GNUNET_REGEX_Announcement * -GNUNET_REGEX_announce(const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *regex, - struct GNUNET_TIME_Relative refresh_delay, - uint16_t compression) +GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *regex, + struct GNUNET_TIME_Relative refresh_delay, + uint16_t compression) { struct GNUNET_REGEX_Announcement *a; size_t slen; - slen = strlen(regex) + 1; + slen = strlen (regex) + 1; if (slen + sizeof(struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - _("Regex `%s' is too long!\n"), - regex); - GNUNET_break(0); - return NULL; - } - a = GNUNET_new(struct GNUNET_REGEX_Announcement); + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ ("Regex `%s' is too long!\n"), + regex); + GNUNET_break (0); + return NULL; + } + a = GNUNET_new (struct GNUNET_REGEX_Announcement); a->cfg = cfg; a->refresh_delay = refresh_delay; a->compression = compression; - a->regex = GNUNET_strdup(regex); - announce_reconnect(a); + a->regex = GNUNET_strdup (regex); + announce_reconnect (a); if (NULL == a->mq) - { - GNUNET_free(a->regex); - GNUNET_free(a); - return NULL; - } + { + GNUNET_free (a->regex); + GNUNET_free (a); + return NULL; + } return a; } @@ -175,11 +176,11 @@ GNUNET_REGEX_announce(const struct GNUNET_CONFIGURATION_Handle *cfg, * @param a handle returned by a previous #GNUNET_REGEX_announce() call. */ void -GNUNET_REGEX_announce_cancel(struct GNUNET_REGEX_Announcement *a) +GNUNET_REGEX_announce_cancel (struct GNUNET_REGEX_Announcement *a) { - GNUNET_MQ_destroy(a->mq); - GNUNET_free(a->regex); - GNUNET_free(a); + GNUNET_MQ_destroy (a->mq); + GNUNET_free (a->regex); + GNUNET_free (a); } /* end of regex_api_announce.c */ diff --git a/src/regex/regex_api_search.c b/src/regex/regex_api_search.c index 6a6a49a81..960719cbd 100644 --- a/src/regex/regex_api_search.c +++ b/src/regex/regex_api_search.c @@ -30,13 +30,14 @@ #include "gnunet_regex_service.h" #include "regex_ipc.h" -#define LOG(kind, ...) GNUNET_log_from(kind, "regex-api", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from (kind, "regex-api", __VA_ARGS__) /** * Handle to store data about a regex search. */ -struct GNUNET_REGEX_Search { +struct GNUNET_REGEX_Search +{ /** * Connection to the regex service. */ @@ -70,7 +71,7 @@ struct GNUNET_REGEX_Search { * @param s context for the search search for */ static void -search_reconnect(struct GNUNET_REGEX_Search *s); +search_reconnect (struct GNUNET_REGEX_Search *s); /** @@ -82,18 +83,18 @@ search_reconnect(struct GNUNET_REGEX_Search *s); * @return #GNUNET_SYSERR if @a rm is not well-formed. */ static int -check_search_response(void *cls, - const struct ResultMessage *result) +check_search_response (void *cls, + const struct ResultMessage *result) { - uint16_t size = ntohs(result->header.size) - sizeof(*result); - uint16_t gpl = ntohs(result->get_path_length); - uint16_t ppl = ntohs(result->put_path_length); + uint16_t size = ntohs (result->header.size) - sizeof(*result); + uint16_t gpl = ntohs (result->get_path_length); + uint16_t ppl = ntohs (result->put_path_length); if (size != (gpl + ppl) * sizeof(struct GNUNET_PeerIdentity)) - { - GNUNET_break(0); - return GNUNET_SYSERR; - } + { + GNUNET_break (0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -106,24 +107,24 @@ check_search_response(void *cls, * @param result the message */ static void -handle_search_response(void *cls, - const struct ResultMessage *result) +handle_search_response (void *cls, + const struct ResultMessage *result) { struct GNUNET_REGEX_Search *s = cls; - uint16_t gpl = ntohs(result->get_path_length); - uint16_t ppl = ntohs(result->put_path_length); + uint16_t gpl = ntohs (result->get_path_length); + uint16_t ppl = ntohs (result->put_path_length); const struct GNUNET_PeerIdentity *pid; pid = &result->id; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Got regex result %s\n", - GNUNET_i2s(pid)); - s->callback(s->callback_cls, - pid, - &pid[1], - gpl, - &pid[1 + gpl], - ppl); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Got regex result %s\n", + GNUNET_i2s (pid)); + s->callback (s->callback_cls, + pid, + &pid[1], + gpl, + &pid[1 + gpl], + ppl); } @@ -135,14 +136,14 @@ handle_search_response(void *cls, * @param error error code */ static void -mq_error_handler(void *cls, - enum GNUNET_MQ_Error error) +mq_error_handler (void *cls, + enum GNUNET_MQ_Error error) { struct GNUNET_REGEX_Search *s = cls; - GNUNET_MQ_destroy(s->mq); + GNUNET_MQ_destroy (s->mq); s->mq = NULL; - search_reconnect(s); + search_reconnect (s); } @@ -152,35 +153,35 @@ mq_error_handler(void *cls, * @param s context for the search search for */ static void -search_reconnect(struct GNUNET_REGEX_Search *s) +search_reconnect (struct GNUNET_REGEX_Search *s) { struct GNUNET_MQ_MessageHandler handlers[] = { - GNUNET_MQ_hd_var_size(search_response, - GNUNET_MESSAGE_TYPE_REGEX_RESULT, - struct ResultMessage, - s), - GNUNET_MQ_handler_end() + GNUNET_MQ_hd_var_size (search_response, + GNUNET_MESSAGE_TYPE_REGEX_RESULT, + struct ResultMessage, + s), + GNUNET_MQ_handler_end () }; - size_t slen = strlen(s->string) + 1; + size_t slen = strlen (s->string) + 1; struct GNUNET_MQ_Envelope *env; struct RegexSearchMessage *rsm; - GNUNET_assert(NULL == s->mq); - s->mq = GNUNET_CLIENT_connect(s->cfg, - "regex", - handlers, - &mq_error_handler, - s); + GNUNET_assert (NULL == s->mq); + s->mq = GNUNET_CLIENT_connect (s->cfg, + "regex", + handlers, + &mq_error_handler, + s); if (NULL == s->mq) return; - env = GNUNET_MQ_msg_extra(rsm, - slen, - GNUNET_MESSAGE_TYPE_REGEX_SEARCH); - GNUNET_memcpy(&rsm[1], - s->string, - slen); - GNUNET_MQ_send(s->mq, - env); + env = GNUNET_MQ_msg_extra (rsm, + slen, + GNUNET_MESSAGE_TYPE_REGEX_SEARCH); + GNUNET_memcpy (&rsm[1], + s->string, + slen); + GNUNET_MQ_send (s->mq, + env); } @@ -197,37 +198,37 @@ search_reconnect(struct GNUNET_REGEX_Search *s) * Must be freed by calling #GNUNET_REGEX_search_cancel(). */ struct GNUNET_REGEX_Search * -GNUNET_REGEX_search(const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *string, - GNUNET_REGEX_Found callback, - void *callback_cls) +GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *string, + GNUNET_REGEX_Found callback, + void *callback_cls) { struct GNUNET_REGEX_Search *s; - size_t slen = strlen(string) + 1; + size_t slen = strlen (string) + 1; if (slen + sizeof(struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - _("Search string `%s' is too long!\n"), - string); - GNUNET_break(0); - return NULL; - } - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Starting regex search for %s\n", - string); - s = GNUNET_new(struct GNUNET_REGEX_Search); + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _ ("Search string `%s' is too long!\n"), + string); + GNUNET_break (0); + return NULL; + } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Starting regex search for %s\n", + string); + s = GNUNET_new (struct GNUNET_REGEX_Search); s->cfg = cfg; - s->string = GNUNET_strdup(string); + s->string = GNUNET_strdup (string); s->callback = callback; s->callback_cls = callback_cls; - search_reconnect(s); + search_reconnect (s); if (NULL == s->mq) - { - GNUNET_free(s->string); - GNUNET_free(s); - return NULL; - } + { + GNUNET_free (s->string); + GNUNET_free (s); + return NULL; + } return s; } @@ -238,11 +239,11 @@ GNUNET_REGEX_search(const struct GNUNET_CONFIGURATION_Handle *cfg, * @param s Handle returned by a previous #GNUNET_REGEX_search() call. */ void -GNUNET_REGEX_search_cancel(struct GNUNET_REGEX_Search *s) +GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s) { - GNUNET_MQ_destroy(s->mq); - GNUNET_free(s->string); - GNUNET_free(s); + GNUNET_MQ_destroy (s->mq); + GNUNET_free (s->string); + GNUNET_free (s); } diff --git a/src/regex/regex_block_lib.c b/src/regex/regex_block_lib.c index f0feb3628..2c6752e09 100644 --- a/src/regex/regex_block_lib.c +++ b/src/regex/regex_block_lib.c @@ -27,14 +27,15 @@ #include "regex_block_lib.h" #include "gnunet_constants.h" -#define LOG(kind, ...) GNUNET_log_from(kind, "regex-bck", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from (kind, "regex-bck", __VA_ARGS__) GNUNET_NETWORK_STRUCT_BEGIN /** * Information for each edge. */ -struct EdgeInfo { +struct EdgeInfo +{ /** * Index of the destination of this edge in the * unique destinations array. @@ -52,7 +53,8 @@ struct EdgeInfo { /** * @brief Block to announce a regex state. */ -struct RegexBlock { +struct RegexBlock +{ /** * Length of the proof regex string. */ @@ -96,15 +98,15 @@ GNUNET_NETWORK_STRUCT_END * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not */ int -GNUNET_BLOCK_is_accepting(const struct RegexBlock *block, - size_t size) +GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, + size_t size) { if (size < sizeof(struct RegexBlock)) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - return ntohs(block->is_accepting); + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + return ntohs (block->is_accepting); } @@ -117,27 +119,28 @@ GNUNET_BLOCK_is_accepting(const struct RegexBlock *block, * @return #GNUNET_OK if the proof is valid for the given key. */ int -REGEX_BLOCK_check_proof(const char *proof, - size_t proof_len, - const struct GNUNET_HashCode *key) +REGEX_BLOCK_check_proof (const char *proof, + size_t proof_len, + const struct GNUNET_HashCode *key) { struct GNUNET_HashCode key_check; if ((NULL == proof) || (NULL == key)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n"); - return GNUNET_NO; - } - GNUNET_CRYPTO_hash(proof, proof_len, &key_check); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n"); + return GNUNET_NO; + } + GNUNET_CRYPTO_hash (proof, proof_len, &key_check); return (0 == - GNUNET_CRYPTO_hash_cmp(key, &key_check)) ? GNUNET_OK : GNUNET_NO; + GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO; } /** * Struct to keep track of the xquery while iterating all the edges in a block. */ -struct CheckEdgeContext { +struct CheckEdgeContext +{ /** * Xquery: string we are looking for. */ @@ -161,24 +164,24 @@ struct CheckEdgeContext { * @return #GNUNET_YES, to keep iterating */ static int -check_edge(void *cls, - const char *token, - size_t len, - const struct GNUNET_HashCode *key) +check_edge (void *cls, + const char *token, + size_t len, + const struct GNUNET_HashCode *key) { struct CheckEdgeContext *ctx = cls; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "edge %.*s [%u]: %s\n", - (int)len, - token, - (unsigned int)len, - GNUNET_h2s(key)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "edge %.*s [%u]: %s\n", + (int) len, + token, + (unsigned int) len, + GNUNET_h2s (key)); if (NULL == ctx->xquery) return GNUNET_YES; - if (strlen(ctx->xquery) < len) + if (strlen (ctx->xquery) < len) return GNUNET_YES; /* too long */ - if (0 == strncmp(ctx->xquery, token, len)) + if (0 == strncmp (ctx->xquery, token, len)) ctx->found = GNUNET_OK; return GNUNET_YES; /* keep checking for malformed data! */ } @@ -197,48 +200,48 @@ check_edge(void *cls, * #GNUNET_SYSERR if the block is invalid. */ int -REGEX_BLOCK_check(const struct RegexBlock *block, - size_t size, - const struct GNUNET_HashCode *query, - const char *xquery) +REGEX_BLOCK_check (const struct RegexBlock *block, + size_t size, + const struct GNUNET_HashCode *query, + const char *xquery) { struct GNUNET_HashCode key; struct CheckEdgeContext ctx; int res; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Block check\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Block check\n"); if (GNUNET_OK != - REGEX_BLOCK_get_key(block, size, - &key)) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - if (NULL != query && - 0 != GNUNET_memcmp(&key, - query)) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - if ((GNUNET_YES == ntohs(block->is_accepting)) && + REGEX_BLOCK_get_key (block, size, + &key)) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + if ((NULL != query)&& + (0 != GNUNET_memcmp (&key, + query)) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + if ((GNUNET_YES == ntohs (block->is_accepting)) && ((NULL == xquery) || ('\0' == xquery[0]))) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - " out! Is accepting: %u, xquery %p\n", - ntohs(block->is_accepting), - xquery); - return GNUNET_OK; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + " out! Is accepting: %u, xquery %p\n", + ntohs (block->is_accepting), + xquery); + return GNUNET_OK; + } ctx.xquery = xquery; ctx.found = GNUNET_NO; - res = REGEX_BLOCK_iterate(block, size, &check_edge, &ctx); + res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx); if (GNUNET_SYSERR == res) return GNUNET_SYSERR; if (NULL == xquery) return GNUNET_YES; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found); return ctx.found; } @@ -252,9 +255,9 @@ REGEX_BLOCK_check(const struct RegexBlock *block, * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed */ int -REGEX_BLOCK_get_key(const struct RegexBlock *block, - size_t block_len, - struct GNUNET_HashCode *key) +REGEX_BLOCK_get_key (const struct RegexBlock *block, + size_t block_len, + struct GNUNET_HashCode *key) { uint16_t len; const struct GNUNET_HashCode *destinations; @@ -264,22 +267,24 @@ REGEX_BLOCK_get_key(const struct RegexBlock *block, size_t total; if (block_len < sizeof(struct RegexBlock)) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - num_destinations = ntohs(block->num_destinations); - num_edges = ntohs(block->num_edges); - len = ntohs(block->proof_len); - destinations = (const struct GNUNET_HashCode *)&block[1]; - edges = (const struct EdgeInfo *)&destinations[num_destinations]; - total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len; + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + num_destinations = ntohs (block->num_destinations); + num_edges = ntohs (block->num_edges); + len = ntohs (block->proof_len); + destinations = (const struct GNUNET_HashCode *) &block[1]; + edges = (const struct EdgeInfo *) &destinations[num_destinations]; + total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct + GNUNET_HashCode) + + num_edges * sizeof(struct EdgeInfo) + len; if (block_len < total) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - GNUNET_CRYPTO_hash(&edges[num_edges], len, key); + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + GNUNET_CRYPTO_hash (&edges[num_edges], len, key); return GNUNET_OK; } @@ -300,10 +305,10 @@ REGEX_BLOCK_get_key(const struct RegexBlock *block, * be errors in further edges. */ int -REGEX_BLOCK_iterate(const struct RegexBlock *block, - size_t size, - REGEX_INTERNAL_EgdeIterator iterator, - void *iter_cls) +REGEX_BLOCK_iterate (const struct RegexBlock *block, + size_t size, + REGEX_INTERNAL_EgdeIterator iterator, + void *iter_cls) { uint16_t len; const struct GNUNET_HashCode *destinations; @@ -315,54 +320,57 @@ REGEX_BLOCK_iterate(const struct RegexBlock *block, unsigned int n; size_t off; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n"); if (size < sizeof(struct RegexBlock)) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } - num_destinations = ntohs(block->num_destinations); - num_edges = ntohs(block->num_edges); - len = ntohs(block->proof_len); - destinations = (const struct GNUNET_HashCode *)&block[1]; - edges = (const struct EdgeInfo *)&destinations[num_destinations]; - aux = (const char *)&edges[num_edges]; - total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len; + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + num_destinations = ntohs (block->num_destinations); + num_edges = ntohs (block->num_edges); + len = ntohs (block->proof_len); + destinations = (const struct GNUNET_HashCode *) &block[1]; + edges = (const struct EdgeInfo *) &destinations[num_destinations]; + aux = (const char *) &edges[num_edges]; + total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct + GNUNET_HashCode) + + num_edges * sizeof(struct EdgeInfo) + len; if (size < total) - { - GNUNET_break_op(0); - return GNUNET_SYSERR; - } + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } for (n = 0; n < num_edges; n++) - total += ntohs(edges[n].token_length); + total += ntohs (edges[n].token_length); if (size != total) - { - fprintf(stderr, "Expected %u, got %u\n", - (unsigned int)size, - (unsigned int)total); - GNUNET_break_op(0); - return GNUNET_SYSERR; - } + { + fprintf (stderr, "Expected %u, got %u\n", + (unsigned int) size, + (unsigned int) total); + GNUNET_break_op (0); + return GNUNET_SYSERR; + } off = len; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Start iterating block of size %u, proof %u, off %u edges %u\n", - size, len, off, n); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Start iterating block of size %u, proof %u, off %u edges %u\n", + size, len, off, n); /* &aux[off] always points to our token */ for (n = 0; n < num_edges; n++) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Edge %u/%u, off %u tokenlen %u (%.*s)\n", - n + 1, num_edges, off, - ntohs(edges[n].token_length), ntohs(edges[n].token_length), - &aux[off]); - if (NULL != iterator) - if (GNUNET_NO == iterator(iter_cls, - &aux[off], - ntohs(edges[n].token_length), - &destinations[ntohs(edges[n].destination_index)])) - return GNUNET_OK; - off += ntohs(edges[n].token_length); - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Edge %u/%u, off %u tokenlen %u (%.*s)\n", + n + 1, num_edges, off, + ntohs (edges[n].token_length), ntohs (edges[n].token_length), + &aux[off]); + if (NULL != iterator) + if (GNUNET_NO == iterator (iter_cls, + &aux[off], + ntohs (edges[n].token_length), + &destinations[ntohs ( + edges[n].destination_index)])) + return GNUNET_OK; + off += ntohs (edges[n].token_length); + } return GNUNET_OK; } @@ -378,11 +386,11 @@ REGEX_BLOCK_iterate(const struct RegexBlock *block, * @return the regex block, NULL on error */ struct RegexBlock * -REGEX_BLOCK_create(const char *proof, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges, - int accepting, - size_t *rsize) +REGEX_BLOCK_create (const char *proof, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges, + int accepting, + size_t *rsize) { struct RegexBlock *block; struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */ @@ -398,64 +406,66 @@ REGEX_BLOCK_create(const char *proof, unsigned int i; char *aux; - len = strlen(proof); + len = strlen (proof); if (len > UINT16_MAX) - { - GNUNET_break(0); - return NULL; - } + { + GNUNET_break (0); + return NULL; + } unique_destinations = 0; total = sizeof(struct RegexBlock) + len; for (i = 0; i < num_edges; i++) + { + slen = strlen (edges[i].label); + if (slen > UINT16_MAX) { - slen = strlen(edges[i].label); - if (slen > UINT16_MAX) - { - GNUNET_break(0); - return NULL; - } - total += slen; - for (j = 0; j < unique_destinations; j++) - if (0 == memcmp(&destinations[j], - &edges[i].destination, - sizeof(struct GNUNET_HashCode))) - break; - if (j >= 1024) - { - GNUNET_break(0); - return NULL; - } - destination_indices[i] = j; - if (j == unique_destinations) - destinations[unique_destinations++] = edges[i].destination; + GNUNET_break (0); + return NULL; } - total += num_edges * sizeof(struct EdgeInfo) + unique_destinations * sizeof(struct GNUNET_HashCode); - if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE) + total += slen; + for (j = 0; j < unique_destinations; j++) + if (0 == memcmp (&destinations[j], + &edges[i].destination, + sizeof(struct GNUNET_HashCode))) + break; + if (j >= 1024) { - GNUNET_break(0); + GNUNET_break (0); return NULL; } - block = GNUNET_malloc(total); - block->proof_len = htons(len); - block->is_accepting = htons(accepting); - block->num_edges = htons(num_edges); - block->num_destinations = htons(unique_destinations); - dests = (struct GNUNET_HashCode *)&block[1]; - GNUNET_memcpy(dests, destinations, sizeof(struct GNUNET_HashCode) * unique_destinations); - edgeinfos = (struct EdgeInfo *)&dests[unique_destinations]; - aux = (char *)&edgeinfos[num_edges]; + destination_indices[i] = j; + if (j == unique_destinations) + destinations[unique_destinations++] = edges[i].destination; + } + total += num_edges * sizeof(struct EdgeInfo) + unique_destinations + * sizeof(struct GNUNET_HashCode); + if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE) + { + GNUNET_break (0); + return NULL; + } + block = GNUNET_malloc (total); + block->proof_len = htons (len); + block->is_accepting = htons (accepting); + block->num_edges = htons (num_edges); + block->num_destinations = htons (unique_destinations); + dests = (struct GNUNET_HashCode *) &block[1]; + GNUNET_memcpy (dests, destinations, sizeof(struct GNUNET_HashCode) + * unique_destinations); + edgeinfos = (struct EdgeInfo *) &dests[unique_destinations]; + aux = (char *) &edgeinfos[num_edges]; off = len; - GNUNET_memcpy(aux, proof, len); + GNUNET_memcpy (aux, proof, len); for (i = 0; i < num_edges; i++) - { - slen = strlen(edges[i].label); - edgeinfos[i].token_length = htons((uint16_t)slen); - edgeinfos[i].destination_index = htons(destination_indices[i]); - GNUNET_memcpy(&aux[off], - edges[i].label, - slen); - off += slen; - } + { + slen = strlen (edges[i].label); + edgeinfos[i].token_length = htons ((uint16_t) slen); + edgeinfos[i].destination_index = htons (destination_indices[i]); + GNUNET_memcpy (&aux[off], + edges[i].label, + slen); + off += slen; + } *rsize = total; return block; } diff --git a/src/regex/regex_block_lib.h b/src/regex/regex_block_lib.h index 6b2d3bf26..3368e3456 100644 --- a/src/regex/regex_block_lib.h +++ b/src/regex/regex_block_lib.h @@ -49,7 +49,8 @@ struct RegexBlock; /** * Edge representation. */ -struct REGEX_BLOCK_Edge { +struct REGEX_BLOCK_Edge +{ /** * Label of the edge. FIXME: might want to not consume exactly * multiples of 8 bits, need length! @@ -72,9 +73,9 @@ struct REGEX_BLOCK_Edge { * @return #GNUNET_OK if the proof is valid for the given key. */ int -REGEX_BLOCK_check_proof(const char *proof, - size_t proof_len, - const struct GNUNET_HashCode *key); +REGEX_BLOCK_check_proof (const char *proof, + size_t proof_len, + const struct GNUNET_HashCode *key); /** @@ -90,10 +91,10 @@ REGEX_BLOCK_check_proof(const char *proof, * #GNUNET_SYSERR if the block is invalid. */ int -REGEX_BLOCK_check(const struct RegexBlock *block, - size_t size, - const struct GNUNET_HashCode *query, - const char *xquery); +REGEX_BLOCK_check (const struct RegexBlock *block, + size_t size, + const struct GNUNET_HashCode *query, + const char *xquery); /* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */ @@ -130,10 +131,10 @@ typedef int * be errors in further edges. */ int -REGEX_BLOCK_iterate(const struct RegexBlock *block, - size_t size, - REGEX_INTERNAL_EgdeIterator iterator, - void *iter_cls); +REGEX_BLOCK_iterate (const struct RegexBlock *block, + size_t size, + REGEX_INTERNAL_EgdeIterator iterator, + void *iter_cls); /** @@ -145,9 +146,9 @@ REGEX_BLOCK_iterate(const struct RegexBlock *block, * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed */ int -REGEX_BLOCK_get_key(const struct RegexBlock *block, - size_t block_len, - struct GNUNET_HashCode *key); +REGEX_BLOCK_get_key (const struct RegexBlock *block, + size_t block_len, + struct GNUNET_HashCode *key); /** @@ -158,8 +159,8 @@ REGEX_BLOCK_get_key(const struct RegexBlock *block, * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not */ int -GNUNET_BLOCK_is_accepting(const struct RegexBlock *block, - size_t block_len); +GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, + size_t block_len); /** @@ -173,11 +174,11 @@ GNUNET_BLOCK_is_accepting(const struct RegexBlock *block, * @return the regex block, NULL on error */ struct RegexBlock * -REGEX_BLOCK_create(const char *proof, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges, - int accepting, - size_t *rsize); +REGEX_BLOCK_create (const char *proof, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges, + int accepting, + size_t *rsize); #if 0 /* keep Emacsens' auto-indent happy */ diff --git a/src/regex/regex_internal.c b/src/regex/regex_internal.c index 34f0d9c42..78c2de8ab 100644 --- a/src/regex/regex_internal.c +++ b/src/regex/regex_internal.c @@ -39,7 +39,8 @@ /** * Set of states using MDLL API. */ -struct REGEX_INTERNAL_StateSet_MDLL { +struct REGEX_INTERNAL_StateSet_MDLL +{ /** * MDLL of states. */ @@ -64,11 +65,11 @@ struct REGEX_INTERNAL_StateSet_MDLL { * @param state state to be appended */ static void -state_set_append(struct REGEX_INTERNAL_StateSet *set, - struct REGEX_INTERNAL_State *state) +state_set_append (struct REGEX_INTERNAL_StateSet *set, + struct REGEX_INTERNAL_State *state) { if (set->off == set->size) - GNUNET_array_grow(set->states, set->size, set->size * 2 + 4); + GNUNET_array_grow (set->states, set->size, set->size * 2 + 4); set->states[set->off++] = state; } @@ -82,14 +83,14 @@ state_set_append(struct REGEX_INTERNAL_StateSet *set, * @return 0 if the strings are the same or both NULL, 1 or -1 if not. */ static int -nullstrcmp(const char *str1, const char *str2) +nullstrcmp (const char *str1, const char *str2) { if ((NULL == str1) != (NULL == str2)) return -1; if ((NULL == str1) && (NULL == str2)) return 0; - return strcmp(str1, str2); + return strcmp (str1, str2); } @@ -103,40 +104,40 @@ nullstrcmp(const char *str1, const char *str2) * @param to_state state to where the transition should point to */ static void -state_add_transition(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_State *from_state, - const char *label, - struct REGEX_INTERNAL_State *to_state) +state_add_transition (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_State *from_state, + const char *label, + struct REGEX_INTERNAL_State *to_state) { struct REGEX_INTERNAL_Transition *t; struct REGEX_INTERNAL_Transition *oth; if (NULL == from_state) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n"); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create Transition.\n"); + return; + } /* Do not add duplicate state transitions */ for (t = from_state->transitions_head; NULL != t; t = t->next) - { - if (t->to_state == to_state && 0 == nullstrcmp(t->label, label) && - t->from_state == from_state) - return; - } + { + if ((t->to_state == to_state) &&(0 == nullstrcmp (t->label, label)) && + (t->from_state == from_state) ) + return; + } /* sort transitions by label */ for (oth = from_state->transitions_head; NULL != oth; oth = oth->next) - { - if (0 < nullstrcmp(oth->label, label)) - break; - } + { + if (0 < nullstrcmp (oth->label, label)) + break; + } - t = GNUNET_new(struct REGEX_INTERNAL_Transition); + t = GNUNET_new (struct REGEX_INTERNAL_Transition); if (NULL != ctx) t->id = ctx->transition_id++; if (NULL != label) - t->label = GNUNET_strdup(label); + t->label = GNUNET_strdup (label); else t->label = NULL; t->to_state = to_state; @@ -144,10 +145,10 @@ state_add_transition(struct REGEX_INTERNAL_Context *ctx, /* Add outgoing transition to 'from_state' */ from_state->transition_count++; - GNUNET_CONTAINER_DLL_insert_before(from_state->transitions_head, - from_state->transitions_tail, - oth, - t); + GNUNET_CONTAINER_DLL_insert_before (from_state->transitions_head, + from_state->transitions_tail, + oth, + t); } @@ -158,23 +159,23 @@ state_add_transition(struct REGEX_INTERNAL_Context *ctx, * @param transition transition that should be removed from state 'state'. */ static void -state_remove_transition(struct REGEX_INTERNAL_State *state, - struct REGEX_INTERNAL_Transition *transition) +state_remove_transition (struct REGEX_INTERNAL_State *state, + struct REGEX_INTERNAL_Transition *transition) { - if (NULL == state || NULL == transition) + if ((NULL == state)||(NULL == transition)) return; if (transition->from_state != state) return; - GNUNET_free_non_null(transition->label); + GNUNET_free_non_null (transition->label); state->transition_count--; - GNUNET_CONTAINER_DLL_remove(state->transitions_head, - state->transitions_tail, - transition); + GNUNET_CONTAINER_DLL_remove (state->transitions_head, + state->transitions_tail, + transition); - GNUNET_free(transition); + GNUNET_free (transition); } @@ -189,10 +190,10 @@ state_remove_transition(struct REGEX_INTERNAL_State *state, * less than, equal to, or greater than the second. */ static int -state_compare(const void *a, const void *b) +state_compare (const void *a, const void *b) { - struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **)a; - struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **)b; + struct REGEX_INTERNAL_State **s1 = (struct REGEX_INTERNAL_State **) a; + struct REGEX_INTERNAL_State **s2 = (struct REGEX_INTERNAL_State **) b; return (*s1)->id - (*s2)->id; } @@ -208,7 +209,7 @@ state_compare(const void *a, const void *b) * @return number of edges. */ static unsigned int -state_get_edges(struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges) +state_get_edges (struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges) { struct REGEX_INTERNAL_Transition *t; unsigned int count; @@ -219,14 +220,14 @@ state_get_edges(struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges) count = 0; for (t = s->transitions_head; NULL != t; t = t->next) + { + if (NULL != t->to_state) { - if (NULL != t->to_state) - { - edges[count].label = t->label; - edges[count].destination = t->to_state->hash; - count++; - } + edges[count].label = t->label; + edges[count].destination = t->to_state->hash; + count++; } + } return count; } @@ -240,13 +241,13 @@ state_get_edges(struct REGEX_INTERNAL_State *s, struct REGEX_BLOCK_Edge *edges) * @return 0 if the sets are equal, otherwise non-zero */ static int -state_set_compare(struct REGEX_INTERNAL_StateSet *sset1, - struct REGEX_INTERNAL_StateSet *sset2) +state_set_compare (struct REGEX_INTERNAL_StateSet *sset1, + struct REGEX_INTERNAL_StateSet *sset2) { int result; unsigned int i; - if (NULL == sset1 || NULL == sset2) + if ((NULL == sset1)||(NULL == sset2)) return 1; result = sset1->off - sset2->off; @@ -255,7 +256,7 @@ state_set_compare(struct REGEX_INTERNAL_StateSet *sset1, if (result > 0) return 1; for (i = 0; i < sset1->off; i++) - if (0 != (result = state_compare(&sset1->states[i], &sset2->states[i]))) + if (0 != (result = state_compare (&sset1->states[i], &sset2->states[i]))) break; return result; } @@ -267,9 +268,9 @@ state_set_compare(struct REGEX_INTERNAL_StateSet *sset1, * @param set set to be cleared */ static void -state_set_clear(struct REGEX_INTERNAL_StateSet *set) +state_set_clear (struct REGEX_INTERNAL_StateSet *set) { - GNUNET_array_grow(set->states, set->size, 0); + GNUNET_array_grow (set->states, set->size, 0); set->off = 0; } @@ -281,7 +282,7 @@ state_set_clear(struct REGEX_INTERNAL_StateSet *set) * @param a automaton to be cleared */ static void -automaton_fragment_clear(struct REGEX_INTERNAL_Automaton *a) +automaton_fragment_clear (struct REGEX_INTERNAL_Automaton *a) { if (NULL == a) return; @@ -291,7 +292,7 @@ automaton_fragment_clear(struct REGEX_INTERNAL_Automaton *a) a->states_head = NULL; a->states_tail = NULL; a->state_count = 0; - GNUNET_free(a); + GNUNET_free (a); } @@ -301,7 +302,7 @@ automaton_fragment_clear(struct REGEX_INTERNAL_Automaton *a) * @param s state that should be destroyed */ static void -automaton_destroy_state(struct REGEX_INTERNAL_State *s) +automaton_destroy_state (struct REGEX_INTERNAL_State *s) { struct REGEX_INTERNAL_Transition *t; struct REGEX_INTERNAL_Transition *next_t; @@ -309,16 +310,16 @@ automaton_destroy_state(struct REGEX_INTERNAL_State *s) if (NULL == s) return; - GNUNET_free_non_null(s->name); - GNUNET_free_non_null(s->proof); - state_set_clear(&s->nfa_set); + GNUNET_free_non_null (s->name); + GNUNET_free_non_null (s->proof); + state_set_clear (&s->nfa_set); for (t = s->transitions_head; NULL != t; t = next_t) - { - next_t = t->next; - state_remove_transition(s, t); - } + { + next_t = t->next; + state_remove_transition (s, t); + } - GNUNET_free(s); + GNUNET_free (s); } @@ -331,33 +332,33 @@ automaton_destroy_state(struct REGEX_INTERNAL_State *s) * @param s state to remove */ static void -automaton_remove_state(struct REGEX_INTERNAL_Automaton *a, - struct REGEX_INTERNAL_State *s) +automaton_remove_state (struct REGEX_INTERNAL_Automaton *a, + struct REGEX_INTERNAL_State *s) { struct REGEX_INTERNAL_State *s_check; struct REGEX_INTERNAL_Transition *t_check; struct REGEX_INTERNAL_Transition *t_check_next; - if (NULL == a || NULL == s) + if ((NULL == a)||(NULL == s)) return; /* remove all transitions leading to this state */ for (s_check = a->states_head; NULL != s_check; s_check = s_check->next) + { + for (t_check = s_check->transitions_head; NULL != t_check; + t_check = t_check_next) { - for (t_check = s_check->transitions_head; NULL != t_check; - t_check = t_check_next) - { - t_check_next = t_check->next; - if (t_check->to_state == s) - state_remove_transition(s_check, t_check); - } + t_check_next = t_check->next; + if (t_check->to_state == s) + state_remove_transition (s_check, t_check); } + } /* remove state */ - GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s); + GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s); a->state_count--; - automaton_destroy_state(s); + automaton_destroy_state (s); } @@ -371,10 +372,10 @@ automaton_remove_state(struct REGEX_INTERNAL_Automaton *a, * @param s2 second state, will be destroyed */ static void -automaton_merge_states(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_Automaton *a, - struct REGEX_INTERNAL_State *s1, - struct REGEX_INTERNAL_State *s2) +automaton_merge_states (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_Automaton *a, + struct REGEX_INTERNAL_State *s1, + struct REGEX_INTERNAL_State *s2) { struct REGEX_INTERNAL_State *s_check; struct REGEX_INTERNAL_Transition *t_check; @@ -388,47 +389,47 @@ automaton_merge_states(struct REGEX_INTERNAL_Context *ctx, /* 1. Make all transitions pointing to s2 point to s1, unless this transition * does not already exists, if it already exists remove transition. */ for (s_check = a->states_head; NULL != s_check; s_check = s_check->next) + { + for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next) { - for (t_check = s_check->transitions_head; NULL != t_check; t_check = t_next) - { - t_next = t_check->next; + t_next = t_check->next; - if (s2 == t_check->to_state) - { - is_dup = GNUNET_NO; - for (t = t_check->from_state->transitions_head; NULL != t; t = t->next) - { - if (t->to_state == s1 && 0 == strcmp(t_check->label, t->label)) - is_dup = GNUNET_YES; - } - if (GNUNET_NO == is_dup) - t_check->to_state = s1; - else - state_remove_transition(t_check->from_state, t_check); - } + if (s2 == t_check->to_state) + { + is_dup = GNUNET_NO; + for (t = t_check->from_state->transitions_head; NULL != t; t = t->next) + { + if ((t->to_state == s1) &&(0 == strcmp (t_check->label, t->label)) ) + is_dup = GNUNET_YES; } + if (GNUNET_NO == is_dup) + t_check->to_state = s1; + else + state_remove_transition (t_check->from_state, t_check); + } } + } /* 2. Add all transitions from s2 to sX to s1 */ for (t_check = s2->transitions_head; NULL != t_check; t_check = t_check->next) - { - if (t_check->to_state != s1) - state_add_transition(ctx, s1, t_check->label, t_check->to_state); - } + { + if (t_check->to_state != s1) + state_add_transition (ctx, s1, t_check->label, t_check->to_state); + } /* 3. Rename s1 to {s1,s2} */ #if REGEX_DEBUG_DFA char *new_name; new_name = s1->name; - GNUNET_asprintf(&s1->name, "{%s,%s}", new_name, s2->name); - GNUNET_free(new_name); + GNUNET_asprintf (&s1->name, "{%s,%s}", new_name, s2->name); + GNUNET_free (new_name); #endif /* remove state */ - GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s2); + GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s2); a->state_count--; - automaton_destroy_state(s2); + automaton_destroy_state (s2); } @@ -440,10 +441,10 @@ automaton_merge_states(struct REGEX_INTERNAL_Context *ctx, * @param s state that should be added */ static void -automaton_add_state(struct REGEX_INTERNAL_Automaton *a, - struct REGEX_INTERNAL_State *s) +automaton_add_state (struct REGEX_INTERNAL_Automaton *a, + struct REGEX_INTERNAL_State *s) { - GNUNET_CONTAINER_DLL_insert(a->states_head, a->states_tail, s); + GNUNET_CONTAINER_DLL_insert (a->states_head, a->states_tail, s); a->state_count++; } @@ -463,13 +464,13 @@ automaton_add_state(struct REGEX_INTERNAL_Automaton *a, * @param action_cls closure for action. */ static void -automaton_state_traverse(struct REGEX_INTERNAL_State *s, - int *marks, - unsigned int *count, - REGEX_INTERNAL_traverse_check check, - void *check_cls, - REGEX_INTERNAL_traverse_action action, - void *action_cls) +automaton_state_traverse (struct REGEX_INTERNAL_State *s, + int *marks, + unsigned int *count, + REGEX_INTERNAL_traverse_check check, + void *check_cls, + REGEX_INTERNAL_traverse_action action, + void *action_cls) { struct REGEX_INTERNAL_Transition *t; @@ -479,24 +480,24 @@ automaton_state_traverse(struct REGEX_INTERNAL_State *s, marks[s->traversal_id] = GNUNET_YES; if (NULL != action) - action(action_cls, *count, s); + action (action_cls, *count, s); (*count)++; for (t = s->transitions_head; NULL != t; t = t->next) + { + if ((NULL == check) || + ((NULL != check) &&(GNUNET_YES == check (check_cls, s, t)) )) { - if (NULL == check || - (NULL != check && GNUNET_YES == check(check_cls, s, t))) - { - automaton_state_traverse(t->to_state, - marks, - count, - check, - check_cls, - action, - action_cls); - } + automaton_state_traverse (t->to_state, + marks, + count, + check, + check_cls, + action, + action_cls); } + } } @@ -514,27 +515,27 @@ automaton_state_traverse(struct REGEX_INTERNAL_State *s, * @param action_cls closure for @a action */ void -REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a, - struct REGEX_INTERNAL_State *start, - REGEX_INTERNAL_traverse_check check, - void *check_cls, - REGEX_INTERNAL_traverse_action action, - void *action_cls) +REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a, + struct REGEX_INTERNAL_State *start, + REGEX_INTERNAL_traverse_check check, + void *check_cls, + REGEX_INTERNAL_traverse_action action, + void *action_cls) { unsigned int count; struct REGEX_INTERNAL_State *s; - if (NULL == a || 0 == a->state_count) + if ((NULL == a)||(0 == a->state_count)) return; int marks[a->state_count]; for (count = 0, s = a->states_head; NULL != s && count < a->state_count; s = s->next, count++) - { - s->traversal_id = count; - marks[s->traversal_id] = GNUNET_NO; - } + { + s->traversal_id = count; + marks[s->traversal_id] = GNUNET_NO; + } count = 0; @@ -543,20 +544,21 @@ REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a, else s = start; - automaton_state_traverse(s, - marks, - &count, - check, - check_cls, - action, - action_cls); + automaton_state_traverse (s, + marks, + &count, + check, + check_cls, + action, + action_cls); } /** * String container for faster string operations. */ -struct StringBuffer { +struct StringBuffer +{ /** * Buffer holding the string (may start in the middle!); * NOT 0-terminated! @@ -605,7 +607,7 @@ struct StringBuffer { * @return 0 if the strings are the same or both NULL, 1 or -1 if not. */ static int -sb_nullstrcmp(const struct StringBuffer *s1, const struct StringBuffer *s2) +sb_nullstrcmp (const struct StringBuffer *s1, const struct StringBuffer *s2) { if ((GNUNET_YES == s1->null_flag) && (GNUNET_YES == s2->null_flag)) return 0; @@ -615,7 +617,7 @@ sb_nullstrcmp(const struct StringBuffer *s1, const struct StringBuffer *s2) return -1; if (0 == s1->slen) return 0; - return memcmp(s1->sbuf, s2->sbuf, s1->slen); + return memcmp (s1->sbuf, s2->sbuf, s1->slen); } @@ -628,13 +630,13 @@ sb_nullstrcmp(const struct StringBuffer *s1, const struct StringBuffer *s2) * @return 0 if the strings are the same, 1 or -1 if not. */ static int -sb_strcmp(const struct StringBuffer *s1, const struct StringBuffer *s2) +sb_strcmp (const struct StringBuffer *s1, const struct StringBuffer *s2) { if (s1->slen != s2->slen) return -1; if (0 == s1->slen) return 0; - return memcmp(s1->sbuf, s2->sbuf, s1->slen); + return memcmp (s1->sbuf, s2->sbuf, s1->slen); } @@ -646,17 +648,17 @@ sb_strcmp(const struct StringBuffer *s1, const struct StringBuffer *s2) * @param nlen target length for the buffer, must be at least ret->slen */ static void -sb_realloc(struct StringBuffer *ret, size_t nlen) +sb_realloc (struct StringBuffer *ret, size_t nlen) { char *old; - GNUNET_assert(nlen >= ret->slen); + GNUNET_assert (nlen >= ret->slen); old = ret->abuf; - ret->abuf = GNUNET_malloc(nlen); + ret->abuf = GNUNET_malloc (nlen); ret->blen = nlen; - GNUNET_memcpy(ret->abuf, ret->sbuf, ret->slen); + GNUNET_memcpy (ret->abuf, ret->sbuf, ret->slen); ret->sbuf = ret->abuf; - GNUNET_free_non_null(old); + GNUNET_free_non_null (old); } @@ -667,14 +669,14 @@ sb_realloc(struct StringBuffer *ret, size_t nlen) * @param sarg string to append */ static void -sb_append(struct StringBuffer *ret, const struct StringBuffer *sarg) +sb_append (struct StringBuffer *ret, const struct StringBuffer *sarg) { if (GNUNET_YES == ret->null_flag) ret->slen = 0; ret->null_flag = GNUNET_NO; if (ret->blen < sarg->slen + ret->slen) - sb_realloc(ret, ret->blen + sarg->slen + 128); - GNUNET_memcpy(&ret->sbuf[ret->slen], sarg->sbuf, sarg->slen); + sb_realloc (ret, ret->blen + sarg->slen + 128); + GNUNET_memcpy (&ret->sbuf[ret->slen], sarg->sbuf, sarg->slen); ret->slen += sarg->slen; } @@ -686,16 +688,16 @@ sb_append(struct StringBuffer *ret, const struct StringBuffer *sarg) * @param cstr string to append */ static void -sb_append_cstr(struct StringBuffer *ret, const char *cstr) +sb_append_cstr (struct StringBuffer *ret, const char *cstr) { - size_t cstr_len = strlen(cstr); + size_t cstr_len = strlen (cstr); if (GNUNET_YES == ret->null_flag) ret->slen = 0; ret->null_flag = GNUNET_NO; if (ret->blen < cstr_len + ret->slen) - sb_realloc(ret, ret->blen + cstr_len + 128); - GNUNET_memcpy(&ret->sbuf[ret->slen], cstr, cstr_len); + sb_realloc (ret, ret->blen + cstr_len + 128); + GNUNET_memcpy (&ret->sbuf[ret->slen], cstr, cstr_len); ret->slen += cstr_len; } @@ -711,20 +713,20 @@ sb_append_cstr(struct StringBuffer *ret, const char *cstr) * @param extra_chars how long will the result be, in addition to 'sarg' length */ static void -sb_wrap(struct StringBuffer *ret, const char *format, size_t extra_chars) +sb_wrap (struct StringBuffer *ret, const char *format, size_t extra_chars) { char *temp; if (GNUNET_YES == ret->null_flag) ret->slen = 0; ret->null_flag = GNUNET_NO; - temp = GNUNET_malloc(ret->slen + extra_chars + 1); - GNUNET_snprintf(temp, - ret->slen + extra_chars + 1, - format, - (int)ret->slen, - ret->sbuf); - GNUNET_free_non_null(ret->abuf); + temp = GNUNET_malloc (ret->slen + extra_chars + 1); + GNUNET_snprintf (temp, + ret->slen + extra_chars + 1, + format, + (int) ret->slen, + ret->sbuf); + GNUNET_free_non_null (ret->abuf); ret->abuf = temp; ret->sbuf = temp; ret->blen = ret->slen + extra_chars + 1; @@ -742,17 +744,17 @@ sb_wrap(struct StringBuffer *ret, const char *format, size_t extra_chars) * @param sarg string to print into the format */ static void -sb_printf1(struct StringBuffer *ret, - const char *format, - size_t extra_chars, - const struct StringBuffer *sarg) +sb_printf1 (struct StringBuffer *ret, + const char *format, + size_t extra_chars, + const struct StringBuffer *sarg) { if (ret->blen < sarg->slen + extra_chars + 1) - sb_realloc(ret, sarg->slen + extra_chars + 1); + sb_realloc (ret, sarg->slen + extra_chars + 1); ret->null_flag = GNUNET_NO; ret->sbuf = ret->abuf; ret->slen = sarg->slen + extra_chars; - GNUNET_snprintf(ret->sbuf, ret->blen, format, (int)sarg->slen, sarg->sbuf); + GNUNET_snprintf (ret->sbuf, ret->blen, format, (int) sarg->slen, sarg->sbuf); } @@ -766,24 +768,24 @@ sb_printf1(struct StringBuffer *ret, * @param sarg2 second string to print into the format */ static void -sb_printf2(struct StringBuffer *ret, - const char *format, - size_t extra_chars, - const struct StringBuffer *sarg1, - const struct StringBuffer *sarg2) +sb_printf2 (struct StringBuffer *ret, + const char *format, + size_t extra_chars, + const struct StringBuffer *sarg1, + const struct StringBuffer *sarg2) { if (ret->blen < sarg1->slen + sarg2->slen + extra_chars + 1) - sb_realloc(ret, sarg1->slen + sarg2->slen + extra_chars + 1); + sb_realloc (ret, sarg1->slen + sarg2->slen + extra_chars + 1); ret->null_flag = GNUNET_NO; ret->slen = sarg1->slen + sarg2->slen + extra_chars; ret->sbuf = ret->abuf; - GNUNET_snprintf(ret->sbuf, - ret->blen, - format, - (int)sarg1->slen, - sarg1->sbuf, - (int)sarg2->slen, - sarg2->sbuf); + GNUNET_snprintf (ret->sbuf, + ret->blen, + format, + (int) sarg1->slen, + sarg1->sbuf, + (int) sarg2->slen, + sarg2->sbuf); } @@ -799,27 +801,27 @@ sb_printf2(struct StringBuffer *ret, * @param sarg3 third string to print into the format */ static void -sb_printf3(struct StringBuffer *ret, - const char *format, - size_t extra_chars, - const struct StringBuffer *sarg1, - const struct StringBuffer *sarg2, - const struct StringBuffer *sarg3) +sb_printf3 (struct StringBuffer *ret, + const char *format, + size_t extra_chars, + const struct StringBuffer *sarg1, + const struct StringBuffer *sarg2, + const struct StringBuffer *sarg3) { if (ret->blen < sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1) - sb_realloc(ret, sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1); + sb_realloc (ret, sarg1->slen + sarg2->slen + sarg3->slen + extra_chars + 1); ret->null_flag = GNUNET_NO; ret->slen = sarg1->slen + sarg2->slen + sarg3->slen + extra_chars; ret->sbuf = ret->abuf; - GNUNET_snprintf(ret->sbuf, - ret->blen, - format, - (int)sarg1->slen, - sarg1->sbuf, - (int)sarg2->slen, - sarg2->sbuf, - (int)sarg3->slen, - sarg3->sbuf); + GNUNET_snprintf (ret->sbuf, + ret->blen, + format, + (int) sarg1->slen, + sarg1->sbuf, + (int) sarg2->slen, + sarg2->sbuf, + (int) sarg3->slen, + sarg3->sbuf); } @@ -830,9 +832,9 @@ sb_printf3(struct StringBuffer *ret, * should not be individually allocated) */ static void -sb_free(struct StringBuffer *sb) +sb_free (struct StringBuffer *sb) { - GNUNET_array_grow(sb->abuf, sb->blen, 0); + GNUNET_array_grow (sb->abuf, sb->blen, 0); sb->slen = 0; sb->sbuf = NULL; sb->null_flag = GNUNET_YES; @@ -846,19 +848,19 @@ sb_free(struct StringBuffer *sb) * @param out output string */ static void -sb_strdup(struct StringBuffer *out, const struct StringBuffer *in) +sb_strdup (struct StringBuffer *out, const struct StringBuffer *in) { out->null_flag = in->null_flag; if (GNUNET_YES == out->null_flag) return; if (out->blen < in->slen) - { - GNUNET_array_grow(out->abuf, out->blen, in->slen); - } + { + GNUNET_array_grow (out->abuf, out->blen, in->slen); + } out->sbuf = out->abuf; out->slen = in->slen; - GNUNET_memcpy(out->sbuf, in->sbuf, out->slen); + GNUNET_memcpy (out->sbuf, in->sbuf, out->slen); } @@ -869,21 +871,21 @@ sb_strdup(struct StringBuffer *out, const struct StringBuffer *in) * @param out output string */ static void -sb_strdup_cstr(struct StringBuffer *out, const char *cstr) +sb_strdup_cstr (struct StringBuffer *out, const char *cstr) { if (NULL == cstr) - { - out->null_flag = GNUNET_YES; - return; - } + { + out->null_flag = GNUNET_YES; + return; + } out->null_flag = GNUNET_NO; - out->slen = strlen(cstr); + out->slen = strlen (cstr); if (out->blen < out->slen) - { - GNUNET_array_grow(out->abuf, out->blen, out->slen); - } + { + GNUNET_array_grow (out->abuf, out->blen, out->slen); + } out->sbuf = out->abuf; - GNUNET_memcpy(out->sbuf, cstr, out->slen); + GNUNET_memcpy (out->sbuf, cstr, out->slen); } @@ -896,7 +898,7 @@ sb_strdup_cstr(struct StringBuffer *out, const char *cstr) * @return #GNUNET_YES if parentheses are needed, #GNUNET_NO otherwise */ static int -needs_parentheses(const struct StringBuffer *str) +needs_parentheses (const struct StringBuffer *str) { size_t slen; const char *op; @@ -914,23 +916,23 @@ needs_parentheses(const struct StringBuffer *str) cnt = 1; pos++; while (cnt > 0) + { + cl = memchr (pos, ')', end - pos); + if (NULL == cl) { - cl = memchr(pos, ')', end - pos); - if (NULL == cl) - { - GNUNET_break(0); - return GNUNET_YES; - } - /* while '(' before ')', count opening parens */ - while ((NULL != (op = memchr(pos, '(', end - pos))) && (op < cl)) - { - cnt++; - pos = op + 1; - } - /* got ')' first */ - cnt--; - pos = cl + 1; + GNUNET_break (0); + return GNUNET_YES; + } + /* while '(' before ')', count opening parens */ + while ((NULL != (op = memchr (pos, '(', end - pos))) && (op < cl)) + { + cnt++; + pos = op + 1; } + /* got ')' first */ + cnt--; + pos = cl + 1; + } return (*pos == '\0') ? GNUNET_NO : GNUNET_YES; } @@ -945,7 +947,7 @@ needs_parentheses(const struct StringBuffer *str) * epsilon could be found, NULL if 'str' was NULL */ static void -remove_parentheses(struct StringBuffer *str) +remove_parentheses (struct StringBuffer *str) { size_t slen; const char *pos; @@ -964,30 +966,30 @@ remove_parentheses(struct StringBuffer *str) cnt = 0; pos = &sbuf[1]; end = &sbuf[slen - 1]; - op = memchr(pos, '(', end - pos); - cp = memchr(pos, ')', end - pos); + op = memchr (pos, '(', end - pos); + cp = memchr (pos, ')', end - pos); while (NULL != cp) + { + while ((NULL != op) && (op < cp)) { - while ((NULL != op) && (op < cp)) - { - cnt++; - pos = op + 1; - op = memchr(pos, '(', end - pos); - } - while ((NULL != cp) && ((NULL == op) || (cp < op))) - { - if (0 == cnt) - return; /* can't strip parens */ - cnt--; - pos = cp + 1; - cp = memchr(pos, ')', end - pos); - } + cnt++; + pos = op + 1; + op = memchr (pos, '(', end - pos); } - if (0 != cnt) + while ((NULL != cp) && ((NULL == op) || (cp < op))) { - GNUNET_break(0); - return; + if (0 == cnt) + return; /* can't strip parens */ + cnt--; + pos = cp + 1; + cp = memchr (pos, ')', end - pos); } + } + if (0 != cnt) + { + GNUNET_break (0); + return; + } str->sbuf++; str->slen -= 2; } @@ -1002,7 +1004,7 @@ remove_parentheses(struct StringBuffer *str) * @return 0 if str has no epsilon, 1 if str starts with '(|' and ends with ')' */ static int -has_epsilon(const struct StringBuffer *str) +has_epsilon (const struct StringBuffer *str) { return (GNUNET_YES != str->null_flag) && (0 < str->slen) && ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) && @@ -1020,27 +1022,27 @@ has_epsilon(const struct StringBuffer *str) * epsilon could be found, NULL if 'str' was NULL */ static void -remove_epsilon(const struct StringBuffer *str, struct StringBuffer *ret) +remove_epsilon (const struct StringBuffer *str, struct StringBuffer *ret) { if (GNUNET_YES == str->null_flag) - { - ret->null_flag = GNUNET_YES; - return; - } + { + ret->null_flag = GNUNET_YES; + return; + } if ((str->slen > 1) && ('(' == str->sbuf[0]) && ('|' == str->sbuf[1]) && (')' == str->sbuf[str->slen - 1])) + { + /* remove epsilon */ + if (ret->blen < str->slen - 3) { - /* remove epsilon */ - if (ret->blen < str->slen - 3) - { - GNUNET_array_grow(ret->abuf, ret->blen, str->slen - 3); - } - ret->sbuf = ret->abuf; - ret->slen = str->slen - 3; - GNUNET_memcpy(ret->sbuf, &str->sbuf[2], ret->slen); - return; + GNUNET_array_grow (ret->abuf, ret->blen, str->slen - 3); } - sb_strdup(ret, str); + ret->sbuf = ret->abuf; + ret->slen = str->slen - 3; + GNUNET_memcpy (ret->sbuf, &str->sbuf[2], ret->slen); + return; + } + sb_strdup (ret, str); } @@ -1054,18 +1056,18 @@ remove_epsilon(const struct StringBuffer *str, struct StringBuffer *ret) * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise */ static int -sb_strncmp(const struct StringBuffer *str1, - const struct StringBuffer *str2, - size_t n) +sb_strncmp (const struct StringBuffer *str1, + const struct StringBuffer *str2, + size_t n) { size_t max; if ((str1->slen != str2->slen) && ((str1->slen < n) || (str2->slen < n))) return -1; - max = GNUNET_MAX(str1->slen, str2->slen); + max = GNUNET_MAX (str1->slen, str2->slen); if (max > n) max = n; - return memcmp(str1->sbuf, str2->sbuf, max); + return memcmp (str1->sbuf, str2->sbuf, max); } @@ -1079,11 +1081,11 @@ sb_strncmp(const struct StringBuffer *str1, * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise */ static int -sb_strncmp_cstr(const struct StringBuffer *str1, const char *str2, size_t n) +sb_strncmp_cstr (const struct StringBuffer *str1, const char *str2, size_t n) { if (str1->slen < n) return -1; - return memcmp(str1->sbuf, str2, n); + return memcmp (str1->sbuf, str2, n); } @@ -1095,10 +1097,10 @@ sb_strncmp_cstr(const struct StringBuffer *str1, const char *str2, size_t n) * @param n desired target length */ static void -sb_init(struct StringBuffer *sb, size_t n) +sb_init (struct StringBuffer *sb, size_t n) { sb->null_flag = GNUNET_NO; - sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc(n); + sb->abuf = sb->sbuf = (0 == n) ? NULL : GNUNET_malloc (n); sb->blen = n; sb->slen = 0; } @@ -1114,14 +1116,14 @@ sb_init(struct StringBuffer *sb, size_t n) * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise */ static int -sb_strkcmp(const struct StringBuffer *str1, - const struct StringBuffer *str2, - size_t k) +sb_strkcmp (const struct StringBuffer *str1, + const struct StringBuffer *str2, + size_t k) { if ((GNUNET_YES == str1->null_flag) || (GNUNET_YES == str2->null_flag) || (k > str1->slen) || (str1->slen - k != str2->slen)) return -1; - return memcmp(&str1->sbuf[k], str2->sbuf, str2->slen); + return memcmp (&str1->sbuf[k], str2->sbuf, str2->slen); } @@ -1134,9 +1136,9 @@ sb_strkcmp(const struct StringBuffer *str1, * @param s current state. */ static void -number_states(void *cls, - const unsigned int count, - struct REGEX_INTERNAL_State *s) +number_states (void *cls, + const unsigned int count, + struct REGEX_INTERNAL_State *s) { struct REGEX_INTERNAL_State **states = cls; @@ -1147,7 +1149,7 @@ number_states(void *cls, #define PRIS(a) \ - ((GNUNET_YES == a.null_flag) ? 6 : (int)a.slen), \ + ((GNUNET_YES == a.null_flag) ? 6 : (int) a.slen), \ ((GNUNET_YES == a.null_flag) ? "(null)" : a.sbuf) @@ -1166,13 +1168,13 @@ number_states(void *cls, * @param R_cur_r optimization -- kept between iterations to avoid realloc */ static void -automaton_create_proofs_simplify(const struct StringBuffer *R_last_ij, - const struct StringBuffer *R_last_ik, - const struct StringBuffer *R_last_kk, - const struct StringBuffer *R_last_kj, - struct StringBuffer *R_cur_ij, - struct StringBuffer *R_cur_l, - struct StringBuffer *R_cur_r) +automaton_create_proofs_simplify (const struct StringBuffer *R_last_ij, + const struct StringBuffer *R_last_ik, + const struct StringBuffer *R_last_kk, + const struct StringBuffer *R_last_kj, + struct StringBuffer *R_cur_ij, + struct StringBuffer *R_cur_l, + struct StringBuffer *R_cur_r) { struct StringBuffer R_temp_ij; struct StringBuffer R_temp_ik; @@ -1200,27 +1202,27 @@ automaton_create_proofs_simplify(const struct StringBuffer *R_last_ij, if ((GNUNET_YES == R_last_ij->null_flag) && ((GNUNET_YES == R_last_ik->null_flag) || (GNUNET_YES == R_last_kj->null_flag))) - { - /* R^{(k)}_{ij} = N | N */ - R_cur_ij->null_flag = GNUNET_YES; - R_cur_ij->synced = GNUNET_NO; - return; - } + { + /* R^{(k)}_{ij} = N | N */ + R_cur_ij->null_flag = GNUNET_YES; + R_cur_ij->synced = GNUNET_NO; + return; + } if ((GNUNET_YES == R_last_ik->null_flag) || (GNUNET_YES == R_last_kj->null_flag)) + { + /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */ + if (GNUNET_YES == R_last_ij->synced) { - /* R^{(k)}_{ij} = R^{(k-1)}_{ij} | N */ - if (GNUNET_YES == R_last_ij->synced) - { - R_cur_ij->synced = GNUNET_YES; - R_cur_ij->null_flag = GNUNET_NO; - return; - } R_cur_ij->synced = GNUNET_YES; - sb_strdup(R_cur_ij, R_last_ij); + R_cur_ij->null_flag = GNUNET_NO; return; } + R_cur_ij->synced = GNUNET_YES; + sb_strdup (R_cur_ij, R_last_ij); + return; + } R_cur_ij->synced = GNUNET_NO; /* $R^{(k)}_{ij} = N | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} OR @@ -1232,334 +1234,334 @@ automaton_create_proofs_simplify(const struct StringBuffer *R_last_ij, R_cur_l->slen = 0; /* cache results from strcmp, we might need these many times */ - ij_kj_cmp = sb_nullstrcmp(R_last_ij, R_last_kj); - ij_ik_cmp = sb_nullstrcmp(R_last_ij, R_last_ik); - ik_kk_cmp = sb_nullstrcmp(R_last_ik, R_last_kk); - kk_kj_cmp = sb_nullstrcmp(R_last_kk, R_last_kj); + ij_kj_cmp = sb_nullstrcmp (R_last_ij, R_last_kj); + ij_ik_cmp = sb_nullstrcmp (R_last_ij, R_last_ik); + ik_kk_cmp = sb_nullstrcmp (R_last_ik, R_last_kk); + kk_kj_cmp = sb_nullstrcmp (R_last_kk, R_last_kj); /* Assign R_temp_(ik|kk|kj) to R_last[][] and remove epsilon as well * as parentheses, so we can better compare the contents */ - memset(&R_temp_ij, 0, sizeof(struct StringBuffer)); - memset(&R_temp_ik, 0, sizeof(struct StringBuffer)); - memset(&R_temp_kk, 0, sizeof(struct StringBuffer)); - memset(&R_temp_kj, 0, sizeof(struct StringBuffer)); - remove_epsilon(R_last_ik, &R_temp_ik); - remove_epsilon(R_last_kk, &R_temp_kk); - remove_epsilon(R_last_kj, &R_temp_kj); - remove_parentheses(&R_temp_ik); - remove_parentheses(&R_temp_kk); - remove_parentheses(&R_temp_kj); - clean_ik_kk_cmp = sb_nullstrcmp(R_last_ik, &R_temp_kk); - clean_kk_kj_cmp = sb_nullstrcmp(&R_temp_kk, R_last_kj); + memset (&R_temp_ij, 0, sizeof(struct StringBuffer)); + memset (&R_temp_ik, 0, sizeof(struct StringBuffer)); + memset (&R_temp_kk, 0, sizeof(struct StringBuffer)); + memset (&R_temp_kj, 0, sizeof(struct StringBuffer)); + remove_epsilon (R_last_ik, &R_temp_ik); + remove_epsilon (R_last_kk, &R_temp_kk); + remove_epsilon (R_last_kj, &R_temp_kj); + remove_parentheses (&R_temp_ik); + remove_parentheses (&R_temp_kk); + remove_parentheses (&R_temp_kj); + clean_ik_kk_cmp = sb_nullstrcmp (R_last_ik, &R_temp_kk); + clean_kk_kj_cmp = sb_nullstrcmp (&R_temp_kk, R_last_kj); /* construct R_cur_l (and, if necessary R_cur_r) */ if (GNUNET_YES != R_last_ij->null_flag) + { + /* Assign R_temp_ij to R_last_ij and remove epsilon as well + * as parentheses, so we can better compare the contents */ + remove_epsilon (R_last_ij, &R_temp_ij); + remove_parentheses (&R_temp_ij); + + if ((0 == sb_strcmp (&R_temp_ij, &R_temp_ik)) && + (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) && + (0 == sb_strcmp (&R_temp_kk, &R_temp_kj))) { - /* Assign R_temp_ij to R_last_ij and remove epsilon as well - * as parentheses, so we can better compare the contents */ - remove_epsilon(R_last_ij, &R_temp_ij); - remove_parentheses(&R_temp_ij); - - if ((0 == sb_strcmp(&R_temp_ij, &R_temp_ik)) && - (0 == sb_strcmp(&R_temp_ik, &R_temp_kk)) && - (0 == sb_strcmp(&R_temp_kk, &R_temp_kj))) - { - if (0 == R_temp_ij.slen) - { - R_cur_r->null_flag = GNUNET_NO; - } - else if ((0 == sb_strncmp_cstr(R_last_ij, "(|", 2)) || - (0 == sb_strncmp_cstr(R_last_ik, "(|", 2) && - 0 == sb_strncmp_cstr(R_last_kj, "(|", 2))) - { - /* - * a|(e|a)a*(e|a) = a* - * a|(e|a)(e|a)*(e|a) = a* - * (e|a)|aa*a = a* - * (e|a)|aa*(e|a) = a* - * (e|a)|(e|a)a*a = a* - * (e|a)|(e|a)a*(e|a) = a* - * (e|a)|(e|a)(e|a)*(e|a) = a* - */ - if (GNUNET_YES == needs_parentheses(&R_temp_ij)) - sb_printf1(R_cur_r, "(%.*s)*", 3, &R_temp_ij); - else - sb_printf1(R_cur_r, "%.*s*", 1, &R_temp_ij); - } - else - { - /* - * a|aa*a = a+ - * a|(e|a)a*a = a+ - * a|aa*(e|a) = a+ - * a|(e|a)(e|a)*a = a+ - * a|a(e|a)*(e|a) = a+ - */ - if (GNUNET_YES == needs_parentheses(&R_temp_ij)) - sb_printf1(R_cur_r, "(%.*s)+", 3, &R_temp_ij); - else - sb_printf1(R_cur_r, "%.*s+", 1, &R_temp_ij); - } - } - else if ((0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && - (0 != clean_ik_kk_cmp)) - { - /* a|ab*b = ab* */ - if (0 == R_last_kk->slen) - sb_strdup(R_cur_r, R_last_ij); - else if (GNUNET_YES == needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); - else - sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk); - R_cur_l->null_flag = GNUNET_YES; - } - else if ((0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && - (0 != clean_kk_kj_cmp)) - { - /* a|bb*a = b*a */ - if (R_last_kk->slen < 1) - { - sb_strdup(R_cur_r, R_last_kj); - } - else if (GNUNET_YES == needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj); - else - sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj); + if (0 == R_temp_ij.slen) + { + R_cur_r->null_flag = GNUNET_NO; + } + else if ((0 == sb_strncmp_cstr (R_last_ij, "(|", 2)) || + ((0 == sb_strncmp_cstr (R_last_ik, "(|", 2)) && + (0 == sb_strncmp_cstr (R_last_kj, "(|", 2)) )) + { + /* + * a|(e|a)a*(e|a) = a* + * a|(e|a)(e|a)*(e|a) = a* + * (e|a)|aa*a = a* + * (e|a)|aa*(e|a) = a* + * (e|a)|(e|a)a*a = a* + * (e|a)|(e|a)a*(e|a) = a* + * (e|a)|(e|a)(e|a)*(e|a) = a* + */ + if (GNUNET_YES == needs_parentheses (&R_temp_ij)) + sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_ij); + else + sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_ij); + } + else + { + /* + * a|aa*a = a+ + * a|(e|a)a*a = a+ + * a|aa*(e|a) = a+ + * a|(e|a)(e|a)*a = a+ + * a|a(e|a)*(e|a) = a+ + */ + if (GNUNET_YES == needs_parentheses (&R_temp_ij)) + sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_ij); + else + sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_ij); + } + } + else if ((0 == ij_ik_cmp) && (0 == clean_kk_kj_cmp) && + (0 != clean_ik_kk_cmp)) + { + /* a|ab*b = ab* */ + if (0 == R_last_kk->slen) + sb_strdup (R_cur_r, R_last_ij); + else if (GNUNET_YES == needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); + else + sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, R_last_kk); + R_cur_l->null_flag = GNUNET_YES; + } + else if ((0 == ij_kj_cmp) && (0 == clean_ik_kk_cmp) && + (0 != clean_kk_kj_cmp)) + { + /* a|bb*a = b*a */ + if (R_last_kk->slen < 1) + { + sb_strdup (R_cur_r, R_last_kj); + } + else if (GNUNET_YES == needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj); + else + sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj); - R_cur_l->null_flag = GNUNET_YES; - } - else if ((0 == ij_ik_cmp) && (0 == kk_kj_cmp) && - (!has_epsilon(R_last_ij)) && has_epsilon(R_last_kk)) - { - /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */ - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); - else - sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk); - R_cur_l->null_flag = GNUNET_YES; - } - else if ((0 == ij_kj_cmp) && (0 == ik_kk_cmp) && - (!has_epsilon(R_last_ij)) && has_epsilon(R_last_kk)) - { - /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */ - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij); - else - sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij); - R_cur_l->null_flag = GNUNET_YES; - } + R_cur_l->null_flag = GNUNET_YES; + } + else if ((0 == ij_ik_cmp) && (0 == kk_kj_cmp) && + (! has_epsilon (R_last_ij)) && has_epsilon (R_last_kk)) + { + /* a|a(e|b)*(e|b) = a|ab* = a|a|ab|abb|abbb|... = ab* */ + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ij, &R_temp_kk); else - { - sb_strdup(R_cur_l, R_last_ij); - remove_parentheses(R_cur_l); - } + sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ij, &R_temp_kk); + R_cur_l->null_flag = GNUNET_YES; } - else + else if ((0 == ij_kj_cmp) && (0 == ik_kk_cmp) && + (! has_epsilon (R_last_ij)) && has_epsilon (R_last_kk)) { - /* we have no left side */ + /* a|(e|b)(e|b)*a = a|b*a = a|a|ba|bba|bbba|... = b*a */ + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_ij); + else + sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_ij); R_cur_l->null_flag = GNUNET_YES; } + else + { + sb_strdup (R_cur_l, R_last_ij); + remove_parentheses (R_cur_l); + } + } + else + { + /* we have no left side */ + R_cur_l->null_flag = GNUNET_YES; + } /* construct R_cur_r, if not already constructed */ if (GNUNET_YES == R_cur_r->null_flag) + { + length = R_temp_kk.slen - R_last_ik->slen; + + /* a(ba)*bx = (ab)+x */ + if ((length > 0) && (GNUNET_YES != R_last_kk->null_flag) && + (0 < R_last_kk->slen) && (GNUNET_YES != R_last_kj->null_flag) && + (0 < R_last_kj->slen) && (GNUNET_YES != R_last_ik->null_flag) && + (0 < R_last_ik->slen) && + (0 == sb_strkcmp (&R_temp_kk, R_last_ik, length)) && + (0 == sb_strncmp (&R_temp_kk, R_last_kj, length))) + { + struct StringBuffer temp_a; + struct StringBuffer temp_b; + + sb_init (&temp_a, length); + sb_init (&temp_b, R_last_kj->slen - length); + + length_l = length; + temp_a.sbuf = temp_a.abuf; + GNUNET_memcpy (temp_a.sbuf, R_last_kj->sbuf, length_l); + temp_a.slen = length_l; + + length_r = R_last_kj->slen - length; + temp_b.sbuf = temp_b.abuf; + GNUNET_memcpy (temp_b.sbuf, &R_last_kj->sbuf[length], length_r); + temp_b.slen = length_r; + + /* e|(ab)+ = (ab)* */ + if ((GNUNET_YES != R_cur_l->null_flag) && (0 == R_cur_l->slen) && + (0 == temp_b.slen)) + { + sb_printf2 (R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a); + sb_free (R_cur_l); + R_cur_l->null_flag = GNUNET_YES; + } + else + { + sb_printf3 (R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b); + } + sb_free (&temp_a); + sb_free (&temp_b); + } + else if ((0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) && + (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) ) { - length = R_temp_kk.slen - R_last_ik->slen; - - /* a(ba)*bx = (ab)+x */ - if ((length > 0) && (GNUNET_YES != R_last_kk->null_flag) && - (0 < R_last_kk->slen) && (GNUNET_YES != R_last_kj->null_flag) && - (0 < R_last_kj->slen) && (GNUNET_YES != R_last_ik->null_flag) && - (0 < R_last_ik->slen) && - (0 == sb_strkcmp(&R_temp_kk, R_last_ik, length)) && - (0 == sb_strncmp(&R_temp_kk, R_last_kj, length))) - { - struct StringBuffer temp_a; - struct StringBuffer temp_b; - - sb_init(&temp_a, length); - sb_init(&temp_b, R_last_kj->slen - length); - - length_l = length; - temp_a.sbuf = temp_a.abuf; - GNUNET_memcpy(temp_a.sbuf, R_last_kj->sbuf, length_l); - temp_a.slen = length_l; - - length_r = R_last_kj->slen - length; - temp_b.sbuf = temp_b.abuf; - GNUNET_memcpy(temp_b.sbuf, &R_last_kj->sbuf[length], length_r); - temp_b.slen = length_r; - - /* e|(ab)+ = (ab)* */ - if ((GNUNET_YES != R_cur_l->null_flag) && (0 == R_cur_l->slen) && - (0 == temp_b.slen)) - { - sb_printf2(R_cur_r, "(%.*s%.*s)*", 3, R_last_ik, &temp_a); - sb_free(R_cur_l); - R_cur_l->null_flag = GNUNET_YES; - } - else - { - sb_printf3(R_cur_r, "(%.*s%.*s)+%.*s", 3, R_last_ik, &temp_a, &temp_b); - } - sb_free(&temp_a); - sb_free(&temp_b); - } - else if (0 == sb_strcmp(&R_temp_ik, &R_temp_kk) && - 0 == sb_strcmp(&R_temp_kk, &R_temp_kj)) - { - /* - * (e|a)a*(e|a) = a* - * (e|a)(e|a)*(e|a) = a* - */ - if (has_epsilon(R_last_ik) && has_epsilon(R_last_kj)) - { - if (needs_parentheses(&R_temp_kk)) - sb_printf1(R_cur_r, "(%.*s)*", 3, &R_temp_kk); - else - sb_printf1(R_cur_r, "%.*s*", 1, &R_temp_kk); - } - /* aa*a = a+a */ - else if ((0 == clean_ik_kk_cmp) && (0 == clean_kk_kj_cmp) && - (!has_epsilon(R_last_ik))) - { - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk); - else - sb_printf2(R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk); - } - /* - * (e|a)a*a = a+ - * aa*(e|a) = a+ - * a(e|a)*(e|a) = a+ - * (e|a)a*a = a+ - */ - else - { - eps_check = (has_epsilon(R_last_ik) + has_epsilon(R_last_kk) + - has_epsilon(R_last_kj)); - - if (1 == eps_check) - { - if (needs_parentheses(&R_temp_kk)) - sb_printf1(R_cur_r, "(%.*s)+", 3, &R_temp_kk); - else - sb_printf1(R_cur_r, "%.*s+", 1, &R_temp_kk); - } - } - } /* - * aa*b = a+b - * (e|a)(e|a)*b = a*b + * (e|a)a*(e|a) = a* + * (e|a)(e|a)*(e|a) = a* */ - else if (0 == sb_strcmp(&R_temp_ik, &R_temp_kk)) - { - if (has_epsilon(R_last_ik)) - { - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj); - else - sb_printf2(R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj); - } - else - { - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj); - else - sb_printf2(R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj); - } - } + if (has_epsilon (R_last_ik) && has_epsilon (R_last_kj)) + { + if (needs_parentheses (&R_temp_kk)) + sb_printf1 (R_cur_r, "(%.*s)*", 3, &R_temp_kk); + else + sb_printf1 (R_cur_r, "%.*s*", 1, &R_temp_kk); + } + /* aa*a = a+a */ + else if ((0 == clean_ik_kk_cmp) && (0 == clean_kk_kj_cmp) && + (! has_epsilon (R_last_ik))) + { + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, &R_temp_kk); + else + sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, &R_temp_kk); + } /* - * ba*a = ba+ - * b(e|a)*(e|a) = ba* + * (e|a)a*a = a+ + * aa*(e|a) = a+ + * a(e|a)*(e|a) = a+ + * (e|a)a*a = a+ */ - else if (0 == sb_strcmp(&R_temp_kk, &R_temp_kj)) + else + { + eps_check = (has_epsilon (R_last_ik) + has_epsilon (R_last_kk) + + has_epsilon (R_last_kj)); + + if (1 == eps_check) { - if (has_epsilon(R_last_kj)) - { - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk); - else - sb_printf2(R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk); - } + if (needs_parentheses (&R_temp_kk)) + sb_printf1 (R_cur_r, "(%.*s)+", 3, &R_temp_kk); else - { - if (needs_parentheses(&R_temp_kk)) - sb_printf2(R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk); - else - sb_printf2(R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk); - } + sb_printf1 (R_cur_r, "%.*s+", 1, &R_temp_kk); } + } + } + /* + * aa*b = a+b + * (e|a)(e|a)*b = a*b + */ + else if (0 == sb_strcmp (&R_temp_ik, &R_temp_kk)) + { + if (has_epsilon (R_last_ik)) + { + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)*%.*s", 3, &R_temp_kk, R_last_kj); + else + sb_printf2 (R_cur_r, "%.*s*%.*s", 1, &R_temp_kk, R_last_kj); + } + else + { + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, &R_temp_kk, R_last_kj); + else + sb_printf2 (R_cur_r, "%.*s+%.*s", 1, &R_temp_kk, R_last_kj); + } + } + /* + * ba*a = ba+ + * b(e|a)*(e|a) = ba* + */ + else if (0 == sb_strcmp (&R_temp_kk, &R_temp_kj)) + { + if (has_epsilon (R_last_kj)) + { + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "%.*s(%.*s)*", 3, R_last_ik, &R_temp_kk); + else + sb_printf2 (R_cur_r, "%.*s%.*s*", 1, R_last_ik, &R_temp_kk); + } else + { + if (needs_parentheses (&R_temp_kk)) + sb_printf2 (R_cur_r, "(%.*s)+%.*s", 3, R_last_ik, &R_temp_kk); + else + sb_printf2 (R_cur_r, "%.*s+%.*s", 1, R_last_ik, &R_temp_kk); + } + } + else + { + if (0 < R_temp_kk.slen) + { + if (needs_parentheses (&R_temp_kk)) { - if (0 < R_temp_kk.slen) - { - if (needs_parentheses(&R_temp_kk)) - { - sb_printf3(R_cur_r, - "%.*s(%.*s)*%.*s", - 3, - R_last_ik, - &R_temp_kk, - R_last_kj); - } - else - { - sb_printf3(R_cur_r, - "%.*s%.*s*%.*s", - 1, - R_last_ik, - &R_temp_kk, - R_last_kj); - } - } - else - { - sb_printf2(R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj); - } + sb_printf3 (R_cur_r, + "%.*s(%.*s)*%.*s", + 3, + R_last_ik, + &R_temp_kk, + R_last_kj); + } + else + { + sb_printf3 (R_cur_r, + "%.*s%.*s*%.*s", + 1, + R_last_ik, + &R_temp_kk, + R_last_kj); } + } + else + { + sb_printf2 (R_cur_r, "%.*s%.*s", 0, R_last_ik, R_last_kj); + } } - sb_free(&R_temp_ij); - sb_free(&R_temp_ik); - sb_free(&R_temp_kk); - sb_free(&R_temp_kj); + } + sb_free (&R_temp_ij); + sb_free (&R_temp_ik); + sb_free (&R_temp_kk); + sb_free (&R_temp_kj); if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag)) - { - R_cur_ij->null_flag = GNUNET_YES; - return; - } + { + R_cur_ij->null_flag = GNUNET_YES; + return; + } if ((GNUNET_YES != R_cur_l->null_flag) && (GNUNET_YES == R_cur_r->null_flag)) - { - struct StringBuffer tmp; + { + struct StringBuffer tmp; - tmp = *R_cur_ij; - *R_cur_ij = *R_cur_l; - *R_cur_l = tmp; - return; - } + tmp = *R_cur_ij; + *R_cur_ij = *R_cur_l; + *R_cur_l = tmp; + return; + } if ((GNUNET_YES == R_cur_l->null_flag) && (GNUNET_YES != R_cur_r->null_flag)) - { - struct StringBuffer tmp; + { + struct StringBuffer tmp; - tmp = *R_cur_ij; - *R_cur_ij = *R_cur_r; - *R_cur_r = tmp; - return; - } + tmp = *R_cur_ij; + *R_cur_ij = *R_cur_r; + *R_cur_r = tmp; + return; + } - if (0 == sb_nullstrcmp(R_cur_l, R_cur_r)) - { - struct StringBuffer tmp; + if (0 == sb_nullstrcmp (R_cur_l, R_cur_r)) + { + struct StringBuffer tmp; - tmp = *R_cur_ij; - *R_cur_ij = *R_cur_l; - *R_cur_l = tmp; - return; - } - sb_printf2(R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r); + tmp = *R_cur_ij; + *R_cur_ij = *R_cur_l; + *R_cur_l = tmp; + return; + } + sb_printf2 (R_cur_ij, "(%.*s|%.*s)", 3, R_cur_l, R_cur_r); } @@ -1575,7 +1577,7 @@ automaton_create_proofs_simplify(const struct StringBuffer *R_last_ij, * @param a automaton for which to assign proofs and hashes, must not be NULL */ static int -automaton_create_proofs(struct REGEX_INTERNAL_Automaton *a) +automaton_create_proofs (struct REGEX_INTERNAL_Automaton *a) { unsigned int n = a->state_count; struct REGEX_INTERNAL_State *states[n]; @@ -1590,143 +1592,143 @@ automaton_create_proofs(struct REGEX_INTERNAL_Automaton *a) unsigned int j; unsigned int k; - R_last = GNUNET_malloc_large(sizeof(struct StringBuffer) * n * n); - R_cur = GNUNET_malloc_large(sizeof(struct StringBuffer) * n * n); + R_last = GNUNET_malloc_large (sizeof(struct StringBuffer) * n * n); + R_cur = GNUNET_malloc_large (sizeof(struct StringBuffer) * n * n); if ((NULL == R_last) || (NULL == R_cur)) - { - GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc"); - GNUNET_free_non_null(R_cur); - GNUNET_free_non_null(R_last); - return GNUNET_SYSERR; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); + GNUNET_free_non_null (R_cur); + GNUNET_free_non_null (R_last); + return GNUNET_SYSERR; + } /* create depth-first numbering of the states, initializes 'state' */ - REGEX_INTERNAL_automaton_traverse(a, - a->start, - NULL, - NULL, - &number_states, - states); + REGEX_INTERNAL_automaton_traverse (a, + a->start, + NULL, + NULL, + &number_states, + states); for (i = 0; i < n; i++) - GNUNET_assert(NULL != states[i]); + GNUNET_assert (NULL != states[i]); for (i = 0; i < n; i++) for (j = 0; j < n; j++) R_last[i * n + j].null_flag = GNUNET_YES; /* Compute regular expressions of length "1" between each pair of states */ for (i = 0; i < n; i++) + { + for (t = states[i]->transitions_head; NULL != t; t = t->next) { - for (t = states[i]->transitions_head; NULL != t; t = t->next) - { - j = t->to_state->dfs_id; - if (GNUNET_YES == R_last[i * n + j].null_flag) - { - sb_strdup_cstr(&R_last[i * n + j], t->label); - } - else - { - sb_append_cstr(&R_last[i * n + j], "|"); - sb_append_cstr(&R_last[i * n + j], t->label); - } - } - /* add self-loop: i is reachable from i via epsilon-transition */ - if (GNUNET_YES == R_last[i * n + i].null_flag) - { - R_last[i * n + i].slen = 0; - R_last[i * n + i].null_flag = GNUNET_NO; - } + j = t->to_state->dfs_id; + if (GNUNET_YES == R_last[i * n + j].null_flag) + { + sb_strdup_cstr (&R_last[i * n + j], t->label); + } else - { - sb_wrap(&R_last[i * n + i], "(|%.*s)", 3); - } + { + sb_append_cstr (&R_last[i * n + j], "|"); + sb_append_cstr (&R_last[i * n + j], t->label); + } + } + /* add self-loop: i is reachable from i via epsilon-transition */ + if (GNUNET_YES == R_last[i * n + i].null_flag) + { + R_last[i * n + i].slen = 0; + R_last[i * n + i].null_flag = GNUNET_NO; + } + else + { + sb_wrap (&R_last[i * n + i], "(|%.*s)", 3); } + } for (i = 0; i < n; i++) for (j = 0; j < n; j++) - if (needs_parentheses(&R_last[i * n + j])) - sb_wrap(&R_last[i * n + j], "(%.*s)", 2); + if (needs_parentheses (&R_last[i * n + j])) + sb_wrap (&R_last[i * n + j], "(%.*s)", 2); /* Compute regular expressions of length "k" between each pair of states per * induction */ - memset(&R_cur_l, 0, sizeof(struct StringBuffer)); - memset(&R_cur_r, 0, sizeof(struct StringBuffer)); + memset (&R_cur_l, 0, sizeof(struct StringBuffer)); + memset (&R_cur_r, 0, sizeof(struct StringBuffer)); for (k = 0; k < n; k++) + { + for (i = 0; i < n; i++) { - for (i = 0; i < n; i++) - { - for (j = 0; j < n; j++) - { - /* Basis for the recursion: - * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} - * R_last == R^{(k-1)}, R_cur == R^{(k)} - */ - - /* Create R_cur[i][j] and simplify the expression */ - automaton_create_proofs_simplify(&R_last[i * n + j], - &R_last[i * n + k], - &R_last[k * n + k], - &R_last[k * n + j], - &R_cur[i * n + j], - &R_cur_l, - &R_cur_r); - } - } - /* set R_last = R_cur */ - R_swap = R_last; - R_last = R_cur; - R_cur = R_swap; - /* clear 'R_cur' for next iteration */ - for (i = 0; i < n; i++) - for (j = 0; j < n; j++) - R_cur[i * n + j].null_flag = GNUNET_YES; + for (j = 0; j < n; j++) + { + /* Basis for the recursion: + * $R^{(k)}_{ij} = R^{(k-1)}_{ij} | R^{(k-1)}_{ik} ( R^{(k-1)}_{kk} )^* R^{(k-1)}_{kj} + * R_last == R^{(k-1)}, R_cur == R^{(k)} + */ + + /* Create R_cur[i][j] and simplify the expression */ + automaton_create_proofs_simplify (&R_last[i * n + j], + &R_last[i * n + k], + &R_last[k * n + k], + &R_last[k * n + j], + &R_cur[i * n + j], + &R_cur_l, + &R_cur_r); + } } - sb_free(&R_cur_l); - sb_free(&R_cur_r); + /* set R_last = R_cur */ + R_swap = R_last; + R_last = R_cur; + R_cur = R_swap; + /* clear 'R_cur' for next iteration */ + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + R_cur[i * n + j].null_flag = GNUNET_YES; + } + sb_free (&R_cur_l); + sb_free (&R_cur_r); /* assign proofs and hashes */ for (i = 0; i < n; i++) + { + if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) { - if (GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) - { - states[i]->proof = GNUNET_strndup(R_last[a->start->dfs_id * n + i].sbuf, - R_last[a->start->dfs_id * n + i].slen); - GNUNET_CRYPTO_hash(states[i]->proof, - strlen(states[i]->proof), - &states[i]->hash); - } + states[i]->proof = GNUNET_strndup (R_last[a->start->dfs_id * n + i].sbuf, + R_last[a->start->dfs_id * n + i].slen); + GNUNET_CRYPTO_hash (states[i]->proof, + strlen (states[i]->proof), + &states[i]->hash); } + } /* complete regex for whole DFA: union of all pairs (start state/accepting * state(s)). */ - sb_init(&complete_regex, 16 * n); + sb_init (&complete_regex, 16 * n); for (i = 0; i < n; i++) + { + if (states[i]->accepting) { - if (states[i]->accepting) - { - if ((0 == complete_regex.slen) && - (0 < R_last[a->start->dfs_id * n + i].slen)) - { - sb_append(&complete_regex, &R_last[a->start->dfs_id * n + i]); - } - else if ((GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) && - (0 < R_last[a->start->dfs_id * n + i].slen)) - { - sb_append_cstr(&complete_regex, "|"); - sb_append(&complete_regex, &R_last[a->start->dfs_id * n + i]); - } - } + if ((0 == complete_regex.slen) && + (0 < R_last[a->start->dfs_id * n + i].slen)) + { + sb_append (&complete_regex, &R_last[a->start->dfs_id * n + i]); + } + else if ((GNUNET_YES != R_last[a->start->dfs_id * n + i].null_flag) && + (0 < R_last[a->start->dfs_id * n + i].slen)) + { + sb_append_cstr (&complete_regex, "|"); + sb_append (&complete_regex, &R_last[a->start->dfs_id * n + i]); + } } + } a->canonical_regex = - GNUNET_strndup(complete_regex.sbuf, complete_regex.slen); + GNUNET_strndup (complete_regex.sbuf, complete_regex.slen); /* cleanup */ - sb_free(&complete_regex); + sb_free (&complete_regex); for (i = 0; i < n; i++) for (j = 0; j < n; j++) - { - sb_free(&R_cur[i * n + j]); - sb_free(&R_last[i * n + j]); - } - GNUNET_free(R_cur); - GNUNET_free(R_last); + { + sb_free (&R_cur[i * n + j]); + sb_free (&R_last[i * n + j]); + } + GNUNET_free (R_cur); + GNUNET_free (R_last); return GNUNET_OK; } @@ -1741,8 +1743,8 @@ automaton_create_proofs(struct REGEX_INTERNAL_Automaton *a) * @return new DFA state */ static struct REGEX_INTERNAL_State * -dfa_state_create(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_StateSet *nfa_states) +dfa_state_create (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_StateSet *nfa_states) { struct REGEX_INTERNAL_State *s; char *pos; @@ -1751,16 +1753,16 @@ dfa_state_create(struct REGEX_INTERNAL_Context *ctx, struct REGEX_INTERNAL_Transition *ctran; unsigned int i; - s = GNUNET_new(struct REGEX_INTERNAL_State); + s = GNUNET_new (struct REGEX_INTERNAL_State); s->id = ctx->state_id++; s->index = -1; s->lowlink = -1; if (NULL == nfa_states) - { - GNUNET_asprintf(&s->name, "s%i", s->id); - return s; - } + { + GNUNET_asprintf (&s->name, "s%i", s->id); + return s; + } s->nfa_set = *nfa_states; @@ -1769,30 +1771,30 @@ dfa_state_create(struct REGEX_INTERNAL_Context *ctx, /* Create a name based on 'nfa_states' */ len = nfa_states->off * 14 + 4; - s->name = GNUNET_malloc(len); - strcat(s->name, "{"); + s->name = GNUNET_malloc (len); + strcat (s->name, "{"); pos = s->name + 1; for (i = 0; i < nfa_states->off; i++) - { - cstate = nfa_states->states[i]; - GNUNET_snprintf(pos, pos - s->name + len, "%i,", cstate->id); - pos += strlen(pos); - - /* Add a transition for each distinct label to NULL state */ - for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next) - if (NULL != ctran->label) - state_add_transition(ctx, s, ctran->label, NULL); - - /* If the nfa_states contain an accepting state, the new dfa state is also - * accepting. */ - if (cstate->accepting) - s->accepting = 1; - } + { + cstate = nfa_states->states[i]; + GNUNET_snprintf (pos, pos - s->name + len, "%i,", cstate->id); + pos += strlen (pos); + + /* Add a transition for each distinct label to NULL state */ + for (ctran = cstate->transitions_head; NULL != ctran; ctran = ctran->next) + if (NULL != ctran->label) + state_add_transition (ctx, s, ctran->label, NULL); + + /* If the nfa_states contain an accepting state, the new dfa state is also + * accepting. */ + if (cstate->accepting) + s->accepting = 1; + } pos[-1] = '}'; - s->name = GNUNET_realloc(s->name, strlen(s->name) + 1); + s->name = GNUNET_realloc (s->name, strlen (s->name) + 1); - memset(nfa_states, 0, sizeof(struct REGEX_INTERNAL_StateSet)); + memset (nfa_states, 0, sizeof(struct REGEX_INTERNAL_StateSet)); return s; } @@ -1811,7 +1813,7 @@ dfa_state_create(struct REGEX_INTERNAL_Context *ctx, * @return length of the substring comsumed from 'str' */ static unsigned int -dfa_move(struct REGEX_INTERNAL_State **s, const char *str) +dfa_move (struct REGEX_INTERNAL_State **s, const char *str) { struct REGEX_INTERNAL_Transition *t; struct REGEX_INTERNAL_State *new_s; @@ -1824,18 +1826,18 @@ dfa_move(struct REGEX_INTERNAL_State **s, const char *str) new_s = NULL; max_len = 0; for (t = (*s)->transitions_head; NULL != t; t = t->next) - { - len = strlen(t->label); + { + len = strlen (t->label); - if (0 == strncmp(t->label, str, len)) - { - if (len >= max_len) - { - max_len = len; - new_s = t->to_state; - } - } + if (0 == strncmp (t->label, str, len)) + { + if (len >= max_len) + { + max_len = len; + new_s = t->to_state; + } } + } *s = new_s; return max_len; @@ -1852,9 +1854,9 @@ dfa_move(struct REGEX_INTERNAL_State **s, const char *str) * @param s state where the marked attribute will be set to #GNUNET_YES. */ static void -mark_states(void *cls, - const unsigned int count, - struct REGEX_INTERNAL_State *s) +mark_states (void *cls, + const unsigned int count, + struct REGEX_INTERNAL_State *s) { s->marked = GNUNET_YES; } @@ -1867,7 +1869,7 @@ mark_states(void *cls, * @param a DFA automaton */ static void -dfa_remove_unreachable_states(struct REGEX_INTERNAL_Automaton *a) +dfa_remove_unreachable_states (struct REGEX_INTERNAL_Automaton *a) { struct REGEX_INTERNAL_State *s; struct REGEX_INTERNAL_State *s_next; @@ -1877,20 +1879,20 @@ dfa_remove_unreachable_states(struct REGEX_INTERNAL_Automaton *a) s->marked = GNUNET_NO; /* 2. traverse dfa from start state and mark all visited states */ - REGEX_INTERNAL_automaton_traverse(a, - a->start, - NULL, - NULL, - &mark_states, - NULL); + REGEX_INTERNAL_automaton_traverse (a, + a->start, + NULL, + NULL, + &mark_states, + NULL); /* 3. delete all states that were not visited */ for (s = a->states_head; NULL != s; s = s_next) - { - s_next = s->next; - if (GNUNET_NO == s->marked) - automaton_remove_state(a, s); - } + { + s_next = s->next; + if (GNUNET_NO == s->marked) + automaton_remove_state (a, s); + } } @@ -1901,38 +1903,38 @@ dfa_remove_unreachable_states(struct REGEX_INTERNAL_Automaton *a) * @param a DFA automaton */ static void -dfa_remove_dead_states(struct REGEX_INTERNAL_Automaton *a) +dfa_remove_dead_states (struct REGEX_INTERNAL_Automaton *a) { struct REGEX_INTERNAL_State *s; struct REGEX_INTERNAL_State *s_next; struct REGEX_INTERNAL_Transition *t; int dead; - GNUNET_assert(DFA == a->type); + GNUNET_assert (DFA == a->type); for (s = a->states_head; NULL != s; s = s_next) - { - s_next = s->next; + { + s_next = s->next; - if (s->accepting) - continue; + if (s->accepting) + continue; - dead = 1; - for (t = s->transitions_head; NULL != t; t = t->next) - { - if (NULL != t->to_state && t->to_state != s) - { - dead = 0; - break; - } - } + dead = 1; + for (t = s->transitions_head; NULL != t; t = t->next) + { + if ((NULL != t->to_state) &&(t->to_state != s) ) + { + dead = 0; + break; + } + } - if (0 == dead) - continue; + if (0 == dead) + continue; - /* state s is dead, remove it */ - automaton_remove_state(a, s); - } + /* state s is dead, remove it */ + automaton_remove_state (a, s); + } } @@ -1944,8 +1946,8 @@ dfa_remove_dead_states(struct REGEX_INTERNAL_Automaton *a) * @return #GNUNET_OK on success */ static int -dfa_merge_nondistinguishable_states(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_Automaton *a) +dfa_merge_nondistinguishable_states (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_Automaton *a) { uint32_t *table; struct REGEX_INTERNAL_State *s1; @@ -1962,20 +1964,20 @@ dfa_merge_nondistinguishable_states(struct REGEX_INTERNAL_Context *ctx, unsigned long long idx1; if ((NULL == a) || (0 == a->state_count)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not merge nondistinguishable states, automaton was NULL.\n"); - return GNUNET_SYSERR; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not merge nondistinguishable states, automaton was NULL.\n"); + return GNUNET_SYSERR; + } state_cnt = a->state_count; - table = GNUNET_malloc_large( + table = GNUNET_malloc_large ( (sizeof(uint32_t) * state_cnt * state_cnt / 32) + sizeof(uint32_t)); if (NULL == table) - { - GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc"); - return GNUNET_SYSERR; - } + { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); + return GNUNET_SYSERR; + } for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next) s1->marked = i++; @@ -1983,74 +1985,74 @@ dfa_merge_nondistinguishable_states(struct REGEX_INTERNAL_Context *ctx, /* Mark all pairs of accepting/!accepting states */ for (s1 = a->states_head; NULL != s1; s1 = s1->next) for (s2 = a->states_head; NULL != s2; s2 = s2->next) - if ((s1->accepting && !s2->accepting) || - (!s1->accepting && s2->accepting)) - { - idx = (unsigned long long)s1->marked * state_cnt + s2->marked; - table[idx / 32] |= (1U << (idx % 32)); - } + if ((s1->accepting && ! s2->accepting) || + (! s1->accepting && s2->accepting)) + { + idx = (unsigned long long) s1->marked * state_cnt + s2->marked; + table[idx / 32] |= (1U << (idx % 32)); + } /* Find all equal states */ change = 1; while (0 != change) + { + change = 0; + for (s1 = a->states_head; NULL != s1; s1 = s1->next) { - change = 0; - for (s1 = a->states_head; NULL != s1; s1 = s1->next) + for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next) + { + idx = (unsigned long long) s1->marked * state_cnt + s2->marked; + if (0 != (table[idx / 32] & (1U << (idx % 32)))) + continue; + num_equal_edges = 0; + for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next) { - for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2->next) + for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next) + { + if (0 == strcmp (t1->label, t2->label)) { - idx = (unsigned long long)s1->marked * state_cnt + s2->marked; - if (0 != (table[idx / 32] & (1U << (idx % 32)))) - continue; - num_equal_edges = 0; - for (t1 = s1->transitions_head; NULL != t1; t1 = t1->next) - { - for (t2 = s2->transitions_head; NULL != t2; t2 = t2->next) - { - if (0 == strcmp(t1->label, t2->label)) - { - num_equal_edges++; - /* same edge, but targets definitively different, so we're different - as well */ - if (t1->to_state->marked > t2->to_state->marked) - idx1 = (unsigned long long)t1->to_state->marked * state_cnt + - t2->to_state->marked; - else - idx1 = (unsigned long long)t2->to_state->marked * state_cnt + - t1->to_state->marked; - if (0 != (table[idx1 / 32] & (1U << (idx1 % 32)))) - { - table[idx / 32] |= (1U << (idx % 32)); - change = 1; /* changed a marker, need to run again */ - } - } - } - } - if ((num_equal_edges != s1->transition_count) || - (num_equal_edges != s2->transition_count)) - { - /* Make sure ALL edges of possible equal states are the same */ - table[idx / 32] |= (1U << (idx % 32)); - change = 1; /* changed a marker, need to run again */ - } + num_equal_edges++; + /* same edge, but targets definitively different, so we're different + as well */ + if (t1->to_state->marked > t2->to_state->marked) + idx1 = (unsigned long long) t1->to_state->marked * state_cnt + + t2->to_state->marked; + else + idx1 = (unsigned long long) t2->to_state->marked * state_cnt + + t1->to_state->marked; + if (0 != (table[idx1 / 32] & (1U << (idx1 % 32)))) + { + table[idx / 32] |= (1U << (idx % 32)); + change = 1; /* changed a marker, need to run again */ + } } + } } + if ((num_equal_edges != s1->transition_count) || + (num_equal_edges != s2->transition_count)) + { + /* Make sure ALL edges of possible equal states are the same */ + table[idx / 32] |= (1U << (idx % 32)); + change = 1; /* changed a marker, need to run again */ + } + } } + } /* Merge states that are equal */ for (s1 = a->states_head; NULL != s1; s1 = s1_next) + { + s1_next = s1->next; + for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next) { - s1_next = s1->next; - for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next) - { - s2_next = s2->next; - idx = (unsigned long long)s1->marked * state_cnt + s2->marked; - if (0 == (table[idx / 32] & (1U << (idx % 32)))) - automaton_merge_states(ctx, a, s1, s2); - } + s2_next = s2->next; + idx = (unsigned long long) s1->marked * state_cnt + s2->marked; + if (0 == (table[idx / 32] & (1U << (idx % 32)))) + automaton_merge_states (ctx, a, s1, s2); } + } - GNUNET_free(table); + GNUNET_free (table); return GNUNET_OK; } @@ -2064,22 +2066,22 @@ dfa_merge_nondistinguishable_states(struct REGEX_INTERNAL_Context *ctx, * @return GNUNET_OK on success */ static int -dfa_minimize(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_Automaton *a) +dfa_minimize (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_Automaton *a) { if (NULL == a) return GNUNET_SYSERR; - GNUNET_assert(DFA == a->type); + GNUNET_assert (DFA == a->type); /* 1. remove unreachable states */ - dfa_remove_unreachable_states(a); + dfa_remove_unreachable_states (a); /* 2. remove dead states */ - dfa_remove_dead_states(a); + dfa_remove_dead_states (a); /* 3. Merge nondistinguishable states */ - if (GNUNET_OK != dfa_merge_nondistinguishable_states(ctx, a)) + if (GNUNET_OK != dfa_merge_nondistinguishable_states (ctx, a)) return GNUNET_SYSERR; return GNUNET_OK; } @@ -2088,7 +2090,8 @@ dfa_minimize(struct REGEX_INTERNAL_Context *ctx, /** * Context for adding strided transitions to a DFA. */ -struct REGEX_INTERNAL_Strided_Context { +struct REGEX_INTERNAL_Strided_Context +{ /** * Length of the strides. */ @@ -2118,50 +2121,50 @@ struct REGEX_INTERNAL_Strided_Context { * @param s current state in the depth-first traversal */ static void -dfa_add_multi_strides_helper(void *cls, - const unsigned int depth, - char *label, - struct REGEX_INTERNAL_State *start, - struct REGEX_INTERNAL_State *s) +dfa_add_multi_strides_helper (void *cls, + const unsigned int depth, + char *label, + struct REGEX_INTERNAL_State *start, + struct REGEX_INTERNAL_State *s) { struct REGEX_INTERNAL_Strided_Context *ctx = cls; struct REGEX_INTERNAL_Transition *t; char *new_label; if (depth == ctx->stride) - { - t = GNUNET_new(struct REGEX_INTERNAL_Transition); - t->label = GNUNET_strdup(label); - t->to_state = s; - t->from_state = start; - GNUNET_CONTAINER_DLL_insert(ctx->transitions_head, - ctx->transitions_tail, - t); - } + { + t = GNUNET_new (struct REGEX_INTERNAL_Transition); + t->label = GNUNET_strdup (label); + t->to_state = s; + t->from_state = start; + GNUNET_CONTAINER_DLL_insert (ctx->transitions_head, + ctx->transitions_tail, + t); + } else + { + for (t = s->transitions_head; NULL != t; t = t->next) { - for (t = s->transitions_head; NULL != t; t = t->next) - { - /* Do not consider self-loops, because it end's up in too many - * transitions */ - if (t->to_state == t->from_state) - continue; + /* Do not consider self-loops, because it end's up in too many + * transitions */ + if (t->to_state == t->from_state) + continue; - if (NULL != label) - { - GNUNET_asprintf(&new_label, "%s%s", label, t->label); - } - else - new_label = GNUNET_strdup(t->label); + if (NULL != label) + { + GNUNET_asprintf (&new_label, "%s%s", label, t->label); + } + else + new_label = GNUNET_strdup (t->label); - dfa_add_multi_strides_helper(cls, - (depth + 1), - new_label, - start, - t->to_state); - } + dfa_add_multi_strides_helper (cls, + (depth + 1), + new_label, + start, + t->to_state); } - GNUNET_free_non_null(label); + } + GNUNET_free_non_null (label); } @@ -2174,11 +2177,11 @@ dfa_add_multi_strides_helper(void *cls, * @param s current state. */ static void -dfa_add_multi_strides(void *cls, - const unsigned int count, - struct REGEX_INTERNAL_State *s) +dfa_add_multi_strides (void *cls, + const unsigned int count, + struct REGEX_INTERNAL_State *s) { - dfa_add_multi_strides_helper(cls, 0, NULL, s, s); + dfa_add_multi_strides_helper (cls, 0, NULL, s, s); } @@ -2190,34 +2193,34 @@ dfa_add_multi_strides(void *cls, * @param stride_len length of the strides. */ void -REGEX_INTERNAL_dfa_add_multi_strides(struct REGEX_INTERNAL_Context *regex_ctx, - struct REGEX_INTERNAL_Automaton *dfa, - const unsigned int stride_len) +REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx, + struct REGEX_INTERNAL_Automaton *dfa, + const unsigned int stride_len) { struct REGEX_INTERNAL_Strided_Context ctx = { stride_len, NULL, NULL }; struct REGEX_INTERNAL_Transition *t; struct REGEX_INTERNAL_Transition *t_next; - if (1 > stride_len || GNUNET_YES == dfa->is_multistrided) + if ((1 > stride_len)||(GNUNET_YES == dfa->is_multistrided)) return; /* Compute the new transitions of given stride_len */ - REGEX_INTERNAL_automaton_traverse(dfa, - dfa->start, - NULL, - NULL, - &dfa_add_multi_strides, - &ctx); + REGEX_INTERNAL_automaton_traverse (dfa, + dfa->start, + NULL, + NULL, + &dfa_add_multi_strides, + &ctx); /* Add all the new transitions to the automaton. */ for (t = ctx.transitions_head; NULL != t; t = t_next) - { - t_next = t->next; - state_add_transition(regex_ctx, t->from_state, t->label, t->to_state); - GNUNET_CONTAINER_DLL_remove(ctx.transitions_head, ctx.transitions_tail, t); - GNUNET_free_non_null(t->label); - GNUNET_free(t); - } + { + t_next = t->next; + state_add_transition (regex_ctx, t->from_state, t->label, t->to_state); + GNUNET_CONTAINER_DLL_remove (ctx.transitions_head, ctx.transitions_tail, t); + GNUNET_free_non_null (t->label); + GNUNET_free (t); + } /* Mark this automaton as multistrided */ dfa->is_multistrided = GNUNET_YES; @@ -2237,70 +2240,70 @@ REGEX_INTERNAL_dfa_add_multi_strides(struct REGEX_INTERNAL_Context *regex_ctx, * @param transitions_tail transitions DLL. */ void -dfa_compress_paths_helper(struct REGEX_INTERNAL_Automaton *dfa, - struct REGEX_INTERNAL_State *start, - struct REGEX_INTERNAL_State *cur, - char *label, - unsigned int max_len, - struct REGEX_INTERNAL_Transition **transitions_head, - struct REGEX_INTERNAL_Transition **transitions_tail) +dfa_compress_paths_helper (struct REGEX_INTERNAL_Automaton *dfa, + struct REGEX_INTERNAL_State *start, + struct REGEX_INTERNAL_State *cur, + char *label, + unsigned int max_len, + struct REGEX_INTERNAL_Transition **transitions_head, + struct REGEX_INTERNAL_Transition **transitions_tail) { struct REGEX_INTERNAL_Transition *t; char *new_label; - if (NULL != label && - ((cur->incoming_transition_count > 1 || GNUNET_YES == cur->accepting || - GNUNET_YES == cur->marked) || - (start != dfa->start && max_len > 0 && max_len == strlen(label)) || - (start == dfa->start && GNUNET_REGEX_INITIAL_BYTES == strlen(label)))) - { - t = GNUNET_new(struct REGEX_INTERNAL_Transition); - t->label = GNUNET_strdup(label); - t->to_state = cur; - t->from_state = start; - GNUNET_CONTAINER_DLL_insert(*transitions_head, *transitions_tail, t); - - if (GNUNET_NO == cur->marked) - { - dfa_compress_paths_helper(dfa, - cur, - cur, - NULL, - max_len, - transitions_head, - transitions_tail); - } - return; + if ((NULL != label)&& + (((cur->incoming_transition_count > 1)||(GNUNET_YES == cur->accepting)|| + (GNUNET_YES == cur->marked) ) || + ((start != dfa->start)&&(max_len > 0)&&(max_len == strlen (label))) || + ((start == dfa->start)&&(GNUNET_REGEX_INITIAL_BYTES == strlen (label))))) + { + t = GNUNET_new (struct REGEX_INTERNAL_Transition); + t->label = GNUNET_strdup (label); + t->to_state = cur; + t->from_state = start; + GNUNET_CONTAINER_DLL_insert (*transitions_head, *transitions_tail, t); + + if (GNUNET_NO == cur->marked) + { + dfa_compress_paths_helper (dfa, + cur, + cur, + NULL, + max_len, + transitions_head, + transitions_tail); } + return; + } else if (cur != start) cur->contained = GNUNET_YES; - if (GNUNET_YES == cur->marked && cur != start) + if ((GNUNET_YES == cur->marked)&&(cur != start)) return; cur->marked = GNUNET_YES; for (t = cur->transitions_head; NULL != t; t = t->next) - { - if (NULL != label) - GNUNET_asprintf(&new_label, "%s%s", label, t->label); - else - new_label = GNUNET_strdup(t->label); + { + if (NULL != label) + GNUNET_asprintf (&new_label, "%s%s", label, t->label); + else + new_label = GNUNET_strdup (t->label); - if (t->to_state != cur) - { - dfa_compress_paths_helper(dfa, - start, - t->to_state, - new_label, - max_len, - transitions_head, - transitions_tail); - } - GNUNET_free(new_label); + if (t->to_state != cur) + { + dfa_compress_paths_helper (dfa, + start, + t->to_state, + new_label, + max_len, + transitions_head, + transitions_tail); } + GNUNET_free (new_label); + } } @@ -2313,9 +2316,9 @@ dfa_compress_paths_helper(struct REGEX_INTERNAL_Automaton *dfa, * @param max_len maximal length of the compressed paths. */ static void -dfa_compress_paths(struct REGEX_INTERNAL_Context *regex_ctx, - struct REGEX_INTERNAL_Automaton *dfa, - unsigned int max_len) +dfa_compress_paths (struct REGEX_INTERNAL_Context *regex_ctx, + struct REGEX_INTERNAL_Automaton *dfa, + unsigned int max_len) { struct REGEX_INTERNAL_State *s; struct REGEX_INTERNAL_State *s_next; @@ -2329,47 +2332,47 @@ dfa_compress_paths(struct REGEX_INTERNAL_Context *regex_ctx, /* Count the incoming transitions on each state. */ for (s = dfa->states_head; NULL != s; s = s->next) + { + for (t = s->transitions_head; NULL != t; t = t->next) { - for (t = s->transitions_head; NULL != t; t = t->next) - { - if (NULL != t->to_state) - t->to_state->incoming_transition_count++; - } + if (NULL != t->to_state) + t->to_state->incoming_transition_count++; } + } /* Unmark all states. */ for (s = dfa->states_head; NULL != s; s = s->next) - { - s->marked = GNUNET_NO; - s->contained = GNUNET_NO; - } + { + s->marked = GNUNET_NO; + s->contained = GNUNET_NO; + } /* Add strides and mark states that can be deleted. */ - dfa_compress_paths_helper(dfa, - dfa->start, - dfa->start, - NULL, - max_len, - &transitions_head, - &transitions_tail); + dfa_compress_paths_helper (dfa, + dfa->start, + dfa->start, + NULL, + max_len, + &transitions_head, + &transitions_tail); /* Add all the new transitions to the automaton. */ for (t = transitions_head; NULL != t; t = t_next) - { - t_next = t->next; - state_add_transition(regex_ctx, t->from_state, t->label, t->to_state); - GNUNET_CONTAINER_DLL_remove(transitions_head, transitions_tail, t); - GNUNET_free_non_null(t->label); - GNUNET_free(t); - } + { + t_next = t->next; + state_add_transition (regex_ctx, t->from_state, t->label, t->to_state); + GNUNET_CONTAINER_DLL_remove (transitions_head, transitions_tail, t); + GNUNET_free_non_null (t->label); + GNUNET_free (t); + } /* Remove marked states (including their incoming and outgoing transitions). */ for (s = dfa->states_head; NULL != s; s = s_next) - { - s_next = s->next; - if (GNUNET_YES == s->contained) - automaton_remove_state(dfa, s); - } + { + s_next = s->next; + if (GNUNET_YES == s->contained) + automaton_remove_state (dfa, s); + } } @@ -2383,23 +2386,23 @@ dfa_compress_paths(struct REGEX_INTERNAL_Context *regex_ctx, * @return new NFA fragment */ static struct REGEX_INTERNAL_Automaton * -nfa_fragment_create(struct REGEX_INTERNAL_State *start, - struct REGEX_INTERNAL_State *end) +nfa_fragment_create (struct REGEX_INTERNAL_State *start, + struct REGEX_INTERNAL_State *end) { struct REGEX_INTERNAL_Automaton *n; - n = GNUNET_new(struct REGEX_INTERNAL_Automaton); + n = GNUNET_new (struct REGEX_INTERNAL_Automaton); n->type = NFA; n->start = NULL; n->end = NULL; n->state_count = 0; - if (NULL == start || NULL == end) + if ((NULL == start)||(NULL == end)) return n; - automaton_add_state(n, end); - automaton_add_state(n, start); + automaton_add_state (n, end); + automaton_add_state (n, start); n->state_count = 2; @@ -2418,30 +2421,30 @@ nfa_fragment_create(struct REGEX_INTERNAL_State *start, * @param states_tail tail of the DLL of states */ static void -nfa_add_states(struct REGEX_INTERNAL_Automaton *n, - struct REGEX_INTERNAL_State *states_head, - struct REGEX_INTERNAL_State *states_tail) +nfa_add_states (struct REGEX_INTERNAL_Automaton *n, + struct REGEX_INTERNAL_State *states_head, + struct REGEX_INTERNAL_State *states_tail) { struct REGEX_INTERNAL_State *s; - if (NULL == n || NULL == states_head) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not add states\n"); - return; - } + if ((NULL == n)||(NULL == states_head)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not add states\n"); + return; + } if (NULL == n->states_head) - { - n->states_head = states_head; - n->states_tail = states_tail; - return; - } + { + n->states_head = states_head; + n->states_tail = states_tail; + return; + } if (NULL != states_head) - { - n->states_tail->next = states_head; - n->states_tail = states_tail; - } + { + n->states_tail->next = states_head; + n->states_tail = states_tail; + } for (s = states_head; NULL != s; s = s->next) n->state_count++; @@ -2457,11 +2460,11 @@ nfa_add_states(struct REGEX_INTERNAL_Automaton *n, * @return new NFA state */ static struct REGEX_INTERNAL_State * -nfa_state_create(struct REGEX_INTERNAL_Context *ctx, int accepting) +nfa_state_create (struct REGEX_INTERNAL_Context *ctx, int accepting) { struct REGEX_INTERNAL_State *s; - s = GNUNET_new(struct REGEX_INTERNAL_State); + s = GNUNET_new (struct REGEX_INTERNAL_State); s->id = ctx->state_id++; s->accepting = accepting; s->marked = GNUNET_NO; @@ -2470,7 +2473,7 @@ nfa_state_create(struct REGEX_INTERNAL_Context *ctx, int accepting) s->lowlink = -1; s->scc_id = 0; s->name = NULL; - GNUNET_asprintf(&s->name, "s%i", s->id); + GNUNET_asprintf (&s->name, "s%i", s->id); return s; } @@ -2486,10 +2489,10 @@ nfa_state_create(struct REGEX_INTERNAL_Context *ctx, int accepting) * pass NULL for epsilon transition */ static void -nfa_closure_set_create(struct REGEX_INTERNAL_StateSet *ret, - struct REGEX_INTERNAL_Automaton *nfa, - struct REGEX_INTERNAL_StateSet *states, - const char *label) +nfa_closure_set_create (struct REGEX_INTERNAL_StateSet *ret, + struct REGEX_INTERNAL_Automaton *nfa, + struct REGEX_INTERNAL_StateSet *states, + const char *label) { struct REGEX_INTERNAL_State *s; unsigned int i; @@ -2498,58 +2501,58 @@ nfa_closure_set_create(struct REGEX_INTERNAL_StateSet *ret, struct REGEX_INTERNAL_State *currentstate; struct REGEX_INTERNAL_Transition *ctran; - memset(ret, 0, sizeof(struct REGEX_INTERNAL_StateSet)); + memset (ret, 0, sizeof(struct REGEX_INTERNAL_StateSet)); if (NULL == states) return; for (i = 0; i < states->off; i++) - { - s = states->states[i]; - - /* Add start state to closure only for epsilon closure */ - if (NULL == label) - state_set_append(ret, s); - - /* initialize work stack */ - cls_stack.head = NULL; - cls_stack.tail = NULL; - GNUNET_CONTAINER_MDLL_insert(ST, cls_stack.head, cls_stack.tail, s); - cls_stack.len = 1; - - while (NULL != (currentstate = cls_stack.tail)) - { - GNUNET_CONTAINER_MDLL_remove(ST, - cls_stack.head, - cls_stack.tail, - currentstate); - cls_stack.len--; - for (ctran = currentstate->transitions_head; NULL != ctran; - ctran = ctran->next) - { - if (NULL == (clsstate = ctran->to_state)) - continue; - if (0 != clsstate->contained) - continue; - if (0 != nullstrcmp(label, ctran->label)) - continue; - state_set_append(ret, clsstate); - GNUNET_CONTAINER_MDLL_insert_tail(ST, - cls_stack.head, - cls_stack.tail, - clsstate); - cls_stack.len++; - clsstate->contained = 1; - } - } + { + s = states->states[i]; + + /* Add start state to closure only for epsilon closure */ + if (NULL == label) + state_set_append (ret, s); + + /* initialize work stack */ + cls_stack.head = NULL; + cls_stack.tail = NULL; + GNUNET_CONTAINER_MDLL_insert (ST, cls_stack.head, cls_stack.tail, s); + cls_stack.len = 1; + + while (NULL != (currentstate = cls_stack.tail)) + { + GNUNET_CONTAINER_MDLL_remove (ST, + cls_stack.head, + cls_stack.tail, + currentstate); + cls_stack.len--; + for (ctran = currentstate->transitions_head; NULL != ctran; + ctran = ctran->next) + { + if (NULL == (clsstate = ctran->to_state)) + continue; + if (0 != clsstate->contained) + continue; + if (0 != nullstrcmp (label, ctran->label)) + continue; + state_set_append (ret, clsstate); + GNUNET_CONTAINER_MDLL_insert_tail (ST, + cls_stack.head, + cls_stack.tail, + clsstate); + cls_stack.len++; + clsstate->contained = 1; + } } + } for (i = 0; i < ret->off; i++) ret->states[i]->contained = 0; if (ret->off > 1) - qsort(ret->states, - ret->off, - sizeof(struct REGEX_INTERNAL_State *), - &state_compare); + qsort (ret->states, + ret->off, + sizeof(struct REGEX_INTERNAL_State *), + &state_compare); } @@ -2559,33 +2562,33 @@ nfa_closure_set_create(struct REGEX_INTERNAL_StateSet *ret, * @param ctx context */ static void -nfa_add_concatenation(struct REGEX_INTERNAL_Context *ctx) +nfa_add_concatenation (struct REGEX_INTERNAL_Context *ctx) { struct REGEX_INTERNAL_Automaton *a; struct REGEX_INTERNAL_Automaton *b; struct REGEX_INTERNAL_Automaton *new_nfa; b = ctx->stack_tail; - GNUNET_assert(NULL != b); - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, b); + GNUNET_assert (NULL != b); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b); a = ctx->stack_tail; - GNUNET_assert(NULL != a); - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a); + GNUNET_assert (NULL != a); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); - state_add_transition(ctx, a->end, NULL, b->start); + state_add_transition (ctx, a->end, NULL, b->start); a->end->accepting = 0; b->end->accepting = 1; - new_nfa = nfa_fragment_create(NULL, NULL); - nfa_add_states(new_nfa, a->states_head, a->states_tail); - nfa_add_states(new_nfa, b->states_head, b->states_tail); + new_nfa = nfa_fragment_create (NULL, NULL); + nfa_add_states (new_nfa, a->states_head, a->states_tail); + nfa_add_states (new_nfa, b->states_head, b->states_tail); new_nfa->start = a->start; new_nfa->end = b->end; new_nfa->state_count += a->state_count + b->state_count; - automaton_fragment_clear(a); - automaton_fragment_clear(b); + automaton_fragment_clear (a); + automaton_fragment_clear (b); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); } @@ -2595,7 +2598,7 @@ nfa_add_concatenation(struct REGEX_INTERNAL_Context *ctx) * @param ctx context */ static void -nfa_add_star_op(struct REGEX_INTERNAL_Context *ctx) +nfa_add_star_op (struct REGEX_INTERNAL_Context *ctx) { struct REGEX_INTERNAL_Automaton *a; struct REGEX_INTERNAL_Automaton *new_nfa; @@ -2605,31 +2608,31 @@ nfa_add_star_op(struct REGEX_INTERNAL_Context *ctx) a = ctx->stack_tail; if (NULL == a) - { - GNUNET_log( - GNUNET_ERROR_TYPE_ERROR, - "nfa_add_star_op failed, because there was no element on the stack"); - return; - } + { + GNUNET_log ( + GNUNET_ERROR_TYPE_ERROR, + "nfa_add_star_op failed, because there was no element on the stack"); + return; + } - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); - start = nfa_state_create(ctx, 0); - end = nfa_state_create(ctx, 1); + start = nfa_state_create (ctx, 0); + end = nfa_state_create (ctx, 1); - state_add_transition(ctx, start, NULL, a->start); - state_add_transition(ctx, start, NULL, end); - state_add_transition(ctx, a->end, NULL, a->start); - state_add_transition(ctx, a->end, NULL, end); + state_add_transition (ctx, start, NULL, a->start); + state_add_transition (ctx, start, NULL, end); + state_add_transition (ctx, a->end, NULL, a->start); + state_add_transition (ctx, a->end, NULL, end); a->end->accepting = 0; end->accepting = 1; - new_nfa = nfa_fragment_create(start, end); - nfa_add_states(new_nfa, a->states_head, a->states_tail); - automaton_fragment_clear(a); + new_nfa = nfa_fragment_create (start, end); + nfa_add_states (new_nfa, a->states_head, a->states_tail); + automaton_fragment_clear (a); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); } @@ -2639,25 +2642,25 @@ nfa_add_star_op(struct REGEX_INTERNAL_Context *ctx) * @param ctx context */ static void -nfa_add_plus_op(struct REGEX_INTERNAL_Context *ctx) +nfa_add_plus_op (struct REGEX_INTERNAL_Context *ctx) { struct REGEX_INTERNAL_Automaton *a; a = ctx->stack_tail; if (NULL == a) - { - GNUNET_log( - GNUNET_ERROR_TYPE_ERROR, - "nfa_add_plus_op failed, because there was no element on the stack"); - return; - } + { + GNUNET_log ( + GNUNET_ERROR_TYPE_ERROR, + "nfa_add_plus_op failed, because there was no element on the stack"); + return; + } - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); - state_add_transition(ctx, a->end, NULL, a->start); + state_add_transition (ctx, a->end, NULL, a->start); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, a); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, a); } @@ -2667,7 +2670,7 @@ nfa_add_plus_op(struct REGEX_INTERNAL_Context *ctx) * @param ctx context */ static void -nfa_add_question_op(struct REGEX_INTERNAL_Context *ctx) +nfa_add_question_op (struct REGEX_INTERNAL_Context *ctx) { struct REGEX_INTERNAL_Automaton *a; struct REGEX_INTERNAL_Automaton *new_nfa; @@ -2676,28 +2679,28 @@ nfa_add_question_op(struct REGEX_INTERNAL_Context *ctx) a = ctx->stack_tail; if (NULL == a) - { - GNUNET_log( - GNUNET_ERROR_TYPE_ERROR, - "nfa_add_question_op failed, because there was no element on the stack"); - return; - } + { + GNUNET_log ( + GNUNET_ERROR_TYPE_ERROR, + "nfa_add_question_op failed, because there was no element on the stack"); + return; + } - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); - start = nfa_state_create(ctx, 0); - end = nfa_state_create(ctx, 1); + start = nfa_state_create (ctx, 0); + end = nfa_state_create (ctx, 1); - state_add_transition(ctx, start, NULL, a->start); - state_add_transition(ctx, start, NULL, end); - state_add_transition(ctx, a->end, NULL, end); + state_add_transition (ctx, start, NULL, a->start); + state_add_transition (ctx, start, NULL, end); + state_add_transition (ctx, a->end, NULL, end); a->end->accepting = 0; - new_nfa = nfa_fragment_create(start, end); - nfa_add_states(new_nfa, a->states_head, a->states_tail); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa); - automaton_fragment_clear(a); + new_nfa = nfa_fragment_create (start, end); + nfa_add_states (new_nfa, a->states_head, a->states_tail); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); + automaton_fragment_clear (a); } @@ -2708,7 +2711,7 @@ nfa_add_question_op(struct REGEX_INTERNAL_Context *ctx) * @param ctx context */ static void -nfa_add_alternation(struct REGEX_INTERNAL_Context *ctx) +nfa_add_alternation (struct REGEX_INTERNAL_Context *ctx) { struct REGEX_INTERNAL_Automaton *a; struct REGEX_INTERNAL_Automaton *b; @@ -2717,31 +2720,31 @@ nfa_add_alternation(struct REGEX_INTERNAL_Context *ctx) struct REGEX_INTERNAL_State *end; b = ctx->stack_tail; - GNUNET_assert(NULL != b); - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, b); + GNUNET_assert (NULL != b); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, b); a = ctx->stack_tail; - GNUNET_assert(NULL != a); - GNUNET_CONTAINER_DLL_remove(ctx->stack_head, ctx->stack_tail, a); + GNUNET_assert (NULL != a); + GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a); - start = nfa_state_create(ctx, 0); - end = nfa_state_create(ctx, 1); - state_add_transition(ctx, start, NULL, a->start); - state_add_transition(ctx, start, NULL, b->start); + start = nfa_state_create (ctx, 0); + end = nfa_state_create (ctx, 1); + state_add_transition (ctx, start, NULL, a->start); + state_add_transition (ctx, start, NULL, b->start); - state_add_transition(ctx, a->end, NULL, end); - state_add_transition(ctx, b->end, NULL, end); + state_add_transition (ctx, a->end, NULL, end); + state_add_transition (ctx, b->end, NULL, end); a->end->accepting = 0; b->end->accepting = 0; end->accepting = 1; - new_nfa = nfa_fragment_create(start, end); - nfa_add_states(new_nfa, a->states_head, a->states_tail); - nfa_add_states(new_nfa, b->states_head, b->states_tail); - automaton_fragment_clear(a); - automaton_fragment_clear(b); + new_nfa = nfa_fragment_create (start, end); + nfa_add_states (new_nfa, a->states_head, a->states_tail); + nfa_add_states (new_nfa, b->states_head, b->states_tail); + automaton_fragment_clear (a); + automaton_fragment_clear (b); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, new_nfa); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa); } @@ -2752,20 +2755,20 @@ nfa_add_alternation(struct REGEX_INTERNAL_Context *ctx) * @param label label for nfa transition */ static void -nfa_add_label(struct REGEX_INTERNAL_Context *ctx, const char *label) +nfa_add_label (struct REGEX_INTERNAL_Context *ctx, const char *label) { struct REGEX_INTERNAL_Automaton *n; struct REGEX_INTERNAL_State *start; struct REGEX_INTERNAL_State *end; - GNUNET_assert(NULL != ctx); + GNUNET_assert (NULL != ctx); - start = nfa_state_create(ctx, 0); - end = nfa_state_create(ctx, 1); - state_add_transition(ctx, start, label, end); - n = nfa_fragment_create(start, end); - GNUNET_assert(NULL != n); - GNUNET_CONTAINER_DLL_insert_tail(ctx->stack_head, ctx->stack_tail, n); + start = nfa_state_create (ctx, 0); + end = nfa_state_create (ctx, 1); + state_add_transition (ctx, start, label, end); + n = nfa_fragment_create (start, end); + GNUNET_assert (NULL != n); + GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, n); } @@ -2775,13 +2778,13 @@ nfa_add_label(struct REGEX_INTERNAL_Context *ctx, const char *label) * @param ctx context */ static void -REGEX_INTERNAL_context_init(struct REGEX_INTERNAL_Context *ctx) +REGEX_INTERNAL_context_init (struct REGEX_INTERNAL_Context *ctx) { if (NULL == ctx) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Context was NULL!"); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Context was NULL!"); + return; + } ctx->state_id = 0; ctx->transition_id = 0; ctx->stack_head = NULL; @@ -2798,7 +2801,7 @@ REGEX_INTERNAL_context_init(struct REGEX_INTERNAL_Context *ctx) * @return NFA, needs to be freed using REGEX_INTERNAL_destroy_automaton */ struct REGEX_INTERNAL_Automaton * -REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len) +REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len) { struct REGEX_INTERNAL_Context ctx; struct REGEX_INTERNAL_Automaton *nfa; @@ -2811,19 +2814,20 @@ REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len) unsigned int poff; unsigned int psize; - struct { + struct + { int altcount; int atomcount; - } * p; + } *p; - if (NULL == regex || 0 == strlen(regex) || 0 == len) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not parse regex. Empty regex string provided.\n"); + if ((NULL == regex)||(0 == strlen (regex))||(0 == len)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not parse regex. Empty regex string provided.\n"); - return NULL; - } - REGEX_INTERNAL_context_init(&ctx); + return NULL; + } + REGEX_INTERNAL_context_init (&ctx); regexp = regex; curlabel[1] = '\0'; @@ -2835,129 +2839,129 @@ REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len) psize = 0; for (count = 0; count < len && *regexp; count++, regexp++) + { + switch (*regexp) { - switch (*regexp) - { - case '(': - if (atomcount > 1) - { - --atomcount; - nfa_add_concatenation(&ctx); - } - if (poff == psize) - GNUNET_array_grow(p, psize, psize * 2 + 4); /* FIXME why *2 +4? */ - p[poff].altcount = altcount; - p[poff].atomcount = atomcount; - poff++; - altcount = 0; - atomcount = 0; - break; - - case '|': - if (0 == atomcount) - { - error_msg = "Cannot append '|' to nothing"; - goto error; - } - while (--atomcount > 0) - nfa_add_concatenation(&ctx); - altcount++; - break; + case '(': + if (atomcount > 1) + { + --atomcount; + nfa_add_concatenation (&ctx); + } + if (poff == psize) + GNUNET_array_grow (p, psize, psize * 2 + 4); /* FIXME why *2 +4? */ + p[poff].altcount = altcount; + p[poff].atomcount = atomcount; + poff++; + altcount = 0; + atomcount = 0; + break; - case ')': - if (0 == poff) - { - error_msg = "Missing opening '('"; - goto error; - } - if (0 == atomcount) - { - /* Ignore this: "()" */ - poff--; - altcount = p[poff].altcount; - atomcount = p[poff].atomcount; - break; - } - while (--atomcount > 0) - nfa_add_concatenation(&ctx); - for (; altcount > 0; altcount--) - nfa_add_alternation(&ctx); - poff--; - altcount = p[poff].altcount; - atomcount = p[poff].atomcount; - atomcount++; - break; - - case '*': - if (atomcount == 0) - { - error_msg = "Cannot append '*' to nothing"; - goto error; - } - nfa_add_star_op(&ctx); - break; + case '|': + if (0 == atomcount) + { + error_msg = "Cannot append '|' to nothing"; + goto error; + } + while (--atomcount > 0) + nfa_add_concatenation (&ctx); + altcount++; + break; - case '+': - if (atomcount == 0) - { - error_msg = "Cannot append '+' to nothing"; - goto error; - } - nfa_add_plus_op(&ctx); - break; + case ')': + if (0 == poff) + { + error_msg = "Missing opening '('"; + goto error; + } + if (0 == atomcount) + { + /* Ignore this: "()" */ + poff--; + altcount = p[poff].altcount; + atomcount = p[poff].atomcount; + break; + } + while (--atomcount > 0) + nfa_add_concatenation (&ctx); + for (; altcount > 0; altcount--) + nfa_add_alternation (&ctx); + poff--; + altcount = p[poff].altcount; + atomcount = p[poff].atomcount; + atomcount++; + break; - case '?': - if (atomcount == 0) - { - error_msg = "Cannot append '?' to nothing"; - goto error; - } - nfa_add_question_op(&ctx); - break; + case '*': + if (atomcount == 0) + { + error_msg = "Cannot append '*' to nothing"; + goto error; + } + nfa_add_star_op (&ctx); + break; - default: - if (atomcount > 1) - { - --atomcount; - nfa_add_concatenation(&ctx); - } - curlabel[0] = *regexp; - nfa_add_label(&ctx, curlabel); - atomcount++; - break; - } + case '+': + if (atomcount == 0) + { + error_msg = "Cannot append '+' to nothing"; + goto error; + } + nfa_add_plus_op (&ctx); + break; + + case '?': + if (atomcount == 0) + { + error_msg = "Cannot append '?' to nothing"; + goto error; + } + nfa_add_question_op (&ctx); + break; + + default: + if (atomcount > 1) + { + --atomcount; + nfa_add_concatenation (&ctx); + } + curlabel[0] = *regexp; + nfa_add_label (&ctx, curlabel); + atomcount++; + break; } + } if (0 != poff) - { - error_msg = "Unbalanced parenthesis"; - goto error; - } + { + error_msg = "Unbalanced parenthesis"; + goto error; + } while (--atomcount > 0) - nfa_add_concatenation(&ctx); + nfa_add_concatenation (&ctx); for (; altcount > 0; altcount--) - nfa_add_alternation(&ctx); + nfa_add_alternation (&ctx); - GNUNET_array_grow(p, psize, 0); + GNUNET_array_grow (p, psize, 0); nfa = ctx.stack_tail; - GNUNET_CONTAINER_DLL_remove(ctx.stack_head, ctx.stack_tail, nfa); + GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa); if (NULL != ctx.stack_head) - { - error_msg = "Creating the NFA failed. NFA stack was not empty!"; - goto error; - } + { + error_msg = "Creating the NFA failed. NFA stack was not empty!"; + goto error; + } /* Remember the regex that was used to generate this NFA */ - nfa->regex = GNUNET_strdup(regex); + nfa->regex = GNUNET_strdup (regex); /* create depth-first numbering of the states for pretty printing */ - REGEX_INTERNAL_automaton_traverse(nfa, - NULL, - NULL, - NULL, - &number_states, - NULL); + REGEX_INTERNAL_automaton_traverse (nfa, + NULL, + NULL, + NULL, + &number_states, + NULL); /* No multistriding added so far */ nfa->is_multistrided = GNUNET_NO; @@ -2965,17 +2969,17 @@ REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len) return nfa; error: - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse regex: `%s'\n", regex); if (NULL != error_msg) - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", error_msg); - GNUNET_free_non_null(p); + GNUNET_free_non_null (p); while (NULL != (nfa = ctx.stack_head)) - { - GNUNET_CONTAINER_DLL_remove(ctx.stack_head, ctx.stack_tail, nfa); - REGEX_INTERNAL_automaton_destroy(nfa); - } + { + GNUNET_CONTAINER_DLL_remove (ctx.stack_head, ctx.stack_tail, nfa); + REGEX_INTERNAL_automaton_destroy (nfa); + } return NULL; } @@ -2991,10 +2995,10 @@ error: * for starting. */ static void -construct_dfa_states(struct REGEX_INTERNAL_Context *ctx, - struct REGEX_INTERNAL_Automaton *nfa, - struct REGEX_INTERNAL_Automaton *dfa, - struct REGEX_INTERNAL_State *dfa_state) +construct_dfa_states (struct REGEX_INTERNAL_Context *ctx, + struct REGEX_INTERNAL_Automaton *nfa, + struct REGEX_INTERNAL_Automaton *dfa, + struct REGEX_INTERNAL_State *dfa_state) { struct REGEX_INTERNAL_Transition *ctran; struct REGEX_INTERNAL_State *new_dfa_state; @@ -3004,37 +3008,37 @@ construct_dfa_states(struct REGEX_INTERNAL_Context *ctx, struct REGEX_INTERNAL_StateSet nfa_set; for (ctran = dfa_state->transitions_head; NULL != ctran; ctran = ctran->next) - { - if (NULL == ctran->label || NULL != ctran->to_state) - continue; + { + if ((NULL == ctran->label) ||(NULL != ctran->to_state) ) + continue; - nfa_closure_set_create(&tmp, nfa, &dfa_state->nfa_set, ctran->label); - nfa_closure_set_create(&nfa_set, nfa, &tmp, NULL); - state_set_clear(&tmp); + nfa_closure_set_create (&tmp, nfa, &dfa_state->nfa_set, ctran->label); + nfa_closure_set_create (&nfa_set, nfa, &tmp, NULL); + state_set_clear (&tmp); - state_contains = NULL; - for (state_iter = dfa->states_head; NULL != state_iter; - state_iter = state_iter->next) - { - if (0 == state_set_compare(&state_iter->nfa_set, &nfa_set)) - { - state_contains = state_iter; - break; - } - } - if (NULL == state_contains) - { - new_dfa_state = dfa_state_create(ctx, &nfa_set); - automaton_add_state(dfa, new_dfa_state); - ctran->to_state = new_dfa_state; - construct_dfa_states(ctx, nfa, dfa, new_dfa_state); - } - else - { - ctran->to_state = state_contains; - state_set_clear(&nfa_set); - } + state_contains = NULL; + for (state_iter = dfa->states_head; NULL != state_iter; + state_iter = state_iter->next) + { + if (0 == state_set_compare (&state_iter->nfa_set, &nfa_set)) + { + state_contains = state_iter; + break; + } } + if (NULL == state_contains) + { + new_dfa_state = dfa_state_create (ctx, &nfa_set); + automaton_add_state (dfa, new_dfa_state); + ctran->to_state = new_dfa_state; + construct_dfa_states (ctx, nfa, dfa, new_dfa_state); + } + else + { + ctran->to_state = state_contains; + state_set_clear (&nfa_set); + } + } } @@ -3056,9 +3060,9 @@ construct_dfa_states(struct REGEX_INTERNAL_Context *ctx, * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy. */ struct REGEX_INTERNAL_Automaton * -REGEX_INTERNAL_construct_dfa(const char *regex, - const size_t len, - unsigned int max_path_len) +REGEX_INTERNAL_construct_dfa (const char *regex, + const size_t len, + unsigned int max_path_len) { struct REGEX_INTERNAL_Context ctx; struct REGEX_INTERNAL_Automaton *dfa; @@ -3066,50 +3070,50 @@ REGEX_INTERNAL_construct_dfa(const char *regex, struct REGEX_INTERNAL_StateSet nfa_start_eps_cls; struct REGEX_INTERNAL_StateSet singleton_set; - REGEX_INTERNAL_context_init(&ctx); + REGEX_INTERNAL_context_init (&ctx); /* Create NFA */ - nfa = REGEX_INTERNAL_construct_nfa(regex, len); + nfa = REGEX_INTERNAL_construct_nfa (regex, len); if (NULL == nfa) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not create DFA, because NFA creation failed\n"); - return NULL; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not create DFA, because NFA creation failed\n"); + return NULL; + } - dfa = GNUNET_new(struct REGEX_INTERNAL_Automaton); + dfa = GNUNET_new (struct REGEX_INTERNAL_Automaton); dfa->type = DFA; - dfa->regex = GNUNET_strdup(regex); + dfa->regex = GNUNET_strdup (regex); /* Create DFA start state from epsilon closure */ - memset(&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet)); - state_set_append(&singleton_set, nfa->start); - nfa_closure_set_create(&nfa_start_eps_cls, nfa, &singleton_set, NULL); - state_set_clear(&singleton_set); - dfa->start = dfa_state_create(&ctx, &nfa_start_eps_cls); - automaton_add_state(dfa, dfa->start); + memset (&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet)); + state_set_append (&singleton_set, nfa->start); + nfa_closure_set_create (&nfa_start_eps_cls, nfa, &singleton_set, NULL); + state_set_clear (&singleton_set); + dfa->start = dfa_state_create (&ctx, &nfa_start_eps_cls); + automaton_add_state (dfa, dfa->start); - construct_dfa_states(&ctx, nfa, dfa, dfa->start); - REGEX_INTERNAL_automaton_destroy(nfa); + construct_dfa_states (&ctx, nfa, dfa, dfa->start); + REGEX_INTERNAL_automaton_destroy (nfa); /* Minimize DFA */ - if (GNUNET_OK != dfa_minimize(&ctx, dfa)) - { - REGEX_INTERNAL_automaton_destroy(dfa); - return NULL; - } + if (GNUNET_OK != dfa_minimize (&ctx, dfa)) + { + REGEX_INTERNAL_automaton_destroy (dfa); + return NULL; + } /* Create proofs and hashes for all states */ - if (GNUNET_OK != automaton_create_proofs(dfa)) - { - REGEX_INTERNAL_automaton_destroy(dfa); - return NULL; - } + if (GNUNET_OK != automaton_create_proofs (dfa)) + { + REGEX_INTERNAL_automaton_destroy (dfa); + return NULL; + } /* Compress linear DFA paths */ if (1 != max_path_len) - dfa_compress_paths(&ctx, dfa, max_path_len); + dfa_compress_paths (&ctx, dfa, max_path_len); return dfa; } @@ -3122,7 +3126,7 @@ REGEX_INTERNAL_construct_dfa(const char *regex, * @param a automaton to be destroyed */ void -REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a) +REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a) { struct REGEX_INTERNAL_State *s; struct REGEX_INTERNAL_State *next_state; @@ -3130,17 +3134,17 @@ REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a) if (NULL == a) return; - GNUNET_free_non_null(a->regex); - GNUNET_free_non_null(a->canonical_regex); + GNUNET_free_non_null (a->regex); + GNUNET_free_non_null (a->canonical_regex); for (s = a->states_head; NULL != s; s = next_state) - { - next_state = s->next; - GNUNET_CONTAINER_DLL_remove(a->states_head, a->states_tail, s); - automaton_destroy_state(s); - } + { + next_state = s->next; + GNUNET_CONTAINER_DLL_remove (a->states_head, a->states_tail, s); + automaton_destroy_state (s); + } - GNUNET_free(a); + GNUNET_free (a); } @@ -3153,34 +3157,34 @@ REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a) * @return 0 if string matches, non-0 otherwise */ static int -evaluate_dfa(struct REGEX_INTERNAL_Automaton *a, const char *string) +evaluate_dfa (struct REGEX_INTERNAL_Automaton *a, const char *string) { const char *strp; struct REGEX_INTERNAL_State *s; unsigned int step_len; if (DFA != a->type) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Tried to evaluate DFA, but NFA automaton given"); - return -1; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Tried to evaluate DFA, but NFA automaton given"); + return -1; + } s = a->start; /* If the string is empty but the starting state is accepting, we accept. */ - if ((NULL == string || 0 == strlen(string)) && s->accepting) + if (((NULL == string)||(0 == strlen (string))) && s->accepting) return 0; for (strp = string; NULL != strp && *strp; strp += step_len) - { - step_len = dfa_move(&s, strp); + { + step_len = dfa_move (&s, strp); - if (NULL == s) - break; - } + if (NULL == s) + break; + } - if (NULL != s && s->accepting) + if ((NULL != s)&& s->accepting) return 0; return 1; @@ -3195,7 +3199,7 @@ evaluate_dfa(struct REGEX_INTERNAL_Automaton *a, const char *string) * @return 0 if string matches, non-0 otherwise */ static int -evaluate_nfa(struct REGEX_INTERNAL_Automaton *a, const char *string) +evaluate_nfa (struct REGEX_INTERNAL_Automaton *a, const char *string) { const char *strp; char str[2]; @@ -3207,43 +3211,43 @@ evaluate_nfa(struct REGEX_INTERNAL_Automaton *a, const char *string) int result; if (NFA != a->type) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Tried to evaluate NFA, but DFA automaton given"); - return -1; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Tried to evaluate NFA, but DFA automaton given"); + return -1; + } /* If the string is empty but the starting state is accepting, we accept. */ - if ((NULL == string || 0 == strlen(string)) && a->start->accepting) + if (((NULL == string)||(0 == strlen (string))) && a->start->accepting) return 0; result = 1; - memset(&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet)); - state_set_append(&singleton_set, a->start); - nfa_closure_set_create(&sset, a, &singleton_set, NULL); - state_set_clear(&singleton_set); + memset (&singleton_set, 0, sizeof(struct REGEX_INTERNAL_StateSet)); + state_set_append (&singleton_set, a->start); + nfa_closure_set_create (&sset, a, &singleton_set, NULL); + state_set_clear (&singleton_set); str[1] = '\0'; for (strp = string; NULL != strp && *strp; strp++) - { - str[0] = *strp; - nfa_closure_set_create(&new_sset, a, &sset, str); - state_set_clear(&sset); - nfa_closure_set_create(&sset, a, &new_sset, 0); - state_set_clear(&new_sset); - } + { + str[0] = *strp; + nfa_closure_set_create (&new_sset, a, &sset, str); + state_set_clear (&sset); + nfa_closure_set_create (&sset, a, &new_sset, 0); + state_set_clear (&new_sset); + } for (i = 0; i < sset.off; i++) + { + s = sset.states[i]; + if ((NULL != s) && (s->accepting)) { - s = sset.states[i]; - if ((NULL != s) && (s->accepting)) - { - result = 0; - break; - } + result = 0; + break; } + } - state_set_clear(&sset); + state_set_clear (&sset); return result; } @@ -3256,26 +3260,26 @@ evaluate_nfa(struct REGEX_INTERNAL_Automaton *a, const char *string) * @return 0 if string matches, non-0 otherwise */ int -REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a, const char *string) +REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a, const char *string) { int result; switch (a->type) - { - case DFA: - result = evaluate_dfa(a, string); - break; - - case NFA: - result = evaluate_nfa(a, string); - break; - - default: - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Evaluating regex failed, automaton has no type!\n"); - result = GNUNET_SYSERR; - break; - } + { + case DFA: + result = evaluate_dfa (a, string); + break; + + case NFA: + result = evaluate_nfa (a, string); + break; + + default: + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Evaluating regex failed, automaton has no type!\n"); + result = GNUNET_SYSERR; + break; + } return result; } @@ -3293,7 +3297,7 @@ REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a, const char *string) * @return */ const char * -REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a) +REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a) { if (NULL == a) return NULL; @@ -3310,7 +3314,7 @@ REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a) * @return number of transitions in the given automaton. */ unsigned int -REGEX_INTERNAL_get_transition_count(struct REGEX_INTERNAL_Automaton *a) +REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a) { unsigned int t_count; struct REGEX_INTERNAL_State *s; @@ -3337,20 +3341,20 @@ REGEX_INTERNAL_get_transition_count(struct REGEX_INTERNAL_Automaton *a) * to construct the key */ size_t -REGEX_INTERNAL_get_first_key(const char *input_string, - size_t string_len, - struct GNUNET_HashCode *key) +REGEX_INTERNAL_get_first_key (const char *input_string, + size_t string_len, + struct GNUNET_HashCode *key) { size_t size; size = string_len < GNUNET_REGEX_INITIAL_BYTES ? string_len : GNUNET_REGEX_INITIAL_BYTES; if (NULL == input_string) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n"); - return 0; - } - GNUNET_CRYPTO_hash(input_string, size, key); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Given input string was NULL!\n"); + return 0; + } + GNUNET_CRYPTO_hash (input_string, size, key); return size; } @@ -3367,12 +3371,12 @@ REGEX_INTERNAL_get_first_key(const char *input_string, * @param iterator_cls closure for the @a iterator function. */ static void -iterate_initial_edge(unsigned int min_len, - unsigned int max_len, - char *consumed_string, - struct REGEX_INTERNAL_State *state, - REGEX_INTERNAL_KeyIterator iterator, - void *iterator_cls) +iterate_initial_edge (unsigned int min_len, + unsigned int max_len, + char *consumed_string, + struct REGEX_INTERNAL_State *state, + REGEX_INTERNAL_KeyIterator iterator, + void *iterator_cls) { char *temp; struct REGEX_INTERNAL_Transition *t; @@ -3384,91 +3388,91 @@ iterate_initial_edge(unsigned int min_len, unsigned int cur_len; if (NULL != consumed_string) - cur_len = strlen(consumed_string); + cur_len = strlen (consumed_string); else cur_len = 0; if (((cur_len >= min_len) || (GNUNET_YES == state->accepting)) && (cur_len > 0) && (NULL != consumed_string)) + { + if (cur_len <= max_len) { - if (cur_len <= max_len) - { - if ((NULL != state->proof) && - (0 != strcmp(consumed_string, state->proof))) - { - (void)state_get_edges(state, edges); - GNUNET_CRYPTO_hash(consumed_string, strlen(consumed_string), &hash); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Start state for string `%s' is %s\n", - consumed_string, - GNUNET_h2s(&hash)); - iterator(iterator_cls, - &hash, - consumed_string, - state->accepting, - num_edges, - edges); - } + if ((NULL != state->proof) && + (0 != strcmp (consumed_string, state->proof))) + { + (void) state_get_edges (state, edges); + GNUNET_CRYPTO_hash (consumed_string, strlen (consumed_string), &hash); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Start state for string `%s' is %s\n", + consumed_string, + GNUNET_h2s (&hash)); + iterator (iterator_cls, + &hash, + consumed_string, + state->accepting, + num_edges, + edges); + } - if ((GNUNET_YES == state->accepting) && (cur_len > 1) && - (state->transition_count < 1) && (cur_len < max_len)) - { - /* Special case for regex consisting of just a string that is shorter than - * max_len */ - edge[0].label = &consumed_string[cur_len - 1]; - edge[0].destination = state->hash; - temp = GNUNET_strdup(consumed_string); - temp[cur_len - 1] = '\0'; - GNUNET_CRYPTO_hash(temp, cur_len - 1, &hash_new); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Start state for short string `%s' is %s\n", - temp, - GNUNET_h2s(&hash_new)); - iterator(iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge); - GNUNET_free(temp); - } - } - else /* cur_len > max_len */ - { - /* Case where the concatenated labels are longer than max_len, then split. */ - edge[0].label = &consumed_string[max_len]; - edge[0].destination = state->hash; - temp = GNUNET_strdup(consumed_string); - temp[max_len] = '\0'; - GNUNET_CRYPTO_hash(temp, max_len, &hash); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Start state at split edge `%s'-`%s` is %s\n", - temp, - edge[0].label, - GNUNET_h2s(&hash_new)); - iterator(iterator_cls, &hash, temp, GNUNET_NO, 1, edge); - GNUNET_free(temp); - } + if ((GNUNET_YES == state->accepting) && (cur_len > 1) && + (state->transition_count < 1) && (cur_len < max_len)) + { + /* Special case for regex consisting of just a string that is shorter than + * max_len */ + edge[0].label = &consumed_string[cur_len - 1]; + edge[0].destination = state->hash; + temp = GNUNET_strdup (consumed_string); + temp[cur_len - 1] = '\0'; + GNUNET_CRYPTO_hash (temp, cur_len - 1, &hash_new); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Start state for short string `%s' is %s\n", + temp, + GNUNET_h2s (&hash_new)); + iterator (iterator_cls, &hash_new, temp, GNUNET_NO, 1, edge); + GNUNET_free (temp); + } + } + else /* cur_len > max_len */ + { + /* Case where the concatenated labels are longer than max_len, then split. */ + edge[0].label = &consumed_string[max_len]; + edge[0].destination = state->hash; + temp = GNUNET_strdup (consumed_string); + temp[max_len] = '\0'; + GNUNET_CRYPTO_hash (temp, max_len, &hash); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Start state at split edge `%s'-`%s` is %s\n", + temp, + edge[0].label, + GNUNET_h2s (&hash_new)); + iterator (iterator_cls, &hash, temp, GNUNET_NO, 1, edge); + GNUNET_free (temp); } + } if (cur_len < max_len) + { + for (t = state->transitions_head; NULL != t; t = t->next) { - for (t = state->transitions_head; NULL != t; t = t->next) - { - if (NULL != strchr(t->label, (int)'.')) - { - /* Wildcards not allowed during starting states */ - GNUNET_break(0); - continue; - } - if (NULL != consumed_string) - GNUNET_asprintf(&temp, "%s%s", consumed_string, t->label); - else - GNUNET_asprintf(&temp, "%s", t->label); - iterate_initial_edge(min_len, - max_len, - temp, - t->to_state, - iterator, - iterator_cls); - GNUNET_free(temp); - } + if (NULL != strchr (t->label, (int) '.')) + { + /* Wildcards not allowed during starting states */ + GNUNET_break (0); + continue; + } + if (NULL != consumed_string) + GNUNET_asprintf (&temp, "%s%s", consumed_string, t->label); + else + GNUNET_asprintf (&temp, "%s", t->label); + iterate_initial_edge (min_len, + max_len, + temp, + t->to_state, + iterator, + iterator_cls); + GNUNET_free (temp); } + } } @@ -3481,41 +3485,41 @@ iterate_initial_edge(unsigned int min_len, * @param iterator_cls closure. */ void -REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a, - REGEX_INTERNAL_KeyIterator iterator, - void *iterator_cls) +REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a, + REGEX_INTERNAL_KeyIterator iterator, + void *iterator_cls) { struct REGEX_INTERNAL_State *s; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating over starting edges\n"); - iterate_initial_edge(GNUNET_REGEX_INITIAL_BYTES, - GNUNET_REGEX_INITIAL_BYTES, - NULL, - a->start, - iterator, - iterator_cls); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating over DFA edges\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating over starting edges\n"); + iterate_initial_edge (GNUNET_REGEX_INITIAL_BYTES, + GNUNET_REGEX_INITIAL_BYTES, + NULL, + a->start, + iterator, + iterator_cls); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating over DFA edges\n"); for (s = a->states_head; NULL != s; s = s->next) - { - struct REGEX_BLOCK_Edge edges[s->transition_count]; - unsigned int num_edges; - - num_edges = state_get_edges(s, edges); - if (((NULL != s->proof) && (0 < strlen(s->proof))) || s->accepting) - { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Creating DFA edges at `%s' under key %s\n", - s->proof, - GNUNET_h2s(&s->hash)); - iterator(iterator_cls, - &s->hash, - s->proof, - s->accepting, - num_edges, - edges); - } - s->marked = GNUNET_NO; + { + struct REGEX_BLOCK_Edge edges[s->transition_count]; + unsigned int num_edges; + + num_edges = state_get_edges (s, edges); + if (((NULL != s->proof) && (0 < strlen (s->proof))) || s->accepting) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Creating DFA edges at `%s' under key %s\n", + s->proof, + GNUNET_h2s (&s->hash)); + iterator (iterator_cls, + &s->hash, + s->proof, + s->accepting, + num_edges, + edges); } + s->marked = GNUNET_NO; + } } @@ -3525,7 +3529,8 @@ REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a, * Contains the same info as the Regex Iterator parametes except the key, * which comes directly from the HashMap iterator. */ -struct temporal_state_store { +struct temporal_state_store +{ int reachable; char *proof; int accepting; @@ -3537,7 +3542,8 @@ struct temporal_state_store { /** * Store regex iterator and cls in one place to pass to the hashmap iterator. */ -struct client_iterator { +struct client_iterator +{ REGEX_INTERNAL_KeyIterator iterator; void *iterator_cls; }; @@ -3555,31 +3561,31 @@ struct client_iterator { * @param edges edges leaving current state. */ static void -store_all_states(void *cls, - const struct GNUNET_HashCode *key, - const char *proof, - int accepting, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges) +store_all_states (void *cls, + const struct GNUNET_HashCode *key, + const char *proof, + int accepting, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges) { struct GNUNET_CONTAINER_MultiHashMap *hm = cls; struct temporal_state_store *tmp; size_t edges_size; - tmp = GNUNET_new(struct temporal_state_store); + tmp = GNUNET_new (struct temporal_state_store); tmp->reachable = GNUNET_NO; - tmp->proof = GNUNET_strdup(proof); + tmp->proof = GNUNET_strdup (proof); tmp->accepting = accepting; tmp->num_edges = num_edges; edges_size = sizeof(struct REGEX_BLOCK_Edge) * num_edges; - tmp->edges = GNUNET_malloc(edges_size); - GNUNET_memcpy(tmp->edges, edges, edges_size); - GNUNET_assert(GNUNET_YES == - GNUNET_CONTAINER_multihashmap_put( - hm, - key, - tmp, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); + tmp->edges = GNUNET_malloc (edges_size); + GNUNET_memcpy (tmp->edges, edges, edges_size); + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multihashmap_put ( + hm, + key, + tmp, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); } @@ -3592,8 +3598,8 @@ store_all_states(void *cls, * @param hm HashMap which stores all the states indexed by key. */ static void -mark_as_reachable(struct temporal_state_store *state, - struct GNUNET_CONTAINER_MultiHashMap *hm) +mark_as_reachable (struct temporal_state_store *state, + struct GNUNET_CONTAINER_MultiHashMap *hm) { struct temporal_state_store *child; unsigned int i; @@ -3604,16 +3610,16 @@ mark_as_reachable(struct temporal_state_store *state, state->reachable = GNUNET_YES; for (i = 0; i < state->num_edges; i++) + { + child = + GNUNET_CONTAINER_multihashmap_get (hm, &state->edges[i].destination); + if (NULL == child) { - child = - GNUNET_CONTAINER_multihashmap_get(hm, &state->edges[i].destination); - if (NULL == child) - { - GNUNET_break(0); - continue; - } - mark_as_reachable(child, hm); + GNUNET_break (0); + continue; } + mark_as_reachable (child, hm); + } } @@ -3627,9 +3633,9 @@ mark_as_reachable(struct temporal_state_store *state, * #GNUNET_NO if not. */ static int -reachability_iterator(void *cls, - const struct GNUNET_HashCode *key, - void *value) +reachability_iterator (void *cls, + const struct GNUNET_HashCode *key, + void *value) { struct GNUNET_CONTAINER_MultiHashMap *hm = cls; struct temporal_state_store *state = value; @@ -3638,12 +3644,12 @@ reachability_iterator(void *cls, /* already visited and marked */ return GNUNET_YES; - if (GNUNET_REGEX_INITIAL_BYTES > strlen(state->proof) && - GNUNET_NO == state->accepting) + if ((GNUNET_REGEX_INITIAL_BYTES > strlen (state->proof))&& + (GNUNET_NO == state->accepting) ) /* not directly reachable */ return GNUNET_YES; - mark_as_reachable(state, hm); + mark_as_reachable (state, hm); return GNUNET_YES; } @@ -3659,23 +3665,23 @@ reachability_iterator(void *cls, * #GNUNET_NO if not. */ static int -iterate_reachables(void *cls, const struct GNUNET_HashCode *key, void *value) +iterate_reachables (void *cls, const struct GNUNET_HashCode *key, void *value) { struct client_iterator *ci = cls; struct temporal_state_store *state = value; if (GNUNET_YES == state->reachable) - { - ci->iterator(ci->iterator_cls, - key, - state->proof, - state->accepting, - state->num_edges, - state->edges); - } - GNUNET_free(state->edges); - GNUNET_free(state->proof); - GNUNET_free(state); + { + ci->iterator (ci->iterator_cls, + key, + state->proof, + state->accepting, + state->num_edges, + state->edges); + } + GNUNET_free (state->edges); + GNUNET_free (state->proof); + GNUNET_free (state); return GNUNET_YES; } @@ -3690,22 +3696,22 @@ iterate_reachables(void *cls, const struct GNUNET_HashCode *key, void *value) * @param iterator_cls closure. */ void -REGEX_INTERNAL_iterate_reachable_edges(struct REGEX_INTERNAL_Automaton *a, - REGEX_INTERNAL_KeyIterator iterator, - void *iterator_cls) +REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a, + REGEX_INTERNAL_KeyIterator iterator, + void *iterator_cls) { struct GNUNET_CONTAINER_MultiHashMap *hm; struct client_iterator ci; - hm = GNUNET_CONTAINER_multihashmap_create(a->state_count * 2, GNUNET_NO); + hm = GNUNET_CONTAINER_multihashmap_create (a->state_count * 2, GNUNET_NO); ci.iterator = iterator; ci.iterator_cls = iterator_cls; - REGEX_INTERNAL_iterate_all_edges(a, &store_all_states, hm); - GNUNET_CONTAINER_multihashmap_iterate(hm, &reachability_iterator, hm); - GNUNET_CONTAINER_multihashmap_iterate(hm, &iterate_reachables, &ci); + REGEX_INTERNAL_iterate_all_edges (a, &store_all_states, hm); + GNUNET_CONTAINER_multihashmap_iterate (hm, &reachability_iterator, hm); + GNUNET_CONTAINER_multihashmap_iterate (hm, &iterate_reachables, &ci); - GNUNET_CONTAINER_multihashmap_destroy(hm); + GNUNET_CONTAINER_multihashmap_destroy (hm); } diff --git a/src/regex/regex_internal.h b/src/regex/regex_internal.h index ab8b6f22d..1e0722136 100644 --- a/src/regex/regex_internal.h +++ b/src/regex/regex_internal.h @@ -39,7 +39,8 @@ extern "C" * char array of literals that are allowed inside a regex (apart from the * operators) */ -#define ALLOWED_LITERALS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +#define ALLOWED_LITERALS \ + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" /** @@ -47,7 +48,8 @@ extern "C" * which they origin ('from_state'). Each state can have 0-n transitions. * If label is NULL, this is considered to be an epsilon transition. */ -struct REGEX_INTERNAL_Transition { +struct REGEX_INTERNAL_Transition +{ /** * This is a linked list. */ @@ -89,7 +91,8 @@ struct REGEX_INTERNAL_State; /** * Set of states. */ -struct REGEX_INTERNAL_StateSet { +struct REGEX_INTERNAL_StateSet +{ /** * Array of states. */ @@ -110,7 +113,8 @@ struct REGEX_INTERNAL_StateSet { /** * A state. Can be used in DFA and NFA automatons. */ -struct REGEX_INTERNAL_State { +struct REGEX_INTERNAL_State +{ /** * This is a linked list to keep states in an automaton. */ @@ -242,7 +246,8 @@ struct REGEX_INTERNAL_State { /** * Type of an automaton. */ -enum REGEX_INTERNAL_AutomatonType { +enum REGEX_INTERNAL_AutomatonType +{ NFA, DFA }; @@ -251,7 +256,8 @@ enum REGEX_INTERNAL_AutomatonType { /** * Automaton representation. */ -struct REGEX_INTERNAL_Automaton { +struct REGEX_INTERNAL_Automaton +{ /** * Linked list of NFAs used for partial NFA creation. */ @@ -319,7 +325,7 @@ struct REGEX_INTERNAL_Automaton { * @return NFA, needs to be freed using REGEX_INTERNAL_automaton_destroy. */ struct REGEX_INTERNAL_Automaton * -REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len); +REGEX_INTERNAL_construct_nfa (const char *regex, const size_t len); /** @@ -335,8 +341,9 @@ REGEX_INTERNAL_construct_nfa(const char *regex, const size_t len); * @return GNUNET_YES to proceed traversal, GNUNET_NO to stop. */ typedef int (*REGEX_INTERNAL_traverse_check) (void *cls, - struct REGEX_INTERNAL_State * s, - struct REGEX_INTERNAL_Transition * t); + struct REGEX_INTERNAL_State *s, + struct REGEX_INTERNAL_Transition * + t); /** @@ -348,7 +355,7 @@ typedef int (*REGEX_INTERNAL_traverse_check) (void *cls, */ typedef void (*REGEX_INTERNAL_traverse_action) (void *cls, const unsigned int count, - struct REGEX_INTERNAL_State * s); + struct REGEX_INTERNAL_State *s); /** @@ -365,12 +372,12 @@ typedef void (*REGEX_INTERNAL_traverse_action) (void *cls, * @param action_cls closure for action */ void -REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a, - struct REGEX_INTERNAL_State *start, - REGEX_INTERNAL_traverse_check check, - void *check_cls, - REGEX_INTERNAL_traverse_action action, - void *action_cls); +REGEX_INTERNAL_automaton_traverse (const struct REGEX_INTERNAL_Automaton *a, + struct REGEX_INTERNAL_State *start, + REGEX_INTERNAL_traverse_check check, + void *check_cls, + REGEX_INTERNAL_traverse_action action, + void *action_cls); /** * Get the canonical regex of the given automaton. @@ -384,7 +391,7 @@ REGEX_INTERNAL_automaton_traverse(const struct REGEX_INTERNAL_Automaton *a, * @return canonical regex string. */ const char * -REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a); +REGEX_INTERNAL_get_canonical_regex (struct REGEX_INTERNAL_Automaton *a); /** @@ -395,14 +402,15 @@ REGEX_INTERNAL_get_canonical_regex(struct REGEX_INTERNAL_Automaton *a); * @return number of transitions in the given automaton. */ unsigned int -REGEX_INTERNAL_get_transition_count(struct REGEX_INTERNAL_Automaton *a); +REGEX_INTERNAL_get_transition_count (struct REGEX_INTERNAL_Automaton *a); /** * Context that contains an id counter for states and transitions as well as a * DLL of automatons used as a stack for NFA construction. */ -struct REGEX_INTERNAL_Context { +struct REGEX_INTERNAL_Context +{ /** * Unique state id. */ @@ -433,9 +441,9 @@ struct REGEX_INTERNAL_Context { * @param stride_len length of the strides. */ void -REGEX_INTERNAL_dfa_add_multi_strides(struct REGEX_INTERNAL_Context *regex_ctx, - struct REGEX_INTERNAL_Automaton *dfa, - const unsigned int stride_len); +REGEX_INTERNAL_dfa_add_multi_strides (struct REGEX_INTERNAL_Context *regex_ctx, + struct REGEX_INTERNAL_Automaton *dfa, + const unsigned int stride_len); diff --git a/src/regex/regex_internal_dht.c b/src/regex/regex_internal_dht.c index 611fb0a23..3cf48f3c2 100644 --- a/src/regex/regex_internal_dht.c +++ b/src/regex/regex_internal_dht.c @@ -32,7 +32,7 @@ #include "gnunet_signatures.h" -#define LOG(kind, ...) GNUNET_log_from(kind, "regex-dht", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from (kind, "regex-dht", __VA_ARGS__) /** * DHT replication level to use. @@ -53,7 +53,8 @@ /** * Handle to store cached data about a regex announce. */ -struct REGEX_INTERNAL_Announcement { +struct REGEX_INTERNAL_Announcement +{ /** * DHT handle to use, must be initialized externally. */ @@ -92,92 +93,93 @@ struct REGEX_INTERNAL_Announcement { * @param edges edges leaving current state. */ static void -regex_iterator(void *cls, - const struct GNUNET_HashCode *key, - const char *proof, - int accepting, - unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges) +regex_iterator (void *cls, + const struct GNUNET_HashCode *key, + const char *proof, + int accepting, + unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges) { struct REGEX_INTERNAL_Announcement *h = cls; struct RegexBlock *block; size_t size; unsigned int i; - LOG(GNUNET_ERROR_TYPE_INFO, - "DHT PUT for state %s with proof `%s' and %u edges:\n", - GNUNET_h2s(key), - proof, - num_edges); + LOG (GNUNET_ERROR_TYPE_INFO, + "DHT PUT for state %s with proof `%s' and %u edges:\n", + GNUNET_h2s (key), + proof, + num_edges); for (i = 0; i < num_edges; i++) - { - LOG(GNUNET_ERROR_TYPE_INFO, - "Edge %u `%s' towards %s\n", - i, - edges[i].label, - GNUNET_h2s(&edges[i].destination)); - } + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Edge %u `%s' towards %s\n", + i, + edges[i].label, + GNUNET_h2s (&edges[i].destination)); + } if (GNUNET_YES == accepting) - { - struct RegexAcceptBlock ab; - - LOG(GNUNET_ERROR_TYPE_INFO, - "State %s is accepting, putting own id\n", - GNUNET_h2s(key)); - size = sizeof(struct RegexAcceptBlock); - ab.purpose.size = ntohl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + - sizeof(struct GNUNET_TIME_AbsoluteNBO) + - sizeof(struct GNUNET_HashCode)); - ab.purpose.purpose = ntohl(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT); - ab.expiration_time = GNUNET_TIME_absolute_hton(GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_DHT_MAX_EXPIRATION)); - ab.key = *key; - GNUNET_CRYPTO_eddsa_key_get_public(h->priv, - &ab.peer.public_key); - GNUNET_assert(GNUNET_OK == - GNUNET_CRYPTO_eddsa_sign(h->priv, + { + struct RegexAcceptBlock ab; + + LOG (GNUNET_ERROR_TYPE_INFO, + "State %s is accepting, putting own id\n", + GNUNET_h2s (key)); + size = sizeof(struct RegexAcceptBlock); + ab.purpose.size = ntohl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + + sizeof(struct GNUNET_TIME_AbsoluteNBO) + + sizeof(struct GNUNET_HashCode)); + ab.purpose.purpose = ntohl (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT); + ab.expiration_time = GNUNET_TIME_absolute_hton ( + GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_DHT_MAX_EXPIRATION)); + ab.key = *key; + GNUNET_CRYPTO_eddsa_key_get_public (h->priv, + &ab.peer.public_key); + GNUNET_assert (GNUNET_OK == + GNUNET_CRYPTO_eddsa_sign (h->priv, &ab.purpose, &ab.signature)); - GNUNET_STATISTICS_update(h->stats, "# regex accepting blocks stored", - 1, GNUNET_NO); - GNUNET_STATISTICS_update(h->stats, "# regex accepting block bytes stored", - sizeof(struct RegexAcceptBlock), GNUNET_NO); - (void) - GNUNET_DHT_put(h->dht, key, - DHT_REPLICATION, - DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, - GNUNET_BLOCK_TYPE_REGEX_ACCEPT, - size, - &ab, - GNUNET_TIME_relative_to_absolute(DHT_TTL), - NULL, NULL); - } - block = REGEX_BLOCK_create(proof, - num_edges, - edges, - accepting, - &size); + GNUNET_STATISTICS_update (h->stats, "# regex accepting blocks stored", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (h->stats, "# regex accepting block bytes stored", + sizeof(struct RegexAcceptBlock), GNUNET_NO); + (void) + GNUNET_DHT_put (h->dht, key, + DHT_REPLICATION, + DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, + GNUNET_BLOCK_TYPE_REGEX_ACCEPT, + size, + &ab, + GNUNET_TIME_relative_to_absolute (DHT_TTL), + NULL, NULL); + } + block = REGEX_BLOCK_create (proof, + num_edges, + edges, + accepting, + &size); if (NULL == block) return; - (void)GNUNET_DHT_put(h->dht, - key, - DHT_REPLICATION, - DHT_OPT, - GNUNET_BLOCK_TYPE_REGEX, - size, - block, - GNUNET_TIME_relative_to_absolute(DHT_TTL), - NULL, - NULL); - GNUNET_STATISTICS_update(h->stats, - "# regex blocks stored", - 1, - GNUNET_NO); - GNUNET_STATISTICS_update(h->stats, - "# regex block bytes stored", - size, - GNUNET_NO); - GNUNET_free(block); + (void) GNUNET_DHT_put (h->dht, + key, + DHT_REPLICATION, + DHT_OPT, + GNUNET_BLOCK_TYPE_REGEX, + size, + block, + GNUNET_TIME_relative_to_absolute (DHT_TTL), + NULL, + NULL); + GNUNET_STATISTICS_update (h->stats, + "# regex blocks stored", + 1, + GNUNET_NO); + GNUNET_STATISTICS_update (h->stats, + "# regex block bytes stored", + size, + GNUNET_NO); + GNUNET_free (block); } @@ -194,22 +196,22 @@ regex_iterator(void *cls, * Must be freed by calling #REGEX_INTERNAL_announce_cancel(). */ struct REGEX_INTERNAL_Announcement * -REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, - const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, - const char *regex, - uint16_t compression, - struct GNUNET_STATISTICS_Handle *stats) +REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht, + const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, + const char *regex, + uint16_t compression, + struct GNUNET_STATISTICS_Handle *stats) { struct REGEX_INTERNAL_Announcement *h; - GNUNET_assert(NULL != dht); - h = GNUNET_new(struct REGEX_INTERNAL_Announcement); + GNUNET_assert (NULL != dht); + h = GNUNET_new (struct REGEX_INTERNAL_Announcement); h->regex = regex; h->dht = dht; h->stats = stats; h->priv = priv; - h->dfa = REGEX_INTERNAL_construct_dfa(regex, strlen(regex), compression); - REGEX_INTERNAL_reannounce(h); + h->dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), compression); + REGEX_INTERNAL_reannounce (h); return h; } @@ -221,15 +223,15 @@ REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, * @param h Handle returned by a previous #REGEX_INTERNAL_announce call(). */ void -REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h) +REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h) { - GNUNET_assert(NULL != h->dfa); /* make sure to call announce first */ - LOG(GNUNET_ERROR_TYPE_INFO, - "REGEX_INTERNAL_reannounce: %s\n", - h->regex); - REGEX_INTERNAL_iterate_reachable_edges(h->dfa, - ®ex_iterator, - h); + GNUNET_assert (NULL != h->dfa); /* make sure to call announce first */ + LOG (GNUNET_ERROR_TYPE_INFO, + "REGEX_INTERNAL_reannounce: %s\n", + h->regex); + REGEX_INTERNAL_iterate_reachable_edges (h->dfa, + ®ex_iterator, + h); } @@ -240,10 +242,10 @@ REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h) * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. */ void -REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h) +REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h) { - REGEX_INTERNAL_automaton_destroy(h->dfa); - GNUNET_free(h); + REGEX_INTERNAL_automaton_destroy (h->dfa); + GNUNET_free (h); } @@ -254,7 +256,8 @@ REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h) * Struct to keep state of running searches that have consumed a part of * the inital string. */ -struct RegexSearchContext { +struct RegexSearchContext +{ /** * Part of the description already consumed by * this particular search branch. @@ -282,7 +285,8 @@ struct RegexSearchContext { /** * Type of values in `dht_get_results`. */ -struct Result { +struct Result +{ /** * Number of bytes in data. */ @@ -299,7 +303,8 @@ struct Result { * Struct to keep information of searches of services described by a regex * using a user-provided string service description. */ -struct REGEX_INTERNAL_Search { +struct REGEX_INTERNAL_Search +{ /** * DHT handle to use, must be initialized externally. */ @@ -356,9 +361,9 @@ struct REGEX_INTERNAL_Search { * @param ctx Context of the search. */ static void -regex_next_edge(const struct RegexBlock *block, - size_t size, - struct RegexSearchContext *ctx); +regex_next_edge (const struct RegexBlock *block, + size_t size, + struct RegexSearchContext *ctx); /** @@ -377,33 +382,33 @@ regex_next_edge(const struct RegexBlock *block, * @param data Pointer to the result data. */ static void -dht_get_string_accept_handler(void *cls, struct GNUNET_TIME_Absolute exp, - const struct GNUNET_HashCode *key, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length, - enum GNUNET_BLOCK_Type type, - size_t size, const void *data) +dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp, + const struct GNUNET_HashCode *key, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length, + enum GNUNET_BLOCK_Type type, + size_t size, const void *data) { const struct RegexAcceptBlock *block = data; struct RegexSearchContext *ctx = cls; struct REGEX_INTERNAL_Search *info = ctx->info; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Regex result accept for %s (key %s)\n", - info->description, GNUNET_h2s(key)); - - GNUNET_STATISTICS_update(info->stats, - "# regex accepting blocks found", - 1, GNUNET_NO); - GNUNET_STATISTICS_update(info->stats, - "# regex accepting block bytes found", - size, GNUNET_NO); - info->callback(info->callback_cls, - &block->peer, - get_path, get_path_length, - put_path, put_path_length); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Regex result accept for %s (key %s)\n", + info->description, GNUNET_h2s (key)); + + GNUNET_STATISTICS_update (info->stats, + "# regex accepting blocks found", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (info->stats, + "# regex accepting block bytes found", + size, GNUNET_NO); + info->callback (info->callback_cls, + &block->peer, + get_path, get_path_length, + put_path, put_path_length); } @@ -415,28 +420,28 @@ dht_get_string_accept_handler(void *cls, struct GNUNET_TIME_Absolute exp, * @param ctx Context containing info about the string, tunnel, etc. */ static void -regex_find_path(const struct GNUNET_HashCode *key, - struct RegexSearchContext *ctx) +regex_find_path (const struct GNUNET_HashCode *key, + struct RegexSearchContext *ctx) { struct GNUNET_DHT_GetHandle *get_h; - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Accept state found, now searching for paths to %s\n", - GNUNET_h2s(key), - (unsigned int)ctx->position); - get_h = GNUNET_DHT_get_start(ctx->info->dht, /* handle */ - GNUNET_BLOCK_TYPE_REGEX_ACCEPT, /* type */ - key, /* key to search */ - DHT_REPLICATION, /* replication level */ - DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, - NULL, /* xquery */ // FIXME BLOOMFILTER - 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE - &dht_get_string_accept_handler, ctx); - GNUNET_break(GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles, - key, - get_h, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Accept state found, now searching for paths to %s\n", + GNUNET_h2s (key), + (unsigned int) ctx->position); + get_h = GNUNET_DHT_get_start (ctx->info->dht, /* handle */ + GNUNET_BLOCK_TYPE_REGEX_ACCEPT, /* type */ + key, /* key to search */ + DHT_REPLICATION, /* replication level */ + DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, + NULL, /* xquery */ // FIXME BLOOMFILTER + 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE + &dht_get_string_accept_handler, ctx); + GNUNET_break (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_put (ctx->info->dht_get_handles, + key, + get_h, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); } @@ -458,14 +463,14 @@ regex_find_path(const struct GNUNET_HashCode *key, * TODO: re-issue the request after certain time? cancel after X results? */ static void -dht_get_string_handler(void *cls, struct GNUNET_TIME_Absolute exp, - const struct GNUNET_HashCode *key, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length, - enum GNUNET_BLOCK_Type type, - size_t size, const void *data) +dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp, + const struct GNUNET_HashCode *key, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length, + enum GNUNET_BLOCK_Type type, + size_t size, const void *data) { const struct RegexBlock *block = data; struct RegexSearchContext *ctx = cls; @@ -473,32 +478,32 @@ dht_get_string_handler(void *cls, struct GNUNET_TIME_Absolute exp, size_t len; struct Result *copy; - LOG(GNUNET_ERROR_TYPE_INFO, - "DHT GET result for %s (%s)\n", - GNUNET_h2s(key), ctx->info->description); - copy = GNUNET_malloc(sizeof(struct Result) + size); + LOG (GNUNET_ERROR_TYPE_INFO, + "DHT GET result for %s (%s)\n", + GNUNET_h2s (key), ctx->info->description); + copy = GNUNET_malloc (sizeof(struct Result) + size); copy->size = size; copy->data = ©[1]; - GNUNET_memcpy(©[1], block, size); - GNUNET_break(GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, - key, copy, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); - len = strlen(info->description); + GNUNET_memcpy (©[1], block, size); + GNUNET_break (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_put (info->dht_get_results, + key, copy, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + len = strlen (info->description); if (len == ctx->position) // String processed + { + if (GNUNET_YES == GNUNET_BLOCK_is_accepting (block, size)) { - if (GNUNET_YES == GNUNET_BLOCK_is_accepting(block, size)) - { - regex_find_path(key, ctx); - } - else - { - LOG(GNUNET_ERROR_TYPE_INFO, "block not accepting!\n"); - /* FIXME REGEX this block not successful, wait for more? start timeout? */ - } - return; + regex_find_path (key, ctx); } - regex_next_edge(block, size, ctx); + else + { + LOG (GNUNET_ERROR_TYPE_INFO, "block not accepting!\n"); + /* FIXME REGEX this block not successful, wait for more? start timeout? */ + } + return; + } + regex_next_edge (block, size, ctx); } @@ -511,32 +516,32 @@ dht_get_string_handler(void *cls, struct GNUNET_TIME_Absolute exp, * @return #GNUNET_YES: we should always continue to iterate. */ static int -regex_result_iterator(void *cls, - const struct GNUNET_HashCode * key, - void *value) +regex_result_iterator (void *cls, + const struct GNUNET_HashCode *key, + void *value) { struct Result *result = value; const struct RegexBlock *block = result->data; struct RegexSearchContext *ctx = cls; if ((GNUNET_YES == - GNUNET_BLOCK_is_accepting(block, result->size)) && - (ctx->position == strlen(ctx->info->description))) - { - LOG(GNUNET_ERROR_TYPE_INFO, - "Found accepting known block\n"); - regex_find_path(key, ctx); - return GNUNET_YES; // We found an accept state! - } - LOG(GNUNET_ERROR_TYPE_DEBUG, - "* %u, %u, [%u]\n", - ctx->position, - strlen(ctx->info->description), - GNUNET_BLOCK_is_accepting(block, result->size)); - regex_next_edge(block, result->size, ctx); - - GNUNET_STATISTICS_update(ctx->info->stats, "# regex cadet blocks iterated", - 1, GNUNET_NO); + GNUNET_BLOCK_is_accepting (block, result->size)) && + (ctx->position == strlen (ctx->info->description))) + { + LOG (GNUNET_ERROR_TYPE_INFO, + "Found accepting known block\n"); + regex_find_path (key, ctx); + return GNUNET_YES; // We found an accept state! + } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "* %u, %u, [%u]\n", + ctx->position, + strlen (ctx->info->description), + GNUNET_BLOCK_is_accepting (block, result->size)); + regex_next_edge (block, result->size, ctx); + + GNUNET_STATISTICS_update (ctx->info->stats, "# regex cadet blocks iterated", + 1, GNUNET_NO); return GNUNET_YES; } @@ -552,43 +557,43 @@ regex_result_iterator(void *cls, * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise. */ static int -regex_edge_iterator(void *cls, - const char *token, - size_t len, - const struct GNUNET_HashCode *key) +regex_edge_iterator (void *cls, + const char *token, + size_t len, + const struct GNUNET_HashCode *key) { struct RegexSearchContext *ctx = cls; struct REGEX_INTERNAL_Search *info = ctx->info; const char *current; size_t current_len; - GNUNET_STATISTICS_update(info->stats, "# regex edges iterated", - 1, GNUNET_NO); + GNUNET_STATISTICS_update (info->stats, "# regex edges iterated", + 1, GNUNET_NO); current = &info->description[ctx->position]; - current_len = strlen(info->description) - ctx->position; + current_len = strlen (info->description) - ctx->position; if (len > current_len) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Token too long, END\n"); - return GNUNET_YES; - } - if (0 != strncmp(current, token, len)) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Token doesn't match, END\n"); - return GNUNET_YES; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Token too long, END\n"); + return GNUNET_YES; + } + if (0 != strncmp (current, token, len)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Token doesn't match, END\n"); + return GNUNET_YES; + } if (len > ctx->longest_match) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Token is longer, KEEP\n"); - ctx->longest_match = len; - ctx->hash = *key; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Token is longer, KEEP\n"); + ctx->longest_match = len; + ctx->hash = *key; + } else - { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Token is not longer, IGNORE\n"); - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, "Token is not longer, IGNORE\n"); + } - LOG(GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n"); return GNUNET_YES; } @@ -601,9 +606,9 @@ regex_edge_iterator(void *cls, * @param ctx Context of the search. */ static void -regex_next_edge(const struct RegexBlock *block, - size_t size, - struct RegexSearchContext *ctx) +regex_next_edge (const struct RegexBlock *block, + size_t size, + struct RegexSearchContext *ctx) { struct RegexSearchContext *new_ctx; struct REGEX_INTERNAL_Search *info = ctx->info; @@ -612,69 +617,69 @@ regex_next_edge(const struct RegexBlock *block, const char *rest; int result; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Next edge\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Next edge\n"); /* Find the longest match for the current string position, * among tokens in the given block */ ctx->longest_match = 0; - result = REGEX_BLOCK_iterate(block, size, - ®ex_edge_iterator, ctx); - GNUNET_break(GNUNET_OK == result); + result = REGEX_BLOCK_iterate (block, size, + ®ex_edge_iterator, ctx); + GNUNET_break (GNUNET_OK == result); /* Did anything match? */ if (0 == ctx->longest_match) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "no match in block\n"); - return; - } + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "no match in block\n"); + return; + } hash = &ctx->hash; - new_ctx = GNUNET_new(struct RegexSearchContext); + new_ctx = GNUNET_new (struct RegexSearchContext); new_ctx->info = info; new_ctx->position = ctx->position + ctx->longest_match; - GNUNET_array_append(info->contexts, info->n_contexts, new_ctx); + GNUNET_array_append (info->contexts, info->n_contexts, new_ctx); /* Check whether we already have a DHT GET running for it */ if (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, hash)) - { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "GET for %s running, END\n", - GNUNET_h2s(hash)); - GNUNET_CONTAINER_multihashmap_get_multiple(info->dht_get_results, - hash, - ®ex_result_iterator, - new_ctx); - return; /* We are already looking for it */ - } - - GNUNET_STATISTICS_update(info->stats, "# regex nodes traversed", - 1, GNUNET_NO); - - LOG(GNUNET_ERROR_TYPE_DEBUG, - "Following edges at %s for offset %u in `%s'\n", - GNUNET_h2s(hash), - (unsigned int)ctx->position, - info->description); + GNUNET_CONTAINER_multihashmap_contains (info->dht_get_handles, hash)) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GET for %s running, END\n", + GNUNET_h2s (hash)); + GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, + hash, + ®ex_result_iterator, + new_ctx); + return; /* We are already looking for it */ + } + + GNUNET_STATISTICS_update (info->stats, "# regex nodes traversed", + 1, GNUNET_NO); + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Following edges at %s for offset %u in `%s'\n", + GNUNET_h2s (hash), + (unsigned int) ctx->position, + info->description); rest = &new_ctx->info->description[new_ctx->position]; get_h = - GNUNET_DHT_get_start(info->dht, /* handle */ - GNUNET_BLOCK_TYPE_REGEX, /* type */ - hash, /* key to search */ - DHT_REPLICATION, /* replication level */ - DHT_OPT, - rest, /* xquery */ - strlen(rest) + 1, /* xquery bits */ - &dht_get_string_handler, new_ctx); + GNUNET_DHT_get_start (info->dht, /* handle */ + GNUNET_BLOCK_TYPE_REGEX, /* type */ + hash, /* key to search */ + DHT_REPLICATION, /* replication level */ + DHT_OPT, + rest, /* xquery */ + strlen (rest) + 1, /* xquery bits */ + &dht_get_string_handler, new_ctx); if (GNUNET_OK != - GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, - hash, - get_h, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) - { - GNUNET_break(0); - return; - } + GNUNET_CONTAINER_multihashmap_put (info->dht_get_handles, + hash, + get_h, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) + { + GNUNET_break (0); + return; + } } @@ -692,11 +697,11 @@ regex_next_edge(const struct RegexBlock *block, * Must be freed by calling #REGEX_INTERNAL_search_cancel(). */ struct REGEX_INTERNAL_Search * -REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht, - const char *string, - REGEX_INTERNAL_Found callback, - void *callback_cls, - struct GNUNET_STATISTICS_Handle *stats) +REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht, + const char *string, + REGEX_INTERNAL_Found callback, + void *callback_cls, + struct GNUNET_STATISTICS_Handle *stats) { struct REGEX_INTERNAL_Search *h; struct GNUNET_DHT_GetHandle *get_h; @@ -706,49 +711,49 @@ REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht, size_t len; /* Initialize handle */ - GNUNET_assert(NULL != dht); - GNUNET_assert(NULL != callback); - h = GNUNET_new(struct REGEX_INTERNAL_Search); + GNUNET_assert (NULL != dht); + GNUNET_assert (NULL != callback); + h = GNUNET_new (struct REGEX_INTERNAL_Search); h->dht = dht; - h->description = GNUNET_strdup(string); + h->description = GNUNET_strdup (string); h->callback = callback; h->callback_cls = callback_cls; h->stats = stats; - h->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO); - h->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO); + h->dht_get_handles = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); + h->dht_get_results = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); /* Initialize context */ - len = strlen(string); - size = REGEX_INTERNAL_get_first_key(string, len, &key); - LOG(GNUNET_ERROR_TYPE_INFO, - "Initial key for `%s' is %s (based on `%.*s')\n", - string, - GNUNET_h2s(&key), - size, - string); - ctx = GNUNET_new(struct RegexSearchContext); + len = strlen (string); + size = REGEX_INTERNAL_get_first_key (string, len, &key); + LOG (GNUNET_ERROR_TYPE_INFO, + "Initial key for `%s' is %s (based on `%.*s')\n", + string, + GNUNET_h2s (&key), + size, + string); + ctx = GNUNET_new (struct RegexSearchContext); ctx->position = size; ctx->info = h; - GNUNET_array_append(h->contexts, - h->n_contexts, - ctx); + GNUNET_array_append (h->contexts, + h->n_contexts, + ctx); /* Start search in DHT */ - get_h = GNUNET_DHT_get_start(h->dht, /* handle */ - GNUNET_BLOCK_TYPE_REGEX, /* type */ - &key, /* key to search */ - DHT_REPLICATION, /* replication level */ - DHT_OPT, - &h->description[size], /* xquery */ - // FIXME add BLOOMFILTER to exclude filtered peers - len + 1 - size, /* xquery bits */ - // FIXME add BLOOMFILTER SIZE - &dht_get_string_handler, ctx); - GNUNET_break( + get_h = GNUNET_DHT_get_start (h->dht, /* handle */ + GNUNET_BLOCK_TYPE_REGEX, /* type */ + &key, /* key to search */ + DHT_REPLICATION, /* replication level */ + DHT_OPT, + &h->description[size], /* xquery */ + // FIXME add BLOOMFILTER to exclude filtered peers + len + 1 - size, /* xquery bits */ + // FIXME add BLOOMFILTER SIZE + &dht_get_string_handler, ctx); + GNUNET_break ( GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put(h->dht_get_handles, - &key, - get_h, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST) + GNUNET_CONTAINER_multihashmap_put (h->dht_get_handles, + &key, + get_h, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST) ); return h; @@ -766,13 +771,13 @@ REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht, * #GNUNET_NO if not. */ static int -regex_cancel_dht_get(void *cls, - const struct GNUNET_HashCode * key, - void *value) +regex_cancel_dht_get (void *cls, + const struct GNUNET_HashCode *key, + void *value) { struct GNUNET_DHT_GetHandle *h = value; - GNUNET_DHT_get_stop(h); + GNUNET_DHT_get_stop (h); return GNUNET_YES; } @@ -788,11 +793,11 @@ regex_cancel_dht_get(void *cls, * #GNUNET_NO if not. */ static int -regex_free_result(void *cls, - const struct GNUNET_HashCode * key, - void *value) +regex_free_result (void *cls, + const struct GNUNET_HashCode *key, + void *value) { - GNUNET_free(value); + GNUNET_free (value); return GNUNET_YES; } @@ -803,24 +808,24 @@ regex_free_result(void *cls, * @param h the search context. */ void -REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h) +REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h) { unsigned int i; - GNUNET_free(h->description); - GNUNET_CONTAINER_multihashmap_iterate(h->dht_get_handles, - ®ex_cancel_dht_get, NULL); - GNUNET_CONTAINER_multihashmap_iterate(h->dht_get_results, - ®ex_free_result, NULL); - GNUNET_CONTAINER_multihashmap_destroy(h->dht_get_results); - GNUNET_CONTAINER_multihashmap_destroy(h->dht_get_handles); + GNUNET_free (h->description); + GNUNET_CONTAINER_multihashmap_iterate (h->dht_get_handles, + ®ex_cancel_dht_get, NULL); + GNUNET_CONTAINER_multihashmap_iterate (h->dht_get_results, + ®ex_free_result, NULL); + GNUNET_CONTAINER_multihashmap_destroy (h->dht_get_results); + GNUNET_CONTAINER_multihashmap_destroy (h->dht_get_handles); if (0 < h->n_contexts) - { - for (i = 0; i < h->n_contexts; i++) - GNUNET_free(h->contexts[i]); - GNUNET_free(h->contexts); - } - GNUNET_free(h); + { + for (i = 0; i < h->n_contexts; i++) + GNUNET_free (h->contexts[i]); + GNUNET_free (h->contexts); + } + GNUNET_free (h); } diff --git a/src/regex/regex_internal_lib.h b/src/regex/regex_internal_lib.h index 9cb925a17..531f998cc 100644 --- a/src/regex/regex_internal_lib.h +++ b/src/regex/regex_internal_lib.h @@ -64,9 +64,9 @@ struct REGEX_INTERNAL_Automaton; * @return DFA, needs to be freed using #REGEX_INTERNAL_automaton_destroy(). */ struct REGEX_INTERNAL_Automaton * -REGEX_INTERNAL_construct_dfa(const char *regex, - const size_t len, - unsigned int max_path_len); +REGEX_INTERNAL_construct_dfa (const char *regex, + const size_t len, + unsigned int max_path_len); /** @@ -76,7 +76,7 @@ REGEX_INTERNAL_construct_dfa(const char *regex, * @param a automaton to be destroyed. */ void -REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a); +REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a); /** @@ -88,8 +88,8 @@ REGEX_INTERNAL_automaton_destroy(struct REGEX_INTERNAL_Automaton *a); * @return 0 if string matches, non 0 otherwise. */ int -REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a, - const char *string); +REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a, + const char *string); /** @@ -103,9 +103,9 @@ REGEX_INTERNAL_eval(struct REGEX_INTERNAL_Automaton *a, * to construct the key */ size_t -REGEX_INTERNAL_get_first_key(const char *input_string, - size_t string_len, - struct GNUNET_HashCode * key); +REGEX_INTERNAL_get_first_key (const char *input_string, + size_t string_len, + struct GNUNET_HashCode *key); /** @@ -136,9 +136,9 @@ typedef void * @param iterator_cls closure. */ void -REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a, - REGEX_INTERNAL_KeyIterator iterator, - void *iterator_cls); +REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a, + REGEX_INTERNAL_KeyIterator iterator, + void *iterator_cls); /** @@ -152,9 +152,9 @@ REGEX_INTERNAL_iterate_all_edges(struct REGEX_INTERNAL_Automaton *a, * @param iterator_cls closure. */ void -REGEX_INTERNAL_iterate_reachable_edges(struct REGEX_INTERNAL_Automaton *a, - REGEX_INTERNAL_KeyIterator iterator, - void *iterator_cls); +REGEX_INTERNAL_iterate_reachable_edges (struct REGEX_INTERNAL_Automaton *a, + REGEX_INTERNAL_KeyIterator iterator, + void *iterator_cls); @@ -182,11 +182,11 @@ struct REGEX_INTERNAL_Search; * Must be freed by calling #REGEX_INTERNAL_announce_cancel(). */ struct REGEX_INTERNAL_Announcement * -REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, - const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, - const char *regex, - uint16_t compression, - struct GNUNET_STATISTICS_Handle *stats); +REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht, + const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, + const char *regex, + uint16_t compression, + struct GNUNET_STATISTICS_Handle *stats); /** @@ -196,7 +196,7 @@ REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. */ void -REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h); +REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h); /** @@ -206,7 +206,7 @@ REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h); * @param h Handle returned by a previous #REGEX_INTERNAL_announce() call. */ void -REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h); +REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h); /** @@ -242,11 +242,11 @@ typedef void * Must be freed by calling #REGEX_INTERNAL_search_cancel(). */ struct REGEX_INTERNAL_Search * -REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht, - const char *string, - REGEX_INTERNAL_Found callback, - void *callback_cls, - struct GNUNET_STATISTICS_Handle *stats); +REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht, + const char *string, + REGEX_INTERNAL_Found callback, + void *callback_cls, + struct GNUNET_STATISTICS_Handle *stats); /** * 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, * @param h Handle returned by a previous #REGEX_INTERNAL_search() call. */ void -REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h); +REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h); #if 0 /* keep Emacsens' auto-indent happy */ diff --git a/src/regex/regex_ipc.h b/src/regex/regex_ipc.h index 5106ead24..b5a474d56 100644 --- a/src/regex/regex_ipc.h +++ b/src/regex/regex_ipc.h @@ -31,7 +31,8 @@ /** * Request for regex service to announce capability. */ -struct AnnounceMessage { +struct AnnounceMessage +{ /** * Type is GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE */ @@ -59,7 +60,8 @@ struct AnnounceMessage { /** * Message to initiate regex search. */ -struct RegexSearchMessage { +struct RegexSearchMessage +{ /** * Type is GNUNET_MESSAGE_TYPE_REGEX_SEARCH */ @@ -72,7 +74,8 @@ struct RegexSearchMessage { /** * Result from regex search. */ -struct ResultMessage { +struct ResultMessage +{ /** * Type is GNUNET_MESSAGE_TYPE_REGEX_RESULT */ diff --git a/src/regex/regex_test_graph.c b/src/regex/regex_test_graph.c index e809d578d..c988b5aae 100644 --- a/src/regex/regex_test_graph.c +++ b/src/regex/regex_test_graph.c @@ -31,7 +31,8 @@ * Context for graph creation. Passed as the cls to * REGEX_TEST_automaton_save_graph_step. */ -struct REGEX_TEST_Graph_Context { +struct REGEX_TEST_Graph_Context +{ /** * File pointer to the dot file used for output. */ @@ -62,10 +63,10 @@ struct REGEX_TEST_Graph_Context { * @param stack_size current size of the stack */ static void -scc_tarjan_strongconnect(unsigned int *scc_counter, - struct REGEX_INTERNAL_State *v, unsigned int *index, - struct REGEX_INTERNAL_State **stack, - unsigned int *stack_size) +scc_tarjan_strongconnect (unsigned int *scc_counter, + struct REGEX_INTERNAL_State *v, unsigned int *index, + struct REGEX_INTERNAL_State **stack, + unsigned int *stack_size) { struct REGEX_INTERNAL_State *w; struct REGEX_INTERNAL_Transition *t; @@ -77,32 +78,32 @@ scc_tarjan_strongconnect(unsigned int *scc_counter, v->contained = 1; for (t = v->transitions_head; NULL != t; t = t->next) + { + w = t->to_state; + + if (NULL == w) + continue; + + if (w->index < 0) { - w = t->to_state; - - if (NULL == w) - continue; - - if (w->index < 0) - { - scc_tarjan_strongconnect(scc_counter, w, index, stack, stack_size); - v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink; - } - else if (1 == w->contained) - v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink; + scc_tarjan_strongconnect (scc_counter, w, index, stack, stack_size); + v->lowlink = (v->lowlink > w->lowlink) ? w->lowlink : v->lowlink; } + else if (1 == w->contained) + v->lowlink = (v->lowlink > w->index) ? w->index : v->lowlink; + } if (v->lowlink == v->index) + { + (*scc_counter)++; + do { - (*scc_counter)++; - do - { - w = stack[--(*stack_size)]; - w->contained = 0; - w->scc_id = *scc_counter; - } - while (w != v); + w = stack[--(*stack_size)]; + w->contained = 0; + w->scc_id = *scc_counter; } + while (w != v); + } } @@ -113,7 +114,7 @@ scc_tarjan_strongconnect(unsigned int *scc_counter, * @param a the automaton for which SCCs should be computed and assigned. */ static void -scc_tarjan(struct REGEX_INTERNAL_Automaton *a) +scc_tarjan (struct REGEX_INTERNAL_Automaton *a) { unsigned int index; unsigned int scc_counter; @@ -122,21 +123,21 @@ scc_tarjan(struct REGEX_INTERNAL_Automaton *a) unsigned int stack_size; for (v = a->states_head; NULL != v; v = v->next) - { - v->contained = 0; - v->index = -1; - v->lowlink = -1; - } + { + v->contained = 0; + v->index = -1; + v->lowlink = -1; + } stack_size = 0; index = 0; scc_counter = 0; for (v = a->states_head; NULL != v; v = v->next) - { - if (v->index < 0) - scc_tarjan_strongconnect(&scc_counter, v, &index, stack, &stack_size); - } + { + if (v->index < 0) + scc_tarjan_strongconnect (&scc_counter, v, &index, stack, &stack_size); + } } @@ -150,8 +151,8 @@ scc_tarjan(struct REGEX_INTERNAL_Automaton *a) * @param s state. */ void -REGEX_TEST_automaton_save_graph_step(void *cls, unsigned int count, - struct REGEX_INTERNAL_State *s) +REGEX_TEST_automaton_save_graph_step (void *cls, unsigned int count, + struct REGEX_INTERNAL_State *s) { struct REGEX_TEST_Graph_Context *ctx = cls; struct REGEX_INTERNAL_Transition *ctran; @@ -161,100 +162,100 @@ REGEX_TEST_automaton_save_graph_step(void *cls, unsigned int count, char *to_name; if (GNUNET_YES == ctx->verbose) - GNUNET_asprintf(&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof, - GNUNET_h2s(&s->hash)); + GNUNET_asprintf (&name, "%i (%s) (%s) (%s)", s->dfs_id, s->name, s->proof, + GNUNET_h2s (&s->hash)); else - GNUNET_asprintf(&name, "%i", s->dfs_id); + GNUNET_asprintf (&name, "%i", s->dfs_id); if (s->accepting) + { + if (GNUNET_YES == ctx->coloring) { - if (GNUNET_YES == ctx->coloring) - { - GNUNET_asprintf(&s_acc, - "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n", - name, s->scc_id * s->scc_id); - } - else - { - GNUNET_asprintf(&s_acc, "\"%s\" [shape=doublecircle];\n", name, - s->scc_id); - } + GNUNET_asprintf (&s_acc, + "\"%s\" [shape=doublecircle, color=\"0.%i 0.8 0.95\"];\n", + name, s->scc_id * s->scc_id); } - else if (GNUNET_YES == ctx->coloring) + else { - GNUNET_asprintf(&s_acc, - "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name, - s->scc_id * s->scc_id); + GNUNET_asprintf (&s_acc, "\"%s\" [shape=doublecircle];\n", name, + s->scc_id); } + } + else if (GNUNET_YES == ctx->coloring) + { + GNUNET_asprintf (&s_acc, + "\"%s\" [shape=circle, color=\"0.%i 0.8 0.95\"];\n", name, + s->scc_id * s->scc_id); + } else - { - GNUNET_asprintf(&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id); - } + { + GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id); + } - GNUNET_assert(NULL != s_acc); + GNUNET_assert (NULL != s_acc); - fwrite(s_acc, strlen(s_acc), 1, ctx->filep); - GNUNET_free(s_acc); + fwrite (s_acc, strlen (s_acc), 1, ctx->filep); + GNUNET_free (s_acc); s_acc = NULL; for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next) + { + if (NULL == ctran->to_state) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Transition from State %i has no state for transitioning\n", + s->id); + continue; + } + + if (GNUNET_YES == ctx->verbose) + { + GNUNET_asprintf (&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id, + ctran->to_state->name, ctran->to_state->proof, + GNUNET_h2s (&ctran->to_state->hash)); + } + else + GNUNET_asprintf (&to_name, "%i", ctran->to_state->dfs_id); + + if (NULL == ctran->label) { - if (NULL == ctran->to_state) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Transition from State %i has no state for transitioning\n", - s->id); - continue; - } - - if (GNUNET_YES == ctx->verbose) - { - GNUNET_asprintf(&to_name, "%i (%s) (%s) (%s)", ctran->to_state->dfs_id, - ctran->to_state->name, ctran->to_state->proof, - GNUNET_h2s(&ctran->to_state->hash)); - } + if (GNUNET_YES == ctx->coloring) + { + GNUNET_asprintf (&s_tran, + "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n", + name, to_name, s->scc_id * s->scc_id); + } else - GNUNET_asprintf(&to_name, "%i", ctran->to_state->dfs_id); - - if (NULL == ctran->label) - { - if (GNUNET_YES == ctx->coloring) - { - GNUNET_asprintf(&s_tran, - "\"%s\" -> \"%s\" [label = \"ε\", color=\"0.%i 0.8 0.95\"];\n", - name, to_name, s->scc_id * s->scc_id); - } - else - { - GNUNET_asprintf(&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name, - to_name, s->scc_id); - } - } + { + GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"ε\"];\n", name, + to_name, s->scc_id); + } + } + else + { + if (GNUNET_YES == ctx->coloring) + { + GNUNET_asprintf (&s_tran, + "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n", + name, to_name, ctran->label, s->scc_id * s->scc_id); + } else - { - if (GNUNET_YES == ctx->coloring) - { - GNUNET_asprintf(&s_tran, - "\"%s\" -> \"%s\" [label = \"%s\", color=\"0.%i 0.8 0.95\"];\n", - name, to_name, ctran->label, s->scc_id * s->scc_id); - } - else - { - GNUNET_asprintf(&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name, - to_name, ctran->label, s->scc_id); - } - } - - GNUNET_free(to_name); - - GNUNET_assert(NULL != s_tran); - - fwrite(s_tran, strlen(s_tran), 1, ctx->filep); - GNUNET_free(s_tran); - s_tran = NULL; + { + GNUNET_asprintf (&s_tran, "\"%s\" -> \"%s\" [label = \"%s\"];\n", name, + to_name, ctran->label, s->scc_id); + } } - GNUNET_free(name); + GNUNET_free (to_name); + + GNUNET_assert (NULL != s_tran); + + fwrite (s_tran, strlen (s_tran), 1, ctx->filep); + GNUNET_free (s_tran); + s_tran = NULL; + } + + GNUNET_free (name); } @@ -267,51 +268,51 @@ REGEX_TEST_automaton_save_graph_step(void *cls, unsigned int count, * mode */ void -REGEX_TEST_automaton_save_graph(struct REGEX_INTERNAL_Automaton *a, - const char *filename, - enum REGEX_TEST_GraphSavingOptions options) +REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a, + const char *filename, + enum REGEX_TEST_GraphSavingOptions options) { char *start; char *end; struct REGEX_TEST_Graph_Context ctx; if (NULL == a) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!"); - return; - } - - if (NULL == filename || strlen(filename) < 1) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No Filename given!"); - return; - } - - ctx.filep = fopen(filename, "w"); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print NFA, was NULL!"); + return; + } + + if ((NULL == filename)||(strlen (filename) < 1)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No Filename given!"); + return; + } + + ctx.filep = fopen (filename, "w"); ctx.verbose = (0 == (options & REGEX_TEST_GRAPH_VERBOSE)) ? GNUNET_NO : GNUNET_YES; ctx.coloring = (0 == (options & REGEX_TEST_GRAPH_COLORING)) ? GNUNET_NO : GNUNET_YES; if (NULL == ctx.filep) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s", - filename); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s", + filename); + return; + } /* First add the SCCs to the automaton, so we can color them nicely */ if (GNUNET_YES == ctx.coloring) - scc_tarjan(a); + scc_tarjan (a); start = "digraph G {\nrankdir=LR\n"; - fwrite(start, strlen(start), 1, ctx.filep); + fwrite (start, strlen (start), 1, ctx.filep); - REGEX_INTERNAL_automaton_traverse(a, a->start, NULL, NULL, - ®EX_TEST_automaton_save_graph_step, - &ctx); + REGEX_INTERNAL_automaton_traverse (a, a->start, NULL, NULL, + ®EX_TEST_automaton_save_graph_step, + &ctx); end = "\n}\n"; - fwrite(end, strlen(end), 1, ctx.filep); - fclose(ctx.filep); + fwrite (end, strlen (end), 1, ctx.filep); + fclose (ctx.filep); } diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c index d8eb22370..15d6c653a 100644 --- a/src/regex/regex_test_lib.c +++ b/src/regex/regex_test_lib.c @@ -32,7 +32,8 @@ /** * Struct to hold the tree formed by prefix-combining the regexes. */ -struct RegexCombineCtx { +struct RegexCombineCtx +{ /** * Child nodes with same prefix and token. */ @@ -61,34 +62,34 @@ struct RegexCombineCtx { * @return Int in range [0, (base-1)] */ static int -c2i(char c, int size) +c2i (char c, int size) { switch (size) - { - case 2: - case 8: + { + case 2: + case 8: + return c - '0'; + break; + + case 16: + if ((c >= '0') &&(c <= '9') ) return c - '0'; - break; - - case 16: - if (c >= '0' && c <= '9') - return c - '0'; - else if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - else if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - else - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Cannot convert char %c in base %u\n", - c, size); - GNUNET_assert(0); - } - break; - - default: - GNUNET_assert(0); + else if ((c >= 'A') &&(c <= 'F') ) + return c - 'A' + 10; + else if ((c >= 'a') &&(c <= 'f') ) + return c - 'a' + 10; + else + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Cannot convert char %c in base %u\n", + c, size); + GNUNET_assert (0); } + break; + + default: + GNUNET_assert (0); + } } @@ -98,10 +99,10 @@ c2i(char c, int size) * @param n Indentation level */ static void -space(int n) +space (int n) { for (int i = 0; i < n; i++) - fprintf(stderr, "| "); + fprintf (stderr, "| "); } @@ -112,25 +113,25 @@ space(int n) * @param level Indentation level to start with */ static void -debugctx(struct RegexCombineCtx *ctx, int level) +debugctx (struct RegexCombineCtx *ctx, int level) { #if DEBUG_REGEX if (NULL != ctx->s) - { - space(level - 1); - fprintf(stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s); - } + { + space (level - 1); + fprintf (stderr, "%u:'%s'\n", c2i (ctx->s[0], ctx->size), ctx->s); + } else - fprintf(stderr, "ROOT (base %u)\n", ctx->size); + fprintf (stderr, "ROOT (base %u)\n", ctx->size); for (unsigned int i = 0; i < ctx->size; i++) + { + if (NULL != ctx->children[i]) { - if (NULL != ctx->children[i]) - { - space(level); - debugctx(ctx->children[i], level + 1); - } + space (level); + debugctx (ctx->children[i], level + 1); } - fflush(stderr); + } + fflush (stderr); #endif } @@ -142,8 +143,8 @@ debugctx(struct RegexCombineCtx *ctx, int level) * @param regex Regex to add. */ static void -regex_add(struct RegexCombineCtx *ctx, - const char *regex); +regex_add (struct RegexCombineCtx *ctx, + const char *regex); /** @@ -152,14 +153,14 @@ regex_add(struct RegexCombineCtx *ctx, * @param alphabet_size Size of the alphabet (and the Trie array) */ static struct RegexCombineCtx * -new_regex_ctx(unsigned int alphabet_size) +new_regex_ctx (unsigned int alphabet_size) { struct RegexCombineCtx *ctx; size_t array_size; array_size = sizeof(struct RegexCombineCtx *) * alphabet_size; - ctx = GNUNET_new(struct RegexCombineCtx); - ctx->children = GNUNET_malloc(array_size); + ctx = GNUNET_new (struct RegexCombineCtx); + ctx->children = GNUNET_malloc (array_size); ctx->size = alphabet_size; return ctx; @@ -167,19 +168,19 @@ new_regex_ctx(unsigned int alphabet_size) static void -move_children(struct RegexCombineCtx *dst, - const struct RegexCombineCtx *src) +move_children (struct RegexCombineCtx *dst, + const struct RegexCombineCtx *src) { size_t array_size; array_size = sizeof(struct RegexCombineCtx *) * src->size; - GNUNET_memcpy(dst->children, - src->children, - array_size); + GNUNET_memcpy (dst->children, + src->children, + array_size); for (unsigned int i = 0; i < src->size; i++) - { - src->children[i] = NULL; - } + { + src->children[i] = NULL; + } } @@ -191,7 +192,7 @@ move_children(struct RegexCombineCtx *dst, * @return Regex that matches any of the added regexes. */ static char * -regex_combine(struct RegexCombineCtx *ctx) +regex_combine (struct RegexCombineCtx *ctx) { struct RegexCombineCtx *p; unsigned int i; @@ -201,56 +202,57 @@ regex_combine(struct RegexCombineCtx *ctx) char *s; int opt; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); - regex = GNUNET_strdup(""); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new combine %s\n", ctx->s); + regex = GNUNET_strdup (""); opt = GNUNET_NO; for (i = 0; i < ctx->size; i++) + { + p = ctx->children[i]; + if (NULL == p) + continue; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "adding '%s' to innner %s\n", + p->s, ctx->s); + s = regex_combine (p); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); + if (strlen (s) == 0) { - p = ctx->children[i]; - if (NULL == p) - continue; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "adding '%s' to innner %s\n", - p->s, ctx->s); - s = regex_combine(p); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " total '%s'\n", s); - if (strlen(s) == 0) - { - opt = GNUNET_YES; - } - else - { - GNUNET_asprintf(&tmp, "%s%s|", regex, s); - GNUNET_free_non_null(regex); - regex = tmp; - } - GNUNET_free_non_null(s); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, ctx->s); + opt = GNUNET_YES; } - - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); - len = strlen(regex); - if (0 == len) + else { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); - GNUNET_free(regex); - return NULL == ctx->s ? NULL : GNUNET_strdup(ctx->s); + GNUNET_asprintf (&tmp, "%s%s|", regex, s); + GNUNET_free_non_null (regex); + regex = tmp; } + GNUNET_free_non_null (s); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " so far '%s' for inner %s\n", regex, + ctx->s); + } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex); + len = strlen (regex); + if (0 == len) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "empty, returning ''\n"); + GNUNET_free (regex); + return NULL == ctx->s ? NULL : GNUNET_strdup (ctx->s); + } if ('|' == regex[len - 1]) regex[len - 1] = '\0'; if (NULL != ctx->s) - { - if (opt) - GNUNET_asprintf(&s, "%s(%s)?", ctx->s, regex); - else - GNUNET_asprintf(&s, "%s(%s)", ctx->s, regex); - GNUNET_free(regex); - regex = s; - } - - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); + { + if (opt) + GNUNET_asprintf (&s, "%s(%s)?", ctx->s, regex); + else + GNUNET_asprintf (&s, "%s(%s)", ctx->s, regex); + GNUNET_free (regex); + regex = s; + } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); return regex; } @@ -264,22 +266,22 @@ regex_combine(struct RegexCombineCtx *ctx) * @return Number of characters of matching prefix. */ static unsigned int -get_prefix_length(const char *s1, const char *s2) +get_prefix_length (const char *s1, const char *s2) { unsigned int l1; unsigned int l2; unsigned int limit; unsigned int i; - l1 = strlen(s1); - l2 = strlen(s2); + l1 = strlen (s1); + l2 = strlen (s2); limit = l1 > l2 ? l2 : l1; for (i = 0; i < limit; i++) - { - if (s1[i] != s2[i]) - return i; - } + { + if (s1[i] != s2[i]) + return i; + } return limit; } @@ -294,7 +296,7 @@ get_prefix_length(const char *s1, const char *s2) * @return Child with the longest prefix, NULL if no child matches. */ static struct RegexCombineCtx * -get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex) +get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex) { struct RegexCombineCtx *p; struct RegexCombineCtx *best; @@ -306,26 +308,26 @@ get_longest_prefix(struct RegexCombineCtx *ctx, const char *regex) best = NULL; for (i = 0; i < ctx->size; i++) + { + p = ctx->children[i]; + if (NULL == p) + continue; + + l = get_prefix_length (p->s, regex); + if (l > best_l) { - p = ctx->children[i]; - if (NULL == p) - continue; - - l = get_prefix_length(p->s, regex); - if (l > best_l) - { - GNUNET_break(0 == best_l); - best = p; - best_l = l; - } + GNUNET_break (0 == best_l); + best = p; + best_l = l; } + } return best; } static void -regex_add_multiple(struct RegexCombineCtx *ctx, - const char *regex, - struct RegexCombineCtx **children) +regex_add_multiple (struct RegexCombineCtx *ctx, + const char *regex, + struct RegexCombineCtx **children) { char tmp[2]; long unsigned int i; @@ -334,43 +336,43 @@ regex_add_multiple(struct RegexCombineCtx *ctx, unsigned int count; if ('(' != regex[0]) - { - GNUNET_assert(0); - } + { + GNUNET_assert (0); + } /* Does the regex cover *all* possible children? Then don't add any, * as it will be covered by the post-regex "(a-z)*" */ - l = strlen(regex); + l = strlen (regex); count = 0; for (i = 1UL; i < l; i++) + { + if ((regex[i] != '|') &&(regex[i] != ')') ) { - if (regex[i] != '|' && regex[i] != ')') - { - count++; - } + count++; } + } if (count == ctx->size) - { - return; - } + { + return; + } /* Add every component as a child node */ tmp[1] = '\0'; for (i = 1UL; i < l; i++) + { + if ((regex[i] != '|') &&(regex[i] != ')') ) { - if (regex[i] != '|' && regex[i] != ')') - { - tmp[0] = regex[i]; - newctx = new_regex_ctx(ctx->size); - newctx->s = GNUNET_strdup(tmp); - if (children != NULL) - GNUNET_memcpy(newctx->children, - children, - sizeof(*children) * ctx->size); - ctx->children[c2i(tmp[0], ctx->size)] = newctx; - } + tmp[0] = regex[i]; + newctx = new_regex_ctx (ctx->size); + newctx->s = GNUNET_strdup (tmp); + if (children != NULL) + GNUNET_memcpy (newctx->children, + children, + sizeof(*children) * ctx->size); + ctx->children[c2i (tmp[0], ctx->size)] = newctx; } + } } /** @@ -384,19 +386,19 @@ regex_add_multiple(struct RegexCombineCtx *ctx, * @param prefix_l Lenght of common prefix of the new regex and @a ctx->s */ static void -regex_split(struct RegexCombineCtx *ctx, - unsigned int len, - unsigned int prefix_l) +regex_split (struct RegexCombineCtx *ctx, + unsigned int len, + unsigned int prefix_l) { struct RegexCombineCtx *newctx; unsigned int idx; char *suffix; - suffix = GNUNET_malloc(len - prefix_l + 1); + suffix = GNUNET_malloc (len - prefix_l + 1); /* * We can use GNUNET_strlcpy because ctx->s is null-terminated */ - GNUNET_strlcpy(suffix, &ctx->s[prefix_l], len - prefix_l + 1); + GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1); /* Suffix saved, truncate current node so it only contains the prefix, * copy any children nodes to put as grandchildren and initialize new empty @@ -406,22 +408,22 @@ regex_split(struct RegexCombineCtx *ctx, /* If the suffix is an OR expression, add multiple children */ if ('(' == suffix[0]) - { - struct RegexCombineCtx **tmp; - - tmp = ctx->children; - ctx->children = GNUNET_malloc(sizeof(*tmp) * ctx->size); - regex_add_multiple(ctx, suffix, tmp); - GNUNET_free(suffix); - GNUNET_free(tmp); - return; - } + { + struct RegexCombineCtx **tmp; + + tmp = ctx->children; + ctx->children = GNUNET_malloc (sizeof(*tmp) * ctx->size); + regex_add_multiple (ctx, suffix, tmp); + GNUNET_free (suffix); + GNUNET_free (tmp); + return; + } /* The suffix is a normal string, add as one node */ - newctx = new_regex_ctx(ctx->size); + newctx = new_regex_ctx (ctx->size); newctx->s = suffix; - move_children(newctx, ctx); - idx = c2i(suffix[0], ctx->size); + move_children (newctx, ctx); + idx = c2i (suffix[0], ctx->size); ctx->children[idx] = newctx; } @@ -433,7 +435,7 @@ regex_split(struct RegexCombineCtx *ctx, * @param regex Regex to add. */ static void -regex_add(struct RegexCombineCtx *ctx, const char *regex) +regex_add (struct RegexCombineCtx *ctx, const char *regex) { struct RegexCombineCtx *p; struct RegexCombineCtx *newctx; @@ -444,54 +446,54 @@ regex_add(struct RegexCombineCtx *ctx, const char *regex) size_t len; int idx; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "regex_add '%s' into '%s'\n", - regex, ctx->s); - l = strlen(regex); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "regex_add '%s' into '%s'\n", + regex, ctx->s); + l = strlen (regex); if (0UL == l) return; /* If the regex is in the form of (a|b|c), add every character separately */ if ('(' == regex[0]) - { - regex_add_multiple(ctx, regex, NULL); - return; - } + { + regex_add_multiple (ctx, regex, NULL); + return; + } - p = get_longest_prefix(ctx, regex); + p = get_longest_prefix (ctx, regex); if (NULL != p) + { + /* There is some prefix match, reduce regex and try again */ + prefix_l = get_prefix_length (p->s, regex); + rest_s = &p->s[prefix_l]; + rest_r = ®ex[prefix_l]; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); + len = strlen (p->s); + if (prefix_l < len) { - /* There is some prefix match, reduce regex and try again */ - prefix_l = get_prefix_length(p->s, regex); - rest_s = &p->s[prefix_l]; - rest_r = ®ex[prefix_l]; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "chosen '%s' [%u]\n", p->s, prefix_l); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "prefix r '%.*s'\n", prefix_l, p->s); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest r '%s'\n", rest_r); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "rest s '%s'\n", rest_s); - len = strlen(p->s); - if (prefix_l < len) - { - regex_split(p, len, prefix_l); - } - regex_add(p, rest_r); - return; + regex_split (p, len, prefix_l); } + regex_add (p, rest_r); + return; + } /* There is no prefix match, add new */ - idx = c2i(regex[0], ctx->size); - if (NULL == ctx->children[idx] && NULL != ctx->s) - { - /* this was the end before, add empty string */ - newctx = new_regex_ctx(ctx->size); - newctx->s = GNUNET_strdup(""); - ctx->children[idx] = newctx; - } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " no match\n"); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); - newctx = new_regex_ctx(ctx->size); - newctx->s = GNUNET_strdup(regex); + idx = c2i (regex[0], ctx->size); + if ((NULL == ctx->children[idx])&&(NULL != ctx->s)) + { + /* this was the end before, add empty string */ + newctx = new_regex_ctx (ctx->size); + newctx->s = GNUNET_strdup (""); + ctx->children[idx] = newctx; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); + newctx = new_regex_ctx (ctx->size); + newctx->s = GNUNET_strdup (regex); ctx->children[idx] = newctx; } @@ -502,7 +504,7 @@ regex_add(struct RegexCombineCtx *ctx, const char *regex) * @param ctx Context to free. */ static void -regex_ctx_destroy(struct RegexCombineCtx *ctx) +regex_ctx_destroy (struct RegexCombineCtx *ctx) { unsigned int i; @@ -510,12 +512,12 @@ regex_ctx_destroy(struct RegexCombineCtx *ctx) return; for (i = 0; i < ctx->size; i++) - { - regex_ctx_destroy(ctx->children[i]); - } - GNUNET_free_non_null(ctx->s); /* 's' on root node is null */ - GNUNET_free(ctx->children); - GNUNET_free(ctx); + { + regex_ctx_destroy (ctx->children[i]); + } + GNUNET_free_non_null (ctx->s); /* 's' on root node is null */ + GNUNET_free (ctx->children); + GNUNET_free (ctx); } @@ -534,27 +536,27 @@ regex_ctx_destroy(struct RegexCombineCtx *ctx) * @return A string with a single regex that matches any of the original regexes */ char * -REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size) +REGEX_TEST_combine (char *const regexes[], unsigned int alphabet_size) { unsigned int i; char *combined; const char *current; struct RegexCombineCtx *ctx; - ctx = new_regex_ctx(alphabet_size); + ctx = new_regex_ctx (alphabet_size); for (i = 0; regexes[i]; i++) - { - current = regexes[i]; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); - regex_add(ctx, current); - debugctx(ctx, 0); - } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); - debugctx(ctx, 0); + { + current = regexes[i]; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); + regex_add (ctx, current); + debugctx (ctx, 0); + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); + debugctx (ctx, 0); - combined = regex_combine(ctx); + combined = regex_combine (ctx); - regex_ctx_destroy(ctx); + regex_ctx_destroy (ctx); return combined; } @@ -570,7 +572,7 @@ REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size) * @return A newly allocated, NULL terminated array of regexes. */ char ** -REGEX_TEST_read_from_file(const char *filename) +REGEX_TEST_read_from_file (const char *filename) { struct GNUNET_DISK_FileHandle *f; unsigned int nr; @@ -581,55 +583,55 @@ REGEX_TEST_read_from_file(const char *filename) char *regex; char **regexes; - f = GNUNET_DISK_file_open(filename, - GNUNET_DISK_OPEN_READ, - GNUNET_DISK_PERM_NONE); + f = GNUNET_DISK_file_open (filename, + GNUNET_DISK_OPEN_READ, + GNUNET_DISK_PERM_NONE); if (NULL == f) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Can't open file %s for reading\n", filename); - return NULL; - } - if (GNUNET_OK != GNUNET_DISK_file_handle_size(f, &size)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Can't get size of file %s\n", filename); - GNUNET_DISK_file_close(f); - return NULL; - } - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "using file %s, size %llu\n", - filename, (unsigned long long)size); - - buffer = GNUNET_malloc(size + 1); - GNUNET_DISK_file_read(f, buffer, size); - GNUNET_DISK_file_close(f); - regexes = GNUNET_malloc(sizeof(char *)); + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Can't open file %s for reading\n", filename); + return NULL; + } + if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Can't get size of file %s\n", filename); + GNUNET_DISK_file_close (f); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "using file %s, size %llu\n", + filename, (unsigned long long) size); + + buffer = GNUNET_malloc (size + 1); + GNUNET_DISK_file_read (f, buffer, size); + GNUNET_DISK_file_close (f); + regexes = GNUNET_malloc (sizeof(char *)); nr = 1; offset = 0; regex = NULL; do - { - if (NULL == regex) - regex = GNUNET_malloc(size + 1); - len = (size_t)sscanf(&buffer[offset], "%s", regex); - if (0 == len) - break; - len = strlen(regex); - offset += len + 1; - if (len < 1) - continue; - regex[len] = '\0'; - regex = GNUNET_realloc(regex, len + 1); - GNUNET_array_grow(regexes, nr, nr + 1); - GNUNET_assert(NULL == regexes[nr - 2]); - regexes[nr - 2] = regex; - regexes[nr - 1] = NULL; - regex = NULL; - } + { + if (NULL == regex) + regex = GNUNET_malloc (size + 1); + len = (size_t) sscanf (&buffer[offset], "%s", regex); + if (0 == len) + break; + len = strlen (regex); + offset += len + 1; + if (len < 1) + continue; + regex[len] = '\0'; + regex = GNUNET_realloc (regex, len + 1); + GNUNET_array_grow (regexes, nr, nr + 1); + GNUNET_assert (NULL == regexes[nr - 2]); + regexes[nr - 2] = regex; + regexes[nr - 1] = NULL; + regex = NULL; + } while (offset < size); - GNUNET_free_non_null(regex); - GNUNET_free(buffer); + GNUNET_free_non_null (regex); + GNUNET_free (buffer); return regexes; } @@ -641,13 +643,13 @@ REGEX_TEST_read_from_file(const char *filename) * @param regexes NULL-terminated array of regexes. */ void -REGEX_TEST_free_from_file(char **regexes) +REGEX_TEST_free_from_file (char **regexes) { unsigned int i; for (i = 0; regexes[i]; i++) - GNUNET_free(regexes[i]); - GNUNET_free(regexes); + GNUNET_free (regexes[i]); + GNUNET_free (regexes); } /* end of regex_test_lib.c */ diff --git a/src/regex/regex_test_lib.h b/src/regex/regex_test_lib.h index fe4b30498..a0ad0723a 100644 --- a/src/regex/regex_test_lib.h +++ b/src/regex/regex_test_lib.h @@ -54,7 +54,7 @@ extern "C" * @return A string with a single regex that matches any of the original regexes */ char * -REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size); +REGEX_TEST_combine (char *const regexes[], unsigned int alphabet_size); /** @@ -67,7 +67,7 @@ REGEX_TEST_combine(char * const regexes[], unsigned int alphabet_size); * @return A newly allocated, NULL terminated array of regexes. */ char ** -REGEX_TEST_read_from_file(const char *filename); +REGEX_TEST_read_from_file (const char *filename); /** @@ -76,7 +76,7 @@ REGEX_TEST_read_from_file(const char *filename); * @param regexes NULL-terminated array of regexes. */ void -REGEX_TEST_free_from_file(char **regexes); +REGEX_TEST_free_from_file (char **regexes); /** @@ -93,7 +93,7 @@ REGEX_TEST_free_from_file(char **regexes); * needs to be freed, otherwise. */ char * -REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str); +REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str); /** @@ -106,14 +106,15 @@ REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str); * @return random string that needs to be freed. */ char * -REGEX_TEST_generate_random_string(size_t max_len); +REGEX_TEST_generate_random_string (size_t max_len); /** * Options for graph creation function * REGEX_TEST_automaton_save_graph. */ -enum REGEX_TEST_GraphSavingOptions { +enum REGEX_TEST_GraphSavingOptions +{ /** * Default. Do nothing special. */ @@ -141,9 +142,9 @@ enum REGEX_TEST_GraphSavingOptions { * mode */ void -REGEX_TEST_automaton_save_graph(struct REGEX_INTERNAL_Automaton *a, - const char *filename, - enum REGEX_TEST_GraphSavingOptions options); +REGEX_TEST_automaton_save_graph (struct REGEX_INTERNAL_Automaton *a, + const char *filename, + enum REGEX_TEST_GraphSavingOptions options); diff --git a/src/regex/regex_test_random.c b/src/regex/regex_test_random.c index 9a1e9665a..29b5315db 100644 --- a/src/regex/regex_test_random.c +++ b/src/regex/regex_test_random.c @@ -34,13 +34,13 @@ * @return random valid literal */ static char -get_random_literal() +get_random_literal () { uint32_t ridx; ridx = - GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, - (uint32_t)strlen(ALLOWED_LITERALS)); + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + (uint32_t) strlen (ALLOWED_LITERALS)); return ALLOWED_LITERALS[ridx]; } @@ -61,7 +61,7 @@ get_random_literal() * needs to be freed, otherwise. */ char * -REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str) +REGEX_TEST_generate_random_regex (size_t rx_length, char *matching_str) { char *rx; char *rx_p; @@ -80,59 +80,60 @@ REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str) else matching_strp = NULL; - rx = GNUNET_malloc(rx_length + 1); + rx = GNUNET_malloc (rx_length + 1); rx_p = rx; current_char = 0; last_was_op = 1; for (i = 0; i < rx_length; i++) + { + char_op_switch = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2); + + if ((0 == char_op_switch) && ! last_was_op) + { + last_was_op = 1; + rx_op = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 4); + + switch (rx_op) + { + case 0: + current_char = '+'; + break; + + case 1: + current_char = '*'; + break; + + case 2: + current_char = '?'; + break; + + case 3: + if (i < rx_length - 1) /* '|' cannot be at the end */ + current_char = '|'; + else + current_char = get_random_literal (); + break; + } + } + else { - char_op_switch = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2); - - if (0 == char_op_switch && !last_was_op) - { - last_was_op = 1; - rx_op = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 4); - - switch (rx_op) - { - case 0: - current_char = '+'; - break; - - case 1: - current_char = '*'; - break; - - case 2: - current_char = '?'; - break; - - case 3: - if (i < rx_length - 1) /* '|' cannot be at the end */ - current_char = '|'; - else - current_char = get_random_literal(); - break; - } - } - else - { - current_char = get_random_literal(); - last_was_op = 0; - } - - if (NULL != matching_strp && - (current_char != '+' && current_char != '*' && current_char != '?' && - current_char != '|')) - { - *matching_strp = current_char; - matching_strp++; - } - - *rx_p = current_char; - rx_p++; + current_char = get_random_literal (); + last_was_op = 0; } + + if ((NULL != matching_strp) && + ((current_char != '+') &&(current_char != '*') &&(current_char != + '?') && + (current_char != '|') )) + { + *matching_strp = current_char; + matching_strp++; + } + + *rx_p = current_char; + rx_p++; + } *rx_p = '\0'; if (NULL != matching_strp) *matching_strp = '\0'; @@ -151,22 +152,22 @@ REGEX_TEST_generate_random_regex(size_t rx_length, char *matching_str) * @return random string that needs to be freed. */ char * -REGEX_TEST_generate_random_string(size_t max_len) +REGEX_TEST_generate_random_string (size_t max_len) { unsigned int i; char *str; size_t len; if (1 > max_len) - return GNUNET_strdup(""); + return GNUNET_strdup (""); - len = (size_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max_len); - str = GNUNET_malloc(len + 1); + len = (size_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_len); + str = GNUNET_malloc (len + 1); for (i = 0; i < len; i++) - { - str[i] = get_random_literal(); - } + { + str[i] = get_random_literal (); + } str[i] = '\0'; diff --git a/src/regex/test_regex_api.c b/src/regex/test_regex_api.c index 2ee24800a..eaae8c682 100644 --- a/src/regex/test_regex_api.c +++ b/src/regex/test_regex_api.c @@ -31,12 +31,13 @@ /** * How long until we really give up on a particular testcase portion? */ -#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 600) +#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \ + 600) /** * How long until we give up on any particular operation (and retry)? */ -#define BASE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3) +#define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) static struct GNUNET_REGEX_Announcement *a; @@ -49,25 +50,25 @@ static struct GNUNET_SCHEDULER_Task *die_task; static void -end(void *cls) +end (void *cls) { die_task = NULL; - GNUNET_REGEX_announce_cancel(a); + GNUNET_REGEX_announce_cancel (a); a = NULL; - GNUNET_REGEX_search_cancel(s); + GNUNET_REGEX_search_cancel (s); s = NULL; ok = 0; } static void -end_badly() +end_badly () { die_task = NULL; - fprintf(stderr, "%s", "Testcase failed (timeout).\n"); - GNUNET_REGEX_announce_cancel(a); + fprintf (stderr, "%s", "Testcase failed (timeout).\n"); + GNUNET_REGEX_announce_cancel (a); a = NULL; - GNUNET_REGEX_search_cancel(s); + GNUNET_REGEX_search_cancel (s); s = NULL; ok = 1; } @@ -84,44 +85,45 @@ end_badly() * @param put_path_length Length of the put_path. */ static void -found_cb(void *cls, - const struct GNUNET_PeerIdentity *id, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length) +found_cb (void *cls, + const struct GNUNET_PeerIdentity *id, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length) { - GNUNET_SCHEDULER_cancel(die_task); + GNUNET_SCHEDULER_cancel (die_task); die_task = - GNUNET_SCHEDULER_add_now(&end, NULL); + GNUNET_SCHEDULER_add_now (&end, NULL); } static void -run(void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { die_task = - GNUNET_SCHEDULER_add_delayed(TOTAL_TIMEOUT, - &end_badly, NULL); - a = GNUNET_REGEX_announce(cfg, - "my long prefix - hello world(0|1)*", - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, - 5), - 1); - s = GNUNET_REGEX_search(cfg, - "my long prefix - hello world0101", - &found_cb, NULL); + GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, + &end_badly, NULL); + a = GNUNET_REGEX_announce (cfg, + "my long prefix - hello world(0|1)*", + GNUNET_TIME_relative_multiply ( + GNUNET_TIME_UNIT_SECONDS, + 5), + 1); + s = GNUNET_REGEX_search (cfg, + "my long prefix - hello world0101", + &found_cb, NULL); } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_peer_run("test-regex-api", - "test_regex_api_data.conf", - &run, NULL)) + if (0 != GNUNET_TESTING_peer_run ("test-regex-api", + "test_regex_api_data.conf", + &run, NULL)) return 1; return ok; } diff --git a/src/regex/test_regex_eval_api.c b/src/regex/test_regex_eval_api.c index 88a05f912..f6078c7ef 100644 --- a/src/regex/test_regex_eval_api.c +++ b/src/regex/test_regex_eval_api.c @@ -29,12 +29,14 @@ #include "regex_test_lib.h" #include "regex_internal.h" -enum Match_Result { +enum Match_Result +{ match = 0, nomatch = 1 }; -struct Regex_String_Pair { +struct Regex_String_Pair +{ char *regex; int string_count; char *strings[20]; @@ -55,8 +57,8 @@ struct Regex_String_Pair { * @return 0 on success, non 0 otherwise. */ int -test_random(unsigned int rx_length, unsigned int max_str_len, - unsigned int str_count) +test_random (unsigned int rx_length, unsigned int max_str_len, + unsigned int str_count) { unsigned int i; char *rand_rx; @@ -73,108 +75,112 @@ test_random(unsigned int rx_length, unsigned int max_str_len, char *canonical_regex = NULL; /* At least one string is needed for matching */ - GNUNET_assert(str_count > 0); + GNUNET_assert (str_count > 0); /* The string should be at least as long as the regex itself */ - GNUNET_assert(max_str_len >= rx_length); + GNUNET_assert (max_str_len >= rx_length); /* Generate random regex and a string that matches the regex */ - matching_str = GNUNET_malloc(rx_length + 1); - rand_rx = REGEX_TEST_generate_random_regex(rx_length, matching_str); + matching_str = GNUNET_malloc (rx_length + 1); + rand_rx = REGEX_TEST_generate_random_regex (rx_length, matching_str); /* Now match */ result = 0; for (i = 0; i < str_count; i++) + { + if (0 < i) + { + matching_str = REGEX_TEST_generate_random_string (max_str_len); + } + + /* Match string using DFA */ + dfa = REGEX_INTERNAL_construct_dfa (rand_rx, strlen (rand_rx), 0); + if (NULL == dfa) { - if (0 < i) - { - matching_str = REGEX_TEST_generate_random_string(max_str_len); - } - - /* Match string using DFA */ - dfa = REGEX_INTERNAL_construct_dfa(rand_rx, strlen(rand_rx), 0); - if (NULL == dfa) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); - goto error; - } - - eval = REGEX_INTERNAL_eval(dfa, matching_str); - /* save the canonical regex for later comparison */ - canonical_regex = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(dfa)); - REGEX_INTERNAL_automaton_destroy(dfa); - - /* Match string using glibc regex */ - if (0 != regcomp(&rx, rand_rx, REG_EXTENDED)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not compile regex using regcomp: %s\n", rand_rx); - goto error; - } - - eval_check = regexec(&rx, matching_str, 1, matchptr, 0); - regfree(&rx); - - /* We only want to match the whole string, because that's what our DFA does, - * too. */ - if (eval_check == 0 && - (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen(matching_str))) - eval_check = 1; - - /* Match canonical regex */ - dfa = - REGEX_INTERNAL_construct_dfa(canonical_regex, strlen(canonical_regex), - 0); - if (NULL == dfa) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); - goto error; - } - - eval_canonical = REGEX_INTERNAL_eval(dfa, matching_str); - REGEX_INTERNAL_automaton_destroy(dfa); - - if (0 != regcomp(&rx, canonical_regex, REG_EXTENDED)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not compile regex using regcomp: %s\n", - canonical_regex); - goto error; - } - - eval_canonical_check = regexec(&rx, matching_str, 1, matchptr, 0); - regfree(&rx); - - /* We only want to match the whole string, because that's what our DFA does, - * too. */ - if (eval_canonical_check == 0 && - (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen(matching_str))) - eval_canonical_check = 1; - - /* compare results */ - if (eval_check != eval || eval_canonical != eval_canonical_check) - { - regerror(eval_check, &rx, error, sizeof error); - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unexpected result:\nregex: %s\ncanonical_regex: %s\n\ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); + goto error; + } + + eval = REGEX_INTERNAL_eval (dfa, matching_str); + /* save the canonical regex for later comparison */ + canonical_regex = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa)); + REGEX_INTERNAL_automaton_destroy (dfa); + + /* Match string using glibc regex */ + if (0 != regcomp (&rx, rand_rx, REG_EXTENDED)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not compile regex using regcomp: %s\n", rand_rx); + goto error; + } + + eval_check = regexec (&rx, matching_str, 1, matchptr, 0); + regfree (&rx); + + /* We only want to match the whole string, because that's what our DFA does, + * too. */ + if ((eval_check == 0) && + ((matchptr[0].rm_so != 0) ||(matchptr[0].rm_eo != strlen ( + matching_str)) )) + eval_check = 1; + + /* Match canonical regex */ + dfa = + REGEX_INTERNAL_construct_dfa (canonical_regex, strlen (canonical_regex), + 0); + if (NULL == dfa) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n"); + goto error; + } + + eval_canonical = REGEX_INTERNAL_eval (dfa, matching_str); + REGEX_INTERNAL_automaton_destroy (dfa); + + if (0 != regcomp (&rx, canonical_regex, REG_EXTENDED)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not compile regex using regcomp: %s\n", + canonical_regex); + goto error; + } + + eval_canonical_check = regexec (&rx, matching_str, 1, matchptr, 0); + regfree (&rx); + + /* We only want to match the whole string, because that's what our DFA does, + * too. */ + if ((eval_canonical_check == 0) && + ((matchptr[0].rm_so != 0) ||(matchptr[0].rm_eo != strlen ( + matching_str)) )) + eval_canonical_check = 1; + + /* compare results */ + if ((eval_check != eval) ||(eval_canonical != eval_canonical_check) ) + { + regerror (eval_check, &rx, error, sizeof error); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected result:\nregex: %s\ncanonical_regex: %s\n\ string: %s\ngnunet regex: %i\nglibc regex: %i\n\ canonical regex: %i\ncanonical regex glibc: %i\n\ glibc error: %s\n\n", rand_rx, canonical_regex, matching_str, - eval, eval_check, eval_canonical, eval_canonical_check, error); - result += 1; - } - GNUNET_free(canonical_regex); - GNUNET_free(matching_str); - canonical_regex = NULL; - matching_str = NULL; + eval, eval_check, eval_canonical, eval_canonical_check, + error); + result += 1; } + GNUNET_free (canonical_regex); + GNUNET_free (matching_str); + canonical_regex = NULL; + matching_str = NULL; + } - GNUNET_free(rand_rx); + GNUNET_free (rand_rx); return result; error: - GNUNET_free_non_null(matching_str); - GNUNET_free_non_null(rand_rx); - GNUNET_free_non_null(canonical_regex); + GNUNET_free_non_null (matching_str); + GNUNET_free_non_null (rand_rx); + GNUNET_free_non_null (canonical_regex); return -1; } @@ -191,8 +197,8 @@ error: * @return 0 on successfull, non 0 otherwise */ int -test_automaton(struct REGEX_INTERNAL_Automaton *a, regex_t * rx, - struct Regex_String_Pair *rxstr) +test_automaton (struct REGEX_INTERNAL_Automaton *a, regex_t *rx, + struct Regex_String_Pair *rxstr) { int result; int eval; @@ -202,48 +208,49 @@ test_automaton(struct REGEX_INTERNAL_Automaton *a, regex_t * rx, int i; if (NULL == a) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n"); - return 1; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n"); + return 1; + } result = 0; for (i = 0; i < rxstr->string_count; i++) + { + eval = REGEX_INTERNAL_eval (a, rxstr->strings[i]); + eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0); + + /* We only want to match the whole string, because that's what our DFA does, + * too. */ + if ((eval_check == 0) && + ((matchptr[0].rm_so != 0) || + (matchptr[0].rm_eo != strlen (rxstr->strings[i])) )) + eval_check = 1; + + if (((rxstr->expected_results[i] == match) && ((0 != eval) ||(0 != + eval_check) )) + || ((rxstr->expected_results[i] == nomatch) && + ((0 == eval) ||(0 == eval_check) ))) { - eval = REGEX_INTERNAL_eval(a, rxstr->strings[i]); - eval_check = regexec(rx, rxstr->strings[i], 1, matchptr, 0); - - /* We only want to match the whole string, because that's what our DFA does, - * too. */ - if (eval_check == 0 && - (matchptr[0].rm_so != 0 || - matchptr[0].rm_eo != strlen(rxstr->strings[i]))) - eval_check = 1; - - if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check)) - || (rxstr->expected_results[i] == nomatch && - (0 == eval || 0 == eval_check))) - { - result = 1; - regerror(eval_check, rx, error, sizeof error); - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Unexpected result:\nregex: %s\ncanonical_regex: %s\n" - "string: %s\nexpected result: %i\n" - "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n" - "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex, - REGEX_INTERNAL_get_canonical_regex(a), rxstr->strings[i], - rxstr->expected_results[i], eval, eval_check, error, - matchptr[0].rm_so, matchptr[0].rm_eo); - } + result = 1; + regerror (eval_check, rx, error, sizeof error); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected result:\nregex: %s\ncanonical_regex: %s\n" + "string: %s\nexpected result: %i\n" + "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n" + "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex, + REGEX_INTERNAL_get_canonical_regex (a), rxstr->strings[i], + rxstr->expected_results[i], eval, eval_check, error, + matchptr[0].rm_so, matchptr[0].rm_eo); } + } return result; } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { - GNUNET_log_setup("test-regex", "WARNING", NULL); + GNUNET_log_setup ("test-regex", "WARNING", NULL); struct REGEX_INTERNAL_Automaton *a; regex_t rx; @@ -270,15 +277,24 @@ main(int argc, char *argv[]) { "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1, { "kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg" }, { nomatch } }, - { "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, + { + "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, { "kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg" }, - { nomatch } }, - { "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, + { nomatch } + }, + { + "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, { "osfjsodfonONONOnosndfsdnfsd" }, - { nomatch } }, - { "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, + { nomatch } + }, + { + "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, { "VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C" }, - { nomatch } }, + { nomatch } + }, { "(bla)*", 8, { "", "bla", "blabla", "bl", "la", "b", "l", "a" }, { match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch } }, @@ -314,11 +330,13 @@ main(int argc, char *argv[]) { "a()b", 1, { "ab" }, { match } }, - { "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)*)", + { + "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)*)", 2, { "GNVPN-0001-PAD1101111010100101011101010101010101", "GNVPN-0001-PAD11001010100111000101101010101" }, - { match, match } } + { match, match } + } }; check_nfa = 0; @@ -326,39 +344,40 @@ main(int argc, char *argv[]) check_rand = 0; for (i = 0; i < 19; i++) + { + if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED)) { - if (0 != regcomp(&rx, rxstr[i].regex, REG_EXTENDED)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Could not compile regex using regcomp()\n"); - return 1; - } - - /* NFA test */ - a = REGEX_INTERNAL_construct_nfa(rxstr[i].regex, strlen(rxstr[i].regex)); - check_nfa += test_automaton(a, &rx, &rxstr[i]); - REGEX_INTERNAL_automaton_destroy(a); - - /* DFA test */ - a = REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0); - check_dfa += test_automaton(a, &rx, &rxstr[i]); - check_proof = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(a)); - REGEX_INTERNAL_automaton_destroy(a); - - a = REGEX_INTERNAL_construct_dfa(check_proof, strlen(check_proof), 0); - check_dfa += test_automaton(a, &rx, &rxstr[i]); - REGEX_INTERNAL_automaton_destroy(a); - if (0 != check_dfa) - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof); - GNUNET_free_non_null(check_proof); - - regfree(&rx); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not compile regex using regcomp()\n"); + return 1; } + /* NFA test */ + a = REGEX_INTERNAL_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex)); + check_nfa += test_automaton (a, &rx, &rxstr[i]); + REGEX_INTERNAL_automaton_destroy (a); + + /* DFA test */ + a = REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), + 0); + check_dfa += test_automaton (a, &rx, &rxstr[i]); + check_proof = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (a)); + REGEX_INTERNAL_automaton_destroy (a); + + a = REGEX_INTERNAL_construct_dfa (check_proof, strlen (check_proof), 0); + check_dfa += test_automaton (a, &rx, &rxstr[i]); + REGEX_INTERNAL_automaton_destroy (a); + if (0 != check_dfa) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof); + GNUNET_free_non_null (check_proof); + + regfree (&rx); + } + /* Random tests */ - srand(time(NULL)); + srand (time (NULL)); for (i = 0; i < 20; i++) - check_rand += test_random(50, 60, 10); + check_rand += test_random (50, 60, 10); return check_nfa + check_dfa + check_rand; } diff --git a/src/regex/test_regex_graph_api.c b/src/regex/test_regex_graph_api.c index 356782dec..923bb9f42 100644 --- a/src/regex/test_regex_graph_api.c +++ b/src/regex/test_regex_graph_api.c @@ -39,39 +39,39 @@ * @return 0 if ok, non 0 on error. */ static int -filecheck(const char *filename) +filecheck (const char *filename) { int error = 0; FILE *fp; /* Check if file was created and delete it again */ - if (NULL == (fp = fopen(filename, "r"))) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename); - return 1; - } - - GNUNET_break(0 == fseek(fp, 0L, SEEK_END)); - if (1 > ftell(fp)) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Graph writing failed, got empty file (%s)!\n", filename); - error = 2; - } - - GNUNET_assert(0 == fclose(fp)); - - if (!KEEP_FILES) - { - if (0 != unlink(filename)) - GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "unlink", filename); - } + if (NULL == (fp = fopen (filename, "r"))) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename); + return 1; + } + + GNUNET_break (0 == fseek (fp, 0L, SEEK_END)); + if (1 > ftell (fp)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Graph writing failed, got empty file (%s)!\n", filename); + error = 2; + } + + GNUNET_assert (0 == fclose (fp)); + + if (! KEEP_FILES) + { + if (0 != unlink (filename)) + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink", filename); + } return error; } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { int error; struct REGEX_INTERNAL_Automaton *a; @@ -93,65 +93,65 @@ main(int argc, char *argv[]) "PADPADPADPADPADPabcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd" }; - GNUNET_log_setup("test-regex", "WARNING", NULL); + GNUNET_log_setup ("test-regex", "WARNING", NULL); error = 0; for (i = 0; i < 12; i++) - { - /* Check NFA graph creation */ - a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i])); - REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i])); - REGEX_TEST_automaton_save_graph(a, filename, - REGEX_TEST_GRAPH_DEFAULT | - REGEX_TEST_GRAPH_VERBOSE); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i])); - REGEX_TEST_automaton_save_graph(a, filename, - REGEX_TEST_GRAPH_DEFAULT | - REGEX_TEST_GRAPH_COLORING); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - a = REGEX_INTERNAL_construct_nfa(regex[i], strlen(regex[i])); - REGEX_TEST_automaton_save_graph(a, filename, - REGEX_TEST_GRAPH_DEFAULT | - REGEX_TEST_GRAPH_VERBOSE | - REGEX_TEST_GRAPH_COLORING); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - - /* Check DFA graph creation */ - a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0); - REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0); - REGEX_TEST_automaton_save_graph(a, filename, - REGEX_TEST_GRAPH_DEFAULT | - REGEX_TEST_GRAPH_VERBOSE); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 0); - REGEX_TEST_automaton_save_graph(a, filename, - REGEX_TEST_GRAPH_DEFAULT | - REGEX_TEST_GRAPH_COLORING); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - - - a = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 4); - REGEX_TEST_automaton_save_graph(a, filename, REGEX_TEST_GRAPH_DEFAULT); - REGEX_INTERNAL_automaton_destroy(a); - error += filecheck(filename); - } + { + /* Check NFA graph creation */ + a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); + REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); + REGEX_TEST_automaton_save_graph (a, filename, + REGEX_TEST_GRAPH_DEFAULT + | REGEX_TEST_GRAPH_VERBOSE); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); + REGEX_TEST_automaton_save_graph (a, filename, + REGEX_TEST_GRAPH_DEFAULT + | REGEX_TEST_GRAPH_COLORING); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + a = REGEX_INTERNAL_construct_nfa (regex[i], strlen (regex[i])); + REGEX_TEST_automaton_save_graph (a, filename, + REGEX_TEST_GRAPH_DEFAULT + | REGEX_TEST_GRAPH_VERBOSE + | REGEX_TEST_GRAPH_COLORING); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + + /* Check DFA graph creation */ + a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); + REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); + REGEX_TEST_automaton_save_graph (a, filename, + REGEX_TEST_GRAPH_DEFAULT + | REGEX_TEST_GRAPH_VERBOSE); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 0); + REGEX_TEST_automaton_save_graph (a, filename, + REGEX_TEST_GRAPH_DEFAULT + | REGEX_TEST_GRAPH_COLORING); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + + + a = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 4); + REGEX_TEST_automaton_save_graph (a, filename, REGEX_TEST_GRAPH_DEFAULT); + REGEX_INTERNAL_automaton_destroy (a); + error += filecheck (filename); + } return error; } diff --git a/src/regex/test_regex_integration.c b/src/regex/test_regex_integration.c index b107c6d09..c7a7e38b5 100644 --- a/src/regex/test_regex_integration.c +++ b/src/regex/test_regex_integration.c @@ -35,12 +35,13 @@ /** * How long until we really give up on a particular testcase portion? */ -#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 600) +#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \ + 600) /** * How long until we give up on any particular operation (and retry)? */ -#define BASE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3) +#define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) static struct GNUNET_REGEX_Announcement *a4; @@ -57,26 +58,26 @@ static struct GNUNET_SCHEDULER_Task *die_task; static void -end(void *cls) +end (void *cls) { die_task = NULL; - GNUNET_REGEX_announce_cancel(a4); + GNUNET_REGEX_announce_cancel (a4); a4 = NULL; - GNUNET_REGEX_search_cancel(s4); + GNUNET_REGEX_search_cancel (s4); s4 = NULL; - GNUNET_REGEX_announce_cancel(a6); + GNUNET_REGEX_announce_cancel (a6); a6 = NULL; - GNUNET_REGEX_search_cancel(s6); + GNUNET_REGEX_search_cancel (s6); s6 = NULL; ok = 0; } static void -end_badly() +end_badly () { - fprintf(stderr, "%s", "Testcase failed (timeout).\n"); - end(NULL); + fprintf (stderr, "%s", "Testcase failed (timeout).\n"); + end (NULL); ok = 1; } @@ -92,36 +93,36 @@ end_badly() * @param put_path_length Length of the @a put_path. */ static void -found_cb(void *cls, - const struct GNUNET_PeerIdentity *id, - const struct GNUNET_PeerIdentity *get_path, - unsigned int get_path_length, - const struct GNUNET_PeerIdentity *put_path, - unsigned int put_path_length) +found_cb (void *cls, + const struct GNUNET_PeerIdentity *id, + const struct GNUNET_PeerIdentity *get_path, + unsigned int get_path_length, + const struct GNUNET_PeerIdentity *put_path, + unsigned int put_path_length) { const char *str = cls; static int found; - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "IPv%s-exit found\n", - str); - if (0 == strcmp(str, "4")) + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "IPv%s-exit found\n", + str); + if (0 == strcmp (str, "4")) found |= 4; - if (0 == strcmp(str, "6")) + if (0 == strcmp (str, "6")) found |= 2; if ((4 | 2) == found) - { - GNUNET_SCHEDULER_cancel(die_task); - die_task = - GNUNET_SCHEDULER_add_now(&end, NULL); - } + { + GNUNET_SCHEDULER_cancel (die_task); + die_task = + GNUNET_SCHEDULER_add_now (&end, NULL); + } } static void -run(void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg, - struct GNUNET_TESTING_Peer *peer) +run (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg, + struct GNUNET_TESTING_Peer *peer) { char rxstr4[GNUNET_TUN_IPV4_REGEXLEN]; char rxstr6[GNUNET_TUN_IPV6_REGEXLEN]; @@ -135,72 +136,74 @@ run(void *cls, struct in6_addr i6; die_task = - GNUNET_SCHEDULER_add_delayed(TOTAL_TIMEOUT, - &end_badly, NULL); - GNUNET_assert(1 == - inet_pton(AF_INET, - "127.0.0.1", - &i4)); - GNUNET_assert(1 == - inet_pton(AF_INET6, - "::1:5", - &i6)); - GNUNET_TUN_ipv4toregexsearch(&i4, - 8080, - rxstr4); - GNUNET_TUN_ipv6toregexsearch(&i6, - 8686, - rxstr6); - GNUNET_asprintf(&ss4, - "%s%s", - GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, - rxstr4); - GNUNET_asprintf(&ss6, - "%s%s", - GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, - rxstr6); - p4r = GNUNET_TUN_ipv4policy2regex("0.0.0.0/0:!25;"); - p6r = GNUNET_TUN_ipv6policy2regex("::/0:!25;"); - GNUNET_asprintf(&p4, - "%s%s", - GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, - p4r); - GNUNET_asprintf(&p6, - "%s%s", - GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, - p6r); - GNUNET_free(p4r); - GNUNET_free(p6r); - a4 = GNUNET_REGEX_announce(cfg, - p4, - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, - 5), - 1); - a6 = GNUNET_REGEX_announce(cfg, - p6, - GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, - 5), - 1); - GNUNET_free(p4); - GNUNET_free(p6); - - s4 = GNUNET_REGEX_search(cfg, - ss4, - &found_cb, "4"); - s6 = GNUNET_REGEX_search(cfg, - ss6, - &found_cb, "6"); - GNUNET_free(ss4); - GNUNET_free(ss6); + GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, + &end_badly, NULL); + GNUNET_assert (1 == + inet_pton (AF_INET, + "127.0.0.1", + &i4)); + GNUNET_assert (1 == + inet_pton (AF_INET6, + "::1:5", + &i6)); + GNUNET_TUN_ipv4toregexsearch (&i4, + 8080, + rxstr4); + GNUNET_TUN_ipv6toregexsearch (&i6, + 8686, + rxstr6); + GNUNET_asprintf (&ss4, + "%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + rxstr4); + GNUNET_asprintf (&ss6, + "%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + rxstr6); + p4r = GNUNET_TUN_ipv4policy2regex ("0.0.0.0/0:!25;"); + p6r = GNUNET_TUN_ipv6policy2regex ("::/0:!25;"); + GNUNET_asprintf (&p4, + "%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + p4r); + GNUNET_asprintf (&p6, + "%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + p6r); + GNUNET_free (p4r); + GNUNET_free (p6r); + a4 = GNUNET_REGEX_announce (cfg, + p4, + GNUNET_TIME_relative_multiply ( + GNUNET_TIME_UNIT_SECONDS, + 5), + 1); + a6 = GNUNET_REGEX_announce (cfg, + p6, + GNUNET_TIME_relative_multiply ( + GNUNET_TIME_UNIT_SECONDS, + 5), + 1); + GNUNET_free (p4); + GNUNET_free (p6); + + s4 = GNUNET_REGEX_search (cfg, + ss4, + &found_cb, "4"); + s6 = GNUNET_REGEX_search (cfg, + ss6, + &found_cb, "6"); + GNUNET_free (ss4); + GNUNET_free (ss6); } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { - if (0 != GNUNET_TESTING_peer_run("test-regex-integration", - "test_regex_api_data.conf", - &run, NULL)) + if (0 != GNUNET_TESTING_peer_run ("test-regex-integration", + "test_regex_api_data.conf", + &run, NULL)) return 1; return ok; } diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c index 7711a1154..e7ef72b58 100644 --- a/src/regex/test_regex_iterate_api.c +++ b/src/regex/test_regex_iterate_api.c @@ -41,7 +41,8 @@ static unsigned int transition_counter; -struct IteratorContext { +struct IteratorContext +{ int error; int should_save_graph; FILE *graph_filep; @@ -50,7 +51,8 @@ struct IteratorContext { unsigned int match_count; }; -struct RegexStringPair { +struct RegexStringPair +{ char *regex; unsigned int string_count; char *strings[20]; @@ -58,63 +60,63 @@ struct RegexStringPair { static void -key_iterator(void *cls, const struct GNUNET_HashCode *key, - const char *proof, - int accepting, unsigned int num_edges, - const struct REGEX_BLOCK_Edge *edges) +key_iterator (void *cls, const struct GNUNET_HashCode *key, + const char *proof, + int accepting, unsigned int num_edges, + const struct REGEX_BLOCK_Edge *edges) { unsigned int i; struct IteratorContext *ctx = cls; char *out_str; - char *state_id = GNUNET_strdup(GNUNET_h2s(key)); + char *state_id = GNUNET_strdup (GNUNET_h2s (key)); - GNUNET_assert(NULL != proof); + GNUNET_assert (NULL != proof); if (GNUNET_YES == ctx->should_save_graph) + { + if (GNUNET_YES == accepting) + GNUNET_asprintf (&out_str, "\"%s\" [shape=doublecircle]\n", state_id); + else + GNUNET_asprintf (&out_str, "\"%s\" [shape=circle]\n", state_id); + fwrite (out_str, strlen (out_str), 1, ctx->graph_filep); + GNUNET_free (out_str); + + for (i = 0; i < num_edges; i++) { - if (GNUNET_YES == accepting) - GNUNET_asprintf(&out_str, "\"%s\" [shape=doublecircle]\n", state_id); - else - GNUNET_asprintf(&out_str, "\"%s\" [shape=circle]\n", state_id); - fwrite(out_str, strlen(out_str), 1, ctx->graph_filep); - GNUNET_free(out_str); - - for (i = 0; i < num_edges; i++) - { - transition_counter++; - GNUNET_asprintf(&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n", - state_id, GNUNET_h2s(&edges[i].destination), - edges[i].label, proof); - fwrite(out_str, strlen(out_str), 1, ctx->graph_filep); - - GNUNET_free(out_str); - } + transition_counter++; + GNUNET_asprintf (&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n", + state_id, GNUNET_h2s (&edges[i].destination), + edges[i].label, proof); + fwrite (out_str, strlen (out_str), 1, ctx->graph_filep); + + GNUNET_free (out_str); } + } else - { - for (i = 0; i < num_edges; i++) - transition_counter++; - } + { + for (i = 0; i < num_edges; i++) + transition_counter++; + } for (i = 0; i < ctx->string_count; i++) - { - if (0 == strcmp(proof, ctx->strings[i])) - ctx->match_count++; - } - - if (GNUNET_OK != REGEX_BLOCK_check_proof(proof, strlen(proof), key)) - { - ctx->error++; - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Proof check failed: proof: %s key: %s\n", proof, state_id); - } - GNUNET_free(state_id); + { + if (0 == strcmp (proof, ctx->strings[i])) + ctx->match_count++; + } + + if (GNUNET_OK != REGEX_BLOCK_check_proof (proof, strlen (proof), key)) + { + ctx->error++; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Proof check failed: proof: %s key: %s\n", proof, state_id); + } + GNUNET_free (state_id); } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { - GNUNET_log_setup("test-regex", "WARNING", NULL); + GNUNET_log_setup ("test-regex", "WARNING", NULL); int error; struct REGEX_INTERNAL_Automaton *dfa; @@ -143,115 +145,117 @@ main(int argc, char *argv[]) 2, { INITIAL_PADDING "abcd:000", INITIAL_PADDING "abcd:101" } }, { INITIAL_PADDING "(x*|(0|1|2)(a|b|c|d)+)", 2, { INITIAL_PADDING "xxxxxxxx", INITIAL_PADDING "0abcdbad" } }, - { INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, { INITIAL_PADDING "11234567" } }, + { INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, + { INITIAL_PADDING "11234567" } }, { INITIAL_PADDING "0*123456789ABC*", 3, { INITIAL_PADDING "00123456", INITIAL_PADDING "00000000", INITIAL_PADDING "12345678" } }, { INITIAL_PADDING "0123456789A*BC", 1, { INITIAL_PADDING "01234567" } }, - { "GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, { "GNUNETVPN000100000IPEX6-" } } + { "GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, + { "GNUNETVPN000100000IPEX6-" } } }; const char *graph_start_str = "digraph G {\nrankdir=LR\n"; const char *graph_end_str = "\n}\n"; for (i = 0; i < 13; i++) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n", + rxstr[i].regex); + + + /* Create graph */ + if (GNUNET_YES == REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH) { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n", - rxstr[i].regex); - - - /* Create graph */ - if (GNUNET_YES == REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH) - { - GNUNET_asprintf(&filename, "iteration_graph_%u.dot", i); - ctx.graph_filep = fopen(filename, "w"); - if (NULL == ctx.graph_filep) - { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, - "Could not open file %s for saving iteration graph.\n", - filename); - ctx.should_save_graph = GNUNET_NO; - } - else - { - ctx.should_save_graph = GNUNET_YES; - fwrite(graph_start_str, strlen(graph_start_str), 1, ctx.graph_filep); - } - GNUNET_free(filename); - } + GNUNET_asprintf (&filename, "iteration_graph_%u.dot", i); + ctx.graph_filep = fopen (filename, "w"); + if (NULL == ctx.graph_filep) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Could not open file %s for saving iteration graph.\n", + filename); + ctx.should_save_graph = GNUNET_NO; + } else - { - ctx.should_save_graph = GNUNET_NO; - ctx.graph_filep = NULL; - } - - /* Iterate over DFA edges */ - transition_counter = 0; - ctx.string_count = rxstr[i].string_count; - ctx.strings = rxstr[i].strings; - ctx.match_count = 0; - dfa = - REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0); - REGEX_INTERNAL_iterate_all_edges(dfa, key_iterator, &ctx); - num_transitions = - REGEX_INTERNAL_get_transition_count(dfa) - dfa->start->transition_count; - - if (transition_counter < num_transitions) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Automaton has %d transitions, iterated over %d transitions\n", - num_transitions, transition_counter); - error += 1; - } - - if (ctx.match_count < ctx.string_count) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Missing initial states for regex %s\n", rxstr[i].regex); - error += (ctx.string_count - ctx.match_count); - } - else if (ctx.match_count > ctx.string_count) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Duplicate initial transitions for regex %s\n", - rxstr[i].regex); - error += (ctx.string_count - ctx.match_count); - } - - REGEX_INTERNAL_automaton_destroy(dfa); - - /* Finish graph */ - if (GNUNET_YES == ctx.should_save_graph) - { - fwrite(graph_end_str, strlen(graph_end_str), 1, ctx.graph_filep); - fclose(ctx.graph_filep); - ctx.graph_filep = NULL; - ctx.should_save_graph = GNUNET_NO; - } + { + ctx.should_save_graph = GNUNET_YES; + fwrite (graph_start_str, strlen (graph_start_str), 1, ctx.graph_filep); + } + GNUNET_free (filename); + } + else + { + ctx.should_save_graph = GNUNET_NO; + ctx.graph_filep = NULL; } + /* Iterate over DFA edges */ + transition_counter = 0; + ctx.string_count = rxstr[i].string_count; + ctx.strings = rxstr[i].strings; + ctx.match_count = 0; + dfa = + REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0); + REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx); + num_transitions = + REGEX_INTERNAL_get_transition_count (dfa) - dfa->start->transition_count; + + if (transition_counter < num_transitions) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Automaton has %d transitions, iterated over %d transitions\n", + num_transitions, transition_counter); + error += 1; + } + + if (ctx.match_count < ctx.string_count) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Missing initial states for regex %s\n", rxstr[i].regex); + error += (ctx.string_count - ctx.match_count); + } + else if (ctx.match_count > ctx.string_count) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Duplicate initial transitions for regex %s\n", + rxstr[i].regex); + error += (ctx.string_count - ctx.match_count); + } + + REGEX_INTERNAL_automaton_destroy (dfa); + + /* Finish graph */ + if (GNUNET_YES == ctx.should_save_graph) + { + fwrite (graph_end_str, strlen (graph_end_str), 1, ctx.graph_filep); + fclose (ctx.graph_filep); + ctx.graph_filep = NULL; + ctx.should_save_graph = GNUNET_NO; + } + } + for (i = 0; i < 13; i++) + { + ctx.string_count = rxstr[i].string_count; + ctx.strings = rxstr[i].strings; + ctx.match_count = 0; + + dfa = + REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0); + REGEX_INTERNAL_dfa_add_multi_strides (NULL, dfa, 2); + REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx); + + if (ctx.match_count < ctx.string_count) { - ctx.string_count = rxstr[i].string_count; - ctx.strings = rxstr[i].strings; - ctx.match_count = 0; - - dfa = - REGEX_INTERNAL_construct_dfa(rxstr[i].regex, strlen(rxstr[i].regex), 0); - REGEX_INTERNAL_dfa_add_multi_strides(NULL, dfa, 2); - REGEX_INTERNAL_iterate_all_edges(dfa, key_iterator, &ctx); - - if (ctx.match_count < ctx.string_count) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Missing initial states for regex %s\n", rxstr[i].regex); - error += (ctx.string_count - ctx.match_count); - } - - REGEX_INTERNAL_automaton_destroy(dfa); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Missing initial states for regex %s\n", rxstr[i].regex); + error += (ctx.string_count - ctx.match_count); } + REGEX_INTERNAL_automaton_destroy (dfa); + } + error += ctx.error; return error; diff --git a/src/regex/test_regex_proofs.c b/src/regex/test_regex_proofs.c index 3649ac8aa..8908084b0 100644 --- a/src/regex/test_regex_proofs.c +++ b/src/regex/test_regex_proofs.c @@ -40,32 +40,32 @@ * @return 0 on success, 1 on failure */ static unsigned int -test_proof(const char *regex) +test_proof (const char *regex) { unsigned int error; struct REGEX_INTERNAL_Automaton *dfa; char *c_rx1; const char *c_rx2; - dfa = REGEX_INTERNAL_construct_dfa(regex, strlen(regex), 1); - GNUNET_assert(NULL != dfa); - c_rx1 = GNUNET_strdup(REGEX_INTERNAL_get_canonical_regex(dfa)); - REGEX_INTERNAL_automaton_destroy(dfa); - dfa = REGEX_INTERNAL_construct_dfa(c_rx1, strlen(c_rx1), 1); - GNUNET_assert(NULL != dfa); - c_rx2 = REGEX_INTERNAL_get_canonical_regex(dfa); + dfa = REGEX_INTERNAL_construct_dfa (regex, strlen (regex), 1); + GNUNET_assert (NULL != dfa); + c_rx1 = GNUNET_strdup (REGEX_INTERNAL_get_canonical_regex (dfa)); + REGEX_INTERNAL_automaton_destroy (dfa); + dfa = REGEX_INTERNAL_construct_dfa (c_rx1, strlen (c_rx1), 1); + GNUNET_assert (NULL != dfa); + c_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa); - error = (0 == strcmp(c_rx1, c_rx2)) ? 0 : 1; + error = (0 == strcmp (c_rx1, c_rx2)) ? 0 : 1; if (error > 0) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n", - regex, c_rx1, c_rx2); - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Comparing canonical regex of\n%s\nfailed:\n%s\nvs.\n%s\n", + regex, c_rx1, c_rx2); + } - GNUNET_free(c_rx1); - REGEX_INTERNAL_automaton_destroy(dfa); + GNUNET_free (c_rx1); + REGEX_INTERNAL_automaton_destroy (dfa); return error; } @@ -81,7 +81,7 @@ test_proof(const char *regex) * @return 0 on succes, number of failures otherwise. */ static unsigned int -test_proofs_random(unsigned int count, size_t rx_length) +test_proofs_random (unsigned int count, size_t rx_length) { unsigned int i; char *rand_rx; @@ -90,11 +90,11 @@ test_proofs_random(unsigned int count, size_t rx_length) failures = 0; for (i = 0; i < count; i++) - { - rand_rx = REGEX_TEST_generate_random_regex(rx_length, NULL); - failures += test_proof(rand_rx); - GNUNET_free(rand_rx); - } + { + rand_rx = REGEX_TEST_generate_random_regex (rx_length, NULL); + failures += test_proof (rand_rx); + GNUNET_free (rand_rx); + } return failures; } @@ -106,7 +106,7 @@ test_proofs_random(unsigned int count, size_t rx_length) * @return 0 on success, number of failures otherwise. */ static unsigned int -test_proofs_static() +test_proofs_static () { unsigned int i; unsigned int error; @@ -130,43 +130,44 @@ test_proofs_static() error = 0; for (i = 0; i < 8; i += 2) - { - dfa1 = REGEX_INTERNAL_construct_dfa(regex[i], strlen(regex[i]), 1); - dfa2 = REGEX_INTERNAL_construct_dfa(regex[i + 1], strlen(regex[i + 1]), 1); - GNUNET_assert(NULL != dfa1); - GNUNET_assert(NULL != dfa2); - - canon_rx1 = REGEX_INTERNAL_get_canonical_regex(dfa1); - canon_rx2 = REGEX_INTERNAL_get_canonical_regex(dfa2); + { + dfa1 = REGEX_INTERNAL_construct_dfa (regex[i], strlen (regex[i]), 1); + dfa2 = REGEX_INTERNAL_construct_dfa (regex[i + 1], strlen (regex[i + 1]), + 1); + GNUNET_assert (NULL != dfa1); + GNUNET_assert (NULL != dfa2); - error += (0 == strcmp(canon_rx1, canon_rx2)) ? 0 : 1; + canon_rx1 = REGEX_INTERNAL_get_canonical_regex (dfa1); + canon_rx2 = REGEX_INTERNAL_get_canonical_regex (dfa2); - if (error > 0) - { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, - "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n", - regex[i], canon_rx1, regex[i + 1], canon_rx2); - } + error += (0 == strcmp (canon_rx1, canon_rx2)) ? 0 : 1; - REGEX_INTERNAL_automaton_destroy(dfa1); - REGEX_INTERNAL_automaton_destroy(dfa2); + if (error > 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Comparing canonical regex failed:\nrx1:\t%s\ncrx1:\t%s\nrx2:\t%s\ncrx2:\t%s\n", + regex[i], canon_rx1, regex[i + 1], canon_rx2); } + REGEX_INTERNAL_automaton_destroy (dfa1); + REGEX_INTERNAL_automaton_destroy (dfa2); + } + return error; } int -main(int argc, char *argv[]) +main (int argc, char *argv[]) { - GNUNET_log_setup("test-regex", "WARNING", NULL); + GNUNET_log_setup ("test-regex", "WARNING", NULL); int error; error = 0; - error += test_proofs_static(); - error += test_proofs_random(100, 30); + error += test_proofs_static (); + error += test_proofs_random (100, 30); return error; } -- cgit v1.2.3