ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fp_read_radix.c Source File

fp_read_radix.c

00001 /* TomsFastMath, a fast ISO C bignum library.
00002  * 
00003  * This project is meant to fill in where LibTomMath
00004  * falls short.  That is speed ;-)
00005  *
00006  * This project is public domain and free for all purposes.
00007  * 
00008  * Tom St Denis, tomstdenis@gmail.com
00009  */
00010 #include <tfm.h>
00011 
00012 int fp_read_radix(fp_int *a, char *str, int radix)
00013 {
00014   int     y, neg;
00015   char    ch;
00016 
00017   /* make sure the radix is ok */
00018   if (radix < 2 || radix > 64) {
00019     return FP_VAL;
00020   }
00021 
00022   /* if the leading digit is a
00023    * minus set the sign to negative.
00024    */
00025   if (*str == '-') {
00026     ++str;
00027     neg = FP_NEG;
00028   } else {
00029     neg = FP_ZPOS;
00030   }
00031 
00032   /* set the integer to the default of zero */
00033   fp_zero (a);
00034 
00035   /* process each digit of the string */
00036   while (*str) {
00037     /* if the radix < 36 the conversion is case insensitive
00038      * this allows numbers like 1AB and 1ab to represent the same  value
00039      * [e.g. in hex]
00040      */
00041     ch = (char) ((radix < 36) ? toupper (*str) : *str);
00042     for (y = 0; y < 64; y++) {
00043       if (ch == fp_s_rmap[y]) {
00044          break;
00045       }
00046     }
00047 
00048     /* if the char was found in the map
00049      * and is less than the given radix add it
00050      * to the number, otherwise exit the loop.
00051      */
00052     if (y < radix) {
00053       fp_mul_d (a, (fp_digit) radix, a);
00054       fp_add_d (a, (fp_digit) y, a);
00055     } else {
00056       break;
00057     }
00058     ++str;
00059   }
00060 
00061   /* set the sign only if a != 0 */
00062   if (fp_iszero(a) != FP_YES) {
00063      a->sign = neg;
00064   }
00065   return FP_OKAY;
00066 }
00067 
00068 /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_read_radix.c,v $ */
00069 /* $Revision: 1.1 $ */
00070 /* $Date: 2006/12/31 21:25:53 $ */