From c6c6d70ac090acbc8b9c75f19af249030e2a751b Mon Sep 17 00:00:00 2001 From: Maximilian Szengel Date: Wed, 4 Jul 2012 15:46:47 +0000 Subject: [PATCH] regex: actually checking the proof, fixes --- src/include/gnunet_regex_lib.h | 12 +++--- src/regex/regex.c | 21 +++++----- src/regex/test_regex_iterate_api.c | 64 ++++++++++++++---------------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/include/gnunet_regex_lib.h b/src/include/gnunet_regex_lib.h index 911128647..82a29ec6a 100644 --- a/src/include/gnunet_regex_lib.h +++ b/src/include/gnunet_regex_lib.h @@ -119,7 +119,7 @@ GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, /** * Get the first key for the given 'input_string'. This hashes - * the first x bits of the 'input_strings'. + * the first x bits of the 'input_string'. * * @param input_string string. * @param string_len length of the 'input_string'. @@ -128,18 +128,18 @@ GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, * @return number of bits of 'input_string' that have been consumed * to construct the key */ -unsigned int /* FIXME: size_t */ -GNUNET_REGEX_get_first_key (const char *input_string, /* FIXME: size_t */ unsigned int string_len, +size_t +GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len, struct GNUNET_HashCode * key); /** * Check if the given 'proof' matches the given 'key'. * - * @param proof partial regex - * @param key hash + * @param proof partial regex of a state. + * @param key hash of a state. * - * @return GNUNET_OK if the proof is valid for the given key + * @return GNUNET_OK if the proof is valid for the given key. */ int GNUNET_REGEX_check_proof (const char *proof, diff --git a/src/regex/regex.c b/src/regex/regex.c index a1619ef28..1290569cc 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c @@ -287,7 +287,8 @@ state_compare (const void *a, const void *b) * Get all edges leaving state 's'. * * @param s state. - * @param edges all edges leaving 's'. + * @param edges all edges leaving 's', expected to be allocated and have enough + * space for s->transitions_count elements. * * @return number of edges. */ @@ -2471,7 +2472,7 @@ GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a) /** * Get the first key for the given 'input_string'. This hashes the first x bits - * of the 'input_strings'. + * of the 'input_string'. * * @param input_string string. * @param string_len length of the 'input_string'. @@ -2480,9 +2481,9 @@ GNUNET_REGEX_get_canonical_regex (struct GNUNET_REGEX_Automaton *a) * @return number of bits of 'input_string' that have been consumed * to construct the key */ -unsigned int -GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len, - struct GNUNET_HashCode *key) +size_t +GNUNET_REGEX_get_first_key (const char *input_string, size_t string_len, + struct GNUNET_HashCode * key) { unsigned int size; @@ -2503,15 +2504,17 @@ GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len, /** * Check if the given 'proof' matches the given 'key'. * - * @param proof partial regex - * @param key hash + * @param proof partial regex of a state. + * @param key hash of a state. * - * @return GNUNET_OK if the proof is valid for the given key + * @return GNUNET_OK if the proof is valid for the given key. */ int GNUNET_REGEX_check_proof (const char *proof, const struct GNUNET_HashCode *key) { - return GNUNET_OK; + struct GNUNET_HashCode key_check; + GNUNET_CRYPTO_hash (proof, strlen (proof), &key_check); + return (0 == GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO; } diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c index 9f4f2280e..246ae7767 100644 --- a/src/regex/test_regex_iterate_api.c +++ b/src/regex/test_regex_iterate_api.c @@ -33,6 +33,7 @@ key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof, const struct GNUNET_REGEX_Edge *edges) { unsigned int i; + int *error = cls; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating... (accepting: %i)\n", accepting); @@ -41,6 +42,8 @@ key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Edge %i: %s\n", i, edges[i].label); } + *error += (GNUNET_OK == GNUNET_REGEX_check_proof (proof, key)) ? 0 : 1; + if (NULL != proof) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof: %s\n", proof); } @@ -57,44 +60,37 @@ main (int argc, char *argv[]) NULL); int error; - const char *regex; + int i; struct GNUNET_REGEX_Automaton *dfa; error = 0; - /* regex = "ab(c|d)+c*(a(b|c)+d)+(bla)+"; */ - /* regex = "(bla)*"; */ - /*regex = "b(lab)*la"; */ - /* regex = "(ab)*"; */ - regex = "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*"; - /*regex = "z(abc|def)?xyz"; */ - /* regex = "1*0(0|1)*"; */ - /* regex = "a*b*"; */ - /* regex = "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*"; */ - /* regex = "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)"; */ - /* regex = "abc(1|0)*def"; */ - /* regex = "ab|ac"; */ - /* regex = "(ab)(ab)*"; */ - /* regex = "ab|cd|ef|gh"; */ - /* regex = "a|b|c|d|e|f|g"; */ - /* regex = "(ab)|(ac)"; */ - /* regex = "a(b|c)"; */ - /* regex = "a*a"; */ - /* regex = "ab?(abcd)?"; */ - /* regex = "(ab)+"; */ - /* regex = "(ab|cs|df|sdf)*"; */ - /* regex = "(ab|cd)*"; */ - /* regex = "(cd|ab)*"; */ - /* regex = "(ab|c)+"; */ - /* regex = "(a|bc)+"; */ - /* regex = "(ab|c)(ab|c)*"; */ - /* regex = "(a|bc)(a|bc)*"; */ - /* regex = "(ac|b)*"; */ - /* regex = "a|aa*a"; */ - dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex)); - GNUNET_REGEX_automaton_save_graph (dfa, "dfa.dot"); - GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, NULL); - GNUNET_REGEX_automaton_destroy (dfa); + const char *regex[17] = { + "ab(c|d)+c*(a(b|c)+d)+(bla)+", + "(bla)*", + "b(lab)*la", + "(ab)*", + "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", + "z(abc|def)?xyz", + "1*0(0|1)*", + "a*b*", + "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", + "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)", + "abc(1|0)*def", + "ab|ac", + "(ab)(ab)*", + "ab|cd|ef|gh", + "a|b|c|d|e|f|g", + "(ab)|(ac)", + "a(b|c)" + }; + for (i = 0; i < 17; i++) + { + dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i])); + GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &error); + GNUNET_REGEX_automaton_destroy (dfa); + } + return error; } -- 2.25.1