bn_mp_fwrite.c revision 1.1.1.2
1/* $NetBSD: bn_mp_fwrite.c,v 1.1.1.2 2014/04/24 12:45:31 pettai Exp $ */ 2 3#include <tommath.h> 4#ifdef BN_MP_FWRITE_C 5/* LibTomMath, multiple-precision integer library -- Tom St Denis 6 * 7 * LibTomMath is a library that provides multiple-precision 8 * integer arithmetic as well as number theoretic functionality. 9 * 10 * The library was designed directly after the MPI library by 11 * Michael Fromberger but has been written from scratch with 12 * additional optimizations in place. 13 * 14 * The library is free for all purposes without any express 15 * guarantee it works. 16 * 17 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org 18 */ 19 20int mp_fwrite(mp_int *a, int radix, FILE *stream) 21{ 22 char *buf; 23 int err, len, x; 24 25 if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { 26 return err; 27 } 28 29 buf = OPT_CAST(char) XMALLOC (len); 30 if (buf == NULL) { 31 return MP_MEM; 32 } 33 34 if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { 35 XFREE (buf); 36 return err; 37 } 38 39 for (x = 0; x < len; x++) { 40 if (fputc(buf[x], stream) == EOF) { 41 XFREE (buf); 42 return MP_VAL; 43 } 44 } 45 46 XFREE (buf); 47 return MP_OKAY; 48} 49 50#endif 51 52/* Source: /cvs/libtom/libtommath/bn_mp_fwrite.c,v */ 53/* Revision: 1.4 */ 54/* Date: 2006/12/28 01:25:13 */ 55