diff options
author | bartpolot <bartpolot@140774ce-b5e7-0310-ab8b-a85725594a96> | 2012-12-17 12:35:19 +0000 |
---|---|---|
committer | bartpolot <bartpolot@140774ce-b5e7-0310-ab8b-a85725594a96> | 2012-12-17 12:35:19 +0000 |
commit | 9695ca49ea8b70ab57bed1346085539845b42f9e (patch) | |
tree | 2c807dc234644519ff2f341cf6ca2f1cb2a084f2 /src/regex/regex.c | |
parent | 3b2a312bb9466ea90f2b39d9136df0fb26a6e5e6 (diff) |
- move regex_from_file and combine to separate lib file
git-svn-id: https://gnunet.org/svn/gnunet@25508 140774ce-b5e7-0310-ab8b-a85725594a96
Diffstat (limited to 'src/regex/regex.c')
-rw-r--r-- | src/regex/regex.c | 190 |
1 files changed, 31 insertions, 159 deletions
diff --git a/src/regex/regex.c b/src/regex/regex.c index 0a2671fa0f..92943391a9 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, NULL, a->start, iterator, iterator_cls); } - /** * Create a string with binary IP notation for the given 'addr' in 'str'. * @@ -3493,37 +3492,37 @@ static void iptobinstr (const int af, const void *addr, char *str) { int i; - + switch (af) { - case AF_INET: - { - uint32_t b = htonl (((struct in_addr *) addr)->s_addr); - - str[32] = '\0'; - str += 31; - for (i = 31; i >= 0; i--) + case AF_INET: { - *str = (b & 1) + '0'; - str--; - b >>= 1; + uint32_t b = htonl (((struct in_addr *) addr)->s_addr); + + str[32] = '\0'; + str += 31; + for (i = 31; i >= 0; i--) + { + *str = (b & 1) + '0'; + str--; + b >>= 1; + } + break; } - break; - } - case AF_INET6: - { - struct in6_addr b = *(const struct in6_addr *) addr; - - str[128] = '\0'; - str += 127; - for (i = 127; i >= 0; i--) + case AF_INET6: { - *str = (b.s6_addr[i / 8] & 1) + '0'; - str--; - b.s6_addr[i / 8] >>= 1; + struct in6_addr b = *(const struct in6_addr *) addr; + + str[128] = '\0'; + str += 127; + for (i = 127; i >= 0; i--) + { + *str = (b.s6_addr[i / 8] & 1) + '0'; + str--; + b.s6_addr[i / 8] >>= 1; + } + break; } - break; - } } } @@ -3541,7 +3540,7 @@ ipv4netmasktoprefixlen (const char *netmask) struct in_addr a; unsigned int len; uint32_t t; - + if (1 != inet_pton (AF_INET, netmask, &a)) return 0; len = 32; @@ -3564,12 +3563,12 @@ GNUNET_REGEX_ipv4toregex (const struct in_addr *ip, const char *netmask, char *rxstr) { unsigned int pfxlen; - + pfxlen = ipv4netmasktoprefixlen (netmask); iptobinstr (AF_INET, ip, rxstr); rxstr[pfxlen] = '\0'; - if (pfxlen < 32) - strcat (rxstr, "(0|1)+"); + if (pfxlen < 32) + strcat (rxstr, "(0|1)+"); } @@ -3587,136 +3586,9 @@ GNUNET_REGEX_ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen, { iptobinstr (AF_INET6, ipv6, rxstr); rxstr[prefixlen] = '\0'; - if (prefixlen < 128) - strcat (rxstr, "(0|1)+"); -} - - -struct RegexCombineCtx { - struct RegexCombineCtx *next; - struct RegexCombineCtx *prev; - - struct RegexCombineCtx *head; - struct RegexCombineCtx *tail; - - char *s; -}; - - -static char * -regex_combine (struct RegexCombineCtx *ctx) -{ - struct RegexCombineCtx *p; - size_t len; - char *regex; - char *tmp; - char *s; - - if (NULL != ctx->s) - GNUNET_asprintf (®ex, "%s(", ctx->s); - else - regex = GNUNET_strdup ("("); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "prefix: %s\n", regex); - - for (p = ctx->head; NULL != p; p = p->next) - { - s = regex_combine (p); - GNUNET_asprintf (&tmp, "%s%s|", regex, s); - GNUNET_free_non_null (s); - GNUNET_free_non_null (regex); - regex = tmp; - } - len = strlen (regex); - if (1 == len) - return GNUNET_strdup (""); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-partial: %s\n", regex); - if ('|' == regex[len - 1]) - regex[len - 1] = ')'; - if ('(' == regex[len - 1]) - regex[len - 1] = '\0'; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "partial: %s\n", regex); - return regex; -} - -static void -regex_add (struct RegexCombineCtx *ctx, const char *regex) -{ - struct RegexCombineCtx *p; - const char *rest; - - rest = ®ex[1]; - for (p = ctx->head; NULL != p; p = p->next) - { - if (p->s[0] == regex[0]) - { - if (1 == strlen(p->s)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "common char %s\n", p->s); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding %s\n", rest); - regex_add (p, rest); - } - else - { - struct RegexCombineCtx *new; - new = GNUNET_malloc (sizeof (struct RegexCombineCtx)); - new->s = GNUNET_strdup (&p->s[1]); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p has now %s\n", p->s); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " p will have %.1s\n", p->s); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex is %s\n", regex); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " new has now %s\n", new->s); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " rest is now %s\n", rest); - p->s[1] = '\0'; /* dont realloc */ - GNUNET_CONTAINER_DLL_insert (p->head, p->tail, new); - regex_add (p, rest); - } - return; - } - } - 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); - p = GNUNET_malloc (sizeof (struct RegexCombineCtx)); - p->s = GNUNET_strdup (regex); - GNUNET_CONTAINER_DLL_insert (ctx->head, ctx->tail, p); + if (prefixlen < 128) + strcat (rxstr, "(0|1)+"); } -/* -static void -debug (struct RegexCombineCtx *ctx, int lvl) -{ - struct RegexCombineCtx *p; - unsigned int i; - - for (i = 0; i < lvl; i++) fprintf (stderr, " "); - fprintf (stderr, "%s\n", ctx->s); - - for (p = ctx->head; NULL != p; p = p->next) - { - debug (p, lvl + 2); - } -}*/ -char * -GNUNET_REGEX_combine (char * const regexes[]) -{ - unsigned int i; - char *combined; - const char *current; - struct RegexCombineCtx ctx; - - memset (&ctx, 0, sizeof (struct RegexCombineCtx)); - 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); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); - - combined = regex_combine (&ctx); - - return combined; -} /* end of regex.c */ |