2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2008, 2009 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 2, 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 EXTRA_CHECKS ALLOW_EXTRA_CHECKS
39 EXTRACTOR_KeywordType type;
44 * Meta data to associate with a file, directory or namespace.
46 struct GNUNET_CONTAINER_MetaData
53 * Create a fresh struct CONTAINER_MetaData token.
55 struct GNUNET_CONTAINER_MetaData *
56 GNUNET_CONTAINER_meta_data_create ()
58 struct GNUNET_CONTAINER_MetaData *ret;
59 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MetaData));
69 GNUNET_CONTAINER_meta_data_destroy (struct GNUNET_CONTAINER_MetaData *md)
75 for (i = 0; i < md->itemCount; i++)
76 GNUNET_free (md->items[i].data);
77 GNUNET_array_grow (md->items, md->itemCount, 0);
82 * Add the current time as the publication date
86 GNUNET_CONTAINER_meta_data_add_publication_date (struct
87 GNUNET_CONTAINER_MetaData
91 struct GNUNET_TIME_Absolute t;
93 t = GNUNET_TIME_absolute_get ();
94 GNUNET_CONTAINER_meta_data_delete (md, EXTRACTOR_PUBLICATION_DATE, NULL);
95 dat = GNUNET_STRINGS_absolute_time_to_string (t);
96 GNUNET_CONTAINER_meta_data_insert (md, EXTRACTOR_PUBLICATION_DATE, dat);
102 * @return GNUNET_OK on success, GNUNET_SYSERR if this entry already exists
105 GNUNET_CONTAINER_meta_data_insert (struct GNUNET_CONTAINER_MetaData *md,
106 EXTRACTOR_KeywordType type,
112 GNUNET_assert (data != NULL);
113 for (idx = 0; idx < md->itemCount; idx++)
115 if ((md->items[idx].type == type) &&
116 (0 == strcmp (md->items[idx].data, data)))
117 return GNUNET_SYSERR;
120 GNUNET_array_grow (md->items, md->itemCount, md->itemCount + 1);
121 md->items[idx].type = type;
122 md->items[idx].data = p = GNUNET_strdup (data);
124 /* change OS native dir separators to unix '/' and others to '_' */
125 if (type == EXTRACTOR_FILENAME)
129 if (*p == DIR_SEPARATOR)
142 * @return GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md
145 GNUNET_CONTAINER_meta_data_delete (struct GNUNET_CONTAINER_MetaData *md,
146 EXTRACTOR_KeywordType type,
150 int ret = GNUNET_SYSERR;
151 for (idx = 0; idx < md->itemCount; idx++)
153 if ((md->items[idx].type == type) &&
154 ((data == NULL) || (0 == strcmp (md->items[idx].data, data))))
156 GNUNET_free (md->items[idx].data);
157 md->items[idx] = md->items[md->itemCount - 1];
158 GNUNET_array_grow (md->items, md->itemCount, md->itemCount - 1);
171 * Iterate over MD entries, excluding thumbnails.
173 * @return number of entries
176 GNUNET_CONTAINER_meta_data_get_contents (const struct
177 GNUNET_CONTAINER_MetaData *md,
178 GNUNET_CONTAINER_MetaDataProcessor
179 iterator, void *closure)
185 for (i = 0; i < md->itemCount; i++)
187 if (!EXTRACTOR_isBinaryType (md->items[i].type))
189 if ((iterator != NULL) &&
190 (GNUNET_OK != iterator (closure,
193 return GNUNET_SYSERR;
198 return (int) (md->itemCount - sub);
202 * Iterate over MD entries
204 * @return number of entries
207 GNUNET_CONTAINER_meta_data_get_by_type (const struct GNUNET_CONTAINER_MetaData
208 *md, EXTRACTOR_KeywordType type)
212 for (i = 0; i < md->itemCount; i++)
213 if (type == md->items[i].type)
214 return GNUNET_strdup (md->items[i].data);
219 * Iterate over MD entries
221 * @return number of entries
224 GNUNET_CONTAINER_meta_data_get_first_by_types (const struct
225 GNUNET_CONTAINER_MetaData *md,
230 EXTRACTOR_KeywordType type;
236 type = va_arg (args, EXTRACTOR_KeywordType);
239 ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type);
248 * Get a thumbnail from the meta-data (if present).
250 * @param thumb will be set to the thumbnail data. Must be
251 * freed by the caller!
252 * @return number of bytes in thumbnail, 0 if not available
255 GNUNET_CONTAINER_meta_data_get_thumbnail (const struct
256 GNUNET_CONTAINER_MetaData * md,
257 unsigned char **thumb)
264 GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_THUMBNAIL_DATA);
267 if (strlen (encoded) == 0)
269 GNUNET_free (encoded);
270 return 0; /* invalid */
273 ret = EXTRACTOR_binaryDecode (encoded, thumb, &size);
274 GNUNET_free (encoded);
281 * Duplicate struct GNUNET_CONTAINER_MetaData.
283 struct GNUNET_CONTAINER_MetaData *
284 GNUNET_CONTAINER_meta_data_duplicate (const struct GNUNET_CONTAINER_MetaData
288 struct GNUNET_CONTAINER_MetaData *ret;
292 ret = GNUNET_CONTAINER_meta_data_create ();
293 for (i = 0; i < md->itemCount; i++)
294 GNUNET_CONTAINER_meta_data_insert (ret, md->items[i].type,
300 * Extract meta-data from a file.
302 * @return GNUNET_SYSERR on error, otherwise the number
303 * of meta-data items obtained
306 GNUNET_CONTAINER_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
307 *md, const char *filename,
308 EXTRACTOR_ExtractorList *
311 EXTRACTOR_KeywordList *head;
312 EXTRACTOR_KeywordList *pos;
315 if (filename == NULL)
316 return GNUNET_SYSERR;
317 if (extractors == NULL)
319 head = EXTRACTOR_getKeywords (extractors, filename);
320 head = EXTRACTOR_removeDuplicateKeywords (head,
321 EXTRACTOR_DUPLICATES_REMOVE_UNKNOWN);
327 GNUNET_CONTAINER_meta_data_insert (md, pos->keywordType,
332 EXTRACTOR_freeKeywords (head);
337 tryCompression (char *data, unsigned int oldSize)
343 dlen = compressBound (oldSize);
345 dlen = oldSize + (oldSize / 100) + 20;
346 /* documentation says 100.1% oldSize + 12 bytes, but we
347 should be able to overshoot by more to be safe */
349 tmp = GNUNET_malloc (dlen);
350 if (Z_OK == compress2 ((Bytef *) tmp,
351 &dlen, (const Bytef *) data, oldSize, 9))
355 memcpy (data, tmp, dlen);
365 * Decompress input, return the decompressed data
366 * as output, set outputSize to the number of bytes
369 * @return NULL on error
372 decompress (const char *input,
373 unsigned int inputSize, unsigned int outputSize)
379 output = GNUNET_malloc (olen);
380 if (Z_OK == uncompress ((Bytef *) output,
381 &olen, (const Bytef *) input, inputSize))
387 GNUNET_free (output);
393 * Flag in 'version' that indicates compressed meta-data.
395 #define HEADER_COMPRESSED 0x80000000
398 * Bits in 'version' that give the version number.
400 #define HEADER_VERSION_MASK 0x7FFFFFFF
402 struct MetaDataHeader
405 * The version of the MD serialization.
406 * The highest bit is used to indicate
409 * Version 0 is the current version;
410 * Version is 1 for a NULL pointer.
411 * Other version numbers are not yet defined.
416 * How many MD entries are there?
421 * Size of the MD (decompressed)
426 * This is followed by 'entries' values of type 'unsigned int' that
427 * correspond to EXTRACTOR_KeywordTypes. After that, the meta-data
428 * keywords follow (0-terminated). The MD block always ends with
429 * 0-termination, padding with 0 until a multiple of 8 bytes.
435 * Serialize meta-data to target.
437 * @param size maximum number of bytes available
438 * @param part is it ok to just write SOME of the
439 * meta-data to match the size constraint,
440 * possibly discarding some data?
441 * @return number of bytes written on success,
442 * GNUNET_SYSERR on error (typically: not enough
446 GNUNET_CONTAINER_meta_data_serialize (const struct GNUNET_CONTAINER_MetaData
447 *md, char *target, size_t max,
449 GNUNET_CONTAINER_MetaDataSerializationOptions
452 struct MetaDataHeader *hdr;
459 if (max < sizeof (struct MetaDataHeader))
460 return GNUNET_SYSERR; /* far too small */
461 ic = md ? md->itemCount : 0;
465 size = sizeof (struct MetaDataHeader);
466 size += sizeof (uint32_t) * ic;
467 for (i = 0; i < ic; i++)
468 size += 1 + strlen (md->items[i].data);
469 while (size % 8 != 0)
471 hdr = GNUNET_malloc (size);
472 hdr->version = htonl (md == NULL ? 1 : 0);
473 hdr->entries = htonl (ic);
474 for (i = 0; i < ic; i++)
475 ((uint32_t *) &hdr[1])[i] =
476 htonl ((uint32_t) md->items[i].type);
477 pos = sizeof (struct MetaDataHeader);
478 pos += sizeof (unsigned int) * ic;
479 for (i = 0; i < ic; i++)
481 len = strlen (md->items[i].data) + 1;
482 memcpy (&((char *) hdr)[pos], md->items[i].data, len);
486 hdr->size = htonl (size);
487 if ((part & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS) == 0)
489 pos = tryCompression ((char *) &hdr[1],
490 size - sizeof (struct MetaDataHeader));
494 pos = size - sizeof (struct MetaDataHeader);
496 if (pos < size - sizeof (struct MetaDataHeader))
498 hdr->version = htonl (HEADER_COMPRESSED);
499 size = pos + sizeof (struct MetaDataHeader);
506 if ((part & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART) == 0)
508 return GNUNET_SYSERR; /* does not fit! */
510 /* partial serialization ok, try again with less meta-data */
512 ic = ic * 2 / 3; /* still far too big, make big reductions */
514 ic--; /* small steps, we're close */
516 GNUNET_assert (size <= max);
517 memcpy (target, hdr, size);
519 /* extra check: deserialize! */
522 struct GNUNET_CONTAINER_MetaData *mdx;
523 mdx = GNUNET_CONTAINER_meta_data_deserialize (target, size);
524 GNUNET_assert (NULL != mdx);
525 GNUNET_CONTAINER_meta_data_destroy (mdx);
532 * Estimate (!) the size of the meta-data in
533 * serialized form. The estimate MAY be higher
534 * than what is strictly needed.
537 GNUNET_CONTAINER_meta_data_get_serialized_size (const struct
538 GNUNET_CONTAINER_MetaData *md,
540 GNUNET_CONTAINER_MetaDataSerializationOptions
543 struct MetaDataHeader *hdr;
550 ic = md ? md->itemCount : 0;
551 size = sizeof (struct MetaDataHeader);
552 size += sizeof (uint32_t) * ic;
553 for (i = 0; i < ic; i++)
554 size += 1 + strlen (md->items[i].data);
555 while (size % 8 != 0)
557 hdr = GNUNET_malloc (size);
558 hdr->version = htonl (md == NULL ? 1 : 0);
559 hdr->entries = htonl (ic);
560 for (i = 0; i < ic; i++)
561 ((uint32_t *) &hdr[1])[i] = htonl ((uint32_t) md->items[i].type);
562 pos = sizeof (struct MetaDataHeader);
563 pos += sizeof (uint32_t) * ic;
564 for (i = 0; i < ic; i++)
566 len = strlen (md->items[i].data) + 1;
567 memcpy (&((char *) hdr)[pos], md->items[i].data, len);
570 if ((part & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS) == 0)
573 tryCompression ((char *) &hdr[1],
574 size - sizeof (struct MetaDataHeader));
578 pos = size - sizeof (struct MetaDataHeader);
580 if (pos < size - sizeof (struct MetaDataHeader))
581 size = pos + sizeof (struct MetaDataHeader);
587 * Deserialize meta-data. Initializes md.
588 * @param size number of bytes available
589 * @return MD on success, NULL on error (i.e.
592 struct GNUNET_CONTAINER_MetaData *
593 GNUNET_CONTAINER_meta_data_deserialize (const char *input, size_t size)
595 struct GNUNET_CONTAINER_MetaData *md;
596 const struct MetaDataHeader *hdr;
607 if (size < sizeof (struct MetaDataHeader))
609 hdr = (const struct MetaDataHeader *) input;
610 version = ntohl (MAKE_UNALIGNED (hdr->version)) & HEADER_VERSION_MASK;
612 return NULL; /* null pointer */
615 GNUNET_break_op (0); /* unsupported version */
618 ic = ntohl (MAKE_UNALIGNED (hdr->entries));
620 (ntohl (MAKE_UNALIGNED (hdr->version)) & HEADER_COMPRESSED) != 0;
624 ntohl (MAKE_UNALIGNED (hdr->size)) - sizeof (struct MetaDataHeader);
625 if (dataSize > 2 * 1042 * 1024)
628 return NULL; /* only 2 MB allowed [to make sure we don't blow
629 our memory limit because of a mal-formed
633 decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
634 size - sizeof (struct MetaDataHeader), dataSize);
645 cdata = (const char *) &hdr[1];
646 dataSize = size - sizeof (struct MetaDataHeader);
647 if (size != ntohl (MAKE_UNALIGNED (hdr->size)))
654 if ((sizeof (uint32_t) * ic + ic) > dataSize)
659 if ((ic > 0) && (cdata[dataSize - 1] != '\0'))
665 md = GNUNET_CONTAINER_meta_data_create ();
666 GNUNET_array_grow (md->items, md->itemCount, ic);
668 pos = sizeof (uint32_t) * ic;
669 while ((pos < dataSize) && (i < ic))
671 len = strlen (&cdata[pos]) + 1;
672 md->items[i].type = (EXTRACTOR_KeywordType)
673 ntohl (MAKE_UNALIGNED (((const uint32_t *) cdata)[i]));
674 md->items[i].data = GNUNET_strdup (&cdata[pos]);
680 GNUNET_CONTAINER_meta_data_destroy (md);
683 GNUNET_free_non_null (data);
686 GNUNET_free_non_null (data);
687 return NULL; /* size too small */
691 * Test if two MDs are equal.
694 GNUNET_CONTAINER_meta_data_test_equal (const struct GNUNET_CONTAINER_MetaData
696 const struct GNUNET_CONTAINER_MetaData
703 if (md1->itemCount != md2->itemCount)
705 for (i = 0; i < md1->itemCount; i++)
708 for (j = 0; j < md2->itemCount; j++)
709 if ((md1->items[i].type == md2->items[j].type) &&
710 (0 == strcmp (md1->items[i].data, md2->items[j].data)))
715 if (found == GNUNET_NO)
722 /* end of container_meta_data.c */