comparison libtommath/bn_mp_fwrite.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_FWRITE_C 2 #ifdef BN_MP_FWRITE_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 #ifndef LTM_NO_FILE 6 #ifndef MP_NO_FILE
19 int mp_fwrite(mp_int *a, int radix, FILE *stream) 7 mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
20 { 8 {
21 char *buf; 9 char *buf;
22 int err, len, x; 10 mp_err err;
23 11 int len;
24 if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { 12 size_t written;
25 return err; 13
14 /* TODO: this function is not in this PR */
15 if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) {
16 /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY) goto LBL_ERR; */
17 } else {
18 if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
19 return err;
20 }
26 } 21 }
27 22
28 buf = OPT_CAST(char) XMALLOC (len); 23 buf = (char *) MP_MALLOC((size_t)len);
29 if (buf == NULL) { 24 if (buf == NULL) {
30 return MP_MEM; 25 return MP_MEM;
31 } 26 }
32 27
33 if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { 28 if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) {
34 XFREE (buf); 29 goto LBL_ERR;
35 return err;
36 } 30 }
37 31
38 for (x = 0; x < len; x++) { 32 if (fwrite(buf, written, 1uL, stream) != 1uL) {
39 if (fputc(buf[x], stream) == EOF) { 33 err = MP_ERR;
40 XFREE (buf); 34 goto LBL_ERR;
41 return MP_VAL;
42 }
43 } 35 }
44 36 err = MP_OKAY;
45 XFREE (buf); 37
46 return MP_OKAY; 38
39 LBL_ERR:
40 MP_FREE_BUFFER(buf, (size_t)len);
41 return err;
47 } 42 }
48 #endif 43 #endif
49 44
50 #endif 45 #endif
51
52 /* ref: $Format:%D$ */
53 /* git commit: $Format:%H$ */
54 /* commit time: $Format:%ai$ */