Mercurial > dropbear
comparison libtommath/bn_mp_read_radix.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | f52919ffd3b1 |
children |
comparison
equal
deleted
inserted
replaced
1691:2d3745d58843 | 1692:1051e4eea25a |
---|---|
1 #include "tommath_private.h" | 1 #include "tommath_private.h" |
2 #ifdef BN_MP_READ_RADIX_C | 2 #ifdef BN_MP_READ_RADIX_C |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 * | 4 /* SPDX-License-Identifier: Unlicense */ |
5 * LibTomMath is a library that provides multiple-precision | 5 |
6 * integer arithmetic as well as number theoretic functionality. | 6 #define MP_TOUPPER(c) ((((c) >= 'a') && ((c) <= 'z')) ? (((c) + 'A') - 'a') : (c)) |
7 * | |
8 * The library was designed directly after the MPI library by | |
9 * Michael Fromberger but has been written from scratch with | |
10 * additional optimizations in place. | |
11 * | |
12 * SPDX-License-Identifier: Unlicense | |
13 */ | |
14 | 7 |
15 /* read a string [ASCII] in a given radix */ | 8 /* read a string [ASCII] in a given radix */ |
16 int mp_read_radix(mp_int *a, const char *str, int radix) | 9 mp_err mp_read_radix(mp_int *a, const char *str, int radix) |
17 { | 10 { |
18 int y, res, neg; | 11 mp_err err; |
12 int y; | |
13 mp_sign neg; | |
19 unsigned pos; | 14 unsigned pos; |
20 char ch; | 15 char ch; |
21 | 16 |
22 /* zero the digit bignum */ | 17 /* zero the digit bignum */ |
23 mp_zero(a); | 18 mp_zero(a); |
24 | 19 |
25 /* make sure the radix is ok */ | 20 /* make sure the radix is ok */ |
44 while (*str != '\0') { | 39 while (*str != '\0') { |
45 /* if the radix <= 36 the conversion is case insensitive | 40 /* if the radix <= 36 the conversion is case insensitive |
46 * this allows numbers like 1AB and 1ab to represent the same value | 41 * this allows numbers like 1AB and 1ab to represent the same value |
47 * [e.g. in hex] | 42 * [e.g. in hex] |
48 */ | 43 */ |
49 ch = (radix <= 36) ? (char)toupper((int)*str) : *str; | 44 ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str; |
50 pos = (unsigned)(ch - '('); | 45 pos = (unsigned)(ch - '('); |
51 if (mp_s_rmap_reverse_sz < pos) { | 46 if (mp_s_rmap_reverse_sz < pos) { |
52 break; | 47 break; |
53 } | 48 } |
54 y = (int)mp_s_rmap_reverse[pos]; | 49 y = (int)mp_s_rmap_reverse[pos]; |
58 * to the number, otherwise exit the loop. | 53 * to the number, otherwise exit the loop. |
59 */ | 54 */ |
60 if ((y == 0xff) || (y >= radix)) { | 55 if ((y == 0xff) || (y >= radix)) { |
61 break; | 56 break; |
62 } | 57 } |
63 if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { | 58 if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { |
64 return res; | 59 return err; |
65 } | 60 } |
66 if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { | 61 if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { |
67 return res; | 62 return err; |
68 } | 63 } |
69 ++str; | 64 ++str; |
70 } | 65 } |
71 | 66 |
72 /* if an illegal character was found, fail. */ | 67 /* if an illegal character was found, fail. */ |
74 mp_zero(a); | 69 mp_zero(a); |
75 return MP_VAL; | 70 return MP_VAL; |
76 } | 71 } |
77 | 72 |
78 /* set the sign only if a != 0 */ | 73 /* set the sign only if a != 0 */ |
79 if (mp_iszero(a) != MP_YES) { | 74 if (!MP_IS_ZERO(a)) { |
80 a->sign = neg; | 75 a->sign = neg; |
81 } | 76 } |
82 return MP_OKAY; | 77 return MP_OKAY; |
83 } | 78 } |
84 #endif | 79 #endif |
85 | |
86 /* ref: HEAD -> master, tag: v1.1.0 */ | |
87 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
88 /* commit time: 2019-01-28 20:32:32 +0100 */ |