2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2004-10-29 22:07:18 +00:00
|
|
|
#ifdef BN_MP_FWRITE_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-07-02 15:39:39 +00:00
|
|
|
|
2019-04-26 10:35:39 +00:00
|
|
|
#ifndef MP_NO_FILE
|
2017-09-20 14:59:43 +00:00
|
|
|
int mp_fwrite(const mp_int *a, int radix, FILE *stream)
|
2003-07-02 15:39:39 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
2019-05-12 14:05:53 +00:00
|
|
|
int err, len;
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2004-01-25 17:40:21 +00:00
|
|
|
if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
|
|
|
|
return err;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2004-01-25 17:40:21 +00:00
|
|
|
|
2019-04-09 09:08:26 +00:00
|
|
|
buf = (char *) MP_MALLOC((size_t)len);
|
2003-07-02 15:39:39 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
return MP_MEM;
|
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2003-07-02 15:39:39 +00:00
|
|
|
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
|
2019-05-11 08:22:20 +00:00
|
|
|
MP_FREE_BUFFER(buf, (size_t)len);
|
2003-07-02 15:39:39 +00:00
|
|
|
return err;
|
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2019-05-12 14:05:53 +00:00
|
|
|
if (fwrite(buf, (size_t)len, 1, stream) != 1) {
|
|
|
|
MP_FREE_BUFFER(buf, (size_t)len);
|
|
|
|
return MP_ERR;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2019-05-11 08:22:20 +00:00
|
|
|
MP_FREE_BUFFER(buf, (size_t)len);
|
2003-07-02 15:39:39 +00:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2017-04-11 14:21:36 +00:00
|
|
|
#endif
|
2003-07-02 15:39:39 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|