From: Maximilian Szengel Date: Tue, 3 Apr 2012 13:46:35 +0000 (+0000) Subject: fix X-Git-Tag: initial-import-from-subversion-38251~13999 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=d7c871cc85896f2603e69fae17161b326193cc0d;p=oweals%2Fgnunet.git fix --- diff --git a/src/include/gnunet_regex_lib.h b/src/include/gnunet_regex_lib.h index 1138ad4ca..bd458478b 100644 --- a/src/include/gnunet_regex_lib.h +++ b/src/include/gnunet_regex_lib.h @@ -89,7 +89,7 @@ GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a, * @param a automaton * @param string string to check * - * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise + * @return 0 if string matches, non 0 otherwise */ int GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am index 729773399..7b9d31095 100644 --- a/src/regex/Makefile.am +++ b/src/regex/Makefile.am @@ -11,7 +11,7 @@ endif lib_LTLIBRARIES = libgnunetregex.la libgnunetregex_la_SOURCES = \ - regex.c regex.h + regex.c libgnunetregex_la_LIBADD = -lm \ $(top_builddir)/src/util/libgnunetutil.la libgnunetregex_la_LDFLAGS = \ diff --git a/src/regex/regex.c b/src/regex/regex.c index a524ace29..6432075a5 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c @@ -721,7 +721,10 @@ nfa_closure_set_create (struct StateSet *states, const char literal) for (k = 0; k < cls->len; k++) { if (sset->states[j]->id == cls->states[k]->id) + { contains = 1; + break; + } } if (!contains) GNUNET_array_append (cls->states, cls->len, sset->states[j]); @@ -1249,7 +1252,7 @@ GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a, * @param a automaton, type must be DFA * @param string string that should be evaluated * - * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR otherwise + * @return 0 if string matches, non 0 otherwise */ static int evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string) @@ -1261,7 +1264,7 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to evaluate DFA, but NFA automaton given"); - return GNUNET_SYSERR; + return -1; } s = a->start; @@ -1274,9 +1277,9 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string) } if (NULL != s && s->accepting) - return GNUNET_YES; + return 0; - return GNUNET_NO; + return 1; } /** @@ -1285,7 +1288,7 @@ evaluate_dfa (struct GNUNET_REGEX_Automaton *a, const char *string) * @param a automaton, type must be NFA * @param string string that should be evaluated * - * @return GNUNET_YES if string matches, GNUNET_NO if not, GNUNET_SYSERR otherwise + * @return 0 if string matches, non 0 otherwise */ static int evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string) @@ -1301,13 +1304,12 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to evaluate NFA, but DFA automaton given"); - return GNUNET_SYSERR; + return -1; } - result = GNUNET_NO; + result = 1; strp = string; - sset = GNUNET_malloc (sizeof (struct StateSet)); - GNUNET_array_append (sset->states, sset->len, a->start); + sset = nfa_closure_create (a->start, 0); for (strp = string; NULL != strp && *strp; strp++) { @@ -1322,7 +1324,7 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string) s = sset->states[i]; if (NULL != s && s->accepting) { - result = GNUNET_YES; + result = 0; break; } } @@ -1338,7 +1340,7 @@ evaluate_nfa (struct GNUNET_REGEX_Automaton *a, const char *string) * @param a automaton * @param string string to check * - * @return GNUNET_YES if 'a' matches 'string', GNUNET_NO otherwise + * @return 0 if string matches, non 0 otherwise */ int GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string) diff --git a/src/regex/regex.h b/src/regex/regex.h deleted file mode 100644 index 4f8c3853b..000000000 --- a/src/regex/regex.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - This file is part of GNUnet - (C) 2012 Christian Grothoff (and other contributing authors) - - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. -*/ -/** - * @file src/regex/regex.h - * @brief library to create automatons from regular expressions - * @author Maximilian Szengel - */ -#ifndef REGEX_H -#define REGEX_H -#include "gnunet_regex_lib.h" - -#endif diff --git a/src/regex/test_regex.c b/src/regex/test_regex.c index 6835d1b83..7428c001e 100644 --- a/src/regex/test_regex.c +++ b/src/regex/test_regex.c @@ -22,10 +22,60 @@ * @brief test for regex.c * @author Maximilian Szengel */ +#include + #include "platform.h" #include "gnunet_regex_lib.h" -static int err = 0; +enum Match_Result +{ + match = 0, + nomatch = 1 +}; + +struct Regex_String_Pair +{ + char *regex; + char *string; + enum Match_Result expected_result; +}; + + +int +test_automaton (struct GNUNET_REGEX_Automaton *a, struct Regex_String_Pair *rxstr) +{ + regex_t rx; + int result; + int eval; + int eval_check; + + if (NULL == a) + return 1; + + result = 0; + + eval = GNUNET_REGEX_eval (a, rxstr->string); + regcomp (&rx, rxstr->regex, REG_EXTENDED); + eval_check = regexec (&rx, rxstr->string, 0, NULL, 0); + + if ((rxstr->expected_result == match + && (0 != eval || 0 != eval_check)) + || + (rxstr->expected_result == nomatch + && (0 == eval || 0 == eval_check))) + { + result = 1; + char error[200]; + regerror (eval_check, &rx, error, sizeof error); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected result:\nregex: %s\nstring: %s\nexpected result: %i\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n", + rxstr->regex, rxstr->string, rxstr->expected_result, eval, eval_check, error); + } + + regfree (&rx); + + return result; +} int main (int argc, char *argv[]) @@ -38,47 +88,36 @@ main (int argc, char *argv[]) #endif NULL); - struct GNUNET_REGEX_Automaton *nfa; - struct GNUNET_REGEX_Automaton *dfa; - char *regex; - char *string; - int eval; + int check_nfa; + int check_dfa; + struct Regex_String_Pair rxstr[3]; + struct GNUNET_REGEX_Automaton *a; + int i; - nfa = NULL; - dfa = NULL; + rxstr[0].regex = "ab(c|d)+c*(a(b|c)d)+"; + rxstr[0].string = "abcdcdcdcdddddabd"; + rxstr[0].expected_result = match; - regex = "a\\*b(c|d)+c*(a(b|c)d)+"; - string = "a*bcdcdcdcdddddabd"; - /*regex = "VPN TCP (IPv4|IPv6) Port53"; */ - /*string = "VPN TCP IPv4 Port53"; */ - /*regex = "\\*a(a|b)b"; */ - /*regex = "a(a|b)c"; */ - /*regex = "(a|aa)+"; */ - nfa = GNUNET_REGEX_construct_nfa (regex, strlen (regex)); + rxstr[1].regex = "a*"; + rxstr[1].string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + rxstr[1].expected_result = match; - if (nfa) - { - GNUNET_REGEX_automaton_save_graph (nfa, "nfa_graph.dot"); - eval = GNUNET_REGEX_eval (nfa, string); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating %s result: %i\n", string, - eval); - if (GNUNET_YES != eval) - err = 1; - GNUNET_REGEX_automaton_destroy (nfa); - } - else - err = 1; + rxstr[2].regex = "a*b*c*d+"; + rxstr[2].string = "a"; + rxstr[2].expected_result = nomatch; - dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex)); - if (dfa) + for (i=0; i<3; i++) { - GNUNET_REGEX_automaton_save_graph (dfa, "dfa_graph.dot"); - eval = GNUNET_REGEX_eval (dfa, string); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Evaluating %s result: %i\n", string, - eval); - if (GNUNET_YES != eval) - err = 1; - GNUNET_REGEX_automaton_destroy (dfa); + // NFA test + a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex)); + check_nfa += test_automaton (a, &rxstr[i]); + GNUNET_REGEX_automaton_destroy (a); + + // DFA test + a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex)); + check_dfa += test_automaton (a, &rxstr[i]); + GNUNET_REGEX_automaton_destroy (a); } - return err; + + return check_nfa + check_dfa; }