GNUnet  0.20.0

Meta data processingGNU libextractor key-value pairs. More...

Collaboration diagram for Metadata:

Enumerations

enum  GNUNET_FS_MetaDataSerializationOptions { GNUNET_FS_META_DATA_SERIALIZE_FULL = 0 , GNUNET_FS_META_DATA_SERIALIZE_PART = 1 , GNUNET_FS_META_DATA_SERIALIZE_NO_COMPRESS = 2 }
 Options for metadata serialization. More...
 

Functions

struct GNUNET_FS_MetaDataGNUNET_FS_meta_data_create (void)
 Create a fresh meta data container. More...
 
struct GNUNET_FS_MetaDataGNUNET_FS_meta_data_duplicate (const struct GNUNET_FS_MetaData *md)
 Duplicate a MetaData token. More...
 
void GNUNET_FS_meta_data_destroy (struct GNUNET_FS_MetaData *md)
 Free meta data. More...
 
int GNUNET_FS_meta_data_test_equal (const struct GNUNET_FS_MetaData *md1, const struct GNUNET_FS_MetaData *md2)
 Test if two MDs are equal. More...
 
int GNUNET_FS_meta_data_insert (struct GNUNET_FS_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
 Extend metadata. More...
 
void GNUNET_FS_meta_data_merge (struct GNUNET_FS_MetaData *md, const struct GNUNET_FS_MetaData *in)
 Extend metadata. More...
 
int GNUNET_FS_meta_data_delete (struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type, const char *data, size_t data_size)
 Remove an item. More...
 
void GNUNET_FS_meta_data_clear (struct GNUNET_FS_MetaData *md)
 Remove all items in the container. More...
 
void GNUNET_FS_meta_data_add_publication_date (struct GNUNET_FS_MetaData *md)
 Add the current time as the publication date to the meta-data. More...
 
int GNUNET_FS_meta_data_iterate (const struct GNUNET_FS_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
 Iterate over MD entries. More...
 
char * GNUNET_FS_meta_data_get_by_type (const struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type)
 Get the first MD entry of the given type. More...
 
char * GNUNET_FS_meta_data_get_first_by_types (const struct GNUNET_FS_MetaData *md,...)
 Get the first matching MD entry of the given types. More...
 
size_t GNUNET_FS_meta_data_get_thumbnail (const struct GNUNET_FS_MetaData *md, unsigned char **thumb)
 Get a thumbnail from the meta-data (if present). More...
 
ssize_t GNUNET_FS_meta_data_serialize (const struct GNUNET_FS_MetaData *md, char **target, size_t max, enum GNUNET_FS_MetaDataSerializationOptions opt)
 Serialize meta-data to target. More...
 
ssize_t GNUNET_FS_meta_data_get_serialized_size (const struct GNUNET_FS_MetaData *md)
 Get the size of the full meta-data in serialized form. More...
 
struct GNUNET_FS_MetaDataGNUNET_FS_meta_data_deserialize (const char *input, size_t size)
 Deserialize meta-data. More...
 

Detailed Description

Meta data processing

GNU libextractor key-value pairs.

Enumeration Type Documentation

◆ GNUNET_FS_MetaDataSerializationOptions

Options for metadata serialization.

Enumerator
GNUNET_FS_META_DATA_SERIALIZE_FULL 

Serialize all of the data.

GNUNET_FS_META_DATA_SERIALIZE_PART 

If not enough space is available, it is acceptable to only serialize some of the metadata.

GNUNET_FS_META_DATA_SERIALIZE_NO_COMPRESS 

Speed is of the essence, do not allow compression.

Definition at line 3102 of file gnunet_fs_service.h.

3103 {
3109 
3116 
3122 };
@ GNUNET_FS_META_DATA_SERIALIZE_FULL
Serialize all of the data.
@ GNUNET_FS_META_DATA_SERIALIZE_PART
If not enough space is available, it is acceptable to only serialize some of the metadata.
@ GNUNET_FS_META_DATA_SERIALIZE_NO_COMPRESS
Speed is of the essence, do not allow compression.

Function Documentation

◆ GNUNET_FS_meta_data_create()

struct GNUNET_FS_MetaData* GNUNET_FS_meta_data_create ( void  )

Create a fresh meta data container.

Returns
empty meta-data container

Create a fresh meta data container.

Returns
empty meta-data container

Definition at line 132 of file meta_data.c.

133 {
134  return GNUNET_new (struct GNUNET_FS_MetaData);
135 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Meta data to associate with a file, directory or namespace.
Definition: meta_data.c:97

References GNUNET_new.

Referenced by extract_files(), get_file_information(), GNUNET_FS_directory_builder_create(), GNUNET_FS_file_information_create_from_reader(), GNUNET_FS_GETOPT_METADATA(), GNUNET_FS_meta_data_deserialize(), GNUNET_FS_meta_data_duplicate(), migrate_and_drop_metadata(), process_kblock(), and share_tree_trim().

Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_duplicate()

struct GNUNET_FS_MetaData* GNUNET_FS_meta_data_duplicate ( const struct GNUNET_FS_MetaData md)

Duplicate a MetaData token.

Parameters
mdwhat to duplicate
Returns
duplicate meta-data container

Duplicate a MetaData token.

Parameters
mdwhat to duplicate
Returns
duplicate meta-data container

Definition at line 532 of file meta_data.c.

534 {
535  struct GNUNET_FS_MetaData *ret;
536  struct MetaItem *pos;
537 
538  if (NULL == md)
539  return NULL;
541  for (pos = md->items_tail; NULL != pos; pos = pos->prev)
543  pos->format, pos->mime_type, pos->data,
544  pos->data_size);
545  return ret;
546 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_FS_MetaData * GNUNET_FS_meta_data_create()
Create a fresh struct FS_MetaData token.
Definition: meta_data.c:132
int GNUNET_FS_meta_data_insert(struct GNUNET_FS_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Extend metadata.
Definition: meta_data.c:259
struct MetaItem * items_tail
Tail of linked list of the meta data items.
Definition: meta_data.c:106
Meta data item.
Definition: meta_data.c:51
struct MetaItem * prev
This is a doubly linked list.
Definition: meta_data.c:60
enum EXTRACTOR_MetaType type
Type of the meta data.
Definition: meta_data.c:85
size_t data_size
Number of bytes in 'data'.
Definition: meta_data.c:80
char * mime_type
Mime-type of data.
Definition: meta_data.c:70
enum EXTRACTOR_MetaFormat format
Format of the meta data.
Definition: meta_data.c:90
char * data
The actual meta data.
Definition: meta_data.c:75
char * plugin_name
Name of the extracting plugin.
Definition: meta_data.c:65

References MetaItem::data, MetaItem::data_size, MetaItem::format, GNUNET_FS_meta_data_create(), GNUNET_FS_meta_data_insert(), GNUNET_FS_MetaData::items_tail, MetaItem::mime_type, MetaItem::plugin_name, MetaItem::prev, ret, and MetaItem::type.

Referenced by create_download_context(), GNUNET_FS_directory_builder_add(), GNUNET_FS_directory_builder_create(), GNUNET_FS_file_information_create_empty_directory(), GNUNET_FS_file_information_create_from_reader(), GNUNET_FS_probe(), GNUNET_FS_publish_ksk(), GNUNET_FS_publish_sks(), process_ksk_result(), and process_sks_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_destroy()

void GNUNET_FS_meta_data_destroy ( struct GNUNET_FS_MetaData md)

Free meta data.

Parameters
mdwhat to free

Definition at line 171 of file meta_data.c.

172 {
173  struct MetaItem *pos;
174 
175  if (NULL == md)
176  return;
177  while (NULL != (pos = md->items_head))
178  {
180  meta_item_free (pos);
181  }
182  GNUNET_free (md->sbuf);
183  GNUNET_free (md);
184 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_free(ptr)
Wrapper around free.
static void meta_item_free(struct MetaItem *mi)
Free meta data item.
Definition: meta_data.c:144
struct MetaItem * items_head
Head of linked list of the meta data items.
Definition: meta_data.c:101
char * sbuf
Complete serialized and compressed buffer of the items.
Definition: meta_data.c:112

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_FS_MetaData::items_head, GNUNET_FS_MetaData::items_tail, meta_item_free(), and GNUNET_FS_MetaData::sbuf.

Referenced by deserialize_search_result(), do_stop_task(), extract_files(), free_download_context(), free_result(), free_update_information_graph(), GNUNET_FS_directory_builder_add(), GNUNET_FS_directory_builder_finish(), GNUNET_FS_directory_list_contents(), GNUNET_FS_download_signal_suspend_(), GNUNET_FS_file_information_destroy(), GNUNET_FS_probe_stop(), GNUNET_FS_publish_ksk_cancel(), GNUNET_FS_publish_sks_cancel(), GNUNET_FS_share_tree_free(), process_kblock(), process_sblock(), publish_inspector(), read_update_information_graph(), search_result_free(), and search_result_suspend().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_test_equal()

int GNUNET_FS_meta_data_test_equal ( const struct GNUNET_FS_MetaData md1,
const struct GNUNET_FS_MetaData md2 
)

Test if two MDs are equal.

We consider them equal if the meta types, formats and content match (we do not include the mime types and plugins names in this consideration).

Parameters
md1first value to check
md2other value to check
Returns
GNUNET_YES if they are equal

Definition at line 205 of file meta_data.c.

209 {
210  struct MetaItem *i;
211  struct MetaItem *j;
212  int found;
213 
214  if (md1 == md2)
215  return GNUNET_YES;
216  if (md1->item_count != md2->item_count)
217  return GNUNET_NO;
218  for (i = md1->items_head; NULL != i; i = i->next)
219  {
220  found = GNUNET_NO;
221  for (j = md2->items_head; NULL != j; j = j->next)
222  {
223  if ((i->type == j->type) && (i->format == j->format) &&
224  (i->data_size == j->data_size) &&
225  (0 == memcmp (i->data, j->data, i->data_size)))
226  {
227  found = GNUNET_YES;
228  break;
229  }
230  if (j->data_size < i->data_size)
231  break; /* elements are sorted by (decreasing) size... */
232  }
233  if (GNUNET_NO == found)
234  return GNUNET_NO;
235  }
236  return GNUNET_YES;
237 }
@ GNUNET_YES
@ GNUNET_NO
unsigned int item_count
Number of items in the linked list.
Definition: meta_data.c:122
struct MetaItem * next
This is a doubly linked list.
Definition: meta_data.c:55

References MetaItem::data, MetaItem::data_size, MetaItem::format, GNUNET_NO, GNUNET_YES, GNUNET_FS_MetaData::item_count, GNUNET_FS_MetaData::items_head, MetaItem::next, and MetaItem::type.

◆ GNUNET_FS_meta_data_insert()

int GNUNET_FS_meta_data_insert ( struct GNUNET_FS_MetaData md,
const char *  plugin_name,
enum EXTRACTOR_MetaType  type,
enum EXTRACTOR_MetaFormat  format,
const char *  data_mime_type,
const char *  data,
size_t  data_size 
)

Extend metadata.

Parameters
mdmetadata to extend
plugin_namename of the plugin that produced this value; special values can be used (e.g. '<zlib>' for zlib being used in the main libextractor library and yielding meta data).
typelibextractor-type describing the meta data
formatbasic format information about data
data_mime_typemime-type of data (not of the original file); can be NULL (if mime-type is not known)
dataactual meta-data found
data_sizenumber of bytes in data
Returns
GNUNET_OK on success, GNUNET_SYSERR if this entry already exists data_mime_type and plugin_name are not considered for "exists" checks

Note that the list of meta data items is sorted by size (largest first).

Parameters
mdmetadata to extend
plugin_namename of the plugin that produced this value; special values can be used (e.g. '<zlib>' for zlib being used in the main libextractor library and yielding meta data).
typelibextractor-type describing the meta data
formatbasic format information about data
data_mime_typemime-type of data (not of the original file); can be NULL (if mime-type is not known)
dataactual meta-data found
data_sizenumber of bytes in data
Returns
GNUNET_OK on success, GNUNET_SYSERR if this entry already exists data_mime_type and plugin_name are not considered for "exists" checks

Definition at line 259 of file meta_data.c.

265 {
266  struct MetaItem *pos;
267  struct MetaItem *mi;
268  char *p;
269 
270  if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
271  (EXTRACTOR_METAFORMAT_C_STRING == format))
272  GNUNET_break ('\0' == data[data_size - 1]);
273 
274  for (pos = md->items_head; NULL != pos; pos = pos->next)
275  {
276  if (pos->data_size < data_size)
277  break; /* elements are sorted by size in the list */
278  if ((pos->type == type) && (pos->data_size == data_size) &&
279  (0 == memcmp (pos->data, data, data_size)))
280  {
281  if ((NULL == pos->mime_type) && (NULL != data_mime_type))
282  {
283  pos->mime_type = GNUNET_strdup (data_mime_type);
284  invalidate_sbuf (md);
285  }
286  if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) &&
287  (EXTRACTOR_METAFORMAT_UTF8 == format))
288  {
289  pos->format = EXTRACTOR_METAFORMAT_UTF8;
290  invalidate_sbuf (md);
291  }
292  return GNUNET_SYSERR;
293  }
294  }
295  md->item_count++;
296  mi = GNUNET_new (struct MetaItem);
297  mi->type = type;
298  mi->format = format;
299  mi->data_size = data_size;
300  if (NULL == pos)
302  md->items_tail,
303  mi);
304  else
306  md->items_tail,
307  pos->prev,
308  mi);
309  mi->mime_type =
310  (NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
311  mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
312  mi->data = GNUNET_malloc (data_size);
314  /* change all dir separators to POSIX style ('/') */
315  if ((EXTRACTOR_METATYPE_FILENAME == type) ||
317  {
318  p = mi->data;
319  while (('\0' != *p) && (p < mi->data + data_size))
320  {
321  if ('\\' == *p)
322  *p = '/';
323  p++;
324  }
325  }
326  invalidate_sbuf (md);
327  return GNUNET_OK;
328 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t data
The data value.
static char * plugin_name
Name of our plugin.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
#define GNUNET_CONTAINER_DLL_insert_after(head, tail, other, element)
Insert an element into a DLL after the given other element.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_malloc(size)
Wrapper around malloc.
static void invalidate_sbuf(struct GNUNET_FS_MetaData *md)
The meta data has changed, invalidate its serialization buffer.
Definition: meta_data.c:160
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References MetaItem::data, data, data_size, MetaItem::data_size, EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, MetaItem::format, GNUNET_break, GNUNET_CONTAINER_DLL_insert_after, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, invalidate_sbuf(), GNUNET_FS_MetaData::item_count, GNUNET_FS_MetaData::items_head, GNUNET_FS_MetaData::items_tail, MetaItem::mime_type, MetaItem::next, p, plugin_name, MetaItem::plugin_name, MetaItem::prev, MetaItem::type, and type.

Referenced by GNUNET_FS_directory_builder_add(), GNUNET_FS_file_information_create_from_file(), GNUNET_FS_GETOPT_METADATA(), GNUNET_FS_meta_data_add_publication_date(), GNUNET_FS_meta_data_deserialize(), GNUNET_FS_meta_data_duplicate(), GNUNET_FS_meta_data_make_directory(), merge_helper(), migrate_and_drop_metadata(), process_helper_msgs(), and share_tree_trim().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_merge()

void GNUNET_FS_meta_data_merge ( struct GNUNET_FS_MetaData md,
const struct GNUNET_FS_MetaData in 
)

Extend metadata.

Merges the meta data from the second argument into the first, discarding duplicate key-value pairs.

Parameters
mdmetadata to extend
inmetadata to merge

Definition at line 361 of file meta_data.c.

363 {
365 }
int GNUNET_FS_meta_data_iterate(const struct GNUNET_FS_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.
Definition: meta_data.c:423
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_size)
Merge given meta data.
Definition: meta_data.c:348

References GNUNET_FS_meta_data_iterate(), and merge_helper().

Referenced by process_ksk_result(), and publish_inspector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_delete()

int GNUNET_FS_meta_data_delete ( struct GNUNET_FS_MetaData md,
enum EXTRACTOR_MetaType  type,
const char *  data,
size_t  data_size 
)

Remove an item.

Parameters
mdmetadata to manipulate
typetype of the item to remove
dataspecific value to remove, NULL to remove all entries of the given type
data_sizenumber of bytes in data
Returns
GNUNET_OK on success, GNUNET_SYSERR if the item does not exist in md

Definition at line 369 of file meta_data.c.

372 {
373  struct MetaItem *pos;
374 
375  for (pos = md->items_head; NULL != pos; pos = pos->next)
376  {
377  if (pos->data_size < data_size)
378  break; /* items are sorted by (decreasing) size */
379  if ((pos->type == type) &&
380  ((NULL == data) ||
381  ((pos->data_size == data_size) &&
382  (0 == memcmp (pos->data, data, data_size)))))
383  {
385  meta_item_free (pos);
386  md->item_count--;
387  invalidate_sbuf (md);
388  return GNUNET_OK;
389  }
390  }
391  return GNUNET_SYSERR;
392 }

References MetaItem::data, data, data_size, MetaItem::data_size, GNUNET_CONTAINER_DLL_remove, GNUNET_OK, GNUNET_SYSERR, invalidate_sbuf(), GNUNET_FS_MetaData::item_count, GNUNET_FS_MetaData::items_head, GNUNET_FS_MetaData::items_tail, meta_item_free(), MetaItem::next, MetaItem::type, and type.

Referenced by get_file_information(), GNUNET_FS_meta_data_add_publication_date(), and process_helper_msgs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_clear()

void GNUNET_FS_meta_data_clear ( struct GNUNET_FS_MetaData md)

Remove all items in the container.

Parameters
mdmetadata to manipulate

Definition at line 188 of file meta_data.c.

189 {
190  struct MetaItem *mi;
191 
192  if (NULL == md)
193  return;
194  while (NULL != (mi = md->items_head))
195  {
197  meta_item_free (mi);
198  }
199  GNUNET_free (md->sbuf);
200  memset (md, 0, sizeof(struct GNUNET_FS_MetaData));
201 }

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_FS_MetaData::items_head, GNUNET_FS_MetaData::items_tail, meta_item_free(), and GNUNET_FS_MetaData::sbuf.

Here is the call graph for this function:

◆ GNUNET_FS_meta_data_add_publication_date()

void GNUNET_FS_meta_data_add_publication_date ( struct GNUNET_FS_MetaData md)

Add the current time as the publication date to the meta-data.

Parameters
mdmetadata to modify

Definition at line 396 of file meta_data.c.

398 {
399  const char *dat;
400  struct GNUNET_TIME_Absolute t;
401 
404  EXTRACTOR_METATYPE_PUBLICATION_DATE,
405  NULL, 0);
407  GNUNET_FS_meta_data_insert (md, "<gnunet>",
408  EXTRACTOR_METATYPE_PUBLICATION_DATE,
409  EXTRACTOR_METAFORMAT_UTF8, "text/plain",
410  dat, strlen (dat) + 1);
411 }
static struct GNUNET_SCHEDULER_Task * t
Main task.
int GNUNET_FS_meta_data_delete(struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type, const char *data, size_t data_size)
Remove an item.
Definition: meta_data.c:369
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:616
Time for absolute times used by GNUnet, in microseconds.

References GNUNET_FS_meta_data_delete(), GNUNET_FS_meta_data_insert(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_get(), and t.

Referenced by publish_inspector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_iterate()

int GNUNET_FS_meta_data_iterate ( const struct GNUNET_FS_MetaData md,
EXTRACTOR_MetaDataProcessor  iter,
void *  iter_cls 
)

Iterate over MD entries.

Parameters
mdmetadata to inspect
iterfunction to call on each entry, return 0 to continue to iterate and 1 to abort iteration in this function (GNU libextractor API!)
iter_clsclosure for iter
Returns
number of entries
Parameters
mdmetadata to inspect
iterfunction to call on each entry
iter_clsclosure for iterator
Returns
number of entries

Definition at line 423 of file meta_data.c.

426 {
427  struct MetaItem *pos;
428 
429  if (NULL == md)
430  return 0;
431  if (NULL == iter)
432  return md->item_count;
433  for (pos = md->items_head; NULL != pos; pos = pos->next)
434  if (0 !=
435  iter (iter_cls, pos->plugin_name, pos->type, pos->format,
436  pos->mime_type, pos->data, pos->data_size))
437  return md->item_count;
438  return md->item_count;
439 }

References MetaItem::data, MetaItem::data_size, MetaItem::format, GNUNET_FS_MetaData::item_count, GNUNET_FS_MetaData::items_head, MetaItem::mime_type, MetaItem::next, MetaItem::plugin_name, and MetaItem::type.

Referenced by GNUNET_FS_directory_list_contents(), GNUNET_FS_download_start_task_(), GNUNET_FS_meta_data_merge(), GNUNET_FS_uri_ksk_create_from_meta_data(), print_entry(), print_search_result(), publish_inspector(), and share_tree_trim().

Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_get_by_type()

char* GNUNET_FS_meta_data_get_by_type ( const struct GNUNET_FS_MetaData md,
enum EXTRACTOR_MetaType  type 
)

Get the first MD entry of the given type.

Caller is responsible for freeing the return value. Also, only meta data items that are strings (0-terminated) are returned by this function.

Parameters
mdmetadata to inspect
typetype to look for
Returns
NULL if no entry was found

Definition at line 443 of file meta_data.c.

446 {
447  struct MetaItem *pos;
448 
449  if (NULL == md)
450  return NULL;
451  for (pos = md->items_head; NULL != pos; pos = pos->next)
452  if ((type == pos->type) &&
453  ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
454  (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
455  return GNUNET_strdup (pos->data);
456  return NULL;
457 }

References MetaItem::data, MetaItem::format, GNUNET_strdup, GNUNET_FS_MetaData::items_head, MetaItem::next, MetaItem::type, and type.

Referenced by GNUNET_FS_directory_list_contents(), GNUNET_FS_meta_data_get_first_by_types(), GNUNET_FS_meta_data_make_directory(), GNUNET_FS_meta_data_suggest_filename(), GNUNET_FS_meta_data_test_for_directory(), GNUNET_FS_publish_main_(), print_entry(), print_search_result(), progress_cb(), and publish_inspector().

Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_get_first_by_types()

char* GNUNET_FS_meta_data_get_first_by_types ( const struct GNUNET_FS_MetaData md,
  ... 
)

Get the first matching MD entry of the given types.

Caller is responsible for freeing the return value. Also, only meta data items that are strings (0-terminated) are returned by this function.

Parameters
mdmetadata to inspect
...-1-terminated list of types
Returns
NULL if we do not have any such entry, otherwise client is responsible for freeing the value!

Definition at line 461 of file meta_data.c.

464 {
465  char *ret;
466  va_list args;
467  int type;
468 
469  if (NULL == md)
470  return NULL;
471  ret = NULL;
472  va_start (args, md);
473  while (1)
474  {
475  type = va_arg (args, int);
476  if (-1 == type)
477  break;
478  if (NULL != (ret = GNUNET_FS_meta_data_get_by_type (md, type)))
479  break;
480  }
481  va_end (args);
482  return ret;
483 }
char * GNUNET_FS_meta_data_get_by_type(const struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
Definition: meta_data.c:443

References consensus-simulation::args, GNUNET_FS_meta_data_get_by_type(), ret, and type.

Referenced by GNUNET_FS_meta_data_suggest_filename(), and GNUNET_FS_uri_ksk_create_from_meta_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_get_thumbnail()

size_t GNUNET_FS_meta_data_get_thumbnail ( const struct GNUNET_FS_MetaData md,
unsigned char **  thumb 
)

Get a thumbnail from the meta-data (if present).

Only matches meta data with mime type "image" and binary format.

Parameters
mdmetadata to inspect
thumbwill be set to the thumbnail data. Must be freed by the caller!
Returns
number of bytes in thumbnail, 0 if not available
Parameters
mdmetadata to get the thumbnail from
thumbwill be set to the thumbnail data. Must be freed by the caller!
Returns
number of bytes in thumbnail, 0 if not available

Definition at line 495 of file meta_data.c.

497 {
498  struct MetaItem *pos;
499  struct MetaItem *match;
500 
501  if (NULL == md)
502  return 0;
503  match = NULL;
504  for (pos = md->items_head; NULL != pos; pos = pos->next)
505  {
506  if ((NULL != pos->mime_type) &&
507  (0 == strncasecmp ("image/", pos->mime_type, strlen ("image/"))) &&
508  (EXTRACTOR_METAFORMAT_BINARY == pos->format))
509  {
510  if (NULL == match)
511  match = pos;
512  else if ((match->type != EXTRACTOR_METATYPE_THUMBNAIL) &&
513  (pos->type == EXTRACTOR_METATYPE_THUMBNAIL))
514  match = pos;
515  }
516  }
517  if ((NULL == match) || (0 == match->data_size))
518  return 0;
519  *thumb = GNUNET_malloc (match->data_size);
520  GNUNET_memcpy (*thumb, match->data, match->data_size);
521  return match->data_size;
522 }

References MetaItem::data, MetaItem::data_size, MetaItem::format, GNUNET_malloc, GNUNET_memcpy, GNUNET_FS_MetaData::items_head, MetaItem::mime_type, MetaItem::next, and MetaItem::type.

◆ GNUNET_FS_meta_data_serialize()

ssize_t GNUNET_FS_meta_data_serialize ( const struct GNUNET_FS_MetaData md,
char **  target,
size_t  max,
enum GNUNET_FS_MetaDataSerializationOptions  opt 
)

Serialize meta-data to target.

Parameters
mdmetadata to serialize
targetwhere to write the serialized metadata; *target can be NULL, in which case memory is allocated
maxmaximum number of bytes available
optis it ok to just write SOME of the meta-data to match the size constraint, possibly discarding some data?
Returns
number of bytes written on success, -1 on error (typically: not enough space)
Parameters
mdmetadata to serialize
targetwhere to write the serialized metadata; *target can be NULL, in which case memory is allocated
maxmaximum number of bytes available in target
optis it ok to just write SOME of the meta-data to match the size constraint, possibly discarding some data?
Returns
number of bytes written on success, GNUNET_SYSERR on error (typically: not enough space)

Definition at line 642 of file meta_data.c.

647 {
648  struct GNUNET_FS_MetaData *vmd;
649  struct MetaItem *pos;
650  struct MetaDataHeader ihdr;
651  struct MetaDataHeader *hdr;
652  struct MetaDataEntry *ent;
653  char *dst;
654  unsigned int i;
655  uint64_t msize;
656  size_t off;
657  char *mdata;
658  char *cdata;
659  size_t mlen;
660  size_t plen;
661  size_t size;
662  size_t left;
663  size_t clen;
664  size_t rlen;
665  int comp;
666 
667  if (max < sizeof(struct MetaDataHeader))
668  return GNUNET_SYSERR; /* far too small */
669  if (NULL == md)
670  return 0;
671 
672  if (NULL != md->sbuf)
673  {
674  /* try to use serialization cache */
675  if (md->sbuf_size <= max)
676  {
677  if (NULL == *target)
678  *target = GNUNET_malloc (md->sbuf_size);
679  GNUNET_memcpy (*target, md->sbuf, md->sbuf_size);
680  return md->sbuf_size;
681  }
682  if (0 == (opt & GNUNET_FS_META_DATA_SERIALIZE_PART))
683  return GNUNET_SYSERR; /* can say that this will fail */
684  /* need to compute a partial serialization, sbuf useless ... */
685  }
686  dst = NULL;
687  msize = 0;
688  for (pos = md->items_tail; NULL != pos; pos = pos->prev)
689  {
690  msize += sizeof(struct MetaDataEntry);
691  msize += pos->data_size;
692  if (NULL != pos->plugin_name)
693  msize += strlen (pos->plugin_name) + 1;
694  if (NULL != pos->mime_type)
695  msize += strlen (pos->mime_type) + 1;
696  }
697  size = (size_t) msize;
698  if (size != msize)
699  {
700  GNUNET_break (0); /* integer overflow */
701  return GNUNET_SYSERR;
702  }
704  {
705  /* too large to be processed */
706  return GNUNET_SYSERR;
707  }
708  ent = GNUNET_malloc (size);
709  mdata = (char *) &ent[md->item_count];
710  off = size - (md->item_count * sizeof(struct MetaDataEntry));
711  i = 0;
712  for (pos = md->items_head; NULL != pos; pos = pos->next)
713  {
714  ent[i].type = htonl ((uint32_t) pos->type);
715  ent[i].format = htonl ((uint32_t) pos->format);
716  ent[i].data_size = htonl ((uint32_t) pos->data_size);
717  if (NULL == pos->plugin_name)
718  plen = 0;
719  else
720  plen = strlen (pos->plugin_name) + 1;
721  ent[i].plugin_name_len = htonl ((uint32_t) plen);
722  if (NULL == pos->mime_type)
723  mlen = 0;
724  else
725  mlen = strlen (pos->mime_type) + 1;
726  ent[i].mime_type_len = htonl ((uint32_t) mlen);
727  off -= pos->data_size;
728  if ((EXTRACTOR_METAFORMAT_UTF8 == pos->format) ||
729  (EXTRACTOR_METAFORMAT_C_STRING == pos->format))
730  GNUNET_break ('\0' == pos->data[pos->data_size - 1]);
731  GNUNET_memcpy (&mdata[off], pos->data, pos->data_size);
732  off -= plen;
733  if (NULL != pos->plugin_name)
734  GNUNET_memcpy (&mdata[off], pos->plugin_name, plen);
735  off -= mlen;
736  if (NULL != pos->mime_type)
737  GNUNET_memcpy (&mdata[off], pos->mime_type, mlen);
738  i++;
739  }
740  GNUNET_assert (0 == off);
741 
742  clen = 0;
743  cdata = NULL;
744  left = size;
745  i = 0;
746  for (pos = md->items_head; NULL != pos; pos = pos->next)
747  {
748  comp = GNUNET_NO;
750  comp = GNUNET_try_compression ((const char *) &ent[i],
751  left,
752  &cdata,
753  &clen);
754 
755  if ((NULL == md->sbuf) && (0 == i))
756  {
757  /* fill 'sbuf'; this "modifies" md, but since this is only
758  * an internal cache we will cast away the 'const' instead
759  * of making the API look strange. */
760  vmd = (struct GNUNET_FS_MetaData *) md;
761  hdr = GNUNET_malloc (left + sizeof(struct MetaDataHeader));
762  hdr->size = htonl (left);
763  hdr->entries = htonl (md->item_count);
764  if (GNUNET_YES == comp)
765  {
766  GNUNET_assert (clen < left);
767  hdr->version = htonl (2 | HEADER_COMPRESSED);
768  GNUNET_memcpy (&hdr[1], cdata, clen);
769  vmd->sbuf_size = clen + sizeof(struct MetaDataHeader);
770  }
771  else
772  {
773  hdr->version = htonl (2);
774  GNUNET_memcpy (&hdr[1], &ent[0], left);
775  vmd->sbuf_size = left + sizeof(struct MetaDataHeader);
776  }
777  vmd->sbuf = (char *) hdr;
778  }
779 
780  if (((left + sizeof(struct MetaDataHeader)) <= max) ||
781  ((GNUNET_YES == comp) && (clen <= max)))
782  {
783  /* success, this now fits! */
784  if (GNUNET_YES == comp)
785  {
786  if (NULL == dst)
787  dst = GNUNET_malloc (clen + sizeof(struct MetaDataHeader));
788  hdr = (struct MetaDataHeader *) dst;
789  hdr->version = htonl (2 | HEADER_COMPRESSED);
790  hdr->size = htonl (left);
791  hdr->entries = htonl (md->item_count - i);
792  GNUNET_memcpy (&dst[sizeof(struct MetaDataHeader)], cdata, clen);
793  GNUNET_free (cdata);
794  cdata = NULL;
795  GNUNET_free (ent);
796  rlen = clen + sizeof(struct MetaDataHeader);
797  }
798  else
799  {
800  if (NULL == dst)
801  dst = GNUNET_malloc (left + sizeof(struct MetaDataHeader));
802  hdr = (struct MetaDataHeader *) dst;
803  hdr->version = htonl (2);
804  hdr->entries = htonl (md->item_count - i);
805  hdr->size = htonl (left);
806  GNUNET_memcpy (&dst[sizeof(struct MetaDataHeader)], &ent[i], left);
807  GNUNET_free (ent);
808  rlen = left + sizeof(struct MetaDataHeader);
809  }
810  if (NULL != *target)
811  {
812  if (GNUNET_YES == comp)
813  GNUNET_memcpy (*target, dst, clen + sizeof(struct MetaDataHeader));
814  else
815  GNUNET_memcpy (*target, dst, left + sizeof(struct MetaDataHeader));
816  GNUNET_free (dst);
817  }
818  else
819  {
820  *target = dst;
821  }
822  return rlen;
823  }
824 
825  if (0 == (opt & GNUNET_FS_META_DATA_SERIALIZE_PART))
826  {
827  /* does not fit! */
828  GNUNET_free (ent);
829  if (NULL != cdata)
830  GNUNET_free (cdata);
831  cdata = NULL;
832  return GNUNET_SYSERR;
833  }
834 
835  /* next iteration: ignore the corresponding meta data at the
836  * end and try again without it */
837  left -= sizeof(struct MetaDataEntry);
838  left -= pos->data_size;
839  if (NULL != pos->plugin_name)
840  left -= strlen (pos->plugin_name) + 1;
841  if (NULL != pos->mime_type)
842  left -= strlen (pos->mime_type) + 1;
843 
844  if (NULL != cdata)
845  GNUNET_free (cdata);
846  cdata = NULL;
847  i++;
848  }
849  GNUNET_free (ent);
850 
851  /* nothing fit, only write header! */
852  ihdr.version = htonl (2);
853  ihdr.entries = htonl (0);
854  ihdr.size = htonl (0);
855  if (NULL == *target)
856  *target = (char *) GNUNET_new (struct MetaDataHeader);
857  GNUNET_memcpy (*target, &ihdr, sizeof(struct MetaDataHeader));
858  return sizeof(struct MetaDataHeader);
859 }
int GNUNET_try_compression(const char *data, size_t old_size, char **result, size_t *new_size)
Try to compress the given block of data using libz.
Definition: compress.c:33
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define max(x, y)
#define HEADER_COMPRESSED
Flag in 'version' that indicates compressed meta-data.
Definition: meta_data.c:552
static unsigned int size
Size of the "table".
Definition: peer.c:68
size_t sbuf_size
Number of bytes in 'sbuf'.
Definition: meta_data.c:117
Entry of serialized meta data.
Definition: meta_data.c:599
uint32_t plugin_name_len
Number of bytes in the plugin name including 0-terminator.
Definition: meta_data.c:618
uint32_t mime_type_len
Number of bytes in the mime type including 0-terminator.
Definition: meta_data.c:623
uint32_t type
Meta data type.
Definition: meta_data.c:603
uint32_t data_size
Number of bytes of meta data.
Definition: meta_data.c:613
uint32_t format
Meta data format.
Definition: meta_data.c:608
Header for serialized meta data.
Definition: meta_data.c:565
uint32_t entries
How many MD entries are there?
Definition: meta_data.c:580
uint32_t size
Size of the decompressed meta data.
Definition: meta_data.c:585
uint32_t version
The version of the MD serialization.
Definition: meta_data.c:575

References MetaItem::data, MetaItem::data_size, MetaDataEntry::data_size, MetaDataHeader::entries, MetaItem::format, MetaDataEntry::format, GNUNET_assert, GNUNET_break, GNUNET_free, GNUNET_FS_META_DATA_SERIALIZE_NO_COMPRESS, GNUNET_FS_META_DATA_SERIALIZE_PART, GNUNET_malloc, GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, GNUNET_new, GNUNET_NO, GNUNET_SYSERR, GNUNET_try_compression(), GNUNET_YES, HEADER_COMPRESSED, GNUNET_FS_MetaData::item_count, GNUNET_FS_MetaData::items_head, GNUNET_FS_MetaData::items_tail, max, MetaItem::mime_type, MetaDataEntry::mime_type_len, MetaItem::next, MetaItem::plugin_name, MetaDataEntry::plugin_name_len, MetaItem::prev, GNUNET_FS_MetaData::sbuf, GNUNET_FS_MetaData::sbuf_size, MetaDataHeader::size, size, MetaItem::type, MetaDataEntry::type, and MetaDataHeader::version.

Referenced by extract_files(), GNUNET_FS_directory_builder_add(), GNUNET_FS_directory_builder_finish(), GNUNET_FS_meta_data_get_serialized_size(), and GNUNET_FS_publish_ublock_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_get_serialized_size()

ssize_t GNUNET_FS_meta_data_get_serialized_size ( const struct GNUNET_FS_MetaData md)

Get the size of the full meta-data in serialized form.

Parameters
mdmetadata to inspect
Returns
number of bytes needed for serialization, -1 on error

Definition at line 863 of file meta_data.c.

865 {
866  ssize_t ret;
867  char *ptr;
868 
869  if (NULL != md->sbuf)
870  return md->sbuf_size;
871  ptr = NULL;
872  ret =
875  if (-1 != ret)
876  GNUNET_free (ptr);
877  return ret;
878 }
ssize_t GNUNET_FS_meta_data_serialize(const struct GNUNET_FS_MetaData *md, char **target, size_t max, enum GNUNET_FS_MetaDataSerializationOptions opt)
Serialize meta-data to target.
Definition: meta_data.c:642

References GNUNET_free, GNUNET_FS_meta_data_serialize(), GNUNET_FS_META_DATA_SERIALIZE_FULL, GNUNET_MAX_MALLOC_CHECKED, ret, GNUNET_FS_MetaData::sbuf, and GNUNET_FS_MetaData::sbuf_size.

Referenced by extract_files(), GNUNET_FS_directory_builder_add(), GNUNET_FS_directory_builder_finish(), GNUNET_FS_download_start_task_(), GNUNET_FS_publish_ublock_(), and trigger_recursive_download().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FS_meta_data_deserialize()

struct GNUNET_FS_MetaData* GNUNET_FS_meta_data_deserialize ( const char *  input,
size_t  size 
)

Deserialize meta-data.

Initializes md.

Parameters
inputserialized meta-data.
sizenumber of bytes available
Returns
MD on success, NULL on error (e.g. bad format)

Initializes md.

Parameters
inputbuffer with the serialized metadata
sizenumber of bytes available in input
Returns
MD on success, NULL on error (i.e. bad format)

Definition at line 890 of file meta_data.c.

891 {
892  struct GNUNET_FS_MetaData *md;
893  struct MetaDataHeader hdr;
894  struct MetaDataEntry ent;
895  uint32_t ic;
896  uint32_t i;
897  char *data;
898  const char *cdata;
899  uint32_t version;
900  uint32_t dataSize;
901  int compressed;
902  size_t left;
903  uint32_t mlen;
904  uint32_t plen;
905  uint32_t dlen;
906  const char *mdata;
907  const char *meta_data;
908  const char *plugin_name;
909  const char *mime_type;
910  enum EXTRACTOR_MetaFormat format;
911 
912  if (size < sizeof(struct MetaDataHeader))
913  return NULL;
914  GNUNET_memcpy (&hdr, input, sizeof(struct MetaDataHeader));
915  version = ntohl (hdr.version) & HEADER_VERSION_MASK;
916  compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
917 
918  if (1 == version)
919  return NULL; /* null pointer */
920  if (2 != version)
921  {
922  GNUNET_break_op (0); /* unsupported version */
923  return NULL;
924  }
925 
926  ic = ntohl (hdr.entries);
927  dataSize = ntohl (hdr.size);
928  if (((sizeof(struct MetaDataEntry) * ic) > dataSize) ||
929  ((0 != ic) &&
930  (dataSize / ic < sizeof(struct MetaDataEntry))))
931  {
932  GNUNET_break_op (0);
933  return NULL;
934  }
935 
936  if (compressed)
937  {
938  if (dataSize >= GNUNET_MAX_MALLOC_CHECKED)
939  {
940  /* make sure we don't blow our memory limit because of a mal-formed
941  * message... */
942  GNUNET_break_op (0);
943  return NULL;
944  }
945  data =
946  GNUNET_decompress ((const char *) &input[sizeof(struct MetaDataHeader)],
947  size - sizeof(struct MetaDataHeader),
948  dataSize);
949  if (NULL == data)
950  {
951  GNUNET_break_op (0);
952  return NULL;
953  }
954  cdata = data;
955  }
956  else
957  {
958  data = NULL;
959  cdata = (const char *) &input[sizeof(struct MetaDataHeader)];
960  if (dataSize != size - sizeof(struct MetaDataHeader))
961  {
962  GNUNET_break_op (0);
963  return NULL;
964  }
965  }
966 
968  left = dataSize - ic * sizeof(struct MetaDataEntry);
969  mdata = &cdata[ic * sizeof(struct MetaDataEntry)];
970  for (i = 0; i < ic; i++)
971  {
972  GNUNET_memcpy (&ent, &cdata[i * sizeof(struct MetaDataEntry)],
973  sizeof(struct MetaDataEntry));
974  format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
975  if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&
976  (EXTRACTOR_METAFORMAT_C_STRING != format) &&
977  (EXTRACTOR_METAFORMAT_BINARY != format))
978  {
979  GNUNET_break_op (0);
980  break;
981  }
982  dlen = ntohl (ent.data_size);
983  plen = ntohl (ent.plugin_name_len);
984  mlen = ntohl (ent.mime_type_len);
985  if (dlen > left)
986  {
987  GNUNET_break_op (0);
988  break;
989  }
990  left -= dlen;
991  meta_data = &mdata[left];
992  if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
993  (EXTRACTOR_METAFORMAT_C_STRING == format))
994  {
995  if (0 == dlen)
996  {
997  GNUNET_break_op (0);
998  break;
999  }
1000  if ('\0' != meta_data[dlen - 1])
1001  {
1002  GNUNET_break_op (0);
1003  break;
1004  }
1005  }
1006  if (plen > left)
1007  {
1008  GNUNET_break_op (0);
1009  break;
1010  }
1011  left -= plen;
1012  if ((plen > 0) && ('\0' != mdata[left + plen - 1]))
1013  {
1014  GNUNET_break_op (0);
1015  break;
1016  }
1017  if (0 == plen)
1018  plugin_name = NULL;
1019  else
1020  plugin_name = &mdata[left];
1021 
1022  if (mlen > left)
1023  {
1024  GNUNET_break_op (0);
1025  break;
1026  }
1027  left -= mlen;
1028  if ((mlen > 0) && ('\0' != mdata[left + mlen - 1]))
1029  {
1030  GNUNET_break_op (0);
1031  break;
1032  }
1033  if (0 == mlen)
1034  mime_type = NULL;
1035  else
1036  mime_type = &mdata[left];
1038  (enum EXTRACTOR_MetaType)
1039  ntohl (ent.type), format, mime_type,
1040  meta_data, dlen);
1041  }
1042  GNUNET_free (data);
1043  return md;
1044 }
char * GNUNET_decompress(const char *input, size_t input_size, size_t output_size)
Decompress input, return the decompressed data as output.
Definition: compress.c:70
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define HEADER_VERSION_MASK
Bits in 'version' that give the version number.
Definition: meta_data.c:558

References data, MetaDataEntry::data_size, MetaDataHeader::entries, MetaDataEntry::format, GNUNET_break_op, GNUNET_decompress(), GNUNET_free, GNUNET_FS_meta_data_create(), GNUNET_FS_meta_data_insert(), GNUNET_MAX_MALLOC_CHECKED, GNUNET_memcpy, HEADER_COMPRESSED, HEADER_VERSION_MASK, MetaDataEntry::mime_type_len, plugin_name, MetaDataEntry::plugin_name_len, MetaDataHeader::size, size, MetaDataEntry::type, and MetaDataHeader::version.

Referenced by GNUNET_FS_directory_list_contents(), process_helper_msgs(), process_kblock(), and process_sblock().

Here is the call graph for this function:
Here is the caller graph for this function: