From 04fc0b35a75fd2b3124ce178d8edcb8872f189ab Mon Sep 17 00:00:00 2001 From: Lucio Torre Date: Tue, 15 Sep 2015 00:31:58 -0300 Subject: [PATCH] cbor: introduce cbor_serialize_byte_stringl --- sys/cbor/cbor.c | 10 ++++++++-- sys/include/cbor.h | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sys/cbor/cbor.c b/sys/cbor/cbor.c index ff264bcdac..1665d79bee 100644 --- a/sys/cbor/cbor.c +++ b/sys/cbor/cbor.c @@ -404,8 +404,9 @@ static size_t decode_bytes(const cbor_stream_t *s, size_t offset, char *out, siz } /* A zero copy version of decode_bytes. - Will not null termiante input, but tell you the size of what you read. - Great for reading byte strings which could contain nulls inside + Will not null termiante input, but will tell you the size of what you read. + Great for reading byte strings which could contain nulls inside of unknown size + without forced copies. */ static size_t decode_bytes_no_copy(const cbor_stream_t *s, size_t offset, unsigned char **out, size_t *length) { @@ -640,6 +641,11 @@ size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val) return encode_bytes(CBOR_BYTES, stream, val, strlen(val)); } +size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length) +{ + return encode_bytes(CBOR_BYTES, stream, val, length); +} + size_t cbor_deserialize_unicode_string(const cbor_stream_t *stream, size_t offset, char *val, size_t length) { diff --git a/sys/include/cbor.h b/sys/include/cbor.h index 31400ce83e..f628ac180b 100644 --- a/sys/include/cbor.h +++ b/sys/include/cbor.h @@ -365,6 +365,16 @@ size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset, */ size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val); +/** + * @brief Serializes an arbitrary byte string + * + * @param[out] stream The destination stream for serializing the byte stream + * @param[in] val The arbitrary byte string which may include null bytes + * @param[in] length The size of the byte string in bytes + * + * @return Number of bytes written to stream @p stream + */ +size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length); /** * @brief Deserialize bytes from @p stream to @p val