libtommath/mp_fwrite.c
Daniel Mendler 795cd2013f
simplifications: add s_mp_zero_(digs|buf) and s_mp_copy_digs
Originally I made those as macros. However we have many
other small functions like mp_clamp, mp_exch which are also not implemented
as macros right now.

If we would use c99, I would implement them as private static inline
functions. And mp_exch would be a public static inline function.

But since we are bound to c89, we simply use normal functions.
To achieve optimal performance one should either use link time
optimization or amalgamation.
2019-11-04 15:41:32 +01:00

34 lines
684 B
C

#include "tommath_private.h"
#ifdef MP_FWRITE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#ifndef MP_NO_FILE
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
{
char *buf;
mp_err err;
size_t size, written;
if ((err = mp_radix_size(a, radix, &size)) != MP_OKAY) {
return err;
}
buf = (char *) MP_MALLOC(size);
if (buf == NULL) {
return MP_MEM;
}
if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) {
if (fwrite(buf, written, 1uL, stream) != 1uL) {
err = MP_ERR;
}
}
MP_FREE_BUF(buf, size);
return err;
}
#endif
#endif