simplify amount logic

This commit is contained in:
Christian Grothoff 2020-01-16 08:49:26 +01:00
parent 90e1572039
commit 7c6853d830
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
2 changed files with 10 additions and 99 deletions

@ -1 +1 @@
Subproject commit 934a6a18301e81c4fd1b3a8cda2dc13dca4741cc
Subproject commit ca53235ccfa0458ebf11c204888ca370e20ec3f5

View File

@ -78,15 +78,16 @@ 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 (! json_is_string (root))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
if (GNUNET_OK !=
TALER_string_to_amount (json_string_value (root), r_amount))
TALER_string_to_amount (json_string_value (root),
r_amount))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
@ -94,52 +95,6 @@ parse_amount (void *cls,
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", &currency))
{
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) )
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
if (strlen (currency) >= TALER_CURRENCY_LEN)
{
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;
}
/**
* Provide specification to parse given JSON object to an amount.
@ -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 (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", &currency))
{
char *json_enc;
if (NULL == (json_enc = json_dumps (root,
JSON_COMPACT | JSON_ENCODE_ANY)))
if (! json_is_string (root))
{
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) )
if (GNUNET_OK !=
TALER_string_to_amount_nbo (json_string_value (root),
r_amount))
{
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;
}