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 | |
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')
-rw-r--r-- | src/gnsrecord/plugin_gnsrecord_dns.c | 4 | ||||
-rw-r--r-- | src/include/gnunet_strings_lib.h | 8 | ||||
-rw-r--r-- | src/namestore/plugin_namestore_flat.c | 65 | ||||
-rw-r--r-- | src/util/strings.c | 39 |
4 files changed, 64 insertions, 52 deletions
diff --git a/src/gnsrecord/plugin_gnsrecord_dns.c b/src/gnsrecord/plugin_gnsrecord_dns.c index 188afcae7..254ae15ea 100644 --- a/src/gnsrecord/plugin_gnsrecord_dns.c +++ b/src/gnsrecord/plugin_gnsrecord_dns.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ | 17 | */ |
@@ -463,7 +463,7 @@ dns_string_to_value (void *cls, | |||
463 | } | 463 | } |
464 | cert_size = GNUNET_STRINGS_base64_decode (certp, | 464 | cert_size = GNUNET_STRINGS_base64_decode (certp, |
465 | strlen (certp), | 465 | strlen (certp), |
466 | &cert_data); | 466 | (void **) &cert_data); |
467 | GNUNET_free (sdup); | 467 | GNUNET_free (sdup); |
468 | cert.cert_type = type; | 468 | cert.cert_type = type; |
469 | cert.cert_tag = key; | 469 | cert.cert_tag = key; |
diff --git a/src/include/gnunet_strings_lib.h b/src/include/gnunet_strings_lib.h index 1fdab93b2..c1d76ef71 100644 --- a/src/include/gnunet_strings_lib.h +++ b/src/include/gnunet_strings_lib.h | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ | 17 | */ |
@@ -339,7 +339,9 @@ GNUNET_STRINGS_string_to_data (const char *enc, | |||
339 | * @return the size of the output | 339 | * @return the size of the output |
340 | */ | 340 | */ |
341 | size_t | 341 | size_t |
342 | GNUNET_STRINGS_base64_encode (const char *data, size_t len, char **output); | 342 | GNUNET_STRINGS_base64_encode (const void *in, |
343 | size_t len, | ||
344 | char **output); | ||
343 | 345 | ||
344 | 346 | ||
345 | /** | 347 | /** |
@@ -354,7 +356,7 @@ GNUNET_STRINGS_base64_encode (const char *data, size_t len, char **output); | |||
354 | size_t | 356 | size_t |
355 | GNUNET_STRINGS_base64_decode (const char *data, | 357 | GNUNET_STRINGS_base64_decode (const char *data, |
356 | size_t len, | 358 | size_t len, |
357 | char **output); | 359 | void **output); |
358 | 360 | ||
359 | 361 | ||
360 | /** | 362 | /** |
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); |
diff --git a/src/util/strings.c b/src/util/strings.c index 5ed195933..ea3c8cfb9 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ | 17 | */ |
@@ -1947,27 +1947,27 @@ static char *cvt = | |||
1947 | /** | 1947 | /** |
1948 | * Encode into Base64. | 1948 | * Encode into Base64. |
1949 | * | 1949 | * |
1950 | * @param data the data to encode | 1950 | * @param in the data to encode |
1951 | * @param len the length of the input | 1951 | * @param len the length of the input |
1952 | * @param output where to write the output (*output should be NULL, | 1952 | * @param output where to write the output (*output should be NULL, |
1953 | * is allocated) | 1953 | * is allocated) |
1954 | * @return the size of the output | 1954 | * @return the size of the output |
1955 | */ | 1955 | */ |
1956 | size_t | 1956 | size_t |
1957 | GNUNET_STRINGS_base64_encode (const char *data, | 1957 | GNUNET_STRINGS_base64_encode (const void *in, |
1958 | size_t len, | 1958 | size_t len, |
1959 | char **output) | 1959 | char **output) |
1960 | { | 1960 | { |
1961 | size_t i; | 1961 | const char *data = in; |
1962 | char c; | ||
1963 | size_t ret; | 1962 | size_t ret; |
1964 | char *opt; | 1963 | char *opt; |
1965 | 1964 | ||
1966 | ret = 0; | 1965 | ret = 0; |
1967 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); | 1966 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); |
1968 | *output = opt; | 1967 | for (size_t i = 0; i < len; ++i) |
1969 | for (i = 0; i < len; ++i) | ||
1970 | { | 1968 | { |
1969 | char c; | ||
1970 | |||
1971 | c = (data[i] >> 2) & 0x3f; | 1971 | c = (data[i] >> 2) & 0x3f; |
1972 | opt[ret++] = cvt[(int) c]; | 1972 | opt[ret++] = cvt[(int) c]; |
1973 | c = (data[i] << 4) & 0x3f; | 1973 | c = (data[i] << 4) & 0x3f; |
@@ -1997,6 +1997,7 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
1997 | } | 1997 | } |
1998 | } | 1998 | } |
1999 | opt[ret++] = FILLCHAR; | 1999 | opt[ret++] = FILLCHAR; |
2000 | *output = opt; | ||
2000 | return ret; | 2001 | return ret; |
2001 | } | 2002 | } |
2002 | 2003 | ||
@@ -2018,11 +2019,10 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
2018 | */ | 2019 | */ |
2019 | size_t | 2020 | size_t |
2020 | GNUNET_STRINGS_base64_decode (const char *data, | 2021 | GNUNET_STRINGS_base64_decode (const char *data, |
2021 | size_t len, char **output) | 2022 | size_t len, |
2023 | void **out) | ||
2022 | { | 2024 | { |
2023 | size_t i; | 2025 | char *output; |
2024 | char c; | ||
2025 | char c1; | ||
2026 | size_t ret = 0; | 2026 | size_t ret = 0; |
2027 | 2027 | ||
2028 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ | 2028 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ |
@@ -2031,12 +2031,15 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2031 | if (i >= len) goto END; \ | 2031 | if (i >= len) goto END; \ |
2032 | } | 2032 | } |
2033 | 2033 | ||
2034 | *output = GNUNET_malloc ((len * 3 / 4) + 8); | 2034 | output = GNUNET_malloc ((len * 3 / 4) + 8); |
2035 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2035 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2036 | "base64_decode decoding len=%d\n", | 2036 | "base64_decode decoding len=%d\n", |
2037 | (int) len); | 2037 | (int) len); |
2038 | for (i = 0; i < len; ++i) | 2038 | for (size_t i = 0; i < len; ++i) |
2039 | { | 2039 | { |
2040 | char c; | ||
2041 | char c1; | ||
2042 | |||
2040 | CHECK_CRLF; | 2043 | CHECK_CRLF; |
2041 | if (FILLCHAR == data[i]) | 2044 | if (FILLCHAR == data[i]) |
2042 | break; | 2045 | break; |
@@ -2045,7 +2048,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2045 | CHECK_CRLF; | 2048 | CHECK_CRLF; |
2046 | c1 = (char) cvtfind (data[i]); | 2049 | c1 = (char) cvtfind (data[i]); |
2047 | c = (c << 2) | ((c1 >> 4) & 0x3); | 2050 | c = (c << 2) | ((c1 >> 4) & 0x3); |
2048 | (*output)[ret++] = c; | 2051 | output[ret++] = c; |
2049 | if (++i < len) | 2052 | if (++i < len) |
2050 | { | 2053 | { |
2051 | CHECK_CRLF; | 2054 | CHECK_CRLF; |
@@ -2054,7 +2057,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2054 | break; | 2057 | break; |
2055 | c = (char) cvtfind (c); | 2058 | c = (char) cvtfind (c); |
2056 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | 2059 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); |
2057 | (*output)[ret++] = c1; | 2060 | output[ret++] = c1; |
2058 | } | 2061 | } |
2059 | if (++i < len) | 2062 | if (++i < len) |
2060 | { | 2063 | { |
@@ -2065,15 +2068,13 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2065 | 2068 | ||
2066 | c1 = (char) cvtfind (c1); | 2069 | c1 = (char) cvtfind (c1); |
2067 | c = ((c << 6) & 0xc0) | c1; | 2070 | c = ((c << 6) & 0xc0) | c1; |
2068 | (*output)[ret++] = c; | 2071 | output[ret++] = c; |
2069 | } | 2072 | } |
2070 | } | 2073 | } |
2071 | END: | 2074 | END: |
2075 | *out = output; | ||
2072 | return ret; | 2076 | return ret; |
2073 | } | 2077 | } |
2074 | 2078 | ||
2075 | 2079 | ||
2076 | |||
2077 | |||
2078 | |||
2079 | /* end of strings.c */ | 2080 | /* end of strings.c */ |