comparison libtommath/bn_mp_fread.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_FREAD_C 2 #ifdef BN_MP_FREAD_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 /* read a bigint from a file stream in ASCII */ 7 /* read a bigint from a file stream in ASCII */
17 int mp_fread(mp_int *a, int radix, FILE *stream) 8 mp_err mp_fread(mp_int *a, int radix, FILE *stream)
18 { 9 {
19 int err, ch, neg, y; 10 mp_err err;
20 unsigned pos; 11 mp_sign neg;
21
22 /* clear a */
23 mp_zero(a);
24 12
25 /* if first digit is - then set negative */ 13 /* if first digit is - then set negative */
26 ch = fgetc(stream); 14 int ch = fgetc(stream);
27 if (ch == (int)'-') { 15 if (ch == (int)'-') {
28 neg = MP_NEG; 16 neg = MP_NEG;
29 ch = fgetc(stream); 17 ch = fgetc(stream);
30 } else { 18 } else {
31 neg = MP_ZPOS; 19 neg = MP_ZPOS;
32 } 20 }
33 21
34 for (;;) { 22 /* no digits, return error */
35 pos = (unsigned)(ch - (int)'('); 23 if (ch == EOF) {
24 return MP_ERR;
25 }
26
27 /* clear a */
28 mp_zero(a);
29
30 do {
31 int y;
32 unsigned pos = (unsigned)(ch - (int)'(');
36 if (mp_s_rmap_reverse_sz < pos) { 33 if (mp_s_rmap_reverse_sz < pos) {
37 break; 34 break;
38 } 35 }
39 36
40 y = (int)mp_s_rmap_reverse[pos]; 37 y = (int)mp_s_rmap_reverse[pos];
48 return err; 45 return err;
49 } 46 }
50 if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { 47 if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
51 return err; 48 return err;
52 } 49 }
50 } while ((ch = fgetc(stream)) != EOF);
53 51
54 ch = fgetc(stream); 52 if (a->used != 0) {
55 }
56 if (mp_cmp_d(a, 0uL) != MP_EQ) {
57 a->sign = neg; 53 a->sign = neg;
58 } 54 }
59 55
60 return MP_OKAY; 56 return MP_OKAY;
61 } 57 }
62 #endif 58 #endif
63 59
64 #endif 60 #endif
65
66 /* ref: HEAD -> master, tag: v1.1.0 */
67 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
68 /* commit time: 2019-01-28 20:32:32 +0100 */