xref: /dflybsd-src/contrib/gmp/mpz/get_str.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpz_get_str (string, base, mp_src) -- Convert the multiple precision
286d7f5d3SJohn Marino    number MP_SRC to a string STRING of base BASE.  If STRING is NULL
386d7f5d3SJohn Marino    allocate space for the result.  In any case, return a pointer to the
486d7f5d3SJohn Marino    result.  If STRING is not NULL, the caller must ensure enough space is
586d7f5d3SJohn Marino    available to store the result.
686d7f5d3SJohn Marino 
786d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2005 Free Software
886d7f5d3SJohn Marino Foundation, Inc.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino This file is part of the GNU MP Library.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1386d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1486d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1586d7f5d3SJohn Marino option) any later version.
1686d7f5d3SJohn Marino 
1786d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1886d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1986d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
2086d7f5d3SJohn Marino License for more details.
2186d7f5d3SJohn Marino 
2286d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2386d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino #include <string.h> /* for strlen */
2686d7f5d3SJohn Marino #include "gmp.h"
2786d7f5d3SJohn Marino #include "gmp-impl.h"
2886d7f5d3SJohn Marino #include "longlong.h"
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino char *
mpz_get_str(char * res_str,int base,mpz_srcptr x)3186d7f5d3SJohn Marino mpz_get_str (char *res_str, int base, mpz_srcptr x)
3286d7f5d3SJohn Marino {
3386d7f5d3SJohn Marino   mp_ptr xp;
3486d7f5d3SJohn Marino   mp_size_t x_size = x->_mp_size;
3586d7f5d3SJohn Marino   char *str;
3686d7f5d3SJohn Marino   char *return_str;
3786d7f5d3SJohn Marino   size_t str_size;
3886d7f5d3SJohn Marino   size_t alloc_size = 0;
3986d7f5d3SJohn Marino   char *num_to_text;
4086d7f5d3SJohn Marino   int i;
4186d7f5d3SJohn Marino   TMP_DECL;
4286d7f5d3SJohn Marino 
4386d7f5d3SJohn Marino   if (base >= 0)
4486d7f5d3SJohn Marino     {
4586d7f5d3SJohn Marino       num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
4686d7f5d3SJohn Marino       if (base == 0)
4786d7f5d3SJohn Marino 	base = 10;
4886d7f5d3SJohn Marino       else if (base > 36)
4986d7f5d3SJohn Marino 	{
5086d7f5d3SJohn Marino 	  num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
5186d7f5d3SJohn Marino 	  if (base > 62)
5286d7f5d3SJohn Marino 	    return NULL;
5386d7f5d3SJohn Marino 	}
5486d7f5d3SJohn Marino     }
5586d7f5d3SJohn Marino   else
5686d7f5d3SJohn Marino     {
5786d7f5d3SJohn Marino       base = -base;
5886d7f5d3SJohn Marino       num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5986d7f5d3SJohn Marino     }
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino   /* allocate string for the user if necessary */
6286d7f5d3SJohn Marino   if (res_str == NULL)
6386d7f5d3SJohn Marino     {
6486d7f5d3SJohn Marino       /* digits, null terminator, possible minus sign */
6586d7f5d3SJohn Marino       MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base);
6686d7f5d3SJohn Marino       alloc_size += 1 + (x_size<0);
6786d7f5d3SJohn Marino       res_str = (char *) (*__gmp_allocate_func) (alloc_size);
6886d7f5d3SJohn Marino     }
6986d7f5d3SJohn Marino   return_str = res_str;
7086d7f5d3SJohn Marino 
7186d7f5d3SJohn Marino   if (x_size < 0)
7286d7f5d3SJohn Marino     {
7386d7f5d3SJohn Marino       *res_str++ = '-';
7486d7f5d3SJohn Marino       x_size = -x_size;
7586d7f5d3SJohn Marino     }
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino   /* mpn_get_str clobbers its input on non power-of-2 bases */
7886d7f5d3SJohn Marino   TMP_MARK;
7986d7f5d3SJohn Marino   xp = x->_mp_d;
8086d7f5d3SJohn Marino   if (! POW2_P (base))
8186d7f5d3SJohn Marino     {
8286d7f5d3SJohn Marino       xp = TMP_ALLOC_LIMBS (x_size + 1);  /* +1 in case x_size==0 */
8386d7f5d3SJohn Marino       MPN_COPY (xp, x->_mp_d, x_size);
8486d7f5d3SJohn Marino     }
8586d7f5d3SJohn Marino 
8686d7f5d3SJohn Marino   str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
8786d7f5d3SJohn Marino   ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
8886d7f5d3SJohn Marino 
8986d7f5d3SJohn Marino   /* might have a leading zero, skip it */
9086d7f5d3SJohn Marino   str = res_str;
9186d7f5d3SJohn Marino   if (*res_str == 0 && str_size != 1)
9286d7f5d3SJohn Marino     {
9386d7f5d3SJohn Marino       str_size--;
9486d7f5d3SJohn Marino       str++;
9586d7f5d3SJohn Marino       ASSERT (*str != 0);  /* at most one leading zero */
9686d7f5d3SJohn Marino     }
9786d7f5d3SJohn Marino 
9886d7f5d3SJohn Marino   /* Convert result to printable chars, and move down if there was a leading
9986d7f5d3SJohn Marino      zero.  */
10086d7f5d3SJohn Marino   for (i = 0; i < str_size; i++)
10186d7f5d3SJohn Marino     res_str[i] = num_to_text[(int) str[i]];
10286d7f5d3SJohn Marino   res_str[str_size] = 0;
10386d7f5d3SJohn Marino 
10486d7f5d3SJohn Marino   TMP_FREE;
10586d7f5d3SJohn Marino 
10686d7f5d3SJohn Marino   /* if allocated then resize down to the actual space required */
10786d7f5d3SJohn Marino   if (alloc_size != 0)
10886d7f5d3SJohn Marino     {
10986d7f5d3SJohn Marino       size_t  actual_size = str_size + 1 + (res_str - return_str);
11086d7f5d3SJohn Marino       ASSERT (actual_size == strlen (return_str) + 1);
11186d7f5d3SJohn Marino       __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size,
11286d7f5d3SJohn Marino                                         char);
11386d7f5d3SJohn Marino     }
11486d7f5d3SJohn Marino   return return_str;
11586d7f5d3SJohn Marino }
116