/src/gmp-6.2.1/mpz/get_str.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_get_str (string, base, mp_src) -- Convert the multiple precision |
2 | | number MP_SRC to a string STRING of base BASE. If STRING is NULL |
3 | | allocate space for the result. In any case, return a pointer to the |
4 | | result. If STRING is not NULL, the caller must ensure enough space is |
5 | | available to store the result. |
6 | | |
7 | | Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2012, 2017 Free |
8 | | Software Foundation, Inc. |
9 | | |
10 | | This file is part of the GNU MP Library. |
11 | | |
12 | | The GNU MP Library is free software; you can redistribute it and/or modify |
13 | | it under the terms of either: |
14 | | |
15 | | * the GNU Lesser General Public License as published by the Free |
16 | | Software Foundation; either version 3 of the License, or (at your |
17 | | option) any later version. |
18 | | |
19 | | or |
20 | | |
21 | | * the GNU General Public License as published by the Free Software |
22 | | Foundation; either version 2 of the License, or (at your option) any |
23 | | later version. |
24 | | |
25 | | or both in parallel, as here. |
26 | | |
27 | | The GNU MP Library is distributed in the hope that it will be useful, but |
28 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
29 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
30 | | for more details. |
31 | | |
32 | | You should have received copies of the GNU General Public License and the |
33 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
34 | | see https://www.gnu.org/licenses/. */ |
35 | | |
36 | | #include <string.h> /* for strlen */ |
37 | | #include "gmp-impl.h" |
38 | | #include "longlong.h" |
39 | | |
40 | | char * |
41 | | mpz_get_str (char *res_str, int base, mpz_srcptr x) |
42 | 1.89k | { |
43 | 1.89k | mp_ptr xp; |
44 | 1.89k | mp_size_t x_size = SIZ (x); |
45 | 1.89k | char *return_str; |
46 | 1.89k | size_t str_size; |
47 | 1.89k | size_t alloc_size = 0; |
48 | 1.89k | const char *num_to_text; |
49 | 1.89k | int i; |
50 | 1.89k | TMP_DECL; |
51 | | |
52 | 1.89k | num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
53 | 1.89k | if (base > 1) |
54 | 1.89k | { |
55 | 1.89k | if (base <= 36) |
56 | 1.89k | num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz"; |
57 | 0 | else if (UNLIKELY (base > 62)) |
58 | 0 | return NULL; |
59 | 1.89k | } |
60 | 0 | else if (base > -2) |
61 | 0 | { |
62 | 0 | base = 10; |
63 | 0 | } |
64 | 0 | else |
65 | 0 | { |
66 | 0 | base = -base; |
67 | 0 | if (UNLIKELY (base > 36)) |
68 | 0 | return NULL; |
69 | 0 | } |
70 | | |
71 | | /* allocate string for the user if necessary */ |
72 | 1.89k | if (res_str == NULL) |
73 | 1.89k | { |
74 | | /* digits, null terminator, possible minus sign */ |
75 | 1.89k | MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base); |
76 | 1.89k | alloc_size += 1 + (x_size<0); |
77 | 1.89k | res_str = __GMP_ALLOCATE_FUNC_TYPE (alloc_size, char); |
78 | 1.89k | } |
79 | 1.89k | return_str = res_str; |
80 | | |
81 | 1.89k | if (x_size < 0) |
82 | 0 | { |
83 | 0 | *res_str++ = '-'; |
84 | 0 | x_size = -x_size; |
85 | 0 | } |
86 | | |
87 | | /* mpn_get_str clobbers its input on non power-of-2 bases */ |
88 | 1.89k | TMP_MARK; |
89 | 1.89k | xp = PTR (x); |
90 | 1.89k | if (! POW2_P (base)) |
91 | 1.89k | { |
92 | 1.89k | xp = TMP_ALLOC_LIMBS (x_size | 1); /* |1 in case x_size==0 */ |
93 | 1.89k | MPN_COPY (xp, PTR (x), x_size); |
94 | 1.89k | } |
95 | | |
96 | 1.89k | str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size); |
97 | 1.89k | ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0)); |
98 | | |
99 | | /* Convert result to printable chars. */ |
100 | 179k | for (i = 0; i < str_size; i++) |
101 | 177k | res_str[i] = num_to_text[(int) res_str[i]]; |
102 | 1.89k | res_str[str_size] = 0; |
103 | | |
104 | 1.89k | TMP_FREE; |
105 | | |
106 | | /* if allocated then resize down to the actual space required */ |
107 | 1.89k | if (alloc_size != 0) |
108 | 1.89k | { |
109 | 1.89k | size_t actual_size = str_size + 1 + (res_str - return_str); |
110 | 1.89k | ASSERT (actual_size == strlen (return_str) + 1); |
111 | 1.89k | __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size, |
112 | 1.89k | char); |
113 | 1.89k | } |
114 | 1.89k | return return_str; |
115 | 1.89k | } |