@@ -6,22 +6,19 @@ typedef int (*basic_big_unary_func)(mbedtls_mpi* result, const mbedtls_mpi* a);
66
77static void basic_big_error (struct basic_ctx * ctx , const char * name , int rc )
88{
9- if (rc == MBEDTLS_ERR_MPI_INVALID_CHARACTER ) {
10- tokenizer_error_printf (ctx , "%s invalid character" , name );
11- return ;
9+ switch (rc ) {
10+ case MBEDTLS_ERR_MPI_INVALID_CHARACTER :
11+ tokenizer_error_printf (ctx , "%s invalid character" , name );
12+ break ;
13+ case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO :
14+ tokenizer_error_printf (ctx , "%s divide by zero" , name );
15+ break ;
16+ case MBEDTLS_ERR_MPI_NEGATIVE_VALUE :
17+ tokenizer_error_printf (ctx , "%s negative value" , name );
18+ break ;
19+ default :
20+ tokenizer_error_printf (ctx , "%s failed" , name );
1221 }
13-
14- if (rc == MBEDTLS_ERR_MPI_DIVISION_BY_ZERO ) {
15- tokenizer_error_printf (ctx , "%s divide by zero" , name );
16- return ;
17- }
18-
19- if (rc == MBEDTLS_ERR_MPI_NEGATIVE_VALUE ) {
20- tokenizer_error_printf (ctx , "%s negative value" , name );
21- return ;
22- }
23-
24- tokenizer_error_printf (ctx , "%s failed" , name );
2522}
2623
2724static int basic_big_read (struct basic_ctx * ctx , const char * name , mbedtls_mpi * value , const char * text )
@@ -38,31 +35,32 @@ static int basic_big_read(struct basic_ctx* ctx, const char* name, mbedtls_mpi*
3835static char * basic_big_write (struct basic_ctx * ctx , const char * name , mbedtls_mpi * value )
3936{
4037 size_t size = 32 ;
41-
42- for (;;) {
43- char * buffer = buddy_malloc (ctx -> allocator , size );
44- if (!buffer ) {
38+ char * buffer = buddy_malloc (ctx -> allocator , size );
39+ if (!buffer ) {
40+ tokenizer_error_printf (ctx , "%s out of memory" , name );
41+ return "" ;
42+ }
43+ size_t written = 0 ;
44+ int rc = mbedtls_mpi_write_string (value , 10 , buffer , size , & written );
45+ if (rc == MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ) {
46+ size = written ;
47+ char * newbuf = buddy_realloc (ctx -> allocator , buffer , size );
48+ if (!newbuf ) {
49+ buddy_free (ctx -> allocator , buffer );
4550 tokenizer_error_printf (ctx , "%s out of memory" , name );
4651 return "" ;
4752 }
48-
49- size_t written = 0 ;
50- int rc = mbedtls_mpi_write_string (value , 10 , buffer , size , & written );
51- if (rc == 0 ) {
52- char * out = (char * )gc_strdup (ctx , buffer );
53- buddy_free (ctx -> allocator , buffer );
54- return out ;
55- }
56-
53+ buffer = newbuf ;
54+ rc = mbedtls_mpi_write_string (value , 10 , buffer , size , & written );
55+ }
56+ if (rc != 0 ) {
5757 buddy_free (ctx -> allocator , buffer );
58-
59- if (rc != MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ) {
60- basic_big_error (ctx , name , rc );
61- return "" ;
62- }
63-
64- size *= 2 ;
58+ basic_big_error (ctx , name , rc );
59+ return "" ;
6560 }
61+ char * out = (char * )gc_strdup (ctx , buffer );
62+ buddy_free (ctx -> allocator , buffer );
63+ return out ;
6664}
6765
6866static char * basic_big_binary (struct basic_ctx * ctx , const char * name , const char * a_text , const char * b_text , basic_big_binary_func func )
0 commit comments