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.
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.
8 Copyright 1991, 1993, 1994, 1996, 1998, 2000-2003, 2011-2013 Free Software
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of either:
16 * the GNU Lesser General Public License as published by the Free
17 Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
22 * the GNU General Public License as published by the Free Software
23 Foundation; either version 2 of the License, or (at your option) any
26 or both in parallel, as here.
28 The GNU MP Library is distributed in the hope that it will be useful, but
29 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
33 You should have received copies of the GNU General Public License and the
34 GNU Lesser General Public License along with the GNU MP Library. If not,
35 see https://www.gnu.org/licenses/. */
43 #define digit_value_tab __gmp_digit_value_tab
46 mpz_inp_str (mpz_ptr x
, FILE *stream
, int base
)
56 /* Skip whitespace. */
64 return mpz_inp_str_nowhite (x
, stream
, base
, c
, nread
);
67 /* shared by mpq_inp_str */
69 mpz_inp_str_nowhite (mpz_ptr x
, FILE *stream
, int base
, int c
, size_t nread
)
72 size_t alloc_size
, str_size
;
75 const unsigned char *digit_value
;
77 ASSERT_ALWAYS (EOF
== -1); /* FIXME: handle this by adding explicit */
78 /* comparisons of c and EOF before each */
79 /* read of digit_value[]. */
81 digit_value
= digit_value_tab
;
84 /* For bases > 36, use the collating sequence
85 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */
88 return 0; /* too large base */
99 if (c
== EOF
|| digit_value
[c
] >= (base
== 0 ? 10 : base
))
100 return 0; /* error if no digits */
102 /* If BASE is 0, try to find out the base by looking at the initial
112 if (c
== 'x' || c
== 'X')
118 else if (c
== 'b' || c
== 'B')
127 /* Skip leading zeros. */
135 str
= (char *) (*__gmp_allocate_func
) (alloc_size
);
141 dig
= digit_value
[c
];
144 if (str_size
>= alloc_size
)
146 size_t old_alloc_size
= alloc_size
;
147 alloc_size
= alloc_size
* 3 / 2;
148 str
= (char *) (*__gmp_reallocate_func
) (str
, old_alloc_size
, alloc_size
);
150 str
[str_size
++] = dig
;
158 /* Make sure the string is not empty, mpn_set_str would fail. */
165 LIMBS_PER_DIGIT_IN_BASE (xsize
, str_size
, base
);
166 MPZ_REALLOC (x
, xsize
);
168 /* Convert the byte array in base BASE to our bignum format. */
169 xsize
= mpn_set_str (PTR (x
), (unsigned char *) str
, str_size
, base
);
170 SIZ (x
) = negative
? -xsize
: xsize
;
172 (*__gmp_free_func
) (str
, alloc_size
);