From 7c6853d830336a377410fb15411a0350f184f092 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 16 Jan 2020 08:49:26 +0100 Subject: simplify amount logic --- doc/prebuilt | 2 +- src/json/json_helper.c | 107 +++++-------------------------------------------- 2 files changed, 10 insertions(+), 99 deletions(-) diff --git a/doc/prebuilt b/doc/prebuilt index 934a6a183..ca53235cc 160000 --- a/doc/prebuilt +++ b/doc/prebuilt @@ -1 +1 @@ -Subproject commit 934a6a18301e81c4fd1b3a8cda2dc13dca4741cc +Subproject commit ca53235ccfa0458ebf11c204888ca370e20ec3f5 diff --git a/src/json/json_helper.c b/src/json/json_helper.c index 0f67e9d80..536eece90 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -78,65 +78,20 @@ parse_amount (void *cls, struct GNUNET_JSON_Specification *spec) { struct TALER_Amount *r_amount = spec->ptr; - json_int_t value; - json_int_t fraction; - const char *currency; (void) cls; - if (json_is_string (root)) - { - if (GNUNET_OK != - TALER_string_to_amount (json_string_value (root), r_amount)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - return GNUNET_OK; - } - - /* Also allow the legacy { value, fraction, currency} format. - This might be removed in the future. */ - - memset (r_amount, - 0, - sizeof (struct TALER_Amount)); - if (0 != json_unpack (root, - "{s:I, s:I, s:s}", - "value", &value, - "fraction", &fraction, - "currency", ¤cy)) - { - char *json_enc; - - if (NULL == (json_enc = json_dumps (root, - JSON_COMPACT | JSON_ENCODE_ANY))) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Malformed JSON amount: %s\n", - json_enc); - free (json_enc); - return GNUNET_SYSERR; - } - if ( (value < 0) || - (fraction < 0) || - (((uint64_t) value) > UINT64_MAX) || - (fraction > UINT32_MAX) ) + if (! json_is_string (root)) { GNUNET_break_op (0); return GNUNET_SYSERR; } - if (strlen (currency) >= TALER_CURRENCY_LEN) + if (GNUNET_OK != + TALER_string_to_amount (json_string_value (root), + r_amount)) { GNUNET_break_op (0); return GNUNET_SYSERR; } - r_amount->value = (uint64_t) value; - r_amount->fraction = (uint32_t) fraction; - strcpy (r_amount->currency, currency); - (void) TALER_amount_normalize (r_amount); return GNUNET_OK; } @@ -178,64 +133,20 @@ parse_amount_nbo (void *cls, struct GNUNET_JSON_Specification *spec) { struct TALER_AmountNBO *r_amount = spec->ptr; - struct TALER_Amount amount; - json_int_t value; - json_int_t fraction; - const char *currency; (void) cls; - if (json_is_string (root)) + if (! json_is_string (root)) { - if (GNUNET_OK != - TALER_string_to_amount_nbo (json_string_value (root), r_amount)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - return GNUNET_OK; - } - - memset (&amount, - 0, - sizeof (struct TALER_Amount)); - if (0 != json_unpack (root, - "{s:I, s:I, s:s}", - "value", &value, - "fraction", &fraction, - "currency", ¤cy)) - { - char *json_enc; - - if (NULL == (json_enc = json_dumps (root, - JSON_COMPACT | JSON_ENCODE_ANY))) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Malformed JSON amount: %s\n", - json_enc); - free (json_enc); - return GNUNET_SYSERR; - } - if ( (value < 0) || - (fraction < 0) || - (fraction > (json_int_t) UINT32_MAX) ) - { - GNUNET_break_op (0); + GNUNET_break (0); return GNUNET_SYSERR; } - if (strlen (currency) >= TALER_CURRENCY_LEN) + if (GNUNET_OK != + TALER_string_to_amount_nbo (json_string_value (root), + r_amount)) { GNUNET_break_op (0); return GNUNET_SYSERR; } - amount.value = (uint64_t) value; - amount.fraction = (uint32_t) fraction; - strcpy (amount.currency, currency); - (void) TALER_amount_normalize (&amount); - TALER_amount_hton (r_amount, - &amount); return GNUNET_OK; } -- cgit v1.2.3