3 * $Id: mp-io.c,v 1.3 1999/11/21 22:13:02 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.3 1999/11/21 22:13:02 mdw
34 * Add mp version of MPX_BITS.
36 * Revision 1.2 1999/11/19 13:19:06 mdw
37 * Set flags on results correctly.
39 * Revision 1.1 1999/11/17 18:02:16 mdw
40 * New multiprecision integer arithmetic suite.
44 /*----- Header files ------------------------------------------------------*/
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @mp_octets@ --- *
52 * Arguments: @const mp *m@ = a multiprecision integer
54 * Returns: The number of octets required to represent @m@.
56 * Use: Calculates the external storage required for a multiprecision
60 size_t mp_octets(const mp
*m
)
63 MPX_OCTETS(sz
, m
->v
, m
->vl
);
67 /* --- @mp_bits@ --- *
69 * Arguments: @const mp *m@ = a multiprecision integer
71 * Returns: The number of bits required to represent @m@.
73 * Use: Calculates the external storage required for a multiprecision
77 unsigned long mp_bits(const mp
*m
)
80 MPX_BITS(bits
, m
->v
, m
->vl
);
84 /* --- @mp_loadl@ --- *
86 * Arguments: @mp *d@ = destination
87 * @const void *pv@ = pointer to source data
88 * @size_t sz@ = size of the source data
90 * Returns: Resulting multiprecision number.
92 * Use: Loads a multiprecision number from an array of octets. The
93 * first byte in the array is the least significant. More
94 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
95 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
98 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
100 MP_MODIFY(d
, MPW_RQ(sz
));
101 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
102 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
107 /* --- @mp_storel@ --- *
109 * Arguments: @const mp *m@ = source
110 * @void *pv@ = pointer to output array
111 * @size_t sz@ = size of the output array
115 * Use: Stores a multiprecision number in an array of octets. The
116 * first byte in the array is the least significant. If the
117 * array is too small to represent the number, high-order bits
118 * are truncated; if the array is too large, high order bytes
119 * are filled with zeros. More formally, if the number is
120 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
121 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
124 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
126 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
129 /* --- @mp_loadb@ --- *
131 * Arguments: @mp *d@ = destination
132 * @const void *pv@ = pointer to source data
133 * @size_t sz@ = size of the source data
135 * Returns: Resulting multiprecision number.
137 * Use: Loads a multiprecision number from an array of octets. The
138 * last byte in the array is the least significant. More
139 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
140 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
143 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
145 MP_MODIFY(d
, MPW_RQ(sz
));
146 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
147 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
152 /* --- @mp_storeb@ --- *
154 * Arguments: @const mp *m@ = source
155 * @void *pv@ = pointer to output array
156 * @size_t sz@ = size of the output array
160 * Use: Stores a multiprecision number in an array of octets. The
161 * last byte in the array is the least significant. If the
162 * array is too small to represent the number, high-order bits
163 * are truncated; if the array is too large, high order bytes
164 * are filled with zeros. More formally, if the number is
165 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
166 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
169 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
171 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
174 /*----- That's all, folks -------------------------------------------------*/