comparison libtommath/bn_mp_read_radix.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 7
18 /* read a string [ASCII] in a given radix */ 8 /* read a string [ASCII] in a given radix */
19 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)
20 { 10 {
21 int y, res, neg; 11 mp_err err;
22 char ch; 12 int y;
13 mp_sign neg;
14 unsigned pos;
15 char ch;
23 16
24 /* zero the digit bignum */ 17 /* zero the digit bignum */
25 mp_zero(a); 18 mp_zero(a);
26 19
27 /* make sure the radix is ok */ 20 /* make sure the radix is ok */
28 if ((radix < 2) || (radix > 64)) { 21 if ((radix < 2) || (radix > 64)) {
29 return MP_VAL; 22 return MP_VAL;
30 } 23 }
31 24
32 /* if the leading digit is a 25 /* if the leading digit is a
33 * minus set the sign to negative. 26 * minus set the sign to negative.
34 */ 27 */
35 if (*str == '-') { 28 if (*str == '-') {
36 ++str; 29 ++str;
37 neg = MP_NEG; 30 neg = MP_NEG;
38 } else { 31 } else {
39 neg = MP_ZPOS; 32 neg = MP_ZPOS;
40 } 33 }
41 34
42 /* set the integer to the default of zero */ 35 /* set the integer to the default of zero */
43 mp_zero (a); 36 mp_zero(a);
44 37
45 /* process each digit of the string */ 38 /* process each digit of the string */
46 while (*str != '\0') { 39 while (*str != '\0') {
47 /* if the radix <= 36 the conversion is case insensitive 40 /* if the radix <= 36 the conversion is case insensitive
48 * 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
49 * [e.g. in hex] 42 * [e.g. in hex]
50 */ 43 */
51 ch = (radix <= 36) ? (char)toupper((int)*str) : *str; 44 ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str;
52 for (y = 0; y < 64; y++) { 45 pos = (unsigned)(ch - '(');
53 if (ch == mp_s_rmap[y]) { 46 if (mp_s_rmap_reverse_sz < pos) {
54 break; 47 break;
55 } 48 }
56 } 49 y = (int)mp_s_rmap_reverse[pos];
57 50
58 /* if the char was found in the map 51 /* if the char was found in the map
59 * and is less than the given radix add it 52 * and is less than the given radix add it
60 * to the number, otherwise exit the loop. 53 * to the number, otherwise exit the loop.
61 */ 54 */
62 if (y < radix) { 55 if ((y == 0xff) || (y >= radix)) {
63 if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { 56 break;
64 return res;
65 } 57 }
66 if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) { 58 if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
67 return res; 59 return err;
68 } 60 }
69 } else { 61 if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
70 break; 62 return err;
71 } 63 }
72 ++str; 64 ++str;
73 } 65 }
74 66
75 /* set the sign only if a != 0 */ 67 /* if an illegal character was found, fail. */
76 if (mp_iszero(a) != MP_YES) { 68 if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) {
77 a->sign = neg; 69 mp_zero(a);
78 } 70 return MP_VAL;
79 return MP_OKAY; 71 }
72
73 /* set the sign only if a != 0 */
74 if (!MP_IS_ZERO(a)) {
75 a->sign = neg;
76 }
77 return MP_OKAY;
80 } 78 }
81 #endif 79 #endif
82
83 /* ref: $Format:%D$ */
84 /* git commit: $Format:%H$ */
85 /* commit time: $Format:%ai$ */