1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:32:45 +01:00

Merge pull request #16363 from haukepetersen/add_byteorder_letofrombuf

sys/byteorder: add little endian to/from buf functions
This commit is contained in:
Kaspar Schleiser 2021-05-21 13:35:42 +02:00 committed by GitHub
commit ca60cc9f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,7 @@
#ifndef BYTEORDER_H
#define BYTEORDER_H
#include <string.h>
#include <stdint.h>
#include "unaligned.h"
@ -682,6 +683,93 @@ static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
#endif
}
static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
return unaligned_get_u16(buf);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return (uint16_t)((buf[0] << 8) | (buf[1] << 0));
#endif
}
static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
return unaligned_get_u32(buf);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return (((uint32_t) buf[0] << 24)
| ((uint32_t) buf[1] << 16)
| ((uint32_t) buf[2] << 8)
| ((uint32_t) buf[3] << 0));
#endif
}
static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
return unaligned_get_u64(buf);
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return (((uint64_t) buf[0] << 56)
| ((uint64_t) buf[1] << 48)
| ((uint64_t) buf[2] << 40)
| ((uint64_t) buf[3] << 32)
| ((uint64_t) buf[4] << 24)
| ((uint64_t) buf[5] << 16)
| ((uint64_t) buf[6] << 8)
| ((uint64_t) buf[7] << 0));
#endif
}
static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
memcpy(buf, &val, sizeof(val));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
buf[0] = (uint8_t)(val >> 8);
buf[1] = (uint8_t)(val >> 0);
#endif
}
static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
memcpy(buf, &val, sizeof(val));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
buf[0] = (uint8_t)(val >> 24);
buf[1] = (uint8_t)(val >> 16);
buf[2] = (uint8_t)(val >> 8);
buf[3] = (uint8_t)(val >> 0);
#endif
}
static inline void byteorder_htolebufll(uint8_t *buf, uint64_t val)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/* little endian to little endian conversion is easy, but buffer might be
* unaligned */
memcpy(buf, &val, sizeof(val));
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
buf[0] = (uint8_t)(val >> 56);
buf[1] = (uint8_t)(val >> 48);
buf[2] = (uint8_t)(val >> 40);
buf[3] = (uint8_t)(val >> 32);
buf[4] = (uint8_t)(val >> 24);
buf[5] = (uint8_t)(val >> 16);
buf[6] = (uint8_t)(val >> 8);
buf[7] = (uint8_t)(val >> 0);
#endif
}
#ifdef __cplusplus
}
#endif