11#include "tommath_private.h"
22#include <string.h>
3- #include <stdio.h>
43#ifdef BN_MP_TODECIMAL_FAST_C
54/* LibTomMath, multiple-precision integer library -- Tom St Denis */
65/* 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
1312 mp_err err ;
1413
1514 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 ;
2218 return MP_OKAY ;
2319 }
2420
@@ -70,11 +66,12 @@ mp_err mp_todecimal_fast_rec(mp_int *number, mp_int *nL, mp_int *shiftL, mp_int
7066 return err ;
7167}
7268
73- mp_err mp_todecimal_fast (mp_int * number , char * * result )
69+ mp_err mp_todecimal_fast (mp_int * number , char * result )
7470{
7571 mp_int n , shift , M , M2 , M22 , M4 , M44 ;
7672 mp_int nL [20 ], shiftL [20 ], mL [20 ];
7773 mp_err err ;
74+ char * * result_addr = & result ;
7875 int precalc_array_index = 1 ;
7976
8077 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)
8582 if ((err = mp_neg (number , number )) != MP_OKAY ) {
8683 goto LBL_ERR ;
8784 }
88- * result [0 ] = '-' ;
85+ result [0 ] = '-' ;
86+ result_addr += 1 ;
8987 }
9088 if ((err = mp_init_set (& n , (mp_digit )1000 )) != MP_OKAY ) {
9189 goto LBL_ERR ;
@@ -189,7 +187,7 @@ mp_err mp_todecimal_fast(mp_int *number, char **result)
189187 precalc_array_index ++ ;
190188 }
191189
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 ) {
193191 goto LBL_ERR ;
194192 }
195193
0 commit comments