diff --git a/datapoolglob/PoolRawAccess.cpp b/datapoolglob/PoolRawAccess.cpp index bec54ab3..cd4138d3 100644 --- a/datapoolglob/PoolRawAccess.cpp +++ b/datapoolglob/PoolRawAccess.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include @@ -130,15 +130,7 @@ ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t* buffer, return DATA_POOL_ACCESS_FAILED; if (typeSize > max_size) return INCORRECT_SIZE; -#ifndef BYTE_ORDER_SYSTEM -#error BYTE_ORDER_SYSTEM not defined -#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN - for (uint8_t count = 0; count < typeSize; count++) { - buffer[count] = data_ptr[typeSize - count - 1]; - } -#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN - memcpy(buffer, data_ptr, typeSize); -#endif + EndianConverter::convertBigEndian(buffer, data_ptr, typeSize); *writtenBytes = typeSize; return HasReturnvaluesIF::RETURN_OK; } @@ -146,21 +138,18 @@ ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t* buffer, ReturnValue_t PoolRawAccess::serialize(uint8_t** buffer, size_t* size, size_t maxSize, Endianness streamEndianness) const { - //TODO integer overflow if (typeSize + *size <= maxSize) { -#warning use endian swapper - if (1) { -#ifndef BYTE_ORDER_SYSTEM -#error BYTE_ORDER_SYSTEM not defined -#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN - for (uint8_t count = 0; count < typeSize; count++) { - (*buffer)[count] = value[typeSize - count - 1]; - } -#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN - memcpy(*buffer, value, typeSize); -#endif - } else { + switch(streamEndianness) { + case(Endianness::BIG): + EndianConverter::convertBigEndian(*buffer, value, typeSize); + break; + case(Endianness::LITTLE): + EndianConverter::convertLittleEndian(*buffer, value, typeSize); + break; + case(Endianness::MACHINE): + default: memcpy(*buffer, value, typeSize); + break; } *size += typeSize; (*buffer) += typeSize; @@ -175,11 +164,11 @@ Type PoolRawAccess::getType() { return type; } -uint8_t PoolRawAccess::getSizeOfType() { +size_t PoolRawAccess::getSizeOfType() { return typeSize; } -uint8_t PoolRawAccess::getArraySize(){ +size_t PoolRawAccess::getArraySize(){ return arraySize; } @@ -191,22 +180,14 @@ PoolVariableIF::ReadWriteMode_t PoolRawAccess::getReadWriteMode() const { return readWriteMode; } -ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t* buffer, - uint32_t setSize) { +ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t *buffer, + size_t setSize) { if (typeSize == setSize) { -#ifndef BYTE_ORDER_SYSTEM -#error BYTE_ORDER_SYSTEM not defined -#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN - for (uint8_t count = 0; count < typeSize; count++) { - value[count] = buffer[typeSize - count - 1]; - } -#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN - memcpy(value, buffer, typeSize); -#endif + EndianConverter::convertBigEndian(value, buffer, typeSize); return HasReturnvaluesIF::RETURN_OK; } else { - sif::error << "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal" - << (uint32_t) typeSize << ", Requested: " << setSize + sif::error << "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: " + "Internal" << (uint32_t) typeSize << ", Requested: " << setSize << std::endl; return INCORRECT_SIZE; } @@ -232,27 +213,24 @@ size_t PoolRawAccess::getSerializedSize() const { return typeSize; } -ReturnValue_t PoolRawAccess::deSerialize(const uint8_t** buffer, size_t* size, +ReturnValue_t PoolRawAccess::deSerialize(const uint8_t **buffer, size_t *size, Endianness streamEndianness) { if (*size >= typeSize) { - *size -= typeSize; - if (1) { -#ifndef BYTE_ORDER_SYSTEM -#error BYTE_ORDER_SYSTEM not defined -#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN - for (uint8_t count = 0; count < typeSize; count++) { - value[count] = (*buffer)[typeSize - count - 1]; - } -#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN - memcpy(value, *buffer, typeSize); -#endif - } else { + switch(streamEndianness) { + case(Endianness::BIG): + EndianConverter::convertBigEndian(value, *buffer, typeSize); + break; + case(Endianness::LITTLE): + EndianConverter::convertLittleEndian(value, *buffer, typeSize); + break; + case(Endianness::MACHINE): + default: memcpy(value, *buffer, typeSize); + break; } + *size -= typeSize; *buffer += typeSize; return HasReturnvaluesIF::RETURN_OK; - } else { - return SerializeIF::STREAM_TOO_SHORT; } } diff --git a/datapoolglob/PoolRawAccess.h b/datapoolglob/PoolRawAccess.h index ea07356e..2ef1d653 100644 --- a/datapoolglob/PoolRawAccess.h +++ b/datapoolglob/PoolRawAccess.h @@ -85,7 +85,7 @@ public: * - @c RETURN_FAILED on failure */ ReturnValue_t setEntryFromBigEndian(const uint8_t* buffer, - uint32_t setSize); + size_t setSize); /** * @brief This operation returns the type of the entry currently stored. */ @@ -93,12 +93,12 @@ public: /** * @brief This operation returns the size of the entry currently stored. */ - uint8_t getSizeOfType(); + size_t getSizeOfType(); /** * * @return the size of the datapool array */ - uint8_t getArraySize(); + size_t getArraySize(); /** * @brief This operation returns the data pool id of the variable. */