Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_set_str(mp_dest, string, base) -- Convert the \0-terminated |
2 | | string STRING in base BASE to multiple precision integer in |
3 | | MP_DEST. Allow white space in the string. If BASE == 0 determine |
4 | | the base in the C standard way, i.e. 0xhh...h means base 16, |
5 | | 0oo...o means base 8, otherwise assume base 10. |
6 | | |
7 | | Copyright 1991, 1993, 1994, 1996-1998, 2000-2003, 2005, 2011-2013 Free Software |
8 | | 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> |
37 | | #include <ctype.h> |
38 | | #include "gmp-impl.h" |
39 | | #include "longlong.h" |
40 | | |
41 | 0 | #define digit_value_tab __gmp_digit_value_tab |
42 | | |
43 | | int |
44 | | mpz_set_str (mpz_ptr x, const char *str, int base) |
45 | 0 | { |
46 | 0 | size_t str_size; |
47 | 0 | char *s, *begs; |
48 | 0 | size_t i; |
49 | 0 | mp_size_t xsize; |
50 | 0 | int c; |
51 | 0 | int negative; |
52 | 0 | const unsigned char *digit_value; |
53 | 0 | TMP_DECL; |
54 | |
|
55 | 0 | digit_value = digit_value_tab; |
56 | 0 | if (base > 36) |
57 | 0 | { |
58 | | /* For bases > 36, use the collating sequence |
59 | | 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */ |
60 | 0 | digit_value += 208; |
61 | 0 | if (UNLIKELY (base > 62)) |
62 | 0 | return -1; /* too large base */ |
63 | 0 | } |
64 | | |
65 | | /* Skip whitespace. */ |
66 | 0 | do |
67 | 0 | c = (unsigned char) *str++; |
68 | 0 | while (isspace (c)); |
69 | |
|
70 | 0 | negative = 0; |
71 | 0 | if (c == '-') |
72 | 0 | { |
73 | 0 | negative = 1; |
74 | 0 | c = (unsigned char) *str++; |
75 | 0 | } |
76 | |
|
77 | 0 | if (digit_value[c] >= (base == 0 ? 10 : base)) |
78 | 0 | return -1; /* error if no valid digits */ |
79 | | |
80 | | /* If BASE is 0, try to find out the base by looking at the initial |
81 | | characters. */ |
82 | 0 | if (base == 0) |
83 | 0 | { |
84 | 0 | base = 10; |
85 | 0 | if (c == '0') |
86 | 0 | { |
87 | 0 | base = 8; |
88 | 0 | c = (unsigned char) *str++; |
89 | 0 | if (c == 'x' || c == 'X') |
90 | 0 | { |
91 | 0 | base = 16; |
92 | 0 | c = (unsigned char) *str++; |
93 | 0 | } |
94 | 0 | else if (c == 'b' || c == 'B') |
95 | 0 | { |
96 | 0 | base = 2; |
97 | 0 | c = (unsigned char) *str++; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | /* Skip leading zeros and white space. */ |
103 | 0 | while (c == '0' || isspace (c)) |
104 | 0 | c = (unsigned char) *str++; |
105 | | /* Make sure the string does not become empty, mpn_set_str would fail. */ |
106 | 0 | if (c == 0) |
107 | 0 | { |
108 | 0 | SIZ (x) = 0; |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | 0 | TMP_MARK; |
113 | 0 | str_size = strlen (str - 1); |
114 | 0 | s = begs = (char *) TMP_ALLOC (str_size + 1); |
115 | | |
116 | | /* Remove spaces from the string and convert the result from ASCII to a |
117 | | byte array. */ |
118 | 0 | for (i = 0; i < str_size; i++) |
119 | 0 | { |
120 | 0 | if (!isspace (c)) |
121 | 0 | { |
122 | 0 | int dig = digit_value[c]; |
123 | 0 | if (UNLIKELY (dig >= base)) |
124 | 0 | { |
125 | 0 | TMP_FREE; |
126 | 0 | return -1; |
127 | 0 | } |
128 | 0 | *s++ = dig; |
129 | 0 | } |
130 | 0 | c = (unsigned char) *str++; |
131 | 0 | } |
132 | | |
133 | 0 | str_size = s - begs; |
134 | |
|
135 | 0 | LIMBS_PER_DIGIT_IN_BASE (xsize, str_size, base); |
136 | 0 | MPZ_NEWALLOC (x, xsize); |
137 | | |
138 | | /* Convert the byte array in base BASE to our bignum format. */ |
139 | 0 | xsize = mpn_set_str (PTR (x), (unsigned char *) begs, str_size, base); |
140 | 0 | SIZ (x) = negative ? -xsize : xsize; |
141 | |
|
142 | 0 | TMP_FREE; |
143 | 0 | return 0; |
144 | 0 | } |