Merge pull request #439 from libtom/radix-code-cleanup mp_radix off-by-one error and other related code-cleanup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
diff --git a/demo/test.c b/demo/test.c
index cfe2e5a..f2f5800 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1138,7 +1138,7 @@ static int test_mp_read_radix(void)
DO(mp_read_radix(&a, "123456", 10));
- DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
+ DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
printf(" '123456' a == %s, length = %zu", buf, written);
/* See comment in mp_to_radix.c */
@@ -1153,11 +1153,11 @@ static int test_mp_read_radix(void)
buf, written, mp_error_to_string(err));
*/
DO(mp_read_radix(&a, "-123456", 10));
- DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
+ DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
printf("\r '-123456' a == %s, length = %zu", buf, written);
DO(mp_read_radix(&a, "0", 10));
- DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
+ DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
printf("\r '0' a == %s, length = %zu", buf, written);
while (0) {
@@ -1335,7 +1335,7 @@ static int test_mp_reduce_2k_l(void)
mp_int a, b, c, d;
int cnt;
char buf[4096];
- size_t length[1];
+ size_t length;
DOR(mp_init_multi(&a, &b, NULL));
/* test the mp_reduce_2k_l code */
# if LTM_DEMO_TEST_REDUCE_2K_L == 1
@@ -1353,9 +1353,8 @@ static int test_mp_reduce_2k_l(void)
# else
# error oops
# endif
- *length = sizeof(buf);
- DO(mp_to_radix(&a, buf, length, 10));
- printf("\n\np==%s, length = %zu\n", buf, *length);
+ DO(mp_to_radix(&a, buf, sizeof(buf), &length, 10));
+ printf("\n\np==%s, length = %zu\n", buf, length);
/* now mp_reduce_is_2k_l() should return */
if (mp_reduce_is_2k_l(&a) != 1) {
printf("mp_reduce_is_2k_l() return 0, should be 1\n");
diff --git a/mp_fread.c b/mp_fread.c
index 005c62a..bd336ce 100644
--- a/mp_fread.c
+++ b/mp_fread.c
@@ -35,14 +35,14 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
uint8_t y;
unsigned pos;
ch = (radix <= 36) ? MP_TOUPPER(ch) : ch;
- pos = (unsigned)(ch - (int)'(');
- if (MP_RMAP_REVERSE_SIZE < pos) {
+ pos = (unsigned)(ch - (int)'+');
+ if (MP_RMAP_REVERSE_SIZE <= pos) {
break;
}
y = s_mp_rmap_reverse[pos];
- if ((y == 0xff) || (y >= radix)) {
+ if (y >= radix) {
break;
}
diff --git a/mp_radix_smap.c b/mp_radix_smap.c
index 678e806..73fd7cf 100644
--- a/mp_radix_smap.c
+++ b/mp_radix_smap.c
@@ -6,17 +6,14 @@
/* chars used in radix conversions */
const char s_mp_rmap[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
const uint8_t s_mp_rmap_reverse[] = {
- 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
- 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */
- 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */
- 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */
- 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */
- 0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */
- 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */
- 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */
- 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */
- 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */
+ 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04, /* +,-./01234 */
+ 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, /* 56789:;<=> */
+ 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, /* ?@ABCDEFGH */
+ 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, /* IJKLMNOPQR */
+ 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0xff, 0xff, /* STUVWXYZ[\ */
+ 0xff, 0xff, 0xff, 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, /* ]^_`abcdef */
+ 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, /* ghijklmnop */
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d /* qrstuvwxyz */
};
MP_STATIC_ASSERT(correct_rmap_reverse_size, sizeof(s_mp_rmap_reverse) == MP_RMAP_REVERSE_SIZE)
#endif
diff --git a/mp_read_radix.c b/mp_read_radix.c
index df8059a..500f2d9 100644
--- a/mp_read_radix.c
+++ b/mp_read_radix.c
@@ -33,8 +33,8 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
*/
uint8_t y;
char ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str;
- unsigned pos = (unsigned)(ch - '(');
- if (MP_RMAP_REVERSE_SIZE < pos) {
+ unsigned pos = (unsigned)(ch - '+');
+ if (MP_RMAP_REVERSE_SIZE <= pos) {
break;
}
y = s_mp_rmap_reverse[pos];
@@ -43,7 +43,7 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
* and is less than the given radix add it
* to the number, otherwise exit the loop.
*/
- if ((y == 0xff) || (y >= radix)) {
+ if (y >= radix) {
break;
}
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
diff --git a/tommath_private.h b/tommath_private.h
index 9a3e2f8..90da269 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -193,7 +193,7 @@ MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
-#define MP_RMAP_REVERSE_SIZE 88u
+#define MP_RMAP_REVERSE_SIZE 80u
extern MP_PRIVATE const char s_mp_rmap[];
extern MP_PRIVATE const uint8_t s_mp_rmap_reverse[];
extern MP_PRIVATE const mp_digit s_mp_prime_tab[];