1
1
#include "tommath_private.h"
2
2
#include <string.h>
3
- #include <stdio.h>
4
3
#ifdef BN_MP_TODECIMAL_FAST_C
5
4
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
6
5
/* SPDX-License-Identifier: Unlicense */
@@ -13,12 +12,9 @@ mp_err mp_todecimal_fast_rec(mp_int *number, mp_int *nL, mp_int *shiftL, mp_int
13
12
mp_err err ;
14
13
15
14
if (precalc_array_index < 0 ) {
16
- char * next_piece = calloc (4 , sizeof (char ));
17
- int s_s = left ? snprintf (next_piece , 4 , "%u" , mp_get_i32 (number )) : snprintf (next_piece , 4 , "%03u" ,
18
- mp_get_i32 (number ));
19
- int r_s = (int )strlen (* result );
20
- (* result ) = realloc (* result , (size_t )(r_s + s_s + 2 ));
21
- strcat (* result , next_piece );
15
+ int new_pos = left ? snprintf (* result , 4 , "%u" , mp_get_i32 (number )) : snprintf (* result , 4 , "%03u" ,
16
+ mp_get_i32 (number ));
17
+ * result += new_pos ;
22
18
return MP_OKAY ;
23
19
}
24
20
@@ -70,11 +66,12 @@ mp_err mp_todecimal_fast_rec(mp_int *number, mp_int *nL, mp_int *shiftL, mp_int
70
66
return err ;
71
67
}
72
68
73
- mp_err mp_todecimal_fast (mp_int * number , char * * result )
69
+ mp_err mp_todecimal_fast (mp_int * number , char * result )
74
70
{
75
71
mp_int n , shift , M , M2 , M22 , M4 , M44 ;
76
72
mp_int nL [20 ], shiftL [20 ], mL [20 ];
77
73
mp_err err ;
74
+ char * * result_addr = & result ;
78
75
int precalc_array_index = 1 ;
79
76
80
77
if ((err = mp_init_multi (& M2 , & M22 , & M4 , & M44 , NULL )) != MP_OKAY ) {
@@ -85,7 +82,8 @@ mp_err mp_todecimal_fast(mp_int *number, char **result)
85
82
if ((err = mp_neg (number , number )) != MP_OKAY ) {
86
83
goto LBL_ERR ;
87
84
}
88
- * result [0 ] = '-' ;
85
+ result [0 ] = '-' ;
86
+ result_addr += 1 ;
89
87
}
90
88
if ((err = mp_init_set (& n , (mp_digit )1000 )) != MP_OKAY ) {
91
89
goto LBL_ERR ;
@@ -189,7 +187,7 @@ mp_err mp_todecimal_fast(mp_int *number, char **result)
189
187
precalc_array_index ++ ;
190
188
}
191
189
192
- if ((err = mp_todecimal_fast_rec (number , nL , shiftL , mL , precalc_array_index - 1 , 1 , result )) != MP_OKAY ) {
190
+ if ((err = mp_todecimal_fast_rec (number , nL , shiftL , mL , precalc_array_index - 1 , 1 , result_addr )) != MP_OKAY ) {
193
191
goto LBL_ERR ;
194
192
}
195
193
0 commit comments