mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-28 08:37:25 +00:00
Simplify code
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
parent
5a5c0c5f0a
commit
c414ba3fc0
@ -185,28 +185,27 @@ int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
||||
const unsigned char *buf,
|
||||
size_t buflen )
|
||||
{
|
||||
const size_t limbs = CHARS_TO_LIMBS( buflen );
|
||||
size_t const limbs = CHARS_TO_LIMBS( buflen );
|
||||
|
||||
if( nx < limbs )
|
||||
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||
|
||||
if( X != NULL )
|
||||
/* If nx is 0, buflen must also be 0 (from previous test). Nothing to do. */
|
||||
if( nx == 0 )
|
||||
return( 0 );
|
||||
|
||||
memset( X, 0, nx * ciL );
|
||||
|
||||
/* memcpy() with (NULL, 0) is undefined behaviour */
|
||||
if( buflen != 0 )
|
||||
{
|
||||
memset( X, 0, nx * ciL );
|
||||
|
||||
const size_t overhead = ( nx * ciL ) - buflen;
|
||||
|
||||
/* Avoid calling `memcpy` with NULL source or destination argument,
|
||||
* even if buflen is 0. */
|
||||
if( buf != NULL )
|
||||
{
|
||||
unsigned char *Xp = (unsigned char *) X;
|
||||
memcpy( Xp + overhead, buf, buflen );
|
||||
|
||||
mbedtls_mpi_core_bigendian_to_host( X, nx );
|
||||
}
|
||||
size_t overhead = ( nx * ciL ) - buflen;
|
||||
unsigned char *Xp = (unsigned char *) X;
|
||||
memcpy( Xp + overhead, buf, buflen );
|
||||
}
|
||||
|
||||
mbedtls_mpi_core_bigendian_to_host( X, nx );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,10 @@ int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
||||
* most significant zero bytes in the input).
|
||||
*
|
||||
* \param X The address of the MPI.
|
||||
* May only be #NULL if \nx is 0 and \p buflen is 0.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param buf The input buffer to import from.
|
||||
* May only be #NULL if \p buflen is 0.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
|
Loading…
x
Reference in New Issue
Block a user