libtommath/bn_mp_fwrite.c

39 lines
755 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
#ifndef LTM_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;
int err, len, x;
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(buf, len);
2003-07-02 15:39:39 +00:00
return err;
}
2017-08-30 03:51:11 +00:00
2003-07-02 15:39:39 +00:00
for (x = 0; x < len; x++) {
2017-10-15 17:58:35 +00:00
if (fputc((int)buf[x], stream) == EOF) {
MP_FREE(buf, len);
2017-08-29 20:23:48 +00:00
return MP_VAL;
}
2003-07-02 15:39:39 +00:00
}
2017-08-30 03:51:11 +00:00
MP_FREE(buf, 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