From d9093e3245f9619850cea391adcad1a12164d38e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 5 Jun 2024 16:32:46 +0200 Subject: the large library cleanup --- lib/mpack | 1 - lib/mpack/CMakeLists.txt | 26 ++++++++++++++++++++++++++ lib/mpack/makefile | 4 ++++ lib/mpack/src | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) delete mode 160000 lib/mpack create mode 100644 lib/mpack/CMakeLists.txt create mode 100644 lib/mpack/makefile create mode 160000 lib/mpack/src (limited to 'lib/mpack') diff --git a/lib/mpack b/lib/mpack deleted file mode 160000 index 79d3fcd..0000000 --- a/lib/mpack +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 79d3fcd3e04338b06e82d01a62f4aa98c7bad5f7 diff --git a/lib/mpack/CMakeLists.txt b/lib/mpack/CMakeLists.txt new file mode 100644 index 0000000..0a904b0 --- /dev/null +++ b/lib/mpack/CMakeLists.txt @@ -0,0 +1,26 @@ +if(TARGET mpack) + return() +endif() + +cmake_minimum_required(VERSION 3.29) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + +project(mpack C) + +add_library(mpack STATIC + src/src/mpack/mpack-common.c + src/src/mpack/mpack-expect.c + src/src/mpack/mpack-node.c + src/src/mpack/mpack-platform.c + src/src/mpack/mpack-reader.c + src/src/mpack/mpack-writer.c + ) +target_include_directories(mpack SYSTEM INTERFACE + src/src/mpack + ) + +# causes some wild crashes, please leave off +add_compile_definitions(MPACK_READ_TRACKING=0) + diff --git a/lib/mpack/makefile b/lib/mpack/makefile new file mode 100644 index 0000000..e96794a --- /dev/null +++ b/lib/mpack/makefile @@ -0,0 +1,4 @@ +TARGET = $(BUILD_DIR)/libmpack.a + +include ../../lazy.mk + diff --git a/lib/mpack/src b/lib/mpack/src new file mode 160000 index 0000000..79d3fcd --- /dev/null +++ b/lib/mpack/src @@ -0,0 +1 @@ +Subproject commit 79d3fcd3e04338b06e82d01a62f4aa98c7bad5f7 -- cgit v1.2.3 From 68471e5800b81285453a26547721d264dbcf49b9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 5 Jun 2024 16:48:59 +0200 Subject: add `mpack_read_remaining_bytes` function to mpack --- lib/i2ctcp/i2ctcpv1.c | 11 +---------- lib/mpack/CMakeLists.txt | 2 ++ lib/mpack/mpack.h | 19 +++++++++++++++++++ lib/mpack/read-remaining.c | 10 ++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 lib/mpack/mpack.h create mode 100644 lib/mpack/read-remaining.c (limited to 'lib/mpack') diff --git a/lib/i2ctcp/i2ctcpv1.c b/lib/i2ctcp/i2ctcpv1.c index 944df3a..f70cbb1 100644 --- a/lib/i2ctcp/i2ctcpv1.c +++ b/lib/i2ctcp/i2ctcpv1.c @@ -15,21 +15,12 @@ int i2ctcp_read(i2ctcp_msg_t * target, const char * buf, size_t buf_sz) { target->length = target->_rdata = mpack_expect_bin(&reader); if (mpack_reader_error(&reader) != mpack_ok) return -1; target->data = (char *) malloc(target->length); - - // seek forward in buf to where binary data begins (to avoid having to read - // from private member reader.data in the memcpy below) - buf += buf_sz - mpack_reader_remaining(&reader, NULL); } // continue reading chunks of target->data until the amount of bytes // specified in target->length - size_t to_read = MIN(mpack_reader_remaining(&reader, NULL), target->_rdata); char * data = target->data + target->length - target->_rdata; - memcpy(data, buf, to_read); - target->_rdata -= to_read; - // NOTE: memcpy is used here because mpack_read_bytes requires that a tag was - // opened, which is not the case for the chunks following the initial mpack - // header + target->_rdata -= mpack_read_remaining_bytes(&reader, data, target->_rdata); // if rdata = 0, the message was completely read return target->_rdata; diff --git a/lib/mpack/CMakeLists.txt b/lib/mpack/CMakeLists.txt index 0a904b0..0e4359d 100644 --- a/lib/mpack/CMakeLists.txt +++ b/lib/mpack/CMakeLists.txt @@ -16,8 +16,10 @@ add_library(mpack STATIC src/src/mpack/mpack-platform.c src/src/mpack/mpack-reader.c src/src/mpack/mpack-writer.c + read-remaining.c ) target_include_directories(mpack SYSTEM INTERFACE + . src/src/mpack ) diff --git a/lib/mpack/mpack.h b/lib/mpack/mpack.h new file mode 100644 index 0000000..7c0c089 --- /dev/null +++ b/lib/mpack/mpack.h @@ -0,0 +1,19 @@ +#pragma once + +#include "src/src/mpack/mpack.h" + +/** + * \brief read remaining bytes in reader without opening a tag first + * + * \param reader pointer to mpack reader object + * \param p pointer to write data to + * \param count maximum number of bytes to read + * + * This function reads *up to* the amount of bytes specified in \p count, or + * less if there is less remaining data in the buffer. If \p count is equal to + * 0, all remaining data in the buffer is read. + * + * \return amount of bytes read + */ +size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, size_t count); + diff --git a/lib/mpack/read-remaining.c b/lib/mpack/read-remaining.c new file mode 100644 index 0000000..ebc9b56 --- /dev/null +++ b/lib/mpack/read-remaining.c @@ -0,0 +1,10 @@ +#include "mpack.h" + +size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, size_t count) { + size_t limit =mpack_reader_remaining(reader, NULL); + if (0 < count && count < limit) + limit = count; + memcpy(p, reader->data, limit); + return limit; +} + -- cgit v1.2.3