- coverity
[oweals/gnunet.git] / src / namestore / namestore_common.c
index 45fccbb186e16aea65dc1aa5c33dc0b77e5de256..24711ba470c8326ca45c677fe716a6d14e91a8c9 100644 (file)
@@ -66,6 +66,25 @@ struct NetworkRecord
   
 };
 
+
+/**
+ * Convert a short hash to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param hc the short hash code
+ * @return string form; will be overwritten by next call to GNUNET_h2s.
+ */
+const char *
+GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc)
+{
+  static struct GNUNET_CRYPTO_ShortHashAsciiEncoded ret;
+
+  GNUNET_CRYPTO_short_hash_to_enc (hc, &ret);
+  return (const char *) &ret;
+}
+
+
 /**
  * Calculate how many bytes we will need to serialize the given
  * records.
@@ -258,6 +277,22 @@ GNUNET_NAMESTORE_create_signature (const struct GNUNET_CRYPTO_RsaPrivateKey *key
   return sig;
 }
 
+/**
+ * Checks if a name is wellformed
+ *
+ * @param name the name to check
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_NAMESTORE_check_name (const char * name)
+{
+  if (name == NULL)
+    return GNUNET_SYSERR;
+  if (strlen (name) > 63)
+    return GNUNET_SYSERR;
+  return GNUNET_OK;
+}
+
 
 /**
  * Convert the 'value' of a record to a string.
@@ -273,6 +308,7 @@ GNUNET_NAMESTORE_value_to_string (uint32_t type,
                                  size_t data_size)
 {
   char tmp[INET6_ADDRSTRLEN];
+  struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
 
   switch (type)
   {
@@ -309,9 +345,11 @@ GNUNET_NAMESTORE_value_to_string (uint32_t type,
       return NULL;
     return GNUNET_strdup (tmp);
   case GNUNET_NAMESTORE_TYPE_PKEY:
-    if (data_size != sizeof (GNUNET_HashCode))
+    if (data_size != sizeof (struct GNUNET_CRYPTO_ShortHashCode))
       return NULL;
-    return GNUNET_strdup (GNUNET_h2s_full (data));
+    GNUNET_CRYPTO_short_hash_to_enc (data,
+                                    &enc);
+    return GNUNET_strdup ((const char*) enc.short_encoding);
   case GNUNET_NAMESTORE_TYPE_PSEU:
     return GNUNET_strndup (data, data_size);
   default:
@@ -340,7 +378,7 @@ GNUNET_NAMESTORE_string_to_value (uint32_t type,
 {
   struct in_addr value_a;
   struct in6_addr value_aaaa;
-  GNUNET_HashCode pkey;
+  struct GNUNET_CRYPTO_ShortHashCode pkey;
 
   switch (type)
   {
@@ -381,15 +419,16 @@ GNUNET_NAMESTORE_string_to_value (uint32_t type,
     if (1 != inet_pton (AF_INET6, s, &value_aaaa))    
       return GNUNET_SYSERR;    
     *data = GNUNET_malloc (sizeof (struct in6_addr));
+    *data_size = sizeof (struct in6_addr);
     memcpy (*data, &value_aaaa, sizeof (value_aaaa));
     return GNUNET_OK;
   case GNUNET_NAMESTORE_TYPE_PKEY:
     if (GNUNET_OK !=
-       GNUNET_CRYPTO_hash_from_string (s, &pkey))
+       GNUNET_CRYPTO_short_hash_from_string (s, &pkey))
       return GNUNET_SYSERR;
-    *data = GNUNET_malloc (sizeof (GNUNET_HashCode));
+    *data = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
     memcpy (*data, &pkey, sizeof (pkey));
-    *data_size = sizeof (GNUNET_HashCode);
+    *data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
     return GNUNET_OK;
   case GNUNET_NAMESTORE_TYPE_PSEU:
     *data = GNUNET_strdup (s);