3 * $Id: mp-io.c,v 1.4 2000/06/17 11:45:09 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.4 2000/06/17 11:45:09 mdw
34 * Major memory management overhaul. Added arena support. Use the secure
35 * arena for secret integers. Replace and improve the MP management macros
36 * (e.g., replace MP_MODIFY by MP_DEST).
38 * Revision 1.3 1999/11/21 22:13:02 mdw
39 * Add mp version of MPX_BITS.
41 * Revision 1.2 1999/11/19 13:19:06 mdw
42 * Set flags on results correctly.
44 * Revision 1.1 1999/11/17 18:02:16 mdw
45 * New multiprecision integer arithmetic suite.
49 /*----- Header files ------------------------------------------------------*/
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @mp_octets@ --- *
57 * Arguments: @const mp *m@ = a multiprecision integer
59 * Returns: The number of octets required to represent @m@.
61 * Use: Calculates the external storage required for a multiprecision
65 size_t mp_octets(const mp
*m
)
68 MPX_OCTETS(sz
, m
->v
, m
->vl
);
72 /* --- @mp_bits@ --- *
74 * Arguments: @const mp *m@ = a multiprecision integer
76 * Returns: The number of bits required to represent @m@.
78 * Use: Calculates the external storage required for a multiprecision
82 unsigned long mp_bits(const mp
*m
)
85 MPX_BITS(bits
, m
->v
, m
->vl
);
89 /* --- @mp_loadl@ --- *
91 * Arguments: @mp *d@ = destination
92 * @const void *pv@ = pointer to source data
93 * @size_t sz@ = size of the source data
95 * Returns: Resulting multiprecision number.
97 * Use: Loads a multiprecision number from an array of octets. The
98 * first byte in the array is the least significant. More
99 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
100 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
103 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
105 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
106 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
107 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
112 /* --- @mp_storel@ --- *
114 * Arguments: @const mp *m@ = source
115 * @void *pv@ = pointer to output array
116 * @size_t sz@ = size of the output array
120 * Use: Stores a multiprecision number in an array of octets. The
121 * first byte in the array is the least significant. If the
122 * array is too small to represent the number, high-order bits
123 * are truncated; if the array is too large, high order bytes
124 * are filled with zeros. More formally, if the number is
125 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
126 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
129 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
131 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
134 /* --- @mp_loadb@ --- *
136 * Arguments: @mp *d@ = destination
137 * @const void *pv@ = pointer to source data
138 * @size_t sz@ = size of the source data
140 * Returns: Resulting multiprecision number.
142 * Use: Loads a multiprecision number from an array of octets. The
143 * last byte in the array is the least significant. More
144 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
145 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
148 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
150 MP_DEST(d
, MPW_RQ(sz
), MP_UNDEF
);
151 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
152 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
157 /* --- @mp_storeb@ --- *
159 * Arguments: @const mp *m@ = source
160 * @void *pv@ = pointer to output array
161 * @size_t sz@ = size of the output array
165 * Use: Stores a multiprecision number in an array of octets. The
166 * last byte in the array is the least significant. If the
167 * array is too small to represent the number, high-order bits
168 * are truncated; if the array is too large, high order bytes
169 * are filled with zeros. More formally, if the number is
170 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
171 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
174 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
176 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
179 /*----- That's all, folks -------------------------------------------------*/