2016-06-15 23:48:41 +02:00
|
|
|
#ifndef ENDIANSWAPPER_H_
|
|
|
|
#define ENDIANSWAPPER_H_
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/osal/Endiness.h>
|
|
|
|
#include <cstring>
|
2018-07-13 18:28:26 +02:00
|
|
|
#include <iostream>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-12-08 22:57:03 +01:00
|
|
|
/**
|
|
|
|
* @brief Can be used to swap endianness of data
|
|
|
|
* into big endian
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
class EndianSwapper {
|
|
|
|
private:
|
|
|
|
EndianSwapper() {
|
|
|
|
}
|
|
|
|
;
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static T swap(T in) {
|
2018-07-13 18:28:26 +02:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
T tmp;
|
|
|
|
uint8_t *pointerOut = (uint8_t *) &tmp;
|
|
|
|
uint8_t *pointerIn = (uint8_t *) ∈
|
|
|
|
for (uint8_t count = 0; count < sizeof(T); count++) {
|
|
|
|
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
|
|
|
}
|
|
|
|
return tmp;
|
2018-07-13 18:28:26 +02:00
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
return in;
|
2018-07-13 15:56:37 +02:00
|
|
|
#else
|
|
|
|
#error Unknown Byte Order
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static void swap(uint8_t* out, const uint8_t* in, uint32_t size) {
|
2018-07-13 18:28:26 +02:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
for (uint8_t count = 0; count < size; count++) {
|
|
|
|
out[size - count - 1] = in[count];
|
|
|
|
}
|
|
|
|
return;
|
2018-07-13 18:28:26 +02:00
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
2016-06-15 23:48:41 +02:00
|
|
|
memcpy(out, in, size);
|
|
|
|
return;
|
2019-12-09 12:27:14 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-01-16 18:46:29 +01:00
|
|
|
static void swap(uint8_t * out, const uint8_t * in, uint32_t size) {
|
2019-12-09 12:27:14 +01:00
|
|
|
#ifndef BYTE_ORDER_SYSTEM
|
|
|
|
#error BYTE_ORDER_SYSTEM not defined
|
|
|
|
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
|
|
|
for (uint8_t count = 0; count < size; count++) {
|
|
|
|
for(uint8_t i = 0; i < sizeof(T);i++) {
|
2020-01-16 18:46:29 +01:00
|
|
|
out[sizeof(T)* (count + 1) - i - 1] = in[count * sizeof(T) + i];
|
2019-12-09 12:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
|
|
|
memcpy(out, in, size*sizeof(T));
|
|
|
|
return;
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ENDIANSWAPPER_H_ */
|