MPI: Fix mpi_read_buffer
authorTadeusz Struk <tadeusz.struk@intel.com>
Mon, 24 Aug 2015 14:52:14 +0000 (07:52 -0700)
committerHerbert Xu <herbert@gondor.apana.org.au>
Tue, 25 Aug 2015 13:13:16 +0000 (21:13 +0800)
Change mpi_read_buffer to return a number without leading zeros
so that mpi_read_buffer and mpi_get_buffer return the same thing.

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
lib/mpi/mpicoder.c

index bc0a1da8afba2c5362fb55772e7154a8ad2ea844..95c52a95259e89b6fc0ab279c4e51cfc5f35bf1f 100644 (file)
@@ -146,18 +146,25 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
        uint8_t *p;
        mpi_limb_t alimb;
        unsigned int n = mpi_get_size(a);
-       int i;
+       int i, lzeros = 0;
 
-       if (buf_len < n || !buf)
+       if (buf_len < n || !buf || !nbytes)
                return -EINVAL;
 
        if (sign)
                *sign = a->sign;
 
-       if (nbytes)
-               *nbytes = n;
+       p = (void *)&a->d[a->nlimbs] - 1;
+
+       for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
+               if (!*p)
+                       lzeros++;
+               else
+                       break;
+       }
 
        p = buf;
+       *nbytes = n - lzeros;
 
        for (i = a->nlimbs - 1; i >= 0; i--) {
                alimb = a->d[i];
@@ -178,6 +185,19 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
 #else
 #error please implement for this limb size.
 #endif
+
+               if (lzeros > 0) {
+                       if (lzeros >= sizeof(alimb)) {
+                               p -= sizeof(alimb);
+                       } else {
+                               mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
+                               mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
+                                                       + lzeros;
+                               *limb1 = *limb2;
+                               p -= lzeros;
+                       }
+                       lzeros -= sizeof(alimb);
+               }
        }
        return 0;
 }
@@ -197,7 +217,7 @@ EXPORT_SYMBOL_GPL(mpi_read_buffer);
  */
 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
 {
-       uint8_t *buf, *p;
+       uint8_t *buf;
        unsigned int n;
        int ret;
 
@@ -220,14 +240,6 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
                kfree(buf);
                return NULL;
        }
-
-       /* this is sub-optimal but we need to do the shift operation
-        * because the caller has to free the returned buffer */
-       for (p = buf; !*p && *nbytes; p++, --*nbytes)
-               ;
-       if (p != buf)
-               memmove(buf, p, *nbytes);
-
        return buf;
 }
 EXPORT_SYMBOL_GPL(mpi_get_buffer);