aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/i2ctcp/i2ctcpv1.c11
-rw-r--r--lib/mpack/CMakeLists.txt2
-rw-r--r--lib/mpack/mpack.h19
-rw-r--r--lib/mpack/read-remaining.c10
4 files changed, 32 insertions, 10 deletions
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;
+}
+