1 /* mpz_inp_str(dest_integer, stream, base) -- Input a number in base 2 BASE from stdio stream STREAM and store the result in DEST_INTEGER. 3 4 OF THE FUNCTIONS IN THIS FILE, ONLY mpz_inp_str IS FOR EXTERNAL USE, THE 5 REST ARE INTERNALS AND ARE ALMOST CERTAIN TO BE SUBJECT TO INCOMPATIBLE 6 CHANGES OR DISAPPEAR COMPLETELY IN FUTURE GNU MP RELEASES. 7 8 Copyright 1991, 1993, 1994, 1996, 1998, 2000, 2001, 2002, 2003 Free Software 9 Foundation, Inc. 10 11 This file is part of the GNU MP Library. 12 13 The GNU MP Library is free software; you can redistribute it and/or modify 14 it under the terms of the GNU Lesser General Public License as published by 15 the Free Software Foundation; either version 3 of the License, or (at your 16 option) any later version. 17 18 The GNU MP Library is distributed in the hope that it will be useful, but 19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 20 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 21 License for more details. 22 23 You should have received a copy of the GNU Lesser General Public License 24 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 25 26 #include <stdio.h> 27 #include <ctype.h> 28 #include "gmp.h" 29 #include "gmp-impl.h" 30 31 extern const unsigned char __gmp_digit_value_tab[]; 32 #define digit_value_tab __gmp_digit_value_tab 33 34 size_t 35 mpz_inp_str (mpz_ptr x, FILE *stream, int base) 36 { 37 int c; 38 size_t nread; 39 40 if (stream == 0) 41 stream = stdin; 42 43 nread = 0; 44 45 /* Skip whitespace. */ 46 do 47 { 48 c = getc (stream); 49 nread++; 50 } 51 while (isspace (c)); 52 53 return mpz_inp_str_nowhite (x, stream, base, c, nread); 54 } 55 56 /* shared by mpq_inp_str */ 57 size_t 58 mpz_inp_str_nowhite (mpz_ptr x, FILE *stream, int base, int c, size_t nread) 59 { 60 char *str; 61 size_t alloc_size, str_size; 62 int negative; 63 mp_size_t xsize; 64 const unsigned char *digit_value; 65 66 ASSERT_ALWAYS (EOF == -1); /* FIXME: handle this by adding explicit */ 67 /* comparisons of c and EOF before each */ 68 /* read of digit_value[]. */ 69 70 digit_value = digit_value_tab; 71 if (base > 36) 72 { 73 /* For bases > 36, use the collating sequence 74 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */ 75 digit_value += 224; 76 if (base > 62) 77 return 0; /* too large base */ 78 } 79 80 negative = 0; 81 if (c == '-') 82 { 83 negative = 1; 84 c = getc (stream); 85 nread++; 86 } 87 88 if (c == EOF || digit_value[c] >= (base == 0 ? 10 : base)) 89 return 0; /* error if no digits */ 90 91 /* If BASE is 0, try to find out the base by looking at the initial 92 characters. */ 93 if (base == 0) 94 { 95 base = 10; 96 if (c == '0') 97 { 98 base = 8; 99 c = getc (stream); 100 nread++; 101 if (c == 'x' || c == 'X') 102 { 103 base = 16; 104 c = getc (stream); 105 nread++; 106 } 107 else if (c == 'b' || c == 'B') 108 { 109 base = 2; 110 c = getc (stream); 111 nread++; 112 } 113 } 114 } 115 116 /* Skip leading zeros. */ 117 while (c == '0') 118 { 119 c = getc (stream); 120 nread++; 121 } 122 123 alloc_size = 100; 124 str = (char *) (*__gmp_allocate_func) (alloc_size); 125 str_size = 0; 126 127 while (c != EOF) 128 { 129 int dig; 130 dig = digit_value[c]; 131 if (dig >= base) 132 break; 133 if (str_size >= alloc_size) 134 { 135 size_t old_alloc_size = alloc_size; 136 alloc_size = alloc_size * 3 / 2; 137 str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size); 138 } 139 str[str_size++] = dig; 140 c = getc (stream); 141 } 142 nread += str_size; 143 144 ungetc (c, stream); 145 nread--; 146 147 /* Make sure the string is not empty, mpn_set_str would fail. */ 148 if (str_size == 0) 149 { 150 x->_mp_size = 0; 151 } 152 else 153 { 154 xsize = 2 + (mp_size_t) 155 (str_size / (GMP_NUMB_BITS * mp_bases[base].chars_per_bit_exactly)); 156 MPZ_REALLOC (x, xsize); 157 158 /* Convert the byte array in base BASE to our bignum format. */ 159 xsize = mpn_set_str (x->_mp_d, (unsigned char *) str, str_size, base); 160 x->_mp_size = negative ? -xsize : xsize; 161 } 162 (*__gmp_free_func) (str, alloc_size); 163 return nread; 164 } 165