/src/dropbear/libtommath/bn_mp_copy.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "tommath_private.h" |
2 | | #ifdef BN_MP_COPY_C |
3 | | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 | | /* SPDX-License-Identifier: Unlicense */ |
5 | | |
6 | | /* copy, b = a */ |
7 | | mp_err mp_copy(const mp_int *a, mp_int *b) |
8 | 12.4M | { |
9 | 12.4M | int n; |
10 | 12.4M | mp_digit *tmpa, *tmpb; |
11 | 12.4M | mp_err err; |
12 | | |
13 | | /* if dst == src do nothing */ |
14 | 12.4M | if (a == b) { |
15 | 5.73M | return MP_OKAY; |
16 | 5.73M | } |
17 | | |
18 | | /* grow dest */ |
19 | 6.70M | if (b->alloc < a->used) { |
20 | 5.79k | if ((err = mp_grow(b, a->used)) != MP_OKAY) { |
21 | 0 | return err; |
22 | 0 | } |
23 | 5.79k | } |
24 | | |
25 | | /* zero b and copy the parameters over */ |
26 | | /* pointer aliases */ |
27 | | |
28 | | /* source */ |
29 | 6.70M | tmpa = a->dp; |
30 | | |
31 | | /* destination */ |
32 | 6.70M | tmpb = b->dp; |
33 | | |
34 | | /* copy all the digits */ |
35 | 144M | for (n = 0; n < a->used; n++) { |
36 | 138M | *tmpb++ = *tmpa++; |
37 | 138M | } |
38 | | |
39 | | /* clear high digits */ |
40 | 6.70M | MP_ZERO_DIGITS(tmpb, b->used - n); |
41 | | |
42 | | /* copy used count and sign */ |
43 | 6.70M | b->used = a->used; |
44 | 6.70M | b->sign = a->sign; |
45 | 6.70M | return MP_OKAY; |
46 | 6.70M | } |
47 | | #endif |