Blame mpz/get_str.c

Packit 5c3484
/* mpz_get_str (string, base, mp_src) -- Convert the multiple precision
Packit 5c3484
   number MP_SRC to a string STRING of base BASE.  If STRING is NULL
Packit 5c3484
   allocate space for the result.  In any case, return a pointer to the
Packit 5c3484
   result.  If STRING is not NULL, the caller must ensure enough space is
Packit 5c3484
   available to store the result.
Packit 5c3484
Packit 5c3484
Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2012 Free Software
Packit 5c3484
Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <string.h> /* for strlen */
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
char *
Packit 5c3484
mpz_get_str (char *res_str, int base, mpz_srcptr x)
Packit 5c3484
{
Packit 5c3484
  mp_ptr xp;
Packit 5c3484
  mp_size_t x_size = SIZ (x);
Packit 5c3484
  char *return_str;
Packit 5c3484
  size_t str_size;
Packit 5c3484
  size_t alloc_size = 0;
Packit 5c3484
  const char *num_to_text;
Packit 5c3484
  int i;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  if (base >= 0)
Packit 5c3484
    {
Packit 5c3484
      num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
Packit 5c3484
      if (base <= 1)
Packit 5c3484
	base = 10;
Packit 5c3484
      else if (base > 36)
Packit 5c3484
	{
Packit 5c3484
	  num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Packit 5c3484
	  if (base > 62)
Packit 5c3484
	    return NULL;
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      base = -base;
Packit 5c3484
      if (base <= 1)
Packit 5c3484
	base = 10;
Packit 5c3484
      else if (base > 36)
Packit 5c3484
	return NULL;
Packit 5c3484
      num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* allocate string for the user if necessary */
Packit 5c3484
  if (res_str == NULL)
Packit 5c3484
    {
Packit 5c3484
      /* digits, null terminator, possible minus sign */
Packit 5c3484
      MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base);
Packit 5c3484
      alloc_size += 1 + (x_size<0);
Packit 5c3484
      res_str = (char *) (*__gmp_allocate_func) (alloc_size);
Packit 5c3484
    }
Packit 5c3484
  return_str = res_str;
Packit 5c3484
Packit 5c3484
  if (x_size < 0)
Packit 5c3484
    {
Packit 5c3484
      *res_str++ = '-';
Packit 5c3484
      x_size = -x_size;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* mpn_get_str clobbers its input on non power-of-2 bases */
Packit 5c3484
  TMP_MARK;
Packit 5c3484
  xp = PTR (x);
Packit 5c3484
  if (! POW2_P (base))
Packit 5c3484
    {
Packit 5c3484
      xp = TMP_ALLOC_LIMBS (x_size | 1);  /* |1 in case x_size==0 */
Packit 5c3484
      MPN_COPY (xp, PTR (x), x_size);
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
Packit 5c3484
  ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
Packit 5c3484
Packit 5c3484
  /* Convert result to printable chars.  */
Packit 5c3484
  for (i = 0; i < str_size; i++)
Packit 5c3484
    res_str[i] = num_to_text[(int) res_str[i]];
Packit 5c3484
  res_str[str_size] = 0;
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
Packit 5c3484
  /* if allocated then resize down to the actual space required */
Packit 5c3484
  if (alloc_size != 0)
Packit 5c3484
    {
Packit 5c3484
      size_t  actual_size = str_size + 1 + (res_str - return_str);
Packit 5c3484
      ASSERT (actual_size == strlen (return_str) + 1);
Packit 5c3484
      __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size,
Packit 5c3484
					char);
Packit 5c3484
    }
Packit 5c3484
  return return_str;
Packit 5c3484
}