change some parameters in the library to meet the needs of the website httpbin.org
Fork of MiniTLS-GPL by
math/bin/fp_read_radix.c@5:95f70ebfe61f, 2015-02-06 (annotated)
- Committer:
- shiyilei
- Date:
- Fri Feb 06 06:17:33 2015 +0000
- Revision:
- 5:95f70ebfe61f
- Parent:
- 0:35aa5be3b78d
change some parameters in the library to meet the needs of httpbin.org
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MiniTLS | 0:35aa5be3b78d | 1 | /* TomsFastMath, a fast ISO C bignum library. |
MiniTLS | 0:35aa5be3b78d | 2 | * |
MiniTLS | 0:35aa5be3b78d | 3 | * This project is meant to fill in where LibTomMath |
MiniTLS | 0:35aa5be3b78d | 4 | * falls short. That is speed ;-) |
MiniTLS | 0:35aa5be3b78d | 5 | * |
MiniTLS | 0:35aa5be3b78d | 6 | * This project is public domain and free for all purposes. |
MiniTLS | 0:35aa5be3b78d | 7 | * |
MiniTLS | 0:35aa5be3b78d | 8 | * Tom St Denis, tomstdenis@gmail.com |
MiniTLS | 0:35aa5be3b78d | 9 | */ |
MiniTLS | 0:35aa5be3b78d | 10 | #include <tfm.h> |
MiniTLS | 0:35aa5be3b78d | 11 | |
MiniTLS | 0:35aa5be3b78d | 12 | int fp_read_radix(fp_int *a, char *str, int radix) |
MiniTLS | 0:35aa5be3b78d | 13 | { |
MiniTLS | 0:35aa5be3b78d | 14 | int y, neg; |
MiniTLS | 0:35aa5be3b78d | 15 | char ch; |
MiniTLS | 0:35aa5be3b78d | 16 | |
MiniTLS | 0:35aa5be3b78d | 17 | /* make sure the radix is ok */ |
MiniTLS | 0:35aa5be3b78d | 18 | if (radix < 2 || radix > 64) { |
MiniTLS | 0:35aa5be3b78d | 19 | return FP_VAL; |
MiniTLS | 0:35aa5be3b78d | 20 | } |
MiniTLS | 0:35aa5be3b78d | 21 | |
MiniTLS | 0:35aa5be3b78d | 22 | /* if the leading digit is a |
MiniTLS | 0:35aa5be3b78d | 23 | * minus set the sign to negative. |
MiniTLS | 0:35aa5be3b78d | 24 | */ |
MiniTLS | 0:35aa5be3b78d | 25 | if (*str == '-') { |
MiniTLS | 0:35aa5be3b78d | 26 | ++str; |
MiniTLS | 0:35aa5be3b78d | 27 | neg = FP_NEG; |
MiniTLS | 0:35aa5be3b78d | 28 | } else { |
MiniTLS | 0:35aa5be3b78d | 29 | neg = FP_ZPOS; |
MiniTLS | 0:35aa5be3b78d | 30 | } |
MiniTLS | 0:35aa5be3b78d | 31 | |
MiniTLS | 0:35aa5be3b78d | 32 | /* set the integer to the default of zero */ |
MiniTLS | 0:35aa5be3b78d | 33 | fp_zero (a); |
MiniTLS | 0:35aa5be3b78d | 34 | |
MiniTLS | 0:35aa5be3b78d | 35 | /* process each digit of the string */ |
MiniTLS | 0:35aa5be3b78d | 36 | while (*str) { |
MiniTLS | 0:35aa5be3b78d | 37 | /* if the radix < 36 the conversion is case insensitive |
MiniTLS | 0:35aa5be3b78d | 38 | * this allows numbers like 1AB and 1ab to represent the same value |
MiniTLS | 0:35aa5be3b78d | 39 | * [e.g. in hex] |
MiniTLS | 0:35aa5be3b78d | 40 | */ |
MiniTLS | 0:35aa5be3b78d | 41 | ch = (char) ((radix < 36) ? toupper (*str) : *str); |
MiniTLS | 0:35aa5be3b78d | 42 | for (y = 0; y < 64; y++) { |
MiniTLS | 0:35aa5be3b78d | 43 | if (ch == fp_s_rmap[y]) { |
MiniTLS | 0:35aa5be3b78d | 44 | break; |
MiniTLS | 0:35aa5be3b78d | 45 | } |
MiniTLS | 0:35aa5be3b78d | 46 | } |
MiniTLS | 0:35aa5be3b78d | 47 | |
MiniTLS | 0:35aa5be3b78d | 48 | /* if the char was found in the map |
MiniTLS | 0:35aa5be3b78d | 49 | * and is less than the given radix add it |
MiniTLS | 0:35aa5be3b78d | 50 | * to the number, otherwise exit the loop. |
MiniTLS | 0:35aa5be3b78d | 51 | */ |
MiniTLS | 0:35aa5be3b78d | 52 | if (y < radix) { |
MiniTLS | 0:35aa5be3b78d | 53 | fp_mul_d (a, (fp_digit) radix, a); |
MiniTLS | 0:35aa5be3b78d | 54 | fp_add_d (a, (fp_digit) y, a); |
MiniTLS | 0:35aa5be3b78d | 55 | } else { |
MiniTLS | 0:35aa5be3b78d | 56 | break; |
MiniTLS | 0:35aa5be3b78d | 57 | } |
MiniTLS | 0:35aa5be3b78d | 58 | ++str; |
MiniTLS | 0:35aa5be3b78d | 59 | } |
MiniTLS | 0:35aa5be3b78d | 60 | |
MiniTLS | 0:35aa5be3b78d | 61 | /* set the sign only if a != 0 */ |
MiniTLS | 0:35aa5be3b78d | 62 | if (fp_iszero(a) != FP_YES) { |
MiniTLS | 0:35aa5be3b78d | 63 | a->sign = neg; |
MiniTLS | 0:35aa5be3b78d | 64 | } |
MiniTLS | 0:35aa5be3b78d | 65 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 66 | } |
MiniTLS | 0:35aa5be3b78d | 67 | |
MiniTLS | 0:35aa5be3b78d | 68 | /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_read_radix.c,v $ */ |
MiniTLS | 0:35aa5be3b78d | 69 | /* $Revision: 1.1 $ */ |
MiniTLS | 0:35aa5be3b78d | 70 | /* $Date: 2006/12/31 21:25:53 $ */ |