libtommath/mp_radix_size.c

38 lines
805 B
C
Raw Normal View History

#include "tommath_private.h"
#ifdef MP_RADIX_SIZE_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-08-05 01:24:44 +00:00
/* returns size of ASCII representation */
mp_err mp_radix_size(const mp_int *a, int radix, int *size)
2003-08-05 01:24:44 +00:00
{
2019-10-10 22:29:20 +00:00
mp_err err;
mp_int a_;
uint32_t b;
2004-01-25 17:40:21 +00:00
2017-08-30 17:13:53 +00:00
/* make sure the radix is in range */
if ((radix < 2) || (radix > 64)) {
return MP_VAL;
}
2003-08-05 01:24:44 +00:00
if (MP_IS_ZERO(a)) {
2017-08-30 17:13:53 +00:00
*size = 2;
return MP_OKAY;
}
2003-08-05 01:24:44 +00:00
2019-10-10 22:29:20 +00:00
a_ = *a;
a_.sign = MP_ZPOS;
if ((err = mp_log_u32(&a_, (uint32_t)radix, &b)) != MP_OKAY) {
2019-10-10 22:29:20 +00:00
goto LBL_ERR;
2017-08-30 17:13:53 +00:00
}
2005-03-12 11:55:11 +00:00
*size = (int)b;
2003-12-24 18:59:22 +00:00
2019-10-10 22:29:20 +00:00
/* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
2019-10-20 14:42:37 +00:00
*size += 2 + ((a->sign == MP_NEG) ? 1 : 0);
2019-05-29 10:23:08 +00:00
LBL_ERR:
return err;
2003-08-05 01:24:44 +00:00
}
2004-10-29 22:07:18 +00:00
#endif