Skip to content

Display and control your Android device

Add util functions to write in little-endian

Romain Vimontf4d1e49ad915
diff --git a/app/src/util/binary.h b/app/src/util/binary.h
index 6dc1b58e..7de9b505 100644
--- a/app/src/util/binary.h
+++ b/app/src/util/binary.h
@@ -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[4], value >> 32);
+}
+
static inline uint16_t
sc_read16be(const uint8_t *buf) {
return (buf[0] << 8) | buf[1];
diff --git a/app/tests/test_binary.c b/app/tests/test_binary.c
index 82a9c1e0..bce74ce2 100644
--- a/app/tests/test_binary.c
+++ b/app/tests/test_binary.c
@@ -42,6 +42,44 @@ static void test_write64be(void) {
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) {
uint8_t buf[2] = {0xAB, 0xCD};
@@ -108,6 +146,10 @@ int main(int argc, char *argv[]) {
test_read32be();
test_read64be();
+ test_write16le();
+ test_write32le();
+ test_write64le();
+
test_float_to_u16fp();
test_float_to_i16fp();
return 0;