comparison tomsfastmath/src/bin/fp_read_radix.c @ 643:a362b62d38b2 dropbear-tfm

Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a with Makefile.in renamed
author Matt Johnston <matt@ucc.asn.au>
date Wed, 23 Nov 2011 18:10:20 +0700
parents
children
comparison
equal deleted inserted replaced
642:33fd2f3499d2 643:a362b62d38b2
1 /* TomsFastMath, a fast ISO C bignum library.
2 *
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
5 *
6 * This project is public domain and free for all purposes.
7 *
8 * Tom St Denis, [email protected]
9 */
10 #include <tfm.h>
11
12 int fp_read_radix(fp_int *a, char *str, int radix)
13 {
14 int y, neg;
15 char ch;
16
17 /* make sure the radix is ok */
18 if (radix < 2 || radix > 64) {
19 return FP_VAL;
20 }
21
22 /* if the leading digit is a
23 * minus set the sign to negative.
24 */
25 if (*str == '-') {
26 ++str;
27 neg = FP_NEG;
28 } else {
29 neg = FP_ZPOS;
30 }
31
32 /* set the integer to the default of zero */
33 fp_zero (a);
34
35 /* process each digit of the string */
36 while (*str) {
37 /* if the radix < 36 the conversion is case insensitive
38 * this allows numbers like 1AB and 1ab to represent the same value
39 * [e.g. in hex]
40 */
41 ch = (char) ((radix < 36) ? toupper ((int)*str) : *str);
42 for (y = 0; y < 64; y++) {
43 if (ch == fp_s_rmap[y]) {
44 break;
45 }
46 }
47
48 /* if the char was found in the map
49 * and is less than the given radix add it
50 * to the number, otherwise exit the loop.
51 */
52 if (y < radix) {
53 fp_mul_d (a, (fp_digit) radix, a);
54 fp_add_d (a, (fp_digit) y, a);
55 } else {
56 break;
57 }
58 ++str;
59 }
60
61 /* set the sign only if a != 0 */
62 if (fp_iszero(a) != FP_YES) {
63 a->sign = neg;
64 }
65 return FP_OKAY;
66 }
67
68 /* $Source$ */
69 /* $Revision$ */
70 /* $Date$ */