]> git.karo-electronics.de Git - linux-beck.git/commitdiff
lib/mpi: mpi_read_buffer(): replace open coded endian conversion
authorNicolai Stange <nicstange@gmail.com>
Tue, 22 Mar 2016 12:12:41 +0000 (13:12 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Tue, 5 Apr 2016 12:35:49 +0000 (20:35 +0800)
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>
lib/mpi/mpicoder.c

index 2fd8d418526ca9c03f26a15c28c7416d5da6b3ae..a999ee1cddc5805638d598185dd40ba2c95e888e 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/bitops.h>
 #include <linux/count_zeros.h>
 #include <linux/byteorder/generic.h>
+#include <linux/string.h>
 #include "mpi-internal.h"
 
 #define MAX_EXTERN_MPI_BITS 16384
@@ -164,7 +165,13 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
                    int *sign)
 {
        uint8_t *p;
-       mpi_limb_t alimb;
+#if BYTES_PER_MPI_LIMB == 4
+       __be32 alimb;
+#elif BYTES_PER_MPI_LIMB == 8
+       __be64 alimb;
+#else
+#error please implement for this limb size.
+#endif
        unsigned int n = mpi_get_size(a);
        int i, lzeros;
 
@@ -187,25 +194,15 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
        for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
                        lzeros %= BYTES_PER_MPI_LIMB;
                i >= 0; i--) {
-               alimb = a->d[i];
 #if BYTES_PER_MPI_LIMB == 4
-               *p++ = alimb >> 24;
-               *p++ = alimb >> 16;
-               *p++ = alimb >> 8;
-               *p++ = alimb;
+               alimb = cpu_to_be32(a->d[i]);
 #elif BYTES_PER_MPI_LIMB == 8
-               *p++ = alimb >> 56;
-               *p++ = alimb >> 48;
-               *p++ = alimb >> 40;
-               *p++ = alimb >> 32;
-               *p++ = alimb >> 24;
-               *p++ = alimb >> 16;
-               *p++ = alimb >> 8;
-               *p++ = alimb;
+               alimb = cpu_to_be64(a->d[i]);
 #else
 #error please implement for this limb size.
 #endif
-
+               memcpy(p, &alimb, BYTES_PER_MPI_LIMB);
+               p += BYTES_PER_MPI_LIMB;
                if (lzeros > 0) {
                        mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
                        mpi_limb_t *limb2 = (void *)p - sizeof(alimb)