3 * $Id: mp-io.c,v 1.6 2002/10/20 01:12:31 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2002/10/20 01:12:31 mdw
34 * Two's complement I/O fixes.
36 * Revision 1.5 2002/10/06 22:52:50 mdw
37 * Pile of changes for supporting two's complement properly.
39 * Revision 1.4 2000/06/17 11:45:09 mdw
40 * Major memory management overhaul. Added arena support. Use the secure
41 * arena for secret integers. Replace and improve the MP management macros
42 * (e.g., replace MP_MODIFY by MP_DEST).
44 * Revision 1.3 1999/11/21 22:13:02 mdw
45 * Add mp version of MPX_BITS.
47 * Revision 1.2 1999/11/19 13:19:06 mdw
48 * Set flags on results correctly.
50 * Revision 1.1 1999/11/17 18:02:16 mdw
51 * New multiprecision integer arithmetic suite.
55 /*----- Header files ------------------------------------------------------*/
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @mp_octets@ --- *
63 * Arguments: @const mp *m@ = a multiprecision integer
65 * Returns: The number of octets required to represent @m@.
67 * Use: Calculates the external storage required for a multiprecision
71 size_t mp_octets(const mp
*m
)
74 MPX_OCTETS(sz
, m
->v
, m
->vl
);
78 /* --- @mp_octets2c@ --- *
80 * Arguments: @const mp *m@ = a multiprecision integer
82 * Returns: The number of octets required to represent @m@.
84 * Use: Calculates the external storage required for a multiprecision
85 * integer represented as two's complement.
88 size_t mp_octets2c(const mp
*m
)
91 MPX_OCTETS2C(sz
, m
->v
, m
->vl
);
95 /* --- @mp_bits@ --- *
97 * Arguments: @const mp *m@ = a multiprecision integer
99 * Returns: The number of bits required to represent @m@.
101 * Use: Calculates the external storage required for a multiprecision
105 unsigned long mp_bits(const mp
*m
)
108 MPX_BITS(bits
, m
->v
, m
->vl
);
112 /* --- @mp_loadl@ --- *
114 * Arguments: @mp *d@ = destination
115 * @const void *pv@ = pointer to source data
116 * @size_t sz@ = size of the source data
118 * Returns: Resulting multiprecision number.
120 * Use: Loads a multiprecision number from an array of octets. The
121 * first byte in the array is the least significant. More
122 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
123 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
126 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
128 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
129 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
130 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
135 /* --- @mp_storel@ --- *
137 * Arguments: @const mp *m@ = source
138 * @void *pv@ = pointer to output array
139 * @size_t sz@ = size of the output array
143 * Use: Stores a multiprecision number in an array of octets. The
144 * first byte in the array is the least significant. If the
145 * array is too small to represent the number, high-order bits
146 * are truncated; if the array is too large, high order bytes
147 * are filled with zeros. More formally, if the number is
148 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
149 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
152 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
154 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
157 /* --- @mp_loadb@ --- *
159 * Arguments: @mp *d@ = destination
160 * @const void *pv@ = pointer to source data
161 * @size_t sz@ = size of the source data
163 * Returns: Resulting multiprecision number.
165 * Use: Loads a multiprecision number from an array of octets. The
166 * last byte in the array is the least significant. More
167 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
168 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
171 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
173 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
174 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
175 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
180 /* --- @mp_storeb@ --- *
182 * Arguments: @const mp *m@ = source
183 * @void *pv@ = pointer to output array
184 * @size_t sz@ = size of the output array
188 * Use: Stores a multiprecision number in an array of octets. The
189 * last byte in the array is the least significant. If the
190 * array is too small to represent the number, high-order bits
191 * are truncated; if the array is too large, high order bytes
192 * are filled with zeros. More formally, if the number is
193 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
194 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
197 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
199 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
202 /* --- @mp_loadl2c@ --- *
204 * Arguments: @mp *d@ = destination
205 * @const void *pv@ = pointer to source data
206 * @size_t sz@ = size of the source data
208 * Returns: Resulting multiprecision number.
210 * Use: Loads a multiprecision number from an array of octets as
211 * two's complement. The first byte in the array is the least
215 mp
*mp_loadl2c(mp
*d
, const void *pv
, size_t sz
)
217 const octet
*ov
= pv
;
218 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
219 if (!sz
|| !(ov
[sz
- 1] & 0x80)) {
220 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
223 mpx_loadl2cn(d
->v
, d
->vl
, pv
, sz
);
231 /* --- @mp_storel2c@ --- *
233 * Arguments: @const mp *m@ = source
234 * @void *pv@ = pointer to output array
235 * @size_t sz@ = size of the output array
239 * Use: Stores a multiprecision number in an array of octets as two's
240 * complement. The first byte in the array is the least
241 * significant. If the array is too small to represent the
242 * number, high-order bits are truncated; if the array is too
243 * large, high order bytes are sign-extended.
246 void mp_storel2c(const mp
*m
, void *pv
, size_t sz
)
249 mpx_storel2cn(m
->v
, m
->vl
, pv
, sz
);
251 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
254 /* --- @mp_loadb2c@ --- *
256 * Arguments: @mp *d@ = destination
257 * @const void *pv@ = pointer to source data
258 * @size_t sz@ = size of the source data
260 * Returns: Resulting multiprecision number.
262 * Use: Loads a multiprecision number from an array of octets as
263 * two's complement. The last byte in the array is the least
267 mp
*mp_loadb2c(mp
*d
, const void *pv
, size_t sz
)
269 const octet
*ov
= pv
;
270 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
271 if (!sz
|| !(ov
[0] & 0x80)) {
272 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
275 mpx_loadb2cn(d
->v
, d
->vl
, pv
, sz
);
283 /* --- @mp_storeb2c@ --- *
285 * Arguments: @const mp *m@ = source
286 * @void *pv@ = pointer to output array
287 * @size_t sz@ = size of the output array
291 * Use: Stores a multiprecision number in an array of octets, as
292 * two's complement. The last byte in the array is the least
293 * significant. If the array is too small to represent the
294 * number, high-order bits are truncated; if the array is too
295 * large, high order bytes are sign-extended.
298 void mp_storeb2c(const mp
*m
, void *pv
, size_t sz
)
301 mpx_storeb2cn(m
->v
, m
->vl
, pv
, sz
);
303 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
306 /*----- That's all, folks -------------------------------------------------*/