diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/amount.c | 41 | ||||
-rw-r--r-- | src/util/config.c | 9 | ||||
-rw-r--r-- | src/util/test_amount.c | 14 |
3 files changed, 47 insertions, 17 deletions
diff --git a/src/util/amount.c b/src/util/amount.c index 97a1cf46a..88f4b6aae 100644 --- a/src/util/amount.c +++ b/src/util/amount.c @@ -40,6 +40,31 @@ invalidate (struct TALER_Amount *a) enum GNUNET_GenericReturnValue +TALER_check_currency (const char *str) +{ + if (strlen (str) >= TALER_CURRENCY_LEN) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Currency code name `%s' is too long\n", + str); + return GNUNET_SYSERR; + } + /* validate str has only legal characters in it! */ + for (unsigned int i = 0; '\0' != str[i]; i++) + { + if ( ('A' > str[i]) || ('Z' < str[i]) ) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Currency code name `%s' contains illegal characters (only A-Z allowed)\n", + str); + return GNUNET_SYSERR; + } + } + return GNUNET_OK; +} + + +enum GNUNET_GenericReturnValue TALER_string_to_amount (const char *str, struct TALER_Amount *amount) { @@ -73,12 +98,14 @@ TALER_string_to_amount (const char *str, GNUNET_assert (TALER_CURRENCY_LEN > (colon - str)); for (unsigned int i = 0; i<colon - str; i++) - amount->currency[i] = toupper (str[i]); + amount->currency[i] = str[i]; /* 0-terminate *and* normalize buffer by setting everything to '\0' */ memset (&amount->currency [colon - str], 0, TALER_CURRENCY_LEN - (colon - str)); - + if (GNUNET_OK != + TALER_check_currency (amount->currency)) + return GNUNET_SYSERR; /* skip colon */ value = colon + 1; if ('\0' == value[0]) @@ -193,7 +220,7 @@ TALER_amount_hton (struct TALER_AmountNBO *res, res->value = GNUNET_htonll (d->value); res->fraction = htonl (d->fraction); for (unsigned int i = 0; i<TALER_CURRENCY_LEN; i++) - res->currency[i] = toupper (d->currency[i]); + res->currency[i] = d->currency[i]; } @@ -217,15 +244,15 @@ TALER_amount_set_zero (const char *cur, { size_t slen; - slen = strlen (cur); - if (slen >= TALER_CURRENCY_LEN) + if (GNUNET_OK != + TALER_check_currency (cur)) return GNUNET_SYSERR; + slen = strlen (cur); memset (amount, 0, sizeof (struct TALER_Amount)); for (unsigned int i = 0; i<slen; i++) - amount->currency[i] = toupper (cur[i]); - /* FIXME: check currency consists only of legal characters! */ + amount->currency[i] = cur[i]; return GNUNET_OK; } diff --git a/src/util/config.c b/src/util/config.c index 718c3a102..f00d11baa 100644 --- a/src/util/config.c +++ b/src/util/config.c @@ -250,17 +250,20 @@ parse_currencies_cb (void *cls, cpc->failure = true; return; } - if (strlen (str) >= TALER_CURRENCY_LEN) + if (GNUNET_OK != + TALER_check_currency (str)) { GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, section, "CODE", - "Currency code name given is too long"); + "Currency code name given is invalid"); cpc->failure = true; GNUNET_free (str); return; } - /* FIXME: validate str has only legal characters in it! */ + memset (cspec->currency, + 0, + sizeof (cspec->currency)); strcpy (cspec->currency, str); GNUNET_free (str); diff --git a/src/util/test_amount.c b/src/util/test_amount.c index a45b71de7..57d73b14f 100644 --- a/src/util/test_amount.c +++ b/src/util/test_amount.c @@ -78,31 +78,31 @@ main (int argc, /* test conversion with leading zero in fraction */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("eur:0.02", + TALER_string_to_amount ("EUR:0.02", &a2)); - GNUNET_assert (0 == strcasecmp ("eur", + GNUNET_assert (0 == strcasecmp ("EUR", a2.currency)); GNUNET_assert (0 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 2 == a2.fraction); c = TALER_amount_to_string (&a2); - GNUNET_assert (0 == strcasecmp ("eur:0.02", + GNUNET_assert (0 == strcasecmp ("EUR:0.02", c)); GNUNET_free (c); /* test conversion with leading space and with fraction */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount (" eur:4.12", + TALER_string_to_amount (" EUR:4.12", &a2)); - GNUNET_assert (0 == strcasecmp ("eur", + GNUNET_assert (0 == strcasecmp ("EUR", a2.currency)); GNUNET_assert (4 == a2.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 100 * 12 == a2.fraction); /* test use of local currency */ GNUNET_assert (GNUNET_OK == - TALER_string_to_amount (" *LOCAL:4444.1000", + TALER_string_to_amount (" LOCAL:4444.1000", &a3)); - GNUNET_assert (0 == strcasecmp ("*LOCAL", + GNUNET_assert (0 == strcasecmp ("LOCAL", a3.currency)); GNUNET_assert (4444 == a3.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE / 10 == a3.fraction); |