xref: /freebsd/lib/libutil/expand_number.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
5  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <inttypes.h>
34 #include <libutil.h>
35 #include <stdint.h>
36 
37 int
38 expand_number(const char *buf, uint64_t *num)
39 {
40 	char *endptr;
41 	uintmax_t umaxval;
42 	uint64_t number;
43 	unsigned shift;
44 	int serrno;
45 
46 	serrno = errno;
47 	errno = 0;
48 	umaxval = strtoumax(buf, &endptr, 0);
49 	if (umaxval > UINT64_MAX)
50 		errno = ERANGE;
51 	if (errno != 0)
52 		return (-1);
53 	errno = serrno;
54 	number = umaxval;
55 
56 	switch (tolower((unsigned char)*endptr)) {
57 	case 'e':
58 		shift = 60;
59 		break;
60 	case 'p':
61 		shift = 50;
62 		break;
63 	case 't':
64 		shift = 40;
65 		break;
66 	case 'g':
67 		shift = 30;
68 		break;
69 	case 'm':
70 		shift = 20;
71 		break;
72 	case 'k':
73 		shift = 10;
74 		break;
75 	case 'b':
76 		shift = 0;
77 		break;
78 	case '\0': /* No unit. */
79 		*num = number;
80 		return (0);
81 	default:
82 		/* Unrecognized unit. */
83 		errno = EINVAL;
84 		return (-1);
85 	}
86 
87 	/*
88 	 * Treat 'b' as an ignored suffix for all unit except 'b',
89 	 * otherwise there should be no remaining character(s).
90 	 */
91 	endptr++;
92 	if (shift != 0 && tolower((unsigned char)*endptr) == 'b')
93 		endptr++;
94 	if (*endptr != '\0') {
95 		errno = EINVAL;
96 		return (-1);
97 	}
98 
99 	if ((number << shift) >> shift != number) {
100 		/* Overflow */
101 		errno = ERANGE;
102 		return (-1);
103 	}
104 	*num = number << shift;
105 	return (0);
106 }
107