libtommath/mp_fwrite.c

34 lines
684 B
C
Raw Normal View History

#include "tommath_private.h"
#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
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
2003-07-02 15:39:39 +00:00
{
char *buf;
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-29 17:41:25 +00:00
if ((err = mp_radix_size(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
MP_FREE_BUF(buf, size);
2019-05-29 10:23:08 +00:00
return err;
2003-07-02 15:39:39 +00:00
}
#endif
2003-07-02 15:39:39 +00:00
2004-10-29 22:07:18 +00:00
#endif