Skip to content

Commit

Permalink
Revert "Removed Utils for Byte Array packing"
Browse files Browse the repository at this point in the history
This reverts commit e920df4.
  • Loading branch information
gabryelreyes committed Oct 27, 2023
1 parent 782bd46 commit 45a588e
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 29 deletions.
93 changes: 93 additions & 0 deletions lib/Service/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,99 @@ void Util::intToStr(char* str, size_t size, int32_t value)
}
}

void Util::int16ToByteArray(uint8_t* buffer, size_t size, int16_t value)
{
if ((nullptr != buffer) && (sizeof(int16_t) <= size))
{
buffer[0U] = ((value >> 8U) & 0xFF);
buffer[1U] = ((value >> 0U) & 0xFF);
}
}

void Util::uint16ToByteArray(uint8_t* buffer, size_t size, uint16_t value)
{
if ((nullptr != buffer) && (sizeof(uint16_t) <= size))
{
buffer[0U] = ((value >> 8U) & 0xFF);
buffer[1U] = ((value >> 0U) & 0xFF);
}
}

void Util::int32ToByteArray(uint8_t* buffer, size_t size, int32_t value)
{
if ((nullptr != buffer) && (sizeof(int32_t) <= size))
{
uint16_t hiBytes = ((value >> 16U) & 0xFFFF);
uint16_t lowBytes = ((value >> 0U) & 0xFFFF);

buffer[0U] = ((hiBytes >> 8U) & 0xFF);
buffer[1U] = ((hiBytes >> 0U) & 0xFF);
buffer[2U] = ((lowBytes >> 8U) & 0xFF);
buffer[3U] = ((lowBytes >> 0U) & 0xFF);
}
}

void Util::uint32ToByteArray(uint8_t* buffer, size_t size, uint32_t value)
{
if ((nullptr != buffer) && (sizeof(uint32_t) <= size))
{
uint16_t hiBytes = ((value >> 16U) & 0xFFFF);
uint16_t lowBytes = ((value >> 0U) & 0xFFFF);

buffer[0U] = ((hiBytes >> 8U) & 0xFF);
buffer[1U] = ((hiBytes >> 0U) & 0xFF);
buffer[2U] = ((lowBytes >> 8U) & 0xFF);
buffer[3U] = ((lowBytes >> 0U) & 0xFF);
}
}

bool Util::byteArrayToInt16(const uint8_t* buffer, size_t size, int16_t& value)
{
bool isSuccess = false;

if ((nullptr != buffer) && (sizeof(int16_t) <= size))
{
value = ((static_cast<int16_t>(buffer[0U]) << 8U) & 0xFF00) |
((static_cast<int16_t>(buffer[1U]) << 0U) & 0x00FF) ;

isSuccess = true;
}

return isSuccess;
}

bool Util::byteArrayToUint16(const uint8_t* buffer, size_t size, uint16_t& value)
{
bool isSuccess = false;

if ((nullptr != buffer) && (sizeof(uint16_t) <= size))
{
value = ((static_cast<uint16_t>(buffer[0U]) << 8U) & 0xFF00) |
((static_cast<uint16_t>(buffer[1U]) << 0U) & 0x00FF) ;

isSuccess = true;
}

return isSuccess;
}

bool Util::byteArrayToUint32(const uint8_t* buffer, size_t size, uint32_t& value)
{
bool isSuccess = false;

if ((nullptr != buffer) && (sizeof(uint32_t) <= size))
{
value = ((static_cast<uint32_t>(buffer[0U]) << 24U) & 0xFF000000) |
((static_cast<uint32_t>(buffer[1U]) << 16U) & 0x00FF0000) |
((static_cast<uint32_t>(buffer[2U]) << 8U) & 0x0000FF00) |
((static_cast<uint32_t>(buffer[3U]) << 0U) & 0x000000FF) ;

isSuccess = true;
}

return isSuccess;
}

/******************************************************************************
* Local Functions
*****************************************************************************/
121 changes: 92 additions & 29 deletions lib/Service/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,98 @@
namespace Util
{

/******************************************************************************
* Macros
*****************************************************************************/

/******************************************************************************
* Types and Classes
*****************************************************************************/

/******************************************************************************
* Functions
*****************************************************************************/

/**
* Unsigned integer to string, without preceeding zeros.
*
* @param[out] str Destination string
* @param[in] size Size of the destination string in byte
* @param[in] value Value
*/
void uintToStr(char* str, size_t size, uint32_t value);

/**
* Signed integer to string, without preceeding zeros.
*
* @param[out] str Destination string
* @param[in] size Size of the destination string in byte
* @param[in] value Value
*/
void intToStr(char* str, size_t size, int32_t value);
/******************************************************************************
* Macros
*****************************************************************************/

/******************************************************************************
* Types and Classes
*****************************************************************************/

/******************************************************************************
* Functions
*****************************************************************************/

/**
* Unsigned integer to string, without preceeding zeros.
*
* @param[out] str Destination string
* @param[in] size Size of the destination string in byte
* @param[in] value Value
*/
void uintToStr(char* str, size_t size, uint32_t value);

/**
* Signed integer to string, without preceeding zeros.
*
* @param[out] str Destination string
* @param[in] size Size of the destination string in byte
* @param[in] value Value
*/
void intToStr(char* str, size_t size, int32_t value);

/**
* Signed 16-bit integer to byte array.
* Endianness: Big endian
* @param[out] buffer Destination array
* @param[in] size Size of the destination buffer in byte
* @param[in] value Value
*/
void int16ToByteArray(uint8_t* buffer, size_t size, int16_t value);

/**
* Unsigned 16-bit integer to byte array.
* Endianness: Big endian
* @param[out] buffer Destination array
* @param[in] size Size of the destination buffer in byte
* @param[in] value Value
*/
void uint16ToByteArray(uint8_t* buffer, size_t size, uint16_t value);

/**
* Signed 32-bit integer to byte array.
* Endianness: Big endian
* @param[out] buffer Destination array
* @param[in] size Size of the destination buffer in byte
* @param[in] value Value
*/
void int32ToByteArray(uint8_t* buffer, size_t size, int32_t value);

/**
* Unsigned 32-bit integer to byte array.
* Endianness: Big endian.
* @param[out] buffer Destination array.
* @param[in] size Size of the destination buffer in byte.
* @param[in] value Value.
*/
void uint32ToByteArray(uint8_t* buffer, size_t size, uint32_t value);

/**
* Big endian byte array to int16_t.
* @param[in] buffer Source Array.
* @param[in] size Size of source array.
* @param[out] value Destination integer.
* @returns true if succesfully parsed. Otherwise, false.
*/
bool byteArrayToInt16(const uint8_t* buffer, size_t size, int16_t& value);

/**
* Big endian byte array to uint16_t.
* @param[in] buffer Source Array.
* @param[in] size Size of source array.
* @param[out] value Destination integer.
* @returns true if succesfully parsed. Otherwise, false.
*/
bool byteArrayToUint16(const uint8_t* buffer, size_t size, uint16_t& value);

/**
* Big endian byte array to uint32_t.
* @param[in] buffer Source Array.
* @param[in] size Size of source array.
* @param[out] value Destination integer.
* @returns true if succesfully parsed. Otherwise, false.
*/
bool byteArrayToUint32(const uint8_t* buffer, size_t size, uint32_t& value);

} // namespace Util

Expand Down

0 comments on commit 45a588e

Please sign in to comment.