From 5e8ef386803c399c1b6d780181b0b8662c418db0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 18 Apr 2017 21:05:27 +0200 Subject: fixing #4980 --- src/json/json_helper.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) (limited to 'src/json/json_helper.c') diff --git a/src/json/json_helper.c b/src/json/json_helper.c index f964f8f17..1d2a33156 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c @@ -119,7 +119,6 @@ parse_amount (void *cls, } - /** * Provide specification to parse given JSON object to an amount. * @@ -143,6 +142,94 @@ TALER_JSON_spec_amount (const char *name, } +/** + * Parse given JSON object to Amount in NBO. + * + * @param cls closure, NULL + * @param root the json object representing data + * @param[out] spec where to write the data + * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error + */ +static int +parse_amount_nbo (void *cls, + json_t *root, + 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; + + 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) || + (value > UINT64_MAX) || + (fraction > UINT32_MAX) ) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + if (strlen (currency) >= TALER_CURRENCY_LEN) + { + 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; +} + + +/** + * Provide specification to parse given JSON object to an amount. + * + * @param name name of the amount field in the JSON + * @param[out] r_amount where the amount has to be written + */ +struct GNUNET_JSON_Specification +TALER_JSON_spec_amount_nbo (const char *name, + struct TALER_AmountNBO *r_amount) +{ + struct GNUNET_JSON_Specification ret = { + .parser = &parse_amount_nbo, + .cleaner = NULL, + .cls = NULL, + .field = name, + .ptr = r_amount, + .ptr_size = 0, + .size_ptr = NULL + }; + return ret; +} + + /** * Generate line in parser specification for denomination public key. * -- cgit v1.2.3