Skip to content

Commit a919731

Browse files
committed
make mp_to_radix return the count of characters of the converted number
1 parent d6a8de5 commit a919731

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

bn_deprecated.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,20 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
232232
#ifdef BN_MP_TORADIX_N_C
233233
mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
234234
{
235+
size_t length[1];
235236
if (maxlen < 0) {
236237
return MP_VAL;
237238
}
238-
return mp_to_radix(a, str, (size_t)maxlen, radix);
239+
*length = (size_t)maxlen;
240+
return mp_to_radix(a, str, length, radix);
239241
}
240242
#endif
241243
#ifdef BN_MP_TORADIX_C
242244
mp_err mp_toradix(const mp_int *a, char *str, int radix)
243245
{
244-
return mp_to_radix(a, str, SIZE_MAX, radix);
246+
size_t length[1];
247+
*length = SIZE_MAX;
248+
return mp_to_radix(a, str, length, radix);
245249
}
246250
#endif
247251
#endif

bn_mp_fwrite.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
99
char *buf;
1010
mp_err err;
1111
int len;
12+
/* TODO: that's a bit awkward */
13+
size_t length[1];
1214

1315
if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
1416
return err;
1517
}
1618

17-
buf = (char *) MP_MALLOC((size_t)len);
19+
*length = (size_t)len;
20+
21+
buf = (char *) MP_MALLOC(*length);
1822
if (buf == NULL) {
1923
return MP_MEM;
2024
}
21-
22-
if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) {
25+
if ((err = mp_to_radix(a, buf, length, radix)) != MP_OKAY) {
2326
goto LBL_ERR;
2427
}
25-
26-
if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) {
28+
if (fwrite(buf, *length, 1uL, stream) != 1uL) {
2729
err = MP_ERR;
2830
goto LBL_ERR;
2931
}
3032
err = MP_OKAY;
3133

3234
LBL_ERR:
33-
MP_FREE_BUFFER(buf, (size_t)len);
35+
MP_FREE_BUFFER(buf, *length);
3436
return err;
3537
}
3638
#endif

bn_mp_to_radix.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Stores upto maxlen-1 chars and always a NULL byte
99
*/
10-
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
10+
mp_err mp_to_radix(const mp_int *a, char *str, size_t *maxlen, int radix)
1111
{
1212
int digs;
1313
mp_err err;
@@ -16,7 +16,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
1616
char *_s = str;
1717

1818
/* check range of the maxlen, radix */
19-
if ((maxlen < 2u) || (radix < 2) || (radix > 64)) {
19+
if ((*maxlen < 2u) || (radix < 2) || (radix > 64)) {
2020
return MP_VAL;
2121
}
2222

@@ -46,7 +46,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
4646

4747
digs = 0;
4848
while (!MP_IS_ZERO(&t)) {
49-
if (--maxlen < 1u) {
49+
if (--(*maxlen) < 1u) {
5050
/* no more room */
5151
err = MP_VAL;
5252
break;
@@ -65,6 +65,8 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
6565

6666
/* append a NULL so the string is properly terminated */
6767
*str = '\0';
68+
digs++;
69+
*maxlen = digs;
6870

6971
LBL_ERR:
7072
mp_clear(&t);

demo/test.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ static int test_mp_montgomery_reduce(void)
11881188
mp_digit mp;
11891189
int ix, i, n;
11901190
char buf[4096];
1191+
size_t length[1];
11911192

11921193
mp_int a, b, c, d, e;
11931194
if (mp_init_multi(&a, &b, &c, &d, &e, NULL)!= MP_OKAY) {
@@ -1221,10 +1222,17 @@ static int test_mp_montgomery_reduce(void)
12211222
if (mp_cmp(&c, &d) != MP_EQ) {
12221223
/* *INDENT-OFF* */
12231224
printf("d = e mod a, c = e MOD a\n");
1225+
*length = sizeof(buf);
1226+
mp_to_radix(&a, buf, length, 10); printf("a = %s, length = %zu\n", buf, *length);
1227+
mp_to_radix(&e, buf, length, 10); printf("e = %s, length = %zu\n", buf, *length);
1228+
mp_to_radix(&d, buf, length, 10); printf("d = %s, length = %zu\n", buf, *length);
1229+
mp_to_radix(&c, buf, length, 10); printf("c = %s, length = %zu\n", buf, *length);
1230+
/*
12241231
mp_to_decimal(&a, buf, sizeof(buf)); printf("a = %s\n", buf);
12251232
mp_to_decimal(&e, buf, sizeof(buf)); printf("e = %s\n", buf);
12261233
mp_to_decimal(&d, buf, sizeof(buf)); printf("d = %s\n", buf);
12271234
mp_to_decimal(&c, buf, sizeof(buf)); printf("c = %s\n", buf);
1235+
*/
12281236
printf("compare no compare!\n"); goto LBL_ERR;
12291237
/* *INDENT-ON* */
12301238
}
@@ -1250,26 +1258,31 @@ static int test_mp_montgomery_reduce(void)
12501258
static int test_mp_read_radix(void)
12511259
{
12521260
char buf[4096];
1261+
size_t length[1];
12531262

12541263
mp_int a;
12551264
if (mp_init_multi(&a, NULL)!= MP_OKAY) {
12561265
return EXIT_FAILURE;
12571266
}
12581267

12591268
mp_read_radix(&a, "123456", 10);
1260-
mp_to_radix(&a, buf, 3, 10);
1261-
printf("a == %s\n", buf);
1262-
mp_to_radix(&a, buf, 4, 10);
1263-
printf("a == %s\n", buf);
1264-
mp_to_radix(&a, buf, 30, 10);
1265-
printf("a == %s\n", buf);
1269+
*length = 3u;
1270+
mp_to_radix(&a, buf, length, 10);
1271+
printf("a == %s, length = %zu\n", buf, *length);
1272+
*length = 4u;
1273+
mp_to_radix(&a, buf, length, 10);
1274+
printf("a == %s, length = %zu\n", buf, *length);
1275+
*length = 30u;
1276+
mp_to_radix(&a, buf, length, 10);
1277+
printf("a == %s, length = %zu\n", buf, *length);
12661278

12671279
while (0) {
12681280
char *s = fgets(buf, sizeof(buf), stdin);
12691281
if (s != buf) break;
12701282
mp_read_radix(&a, buf, 10);
12711283
mp_prime_next_prime(&a, 5, 1);
1272-
mp_to_radix(&a, buf, sizeof(buf), 10);
1284+
*length = sizeof(buf);
1285+
mp_to_radix(&a, buf, length, 10);
12731286
printf("%s, %lu\n", buf, (unsigned long)a.dp[0] & 3uL);
12741287
}
12751288

@@ -1444,6 +1457,7 @@ static int test_mp_reduce_2k_l(void)
14441457
mp_int a, b, c, d;
14451458
int cnt;
14461459
char buf[4096];
1460+
size_t length[1];
14471461
if (mp_init_multi(&a, &b, NULL)!= MP_OKAY) {
14481462
return EXIT_FAILURE;
14491463
}
@@ -1463,9 +1477,9 @@ static int test_mp_reduce_2k_l(void)
14631477
# else
14641478
# error oops
14651479
# endif
1466-
1467-
mp_to_decimal(&a, buf, sizeof(buf));
1468-
printf("\n\np==%s\n", buf);
1480+
*length = sizeof(buf);
1481+
mp_to_radix(&a, buf, length, 10);
1482+
printf("\n\np==%s, length = %zu\n", buf, *length);
14691483
/* now mp_reduce_is_2k_l() should return */
14701484
if (mp_reduce_is_2k_l(&a) != 1) {
14711485
printf("mp_reduce_is_2k_l() return 0, should be 1\n");

tommath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outl
709709
mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
710710
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
711711
MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
712-
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) MP_WUR;
712+
mp_err mp_to_radix(const mp_int *a, char *str, size_t *maxlen, int radix) MP_WUR;
713713
mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
714714

715715
#ifndef MP_NO_FILE

0 commit comments

Comments
 (0)