- fix coverity
[oweals/gnunet.git] / src / cadet / cadet_common.c
index dbb62d392cf8ef4823d0d26e704d6d3d6aac063e..08e95bbdba9c5ad488f65d36e26dabc59b22fcdd 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 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
@@ -47,7 +47,7 @@ GC_f2s (int fwd)
   else
   {
     /* Not an error, can happen with CONNECTION_BROKEN messages. */
-    return "";
+    return "\???";
   }
 }
 
@@ -99,6 +99,38 @@ GC_h2s (const struct GNUNET_CADET_Hash *id)
 }
 
 
+/**
+ * Allocate a string with a hexdump of any binary data.
+ *
+ * @param bin Arbitrary binary data.
+ * @param len Length of @a bin in bytes.
+ * @param output Where to write the output (if *output be NULL it's allocated).
+ *
+ * @return The size of the output.
+ */
+size_t
+GC_bin2s (void *bin, unsigned int len, char **output)
+{
+  char *data = bin;
+  char *buf;
+  unsigned int s_len;
+  unsigned int i;
+
+  s_len = 2 * len + 1;
+  if (NULL == *output)
+    *output = GNUNET_malloc (s_len);
+  buf = *output;
+
+  for (i = 0; i < len; i++)
+  {
+    SPRINTF (&buf[2 * i], "%2X", data[i]);
+  }
+  buf[s_len - 1] = '\0';
+
+  return s_len;
+}
+
+
 #if !defined(GNUNET_CULL_LOGGING)
 const char *
 GC_m2s (uint16_t m)
@@ -114,7 +146,7 @@ GC_m2s (uint16_t m)
      * Used to mark the "payload" of a non-payload message.
      */
     case 0:
-      s = "retrnsmit";
+      s = "retransmit";
       break;
 
       /**
@@ -348,8 +380,16 @@ GC_m2s (uint16_t m)
       s = "INFO_DUMP";
       break;
 
+      /**
+       * Used to mark the "payload" of a non-payload message.
+       */
+    case UINT16_MAX:
+      s = "      N/A";
+      break;
+
+
     default:
-      SPRINTF (buf[idx], "%u (UNKNOWN)", m);
+      SPRINTF (buf[idx], "{UNK: %5u}", m);
       return buf[idx];
   }
   SPRINTF (buf[idx], "{%10s}", s);