diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-06-30 11:57:07 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-06-30 11:57:28 +0200 |
commit | bc458ee7949e01e0b5a1f7f15773c02be94cb8fa (patch) | |
tree | 3491aafa2f3dc5265145b5d27a2b468b140c50ee /src/namestore | |
parent | f9b0e00a193e735db74f0abe907caf97c7da054b (diff) | |
download | gnunet-bc458ee7949e01e0b5a1f7f15773c02be94cb8fa.tar.gz gnunet-bc458ee7949e01e0b5a1f7f15773c02be94cb8fa.zip |
simplify memory allocation in plugin_namestore_flat, use cleaner signatures for base64 encoding/decoding functions
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/plugin_namestore_flat.c | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c index 33c48b244..e16fe91b7 100644 --- a/src/namestore/plugin_namestore_flat.c +++ b/src/namestore/plugin_namestore_flat.c | |||
@@ -55,7 +55,7 @@ struct FlatFileEntry | |||
55 | /** | 55 | /** |
56 | * Entry zone | 56 | * Entry zone |
57 | */ | 57 | */ |
58 | struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key; | 58 | struct GNUNET_CRYPTO_EcdsaPrivateKey private_key; |
59 | 59 | ||
60 | /** | 60 | /** |
61 | * Record cound | 61 | * Record cound |
@@ -93,7 +93,6 @@ static int | |||
93 | database_setup (struct Plugin *plugin) | 93 | database_setup (struct Plugin *plugin) |
94 | { | 94 | { |
95 | char *afsdir; | 95 | char *afsdir; |
96 | char *key; | ||
97 | char *record_data; | 96 | char *record_data; |
98 | char *zone_private_key; | 97 | char *zone_private_key; |
99 | char *record_data_b64; | 98 | char *record_data_b64; |
@@ -104,7 +103,6 @@ database_setup (struct Plugin *plugin) | |||
104 | char *record_count; | 103 | char *record_count; |
105 | size_t record_data_size; | 104 | size_t record_data_size; |
106 | uint64_t size; | 105 | uint64_t size; |
107 | size_t key_len; | ||
108 | struct GNUNET_HashCode hkey; | 106 | struct GNUNET_HashCode hkey; |
109 | struct GNUNET_DISK_FileHandle *fh; | 107 | struct GNUNET_DISK_FileHandle *fh; |
110 | struct FlatFileEntry *entry; | 108 | struct FlatFileEntry *entry; |
@@ -232,7 +230,7 @@ database_setup (struct Plugin *plugin) | |||
232 | record_data_size | 230 | record_data_size |
233 | = GNUNET_STRINGS_base64_decode (record_data_b64, | 231 | = GNUNET_STRINGS_base64_decode (record_data_b64, |
234 | strlen (record_data_b64), | 232 | strlen (record_data_b64), |
235 | &record_data); | 233 | (void **) &record_data); |
236 | entry->record_data = | 234 | entry->record_data = |
237 | GNUNET_new_array (entry->record_count, | 235 | GNUNET_new_array (entry->record_count, |
238 | struct GNUNET_GNSRECORD_Data); | 236 | struct GNUNET_GNSRECORD_Data); |
@@ -251,21 +249,34 @@ database_setup (struct Plugin *plugin) | |||
251 | break; | 249 | break; |
252 | } | 250 | } |
253 | GNUNET_free (record_data); | 251 | GNUNET_free (record_data); |
254 | GNUNET_STRINGS_base64_decode (zone_private_key, | 252 | |
255 | strlen (zone_private_key), | 253 | { |
256 | (char**)&entry->private_key); | 254 | struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key; |
257 | key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); | 255 | |
258 | key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); | 256 | GNUNET_STRINGS_base64_decode (zone_private_key, |
259 | GNUNET_memcpy (key, | 257 | strlen (zone_private_key), |
260 | label, | 258 | (void**)&private_key); |
261 | strlen (label)); | 259 | entry->private_key = *private_key; |
262 | GNUNET_memcpy (key+strlen(label), | 260 | GNUNET_free (private_key); |
263 | entry->private_key, | 261 | } |
264 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); | 262 | |
265 | GNUNET_CRYPTO_hash (key, | 263 | { |
266 | key_len, | 264 | char *key; |
267 | &hkey); | 265 | size_t key_len; |
268 | GNUNET_free (key); | 266 | |
267 | key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); | ||
268 | key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); | ||
269 | GNUNET_memcpy (key, | ||
270 | label, | ||
271 | strlen (label)); | ||
272 | GNUNET_memcpy (key+strlen(label), | ||
273 | &entry->private_key, | ||
274 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); | ||
275 | GNUNET_CRYPTO_hash (key, | ||
276 | key_len, | ||
277 | &hkey); | ||
278 | GNUNET_free (key); | ||
279 | } | ||
269 | if (GNUNET_OK != | 280 | if (GNUNET_OK != |
270 | GNUNET_CONTAINER_multihashmap_put (plugin->hm, | 281 | GNUNET_CONTAINER_multihashmap_put (plugin->hm, |
271 | &hkey, | 282 | &hkey, |
@@ -302,7 +313,7 @@ store_and_free_entries (void *cls, | |||
302 | ssize_t data_size; | 313 | ssize_t data_size; |
303 | 314 | ||
304 | (void) key; | 315 | (void) key; |
305 | GNUNET_STRINGS_base64_encode ((char*)entry->private_key, | 316 | GNUNET_STRINGS_base64_encode (&entry->private_key, |
306 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), | 317 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), |
307 | &zone_private_key); | 318 | &zone_private_key); |
308 | data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count, | 319 | data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count, |
@@ -353,7 +364,6 @@ store_and_free_entries (void *cls, | |||
353 | strlen (line)); | 364 | strlen (line)); |
354 | 365 | ||
355 | GNUNET_free (line); | 366 | GNUNET_free (line); |
356 | GNUNET_free (entry->private_key); | ||
357 | GNUNET_free (entry->label); | 367 | GNUNET_free (entry->label); |
358 | GNUNET_free (entry->record_data); | 368 | GNUNET_free (entry->record_data); |
359 | GNUNET_free (entry); | 369 | GNUNET_free (entry); |
@@ -441,11 +451,10 @@ namestore_flat_store_records (void *cls, | |||
441 | return GNUNET_OK; | 451 | return GNUNET_OK; |
442 | } | 452 | } |
443 | entry = GNUNET_new (struct FlatFileEntry); | 453 | entry = GNUNET_new (struct FlatFileEntry); |
444 | entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey); | ||
445 | GNUNET_asprintf (&entry->label, | 454 | GNUNET_asprintf (&entry->label, |
446 | label, | 455 | label, |
447 | strlen (label)); | 456 | strlen (label)); |
448 | GNUNET_memcpy (entry->private_key, | 457 | GNUNET_memcpy (&entry->private_key, |
449 | zone_key, | 458 | zone_key, |
450 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); | 459 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); |
451 | entry->rvalue = rvalue; | 460 | entry->rvalue = rvalue; |
@@ -519,7 +528,7 @@ namestore_flat_lookup_records (void *cls, | |||
519 | if (NULL != iter) | 528 | if (NULL != iter) |
520 | iter (iter_cls, | 529 | iter (iter_cls, |
521 | 0, | 530 | 0, |
522 | entry->private_key, | 531 | &entry->private_key, |
523 | entry->label, | 532 | entry->label, |
524 | entry->record_count, | 533 | entry->record_count, |
525 | entry->record_data); | 534 | entry->record_data); |
@@ -586,7 +595,7 @@ iterate_zones (void *cls, | |||
586 | if (0 == ic->limit) | 595 | if (0 == ic->limit) |
587 | return GNUNET_NO; | 596 | return GNUNET_NO; |
588 | if ( (NULL != ic->zone) && | 597 | if ( (NULL != ic->zone) && |
589 | (0 != memcmp (entry->private_key, | 598 | (0 != memcmp (&entry->private_key, |
590 | ic->zone, | 599 | ic->zone, |
591 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ) | 600 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ) |
592 | return GNUNET_YES; | 601 | return GNUNET_YES; |
@@ -598,7 +607,7 @@ iterate_zones (void *cls, | |||
598 | } | 607 | } |
599 | ic->iter (ic->iter_cls, | 608 | ic->iter (ic->iter_cls, |
600 | ic->pos, | 609 | ic->pos, |
601 | entry->private_key, | 610 | &entry->private_key, |
602 | entry->label, | 611 | entry->label, |
603 | entry->record_count, | 612 | entry->record_count, |
604 | entry->record_data); | 613 | entry->record_data); |
@@ -668,7 +677,7 @@ zone_to_name (void *cls, | |||
668 | struct FlatFileEntry *entry = value; | 677 | struct FlatFileEntry *entry = value; |
669 | 678 | ||
670 | (void) key; | 679 | (void) key; |
671 | if (0 != memcmp (entry->private_key, | 680 | if (0 != memcmp (&entry->private_key, |
672 | ztn->zone, | 681 | ztn->zone, |
673 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) | 682 | sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) |
674 | return GNUNET_YES; | 683 | return GNUNET_YES; |
@@ -683,7 +692,7 @@ zone_to_name (void *cls, | |||
683 | { | 692 | { |
684 | ztn->iter (ztn->iter_cls, | 693 | ztn->iter (ztn->iter_cls, |
685 | 0, | 694 | 0, |
686 | entry->private_key, | 695 | &entry->private_key, |
687 | entry->label, | 696 | entry->label, |
688 | entry->record_count, | 697 | entry->record_count, |
689 | entry->record_data); | 698 | entry->record_data); |