.gitignore: Ignore `auto-version' script.
[u/mdw/catacomb] / mp-io.c
CommitLineData
d3409d5e 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
d3409d5e 4 *
5 * Loading and storing of multiprecision integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
d3409d5e 11 *
12 * This file is part of Catacomb.
13 *
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.
45c0fd36 18 *
d3409d5e 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.
45c0fd36 23 *
d3409d5e 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,
27 * MA 02111-1307, USA.
28 */
29
d3409d5e 30/*----- Header files ------------------------------------------------------*/
31
32#include "mp.h"
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @mp_octets@ --- *
37 *
38 * Arguments: @const mp *m@ = a multiprecision integer
39 *
40 * Returns: The number of octets required to represent @m@.
41 *
42 * Use: Calculates the external storage required for a multiprecision
43 * integer.
44 */
45
46size_t mp_octets(const mp *m)
47{
48 size_t sz;
49 MPX_OCTETS(sz, m->v, m->vl);
50 return (sz);
51}
52
f09e814a 53/* --- @mp_octets2c@ --- *
54 *
55 * Arguments: @const mp *m@ = a multiprecision integer
56 *
57 * Returns: The number of octets required to represent @m@.
58 *
59 * Use: Calculates the external storage required for a multiprecision
60 * integer represented as two's complement.
61 */
62
63size_t mp_octets2c(const mp *m)
64{
65 size_t sz;
66 MPX_OCTETS2C(sz, m->v, m->vl);
67 return (sz);
68}
69
24e1e862 70/* --- @mp_bits@ --- *
71 *
72 * Arguments: @const mp *m@ = a multiprecision integer
73 *
74 * Returns: The number of bits required to represent @m@.
75 *
76 * Use: Calculates the external storage required for a multiprecision
77 * integer.
78 */
79
80unsigned long mp_bits(const mp *m)
81{
82 unsigned long bits;
83 MPX_BITS(bits, m->v, m->vl);
84 return (bits);
85}
86
d3409d5e 87/* --- @mp_loadl@ --- *
88 *
89 * Arguments: @mp *d@ = destination
90 * @const void *pv@ = pointer to source data
91 * @size_t sz@ = size of the source data
92 *
93 * Returns: Resulting multiprecision number.
94 *
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}$%.
99 */
100
101mp *mp_loadl(mp *d, const void *pv, size_t sz)
102{
d34decd2 103 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
d3409d5e 104 mpx_loadl(d->v, d->vl, pv, sz);
8857f8eb 105 d->f &= ~(MP_UNDEF | MP_NEG);
d3409d5e 106 mp_shrink(d);
107 return (d);
108}
109
110/* --- @mp_storel@ --- *
111 *
112 * Arguments: @const mp *m@ = source
113 * @void *pv@ = pointer to output array
114 * @size_t sz@ = size of the output array
115 *
116 * Returns: ---
117 *
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}$%.
125 */
126
127void mp_storel(const mp *m, void *pv, size_t sz)
128{
129 mpx_storel(m->v, m->vl, pv, sz);
130}
131
132/* --- @mp_loadb@ --- *
133 *
134 * Arguments: @mp *d@ = destination
135 * @const void *pv@ = pointer to source data
136 * @size_t sz@ = size of the source data
137 *
138 * Returns: Resulting multiprecision number.
139 *
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}$%.
144 */
145
146mp *mp_loadb(mp *d, const void *pv, size_t sz)
147{
d34decd2 148 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
d3409d5e 149 mpx_loadb(d->v, d->vl, pv, sz);
8857f8eb 150 d->f &= ~(MP_UNDEF | MP_NEG);
d3409d5e 151 mp_shrink(d);
152 return (d);
153}
154
155/* --- @mp_storeb@ --- *
156 *
157 * Arguments: @const mp *m@ = source
158 * @void *pv@ = pointer to output array
159 * @size_t sz@ = size of the output array
160 *
161 * Returns: ---
162 *
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$%.
170 */
171
172void mp_storeb(const mp *m, void *pv, size_t sz)
173{
174 mpx_storeb(m->v, m->vl, pv, sz);
175}
176
f09e814a 177/* --- @mp_loadl2c@ --- *
178 *
179 * Arguments: @mp *d@ = destination
180 * @const void *pv@ = pointer to source data
181 * @size_t sz@ = size of the source data
182 *
183 * Returns: Resulting multiprecision number.
184 *
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
187 * significant.
188 */
189
190mp *mp_loadl2c(mp *d, const void *pv, size_t sz)
191{
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);
196 d->f &= ~MP_NEG;
197 } else {
198 mpx_loadl2cn(d->v, d->vl, pv, sz);
199 d->f |= MP_NEG;
200 }
201 d->f &= ~MP_UNDEF;
202 mp_shrink(d);
203 return (d);
204}
205
206/* --- @mp_storel2c@ --- *
207 *
208 * Arguments: @const mp *m@ = source
209 * @void *pv@ = pointer to output array
210 * @size_t sz@ = size of the output array
211 *
212 * Returns: ---
213 *
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.
219 */
220
221void mp_storel2c(const mp *m, void *pv, size_t sz)
222{
a69a3efd 223 if (MP_NEGP(m))
f09e814a 224 mpx_storel2cn(m->v, m->vl, pv, sz);
225 else
226 mpx_storel(m->v, m->vl, pv, sz);
227}
228
229/* --- @mp_loadb2c@ --- *
230 *
231 * Arguments: @mp *d@ = destination
232 * @const void *pv@ = pointer to source data
233 * @size_t sz@ = size of the source data
234 *
235 * Returns: Resulting multiprecision number.
236 *
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
239 * significant.
240 */
241
242mp *mp_loadb2c(mp *d, const void *pv, size_t sz)
243{
244 const octet *ov = pv;
245 MP_DEST(d, MPW_RQ(sz), MP_UNDEF);
246 if (!sz || !(ov[0] & 0x80)) {
2bd53494 247 mpx_loadb(d->v, d->vl, pv, sz);
f09e814a 248 d->f &= ~MP_NEG;
249 } else {
2bd53494 250 mpx_loadb2cn(d->v, d->vl, pv, sz);
f09e814a 251 d->f |= MP_NEG;
252 }
253 d->f &= ~MP_UNDEF;
254 mp_shrink(d);
255 return (d);
256}
257
258/* --- @mp_storeb2c@ --- *
259 *
260 * Arguments: @const mp *m@ = source
261 * @void *pv@ = pointer to output array
262 * @size_t sz@ = size of the output array
263 *
264 * Returns: ---
265 *
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.
271 */
272
273void mp_storeb2c(const mp *m, void *pv, size_t sz)
274{
a69a3efd 275 if (MP_NEGP(m))
f09e814a 276 mpx_storeb2cn(m->v, m->vl, pv, sz);
277 else
278 mpx_storeb(m->v, m->vl, pv, sz);
279}
280
d3409d5e 281/*----- That's all, folks -------------------------------------------------*/