bn_mp_read_radix.cGo to the documentation of this file.00001 #include <tommath.h> 00002 #ifdef BN_MP_READ_RADIX_C 00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis 00004 * 00005 * LibTomMath is a library that provides multiple-precision 00006 * integer arithmetic as well as number theoretic functionality. 00007 * 00008 * The library was designed directly after the MPI library by 00009 * Michael Fromberger but has been written from scratch with 00010 * additional optimizations in place. 00011 * 00012 * The library is free for all purposes without any express 00013 * guarantee it works. 00014 * 00015 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com 00016 */ 00017 00018 /* read a string [ASCII] in a given radix */ 00019 int mp_read_radix (mp_int * a, const char *str, int radix) 00020 { 00021 int y, res, neg; 00022 char ch; 00023 00024 /* zero the digit bignum */ 00025 mp_zero(a); 00026 00027 /* make sure the radix is ok */ 00028 if (radix < 2 || radix > 64) { 00029 return MP_VAL; 00030 } 00031 00032 /* if the leading digit is a 00033 * minus set the sign to negative. 00034 */ 00035 if (*str == '-') { 00036 ++str; 00037 neg = MP_NEG; 00038 } else { 00039 neg = MP_ZPOS; 00040 } 00041 00042 /* set the integer to the default of zero */ 00043 mp_zero (a); 00044 00045 /* process each digit of the string */ 00046 while (*str) { 00047 /* if the radix < 36 the conversion is case insensitive 00048 * this allows numbers like 1AB and 1ab to represent the same value 00049 * [e.g. in hex] 00050 */ 00051 ch = (char) ((radix < 36) ? toupper (*str) : *str); 00052 for (y = 0; y < 64; y++) { 00053 if (ch == mp_s_rmap[y]) { 00054 break; 00055 } 00056 } 00057 00058 /* if the char was found in the map 00059 * and is less than the given radix add it 00060 * to the number, otherwise exit the loop. 00061 */ 00062 if (y < radix) { 00063 if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { 00064 return res; 00065 } 00066 if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) { 00067 return res; 00068 } 00069 } else { 00070 break; 00071 } 00072 ++str; 00073 } 00074 00075 /* if an illegal character was found, fail. */ 00076 00077 if ( *str != '\0' ) { 00078 mp_zero( a ); 00079 return MP_VAL; 00080 } 00081 00082 /* set the sign only if a != 0 */ 00083 if (mp_iszero(a) != 1) { 00084 a->sign = neg; 00085 } 00086 return MP_OKAY; 00087 } 00088 #endif 00089 00090 /* $Source: /cvsroot/tcl/libtommath/bn_mp_read_radix.c,v $ */ 00091 /* Tom's revision is 1.4. */ 00092 /* $Revision: 1.5 $ */ 00093 /* $Date: 2006/12/01 00:31:32 $ */ 00094
Generated on Wed Mar 12 12:18:25 2008 by 1.5.1 |