aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolai Stange <nicstange@gmail.com>2016-05-26 17:19:55 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-05-31 04:42:01 -0400
commit20b5b7f3c2df2fb69b3b27dc83314b8891614ba5 (patch)
tree48df6ac9ff7c440bc8c26646dbc225e363ed6adf
parentcdf24b42c6740ec429e85a8405e5e917abac8595 (diff)
lib/mpi: refactor mpi_read_from_buffer() in terms of mpi_read_raw_data()
mpi_read_from_buffer() and mpi_read_raw_data() do basically the same thing except that the former extracts the number of payload bits from the first two bytes of the input buffer. Besides that, the data copying logic is exactly the same. Replace the open coded buffer to MPI instance conversion by a call to mpi_read_raw_data(). Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--lib/mpi/mpicoder.c24
1 files changed, 3 insertions, 21 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index f4f9e3396f3e..823cf5f5196b 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -80,10 +80,8 @@ EXPORT_SYMBOL_GPL(mpi_read_raw_data);
80MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) 80MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
81{ 81{
82 const uint8_t *buffer = xbuffer; 82 const uint8_t *buffer = xbuffer;
83 int i, j; 83 unsigned int nbits, nbytes;
84 unsigned nbits, nbytes, nlimbs; 84 MPI val;
85 mpi_limb_t a;
86 MPI val = NULL;
87 85
88 if (*ret_nread < 2) 86 if (*ret_nread < 2)
89 return ERR_PTR(-EINVAL); 87 return ERR_PTR(-EINVAL);
@@ -93,7 +91,6 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
93 pr_info("MPI: mpi too large (%u bits)\n", nbits); 91 pr_info("MPI: mpi too large (%u bits)\n", nbits);
94 return ERR_PTR(-EINVAL); 92 return ERR_PTR(-EINVAL);
95 } 93 }
96 buffer += 2;
97 94
98 nbytes = DIV_ROUND_UP(nbits, 8); 95 nbytes = DIV_ROUND_UP(nbits, 8);
99 if (nbytes + 2 > *ret_nread) { 96 if (nbytes + 2 > *ret_nread) {
@@ -102,24 +99,9 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
102 return ERR_PTR(-EINVAL); 99 return ERR_PTR(-EINVAL);
103 } 100 }
104 101
105 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); 102 val = mpi_read_raw_data(buffer + 2, nbytes);
106 val = mpi_alloc(nlimbs);
107 if (!val) 103 if (!val)
108 return ERR_PTR(-ENOMEM); 104 return ERR_PTR(-ENOMEM);
109 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
110 i %= BYTES_PER_MPI_LIMB;
111 val->nbits = nbits;
112 j = val->nlimbs = nlimbs;
113 val->sign = 0;
114 for (; j > 0; j--) {
115 a = 0;
116 for (; i < BYTES_PER_MPI_LIMB; i++) {
117 a <<= 8;
118 a |= *buffer++;
119 }
120 i = 0;
121 val->d[j - 1] = a;
122 }
123 105
124 *ret_nread = nbytes + 2; 106 *ret_nread = nbytes + 2;
125 return val; 107 return val;