2
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* read a bigint from a file stream in ASCII */ |
|
18 int mp_fread(mp_int *a, int radix, FILE *stream) |
|
19 { |
|
20 int err, ch, neg, y; |
|
21 |
|
22 /* clear a */ |
|
23 mp_zero(a); |
|
24 |
|
25 /* if first digit is - then set negative */ |
|
26 ch = fgetc(stream); |
|
27 if (ch == '-') { |
|
28 neg = MP_NEG; |
|
29 ch = fgetc(stream); |
|
30 } else { |
|
31 neg = MP_ZPOS; |
|
32 } |
|
33 |
|
34 for (;;) { |
|
35 /* find y in the radix map */ |
|
36 for (y = 0; y < radix; y++) { |
|
37 if (mp_s_rmap[y] == ch) { |
|
38 break; |
|
39 } |
|
40 } |
|
41 if (y == radix) { |
|
42 break; |
|
43 } |
|
44 |
|
45 /* shift up and add */ |
|
46 if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { |
|
47 return err; |
|
48 } |
|
49 if ((err = mp_add_d(a, y, a)) != MP_OKAY) { |
|
50 return err; |
|
51 } |
|
52 |
|
53 ch = fgetc(stream); |
|
54 } |
|
55 if (mp_cmp_d(a, 0) != MP_EQ) { |
|
56 a->sign = neg; |
|
57 } |
|
58 |
|
59 return MP_OKAY; |
|
60 } |
|
61 |