Improve consitency throughout library/common.h

Replace the contents of MBEDTLS_PUT_UINTx_yz contained inconsitent
but similar/duplicate code to the MBEDTLS_BYTE_x macros. Therefore
the contents of the macros now utilise the byte reading macros.

MBEDTLS_PUT_UINT64_LE's written order was also not consitent with
the other PUT macros, so that was modified.

Documentation comment said LSB instead of MSB and that has also been
resolved.

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
This commit is contained in:
Joe Subbiani 2021-07-19 15:29:18 +01:00
parent c54e908656
commit 896f4eeaf7

View file

@ -104,12 +104,12 @@
*/
#ifndef MBEDTLS_PUT_UINT32_BE
#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
do { \
( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \
( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \
( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \
( data )[( offset ) + 3] = (unsigned char) ( (n) ); \
} while( 0 )
{ \
( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
}
#endif
/**
@ -142,12 +142,12 @@
*/
#ifndef MBEDTLS_PUT_UINT32_LE
#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
do { \
( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
} while( 0 )
{ \
( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
}
#endif
/**
@ -179,14 +179,14 @@
#ifndef MBEDTLS_PUT_UINT16_LE
#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
{ \
( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
}
#endif
/**
* Get the unsigned 16 bits integer corresponding to two bytes in
* big-endian order (LSB first).
* big-endian order (MSB first).
*
* \param data Base address of the memory to get the two bytes from.
* \param offset Offset from \p base of the first and most significant
@ -213,8 +213,8 @@
#ifndef MBEDTLS_PUT_UINT16_BE
#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
{ \
( data )[( offset ) ] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
( data )[( offset ) + 1] = (unsigned char) ( ( (n) ) & 0xFF ); \
( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
}
#endif
@ -253,14 +253,14 @@
#ifndef MBEDTLS_PUT_UINT64_BE
#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
{ \
( data )[( offset ) ] = (unsigned char) ( (n) >> 56 ); \
( data )[( offset ) + 1] = (unsigned char) ( (n) >> 48 ); \
( data )[( offset ) + 2] = (unsigned char) ( (n) >> 40 ); \
( data )[( offset ) + 3] = (unsigned char) ( (n) >> 32 ); \
( data )[( offset ) + 4] = (unsigned char) ( (n) >> 24 ); \
( data )[( offset ) + 5] = (unsigned char) ( (n) >> 16 ); \
( data )[( offset ) + 6] = (unsigned char) ( (n) >> 8 ); \
( data )[( offset ) + 7] = (unsigned char) ( (n) ); \
( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
}
#endif
@ -299,14 +299,14 @@
#ifndef MBEDTLS_PUT_UINT64_LE
#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
{ \
( data )[( offset ) + 7] = (unsigned char) ( (n) >> 56 ); \
( data )[( offset ) + 6] = (unsigned char) ( (n) >> 48 ); \
( data )[( offset ) + 5] = (unsigned char) ( (n) >> 40 ); \
( data )[( offset ) + 4] = (unsigned char) ( (n) >> 32 ); \
( data )[( offset ) + 3] = (unsigned char) ( (n) >> 24 ); \
( data )[( offset ) + 2] = (unsigned char) ( (n) >> 16 ); \
( data )[( offset ) + 1] = (unsigned char) ( (n) >> 8 ); \
( data )[( offset ) ] = (unsigned char) ( (n) ); \
( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
}
#endif