diff --git a/src/include/taler_extensions.h b/src/include/taler_extensions.h index 243811eb5..bedffaea9 100644 --- a/src/include/taler_extensions.h +++ b/src/include/taler_extensions.h @@ -28,14 +28,6 @@ #define TALER_EXTENSION_SECTION_PREFIX "exchange-extension-" -enum TALER_Extension_ReturnValue -{ - TALER_Extension_OK = 0, - TALER_Extension_ERROR_PARSING = 1, - TALER_Extension_ERROR_INVALID = 2, - TALER_Extension_ERROR_SYS = 3 -}; - enum TALER_Extension_Type { TALER_Extension_AgeRestriction = 0, @@ -109,7 +101,7 @@ TALER_extension_get_by_name (const char *name, * @param[out] mask Mask representation for age restriction. * @return Error, if age groups were invalid, OK otherwise. */ -enum TALER_Extension_ReturnValue +enum GNUNET_GenericReturnValue TALER_parse_age_group_string (const char *groups, struct TALER_AgeMask *mask); @@ -133,7 +125,7 @@ TALER_age_mask_to_string (const struct TALER_AgeMask *mask); * @return Error if extension for age restriction was set but age groups were * invalid, OK otherwise. */ -enum TALER_Extension_ReturnValue +enum GNUNET_GenericReturnValue TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, struct TALER_AgeMask *mask); diff --git a/src/util/extension_age_restriction.c b/src/util/extension_age_restriction.c index b29a8ca88..2c5ed2c9c 100644 --- a/src/util/extension_age_restriction.c +++ b/src/util/extension_age_restriction.c @@ -30,12 +30,12 @@ * @return Error if extension for age restriction was set, but age groups were * invalid, OK otherwise. */ -enum TALER_Extension_ReturnValue +enum GNUNET_GenericReturnValue TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, struct TALER_AgeMask *mask) { char *groups; - enum TALER_Extension_ReturnValue ret = TALER_Extension_ERROR_SYS; + enum GNUNET_GenericReturnValue ret = GNUNET_SYSERR; if ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg, TALER_EXTENSION_SECTION_AGE_RESTRICTION, @@ -46,7 +46,7 @@ TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, { /* Age restriction is not enabled */ mask->mask = 0; - return TALER_Extension_OK; + return GNUNET_OK; } /* Age restriction is enabled, extract age groups */ @@ -56,13 +56,13 @@ TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, &groups)) { /* FIXME: log error? */ - return TALER_Extension_ERROR_SYS; + return GNUNET_SYSERR; } if (groups == NULL) { /* No groups defined in config, return default_age_mask */ mask->mask = TALER_EXTENSION_DEFAULT_AGE_MASK; - return TALER_Extension_OK; + return GNUNET_OK; } ret = TALER_parse_age_group_string (groups, mask); @@ -79,59 +79,46 @@ TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, * @param[out] mask Bit representation of the age groups. * @return Error if string was invalid, OK otherwise. */ -enum TALER_Extension_ReturnValue +enum GNUNET_GenericReturnValue TALER_parse_age_group_string (const char *groups, struct TALER_AgeMask *mask) { - enum TALER_Extension_ReturnValue ret = TALER_Extension_ERROR_SYS; - char *pos; + + const char *end = groups + strlen (groups); + const char *pos = groups; unsigned int prev = 0; - unsigned int val; - char dummy; + unsigned int val = 0; - while (1) + while (pos < end) { - pos = strchr (groups, ':'); - if (NULL != pos) + char c = *pos++; + if (':' == c) { - *pos = 0; - } + if (prev >= val) + return GNUNET_SYSERR; - if (1 != sscanf (groups, - "%u%c", - &val, - &dummy)) + mask->mask |= 1 << val; + prev = val; + val = 0; + } + else { - /* Invalid input */ - mask->mask = 0; - ret = TALER_Extension_ERROR_PARSING; - break; - } - else if ((0 >= val) || (32 <= val) || (prev >= val)) - { - /* Invalid value */ - mask->mask = 0; - ret = TALER_Extension_ERROR_INVALID; - break; - } + if ('0'>c || '9'mask |= 1 << val; + val = 10 * val + c - '0'; - if (NULL == pos) - { - /* We reached the end. Mark zeroth age-group and exit. */ - mask->mask |= 1; - ret = TALER_Extension_OK; - break; + if (0>=val || 32<=val) + return GNUNET_SYSERR; } - - prev = val; - *pos = ':'; - groups = pos + 1; } - return ret; + if (0>=val || 32<=val || prev>=val) + return GNUNET_SYSERR; + + mask->mask |= 1 << val; + + return GNUNET_OK; }