aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim-attribute/reclaim_attribute.c
diff options
context:
space:
mode:
authorMarkus Voggenreiter <Markus.Voggenreiter@tum.de>2019-10-09 21:10:14 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2020-01-13 13:31:01 +0100
commite6eb523e12729ef1d6a6ee3f140671e5e51d8d1c (patch)
tree746c6b899bc5fdc491ad477463dda3b56295304e /src/reclaim-attribute/reclaim_attribute.c
parent60d2660de243053bc0f41657ad9d67537723276c (diff)
downloadgnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.tar.gz
gnunet-e6eb523e12729ef1d6a6ee3f140671e5e51d8d1c.zip
Adapted JSON Conversion and Serialization
Diffstat (limited to 'src/reclaim-attribute/reclaim_attribute.c')
-rw-r--r--src/reclaim-attribute/reclaim_attribute.c242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c
index b81394ad2..6dd1632fd 100644
--- a/src/reclaim-attribute/reclaim_attribute.c
+++ b/src/reclaim-attribute/reclaim_attribute.c
@@ -217,6 +217,117 @@ 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
233 init ();
234 for (i = 0; i < num_plugins; i++)
235 {
236 plugin = attr_plugins[i];
237 if (UINT32_MAX !=
238 (ret = plugin->api->typename_to_number_attest (plugin->api->cls,
239 typename)))
240 return ret;
241 }
242 return UINT32_MAX;
243}
244
245/**
246 * Convert an attestation type number to the corresponding attestation type string
247 *
248 * @param type number of a type
249 * @return corresponding typestring, NULL on error
250 */
251const char *
252GNUNET_RECLAIM_ATTESTATION_number_to_typename (uint32_t type)
253{
254 unsigned int i;
255 struct Plugin *plugin;
256 const char *ret;
257
258 init ();
259 for (i = 0; i < num_plugins; i++)
260 {
261 plugin = attr_plugins[i];
262 if (NULL !=
263 (ret = plugin->api->number_to_typename_attest (plugin->api->cls, type)))
264 return ret;
265 }
266 return NULL;
267}
268/**
269 * Convert human-readable version of a 'claim' of an attestation to the binary
270 * representation
271 *
272 * @param type type of the claim
273 * @param s human-readable string
274 * @param data set to value in binary encoding (will be allocated)
275 * @param data_size set to number of bytes in @a data
276 * @return #GNUNET_OK on success
277 */
278int
279GNUNET_RECLAIM_ATTESTATION_string_to_value (uint32_t type,
280 const char *s,
281 void **data,
282 size_t *data_size)
283{
284 unsigned int i;
285 struct Plugin *plugin;
286
287 init ();
288 for (i = 0; i < num_plugins; i++)
289 {
290 plugin = attr_plugins[i];
291 if (GNUNET_OK == plugin->api->string_to_value_attest (plugin->api->cls,
292 type,
293 s,
294 data,
295 data_size))
296 return GNUNET_OK;
297 }
298 return GNUNET_SYSERR;
299}
300
301
302/**
303 * Convert the 'claim' of an attestation to a string
304 *
305 * @param type the type of attestation
306 * @param data claim in binary encoding
307 * @param data_size number of bytes in @a data
308 * @return NULL on error, otherwise human-readable representation of the claim
309 */
310char *
311GNUNET_RECLAIM_ATTESTATION_value_to_string (uint32_t type,
312 const void *data,
313 size_t data_size)
314{
315 unsigned int i;
316 struct Plugin *plugin;
317 char *ret;
318
319 init ();
320 for (i = 0; i < num_plugins; i++)
321 {
322 plugin = attr_plugins[i];
323 if (NULL != (ret = plugin->api->value_to_string_attest (plugin->api->cls,
324 type,
325 data,
326 data_size)))
327 return ret;
328 }
329 return NULL;
330}
220 331
221/** 332/**
222 * Create a new attribute. 333 * Create a new attribute.
@@ -254,6 +365,41 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name,
254 return attr; 365 return attr;
255} 366}
256 367
368/**
369 * Create a new attestation.
370 *
371 * @param attr_name the attestation name
372 * @param type the attestation type
373 * @param data the attestation value
374 * @param data_size the attestation value size
375 * @return the new attestation
376 */
377struct GNUNET_RECLAIM_ATTESTATION_Claim *
378GNUNET_RECLAIM_ATTESTATION_claim_new (const char *attr_name,
379 uint32_t type,
380 const void *data,
381 size_t data_size)
382{
383 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
384 char *write_ptr;
385 char *attr_name_tmp = GNUNET_strdup (attr_name);
386
387 GNUNET_STRINGS_utf8_tolower (attr_name, attr_name_tmp);
388
389 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
390 + strlen (attr_name_tmp) + 1 + data_size);
391 attr->type = type;
392 attr->data_size = data_size;
393 attr->version = 0;
394 write_ptr = (char *) &attr[1];
395 GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1);
396 attr->name = write_ptr;
397 write_ptr += strlen (attr->name) + 1;
398 GNUNET_memcpy (write_ptr, data, data_size);
399 attr->data = write_ptr;
400 GNUNET_free (attr_name_tmp);
401 return attr;
402}
257 403
258/** 404/**
259 * Add a new attribute to a claim list 405 * Add a new attribute to a claim list
@@ -521,4 +667,100 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size)
521} 667}
522 668
523 669
670/**
671 * Get required size for serialization buffer
672 *
673 * @param attr the attestation to serialize
674 * @return the required buffer size
675 */
676size_t
677GNUNET_RECLAIM_ATTESTATION_serialize_get_size (
678 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr)
679{
680 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
681}
682
683/**
684 * Serialize an attestation
685 *
686 * @param attr the attestation to serialize
687 * @param result the serialized attestation
688 * @return length of serialized data
689 */
690size_t
691GNUNET_RECLAIM_ATTESTATION_serialize (
692 const struct GNUNET_RECLAIM_ATTESTATION_Claim *attr,
693 char *result)
694{
695 size_t data_len_ser;
696 size_t name_len;
697 struct Attestation *attr_ser;
698 char *write_ptr;
699
700 attr_ser = (struct Attestation *) result;
701 attr_ser->attestation_type = htons (attr->type);
702 attr_ser->attestation_type = htonl (attr->version);
703 attr_ser->attestation_type = GNUNET_htonll (attr->id);
704 name_len = strlen (attr->name);
705 attr_ser->name_len = htons (name_len);
706 write_ptr = (char *) &attr_ser[1];
707 GNUNET_memcpy (write_ptr, attr->name, name_len);
708 write_ptr += name_len;
709 // TODO plugin-ize
710 // data_len_ser = plugin->serialize_attribute_value (attr,
711 // &attr_ser[1]);
712 data_len_ser = attr->data_size;
713 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
714 attr_ser->data_size = htons (data_len_ser);
715
716 return sizeof(struct Attestation) + strlen (attr->name) + attr->data_size;
717}
718
719/**
720 * Deserialize an attestation
721 *
722 * @param data the serialized attestation
723 * @param data_size the length of the serialized data
724 *
725 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
726 */
727struct GNUNET_RECLAIM_ATTESTATION_Claim *
728GNUNET_RECLAIM_ATTESTATION_deserialize (const char *data, size_t data_size)
729{
730 struct GNUNET_RECLAIM_ATTESTATION_Claim *attr;
731 struct Attestation *attr_ser;
732 size_t data_len;
733 size_t name_len;
734 char *write_ptr;
735
736 if (data_size < sizeof(struct Attestation))
737 return NULL;
738
739 attr_ser = (struct Attestation *) data;
740 data_len = ntohs (attr_ser->data_size);
741 name_len = ntohs (attr_ser->name_len);
742 if (data_size < sizeof(struct Attestation) + data_len + name_len)
743 {
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
745 "Buffer too small to deserialize\n");
746 return NULL;
747 }
748 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_ATTESTATION_Claim)
749 + data_len + name_len + 1);
750 attr->type = ntohs (attr_ser->attestation_type);
751 attr->version = ntohl (attr_ser->attestation_version);
752 attr->id = GNUNET_ntohll (attr_ser->attestation_id);
753 attr->data_size = data_len;
754
755 write_ptr = (char *) &attr[1];
756 GNUNET_memcpy (write_ptr, &attr_ser[1], name_len);
757 write_ptr[name_len] = '\0';
758 attr->name = write_ptr;
759
760 write_ptr += name_len + 1;
761 GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size);
762 attr->data = write_ptr;
763 return attr;
764}
765
524/* end of reclaim_attribute.c */ 766/* end of reclaim_attribute.c */