/src/gnutls/lib/nettle/int/mpn-base256.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* gmp-glue.c |
2 | | |
3 | | Copyright (C) 2013 Niels Möller |
4 | | Copyright (C) 2013 Red Hat |
5 | | |
6 | | This file is part of GNU Nettle. |
7 | | |
8 | | GNU Nettle is free software: you can redistribute it and/or |
9 | | modify it under the terms of either: |
10 | | |
11 | | * the GNU Lesser General Public License as published by the Free |
12 | | Software Foundation; either version 3 of the License, or (at your |
13 | | option) any later version. |
14 | | |
15 | | or |
16 | | |
17 | | * the GNU General Public License as published by the Free |
18 | | Software Foundation; either version 2 of the License, or (at your |
19 | | option) any later version. |
20 | | |
21 | | or both in parallel, as here. |
22 | | |
23 | | GNU Nettle is distributed in the hope that it will be useful, |
24 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
26 | | General Public License for more details. |
27 | | |
28 | | You should have received copies of the GNU General Public License and |
29 | | the GNU Lesser General Public License along with this program. If |
30 | | not, see https://www.gnu.org/licenses/. |
31 | | */ |
32 | | |
33 | | #if HAVE_CONFIG_H |
34 | | # include "config.h" |
35 | | #endif |
36 | | |
37 | | #include "mpn-base256.h" |
38 | | |
39 | | void |
40 | | mpn_set_base256(mp_limb_t * rp, mp_size_t rn, const uint8_t * xp, size_t xn) |
41 | 0 | { |
42 | 0 | size_t xi; |
43 | 0 | mp_limb_t out; |
44 | 0 | unsigned bits; |
45 | 0 | for (xi = xn, out = bits = 0; xi > 0 && rn > 0;) { |
46 | 0 | mp_limb_t in = xp[--xi]; |
47 | 0 | out |= (in << bits) & GMP_NUMB_MASK; |
48 | 0 | bits += 8; |
49 | 0 | if (bits >= GMP_NUMB_BITS) { |
50 | 0 | *rp++ = out; |
51 | 0 | rn--; |
52 | |
|
53 | 0 | bits -= GMP_NUMB_BITS; |
54 | 0 | out = in >> (8 - bits); |
55 | 0 | } |
56 | 0 | } |
57 | 0 | if (rn > 0) { |
58 | 0 | *rp++ = out; |
59 | 0 | if (--rn > 0) |
60 | 0 | mpn_zero(rp, rn); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | void |
65 | | mpn_get_base256(uint8_t * rp, size_t rn, const mp_limb_t * xp, mp_size_t xn) |
66 | 0 | { |
67 | 0 | unsigned bits; |
68 | 0 | mp_limb_t in; |
69 | 0 | for (bits = in = 0; xn > 0 && rn > 0;) { |
70 | 0 | if (bits >= 8) { |
71 | 0 | rp[--rn] = in; |
72 | 0 | in >>= 8; |
73 | 0 | bits -= 8; |
74 | 0 | } else { |
75 | 0 | uint8_t old = in; |
76 | 0 | in = *xp++; |
77 | 0 | xn--; |
78 | 0 | rp[--rn] = old | (in << bits); |
79 | 0 | in >>= (8 - bits); |
80 | 0 | bits += GMP_NUMB_BITS - 8; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | while (rn > 0) { |
84 | 0 | rp[--rn] = in; |
85 | 0 | in >>= 8; |
86 | 0 | } |
87 | 0 | } |