3 * $Id: mp-io.c,v 1.7 2004/04/08 01:36:15 mdw Exp $
5 * Loading and storing of multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @mp_octets@ --- *
38 * Arguments: @const mp *m@ = a multiprecision integer
40 * Returns: The number of octets required to represent @m@.
42 * Use: Calculates the external storage required for a multiprecision
46 size_t mp_octets(const mp
*m
)
49 MPX_OCTETS(sz
, m
->v
, m
->vl
);
53 /* --- @mp_octets2c@ --- *
55 * Arguments: @const mp *m@ = a multiprecision integer
57 * Returns: The number of octets required to represent @m@.
59 * Use: Calculates the external storage required for a multiprecision
60 * integer represented as two's complement.
63 size_t mp_octets2c(const mp
*m
)
66 MPX_OCTETS2C(sz
, m
->v
, m
->vl
);
70 /* --- @mp_bits@ --- *
72 * Arguments: @const mp *m@ = a multiprecision integer
74 * Returns: The number of bits required to represent @m@.
76 * Use: Calculates the external storage required for a multiprecision
80 unsigned long mp_bits(const mp
*m
)
83 MPX_BITS(bits
, m
->v
, m
->vl
);
87 /* --- @mp_loadl@ --- *
89 * Arguments: @mp *d@ = destination
90 * @const void *pv@ = pointer to source data
91 * @size_t sz@ = size of the source data
93 * Returns: Resulting multiprecision number.
95 * Use: Loads a multiprecision number from an array of octets. The
96 * first byte in the array is the least significant. More
97 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
98 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
101 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
103 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
104 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
105 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
110 /* --- @mp_storel@ --- *
112 * Arguments: @const mp *m@ = source
113 * @void *pv@ = pointer to output array
114 * @size_t sz@ = size of the output array
118 * Use: Stores a multiprecision number in an array of octets. The
119 * first byte in the array is the least significant. If the
120 * array is too small to represent the number, high-order bits
121 * are truncated; if the array is too large, high order bytes
122 * are filled with zeros. More formally, if the number is
123 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
124 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
127 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
129 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
132 /* --- @mp_loadb@ --- *
134 * Arguments: @mp *d@ = destination
135 * @const void *pv@ = pointer to source data
136 * @size_t sz@ = size of the source data
138 * Returns: Resulting multiprecision number.
140 * Use: Loads a multiprecision number from an array of octets. The
141 * last byte in the array is the least significant. More
142 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
143 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
146 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
148 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
149 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
150 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
155 /* --- @mp_storeb@ --- *
157 * Arguments: @const mp *m@ = source
158 * @void *pv@ = pointer to output array
159 * @size_t sz@ = size of the output array
163 * Use: Stores a multiprecision number in an array of octets. The
164 * last byte in the array is the least significant. If the
165 * array is too small to represent the number, high-order bits
166 * are truncated; if the array is too large, high order bytes
167 * are filled with zeros. More formally, if the number is
168 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
169 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
172 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
174 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
177 /* --- @mp_loadl2c@ --- *
179 * Arguments: @mp *d@ = destination
180 * @const void *pv@ = pointer to source data
181 * @size_t sz@ = size of the source data
183 * Returns: Resulting multiprecision number.
185 * Use: Loads a multiprecision number from an array of octets as
186 * two's complement. The first byte in the array is the least
190 mp
*mp_loadl2c(mp
*d
, const void *pv
, size_t sz
)
192 const octet
*ov
= pv
;
193 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
194 if (!sz
|| !(ov
[sz
- 1] & 0x80)) {
195 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
198 mpx_loadl2cn(d
->v
, d
->vl
, pv
, sz
);
206 /* --- @mp_storel2c@ --- *
208 * Arguments: @const mp *m@ = source
209 * @void *pv@ = pointer to output array
210 * @size_t sz@ = size of the output array
214 * Use: Stores a multiprecision number in an array of octets as two's
215 * complement. The first byte in the array is the least
216 * significant. If the array is too small to represent the
217 * number, high-order bits are truncated; if the array is too
218 * large, high order bytes are sign-extended.
221 void mp_storel2c(const mp
*m
, void *pv
, size_t sz
)
224 mpx_storel2cn(m
->v
, m
->vl
, pv
, sz
);
226 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
229 /* --- @mp_loadb2c@ --- *
231 * Arguments: @mp *d@ = destination
232 * @const void *pv@ = pointer to source data
233 * @size_t sz@ = size of the source data
235 * Returns: Resulting multiprecision number.
237 * Use: Loads a multiprecision number from an array of octets as
238 * two's complement. The last byte in the array is the least
242 mp
*mp_loadb2c(mp
*d
, const void *pv
, size_t sz
)
244 const octet
*ov
= pv
;
245 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
246 if (!sz
|| !(ov
[0] & 0x80)) {
247 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
250 mpx_loadb2cn(d
->v
, d
->vl
, pv
, sz
);
258 /* --- @mp_storeb2c@ --- *
260 * Arguments: @const mp *m@ = source
261 * @void *pv@ = pointer to output array
262 * @size_t sz@ = size of the output array
266 * Use: Stores a multiprecision number in an array of octets, as
267 * two's complement. The last byte in the array is the least
268 * significant. If the array is too small to represent the
269 * number, high-order bits are truncated; if the array is too
270 * large, high order bytes are sign-extended.
273 void mp_storeb2c(const mp
*m
, void *pv
, size_t sz
)
276 mpx_storeb2cn(m
->v
, m
->vl
, pv
, sz
);
278 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
281 /*----- That's all, folks -------------------------------------------------*/