1*1c9681d1Schristos /* $NetBSD: bn_mp_toradix.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2f59d82ffSelric
3f59d82ffSelric #include <tommath.h>
4f59d82ffSelric #ifdef BN_MP_TORADIX_C
5f59d82ffSelric /* LibTomMath, multiple-precision integer library -- Tom St Denis
6f59d82ffSelric *
7f59d82ffSelric * LibTomMath is a library that provides multiple-precision
8f59d82ffSelric * integer arithmetic as well as number theoretic functionality.
9f59d82ffSelric *
10f59d82ffSelric * The library was designed directly after the MPI library by
11f59d82ffSelric * Michael Fromberger but has been written from scratch with
12f59d82ffSelric * additional optimizations in place.
13f59d82ffSelric *
14f59d82ffSelric * The library is free for all purposes without any express
15f59d82ffSelric * guarantee it works.
16f59d82ffSelric *
17f59d82ffSelric * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18f59d82ffSelric */
19f59d82ffSelric
20f59d82ffSelric /* stores a bignum as a ASCII string in a given radix (2..64) */
mp_toradix(mp_int * a,char * str,int radix)21f59d82ffSelric int mp_toradix (mp_int * a, char *str, int radix)
22f59d82ffSelric {
23f59d82ffSelric int res, digs;
24f59d82ffSelric mp_int t;
25f59d82ffSelric mp_digit d;
26f59d82ffSelric char *_s = str;
27f59d82ffSelric
28f59d82ffSelric /* check range of the radix */
29f59d82ffSelric if (radix < 2 || radix > 64) {
30f59d82ffSelric return MP_VAL;
31f59d82ffSelric }
32f59d82ffSelric
33f59d82ffSelric /* quick out if its zero */
34f59d82ffSelric if (mp_iszero(a) == 1) {
35f59d82ffSelric *str++ = '0';
36f59d82ffSelric *str = '\0';
37f59d82ffSelric return MP_OKAY;
38f59d82ffSelric }
39f59d82ffSelric
40f59d82ffSelric if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
41f59d82ffSelric return res;
42f59d82ffSelric }
43f59d82ffSelric
44f59d82ffSelric /* if it is negative output a - */
45f59d82ffSelric if (t.sign == MP_NEG) {
46f59d82ffSelric ++_s;
47f59d82ffSelric *str++ = '-';
48f59d82ffSelric t.sign = MP_ZPOS;
49f59d82ffSelric }
50f59d82ffSelric
51f59d82ffSelric digs = 0;
52f59d82ffSelric while (mp_iszero (&t) == 0) {
53f59d82ffSelric if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
54f59d82ffSelric mp_clear (&t);
55f59d82ffSelric return res;
56f59d82ffSelric }
57f59d82ffSelric *str++ = mp_s_rmap[d];
58f59d82ffSelric ++digs;
59f59d82ffSelric }
60f59d82ffSelric
61f59d82ffSelric /* reverse the digits of the string. In this case _s points
62f59d82ffSelric * to the first digit [exluding the sign] of the number]
63f59d82ffSelric */
64f59d82ffSelric bn_reverse ((unsigned char *)_s, digs);
65f59d82ffSelric
66f59d82ffSelric /* append a NULL so the string is properly terminated */
67f59d82ffSelric *str = '\0';
68f59d82ffSelric
69f59d82ffSelric mp_clear (&t);
70f59d82ffSelric return MP_OKAY;
71f59d82ffSelric }
72f59d82ffSelric
73f59d82ffSelric #endif
74f59d82ffSelric
75f59d82ffSelric /* Source: /cvs/libtom/libtommath/bn_mp_toradix.c,v */
76f59d82ffSelric /* Revision: 1.4 */
77f59d82ffSelric /* Date: 2006/12/28 01:25:13 */
78