Use builtin byteswap functions to perform endianess conversion

This commit is contained in:
Mario Hüttel 2023-01-20 19:37:10 +01:00
parent 891df1803e
commit 933680c80d

View File

@ -90,15 +90,16 @@ static uint32_t get_uint32_from_byte_string(const uint8_t *data, bool little_end
uint32_t out = 0ul; uint32_t out = 0ul;
int i; int i;
/* Always shift in in big endian format */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if (little_endian)
out >>= 8u;
else
out <<= 8u; out <<= 8u;
out |= (uint32_t)data[i];
out |= (((uint32_t)data[i]) << (little_endian ? 24u : 0u));
} }
/* Swap bytes if little endian */
if (little_endian)
out = bswap_32(out);
return out; return out;
} }
@ -109,14 +110,12 @@ static void write_crc_to_byte_array(uint8_t *byte_array, uint32_t crc, uint8_t c
if (!byte_array) if (!byte_array)
return; return;
if (!little_endian)
crc = bswap_32(crc);
for (i = 0; i < crc_size_bytes; i++) { for (i = 0; i < crc_size_bytes; i++) {
if (little_endian) { byte_array[i] = (uint8_t)(crc & 0xFFul);
byte_array[i] = (uint8_t)(crc & 0xFFul); crc >>= 8u;
crc >>= 8u;
} else {
byte_array[i] = (uint8_t)((crc & 0xFF000000ul) >> 24u);
crc <<= 8u;
}
} }
} }