laurencelundblade / QCBOR

Comprehensive, powerful, commercial-quality CBOR encoder/ decoder that is still suited for small devices.
Other
187 stars 47 forks source link

How to decode null terminated string? #151

Closed bjh0 closed 2 years ago

bjh0 commented 2 years ago

I am new to CBOR and also the QCBOR library, so please excuse my ignorance in advance.

I can see that QCBOR supports the encode/decode of text by the use of a UsefulBufC struct:

static void QCBOREncode_AddText(QCBOREncodeContext* pCtx, UsefulBufC Text);

I can also see that QCBOR appears to support the encode of basic null terminated data:

static void QCBOREncode_AddSZString(QCBOREncodeContext* pCtx, const char* szString);

However, I don't see an associated decode function for QCBOREncode_AddSZString. Is this a missing feature?

laurencelundblade commented 2 years ago

There is nothing to decode to a null-terminated string because that would require a memory allocator to allocate a buffer one byte longer than the string to make room for the \0.

For the most part QCBOR works without a memory allocator since it is focused on embedded use cases and it returns strings as a pointer & length into the original encoded CBOR.

You could write a little wrapper yourself that uses malloc().

(When QCBOR handles indefinite-length string chunks it does make use of an allocator, QCBORStringAllocate, that can be configured, but is not set up by default. That allocator could be used to return null terminated strings as a special mode. That would be a small-medium scale enhancement project for QCBOR.)

bjh0 commented 2 years ago

Understood - thank you for your detailed response @laurencelundblade