arg
[oweals/gnunet.git] / src / fs / fs_uri.c
index 38dba89910025f202ea218e70cbee7cf542fa8ad..4b249d6bf37f3bd20550f11d9f34dc94a00b7eca 100644 (file)
@@ -4,7 +4,7 @@
 
      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 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -240,6 +240,7 @@ percent_decode_keyword (const char *in, char **emsg)
   return ret;
 }
 
+#define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
 
 /**
  * Parse a KSK URI.
@@ -263,9 +264,9 @@ uri_ksk_parse (const char *s, char **emsg)
 
   GNUNET_assert (s != NULL);
   slen = strlen (s);
-  pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX);
+  pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
   if ( (slen <= pos) ||
-       (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX, 
+       (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX,
                      pos) ) )
     return NULL;       /* not KSK URI */
   if ( (s[slen - 1] == '+') ||
@@ -337,6 +338,8 @@ CLEANUP:
 }
 
 
+#define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
+
 /**
  * Parse an SKS URI.
  *
@@ -356,12 +359,12 @@ uri_sks_parse (const char *s, char **emsg)
 
   GNUNET_assert (s != NULL);
   slen = strlen (s);
-  pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
+  pos = strlen ( GNUNET_FS_URI_SKS_PREFIX);
   if ( (slen <= pos) ||
-       (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX, 
+       (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX,
                      pos) ) )
     return NULL; /* not an SKS URI */
-  if ( (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
+  if ( (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) ||
        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
     {
       *emsg = GNUNET_strdup (_("Malformed SKS URI"));
@@ -382,6 +385,8 @@ uri_sks_parse (const char *s, char **emsg)
   return ret;
 }
 
+#define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
+
 
 /**
  * Parse a CHK URI.
@@ -401,12 +406,13 @@ uri_chk_parse (const char *s, char **emsg)
   char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
   char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
 
+  if (NULL == s)
+    return NULL;
   GNUNET_assert (s != NULL);
-
   slen = strlen (s);
-  pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
+  pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
   if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
-       (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX, 
+       (0 != strncmp (s,  GNUNET_FS_URI_CHK_PREFIX, 
                      pos) ) )
     return NULL; /* not a CHK URI */
   if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
@@ -527,6 +533,8 @@ struct LocUriAssembly
 };
 
 
+#define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
+
 /**
  * Parse a LOC URI.
  * Also verifies validity of the location URI.
@@ -553,9 +561,9 @@ uri_loc_parse (const char *s, char **emsg)
 
   GNUNET_assert (s != NULL);
   slen = strlen (s);
-  pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
+  pos = strlen ( GNUNET_FS_URI_LOC_PREFIX);
   if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
-       (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX, 
+       (0 != strncmp (s,  GNUNET_FS_URI_LOC_PREFIX,
                      pos) ) )
     return NULL; /* not an SKS URI */
   if ( (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
@@ -630,7 +638,7 @@ uri_loc_parse (const char *s, char **emsg)
     }
   ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
   ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
-  et.value = exptime;
+  et.abs_value = exptime;
   ass.exptime = GNUNET_TIME_absolute_hton (et);
   if (GNUNET_OK != 
       GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
@@ -765,6 +773,70 @@ GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
 }
 
 
+/**
+ * Add the given keyword to the set of keywords represented by the URI.
+ * Does nothing if the keyword is already present.
+ *
+ * @param uri ksk uri to modify
+ * @param keyword keyword to add
+ * @param is_mandatory is this keyword mandatory?
+ */
+void
+GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
+                              const char *keyword,
+                              int is_mandatory)
+{
+  unsigned int i;
+  const char *old;
+  char *n;
+
+  GNUNET_assert (uri->type == ksk);
+  for (i = 0; i < uri->data.ksk.keywordCount; i++)
+    {
+      old = uri->data.ksk.keywords[i];
+      if (0 == strcmp (&old[1], keyword))
+       return;
+    }
+  GNUNET_asprintf (&n,
+                  is_mandatory ? "+%s" : " %s",
+                  keyword);
+  GNUNET_array_append (uri->data.ksk.keywords,
+                      uri->data.ksk.keywordCount,
+                      n);
+}
+
+
+/**
+ * Remove the given keyword from the set of keywords represented by the URI.
+ * Does nothing if the keyword is not present.
+ *
+ * @param uri ksk uri to modify
+ * @param keyword keyword to add
+ */
+void
+GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
+                                 const char *keyword)
+{
+  unsigned int i;
+  char *old;
+
+  GNUNET_assert (uri->type == ksk);
+  for (i = 0; i < uri->data.ksk.keywordCount; i++)
+    {
+      old = uri->data.ksk.keywords[i];
+      if (0 == strcmp (&old[1], keyword))
+       {
+         uri->data.ksk.keywords[i] = uri->data.ksk.keywords[uri->data.ksk.keywordCount-1];
+         GNUNET_array_grow (uri->data.ksk.keywords,
+                            uri->data.ksk.keywordCount,
+                            uri->data.ksk.keywordCount - 1);
+         GNUNET_free (old);
+         return;
+       }
+    }
+}
+
+
 /**
  * Obtain the identity of the peer offering the data
  *
@@ -785,6 +857,21 @@ GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
 }
 
 
+/**
+ * Obtain the expiration of the LOC URI.
+ *
+ * @param uri location URI to get the expiration from
+ * @return expiration time of the URI
+ */
+struct GNUNET_TIME_Absolute
+GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
+{
+  GNUNET_assert (uri->type == loc);
+  return uri->data.loc.expirationTime; 
+}
+
+
+
 /**
  * Obtain the URI of the content itself.
  *
@@ -815,7 +902,7 @@ GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
  */
 struct GNUNET_FS_Uri *
 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
-                         struct GNUNET_CONFIGURATION_Handle *cfg,
+                         const struct GNUNET_CONFIGURATION_Handle *cfg,
                          struct GNUNET_TIME_Absolute expiration_time)
 {
   struct GNUNET_FS_Uri *uri;
@@ -866,6 +953,55 @@ GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
 }
 
 
+/**
+ * Create an SKS URI from a namespace and an identifier.
+ *
+ * @param ns namespace
+ * @param id identifier
+ * @param emsg where to store an error message
+ * @return an FS URI for the given namespace and identifier
+ */
+struct GNUNET_FS_Uri *
+GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns,
+                         const char *id,
+                         char **emsg)
+{
+  struct GNUNET_FS_Uri *ns_uri;
+  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
+             
+  ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
+  ns_uri->type = sks;
+  GNUNET_CRYPTO_rsa_key_get_public (ns->key,
+                                   &pk);
+  GNUNET_CRYPTO_hash (&pk,
+                     sizeof (pk),
+                     &ns_uri->data.sks.namespace);
+  ns_uri->data.sks.identifier = GNUNET_strdup (id);
+  return ns_uri;
+}
+
+
+/**
+ * Create an SKS URI from a namespace ID and an identifier.
+ *
+ * @param nsid namespace ID
+ * @param id identifier
+ * @return an FS URI for the given namespace and identifier
+ */
+struct GNUNET_FS_Uri *
+GNUNET_FS_uri_sks_create_from_nsid (GNUNET_HashCode *nsid,
+                                   const char *id)
+{
+  struct GNUNET_FS_Uri *ns_uri;
+             
+  ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
+  ns_uri->type = sks;
+  ns_uri->data.sks.namespace = *nsid;
+  ns_uri->data.sks.identifier = GNUNET_strdup (id);
+  return ns_uri;
+}
+
+
 /**
  * Canonicalize a keyword.
  * 
@@ -884,7 +1020,7 @@ canonicalize_keyword (const char *in)
   rpos = in;
   while ('\0' != *rpos)
     {
-      switch (tolower(*rpos))
+      switch (tolower( (unsigned char) *rpos))
        {
        case 'a':
        case 'e':
@@ -896,7 +1032,6 @@ canonicalize_keyword (const char *in)
        case '\n':
        case '\r':
          /* skip characters listed above */
-         rpos++;
          break;
        case 'b':
        case 'c':
@@ -919,8 +1054,9 @@ canonicalize_keyword (const char *in)
        case 'y':
        case 'z':
          /* convert characters listed above to lower case */
-         *wpos = tolower(*rpos);
+         *wpos = tolower( (unsigned char)*rpos);
          wpos++;
+         break;
        case '!':
        case '.':
        case '?':
@@ -928,10 +1064,12 @@ canonicalize_keyword (const char *in)
          /* keep characters listed above without changes */
          *wpos = *rpos;
          wpos++;
+         break;
        default:
          /* replace characters listed above with '_' */
          *wpos = '_';
          wpos++;
+         break;
        }
       rpos++;
     }
@@ -988,6 +1126,12 @@ GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
   const char *kp;
   char **kl;
 
+  if ( (u1 == NULL) && (u2 == NULL) )
+    return NULL;
+  if (u1 == NULL)
+    return GNUNET_FS_uri_dup (u2);
+  if (u2 == NULL)
+    return GNUNET_FS_uri_dup (u1);
   if ( (u1->type != ksk) ||
        (u2->type != ksk) )
     {
@@ -1012,7 +1156,7 @@ GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
            break;
          }
       if (0 == found)
-       kl[kc++] = GNUNET_strdup (kp - 1);
+       kl[kc++] = GNUNET_strdup (kp);
     }
   ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
   ret->type = ksk;
@@ -1034,13 +1178,21 @@ GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
   struct GNUNET_FS_Uri *ret;
   unsigned int i;
 
+  if (uri == NULL)
+    return NULL;
   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
   memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
   switch (ret->type)
     {
     case ksk:
+      if (ret->data.ksk.keywordCount >= GNUNET_MAX_MALLOC_CHECKED / sizeof (char*))
+       {
+         GNUNET_break (0);
+         GNUNET_free (ret);
+         return NULL;
+       }
       if (ret->data.ksk.keywordCount > 0)
-        {
+        {        
           ret->data.ksk.keywords
             = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
           for (i = 0; i < ret->data.ksk.keywordCount; i++)
@@ -1093,6 +1245,7 @@ GNUNET_FS_uri_ksk_create (const char *keywords,
 
   if (keywords == NULL)
     {
+      *emsg = GNUNET_strdup (_("No keywords specified!\n"));
       GNUNET_break (0);
       return NULL;
     }
@@ -1103,7 +1256,7 @@ GNUNET_FS_uri_ksk_create (const char *keywords,
   pos = searchString;
   while ('\0' != *pos)
     {
-      if ((saw_quote == 0) && (isspace (*pos)))
+      if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
         {
           inWord = 0;
         }
@@ -1134,7 +1287,7 @@ GNUNET_FS_uri_ksk_create (const char *keywords,
   pos = searchString;
   while ('\0' != *pos)
     {
-      if ((saw_quote == 0) && (isspace (*pos)))
+      if ((saw_quote == 0) && (isspace ( (unsigned char) *pos)))
         {
           inWord = 0;
           *pos = '\0';
@@ -1453,27 +1606,42 @@ GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
  * Adds it to the URI.
  *
  * @param cls URI to update
- * @param type type of the meta data
- * @param data value of the meta data
- * @return GNUNET_OK (always)
+ * @param plugin_name name of the plugin that produced this value;
+ *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
+ *        used in the main libextractor library and yielding
+ *        meta data).
+ * @param type libextractor-type describing the meta data
+ * @param format basic format information about data 
+ * @param data_mime_type mime-type of data (not of the original file);
+ *        can be NULL (if mime-type is not known)
+ * @param data actual meta-data found
+ * @param data_len number of bytes in data
+ * @return 0 (always)
  */
 static int
 gather_uri_data (void *cls,
-                EXTRACTOR_KeywordType type, 
-                const char *data)
+                const char *plugin_name,
+                enum EXTRACTOR_MetaType type, 
+                enum EXTRACTOR_MetaFormat format,
+                const char *data_mime_type,
+                const char *data,
+                size_t data_len)
 {
   struct GNUNET_FS_Uri *uri = cls;
   char *nkword;
   int j;
   
+  if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
+       (format != EXTRACTOR_METAFORMAT_C_STRING) )
+    return 0;
   for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
     if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
       return GNUNET_OK;
-  nkword = GNUNET_malloc (strlen (data) + 2);
-  strcpy (nkword, " ");         /* not mandatory */
-  strcat (nkword, data);
+  GNUNET_asprintf (&nkword,
+                  " %s", /* space to mark as 'non mandatory' */
+                  data);
   uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
-  return GNUNET_OK;
+  return 0;
 }
 
 
@@ -1489,19 +1657,20 @@ struct GNUNET_FS_Uri *
 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
 {
   struct GNUNET_FS_Uri *ret;
+  int ent;
 
   if (md == NULL)
     return NULL;
   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
   ret->type = ksk;
-  ret->data.ksk.keywordCount = 0;
-  ret->data.ksk.keywords = NULL;
-  ret->data.ksk.keywords
-    = GNUNET_malloc (sizeof (char *) *
-                     GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
-  GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
+  ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
+  if (ent > 0)
+    {
+      ret->data.ksk.keywords
+       = GNUNET_malloc (sizeof (char *) * ent);                     
+      GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
+    }
   return ret;
-
 }
 
 
@@ -1512,7 +1681,7 @@ GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
 static int
 needs_percent (char c)
 {
-  return (!((isalnum (c)) ||
+  return (!((isalnum ( (unsigned char) c)) ||
             (c == '-') || (c == '_') || (c == '.') || (c == '~')));
 }
 
@@ -1689,7 +1858,7 @@ bin2enc (const void *data, size_t size)
         }
     }
   if (hbits > 0)
-    ret[len++] = tbl[bits & 63];
+    ret[len] = tbl[bits & 63];
   return ret;
 }
 
@@ -1724,7 +1893,7 @@ uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
                    (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
                    peerId,
                   peerSig,
-                  (unsigned long long) uri->data.loc.expirationTime.value);
+                  (unsigned long long) uri->data.loc.expirationTime.abs_value);
   GNUNET_free (peerSig);
   GNUNET_free (peerId);
   return ret;