2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/container_meta_data.c
23 * @brief Storing of meta data
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include <extractor.h>
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40 * This is a doubly linked list.
42 struct MetaItem *next;
45 * This is a doubly linked list.
47 struct MetaItem *prev;
50 * Name of the extracting plugin.
60 * The actual meta data.
65 * Number of bytes in 'data'.
70 * Type of the meta data.
72 enum EXTRACTOR_MetaType type;
75 * Format of the meta data.
77 enum EXTRACTOR_MetaFormat format;
82 * Meta data to associate with a file, directory or namespace.
84 struct GNUNET_CONTAINER_MetaData
87 * Head of linked list of the meta data items.
89 struct MetaItem *items_head;
92 * Tail of linked list of the meta data items.
94 struct MetaItem *items_tail;
97 * Complete serialized and compressed buffer of the items.
98 * NULL if we have not computed that buffer yet.
103 * Number of bytes in 'sbuf'. 0 if the buffer is stale.
108 * Number of items in the linked list.
110 unsigned int item_count;
116 * Create a fresh struct CONTAINER_MetaData token.
118 * @return empty meta-data container
120 struct GNUNET_CONTAINER_MetaData *
121 GNUNET_CONTAINER_meta_data_create ()
123 return GNUNET_new (struct GNUNET_CONTAINER_MetaData);
128 * Free meta data item.
130 * @param mi item to free
133 meta_item_free (struct MetaItem *mi)
135 GNUNET_free_non_null (mi->plugin_name);
136 GNUNET_free_non_null (mi->mime_type);
137 GNUNET_free_non_null (mi->data);
143 * The meta data has changed, invalidate its serialization
146 * @param md meta data that changed
149 invalidate_sbuf (struct GNUNET_CONTAINER_MetaData *md)
151 if (NULL == md->sbuf)
153 GNUNET_free (md->sbuf);
162 * @param md what to free
165 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
167 struct MetaItem *pos;
171 while (NULL != (pos = md->items_head))
173 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
174 meta_item_free (pos);
176 GNUNET_free_non_null (md->sbuf);
182 * Remove all items in the container.
184 * @param md metadata to manipulate
187 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
193 while (NULL != (mi = md->items_head))
195 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, mi);
198 GNUNET_free_non_null (md->sbuf);
199 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData));
204 * Test if two MDs are equal. We consider them equal if
205 * the meta types, formats and content match (we do not
206 * include the mime types and plugins names in this
209 * @param md1 first value to check
210 * @param md2 other value to check
211 * @return #GNUNET_YES if they are equal
214 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
216 const struct GNUNET_CONTAINER_MetaData
225 if (md1->item_count != md2->item_count)
227 for (i = md1->items_head; NULL != i; i = i->next)
230 for (j = md2->items_head; NULL != j; j = j->next)
232 if ((i->type == j->type) && (i->format == j->format) &&
233 (i->data_size == j->data_size) &&
234 (0 == memcmp (i->data, j->data, i->data_size)))
239 if (j->data_size < i->data_size)
240 break; /* elements are sorted by (decreasing) size... */
242 if (GNUNET_NO == found)
250 * Extend metadata. Note that the list of meta data items is
251 * sorted by size (largest first).
253 * @param md metadata to extend
254 * @param plugin_name name of the plugin that produced this value;
255 * special values can be used (i.e. '<zlib>' for zlib being
256 * used in the main libextractor library and yielding
258 * @param type libextractor-type describing the meta data
259 * @param format basic format information about data
260 * @param data_mime_type mime-type of data (not of the original file);
261 * can be NULL (if mime-type is not known)
262 * @param data actual meta-data found
263 * @param data_size number of bytes in @a data
264 * @return #GNUNET_OK on success, #GNUNET_SYSERR if this entry already exists
265 * data_mime_type and plugin_name are not considered for "exists" checks
268 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
269 const char *plugin_name,
270 enum EXTRACTOR_MetaType type,
271 enum EXTRACTOR_MetaFormat format,
272 const char *data_mime_type, const char *data,
275 struct MetaItem *pos;
279 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
280 (EXTRACTOR_METAFORMAT_C_STRING == format))
281 GNUNET_break ('\0' == data[data_size - 1]);
283 for (pos = md->items_head; NULL != pos; pos = pos->next)
285 if (pos->data_size < data_size)
286 break; /* elements are sorted by size in the list */
287 if ((pos->type == type) && (pos->data_size == data_size) &&
288 (0 == memcmp (pos->data, data, data_size)))
290 if ((NULL == pos->mime_type) && (NULL != data_mime_type))
292 pos->mime_type = GNUNET_strdup (data_mime_type);
293 invalidate_sbuf (md);
295 if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) &&
296 (EXTRACTOR_METAFORMAT_UTF8 == format))
298 pos->format = EXTRACTOR_METAFORMAT_UTF8;
299 invalidate_sbuf (md);
301 return GNUNET_SYSERR;
305 mi = GNUNET_new (struct MetaItem);
308 mi->data_size = data_size;
310 GNUNET_CONTAINER_DLL_insert_tail (md->items_head,
314 GNUNET_CONTAINER_DLL_insert_after (md->items_head,
319 (NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
320 mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
321 mi->data = GNUNET_malloc (data_size);
322 memcpy (mi->data, data, data_size);
323 /* change all dir separators to POSIX style ('/') */
324 if ( (EXTRACTOR_METATYPE_FILENAME == type) ||
325 (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type) )
328 while (('\0' != *p) && (p < mi->data + data_size))
335 invalidate_sbuf (md);
341 * Merge given meta data.
343 * @param cls the `struct GNUNET_CONTAINER_MetaData` to merge into
344 * @param plugin_name name of the plugin that produced this value;
345 * special values can be used (i.e. '<zlib>' for zlib being
346 * used in the main libextractor library and yielding
348 * @param type libextractor-type describing the meta data
349 * @param format basic format information about data
350 * @param data_mime_type mime-type of data (not of the original file);
351 * can be NULL (if mime-type is not known)
352 * @param data actual meta-data found
353 * @param data_size number of bytes in @a data
354 * @return 0 (to continue)
357 merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
358 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
359 const char *data, size_t data_size)
361 struct GNUNET_CONTAINER_MetaData *md = cls;
363 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
364 data_mime_type, data, data_size);
370 * Extend metadata. Merges the meta data from the second argument
371 * into the first, discarding duplicate key-value pairs.
373 * @param md metadata to extend
374 * @param in metadata to merge
377 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
378 const struct GNUNET_CONTAINER_MetaData *in)
380 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
387 * @param md metadata to manipulate
388 * @param type type of the item to remove
389 * @param data specific value to remove, NULL to remove all
390 * entries of the given type
391 * @param data_size number of bytes in @a data
392 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the item does not exist in md
395 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
396 enum EXTRACTOR_MetaType type,
397 const char *data, size_t data_size)
399 struct MetaItem *pos;
401 for (pos = md->items_head; NULL != pos; pos = pos->next)
403 if (pos->data_size < data_size)
404 break; /* items are sorted by (decreasing) size */
405 if ((pos->type == type) &&
407 ((pos->data_size == data_size) &&
408 (0 == memcmp (pos->data, data, data_size)))))
410 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
411 meta_item_free (pos);
413 invalidate_sbuf (md);
417 return GNUNET_SYSERR;
422 * Add the current time as the publication date
425 * @param md metadata to modify
428 GNUNET_CONTAINER_meta_data_add_publication_date (struct
429 GNUNET_CONTAINER_MetaData *md)
432 struct GNUNET_TIME_Absolute t;
434 t = GNUNET_TIME_absolute_get ();
435 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE,
437 dat = GNUNET_STRINGS_absolute_time_to_string (t);
438 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
439 EXTRACTOR_METATYPE_PUBLICATION_DATE,
440 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
441 dat, strlen (dat) + 1);
446 * Iterate over MD entries.
448 * @param md metadata to inspect
449 * @param iter function to call on each entry
450 * @param iter_cls closure for iterator
451 * @return number of entries
454 GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
455 EXTRACTOR_MetaDataProcessor iter,
458 struct MetaItem *pos;
463 return md->item_count;
464 for (pos = md->items_head; NULL != pos; pos = pos->next)
466 iter (iter_cls, pos->plugin_name, pos->type, pos->format,
467 pos->mime_type, pos->data, pos->data_size))
468 return md->item_count;
469 return md->item_count;
474 * Get the first MD entry of the given type. Caller
475 * is responsible for freeing the return value.
476 * Also, only meta data items that are strings (0-terminated)
477 * are returned by this function.
479 * @param md metadata to inspect
480 * @param type type to look for
481 * @return NULL if no entry was found
484 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
485 *md, enum EXTRACTOR_MetaType type)
487 struct MetaItem *pos;
491 for (pos = md->items_head; NULL != pos; pos = pos->next)
492 if ((type == pos->type) &&
493 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
494 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
495 return GNUNET_strdup (pos->data);
501 * Get the first matching MD entry of the given types. Caller is
502 * responsible for freeing the return value. Also, only meta data
503 * items that are strings (0-terminated) are returned by this
506 * @param md metadata to inspect
507 * @param ... -1-terminated list of types
508 * @return NULL if we do not have any such entry,
509 * otherwise client is responsible for freeing the value!
512 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
513 GNUNET_CONTAINER_MetaData *md,
518 enum EXTRACTOR_MetaType type;
526 type = va_arg (args, enum EXTRACTOR_MetaType);
529 if (NULL != (ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type)))
538 * Get a thumbnail from the meta-data (if present).
540 * @param md metadata to get the thumbnail from
541 * @param thumb will be set to the thumbnail data. Must be
542 * freed by the caller!
543 * @return number of bytes in thumbnail, 0 if not available
546 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
547 * md, unsigned char **thumb)
549 struct MetaItem *pos;
550 struct MetaItem *match;
555 for (pos = md->items_head; NULL != pos; pos = pos->next)
557 if ((NULL != pos->mime_type) &&
558 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
559 (EXTRACTOR_METAFORMAT_BINARY == pos->format))
563 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
564 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
568 if ((NULL == match) || (0 == match->data_size))
570 *thumb = GNUNET_malloc (match->data_size);
571 memcpy (*thumb, match->data, match->data_size);
572 return match->data_size;
577 * Duplicate a `struct GNUNET_CONTAINER_MetaData`.
579 * @param md what to duplicate
580 * @return duplicate meta-data container
582 struct GNUNET_CONTAINER_MetaData *
583 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
586 struct GNUNET_CONTAINER_MetaData *ret;
587 struct MetaItem *pos;
591 ret = GNUNET_CONTAINER_meta_data_create ();
592 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
593 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type,
594 pos->format, pos->mime_type, pos->data,
602 * Try to compress the given block of data.
604 * @param data block to compress; if compression
605 * resulted in a smaller block, the first
606 * bytes of data are updated to the compressed
608 * @param oldSize number of bytes in data
609 * @param result set to the compressed data
610 * @param newSize set to size of result
611 * @return #GNUNET_YES if compression reduce the size,
612 * #GNUNET_NO if compression did not help
615 try_compression (const char *data, size_t oldSize, char **result,
622 dlen = compressBound (oldSize);
624 dlen = oldSize + (oldSize / 100) + 20;
625 /* documentation says 100.1% oldSize + 12 bytes, but we
626 * should be able to overshoot by more to be safe */
628 tmp = GNUNET_malloc (dlen);
630 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
645 * Flag in 'version' that indicates compressed meta-data.
647 #define HEADER_COMPRESSED 0x80000000
651 * Bits in 'version' that give the version number.
653 #define HEADER_VERSION_MASK 0x7FFFFFFF
657 * Header for serialized meta data.
659 struct MetaDataHeader
662 * The version of the MD serialization. The highest bit is used to
663 * indicate compression.
665 * Version 0 is traditional (pre-0.9) meta data (unsupported)
666 * Version is 1 for a NULL pointer
667 * Version 2 is for 0.9.x (and possibly higher)
668 * Other version numbers are not yet defined.
673 * How many MD entries are there?
678 * Size of the decompressed meta data.
683 * This is followed by 'entries' values of type 'struct MetaDataEntry'
684 * and then by 'entry' plugin names, mime-types and data blocks
685 * as specified in those meta data entries.
691 * Entry of serialized meta data.
696 * Meta data type. Corresponds to an 'enum EXTRACTOR_MetaType'
701 * Meta data format. Corresponds to an 'enum EXTRACTOR_MetaFormat'
706 * Number of bytes of meta data.
711 * Number of bytes in the plugin name including 0-terminator. 0 for NULL.
713 uint32_t plugin_name_len;
716 * Number of bytes in the mime type including 0-terminator. 0 for NULL.
718 uint32_t mime_type_len;
724 * Serialize meta-data to target.
726 * @param md metadata to serialize
727 * @param target where to write the serialized metadata;
728 * *target can be NULL, in which case memory is allocated
729 * @param max maximum number of bytes available in target
730 * @param opt is it ok to just write SOME of the
731 * meta-data to match the size constraint,
732 * possibly discarding some data?
733 * @return number of bytes written on success,
734 * #GNUNET_SYSERR on error (typically: not enough
738 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
739 *md, char **target, size_t max,
741 GNUNET_CONTAINER_MetaDataSerializationOptions
744 struct GNUNET_CONTAINER_MetaData *vmd;
745 struct MetaItem *pos;
746 struct MetaDataHeader ihdr;
747 struct MetaDataHeader *hdr;
748 struct MetaDataEntry *ent;
763 if (max < sizeof (struct MetaDataHeader))
764 return GNUNET_SYSERR; /* far too small */
768 if (NULL != md->sbuf)
770 /* try to use serialization cache */
771 if (md->sbuf_size <= max)
774 *target = GNUNET_malloc (md->sbuf_size);
775 memcpy (*target, md->sbuf, md->sbuf_size);
776 return md->sbuf_size;
778 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
779 return GNUNET_SYSERR; /* can say that this will fail */
780 /* need to compute a partial serialization, sbuf useless ... */
784 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
786 msize += sizeof (struct MetaDataEntry);
787 msize += pos->data_size;
788 if (NULL != pos->plugin_name)
789 msize += strlen (pos->plugin_name) + 1;
790 if (NULL != pos->mime_type)
791 msize += strlen (pos->mime_type) + 1;
793 size = (size_t) msize;
796 GNUNET_break (0); /* integer overflow */
797 return GNUNET_SYSERR;
799 if (size >= GNUNET_MAX_MALLOC_CHECKED)
801 /* too large to be processed */
802 return GNUNET_SYSERR;
804 ent = GNUNET_malloc (size);
805 mdata = (char *) &ent[md->item_count];
806 off = size - (md->item_count * sizeof (struct MetaDataEntry));
808 for (pos = md->items_head; NULL != pos; pos = pos->next)
810 ent[i].type = htonl ((uint32_t) pos->type);
811 ent[i].format = htonl ((uint32_t) pos->format);
812 ent[i].data_size = htonl ((uint32_t) pos->data_size);
813 if (NULL == pos->plugin_name)
816 plen = strlen (pos->plugin_name) + 1;
817 ent[i].plugin_name_len = htonl ((uint32_t) plen);
818 if (NULL == pos->mime_type)
821 mlen = strlen (pos->mime_type) + 1;
822 ent[i].mime_type_len = htonl ((uint32_t) mlen);
823 off -= pos->data_size;
824 if ((EXTRACTOR_METAFORMAT_UTF8 == pos->format) ||
825 (EXTRACTOR_METAFORMAT_C_STRING == pos->format))
826 GNUNET_break ('\0' == pos->data[pos->data_size - 1]);
827 memcpy (&mdata[off], pos->data, pos->data_size);
829 if (NULL != pos->plugin_name)
830 memcpy (&mdata[off], pos->plugin_name, plen);
832 if (NULL != pos->mime_type)
833 memcpy (&mdata[off], pos->mime_type, mlen);
836 GNUNET_assert (0 == off);
842 for (pos = md->items_head; NULL != pos; pos = pos->next)
845 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
846 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
848 if ((NULL == md->sbuf) && (0 == i))
850 /* fill 'sbuf'; this "modifies" md, but since this is only
851 * an internal cache we will cast away the 'const' instead
852 * of making the API look strange. */
853 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
854 hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
855 hdr->size = htonl (left);
856 hdr->entries = htonl (md->item_count);
857 if (GNUNET_YES == comp)
859 GNUNET_assert (clen < left);
860 hdr->version = htonl (2 | HEADER_COMPRESSED);
861 memcpy (&hdr[1], cdata, clen);
862 vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
866 hdr->version = htonl (2);
867 memcpy (&hdr[1], &ent[0], left);
868 vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
870 vmd->sbuf = (char *) hdr;
873 if (((left + sizeof (struct MetaDataHeader)) <= max) ||
874 ((GNUNET_YES == comp) && (clen <= max)))
876 /* success, this now fits! */
877 if (GNUNET_YES == comp)
880 dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
881 hdr = (struct MetaDataHeader *) dst;
882 hdr->version = htonl (2 | HEADER_COMPRESSED);
883 hdr->size = htonl (left);
884 hdr->entries = htonl (md->item_count - i);
885 memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
889 rlen = clen + sizeof (struct MetaDataHeader);
894 dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
895 hdr = (struct MetaDataHeader *) dst;
896 hdr->version = htonl (2);
897 hdr->entries = htonl (md->item_count - i);
898 hdr->size = htonl (left);
899 memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
901 rlen = left + sizeof (struct MetaDataHeader);
905 if (GNUNET_YES == comp)
906 memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
908 memcpy (*target, dst, left + sizeof (struct MetaDataHeader));
918 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
922 return GNUNET_SYSERR;
925 /* next iteration: ignore the corresponding meta data at the
926 * end and try again without it */
927 left -= sizeof (struct MetaDataEntry);
928 left -= pos->data_size;
929 if (NULL != pos->plugin_name)
930 left -= strlen (pos->plugin_name) + 1;
931 if (NULL != pos->mime_type)
932 left -= strlen (pos->mime_type) + 1;
934 GNUNET_free_non_null (cdata);
941 /* nothing fit, only write header! */
942 ihdr.version = htonl (2);
943 ihdr.entries = htonl (0);
944 ihdr.size = htonl (0);
946 *target = (char *) GNUNET_new (struct MetaDataHeader);
947 memcpy (*target, &ihdr, sizeof (struct MetaDataHeader));
948 return sizeof (struct MetaDataHeader);
953 * Get the size of the full meta-data in serialized form.
955 * @param md metadata to inspect
956 * @return number of bytes needed for serialization, -1 on error
959 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
960 GNUNET_CONTAINER_MetaData *md)
965 if (NULL != md->sbuf)
966 return md->sbuf_size;
969 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED,
970 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
978 * Decompress input, return the decompressed data
979 * as output, set outputSize to the number of bytes
982 * @param input compressed data
983 * @param inputSize number of bytes in input
984 * @param outputSize expected size of the output
985 * @return NULL on error
988 decompress (const char *input, size_t inputSize, size_t outputSize)
994 output = GNUNET_malloc (olen);
996 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize))
998 GNUNET_free (output);
1004 * Deserialize meta-data. Initializes md.
1006 * @param input buffer with the serialized metadata
1007 * @param size number of bytes available in input
1008 * @return MD on success, NULL on error (i.e.
1011 struct GNUNET_CONTAINER_MetaData *
1012 GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
1014 struct GNUNET_CONTAINER_MetaData *md;
1015 struct MetaDataHeader hdr;
1016 struct MetaDataEntry ent;
1029 const char *meta_data;
1030 const char *plugin_name;
1031 const char *mime_type;
1032 enum EXTRACTOR_MetaFormat format;
1034 if (size < sizeof (struct MetaDataHeader))
1036 memcpy (&hdr, input, sizeof (struct MetaDataHeader));
1037 version = ntohl (hdr.version) & HEADER_VERSION_MASK;
1038 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
1041 return NULL; /* null pointer */
1044 GNUNET_break_op (0); /* unsupported version */
1048 ic = ntohl (hdr.entries);
1049 dataSize = ntohl (hdr.size);
1050 if ((sizeof (struct MetaDataEntry) * ic) > dataSize)
1052 GNUNET_break_op (0);
1058 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1060 /* make sure we don't blow our memory limit because of a mal-formed
1062 GNUNET_break_op (0);
1066 decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
1067 size - sizeof (struct MetaDataHeader), dataSize);
1070 GNUNET_break_op (0);
1078 cdata = (const char *) &input[sizeof (struct MetaDataHeader)];
1079 if (dataSize != size - sizeof (struct MetaDataHeader))
1081 GNUNET_break_op (0);
1086 md = GNUNET_CONTAINER_meta_data_create ();
1087 left = dataSize - ic * sizeof (struct MetaDataEntry);
1088 mdata = &cdata[ic * sizeof (struct MetaDataEntry)];
1089 for (i = 0; i < ic; i++)
1091 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
1092 sizeof (struct MetaDataEntry));
1093 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1094 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
1095 (EXTRACTOR_METAFORMAT_C_STRING != format) &&
1096 (EXTRACTOR_METAFORMAT_BINARY != format))
1098 GNUNET_break_op (0);
1101 dlen = ntohl (ent.data_size);
1102 plen = ntohl (ent.plugin_name_len);
1103 mlen = ntohl (ent.mime_type_len);
1106 GNUNET_break_op (0);
1110 meta_data = &mdata[left];
1111 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
1112 (EXTRACTOR_METAFORMAT_C_STRING == format))
1116 GNUNET_break_op (0);
1119 if ('\0' != meta_data[dlen - 1])
1121 GNUNET_break_op (0);
1127 GNUNET_break_op (0);
1131 if ((plen > 0) && ('\0' != mdata[left + plen - 1]))
1133 GNUNET_break_op (0);
1139 plugin_name = &mdata[left];
1143 GNUNET_break_op (0);
1147 if ((mlen > 0) && ('\0' != mdata[left + mlen - 1]))
1149 GNUNET_break_op (0);
1155 mime_type = &mdata[left];
1156 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1157 (enum EXTRACTOR_MetaType)
1158 ntohl (ent.type), format, mime_type,
1161 GNUNET_free_non_null (data);
1166 /* end of container_meta_data.c */