comparison libtommath/bn_mp_read_radix.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
1 #include <tommath.h> 1 #include <tommath_private.h>
2 #ifdef BN_MP_READ_RADIX_C 2 #ifdef BN_MP_READ_RADIX_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * The library is free for all purposes without any express
13 * guarantee it works. 13 * guarantee it works.
14 * 14 *
15 * Tom St Denis, [email protected], http://math.libtomcrypt.com 15 * Tom St Denis, [email protected], http://libtom.org
16 */ 16 */
17 17
18 /* read a string [ASCII] in a given radix */ 18 /* read a string [ASCII] in a given radix */
19 int mp_read_radix (mp_int * a, const char *str, int radix) 19 int mp_read_radix (mp_int * a, const char *str, int radix)
20 { 20 {
23 23
24 /* zero the digit bignum */ 24 /* zero the digit bignum */
25 mp_zero(a); 25 mp_zero(a);
26 26
27 /* make sure the radix is ok */ 27 /* make sure the radix is ok */
28 if (radix < 2 || radix > 64) { 28 if ((radix < 2) || (radix > 64)) {
29 return MP_VAL; 29 return MP_VAL;
30 } 30 }
31 31
32 /* if the leading digit is a 32 /* if the leading digit is a
33 * minus set the sign to negative. 33 * minus set the sign to negative.
41 41
42 /* set the integer to the default of zero */ 42 /* set the integer to the default of zero */
43 mp_zero (a); 43 mp_zero (a);
44 44
45 /* process each digit of the string */ 45 /* process each digit of the string */
46 while (*str) { 46 while (*str != '\0') {
47 /* if the radix < 36 the conversion is case insensitive 47 /* if the radix <= 36 the conversion is case insensitive
48 * this allows numbers like 1AB and 1ab to represent the same value 48 * this allows numbers like 1AB and 1ab to represent the same value
49 * [e.g. in hex] 49 * [e.g. in hex]
50 */ 50 */
51 ch = (char) ((radix < 36) ? toupper (*str) : *str); 51 ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
52 for (y = 0; y < 64; y++) { 52 for (y = 0; y < 64; y++) {
53 if (ch == mp_s_rmap[y]) { 53 if (ch == mp_s_rmap[y]) {
54 break; 54 break;
55 } 55 }
56 } 56 }
71 } 71 }
72 ++str; 72 ++str;
73 } 73 }
74 74
75 /* set the sign only if a != 0 */ 75 /* set the sign only if a != 0 */
76 if (mp_iszero(a) != 1) { 76 if (mp_iszero(a) != MP_YES) {
77 a->sign = neg; 77 a->sign = neg;
78 } 78 }
79 return MP_OKAY; 79 return MP_OKAY;
80 } 80 }
81 #endif 81 #endif
82 82
83 /* $Source: /cvs/libtom/libtommath/bn_mp_read_radix.c,v $ */ 83 /* $Source$ */
84 /* $Revision: 1.4 $ */ 84 /* $Revision$ */
85 /* $Date: 2006/03/31 14:18:44 $ */ 85 /* $Date$ */