diff --git a/src/util.h b/src/util.h index f166457..365ffe5 100644 --- a/src/util.h +++ b/src/util.h @@ -264,6 +264,15 @@ static inline unsigned long SnortStrtoul(const char *nptr, char **endptr, int ba return iRet; } +static inline unsigned long long SnortStrtoull(const char *nptr, char **endptr, int base) +{ + unsigned long long iRet; + errno = 0; + iRet = strtoull(nptr, endptr, base); + + return iRet; +} + // Checks to make sure we're not going to evaluate a negative number for which // strtoul() gladly accepts and parses returning an underflowed wrapped unsigned // long without error. @@ -323,6 +332,49 @@ static inline int SnortStrToU32(const char *buffer, char **endptr, return 0; } +/** + * SnortStrToU64 - U64 version of SnortStrToU32 + */ +static inline int SnortStrToU64(const char *buffer, char **endptr, + uint64_t *value, int base) +{ + uint64_t tmp; + + if ((buffer == NULL) || (endptr == NULL) || (value == NULL)) + return -1; + + // Only positive numbers should be processed and strtoull will + // eat up white space and process '-' and '+' so move past + // white space and check for a negative sign. + while (isspace((int)*buffer)) + buffer++; + + // If all spaces or a negative sign is found, return error. + // XXX May also want to exclude '+' as well. + if ((*buffer == '\0') || (*buffer == '-')) + return -1; + + tmp = SnortStrtoull(buffer, endptr, base); + + // The user of the function should check for ERANGE in errno since this + // function can be used such that an ERANGE error is acceptable and + // value gets truncated to UINT64_MAX. + if ((errno == EINVAL) || (*endptr == buffer)) + return -1; + + // If value is greater than a UINT64_MAX set value to UINT64_MAX + // and errno to ERANGE + if (tmp > UINT64_MAX) + { + tmp = UINT64_MAX; + errno = ERANGE; + } + + *value = (uint64_t)tmp; + + return 0; +} + static inline long SnortStrtolRange(const char *nptr, char **endptr, int base, long lo, long hi) { long iRet = SnortStrtol(nptr, endptr, base);