xref: /netbsd-src/external/lgpl3/mpfr/dist/src/inp_str.c (revision ae87de8892f277bece3527c15b186ebcfa188227)
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 (str_size == (size_t) -1)
73         break;
74       c = getc (stream);
75     }
76   /* FIXME: The use of ungetc has been deprecated since C99 when it
77      occurs at the beginning of a binary stream, and this may happen
78      on /dev/null. One could add a "if (c != EOF)" test, but let's
79      wait for some discussion in comp.std.c first... */
80   ungetc (c, stream);
81 
82   if (MPFR_UNLIKELY (str_size == (size_t) -1 || str_size == 0 ||
83                      (c == EOF && ! feof (stream))))
84     {
85       /* size_t overflow or I/O error */
86       retval = -1;
87     }
88   else
89     {
90       /* number of characters read is nread + str_size - 1 */
91 
92       /* We exited the "for" loop by the first break instruction,
93          then necessarily str_size >= alloc_size was checked, so
94          now str_size < alloc_size. */
95       MPFR_ASSERTD (str_size < alloc_size);
96 
97       str[str_size] = '\0';
98 
99       retval = mpfr_set_str (rop, (char *) str, base, rnd_mode);
100     }
101 
102   mpfr_free_func (str, alloc_size);
103 
104   if (retval == -1)
105     return 0;                   /* error */
106 
107   MPFR_ASSERTD (nread >= 1);
108   str_size += nread - 1;
109   if (MPFR_UNLIKELY (str_size < nread - 1))  /* size_t overflow */
110     return 0;  /* however, rop has been set successfully */
111   else
112     return str_size;
113 }
114