d3409d5e |
1 | /* -*-c-*- |
2 | * |
24e1e862 |
3 | * $Id: mp-io.c,v 1.3 1999/11/21 22:13:02 mdw Exp $ |
d3409d5e |
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 $ |
24e1e862 |
33 | * Revision 1.3 1999/11/21 22:13:02 mdw |
34 | * Add mp version of MPX_BITS. |
35 | * |
8857f8eb |
36 | * Revision 1.2 1999/11/19 13:19:06 mdw |
37 | * Set flags on results correctly. |
38 | * |
d3409d5e |
39 | * Revision 1.1 1999/11/17 18:02:16 mdw |
40 | * New multiprecision integer arithmetic suite. |
41 | * |
42 | */ |
43 | |
44 | /*----- Header files ------------------------------------------------------*/ |
45 | |
46 | #include "mp.h" |
47 | |
48 | /*----- Main code ---------------------------------------------------------*/ |
49 | |
50 | /* --- @mp_octets@ --- * |
51 | * |
52 | * Arguments: @const mp *m@ = a multiprecision integer |
53 | * |
54 | * Returns: The number of octets required to represent @m@. |
55 | * |
56 | * Use: Calculates the external storage required for a multiprecision |
57 | * integer. |
58 | */ |
59 | |
60 | size_t mp_octets(const mp *m) |
61 | { |
62 | size_t sz; |
63 | MPX_OCTETS(sz, m->v, m->vl); |
64 | return (sz); |
65 | } |
66 | |
24e1e862 |
67 | /* --- @mp_bits@ --- * |
68 | * |
69 | * Arguments: @const mp *m@ = a multiprecision integer |
70 | * |
71 | * Returns: The number of bits required to represent @m@. |
72 | * |
73 | * Use: Calculates the external storage required for a multiprecision |
74 | * integer. |
75 | */ |
76 | |
77 | unsigned long mp_bits(const mp *m) |
78 | { |
79 | unsigned long bits; |
80 | MPX_BITS(bits, m->v, m->vl); |
81 | return (bits); |
82 | } |
83 | |
d3409d5e |
84 | /* --- @mp_loadl@ --- * |
85 | * |
86 | * Arguments: @mp *d@ = destination |
87 | * @const void *pv@ = pointer to source data |
88 | * @size_t sz@ = size of the source data |
89 | * |
90 | * Returns: Resulting multiprecision number. |
91 | * |
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}$%. |
96 | */ |
97 | |
98 | mp *mp_loadl(mp *d, const void *pv, size_t sz) |
99 | { |
100 | MP_MODIFY(d, MPW_RQ(sz)); |
101 | mpx_loadl(d->v, d->vl, pv, sz); |
8857f8eb |
102 | d->f &= ~(MP_UNDEF | MP_NEG); |
d3409d5e |
103 | mp_shrink(d); |
104 | return (d); |
105 | } |
106 | |
107 | /* --- @mp_storel@ --- * |
108 | * |
109 | * Arguments: @const mp *m@ = source |
110 | * @void *pv@ = pointer to output array |
111 | * @size_t sz@ = size of the output array |
112 | * |
113 | * Returns: --- |
114 | * |
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}$%. |
122 | */ |
123 | |
124 | void mp_storel(const mp *m, void *pv, size_t sz) |
125 | { |
126 | mpx_storel(m->v, m->vl, pv, sz); |
127 | } |
128 | |
129 | /* --- @mp_loadb@ --- * |
130 | * |
131 | * Arguments: @mp *d@ = destination |
132 | * @const void *pv@ = pointer to source data |
133 | * @size_t sz@ = size of the source data |
134 | * |
135 | * Returns: Resulting multiprecision number. |
136 | * |
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}$%. |
141 | */ |
142 | |
143 | mp *mp_loadb(mp *d, const void *pv, size_t sz) |
144 | { |
145 | MP_MODIFY(d, MPW_RQ(sz)); |
146 | mpx_loadb(d->v, d->vl, pv, sz); |
8857f8eb |
147 | d->f &= ~(MP_UNDEF | MP_NEG); |
d3409d5e |
148 | mp_shrink(d); |
149 | return (d); |
150 | } |
151 | |
152 | /* --- @mp_storeb@ --- * |
153 | * |
154 | * Arguments: @const mp *m@ = source |
155 | * @void *pv@ = pointer to output array |
156 | * @size_t sz@ = size of the output array |
157 | * |
158 | * Returns: --- |
159 | * |
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$%. |
167 | */ |
168 | |
169 | void mp_storeb(const mp *m, void *pv, size_t sz) |
170 | { |
171 | mpx_storeb(m->v, m->vl, pv, sz); |
172 | } |
173 | |
174 | /*----- That's all, folks -------------------------------------------------*/ |