aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mpi/mpicoder.c
diff options
context:
space:
mode:
authorNicolai Stange <nicstange@gmail.com>2016-03-22 08:12:41 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-04-05 08:35:49 -0400
commit90f864e20029600a8dc33e27b1192af4636100d4 (patch)
tree73eba1689c2e633581f9fc84b2e1baf55d838d45 /lib/mpi/mpicoder.c
parentf00fa2417b3e1252b1a761f88731af03afff4407 (diff)
lib/mpi: mpi_read_buffer(): replace open coded endian conversion
Currently, the endian conversion from CPU order to BE is open coded in mpi_read_buffer(). Replace this by the centrally provided cpu_to_be*() macros. Copy from the temporary storage on stack to the destination buffer by means of memcpy(). Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'lib/mpi/mpicoder.c')
-rw-r--r--lib/mpi/mpicoder.c27
1 files changed, 12 insertions, 15 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index 2fd8d418526c..a999ee1cddc5 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -21,6 +21,7 @@
21#include <linux/bitops.h> 21#include <linux/bitops.h>
22#include <linux/count_zeros.h> 22#include <linux/count_zeros.h>
23#include <linux/byteorder/generic.h> 23#include <linux/byteorder/generic.h>
24#include <linux/string.h>
24#include "mpi-internal.h" 25#include "mpi-internal.h"
25 26
26#define MAX_EXTERN_MPI_BITS 16384 27#define MAX_EXTERN_MPI_BITS 16384
@@ -164,7 +165,13 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
164 int *sign) 165 int *sign)
165{ 166{
166 uint8_t *p; 167 uint8_t *p;
167 mpi_limb_t alimb; 168#if BYTES_PER_MPI_LIMB == 4
169 __be32 alimb;
170#elif BYTES_PER_MPI_LIMB == 8
171 __be64 alimb;
172#else
173#error please implement for this limb size.
174#endif
168 unsigned int n = mpi_get_size(a); 175 unsigned int n = mpi_get_size(a);
169 int i, lzeros; 176 int i, lzeros;
170 177
@@ -187,25 +194,15 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
187 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, 194 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
188 lzeros %= BYTES_PER_MPI_LIMB; 195 lzeros %= BYTES_PER_MPI_LIMB;
189 i >= 0; i--) { 196 i >= 0; i--) {
190 alimb = a->d[i];
191#if BYTES_PER_MPI_LIMB == 4 197#if BYTES_PER_MPI_LIMB == 4
192 *p++ = alimb >> 24; 198 alimb = cpu_to_be32(a->d[i]);
193 *p++ = alimb >> 16;
194 *p++ = alimb >> 8;
195 *p++ = alimb;
196#elif BYTES_PER_MPI_LIMB == 8 199#elif BYTES_PER_MPI_LIMB == 8
197 *p++ = alimb >> 56; 200 alimb = cpu_to_be64(a->d[i]);
198 *p++ = alimb >> 48;
199 *p++ = alimb >> 40;
200 *p++ = alimb >> 32;
201 *p++ = alimb >> 24;
202 *p++ = alimb >> 16;
203 *p++ = alimb >> 8;
204 *p++ = alimb;
205#else 201#else
206#error please implement for this limb size. 202#error please implement for this limb size.
207#endif 203#endif
208 204 memcpy(p, &alimb, BYTES_PER_MPI_LIMB);
205 p += BYTES_PER_MPI_LIMB;
209 if (lzeros > 0) { 206 if (lzeros > 0) {
210 mpi_limb_t *limb1 = (void *)p - sizeof(alimb); 207 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
211 mpi_limb_t *limb2 = (void *)p - sizeof(alimb) 208 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)