Set flags on results correctly.
[u/mdw/catacomb] / mp-io.c
1 /* -*-c-*-
2 *
3 * $Id: mp-io.c,v 1.2 1999/11/19 13:19:06 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.2 1999/11/19 13:19:06 mdw
34 * Set flags on results correctly.
35 *
36 * Revision 1.1 1999/11/17 18:02:16 mdw
37 * New multiprecision integer arithmetic suite.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include "mp.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @mp_octets@ --- *
48 *
49 * Arguments: @const mp *m@ = a multiprecision integer
50 *
51 * Returns: The number of octets required to represent @m@.
52 *
53 * Use: Calculates the external storage required for a multiprecision
54 * integer.
55 */
56
57 size_t mp_octets(const mp *m)
58 {
59 size_t sz;
60 MPX_OCTETS(sz, m->v, m->vl);
61 return (sz);
62 }
63
64 /* --- @mp_loadl@ --- *
65 *
66 * Arguments: @mp *d@ = destination
67 * @const void *pv@ = pointer to source data
68 * @size_t sz@ = size of the source data
69 *
70 * Returns: Resulting multiprecision number.
71 *
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}$%.
76 */
77
78 mp *mp_loadl(mp *d, const void *pv, size_t sz)
79 {
80 MP_MODIFY(d, MPW_RQ(sz));
81 mpx_loadl(d->v, d->vl, pv, sz);
82 d->f &= ~(MP_UNDEF | MP_NEG);
83 mp_shrink(d);
84 return (d);
85 }
86
87 /* --- @mp_storel@ --- *
88 *
89 * Arguments: @const mp *m@ = source
90 * @void *pv@ = pointer to output array
91 * @size_t sz@ = size of the output array
92 *
93 * Returns: ---
94 *
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}$%.
102 */
103
104 void mp_storel(const mp *m, void *pv, size_t sz)
105 {
106 mpx_storel(m->v, m->vl, pv, sz);
107 }
108
109 /* --- @mp_loadb@ --- *
110 *
111 * Arguments: @mp *d@ = destination
112 * @const void *pv@ = pointer to source data
113 * @size_t sz@ = size of the source data
114 *
115 * Returns: Resulting multiprecision number.
116 *
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}$%.
121 */
122
123 mp *mp_loadb(mp *d, const void *pv, size_t sz)
124 {
125 MP_MODIFY(d, MPW_RQ(sz));
126 mpx_loadb(d->v, d->vl, pv, sz);
127 d->f &= ~(MP_UNDEF | MP_NEG);
128 mp_shrink(d);
129 return (d);
130 }
131
132 /* --- @mp_storeb@ --- *
133 *
134 * Arguments: @const mp *m@ = source
135 * @void *pv@ = pointer to output array
136 * @size_t sz@ = size of the output array
137 *
138 * Returns: ---
139 *
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$%.
147 */
148
149 void mp_storeb(const mp *m, void *pv, size_t sz)
150 {
151 mpx_storeb(m->v, m->vl, pv, sz);
152 }
153
154 /*----- That's all, folks -------------------------------------------------*/