diff options
Diffstat (limited to 'src/reclaim-attribute/reclaim_attribute.c')
-rw-r--r-- | src/reclaim-attribute/reclaim_attribute.c | 208 |
1 files changed, 155 insertions, 53 deletions
diff --git a/src/reclaim-attribute/reclaim_attribute.c b/src/reclaim-attribute/reclaim_attribute.c index b434c386e..6e5f14ddf 100644 --- a/src/reclaim-attribute/reclaim_attribute.c +++ b/src/reclaim-attribute/reclaim_attribute.c | |||
@@ -24,11 +24,11 @@ | |||
24 | * @author Martin Schanzenbach | 24 | * @author Martin Schanzenbach |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | |||
28 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
29 | |||
30 | #include "gnunet_reclaim_attribute_plugin.h" | 28 | #include "gnunet_reclaim_attribute_plugin.h" |
31 | #include "reclaim_attribute.h" | 29 | #include "reclaim_attribute.h" |
30 | |||
31 | |||
32 | /** | 32 | /** |
33 | * Handle for a plugin | 33 | * Handle for a plugin |
34 | */ | 34 | */ |
@@ -45,23 +45,31 @@ struct Plugin | |||
45 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api; | 45 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api; |
46 | }; | 46 | }; |
47 | 47 | ||
48 | |||
48 | /** | 49 | /** |
49 | * Plugins | 50 | * Plugins |
50 | */ | 51 | */ |
51 | static struct Plugin **attr_plugins; | 52 | static struct Plugin **attr_plugins; |
52 | 53 | ||
54 | |||
53 | /** | 55 | /** |
54 | * Number of plugins | 56 | * Number of plugins |
55 | */ | 57 | */ |
56 | static unsigned int num_plugins; | 58 | static unsigned int num_plugins; |
57 | 59 | ||
60 | |||
58 | /** | 61 | /** |
59 | * Init canary | 62 | * Init canary |
60 | */ | 63 | */ |
61 | static int initialized; | 64 | static int initialized; |
62 | 65 | ||
66 | |||
63 | /** | 67 | /** |
64 | * Add a plugin | 68 | * Add a plugin |
69 | * | ||
70 | * @param cls closure | ||
71 | * @param library_name name of the API library | ||
72 | * @param lib_ret the plugin API pointer | ||
65 | */ | 73 | */ |
66 | static void | 74 | static void |
67 | add_plugin (void *cls, const char *library_name, void *lib_ret) | 75 | add_plugin (void *cls, const char *library_name, void *lib_ret) |
@@ -69,7 +77,8 @@ add_plugin (void *cls, const char *library_name, void *lib_ret) | |||
69 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret; | 77 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = lib_ret; |
70 | struct Plugin *plugin; | 78 | struct Plugin *plugin; |
71 | 79 | ||
72 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading attribute plugin `%s'\n", | 80 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
81 | "Loading attribute plugin `%s'\n", | ||
73 | library_name); | 82 | library_name); |
74 | plugin = GNUNET_new (struct Plugin); | 83 | plugin = GNUNET_new (struct Plugin); |
75 | plugin->api = api; | 84 | plugin->api = api; |
@@ -77,6 +86,7 @@ add_plugin (void *cls, const char *library_name, void *lib_ret) | |||
77 | GNUNET_array_append (attr_plugins, num_plugins, plugin); | 86 | GNUNET_array_append (attr_plugins, num_plugins, plugin); |
78 | } | 87 | } |
79 | 88 | ||
89 | |||
80 | /** | 90 | /** |
81 | * Load plugins | 91 | * Load plugins |
82 | */ | 92 | */ |
@@ -86,10 +96,13 @@ init () | |||
86 | if (GNUNET_YES == initialized) | 96 | if (GNUNET_YES == initialized) |
87 | return; | 97 | return; |
88 | initialized = GNUNET_YES; | 98 | initialized = GNUNET_YES; |
89 | GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_", NULL, | 99 | GNUNET_PLUGIN_load_all ("libgnunet_plugin_reclaim_attribute_", |
90 | &add_plugin, NULL); | 100 | NULL, |
101 | &add_plugin, | ||
102 | NULL); | ||
91 | } | 103 | } |
92 | 104 | ||
105 | |||
93 | /** | 106 | /** |
94 | * Convert a type name to the corresponding number | 107 | * Convert a type name to the corresponding number |
95 | * | 108 | * |
@@ -104,7 +117,8 @@ GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename) | |||
104 | uint32_t ret; | 117 | uint32_t ret; |
105 | 118 | ||
106 | init (); | 119 | init (); |
107 | for (i = 0; i < num_plugins; i++) { | 120 | for (i = 0; i < num_plugins; i++) |
121 | { | ||
108 | plugin = attr_plugins[i]; | 122 | plugin = attr_plugins[i]; |
109 | if (UINT32_MAX != | 123 | if (UINT32_MAX != |
110 | (ret = plugin->api->typename_to_number (plugin->api->cls, typename))) | 124 | (ret = plugin->api->typename_to_number (plugin->api->cls, typename))) |
@@ -113,6 +127,7 @@ GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (const char *typename) | |||
113 | return UINT32_MAX; | 127 | return UINT32_MAX; |
114 | } | 128 | } |
115 | 129 | ||
130 | |||
116 | /** | 131 | /** |
117 | * Convert a type number to the corresponding type string | 132 | * Convert a type number to the corresponding type string |
118 | * | 133 | * |
@@ -127,7 +142,8 @@ GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) | |||
127 | const char *ret; | 142 | const char *ret; |
128 | 143 | ||
129 | init (); | 144 | init (); |
130 | for (i = 0; i < num_plugins; i++) { | 145 | for (i = 0; i < num_plugins; i++) |
146 | { | ||
131 | plugin = attr_plugins[i]; | 147 | plugin = attr_plugins[i]; |
132 | if (NULL != | 148 | if (NULL != |
133 | (ret = plugin->api->number_to_typename (plugin->api->cls, type))) | 149 | (ret = plugin->api->number_to_typename (plugin->api->cls, type))) |
@@ -136,6 +152,7 @@ GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) | |||
136 | return NULL; | 152 | return NULL; |
137 | } | 153 | } |
138 | 154 | ||
155 | |||
139 | /** | 156 | /** |
140 | * Convert human-readable version of a 'claim' of an attribute to the binary | 157 | * Convert human-readable version of a 'claim' of an attribute to the binary |
141 | * representation | 158 | * representation |
@@ -147,22 +164,29 @@ GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (uint32_t type) | |||
147 | * @return #GNUNET_OK on success | 164 | * @return #GNUNET_OK on success |
148 | */ | 165 | */ |
149 | int | 166 | int |
150 | GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, const char *s, | 167 | GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, |
151 | void **data, size_t *data_size) | 168 | const char *s, |
169 | void **data, | ||
170 | size_t *data_size) | ||
152 | { | 171 | { |
153 | unsigned int i; | 172 | unsigned int i; |
154 | struct Plugin *plugin; | 173 | struct Plugin *plugin; |
155 | 174 | ||
156 | init (); | 175 | init (); |
157 | for (i = 0; i < num_plugins; i++) { | 176 | for (i = 0; i < num_plugins; i++) |
177 | { | ||
158 | plugin = attr_plugins[i]; | 178 | plugin = attr_plugins[i]; |
159 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, type, s, | 179 | if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, |
160 | data, data_size)) | 180 | type, |
181 | s, | ||
182 | data, | ||
183 | data_size)) | ||
161 | return GNUNET_OK; | 184 | return GNUNET_OK; |
162 | } | 185 | } |
163 | return GNUNET_SYSERR; | 186 | return GNUNET_SYSERR; |
164 | } | 187 | } |
165 | 188 | ||
189 | |||
166 | /** | 190 | /** |
167 | * Convert the 'claim' of an attribute to a string | 191 | * Convert the 'claim' of an attribute to a string |
168 | * | 192 | * |
@@ -172,7 +196,8 @@ GNUNET_RECLAIM_ATTRIBUTE_string_to_value (uint32_t type, const char *s, | |||
172 | * @return NULL on error, otherwise human-readable representation of the claim | 196 | * @return NULL on error, otherwise human-readable representation of the claim |
173 | */ | 197 | */ |
174 | char * | 198 | char * |
175 | GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, const void *data, | 199 | GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, |
200 | const void *data, | ||
176 | size_t data_size) | 201 | size_t data_size) |
177 | { | 202 | { |
178 | unsigned int i; | 203 | unsigned int i; |
@@ -180,15 +205,19 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, const void *data, | |||
180 | char *ret; | 205 | char *ret; |
181 | 206 | ||
182 | init (); | 207 | init (); |
183 | for (i = 0; i < num_plugins; i++) { | 208 | for (i = 0; i < num_plugins; i++) |
209 | { | ||
184 | plugin = attr_plugins[i]; | 210 | plugin = attr_plugins[i]; |
185 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, type, | 211 | if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, |
186 | data, data_size))) | 212 | type, |
213 | data, | ||
214 | data_size))) | ||
187 | return ret; | 215 | return ret; |
188 | } | 216 | } |
189 | return NULL; | 217 | return NULL; |
190 | } | 218 | } |
191 | 219 | ||
220 | |||
192 | /** | 221 | /** |
193 | * Create a new attribute. | 222 | * Create a new attribute. |
194 | * | 223 | * |
@@ -199,8 +228,10 @@ GNUNET_RECLAIM_ATTRIBUTE_value_to_string (uint32_t type, const void *data, | |||
199 | * @return the new attribute | 228 | * @return the new attribute |
200 | */ | 229 | */ |
201 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim * | 230 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim * |
202 | GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, uint32_t type, | 231 | GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, |
203 | const void *data, size_t data_size) | 232 | uint32_t type, |
233 | const void *data, | ||
234 | size_t data_size) | ||
204 | { | 235 | { |
205 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; | 236 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; |
206 | char *write_ptr; | 237 | char *write_ptr; |
@@ -213,7 +244,7 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, uint32_t type, | |||
213 | attr->type = type; | 244 | attr->type = type; |
214 | attr->data_size = data_size; | 245 | attr->data_size = data_size; |
215 | attr->version = 0; | 246 | attr->version = 0; |
216 | write_ptr = (char *)&attr[1]; | 247 | write_ptr = (char *) &attr[1]; |
217 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); | 248 | GNUNET_memcpy (write_ptr, attr_name_tmp, strlen (attr_name_tmp) + 1); |
218 | attr->name = write_ptr; | 249 | attr->name = write_ptr; |
219 | write_ptr += strlen (attr->name) + 1; | 250 | write_ptr += strlen (attr->name) + 1; |
@@ -223,32 +254,42 @@ GNUNET_RECLAIM_ATTRIBUTE_claim_new (const char *attr_name, uint32_t type, | |||
223 | return attr; | 254 | return attr; |
224 | } | 255 | } |
225 | 256 | ||
257 | |||
226 | /** | 258 | /** |
227 | * Add a new claim list entry. | 259 | * Add a new attribute to a claim list |
228 | * | 260 | * |
229 | * @param claim_list the attribute name | 261 | * @param attr_name the name of the new attribute claim |
230 | * @param attr_name the attribute name | 262 | * @param type the type of the claim |
231 | * @param type the attribute type | 263 | * @param data claim payload |
232 | * @param data the attribute value | 264 | * @param data_size claim payload size |
233 | * @param data_size the attribute value size | ||
234 | * @return | ||
235 | */ | 265 | */ |
236 | void | 266 | void |
237 | GNUNET_RECLAIM_ATTRIBUTE_list_add ( | 267 | GNUNET_RECLAIM_ATTRIBUTE_list_add ( |
238 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list, | 268 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *claim_list, |
239 | const char *attr_name, uint32_t type, const void *data, size_t data_size) | 269 | const char *attr_name, |
270 | uint32_t type, | ||
271 | const void *data, | ||
272 | size_t data_size) | ||
240 | { | 273 | { |
241 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 274 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
242 | le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); | 275 | le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); |
243 | le->claim = | 276 | le->claim = |
244 | GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size); | 277 | GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size); |
245 | GNUNET_CONTAINER_DLL_insert (claim_list->list_head, claim_list->list_tail, | 278 | GNUNET_CONTAINER_DLL_insert (claim_list->list_head, |
279 | claim_list->list_tail, | ||
246 | le); | 280 | le); |
247 | } | 281 | } |
248 | 282 | ||
283 | |||
284 | /** | ||
285 | * Get required size for serialization buffer | ||
286 | * | ||
287 | * @param attrs the attribute list to serialize | ||
288 | * @return the required buffer size | ||
289 | */ | ||
249 | size_t | 290 | size_t |
250 | GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size ( | 291 | GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size ( |
251 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) | 292 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) |
252 | { | 293 | { |
253 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 294 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
254 | size_t len = 0; | 295 | size_t len = 0; |
@@ -257,9 +298,18 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size ( | |||
257 | return len; | 298 | return len; |
258 | } | 299 | } |
259 | 300 | ||
301 | |||
302 | /** | ||
303 | * Serialize an attribute list | ||
304 | * | ||
305 | * @param attrs the attribute list to serialize | ||
306 | * @param result the serialized attribute | ||
307 | * @return length of serialized data | ||
308 | */ | ||
260 | size_t | 309 | size_t |
261 | GNUNET_RECLAIM_ATTRIBUTE_list_serialize ( | 310 | GNUNET_RECLAIM_ATTRIBUTE_list_serialize ( |
262 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, char *result) | 311 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs, |
312 | char *result) | ||
263 | { | 313 | { |
264 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 314 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
265 | size_t len; | 315 | size_t len; |
@@ -268,7 +318,8 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize ( | |||
268 | 318 | ||
269 | write_ptr = result; | 319 | write_ptr = result; |
270 | total_len = 0; | 320 | total_len = 0; |
271 | for (le = attrs->list_head; NULL != le; le = le->next) { | 321 | for (le = attrs->list_head; NULL != le; le = le->next) |
322 | { | ||
272 | len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr); | 323 | len = GNUNET_RECLAIM_ATTRIBUTE_serialize (le->claim, write_ptr); |
273 | total_len += len; | 324 | total_len += len; |
274 | write_ptr += len; | 325 | write_ptr += len; |
@@ -276,6 +327,14 @@ GNUNET_RECLAIM_ATTRIBUTE_list_serialize ( | |||
276 | return total_len; | 327 | return total_len; |
277 | } | 328 | } |
278 | 329 | ||
330 | |||
331 | /** | ||
332 | * Deserialize an attribute list | ||
333 | * | ||
334 | * @param data the serialized attribute list | ||
335 | * @param data_size the length of the serialized data | ||
336 | * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller | ||
337 | */ | ||
279 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * | 338 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * |
280 | GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size) | 339 | GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size) |
281 | { | 340 | { |
@@ -289,12 +348,15 @@ GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size) | |||
289 | 348 | ||
290 | attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); | 349 | attrs = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); |
291 | read_ptr = data; | 350 | read_ptr = data; |
292 | while (((data + data_size) - read_ptr) >= sizeof (struct Attribute)) { | 351 | while (((data + data_size) - read_ptr) >= sizeof (struct Attribute)) |
352 | { | ||
293 | 353 | ||
294 | le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); | 354 | le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); |
295 | le->claim = GNUNET_RECLAIM_ATTRIBUTE_deserialize ( | 355 | le->claim = |
296 | read_ptr, data_size - (read_ptr - data)); | 356 | GNUNET_RECLAIM_ATTRIBUTE_deserialize (read_ptr, |
297 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized attribute %s\n", | 357 | data_size - (read_ptr - data)); |
358 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
359 | "Deserialized attribute %s\n", | ||
298 | le->claim->name); | 360 | le->claim->name); |
299 | GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le); | 361 | GNUNET_CONTAINER_DLL_insert (attrs->list_head, attrs->list_tail, le); |
300 | attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim); | 362 | attr_len = GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size (le->claim); |
@@ -303,37 +365,52 @@ GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (const char *data, size_t data_size) | |||
303 | return attrs; | 365 | return attrs; |
304 | } | 366 | } |
305 | 367 | ||
368 | |||
369 | /** | ||
370 | * Make a (deep) copy of a claim list | ||
371 | * @param attrs claim list to copy | ||
372 | * @return copied claim list | ||
373 | */ | ||
306 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * | 374 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList * |
307 | GNUNET_RECLAIM_ATTRIBUTE_list_dup ( | 375 | GNUNET_RECLAIM_ATTRIBUTE_list_dup ( |
308 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) | 376 | const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) |
309 | { | 377 | { |
310 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 378 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
311 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le; | 379 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *result_le; |
312 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result; | 380 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *result; |
313 | 381 | ||
314 | result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); | 382 | result = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList); |
315 | for (le = attrs->list_head; NULL != le; le = le->next) { | 383 | for (le = attrs->list_head; NULL != le; le = le->next) |
384 | { | ||
316 | result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); | 385 | result_le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry); |
317 | result_le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new ( | 386 | result_le->claim = |
318 | le->claim->name, le->claim->type, le->claim->data, | 387 | GNUNET_RECLAIM_ATTRIBUTE_claim_new (le->claim->name, |
319 | le->claim->data_size); | 388 | le->claim->type, |
389 | le->claim->data, | ||
390 | le->claim->data_size); | ||
320 | result_le->claim->version = le->claim->version; | 391 | result_le->claim->version = le->claim->version; |
321 | result_le->claim->id = le->claim->id; | 392 | result_le->claim->id = le->claim->id; |
322 | GNUNET_CONTAINER_DLL_insert (result->list_head, result->list_tail, | 393 | GNUNET_CONTAINER_DLL_insert (result->list_head, |
394 | result->list_tail, | ||
323 | result_le); | 395 | result_le); |
324 | } | 396 | } |
325 | return result; | 397 | return result; |
326 | } | 398 | } |
327 | 399 | ||
328 | 400 | ||
329 | void | 401 | /** |
402 | * Destroy claim list | ||
403 | * | ||
404 | * @param attrs list to destroy | ||
405 | */ | ||
330 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy ( | 406 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy ( |
331 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) | 407 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs) |
332 | { | 408 | { |
333 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 409 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
334 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le; | 410 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *tmp_le; |
335 | 411 | ||
336 | for (le = attrs->list_head; NULL != le;) { | 412 | for (le = attrs->list_head; NULL != le;) |
413 | { | ||
337 | GNUNET_free (le->claim); | 414 | GNUNET_free (le->claim); |
338 | tmp_le = le; | 415 | tmp_le = le; |
339 | le = le->next; | 416 | le = le->next; |
@@ -342,29 +419,45 @@ GNUNET_RECLAIM_ATTRIBUTE_list_destroy ( | |||
342 | GNUNET_free (attrs); | 419 | GNUNET_free (attrs); |
343 | } | 420 | } |
344 | 421 | ||
422 | |||
423 | /** | ||
424 | * Get required size for serialization buffer | ||
425 | * | ||
426 | * @param attr the attribute to serialize | ||
427 | * @return the required buffer size | ||
428 | */ | ||
345 | size_t | 429 | size_t |
346 | GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size ( | 430 | GNUNET_RECLAIM_ATTRIBUTE_serialize_get_size ( |
347 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) | 431 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr) |
348 | { | 432 | { |
349 | return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; | 433 | return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; |
350 | } | 434 | } |
351 | 435 | ||
436 | |||
437 | /** | ||
438 | * Serialize an attribute | ||
439 | * | ||
440 | * @param attr the attribute to serialize | ||
441 | * @param result the serialized attribute | ||
442 | * @return length of serialized data | ||
443 | */ | ||
352 | size_t | 444 | size_t |
353 | GNUNET_RECLAIM_ATTRIBUTE_serialize ( | 445 | GNUNET_RECLAIM_ATTRIBUTE_serialize ( |
354 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr, char *result) | 446 | const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr, |
447 | char *result) | ||
355 | { | 448 | { |
356 | size_t data_len_ser; | 449 | size_t data_len_ser; |
357 | size_t name_len; | 450 | size_t name_len; |
358 | struct Attribute *attr_ser; | 451 | struct Attribute *attr_ser; |
359 | char *write_ptr; | 452 | char *write_ptr; |
360 | 453 | ||
361 | attr_ser = (struct Attribute *)result; | 454 | attr_ser = (struct Attribute *) result; |
362 | attr_ser->attribute_type = htons (attr->type); | 455 | attr_ser->attribute_type = htons (attr->type); |
363 | attr_ser->attribute_version = htonl (attr->version); | 456 | attr_ser->attribute_version = htonl (attr->version); |
364 | attr_ser->attribute_id = GNUNET_htonll (attr->id); | 457 | attr_ser->attribute_id = GNUNET_htonll (attr->id); |
365 | name_len = strlen (attr->name); | 458 | name_len = strlen (attr->name); |
366 | attr_ser->name_len = htons (name_len); | 459 | attr_ser->name_len = htons (name_len); |
367 | write_ptr = (char *)&attr_ser[1]; | 460 | write_ptr = (char *) &attr_ser[1]; |
368 | GNUNET_memcpy (write_ptr, attr->name, name_len); | 461 | GNUNET_memcpy (write_ptr, attr->name, name_len); |
369 | write_ptr += name_len; | 462 | write_ptr += name_len; |
370 | // TODO plugin-ize | 463 | // TODO plugin-ize |
@@ -377,6 +470,15 @@ GNUNET_RECLAIM_ATTRIBUTE_serialize ( | |||
377 | return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; | 470 | return sizeof (struct Attribute) + strlen (attr->name) + attr->data_size; |
378 | } | 471 | } |
379 | 472 | ||
473 | |||
474 | /** | ||
475 | * Deserialize an attribute | ||
476 | * | ||
477 | * @param data the serialized attribute | ||
478 | * @param data_size the length of the serialized data | ||
479 | * | ||
480 | * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller | ||
481 | */ | ||
380 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim * | 482 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim * |
381 | GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) | 483 | GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) |
382 | { | 484 | { |
@@ -389,7 +491,7 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) | |||
389 | if (data_size < sizeof (struct Attribute)) | 491 | if (data_size < sizeof (struct Attribute)) |
390 | return NULL; | 492 | return NULL; |
391 | 493 | ||
392 | attr_ser = (struct Attribute *)data; | 494 | attr_ser = (struct Attribute *) data; |
393 | data_len = ntohs (attr_ser->data_size); | 495 | data_len = ntohs (attr_ser->data_size); |
394 | name_len = ntohs (attr_ser->name_len); | 496 | name_len = ntohs (attr_ser->name_len); |
395 | attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) + | 497 | attr = GNUNET_malloc (sizeof (struct GNUNET_RECLAIM_ATTRIBUTE_Claim) + |
@@ -399,13 +501,13 @@ GNUNET_RECLAIM_ATTRIBUTE_deserialize (const char *data, size_t data_size) | |||
399 | attr->id = GNUNET_ntohll (attr_ser->attribute_id); | 501 | attr->id = GNUNET_ntohll (attr_ser->attribute_id); |
400 | attr->data_size = ntohs (attr_ser->data_size); | 502 | attr->data_size = ntohs (attr_ser->data_size); |
401 | 503 | ||
402 | write_ptr = (char *)&attr[1]; | 504 | write_ptr = (char *) &attr[1]; |
403 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); | 505 | GNUNET_memcpy (write_ptr, &attr_ser[1], name_len); |
404 | write_ptr[name_len] = '\0'; | 506 | write_ptr[name_len] = '\0'; |
405 | attr->name = write_ptr; | 507 | attr->name = write_ptr; |
406 | 508 | ||
407 | write_ptr += name_len + 1; | 509 | write_ptr += name_len + 1; |
408 | GNUNET_memcpy (write_ptr, (char *)&attr_ser[1] + name_len, attr->data_size); | 510 | GNUNET_memcpy (write_ptr, (char *) &attr_ser[1] + name_len, attr->data_size); |
409 | attr->data = write_ptr; | 511 | attr->data = write_ptr; |
410 | return attr; | 512 | return attr; |
411 | } | 513 | } |