1 #include "tommath_private.h"
2 #ifdef BN_MP_RADIX_SIZE_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* returns size of ASCII representation */
7 mp_err
mp_radix_size(const mp_int
*a
, int radix
, int *size
)
16 /* make sure the radix is in range */
17 if ((radix
< 2) || (radix
> 64)) {
26 /* special case for binary */
28 *size
= (mp_count_bits(a
) + ((a
->sign
== MP_NEG
) ? 1 : 0) + 1);
32 /* digs is the digit count */
35 /* if it's negative add one for the sign */
36 if (a
->sign
== MP_NEG
) {
40 /* init a copy of the input */
41 if ((err
= mp_init_copy(&t
, a
)) != MP_OKAY
) {
45 /* force temp to positive */
48 /* fetch out all of the digits */
49 while (!MP_IS_ZERO(&t
)) {
50 if ((err
= mp_div_d(&t
, (mp_digit
)radix
, &t
, &d
)) != MP_OKAY
) {
56 /* return digs + 1, the 1 is for the NULL byte that would be required. */