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;
128 struct GNUNET_IDENTITY_PROVIDER_AttributeList*
129 attribute_list_dup (const struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
131 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
132 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *result_le;
133 struct GNUNET_IDENTITY_PROVIDER_AttributeList *result;
136 result = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeList);
137 for (le = attrs->list_head; NULL != le; le = le->next)
139 result_le = GNUNET_new (struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry);
140 len = sizeof (struct GNUNET_IDENTITY_PROVIDER_Attribute) + le->attribute->data_size;
141 result_le->attribute = GNUNET_malloc (len);
142 GNUNET_memcpy (result_le->attribute,
145 result_le->attribute->name = (const char*)&result_le->attribute[1];
146 GNUNET_CONTAINER_DLL_insert (result->list_head,
155 attribute_list_destroy (struct GNUNET_IDENTITY_PROVIDER_AttributeList *attrs)
157 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *le;
158 struct GNUNET_IDENTITY_PROVIDER_AttributeListEntry *tmp_le;
160 for (le = attrs->list_head; NULL != le;)
162 GNUNET_free (le->attribute);
165 GNUNET_free (tmp_le);
172 attribute_serialize_get_size (const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr)
174 return sizeof (struct Attribute)
175 + strlen (attr->name)
176 + attr->data_size; //TODO get data_size from plugin
180 attribute_serialize (const struct GNUNET_IDENTITY_PROVIDER_Attribute *attr,
185 struct Attribute *attr_ser;
188 attr_ser = (struct Attribute*)result;
189 attr_ser->attribute_type = htons (attr->attribute_type);
190 attr_ser->attribute_version = htonl (attr->attribute_version);
191 name_len = strlen (attr->name);
192 attr_ser->name_len = htons (name_len);
193 write_ptr = (char*)&attr_ser[1];
194 GNUNET_memcpy (write_ptr, attr->name, name_len);
195 write_ptr += name_len;
197 //data_len_ser = plugin->serialize_attribute_value (attr,
199 data_len_ser = attr->data_size;
200 GNUNET_memcpy (write_ptr, attr->data, attr->data_size);
201 attr_ser->data_size = htons (data_len_ser);
203 return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size;
206 struct GNUNET_IDENTITY_PROVIDER_Attribute *
207 attribute_deserialize (const char* data,
210 struct GNUNET_IDENTITY_PROVIDER_Attribute *attr;
211 struct Attribute *attr_ser;
216 if (data_size < sizeof (struct Attribute))
219 attr_ser = (struct Attribute*)data;
221 data_len = ntohs (attr_ser->data_size);
222 name_len = ntohs (attr_ser->name_len);
223 attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_Attribute)
224 + data_len + name_len + 1);
225 attr->attribute_type = ntohs (attr_ser->attribute_type);
226 attr->attribute_version = ntohl (attr_ser->attribute_version);
227 attr->data_size = ntohs (attr_ser->data_size);
229 write_ptr = (char*)&attr[1];
230 GNUNET_memcpy (write_ptr,
233 write_ptr[name_len] = '\0';
234 attr->name = write_ptr;
236 write_ptr += name_len + 1;
237 GNUNET_memcpy (write_ptr,
238 (char*)&attr_ser[1] + name_len,
240 attr->data = write_ptr;
245 /* end of identity_attribute.c */