3 * Loading and storing of multiprecision integers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 /* --- @mp_octets@ --- *
36 * Arguments: @const mp *m@ = a multiprecision integer
38 * Returns: The number of octets required to represent @m@.
40 * Use: Calculates the external storage required for a multiprecision
44 size_t mp_octets(const mp *m)
47 MPX_OCTETS(sz, m->v, m->vl);
51 /* --- @mp_octets2c@ --- *
53 * Arguments: @const mp *m@ = a multiprecision integer
55 * Returns: The number of octets required to represent @m@.
57 * Use: Calculates the external storage required for a multiprecision
58 * integer represented as two's complement.
61 size_t mp_octets2c(const mp *m)
64 MPX_OCTETS2C(sz, m->v, m->vl);
68 /* --- @mp_bits@ --- *
70 * Arguments: @const mp *m@ = a multiprecision integer
72 * Returns: The number of bits required to represent @m@.
74 * Use: Calculates the external storage required for a multiprecision
78 unsigned long mp_bits(const mp *m)
81 MPX_BITS(bits, m->v, m->vl);
85 /* --- @mp_loadl@ --- *
87 * Arguments: @mp *d@ = destination
88 * @const void *pv@ = pointer to source data
89 * @size_t sz@ = size of the source data
91 * Returns: Resulting multiprecision number.
93 * Use: Loads a multiprecision number from an array of octets. The
94 * first byte in the array is the least significant. More
95 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
96 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
99 mp *mp_loadl(mp *d, const void *pv, size_t sz)
101 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
102 mpx_loadl(d->v, d->vl, pv, sz);
103 d->f &= ~(MP_UNDEF | MP_NEG);
108 /* --- @mp_storel@ --- *
110 * Arguments: @const mp *m@ = source
111 * @void *pv@ = pointer to output array
112 * @size_t sz@ = size of the output array
116 * Use: Stores a multiprecision number in an array of octets. The
117 * first byte in the array is the least significant. If the
118 * array is too small to represent the number, high-order bits
119 * are truncated; if the array is too large, high order bytes
120 * are filled with zeros. More formally, if the number is
121 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
122 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
125 void mp_storel(const mp *m, void *pv, size_t sz)
127 mpx_storel(m->v, m->vl, pv, sz);
130 /* --- @mp_loadb@ --- *
132 * Arguments: @mp *d@ = destination
133 * @const void *pv@ = pointer to source data
134 * @size_t sz@ = size of the source data
136 * Returns: Resulting multiprecision number.
138 * Use: Loads a multiprecision number from an array of octets. The
139 * last byte in the array is the least significant. More
140 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
141 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
144 mp *mp_loadb(mp *d, const void *pv, size_t sz)
146 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
147 mpx_loadb(d->v, d->vl, pv, sz);
148 d->f &= ~(MP_UNDEF | MP_NEG);
153 /* --- @mp_storeb@ --- *
155 * Arguments: @const mp *m@ = source
156 * @void *pv@ = pointer to output array
157 * @size_t sz@ = size of the output array
161 * Use: Stores a multiprecision number in an array of octets. The
162 * last byte in the array is the least significant. If the
163 * array is too small to represent the number, high-order bits
164 * are truncated; if the array is too large, high order bytes
165 * are filled with zeros. More formally, if the number is
166 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
167 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
170 void mp_storeb(const mp *m, void *pv, size_t sz)
172 mpx_storeb(m->v, m->vl, pv, sz);
175 /* --- @mp_loadl2c@ --- *
177 * Arguments: @mp *d@ = destination
178 * @const void *pv@ = pointer to source data
179 * @size_t sz@ = size of the source data
181 * Returns: Resulting multiprecision number.
183 * Use: Loads a multiprecision number from an array of octets as
184 * two's complement. The first byte in the array is the least
188 mp *mp_loadl2c(mp *d, const void *pv, size_t sz)
190 const octet *ov = pv;
191 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
192 if (!sz || !(ov[sz - 1] & 0x80)) {
193 mpx_loadl(d->v, d->vl, pv, sz);
196 mpx_loadl2cn(d->v, d->vl, pv, sz);
204 /* --- @mp_storel2c@ --- *
206 * Arguments: @const mp *m@ = source
207 * @void *pv@ = pointer to output array
208 * @size_t sz@ = size of the output array
212 * Use: Stores a multiprecision number in an array of octets as two's
213 * complement. The first byte in the array is the least
214 * significant. If the array is too small to represent the
215 * number, high-order bits are truncated; if the array is too
216 * large, high order bytes are sign-extended.
219 void mp_storel2c(const mp *m, void *pv, size_t sz)
222 mpx_storel2cn(m->v, m->vl, pv, sz);
224 mpx_storel(m->v, m->vl, pv, sz);
227 /* --- @mp_loadb2c@ --- *
229 * Arguments: @mp *d@ = destination
230 * @const void *pv@ = pointer to source data
231 * @size_t sz@ = size of the source data
233 * Returns: Resulting multiprecision number.
235 * Use: Loads a multiprecision number from an array of octets as
236 * two's complement. The last byte in the array is the least
240 mp *mp_loadb2c(mp *d, const void *pv, size_t sz)
242 const octet *ov = pv;
243 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
244 if (!sz || !(ov[0] & 0x80)) {
245 mpx_loadb(d->v, d->vl, pv, sz);
248 mpx_loadb2cn(d->v, d->vl, pv, sz);
256 /* --- @mp_storeb2c@ --- *
258 * Arguments: @const mp *m@ = source
259 * @void *pv@ = pointer to output array
260 * @size_t sz@ = size of the output array
264 * Use: Stores a multiprecision number in an array of octets, as
265 * two's complement. The last byte in the array is the least
266 * significant. If the array is too small to represent the
267 * number, high-order bits are truncated; if the array is too
268 * large, high order bytes are sign-extended.
271 void mp_storeb2c(const mp *m, void *pv, size_t sz)
274 mpx_storeb2cn(m->v, m->vl, pv, sz);
276 mpx_storeb(m->v, m->vl, pv, sz);
279 /*----- That's all, folks -------------------------------------------------*/