diff --git a/inc/common/common.h b/inc/common/common.h index dd483dfc3a329b1e4d26cd8dcc0cfe40e0a6ffd1..e07e7bf97544ed240ac2377cbdf92cf7ca203069 100644 --- a/inc/common/common.h +++ b/inc/common/common.h @@ -27,36 +27,15 @@ double be_double_t(const uint8_t bytes[]); be_uint32_t defined function. */ float be_float_t(const uint8_t bytes[]); -#if defined (_LITTLE_ENDIAN) - -/* Destination: Little-endian */ -#define be_uint32_t be_uint32_t_to_le -#define be_uint64_t be_uint64_t_to_le - /* Read `bytes` as the big endian representation of a 32-bit unsigned integer. - `n` is the number of bytes in `bytes`. The result is a Little-endian. + `n` is the number of bytes in `bytes`. The result is a 'correct'-endian. Returns 0 if n is 0. Restriction: n <= 4 */ -uint32_t be_uint32_t_to_le(const uint8_t bytes[], size_t n); +uint32_t be_uint32_t(const uint8_t bytes[], size_t n); /* Same as `be_uint32_t` but for 64-bit unsigned integers. The result is a - Little-endian. Restriction: n <= 8 */ -uint64_t be_uint64_t_to_le(const uint8_t bytes[], size_t n); - -#else -#if defined(_BIG_ENDIAN) - -/* Destination: Big-endian */ -#error "You must define functions to read big endian and store them as big endian." -#define be_uint32_t be_uint32_t_to_be -#define be_uint64_t be_uint64_t_to_be - -/* Same as the be_to_le versions */ -uint32_t be_uint32_t_to_be(const uint8_t bytes[], size_t n); -uint64_t be_uint64_t_to_be(const uint8_t bytes[], size_t n); - -#endif /* _BIG_ENDIAN */ -#endif /* _LITTLE_ENDIAN */ + 'correct'-endian. Restriction: n <= 8 */ +uint64_t be_uint64_t(const uint8_t bytes[], size_t n); /* Trim the string `str` of the char `c` from the right and the left. The string may not be null terminated. diff --git a/src/common.c b/src/common.c index f1eb624171f805a274a836b8ae25af56e82e90cb..9dc9ca34a8d5dcb973079297d3bfc71afe461d55 100644 --- a/src/common.c +++ b/src/common.c @@ -26,15 +26,6 @@ __unused(void *dummy, ...) /* Endian stuff */ -uint32_t -be_uint32_t_to_le(const uint8_t bytes[], size_t n) -{ - uint32_t res = 0; - for (size_t i = 0; i < n; i++) - res = (res << 8u) | bytes[i]; - return res; -} - float be_float_t(const uint8_t bytes[]) { @@ -47,11 +38,20 @@ be_float_t(const uint8_t bytes[]) } uint64_t -be_uint64_t_to_le(const uint8_t bytes[], size_t n) +be_uint64_t(const uint8_t bytes[], size_t n) { uint64_t res = 0; - for (size_t i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) { +#if defined (_LITTLE_ENDIAN) res = (res << 8u) | bytes[i]; +#else +#if defined(_BIG_ENDIAN) + res = (res >> 8u) | bytes[i]; +#else +#error "Nor BIG nor LITTLE endian" +#endif /* _BIG_ENDIAN */ +#endif /* _LITTLE_ENDIAN */ + } return res; } @@ -67,20 +67,20 @@ be_double_t(const uint8_t bytes[]) } uint32_t -be_uint32_t_to_be(const uint8_t bytes[], size_t n) +be_uint32_t(const uint8_t bytes[], size_t n) { uint32_t res = 0; - for (size_t i = 0; i < n; i++) - res = (res >> 8u) | bytes[i]; - return res; -} - -uint64_t -be_uint64_t_to_be(const uint8_t bytes[], size_t n) -{ - uint64_t res = 0; - for (size_t i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) { +#if defined (_LITTLE_ENDIAN) + res = (res << 8u) | bytes[i]; +#else +#if defined(_BIG_ENDIAN) res = (res >> 8u) | bytes[i]; +#else +#error "Nor BIG nor LITTLE endian" +#endif /* _BIG_ENDIAN */ +#endif /* _LITTLE_ENDIAN */ + } return res; }