fix TALER_parse_age_group_string

- parser works now also on string literals
- use GNUNET_GenericReturnValue
This commit is contained in:
Özgür Kesim 2022-01-18 11:04:57 +01:00
parent 18394e4afe
commit f94f502a5e
Signed by: oec
GPG Key ID: 3D76A56D79EDD9D7
2 changed files with 33 additions and 54 deletions

View File

@ -28,14 +28,6 @@
#define TALER_EXTENSION_SECTION_PREFIX "exchange-extension-" #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 enum TALER_Extension_Type
{ {
TALER_Extension_AgeRestriction = 0, TALER_Extension_AgeRestriction = 0,
@ -109,7 +101,7 @@ TALER_extension_get_by_name (const char *name,
* @param[out] mask Mask representation for age restriction. * @param[out] mask Mask representation for age restriction.
* @return Error, if age groups were invalid, OK otherwise. * @return Error, if age groups were invalid, OK otherwise.
*/ */
enum TALER_Extension_ReturnValue enum GNUNET_GenericReturnValue
TALER_parse_age_group_string (const char *groups, TALER_parse_age_group_string (const char *groups,
struct TALER_AgeMask *mask); 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 * @return Error if extension for age restriction was set but age groups were
* invalid, OK otherwise. * invalid, OK otherwise.
*/ */
enum TALER_Extension_ReturnValue enum GNUNET_GenericReturnValue
TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg,
struct TALER_AgeMask *mask); struct TALER_AgeMask *mask);

View File

@ -30,12 +30,12 @@
* @return Error if extension for age restriction was set, but age groups were * @return Error if extension for age restriction was set, but age groups were
* invalid, OK otherwise. * invalid, OK otherwise.
*/ */
enum TALER_Extension_ReturnValue enum GNUNET_GenericReturnValue
TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg, TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg,
struct TALER_AgeMask *mask) struct TALER_AgeMask *mask)
{ {
char *groups; 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, if ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
TALER_EXTENSION_SECTION_AGE_RESTRICTION, TALER_EXTENSION_SECTION_AGE_RESTRICTION,
@ -46,7 +46,7 @@ TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg,
{ {
/* Age restriction is not enabled */ /* Age restriction is not enabled */
mask->mask = 0; mask->mask = 0;
return TALER_Extension_OK; return GNUNET_OK;
} }
/* Age restriction is enabled, extract age groups */ /* Age restriction is enabled, extract age groups */
@ -56,13 +56,13 @@ TALER_get_age_mask (const struct GNUNET_CONFIGURATION_Handle *cfg,
&groups)) &groups))
{ {
/* FIXME: log error? */ /* FIXME: log error? */
return TALER_Extension_ERROR_SYS; return GNUNET_SYSERR;
} }
if (groups == NULL) if (groups == NULL)
{ {
/* No groups defined in config, return default_age_mask */ /* No groups defined in config, return default_age_mask */
mask->mask = TALER_EXTENSION_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); 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. * @param[out] mask Bit representation of the age groups.
* @return Error if string was invalid, OK otherwise. * @return Error if string was invalid, OK otherwise.
*/ */
enum TALER_Extension_ReturnValue enum GNUNET_GenericReturnValue
TALER_parse_age_group_string (const char *groups, TALER_parse_age_group_string (const char *groups,
struct TALER_AgeMask *mask) 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 prev = 0;
unsigned int val; unsigned int val = 0;
char dummy;
while (1) while (pos < end)
{ {
pos = strchr (groups, ':'); char c = *pos++;
if (NULL != pos) if (':' == c)
{ {
*pos = 0; if (prev >= val)
} return GNUNET_SYSERR;
if (1 != sscanf (groups, mask->mask |= 1 << val;
"%u%c", prev = val;
&val, val = 0;
&dummy)) }
else
{ {
/* Invalid input */ if ('0'>c || '9'<c)
mask->mask = 0; return GNUNET_SYSERR;
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;
}
/* Set the corresponding bit in the mask */ val = 10 * val + c - '0';
mask->mask |= 1 << val;
if (NULL == pos) if (0>=val || 32<=val)
{ return GNUNET_SYSERR;
/* We reached the end. Mark zeroth age-group and exit. */
mask->mask |= 1;
ret = TALER_Extension_OK;
break;
} }
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;
} }