strnum.c revision 7b58c089a0711e5e874fbf24c03d890ed2eebd62
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2010-2015 Dovecot authors, see the included COPYING file */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenbool str_is_numeric(const char *str, char end_char)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen * Unsigned decimal
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint name(const char *str, type *num_r, const char **endp_r) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_parse_uintmax(str, &l, endp_r) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_U__TEMPLATE(str_parse_uint, unsigned int)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_U__TEMPLATE(str_parse_ulong, unsigned long)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_U__TEMPLATE(str_parse_ullong, unsigned long long)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_U__TEMPLATE(str_parse_uint32, uint32_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_U__TEMPLATE(str_parse_uint64, uint64_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_to_uintmax(str, &l) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_U__TEMPLATE(str_to_ulong, unsigned long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_U__TEMPLATE(str_to_ullong, unsigned long long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_parse_uintmax(const char *str, uintmax_t *num_r,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen const char **endp_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if ((uintmax_t)(*str - '0') > ((uintmax_t)-1 % 10))
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_to_uintmax(const char *str, uintmax_t *num_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenbool str_uint_equals(const char *str, uintmax_t num)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch return l == num;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen * Unsigned hexadecimal
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint name(const char *str, type *num_r, const char **endp_r) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_parse_uintmax_hex(str, &l, endp_r) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UHEX__TEMPLATE(str_parse_uint_hex, unsigned int)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UHEX__TEMPLATE(str_parse_ulong_hex, unsigned long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UHEX__TEMPLATE(str_parse_ullong_hex, unsigned long long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UHEX__TEMPLATE(str_parse_uint32_hex, uint32_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UHEX__TEMPLATE(str_parse_uint64_hex, uint64_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_to_uintmax_hex(str, &l) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UHEX__TEMPLATE(str_to_uint_hex, unsigned int)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UHEX__TEMPLATE(str_to_ulong_hex, unsigned long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UHEX__TEMPLATE(str_to_ullong_hex, unsigned long long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UHEX__TEMPLATE(str_to_uint32_hex, uint32_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UHEX__TEMPLATE(str_to_uint64_hex, uint64_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic inline int _str_parse_hex(const char ch,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen unsigned int *hex_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen case '0': case '1': case '2': case '3': case '4':
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen case '5': case '6': case '7': case '8': case '9':
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_parse_uintmax_hex(const char *str, uintmax_t *num_r,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen const char **endp_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen unsigned int hex;
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_to_uintmax_hex(const char *str, uintmax_t *num_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen int ret = str_parse_uintmax_hex(str, &n, &endp);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen * Unsigned octal
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint name(const char *str, type *num_r, const char **endp_r) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_parse_uintmax_oct(str, &l, endp_r) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UOCT__TEMPLATE(str_parse_uint_oct, unsigned int)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UOCT__TEMPLATE(str_parse_ulong_oct, unsigned long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UOCT__TEMPLATE(str_parse_ullong_oct, unsigned long long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UOCT__TEMPLATE(str_parse_uint32_oct, uint32_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_PARSE_UOCT__TEMPLATE(str_parse_uint64_oct, uint64_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if (str_to_uintmax_oct(str, &l) < 0 || l > (type)-1) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UOCT__TEMPLATE(str_to_uint_oct, unsigned int)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UOCT__TEMPLATE(str_to_ulong_oct, unsigned long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UOCT__TEMPLATE(str_to_ullong_oct, unsigned long long)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UOCT__TEMPLATE(str_to_uint32_oct, uint32_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo SirainenSTR_TO_UOCT__TEMPLATE(str_to_uint64_oct, uint64_t)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_parse_uintmax_oct(const char *str, uintmax_t *num_r,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen const char **endp_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if ((uintmax_t)(*str - '0') > ((uintmax_t)-1 & 0x03))
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Boschint str_to_uintmax_oct(const char *str, uintmax_t *num_r)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch int ret = str_parse_uintmax_oct(str, &n, &endp);
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen#define STR_PARSE_S__TEMPLATE(name, type, int_min, int_max) \
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint name(const char *str, type *num_r, const char **endp_r) \
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_S__TEMPLATE(str_parse_int, int, INT_MIN, INT_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_S__TEMPLATE(str_parse_long, long, LONG_MIN, LONG_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_S__TEMPLATE(str_parse_llong, long long, LLONG_MIN, LLONG_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_S__TEMPLATE(str_parse_int32, int32_t, INT32_MIN, INT32_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_PARSE_S__TEMPLATE(str_parse_int64, int64_t, INT64_MIN, INT64_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch#define STR_TO_S__TEMPLATE(name, type, int_min, int_max) \
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_TO_S__TEMPLATE(str_to_int, int, INT_MIN, INT_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_TO_S__TEMPLATE(str_to_long, long, LONG_MIN, LONG_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_TO_S__TEMPLATE(str_to_llong, long long, LLONG_MIN, LLONG_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_TO_S__TEMPLATE(str_to_int32, int32_t, INT32_MIN, INT32_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan BoschSTR_TO_S__TEMPLATE(str_to_int64, int64_t, INT64_MIN, INT64_MAX)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Boschint str_parse_intmax(const char *str, intmax_t *num_r,
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch const char **endp_r)
8ccdf195768afdfbc32088d7be77dfca7dddd829Stephan Bosch if (l > UINTMAX_MAX - (UINTMAX_MAX + INTMAX_MIN))
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenint str_to_intmax(const char *str, intmax_t *num_r)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen * Special numeric types
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainenstatic int verify_xid(uintmax_t l, unsigned int result_size)
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen /* we assume that result is a signed type,
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen but that it can never be negative */
02c75e04c6ff80726bb59e3ea34a7995ad1f6f7cTimo Sirainen if ((l >> result_bits) != 0)
uintmax_t l;
#ifndef __APPLE__
uintmax_t l;
uintmax_t l;
intmax_t l;