1 /* $NetBSD: humanize_number.c,v 1.19 2024/01/20 14:52:47 christos Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
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 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: humanize_number.c,v 1.19 2024/01/20 14:52:47 christos Exp $");
36 #endif /* LIBC_SCCS and not lint */
37
38 #include "namespace.h"
39 #include <assert.h>
40 #include <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <locale.h>
45
46 int
humanize_number(char * buf,size_t len,int64_t bytes,const char * suffix,int scale,int flags)47 humanize_number(char *buf, size_t len, int64_t bytes,
48 const char *suffix, int scale, int flags)
49 {
50 const char *prefixes, *sep;
51 int b, i, r, s1, s2, sign;
52 int64_t divisor, max, post = 1;
53 size_t baselen;
54 int maxscale;
55
56 _DIAGASSERT(buf != NULL);
57 _DIAGASSERT(scale >= 0);
58
59 if (suffix == NULL)
60 suffix = "";
61
62 if (flags & HN_DIVISOR_1000) {
63 /* SI for decimal multiplies */
64 divisor = 1000;
65 if (flags & HN_B)
66 prefixes = "B\0k\0M\0G\0T\0P\0E";
67 else
68 prefixes = "\0\0k\0M\0G\0T\0P\0E";
69 } else {
70 /*
71 * binary multiplies
72 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
73 */
74 divisor = 1024;
75 if (flags & HN_B)
76 prefixes = "B\0K\0M\0G\0T\0P\0E";
77 else
78 prefixes = "\0\0K\0M\0G\0T\0P\0E";
79 }
80
81 #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
82 maxscale = 6;
83
84 if (scale < 0 || (scale > maxscale &&
85 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0))
86 return (-1);
87
88 if (buf == NULL)
89 return (-1);
90
91 if (len > 0)
92 buf[0] = '\0';
93
94 if (bytes < 0) {
95 sign = -1;
96 baselen = 3; /* sign, digit, prefix */
97 if (-bytes < INT64_MAX / 100)
98 bytes *= -100;
99 else {
100 bytes = -bytes;
101 post = 100;
102 baselen += 2;
103 }
104 } else {
105 sign = 1;
106 baselen = 2; /* digit, prefix */
107 if (bytes < INT64_MAX / 100)
108 bytes *= 100;
109 else {
110 post = 100;
111 baselen += 2;
112 }
113 }
114 if (flags & HN_NOSPACE)
115 sep = "";
116 else {
117 sep = " ";
118 baselen++;
119 }
120 baselen += strlen(suffix);
121
122 /* Check if enough room for `x y' + suffix + `\0' */
123 if (len < baselen + 1)
124 return (-1);
125
126 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
127 /*
128 * 19 is number of digits in biggest possible int64_t
129 * If we don't do this, the calc of max just below can
130 * overflow, leading to absurd results. If the buffer
131 * is big enough for the number, simply use it, no scaling.
132 */
133 if (len - baselen > 19)
134 i = 0;
135 else {
136 size_t j;
137 /* See if there are additional columns to be used. */
138 for (max = 100, j = len - baselen; j-- > 0;)
139 max *= 10;
140
141 /*
142 * Divide the number until it fits the avail buffer.
143 * If there will be an overflow by the rounding below,
144 * (the "-50") divide once more.
145 */
146 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
147 bytes /= divisor;
148 }
149 if (scale & HN_GETSCALE) {
150 _DIAGASSERT(__type_fit(int, i));
151 return i;
152 }
153 } else {
154 /* XXX
155 * we already know scale <= maxscale, so
156 * i < scale ==> i < maxscale
157 */
158 for (i = 0; i < scale && i < maxscale; i++)
159 bytes /= divisor;
160 }
161
162 if (i == 0) {
163 /*
164 * Cannot go the bytes *= post route, as
165 * that can cause overflow of bytes
166 *
167 * but if we already scaled up, undo that.
168 */
169 if (post == 1)
170 bytes /= 100;
171
172 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
173 sign * bytes, sep, SCALE2PREFIX(0), suffix);
174 } else {
175 /*
176 * Here this is safe, as if i > 0, we have already
177 * divided bytes by at least 1000, post <= 100, so ...
178 */
179 bytes *= post;
180
181 /* If a value <= 9.9 after rounding and ... */
182 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
183 /* baselen + \0 + .N */
184 if (len < baselen + 1 + 1 +
185 strlen(localeconv()->decimal_point))
186 return (-1);
187 b = ((int)bytes + 5) / 10;
188 s1 = b / 10;
189 s2 = b % 10;
190 r = snprintf(buf, len, "%d%s%d%s%s%s",
191 sign * s1, localeconv()->decimal_point, s2,
192 sep, SCALE2PREFIX(i), suffix);
193 } else
194 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
195 sign * ((bytes + 50) / 100),
196 sep, SCALE2PREFIX(i), suffix);
197 }
198
199 if ((size_t)r > len)
200 r = -1;
201
202 return (r);
203 }
204