From 49b7a96eec92731b170c7b484cb14ef7f85fd821 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sun, 4 Apr 2010 18:07:32 +0200 Subject: [PATCH] Add safe_hextou64() --- src/misc.c | 36 ++++++++++++++++++++++++++++++++++++ src/misc.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/src/misc.c b/src/misc.c index f9c55cbb..0665d385 100644 --- a/src/misc.c +++ b/src/misc.c @@ -181,6 +181,42 @@ safe_atou64(const char *str, uint64_t *val) return 0; } +int +safe_hextou64(const char *str, uint64_t *val) +{ + char *end; + unsigned long long intval; + + errno = 0; + intval = strtoull(str, &end, 16); + + if (((errno == ERANGE) && (intval == ULLONG_MAX)) + || ((errno != 0) && (intval == 0))) + { + DPRINTF(E_DBG, L_MISC, "Invalid integer in string (%s): %s\n", str, strerror(errno)); + + return -1; + } + + if (end == str) + { + DPRINTF(E_DBG, L_MISC, "No integer found in string (%s)\n", str); + + return -1; + } + + if (intval > UINT64_MAX) + { + DPRINTF(E_DBG, L_MISC, "Integer value too large (%s)\n", str); + + return -1; + } + + *val = (uint64_t)intval; + + return 0; +} + char * m_realpath(const char *pathname) { diff --git a/src/misc.h b/src/misc.h index bd006d63..d99665fb 100644 --- a/src/misc.h +++ b/src/misc.h @@ -16,6 +16,9 @@ safe_atoi64(const char *str, int64_t *val); int safe_atou64(const char *str, uint64_t *val); +int +safe_hextou64(const char *str, uint64_t *val); + char * m_realpath(const char *pathname);