@@ 87,6 87,9 @@ 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);
size_t
@@ 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);