From 068d8f091c45eb7987031b3ae0a871d268bfe7bb Mon Sep 17 00:00:00 2001 From: Alvin Cheng <88267875+cheng-alvin@users.noreply.github.com> Date: Thu, 2 Nov 2023 23:22:44 +1100 Subject: [PATCH] Update write.c --- src/write.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/write.c b/src/write.c index dc68787a..590ba722 100644 --- a/src/write.c +++ b/src/write.c @@ -1,22 +1,26 @@ #include "write.h" +#include "endian.h" #include #include +#include +#include -uint8_t *write8(uint8_t *buffer, uint8_t value) { - const int len = *buffer / sizeof(uint8_t); - buffer = realloc(buffer, len + sizeof(uint8_t)); +uint8_t *write8(uint8_t *buffer, uint8_t value, int *len) { + buffer = realloc(buffer, (*len) + sizeof(uint8_t)); - buffer[len] = value; + buffer[(*len)] = value; + (*len) += sizeof(uint8_t); return buffer; } -uint8_t *write16(uint8_t *buffer, uint16_t value) { - const int len = *buffer / sizeof(uint16_t); - buffer = realloc(buffer, len + sizeof(uint16_t)); - +uint8_t *write16(uint8_t *buffer, uint16_t value, int *len) { + buffer = realloc(buffer, (*len) + sizeof(uint16_t)); value = little_endian16(value); - buffer[len] = value; + + buffer[(*len)] = (value >> 8) & 0xFF; + buffer[(*len) + 1] = value & 0xFF; + (*len) += sizeof(uint16_t); return buffer; -} \ No newline at end of file +}