change some parameters in the library to meet the needs of the website httpbin.org
Fork of MiniTLS-GPL by
math/bin/fp_toradix.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_toradix(fp_int *a, char *str, int radix) |
MiniTLS | 0:35aa5be3b78d | 13 | { |
MiniTLS | 0:35aa5be3b78d | 14 | int digs; |
MiniTLS | 0:35aa5be3b78d | 15 | fp_int t; |
MiniTLS | 0:35aa5be3b78d | 16 | fp_digit d; |
MiniTLS | 0:35aa5be3b78d | 17 | char *_s = str; |
MiniTLS | 0:35aa5be3b78d | 18 | |
MiniTLS | 0:35aa5be3b78d | 19 | /* check range of the radix */ |
MiniTLS | 0:35aa5be3b78d | 20 | if (radix < 2 || radix > 64) { |
MiniTLS | 0:35aa5be3b78d | 21 | return FP_VAL; |
MiniTLS | 0:35aa5be3b78d | 22 | } |
MiniTLS | 0:35aa5be3b78d | 23 | |
MiniTLS | 0:35aa5be3b78d | 24 | /* quick out if its zero */ |
MiniTLS | 0:35aa5be3b78d | 25 | if (fp_iszero(a) == 1) { |
MiniTLS | 0:35aa5be3b78d | 26 | *str++ = '0'; |
MiniTLS | 0:35aa5be3b78d | 27 | *str = '\0'; |
MiniTLS | 0:35aa5be3b78d | 28 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 29 | } |
MiniTLS | 0:35aa5be3b78d | 30 | |
MiniTLS | 0:35aa5be3b78d | 31 | fp_init_copy(&t, a); |
MiniTLS | 0:35aa5be3b78d | 32 | |
MiniTLS | 0:35aa5be3b78d | 33 | /* if it is negative output a - */ |
MiniTLS | 0:35aa5be3b78d | 34 | if (t.sign == FP_NEG) { |
MiniTLS | 0:35aa5be3b78d | 35 | ++_s; |
MiniTLS | 0:35aa5be3b78d | 36 | *str++ = '-'; |
MiniTLS | 0:35aa5be3b78d | 37 | t.sign = FP_ZPOS; |
MiniTLS | 0:35aa5be3b78d | 38 | } |
MiniTLS | 0:35aa5be3b78d | 39 | |
MiniTLS | 0:35aa5be3b78d | 40 | digs = 0; |
MiniTLS | 0:35aa5be3b78d | 41 | while (fp_iszero (&t) == FP_NO) { |
MiniTLS | 0:35aa5be3b78d | 42 | fp_div_d (&t, (fp_digit) radix, &t, &d); |
MiniTLS | 0:35aa5be3b78d | 43 | *str++ = fp_s_rmap[d]; |
MiniTLS | 0:35aa5be3b78d | 44 | ++digs; |
MiniTLS | 0:35aa5be3b78d | 45 | } |
MiniTLS | 0:35aa5be3b78d | 46 | |
MiniTLS | 0:35aa5be3b78d | 47 | /* reverse the digits of the string. In this case _s points |
MiniTLS | 0:35aa5be3b78d | 48 | * to the first digit [exluding the sign] of the number] |
MiniTLS | 0:35aa5be3b78d | 49 | */ |
MiniTLS | 0:35aa5be3b78d | 50 | fp_reverse ((unsigned char *)_s, digs); |
MiniTLS | 0:35aa5be3b78d | 51 | |
MiniTLS | 0:35aa5be3b78d | 52 | /* append a NULL so the string is properly terminated */ |
MiniTLS | 0:35aa5be3b78d | 53 | *str = '\0'; |
MiniTLS | 0:35aa5be3b78d | 54 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 55 | } |
MiniTLS | 0:35aa5be3b78d | 56 | |
MiniTLS | 0:35aa5be3b78d | 57 | /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_toradix.c,v $ */ |
MiniTLS | 0:35aa5be3b78d | 58 | /* $Revision: 1.2 $ */ |
MiniTLS | 0:35aa5be3b78d | 59 | /* $Date: 2007/02/27 02:38:44 $ */ |