1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* Copyright (c) 2003 Bob Deblier
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*!\file mpnumber.c
* \brief Multi-precision numbers.
* \author Bob Deblier <bob.deblier@pandora.be>
* \ingroup MP_m
*/
#include "system.h"
#include "mpnumber.h"
#include "mp.h"
#include "debug.h"
void mpnzero(mpnumber* n)
{
n->size = 0;
n->data = (mpw*) 0;
}
/*@-compdef @*/ /* n->data not initialized */
void mpnsize(mpnumber* n, size_t size)
{
if (size)
{
if (n->data)
{
if (n->size != size)
n->data = (mpw*) realloc(n->data, size * sizeof(*n->data));
}
else
n->data = (mpw*) malloc(size * sizeof(*n->data));
if (n->data)
n->size = size;
else
{
n->size = 0;
n->data = (mpw*) 0;
}
}
else if (n->data)
{
free(n->data);
n->data = (mpw*) 0;
n->size = 0;
}
}
/*@=compdef @*/
void mpninit(mpnumber* n, size_t size, const mpw* data)
{
n->size = size;
if (n->data)
{
free(n->data);
n->data = (mpw*) 0;
}
n->data = (mpw*) malloc(size * sizeof(*n->data));
if (n->data && data)
mpcopy(size, n->data, data);
}
void mpnfree(mpnumber* n)
{
if (n->data)
{
free(n->data);
n->data = (mpw*) 0;
}
n->size = 0;
}
void mpncopy(mpnumber* n, const mpnumber* copy)
{
mpnset(n, copy->size, copy->data);
}
void mpnwipe(mpnumber* n)
{
if (n->data)
mpzero(n->size, n->data);
}
void mpnset(mpnumber* n, size_t size, const mpw* data)
{
if (size)
{
if (n->data)
{
if (n->size != size)
n->data = (mpw*) realloc(n->data, size * sizeof(*n->data));
}
else
n->data = (mpw*) malloc(size * sizeof(*n->data));
if (n->data && data)
/*@-nullpass@*/ /* data is notnull */
mpcopy(n->size = size, n->data, data);
/*@=nullpass@*/
else
{
n->size = 0;
n->data = (mpw*) 0;
}
}
else if (n->data)
{
free(n->data);
n->data = (mpw*) 0;
n->size = 0;
}
}
void mpnsetw(mpnumber* n, mpw val)
{
if (n->data)
{
if (n->size != 1)
n->data = (mpw*) realloc(n->data, 1 * sizeof(*n->data));
}
else
n->data = (mpw*) malloc(1 * sizeof(*n->data));
if (n->data)
{
n->size = 1;
n->data[0] = val;
}
else
{
n->size = 0;
n->data = (mpw*) 0;
}
}
/*@-usedef @*/ /* n->data may be NULL */
void mpnsethex(mpnumber* n, const char* hex)
{
register size_t len = strlen(hex);
register size_t size = MP_NIBBLES_TO_WORDS(len + MP_WNIBBLES - 1);
if (n->data)
{
if (n->size != size)
n->data = (mpw*) realloc(n->data, size * sizeof(*n->data));
}
else
n->data = (mpw*) malloc(size * sizeof(*n->data));
if (n->data)
{
n->size = size;
(void) hs2ip(n->data, size, hex, len);
}
else {
n->size = 0;
n->data = (mpw*)0;
}
}
/*@=usedef @*/
|