aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim-attribute/reclaim_attribute.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reclaim-attribute/reclaim_attribute.c')
-rw-r--r--src/reclaim-attribute/reclaim_attribute.c587
1 files changed, 559 insertions, 28 deletions
diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c
index b81394ad2..43199c108 100644
--- a/src/reclaim-attribute/reclaim_attribute.c
+++ b/src/reclaim-attribute/reclaim_attribute.c
@@ -217,6 +217,116 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type,
217 return NULL; 217 return NULL;
218} 218}
219 219
220/**
221 * Convert an attestation type name to the corresponding number
222 *
223 * @param typename name to convert
224 * @return corresponding number, UINT32_MAX on error
225 */
226uint32_t
227GNUNET_RECLAIM_ATTESTATION_typename_to_number (const char *typename)
228{
229 unsigned int i;
230 struct Plugin *plugin;
231 uint32_t ret;
232 init ();
233 for (i = 0; i < num_plugins; i++)
234 {
235 plugin = attr_plugins[i];
236 if (UINT32_MAX !=
237 (ret = plugin->api->typename_to_number_attest (plugin->api->cls,
238 typename)))
239 return ret;
240 }
241 return UINT32_MAX;
242}
243
244/**
245 * Convert an attestation type number to the corresponding attestation type string
246 *
247 * @param type number of a type
248 * @return corresponding typestring, NULL on error
249 */
250const char *
251GNUNET_RECLAIM_ATTESTATION_number_to_typename (uint32_t type)
252{
253 unsigned int i;
254 struct Plugin *plugin;
255 const char *ret;
256
257 init ();
258 for (i = 0; i < num_plugins; i++)
259 {
260 plugin = attr_plugins[i];
261 if (NULL !=
262 (ret = plugin->api->number_to_typename_attest (plugin->api->cls, type)))
263 return ret;
264 }
265 return NULL;
266}
267/**
268 * Convert human-readable version of a 'claim' of an attestation to the binary
269 * representation
270 *
271 * @param type type of the claim
272 * @param s human-readable string
273 * @param data set to value in binary encoding (will be allocated)
274 * @param data_size set to number of bytes in @a data
275 * @return #GNUNET_OK on success
276 */
277int
278GNUNET_RECLAIM_ATTESTATION_string_to_value (uint32_t type,
279 const char *s,
280 void **data,
281 size_t *data_size)
282{
283 unsigned int i;
284 struct Plugin *plugin;
285
286 init ();
287 for (i = 0; i < num_plugins; i++)
288 {
289 plugin = attr_plugins[i];
290 if (GNUNET_OK == plugin->api->string_to_value_attest (plugin->api->cls,
291 type,
292 s,
293 data,
294 data_size))
295 return GNUNET_OK;
296 }
297 return GNUNET_SYSERR;
298}
299
300
301/**
302 * Convert the 'claim' of an attestation to a string
303 *
304 * @param type the type of attestation
305 * @param data claim in binary encoding
306 * @param data_size number of bytes in @a data
307 * @return NULL on error, otherwise human-readable representation of the claim
308 */
309char *
310GNUNET_RECLAIM_ATTESTATION_value_to_string (uint32_t type,
311 const void *data,
312 size_t data_size)
313{
314 unsigned int i;
315 struct Plugin *plugin;
316 char *ret;
317
318 init ();
319 for (i = 0; i < num_plugins; i++)
320 {
321 plugin = attr_plugins[i];
322 if (NULL != (ret = plugin->api->value_to_string_attest (plugin->api->cls,
323 type,
324 data,
325 data_size)))
326 return ret;
327 }
328 return NULL;
329}
220 330
221/** 331/**
222 * Create a new attribute. 332 * Create a new attribute.
@@ -243,6 +353,42 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name,
243 + strlen (attr_name_tmp) + 1 + data_size); 353 + strlen (attr_name_tmp) + 1 + data_size);
244 attr->type = type; 354 attr->type = type;
245 attr->data_size = data_size; 355 attr->data_size = data_size;
356 attr->flag = 0;
357 write_ptr = (char *) &attr[1];
358 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
359 attr->name = write_ptr;
360 write_ptr += strlen (attr->name) + 1;
361 GNUNET_memcpy (write_ptr, data, data_size);
362 attr->data = write_ptr;
363 GNUNET_free (attr_name_tmp);
364 return attr;
365}
366
367/**
368 * Create a new attestation.
369 *
370 * @param attr_name the attestation name
371 * @param type the attestation type
372 * @param data the attestation value
373 * @param data_size the attestation value size
374 * @return the new attestation
375 */
376struct GNUNET_RECLAIM_ATTESTATION_Claim *
377GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name,
378 uint32_t type,
379 const void *data,
380 size_t data_size)
381{
382 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
383 char *write_ptr;
384 char *attr_name_tmp = GNUNET_strdup (attr_name);
385
386 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
387
388 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
389 + strlen (attr_name_tmp) + 1 + data_size);
390 attr->type = type;
391 attr->data_size = data_size;
246 attr->version = 0; 392 attr->version = 0;
247 write_ptr = (char *) &attr[1]; 393 write_ptr = (char *) &attr[1];
248 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); 394 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
@@ -254,6 +400,40 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name,
254 return attr; 400 return attr;
255} 401}
256 402
403/**
404 * Create a new attestation reference.
405 *
406 * @param attr_name the referenced claim name
407 * @param ref_value the claim name in the attestation
408 * @return the new reference
409 */
410struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *
411GNUNET_RECLAIM_ATTESTATION_reference_new (const char *attr_name,
412 const char *ref_value)
413{
414 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr;
415 char *write_ptr;
416 char *attr_name_tmp = GNUNET_strdup (attr_name);
417 char *ref_value_tmp = GNUNET_strdup (ref_value);
418
419 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
420 GNUNET_STRINGS_utf8_tolower (ref_value, ref_value_tmp);
421
422 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE)
423 + strlen (attr_name_tmp) + strlen (ref_value_tmp) + 2);
424
425 write_ptr = (char *) &attr[1];
426 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
427 attr->name = write_ptr;
428
429 write_ptr += strlen (attr_name) + 1;
430 GNUNET_memcpy (write_ptr, ref_value_tmp, strlen (ref_value_tmp) + 1);
431 attr->reference_value = write_ptr;
432
433 GNUNET_free (attr_name_tmp);
434 GNUNET_free (ref_value_tmp);
435 return attr;
436}
257 437
258/** 438/**
259 * Add a new attribute to a claim list 439 * Add a new attribute to a claim list
@@ -296,7 +476,30 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (
296 size_t len = 0; 476 size_t len = 0;
297 477
298 for (le = attrs->list_head; NULL != le; le = le->next) 478 for (le = attrs->list_head; NULL != le; le = le->next)
299 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim); 479 {
480 if (NULL != le->claim)
481 {
482 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
483 len += GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
484 }
485 else if (NULL != le->attest )
486 {
487 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
488 len += GNUNET_RECLAIM_ATTESTATION_serialize_get_size (le->attest);
489 }
490 else if (NULL != le->reference)
491 {
492 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
493 len += GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (le->reference);
494 }
495 else
496 {
497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498 "Unserialized Claim List Entry Type for size not known.\n");
499 break;
500 }
501 len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
502 }
300 return len; 503 return len;
301} 504}
302 505
@@ -317,14 +520,50 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize (
317 size_t len; 520 size_t len;
318 size_t total_len; 521 size_t total_len;
319 char *write_ptr; 522 char *write_ptr;
320
321 write_ptr = result; 523 write_ptr = result;
322 total_len = 0; 524 total_len = 0;
323 for (le = attrs->list_head; NULL != le; le = le->next) 525 for (le = attrs->list_head; NULL != le; le = le->next)
324 { 526 {
325 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr); 527 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *list_type;
326 total_len += len; 528 if (NULL != le->claim)
327 write_ptr += len; 529 {
530 list_type = (struct
531 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
532 list_type->type = htons (1);
533 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
534 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
535 len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr);
536 total_len += len;
537 write_ptr += len;
538 }
539 else if (NULL != le->attest )
540 {
541 list_type = (struct
542 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
543 list_type->type = htons (2);
544 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
545 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
546 len = GNUNET_RECLAIM_ATTESTATION_serialize (le->attest, write_ptr);
547 total_len += len;
548 write_ptr += len;
549 }
550 else if (NULL != le->reference)
551 {
552 list_type = (struct
553 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) write_ptr;
554 list_type->type = htons (3);
555 total_len += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
556 write_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
557 len = GNUNET_RECLAIM_ATTESTATION_REF_serialize (le->reference, write_ptr);
558 total_len += len;
559 write_ptr += len;
560 }
561 else
562 {
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Unserialized Claim List Entry Type not known.\n");
565 continue;
566 }
328 } 567 }
329 return total_len; 568 return total_len;
330} 569}
@@ -345,23 +584,75 @@ GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size)
345 size_t attr_len; 584 size_t attr_len;
346 const char *read_ptr; 585 const char *read_ptr;
347 586
348 if (data_size < sizeof(struct Attribute)) 587 if ((data_size < sizeof(struct Attribute) + sizeof(struct
588 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry))
589 && (data_size < sizeof(struct
590 Attestation)
591 + sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry)) &&
592 (data_size < sizeof(struct Attestation_Reference) + sizeof(struct
593 GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry)) )
349 return NULL; 594 return NULL;
350 595
351 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); 596 attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
352 read_ptr = data; 597 read_ptr = data;
353 while (((data + data_size) - read_ptr) >= sizeof(struct Attribute)) 598 while (((data + data_size) - read_ptr) >= sizeof(struct Attribute))
354 { 599 {
355 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); 600 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *list_type;
356 le->claim = 601 list_type = (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType *) read_ptr;
357 GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr, 602 if (1 == ntohs (list_type->type))
358 data_size - (read_ptr - data)); 603 {
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 604 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
360 "Deserialized attribute %s\n", 605 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
361 le->claim->name); 606 if (((data + data_size) - read_ptr) < sizeof(struct Attribute))
362 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le); 607 break;
363 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim); 608 le->attest = NULL;
364 read_ptr += attr_len; 609 le->reference = NULL;
610 le->claim =
611 GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr,
612 data_size - (read_ptr - data));
613 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
614 attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim);
615 read_ptr += attr_len;
616 }
617 else if (2 == ntohs (list_type->type))
618 {
619 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
620 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
621 if (((data + data_size) - read_ptr) < sizeof(struct Attestation))
622 break;
623 le->claim = NULL;
624 le->reference = NULL;
625 le->attest =
626 GNUNET_RECLAIM_ATTESTATION_deserialize (read_ptr,
627 data_size - (read_ptr - data));
628 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
629 attr_len = GNUNET_RECLAIM_ATTESTATION_serialize_get_size (le->attest);
630 read_ptr += attr_len;
631 }
632 else if (3 == ntohs (list_type->type))
633 {
634 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
635 read_ptr += sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntryType);
636 if (((data + data_size) - read_ptr) < sizeof(struct
637 Attestation_Reference))
638 break;
639 le->claim = NULL;
640 le->attest = NULL;
641 le->reference =
642 GNUNET_RECLAIM_ATTESTATION_REF_deserialize (read_ptr,
643 data_size - (read_ptr
644 - data));
645 GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le);
646 attr_len = GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (
647 le->reference);
648 read_ptr += attr_len;
649 }
650 else
651 {
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Serialized Claim List Entry Type not known.\n");
654 break;
655 }
365 } 656 }
366 return attrs; 657 return attrs;
367} 658}
@@ -381,16 +672,45 @@ GNUNET_RECLAIM_ATTRIBUTE_list_dup (
381 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result; 672 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result;
382 673
383 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); 674 result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
675 if (NULL == attrs->list_head)
676 {
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Duplicating empty List\n");
678 }
384 for (le = attrs->list_head; NULL != le; le = le->next) 679 for (le = attrs->list_head; NULL != le; le = le->next)
385 { 680 {
386 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); 681 result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
387 result_le->claim = 682 result_le->claim = NULL;
388 GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name, 683 result_le->attest = NULL;
389 le->claim->type, 684 result_le->reference = NULL;
390 le->claim->data, 685 if (NULL != le->claim)
391 le->claim->data_size); 686 {
392 result_le->claim->version = le->claim->version; 687 result_le->claim =
393 result_le->claim->id = le->claim->id; 688 GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name,
689 le->claim->type,
690 le->claim->data,
691 le->claim->data_size);
692
693 result_le->claim->id = le->claim->id;
694 result_le->claim->flag = le->claim->flag;
695 }
696 if ( NULL != le->attest)
697 {
698 result_le->attest = GNUNET_RECLAIM_ATTESTATION_claim_new (
699 le->attest->name,
700 le->attest->type,
701 le->attest->data,
702 le->attest->
703 data_size);
704 result_le->attest->id = le->attest->id;
705 }
706 if (NULL !=le->reference)
707 {
708 result_le->reference = GNUNET_RECLAIM_ATTESTATION_reference_new (
709 le->reference->name,
710 le->reference->reference_value);
711 result_le->reference->id = le->reference->id;
712 result_le->reference->id_attest = le->reference->id_attest;
713 }
394 GNUNET_CONTAINER_DLL_insert (result->list_head, 714 GNUNET_CONTAINER_DLL_insert (result->list_head,
395 result->list_tail, 715 result->list_tail,
396 result_le); 716 result_le);
@@ -411,9 +731,14 @@ GNUNET_RECLAIM_ATTRIBUTE_list_destroy (
411 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; 731 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
412 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le; 732 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le;
413 733
414 for (le = attrs->list_head; NULL != le;) 734 for (le = attrs->list_head; NULL != le; le = le->next)
415 { 735 {
416 GNUNET_free (le->claim); 736 if (NULL != le->claim)
737 GNUNET_free (le->claim);
738 if (NULL != le->attest)
739 GNUNET_free (le->attest);
740 if (NULL != le->reference)
741 GNUNET_free (le->reference);
417 tmp_le = le; 742 tmp_le = le;
418 le = le->next; 743 le = le->next;
419 GNUNET_free (tmp_le); 744 GNUNET_free (tmp_le);
@@ -421,7 +746,24 @@ GNUNET_RECLAIM_ATTRIBUTE_list_destroy (
421 GNUNET_free (attrs); 746 GNUNET_free (attrs);
422} 747}
423 748
424 749/**
750 * Count attestations in claim list
751 *
752 * @param attrs list
753 */
754int
755GNUNET_RECLAIM_ATTRIBUTE_list_count_attest (
756 const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs)
757{
758 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
759 int i = 0;
760 for (le = attrs->list_head; NULL != le; le = le->next)
761 {
762 if (NULL != le->attest)
763 i++;
764 }
765 return i;
766}
425/** 767/**
426 * Get required size for serialization buffer 768 * Get required size for serialization buffer
427 * 769 *
@@ -455,7 +797,7 @@ GNUNET_RECLAIM_ATTRIBUTE_serialize (
455 797
456 attr_ser = (struct Attribute *) result; 798 attr_ser = (struct Attribute *) result;
457 attr_ser->attribute_type = htons (attr->type); 799 attr_ser->attribute_type = htons (attr->type);
458 attr_ser->attribute_version = htonl (attr->version); 800 attr_ser->attribute_version = htonl (attr->flag);
459 attr_ser->attribute_id = GNUNET_htonll (attr->id); 801 attr_ser->attribute_id = GNUNET_htonll (attr->id);
460 name_len = strlen (attr->name); 802 name_len = strlen (attr->name);
461 attr_ser->name_len = htons (name_len); 803 attr_ser->name_len = htons (name_len);
@@ -505,7 +847,7 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size)
505 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim) 847 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTRIBUTE_Claim)
506 + data_len + name_len + 1); 848 + data_len + name_len + 1);
507 attr->type = ntohs (attr_ser->attribute_type); 849 attr->type = ntohs (attr_ser->attribute_type);
508 attr->version = ntohl (attr_ser->attribute_version); 850 attr->flag = ntohl (attr_ser->attribute_version);
509 attr->id = GNUNET_ntohll (attr_ser->attribute_id); 851 attr->id = GNUNET_ntohll (attr_ser->attribute_id);
510 attr->data_size = data_len; 852 attr->data_size = data_len;
511 853
@@ -521,4 +863,193 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size)
521} 863}
522 864
523 865
866/**
867 * Get required size for serialization buffer
868 *
869 * @param attr the attestation to serialize
870 * @return the required buffer size
871 */
872size_t
873GNUNET_RECLAIM_ATTESTATION_serialize_get_size (
874 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr)
875{
876 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
877}
878
879/**
880 * Serialize an attestation
881 *
882 * @param attr the attestation to serialize
883 * @param result the serialized attestation
884 * @return length of serialized data
885 */
886size_t
887GNUNET_RECLAIM_ATTESTATION_serialize (
888 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr,
889 char *result)
890{
891 size_t data_len_ser;
892 size_t name_len;
893 struct Attestation *attr_ser;
894 char *write_ptr;
895
896 attr_ser = (struct Attestation *) result;
897 attr_ser->attestation_type = htons (attr->type);
898 attr_ser->attestation_version = htonl (attr->version);
899 attr_ser->attestation_id = GNUNET_htonll (attr->id);
900 name_len = strlen (attr->name);
901 attr_ser->name_len = htons (name_len);
902 write_ptr = (char *) &attr_ser[1];
903 GNUNET_memcpy (write_ptr, attr->name, name_len);
904 write_ptr += name_len;
905 // TODO plugin-ize
906 // data_len_ser = plugin->serialize_attribute_value (attr,
907 // &attr_ser[1]);
908 data_len_ser = attr->data_size;
909 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
910 attr_ser->data_size = htons (data_len_ser);
911
912 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
913}
914
915/**
916 * Deserialize an attestation
917 *
918 * @param data the serialized attestation
919 * @param data_size the length of the serialized data
920 *
921 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
922 */
923struct GNUNET_RECLAIM_ATTESTATION_Claim *
924GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size)
925{
926 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
927 struct Attestation *attr_ser;
928 size_t data_len;
929 size_t name_len;
930 char *write_ptr;
931
932 if (data_size < sizeof(struct Attestation))
933 return NULL;
934
935 attr_ser = (struct Attestation *) data;
936 data_len = ntohs (attr_ser->data_size);
937 name_len = ntohs (attr_ser->name_len);
938 if (data_size < sizeof(struct Attestation) + data_len + name_len)
939 {
940 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
941 "Buffer too small to deserialize\n");
942 return NULL;
943 }
944 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
945 + data_len + name_len + 1);
946 attr->type = ntohs (attr_ser->attestation_type);
947 attr->version = ntohl (attr_ser->attestation_version);
948 attr->id = GNUNET_ntohll (attr_ser->attestation_id);
949 attr->data_size = data_len;
950
951 write_ptr = (char *) &attr[1];
952 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
953 write_ptr[name_len] = '\0';
954 attr->name = write_ptr;
955
956 write_ptr += name_len + 1;
957 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
958 attr->data = write_ptr;
959 return attr;
960}
961
962/**
963 * Get required size for serialization buffer
964 *
965 * @param attr the reference to serialize
966 * @return the required buffer size
967 */
968size_t
969GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size (
970 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr)
971{
972 return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen (
973 attr->reference_value);
974}
975
976
977/**
978 * Serialize a reference
979 *
980 * @param attr the reference to serialize
981 * @param result the serialized reference
982 * @return length of serialized data
983 */
984size_t
985GNUNET_RECLAIM_ATTESTATION_REF_serialize (
986 const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr,
987 char *result)
988{
989 size_t name_len;
990 size_t refval_len;
991 struct Attestation_Reference *attr_ser;
992 char *write_ptr;
993 attr_ser = (struct Attestation_Reference *) result;
994 attr_ser->reference_id = GNUNET_htonll (attr->id);
995 attr_ser->attestation_id = GNUNET_htonll (attr->id_attest);
996 name_len = strlen (attr->name);
997 refval_len = strlen (attr->reference_value);
998 attr_ser->name_len = htons (name_len);
999 attr_ser->ref_value_len = htons (refval_len);
1000 write_ptr = (char *) &attr_ser[1];
1001 GNUNET_memcpy (write_ptr, attr->name, name_len);
1002 write_ptr += name_len;
1003 GNUNET_memcpy (write_ptr, attr->reference_value, refval_len);
1004
1005 return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen (
1006 attr->reference_value);
1007}
1008
1009
1010/**
1011 * Deserialize a reference
1012 *
1013 * @param data the serialized reference
1014 * @param data_size the length of the serialized data
1015 *
1016 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
1017 */
1018struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *
1019GNUNET_RECLAIM_ATTESTATION_REF_deserialize (const char *data, size_t data_size)
1020{
1021 struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr;
1022 struct Attestation_Reference *attr_ser;
1023 size_t name_len;
1024 size_t refval_len;
1025 char *write_ptr;
1026
1027 if (data_size < sizeof(struct Attestation_Reference))
1028 return NULL;
1029 attr_ser = (struct Attestation_Reference *) data;
1030 name_len = ntohs (attr_ser->name_len);
1031 refval_len = ntohs (attr_ser->ref_value_len);
1032 if (data_size < sizeof(struct Attestation_Reference) + refval_len + name_len)
1033 {
1034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1035 "Buffer too small to deserialize\n");
1036 return NULL;
1037 }
1038 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE)
1039 + refval_len + name_len + 2);
1040
1041 attr->id = GNUNET_ntohll (attr_ser->reference_id);
1042 attr->id_attest = GNUNET_ntohll (attr_ser->attestation_id);
1043
1044 write_ptr = (char *) &attr[1];
1045 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
1046 write_ptr[name_len] = '\0';
1047 attr->name = write_ptr;
1048
1049 write_ptr += name_len + 1;
1050 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, refval_len);
1051 write_ptr[refval_len] = '\0';
1052 attr->reference_value = write_ptr;
1053 return attr;
1054}
524/* end of reclaim_attribute.c */ 1055/* end of reclaim_attribute.c */