libtommath/bn_mp_fwrite.c

38 lines
772 B
C
Raw Normal View History

#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
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;
int 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
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) {
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-18 10:37:54 +00:00
if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) {
2019-05-12 14:05:53 +00:00
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
MP_FREE_BUFFER(buf, (size_t)len);
2003-07-02 15:39:39 +00:00
return MP_OKAY;
}
#endif
2003-07-02 15:39:39 +00:00
2004-10-29 22:07:18 +00:00
#endif