diff options
Diffstat (limited to 'src/identity-attribute/identity_attribute.c')
-rw-r--r-- | src/identity-attribute/identity_attribute.c | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/src/identity-attribute/identity_attribute.c b/src/identity-attribute/identity_attribute.c new file mode 100644 index 000000000..377eb3211 --- /dev/null +++ b/src/identity-attribute/identity_attribute.c | |||
@@ -0,0 +1,245 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2015 GNUnet e.V. | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file identity-provider/identity_attribute.c | ||
23 | * @brief helper library to manage identity attributes | ||
24 | * @author Martin Schanzenbach | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "identity_attribute.h" | ||
29 | |||
30 | /** | ||
31 | * Create a new attribute. | ||
32 | * | ||
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 | ||
38 | */ | ||
39 | struct GNUNET_IDENTITY_ATTRIBUTE_Claim * | ||
40 | GNUNET_IDENTITY_ATTRIBUTE_claim_new (const char* attr_name, | ||
41 | uint32_t attr_type, | ||
42 | const void* data, | ||
43 | size_t data_size) | ||
44 | { | ||
45 | struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr; | ||
46 | char *write_ptr; | ||
47 | |||
48 | attr = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_ATTRIBUTE_Claim) + | ||
49 | strlen (attr_name) + 1 + | ||
50 | data_size); | ||
51 | attr->type = attr_type; | ||
52 | attr->data_size = data_size; | ||
53 | attr->version = 0; | ||
54 | write_ptr = (char*)&attr[1]; | ||
55 | GNUNET_memcpy (write_ptr, | ||
56 | attr_name, | ||
57 | strlen (attr_name) + 1); | ||
58 | attr->name = write_ptr; | ||
59 | write_ptr += strlen (attr->name) + 1; | ||
60 | GNUNET_memcpy (write_ptr, | ||
61 | data, | ||
62 | data_size); | ||
63 | attr->data = write_ptr; | ||
64 | return attr; | ||
65 | } | ||
66 | |||
67 | size_t | ||
68 | GNUNET_IDENTITY_ATTRIBUTE_list_serialize_get_size (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs) | ||
69 | { | ||
70 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le; | ||
71 | size_t len = 0; | ||
72 | for (le = attrs->list_head; NULL != le; le = le->next) | ||
73 | len += GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (le->claim); | ||
74 | return len; | ||
75 | } | ||
76 | |||
77 | size_t | ||
78 | GNUNET_IDENTITY_ATTRIBUTE_list_serialize (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs, | ||
79 | char *result) | ||
80 | { | ||
81 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le; | ||
82 | size_t len; | ||
83 | size_t total_len; | ||
84 | char* write_ptr; | ||
85 | |||
86 | write_ptr = result; | ||
87 | total_len = 0; | ||
88 | for (le = attrs->list_head; NULL != le; le = le->next) | ||
89 | { | ||
90 | len = GNUNET_IDENTITY_ATTRIBUTE_serialize (le->claim, | ||
91 | write_ptr); | ||
92 | total_len += len; | ||
93 | write_ptr += len; | ||
94 | } | ||
95 | return total_len; | ||
96 | } | ||
97 | |||
98 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList * | ||
99 | GNUNET_IDENTITY_ATTRIBUTE_list_deserialize (const char* data, | ||
100 | size_t data_size) | ||
101 | { | ||
102 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs; | ||
103 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le; | ||
104 | size_t attr_len; | ||
105 | const char* read_ptr; | ||
106 | |||
107 | if (data_size < sizeof (struct Attribute)) | ||
108 | return NULL; | ||
109 | |||
110 | attrs = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList); | ||
111 | read_ptr = data; | ||
112 | while (((data + data_size) - read_ptr) >= sizeof (struct Attribute)) | ||
113 | { | ||
114 | |||
115 | le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry); | ||
116 | le->claim = GNUNET_IDENTITY_ATTRIBUTE_deserialize (read_ptr, | ||
117 | data_size - (read_ptr - data)); | ||
118 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
119 | "Deserialized attribute %s\n", le->claim->name); | ||
120 | GNUNET_CONTAINER_DLL_insert (attrs->list_head, | ||
121 | attrs->list_tail, | ||
122 | le); | ||
123 | attr_len = GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (le->claim); | ||
124 | read_ptr += attr_len; | ||
125 | } | ||
126 | return attrs; | ||
127 | } | ||
128 | |||
129 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList* | ||
130 | GNUNET_IDENTITY_ATTRIBUTE_list_dup (const struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs) | ||
131 | { | ||
132 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le; | ||
133 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *result_le; | ||
134 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *result; | ||
135 | size_t len; | ||
136 | |||
137 | result = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList); | ||
138 | for (le = attrs->list_head; NULL != le; le = le->next) | ||
139 | { | ||
140 | result_le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry); | ||
141 | len = sizeof (struct GNUNET_IDENTITY_ATTRIBUTE_Claim) + le->claim->data_size; | ||
142 | result_le->claim = GNUNET_malloc (len); | ||
143 | GNUNET_memcpy (result_le->claim, | ||
144 | le->claim, | ||
145 | len); | ||
146 | result_le->claim->name = (const char*)&result_le->claim[1]; | ||
147 | GNUNET_CONTAINER_DLL_insert (result->list_head, | ||
148 | result->list_tail, | ||
149 | result_le); | ||
150 | } | ||
151 | return result; | ||
152 | } | ||
153 | |||
154 | |||
155 | void | ||
156 | GNUNET_IDENTITY_ATTRIBUTE_list_destroy (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attrs) | ||
157 | { | ||
158 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le; | ||
159 | struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *tmp_le; | ||
160 | |||
161 | for (le = attrs->list_head; NULL != le;) | ||
162 | { | ||
163 | GNUNET_free (le->claim); | ||
164 | tmp_le = le; | ||
165 | le = le->next; | ||
166 | GNUNET_free (tmp_le); | ||
167 | } | ||
168 | GNUNET_free (attrs); | ||
169 | |||
170 | } | ||
171 | |||
172 | size_t | ||
173 | GNUNET_IDENTITY_ATTRIBUTE_serialize_get_size (const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr) | ||
174 | { | ||
175 | return sizeof (struct Attribute) | ||
176 | + strlen (attr->name) | ||
177 | + attr->data_size; | ||
178 | } | ||
179 | |||
180 | size_t | ||
181 | GNUNET_IDENTITY_ATTRIBUTE_serialize (const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr, | ||
182 | char *result) | ||
183 | { | ||
184 | size_t data_len_ser; | ||
185 | size_t name_len; | ||
186 | struct Attribute *attr_ser; | ||
187 | char* write_ptr; | ||
188 | |||
189 | attr_ser = (struct Attribute*)result; | ||
190 | attr_ser->attribute_type = htons (attr->type); | ||
191 | attr_ser->attribute_version = htonl (attr->version); | ||
192 | name_len = strlen (attr->name); | ||
193 | attr_ser->name_len = htons (name_len); | ||
194 | write_ptr = (char*)&attr_ser[1]; | ||
195 | GNUNET_memcpy (write_ptr, attr->name, name_len); | ||
196 | write_ptr += name_len; | ||
197 | //TODO plugin-ize | ||
198 | //data_len_ser = plugin->serialize_attribute_value (attr, | ||
199 | // &attr_ser[1]); | ||
200 | data_len_ser = attr->data_size; | ||
201 | GNUNET_memcpy (write_ptr, attr->data, attr->data_size); | ||
202 | attr_ser->data_size = htons (data_len_ser); | ||
203 | |||
204 | return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; | ||
205 | } | ||
206 | |||
207 | struct GNUNET_IDENTITY_ATTRIBUTE_Claim * | ||
208 | GNUNET_IDENTITY_ATTRIBUTE_deserialize (const char* data, | ||
209 | size_t data_size) | ||
210 | { | ||
211 | struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr; | ||
212 | struct Attribute *attr_ser; | ||
213 | size_t data_len; | ||
214 | size_t name_len; | ||
215 | char* write_ptr; | ||
216 | |||
217 | if (data_size < sizeof (struct Attribute)) | ||
218 | return NULL; | ||
219 | |||
220 | 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_ATTRIBUTE_Claim) | ||
224 | + data_len + name_len + 1); | ||
225 | attr->type = ntohs (attr_ser->attribute_type); | ||
226 | attr->version = ntohl (attr_ser->attribute_version); | ||
227 | attr->data_size = ntohs (attr_ser->data_size); | ||
228 | |||
229 | write_ptr = (char*)&attr[1]; | ||
230 | GNUNET_memcpy (write_ptr, | ||
231 | &attr_ser[1], | ||
232 | name_len); | ||
233 | write_ptr[name_len] = '\0'; | ||
234 | attr->name = write_ptr; | ||
235 | |||
236 | write_ptr += name_len + 1; | ||
237 | GNUNET_memcpy (write_ptr, | ||
238 | (char*)&attr_ser[1] + name_len, | ||
239 | attr->data_size); | ||
240 | attr->data = write_ptr; | ||
241 | return attr; | ||
242 | |||
243 | } | ||
244 | |||
245 | /* end of identity_attribute.c */ | ||