Static analyzer bug fixes
[silc.git] / lib / silcmath / mp_tma.c
1 /*
2
3   mp_tma.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "mp_tma.h"
23
24 void silc_mp_init(SilcMPInt *mp)
25 {
26   (void)tma_mp_init(mp);
27 }
28
29 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp)
30 {
31   /* XXX TODO */
32   tma_mp_init(mp);
33   return TRUE;
34 }
35
36 void silc_mp_uninit(SilcMPInt *mp)
37 {
38   if (mp)
39     tma_mp_clear(mp);
40 }
41
42 size_t silc_mp_size(SilcMPInt *mp)
43 {
44   return tma_mp_unsigned_bin_size(mp);
45 }
46
47 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base)
48 {
49   int size = 0;
50   tma_mp_radix_size(mp, base, &size);
51   if (size > 1)
52     size--;
53   return size;
54 }
55
56 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src)
57 {
58   (void)tma_mp_copy(src, dst);
59 }
60
61 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui)
62 {
63   (void)tma_mp_set_int(dst, ui);
64 }
65
66 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si)
67 {
68   (void)tma_mp_set_int(dst, si);
69 }
70
71 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base)
72 {
73   (void)tma_mp_read_radix(dst, str, base);
74 }
75
76 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp)
77 {
78   return (SilcUInt32)tma_mp_get_int(mp);
79 }
80
81 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base)
82 {
83   if (tma_mp_toradix(mp, str, base) != MP_OKAY)
84     return NULL;
85   return str;
86 }
87
88 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
89 {
90   (void)tma_mp_add(mp1, mp2, dst);
91 }
92
93 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
94 {
95   tma_mp_add_d(mp1, (tma_mp_digit)ui, dst);
96 }
97
98 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
99 {
100   (void)tma_mp_sub(mp1, mp2, dst);
101 }
102
103 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
104 {
105   (void)tma_mp_sub_d(mp1, (tma_mp_digit)ui, dst);
106 }
107
108 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
109 {
110   (void)tma_mp_mul(mp1, mp2, dst);
111 }
112
113 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
114 {
115   (void)tma_mp_mul_d(mp1, (tma_mp_digit)ui, dst);
116 }
117
118 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
119 {
120   (void)tma_mp_mul_2d(mp1, exp, dst);
121 }
122
123 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src)
124 {
125   (void)tma_mp_sqrt(src, dst);
126 }
127
128 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
129 {
130   (void)tma_mp_div(mp1, mp2, dst, NULL);
131 }
132
133 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
134 {
135   (void)tma_mp_div_d(mp1, (tma_mp_digit)ui, dst, NULL);
136 }
137
138 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
139                     SilcMPInt *mp2)
140 {
141   (void)tma_mp_div(mp1, mp2, q, r);
142 }
143
144 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
145 {
146   (void)tma_mp_div_2d(mp1, exp, dst, NULL);
147 }
148
149 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
150                          SilcUInt32 exp)
151 {
152   (void)tma_mp_div_2d(mp1, exp, q, r);
153 }
154
155 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
156 {
157   (void)tma_mp_mod(mp1, mp2, dst);
158 }
159
160 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
161 {
162   tma_mp_digit d;
163   (void)tma_mp_mod_d(mp1, ui, &d);
164   silc_mp_set_ui(dst, d);
165 }
166
167 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
168 {
169   (void)tma_mp_mod_2d(mp1, ui, dst);
170 }
171
172 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp)
173 {
174   SILC_NOT_IMPLEMENTED("silc_mp_pow");
175   assert(FALSE);
176 }
177
178 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
179 {
180   (void)tma_mp_expt_d(mp1, (tma_mp_digit)exp, dst);
181 }
182
183 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
184                      SilcMPInt *mod)
185 {
186   (void)tma_mp_exptmod(mp1, exp, mod, dst);
187 }
188
189 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
190                         SilcMPInt *mod)
191 {
192   SilcMPInt tmp;
193   silc_mp_init(&tmp);
194   silc_mp_set_ui(&tmp, exp);
195   silc_mp_pow_mod(dst, mp1, &tmp, mod);
196   silc_mp_uninit(&tmp);
197 }
198
199 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
200 {
201   (void)tma_mp_gcd(mp1, mp2, dst);
202 }
203
204 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
205                     SilcMPInt *mp2)
206 {
207   (void)tma_mp_exteuclid(mp1, mp2, s, t, g);
208 }
209
210 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2)
211 {
212   return tma_mp_cmp(mp1, mp2);
213 }
214
215 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si)
216 {
217   return tma_mp_cmp_d(mp1, si);
218 }
219
220 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui)
221 {
222   return tma_mp_cmp_d(mp1, ui);
223 }
224
225 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src)
226 {
227   (void)tma_mp_abs(src, dst);
228 }
229
230 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src)
231 {
232   (void)tma_mp_neg(src, dst);
233 }
234
235 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
236 {
237   (void)tma_mp_and(mp1, mp2, dst);
238 }
239
240 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
241 {
242   (void)tma_mp_or(mp1, mp2, dst);
243 }
244
245 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
246 {
247   (void)tma_mp_xor(mp1, mp2, dst);
248 }