Annotation of OpenXM_contrib/gmp/mpbsd/xtom.c, Revision 1.1.1.2
1.1 maekawa 1: /* xtom -- convert a hexadecimal string to a MINT, and return a pointer to
2: the MINT.
3:
1.1.1.2 ! maekawa 4: Copyright (C) 1991, 1994, 1995, 1996, 2000 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: #if __STDC__
! 31: digit_value_in_base (int c, int base)
! 32: #else
1.1 maekawa 33: digit_value_in_base (c, base)
34: int c;
35: int base;
1.1.1.2 ! maekawa 36: #endif
1.1 maekawa 37: {
38: int digit;
39:
40: if (isdigit (c))
41: digit = c - '0';
42: else if (islower (c))
43: digit = c - 'a' + 10;
44: else if (isupper (c))
45: digit = c - 'A' + 10;
46: else
47: return -1;
48:
49: if (digit < base)
50: return digit;
51: return -1;
52: }
53:
54: MINT *
55: #if __STDC__
56: xtom (const char *str)
57: #else
58: xtom (str)
59: const char *str;
60: #endif
61: {
62: size_t str_size;
63: char *s, *begs;
64: size_t i;
65: mp_size_t xsize;
66: int c;
67: int negative;
68: MINT *x = (MINT *) (*_mp_allocate_func) (sizeof (MINT));
69: TMP_DECL (marker);
70:
71: /* Skip whitespace. */
72: do
73: c = *str++;
74: while (isspace (c));
75:
76: negative = 0;
77: if (c == '-')
78: {
79: negative = 1;
80: c = *str++;
81: }
82:
83: if (digit_value_in_base (c, 16) < 0)
84: return 0; /* error if no digits */
85:
86: TMP_MARK (marker);
87: str_size = strlen (str - 1);
88: s = begs = (char *) TMP_ALLOC (str_size + 1);
89:
90: for (i = 0; i < str_size; i++)
91: {
92: if (!isspace (c))
93: {
94: int dig = digit_value_in_base (c, 16);
95: if (dig < 0)
96: {
97: TMP_FREE (marker);
98: return 0;
99: }
100: *s++ = dig;
101: }
102: c = *str++;
103: }
104:
105: str_size = s - begs;
106:
107: xsize = str_size / __mp_bases[16].chars_per_limb + 1;
108: x->_mp_alloc = xsize;
109: x->_mp_d = (mp_ptr) (*_mp_allocate_func) (xsize * BYTES_PER_MP_LIMB);
110:
111: xsize = mpn_set_str (x->_mp_d, (unsigned char *) begs, str_size, 16);
112: x->_mp_size = negative ? -xsize : xsize;
113:
114: TMP_FREE (marker);
115: return x;
116: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>