Add util functions to write in little-endian

This will be helpful for writing HID values.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
parent 4565f36ee6
commit f4d1e49ad9
2 changed files with 62 additions and 0 deletions

View File

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

View File

@ -42,6 +42,44 @@ static void test_write64be(void) {
assert(buf[7] == 0xEF); assert(buf[7] == 0xEF);
} }
static void test_write16le(void) {
uint16_t val = 0xABCD;
uint8_t buf[2];
sc_write16le(buf, val);
assert(buf[0] == 0xCD);
assert(buf[1] == 0xAB);
}
static void test_write32le(void) {
uint32_t val = 0xABCD1234;
uint8_t buf[4];
sc_write32le(buf, val);
assert(buf[0] == 0x34);
assert(buf[1] == 0x12);
assert(buf[2] == 0xCD);
assert(buf[3] == 0xAB);
}
static void test_write64le(void) {
uint64_t val = 0xABCD1234567890EF;
uint8_t buf[8];
sc_write64le(buf, val);
assert(buf[0] == 0xEF);
assert(buf[1] == 0x90);
assert(buf[2] == 0x78);
assert(buf[3] == 0x56);
assert(buf[4] == 0x34);
assert(buf[5] == 0x12);
assert(buf[6] == 0xCD);
assert(buf[7] == 0xAB);
}
static void test_read16be(void) { static void test_read16be(void) {
uint8_t buf[2] = {0xAB, 0xCD}; uint8_t buf[2] = {0xAB, 0xCD};
@ -108,6 +146,10 @@ int main(int argc, char *argv[]) {
test_read32be(); test_read32be();
test_read64be(); test_read64be();
test_write16le();
test_write32le();
test_write64le();
test_float_to_u16fp(); test_float_to_u16fp();
test_float_to_i16fp(); test_float_to_i16fp();
return 0; return 0;