23 RCSID(
"$Id: bf93de635b48c588c48a809721c2a68f1e3bb121 $")
25 #include <freeradius-devel/util/math.h>
26 #include <freeradius-devel/util/sbuff.h>
42 static uint64_t base2_units[]= {
43 [
'k'] = (uint64_t)1024,
44 [
'm'] = (uint64_t)1024 * 1024,
45 [
'g'] = (uint64_t)1024 * 1024 * 1024,
46 [
't'] = (uint64_t)1024 * 1024 * 1024 * 1024,
47 [
'p'] = (uint64_t)1024 * 1024 * 1024 * 1024 * 1024,
48 [
'e'] = (uint64_t)1024 * 1024 * 1024 * 1024 * 1024 * 1024,
50 static size_t base2_units_len =
NUM_ELEMENTS(base2_units);
52 static uint64_t base10_units[] = {
53 [
'k'] = (uint64_t)1000,
54 [
'm'] = (uint64_t)1000 * 1000,
55 [
'g'] = (uint64_t)1000 * 1000 * 1000,
56 [
't'] = (uint64_t)1000 * 1000 * 1000 * 1000,
57 [
'p'] = (uint64_t)1000 * 1000 * 1000 * 1000 * 1000,
58 [
'e'] = (uint64_t)1000 * 1000 * 1000 * 1000 * 1000 * 1000,
60 static size_t base10_units_len =
NUM_ELEMENTS(base10_units);
78 fr_sbuff_next(&our_in);
81 fr_sbuff_set_to_start(&our_in);
91 fr_sbuff_next(&our_in);
100 fr_sbuff_next(&our_in);
107 units_len = base2_units_len;
109 units = base10_units;
110 units_len = base10_units_len;
113 if (((
size_t)c >= units_len) || units[(
uint8_t)c] == 0) {
121 fr_sbuff_set_to_start(&our_in);
127 if (size > SIZE_MAX) {
129 "file/memory size of this system (%zu)", size, (
size_t)SIZE_MAX);
160 {
"B", (uint64_t)1 },
161 {
"KiB", (uint64_t)1024 },
162 {
"MiB", (uint64_t)1024 * 1024 },
163 {
"GiB", (uint64_t)1024 * 1024 * 1024},
164 {
"TiB", (uint64_t)1024 * 1024 * 1024 * 1024},
165 {
"PiB", (uint64_t)1024 * 1024 * 1024 * 1024 * 1024},
166 {
"EiB", (uint64_t)1024 * 1024 * 1024 * 1024 * 1024 * 1024},
169 {
"B", (uint64_t)1 },
170 {
"KB", (uint64_t)1000 },
171 {
"MB", (uint64_t)1000 * 1000 },
172 {
"GB", (uint64_t)1000 * 1000 * 1000},
173 {
"TB", (uint64_t)1000 * 1000 * 1000 * 1000},
174 {
"PB", (uint64_t)1000 * 1000 * 1000 * 1000 * 1000},
175 {
"EB", (uint64_t)1000 * 1000 * 1000 * 1000 * 1000 * 1000},
179 uint8_t b2_idx = 0, b10_idx = 0;
188 if (pos2 < 3)
goto done;
194 for (tmp =
in, pos10 = 0; tmp && ((tmp % 1000) == 0); pos10++) tmp /= 1000;
196 if (pos10 > 0) b10_idx = (
uint8_t)pos10;
197 if (pos2 >= 10) b2_idx = (
uint8_t)(pos2 / 10);
202 if (b2_idx > b10_idx) {
203 unit = &base2_units[b2_idx];
205 unit = &base10_units[b10_idx];
210 if (slen < 0)
return slen;
static uint8_t fr_low_bit_pos(uint64_t num)
Find the lowest order high bit in an unsigned 64 bit integer.
#define fr_multiply(_out, _a, _b)
Multiplies two integers together.
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_char(_sbuff_or_marker, _eob)
#define fr_sbuff_extend(_sbuff_or_marker)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_out(_err, _out, _in)
fr_slen_t fr_size_from_str(size_t *out, fr_sbuff_t *in)
Parse a size string with optional unit.
fr_slen_t fr_size_to_str(fr_sbuff_t *out, size_t in)
Print a size string with unit.
Boxed value structures and functions to manipulate them.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
FR_SBUFF_SET_RETURN(sbuff, &our_sbuff)
static size_t char ** out