github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/set_str.c (about) 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.h" 39 #include "gmp-impl.h" 40 #include "longlong.h" 41 42 #define digit_value_tab __gmp_digit_value_tab 43 44 int 45 mpz_set_str (mpz_ptr x, const char *str, int base) 46 { 47 size_t str_size; 48 char *s, *begs; 49 size_t i; 50 mp_size_t xsize; 51 int c; 52 int negative; 53 const unsigned char *digit_value; 54 TMP_DECL; 55 56 digit_value = digit_value_tab; 57 if (base > 36) 58 { 59 /* For bases > 36, use the collating sequence 60 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. */ 61 digit_value += 208; 62 if (base > 62) 63 return -1; /* too large base */ 64 } 65 66 /* Skip whitespace. */ 67 do 68 c = (unsigned char) *str++; 69 while (isspace (c)); 70 71 negative = 0; 72 if (c == '-') 73 { 74 negative = 1; 75 c = (unsigned char) *str++; 76 } 77 78 if (digit_value[c] >= (base == 0 ? 10 : base)) 79 return -1; /* error if no valid digits */ 80 81 /* If BASE is 0, try to find out the base by looking at the initial 82 characters. */ 83 if (base == 0) 84 { 85 base = 10; 86 if (c == '0') 87 { 88 base = 8; 89 c = (unsigned char) *str++; 90 if (c == 'x' || c == 'X') 91 { 92 base = 16; 93 c = (unsigned char) *str++; 94 } 95 else if (c == 'b' || c == 'B') 96 { 97 base = 2; 98 c = (unsigned char) *str++; 99 } 100 } 101 } 102 103 /* Skip leading zeros and white space. */ 104 while (c == '0' || isspace (c)) 105 c = (unsigned char) *str++; 106 /* Make sure the string does not become empty, mpn_set_str would fail. */ 107 if (c == 0) 108 { 109 SIZ (x) = 0; 110 return 0; 111 } 112 113 TMP_MARK; 114 str_size = strlen (str - 1); 115 s = begs = (char *) TMP_ALLOC (str_size + 1); 116 117 /* Remove spaces from the string and convert the result from ASCII to a 118 byte array. */ 119 for (i = 0; i < str_size; i++) 120 { 121 if (!isspace (c)) 122 { 123 int dig = digit_value[c]; 124 if (dig >= base) 125 { 126 TMP_FREE; 127 return -1; 128 } 129 *s++ = dig; 130 } 131 c = (unsigned char) *str++; 132 } 133 134 str_size = s - begs; 135 136 LIMBS_PER_DIGIT_IN_BASE (xsize, str_size, base); 137 MPZ_REALLOC (x, xsize); 138 139 /* Convert the byte array in base BASE to our bignum format. */ 140 xsize = mpn_set_str (PTR (x), (unsigned char *) begs, str_size, base); 141 SIZ (x) = negative ? -xsize : xsize; 142 143 TMP_FREE; 144 return 0; 145 }