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_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_strings_lib.h"
31 #include "gnunet_time_lib.h"
32 #include <extractor.h>
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
43 * This is a doubly linked list.
45 struct MetaItem *next;
48 * This is a doubly linked list.
50 struct MetaItem *prev;
53 * Name of the extracting plugin.
63 * The actual meta data.
68 * Number of bytes in 'data'.
73 * Type of the meta data.
75 enum EXTRACTOR_MetaType type;
78 * Format of the meta data.
80 enum EXTRACTOR_MetaFormat format;
85 * Meta data to associate with a file, directory or namespace.
87 struct GNUNET_CONTAINER_MetaData
90 * Head of linked list of the meta data items.
92 struct MetaItem *items_head;
95 * Tail of linked list of the meta data items.
97 struct MetaItem *items_tail;
100 * Complete serialized and compressed buffer of the items.
101 * NULL if we have not computed that buffer yet.
106 * Number of bytes in 'sbuf'. 0 if the buffer is stale.
111 * Number of items in the linked list.
113 unsigned int item_count;
119 * Create a fresh struct CONTAINER_MetaData token.
121 * @return empty meta-data container
123 struct GNUNET_CONTAINER_MetaData *
124 GNUNET_CONTAINER_meta_data_create ()
126 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MetaData));
131 * Free meta data item.
133 * @param mi item to free
136 meta_item_free (struct MetaItem *mi)
138 GNUNET_free_non_null (mi->plugin_name);
139 GNUNET_free_non_null (mi->mime_type);
140 GNUNET_free_non_null (mi->data);
146 * The meta data has changed, invalidate its serialization
149 * @param md meta data that changed
152 invalidate_sbuf (struct GNUNET_CONTAINER_MetaData *md)
154 if (NULL == md->sbuf)
156 GNUNET_free (md->sbuf);
165 * @param md what to free
168 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
170 struct MetaItem *pos;
174 while (NULL != (pos = md->items_head))
176 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
177 meta_item_free (pos);
179 GNUNET_free_non_null (md->sbuf);
185 * Remove all items in the container.
187 * @param md metadata to manipulate
190 GNUNET_CONTAINER_meta_data_clear (struct GNUNET_CONTAINER_MetaData *md)
196 while (NULL != (mi = md->items_head))
198 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, mi);
201 GNUNET_free_non_null (md->sbuf);
202 memset (md, 0, sizeof (struct GNUNET_CONTAINER_MetaData));
207 * Test if two MDs are equal. We consider them equal if
208 * the meta types, formats and content match (we do not
209 * include the mime types and plugins names in this
212 * @param md1 first value to check
213 * @param md2 other value to check
214 * @return GNUNET_YES if they are equal
217 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
219 const struct GNUNET_CONTAINER_MetaData
228 if (md1->item_count != md2->item_count)
230 for (i = md1->items_head; NULL != i; i = i->next)
233 for (j = md2->items_head; NULL != j; j = j->next)
235 if ((i->type == j->type) && (i->format == j->format) &&
236 (i->data_size == j->data_size) &&
237 (0 == memcmp (i->data, j->data, i->data_size)))
242 if (j->data_size < i->data_size)
243 break; /* elements are sorted by (decreasing) size... */
245 if (GNUNET_NO == found)
253 * Extend metadata. Note that the list of meta data items is
254 * sorted by size (largest first).
256 * @param md metadata to extend
257 * @param plugin_name name of the plugin that produced this value;
258 * special values can be used (i.e. '<zlib>' for zlib being
259 * used in the main libextractor library and yielding
261 * @param type libextractor-type describing the meta data
262 * @param format basic format information about data
263 * @param data_mime_type mime-type of data (not of the original file);
264 * can be NULL (if mime-type is not known)
265 * @param data actual meta-data found
266 * @param data_size number of bytes in data
267 * @return GNUNET_OK on success, GNUNET_SYSERR if this entry already exists
268 * data_mime_type and plugin_name are not considered for "exists" checks
271 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
272 const char *plugin_name,
273 enum EXTRACTOR_MetaType type,
274 enum EXTRACTOR_MetaFormat format,
275 const char *data_mime_type, const char *data,
278 struct MetaItem *pos;
282 for (pos = md->items_head; NULL != pos; pos = pos->next)
284 if (pos->data_size < data_size)
285 break; /* elements are sorted by size in the list */
286 if ((pos->type == type) && (pos->data_size == data_size) &&
287 (0 == memcmp (pos->data, data, data_size)))
289 if ((NULL == pos->mime_type) && (NULL != data_mime_type))
291 pos->mime_type = GNUNET_strdup (data_mime_type);
292 invalidate_sbuf (md);
294 if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) &&
295 (EXTRACTOR_METAFORMAT_UTF8 == format))
297 pos->format = EXTRACTOR_METAFORMAT_UTF8;
298 invalidate_sbuf (md);
300 return GNUNET_SYSERR;
304 mi = GNUNET_malloc (sizeof (struct MetaItem));
307 mi->data_size = data_size;
309 GNUNET_CONTAINER_DLL_insert_tail (md->items_head,
313 GNUNET_CONTAINER_DLL_insert_after (md->items_head,
318 (NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
319 mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
320 mi->data = GNUNET_malloc (data_size);
321 memcpy (mi->data, data, data_size);
322 /* change all dir separators to POSIX style ('/') */
323 if ( (EXTRACTOR_METATYPE_FILENAME == type) ||
324 (EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type) )
327 while (('\0' != *p) && (p < mi->data + data_size))
334 invalidate_sbuf (md);
340 * Merge given meta data.
342 * @param cls the 'struct GNUNET_CONTAINER_MetaData' to merge into
343 * @param plugin_name name of the plugin that produced this value;
344 * special values can be used (i.e. '<zlib>' for zlib being
345 * used in the main libextractor library and yielding
347 * @param type libextractor-type describing the meta data
348 * @param format basic format information about data
349 * @param data_mime_type mime-type of data (not of the original file);
350 * can be NULL (if mime-type is not known)
351 * @param data actual meta-data found
352 * @param data_size number of bytes in data
353 * @return 0 (to continue)
356 merge_helper (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
357 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
358 const char *data, size_t data_size)
360 struct GNUNET_CONTAINER_MetaData *md = cls;
362 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
363 data_mime_type, data, data_size);
369 * Extend metadata. Merges the meta data from the second argument
370 * into the first, discarding duplicate key-value pairs.
372 * @param md metadata to extend
373 * @param in metadata to merge
376 GNUNET_CONTAINER_meta_data_merge (struct GNUNET_CONTAINER_MetaData *md,
377 const struct GNUNET_CONTAINER_MetaData *in)
379 GNUNET_CONTAINER_meta_data_iterate (in, &merge_helper, md);
386 * @param md metadata to manipulate
387 * @param type type of the item to remove
388 * @param data specific value to remove, NULL to remove all
389 * entries of the given type
390 * @param data_size number of bytes in data
391 * @return GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md
394 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
395 enum EXTRACTOR_MetaType type,
396 const char *data, size_t data_size)
398 struct MetaItem *pos;
400 for (pos = md->items_head; NULL != pos; pos = pos->next)
402 if (pos->data_size < data_size)
403 break; /* items are sorted by (decreasing) size */
404 if ((pos->type == type) &&
406 ((pos->data_size == data_size) &&
407 (0 == memcmp (pos->data, data, data_size)))))
409 GNUNET_CONTAINER_DLL_remove (md->items_head, md->items_tail, pos);
410 meta_item_free (pos);
412 invalidate_sbuf (md);
416 return GNUNET_SYSERR;
421 * Add the current time as the publication date
424 * @param md metadata to modify
427 GNUNET_CONTAINER_meta_data_add_publication_date (struct
428 GNUNET_CONTAINER_MetaData *md)
431 struct GNUNET_TIME_Absolute t;
433 t = GNUNET_TIME_absolute_get ();
434 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_METATYPE_PUBLICATION_DATE,
436 dat = GNUNET_STRINGS_absolute_time_to_string (t);
437 GNUNET_CONTAINER_meta_data_insert (md, "<gnunet>",
438 EXTRACTOR_METATYPE_PUBLICATION_DATE,
439 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
440 dat, strlen (dat) + 1);
445 * Iterate over MD entries.
447 * @param md metadata to inspect
448 * @param iter function to call on each entry
449 * @param iter_cls closure for iterator
450 * @return number of entries
453 GNUNET_CONTAINER_meta_data_iterate (const struct GNUNET_CONTAINER_MetaData *md,
454 EXTRACTOR_MetaDataProcessor iter,
457 struct MetaItem *pos;
462 return md->item_count;
463 for (pos = md->items_head; NULL != pos; pos = pos->next)
465 iter (iter_cls, pos->plugin_name, pos->type, pos->format,
466 pos->mime_type, pos->data, pos->data_size))
467 return md->item_count;
468 return md->item_count;
473 * Get the first MD entry of the given type. Caller
474 * is responsible for freeing the return value.
475 * Also, only meta data items that are strings (0-terminated)
476 * are returned by this function.
478 * @param md metadata to inspect
479 * @param type type to look for
480 * @return NULL if no entry was found
483 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
484 *md, enum EXTRACTOR_MetaType type)
486 struct MetaItem *pos;
490 for (pos = md->items_head; NULL != pos; pos = pos->next)
491 if ((type == pos->type) &&
492 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
493 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
494 return GNUNET_strdup (pos->data);
500 * Get the first matching MD entry of the given types. Caller is
501 * responsible for freeing the return value. Also, only meta data
502 * items that are strings (0-terminated) are returned by this
505 * @param md metadata to inspect
506 * @param ... -1-terminated list of types
507 * @return NULL if we do not have any such entry,
508 * otherwise client is responsible for freeing the value!
511 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
512 GNUNET_CONTAINER_MetaData *md,
517 enum EXTRACTOR_MetaType type;
525 type = va_arg (args, enum EXTRACTOR_MetaType);
528 if (NULL != (ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type)))
537 * Get a thumbnail from the meta-data (if present).
539 * @param md metadata to get the thumbnail from
540 * @param thumb will be set to the thumbnail data. Must be
541 * freed by the caller!
542 * @return number of bytes in thumbnail, 0 if not available
545 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct GNUNET_CONTAINER_MetaData
546 * md, unsigned char **thumb)
548 struct MetaItem *pos;
549 struct MetaItem *match;
554 for (pos = md->items_head; NULL != pos; pos = pos->next)
556 if ((NULL != pos->mime_type) &&
557 (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
558 (EXTRACTOR_METAFORMAT_BINARY == pos->format))
562 else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
563 (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
567 if ((NULL == match) || (0 == match->data_size))
569 *thumb = GNUNET_malloc (match->data_size);
570 memcpy (*thumb, match->data, match->data_size);
571 return match->data_size;
576 * Duplicate struct GNUNET_CONTAINER_MetaData.
578 * @param md what to duplicate
579 * @return duplicate meta-data container
581 struct GNUNET_CONTAINER_MetaData *
582 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
585 struct GNUNET_CONTAINER_MetaData *ret;
586 struct MetaItem *pos;
590 ret = GNUNET_CONTAINER_meta_data_create ();
591 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
592 GNUNET_CONTAINER_meta_data_insert (ret, pos->plugin_name, pos->type,
593 pos->format, pos->mime_type, pos->data,
601 * Try to compress the given block of data.
603 * @param data block to compress; if compression
604 * resulted in a smaller block, the first
605 * bytes of data are updated to the compressed
607 * @param oldSize number of bytes in data
608 * @param result set to the compressed data
609 * @param newSize set to size of result
610 * @return GNUNET_YES if compression reduce the size,
611 * GNUNET_NO if compression did not help
614 try_compression (const char *data, size_t oldSize, char **result,
621 dlen = compressBound (oldSize);
623 dlen = oldSize + (oldSize / 100) + 20;
624 /* documentation says 100.1% oldSize + 12 bytes, but we
625 * should be able to overshoot by more to be safe */
627 tmp = GNUNET_malloc (dlen);
629 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
644 * Flag in 'version' that indicates compressed meta-data.
646 #define HEADER_COMPRESSED 0x80000000
650 * Bits in 'version' that give the version number.
652 #define HEADER_VERSION_MASK 0x7FFFFFFF
656 * Header for serialized meta data.
658 struct MetaDataHeader
661 * The version of the MD serialization. The highest bit is used to
662 * indicate compression.
664 * Version 0 is traditional (pre-0.9) meta data (unsupported)
665 * Version is 1 for a NULL pointer
666 * Version 2 is for 0.9.x (and possibly higher)
667 * Other version numbers are not yet defined.
672 * How many MD entries are there?
677 * Size of the decompressed meta data.
682 * This is followed by 'entries' values of type 'struct MetaDataEntry'
683 * and then by 'entry' plugin names, mime-types and data blocks
684 * as specified in those meta data entries.
690 * Entry of serialized meta data.
695 * Meta data type. Corresponds to an 'enum EXTRACTOR_MetaType'
700 * Meta data format. Corresponds to an 'enum EXTRACTOR_MetaFormat'
705 * Number of bytes of meta data.
710 * Number of bytes in the plugin name including 0-terminator. 0 for NULL.
712 uint32_t plugin_name_len;
715 * Number of bytes in the mime type including 0-terminator. 0 for NULL.
717 uint32_t mime_type_len;
723 * Serialize meta-data to target.
725 * @param md metadata to serialize
726 * @param target where to write the serialized metadata;
727 * *target can be NULL, in which case memory is allocated
728 * @param max maximum number of bytes available in target
729 * @param opt is it ok to just write SOME of the
730 * meta-data to match the size constraint,
731 * possibly discarding some data?
732 * @return number of bytes written on success,
733 * GNUNET_SYSERR on error (typically: not enough
737 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
738 *md, char **target, size_t max,
740 GNUNET_CONTAINER_MetaDataSerializationOptions
743 struct GNUNET_CONTAINER_MetaData *vmd;
744 struct MetaItem *pos;
745 struct MetaDataHeader ihdr;
746 struct MetaDataHeader *hdr;
747 struct MetaDataEntry *ent;
762 if (max < sizeof (struct MetaDataHeader))
763 return GNUNET_SYSERR; /* far too small */
767 if (NULL != md->sbuf)
769 /* try to use serialization cache */
770 if (md->sbuf_size <= max)
773 *target = GNUNET_malloc (md->sbuf_size);
774 memcpy (*target, md->sbuf, md->sbuf_size);
775 return md->sbuf_size;
777 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
778 return GNUNET_SYSERR; /* can say that this will fail */
779 /* need to compute a partial serialization, sbuf useless ... */
783 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
785 msize += sizeof (struct MetaDataEntry);
786 msize += pos->data_size;
787 if (NULL != pos->plugin_name)
788 msize += strlen (pos->plugin_name) + 1;
789 if (NULL != pos->mime_type)
790 msize += strlen (pos->mime_type) + 1;
792 size = (size_t) msize;
795 GNUNET_break (0); /* integer overflow */
796 return GNUNET_SYSERR;
798 if (size >= GNUNET_MAX_MALLOC_CHECKED)
800 /* too large to be processed */
801 return GNUNET_SYSERR;
803 ent = GNUNET_malloc (size);
804 mdata = (char *) &ent[md->item_count];
805 off = size - (md->item_count * sizeof (struct MetaDataEntry));
807 for (pos = md->items_head; NULL != pos; pos = pos->next)
809 ent[i].type = htonl ((uint32_t) pos->type);
810 ent[i].format = htonl ((uint32_t) pos->format);
811 ent[i].data_size = htonl ((uint32_t) pos->data_size);
812 if (pos->plugin_name == NULL)
815 plen = strlen (pos->plugin_name) + 1;
816 ent[i].plugin_name_len = htonl ((uint32_t) plen);
817 if (pos->mime_type == NULL)
820 mlen = strlen (pos->mime_type) + 1;
821 ent[i].mime_type_len = htonl ((uint32_t) mlen);
822 off -= pos->data_size;
823 memcpy (&mdata[off], pos->data, pos->data_size);
825 if (pos->plugin_name != NULL)
826 memcpy (&mdata[off], pos->plugin_name, plen);
828 if (pos->mime_type != NULL)
829 memcpy (&mdata[off], pos->mime_type, mlen);
832 GNUNET_assert (0 == off);
838 for (pos = md->items_head; NULL != pos; pos = pos->next)
841 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
842 comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
844 if ((NULL == md->sbuf) && (0 == i))
846 /* fill 'sbuf'; this "modifies" md, but since this is only
847 * an internal cache we will cast away the 'const' instead
848 * of making the API look strange. */
849 vmd = (struct GNUNET_CONTAINER_MetaData *) md;
850 hdr = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
851 hdr->size = htonl (left);
852 hdr->entries = htonl (md->item_count);
853 if (GNUNET_YES == comp)
855 GNUNET_assert (clen < left);
856 hdr->version = htonl (2 | HEADER_COMPRESSED);
857 memcpy (&hdr[1], cdata, clen);
858 vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
862 hdr->version = htonl (2);
863 memcpy (&hdr[1], &ent[0], left);
864 vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
866 vmd->sbuf = (char *) hdr;
869 if (((left + sizeof (struct MetaDataHeader)) <= max) ||
870 ((GNUNET_YES == comp) && (clen <= max)))
872 /* success, this now fits! */
873 if (GNUNET_YES == comp)
876 dst = GNUNET_malloc (clen + sizeof (struct MetaDataHeader));
877 hdr = (struct MetaDataHeader *) dst;
878 hdr->version = htonl (2 | HEADER_COMPRESSED);
879 hdr->size = htonl (left);
880 hdr->entries = htonl (md->item_count - i);
881 memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
885 rlen = clen + sizeof (struct MetaDataHeader);
890 dst = GNUNET_malloc (left + sizeof (struct MetaDataHeader));
891 hdr = (struct MetaDataHeader *) dst;
892 hdr->version = htonl (2);
893 hdr->entries = htonl (md->item_count - i);
894 hdr->size = htonl (left);
895 memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
897 rlen = left + sizeof (struct MetaDataHeader);
901 if (GNUNET_YES == comp)
902 memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
904 memcpy (*target, dst, left + sizeof (struct MetaDataHeader));
914 if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
918 return GNUNET_SYSERR;
921 /* next iteration: ignore the corresponding meta data at the
922 * end and try again without it */
923 left -= sizeof (struct MetaDataEntry);
924 left -= pos->data_size;
925 if (NULL != pos->plugin_name)
926 left -= strlen (pos->plugin_name) + 1;
927 if (NULL != pos->mime_type)
928 left -= strlen (pos->mime_type) + 1;
930 GNUNET_free_non_null (cdata);
937 /* nothing fit, only write header! */
938 ihdr.version = htonl (2);
939 ihdr.entries = htonl (0);
940 ihdr.size = htonl (0);
942 *target = GNUNET_malloc (sizeof (struct MetaDataHeader));
943 memcpy (*target, &ihdr, sizeof (struct MetaDataHeader));
944 return sizeof (struct MetaDataHeader);
949 * Get the size of the full meta-data in serialized form.
951 * @param md metadata to inspect
952 * @return number of bytes needed for serialization, -1 on error
955 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
956 GNUNET_CONTAINER_MetaData *md)
961 if (NULL != md->sbuf)
962 return md->sbuf_size;
965 GNUNET_CONTAINER_meta_data_serialize (md, &ptr, GNUNET_MAX_MALLOC_CHECKED,
966 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
974 * Decompress input, return the decompressed data
975 * as output, set outputSize to the number of bytes
978 * @param input compressed data
979 * @param inputSize number of bytes in input
980 * @param outputSize expected size of the output
981 * @return NULL on error
984 decompress (const char *input, size_t inputSize, size_t outputSize)
990 output = GNUNET_malloc (olen);
992 uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize))
994 GNUNET_free (output);
1000 * Deserialize meta-data. Initializes md.
1002 * @param input buffer with the serialized metadata
1003 * @param size number of bytes available in input
1004 * @return MD on success, NULL on error (i.e.
1007 struct GNUNET_CONTAINER_MetaData *
1008 GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
1010 struct GNUNET_CONTAINER_MetaData *md;
1011 struct MetaDataHeader hdr;
1012 struct MetaDataEntry ent;
1025 const char *meta_data;
1026 const char *plugin_name;
1027 const char *mime_type;
1028 enum EXTRACTOR_MetaFormat format;
1030 if (size < sizeof (struct MetaDataHeader))
1032 memcpy (&hdr, input, sizeof (struct MetaDataHeader));
1033 version = ntohl (hdr.version) & HEADER_VERSION_MASK;
1034 compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
1037 return NULL; /* null pointer */
1040 GNUNET_break_op (0); /* unsupported version */
1044 ic = ntohl (hdr.entries);
1045 dataSize = ntohl (hdr.size);
1046 if ((sizeof (struct MetaDataEntry) * ic) > dataSize)
1048 GNUNET_break_op (0);
1054 if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
1056 /* make sure we don't blow our memory limit because of a mal-formed
1058 GNUNET_break_op (0);
1062 decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
1063 size - sizeof (struct MetaDataHeader), dataSize);
1066 GNUNET_break_op (0);
1074 cdata = (const char *) &input[sizeof (struct MetaDataHeader)];
1075 if (dataSize != size - sizeof (struct MetaDataHeader))
1077 GNUNET_break_op (0);
1082 md = GNUNET_CONTAINER_meta_data_create ();
1083 left = dataSize - ic * sizeof (struct MetaDataEntry);
1084 mdata = &cdata[ic * sizeof (struct MetaDataEntry)];
1085 for (i = 0; i < ic; i++)
1087 memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
1088 sizeof (struct MetaDataEntry));
1089 format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
1090 if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
1091 (EXTRACTOR_METAFORMAT_C_STRING != format) &&
1092 (EXTRACTOR_METAFORMAT_BINARY != format))
1094 GNUNET_break_op (0);
1097 dlen = ntohl (ent.data_size);
1098 plen = ntohl (ent.plugin_name_len);
1099 mlen = ntohl (ent.mime_type_len);
1102 GNUNET_break_op (0);
1106 meta_data = &mdata[left];
1107 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
1108 (EXTRACTOR_METAFORMAT_C_STRING == format))
1110 if ((0 == dlen) || ('\0' != mdata[left + dlen - 1]))
1112 GNUNET_break_op (0);
1118 GNUNET_break_op (0);
1122 if ((plen > 0) && ('\0' != mdata[left + plen - 1]))
1124 GNUNET_break_op (0);
1130 plugin_name = &mdata[left];
1134 GNUNET_break_op (0);
1138 if ((mlen > 0) && ('\0' != mdata[left + mlen - 1]))
1140 GNUNET_break_op (0);
1146 mime_type = &mdata[left];
1147 GNUNET_CONTAINER_meta_data_insert (md, plugin_name,
1148 (enum EXTRACTOR_MetaType)
1149 ntohl (ent.type), format, mime_type,
1152 GNUNET_free_non_null (data);
1157 /* end of container_meta_data.c */