diff options
author | Bart Polot <bart@net.in.tum.de> | 2012-12-17 12:35:19 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2012-12-17 12:35:19 +0000 |
commit | 11cc044427e734c83af65ee340dd10ec70c97b1c (patch) | |
tree | 2c807dc234644519ff2f341cf6ca2f1cb2a084f2 /src | |
parent | 07ce0aaf09b4bcdf37b1524a9a497d4a185aba91 (diff) | |
download | gnunet-11cc044427e734c83af65ee340dd10ec70c97b1c.tar.gz gnunet-11cc044427e734c83af65ee340dd10ec70c97b1c.zip |
- move regex_from_file and combine to separate lib file
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_regex_lib.h | 10 | ||||
-rw-r--r-- | src/regex/Makefile.am | 14 | ||||
-rw-r--r-- | src/regex/perf-regex.c | 50 | ||||
-rw-r--r-- | src/regex/regex.c | 190 | ||||
-rw-r--r-- | src/regex/regex_test_lib.c | 248 | ||||
-rw-r--r-- | src/regex/regex_test_lib.h | 80 |
6 files changed, 375 insertions, 217 deletions
diff --git a/src/include/gnunet_regex_lib.h b/src/include/gnunet_regex_lib.h index 783e69dff..105ccee54 100644 --- a/src/include/gnunet_regex_lib.h +++ b/src/include/gnunet_regex_lib.h | |||
@@ -249,16 +249,6 @@ GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, | |||
249 | unsigned int prefixlen, char *rxstr); | 249 | unsigned int prefixlen, char *rxstr); |
250 | 250 | ||
251 | 251 | ||
252 | /** | ||
253 | * Combine an array of regexes into a single prefix-shared regex. | ||
254 | * | ||
255 | * @param regexes A NULL-terminated array of regexes. | ||
256 | * | ||
257 | * @retrun A string with a single regex that matches any of the original regexes | ||
258 | */ | ||
259 | char * | ||
260 | GNUNET_REGEX_combine(char * const regexes[]); | ||
261 | |||
262 | 252 | ||
263 | #if 0 /* keep Emacsens' auto-indent happy */ | 253 | #if 0 /* keep Emacsens' auto-indent happy */ |
264 | { | 254 | { |
diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am index c0fc0fd54..8440e7ae3 100644 --- a/src/regex/Makefile.am +++ b/src/regex/Makefile.am | |||
@@ -33,6 +33,17 @@ gnunet_regex_simulation_profiler_DEPENDENCIES = \ | |||
33 | libgnunetregex.la | 33 | libgnunetregex.la |
34 | endif | 34 | endif |
35 | 35 | ||
36 | noinst_LTLIBRARIES = libgnunetregextest.la | ||
37 | |||
38 | libgnunetregextest_la_SOURCES = \ | ||
39 | regex_test_lib.c regex_test_lib.h | ||
40 | libgnunetregextest_la_LIBADD = \ | ||
41 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
42 | $(top_builddir)/src/regex/libgnunetregex.la | ||
43 | libgnunetregextest_la_DEPENDENCIES = \ | ||
44 | libgnunetregex.la | ||
45 | |||
46 | |||
36 | noinst_PROGRAMS = $(noinst_mysql_progs) \ | 47 | noinst_PROGRAMS = $(noinst_mysql_progs) \ |
37 | perf-regex | 48 | perf-regex |
38 | 49 | ||
@@ -40,7 +51,8 @@ perf_regex_SOURCES = \ | |||
40 | perf-regex.c | 51 | perf-regex.c |
41 | perf_regex_LDADD = \ | 52 | perf_regex_LDADD = \ |
42 | $(top_builddir)/src/util/libgnunetutil.la \ | 53 | $(top_builddir)/src/util/libgnunetutil.la \ |
43 | $(top_builddir)/src/regex/libgnunetregex.la | 54 | $(top_builddir)/src/regex/libgnunetregex.la \ |
55 | $(top_builddir)/src/regex/libgnunetregextest.la | ||
44 | perf_regex_DEPENDENCIES = \ | 56 | perf_regex_DEPENDENCIES = \ |
45 | libgnunetregex.la | 57 | libgnunetregex.la |
46 | 58 | ||
diff --git a/src/regex/perf-regex.c b/src/regex/perf-regex.c index e0ac7b3d2..f1d21f564 100644 --- a/src/regex/perf-regex.c +++ b/src/regex/perf-regex.c | |||
@@ -19,7 +19,7 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file regex/prof-regex.c | 22 | * @file src/regex/prof-regex.c |
23 | * @brief Test how long it takes to create a automaton from a string regex. | 23 | * @brief Test how long it takes to create a automaton from a string regex. |
24 | * @author Bartlomiej Polot | 24 | * @author Bartlomiej Polot |
25 | */ | 25 | */ |
@@ -27,6 +27,7 @@ | |||
27 | #include <time.h> | 27 | #include <time.h> |
28 | #include "platform.h" | 28 | #include "platform.h" |
29 | #include "gnunet_regex_lib.h" | 29 | #include "gnunet_regex_lib.h" |
30 | #include "regex_test_lib.h" | ||
30 | 31 | ||
31 | static const char *exe; | 32 | static const char *exe; |
32 | 33 | ||
@@ -54,8 +55,6 @@ main (int argc, char *const *argv) | |||
54 | unsigned int i; | 55 | unsigned int i; |
55 | int compression; | 56 | int compression; |
56 | long size; | 57 | long size; |
57 | size_t len; | ||
58 | FILE *f; | ||
59 | 58 | ||
60 | GNUNET_log_setup ("perf-regex", "DEBUG", NULL); | 59 | GNUNET_log_setup ("perf-regex", "DEBUG", NULL); |
61 | exe = argv[0]; | 60 | exe = argv[0]; |
@@ -64,50 +63,7 @@ main (int argc, char *const *argv) | |||
64 | usage(); | 63 | usage(); |
65 | return 1; | 64 | return 1; |
66 | } | 65 | } |
67 | f = fopen (argv[1], "r"); | 66 | regexes = GNUNET_REGEX_read_from_file (argv[1]); |
68 | if (NULL == f) | ||
69 | { | ||
70 | fprintf (stderr, "Can't open file %s\n", argv[1]); | ||
71 | usage(); | ||
72 | return 2; | ||
73 | } | ||
74 | fseek (f, 0, SEEK_END); | ||
75 | size = ftell (f); | ||
76 | fprintf (stderr, "using file %s, size %ld\n", argv[1], size); | ||
77 | fseek (f, 0, SEEK_SET); | ||
78 | buffer = GNUNET_malloc (size + 1); | ||
79 | regexes = GNUNET_malloc (sizeof (char *)); | ||
80 | nr = 1; | ||
81 | do | ||
82 | { | ||
83 | if (NULL == fgets (buffer, size + 1, f)) | ||
84 | { | ||
85 | fprintf (stderr, "Can't read file %s\n", argv[1]); | ||
86 | usage(); | ||
87 | return 3; | ||
88 | } | ||
89 | len = strlen (buffer); | ||
90 | if (len < 1) | ||
91 | continue; | ||
92 | if ('\n' == buffer[len - 1]) | ||
93 | { | ||
94 | len--; | ||
95 | buffer[len] = '\0'; | ||
96 | } | ||
97 | if (len < 6 || strncmp (&buffer[len - 6], "(0|1)*", 6) != 0) | ||
98 | { | ||
99 | fprintf (stderr, "\nWARNING:\n"); | ||
100 | fprintf (stderr, "%s (line %u) does not end in (0|1)*\n", buffer, nr); | ||
101 | } | ||
102 | else | ||
103 | { | ||
104 | buffer[len - 6] = '\0'; | ||
105 | } | ||
106 | GNUNET_array_grow (regexes, nr, nr+1); | ||
107 | regexes[nr - 2] = GNUNET_strdup (buffer); | ||
108 | regexes[nr - 1] = NULL; | ||
109 | } while (ftell(f) < size); | ||
110 | GNUNET_free (buffer); | ||
111 | 67 | ||
112 | buffer = GNUNET_REGEX_combine (regexes); | 68 | buffer = GNUNET_REGEX_combine (regexes); |
113 | 69 | ||
diff --git a/src/regex/regex.c b/src/regex/regex.c index 0a2671fa0..92943391a 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c | |||
@@ -3479,7 +3479,6 @@ GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a, | |||
3479 | NULL, a->start, iterator, iterator_cls); | 3479 | NULL, a->start, iterator, iterator_cls); |
3480 | } | 3480 | } |
3481 | 3481 | ||
3482 | |||
3483 | /** | 3482 | /** |
3484 | * Create a string with binary IP notation for the given 'addr' in 'str'. | 3483 | * Create a string with binary IP notation for the given 'addr' in 'str'. |
3485 | * | 3484 | * |
@@ -3493,37 +3492,37 @@ static void | |||
3493 | iptobinstr (const int af, const void *addr, char *str) | 3492 | iptobinstr (const int af, const void *addr, char *str) |
3494 | { | 3493 | { |
3495 | int i; | 3494 | int i; |
3496 | 3495 | ||
3497 | switch (af) | 3496 | switch (af) |
3498 | { | 3497 | { |
3499 | case AF_INET: | 3498 | case AF_INET: |
3500 | { | ||
3501 | uint32_t b = htonl (((struct in_addr *) addr)->s_addr); | ||
3502 | |||
3503 | str[32] = '\0'; | ||
3504 | str += 31; | ||
3505 | for (i = 31; i >= 0; i--) | ||
3506 | { | 3499 | { |
3507 | *str = (b & 1) + '0'; | 3500 | uint32_t b = htonl (((struct in_addr *) addr)->s_addr); |
3508 | str--; | 3501 | |
3509 | b >>= 1; | 3502 | str[32] = '\0'; |
3503 | str += 31; | ||
3504 | for (i = 31; i >= 0; i--) | ||
3505 | { | ||
3506 | *str = (b & 1) + '0'; | ||
3507 | str--; | ||
3508 | b >>= 1; | ||
3509 | } | ||
3510 | break; | ||
3510 | } | 3511 | } |
3511 | break; | 3512 | case AF_INET6: |
3512 | } | ||
3513 | case AF_INET6: | ||
3514 | { | ||
3515 | struct in6_addr b = *(const struct in6_addr *) addr; | ||
3516 | |||
3517 | str[128] = '\0'; | ||
3518 | str += 127; | ||
3519 | for (i = 127; i >= 0; i--) | ||
3520 | { | 3513 | { |
3521 | *str = (b.s6_addr[i / 8] & 1) + '0'; | 3514 | struct in6_addr b = *(const struct in6_addr *) addr; |
3522 | str--; | 3515 | |
3523 | b.s6_addr[i / 8] >>= 1; | 3516 | str[128] = '\0'; |
3517 | str += 127; | ||
3518 | for (i = 127; i >= 0; i--) | ||
3519 | { | ||
3520 | *str = (b.s6_addr[i / 8] & 1) + '0'; | ||
3521 | str--; | ||
3522 | b.s6_addr[i / 8] >>= 1; | ||
3523 | } | ||
3524 | break; | ||
3524 | } | 3525 | } |
3525 | break; | ||
3526 | } | ||
3527 | } | 3526 | } |
3528 | } | 3527 | } |
3529 | 3528 | ||
@@ -3541,7 +3540,7 @@ ipv4netmasktoprefixlen (const char *netmask) | |||
3541 | struct in_addr a; | 3540 | struct in_addr a; |
3542 | unsigned int len; | 3541 | unsigned int len; |
3543 | uint32_t t; | 3542 | uint32_t t; |
3544 | 3543 | ||
3545 | if (1 != inet_pton (AF_INET, netmask, &a)) | 3544 | if (1 != inet_pton (AF_INET, netmask, &a)) |
3546 | return 0; | 3545 | return 0; |
3547 | len = 32; | 3546 | len = 32; |
@@ -3564,12 +3563,12 @@ GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask, | |||
3564 | char *rxstr) | 3563 | char *rxstr) |
3565 | { | 3564 | { |
3566 | unsigned int pfxlen; | 3565 | unsigned int pfxlen; |
3567 | 3566 | ||
3568 | pfxlen = ipv4netmasktoprefixlen (netmask); | 3567 | pfxlen = ipv4netmasktoprefixlen (netmask); |
3569 | iptobinstr (AF_INET, ip, rxstr); | 3568 | iptobinstr (AF_INET, ip, rxstr); |
3570 | rxstr[pfxlen] = '\0'; | 3569 | rxstr[pfxlen] = '\0'; |
3571 | if (pfxlen < 32) | 3570 | if (pfxlen < 32) |
3572 | strcat (rxstr, "(0|1)+"); | 3571 | strcat (rxstr, "(0|1)+"); |
3573 | } | 3572 | } |
3574 | 3573 | ||
3575 | 3574 | ||
@@ -3587,136 +3586,9 @@ GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen, | |||
3587 | { | 3586 | { |
3588 | iptobinstr (AF_INET6, ipv6, rxstr); | 3587 | iptobinstr (AF_INET6, ipv6, rxstr); |
3589 | rxstr[prefixlen] = '\0'; | 3588 | rxstr[prefixlen] = '\0'; |
3590 | if (prefixlen < 128) | 3589 | if (prefixlen < 128) |
3591 | strcat (rxstr, "(0|1)+"); | 3590 | strcat (rxstr, "(0|1)+"); |
3592 | } | ||
3593 | |||
3594 | |||
3595 | struct RegexCombineCtx { | ||
3596 | struct RegexCombineCtx *next; | ||
3597 | struct RegexCombineCtx *prev; | ||
3598 | |||
3599 | struct RegexCombineCtx *head; | ||
3600 | struct RegexCombineCtx *tail; | ||
3601 | |||
3602 | char *s; | ||
3603 | }; | ||
3604 | |||
3605 | |||
3606 | static char * | ||
3607 | regex_combine (struct RegexCombineCtx *ctx) | ||
3608 | { | ||
3609 | struct RegexCombineCtx *p; | ||
3610 | size_t len; | ||
3611 | char *regex; | ||
3612 | char *tmp; | ||
3613 | char *s; | ||
3614 | |||
3615 | if (NULL != ctx->s) | ||
3616 | GNUNET_asprintf (®ex, "%s(", ctx->s); | ||
3617 | else | ||
3618 | regex = GNUNET_strdup ("("); | ||
3619 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex); | ||
3620 | |||
3621 | for (p = ctx->head; NULL != p; p = p->next) | ||
3622 | { | ||
3623 | s = regex_combine (p); | ||
3624 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); | ||
3625 | GNUNET_free_non_null (s); | ||
3626 | GNUNET_free_non_null (regex); | ||
3627 | regex = tmp; | ||
3628 | } | ||
3629 | len = strlen (regex); | ||
3630 | if (1 == len) | ||
3631 | return GNUNET_strdup (""); | ||
3632 | |||
3633 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex); | ||
3634 | if ('|' == regex[len - 1]) | ||
3635 | regex[len - 1] = ')'; | ||
3636 | if ('(' == regex[len - 1]) | ||
3637 | regex[len - 1] = '\0'; | ||
3638 | |||
3639 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); | ||
3640 | return regex; | ||
3641 | } | ||
3642 | |||
3643 | static void | ||
3644 | regex_add (struct RegexCombineCtx *ctx, const char *regex) | ||
3645 | { | ||
3646 | struct RegexCombineCtx *p; | ||
3647 | const char *rest; | ||
3648 | |||
3649 | rest = ®ex[1]; | ||
3650 | for (p = ctx->head; NULL != p; p = p->next) | ||
3651 | { | ||
3652 | if (p->s[0] == regex[0]) | ||
3653 | { | ||
3654 | if (1 == strlen(p->s)) | ||
3655 | { | ||
3656 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "common char %s\n", p->s); | ||
3657 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding %s\n", rest); | ||
3658 | regex_add (p, rest); | ||
3659 | } | ||
3660 | else | ||
3661 | { | ||
3662 | struct RegexCombineCtx *new; | ||
3663 | new = GNUNET_malloc (sizeof (struct RegexCombineCtx)); | ||
3664 | new->s = GNUNET_strdup (&p->s[1]); | ||
3665 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s); | ||
3666 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s); | ||
3667 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex); | ||
3668 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s); | ||
3669 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest); | ||
3670 | p->s[1] = '\0'; /* dont realloc */ | ||
3671 | GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new); | ||
3672 | regex_add (p, rest); | ||
3673 | } | ||
3674 | return; | ||
3675 | } | ||
3676 | } | ||
3677 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); | ||
3678 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); | ||
3679 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); | ||
3680 | p = GNUNET_malloc (sizeof (struct RegexCombineCtx)); | ||
3681 | p->s = GNUNET_strdup (regex); | ||
3682 | GNUNET_CONTAINER_DLL_insert (ctx->head, ctx->tail, p); | ||
3683 | } | 3591 | } |
3684 | /* | ||
3685 | static void | ||
3686 | debug (struct RegexCombineCtx *ctx, int lvl) | ||
3687 | { | ||
3688 | struct RegexCombineCtx *p; | ||
3689 | unsigned int i; | ||
3690 | |||
3691 | for (i = 0; i < lvl; i++) fprintf (stderr, " "); | ||
3692 | fprintf (stderr, "%s\n", ctx->s); | ||
3693 | |||
3694 | for (p = ctx->head; NULL != p; p = p->next) | ||
3695 | { | ||
3696 | debug (p, lvl + 2); | ||
3697 | } | ||
3698 | }*/ | ||
3699 | 3592 | ||
3700 | char * | ||
3701 | GNUNET_REGEX_combine (char * const regexes[]) | ||
3702 | { | ||
3703 | unsigned int i; | ||
3704 | char *combined; | ||
3705 | const char *current; | ||
3706 | struct RegexCombineCtx ctx; | ||
3707 | |||
3708 | memset (&ctx, 0, sizeof (struct RegexCombineCtx)); | ||
3709 | for (i = 0; regexes[i]; i++) | ||
3710 | { | ||
3711 | current = regexes[i]; | ||
3712 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); | ||
3713 | regex_add (&ctx, current); | ||
3714 | } | ||
3715 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | ||
3716 | |||
3717 | combined = regex_combine (&ctx); | ||
3718 | |||
3719 | return combined; | ||
3720 | } | ||
3721 | 3593 | ||
3722 | /* end of regex.c */ | 3594 | /* end of regex.c */ |
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c new file mode 100644 index 000000000..d02afa942 --- /dev/null +++ b/src/regex/regex_test_lib.c | |||
@@ -0,0 +1,248 @@ | |||
1 | /* | ||
2 | * This file is part of GNUnet | ||
3 | * (C) 2012 Christian Grothoff (and other contributing authors) | ||
4 | * | ||
5 | * GNUnet is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published | ||
7 | * by the Free Software Foundation; either version 3, or (at your | ||
8 | * option) any later version. | ||
9 | * | ||
10 | * GNUnet is distributed in the hope that it will be useful, but | ||
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with GNUnet; see the file COPYING. If not, write to the | ||
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | * Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file src/regex/regex_test_lib.c | ||
22 | * @brief library to read regexes representing IP networks from a file. | ||
23 | * and simplyfinying the into one big regex, in order to run | ||
24 | * tests (regex performance, mesh profiler). | ||
25 | * @author Bartlomiej Polot | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | struct RegexCombineCtx { | ||
32 | struct RegexCombineCtx *next; | ||
33 | struct RegexCombineCtx *prev; | ||
34 | |||
35 | struct RegexCombineCtx *head; | ||
36 | struct RegexCombineCtx *tail; | ||
37 | |||
38 | char *s; | ||
39 | }; | ||
40 | |||
41 | |||
42 | static char * | ||
43 | regex_combine (struct RegexCombineCtx *ctx) | ||
44 | { | ||
45 | struct RegexCombineCtx *p; | ||
46 | size_t len; | ||
47 | char *regex; | ||
48 | char *tmp; | ||
49 | char *s; | ||
50 | |||
51 | if (NULL != ctx->s) | ||
52 | GNUNET_asprintf (®ex, "%s(", ctx->s); | ||
53 | else | ||
54 | regex = GNUNET_strdup ("("); | ||
55 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex); | ||
56 | |||
57 | for (p = ctx->head; NULL != p; p = p->next) | ||
58 | { | ||
59 | s = regex_combine (p); | ||
60 | GNUNET_asprintf (&tmp, "%s%s|", regex, s); | ||
61 | GNUNET_free_non_null (s); | ||
62 | GNUNET_free_non_null (regex); | ||
63 | regex = tmp; | ||
64 | } | ||
65 | len = strlen (regex); | ||
66 | if (1 == len) | ||
67 | return GNUNET_strdup (""); | ||
68 | |||
69 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex); | ||
70 | if ('|' == regex[len - 1]) | ||
71 | regex[len - 1] = ')'; | ||
72 | if ('(' == regex[len - 1]) | ||
73 | regex[len - 1] = '\0'; | ||
74 | |||
75 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); | ||
76 | return regex; | ||
77 | } | ||
78 | |||
79 | static void | ||
80 | regex_add (struct RegexCombineCtx *ctx, const char *regex) | ||
81 | { | ||
82 | struct RegexCombineCtx *p; | ||
83 | const char *rest; | ||
84 | |||
85 | rest = ®ex[1]; | ||
86 | for (p = ctx->head; NULL != p; p = p->next) | ||
87 | { | ||
88 | if (p->s[0] == regex[0]) | ||
89 | { | ||
90 | if (1 == strlen(p->s)) | ||
91 | { | ||
92 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "common char %s\n", p->s); | ||
93 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding %s\n", rest); | ||
94 | regex_add (p, rest); | ||
95 | } | ||
96 | else | ||
97 | { | ||
98 | struct RegexCombineCtx *new; | ||
99 | new = GNUNET_malloc (sizeof (struct RegexCombineCtx)); | ||
100 | new->s = GNUNET_strdup (&p->s[1]); | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s); | ||
102 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s); | ||
103 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex); | ||
104 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s); | ||
105 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest); | ||
106 | p->s[1] = '\0'; /* dont realloc */ | ||
107 | GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new); | ||
108 | regex_add (p, rest); | ||
109 | } | ||
110 | return; | ||
111 | } | ||
112 | } | ||
113 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no match\n"); | ||
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new state %s\n", regex); | ||
115 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " under %s\n", ctx->s); | ||
116 | p = GNUNET_malloc (sizeof (struct RegexCombineCtx)); | ||
117 | p->s = GNUNET_strdup (regex); | ||
118 | GNUNET_CONTAINER_DLL_insert (ctx->head, ctx->tail, p); | ||
119 | } | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Return a prefix-combine regex that matches the same strings as | ||
124 | * any of the original regexes. | ||
125 | * | ||
126 | * WARNING: only useful for reading specific regexes for specific applications, | ||
127 | * namely the gnunet-regex-profiler / gnunet-regex-daemon. | ||
128 | * This function DOES NOT support arbitrary regex combining. | ||
129 | */ | ||
130 | char * | ||
131 | GNUNET_REGEX_combine (char * const regexes[]) | ||
132 | { | ||
133 | unsigned int i; | ||
134 | char *combined; | ||
135 | const char *current; | ||
136 | struct RegexCombineCtx ctx; | ||
137 | |||
138 | memset (&ctx, 0, sizeof (struct RegexCombineCtx)); | ||
139 | for (i = 0; regexes[i]; i++) | ||
140 | { | ||
141 | current = regexes[i]; | ||
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); | ||
143 | regex_add (&ctx, current); | ||
144 | } | ||
145 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | ||
146 | |||
147 | combined = regex_combine (&ctx); | ||
148 | |||
149 | return combined; | ||
150 | } | ||
151 | |||
152 | |||
153 | /** | ||
154 | * Read a set of regexes from a file, one per line and return them in an array | ||
155 | * suitable for GNUNET_REGEX_combine. | ||
156 | * The array must be free'd using GNUNET_REGEX_free_from_file. | ||
157 | * | ||
158 | * @param filename Name of the file containing the regexes. | ||
159 | * | ||
160 | * @return A newly allocated, NULL terminated array of regexes. | ||
161 | */ | ||
162 | char ** | ||
163 | GNUNET_REGEX_read_from_file (const char *filename) | ||
164 | { | ||
165 | struct GNUNET_DISK_FileHandle *f; | ||
166 | unsigned int nr; | ||
167 | unsigned int offset; | ||
168 | off_t size; | ||
169 | size_t len; | ||
170 | char *buffer; | ||
171 | char *regex; | ||
172 | char **regexes; | ||
173 | |||
174 | f = GNUNET_DISK_file_open (filename, | ||
175 | GNUNET_DISK_OPEN_READ, | ||
176 | GNUNET_DISK_PERM_NONE); | ||
177 | if (NULL == f) | ||
178 | { | ||
179 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
180 | "Can't open file %s for reading\n", filename); | ||
181 | return NULL; | ||
182 | } | ||
183 | if (GNUNET_OK != GNUNET_DISK_file_handle_size (f, &size)) | ||
184 | { | ||
185 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
186 | "Can't get size of file %s\n", filename); | ||
187 | return NULL; | ||
188 | } | ||
189 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
190 | "using file %s, size %llu\n", | ||
191 | filename, (unsigned long long) size); | ||
192 | |||
193 | buffer = GNUNET_malloc (size + 1); | ||
194 | GNUNET_DISK_file_read (f, buffer, size); | ||
195 | GNUNET_DISK_file_close (f); | ||
196 | regexes = GNUNET_malloc (sizeof (char *)); | ||
197 | nr = 1; | ||
198 | offset = 0; | ||
199 | regex = NULL; | ||
200 | do | ||
201 | { | ||
202 | if (NULL == regex) | ||
203 | regex = GNUNET_malloc (size + 1); | ||
204 | len = (size_t) sscanf (&buffer[offset], "%s", regex); | ||
205 | if (0 == len) | ||
206 | break; | ||
207 | len = strlen (regex); | ||
208 | offset += len + 1; | ||
209 | if (len < 1) | ||
210 | continue; | ||
211 | if (len < 6 || strncmp (®ex[len - 6], "(0|1)*", 6) != 0) | ||
212 | { | ||
213 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
214 | "%s (line %u) does not end in \"(0|1)*\"\n", | ||
215 | buffer, nr); | ||
216 | } | ||
217 | else | ||
218 | { | ||
219 | len -= 6; | ||
220 | buffer[len] = '\0'; | ||
221 | } | ||
222 | regex = GNUNET_realloc (regex, len + 1); | ||
223 | GNUNET_array_grow (regexes, nr, nr + 1); | ||
224 | regexes[nr - 2] = regex; | ||
225 | regexes[nr - 1] = NULL; | ||
226 | } while (offset < size); | ||
227 | GNUNET_free (buffer); | ||
228 | |||
229 | return regexes; | ||
230 | } | ||
231 | |||
232 | |||
233 | /** | ||
234 | * Free all memory reserved for a set of regexes created by read_from_file. | ||
235 | * | ||
236 | * @param regexes NULL-terminated array of regexes. | ||
237 | */ | ||
238 | void | ||
239 | GNUNET_REGEX_free_from_file (char **regexes) | ||
240 | { | ||
241 | unsigned int i; | ||
242 | |||
243 | for (i = 0; regexes[i]; i++) | ||
244 | GNUNET_free (regexes[i]); | ||
245 | GNUNET_free (regexes); | ||
246 | } | ||
247 | |||
248 | /* end of regex_test_lib.c */ \ No newline at end of file | ||
diff --git a/src/regex/regex_test_lib.h b/src/regex/regex_test_lib.h new file mode 100644 index 000000000..b21af2ebb --- /dev/null +++ b/src/regex/regex_test_lib.h | |||
@@ -0,0 +1,80 @@ | |||
1 | /* | ||
2 | * This file is part of GNUnet | ||
3 | * (C) 2012 Christian Grothoff (and other contributing authors) | ||
4 | * | ||
5 | * GNUnet is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published | ||
7 | * by the Free Software Foundation; either version 3, or (at your | ||
8 | * option) any later version. | ||
9 | * | ||
10 | * GNUnet is distributed in the hope that it will be useful, but | ||
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with GNUnet; see the file COPYING. If not, write to the | ||
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | * Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file src/regex/regex_test_lib.h | ||
22 | * @brief library to read regexes representing IP networks from a file. | ||
23 | * and simplyfinying the into one big regex, in order to run | ||
24 | * tests (regex performance, mesh profiler). | ||
25 | * @author Bertlomiej Polot | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | #ifndef GNUNET_REGEX_TEST_LIB_H | ||
30 | #define GNUNET_REGEX_TEST_LIB_H | ||
31 | |||
32 | |||
33 | #ifdef __cplusplus | ||
34 | extern "C" | ||
35 | { | ||
36 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
37 | } | ||
38 | #endif | ||
39 | #endif | ||
40 | |||
41 | /** | ||
42 | * Combine an array of regexes into a single prefix-shared regex. | ||
43 | * | ||
44 | * @param regexes A NULL-terminated array of regexes. | ||
45 | * | ||
46 | * @retrun A string with a single regex that matches any of the original regexes | ||
47 | */ | ||
48 | char * | ||
49 | GNUNET_REGEX_combine(char * const regexes[]); | ||
50 | |||
51 | /** | ||
52 | * Read a set of regexes from a file, one per line and return them in an array | ||
53 | * suitable for GNUNET_REGEX_combine. | ||
54 | * The array must be free'd using GNUNET_REGEX_free_from_file. | ||
55 | * | ||
56 | * @param filename Name of the file containing the regexes. | ||
57 | * | ||
58 | * @return A newly allocated, NULL terminated array of regexes. | ||
59 | */ | ||
60 | char ** | ||
61 | GNUNET_REGEX_read_from_file (const char *filename); | ||
62 | |||
63 | |||
64 | /** | ||
65 | * Free all memory reserved for a set of regexes created by read_from_file. | ||
66 | * | ||
67 | * @param regexes NULL-terminated array of regexes. | ||
68 | */ | ||
69 | void | ||
70 | GNUNET_REGEX_free_from_file (char **regexes); | ||
71 | |||
72 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
73 | { | ||
74 | #endif | ||
75 | #ifdef __cplusplus | ||
76 | } | ||
77 | #endif | ||
78 | |||
79 | /* end of gnunet_regex_lib.h */ | ||
80 | #endif \ No newline at end of file | ||