add $(GN_LIBINTL) to Makefile.am (fixes 0005902)
[oweals/gnunet.git] / src / regex / regex_test_lib.c
index 4e388a70bde9c3a605a695d701bd800badac4c5b..05001b5d29ab2186555a6d8c75e652ac23bc7e88 100644 (file)
@@ -2,20 +2,20 @@
  *  This file is part of GNUnet
  *  Copyright (C) 2012-2017 GNUnet e.V.
  *
- *  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 free software: you can redistribute it and/or modify it
+ *  under the terms of the GNU Affero General Public License as published
+ *  by the Free Software Foundation, either version 3 of the License,
+ *  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.
+ *  Affero 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., 51 Franklin Street, Fifth Floor,
- *  Boston, MA 02110-1301, USA.
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
  */
 /**
  * @file src/regex/regex_test_lib.c
@@ -66,27 +66,29 @@ 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:
+    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);
   }
 }
 
@@ -99,8 +101,7 @@ c2i (char c, int size)
 static void
 space (int n)
 {
-  int i;
-  for (i = 0; i < n; i++)
+  for (int i = 0; i < n; i++)
     fprintf (stderr, "| ");
 }
 
@@ -114,16 +115,15 @@ space (int n)
 static void
 debugctx (struct RegexCombineCtx *ctx, int level)
 {
-  return;
-  unsigned int i;
+#if DEBUG_REGEX
   if (NULL != ctx->s)
   {
     space (level - 1);
-    fprintf (stderr, "%u:'%s'\n", c2i(ctx->s[0], ctx->size), ctx->s);
+    fprintf (stderr, "%u:'%s'\n", c2i (ctx->s[0], ctx->size), ctx->s);
   }
   else
     fprintf (stderr, "ROOT (base %u)\n", ctx->size);
-  for (i = 0; i < ctx->size; i++)
+  for (unsigned int i = 0; i < ctx->size; i++)
   {
     if (NULL != ctx->children[i])
     {
@@ -131,7 +131,8 @@ debugctx (struct RegexCombineCtx *ctx, int level)
       debugctx (ctx->children[i], level + 1);
     }
   }
-  fflush(stderr);
+  fflush (stderr);
+#endif
 }
 
 
@@ -142,7 +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);
 
 
 /**
@@ -167,14 +169,14 @@ new_regex_ctx (unsigned int alphabet_size)
 
 static void
 move_children (struct RegexCombineCtx *dst,
-              const struct RegexCombineCtx *src)
+               const struct RegexCombineCtx *src)
 {
   size_t array_size;
 
   array_size = sizeof(struct RegexCombineCtx *) * src->size;
-  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;
@@ -213,7 +215,7 @@ regex_combine (struct RegexCombineCtx *ctx)
                 p->s, ctx->s);
     s = regex_combine (p);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  total '%s'\n", s);
-    if (strlen(s) == 0)
+    if (strlen (s) == 0)
     {
       opt = GNUNET_YES;
     }
@@ -224,7 +226,8 @@ regex_combine (struct RegexCombineCtx *ctx)
       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, "  so far '%s' for inner %s\n", regex,
+                ctx->s);
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "opt: %d, innner: '%s'\n", opt, regex);
@@ -321,6 +324,7 @@ get_longest_prefix (struct RegexCombineCtx *ctx, const char *regex)
   return best;
 }
 
+
 static void
 regex_add_multiple (struct RegexCombineCtx *ctx,
                     const char *regex,
@@ -344,7 +348,7 @@ regex_add_multiple (struct RegexCombineCtx *ctx,
   count = 0;
   for (i = 1UL; i < l; i++)
   {
-    if (regex[i] != '|' && regex[i] != ')')
+    if ((regex[i] != '|') && (regex[i] != ')') )
     {
       count++;
     }
@@ -358,18 +362,21 @@ regex_add_multiple (struct RegexCombineCtx *ctx,
   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 = new_regex_ctx (ctx->size);
       newctx->s = GNUNET_strdup (tmp);
       if (children != NULL)
-        memcpy (newctx->children, children, sizeof (*children) * ctx->size);
-      ctx->children[c2i(tmp[0], ctx->size)] = newctx;
+        GNUNET_memcpy (newctx->children,
+                       children,
+                       sizeof(*children) * ctx->size);
+      ctx->children[c2i (tmp[0], ctx->size)] = newctx;
     }
   }
 }
 
+
 /**
  * Add a single regex to a context, splitting the exisiting state.
  *
@@ -390,7 +397,10 @@ regex_split (struct RegexCombineCtx *ctx,
   char *suffix;
 
   suffix = GNUNET_malloc (len - prefix_l + 1);
-  strncpy (suffix, &ctx->s[prefix_l], 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);
 
   /* Suffix saved, truncate current node so it only contains the prefix,
    * copy any children nodes to put as grandchildren and initialize new empty
@@ -415,7 +425,7 @@ regex_split (struct RegexCombineCtx *ctx,
   newctx = new_regex_ctx (ctx->size);
   newctx->s = suffix;
   move_children (newctx, ctx);
-  idx = c2i(suffix[0], ctx->size);
+  idx = c2i (suffix[0], ctx->size);
   ctx->children[idx] = newctx;
 }
 
@@ -473,8 +483,8 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex)
   }
 
   /* There is no prefix match, add new */
-  idx = c2i(regex[0], ctx->size);
-  if (NULL == ctx->children[idx] && NULL != ctx->s)
+  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);
@@ -484,7 +494,7 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex)
   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 = new_regex_ctx (ctx->size);
   newctx->s = GNUNET_strdup (regex);
   ctx->children[idx] = newctx;
 }
@@ -507,7 +517,7 @@ regex_ctx_destroy (struct RegexCombineCtx *ctx)
   {
     regex_ctx_destroy (ctx->children[i]);
   }
-  GNUNET_free_non_null (ctx->s); /* 's' on root node is null */
+  GNUNET_free_non_null (ctx->s);  /* 's' on root node is null */
   GNUNET_free (ctx->children);
   GNUNET_free (ctx);
 }
@@ -528,7 +538,7 @@ 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;
@@ -598,7 +608,7 @@ REGEX_TEST_read_from_file (const char *filename)
   buffer = GNUNET_malloc (size + 1);
   GNUNET_DISK_file_read (f, buffer, size);
   GNUNET_DISK_file_close (f);
-  regexes = GNUNET_malloc (sizeof (char *));
+  regexes = GNUNET_malloc (sizeof(char *));
   nr = 1;
   offset = 0;
   regex = NULL;
@@ -620,7 +630,8 @@ REGEX_TEST_read_from_file (const char *filename)
     regexes[nr - 2] = regex;
     regexes[nr - 1] = NULL;
     regex = NULL;
-  } while (offset < size);
+  }
+  while (offset < size);
   GNUNET_free_non_null (regex);
   GNUNET_free (buffer);
 
@@ -643,4 +654,5 @@ REGEX_TEST_free_from_file (char **regexes)
   GNUNET_free (regexes);
 }
 
+
 /* end of regex_test_lib.c */