=================================================================== RCS file: /home/cvs/OpenXM_contrib/gmp/mpbsd/Attic/mtox.c,v retrieving revision 1.1.1.2 retrieving revision 1.1.1.3 diff -u -p -r1.1.1.2 -r1.1.1.3 --- OpenXM_contrib/gmp/mpbsd/Attic/mtox.c 2000/09/09 14:13:17 1.1.1.2 +++ OpenXM_contrib/gmp/mpbsd/Attic/mtox.c 2003/08/25 16:06:37 1.1.1.3 @@ -1,7 +1,7 @@ /* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string with the result of the conversion. -Copyright (C) 1991, 1994, 2000 Free Software Foundation, Inc. +Copyright 1991, 1994, 2000, 2001, 2002 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -23,59 +23,40 @@ MA 02111-1307, USA. */ #include "mp.h" #include "gmp.h" #include "gmp-impl.h" +#include "longlong.h" char * -#if __STDC__ mtox (const MINT *x) -#else -mtox (x) - const MINT *x; -#endif { - mp_ptr xp; mp_size_t xsize = x->_mp_size; + mp_ptr xp; mp_size_t xsign; unsigned char *str, *s; - size_t str_size, i; - int zeros; - char *num_to_text; - TMP_DECL (marker); + size_t str_size, alloc_size, i; - if (xsize == 0) - { - str = (unsigned char *) (*_mp_allocate_func) (2); - str[0] = '0'; - str[1] = 0; - return (char *) str; - } xsign = xsize; if (xsize < 0) xsize = -xsize; - TMP_MARK (marker); - str_size = ((size_t) (xsize * BITS_PER_MP_LIMB - * __mp_bases[16].chars_per_bit_exactly)) + 3; - str = (unsigned char *) (*_mp_allocate_func) (str_size); + /* digits, plus '\0', plus possible '-', for an exact size */ + xp = x->_mp_d; + MPN_SIZEINBASE_16 (alloc_size, xp, xsize); + alloc_size += 1 + (xsign < 0); + + str = (unsigned char *) (*__gmp_allocate_func) (alloc_size); s = str; if (xsign < 0) *s++ = '-'; - /* Move the number to convert into temporary space, since mpn_get_str - clobbers its argument + needs one extra high limb.... */ - xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB); - MPN_COPY (xp, x->_mp_d, xsize); - str_size = mpn_get_str (s, 16, xp, xsize); + ASSERT (str_size <= alloc_size - (xsign < 0)); + ASSERT (str_size == 1 || *s != 0); - /* mpn_get_str might make some leading zeros. Skip them. */ - for (zeros = 0; s[zeros] == 0; zeros++) - str_size--; - - /* Translate to printable chars and move string down. */ for (i = 0; i < str_size; i++) - s[i] = "0123456789abcdef"[s[zeros + i]]; + s[i] = "0123456789abcdef"[s[i]]; s[str_size] = 0; + ASSERT (strlen (str) + 1 == alloc_size); return (char *) str; }