libtommath/mp_fwrite.c

41 lines
789 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-21 17:12:22 +00:00
size_t len, written;
2017-08-30 03:51:11 +00:00
/* TODO: this function is not in this PR */
2019-10-21 17:12:22 +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-10-21 17:12:22 +00:00
buf = (char *) MP_MALLOC(len);
2003-07-02 15:39:39 +00:00
if (buf == NULL) {
return MP_MEM;
}
2017-08-30 03:51:11 +00:00
2019-10-21 17:12:22 +00:00
if ((err = mp_to_radix(a, buf, len, &written, radix)) != MP_OKAY) {
2019-05-29 10:23:08 +00:00
goto LBL_ERR;
2003-07-02 15:39:39 +00:00
}
2017-08-30 03:51:11 +00:00
if (fwrite(buf, written, 1uL, stream) != 1uL) {
2019-05-29 10:23:08 +00:00
err = MP_ERR;
goto LBL_ERR;
2003-07-02 15:39:39 +00:00
}
2019-05-29 10:23:08 +00:00
err = MP_OKAY;
2017-08-30 03:51:11 +00:00
2019-05-29 10:23:08 +00:00
LBL_ERR:
2019-10-21 17:12:22 +00:00
MP_FREE_BUFFER(buf, len);
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