LCOV - code coverage report
Current view: top level - util - amount.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 181 218 83.0 %
Date: 2017-09-17 17:24:28 Functions: 16 17 94.1 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file util/amount.c
      18             :  * @brief Common utility functions to deal with units of currency
      19             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      20             :  * @author Florian Dold
      21             :  * @author Benedikt Mueller
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #if HAVE_GNUNET_GNUNET_UTIL_LIB_H
      26             : #include "taler_util.h"
      27             : #elif HAVE_GNUNET_GNUNET_UTIL_TALER_WALLET_LIB_H
      28             : #include "taler_util_wallet.h"
      29             : #endif
      30             : #include <gcrypt.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Parse money amount description, in the format "A:B.C".
      35             :  *
      36             :  * @param str amount description
      37             :  * @param denom amount to write the result to
      38             :  * @return #GNUNET_OK if the string is a valid amount specification,
      39             :  *         #GNUNET_SYSERR if it is invalid.
      40             :  */
      41             : int
      42         404 : TALER_string_to_amount (const char *str,
      43             :                         struct TALER_Amount *denom)
      44             : {
      45             :   size_t i;
      46             :   int n;
      47             :   uint32_t b;
      48             :   const char *colon;
      49             :   const char *value;
      50             : 
      51         404 :   memset (denom,
      52             :           0,
      53             :           sizeof (struct TALER_Amount));
      54             :   /* skip leading whitespace */
      55         810 :   while (isspace(str[0]))
      56           2 :     str++;
      57         404 :   if ('\0' == str[0])
      58             :   {
      59           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      60             :                 "Null before currency\n");
      61           0 :     return GNUNET_SYSERR;
      62             :   }
      63             :   /* parse currency */
      64         404 :   colon = strchr (str, (int) ':');
      65         808 :   if ( (NULL == colon) ||
      66         404 :        ((colon - str) >= TALER_CURRENCY_LEN) )
      67             :   {
      68           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      69             :                 "Invalid currency specified before colon: `%s'",
      70             :                 str);
      71           0 :     goto fail;
      72             :   }
      73         404 :   memcpy (denom->currency,
      74             :           str,
      75         404 :           colon - str);
      76             :   /* skip colon */
      77         404 :   value = colon + 1;
      78         404 :   if ('\0' == value[0])
      79             :   {
      80           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      81             :                 "Null before value\n");
      82           0 :     goto fail;
      83             :   }
      84             : 
      85             :   /* parse value */
      86         404 :   i = 0;
      87        1263 :   while ('.' != value[i])
      88             :   {
      89         511 :     if ('\0' == value[i])
      90             :     {
      91          54 :       return GNUNET_OK;
      92             :     }
      93         457 :     if ( (value[i] < '0') || (value[i] > '9') )
      94             :     {
      95           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      96             :                   "Invalid character `%c'\n",
      97             :                   value[i]);
      98           1 :       goto fail;
      99             :     }
     100         456 :     n = value[i] - '0';
     101         456 :     if (denom->value * 10 + n < denom->value)
     102             :     {
     103           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     104             :                   "Value too large\n");
     105           1 :       goto fail;
     106             :     }
     107         455 :     denom->value = (denom->value * 10) + n;
     108         455 :     i++;
     109             :   }
     110             : 
     111             :   /* skip the dot */
     112         348 :   i++;
     113             : 
     114             :   /* parse fraction */
     115         348 :   if ('\0' == value[i])
     116             :   {
     117           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     118             :                 "Null after dot\n");
     119           0 :     goto fail;
     120             :   }
     121         348 :   b = TALER_AMOUNT_FRAC_BASE / 10;
     122        1436 :   while ('\0' != value[i])
     123             :   {
     124         744 :     if (0 == b)
     125             :     {
     126           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     127             :                   "Fractional value too small (only %u digits supported)\n",
     128             :                   (unsigned int) TALER_AMOUNT_FRAC_LEN);
     129           1 :       goto fail;
     130             :     }
     131         743 :     if ( (value[i] < '0') || (value[i] > '9') )
     132             :     {
     133           3 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     134             :                   "Error after dot\n");
     135           3 :       goto fail;
     136             :     }
     137         740 :     n = value[i] - '0';
     138         740 :     denom->fraction += n * b;
     139         740 :     b /= 10;
     140         740 :     i++;
     141             :   }
     142         344 :   return GNUNET_OK;
     143             : 
     144             :  fail:
     145             :   /* set currency to 'invalid' to prevent accidental use */
     146           6 :   memset (denom->currency,
     147             :           0,
     148             :           TALER_CURRENCY_LEN);
     149           6 :   return GNUNET_SYSERR;
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Parse denomination description, in the format "T:V.F".
     155             :  *
     156             :  * @param str denomination description
     157             :  * @param denom denomination to write the result to, in NBO
     158             :  * @return #GNUNET_OK if the string is a valid denomination specification,
     159             :  *         #GNUNET_SYSERR if it is invalid.
     160             :  */
     161             : int
     162           5 : TALER_string_to_amount_nbo (const char *str,
     163             :                             struct TALER_AmountNBO *denom)
     164             : {
     165             :   struct TALER_Amount amount;
     166             : 
     167           5 :   if (GNUNET_OK !=
     168           5 :       TALER_string_to_amount (str,
     169             :                               &amount))
     170           0 :     return GNUNET_SYSERR;
     171           5 :   TALER_amount_hton (denom,
     172             :                      &amount);
     173           5 :   return GNUNET_OK;
     174             : }
     175             : 
     176             : 
     177             : /**
     178             :  * Convert amount from host to network representation.
     179             :  *
     180             :  * @param res where to store amount in network representation
     181             :  * @param d amount in host representation
     182             :  */
     183             : void
     184        2012 : TALER_amount_hton (struct TALER_AmountNBO *res,
     185             :                    const struct TALER_Amount *d)
     186             : {
     187        2012 :   res->value = GNUNET_htonll (d->value);
     188        2012 :   res->fraction = htonl (d->fraction);
     189        2012 :   memcpy (res->currency,
     190        2012 :           d->currency,
     191             :           TALER_CURRENCY_LEN);
     192        2012 : }
     193             : 
     194             : 
     195             : /**
     196             :  * Convert amount from network to host representation.
     197             :  *
     198             :  * @param res where to store amount in host representation
     199             :  * @param dn amount in network representation
     200             :  */
     201             : void
     202         738 : TALER_amount_ntoh (struct TALER_Amount *res,
     203             :                    const struct TALER_AmountNBO *dn)
     204             : {
     205         738 :   res->value = GNUNET_ntohll (dn->value);
     206         738 :   res->fraction = ntohl (dn->fraction);
     207         738 :   memcpy (res->currency,
     208         738 :           dn->currency,
     209             :           TALER_CURRENCY_LEN);
     210         738 : }
     211             : 
     212             : 
     213             : /**
     214             :  * Get the value of "zero" in a particular currency.
     215             :  *
     216             :  * @param cur currency description
     217             :  * @param denom denomination to write the result to
     218             :  * @return #GNUNET_OK if @a cur is a valid currency specification,
     219             :  *         #GNUNET_SYSERR if it is invalid.
     220             :  */
     221             : int
     222         395 : TALER_amount_get_zero (const char *cur,
     223             :                        struct TALER_Amount *denom)
     224             : {
     225             :   size_t slen;
     226             : 
     227         395 :   slen = strlen (cur);
     228         395 :   if (slen >= TALER_CURRENCY_LEN)
     229           0 :     return GNUNET_SYSERR;
     230         395 :   memset (denom,
     231             :           0,
     232             :           sizeof (struct TALER_Amount));
     233         395 :   memcpy (denom->currency,
     234             :           cur,
     235             :           slen);
     236         395 :   return GNUNET_OK;
     237             : }
     238             : 
     239             : 
     240             : /**
     241             :  * Set @a a to "invalid".
     242             :  *
     243             :  * @param a amount to set to invalid
     244             :  */
     245             : static void
     246           9 : invalidate (struct TALER_Amount *a)
     247             : {
     248           9 :   memset (a,
     249             :           0,
     250             :           sizeof (struct TALER_Amount));
     251           9 : }
     252             : 
     253             : 
     254             : /**
     255             :  * Test if the given amount is valid.
     256             :  *
     257             :  * @param amount amount to check
     258             :  * @return #GNUNET_OK if @a amount is valid
     259             :  */
     260             : int
     261        3450 : TALER_amount_is_valid (const struct TALER_Amount *amount)
     262             : {
     263        3450 :   return ('\0' != amount->currency[0]);
     264             : }
     265             : 
     266             : 
     267             : /**
     268             :  * Test if @a a is valid, NBO variant.
     269             :  *
     270             :  * @param a amount to test
     271             :  * @return #GNUNET_YES if valid,
     272             :  *         #GNUNET_NO if invalid
     273             :  */
     274             : static int
     275         304 : test_valid_nbo (const struct TALER_AmountNBO *a)
     276             : {
     277         304 :   return ('\0' != a->currency[0]);
     278             : }
     279             : 
     280             : 
     281             : /**
     282             :  * Test if @a a1 and @a a2 are the same currency.
     283             :  *
     284             :  * @param a1 amount to test
     285             :  * @param a2 amount to test
     286             :  * @return #GNUNET_YES if @a a1 and @a a2 are the same currency
     287             :  *         #GNUNET_NO if the currencies are different,
     288             :  *         #GNUNET_SYSERR if either amount is invalid
     289             :  */
     290             : int
     291         677 : TALER_amount_cmp_currency (const struct TALER_Amount *a1,
     292             :                            const struct TALER_Amount *a2)
     293             : {
     294        1354 :   if ( (GNUNET_NO == TALER_amount_is_valid (a1)) ||
     295         677 :        (GNUNET_NO == TALER_amount_is_valid (a2)) )
     296           0 :     return GNUNET_SYSERR;
     297         677 :   if (0 == strcasecmp (a1->currency,
     298         677 :                        a2->currency))
     299         675 :     return GNUNET_YES;
     300           2 :   return GNUNET_NO;
     301             : }
     302             : 
     303             : 
     304             : /**
     305             :  * Test if @a a1 and @a a2 are the same currency, NBO variant.
     306             :  *
     307             :  * @param a1 amount to test
     308             :  * @param a2 amount to test
     309             :  * @return #GNUNET_YES if @a a1 and @a a2 are the same currency
     310             :  *         #GNUNET_NO if the currencies are different,
     311             :  *         #GNUNET_SYSERR if either amount is invalid
     312             :  */
     313             : int
     314         152 : TALER_amount_cmp_currency_nbo (const struct TALER_AmountNBO *a1,
     315             :                                const struct TALER_AmountNBO *a2)
     316             : {
     317         304 :   if ( (GNUNET_NO == test_valid_nbo (a1)) ||
     318         152 :        (GNUNET_NO == test_valid_nbo (a2)) )
     319           0 :     return GNUNET_SYSERR;
     320         152 :   if (0 == strcasecmp (a1->currency,
     321         152 :                        a2->currency))
     322         152 :     return GNUNET_YES;
     323           0 :   return GNUNET_NO;
     324             : }
     325             : 
     326             : 
     327             : /**
     328             :  * Compare the value/fraction of two amounts.  Does not compare the currency.
     329             :  * Comparing amounts of different currencies will cause the program to abort().
     330             :  * If unsure, check with #TALER_amount_cmp_currency() first to be sure that
     331             :  * the currencies of the two amounts are identical.
     332             :  *
     333             :  * @param a1 first amount
     334             :  * @param a2 second amount
     335             :  * @return result of the comparison,
     336             :  *         -1 if `a1 < a2`
     337             :  *          1 if `a1 > a2`
     338             :  *          0 if `a1 == a2`.
     339             :  */
     340             : int
     341         306 : TALER_amount_cmp (const struct TALER_Amount *a1,
     342             :                   const struct TALER_Amount *a2)
     343             : {
     344             :   struct TALER_Amount n1;
     345             :   struct TALER_Amount n2;
     346             : 
     347         306 :   GNUNET_assert (GNUNET_YES ==
     348             :                  TALER_amount_cmp_currency (a1, a2));
     349         306 :   n1 = *a1;
     350         306 :   n2 = *a2;
     351         306 :   GNUNET_assert (GNUNET_SYSERR !=
     352             :                  TALER_amount_normalize (&n1));
     353         306 :   GNUNET_assert (GNUNET_SYSERR !=
     354             :                  TALER_amount_normalize (&n2));
     355         306 :   if (n1.value == n2.value)
     356             :   {
     357         195 :     if (n1.fraction < n2.fraction)
     358          18 :       return -1;
     359         177 :     if (n1.fraction > n2.fraction)
     360           0 :       return 1;
     361         177 :     return 0;
     362             :   }
     363         111 :   if (n1.value < n2.value)
     364          67 :     return -1;
     365          44 :   return 1;
     366             : }
     367             : 
     368             : 
     369             : /**
     370             :  * Perform saturating subtraction of amounts.
     371             :  *
     372             :  * @param diff where to store (@a a1 - @a a2), or invalid if @a a2 > @a a1
     373             :  * @param a1 amount to subtract from
     374             :  * @param a2 amount to subtract
     375             :  * @return #GNUNET_OK if the subtraction worked,
     376             :  *         #GNUNET_NO if @a a1 = @a a2
     377             :  *         #GNUNET_SYSERR if @a a2 > @a a1 or currencies are incompatible;
     378             :  *                        @a diff is set to invalid
     379             :  */
     380             : int
     381         144 : TALER_amount_subtract (struct TALER_Amount *diff,
     382             :                        const struct TALER_Amount *a1,
     383             :                        const struct TALER_Amount *a2)
     384             : {
     385             :   struct TALER_Amount n1;
     386             :   struct TALER_Amount n2;
     387             : 
     388         144 :   if (GNUNET_YES !=
     389         144 :       TALER_amount_cmp_currency (a1, a2))
     390             :   {
     391           1 :     invalidate (diff);
     392           1 :     return GNUNET_SYSERR;
     393             :   }
     394         143 :   n1 = *a1;
     395         143 :   n2 = *a2;
     396         286 :   if ( (GNUNET_SYSERR == TALER_amount_normalize (&n1)) ||
     397         143 :        (GNUNET_SYSERR == TALER_amount_normalize (&n2)) )
     398             :   {
     399           0 :     invalidate (diff);
     400           0 :     return GNUNET_SYSERR;
     401             :   }
     402             : 
     403         143 :   if (n1.fraction < n2.fraction)
     404             :   {
     405          38 :     if (0 == n1.value)
     406             :     {
     407           4 :       invalidate (diff);
     408           4 :       return GNUNET_SYSERR;
     409             :     }
     410          34 :     n1.fraction += TALER_AMOUNT_FRAC_BASE;
     411          34 :     n1.value--;
     412             :   }
     413         139 :   if (n1.value < n2.value)
     414             :   {
     415           1 :     invalidate (diff);
     416           1 :     return GNUNET_SYSERR;
     417             :   }
     418         138 :   GNUNET_assert (GNUNET_OK ==
     419             :                  TALER_amount_get_zero (n1.currency,
     420             :                                         diff));
     421         138 :   GNUNET_assert (n1.fraction >= n2.fraction);
     422         138 :   diff->fraction = n1.fraction - n2.fraction;
     423         138 :   GNUNET_assert (n1.value >= n2.value);
     424         138 :   diff->value = n1.value - n2.value;
     425         173 :   if ( (0 == diff->fraction) &&
     426          35 :        (0 == diff->value) )
     427          14 :     return GNUNET_NO;
     428         124 :   return GNUNET_OK;
     429             : }
     430             : 
     431             : 
     432             : /**
     433             :  * Perform addition of amounts.
     434             :  *
     435             :  * @param sum where to store @a a1 + @a a2, set to "invalid" on overflow
     436             :  * @param a1 first amount to add
     437             :  * @param a2 second amount to add
     438             :  * @return #GNUNET_OK if the addition worked,
     439             :  *         #GNUNET_SYSERR on overflow
     440             :  */
     441             : int
     442         211 : TALER_amount_add (struct TALER_Amount *sum,
     443             :                   const struct TALER_Amount *a1,
     444             :                   const struct TALER_Amount *a2)
     445             : {
     446             :   struct TALER_Amount n1;
     447             :   struct TALER_Amount n2;
     448             :   struct TALER_Amount res;
     449             : 
     450         211 :   if (GNUNET_YES !=
     451         211 :       TALER_amount_cmp_currency (a1, a2))
     452             :   {
     453           0 :     invalidate (sum);
     454           0 :     return GNUNET_SYSERR;
     455             :   }
     456         211 :   n1 = *a1;
     457         211 :   n2 = *a2;
     458         422 :   if ( (GNUNET_SYSERR == TALER_amount_normalize (&n1)) ||
     459         211 :        (GNUNET_SYSERR == TALER_amount_normalize (&n2)) )
     460             :   {
     461           0 :     invalidate (sum);
     462           0 :     return GNUNET_SYSERR;
     463             :   }
     464             : 
     465         211 :   GNUNET_assert (GNUNET_OK ==
     466             :                  TALER_amount_get_zero (a1->currency,
     467             :                                         &res));
     468         211 :   res.value = n1.value + n2.value;
     469         211 :   if (res.value < n1.value)
     470             :   {
     471             :     /* integer overflow */
     472           0 :     invalidate (sum);
     473           0 :     return GNUNET_SYSERR;
     474             :   }
     475         211 :   res.fraction = n1.fraction + n2.fraction;
     476         211 :   if (GNUNET_SYSERR ==
     477         211 :       TALER_amount_normalize (&res))
     478             :   {
     479             :     /* integer overflow via carry from fraction */
     480           1 :     invalidate (sum);
     481           1 :     return GNUNET_SYSERR;
     482             :   }
     483         210 :   *sum = res;
     484         210 :   return GNUNET_OK;
     485             : }
     486             : 
     487             : 
     488             : /**
     489             :  * Normalize the given amount.
     490             :  *
     491             :  * @param amount amount to normalize
     492             :  * @return #GNUNET_OK if normalization worked
     493             :  *         #GNUNET_NO if value was already normalized
     494             :  *         #GNUNET_SYSERR if value was invalid or could not be normalized
     495             :  */
     496             : int
     497        1919 : TALER_amount_normalize (struct TALER_Amount *amount)
     498             : {
     499             :   int ret;
     500             : 
     501        1919 :   if (GNUNET_YES != TALER_amount_is_valid (amount))
     502           2 :     return GNUNET_SYSERR;
     503        1917 :   ret = GNUNET_NO;
     504        5764 :   while ( (amount->value != UINT64_MAX) &&
     505        1922 :           (amount->fraction >= TALER_AMOUNT_FRAC_BASE) )
     506             :   {
     507           8 :     amount->fraction -= TALER_AMOUNT_FRAC_BASE;
     508           8 :     amount->value++;
     509           8 :     ret = GNUNET_OK;
     510             :   }
     511        1917 :   if (amount->fraction >= TALER_AMOUNT_FRAC_BASE)
     512             :   {
     513             :     /* failed to normalize, adding up fractions caused
     514             :        main value to overflow! */
     515           2 :     invalidate (amount);
     516           2 :     return GNUNET_SYSERR;
     517             :   }
     518        1915 :   return ret;
     519             : }
     520             : 
     521             : 
     522             : /**
     523             :  * Convert amount to string.
     524             :  *
     525             :  * @param amount amount to convert to string
     526             :  * @return freshly allocated string representation
     527             :  */
     528             : char *
     529         177 : TALER_amount_to_string (const struct TALER_Amount *amount)
     530             : {
     531             :   char *result;
     532             :   unsigned int i;
     533             :   uint32_t n;
     534             :   char tail[TALER_AMOUNT_FRAC_LEN + 1];
     535             :   struct TALER_Amount norm;
     536             : 
     537         177 :   if (GNUNET_YES != TALER_amount_is_valid (amount))
     538           1 :     return NULL;
     539         176 :   norm = *amount;
     540         176 :   GNUNET_break (GNUNET_SYSERR !=
     541             :                 TALER_amount_normalize (&norm));
     542         176 :   if (0 != (n = norm.fraction))
     543             :   {
     544         321 :     for (i = 0; (i < TALER_AMOUNT_FRAC_LEN) && (0 != n); i++)
     545             :     {
     546         202 :       tail[i] = '0' + (n / (TALER_AMOUNT_FRAC_BASE / 10));
     547         202 :       n = (n * 10) % (TALER_AMOUNT_FRAC_BASE);
     548             :     }
     549         119 :     tail[i] = '\0';
     550         119 :     GNUNET_asprintf (&result,
     551             :                      "%s:%llu.%s",
     552             :                      norm.currency,
     553         119 :                      (unsigned long long) norm.value,
     554             :                      tail);
     555             :   }
     556             :   else
     557             :   {
     558          57 :     GNUNET_asprintf (&result,
     559             :                      "%s:%llu",
     560             :                      norm.currency,
     561          57 :                      (unsigned long long) norm.value);
     562             :   }
     563         176 :   return result;
     564             : }
     565             : 
     566             : 
     567             : /**
     568             :  * Convert amount to string.
     569             :  *
     570             :  * @param amount amount to convert to string
     571             :  * @return statically allocated buffer with string representation,
     572             :  *         NULL if the @a amount was invalid
     573             :  */
     574             : const char *
     575           0 : TALER_amount2s (const struct TALER_Amount *amount)
     576             : {
     577             :   static char result[TALER_AMOUNT_FRAC_LEN + TALER_CURRENCY_LEN + 3 + 12];
     578             :   unsigned int i;
     579             :   uint32_t n;
     580             :   char tail[TALER_AMOUNT_FRAC_LEN + 1];
     581             :   struct TALER_Amount norm;
     582             : 
     583           0 :   if (GNUNET_YES != TALER_amount_is_valid (amount))
     584           0 :     return NULL;
     585           0 :   norm = *amount;
     586           0 :   GNUNET_break (GNUNET_SYSERR !=
     587             :                 TALER_amount_normalize (&norm));
     588           0 :   if (0 != (n = norm.fraction))
     589             :   {
     590           0 :     for (i = 0; (i < TALER_AMOUNT_FRAC_LEN) && (0 != n); i++)
     591             :     {
     592           0 :       tail[i] = '0' + (n / (TALER_AMOUNT_FRAC_BASE / 10));
     593           0 :       n = (n * 10) % (TALER_AMOUNT_FRAC_BASE);
     594             :     }
     595           0 :     tail[i] = '\0';
     596           0 :     GNUNET_snprintf (result,
     597             :                      sizeof (result),
     598             :                      "%s:%llu.%s",
     599             :                      norm.currency,
     600           0 :                      (unsigned long long) norm.value,
     601             :                      tail);
     602             :   }
     603             :   else
     604             :   {
     605           0 :     GNUNET_snprintf (result,
     606             :                      sizeof (result),
     607             :                      "%s:%llu",
     608             :                      norm.currency,
     609           0 :                      (unsigned long long) norm.value);
     610             :   }
     611           0 :   return result;
     612             : }
     613             : 
     614             : 
     615             : /**
     616             :  * Divide an amount by a float.  Note that this function
     617             :  * may introduce a rounding error!
     618             :  *
     619             :  * @param result where to store @a dividend / @a divisor
     620             :  * @param dividend amount to divide
     621             :  * @param divisor by what to divide, must be positive
     622             :  */
     623             : void
     624           4 : TALER_amount_divide (struct TALER_Amount *result,
     625             :                      const struct TALER_Amount *dividend,
     626             :                      uint32_t divisor)
     627             : {
     628             :   uint64_t modr;
     629             : 
     630           4 :   GNUNET_assert (0 != divisor);
     631           4 :   *result = *dividend;
     632           4 :   if (1 == divisor)
     633           1 :     return;
     634           3 :   modr = result->value % divisor;
     635           3 :   result->value /= divisor;
     636             :   /* modr is a 32-bit value, so we can safely multiply by (<32-bit) base and add fraction! */
     637           3 :   modr = (modr * TALER_AMOUNT_FRAC_BASE) + result->fraction;
     638           3 :   GNUNET_assert (modr < TALER_AMOUNT_FRAC_BASE * divisor);
     639           3 :   result->fraction = (uint32_t) (modr / divisor);
     640             : }
     641             : 
     642             : 
     643             : /* end of amount.c */

Generated by: LCOV version 1.13