comparison libtommath/bn_mp_to_radix.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
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 {
13 size_t digs;
14 mp_err err;
15 mp_int t;
16 mp_digit d;
17 char *_s = str;
18
19 /* check range of radix and size*/
20 if (maxlen < 2u) {
21 return MP_BUF;
22 }
23 if ((radix < 2) || (radix > 64)) {
24 return MP_VAL;
25 }
26
27 /* quick out if its zero */
28 if (MP_IS_ZERO(a)) {
29 *str++ = '0';
30 *str = '\0';
31 if (written != NULL) {
32 *written = 2u;
33 }
34 return MP_OKAY;
35 }
36
37 if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
38 return err;
39 }
40
41 /* if it is negative output a - */
42 if (t.sign == MP_NEG) {
43 /* we have to reverse our digits later... but not the - sign!! */
44 ++_s;
45
46 /* store the flag and mark the number as positive */
47 *str++ = '-';
48 t.sign = MP_ZPOS;
49
50 /* subtract a char */
51 --maxlen;
52 }
53 digs = 0u;
54 while (!MP_IS_ZERO(&t)) {
55 if (--maxlen < 1u) {
56 /* no more room */
57 err = MP_BUF;
58 goto LBL_ERR;
59 }
60 if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
61 goto LBL_ERR;
62 }
63 *str++ = mp_s_rmap[d];
64 ++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 s_mp_reverse((unsigned char *)_s, digs);
70
71 /* append a NULL so the string is properly terminated */
72 *str = '\0';
73 digs++;
74
75 if (written != NULL) {
76 *written = (a->sign == MP_NEG) ? (digs + 1u): digs;
77 }
78
79 LBL_ERR:
80 mp_clear(&t);
81 return err;
82 }
83
84 #endif