2016-06-15 23:48:41 +02:00
|
|
|
#ifndef ENDIANSWAPPER_H_
|
|
|
|
#define ENDIANSWAPPER_H_
|
|
|
|
|
|
|
|
//#include <endian.h> //for testing on x86
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/osal/Endiness.h>
|
|
|
|
#include <cstring>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
class EndianSwapper {
|
|
|
|
private:
|
|
|
|
EndianSwapper() {
|
|
|
|
}
|
|
|
|
;
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static T swap(T in) {
|
|
|
|
#ifndef BYTE_ORDER
|
|
|
|
#error BYTE_ORDER not defined
|
|
|
|
#elif BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
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;
|
|
|
|
#elif BYTE_ORDER == BIG_ENDIAN
|
|
|
|
return in;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static void swap(uint8_t* out, const uint8_t* in, uint32_t size) {
|
|
|
|
#ifndef BYTE_ORDER
|
|
|
|
#error BYTE_ORDER not defined
|
|
|
|
#elif BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
for (uint8_t count = 0; count < size; count++) {
|
|
|
|
out[size - count - 1] = in[count];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
#elif BYTE_ORDER == BIG_ENDIAN
|
|
|
|
memcpy(out, in, size);
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ENDIANSWAPPER_H_ */
|