chiark / gitweb /
New multiprecision integer arithmetic suite.
[catacomb] / mp-io.c
1 /* -*-c-*-
2  *
3  * $Id: mp-io.c,v 1.1 1999/11/17 18:02:16 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.1  1999/11/17 18:02:16  mdw
34  * New multiprecision integer arithmetic suite.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include "mp.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @mp_octets@ --- *
45  *
46  * Arguments:   @const mp *m@ = a multiprecision integer
47  *
48  * Returns:     The number of octets required to represent @m@.
49  *
50  * Use:         Calculates the external storage required for a multiprecision
51  *              integer.
52  */
53
54 size_t mp_octets(const mp *m)
55 {
56   size_t sz;
57   MPX_OCTETS(sz, m->v, m->vl);
58   return (sz);
59 }
60
61 /* --- @mp_loadl@ --- *
62  *
63  * Arguments:   @mp *d@ = destination
64  *              @const void *pv@ = pointer to source data
65  *              @size_t sz@ = size of the source data
66  *
67  * Returns:     Resulting multiprecision number.
68  *
69  * Use:         Loads a multiprecision number from an array of octets.  The
70  *              first byte in the array is the least significant.  More
71  *              formally, if the bytes are %$b_0, b_1, \ldots, b_{n-1}$%
72  *              then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
73  */
74
75 mp *mp_loadl(mp *d, const void *pv, size_t sz)
76 {
77   MP_MODIFY(d, MPW_RQ(sz));
78   mpx_loadl(d->v, d->vl, pv, sz);
79   mp_shrink(d);
80   return (d);
81 }
82
83 /* --- @mp_storel@ --- *
84  *
85  * Arguments:   @const mp *m@ = source
86  *              @void *pv@ = pointer to output array
87  *              @size_t sz@ = size of the output array
88  *
89  * Returns:     ---
90  *
91  * Use:         Stores a multiprecision number in an array of octets.  The
92  *              first byte in the array is the least significant.  If the
93  *              array is too small to represent the number, high-order bits
94  *              are truncated; if the array is too large, high order bytes
95  *              are filled with zeros.  More formally, if the number is
96  *              %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
97  *              then the array is %$b_0, b_1, \ldots, b_{n-1}$%.
98  */
99
100 void mp_storel(const mp *m, void *pv, size_t sz)
101 {
102   mpx_storel(m->v, m->vl, pv, sz);
103 }
104
105 /* --- @mp_loadb@ --- *
106  *
107  * Arguments:   @mp *d@ = destination
108  *              @const void *pv@ = pointer to source data
109  *              @size_t sz@ = size of the source data
110  *
111  * Returns:     Resulting multiprecision number.
112  *
113  * Use:         Loads a multiprecision number from an array of octets.  The
114  *              last byte in the array is the least significant.  More
115  *              formally, if the bytes are %$b_{n-1}, b_{n-2}, \ldots, b_0$%
116  *              then the result is %$N = \sum_{0 \le i < n} b_i 2^{8i}$%.
117  */
118
119 mp *mp_loadb(mp *d, const void *pv, size_t sz)
120 {
121   MP_MODIFY(d, MPW_RQ(sz));
122   mpx_loadb(d->v, d->vl, pv, sz);
123   mp_shrink(d);
124   return (d);
125 }
126
127 /* --- @mp_storeb@ --- *
128  *
129  * Arguments:   @const mp *m@ = source
130  *              @void *pv@ = pointer to output array
131  *              @size_t sz@ = size of the output array
132  *
133  * Returns:     ---
134  *
135  * Use:         Stores a multiprecision number in an array of octets.  The
136  *              last byte in the array is the least significant.  If the
137  *              array is too small to represent the number, high-order bits
138  *              are truncated; if the array is too large, high order bytes
139  *              are filled with zeros.  More formally, if the number is
140  *              %$N = \sum{0 \le i} b_i 2^{8i}$% where %$0 \le b_i < 256$%,
141  *              then the array is %$b_{n-1}, b_{n-2}, \ldots, b_0$%.
142  */
143
144 void mp_storeb(const mp *m, void *pv, size_t sz)
145 {
146   mpx_storeb(m->v, m->vl, pv, sz);
147 }
148
149 /*----- That's all, folks -------------------------------------------------*/