From 105e24c9e001d98a901eb4869c5dd9b7e19e548f Mon Sep 17 00:00:00 2001 From: Hanspeter Portner Date: Sun, 3 Dec 2023 10:01:19 +0100 Subject: [PATCH] Properly estimate slip encoded size --- osc.lv2/stream.h | 3 +++ src/stream.c | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/osc.lv2/stream.h b/osc.lv2/stream.h index 4dcbcd8..10b7d0d 100644 --- a/osc.lv2/stream.h +++ b/osc.lv2/stream.h @@ -86,6 +86,9 @@ int lv2_osc_stream_init(LV2_OSC_Stream *stream, const char *url, const LV2_OSC_Driver *driv, void *data); +size_t +lv2_osc_slip_encode_size(const uint8_t *dst, size_t len); + size_t lv2_osc_slip_encode_inline(uint8_t *dst, size_t len); diff --git a/src/stream.c b/src/stream.c index 0bcbf53..9d4e452 100644 --- a/src/stream.c +++ b/src/stream.c @@ -592,7 +592,7 @@ lv2_osc_stream_init(LV2_OSC_Stream *stream, const char *url, // SLIP encoding size_t -lv2_osc_slip_encode_inline(uint8_t *dst, size_t len) +lv2_osc_slip_encode_size(const uint8_t *dst, size_t len) { if(len == 0) { @@ -611,6 +611,21 @@ lv2_osc_slip_encode_inline(uint8_t *dst, size_t len) } } + return size; +} + +size_t +lv2_osc_slip_encode_inline(uint8_t *dst, size_t len) +{ + // estimate new size + const size_t size = lv2_osc_slip_encode_size(dst, len); + + // ignore empty message + if(len == 0) + { + return 0; + } + // fast track if no escaping needed if(size == len + 2) { @@ -621,6 +636,8 @@ lv2_osc_slip_encode_inline(uint8_t *dst, size_t len) return size; } + const uint8_t *end = dst + len; + // slow track if some escaping needed uint8_t *to = dst + size - 1; *to-- = SLIP_END; @@ -876,7 +893,9 @@ _lv2_osc_stream_run_tcp(LV2_OSC_Stream *stream) { if(stream->slip) // SLIP framed { - if(tosend <= sizeof(stream->tx_buf)) // check if there is enough memory + const size_t nsize = lv2_osc_slip_encode_size(buf, tosend); + + if(nsize <= sizeof(stream->tx_buf)) // check if there is enough memory { memcpy(stream->tx_buf, buf, tosend); tosend = lv2_osc_slip_encode_inline(stream->tx_buf, tosend); @@ -1087,7 +1106,9 @@ _lv2_osc_stream_run_ser(LV2_OSC_Stream *stream) { if(stream->slip) // SLIP framed { - if(tosend <= sizeof(stream->tx_buf)) // check if there is enough memory + const size_t nsize = lv2_osc_slip_encode_size(buf, tosend); + + if(nsize <= sizeof(stream->tx_buf)) // check if there is enough memory { memcpy(stream->tx_buf, buf, tosend); tosend = lv2_osc_slip_encode_inline(stream->tx_buf, tosend); -- 2.38.5