libtommath/bn_mp_radix_size.c

44 lines
881 B
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_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_, 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
if ((err = mp_init(&b)) != MP_OKAY) {
goto LBL_ERR;
2017-08-30 17:13:53 +00:00
}
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_ilogb(&a_, (uint32_t)radix, &b)) != MP_OKAY) {
goto LBL_ERR;
2017-08-30 17:13:53 +00:00
}
2005-03-12 11:55:11 +00:00
2019-10-10 22:29:20 +00:00
*size = (int)mp_get_l(&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`. */
*size += 2 + (a->sign == MP_NEG);
2019-05-29 10:23:08 +00:00
LBL_ERR:
2019-10-10 22:29:20 +00:00
mp_clear(&b);
2019-05-29 10:23:08 +00:00
return err;
2003-08-05 01:24:44 +00:00
}
2019-10-10 22:29:20 +00:00
2004-10-29 22:07:18 +00:00
#endif