bn_mp_radix_size.cGo to the documentation of this file.00001 #include <tommath.h> 00002 #ifdef BN_MP_RADIX_SIZE_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 /* returns size of ASCII reprensentation */ 00019 int mp_radix_size (mp_int * a, int radix, int *size) 00020 { 00021 int res, digs; 00022 mp_int t; 00023 mp_digit d; 00024 00025 *size = 0; 00026 00027 /* special case for binary */ 00028 if (radix == 2) { 00029 *size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1; 00030 return MP_OKAY; 00031 } 00032 00033 /* make sure the radix is in range */ 00034 if (radix < 2 || radix > 64) { 00035 return MP_VAL; 00036 } 00037 00038 if (mp_iszero(a) == MP_YES) { 00039 *size = 2; 00040 return MP_OKAY; 00041 } 00042 00043 /* digs is the digit count */ 00044 digs = 0; 00045 00046 /* if it's negative add one for the sign */ 00047 if (a->sign == MP_NEG) { 00048 ++digs; 00049 } 00050 00051 /* init a copy of the input */ 00052 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 00053 return res; 00054 } 00055 00056 /* force temp to positive */ 00057 t.sign = MP_ZPOS; 00058 00059 /* fetch out all of the digits */ 00060 while (mp_iszero (&t) == MP_NO) { 00061 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { 00062 mp_clear (&t); 00063 return res; 00064 } 00065 ++digs; 00066 } 00067 mp_clear (&t); 00068 00069 /* 00070 * return digs + 1, the 1 is for the NULL byte that would be required. 00071 * mp_toradix_n requires a minimum of 3 bytes, so never report less than 00072 * that. 00073 */ 00074 00075 if ( digs >= 2 ) { 00076 *size = digs + 1; 00077 } else { 00078 *size = 3; 00079 } 00080 return MP_OKAY; 00081 } 00082 00083 #endif 00084 00085 /* $Source: /cvsroot/tcl/libtommath/bn_mp_radix_size.c,v $ */ 00086 /* Tom's revision is 1.4 */ 00087 /* $Revision: 1.5 $ */ 00088 /* $Date: 2006/12/01 00:31:32 $ */
Generated on Wed Mar 12 12:18:25 2008 by 1.5.1 |