tommath/bn_mp_fwrite.c

47 lines
1.1 KiB
C
Raw Normal View History

2003-07-02 11:39:39 -04:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
2003-08-04 21:24:44 -04:00
* LibTomMath is a library that provides multiple-precision
2003-07-02 11:39:39 -04:00
* integer arithmetic as well as number theoretic functionality.
*
2003-08-04 21:24:44 -04:00
* The library was designed directly after the MPI library by
2003-07-02 11:39:39 -04:00
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
*/
#include <tommath.h>
int mp_fwrite(mp_int *a, int radix, FILE *stream)
{
char *buf;
int err, len, x;
2004-01-25 12:40:21 -05:00
if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
return err;
2003-07-02 11:39:39 -04:00
}
2004-01-25 12:40:21 -05:00
2004-04-11 16:46:22 -04:00
buf = OPT_CAST(char) XMALLOC (len);
2003-07-02 11:39:39 -04:00
if (buf == NULL) {
return MP_MEM;
}
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
2003-12-24 13:59:22 -05:00
XFREE (buf);
2003-07-02 11:39:39 -04:00
return err;
}
for (x = 0; x < len; x++) {
if (fputc(buf[x], stream) == EOF) {
2003-12-24 13:59:22 -05:00
XFREE (buf);
2003-07-02 11:39:39 -04:00
return MP_VAL;
}
}
2003-12-24 13:59:22 -05:00
XFREE (buf);
2003-07-02 11:39:39 -04:00
return MP_OKAY;
}