1/*	$NetBSD: dehumanize_number.c,v 1.8 2020/08/18 19:18:06 christos Exp $	*/
2
3/*
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifdef HAVE_NBTOOL_CONFIG_H
34#include "nbtool_config.h"
35#endif /* HAVE_NBTOOL_CONFIG_H */
36
37#include <sys/cdefs.h>
38#if defined(LIBC_SCCS) && !defined(lint)
39__RCSID("$NetBSD: dehumanize_number.c,v 1.8 2020/08/18 19:18:06 christos Exp $");
40#endif /* LIBC_SCCS and not lint */
41
42#include "namespace.h"
43#include <assert.h>
44#include <inttypes.h>
45#include <ctype.h>
46#include <stdlib.h>
47#include <string.h>
48#include <errno.h>
49#include <limits.h>
50
51/*
52 * Converts the number given in 'str', which may be given in a humanized
53 * form (as described in humanize_number(3), but with some limitations),
54 * to an int64_t without units.
55 * In case of success, 0 is returned and *size holds the value.
56 * Otherwise, -1 is returned and *size is untouched.
57 *
58 * TODO: Internationalization, SI units.
59 */
60int
61dehumanize_number(const char *str, int64_t *size)
62{
63	char *ep, unit;
64	const char *delimit;
65	long multiplier;
66	long long tmp, tmp2;
67	size_t len;
68
69	len = strlen(str);
70	if (len == 0) {
71		errno = EINVAL;
72		return -1;
73	}
74
75	multiplier = 1;
76
77	unit = str[len - 1];
78	if (isalpha((unsigned char)unit)) {
79		switch (tolower((unsigned char)unit)) {
80		case 'b':
81			multiplier = 1;
82			break;
83
84		case 'k':
85			multiplier = 1024;
86			break;
87
88		case 'm':
89			multiplier = 1024 * 1024;
90			break;
91
92		case 'g':
93			multiplier = 1024 * 1024 * 1024;
94			break;
95
96		default:
97			errno = EINVAL;
98			return -1; /* Invalid suffix. */
99		}
100
101		delimit = &str[len - 1];
102	} else
103		delimit = NULL;
104
105	errno = 0;
106	tmp = strtoll(str, &ep, 10);
107	if (str[0] == '\0' || (ep != delimit && *ep != '\0')) {
108		errno = EINVAL;
109		return -1; /* Not a number. */
110	}
111	else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
112		return -1; /* Out of range. */
113
114	tmp2 = tmp * multiplier;
115	tmp2 = tmp2 / multiplier;
116	if (tmp != tmp2) {
117		errno = ERANGE;
118		return -1; /* Out of range. */
119	}
120	tmp *= multiplier;
121#ifdef _DIAGASSERT
122	_DIAGASSERT(__type_fit(int64_t, tmp));
123#endif
124	*size = (int64_t)tmp;
125
126	return 0;
127}
128