GNUnet 0.22.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 3113 of file gnunet_fs_service.h.

3114{
3120
3127
3133};
@ 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 127 of file meta_data.c.

128{
129 return GNUNET_new (struct GNUNET_FS_MetaData);
130}
#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:92

References GNUNET_new.

Referenced by extract_files(), get_file_information(), getopt_set_metadata(), GNUNET_FS_directory_builder_create(), GNUNET_FS_file_information_create_from_reader(), 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 527 of file meta_data.c.

529{
530 struct GNUNET_FS_MetaData *ret;
531 struct MetaItem *pos;
532
533 if (NULL == md)
534 return NULL;
536 for (pos = md->items_tail; NULL != pos; pos = pos->prev)
538 pos->format, pos->mime_type, pos->data,
539 pos->data_size);
540 return ret;
541}
static int ret
Final status code.
Definition: gnunet-arm.c:93
struct GNUNET_FS_MetaData * GNUNET_FS_meta_data_create()
Create a fresh struct FS_MetaData token.
Definition: meta_data.c:127
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:254
struct MetaItem * items_tail
Tail of linked list of the meta data items.
Definition: meta_data.c:101
Meta data item.
Definition: meta_data.c:46
struct MetaItem * prev
This is a doubly linked list.
Definition: meta_data.c:55
enum EXTRACTOR_MetaType type
Type of the meta data.
Definition: meta_data.c:80
size_t data_size
Number of bytes in 'data'.
Definition: meta_data.c:75
char * mime_type
Mime-type of data.
Definition: meta_data.c:65
enum EXTRACTOR_MetaFormat format
Format of the meta data.
Definition: meta_data.c:85
char * data
The actual meta data.
Definition: meta_data.c:70
char * plugin_name
Name of the extracting plugin.
Definition: meta_data.c:60

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 166 of file meta_data.c.

167{
168 struct MetaItem *pos;
169
170 if (NULL == md)
171 return;
172 while (NULL != (pos = md->items_head))
173 {
175 meta_item_free (pos);
176 }
177 GNUNET_free (md->sbuf);
178 GNUNET_free (md);
179}
#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:139
struct MetaItem * items_head
Head of linked list of the meta data items.
Definition: meta_data.c:96
char * sbuf
Complete serialized and compressed buffer of the items.
Definition: meta_data.c:107

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_download_stop(), 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 200 of file meta_data.c.

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

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 254 of file meta_data.c.

260{
261 struct MetaItem *pos;
262 struct MetaItem *mi;
263 char *p;
264
265 if ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
266 (EXTRACTOR_METAFORMAT_C_STRING == format))
267 GNUNET_break ('\0' == data[data_size - 1]);
268
269 for (pos = md->items_head; NULL != pos; pos = pos->next)
270 {
271 if (pos->data_size < data_size)
272 break; /* elements are sorted by size in the list */
273 if ((pos->type == type) && (pos->data_size == data_size) &&
274 (0 == memcmp (pos->data, data, data_size)))
275 {
276 if ((NULL == pos->mime_type) && (NULL != data_mime_type))
277 {
278 pos->mime_type = GNUNET_strdup (data_mime_type);
279 invalidate_sbuf (md);
280 }
281 if ((EXTRACTOR_METAFORMAT_C_STRING == pos->format) &&
282 (EXTRACTOR_METAFORMAT_UTF8 == format))
283 {
284 pos->format = EXTRACTOR_METAFORMAT_UTF8;
285 invalidate_sbuf (md);
286 }
287 return GNUNET_SYSERR;
288 }
289 }
290 md->item_count++;
291 mi = GNUNET_new (struct MetaItem);
292 mi->type = type;
293 mi->format = format;
294 mi->data_size = data_size;
295 if (NULL == pos)
297 md->items_tail,
298 mi);
299 else
301 md->items_tail,
302 pos->prev,
303 mi);
304 mi->mime_type =
305 (NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
306 mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
309 /* change all dir separators to POSIX style ('/') */
310 if ((EXTRACTOR_METATYPE_FILENAME == type) ||
312 {
313 p = mi->data;
314 while (('\0' != *p) && (p < mi->data + data_size))
315 {
316 if ('\\' == *p)
317 *p = '/';
318 p++;
319 }
320 }
321 invalidate_sbuf (md);
322 return GNUNET_OK;
323}
static char * plugin_name
Name of our plugin.
static char * data
The data to insert into the dht.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
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:155

References data, MetaItem::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, type, and MetaItem::type.

Referenced by getopt_set_metadata(), GNUNET_FS_directory_builder_add(), GNUNET_FS_file_information_create_from_file(), 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 356 of file meta_data.c.

358{
360}
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:418
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:343

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 364 of file meta_data.c.

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

References data, MetaItem::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, type, and MetaItem::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 183 of file meta_data.c.

184{
185 struct MetaItem *mi;
186
187 if (NULL == md)
188 return;
189 while (NULL != (mi = md->items_head))
190 {
192 meta_item_free (mi);
193 }
194 GNUNET_free (md->sbuf);
195 memset (md, 0, sizeof(struct GNUNET_FS_MetaData));
196}

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 391 of file meta_data.c.

393{
394 const char *dat;
395 struct GNUNET_TIME_Absolute t;
396
399 EXTRACTOR_METATYPE_PUBLICATION_DATE,
400 NULL, 0);
402 GNUNET_FS_meta_data_insert (md, "<gnunet>",
403 EXTRACTOR_METATYPE_PUBLICATION_DATE,
404 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
405 dat, strlen (dat) + 1);
406}
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:364
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:640
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 418 of file meta_data.c.

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

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 438 of file meta_data.c.

441{
442 struct MetaItem *pos;
443
444 if (NULL == md)
445 return NULL;
446 for (pos = md->items_head; NULL != pos; pos = pos->next)
447 if ((type == pos->type) &&
448 ((pos->format == EXTRACTOR_METAFORMAT_UTF8) ||
449 (pos->format == EXTRACTOR_METAFORMAT_C_STRING)))
450 return GNUNET_strdup (pos->data);
451 return NULL;
452}

References MetaItem::data, MetaItem::format, GNUNET_strdup, GNUNET_FS_MetaData::items_head, MetaItem::next, type, and MetaItem::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 456 of file meta_data.c.

459{
460 char *ret;
461 va_list args;
462 int type;
463
464 if (NULL == md)
465 return NULL;
466 ret = NULL;
467 va_start (args, md);
468 while (1)
469 {
470 type = va_arg (args, int);
471 if (-1 == type)
472 break;
473 if (NULL != (ret = GNUNET_FS_meta_data_get_by_type (md, type)))
474 break;
475 }
476 va_end (args);
477 return ret;
478}
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:438

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 490 of file meta_data.c.

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

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 637 of file meta_data.c.

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

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, size, MetaDataHeader::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(), GNUNET_FS_publish_ublock_(), and GNUNET_FS_write_meta_data().

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 858 of file meta_data.c.

860{
861 ssize_t ret;
862 char *ptr;
863
864 if (NULL != md->sbuf)
865 return md->sbuf_size;
866 ptr = NULL;
867 ret =
870 if (-1 != ret)
871 GNUNET_free (ptr);
872 return ret;
873}
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:637

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 885 of file meta_data.c.

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

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, size, MetaDataHeader::size, MetaDataEntry::type, and MetaDataHeader::version.

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

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