1 #include "tommath_private.h"
2 #ifdef BN_MP_FWRITE_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5 
6 #ifndef MP_NO_FILE
mp_fwrite(const mp_int * a,int radix,FILE * stream)7 mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
8 {
9    char *buf;
10    mp_err err;
11    int len;
12    size_t written;
13 
14    /* TODO: this function is not in this PR */
15    if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) {
16       /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY)      goto LBL_ERR; */
17    } else {
18       if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
19          return err;
20       }
21    }
22 
23    buf = (char *) MP_MALLOC((size_t)len);
24    if (buf == NULL) {
25       return MP_MEM;
26    }
27 
28    if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) {
29       goto LBL_ERR;
30    }
31 
32    if (fwrite(buf, written, 1uL, stream) != 1uL) {
33       err = MP_ERR;
34       goto LBL_ERR;
35    }
36    err = MP_OKAY;
37 
38 
39 LBL_ERR:
40    MP_FREE_BUFFER(buf, (size_t)len);
41    return err;
42 }
43 #endif
44 
45 #endif
46