From 2aa74fc5862e0d40299573a21b149f64c48d2e8f Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Sat, 13 Aug 2011 20:13:13 -0300 Subject: [PATCH] Added the 32 bits version of encode and decode methods --- src/host/messagepayload.cpp | 72 ++++++++++++++++++++++++++++++++++++++++-- src/host/messagepayload.h | 10 ++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index b857b75..6c32e4e 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -80,11 +80,12 @@ uint16_t MessagePayload::decode16( BOOST_ASSERT( left_byte < right_byte ); BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) ); - uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + uint32_t value = static_cast( Payload[ left_byte ] << 8 ); + value += static_cast( Payload[ right_byte ] ); BOOST_ASSERT( value <= numeric_limits::max() ); - return static_cast ( value ); + return static_cast( value ); } /** @@ -108,8 +109,71 @@ void MessagePayload::encode16( BOOST_ASSERT( left_byte < right_byte ); BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) ); - Payload[ left_byte ] = static_cast ( value >> 8 ); - Payload[ right_byte ] = static_cast ( value & 0xFF ); + Payload[ left_byte ] = static_cast( value >> 8 ); + Payload[ right_byte ] = static_cast( value & 0xFF ); +} + +/** + * @brief Retrieve 32 bits from the payload buffer. + * + * @param first_byte The index of the first byte out of 4 + * @param last_byte The index of the last byte out of 4 + * + * @return a concatenation of 4 bytes, from the byte indexed by first_byte to + * the byte indexed by last_byte. + */ +uint32_t MessagePayload::decode32( + const int first_byte, + const int last_byte +) const +{ + BOOST_ASSERT( ( 0 <= first_byte ) && ( first_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 <= last_byte ) && ( last_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( first_byte < last_byte ); + BOOST_ASSERT( ( last_byte - first_byte ) == sizeof(uint32_t) ); + + int current_byte = first_byte; + + uint64_t value = static_cast( Payload[ current_byte ] << 24 ); + value += static_cast( Payload[ ++current_byte ] << 16 ); + value += static_cast( Payload[ ++current_byte ] << 8 ); + value += static_cast( Payload[ ++current_byte ] ); + + BOOST_ASSERT( value <= numeric_limits::max() ); + BOOST_ASSERT( current_byte == last_byte ); + + return static_cast( value ); +} + +/** + * @brief Store 32 bits in the payload buffer. + * + * @param first_byte The index of the first byte out of 4 + * @param last_byte The index of the last byte out of 4 + * @param value a 32 bits data be saved in the bytes indexed from the first_byte + * to the last_byte. + * + * @return void + */ +void MessagePayload::encode32( + const int first_byte, + const int last_byte, + const uint32_t value +) +{ + BOOST_ASSERT( ( 0 < first_byte ) && ( first_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( ( 0 < last_byte ) && ( last_byte < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( first_byte < last_byte ); + BOOST_ASSERT( ( last_byte - first_byte ) == sizeof(uint32_t) ); + + int current_byte = first_byte; + + Payload[ current_byte ] = static_cast( ( value >> 24 ) & 0xFF ); + Payload[ ++current_byte ] = static_cast( ( value >> 16 ) & 0xFF ); + Payload[ ++current_byte ] = static_cast( ( value >> 8 ) & 0xFF ); + Payload[ ++current_byte ] = static_cast( value & 0xFF ); + + BOOST_ASSERT( current_byte == last_byte ); } /** diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h index 5bc19e2..bbe40ed 100644 --- a/src/host/messagepayload.h +++ b/src/host/messagepayload.h @@ -48,6 +48,16 @@ public: const uint16_t value ); + uint32_t decode32( + const int first_byte, + const int last_byte + ) const; + void encode32( + const int first_byte, + const int last_byte, + const uint32_t value + ); + std::istream& read( std::istream &is ); std::ostream& write( std::ostream &os ) const; -- 1.7.1