From a38fa32484286d2895dca10d3f53d3c7599d2f3b Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 20 Mar 2017 02:29:33 +0100 Subject: fixing misc auditor issues --- src/util/amount.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'src/util/amount.c') diff --git a/src/util/amount.c b/src/util/amount.c index 44eefe6a6..e0664853a 100644 --- a/src/util/amount.c +++ b/src/util/amount.c @@ -529,9 +529,9 @@ char * TALER_amount_to_string (const struct TALER_Amount *amount) { char *result; + unsigned int i; uint32_t n; char tail[TALER_AMOUNT_FRAC_LEN + 1]; - unsigned int i; struct TALER_Amount norm; if (GNUNET_YES != TALER_amount_is_valid (amount)) @@ -564,6 +564,54 @@ TALER_amount_to_string (const struct TALER_Amount *amount) } +/** + * Convert amount to string. + * + * @param amount amount to convert to string + * @return statically allocated buffer with string representation, + * NULL if the @a amount was invalid + */ +const char * +TALER_amount2s (const struct TALER_Amount *amount) +{ + static char result[TALER_AMOUNT_FRAC_LEN + TALER_CURRENCY_LEN + 3 + 12]; + unsigned int i; + uint32_t n; + char tail[TALER_AMOUNT_FRAC_LEN + 1]; + struct TALER_Amount norm; + + if (GNUNET_YES != TALER_amount_is_valid (amount)) + return NULL; + norm = *amount; + GNUNET_break (GNUNET_SYSERR != + TALER_amount_normalize (&norm)); + if (0 != (n = norm.fraction)) + { + for (i = 0; (i < TALER_AMOUNT_FRAC_LEN) && (0 != n); i++) + { + tail[i] = '0' + (n / (TALER_AMOUNT_FRAC_BASE / 10)); + n = (n * 10) % (TALER_AMOUNT_FRAC_BASE); + } + tail[i] = '\0'; + 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; +} + + /** * Divide an amount by a float. Note that this function * may introduce a rounding error! -- cgit v1.2.3