| 1 |
#include "tommath_private.h" |
| 2 |
#ifdef BN_MP_READ_RADIX_C |
| 3 |
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
| 4 |
/* SPDX-License-Identifier: Unlicense */ |
| 5 |
|
| 6 |
#define MP_TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (((c) + 'A') - 'a') : (c)) |
| 7 |
|
| 8 |
/* read a string [ASCII] in a given radix */ |
| 9 |
mp_err mp_read_radix(mp_int *a, const char *str, int radix) |
| 10 |
{ |
| 11 |
mp_err err; |
| 12 |
int y; |
| 13 |
mp_sign neg; |
| 14 |
unsigned pos; |
| 15 |
char ch; |
| 16 |
|
| 17 |
/* zero the digit bignum */ |
| 18 |
mp_zero(a); |
| 19 |
|
| 20 |
/* make sure the radix is ok */ |
| 21 |
if ((radix < 2) || (radix > 64)) { |
| 22 |
return MP_VAL; |
| 23 |
} |
| 24 |
|
| 25 |
/* if the leading digit is a |
| 26 |
* minus set the sign to negative. |
| 27 |
*/ |
| 28 |
if (*str == '-') { |
| 29 |
++str; |
| 30 |
neg = MP_NEG; |
| 31 |
} else { |
| 32 |
neg = MP_ZPOS; |
| 33 |
} |
| 34 |
|
| 35 |
/* set the integer to the default of zero */ |
| 36 |
mp_zero(a); |
| 37 |
|
| 38 |
/* process each digit of the string */ |
| 39 |
while (*str != '\0') { |
| 40 |
/* if the radix <= 36 the conversion is case insensitive |
| 41 |
* this allows numbers like 1AB and 1ab to represent the same value |
| 42 |
* [e.g. in hex] |
| 43 |
*/ |
| 44 |
ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str; |
| 45 |
pos = (unsigned)(ch - '('); |
| 46 |
if (mp_s_rmap_reverse_sz < pos) { |
| 47 |
break; |
| 48 |
} |
| 49 |
y = (int)mp_s_rmap_reverse[pos]; |
| 50 |
|
| 51 |
/* if the char was found in the map |
| 52 |
* and is less than the given radix add it |
| 53 |
* to the number, otherwise exit the loop. |
| 54 |
*/ |
| 55 |
if ((y == 0xff) || (y >= radix)) { |
| 56 |
break; |
| 57 |
} |
| 58 |
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { |
| 59 |
return err; |
| 60 |
} |
| 61 |
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { |
| 62 |
return err; |
| 63 |
} |
| 64 |
++str; |
| 65 |
} |
| 66 |
|
| 67 |
/* if an illegal character was found, fail. */ |
| 68 |
if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) { |
| 69 |
mp_zero(a); |
| 70 |
return MP_VAL; |
| 71 |
} |
| 72 |
|
| 73 |
/* set the sign only if a != 0 */ |
| 74 |
if (!MP_IS_ZERO(a)) { |
| 75 |
a->sign = neg; |
| 76 |
} |
| 77 |
return MP_OKAY; |
| 78 |
} |
| 79 |
#endif |