Add util functions to write in little-endian

This will be helpful for writing HID values.
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
parent abec04e8e7
commit c4b6bb312c

View File

@ -13,6 +13,12 @@ sc_write16be(uint8_t *buf, uint16_t value) {
buf[1] = value;
}
static inline void
sc_write16le(uint8_t *buf, uint16_t value) {
buf[0] = value;
buf[1] = value >> 8;
}
static inline void
sc_write32be(uint8_t *buf, uint32_t value) {
buf[0] = value >> 24;
@ -21,12 +27,26 @@ sc_write32be(uint8_t *buf, uint32_t value) {
buf[3] = value;
}
static inline void
sc_write32le(uint8_t *buf, uint32_t value) {
buf[0] = value;
buf[1] = value >> 8;
buf[2] = value >> 16;
buf[3] = value >> 24;
}
static inline void
sc_write64be(uint8_t *buf, uint64_t value) {
sc_write32be(buf, value >> 32);
sc_write32be(&buf[4], (uint32_t) value);
}
static inline void
sc_write64le(uint8_t *buf, uint64_t value) {
sc_write32le(buf, (uint32_t) value);
sc_write32le(buf, value >> 32);
}
static inline uint16_t
sc_read16be(const uint8_t *buf) {
return (buf[0] << 8) | buf[1];