xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/out_str.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpz_out_str(stream, base, integer) -- Output to STREAM the multi prec.
2    integer INTEGER in base BASE.
3 
4 Copyright 1991, 1993, 1994, 1996, 2001, 2005, 2011, 2012, 2017 Free
5 Software Foundation, Inc.
6 
7 This file is part of the GNU MP Library.
8 
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
11 
12   * the GNU Lesser General Public License as published by the Free
13     Software Foundation; either version 3 of the License, or (at your
14     option) any later version.
15 
16 or
17 
18   * the GNU General Public License as published by the Free Software
19     Foundation; either version 2 of the License, or (at your option) any
20     later version.
21 
22 or both in parallel, as here.
23 
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27 for more details.
28 
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library.  If not,
31 see https://www.gnu.org/licenses/.  */
32 
33 #include <stdio.h>
34 #include "gmp-impl.h"
35 #include "longlong.h"
36 
37 size_t
mpz_out_str(FILE * stream,int base,mpz_srcptr x)38 mpz_out_str (FILE *stream, int base, mpz_srcptr x)
39 {
40   mp_ptr xp;
41   mp_size_t x_size = SIZ (x);
42   unsigned char *str;
43   size_t str_size;
44   size_t i;
45   size_t written;
46   const char *num_to_text;
47   TMP_DECL;
48 
49   if (stream == 0)
50     stream = stdout;
51 
52   num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
53   if (base > 1)
54     {
55       if (base <= 36)
56 	num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
57       else if (UNLIKELY (base > 62))
58 	    return 0;
59     }
60   else if (base > -2)
61     {
62       base = 10;
63     }
64   else
65     {
66       base = -base;
67       if (UNLIKELY (base > 36))
68 	return 0;
69     }
70 
71   written = 0;
72 
73   if (x_size < 0)
74     {
75       fputc ('-', stream);
76       x_size = -x_size;
77       written = 1;
78     }
79 
80   TMP_MARK;
81 
82   DIGITS_IN_BASE_PER_LIMB (str_size, x_size, base);
83   str_size += 3;
84   str = (unsigned char *) TMP_ALLOC (str_size);
85 
86   xp = PTR (x);
87   if (! POW2_P (base))
88     {
89       xp = TMP_ALLOC_LIMBS (x_size | 1);  /* |1 in case x_size==0 */
90       MPN_COPY (xp, PTR (x), x_size);
91     }
92 
93   str_size = mpn_get_str (str, base, xp, x_size);
94 
95   /* Convert result to printable chars.  */
96   for (i = 0; i < str_size; i++)
97     str[i] = num_to_text[str[i]];
98   str[str_size] = 0;
99 
100   {
101     size_t fwret;
102     fwret = fwrite ((char *) str, 1, str_size, stream);
103     written += fwret;
104   }
105 
106   TMP_FREE;
107   return ferror (stream) ? 0 : written;
108 }
109