xref: /netbsd-src/crypto/external/bsd/openssh/dist/fmt_scaled.c (revision d91f98a8715141154279122ae81737cb65179572)
1*d91f98a8Spgoyette /*	$NetBSD: fmt_scaled.c,v 1.8 2019/01/27 02:08:33 pgoyette Exp $	*/
2313c6c94Schristos /*	$OpenBSD: fmt_scaled.c,v 1.9 2007/03/20 03:42:52 tedu Exp $	*/
3313c6c94Schristos 
4313c6c94Schristos /*
5313c6c94Schristos  * Copyright (c) 2001, 2002, 2003 Ian F. Darwin.  All rights reserved.
6313c6c94Schristos  *
7313c6c94Schristos  * Redistribution and use in source and binary forms, with or without
8313c6c94Schristos  * modification, are permitted provided that the following conditions
9313c6c94Schristos  * are met:
10313c6c94Schristos  * 1. Redistributions of source code must retain the above copyright
11313c6c94Schristos  *    notice, this list of conditions and the following disclaimer.
12313c6c94Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13313c6c94Schristos  *    notice, this list of conditions and the following disclaimer in the
14313c6c94Schristos  *    documentation and/or other materials provided with the distribution.
15313c6c94Schristos  * 3. The name of the author may not be used to endorse or promote products
16313c6c94Schristos  *    derived from this software without specific prior written permission.
17313c6c94Schristos  *
18313c6c94Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19313c6c94Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20313c6c94Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21313c6c94Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22313c6c94Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23313c6c94Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24313c6c94Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25313c6c94Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26313c6c94Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27313c6c94Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28313c6c94Schristos  */
29313c6c94Schristos 
30313c6c94Schristos /* OPENBSD ORIGINAL: lib/libutil/fmt_scaled.c */
31313c6c94Schristos 
32313c6c94Schristos /*
33313c6c94Schristos  * fmt_scaled: Format numbers scaled for human comprehension
34313c6c94Schristos  * scan_scaled: Scan numbers in this format.
35313c6c94Schristos  *
36313c6c94Schristos  * "Human-readable" output uses 4 digits max, and puts a unit suffix at
37313c6c94Schristos  * the end.  Makes output compact and easy-to-read esp. on huge disks.
38313c6c94Schristos  * Formatting code was originally in OpenBSD "df", converted to library routine.
39313c6c94Schristos  * Scanning code written for OpenBSD libutil.
40313c6c94Schristos  */
41313c6c94Schristos 
42313c6c94Schristos #include "includes.h"
43*d91f98a8Spgoyette __RCSID("$NetBSD: fmt_scaled.c,v 1.8 2019/01/27 02:08:33 pgoyette Exp $");
44313c6c94Schristos 
45313c6c94Schristos #ifndef HAVE_FMT_SCALED
46313c6c94Schristos #include <stdio.h>
47313c6c94Schristos #include <stdlib.h>
48313c6c94Schristos #include <errno.h>
49313c6c94Schristos #include <string.h>
50313c6c94Schristos #include <ctype.h>
51313c6c94Schristos #include <limits.h>
52313c6c94Schristos 
53313c6c94Schristos #include "fmt_scaled.h"
54313c6c94Schristos 
55313c6c94Schristos typedef enum {
56313c6c94Schristos 	NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6
57313c6c94Schristos } unit_type;
58313c6c94Schristos 
59313c6c94Schristos /* These three arrays MUST be in sync!  XXX make a struct */
60313c6c94Schristos static unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
61313c6c94Schristos static char scale_chars[] = "BKMGTPE";
62313c6c94Schristos static long long scale_factors[] = {
63313c6c94Schristos 	1LL,
64313c6c94Schristos 	1024LL,
65313c6c94Schristos 	1024LL*1024,
66313c6c94Schristos 	1024LL*1024*1024,
67313c6c94Schristos 	1024LL*1024*1024*1024,
68313c6c94Schristos 	1024LL*1024*1024*1024*1024,
69313c6c94Schristos 	1024LL*1024*1024*1024*1024*1024,
70313c6c94Schristos };
71313c6c94Schristos #define	SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
72313c6c94Schristos 
73313c6c94Schristos #define MAX_DIGITS (SCALE_LENGTH * 3)	/* XXX strlen(sprintf("%lld", -1)? */
74313c6c94Schristos 
75313c6c94Schristos /** Convert the given input string "scaled" into numeric in "result".
76313c6c94Schristos  * Return 0 on success, -1 and errno set on error.
77313c6c94Schristos  */
7800a838c4Schristos int
scan_scaled(const char * scaled,long long * result)7900a838c4Schristos scan_scaled(const char *scaled, long long *result)
80313c6c94Schristos {
8100a838c4Schristos 	const char *p = scaled;
82313c6c94Schristos 	int sign = 0;
83313c6c94Schristos 	unsigned int i, ndigits = 0, fract_digits = 0;
84313c6c94Schristos 	long long scale_fact = 1, whole = 0, fpart = 0;
85313c6c94Schristos 
86313c6c94Schristos 	/* Skip leading whitespace */
87313c6c94Schristos 	while (isascii((unsigned char)*p) && isspace((unsigned char)*p))
88313c6c94Schristos 		++p;
89313c6c94Schristos 
90313c6c94Schristos 	/* Then at most one leading + or - */
91313c6c94Schristos 	while (*p == '-' || *p == '+') {
92313c6c94Schristos 		if (*p == '-') {
93313c6c94Schristos 			if (sign) {
94313c6c94Schristos 				errno = EINVAL;
95313c6c94Schristos 				return -1;
96313c6c94Schristos 			}
97313c6c94Schristos 			sign = -1;
98313c6c94Schristos 			++p;
99313c6c94Schristos 		} else if (*p == '+') {
100313c6c94Schristos 			if (sign) {
101313c6c94Schristos 				errno = EINVAL;
102313c6c94Schristos 				return -1;
103313c6c94Schristos 			}
104313c6c94Schristos 			sign = +1;
105313c6c94Schristos 			++p;
106313c6c94Schristos 		}
107313c6c94Schristos 	}
108313c6c94Schristos 
109313c6c94Schristos 	/* Main loop: Scan digits, find decimal point, if present.
110313c6c94Schristos 	 * We don't allow exponentials, so no scientific notation
111313c6c94Schristos 	 * (but note that E for Exa might look like e to some!).
112313c6c94Schristos 	 * Advance 'p' to end, to get scale factor.
113313c6c94Schristos 	 */
114313c6c94Schristos 	for (; isascii((unsigned char)*p) && (isdigit((unsigned char)*p) || *p=='.'); ++p) {
115313c6c94Schristos 		if (*p == '.') {
116313c6c94Schristos 			if (fract_digits > 0) {	/* oops, more than one '.' */
117313c6c94Schristos 				errno = EINVAL;
118313c6c94Schristos 				return -1;
119313c6c94Schristos 			}
120313c6c94Schristos 			fract_digits = 1;
121313c6c94Schristos 			continue;
122313c6c94Schristos 		}
123313c6c94Schristos 
124313c6c94Schristos 		i = (*p) - '0';			/* whew! finally a digit we can use */
125313c6c94Schristos 		if (fract_digits > 0) {
126313c6c94Schristos 			if (fract_digits >= MAX_DIGITS-1)
127313c6c94Schristos 				/* ignore extra fractional digits */
128313c6c94Schristos 				continue;
129313c6c94Schristos 			fract_digits++;		/* for later scaling */
130313c6c94Schristos 			fpart *= 10;
131313c6c94Schristos 			fpart += i;
132313c6c94Schristos 		} else {				/* normal digit */
133313c6c94Schristos 			if (++ndigits >= MAX_DIGITS) {
134313c6c94Schristos 				errno = ERANGE;
135313c6c94Schristos 				return -1;
136313c6c94Schristos 			}
137313c6c94Schristos 			whole *= 10;
138313c6c94Schristos 			whole += i;
139313c6c94Schristos 		}
140313c6c94Schristos 	}
141313c6c94Schristos 
142313c6c94Schristos 	if (sign) {
143313c6c94Schristos 		whole *= sign;
144313c6c94Schristos 		fpart *= sign;
145313c6c94Schristos 	}
146313c6c94Schristos 
147313c6c94Schristos 	/* If no scale factor given, we're done. fraction is discarded. */
148313c6c94Schristos 	if (!*p) {
149313c6c94Schristos 		*result = whole;
150313c6c94Schristos 		return 0;
151313c6c94Schristos 	}
152313c6c94Schristos 
153313c6c94Schristos 	/* Validate scale factor, and scale whole and fraction by it. */
154313c6c94Schristos 	for (i = 0; i < SCALE_LENGTH; i++) {
155313c6c94Schristos 
156313c6c94Schristos 		/** Are we there yet? */
157313c6c94Schristos 		if (*p == scale_chars[i] ||
158313c6c94Schristos 			*p == tolower((unsigned char)scale_chars[i])) {
159313c6c94Schristos 
160313c6c94Schristos 			/* If it ends with alphanumerics after the scale char, bad. */
161313c6c94Schristos 			if (isalnum((unsigned char)*(p+1))) {
162313c6c94Schristos 				errno = EINVAL;
163313c6c94Schristos 				return -1;
164313c6c94Schristos 			}
165313c6c94Schristos 			scale_fact = scale_factors[i];
166313c6c94Schristos 
167313c6c94Schristos 			/* scale whole part */
168313c6c94Schristos 			whole *= scale_fact;
169313c6c94Schristos 
170313c6c94Schristos 			/* truncate fpart so it does't overflow.
171313c6c94Schristos 			 * then scale fractional part.
172313c6c94Schristos 			 */
173313c6c94Schristos 			while (fpart >= LLONG_MAX / scale_fact) {
174313c6c94Schristos 				fpart /= 10;
175313c6c94Schristos 				fract_digits--;
176313c6c94Schristos 			}
177313c6c94Schristos 			fpart *= scale_fact;
178313c6c94Schristos 			if (fract_digits > 0) {
179313c6c94Schristos 				for (i = 0; i < fract_digits -1; i++)
180313c6c94Schristos 					fpart /= 10;
181313c6c94Schristos 			}
182313c6c94Schristos 			whole += fpart;
183313c6c94Schristos 			*result = whole;
184313c6c94Schristos 			return 0;
185313c6c94Schristos 		}
186313c6c94Schristos 	}
187313c6c94Schristos 	errno = ERANGE;
188313c6c94Schristos 	return -1;
189313c6c94Schristos }
190313c6c94Schristos 
191313c6c94Schristos /* Format the given "number" into human-readable form in "result".
192313c6c94Schristos  * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
193313c6c94Schristos  * Return 0 on success, -1 and errno set if error.
194313c6c94Schristos  */
195313c6c94Schristos int
fmt_scaled(long long number,char * result)196313c6c94Schristos fmt_scaled(long long number, char *result)
197313c6c94Schristos {
198313c6c94Schristos 	long long abval, fract = 0;
199313c6c94Schristos 	unsigned int i;
200313c6c94Schristos 	unit_type unit = NONE;
201313c6c94Schristos 
202313c6c94Schristos 	abval = (number < 0LL) ? -number : number;	/* no long long_abs yet */
203313c6c94Schristos 
204313c6c94Schristos 	/* Not every negative long long has a positive representation.
205313c6c94Schristos 	 * Also check for numbers that are just too darned big to format
206313c6c94Schristos 	 */
207313c6c94Schristos 	if (abval < 0 || abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
208313c6c94Schristos 		errno = ERANGE;
209313c6c94Schristos 		return -1;
210313c6c94Schristos 	}
211313c6c94Schristos 
212313c6c94Schristos 	/* scale whole part; get unscaled fraction */
213313c6c94Schristos 	for (i = 0; i < SCALE_LENGTH; i++) {
214313c6c94Schristos 		if (abval/1024 < scale_factors[i]) {
215313c6c94Schristos 			unit = units[i];
216313c6c94Schristos 			fract = (i == 0) ? 0 : abval % scale_factors[i];
217313c6c94Schristos 			number /= scale_factors[i];
218313c6c94Schristos 			if (i > 0)
219313c6c94Schristos 				fract /= scale_factors[i - 1];
220313c6c94Schristos 			break;
221313c6c94Schristos 		}
222313c6c94Schristos 	}
223313c6c94Schristos 
224313c6c94Schristos 	fract = (10 * fract + 512) / 1024;
225313c6c94Schristos 	/* if the result would be >= 10, round main number */
226313c6c94Schristos 	if (fract == 10) {
227313c6c94Schristos 		if (number >= 0)
228313c6c94Schristos 			number++;
229313c6c94Schristos 		else
230313c6c94Schristos 			number--;
231313c6c94Schristos 		fract = 0;
232313c6c94Schristos 	}
233313c6c94Schristos 
234313c6c94Schristos 	if (number == 0)
235313c6c94Schristos 		strlcpy(result, "0B", FMT_SCALED_STRSIZE);
236313c6c94Schristos 	else if (unit == NONE || number >= 100 || number <= -100) {
237313c6c94Schristos 		if (fract >= 5) {
238313c6c94Schristos 			if (number >= 0)
239313c6c94Schristos 				number++;
240313c6c94Schristos 			else
241313c6c94Schristos 				number--;
242313c6c94Schristos 		}
243313c6c94Schristos 		(void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
244313c6c94Schristos 			number, scale_chars[unit]);
245313c6c94Schristos 	} else
246313c6c94Schristos 		(void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
247313c6c94Schristos 			number, fract, scale_chars[unit]);
248313c6c94Schristos 
249313c6c94Schristos 	return 0;
250313c6c94Schristos }
251313c6c94Schristos 
252313c6c94Schristos #ifdef	MAIN
253313c6c94Schristos /*
254313c6c94Schristos  * This is the original version of the program in the man page.
255313c6c94Schristos  * Copy-and-paste whatever you need from it.
256313c6c94Schristos  */
257313c6c94Schristos int
main(int argc,char ** argv)258313c6c94Schristos main(int argc, char **argv)
259313c6c94Schristos {
260313c6c94Schristos 	char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE];
261313c6c94Schristos 	long long ninput = 10483892, result;
262313c6c94Schristos 
263313c6c94Schristos 	if (scan_scaled(cinput, &result) == 0)
264313c6c94Schristos 		printf("\"%s\" -> %lld\n", cinput, result);
265313c6c94Schristos 	else
266313c6c94Schristos 		perror(cinput);
267313c6c94Schristos 
268313c6c94Schristos 	if (fmt_scaled(ninput, buf) == 0)
269313c6c94Schristos 		printf("%lld -> \"%s\"\n", ninput, buf);
270313c6c94Schristos 	else
271313c6c94Schristos 		fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno));
272313c6c94Schristos 
273313c6c94Schristos 	return 0;
274313c6c94Schristos }
275313c6c94Schristos #endif
276313c6c94Schristos 
277313c6c94Schristos #endif /* HAVE_FMT_SCALED */
278