[BACK]Return to xtom.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpbsd

Annotation of OpenXM_contrib/gmp/mpbsd/xtom.c, Revision 1.1.1.3

1.1       maekawa     1: /* xtom -- convert a hexadecimal string to a MINT, and return a pointer to
                      2:    the MINT.
                      3:
1.1.1.3 ! ohara       4: Copyright 1991, 1994, 1995, 1996, 2000, 2001 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2   maekawa     9: it under the terms of the GNU Lesser General Public License as published by
                     10: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    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
1.1.1.2   maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2   maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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:
1.1.1.2   maekawa    23: #include <string.h>
                     24: #include <ctype.h>
1.1       maekawa    25: #include "mp.h"
                     26: #include "gmp.h"
                     27: #include "gmp-impl.h"
                     28:
                     29: static int
1.1.1.2   maekawa    30: digit_value_in_base (int c, int base)
1.1       maekawa    31: {
                     32:   int digit;
                     33:
                     34:   if (isdigit (c))
                     35:     digit = c - '0';
                     36:   else if (islower (c))
                     37:     digit = c - 'a' + 10;
                     38:   else if (isupper (c))
                     39:     digit = c - 'A' + 10;
                     40:   else
                     41:     return -1;
                     42:
                     43:   if (digit < base)
                     44:     return digit;
                     45:   return -1;
                     46: }
                     47:
                     48: MINT *
                     49: xtom (const char *str)
                     50: {
                     51:   size_t str_size;
                     52:   char *s, *begs;
                     53:   size_t i;
                     54:   mp_size_t xsize;
                     55:   int c;
                     56:   int negative;
1.1.1.3 ! ohara      57:   MINT *x = (MINT *) (*__gmp_allocate_func) (sizeof (MINT));
1.1       maekawa    58:   TMP_DECL (marker);
                     59:
                     60:   /* Skip whitespace.  */
                     61:   do
                     62:     c = *str++;
                     63:   while (isspace (c));
                     64:
                     65:   negative = 0;
                     66:   if (c == '-')
                     67:     {
                     68:       negative = 1;
                     69:       c = *str++;
                     70:     }
                     71:
                     72:   if (digit_value_in_base (c, 16) < 0)
                     73:     return 0;                  /* error if no digits */
                     74:
                     75:   TMP_MARK (marker);
                     76:   str_size = strlen (str - 1);
                     77:   s = begs = (char *) TMP_ALLOC (str_size + 1);
                     78:
                     79:   for (i = 0; i < str_size; i++)
                     80:     {
                     81:       if (!isspace (c))
                     82:        {
                     83:          int dig = digit_value_in_base (c, 16);
                     84:          if (dig < 0)
                     85:            {
                     86:              TMP_FREE (marker);
                     87:              return 0;
                     88:            }
                     89:          *s++ = dig;
                     90:        }
                     91:       c = *str++;
                     92:     }
                     93:
                     94:   str_size = s - begs;
                     95:
                     96:   xsize = str_size / __mp_bases[16].chars_per_limb + 1;
                     97:   x->_mp_alloc = xsize;
1.1.1.3 ! ohara      98:   x->_mp_d = (mp_ptr) (*__gmp_allocate_func) (xsize * BYTES_PER_MP_LIMB);
1.1       maekawa    99:
                    100:   xsize = mpn_set_str (x->_mp_d, (unsigned char *) begs, str_size, 16);
                    101:   x->_mp_size = negative ? -xsize : xsize;
                    102:
                    103:   TMP_FREE (marker);
                    104:   return x;
                    105: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>