diff options
author | Markus Voggenreiter <Markus.Voggenreiter@tum.de> | 2019-10-23 21:50:46 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-01-13 13:31:03 +0100 |
commit | bb286cb253251e8210ed686dbde3dc8ecee16420 (patch) | |
tree | 4e7f625480adb0397df002f6e26bb6a3755ff3cc /src/reclaim-attribute/reclaim_attribute.c | |
parent | c136a16600cd4f72d7def1af7b4aa7592310c898 (diff) | |
download | gnunet-bb286cb253251e8210ed686dbde3dc8ecee16420.tar.gz gnunet-bb286cb253251e8210ed686dbde3dc8ecee16420.zip |
Preparation for Reference Type
Diffstat (limited to 'src/reclaim-attribute/reclaim_attribute.c')
-rw-r--r-- | src/reclaim-attribute/reclaim_attribute.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c index 9ac75dfba..0083ac53e 100644 --- a/src/reclaim-attribute/reclaim_attribute.c +++ b/src/reclaim-attribute/reclaim_attribute.c | |||
@@ -401,6 +401,41 @@ GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name, | |||
401 | } | 401 | } |
402 | 402 | ||
403 | /** | 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 | */ | ||
410 | struct GNUNET_RECLAIM_ATTESTATION_REFERENCE * | ||
411 | GNUNET_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 = (char *) &attr[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 | } | ||
437 | |||
438 | /** | ||
404 | * Add a new attribute to a claim list | 439 | * Add a new attribute to a claim list |
405 | * | 440 | * |
406 | * @param attr_name the name of the new attribute claim | 441 | * @param attr_name the name of the new attribute claim |
@@ -762,4 +797,97 @@ GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size) | |||
762 | return attr; | 797 | return attr; |
763 | } | 798 | } |
764 | 799 | ||
800 | /** | ||
801 | * Get required size for serialization buffer | ||
802 | * | ||
803 | * @param attr the reference to serialize | ||
804 | * @return the required buffer size | ||
805 | */ | ||
806 | size_t | ||
807 | GNUNET_RECLAIM_ATTESTATION_REF_serialize_get_size ( | ||
808 | const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr) | ||
809 | { | ||
810 | return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen ( | ||
811 | attr->reference_value); | ||
812 | } | ||
813 | |||
814 | |||
815 | /** | ||
816 | * Serialize a reference | ||
817 | * | ||
818 | * @param attr the reference to serialize | ||
819 | * @param result the serialized reference | ||
820 | * @return length of serialized data | ||
821 | */ | ||
822 | size_t | ||
823 | GNUNET_RECLAIM_ATTESTATION_REF_serialize ( | ||
824 | const struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr, | ||
825 | char *result) | ||
826 | { | ||
827 | size_t name_len; | ||
828 | size_t refval_len; | ||
829 | struct Attestation_Reference *attr_ser; | ||
830 | char *write_ptr; | ||
831 | attr_ser = (struct Attestation_Reference *) result; | ||
832 | attr_ser->reference_id = GNUNET_htonll (attr->id); | ||
833 | attr_ser->attestation_id = GNUNET_htonll (attr->id_attest); | ||
834 | name_len = strlen (attr->name); | ||
835 | refval_len = strlen (attr->reference_value); | ||
836 | attr_ser->name_len = htons (name_len); | ||
837 | attr_ser->ref_value_len = htons (refval_len); | ||
838 | write_ptr = (char *) &attr_ser[1]; | ||
839 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
840 | write_ptr += name_len; | ||
841 | GNUNET_memcpy (write_ptr, attr->reference_value, refval_len); | ||
842 | |||
843 | return sizeof(struct Attestation_Reference) + strlen (attr->name) + strlen ( | ||
844 | attr->reference_value); | ||
845 | } | ||
846 | |||
847 | |||
848 | /** | ||
849 | * Deserialize a reference | ||
850 | * | ||
851 | * @param data the serialized reference | ||
852 | * @param data_size the length of the serialized data | ||
853 | * | ||
854 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
855 | */ | ||
856 | struct GNUNET_RECLAIM_ATTESTATION_REFERENCE * | ||
857 | GNUNET_RECLAIM_ATTESTATION_REF_deserialize (const char *data, size_t data_size) | ||
858 | { | ||
859 | struct GNUNET_RECLAIM_ATTESTATION_REFERENCE *attr; | ||
860 | struct Attestation_Reference *attr_ser; | ||
861 | size_t name_len; | ||
862 | size_t refval_len; | ||
863 | char *write_ptr; | ||
864 | |||
865 | if (data_size < sizeof(struct Attestation_Reference)) | ||
866 | return NULL; | ||
867 | attr_ser = (struct Attestation_Reference *) data; | ||
868 | name_len = ntohs (attr_ser->name_len); | ||
869 | refval_len = ntohs (attr_ser->ref_value_len); | ||
870 | if (data_size < sizeof(struct Attestation_Reference) + refval_len + name_len) | ||
871 | { | ||
872 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
873 | "Buffer too small to deserialize\n"); | ||
874 | return NULL; | ||
875 | } | ||
876 | attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_REFERENCE) | ||
877 | + refval_len + name_len + 2); | ||
878 | |||
879 | attr->id = GNUNET_ntohll (attr_ser->reference_id); | ||
880 | attr->id_attest = GNUNET_ntohll (attr_ser->attestation_id); | ||
881 | |||
882 | write_ptr = (char *) &attr[1]; | ||
883 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | ||
884 | write_ptr[name_len] = '\0'; | ||
885 | attr->name = write_ptr; | ||
886 | |||
887 | write_ptr += name_len + 1; | ||
888 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, refval_len); | ||
889 | write_ptr[refval_len] = '\0'; | ||
890 | attr->reference_value = write_ptr; | ||
891 | return attr; | ||
892 | } | ||
765 | /* end of reclaim_attribute.c */ | 893 | /* end of reclaim_attribute.c */ |