2022-10-22 18:41:00 +08:00
|
|
|
/* TomsFastMath, a fast ISO C bignum library.
|
2023-01-14 18:28:39 +08:00
|
|
|
*
|
2022-10-22 18:41:00 +08:00
|
|
|
* This project is meant to fill in where LibTomMath
|
|
|
|
* falls short. That is speed ;-)
|
|
|
|
*
|
|
|
|
* This project is public domain and free for all purposes.
|
2023-01-14 18:28:39 +08:00
|
|
|
*
|
2022-10-22 18:41:00 +08:00
|
|
|
* Tom St Denis, tomstdenis@gmail.com
|
|
|
|
*/
|
2023-01-14 18:28:39 +08:00
|
|
|
#include <tfm_private.h>
|
2022-10-22 18:41:00 +08:00
|
|
|
|
|
|
|
int fp_read_radix(fp_int *a, const char *str, int radix)
|
|
|
|
{
|
|
|
|
int y, neg;
|
|
|
|
char ch;
|
|
|
|
|
2023-01-14 18:28:39 +08:00
|
|
|
/* set the integer to the default of zero */
|
|
|
|
fp_zero (a);
|
|
|
|
|
2022-10-22 18:41:00 +08:00
|
|
|
/* make sure the radix is ok */
|
|
|
|
if (radix < 2 || radix > 64) {
|
|
|
|
return FP_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the leading digit is a
|
|
|
|
* minus set the sign to negative.
|
|
|
|
*/
|
|
|
|
if (*str == '-') {
|
|
|
|
++str;
|
|
|
|
neg = FP_NEG;
|
|
|
|
} else {
|
|
|
|
neg = FP_ZPOS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process each digit of the string */
|
|
|
|
while (*str) {
|
|
|
|
/* if the radix < 36 the conversion is case insensitive
|
|
|
|
* this allows numbers like 1AB and 1ab to represent the same value
|
|
|
|
* [e.g. in hex]
|
|
|
|
*/
|
2023-01-14 18:28:39 +08:00
|
|
|
ch = (char) ((radix <= 36) ? toupper ((int)*str) : *str);
|
2022-10-22 18:41:00 +08:00
|
|
|
for (y = 0; y < 64; y++) {
|
|
|
|
if (ch == fp_s_rmap[y]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the char was found in the map
|
|
|
|
* and is less than the given radix add it
|
|
|
|
* to the number, otherwise exit the loop.
|
|
|
|
*/
|
|
|
|
if (y < radix) {
|
|
|
|
fp_mul_d (a, (fp_digit) radix, a);
|
|
|
|
fp_add_d (a, (fp_digit) y, a);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the sign only if a != 0 */
|
|
|
|
if (fp_iszero(a) != FP_YES) {
|
|
|
|
a->sign = neg;
|
|
|
|
}
|
|
|
|
return FP_OKAY;
|
|
|
|
}
|
|
|
|
|
2023-01-14 18:28:39 +08:00
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|