Line data Source code
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 0 : mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix)
12 : {
13 : size_t digs;
14 : mp_err err;
15 : mp_int t;
16 : 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 : }
23 0 : if ((radix < 2) || (radix > 64)) {
24 0 : return MP_VAL;
25 : }
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 : }
34 0 : return MP_OKAY;
35 : }
36 :
37 0 : if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
38 0 : return err;
39 : }
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 : }
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 : }
60 0 : if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
61 0 : goto LBL_ERR;
62 : }
63 0 : *str++ = mp_s_rmap[d];
64 0 : ++digs;
65 : }
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 : }
78 :
79 0 : LBL_ERR:
80 0 : mp_clear(&t);
81 0 : return err;
82 : }
83 :
84 : #endif
|