Merge branch 'next' of git://git.monstr.eu/linux-2.6-microblaze
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / mpi / mpiutil.c
blobeefc55d6b7f5b8a732992c070d5bca4bac65c374
1 /* mpiutil.ac - Utility functions for MPI
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "mpi-internal.h"
23 /****************
24 * Note: It was a bad idea to use the number of limbs to allocate
25 * because on a alpha the limbs are large but we normally need
26 * integers of n bits - So we should chnage this to bits (or bytes).
28 * But mpi_alloc is used in a lot of places :-)
30 MPI mpi_alloc(unsigned nlimbs)
32 MPI a;
34 a = kmalloc(sizeof *a, GFP_KERNEL);
35 if (!a)
36 return a;
38 if (nlimbs) {
39 a->d = mpi_alloc_limb_space(nlimbs);
40 if (!a->d) {
41 kfree(a);
42 return NULL;
44 } else {
45 a->d = NULL;
48 a->alloced = nlimbs;
49 a->nlimbs = 0;
50 a->sign = 0;
51 a->flags = 0;
52 a->nbits = 0;
53 return a;
55 EXPORT_SYMBOL_GPL(mpi_alloc);
57 mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
59 size_t len = nlimbs * sizeof(mpi_limb_t);
61 return kmalloc(len, GFP_KERNEL);
64 void mpi_free_limb_space(mpi_ptr_t a)
66 if (!a)
67 return;
69 kfree(a);
72 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
74 mpi_free_limb_space(a->d);
75 a->d = ap;
76 a->alloced = nlimbs;
79 /****************
80 * Resize the array of A to NLIMBS. the additional space is cleared
81 * (set to 0) [done by m_realloc()]
83 int mpi_resize(MPI a, unsigned nlimbs)
85 void *p;
87 if (nlimbs <= a->alloced)
88 return 0; /* no need to do it */
90 if (a->d) {
91 p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
92 if (!p)
93 return -ENOMEM;
94 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
95 kfree(a->d);
96 a->d = p;
97 } else {
98 a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
99 if (!a->d)
100 return -ENOMEM;
102 a->alloced = nlimbs;
103 return 0;
106 void mpi_clear(MPI a)
108 a->nlimbs = 0;
109 a->nbits = 0;
110 a->flags = 0;
113 void mpi_free(MPI a)
115 if (!a)
116 return;
118 if (a->flags & 4)
119 kfree(a->d);
120 else
121 mpi_free_limb_space(a->d);
123 if (a->flags & ~7)
124 pr_info("invalid flag value in mpi\n");
125 kfree(a);
127 EXPORT_SYMBOL_GPL(mpi_free);
129 /****************
130 * Note: This copy function should not interpret the MPI
131 * but copy it transparently.
133 int mpi_copy(MPI *copied, const MPI a)
135 size_t i;
136 MPI b;
138 *copied = MPI_NULL;
140 if (a) {
141 b = mpi_alloc(a->nlimbs);
142 if (!b)
143 return -ENOMEM;
145 b->nlimbs = a->nlimbs;
146 b->sign = a->sign;
147 b->flags = a->flags;
148 b->nbits = a->nbits;
150 for (i = 0; i < b->nlimbs; i++)
151 b->d[i] = a->d[i];
153 *copied = b;
156 return 0;
159 int mpi_set(MPI w, const MPI u)
161 mpi_ptr_t wp, up;
162 mpi_size_t usize = u->nlimbs;
163 int usign = u->sign;
165 if (RESIZE_IF_NEEDED(w, (size_t) usize) < 0)
166 return -ENOMEM;
168 wp = w->d;
169 up = u->d;
170 MPN_COPY(wp, up, usize);
171 w->nlimbs = usize;
172 w->nbits = u->nbits;
173 w->flags = u->flags;
174 w->sign = usign;
175 return 0;
178 int mpi_set_ui(MPI w, unsigned long u)
180 if (RESIZE_IF_NEEDED(w, 1) < 0)
181 return -ENOMEM;
182 w->d[0] = u;
183 w->nlimbs = u ? 1 : 0;
184 w->sign = 0;
185 w->nbits = 0;
186 w->flags = 0;
187 return 0;
190 MPI mpi_alloc_set_ui(unsigned long u)
192 MPI w = mpi_alloc(1);
193 if (!w)
194 return w;
195 w->d[0] = u;
196 w->nlimbs = u ? 1 : 0;
197 w->sign = 0;
198 return w;
201 void mpi_swap(MPI a, MPI b)
203 struct gcry_mpi tmp;
205 tmp = *a;
206 *a = *b;
207 *b = tmp;