humanize_number.c revision 1.18 1 /* $NetBSD: humanize_number.c,v 1.18 2019/03/11 15:10:51 kre 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.18 2019/03/11 15:10:51 kre 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
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 /* See if there are additional columns to be used. */
137 for (max = 100, i = len - baselen; i-- > 0;)
138 max *= 10;
139
140 /*
141 * Divide the number until it fits the avail buffer.
142 * If there will be an overflow by the rounding below,
143 * (the "-50") divide once more.
144 */
145 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
146 bytes /= divisor;
147 }
148 if (scale & HN_GETSCALE) {
149 _DIAGASSERT(__type_fit(int, i));
150 return i;
151 }
152 } else {
153 /* XXX
154 * we already know scale <= maxscale, so
155 * i < scale ==> i < maxscale
156 */
157 for (i = 0; i < scale && i < maxscale; i++)
158 bytes /= divisor;
159 }
160
161 if (i == 0) {
162 /*
163 * Cannot go the bytes *= post route, as
164 * that can cause overflow of bytes
165 *
166 * but if we already scaled up, undo that.
167 */
168 if (post == 1)
169 bytes /= 100;
170
171 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
172 sign * bytes, sep, SCALE2PREFIX(0), suffix);
173 } else {
174 /*
175 * Here this is safe, as if i > 0, we have already
176 * divided bytes by at least 1000, post <= 100, so ...
177 */
178 bytes *= post;
179
180 /* If a value <= 9.9 after rounding and ... */
181 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
182 /* baselen + \0 + .N */
183 if (len < baselen + 1 + 1 +
184 strlen(localeconv()->decimal_point))
185 return (-1);
186 b = ((int)bytes + 5) / 10;
187 s1 = b / 10;
188 s2 = b % 10;
189 r = snprintf(buf, len, "%d%s%d%s%s%s",
190 sign * s1, localeconv()->decimal_point, s2,
191 sep, SCALE2PREFIX(i), suffix);
192 } else
193 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
194 sign * ((bytes + 50) / 100),
195 sep, SCALE2PREFIX(i), suffix);
196 }
197
198 if ((size_t)r > len)
199 r = -1;
200
201 return (r);
202 }
203