Coverage Report

Created: 2025-07-11 06:08

/src/dropbear/libtommath/bn_mp_to_radix.c
Line
Count
Source (jump to first uncovered line)
1
#include "tommath_private.h"
2
#ifdef BN_MP_TO_RADIX_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
/* stores a bignum as a ASCII string in a given radix (2..64)
7
 *
8
 * Stores upto "size - 1" chars and always a NULL byte, puts the number of characters
9
 * written, including the '\0', in "written".
10
 */
11
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix)
12
0
{
13
0
   size_t  digs;
14
0
   mp_err  err;
15
0
   mp_int  t;
16
0
   mp_digit d;
17
0
   char   *_s = str;
18
19
   /* check range of radix and size*/
20
0
   if (maxlen < 2u) {
21
0
      return MP_BUF;
22
0
   }
23
0
   if ((radix < 2) || (radix > 64)) {
24
0
      return MP_VAL;
25
0
   }
26
27
   /* quick out if its zero */
28
0
   if (MP_IS_ZERO(a)) {
29
0
      *str++ = '0';
30
0
      *str = '\0';
31
0
      if (written != NULL) {
32
0
         *written = 2u;
33
0
      }
34
0
      return MP_OKAY;
35
0
   }
36
37
0
   if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
38
0
      return err;
39
0
   }
40
41
   /* if it is negative output a - */
42
0
   if (t.sign == MP_NEG) {
43
      /* we have to reverse our digits later... but not the - sign!! */
44
0
      ++_s;
45
46
      /* store the flag and mark the number as positive */
47
0
      *str++ = '-';
48
0
      t.sign = MP_ZPOS;
49
50
      /* subtract a char */
51
0
      --maxlen;
52
0
   }
53
0
   digs = 0u;
54
0
   while (!MP_IS_ZERO(&t)) {
55
0
      if (--maxlen < 1u) {
56
         /* no more room */
57
0
         err = MP_BUF;
58
0
         goto LBL_ERR;
59
0
      }
60
0
      if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
61
0
         goto LBL_ERR;
62
0
      }
63
0
      *str++ = mp_s_rmap[d];
64
0
      ++digs;
65
0
   }
66
   /* reverse the digits of the string.  In this case _s points
67
    * to the first digit [exluding the sign] of the number
68
    */
69
0
   s_mp_reverse((unsigned char *)_s, digs);
70
71
   /* append a NULL so the string is properly terminated */
72
0
   *str = '\0';
73
0
   digs++;
74
75
0
   if (written != NULL) {
76
0
      *written = (a->sign == MP_NEG) ? (digs + 1u): digs;
77
0
   }
78
79
0
LBL_ERR:
80
0
   mp_clear(&t);
81
0
   return err;
82
0
}
83
84
#endif