186d7f5d3SJohn Marino /* mpz_set_str(mp_dest, string, base) -- Convert the \0-terminated
286d7f5d3SJohn Marino string STRING in base BASE to multiple precision integer in
386d7f5d3SJohn Marino MP_DEST. Allow white space in the string. If BASE == 0 determine
486d7f5d3SJohn Marino the base in the C standard way, i.e. 0xhh...h means base 16,
586d7f5d3SJohn Marino 0oo...o means base 8, otherwise assume base 10.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2005
886d7f5d3SJohn Marino Free Software Foundation, Inc.
986d7f5d3SJohn Marino
1086d7f5d3SJohn Marino This file is part of the GNU MP Library.
1186d7f5d3SJohn Marino
1286d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1386d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1486d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1586d7f5d3SJohn Marino option) any later version.
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1886d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1986d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
2086d7f5d3SJohn Marino License for more details.
2186d7f5d3SJohn Marino
2286d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2386d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
2486d7f5d3SJohn Marino
2586d7f5d3SJohn Marino #include <string.h>
2686d7f5d3SJohn Marino #include <ctype.h>
2786d7f5d3SJohn Marino #include "gmp.h"
2886d7f5d3SJohn Marino #include "gmp-impl.h"
2986d7f5d3SJohn Marino
3086d7f5d3SJohn Marino extern const unsigned char __gmp_digit_value_tab[];
3186d7f5d3SJohn Marino #define digit_value_tab __gmp_digit_value_tab
3286d7f5d3SJohn Marino
3386d7f5d3SJohn Marino int
mpz_set_str(mpz_ptr x,const char * str,int base)3486d7f5d3SJohn Marino mpz_set_str (mpz_ptr x, const char *str, int base)
3586d7f5d3SJohn Marino {
3686d7f5d3SJohn Marino size_t str_size;
3786d7f5d3SJohn Marino char *s, *begs;
3886d7f5d3SJohn Marino size_t i;
3986d7f5d3SJohn Marino mp_size_t xsize;
4086d7f5d3SJohn Marino int c;
4186d7f5d3SJohn Marino int negative;
4286d7f5d3SJohn Marino const unsigned char *digit_value;
4386d7f5d3SJohn Marino TMP_DECL;
4486d7f5d3SJohn Marino
4586d7f5d3SJohn Marino digit_value = digit_value_tab;
4686d7f5d3SJohn Marino if (base > 36)
4786d7f5d3SJohn Marino {
4886d7f5d3SJohn Marino /* For bases > 36, use the collating sequence
4986d7f5d3SJohn Marino 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */
5086d7f5d3SJohn Marino digit_value += 224;
5186d7f5d3SJohn Marino if (base > 62)
5286d7f5d3SJohn Marino return -1; /* too large base */
5386d7f5d3SJohn Marino }
5486d7f5d3SJohn Marino
5586d7f5d3SJohn Marino /* Skip whitespace. */
5686d7f5d3SJohn Marino do
5786d7f5d3SJohn Marino c = (unsigned char) *str++;
5886d7f5d3SJohn Marino while (isspace (c));
5986d7f5d3SJohn Marino
6086d7f5d3SJohn Marino negative = 0;
6186d7f5d3SJohn Marino if (c == '-')
6286d7f5d3SJohn Marino {
6386d7f5d3SJohn Marino negative = 1;
6486d7f5d3SJohn Marino c = (unsigned char) *str++;
6586d7f5d3SJohn Marino }
6686d7f5d3SJohn Marino
6786d7f5d3SJohn Marino if (digit_value[c] >= (base == 0 ? 10 : base))
6886d7f5d3SJohn Marino return -1; /* error if no valid digits */
6986d7f5d3SJohn Marino
7086d7f5d3SJohn Marino /* If BASE is 0, try to find out the base by looking at the initial
7186d7f5d3SJohn Marino characters. */
7286d7f5d3SJohn Marino if (base == 0)
7386d7f5d3SJohn Marino {
7486d7f5d3SJohn Marino base = 10;
7586d7f5d3SJohn Marino if (c == '0')
7686d7f5d3SJohn Marino {
7786d7f5d3SJohn Marino base = 8;
7886d7f5d3SJohn Marino c = (unsigned char) *str++;
7986d7f5d3SJohn Marino if (c == 'x' || c == 'X')
8086d7f5d3SJohn Marino {
8186d7f5d3SJohn Marino base = 16;
8286d7f5d3SJohn Marino c = (unsigned char) *str++;
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino else if (c == 'b' || c == 'B')
8586d7f5d3SJohn Marino {
8686d7f5d3SJohn Marino base = 2;
8786d7f5d3SJohn Marino c = (unsigned char) *str++;
8886d7f5d3SJohn Marino }
8986d7f5d3SJohn Marino }
9086d7f5d3SJohn Marino }
9186d7f5d3SJohn Marino
9286d7f5d3SJohn Marino /* Skip leading zeros and white space. */
9386d7f5d3SJohn Marino while (c == '0' || isspace (c))
9486d7f5d3SJohn Marino c = (unsigned char) *str++;
9586d7f5d3SJohn Marino /* Make sure the string does not become empty, mpn_set_str would fail. */
9686d7f5d3SJohn Marino if (c == 0)
9786d7f5d3SJohn Marino {
9886d7f5d3SJohn Marino x->_mp_size = 0;
9986d7f5d3SJohn Marino return 0;
10086d7f5d3SJohn Marino }
10186d7f5d3SJohn Marino
10286d7f5d3SJohn Marino TMP_MARK;
10386d7f5d3SJohn Marino str_size = strlen (str - 1);
10486d7f5d3SJohn Marino s = begs = (char *) TMP_ALLOC (str_size + 1);
10586d7f5d3SJohn Marino
10686d7f5d3SJohn Marino /* Remove spaces from the string and convert the result from ASCII to a
10786d7f5d3SJohn Marino byte array. */
10886d7f5d3SJohn Marino for (i = 0; i < str_size; i++)
10986d7f5d3SJohn Marino {
11086d7f5d3SJohn Marino if (!isspace (c))
11186d7f5d3SJohn Marino {
11286d7f5d3SJohn Marino int dig = digit_value[c];
11386d7f5d3SJohn Marino if (dig >= base)
11486d7f5d3SJohn Marino {
11586d7f5d3SJohn Marino TMP_FREE;
11686d7f5d3SJohn Marino return -1;
11786d7f5d3SJohn Marino }
11886d7f5d3SJohn Marino *s++ = dig;
11986d7f5d3SJohn Marino }
12086d7f5d3SJohn Marino c = (unsigned char) *str++;
12186d7f5d3SJohn Marino }
12286d7f5d3SJohn Marino
12386d7f5d3SJohn Marino str_size = s - begs;
12486d7f5d3SJohn Marino
12586d7f5d3SJohn Marino xsize = 2 + (mp_size_t)
12686d7f5d3SJohn Marino (str_size / (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly));
12786d7f5d3SJohn Marino MPZ_REALLOC (x, xsize);
12886d7f5d3SJohn Marino
12986d7f5d3SJohn Marino /* Convert the byte array in base BASE to our bignum format. */
13086d7f5d3SJohn Marino xsize = mpn_set_str (x->_mp_d, (unsigned char *) begs, str_size, base);
13186d7f5d3SJohn Marino x->_mp_size = negative ? -xsize : xsize;
13286d7f5d3SJohn Marino
13386d7f5d3SJohn Marino TMP_FREE;
13486d7f5d3SJohn Marino return 0;
13586d7f5d3SJohn Marino }
136