Annotation of OpenXM/src/kan96xx/gmp-2.0.2/mpbsd/xtom.c, Revision 1.1.1.1
1.1 maekawa 1: /* xtom -- convert a hexadecimal string to a MINT, and return a pointer to
2: the MINT.
3:
4: Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
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
9: it under the terms of the GNU Library General Public License as published by
10: the Free Software Foundation; either version 2 of the License, or (at your
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
15: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16: License for more details.
17:
18: You should have received a copy of the GNU Library General Public License
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:
23: #include "mp.h"
24: #include "gmp.h"
25: #include "gmp-impl.h"
26:
27: static int
28: digit_value_in_base (c, base)
29: int c;
30: int base;
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: #if __STDC__
50: xtom (const char *str)
51: #else
52: xtom (str)
53: const char *str;
54: #endif
55: {
56: size_t str_size;
57: char *s, *begs;
58: size_t i;
59: mp_size_t xsize;
60: int c;
61: int negative;
62: MINT *x = (MINT *) (*_mp_allocate_func) (sizeof (MINT));
63: TMP_DECL (marker);
64:
65: /* Skip whitespace. */
66: do
67: c = *str++;
68: while (isspace (c));
69:
70: negative = 0;
71: if (c == '-')
72: {
73: negative = 1;
74: c = *str++;
75: }
76:
77: if (digit_value_in_base (c, 16) < 0)
78: return 0; /* error if no digits */
79:
80: TMP_MARK (marker);
81: str_size = strlen (str - 1);
82: s = begs = (char *) TMP_ALLOC (str_size + 1);
83:
84: for (i = 0; i < str_size; i++)
85: {
86: if (!isspace (c))
87: {
88: int dig = digit_value_in_base (c, 16);
89: if (dig < 0)
90: {
91: TMP_FREE (marker);
92: return 0;
93: }
94: *s++ = dig;
95: }
96: c = *str++;
97: }
98:
99: str_size = s - begs;
100:
101: xsize = str_size / __mp_bases[16].chars_per_limb + 1;
102: x->_mp_alloc = xsize;
103: x->_mp_d = (mp_ptr) (*_mp_allocate_func) (xsize * BYTES_PER_MP_LIMB);
104:
105: xsize = mpn_set_str (x->_mp_d, (unsigned char *) begs, str_size, 16);
106: x->_mp_size = negative ? -xsize : xsize;
107:
108: TMP_FREE (marker);
109: return x;
110: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>