/* * Convert string to bytes, allowing either B/b for bytes, K/k for KB, * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on * other error. */ static int do_strtosz(const char *nptr, char **end, const char default_suffix, int64_t unit, uint64_t *result) { int retval; char *endptr; unsigned char c; int mul_required = 0; double val, mul, integral, fraction; errno = 0; val = strtod(nptr, &endptr); if (isnan(val) || endptr == nptr || errno != 0) { retval = -EINVAL; goto out; } fraction = modf(val, &integral); if (fraction != 0) { mul_required = 1; } c = *endptr; mul = suffix_mul(c, unit); if (mul >= 0) { endptr++; } else { mul = suffix_mul(default_suffix, unit); assert(mul >= 0); } if (mul == 1 && mul_required) { retval = -EINVAL; goto out; } /* * Values >= 0xfffffffffffffc00 overflow uint64_t after their trip * through double (53 bits of precision). */ if ((val * mul >= 0xfffffffffffffc00) || val < 0) { retval = -ERANGE; goto out; } *result = val * mul; retval = 0; out: if (end) { *end = endptr; } else if (*endptr) { retval = -EINVAL; } return retval; }
/* * Convert string to bytes, allowing either B/b for bytes, K/k for KB, * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on * other error. */ int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end, const char default_suffix, int64_t unit) { int64_t retval = -EINVAL; char *endptr; unsigned char c; int mul_required = 0; double val, mul, integral, fraction; errno = 0; val = strtod(nptr, &endptr); if (isnan(val) || endptr == nptr || errno != 0) { goto fail; } fraction = modf(val, &integral); if (fraction != 0) { mul_required = 1; } c = *endptr; mul = suffix_mul(c, unit); if (mul >= 0) { endptr++; } else { mul = suffix_mul(default_suffix, unit); assert(mul >= 0); } if (mul == 1 && mul_required) { goto fail; } if ((val * mul >= INT64_MAX) || val < 0) { retval = -ERANGE; goto fail; } retval = val * mul; fail: if (end) { *end = endptr; } return retval; }