*
* @param md metadata to extend
* @param plugin_name name of the plugin that produced this value;
- * special values can be used (i.e. '<zlib>' for zlib being
+ * special values can be used (i.e. '<zlib>' for zlib being
* used in the main libextractor library and yielding
* meta data).
* @param type libextractor-type describing the meta data
}
+/**
+ * Merge given meta data.
+ *
+ * @param cls the 'struct GNUNET_CONTAINER_MetaData' to merge into
+ * @param plugin_name name of the plugin that produced this value;
+ * special values can be used (i.e. '<zlib>' 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 (to continue)
+ */
+static int
+merge_helper(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)
+{
+ struct GNUNET_CONTAINER_MetaData *md = cls;
+ (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
+ type, format,
+ data_mime_type, data, data_len);
+ return 0;
+}
+
+
+/**
+ * Extend metadata. Merges the meta data from the second argument
+ * into the first, discarding duplicate key-value pairs.
+ *
+ * @param md metadata to extend
+ * @param in metadata to merge
+ */
+void
+GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
+ const struct GNUNET_CONTAINER_MetaData *in)
+{
+ GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
+}
+
+
/**
* Remove an item.
*
}
pos = pos->next;
}
- if (match == NULL)
+ if ( (match == NULL) ||
+ (match->data_size == 0) )
return 0;
*thumb = GNUNET_malloc (match->data_size);
memcpy (*thumb, match->data, match->data_size);
*
* @param cls closure, our meta data container
* @param plugin_name name of the plugin that produced this value;
- * special values can be used (i.e. '<zlib>' for zlib being
+ * special values can be used (i.e. '<zlib>' for zlib being
* used in the main libextractor library and yielding
* meta data).
* @param type libextractor-type describing the meta data
{
struct GNUNET_CONTAINER_MetaData *vmd;
struct MetaItem *pos;
+ struct MetaDataHeader ihdr;
struct MetaDataHeader *hdr;
struct MetaDataEntry *ent;
+ char *dst;
unsigned int i;
uint64_t msize;
size_t off;
size_t size;
size_t left;
size_t clen;
+ size_t rlen;
int comp;
if (max < sizeof (struct MetaDataHeader))
return GNUNET_SYSERR; /* far too small */
if (md == NULL)
return 0;
-
+
if (md->sbuf != NULL)
{
/* try to use serialization cache */
- if (md->sbuf_size < max)
+ if (md->sbuf_size <= max)
{
if (NULL == *target)
*target = GNUNET_malloc (md->sbuf_size);
return GNUNET_SYSERR; /* can say that this will fail */
/* need to compute a partial serialization, sbuf useless ... */
}
-
-
+ dst = NULL;
msize = 0;
pos = md->items;
while (NULL != pos)
off -= pos->data_size;
memcpy (&mdata[off], pos->data, pos->data_size);
off -= plen;
- memcpy (&mdata[off], pos->plugin_name, plen);
+ if (pos->plugin_name != NULL)
+ memcpy (&mdata[off], pos->plugin_name, plen);
off -= mlen;
- memcpy (&mdata[off], pos->mime_type, mlen);
+ if (pos->mime_type != NULL)
+ memcpy (&mdata[off], pos->mime_type, mlen);
i++;
pos = pos->next;
}
GNUNET_assert (off == 0);
+ clen = 0;
+ cdata = NULL;
left = size;
- for (i=0;i<md->item_count;i++)
+ i = 0;
+ pos = md->items;
+ while (pos != NULL)
{
comp = GNUNET_NO;
if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
of making the API look strange. */
vmd = (struct GNUNET_CONTAINER_MetaData*) md;
hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
+ hdr->size = htonl (left);
hdr->entries = htonl (md->item_count);
if (GNUNET_YES == comp)
{
- hdr->size = htonl (clen);
+ GNUNET_assert (clen < left);
hdr->version = htonl (2 | HEADER_COMPRESSED);
memcpy (&hdr[1],
cdata,
}
else
{
- hdr->size = htonl (left);
hdr->version = htonl (2);
memcpy (&hdr[1],
&ent[0],
/* success, this now fits! */
if (GNUNET_YES == comp)
{
- hdr = (struct MetaDataHeader*) *target;
- if (hdr == NULL)
- {
- hdr = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
- *target = (char*) hdr;
- }
+ if (dst == NULL)
+ dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
+ hdr = (struct MetaDataHeader*) dst;
hdr->version = htonl (2 | HEADER_COMPRESSED);
- hdr->entries = htonl (md->item_count - i);
hdr->size = htonl (left);
- memcpy (&target[sizeof(struct MetaDataHeader)],
+ hdr->entries = htonl (md->item_count - i);
+ memcpy (&dst[sizeof(struct MetaDataHeader)],
cdata,
clen);
GNUNET_free (cdata);
GNUNET_free (ent);
- return clen + sizeof (struct MetaDataHeader);
+ rlen = clen + sizeof (struct MetaDataHeader);
}
else
{
- hdr = (struct MetaDataHeader*) target;
- if (hdr == NULL)
- {
- hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
- *target = (char*) hdr;
- }
+ if (dst == NULL)
+ dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
+ hdr = (struct MetaDataHeader*) dst;
hdr->version = htonl (2);
hdr->entries = htonl (md->item_count - i);
hdr->size = htonl (left);
- memcpy (&target[sizeof(struct MetaDataHeader)],
+ memcpy (&dst[sizeof(struct MetaDataHeader)],
&ent[i],
left);
GNUNET_free (ent);
- return left + sizeof (struct MetaDataHeader);
+ rlen = left + sizeof (struct MetaDataHeader);
+ }
+ if (NULL != *target)
+ {
+ memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
+ GNUNET_free (dst);
}
+ else
+ {
+ *target = dst;
+ }
+ return rlen;
}
if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
if (pos->plugin_name != NULL)
left -= strlen (pos->plugin_name) + 1;
if (pos->mime_type != NULL)
- left -= strlen (pos->mime_type) + 1;
+ left -= strlen (pos->mime_type) + 1;
+ pos = pos->next;
+ i++;
}
GNUNET_free (ent);
/* nothing fit, only write header! */
- hdr = (struct MetaDataHeader*) target;
- if (hdr == NULL)
- {
- hdr = GNUNET_malloc (sizeof (struct MetaDataHeader));
- *target = (char*) hdr;
- }
- hdr->version = htonl (2);
- hdr->entries = htonl (0);
- hdr->size = htonl (0);
+ ihdr.version = htonl (2);
+ ihdr.entries = htonl (0);
+ ihdr.size = htonl (0);
+ if (*target == NULL)
+ *target = GNUNET_malloc (sizeof (struct MetaDataHeader));
+ memcpy (*target, &ihdr, sizeof (struct MetaDataHeader));
return sizeof (struct MetaDataHeader);
}