2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef 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
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
|
2003-07-02 15:39:39 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err err;
|
2019-10-29 17:41:25 +00:00
|
|
|
size_t size, written;
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2019-10-10 22:06:45 +00:00
|
|
|
if ((err = mp_radix_size_overestimate(a, radix, &size)) != MP_OKAY) {
|
2019-10-21 17:12:22 +00:00
|
|
|
return err;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2004-01-25 17:40:21 +00:00
|
|
|
|
2019-10-29 17:41:25 +00:00
|
|
|
buf = (char *) MP_MALLOC(size);
|
2003-07-02 15:39:39 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
return MP_MEM;
|
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) {
|
|
|
|
if (fwrite(buf, written, 1uL, stream) != 1uL) {
|
|
|
|
err = MP_ERR;
|
|
|
|
}
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2019-10-29 20:48:50 +00:00
|
|
|
MP_FREE_BUF(buf, size);
|
2019-05-29 10:23:08 +00:00
|
|
|
return err;
|
2003-07-02 15:39:39 +00:00
|
|
|
}
|
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
|