Merge pull request #439 from libtom/radix-code-cleanup
mp_radix off-by-one error and other related code-cleanup
This commit is contained in:
commit
c893d217aa
13
demo/test.c
13
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");
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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[];
|
||||
|
Loading…
Reference in New Issue
Block a user