2 This file is part of GNUnet
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file identity-provider/identity_attribute.c
23 * @brief helper library to manage identity attributes
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "identity_attribute.h"
31 * Create a new attribute.
33 * @param name the attribute name
34 * @param type the attribute type
35 * @param data the attribute value
36 * @param data_size the attribute value size
37 * @return the new attribute
39 struct GNUNET_IDENTITY_PROVIDER_Attribute *
40 attribute_new (const char* attr_name,
45 struct GNUNET_IDENTITY_PROVIDER_Attribute *attr;
48 attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_Attribute) +
49 strlen (attr_name) + 1 +
51 attr->attribute_type = attr_type;
52 attr->data_size = data_size;
53 write_ptr = (char*)&attr[1];
54 GNUNET_memcpy (write_ptr,
56 strlen (attr_name) + 1);
57 attr->name = write_ptr;
58 write_ptr += strlen (attr->name) + 1;
59 GNUNET_memcpy (write_ptr,
62 attr->data = write_ptr;
67 attribute_list_serialize_get_size (const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
69 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
71 for (le = attrs->list_head; NULL != le; le = le->next)
72 len += attribute_serialize_get_size (le->attribute);
77 attribute_list_serialize (const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs,
80 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
87 for (le = attrs->list_head; NULL != le; le = le->next)
89 len = attribute_serialize (le->attribute,
97 struct GNUNET_IDENTITY_PROVIDER_AttributeList *
98 attribute_list_deserialize (const char* data,
101 struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs;
102 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
104 const char* read_ptr;
106 if (data_size < sizeof (struct Attribute))
109 attrs = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeList);
111 while (((data + data_size) - read_ptr) >= sizeof (struct Attribute))
114 le = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry);
115 le->attribute = attribute_deserialize (read_ptr,
116 data_size - (read_ptr - data));
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Deserialized attribute %s\n", le->attribute->name);
119 GNUNET_CONTAINER_DLL_insert (attrs->list_head,
122 attr_len = attribute_serialize_get_size (le->attribute);
123 read_ptr += attr_len;
129 attribute_list_destroy (struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
131 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
132 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *tmp_le;
134 for (le = attrs->list_head; NULL != le;)
136 GNUNET_free (le->attribute);
139 GNUNET_free (tmp_le);
146 attribute_serialize_get_size (const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
148 return sizeof (struct Attribute)
149 + strlen (attr->name)
150 + attr->data_size; //TODO get data_size from plugin
154 attribute_serialize (const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr,
159 struct Attribute *attr_ser;
162 attr_ser = (struct Attribute*)result;
163 attr_ser->attribute_type = htons (attr->attribute_type);
164 name_len = strlen (attr->name);
165 attr_ser->name_len = htons (name_len);
166 write_ptr = (char*)&attr_ser[1];
167 GNUNET_memcpy (write_ptr, attr->name, name_len);
168 write_ptr += name_len;
170 //data_len_ser = plugin->serialize_attribute_value (attr,
172 data_len_ser = attr->data_size;
173 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
174 attr_ser->data_size = htons (data_len_ser);
176 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
179 struct GNUNET_IDENTITY_PROVIDER_Attribute *
180 attribute_deserialize (const char* data,
183 struct GNUNET_IDENTITY_PROVIDER_Attribute *attr;
184 struct Attribute *attr_ser;
189 if (data_size < sizeof (struct Attribute))
192 attr_ser = (struct Attribute*)data;
194 data_len = ntohs (attr_ser->data_size);
195 name_len = ntohs (attr_ser->name_len);
196 attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_Attribute)
197 + data_len + name_len + 1);
198 attr->attribute_type = ntohs (attr_ser->attribute_type);
199 attr->data_size = ntohs (attr_ser->data_size);
201 write_ptr = (char*)&attr[1];
202 GNUNET_memcpy (write_ptr,
205 write_ptr[name_len] = '\0';
206 attr->name = write_ptr;
208 write_ptr += name_len + 1;
209 GNUNET_memcpy (write_ptr,
210 (char*)&attr_ser[1] + name_len,
212 attr->data = write_ptr;
217 /* end of identity_attribute.c */