Convert to keyserv, telnetd and telnet to libcrypto's BIGNUM
[dragonfly.git] / contrib / libgmp / mpbsd / mtox.c
1 /* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string
2    with the result of the conversion.
3
4 Copyright (C) 1991, 1994 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
22
23 #include "mp.h"
24 #include "gmp.h"
25 #include "gmp-impl.h"
26
27 char *
28 #if __STDC__
29 mtox (const MINT *x)
30 #else
31 mtox (x)
32      const MINT *x;
33 #endif
34 {
35   mp_ptr xp;
36   mp_size_t xsize = x->_mp_size;
37   mp_size_t xsign;
38   unsigned char *str, *s;
39   size_t str_size, i;
40   int zeros;
41   char *num_to_text;
42   TMP_DECL (marker);
43
44   if (xsize == 0)
45     {
46       str = (unsigned char *) (*_mp_allocate_func) (2);
47       str[0] = '0';
48       str[1] = 0;
49       return str;
50     }
51   xsign = xsize;
52   if (xsize < 0)
53     xsize = -xsize;
54
55   TMP_MARK (marker);
56   str_size = ((size_t) (xsize * BITS_PER_MP_LIMB
57                         * __mp_bases[16].chars_per_bit_exactly)) + 3;
58   str = (unsigned char *) (*_mp_allocate_func) (str_size);
59   s = str;
60
61   if (xsign < 0)
62     *s++ = '-';
63
64   /* Move the number to convert into temporary space, since mpn_get_str
65      clobbers its argument + needs one extra high limb....  */
66   xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB);
67   MPN_COPY (xp, x->_mp_d, xsize);
68
69   str_size = mpn_get_str (s, 16, xp, xsize);
70
71   /* mpn_get_str might make some leading zeros.  Skip them.  */
72   for (zeros = 0; s[zeros] == 0; zeros++)
73     str_size--;
74
75   /* Translate to printable chars and move string down.  */
76   for (i = 0; i < str_size; i++)
77     s[i] = "0123456789abcdef"[s[zeros + i]];
78   s[str_size] = 0;
79
80   return str;
81 }