From 7639861db365066d9c15f4cb7b9b75173f3a20a8 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:13:19 +0200 Subject: [PATCH 01/21] feat: add _z_string_check function --- include/zenoh-pico/collections/string.h | 1 + src/collections/string.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index e03139cfe..d304bbd73 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -70,6 +70,7 @@ typedef struct { } _z_string_t; _z_string_t _z_string_null(void); +_Bool _z_string_check(_z_string_t value); _z_string_t _z_string_make(const char *value); _z_string_t _z_string_wrap(char *value); _z_string_t *_z_string_make_as_ptr(const char *value); diff --git a/src/collections/string.c b/src/collections/string.c index 47c3609ad..fc7e5f868 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -23,6 +23,8 @@ _z_string_t _z_string_null(void) { return s; } +_Bool _z_string_check(_z_string_t value) { return value.val != NULL; } + _z_string_t _z_string_make(const char *value) { _z_string_t s; s.val = _z_str_clone(value); From 5b8f1dd575caf84b0df58fa3e232689372de04fa Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:14:49 +0200 Subject: [PATCH 02/21] feat: replace z_slice by z_string in encodings --- include/zenoh-pico/net/encoding.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/zenoh-pico/net/encoding.h b/include/zenoh-pico/net/encoding.h index cb17a1622..ab3ceac73 100644 --- a/include/zenoh-pico/net/encoding.h +++ b/include/zenoh-pico/net/encoding.h @@ -15,13 +15,13 @@ #define ZENOH_PICO_ENCODING_NETAPI_H #include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/slice.h" +#include "zenoh-pico/collections/string.h" /** * A zenoh encoding. */ typedef struct _z_encoding_t { - _z_slice_t schema; + _z_string_t schema; uint16_t id; } _z_encoding_t; From 9ae74808979f2104350be2e36fd071b1bc5a0a30 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:15:30 +0200 Subject: [PATCH 03/21] feat: update codec with encoding z_string --- include/zenoh-pico/protocol/codec/core.h | 4 +- src/protocol/codec.c | 48 +++++++++++++++++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index cfa32cbad..60fea8290 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -68,15 +68,15 @@ int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *buf); int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf); int8_t _z_slice_encode(_z_wbuf_t *buf, const _z_slice_t *bs); -size_t _z_slice_encode_len(const _z_slice_t *bs); int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *buf); int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf); int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs); int8_t _z_zbuf_read_exact(_z_zbuf_t *zbf, uint8_t *dest, size_t length); int8_t _z_str_encode(_z_wbuf_t *buf, const char *s); -int8_t _z_zstr_encode(_z_wbuf_t *wbf, const _z_string_t *s); int8_t _z_str_decode(char **str, _z_zbuf_t *buf); +int8_t _z_string_encode(_z_wbuf_t *wbf, const _z_string_t *s); +int8_t _z_string_decode(_z_string_t *str, _z_zbuf_t *zbf); size_t _z_encoding_len(const _z_encoding_t *en); int8_t _z_encoding_encode(_z_wbuf_t *wbf, const _z_encoding_t *en); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 2b47de18e..d0d19faa0 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -223,7 +223,6 @@ int8_t _z_slice_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { return ret; } -size_t _z_slice_encode_len(const _z_slice_t *bs) { return _z_zint_len(bs->len) + bs->len; } int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; @@ -271,13 +270,6 @@ int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) { return _z_wbuf_write_bytes(wbf, (const uint8_t *)s, 0, len); } -int8_t _z_zstr_encode(_z_wbuf_t *wbf, const _z_string_t *s) { - size_t len = s->len - (size_t)1; - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, len)) - // Note that this does not put the string terminator on the wire. - return _z_wbuf_write_bytes(wbf, (const uint8_t *)s->val, 0, len); -} - int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; @@ -302,30 +294,58 @@ int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { *str = NULL; ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } - return ret; } +int8_t _z_string_encode(_z_wbuf_t *wbf, const _z_string_t *s) { + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, s->len)) + // Note that this does not put the string terminator on the wire. + return _z_wbuf_write_bytes(wbf, (const uint8_t *)s->val, 0, s->len); +} + +int8_t _z_string_decode(_z_string_t *str, _z_zbuf_t *zbf) { + *str = _z_string_null(); + _z_zint_t len = 0; + // Decode string length + _Z_RETURN_IF_ERR(_z_zsize_decode(&len, zbf)); + // Check if we have enough bytes to read + if (_z_zbuf_len(zbf) < len) { + _Z_DEBUG("WARNING: Not enough bytes to read"); + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + // Allocate space for the string terminator + str->val = (char *)z_malloc(len + (size_t)1); + if (str->val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + str->len = len; + // Add null terminator + str->val[len] = '\0'; + // Read bytes + _z_zbuf_read_bytes(zbf, (uint8_t *)str->val, 0, len); + return _Z_RES_OK; +} + /*------------------ encoding ------------------*/ #define _Z_ENCODING_FLAG_S 0x01 size_t _z_encoding_len(const _z_encoding_t *en) { size_t en_len = _z_zint_len((uint32_t)(en->id) << 1); - if (_z_slice_check(en->schema)) { - en_len += _z_slice_encode_len(&en->schema); + if (_z_string_check(en->schema)) { + en_len += _z_zint_len(en->schema.len) + en->schema.len; } return en_len; } int8_t _z_encoding_encode(_z_wbuf_t *wbf, const _z_encoding_t *en) { - _Bool has_schema = _z_slice_check(en->schema); + _Bool has_schema = _z_string_check(en->schema); uint32_t id = (uint32_t)(en->id) << 1; if (has_schema) { id |= _Z_ENCODING_FLAG_S; } _Z_RETURN_IF_ERR(_z_zint32_encode(wbf, id)); if (has_schema) { - _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &en->schema)); + _Z_RETURN_IF_ERR(_z_string_encode(wbf, &en->schema)); } return _Z_RES_OK; } @@ -339,7 +359,7 @@ int8_t _z_encoding_decode(_z_encoding_t *en, _z_zbuf_t *zbf) { } en->id = (uint16_t)(id >> 1); if (has_schema) { - _Z_RETURN_IF_ERR(_z_slice_decode(&en->schema, zbf)); + _Z_RETURN_IF_ERR(_z_string_decode(&en->schema, zbf)); } return _Z_RES_OK; } From 711369a65ad490bd39f21e24dff96854ad071cb8 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:16:35 +0200 Subject: [PATCH 04/21] feat: update encoding use with z_string --- src/api/api.c | 4 ++-- src/net/encoding.c | 18 ++++++++---------- src/protocol/codec/message.c | 4 ++-- tests/z_msgcodec_test.c | 15 ++++++++++++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index a9c20cf99..b23cccde4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -240,8 +240,8 @@ void z_encoding_drop(z_owned_encoding_t *encoding) { if (encoding == NULL) { return; } - if (!_z_slice_is_empty(&encoding->_val->schema)) { - _z_slice_clear(&encoding->_val->schema); + if (!_z_string_check(encoding->_val->schema)) { + _z_string_clear(&encoding->_val->schema); } z_free(encoding->_val); } diff --git a/src/net/encoding.c b/src/net/encoding.c index 406d10437..230c76086 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -23,37 +23,35 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char encoding->id = id; // Clone schema if (schema != NULL) { - encoding->schema = _z_slice_make(strlen(schema) + 1); - if (encoding->schema.start == NULL) { + encoding->schema = _z_string_make(schema); + if (encoding->schema.val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - strcpy((char *)encoding->schema.start, schema); } else { - encoding->schema = _z_slice_empty(); + encoding->schema = _z_string_null(); } return _Z_RES_OK; } _z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema) { - return (_z_encoding_t){ - .id = id, .schema = _z_slice_wrap((const uint8_t *)schema, (schema == NULL) ? (size_t)0 : strlen(schema))}; + return (_z_encoding_t){.id = id, .schema = (schema == NULL) ? _z_string_null() : _z_string_wrap((char *)schema)}; } _z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(Z_ENCODING_ID_DEFAULT, NULL); } -void _z_encoding_clear(_z_encoding_t *encoding) { _z_slice_clear(&encoding->schema); } +void _z_encoding_clear(_z_encoding_t *encoding) { _z_string_clear(&encoding->schema); } _Bool _z_encoding_check(const _z_encoding_t *encoding) { - return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_slice_check(encoding->schema)); + return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_string_check(encoding->schema)); } void _z_encoding_copy(_z_encoding_t *dst, const _z_encoding_t *src) { dst->id = src->id; - _z_slice_copy(&dst->schema, &src->schema); + _z_string_copy(&dst->schema, &src->schema); } void _z_encoding_move(_z_encoding_t *dst, _z_encoding_t *src) { dst->id = src->id; src->id = Z_ENCODING_ID_DEFAULT; - _z_slice_move(&dst->schema, &src->schema); + _z_string_move(&dst->schema, &src->schema); } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 139395e2a..2b75503a8 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -151,8 +151,8 @@ int8_t _z_locators_encode(_z_wbuf_t *wbf, const _z_locator_array_t *la) { _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, la->_len)) for (size_t i = 0; i < la->_len; i++) { _z_string_t *s = _z_locator_to_string(&la->_val[i]); - _Z_RETURN_IF_ERR(_z_zstr_encode(wbf, s)) - z_free(s); + _Z_RETURN_IF_ERR(_z_string_encode(wbf, s)) + _z_string_free(&s); } return ret; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index ab7a421e7..55514b28f 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -234,6 +234,8 @@ _z_string_vec_t gen_str_array(size_t size) { return sa; } +_z_string_t gen_string(size_t len) { return _z_string_wrap(gen_str(len)); } + _z_locator_array_t gen_locator_array(size_t size) { _z_locator_array_t la = _z_locator_array_make(size); for (size_t i = 0; i < size; i++) { @@ -250,9 +252,9 @@ _z_encoding_t gen_encoding(void) { _z_encoding_t en; en.id = gen_uint16(); if (gen_bool()) { - en.schema = gen_slice(16); + en.schema = gen_string(16); } else { - en.schema = _z_slice_empty(); + en.schema = _z_string_null(); } return en; } @@ -508,6 +510,13 @@ void zbuf_extension(void) { /*------------------ Payload field ------------------*/ void assert_eq_slice(const _z_slice_t *left, const _z_slice_t *right) { assert_eq_uint8_array(left, right); } +void assert_eq_string(const _z_string_t *left, const _z_string_t *right) { + assert(left->len == right->len); + if (left->len > 0) { + assert(_z_str_eq(left->val, right->val) == true); + } +} + void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { assert_eq_slice(&left->_slice, &right->_slice); } @@ -553,7 +562,7 @@ void assert_eq_source_info(const _z_source_info_t *left, const _z_source_info_t } void assert_eq_encoding(const _z_encoding_t *left, const _z_encoding_t *right) { assert(left->id == right->id); - assert_eq_slice(&left->schema, &right->schema); + assert_eq_string(&left->schema, &right->schema); } void assert_eq_value(const _z_value_t *left, const _z_value_t *right) { assert_eq_encoding(&left->encoding, &right->encoding); From 41eadbe686b34929309d74716aec15589014c01a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:17:01 +0200 Subject: [PATCH 05/21] fix: z_string related memory leak --- src/link/endpoint.c | 2 +- tests/z_msgcodec_test.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/link/endpoint.c b/src/link/endpoint.c index b2464ba10..d3274fd97 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -450,6 +450,6 @@ char *_z_endpoint_to_str(const _z_endpoint_t *endpoint) { } } // Clean up - _z_string_clear(locator); + _z_string_free(&locator); return ret; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 55514b28f..e39ee5a11 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -345,8 +345,8 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar printf("%s:%s", ls->val, rs->val); if (i < left->_len - 1) printf(" "); - z_free(ls); - z_free(rs); + _z_string_free(&ls); + _z_string_free(&rs); assert(_z_locator_eq(l, r) == true); } From 540657f408cc2475c319e823fc32b6042c5d33d2 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:18:54 +0200 Subject: [PATCH 06/21] chore: update zenoh-pico.h --- include/zenoh-pico.h | 6 +++--- include/zenoh-pico.h.in | 1 + zenohpico.pc | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico.h b/include/zenoh-pico.h index 6e131a727..26569f564 100644 --- a/include/zenoh-pico.h +++ b/include/zenoh-pico.h @@ -17,9 +17,9 @@ #ifndef ZENOH_PICO_H #define ZENOH_PICO_H -#define ZENOH_PICO "0.11.0.0" -#define ZENOH_PICO_MAJOR 0 -#define ZENOH_PICO_MINOR 11 +#define ZENOH_PICO "1.0.0.0" +#define ZENOH_PICO_MAJOR 1 +#define ZENOH_PICO_MINOR 0 #define ZENOH_PICO_PATCH 0 #define ZENOH_PICO_TWEAK 0 diff --git a/include/zenoh-pico.h.in b/include/zenoh-pico.h.in index 736ab3ff2..86e4ffc57 100644 --- a/include/zenoh-pico.h.in +++ b/include/zenoh-pico.h.in @@ -24,6 +24,7 @@ #define ZENOH_PICO_TWEAK @ZENOH_PICO_TWEAK@ #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/api/handlers.h" #include "zenoh-pico/api/macros.h" #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" diff --git a/zenohpico.pc b/zenohpico.pc index 642e15247..08c617f59 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240517dev +Version: 1.0.20240625dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From ca99c721ac8a2ffe41b673e29beed9c3cdd9e892 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 25 Jun 2024 17:23:58 +0200 Subject: [PATCH 07/21] feat: remove encoding enum from functions --- include/zenoh-pico/api/primitives.h | 4 ++-- include/zenoh-pico/api/types.h | 2 +- include/zenoh-pico/net/encoding.h | 4 ++-- src/api/api.c | 2 +- src/net/encoding.c | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 7b5167615..dc686b3a9 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -343,13 +343,13 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *config, uint8_t key * * Parameters: * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. - * id: A known :c:type:`z_encoding_id_t` value. + * id: A known :c:type:`uint16_t` value. * schema: Pointer to a custom schema string value. * * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t zp_encoding_make(z_owned_encoding_t *encoding, z_encoding_id_t id, const char *schema); +int8_t zp_encoding_make(z_owned_encoding_t *encoding, uint16_t id, const char *schema); /** * Builds a new a :c:type:`z_owned_encoding_t` with default value. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 77b2b0195..7c20bd47f 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -177,7 +177,7 @@ _Z_LOANED_TYPE(_z_query_rc_t, query) * Represents the encoding of a payload, in a MIME-like format. * * Members: - * z_encoding_id_t prefix: The integer prefix of this encoding. + * uint16_t prefix: The integer prefix of this encoding. * z_loaned_slice_t* suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. */ _Z_OWNED_TYPE_PTR(_z_encoding_t, encoding) diff --git a/include/zenoh-pico/net/encoding.h b/include/zenoh-pico/net/encoding.h index ab3ceac73..f19dcdbfe 100644 --- a/include/zenoh-pico/net/encoding.h +++ b/include/zenoh-pico/net/encoding.h @@ -25,8 +25,8 @@ typedef struct _z_encoding_t { uint16_t id; } _z_encoding_t; -int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char *schema); -_z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema); +int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema); +_z_encoding_t _z_encoding_wrap(uint16_t id, const char *schema); _z_encoding_t _z_encoding_null(void); void _z_encoding_clear(_z_encoding_t *encoding); _Bool _z_encoding_check(const _z_encoding_t *encoding); diff --git a/src/api/api.c b/src/api/api.c index b23cccde4..6c240ba4b 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -221,7 +221,7 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, co return _zp_config_insert(sc, key, value); } -int8_t zp_encoding_make(z_owned_encoding_t *encoding, z_encoding_id_t id, const char *schema) { +int8_t zp_encoding_make(z_owned_encoding_t *encoding, uint16_t id, const char *schema) { // Init encoding encoding->_val = (_z_encoding_t *)z_malloc(sizeof(_z_encoding_t)); if (encoding->_val == NULL) { diff --git a/src/net/encoding.c b/src/net/encoding.c index 230c76086..36afb06e8 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -19,7 +19,7 @@ #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" -int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char *schema) { +int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema) { encoding->id = id; // Clone schema if (schema != NULL) { @@ -33,7 +33,7 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char return _Z_RES_OK; } -_z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema) { +_z_encoding_t _z_encoding_wrap(uint16_t id, const char *schema) { return (_z_encoding_t){.id = id, .schema = (schema == NULL) ? _z_string_null() : _z_string_wrap((char *)schema)}; } From 6347cfeb7b27fa2712ee8de890697b2f5f4f6dad Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 12:09:58 +0200 Subject: [PATCH 08/21] feat: remove encoding from most examples --- examples/unix/c11/z_put.c | 9 +-------- examples/unix/c11/z_queryable.c | 10 +--------- examples/unix/c11/z_queryable_attachment.c | 11 +++-------- examples/unix/c99/z_pub.c | 9 +-------- examples/unix/c99/z_put.c | 9 +-------- examples/unix/c99/z_queryable.c | 9 +-------- examples/windows/z_pub.c | 9 +-------- examples/windows/z_put.c | 9 +-------- examples/windows/z_queryable.c | 9 +-------- 9 files changed, 11 insertions(+), 73 deletions(-) diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index 90efbb848..26d8023a6 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -101,15 +101,8 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, value); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options; - z_put_options_default(&options); - options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { printf("Oh no! Put has failed...\n"); } // Clean up diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 394b6cd0e..84ac4e59f 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -39,19 +39,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } z_drop(z_move(payload_string)); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - - z_query_reply_options_t options; - z_query_reply_options_default(&options); - options.encoding = z_move(encoding); - // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_serialize_from_string(&reply_payload, value); - z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); z_drop(z_move(keystr)); msg_nb++; } diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index ad3d7f693..7715e28ef 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -125,14 +125,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { drop_attachment(&kvp); z_drop(z_move(payload_string)); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - - z_query_reply_options_t options; - z_query_reply_options_default(&options); - options.encoding = z_move(encoding); - // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_serialize_from_string(&reply_payload, value); @@ -143,6 +135,9 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { kv_pairs_tx_t kv_ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1}; z_owned_bytes_t attachment; zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx, kv_pairs_size(&kv_ctx)); + + z_query_reply_options_t options; + z_query_reply_options_default(&options); options.attachment = z_move(attachment); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 5395865c3..840bb11c0 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -106,14 +106,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, buf); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - z_publisher_put_options_t options; - z_publisher_put_options_default(&options); - options.encoding = z_encoding_move(&encoding); - - z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), &options); + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); } // Clean up z_undeclare_publisher(z_publisher_move(&pub)); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index dff0186e1..1a68f33b0 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -97,15 +97,8 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, value); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options; - z_put_options_default(&options); - options.encoding = z_encoding_move(&encoding); - if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), &options) < 0) { + if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), NULL) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 22f1e4cd3..bc098b63e 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -38,18 +38,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } z_string_drop(z_string_move(&payload_string)); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - - z_query_reply_options_t options; - z_query_reply_options_default(&options); - options.encoding = z_encoding_move(&encoding); // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_serialize_from_string(&reply_payload, value); - z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), &options); + z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); z_string_drop(z_string_move(&keystr)); } diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 13e748a22..6b0c4bd28 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -68,14 +68,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, buf); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - z_publisher_put_options_t options; - z_publisher_put_options_default(&options); - options.encoding = z_move(encoding); - - z_publisher_put(z_loan(pub), z_move(payload), &options); + z_publisher_put(z_loan(pub), z_move(payload), NULL); } // Clean-up diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index 4b3a76dda..3fa50939b 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -64,15 +64,8 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, value); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options; - z_put_options_default(&options); - options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 294afd37e..3415e2260 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -38,18 +38,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } z_drop(z_move(payload_string)); - // Create encoding - z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - - z_query_reply_options_t options; - z_query_reply_options_default(&options); - options.encoding = z_move(encoding); // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_serialize_from_string(&reply_payload, value); - z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); z_drop(z_move(keystr)); } From 3f3049a8694f9386574692aef02ecbdb1144ae32 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 12:10:46 +0200 Subject: [PATCH 09/21] feat: remove encoding id enum --- include/zenoh-pico/api/constants.h | 63 ++---------------------------- include/zenoh-pico/net/encoding.h | 2 + src/net/encoding.c | 6 +-- 3 files changed, 8 insertions(+), 63 deletions(-) diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 58fecb460..e526a54de 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -98,67 +98,10 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t * A set of associated constants are provided to cover the most common encodings for user convenience. * This is particularly useful in helping Zenoh to perform additional network optimizations. */ -typedef enum { - Z_ENCODING_ID_BYTES = 0, // Primitives types supported in all Zenoh bindings, just some bytes. - Z_ENCODING_ID_INT = 1, // A VLE-encoded signed little-endian integer. - Z_ENCODING_ID_UINT = 2, // A VLE-encoded little-endian unsigned integer. - Z_ENCODING_ID_FLOAT = 3, // A VLE-encoded float. - Z_ENCODING_ID_BOOL = 4, // A boolean. - Z_ENCODING_ID_STRING = 5, // A UTF-8 string. - Z_ENCODING_ID_ERROR = 6, // A zenoh error. - Z_ENCODING_ID_APP_OCTET_STREAM = 7, // An application-specific stream of bytes. - Z_ENCODING_ID_TEXT_PLAIN = 8, // A textual file. - Z_ENCODING_ID_APP_JSON = 9, // JSON data intended to be consumed by an application. - Z_ENCODING_ID_TEXT_JSON = 10, // JSON data intended to be human readable. - Z_ENCODING_ID_APP_CDR = 11, // A Common Data Representation (CDR)-encoded data. - Z_ENCODING_ID_APP_CBOR = 12, // A Concise Binary Object Representation (CBOR)-encoded data. - Z_ENCODING_ID_APP_YAML = 13, // YAML data intended to be consumed by an application. - Z_ENCODING_ID_TEXT_YAML = 14, // YAML data intended to be human readable. - Z_ENCODING_ID_TEXT_JSON5 = 15, // JSON5 encoded data that are human readable. - Z_ENCODING_ID_APP_PYTHON_SERIALIZED_OBJECT = 16, // A Python object serialized using pickle. - Z_ENCODING_ID_APP_PROTOBUF = 17, // An application-specific protobuf-encoded data. - Z_ENCODING_ID_APP_JAVA_SERIALIZED_OBJECT = 18, // A Java serialized object. - Z_ENCODING_ID_APP_OPENMETRICS_TEXT = 19, // An openmetrics data. - Z_ENCODING_ID_IMAGE_PNG = 20, // A Portable Network Graphics (PNG) image. - Z_ENCODING_ID_IMAGE_JPEG = 21, // A Joint Photographic Experts Group (JPEG) image. - Z_ENCODING_ID_IMAGE_GIF = 22, // A Graphics Interchange Format (GIF) image. - Z_ENCODING_ID_IMAGE_BMP = 23, // A BitMap (BMP) image. - Z_ENCODING_ID_IMAGE_WEBP = 24, // A Web Portable (WebP) image. - Z_ENCODING_ID_APP_XML = 25, // An XML file intended to be consumed by an application. - Z_ENCODING_ID_APP_X_WWW_FORM_URLENCODED = 26, // An encoded a list of tuples. - Z_ENCODING_ID_TEXT_HTML = 27, // An HTML file. - Z_ENCODING_ID_TEXT_XML = 28, // An XML file that is human readable. - Z_ENCODING_ID_TEXT_CSS = 29, // A CSS file. - Z_ENCODING_ID_TEXT_JAVASCRIPT = 30, // A JavaScript file. - Z_ENCODING_ID_TEXT_MARKDOWN = 31, // A MarkDown file. - Z_ENCODING_ID_TEXT_CSV = 32, // A CSV file. - Z_ENCODING_ID_APP_SQL = 33, // An application-specific SQL query. - Z_ENCODING_ID_APP_COAP_PAYLOAD = 34, // Constrained Application Protocol (CoAP) data. - Z_ENCODING_ID_APP_JSON_PATCH_JSON = 35, // Defines a JSON document structure. - Z_ENCODING_ID_APP_JSON_SEQ = 36, // A JSON text sequence. - Z_ENCODING_ID_APP_JSONPATH = 37, // A JSONPath defines a string syntax. - Z_ENCODING_ID_APP_JWT = 38, // A JSON Web Token (JWT). - Z_ENCODING_ID_APP_MP4 = 39, // An application-specific MPEG-4 encoded data. - Z_ENCODING_ID_APP_SOAP_XML = 40, // A SOAP 1.2 message serialized as XML 1.0. - Z_ENCODING_ID_APP_YANG = 41, // A YANG-encoded data. - Z_ENCODING_ID_AUDIO_AAC = 42, // A MPEG-4 Advanced Audio Coding (AAC) media. - Z_ENCODING_ID_AUDIO_FLAC = 43, // A Free Lossless Audio Codec (FLAC) media. - Z_ENCODING_ID_AUDIO_MP4 = 44, // An audio codec defined in MPEG-4. - Z_ENCODING_ID_AUDIO_OGG = 45, // An Ogg-encapsulated audio stream. - Z_ENCODING_ID_AUDIO_VORBIS = 46, // A Vorbis-encoded audio stream. - Z_ENCODING_ID_VIDEO_H261 = 47, // A h261-encoded video stream. - Z_ENCODING_ID_VIDEO_H263 = 48, // A h263-encoded video stream. - Z_ENCODING_ID_VIDEO_H264 = 49, // A h264-encoded video stream. - Z_ENCODING_ID_VIDEO_H265 = 50, // A h265-encoded video stream. - Z_ENCODING_ID_VIDEO_H266 = 51, // A h266-encoded video stream. - Z_ENCODING_ID_VIDEO_MP4 = 52, // A video codec defined in MPEG-4. - Z_ENCODING_ID_VIDEO_OGG = 53, // An Ogg-encapsulated video stream. - Z_ENCODING_ID_VIDEO_RAW = 54, // An uncompressed, studio-quality video stream. - Z_ENCODING_ID_VIDEO_VP8 = 55, // A VP8-encoded video stream. - Z_ENCODING_ID_VIDEO_VP9 = 56 // A VP9-encoded video stream. -} z_encoding_id_t; -#define Z_ENCODING_ID_DEFAULT Z_ENCODING_ID_BYTES + + + /** * Consolidation mode values. diff --git a/include/zenoh-pico/net/encoding.h b/include/zenoh-pico/net/encoding.h index f19dcdbfe..25e6c6260 100644 --- a/include/zenoh-pico/net/encoding.h +++ b/include/zenoh-pico/net/encoding.h @@ -17,6 +17,8 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/string.h" +#define _Z_ENCODING_ID_DEFAULT 0 + /** * A zenoh encoding. */ diff --git a/src/net/encoding.c b/src/net/encoding.c index 36afb06e8..45d0b9e75 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -37,12 +37,12 @@ _z_encoding_t _z_encoding_wrap(uint16_t id, const char *schema) { return (_z_encoding_t){.id = id, .schema = (schema == NULL) ? _z_string_null() : _z_string_wrap((char *)schema)}; } -_z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(Z_ENCODING_ID_DEFAULT, NULL); } +_z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(_Z_ENCODING_ID_DEFAULT, NULL); } void _z_encoding_clear(_z_encoding_t *encoding) { _z_string_clear(&encoding->schema); } _Bool _z_encoding_check(const _z_encoding_t *encoding) { - return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_string_check(encoding->schema)); + return ((encoding->id != _Z_ENCODING_ID_DEFAULT) || _z_string_check(encoding->schema)); } void _z_encoding_copy(_z_encoding_t *dst, const _z_encoding_t *src) { @@ -52,6 +52,6 @@ void _z_encoding_copy(_z_encoding_t *dst, const _z_encoding_t *src) { void _z_encoding_move(_z_encoding_t *dst, _z_encoding_t *src) { dst->id = src->id; - src->id = Z_ENCODING_ID_DEFAULT; + src->id = _Z_ENCODING_ID_DEFAULT; _z_string_move(&dst->schema, &src->schema); } From 30ea34d0f84dc16de38ac15cfece89c39f4aceb3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 12:11:22 +0200 Subject: [PATCH 10/21] feat: switch to encoding form string api --- examples/unix/c11/z_pub.c | 2 +- include/zenoh-pico/api/constants.h | 4 --- include/zenoh-pico/api/primitives.h | 49 ++++++++++++++--------------- src/api/api.c | 42 +++++++++++++++++++------ 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index f65f7a172..286524547 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -118,7 +118,7 @@ int main(int argc, char **argv) { // Create encoding z_owned_encoding_t encoding; - zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, "utf8"); + z_encoding_from_str(&encoding, "text/plain;utf8"); z_publisher_put_options_t options; z_publisher_put_options_default(&options); options.encoding = z_move(encoding); diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index e526a54de..5dc6f2e4c 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -99,10 +99,6 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t * This is particularly useful in helping Zenoh to perform additional network optimizations. */ - - - - /** * Consolidation mode values. * diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index dc686b3a9..2974b333f 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -338,30 +338,6 @@ const char *zp_scouting_config_get(const z_loaned_scouting_config_t *config, uin */ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *config, uint8_t key, const char *value); -/** - * Builds a new :c:type:`z_owned_encoding_t`. - * - * Parameters: - * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. - * id: A known :c:type:`uint16_t` value. - * schema: Pointer to a custom schema string value. - * - * Return: - * ``0`` if creation successful, ``negative value`` otherwise. - */ -int8_t zp_encoding_make(z_owned_encoding_t *encoding, uint16_t id, const char *schema); - -/** - * Builds a new a :c:type:`z_owned_encoding_t` with default value. - * - * Parameters: - * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. - * - * Return: - * ``0`` if creation successful, ``negative value`` otherwise. - */ -int8_t zp_encoding_default(z_owned_encoding_t *encoding); - /** * Checks if a :c:type:`z_owned_encoding_t` has non-default values. * @@ -411,15 +387,36 @@ z_loaned_encoding_t *z_encoding_loan_mut(z_owned_encoding_t *encoding); z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding); /** - * Builds a :c:type:`z_owned_encoding_t` with default value. + * Initialize a :c:type:`z_owned_encoding_t` with default value. + * + * Parameters: + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. + */ +void z_encoding_null(z_owned_encoding_t *encoding); + +/** + * Builds a :c:type:`z_owned_encoding_t` from a null terminated string. * * Parameters: * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. + * s: Pointer to the null terminated string to use. + * + * Return: + * ``0`` if creation successful,``negative value`` otherwise. + */ +int8_t z_encoding_from_str(z_owned_encoding_t *encoding, const char *s); + +/** + * Builds a string from a :c:type:`z_loaned_encoding_t`. + * + * Parameters: + * encoding: Pointer to the :c:type:`z_loaned_encoding_t` to use. + * string: Pointer to an uninitialized :c:type:`z_owned_string_t` to store the string. * * Return: * ``0`` if creation successful,``negative value`` otherwise. */ -int8_t z_encoding_null(z_owned_encoding_t *encoding); +int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_t *string); /** * Gets the bytes data from a reply error payload by aliasing it. diff --git a/src/api/api.c b/src/api/api.c index 6c240ba4b..3559067d1 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -221,18 +221,9 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, co return _zp_config_insert(sc, key, value); } -int8_t zp_encoding_make(z_owned_encoding_t *encoding, uint16_t id, const char *schema) { - // Init encoding - encoding->_val = (_z_encoding_t *)z_malloc(sizeof(_z_encoding_t)); - if (encoding->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _z_encoding_make(encoding->_val, id, schema); -} - z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding) { return encoding; } -int8_t z_encoding_null(z_owned_encoding_t *encoding) { return zp_encoding_make(encoding, Z_ENCODING_ID_DEFAULT, NULL); } +void z_encoding_null(z_owned_encoding_t *encoding) { encoding->_val = NULL; } _Bool z_encoding_check(const z_owned_encoding_t *encoding) { return _z_encoding_check(encoding->_val); } @@ -259,6 +250,37 @@ const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding) { z_loaned_encoding_t *z_encoding_loan_mut(z_owned_encoding_t *encoding) { return encoding->_val; } +int8_t z_encoding_from_str(z_owned_encoding_t *encoding, const char *s) { + // Init owned encoding + z_encoding_null(encoding); + encoding->_val = (_z_encoding_t *)z_malloc(sizeof(_z_encoding_t)); + if (encoding->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + if (s != NULL) { + // TODO: Convert to id if possible + _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); + } + return _Z_RES_OK; +} + +int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { + // Init owned string + z_string_null(s); + // Check encoding + if (!_z_encoding_check(encoding)) { + return _Z_RES_OK; + } + // Allocate owned string + s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + if (s->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // TODO: Convert id to string if possible + _z_string_copy(s->_val, &encoding->schema); + return _Z_RES_OK; +} + const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; } size_t z_slice_len(const z_loaned_slice_t *slice) { return slice->len; } From d3e7cce081e9e77599111445421052afb2c9625b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:32:17 +0200 Subject: [PATCH 11/21] feat: add encoding values config token --- include/zenoh-pico/config.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index d7137b3b2..71613d41b 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -239,6 +239,13 @@ #define Z_FEATURE_INTEREST 0 #endif +/** + * Enable encoding values. + */ +#ifndef Z_FEATURE_ENCODING_VALUES +#define Z_FEATURE_ENCODING_VALUES 1 +#endif + /*------------------ Compile-time configuration properties ------------------*/ /** * Default length for Zenoh ID. Maximum size is 16 bytes. From 441f65e761182c262967014a0e56369f29648d2f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:32:38 +0200 Subject: [PATCH 12/21] doc: add encoding values documentation --- include/zenoh-pico/api/constants.h | 75 +++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 5dc6f2e4c..2a651bad2 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -96,8 +96,81 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t * different operations depending on the encoding value. * * A set of associated constants are provided to cover the most common encodings for user convenience. - * This is particularly useful in helping Zenoh to perform additional network optimizations. + * This is particularly useful in helping Zenoh to perform additional wire-level optimizations. + * + * Register your encoding metadata from a string with :c:func:`z_encoding_from_str`. To get the optimization, you need + * Z_FEATURE_ENCODING_VALUES to 1 and your string should follow the format: ";" + * + * E.g: "text/plain;utf8" + * + * Here is the list of constants: */ +// "zenoh/bytes" +// "zenoh/int8" +// "zenoh/int16" +// "zenoh/int32" +// "zenoh/int64" +// "zenoh/int128" +// "zenoh/uint8" +// "zenoh/uint16" +// "zenoh/uint32" +// "zenoh/uint64" +// "zenoh/uint128" +// "zenoh/float32" +// "zenoh/float64" +// "zenoh/bool" +// "zenoh/string" +// "zenoh/error" +// "application/octet-stream" +// "text/plain" +// "application/json" +// "text/json" +// "application/cdr" +// "application/cbor" +// "application/yaml" +// "text/yaml" +// "text/json5" +// "application/python-serialized-object" +// "application/protobuf" +// "application/java-serialized-object" +// "application/openmetrics-text" +// "image/png" +// "image/jpeg" +// "image/gif" +// "image/bmp" +// "image/webp" +// "application/xml" +// "application/x-www-form-urlencoded" +// "text/html" +// "text/xml" +// "text/css" +// "text/javascript" +// "text/markdown" +// "text/csv" +// "application/sql" +// "application/coap-payload" +// "application/json-patch+json" +// "application/json-seq" +// "application/jsonpath" +// "application/jwt" +// "application/mp4" +// "application/soap+xml" +// "application/yang" +// "audio/aac" +// "audio/flac" +// "audio/mp4" +// "audio/ogg" +// "audio/vorbis" +// "video/h261" +// "video/h263" +// "video/h264" +// "video/h265" +// "video/h266" +// "video/mp4" +// "video/ogg" +// "video/raw" +// "video/vp8" +// "video/vp9" /** * Consolidation mode values. From 9471778e4043f01bebde85a301d2b21e3b04bda5 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:33:00 +0200 Subject: [PATCH 13/21] fix: remove encoding from z_pub --- examples/unix/c11/z_pub.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 286524547..958806740 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -116,14 +116,7 @@ int main(int argc, char **argv) { z_owned_bytes_t payload; z_bytes_serialize_from_string(&payload, buf); - // Create encoding - z_owned_encoding_t encoding; - z_encoding_from_str(&encoding, "text/plain;utf8"); - z_publisher_put_options_t options; - z_publisher_put_options_default(&options); - options.encoding = z_move(encoding); - - z_publisher_put(z_loan(pub), z_move(payload), &options); + z_publisher_put(z_loan(pub), z_move(payload), NULL); } // Clean up z_undeclare_publisher(z_move(pub)); From 3050f548299935ec6a9f22ccebdce05c475b9f0b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:33:20 +0200 Subject: [PATCH 14/21] feat: add wire-level optimization on encoding values --- src/api/api.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 5 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 3559067d1..f670325ba 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -18,6 +18,7 @@ #include #include +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/olv_macros.h" #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" @@ -221,6 +222,138 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, co return _zp_config_insert(sc, key, value); } +#if Z_FEATURE_ENCODING_VALUES == 1 +#define ENCODING_SCHEMA_SEPARATOR ';' + +const char *ENCODING_VALUES_ID_TO_STR[] = { + "zenoh/bytes", + "zenoh/int8", + "zenoh/int16", + "zenoh/int32", + "zenoh/int64", + "zenoh/int128", + "zenoh/uint8", + "zenoh/uint16", + "zenoh/uint32", + "zenoh/uint64", + "zenoh/uint128", + "zenoh/float32", + "zenoh/float64", + "zenoh/bool", + "zenoh/string", + "zenoh/error", + "application/octet-stream", + "text/plain", + "application/json", + "text/json", + "application/cdr", + "application/cbor", + "application/yaml", + "text/yaml", + "text/json5", + "application/python-serialized-object", + "application/protobuf", + "application/java-serialized-object", + "application/openmetrics-text", + "image/png", + "image/jpeg", + "image/gif", + "image/bmp", + "image/webp", + "application/xml", + "application/x-www-form-urlencoded", + "text/html", + "text/xml", + "text/css", + "text/javascript", + "text/markdown", + "text/csv", + "application/sql", + "application/coap-payload", + "application/json-patch+json", + "application/json-seq", + "application/jsonpath", + "application/jwt", + "application/mp4", + "application/soap+xml", + "application/yang", + "audio/aac", + "audio/flac", + "audio/mp4", + "audio/ogg", + "audio/vorbis", + "video/h261", + "video/h263", + "video/h264", + "video/h265", + "video/h266", + "video/mp4", + "video/ogg", + "video/raw", + "video/vp8", + "video/vp9", +}; + +static uint16_t _z_encoding_values_str_to_int(const char *schema, size_t len) { + for (size_t i = 0; i < _ZP_ARRAY_SIZE(ENCODING_VALUES_ID_TO_STR); i++) { + if (strncmp(schema, ENCODING_VALUES_ID_TO_STR[i], len) == 0) { + return (uint16_t)i; + } + } + return UINT16_MAX; +} + +static int8_t _z_encoding_convert_from_string(z_owned_encoding_t *encoding, const char *s) { + const char *id_end = strchr(s, ENCODING_SCHEMA_SEPARATOR); + // Check id_end value + corner cases + if ((id_end != NULL) && (id_end != s)) { + // Calc length of the segment before separator + size_t len = (size_t)(id_end - 1 - s); + uint16_t id = _z_encoding_values_str_to_int(s, len); + // Check id + if (id != UINT16_MAX) { + return _z_encoding_make(encoding->_val, id, (id_end[1] == '\0') ? NULL : ++id_end); + } + } + // By default store the string as schema + return _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); +} + +static int8_t _z_encoding_convert_into_int(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { + const char *prefix = NULL; + size_t prefix_len = 0; + // Convert id + if (encoding->id < _ZP_ARRAY_SIZE(ENCODING_VALUES_ID_TO_STR)) { + prefix = ENCODING_VALUES_ID_TO_STR[encoding->id]; + prefix_len = strlen(prefix); + } + // Allocate string (include separator and null terminator) + char *value = (char *)z_malloc(sizeof(char) * (prefix_len + encoding->schema.len + 2)); + if (value == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Copy data + char sep = ENCODING_SCHEMA_SEPARATOR; + (void)strcpy(value, prefix); + (void)strncat(value, &sep, 1); + (void)strcat(value, encoding->schema.val); + // Fill container + *s->_val = _z_string_wrap(value); + return _Z_RES_OK; +} + +#else +static int8_t _z_encoding_convert_from_string(z_owned_encoding_t *encoding, const char *s) { + return _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); +} + +static int8_t _z_encoding_convert_into_int(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { + _z_string_copy(s->_val, &encoding->schema); + return _Z_RES_OK; +} + +#endif + z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding) { return encoding; } void z_encoding_null(z_owned_encoding_t *encoding) { encoding->_val = NULL; } @@ -231,7 +364,7 @@ void z_encoding_drop(z_owned_encoding_t *encoding) { if (encoding == NULL) { return; } - if (!_z_string_check(encoding->_val->schema)) { + if (_z_string_check(encoding->_val->schema)) { _z_string_clear(&encoding->_val->schema); } z_free(encoding->_val); @@ -257,9 +390,9 @@ int8_t z_encoding_from_str(z_owned_encoding_t *encoding, const char *s) { if (encoding->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } + // Convert string to encoding if (s != NULL) { - // TODO: Convert to id if possible - _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); + return _z_encoding_convert_from_string(encoding, s); } return _Z_RES_OK; } @@ -276,8 +409,8 @@ int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_ if (s->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // TODO: Convert id to string if possible - _z_string_copy(s->_val, &encoding->schema); + // Convert encoding to string + _z_encoding_convert_into_int(encoding, s); return _Z_RES_OK; } From bf490ef081a7c1814bd03d70e301d2236e85d9b3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:33:36 +0200 Subject: [PATCH 15/21] feat: add encoding example --- examples/unix/c11/z_pub_attachment.c | 7 +++++++ examples/unix/c11/z_sub_attachment.c | 8 ++++++++ include/zenoh-pico/api/constants.h | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/unix/c11/z_pub_attachment.c b/examples/unix/c11/z_pub_attachment.c index 2376ffd7f..cad72d9de 100644 --- a/examples/unix/c11/z_pub_attachment.c +++ b/examples/unix/c11/z_pub_attachment.c @@ -153,6 +153,9 @@ int main(int argc, char **argv) { // Allocate buffer char buf_ind[16]; + // Create encoding + z_owned_encoding_t encoding; + // Publish data printf("Press CTRL-C to quit...\n"); char buf[256]; @@ -172,6 +175,10 @@ int main(int argc, char **argv) { zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx)); options.attachment = z_move(attachment); + // Add encoding value + z_encoding_from_str(&encoding, "text/plain;utf8"); + options.encoding = z_move(encoding); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean up diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index 32a7c0d0a..8ab1d15c0 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -72,7 +72,14 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); + z_owned_string_t encoding; + z_encoding_to_string(z_sample_encoding(sample), &encoding); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); + // Check encoding + if (z_check(encoding)) { + printf(" with encoding: %s\n", z_string_data(z_loan(encoding))); + } // Check attachment kv_pairs_t kvp = {.current_idx = 0, .len = KVP_LEN, .data = (kv_pair_t *)malloc(KVP_LEN * sizeof(kv_pair_t))}; parse_attachment(&kvp, z_sample_attachment(sample)); @@ -82,6 +89,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { drop_attachment(&kvp); z_drop(z_move(keystr)); z_drop(z_move(value)); + z_drop(z_move(encoding)); msg_nb++; } diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 2a651bad2..579da9cda 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -100,7 +100,7 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t * * Register your encoding metadata from a string with :c:func:`z_encoding_from_str`. To get the optimization, you need * Z_FEATURE_ENCODING_VALUES to 1 and your string should follow the format: ";" - * + * * E.g: "text/plain;utf8" * * Here is the list of constants: From 7271aa94d44b45484736400085190101c12d8257 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:39:39 +0200 Subject: [PATCH 16/21] fix: remove encoding from tests --- tests/z_api_alignment_test.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index be7bc2c36..21c9087d7 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -77,9 +77,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_query_parameters(query, &pred); (void)(pred); const z_loaned_bytes_t *payload = z_query_payload(query); - const z_loaned_encoding_t *encoding = z_query_encoding(query); (void)(payload); - (void)(encoding); z_query_reply_options_t _ret_qreply_opt; z_query_reply_options_default(&_ret_qreply_opt); @@ -319,9 +317,6 @@ int main(int argc, char **argv) { z_put_options_t _ret_put_opt; z_put_options_default(&_ret_put_opt); _ret_put_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_owned_encoding_t _ret_encoding; - zp_encoding_make(&_ret_encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); - _ret_put_opt.encoding = z_move(_ret_encoding); // Create payload z_owned_bytes_t payload; From ff15e508b9ae7167e39ee0365aca09e9908cf286 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:53:05 +0200 Subject: [PATCH 17/21] doc: update api doc --- docs/api.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index b0a076abf..ccc935112 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -25,7 +25,6 @@ Enums .. autocenum:: constants.h::z_whatami_t .. autocenum:: constants.h::zp_keyexpr_canon_status_t .. autocenum:: constants.h::z_sample_kind_t -.. autocenum:: constants.h::z_encoding_id_t .. autocenum:: constants.h::z_consolidation_mode_t .. autocenum:: constants.h::z_reliability_t .. autocenum:: constants.h::z_reply_tag_t @@ -282,7 +281,6 @@ Primitives .. autocfunction:: primitives.h::z_scouting_config_from .. autocfunction:: primitives.h::zp_scouting_config_get .. autocfunction:: primitives.h::zp_scouting_config_insert -.. autocfunction:: primitives.h::zp_encoding_make .. autocfunction:: primitives.h::zp_encoding_default .. autocfunction:: primitives.h::z_encoding_check .. autocfunction:: primitives.h::z_encoding_drop @@ -290,6 +288,8 @@ Primitives .. autocfunction:: primitives.h::z_encoding_loan_mut .. autocfunction:: primitives.h::z_encoding_move .. autocfunction:: primitives.h::z_encoding_null +.. autocfunction:: primitives.h::z_encoding_from_str +.. autocfunction:: primitives.h::z_encoding_to_string .. autocfunction:: primitives.h::z_reply_err_payload .. autocfunction:: primitives.h::z_reply_err_encoding .. autocfunction:: primitives.h::z_slice_data From 5c8c614aaa2309ec8e9014efe7c830c1361ce772 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:56:19 +0200 Subject: [PATCH 18/21] test: update attachment test --- tests/attachment.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/attachment.py b/tests/attachment.py index b4f477541..8624939ce 100644 --- a/tests/attachment.py +++ b/tests/attachment.py @@ -30,22 +30,27 @@ def pub_and_sub(): Declaring Subscriber on 'demo/example/**'... Press CTRL-C to quit... >> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!') + with encoding: text/plain;utf8 with attachment: 0: source, C 1: index, 0 >> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!') + with encoding: text/plain;utf8 with attachment: 0: source, C 1: index, 1 >> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!') + with encoding: text/plain;utf8 with attachment: 0: source, C 1: index, 2 >> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!') + with encoding: text/plain;utf8 with attachment: 0: source, C 1: index, 3 >> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!') + with encoding: text/plain;utf8 with attachment: 0: source, C 1: index, 4''' From b9a92388e92360178c1327ad46029e3552488990 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 26 Jun 2024 16:59:15 +0200 Subject: [PATCH 19/21] doc: update doc api --- docs/api.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index ccc935112..10cce4f84 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -281,7 +281,6 @@ Primitives .. autocfunction:: primitives.h::z_scouting_config_from .. autocfunction:: primitives.h::zp_scouting_config_get .. autocfunction:: primitives.h::zp_scouting_config_insert -.. autocfunction:: primitives.h::zp_encoding_default .. autocfunction:: primitives.h::z_encoding_check .. autocfunction:: primitives.h::z_encoding_drop .. autocfunction:: primitives.h::z_encoding_loan From 862ed802d507d4973a17f1ab8b4fd80af6f85967 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 27 Jun 2024 13:35:39 +0200 Subject: [PATCH 20/21] fix: function name typo --- src/api/api.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index f670325ba..049f1d038 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -319,7 +319,7 @@ static int8_t _z_encoding_convert_from_string(z_owned_encoding_t *encoding, cons return _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); } -static int8_t _z_encoding_convert_into_int(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { +static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { const char *prefix = NULL; size_t prefix_len = 0; // Convert id @@ -347,7 +347,7 @@ static int8_t _z_encoding_convert_from_string(z_owned_encoding_t *encoding, cons return _z_encoding_make(encoding->_val, _Z_ENCODING_ID_DEFAULT, s); } -static int8_t _z_encoding_convert_into_int(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { +static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { _z_string_copy(s->_val, &encoding->schema); return _Z_RES_OK; } @@ -410,7 +410,7 @@ int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_ return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert encoding to string - _z_encoding_convert_into_int(encoding, s); + _z_encoding_convert_into_string(encoding, s); return _Z_RES_OK; } From c37832201877a4d104e4021a70c90077195d0dfe Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 27 Jun 2024 14:46:30 +0200 Subject: [PATCH 21/21] fix: segfault with default encoding --- examples/unix/c11/z_sub_attachment.c | 5 +---- src/api/api.c | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index 8ab1d15c0..92854d2c6 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -76,10 +76,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { z_encoding_to_string(z_sample_encoding(sample), &encoding); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - // Check encoding - if (z_check(encoding)) { - printf(" with encoding: %s\n", z_string_data(z_loan(encoding))); - } + printf(" with encoding: %s\n", z_string_data(z_loan(encoding))); // Check attachment kv_pairs_t kvp = {.current_idx = 0, .len = KVP_LEN, .data = (kv_pair_t *)malloc(KVP_LEN * sizeof(kv_pair_t))}; parse_attachment(&kvp, z_sample_attachment(sample)); diff --git a/src/api/api.c b/src/api/api.c index 049f1d038..91f6a6f39 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -327,16 +327,26 @@ static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encodin prefix = ENCODING_VALUES_ID_TO_STR[encoding->id]; prefix_len = strlen(prefix); } - // Allocate string (include separator and null terminator) - char *value = (char *)z_malloc(sizeof(char) * (prefix_len + encoding->schema.len + 2)); + _Bool has_schema = encoding->schema.len > 0; + // Size include null terminator + size_t total_len = prefix_len + encoding->schema.len + 1; + // Check for schema separator + if (has_schema) { + total_len += 1; + } + // Allocate string + char *value = (char *)z_malloc(sizeof(char) * total_len); if (value == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // Copy data + // Copy prefix char sep = ENCODING_SCHEMA_SEPARATOR; - (void)strcpy(value, prefix); - (void)strncat(value, &sep, 1); - (void)strcat(value, encoding->schema.val); + (void)strncpy(value, prefix, prefix_len); + // Copy schema and separator + if (has_schema) { + (void)strncat(value, &sep, 1); + (void)strncat(value, encoding->schema.val, encoding->schema.len); + } // Fill container *s->_val = _z_string_wrap(value); return _Z_RES_OK; @@ -400,10 +410,6 @@ int8_t z_encoding_from_str(z_owned_encoding_t *encoding, const char *s) { int8_t z_encoding_to_string(const z_loaned_encoding_t *encoding, z_owned_string_t *s) { // Init owned string z_string_null(s); - // Check encoding - if (!_z_encoding_check(encoding)) { - return _Z_RES_OK; - } // Allocate owned string s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); if (s->_val == NULL) {