ats_ril: - removed some redundantly saved plugin environment attributes
[oweals/gnunet.git] / src / fs / fs_uri.c
index 4b25e91ea8691185b54c2d9421ed6d734a38cb06..e64fabeba3b7b9d40352176fdc7bc7faf897b3a2 100644 (file)
@@ -98,7 +98,7 @@
  * @param key where to store the unique key
  */
 void
-GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri, 
+GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
                      struct GNUNET_HashCode *key)
 {
   switch (uri->type)
@@ -368,7 +368,7 @@ uri_sks_parse (const char *s, char **emsg)
   end = strchr (&s[pos], '/');
   if ( (NULL == end) ||
        (GNUNET_OK !=
-       GNUNET_STRINGS_string_to_data (&s[pos], 
+       GNUNET_STRINGS_string_to_data (&s[pos],
                                       end - &s[pos],
                                       &ns,
                                       sizeof (ns))) )
@@ -526,12 +526,12 @@ struct LocUriAssembly
 
   /**
    * File being offered.
-   */ 
+   */
   struct FileIdentifier fi;
 
   /**
    * Peer offering the file.
-   */ 
+   */
   struct GNUNET_PeerIdentity peer;
 
 };
@@ -947,7 +947,7 @@ GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
  * @return an FS URI for the given namespace and identifier
  */
 struct GNUNET_FS_Uri *
-GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EccPublicSignKey *ns, 
+GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EccPublicSignKey *ns,
                          const char *id)
 {
   struct GNUNET_FS_Uri *ns_uri;
@@ -1424,7 +1424,7 @@ GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
  * Add a keyword as non-mandatory (with ' '-prefix) to the
  * given keyword list at offset 'index'.  The array is
  * guaranteed to be long enough.
- * 
+ *
  * @param s keyword to add
  * @param array array to add the keyword to
  * @param index offset where to add the keyword
@@ -1433,20 +1433,23 @@ static void
 insert_non_mandatory_keyword (const char *s, char **array, int index)
 {
   char *nkword;
-  GNUNET_asprintf (&nkword, " %s", /* space to mark as 'non mandatory' */ s);
+
+  GNUNET_asprintf (&nkword,
+                   " %s", /* space to mark as 'non mandatory' */
+                   s);
   array[index] = nkword;
 }
 
 
 /**
- * Test if the given keyword 's' is already present in the 
+ * Test if the given keyword @a s is already present in the
  * given array, ignoring the '+'-mandatory prefix in the array.
  *
  * @param s keyword to test
  * @param array keywords to test against, with ' ' or '+' prefix to ignore
- * @param array_length length of the array
- * @return GNUNET_YES if the keyword exists, GNUNET_NO if not
- */ 
+ * @param array_length length of the @a array
+ * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
+ */
 static int
 find_duplicate (const char *s, const char **array, int array_length)
 {
@@ -1463,8 +1466,9 @@ find_duplicate (const char *s, const char **array, int array_length)
  * FIXME: comment
  */
 static char *
-normalize_metadata (enum EXTRACTOR_MetaFormat format, const char *data,
-    size_t data_len)
+normalize_metadata (enum EXTRACTOR_MetaFormat format,
+                    const char *data,
+                    size_t data_len)
 {
   uint8_t *free_str = NULL;
   uint8_t *str_to_normalize = (uint8_t *) data;
@@ -1535,7 +1539,7 @@ u8_strcount (const uint8_t *s)
  *        to it are used to check for duplicates). ignored if array == NULL.
  * @return number of tokens counted (including duplicates), or number of
  *         tokens extracted (excluding duplicates). 0 if there are no
- *         matching parens in the string (when counting), or when all tokens 
+ *         matching parens in the string (when counting), or when all tokens
  *         were duplicates (when extracting).
  */
 static int
@@ -1612,7 +1616,7 @@ get_keywords_from_parens (const char *s, char **array, int index)
       else
        count++;
       close_paren[0] = tmp;
-    }   
+    }
   }
   GNUNET_free (ss);
   return count;
@@ -1682,6 +1686,7 @@ get_keywords_from_tokens (const char *s, char **array, int index)
 }
 #undef TOKENS
 
+
 /**
  * Function called on each value in the meta data.
  * Adds it to the URI.
@@ -1696,16 +1701,20 @@ get_keywords_from_tokens (const char *s, char **array, int index)
  * @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
+ * @param data_len number of bytes in @a data
  * @return 0 (always)
  */
 static int
 gather_uri_data (void *cls, const char *plugin_name,
-                 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
-                 const char *data_mime_type, const char *data, size_t data_len)
+                 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 *normalized_data;
+  const char *sep;
 
   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
       (format != EXTRACTOR_METAFORMAT_C_STRING))
@@ -1716,19 +1725,43 @@ gather_uri_data (void *cls, const char *plugin_name,
    * If it does - fix the extractor, not this check!
    */
   if (u8_strcount ((const uint8_t *) data) <= 2)
-  {
     return 0;
+  if ( (EXTRACTOR_METATYPE_MIMETYPE == type) &&
+       (NULL != (sep = memchr (data, '/', data_len))) &&
+       (sep != data) )
+  {
+    char *xtra;
+
+    GNUNET_asprintf (&xtra,
+                     "mimetype:%.*s",
+                     (int) (sep - data),
+                     data);
+    if (! find_duplicate (xtra,
+                          (const char **) uri->data.ksk.keywords,
+                          uri->data.ksk.keywordCount))
+    {
+      insert_non_mandatory_keyword (xtra,
+                                    uri->data.ksk.keywords,
+                                    uri->data.ksk.keywordCount);
+      uri->data.ksk.keywordCount++;
+    }
+    GNUNET_free (xtra);
   }
+
   normalized_data = normalize_metadata (format, data, data_len);
-  if (!find_duplicate (data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
+  if (! find_duplicate (data,
+                        (const char **) uri->data.ksk.keywords,
+                        uri->data.ksk.keywordCount))
   {
     insert_non_mandatory_keyword (data,
                                  uri->data.ksk.keywords, uri->data.ksk.keywordCount);
     uri->data.ksk.keywordCount++;
   }
-  if (normalized_data != NULL)
-  {
-    if (!find_duplicate (normalized_data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
+  if (NULL != normalized_data)
+    {
+    if (! find_duplicate (normalized_data,
+                          (const char **) uri->data.ksk.keywords,
+                          uri->data.ksk.keywordCount))
     {
       insert_non_mandatory_keyword (normalized_data,
                                    uri->data.ksk.keywords, uri->data.ksk.keywordCount);
@@ -1749,8 +1782,7 @@ gather_uri_data (void *cls, const char *plugin_name,
  * @return NULL on error, otherwise a KSK URI
  */
 struct GNUNET_FS_Uri *
-GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
-                                         *md)
+GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
 {
   struct GNUNET_FS_Uri *ret;
   char *filename;
@@ -1760,7 +1792,7 @@ GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
   int tok_keywords = 0;
   int paren_keywords = 0;
 
-  if (md == NULL)
+  if (NULL == md)
     return NULL;
   ret = GNUNET_new (struct GNUNET_FS_Uri);
   ret->type = GNUNET_FS_URI_KSK;
@@ -1777,19 +1809,20 @@ GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
       tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
       paren_keywords = get_keywords_from_parens (filename, NULL, 0);
     }
-    /* x2 because there might be a normalized variant of every keyword */
-    ret->data.ksk.keywords = GNUNET_malloc (sizeof (char *) * (ent
-        + tok_keywords + paren_keywords) * 2);
+    /* x3 because there might be a normalized variant of every keyword,
+       plus theoretically one more for mime... */
+    ret->data.ksk.keywords = GNUNET_malloc
+      (sizeof (char *) * (ent + tok_keywords + paren_keywords) * 3);
     GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
   }
   if (tok_keywords > 0)
     ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
-        ret->data.ksk.keywords,
-        ret->data.ksk.keywordCount);
+                                                            ret->data.ksk.keywords,
+                                                            ret->data.ksk.keywordCount);
   if (paren_keywords > 0)
     ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
-        ret->data.ksk.keywords,
-        ret->data.ksk.keywordCount);
+                                                            ret->data.ksk.keywords,
+                                                            ret->data.ksk.keywordCount);
   if (ent > 0)
     GNUNET_free_non_null (full_name);
   return ret;
@@ -1900,7 +1933,7 @@ uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
   GNUNET_assert (NULL != ret);
   ret[0] = '\0';
   GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
-                   GNUNET_FS_URI_SKS_INFIX, buf, 
+                   GNUNET_FS_URI_SKS_INFIX, buf,
                   uri->data.sks.identifier);
   return ret;
 }
@@ -2003,7 +2036,7 @@ uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
   peerSig =
       bin2enc (&uri->data.loc.contentSignature,
                sizeof (struct GNUNET_CRYPTO_EccSignature));
-  GNUNET_asprintf (&ret, 
+  GNUNET_asprintf (&ret,
                   "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
                    GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
                    (const char *) &queryhash,