1 /* mpfr_inp_str -- input a number in base BASE from stdio stream STREAM
2 and store the result in ROP
3
4 Copyright 1999, 2001-2002, 2004, 2006-2023 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramba projects, INRIA.
6
7 This file is part of the GNU MPFR Library.
8
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23
24 #include <ctype.h>
25
26 #include "mpfr-impl.h"
27
28 /* The original version of this function came from GMP's mpf/inp_str.c;
29 it has been adapted for MPFR. */
30
31 size_t
32 mpfr_inp_str (mpfr_ptr rop, FILE *stream, int base, mpfr_rnd_t rnd_mode)
33 {
34 unsigned char *str;
35 size_t alloc_size, str_size;
36 int c;
37 int retval;
38 size_t nread;
39
40 alloc_size = 100;
41 str = (unsigned char *) mpfr_allocate_func (alloc_size);
42 str_size = 0;
43 nread = 0;
44
45 /* Skip whitespace. EOF will be detected later. */
46 do
47 {
48 c = getc (stream);
49 nread++;
50 }
51 while (isspace (c));
52
53 /* number of characters read is nread */
54
55 for (;;)
56 {
57 MPFR_ASSERTD (str_size < (size_t) -1);
58 if (str_size >= alloc_size)
59 {
60 size_t old_alloc_size = alloc_size;
61 alloc_size = alloc_size / 2 * 3;
62 /* Handle overflow in unsigned integer arithmetic... */
63 if (MPFR_UNLIKELY (alloc_size <= old_alloc_size))
64 alloc_size = (size_t) -1;
65 MPFR_ASSERTD (str_size < alloc_size);
66 str = (unsigned char *)
67 mpfr_reallocate_func (str, old_alloc_size, alloc_size);
68 }
69 if (c == EOF || isspace (c))
70 break;
71 str[str_size++] = (unsigned char) c;
72 /* If c is '\0' (while not being a whitespace character), the word will
73 not have a valid format. But in the context of a string in memory,
74 '\0' is a terminating null character. So, to avoid ending with a
75 valid string format (like "1" with ignored characters after the
76 terminating null character), we need to make sure that the string
77 does not have a valid format; so let's start it with '*'. Note
78 that we should read the full word, so we cannot break. */
79 if (MPFR_UNLIKELY (c == '\0'))
80 str[0] = '*';
81 if (str_size == (size_t) -1)
82 break;
83 c = getc (stream);
84 }
85 /* The use of ungetc has been deprecated since C99 when it occurs at the
86 beginning of a binary stream, and this may happen on /dev/null. Here,
87 this is possible only for c == EOF. The condition "if (c != EOF)" below
88 is just there to handle this case. */
89 if (c != EOF)
90 ungetc (c, stream);
91
92 if (MPFR_UNLIKELY (str_size == (size_t) -1 || str_size == 0 ||
93 (c == EOF && ! feof (stream))))
94 {
95 /* size_t overflow or I/O error */
96 retval = -1;
97 }
98 else
99 {
100 /* number of characters read is nread + str_size - 1 */
101
102 /* We exited the "for" loop by the first break instruction,
103 then necessarily str_size >= alloc_size was checked, so
104 now str_size < alloc_size. */
105 MPFR_ASSERTD (str_size < alloc_size);
106
107 str[str_size] = '\0';
108
109 retval = mpfr_set_str (rop, (char *) str, base, rnd_mode);
110 }
111
112 mpfr_free_func (str, alloc_size);
113
114 if (retval == -1)
115 return 0; /* error */
116
117 MPFR_ASSERTD (nread >= 1);
118 str_size += nread - 1;
119 if (MPFR_UNLIKELY (str_size < nread - 1)) /* size_t overflow */
120 return 0; /* however, rop has been set successfully */
121 else
122 return str_size;
123 }