Mercurial > dropbear
comparison libtommath/bn_mp_fwrite.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | f52919ffd3b1 |
children |
comparison
equal
deleted
inserted
replaced
1691:2d3745d58843 | 1692:1051e4eea25a |
---|---|
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 * SPDX-License-Identifier: Unlicense | |
13 */ | |
14 | 5 |
15 #ifndef LTM_NO_FILE | 6 #ifndef MP_NO_FILE |
16 int mp_fwrite(const mp_int *a, int radix, FILE *stream) | 7 mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) |
17 { | 8 { |
18 char *buf; | 9 char *buf; |
19 int err, len, x; | 10 mp_err err; |
11 int len; | |
12 size_t written; | |
20 | 13 |
21 if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { | 14 /* TODO: this function is not in this PR */ |
22 return err; | 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 } | |
23 } | 21 } |
24 | 22 |
25 buf = OPT_CAST(char) XMALLOC((size_t)len); | 23 buf = (char *) MP_MALLOC((size_t)len); |
26 if (buf == NULL) { | 24 if (buf == NULL) { |
27 return MP_MEM; | 25 return MP_MEM; |
28 } | 26 } |
29 | 27 |
30 if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { | 28 if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) { |
31 XFREE(buf); | 29 goto LBL_ERR; |
32 return err; | |
33 } | 30 } |
34 | 31 |
35 for (x = 0; x < len; x++) { | 32 if (fwrite(buf, written, 1uL, stream) != 1uL) { |
36 if (fputc((int)buf[x], stream) == EOF) { | 33 err = MP_ERR; |
37 XFREE(buf); | 34 goto LBL_ERR; |
38 return MP_VAL; | |
39 } | |
40 } | 35 } |
36 err = MP_OKAY; | |
41 | 37 |
42 XFREE(buf); | 38 |
43 return MP_OKAY; | 39 LBL_ERR: |
40 MP_FREE_BUFFER(buf, (size_t)len); | |
41 return err; | |
44 } | 42 } |
45 #endif | 43 #endif |
46 | 44 |
47 #endif | 45 #endif |
48 | |
49 /* ref: HEAD -> master, tag: v1.1.0 */ | |
50 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
51 /* commit time: 2019-01-28 20:32:32 +0100 */ |