/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 mpn_set_base256(mp_limb_t *rp, mp_size_t rn, const uint8_t *xp, size_t xn)  | 
40  | 0  | { | 
41  | 0  |   size_t xi;  | 
42  | 0  |   mp_limb_t out;  | 
43  | 0  |   unsigned bits;  | 
44  | 0  |   for (xi = xn, out = bits = 0; xi > 0 && rn > 0;) { | 
45  | 0  |     mp_limb_t in = xp[--xi];  | 
46  | 0  |     out |= (in << bits) & GMP_NUMB_MASK;  | 
47  | 0  |     bits += 8;  | 
48  | 0  |     if (bits >= GMP_NUMB_BITS) { | 
49  | 0  |       *rp++ = out;  | 
50  | 0  |       rn--;  | 
51  |  | 
  | 
52  | 0  |       bits -= GMP_NUMB_BITS;  | 
53  | 0  |       out = in >> (8 - bits);  | 
54  | 0  |     }  | 
55  | 0  |   }  | 
56  | 0  |   if (rn > 0) { | 
57  | 0  |     *rp++ = out;  | 
58  | 0  |     if (--rn > 0)  | 
59  | 0  |       mpn_zero(rp, rn);  | 
60  | 0  |   }  | 
61  | 0  | }  | 
62  |  |  | 
63  |  | void mpn_get_base256(uint8_t *rp, size_t rn, const mp_limb_t *xp, mp_size_t xn)  | 
64  | 0  | { | 
65  | 0  |   unsigned bits;  | 
66  | 0  |   mp_limb_t in;  | 
67  | 0  |   for (bits = in = 0; xn > 0 && rn > 0;) { | 
68  | 0  |     if (bits >= 8) { | 
69  | 0  |       rp[--rn] = in;  | 
70  | 0  |       in >>= 8;  | 
71  | 0  |       bits -= 8;  | 
72  | 0  |     } else { | 
73  | 0  |       uint8_t old = in;  | 
74  | 0  |       in = *xp++;  | 
75  | 0  |       xn--;  | 
76  | 0  |       rp[--rn] = old | (in << bits);  | 
77  | 0  |       in >>= (8 - bits);  | 
78  | 0  |       bits += GMP_NUMB_BITS - 8;  | 
79  | 0  |     }  | 
80  | 0  |   }  | 
81  | 0  |   while (rn > 0) { | 
82  | 0  |     rp[--rn] = in;  | 
83  | 0  |     in >>= 8;  | 
84  | 0  |   }  | 
85  | 0  | }  |