Add an internal-representation no-op function.
[u/mdw/catacomb] / mp-io.c
1 /* -*-c-*-
2 *
3 * $Id: mp-io.c,v 1.4 2000/06/17 11:45:09 mdw Exp $
4 *
5 * Loading and storing of multiprecision integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mp-io.c,v $
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).
37 *
38 * Revision 1.3 1999/11/21 22:13:02 mdw
39 * Add mp version of MPX_BITS.
40 *
41 * Revision 1.2 1999/11/19 13:19:06 mdw
42 * Set flags on results correctly.
43 *
44 * Revision 1.1 1999/11/17 18:02:16 mdw
45 * New multiprecision integer arithmetic suite.
46 *
47 */
48
49 /*----- Header files ------------------------------------------------------*/
50
51 #include "mp.h"
52
53 /*----- Main code ---------------------------------------------------------*/
54
55 /* --- @mp_octets@ --- *
56 *
57 * Arguments: @const mp *m@ = a multiprecision integer
58 *
59 * Returns: The number of octets required to represent @m@.
60 *
61 * Use: Calculates the external storage required for a multiprecision
62 * integer.
63 */
64
65 size_t mp_octets(const mp *m)
66 {
67 size_t sz;
68 MPX_OCTETS(sz, m->v, m->vl);
69 return (sz);
70 }
71
72 /* --- @mp_bits@ --- *
73 *
74 * Arguments: @const mp *m@ = a multiprecision integer
75 *
76 * Returns: The number of bits required to represent @m@.
77 *
78 * Use: Calculates the external storage required for a multiprecision
79 * integer.
80 */
81
82 unsigned long mp_bits(const mp *m)
83 {
84 unsigned long bits;
85 MPX_BITS(bits, m->v, m->vl);
86 return (bits);
87 }
88
89 /* --- @mp_loadl@ --- *
90 *
91 * Arguments: @mp *d@ = destination
92 * @const void *pv@ = pointer to source data
93 * @size_t sz@ = size of the source data
94 *
95 * Returns: Resulting multiprecision number.
96 *
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}$%.
101 */
102
103 mp *mp_loadl(mp *d, const void *pv, size_t sz)
104 {
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);
108 mp_shrink(d);
109 return (d);
110 }
111
112 /* --- @mp_storel@ --- *
113 *
114 * Arguments: @const mp *m@ = source
115 * @void *pv@ = pointer to output array
116 * @size_t sz@ = size of the output array
117 *
118 * Returns: ---
119 *
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}$%.
127 */
128
129 void mp_storel(const mp *m, void *pv, size_t sz)
130 {
131 mpx_storel(m->v, m->vl, pv, sz);
132 }
133
134 /* --- @mp_loadb@ --- *
135 *
136 * Arguments: @mp *d@ = destination
137 * @const void *pv@ = pointer to source data
138 * @size_t sz@ = size of the source data
139 *
140 * Returns: Resulting multiprecision number.
141 *
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}$%.
146 */
147
148 mp *mp_loadb(mp *d, const void *pv, size_t sz)
149 {
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);
153 mp_shrink(d);
154 return (d);
155 }
156
157 /* --- @mp_storeb@ --- *
158 *
159 * Arguments: @const mp *m@ = source
160 * @void *pv@ = pointer to output array
161 * @size_t sz@ = size of the output array
162 *
163 * Returns: ---
164 *
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$%.
172 */
173
174 void mp_storeb(const mp *m, void *pv, size_t sz)
175 {
176 mpx_storeb(m->v, m->vl, pv, sz);
177 }
178
179 /*----- That's all, folks -------------------------------------------------*/