3 * $Id: mp-io.c,v 1.2 1999/11/19 13:19:06 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.2 1999/11/19 13:19:06 mdw
34 * Set flags on results correctly.
36 * Revision 1.1 1999/11/17 18:02:16 mdw
37 * New multiprecision integer arithmetic suite.
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @mp_octets@ --- *
49 * Arguments: @const mp *m@ = a multiprecision integer
51 * Returns: The number of octets required to represent @m@.
53 * Use: Calculates the external storage required for a multiprecision
57 size_t mp_octets(const mp
*m
)
60 MPX_OCTETS(sz
, m
->v
, m
->vl
);
64 /* --- @mp_loadl@ --- *
66 * Arguments: @mp *d@ = destination
67 * @const void *pv@ = pointer to source data
68 * @size_t sz@ = size of the source data
70 * Returns: Resulting multiprecision number.
72 * Use: Loads a multiprecision number from an array of octets. The
73 * first byte in the array is the least significant. More
74 * formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
75 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
78 mp
*mp_loadl(mp
*d
, const void *pv
, size_t sz
)
80 MP_MODIFY(d
, MPW_RQ(sz
));
81 mpx_loadl(d
->v
, d
->vl
, pv
, sz
);
82 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
87 /* --- @mp_storel@ --- *
89 * Arguments: @const mp *m@ = source
90 * @void *pv@ = pointer to output array
91 * @size_t sz@ = size of the output array
95 * Use: Stores a multiprecision number in an array of octets. The
96 * first byte in the array is the least significant. If the
97 * array is too small to represent the number, high-order bits
98 * are truncated; if the array is too large, high order bytes
99 * are filled with zeros. More formally, if the number is
100 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
101 * then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
104 void mp_storel(const mp
*m
, void *pv
, size_t sz
)
106 mpx_storel(m
->v
, m
->vl
, pv
, sz
);
109 /* --- @mp_loadb@ --- *
111 * Arguments: @mp *d@ = destination
112 * @const void *pv@ = pointer to source data
113 * @size_t sz@ = size of the source data
115 * Returns: Resulting multiprecision number.
117 * Use: Loads a multiprecision number from an array of octets. The
118 * last byte in the array is the least significant. More
119 * formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
120 * then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
123 mp
*mp_loadb(mp
*d
, const void *pv
, size_t sz
)
125 MP_MODIFY(d
, MPW_RQ(sz
));
126 mpx_loadb(d
->v
, d
->vl
, pv
, sz
);
127 d
->f
&= ~(MP_UNDEF
| MP_NEG
);
132 /* --- @mp_storeb@ --- *
134 * Arguments: @const mp *m@ = source
135 * @void *pv@ = pointer to output array
136 * @size_t sz@ = size of the output array
140 * Use: Stores a multiprecision number in an array of octets. The
141 * last byte in the array is the least significant. If the
142 * array is too small to represent the number, high-order bits
143 * are truncated; if the array is too large, high order bytes
144 * are filled with zeros. More formally, if the number is
145 * %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
146 * then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
149 void mp_storeb(const mp
*m
, void *pv
, size_t sz
)
151 mpx_storeb(m
->v
, m
->vl
, pv
, sz
);
154 /*----- That's all, folks -------------------------------------------------*/