2015-01-27 16:18:33 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2021-07-10 14:52:59 +02:00
|
|
|
Copyright (C) 2014-2021 Taler Systems SA
|
2015-01-27 16:18:33 +01:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2016-07-07 17:55:25 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2015-01-27 16:18:33 +01:00
|
|
|
*/
|
|
|
|
/**
|
2015-01-28 20:53:21 +01:00
|
|
|
* @file util/amount.c
|
2015-01-27 16:18:33 +01:00
|
|
|
* @brief Common utility functions to deal with units of currency
|
|
|
|
* @author Sree Harsha Totakura <sreeharsha@totakura.in>
|
|
|
|
* @author Florian Dold
|
|
|
|
* @author Benedikt Mueller
|
2015-04-13 17:20:46 +02:00
|
|
|
* @author Christian Grothoff
|
2015-01-27 16:18:33 +01:00
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "taler_util.h"
|
|
|
|
|
2017-11-02 17:40:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set @a a to "invalid".
|
|
|
|
*
|
2020-01-19 19:44:29 +01:00
|
|
|
* @param[out] a amount to set to invalid
|
2017-11-02 17:40:14 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
invalidate (struct TALER_Amount *a)
|
|
|
|
{
|
|
|
|
memset (a,
|
|
|
|
0,
|
|
|
|
sizeof (struct TALER_Amount));
|
|
|
|
}
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2015-01-27 16:18:33 +01:00
|
|
|
TALER_string_to_amount (const char *str,
|
2020-02-29 17:13:43 +01:00
|
|
|
struct TALER_Amount *amount)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
int n;
|
|
|
|
uint32_t b;
|
|
|
|
const char *colon;
|
|
|
|
const char *value;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* skip leading whitespace */
|
2019-08-25 16:18:24 +02:00
|
|
|
while (isspace ( (unsigned char) str[0]))
|
2015-03-18 18:55:41 +01:00
|
|
|
str++;
|
|
|
|
if ('\0' == str[0])
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-17 11:37:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2015-03-18 18:55:41 +01:00
|
|
|
"Null before currency\n");
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2015-01-27 16:18:33 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* parse currency */
|
|
|
|
colon = strchr (str, (int) ':');
|
|
|
|
if ( (NULL == colon) ||
|
|
|
|
((colon - str) >= TALER_CURRENCY_LEN) )
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2019-02-11 19:55:00 +01:00
|
|
|
"Invalid currency specified before colon: `%s'\n",
|
2015-03-18 18:55:41 +01:00
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
|
|
|
|
GNUNET_assert (TALER_CURRENCY_LEN > (colon - str));
|
2020-02-29 17:13:43 +01:00
|
|
|
memcpy (amount->currency,
|
2015-03-18 18:55:41 +01:00
|
|
|
str,
|
|
|
|
colon - str);
|
2020-01-19 19:39:14 +01:00
|
|
|
/* 0-terminate *and* normalize buffer by setting everything to '\0' */
|
2020-02-29 17:13:43 +01:00
|
|
|
memset (&amount->currency [colon - str],
|
2020-01-19 19:39:14 +01:00
|
|
|
0,
|
|
|
|
TALER_CURRENCY_LEN - (colon - str));
|
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* skip colon */
|
|
|
|
value = colon + 1;
|
|
|
|
if ('\0' == value[0])
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-17 11:37:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2020-01-19 19:39:14 +01:00
|
|
|
"Actual value missing in amount `%s'\n",
|
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:13:43 +01:00
|
|
|
amount->value = 0;
|
|
|
|
amount->fraction = 0;
|
2020-01-19 19:39:14 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* parse value */
|
2020-01-19 19:39:14 +01:00
|
|
|
while ('.' != *value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2020-01-19 19:39:14 +01:00
|
|
|
if ('\0' == *value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2020-01-19 19:39:14 +01:00
|
|
|
/* we are done */
|
2015-01-27 16:18:33 +01:00
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
if ( (*value < '0') ||
|
|
|
|
(*value > '9') )
|
2015-03-18 18:55:41 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2020-01-19 19:39:14 +01:00
|
|
|
"Invalid character `%c' in amount `%s'\n",
|
|
|
|
(int) *value,
|
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-03-18 18:55:41 +01:00
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
n = *value - '0';
|
2020-04-23 12:02:52 +02:00
|
|
|
if ( (amount->value * 10 < amount->value) ||
|
|
|
|
(amount->value * 10 + n < amount->value) ||
|
2021-07-10 14:52:59 +02:00
|
|
|
(amount->value > TALER_AMOUNT_MAX_VALUE) ||
|
|
|
|
(amount->value * 10 + n > TALER_AMOUNT_MAX_VALUE) )
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-17 11:37:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2020-01-19 19:39:14 +01:00
|
|
|
"Value specified in amount `%s' is too large\n",
|
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2020-02-29 17:13:43 +01:00
|
|
|
amount->value = (amount->value * 10) + n;
|
2020-01-19 19:39:14 +01:00
|
|
|
value++;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* skip the dot */
|
2020-01-19 19:39:14 +01:00
|
|
|
value++;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
/* parse fraction */
|
2020-01-19 19:39:14 +01:00
|
|
|
if ('\0' == *value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-17 11:37:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2020-01-19 19:39:14 +01:00
|
|
|
"Amount `%s' ends abruptly after `.'\n",
|
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2015-03-18 18:55:41 +01:00
|
|
|
b = TALER_AMOUNT_FRAC_BASE / 10;
|
2020-01-19 19:39:14 +01:00
|
|
|
while ('\0' != *value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
if (0 == b)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-17 11:37:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2020-01-19 19:39:14 +01:00
|
|
|
"Fractional value too small (only %u digits supported) in amount `%s'\n",
|
|
|
|
(unsigned int) TALER_AMOUNT_FRAC_LEN,
|
|
|
|
str);
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-03-18 18:55:41 +01:00
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
if ( (*value < '0') ||
|
|
|
|
(*value > '9') )
|
2015-03-18 18:55:41 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
2015-04-13 17:20:46 +02:00
|
|
|
"Error after dot\n");
|
2020-02-29 17:13:43 +01:00
|
|
|
invalidate (amount);
|
2020-01-19 19:39:14 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2020-01-19 19:39:14 +01:00
|
|
|
n = *value - '0';
|
2020-02-29 17:13:43 +01:00
|
|
|
amount->fraction += n * b;
|
2015-01-27 16:18:33 +01:00
|
|
|
b /= 10;
|
2020-01-19 19:39:14 +01:00
|
|
|
value++;
|
2017-11-02 17:40:14 +01:00
|
|
|
}
|
2015-01-27 16:18:33 +01:00
|
|
|
return GNUNET_OK;
|
2015-03-18 18:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2016-04-04 16:47:11 +02:00
|
|
|
TALER_string_to_amount_nbo (const char *str,
|
2020-01-19 22:01:20 +01:00
|
|
|
struct TALER_AmountNBO *amount_nbo)
|
2016-04-04 16:47:11 +02:00
|
|
|
{
|
|
|
|
struct TALER_Amount amount;
|
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
TALER_string_to_amount (str,
|
|
|
|
&amount))
|
|
|
|
return GNUNET_SYSERR;
|
2020-01-19 22:01:20 +01:00
|
|
|
TALER_amount_hton (amount_nbo,
|
2016-04-04 16:47:11 +02:00
|
|
|
&amount);
|
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
void
|
|
|
|
TALER_amount_hton (struct TALER_AmountNBO *res,
|
|
|
|
const struct TALER_Amount *d)
|
|
|
|
{
|
2018-01-01 23:07:36 +01:00
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_is_valid (d));
|
2015-03-18 18:55:41 +01:00
|
|
|
res->value = GNUNET_htonll (d->value);
|
|
|
|
res->fraction = htonl (d->fraction);
|
|
|
|
memcpy (res->currency,
|
|
|
|
d->currency,
|
|
|
|
TALER_CURRENCY_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TALER_amount_ntoh (struct TALER_Amount *res,
|
|
|
|
const struct TALER_AmountNBO *dn)
|
|
|
|
{
|
|
|
|
res->value = GNUNET_ntohll (dn->value);
|
|
|
|
res->fraction = ntohl (dn->fraction);
|
|
|
|
memcpy (res->currency,
|
|
|
|
dn->currency,
|
|
|
|
TALER_CURRENCY_LEN);
|
2018-01-01 23:07:36 +01:00
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_is_valid (res));
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2021-07-23 13:02:04 +02:00
|
|
|
TALER_amount_set_zero (const char *cur,
|
2020-01-19 22:01:20 +01:00
|
|
|
struct TALER_Amount *amount)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
size_t slen;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
slen = strlen (cur);
|
|
|
|
if (slen >= TALER_CURRENCY_LEN)
|
|
|
|
return GNUNET_SYSERR;
|
2020-01-19 22:01:20 +01:00
|
|
|
memset (amount,
|
2015-03-18 18:55:41 +01:00
|
|
|
0,
|
|
|
|
sizeof (struct TALER_Amount));
|
2020-01-19 22:01:20 +01:00
|
|
|
memcpy (amount->currency,
|
2015-03-18 18:55:41 +01:00
|
|
|
cur,
|
|
|
|
slen);
|
|
|
|
return GNUNET_OK;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2017-03-15 12:01:06 +01:00
|
|
|
TALER_amount_is_valid (const struct TALER_Amount *amount)
|
2015-03-18 18:55:41 +01:00
|
|
|
{
|
2021-07-10 14:52:59 +02:00
|
|
|
if (amount->value > TALER_AMOUNT_MAX_VALUE)
|
|
|
|
return GNUNET_SYSERR;
|
2020-08-29 11:55:40 +02:00
|
|
|
return ('\0' != amount->currency[0]) ? GNUNET_OK : GNUNET_NO;
|
2015-03-18 18:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-01 11:11:41 +02:00
|
|
|
bool
|
|
|
|
TALER_amount_is_zero (const struct TALER_Amount *amount)
|
|
|
|
{
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
TALER_amount_is_valid (amount))
|
|
|
|
return false;
|
|
|
|
return
|
|
|
|
(0 == amount->value) &&
|
|
|
|
(0 == amount->fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-03 15:41:09 +02:00
|
|
|
/**
|
|
|
|
* Test if @a a is valid, NBO variant.
|
|
|
|
*
|
|
|
|
* @param a amount to test
|
|
|
|
* @return #GNUNET_YES if valid,
|
|
|
|
* #GNUNET_NO if invalid
|
|
|
|
*/
|
2020-08-29 11:55:40 +02:00
|
|
|
static enum GNUNET_GenericReturnValue
|
2015-06-03 15:41:09 +02:00
|
|
|
test_valid_nbo (const struct TALER_AmountNBO *a)
|
|
|
|
{
|
2020-08-29 11:55:40 +02:00
|
|
|
return ('\0' != a->currency[0]) ? GNUNET_YES : GNUNET_NO;
|
2015-06-03 15:41:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_cmp_currency (const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2)
|
|
|
|
{
|
2017-03-15 12:01:06 +01:00
|
|
|
if ( (GNUNET_NO == TALER_amount_is_valid (a1)) ||
|
|
|
|
(GNUNET_NO == TALER_amount_is_valid (a2)) )
|
2015-03-18 18:55:41 +01:00
|
|
|
return GNUNET_SYSERR;
|
2015-04-13 17:20:46 +02:00
|
|
|
if (0 == strcasecmp (a1->currency,
|
2019-08-25 16:18:24 +02:00
|
|
|
a2->currency))
|
2015-03-18 18:55:41 +01:00
|
|
|
return GNUNET_YES;
|
|
|
|
return GNUNET_NO;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2015-06-03 15:41:09 +02:00
|
|
|
TALER_amount_cmp_currency_nbo (const struct TALER_AmountNBO *a1,
|
|
|
|
const struct TALER_AmountNBO *a2)
|
|
|
|
{
|
|
|
|
if ( (GNUNET_NO == test_valid_nbo (a1)) ||
|
|
|
|
(GNUNET_NO == test_valid_nbo (a2)) )
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
if (0 == strcasecmp (a1->currency,
|
2019-08-25 16:18:24 +02:00
|
|
|
a2->currency))
|
2015-06-03 15:41:09 +02:00
|
|
|
return GNUNET_YES;
|
|
|
|
return GNUNET_NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
int
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_cmp (const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
struct TALER_Amount n1;
|
|
|
|
struct TALER_Amount n2;
|
|
|
|
|
|
|
|
GNUNET_assert (GNUNET_YES ==
|
2020-01-19 20:11:32 +01:00
|
|
|
TALER_amount_cmp_currency (a1,
|
|
|
|
a2));
|
2015-03-18 18:55:41 +01:00
|
|
|
n1 = *a1;
|
|
|
|
n2 = *a2;
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_SYSERR !=
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&n1));
|
2015-04-13 17:20:46 +02:00
|
|
|
GNUNET_assert (GNUNET_SYSERR !=
|
2019-08-25 16:18:24 +02:00
|
|
|
TALER_amount_normalize (&n2));
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.value == n2.value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.fraction < n2.fraction)
|
2015-01-27 16:18:33 +01:00
|
|
|
return -1;
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.fraction > n2.fraction)
|
2015-01-27 16:18:33 +01:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.value < n2.value)
|
2015-01-27 16:18:33 +01:00
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 20:12:39 +02:00
|
|
|
int
|
|
|
|
TALER_amount_cmp_nbo (const struct TALER_AmountNBO *a1,
|
|
|
|
const struct TALER_AmountNBO *a2)
|
|
|
|
{
|
|
|
|
struct TALER_Amount h1;
|
|
|
|
struct TALER_Amount h2;
|
|
|
|
|
|
|
|
TALER_amount_ntoh (&h1,
|
|
|
|
a1);
|
|
|
|
TALER_amount_ntoh (&h2,
|
|
|
|
a2);
|
|
|
|
return TALER_amount_cmp (&h1,
|
|
|
|
&h2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 23:52:01 +02:00
|
|
|
enum TALER_AmountArithmeticResult
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_subtract (struct TALER_Amount *diff,
|
|
|
|
const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
struct TALER_Amount n1;
|
|
|
|
struct TALER_Amount n2;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
if (GNUNET_YES !=
|
2020-01-19 20:11:32 +01:00
|
|
|
TALER_amount_cmp_currency (a1,
|
|
|
|
a2))
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
invalidate (diff);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2020-01-19 20:11:32 +01:00
|
|
|
/* make local copies to avoid aliasing problems between
|
|
|
|
diff and a1/a2 */
|
2015-03-18 18:55:41 +01:00
|
|
|
n1 = *a1;
|
|
|
|
n2 = *a2;
|
2015-04-13 17:20:46 +02:00
|
|
|
if ( (GNUNET_SYSERR == TALER_amount_normalize (&n1)) ||
|
|
|
|
(GNUNET_SYSERR == TALER_amount_normalize (&n2)) )
|
|
|
|
{
|
|
|
|
invalidate (diff);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_NORMALIZATION_FAILED;
|
2015-04-13 17:20:46 +02:00
|
|
|
}
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.fraction < n2.fraction)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
if (0 == n1.value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
invalidate (diff);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_NEGATIVE_RESULT;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2015-03-18 18:55:41 +01:00
|
|
|
n1.fraction += TALER_AMOUNT_FRAC_BASE;
|
|
|
|
n1.value--;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2015-03-18 18:55:41 +01:00
|
|
|
if (n1.value < n2.value)
|
|
|
|
{
|
|
|
|
invalidate (diff);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_NEGATIVE_RESULT;
|
2015-03-18 18:55:41 +01:00
|
|
|
}
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2021-07-23 13:02:04 +02:00
|
|
|
TALER_amount_set_zero (n1.currency,
|
2015-03-18 18:55:41 +01:00
|
|
|
diff));
|
|
|
|
GNUNET_assert (n1.fraction >= n2.fraction);
|
|
|
|
diff->fraction = n1.fraction - n2.fraction;
|
|
|
|
GNUNET_assert (n1.value >= n2.value);
|
|
|
|
diff->value = n1.value - n2.value;
|
|
|
|
if ( (0 == diff->fraction) &&
|
|
|
|
(0 == diff->value) )
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_RESULT_ZERO;
|
|
|
|
return TALER_AAR_RESULT_POSITIVE;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 23:52:01 +02:00
|
|
|
enum TALER_AmountArithmeticResult
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_add (struct TALER_Amount *sum,
|
|
|
|
const struct TALER_Amount *a1,
|
|
|
|
const struct TALER_Amount *a2)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
struct TALER_Amount n1;
|
|
|
|
struct TALER_Amount n2;
|
2015-04-13 17:20:46 +02:00
|
|
|
struct TALER_Amount res;
|
2015-01-27 16:18:33 +01:00
|
|
|
|
2015-03-18 18:55:41 +01:00
|
|
|
if (GNUNET_YES !=
|
2021-07-10 14:52:59 +02:00
|
|
|
TALER_amount_cmp_currency (a1,
|
|
|
|
a2))
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
invalidate (sum);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2020-01-19 20:11:32 +01:00
|
|
|
/* make local copies to avoid aliasing problems between
|
|
|
|
diff and a1/a2 */
|
2015-03-18 18:55:41 +01:00
|
|
|
n1 = *a1;
|
|
|
|
n2 = *a2;
|
2021-07-10 14:52:59 +02:00
|
|
|
if ( (GNUNET_SYSERR ==
|
|
|
|
TALER_amount_normalize (&n1)) ||
|
|
|
|
(GNUNET_SYSERR ==
|
|
|
|
TALER_amount_normalize (&n2)) )
|
2015-04-13 17:20:46 +02:00
|
|
|
{
|
|
|
|
invalidate (sum);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_NORMALIZATION_FAILED;
|
2015-04-13 17:20:46 +02:00
|
|
|
}
|
2015-03-18 18:55:41 +01:00
|
|
|
|
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2021-07-23 13:02:04 +02:00
|
|
|
TALER_amount_set_zero (a1->currency,
|
2015-04-13 17:20:46 +02:00
|
|
|
&res));
|
|
|
|
res.value = n1.value + n2.value;
|
|
|
|
if (res.value < n1.value)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
/* integer overflow */
|
|
|
|
invalidate (sum);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2021-07-10 14:52:59 +02:00
|
|
|
if (res.value > TALER_AMOUNT_MAX_VALUE)
|
2017-11-02 17:40:14 +01:00
|
|
|
{
|
|
|
|
/* too large to be legal */
|
|
|
|
invalidate (sum);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
2017-11-02 17:40:14 +01:00
|
|
|
}
|
2015-04-13 17:20:46 +02:00
|
|
|
res.fraction = n1.fraction + n2.fraction;
|
2015-03-18 18:55:41 +01:00
|
|
|
if (GNUNET_SYSERR ==
|
2015-04-13 17:20:46 +02:00
|
|
|
TALER_amount_normalize (&res))
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
/* integer overflow via carry from fraction */
|
|
|
|
invalidate (sum);
|
2020-04-08 23:52:01 +02:00
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
2015-04-13 17:20:46 +02:00
|
|
|
*sum = res;
|
2020-04-08 23:52:01 +02:00
|
|
|
if ( (0 == sum->fraction) &&
|
|
|
|
(0 == sum->value) )
|
|
|
|
return TALER_AAR_RESULT_ZERO;
|
|
|
|
return TALER_AAR_RESULT_POSITIVE;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_normalize (struct TALER_Amount *amount)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2020-04-02 14:01:41 +02:00
|
|
|
uint32_t overflow;
|
2015-03-18 18:55:41 +01:00
|
|
|
|
2017-03-15 12:01:06 +01:00
|
|
|
if (GNUNET_YES != TALER_amount_is_valid (amount))
|
2015-03-18 18:55:41 +01:00
|
|
|
return GNUNET_SYSERR;
|
2020-04-02 14:01:41 +02:00
|
|
|
if (amount->fraction < TALER_AMOUNT_FRAC_BASE)
|
|
|
|
return GNUNET_NO;
|
2020-04-03 20:47:34 +02:00
|
|
|
overflow = amount->fraction / TALER_AMOUNT_FRAC_BASE;
|
2020-04-02 14:01:41 +02:00
|
|
|
amount->fraction %= TALER_AMOUNT_FRAC_BASE;
|
|
|
|
amount->value += overflow;
|
|
|
|
if ( (amount->value < overflow) ||
|
2021-07-10 14:52:59 +02:00
|
|
|
(amount->value > TALER_AMOUNT_MAX_VALUE) )
|
2015-03-18 18:55:41 +01:00
|
|
|
{
|
2015-04-13 17:20:46 +02:00
|
|
|
invalidate (amount);
|
2015-03-18 18:55:41 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-04-02 14:01:41 +02:00
|
|
|
return GNUNET_OK;
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-18 13:20:43 +02:00
|
|
|
/**
|
2020-01-18 13:23:10 +01:00
|
|
|
* Convert the fraction of @a amount to a string in decimals.
|
2019-06-18 13:20:43 +02:00
|
|
|
*
|
|
|
|
* @param amount value to convert
|
2020-03-31 20:57:11 +02:00
|
|
|
* @param[out] tail where to write the result
|
2019-06-18 13:20:43 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
amount_to_tail (const struct TALER_Amount *amount,
|
|
|
|
char tail[TALER_AMOUNT_FRAC_LEN + 1])
|
|
|
|
{
|
|
|
|
uint32_t n = amount->fraction;
|
2020-01-18 14:01:56 +01:00
|
|
|
unsigned int i;
|
2019-06-18 13:20:43 +02:00
|
|
|
|
2020-01-18 14:01:56 +01:00
|
|
|
for (i = 0; (i < TALER_AMOUNT_FRAC_LEN) && (0 != n); i++)
|
2019-06-18 13:20:43 +02:00
|
|
|
{
|
|
|
|
tail[i] = '0' + (n / (TALER_AMOUNT_FRAC_BASE / 10));
|
|
|
|
n = (n * 10) % (TALER_AMOUNT_FRAC_BASE);
|
|
|
|
}
|
|
|
|
tail[i] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
char *
|
2015-03-18 18:55:41 +01:00
|
|
|
TALER_amount_to_string (const struct TALER_Amount *amount)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
char *result;
|
|
|
|
struct TALER_Amount norm;
|
|
|
|
|
2017-03-15 12:01:06 +01:00
|
|
|
if (GNUNET_YES != TALER_amount_is_valid (amount))
|
2015-03-18 18:55:41 +01:00
|
|
|
return NULL;
|
|
|
|
norm = *amount;
|
|
|
|
GNUNET_break (GNUNET_SYSERR !=
|
|
|
|
TALER_amount_normalize (&norm));
|
2019-06-18 13:20:43 +02:00
|
|
|
if (0 != norm.fraction)
|
2015-01-27 16:18:33 +01:00
|
|
|
{
|
2019-06-18 13:20:43 +02:00
|
|
|
char tail[TALER_AMOUNT_FRAC_LEN + 1];
|
|
|
|
|
|
|
|
amount_to_tail (&norm,
|
|
|
|
tail);
|
2015-03-18 18:55:41 +01:00
|
|
|
GNUNET_asprintf (&result,
|
|
|
|
"%s:%llu.%s",
|
|
|
|
norm.currency,
|
|
|
|
(unsigned long long) norm.value,
|
|
|
|
tail);
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-18 18:55:41 +01:00
|
|
|
GNUNET_asprintf (&result,
|
|
|
|
"%s:%llu",
|
|
|
|
norm.currency,
|
|
|
|
(unsigned long long) norm.value);
|
2015-01-27 16:18:33 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-20 02:29:33 +01:00
|
|
|
const char *
|
|
|
|
TALER_amount2s (const struct TALER_Amount *amount)
|
|
|
|
{
|
2020-04-02 13:47:21 +02:00
|
|
|
/* 24 is sufficient for a uint64_t value in decimal; 3 is for ":.\0" */
|
2020-04-02 11:34:52 +02:00
|
|
|
static GNUNET_THREAD_LOCAL char result[TALER_AMOUNT_FRAC_LEN
|
2020-04-02 13:47:21 +02:00
|
|
|
+ TALER_CURRENCY_LEN + 3 + 24];
|
2017-03-20 02:29:33 +01:00
|
|
|
struct TALER_Amount norm;
|
|
|
|
|
|
|
|
if (GNUNET_YES != TALER_amount_is_valid (amount))
|
|
|
|
return NULL;
|
|
|
|
norm = *amount;
|
|
|
|
GNUNET_break (GNUNET_SYSERR !=
|
|
|
|
TALER_amount_normalize (&norm));
|
2019-06-18 13:20:43 +02:00
|
|
|
if (0 != norm.fraction)
|
2017-03-20 02:29:33 +01:00
|
|
|
{
|
2019-06-18 13:20:43 +02:00
|
|
|
char tail[TALER_AMOUNT_FRAC_LEN + 1];
|
|
|
|
|
|
|
|
amount_to_tail (&norm,
|
|
|
|
tail);
|
2017-03-20 02:29:33 +01:00
|
|
|
GNUNET_snprintf (result,
|
|
|
|
sizeof (result),
|
|
|
|
"%s:%llu.%s",
|
|
|
|
norm.currency,
|
|
|
|
(unsigned long long) norm.value,
|
|
|
|
tail);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GNUNET_snprintf (result,
|
|
|
|
sizeof (result),
|
|
|
|
"%s:%llu",
|
|
|
|
norm.currency,
|
|
|
|
(unsigned long long) norm.value);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-06 16:35:30 +01:00
|
|
|
void
|
|
|
|
TALER_amount_divide (struct TALER_Amount *result,
|
|
|
|
const struct TALER_Amount *dividend,
|
|
|
|
uint32_t divisor)
|
|
|
|
{
|
|
|
|
uint64_t modr;
|
|
|
|
|
2020-01-19 20:11:32 +01:00
|
|
|
GNUNET_assert (0 != divisor); /* division by zero is discouraged */
|
2017-03-06 16:35:30 +01:00
|
|
|
*result = *dividend;
|
2020-01-19 20:11:32 +01:00
|
|
|
/* in case @a dividend was not yet normalized */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR !=
|
|
|
|
TALER_amount_normalize (result));
|
2017-03-06 16:35:30 +01:00
|
|
|
if (1 == divisor)
|
|
|
|
return;
|
|
|
|
modr = result->value % divisor;
|
|
|
|
result->value /= divisor;
|
2020-01-19 20:11:32 +01:00
|
|
|
/* modr fits into 32 bits, so we can safely multiply by (<32-bit) base and add fraction! */
|
2017-03-06 16:35:30 +01:00
|
|
|
modr = (modr * TALER_AMOUNT_FRAC_BASE) + result->fraction;
|
|
|
|
result->fraction = (uint32_t) (modr / divisor);
|
2020-01-19 20:11:32 +01:00
|
|
|
/* 'fraction' could now be larger than #TALER_AMOUNT_FRAC_BASE, so we must normalize */
|
2020-01-20 01:40:29 +01:00
|
|
|
GNUNET_assert (GNUNET_SYSERR !=
|
|
|
|
TALER_amount_normalize (result));
|
2017-03-06 16:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-10 14:52:59 +02:00
|
|
|
int
|
|
|
|
TALER_amount_divide2 (const struct TALER_Amount *dividend,
|
|
|
|
const struct TALER_Amount *divisor)
|
|
|
|
{
|
|
|
|
double approx;
|
|
|
|
double d;
|
|
|
|
double r;
|
|
|
|
int ret;
|
|
|
|
struct TALER_Amount tmp;
|
|
|
|
struct TALER_Amount nxt;
|
|
|
|
|
|
|
|
if (GNUNET_YES !=
|
|
|
|
TALER_amount_cmp_currency (dividend,
|
|
|
|
divisor))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( (0 == divisor->fraction) &&
|
|
|
|
(0 == divisor->value) )
|
|
|
|
return INT_MAX;
|
|
|
|
/* first, get rounded approximation */
|
|
|
|
d = ((double) dividend->value) * ((double) TALER_AMOUNT_FRAC_BASE)
|
|
|
|
+ ( (double) dividend->fraction);
|
|
|
|
r = ((double) divisor->value) * ((double) TALER_AMOUNT_FRAC_BASE)
|
|
|
|
+ ( (double) divisor->fraction);
|
|
|
|
approx = d / r;
|
|
|
|
if (approx > ((double) INT_MAX))
|
|
|
|
return INT_MAX; /* 'infinity' */
|
|
|
|
/* round down */
|
|
|
|
if (approx < 2)
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = (int) approx - 2;
|
|
|
|
/* Now do *exact* calculation, using well rounded-down factor as starting
|
|
|
|
point to avoid having to do too many steps. */
|
|
|
|
GNUNET_assert (0 <=
|
|
|
|
TALER_amount_multiply (&tmp,
|
|
|
|
divisor,
|
|
|
|
ret));
|
|
|
|
/* in practice, this loop will only run for one or two iterations */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
GNUNET_assert (0 <=
|
|
|
|
TALER_amount_add (&nxt,
|
|
|
|
&tmp,
|
|
|
|
divisor));
|
|
|
|
if (1 ==
|
|
|
|
TALER_amount_cmp (&nxt,
|
|
|
|
dividend))
|
|
|
|
break; /* nxt > dividend */
|
|
|
|
ret++;
|
|
|
|
tmp = nxt;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum TALER_AmountArithmeticResult
|
|
|
|
TALER_amount_multiply (struct TALER_Amount *result,
|
|
|
|
const struct TALER_Amount *amount,
|
|
|
|
uint32_t factor)
|
|
|
|
{
|
|
|
|
struct TALER_Amount in = *amount;
|
|
|
|
|
|
|
|
if (GNUNET_SYSERR ==
|
|
|
|
TALER_amount_normalize (&in))
|
|
|
|
return TALER_AAR_INVALID_NORMALIZATION_FAILED;
|
|
|
|
memcpy (result->currency,
|
|
|
|
amount->currency,
|
|
|
|
TALER_CURRENCY_LEN);
|
|
|
|
if ( (0 == factor) ||
|
|
|
|
( (0 == in.value) &&
|
|
|
|
(0 == in.fraction) ) )
|
|
|
|
{
|
|
|
|
result->value = 0;
|
|
|
|
result->fraction = 0;
|
|
|
|
return TALER_AAR_RESULT_ZERO;
|
|
|
|
}
|
|
|
|
result->value = in.value * ((uint64_t) factor);
|
|
|
|
if (in.value != result->value / factor)
|
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
|
|
|
{
|
|
|
|
/* This multiplication cannot overflow since both inputs are 32-bit values */
|
|
|
|
uint64_t tmp = ((uint64_t) factor) * ((uint64_t) in.fraction);
|
|
|
|
uint64_t res;
|
|
|
|
|
|
|
|
res = tmp / TALER_AMOUNT_FRAC_BASE;
|
|
|
|
/* check for overflow */
|
|
|
|
if (result->value + res < result->value)
|
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
|
|
|
result->value += res;
|
|
|
|
result->fraction = tmp % TALER_AMOUNT_FRAC_BASE;
|
|
|
|
}
|
|
|
|
if (result->value > TALER_AMOUNT_MAX_VALUE)
|
|
|
|
return TALER_AAR_INVALID_RESULT_OVERFLOW;
|
|
|
|
/* This check should be redundant... */
|
|
|
|
GNUNET_assert (GNUNET_SYSERR !=
|
|
|
|
TALER_amount_normalize (result));
|
|
|
|
return TALER_AAR_RESULT_POSITIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-29 11:55:40 +02:00
|
|
|
enum GNUNET_GenericReturnValue
|
2020-01-15 15:17:02 +01:00
|
|
|
TALER_amount_round_down (struct TALER_Amount *amount,
|
2020-01-17 18:59:15 +01:00
|
|
|
const struct TALER_Amount *round_unit)
|
2020-01-13 18:16:01 +01:00
|
|
|
{
|
2020-03-15 20:42:47 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
TALER_amount_cmp_currency (amount,
|
|
|
|
round_unit))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-01-19 20:11:32 +01:00
|
|
|
if ( (0 != round_unit->fraction) &&
|
|
|
|
(0 != round_unit->value) )
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
if ( (0 == round_unit->fraction) &&
|
|
|
|
(0 == round_unit->value) )
|
|
|
|
return GNUNET_NO; /* no rounding requested */
|
|
|
|
if (0 != round_unit->fraction)
|
|
|
|
{
|
|
|
|
uint32_t delta;
|
2020-01-15 15:17:02 +01:00
|
|
|
|
2020-01-19 20:11:32 +01:00
|
|
|
delta = amount->fraction % round_unit->fraction;
|
|
|
|
if (0 == delta)
|
|
|
|
return GNUNET_NO;
|
|
|
|
amount->fraction -= delta;
|
|
|
|
}
|
|
|
|
if (0 != round_unit->value)
|
|
|
|
{
|
|
|
|
uint64_t delta;
|
2020-01-17 18:59:15 +01:00
|
|
|
|
2020-01-19 20:11:32 +01:00
|
|
|
delta = amount->value % round_unit->value;
|
|
|
|
if (0 == delta)
|
|
|
|
return GNUNET_NO;
|
|
|
|
amount->value -= delta;
|
|
|
|
amount->fraction = 0;
|
|
|
|
}
|
2020-01-13 18:16:01 +01:00
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 16:18:33 +01:00
|
|
|
/* end of amount.c */
|